使用getopt与gdb [英] using getopt with gdb

查看:191
本文介绍了使用getopt与gdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚将 getopt 加入我的 main() func



getopt 为每个电话设置全局变量 optarg


$ b $在 getopt()之后,通过 main() gdb code> call optarg 始终是 NULL (例如(gdb)p optarg printf(%s\\\
,optarg)
按预期输出cmd行参数



发生了什么?为什么这两个不一样?



这是gdb的一个功能,它是如何检查全局变量还是其他事情?

解决方案

可能相关: Bug 13800 - gdb不打印与getopt相关的值的正确值



另请注意:

 (gdb)n 
opt:111,arg,
0x804a040
0x804a034
0x804a020
0x804a030

(gdb)printf%p\\\
%p\\\
%p\\\
%p\\\
,& optarg& opterr& optind& optopt
0x2ae760
0x2ab0f4
0x2ab0f8
0x2ab0f0

其中:

 (gdb)l 
6 int main(int argc,char * argv [])
7 {
8 int c;
9 while((c = getopt(argc,argv,:abf:o:))!= -1){
10 printf(opt:%d,%s,\\\

11%p \\\
%p \\\
%p \\\
%p \\\

12 c,optarg,
13& optarg,& opterr ,& optind,& optopt);


have just incorporated getopt into my main() func

getopt sets the global variable optarg for each call

stepping through main() with gdb, after getopt() call optarg is always NULL (e.g. (gdb) p optarg) yet printf("%s\n", optarg) outputs the cmd line arg as expected

whats going on? why are the two not the same?

Is this an isue with gdb and how it trys to inspect globals or is something else going on?

解决方案

Probably related: Bug 13800 - gdb does not print right values of getopt-related values

Also notice ie:

(gdb) n
opt: 111, arg, 
0x804a040
0x804a034
0x804a020
0x804a030

(gdb) printf "%p\n%p\n%p\n%p\n", &optarg, &opterr, &optind, &optopt
0x2ae760
0x2ab0f4
0x2ab0f8
0x2ab0f0

Where:

(gdb) l
6   int main(int argc, char *argv[])
7   {
8       int c;
9       while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
10          printf("opt: %d, %s, \n"
11              "%p\n%p\n%p\n%p\n",
12              c, optarg,
13              &optarg, &opterr, &optind, &optopt);

这篇关于使用getopt与gdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆