gpt4 book ai didi

c - 为什么 gdb 得到错误的 "optind"变量值?

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:54 25 4
gpt4 key购买 nike

我正在编写一个使用 getopt 函数的简单程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{

char *fname;
int c;

printf("Before getopt: optind is %d, address is %p \n", optind, &optind);

while ((c = getopt(argc, argv, "f:")) != -1)
switch (c) {
case 'f':
fname = optarg;
break;
}

printf("After getopt: optind is %d, address is %p \n", optind, &optind);
return 0;
}

执行它,输出:

# ./test -f 1
Before getopt: optind is 1, address is 0x601040
After getopt: optind is 3, address is 0x601040

但是在使用gdb调试时,出现了一些奇怪的事情:

13              printf("Before getopt: optind is %d, address is %p \n", optind, &optind);
(gdb) n
Before getopt: optind is 1, address is 0x601040
15 while ((c = getopt(argc, argv, "f:")) != -1)
(gdb) p &optind
$1 = (int *) 0x7ffff7dd42a0 <optind>
(gdb) n
16 switch (c) {
(gdb) n
18 fname = optarg;
(gdb)
19 break;
(gdb) p &optind
$2 = (int *) 0x7ffff7dd42a0 <optind>
(gdb) p optind
$3 = 1

我可以看到使用p optind命令,它输出1(应该是3),这个变量地址是0x7ffff7dd42a0 ,而不是 0x601040

使用readelf命令:

# readelf -a test | grep optind
000000601040 000600000005 R_X86_64_COPY 0000000000601040 optind + 0
6: 0000000000601040 4 OBJECT GLOBAL DEFAULT 25 optind@GLIBC_2.2.5 (2)
54: 0000000000601040 4 OBJECT GLOBAL DEFAULT 25 optind@@GLIBC_2.2.5

它还显示一个optind,它的地址应该是0x601040。那么在使用gdb时,为什么会从0x7ffff7dd42a0获取optind呢?这是什么?

更新:
使用最新的gdb 7.11,发现这个问题已经修复。

最佳答案

这是一个 obscure gdb bug ,由另一个称为复制重定位的模糊功能触发。

关于c - 为什么 gdb 得到错误的 "optind"变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com