在进行函数调用时,我正在丢失(全局)变量的地址(在共享对象中定义)。为了证明它,我有意将变量num_accounts的值设置为55,当函数get_num_accounts()开始执行时,指向此变量的指针被错误地接收。我的证据是这个GDB会话:在函数调用期间获取变量的地址不正确
accounts_init() at accounts.c:31
31 err_code=read_accounts();
(gdb) print num_accounts
$1 = 0
(gdb) print &num_accounts
$2 = (account_idx_t *) 0x7ffff767c640 <num_accounts>
(gdb) set var num_accounts=55
(gdb) print num_accounts
$3 = 55
(gdb) s
read_accounts() at accounts.c:66
66 err_code=get_num_accounts(&num_accounts);
(gdb) s
get_num_accounts (num_accounts_ptr=0x604780 <num_accounts>) at accounts.c:119
119 *num_accounts_ptr=0;
(gdb) print num_accounts
$4 = 55
(gdb) print *num_accounts_ptr
$5 = 0
(gdb)
(gdb) print num_accounts_ptr
$6 = (account_idx_t *) 0x604780 <num_accounts>
(gdb)
变量NUM_ACCOUNTS的地址是0x7ffff767c640,但我得到0x604780执行该功能时,为什么这样一个奇怪的事情发生?
功能get_num_accounts()是本的源代码:
err_code_t get_num_accounts(account_idx_t *num_accounts_ptr) {
err_code_t err_code;
uint32_t file_size;
div_t div_result;
unsigned short number;
*num_accounts_ptr=0;
err_code=get_dir();
if (err_code!=ERR_NO_ERROR) return err_code;
err_code=get_file(ACCOUNTS_FILENAME,sizeof(ACCOUNTS_FILENAME),&file_size);
if (err_code!=ERR_NO_ERROR) return err_code;
div_result=div(file_size,sizeof(tbl_account_t));
if (div_result.rem!=0) {
return ERR_BAD_CONFIG_FILE_FORMAT;
}
number=div_result.quot;
*num_accounts_ptr=number;
return ERR_NO_ERROR;
}
类型account_idx_t定义为:
typedef unsigned short account_idx_t;
全局变量NUM_ACCOUNTS在accounts.c文件在限定的开始:
account_idx_t num_accounts=0;
Bas理论上,函数做的是在读取文件之前获取文件的大小并计算文件包含的记录数。 (它的数据库)
这是调用代码,这卡列斯get_num_accounts()函数:
err_code_t accounts_init(void) {
err_code_t err_code;
err_code=read_accounts();
if (err_code!=ERR_NO_ERROR) return err_code;
return ERR_NO_ERROR;
}
err_code_t read_accounts(void) {
err_code_t err_code;
int ret;
err_code=get_num_accounts(&num_accounts);
if (err_code!=ERR_NO_ERROR) return err_code;
if (num_accounts==0) return ERR_NO_ERROR;
int fd=open(filename_buf,O_RDONLY); // filename_buf is global, it holds filename from previous call
if (fd==-1) {
return ERR_SYS_ERROR;
}
ret=read(fd,accounts,sizeof(tbl_account_t)*num_accounts);
if (ret==-1) {
return ERR_SYS_ERROR;
}
ret=close(fd); // TO_DO: validate return value of close(fd)
if (ret==-1) {
return ERR_SYS_ERROR;
}
return ERR_NO_ERROR;
}
我编译具有-fPIC标志库:
[[email protected] src]$ make accounts.o
gcc -g -ffunction-sections -fdata-sections -Wall -Wextra -Wunreachable-code -Wmissing-prototypes -Wmissing-declarations -Wunused -Winline -Wstrict-prototypes -Wimplicit-function-declaration -Wformat -D_GNU_SOURCE -fshort-enums -fPIC -c accounts.c
没有另一个'num_accounts'符号在源代码中的任何地方,我再次检查:
[[email protected] src]$ nm *o|grep num_accounts
0000000000000000 T get_num_accounts
0000000000000000 B num_accounts
[[email protected] src]$
任何有关进一步调试步骤的建议?
我不擅长GDB。所以可能是一个愚蠢的问题。你正在进入'get_num_accounts'或者正在跨越。如果您在阅读'* num_accounts_ptr'时正在逐步完成,则超出范围。 – MotKohn
@MotKohn用's'命令进入它,以便跨过它是命令'n' – Nulik
对不起再次提问,但0x7ffff767c640对我来说看起来不是一个有效的地址。有点太高了,不是吗? – MotKohn