关于gdb查看结构体出现的问题

love521bean 2008-06-05 08:10:48
#include <stdio.h>

struct NewStruct
{
unsigned char a;
unsigned char b;
};

int main()
{
char tmp[2];
char *tmp_p = tmp;
((struct NewStruct *)tmp_p)->a = 1;
printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);

return 1;
}

运行到 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a); 这一行时
用 p ((struct NewStruct *)tmp_p)->a
出来的错误是
no struct type named NewStruct

请注意程序是运行成功的,只是在gdb进行调试的时候看不到具体的值。
如果是以字节一个一个来看的话是可以看到,

例如

p *tmp
p *(tmp + 1)

这样两个值都能看到而且还可以更改,
但是如果结构体里面是bit的话,就没办法了

有没有高手帮忙啊

...全文
1551 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
love521bean 2008-06-06
  • 打赏
  • 举报
回复
酷,怎么查得到阿???
我看了很多gdb文档都是很肤浅,不会提到这个的阿
fierygnu 2008-06-06
  • 打赏
  • 举报
回复
这个选项的名字自解释了:不要忽略未用到的类型。
love521bean 2008-06-06
  • 打赏
  • 举报
回复
搞定了

0_0!!!!

高人阿。。。能不能解释一下上面的那些参数都是什么意思。。

为什么会这样啊???
对了,忘了感谢了。。。

呵呵,非常感谢。。。JTO

fierygnu 2008-06-06
  • 打赏
  • 举报
回复
增加编译选项-fno-eliminate-unused-debug-types
love521bean 2008-06-06
  • 打赏
  • 举报
回复
如何编译的?这和编译的参数也有关系?

只是简单的cc -g main.c

编译通过,运行通过。。
就是用gdb看变量的值得时候出现上面说的问题。。
fierygnu 2008-06-06
  • 打赏
  • 举报
回复
说一下是如何编译的。
love521bean 2008-06-06
  • 打赏
  • 举报
回复
哦,我的是6.5难道是版本问题???
love521bean 2008-06-06
  • 打赏
  • 举报
回复
谢谢啦!!!
fierygnu 2008-06-06
  • 打赏
  • 举报
回复
需要看gcc的文档。
blackbillow 2008-06-05
  • 打赏
  • 举报
回复
$ gdb -v
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
love521bean 2008-06-05
  • 打赏
  • 举报
回复
这个我也注意到了,可能是函数里面提到过这个结构体,所以再用结构体强制转换的时候就没有问题。。

有没有办法不改变代码,让这个强制转换有效呢?

3楼的兄弟,能不能告诉我你的gdb版本啊。。。

gdb难道对于这种强制转换没有办法???
noliper 2008-06-05
  • 打赏
  • 举报
回复
两种奇怪的现象:搂主的代码问题如下:

3 struct NewStruct
4 {
5 unsigned char a;
6 unsigned char b;
7 };
8
9 int main()
10 {
11 char tmp[2];
12 char *tmp_p = tmp;
(gdb)
13 //struct NewStruct * p = tmp;
14 ((struct NewStruct *)tmp_p)->a = 1;
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
16
17 return 1;
18 }
(gdb) b 15
Breakpoint 1 at 0x80483e1: file demo.c, line 15.
(gdb) p ((struct NewStruct *)tmp_p)->a
No struct type named NewStruct.

(gdb)

注意:解开第十三行,重新编译:
3 struct NewStruct
4 {
5 unsigned char a;
6 unsigned char b;
7 };
8
9 int main()
10 {
11 char tmp[2];
12 char *tmp_p = tmp;
(gdb) list
13 struct NewStruct * p = tmp;
14 ((struct NewStruct *)tmp_p)->a = 1;
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
16
17 return 1;
18 }
(gdb) b 15
Breakpoint 1 at 0x80483e7: file demo.c, line 15.
(gdb) r
Starting program: /root/demo
Missing separate debuginfo for /lib/ld-linux.so.2
Try: yum --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/ac/2eeb206486bb7315d6ac4cd64de0cb50838ff6.debug
Missing separate debuginfo for /lib/libc.so.6
Try: yum --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/ba/4ea1118691c826426e9410cafb798f25cefad5.debug

Breakpoint 1, main () at demo.c:15
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
(gdb) p ((struct NewStruct *)tmp_p)->a
$1 = 1 '\001'
(gdb)

两次结果不一样,应该是gdb对于强制类型转换问题的处理方式不同而已。gdb的版本:
gdb -v
GNU gdb Red Hat Linux (6.6-43.fc8rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
noliper 2008-06-05
  • 打赏
  • 举报
回复
两种奇怪的现象:搂主的代码问题如下:

3 struct NewStruct
4 {
5 unsigned char a;
6 unsigned char b;
7 };
8
9 int main()
10 {
11 char tmp[2];
12 char *tmp_p = tmp;
(gdb)
13 //struct NewStruct * p = tmp;
14 ((struct NewStruct *)tmp_p)->a = 1;
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
16
17 return 1;
18 }
(gdb) b 15
Breakpoint 1 at 0x80483e1: file demo.c, line 15.
(gdb) p ((struct NewStruct *)tmp_p)->a
No struct type named NewStruct.
(gdb)

注意:解开第十三行,重新编译:
3 struct NewStruct
4 {
5 unsigned char a;
6 unsigned char b;
7 };
8
9 int main()
10 {
11 char tmp[2];
12 char *tmp_p = tmp;
(gdb) list
13 struct NewStruct * p = tmp;
14 ((struct NewStruct *)tmp_p)->a = 1;
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
16
17 return 1;
18 }
(gdb) b 15
Breakpoint 1 at 0x80483e7: file demo.c, line 15.
(gdb) r
Starting program: /root/demo
Missing separate debuginfo for /lib/ld-linux.so.2
Try: yum --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/ac/2eeb206486bb7315d6ac4cd64de0cb50838ff6.debug
Missing separate debuginfo for /lib/libc.so.6
Try: yum --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/ba/4ea1118691c826426e9410cafb798f25cefad5.debug

Breakpoint 1, main () at demo.c:15
15 printf("a = %d\n", ((struct NewStruct *)tmp_p)->a);
(gdb) p ((struct NewStruct *)tmp_p)->a
$1 = 1 '\001'
(gdb)

两次结果不一样,应该是gdb对于强制类型转换问题的处理方式不同而已。gdb的版本:
gdb -v
GNU gdb Red Hat Linux (6.6-43.fc8rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".

blackbillow 2008-06-05
  • 打赏
  • 举报
回复
(gdb) p ((struct NewStruct *)tmp_p)->a
$4 = 1 '\001'

建议升级gdb
noliper 2008-06-05
  • 打赏
  • 举报
回复
不方便进入linux,不过用dev-cpp调试,是正常的。没有出现过你说的情况
cceczjxy 2008-06-05
  • 打赏
  • 举报
回复
这样的写法是对tmp_p进行类强制类型转换((struct NewStruct *)tmp_p)->a。
应该是在gdb内不能进行强制类型转换。

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧