一.调试打印C程序中的数组的方法
- (gdb) l 23
- Line number 18 out of range; array.c has 17 lines.
- 发现共才17行,于是用下面命令:
- (gdb) l 1,17
- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3 int main()
- 4 {
- 5 int i;
- 6 int len=5;
- 7 int *array;
- 8 array = (int *) malloc (len * sizeof (int));
- 9 for(i=0;i<len;i++)
- 10 {
- 11 //scanf("%d",array+i);
- 12 *(array+i) = i;
- 13 }
- 14 printf("The Result Value is :\n");
- 15 for(i=0;i<len;i++)
- 16 printf("%d\n",*(array++));
- 17 }
-
- (gdb) b 14
- Breakpoint 1 at 0x8048452: file array.c, line 14.
- (gdb) r
- Starting program: /home/admin/c++/array
-
- Breakpoint 1, main () at array.c:14
- 14 printf("The Result Value is :\n");
- (gdb) p *array@5
- $1 = {0, 1, 2, 3, 4}
二.打印C程序中的结构体的方法
- [email protected]:/home/admin/c++# cat struct.c
- #include <stdio.h>
- #include <string.h>
- typedef struct cfg
- {
- int type;
- char name[12];
- } man;
- int main()
- {
- man oneperson;
- oneperson.type = 1;
- char name[12]="jack";
- strcpy(oneperson.name,name);
- printf("jackxiang\n");
- printf("jackxiang\n");
- }
打印整个结构体:
- (gdb) p oneperson
- $1 = {type = 1, name = "jack\000\217\306\000\220\065\260"}
打结构体中的某个值:
- (gdb) p oneperson->type
- $2 = 1
- (gdb) p oneperson->name
- $3 = "jack\000\217\306\000\220\065\260"
- (gdb) p oneperson
- $1 = {type = 1, name = "jack\000\217\306\000\220\065\260"}
如果打开printf pretty这个选项,那么当GDB显示结构体时会比较漂亮。如:
- (gdb) set print pretty on
- (gdb) print oneperson
- $1 = {
- type = 1,
- name = "jack\000\217\306\000\220\065\260"
- }
- [codes=html]
-
- 三.在GDB中加入变量调试For循环中的数组的方法:
- [codes=html]
- (gdb) p oneperson.name
- $2 = "jack\000\217\306\000\220\065\260"
- (gdb) p oneperson->name
- $3 = "jack\000\217\306\000\220\065\260"
-
- (gdb) l
- 1 #include <stdio.h>
- 2 int w[4] = {12,5,8,29};
- 3 main()
- 4 {
- 5 w[2] = 88;
- 6 }
- (gdb) b 6
- Breakpoint 1 at 0x804838c: file set.c, line 6.
- (gdb) r
- Starting program: /home/admin/c++/set
-
- Breakpoint 1, main () at set.c:6
- 6 }
- (gdb) set $i = 0
- (gdb) p w[$i]
- $1 = 12
- (gdb) p w[$i++]
- $2 = 12
- (gdb) p w[$i]
- $3 = 5
- (gdb) p w[$i++]
- $4 = 5
- (gdb) p w[$i]
- $5 = 88
四.修改变量的值
- (gdb) p w[2]
- $6 = 88
- (gdb) p w[2]=99
- $7 = 99
- (gdb) p w[2]
- $8 = 99
他人实践:http://wenku.baidu.com/view/77cbd54f767f5acfa1c7cdda.html
GDB个人总结下载:
下载文件
点击这里下载文件
参看:http://wiki.ubuntu.org.cn/index.php?title=%E7%94%A8GDB%E8%B0%83%E8%AF%95%E7%A8%8B%E5%BA%8F&variant=zh-cn
本站参考:
http://justwinit.cn/post/4014/
http://justwinit.cn/post/757/
使用 GDB 调试多进程程序:
http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/
http://tieba.baidu.com/f?kz=874502809
http://blog.163.com/redhumor@126/blog/static/1955478420108192540752/