我在 i386 中实现了一个模拟 load_arg 的函数。用VSCode调试的时候,发现在函数的倒数第三行:
*(size_t*)stack_top = argc;
这一行的最后,argc没有存入(size_t)stack_top,VSCode显示“optimized out”(如图)。
我该如何解决这个问题?或者如何将 argc 的值放入 (size_t)stack_top?
uint32_t load_arg(PD *pgdir, char *const argv[]) {
char *stack_top = (char*)vm_walk(pgdir, USR_MEM - PGSIZE, 7) + PGSIZE;
size_t argv_va[MAX_ARGS_NUM + 1];
int argc;
for (argc = 0; argv[argc]; ++argc) {
assert(argc < MAX_ARGS_NUM);
// push the string of argv[argc] to stack, record its va to argv_va[argc]
stack_top -= (strlen(argv[argc])+1);
strcpy(stack_top, argv[argc]);
argv_va[argc] = USR_MEM - PGSIZE + ADDR2OFF(stack_top);
}
argv_va[argc] = 0; // set last argv NULL
stack_top -= ADDR2OFF(stack_top) % 4; // align to 4 bytes
for (int i = argc; i >= 0; --i) {
// push the address of argv_va[argc] to stack to make argv array
stack_top -= sizeof(size_t);
*(size_t*)stack_top = argv_va[i];
}
// push the address of the argv array as argument for _start
stack_top -= sizeof(size_t);
*(size_t*)stack_top = USR_MEM - PGSIZE + ADDR2OFF(stack_top);
//push argc as argument for _start
stack_top -= sizeof(size_t);
*(size_t*)stack_top = argc;
stack_top -= sizeof(size_t); // a hole for return value (useless but necessary)
return USR_MEM - PGSIZE + ADDR2OFF(stack_top);
}