当前位置: 技术问答>linux和unix
关于gdb查看结构体出现的问题
来源: 互联网 发布时间:2016-03-23
本文导语: #include 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 = %dn", ((struct NewStruct *)tmp_p)-...
#include
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 = %dn", ((struct NewStruct *)tmp_p)->a);
return 1;
}
运行到 printf("a = %dn", ((struct NewStruct *)tmp_p)->a); 这一行时
用 p ((struct NewStruct *)tmp_p)->a
出来的错误是
no struct type named NewStruct
请注意程序是运行成功的,只是在gdb进行调试的时候看不到具体的值。
如果是以字节一个一个来看的话是可以看到,
例如
p *tmp
p *(tmp + 1)
这样两个值都能看到而且还可以更改,
但是如果结构体里面是bit的话,就没办法了
有没有高手帮忙啊
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 = %dn", ((struct NewStruct *)tmp_p)->a);
return 1;
}
运行到 printf("a = %dn", ((struct NewStruct *)tmp_p)->a); 这一行时
用 p ((struct NewStruct *)tmp_p)->a
出来的错误是
no struct type named NewStruct
请注意程序是运行成功的,只是在gdb进行调试的时候看不到具体的值。
如果是以字节一个一个来看的话是可以看到,
例如
p *tmp
p *(tmp + 1)
这样两个值都能看到而且还可以更改,
但是如果结构体里面是bit的话,就没办法了
有没有高手帮忙啊
|
增加编译选项-fno-eliminate-unused-debug-types
|
两种奇怪的现象:搂主的代码问题如下:
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 = %dn", ((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 = %dn", ((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 = %dn", ((struct NewStruct *)tmp_p)->a);
(gdb) p ((struct NewStruct *)tmp_p)->a
$1 = 1 '01'
(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".
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 = %dn", ((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 = %dn", ((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 = %dn", ((struct NewStruct *)tmp_p)->a);
(gdb) p ((struct NewStruct *)tmp_p)->a
$1 = 1 '01'
(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".