• 嵌入式开发-11 Linux下GDB调试工具


    目录

    1 GDB简介

    2 GDB基本命令

    3 GDB调试程序


    1 GDB简介

    GDB是GNU开源组织发布的一个强大的Linux下的程序调试工具。 一般来说,GDB主要帮助你完成下面四个方面的功能:

    • 1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序(按着自己的想法运行)。
    • 2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
    • 3、当程序被停住时,可以检查此时你的程序中所发生的事。
    • 4、你可以改变你的程序,将一个BUG产生的影响修正从而测试其他BUG。

    2 GDB基本命令

    1. Here are some of the most frequently needed GDB commands:
    2. break [file:]function
    3. Set a breakpoint at function (in file).断点
    4. run [arglist]
    5. Start your program (with arglist, if specified).
    6. bt Backtrace: display the program stack.显示程序堆栈
    7. print expr
    8. Display the value of an expression.打印
    9. c Continue running your program (after stopping, e.g. at a
    10. breakpoint).继续
    11. next
    12. Execute next program line (after stopping); step over any function
    13. calls in the line.下一句
    14. edit [file:]function 查看当前停止的程序行。
    15. look at the program line where it is presently stopped.
    16. list [file:]function 键入程序的文本当程序停止了的位置
    17. type the text of the program in the vicinity of where it is
    18. presently stopped.
    19. step
    20. Execute next program line (after stopping); step into any function
    21. calls in the line. 执行下一行
    22. help [name]
    23. Show information about GDB command name, or general information
    24. about using GDB.
    25. quit
    26. Exit from GDB.
    27. You can, instead, specify a process ID as a second argument or use option "-p", if you want to debug a running process:
    28. gdb program 1234
    29. gdb -p 1234

    示例 

    1. linux@linux:~/Desktop$ ls
    2. a.out gdb.c
    3. linux@linux:~/Desktop$ gcc -g gdb.c
    4. linux@linux:~/Desktop$ ./a.out
    5. 0
    6. 1
    7. 2
    8. 3
    9. 4
    10. hello world
    11. linux@linux:~/Desktop$ gdb a.out
    12. GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
    13. Copyright (C) 2014 Free Software Foundation, Inc.
    14. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    15. This is free software: you are free to change and redistribute it.
    16. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
    17. and "show warranty" for details.
    18. This GDB was configured as "i686-linux-gnu".
    19. Type "show configuration" for configuration details.
    20. For bug reporting instructions, please see:
    21. <http://www.gnu.org/software/gdb/bugs/>.
    22. Find the GDB manual and other documentation resources online at:
    23. <http://www.gnu.org/software/gdb/documentation/>.
    24. For help, type "help".
    25. Type "apropos word" to search for commands related to "word"...
    26. Reading symbols from a.out...done.
    27. (gdb) l
    28. 2
    29. 3 void print()
    30. 4 {
    31. 5 printf("hello world\n");
    32. 6 }
    33. 7 int main(int argc, const char *argv[])
    34. 8 {
    35. 9 int i;
    36. 10
    37. 11 for (i = 0; i < 5; i++)
    38. (gdb) b main
    39. Breakpoint 1 at 0x804846a: file gdb.c, line 11.
    40. (gdb) r
    41. Starting program: /home/linux/Desktop/a.out
    42. Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
    43. 11 for (i = 0; i < 5; i++)
    44. (gdb) c
    45. Continuing.
    46. 0
    47. 1
    48. 2
    49. 3
    50. 4
    51. hello world
    52. [Inferior 1 (process 5010) exited normally]
    53. (gdb) b 10
    54. Note: breakpoint 1 also set at pc 0x804846a.
    55. Breakpoint 2 at 0x804846a: file gdb.c, line 10.
    56. (gdb) r
    57. Starting program: /home/linux/Desktop/a.out
    58. Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
    59. 11 for (i = 0; i < 5; i++)
    60. (gdb) c
    61. Continuing.
    62. 0
    63. 1
    64. 2
    65. 3
    66. 4
    67. hello world
    68. [Inferior 1 (process 5113) exited normally]
    69. (gdb) r
    70. Starting program: /home/linux/Desktop/a.out
    71. Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
    72. 11 for (i = 0; i < 5; i++)
    73. (gdb) n
    74. 12 printf("%d\n",i);
    75. (gdb) n
    76. 0
    77. 11 for (i = 0; i < 5; i++)
    78. (gdb) n
    79. 12 printf("%d\n",i);
    80. (gdb) n
    81. 1
    82. 11 for (i = 0; i < 5; i++)
    83. (gdb) p &i
    84. $1 = (int *) 0xbffff0bc
    85. (gdb) p i
    86. $2 = 1
    87. (gdb) n
    88. 12 printf("%d\n",i);
    89. (gdb) p i
    90. $3 = 2
    91. (gdb) n
    92. 2
    93. 11 for (i = 0; i < 5; i++)
    94. (gdb) n
    95. 12 printf("%d\n",i);
    96. (gdb) n
    97. 3
    98. 11 for (i = 0; i < 5; i++)
    99. (gdb) n
    100. 12 printf("%d\n",i);
    101. (gdb) p i
    102. $4 = 4
    103. (gdb) n
    104. 4
    105. 11 for (i = 0; i < 5; i++)
    106. (gdb) n
    107. 14 print();
    108. (gdb) s
    109. print () at gdb.c:5
    110. 5 printf("hello world\n");
    111. (gdb) n
    112. hello world
    113. 6 }
    114. (gdb) n
    115. main (argc=1, argv=0xbffff164) at gdb.c:15
    116. 15 return 0;
    117. (gdb)

    3 GDB调试程序

    示例:定位错误

    代码

    1. #include
    2. #ifndef _CORE_
    3. void print()
    4. {
    5. printf("hello world\n");
    6. }
    7. int main(int argc, const char *argv[])
    8. {
    9. int i;
    10. for (i = 0; i < 5; i++)
    11. printf("%d\n",i);
    12. print();
    13. return 0;
    14. }
    15. #else
    16. int main(int argc,const char *argv[])
    17. {
    18. int *temp = NULL;
    19. *temp = 10; //没有分配内存空间,直接会出错
    20. return 0;
    21. }
    22. #endif

    定位错误位置 

    1. linux@linux:~/Desktop$ ls
    2. a.out gdb.c
    3. linux@linux:~/Desktop$ gcc -g gdb.c -D _CORE_
    4. linux@linux:~/Desktop$ ./a.out
    5. Segmentation fault (core dumped)
    6. linux@linux:~/Desktop$ ls
    7. a.out core gdb.c
    8. linux@linux:~/Desktop$ gdb a.out core
    9. GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
    10. Copyright (C) 2014 Free Software Foundation, Inc.
    11. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    12. This is free software: you are free to change and redistribute it.
    13. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
    14. and "show warranty" for details.
    15. This GDB was configured as "i686-linux-gnu".
    16. Type "show configuration" for configuration details.
    17. For bug reporting instructions, please see:
    18. <http://www.gnu.org/software/gdb/bugs/>.
    19. Find the GDB manual and other documentation resources online at:
    20. <http://www.gnu.org/software/gdb/documentation/>.
    21. For help, type "help".
    22. Type "apropos word" to search for commands related to "word"...
    23. Reading symbols from a.out...done.
    24. [New LWP 5904]
    25. Core was generated by `./a.out'.
    26. Program terminated with signal SIGSEGV, Segmentation fault.
    27. #0 0x080483fd in main (argc=1, argv=0xbfea3544) at gdb.c:24
    28. 24 *temp = 10; //没有分配内存空间,直接会出错
    29. (gdb)

    如何调试正在运行的进程?

    源码

    1. linux@linux:~/Desktop$ cat gdb.c
    2. #include
    3. #include
    4. int main(int argc, const char *argv[])
    5. {
    6. while(1)
    7. {
    8. int i;
    9. i++;
    10. printf("%d\n",i);
    11. sleep(1);
    12. }
    13. return 0;
    14. }
    15. linux@linux:~/Desktop$ gcc -g gdb.c
    16. linux@linux:~/Desktop$ ./a.out
    17. -1217503231
    18. -1217503230
    19. -1217503229
    20. -1217503228
    21. ...

    再开一个终端

    1. linux@linux:~$ ps aux | grep a.out
    2. linux 6291 0.0 0.0 2028 280 pts/0 S+ 11:47 0:00 ./a.out
    3. linux 6293 0.0 0.0 4680 832 pts/3 S+ 11:47 0:00 grep --color=auto a.out
    4. linux@linux:~$ cd /home/linux/
    5. .bakvim/ .gconf/ .sogouinput/
    6. .cache/ .local/ Templates/
    7. .config/ .mozilla/ tftpboot/
    8. .dbus/ Music/ Videos/
    9. Desktop/ Pictures/ .vim/
    10. Documents/ .pki/ vmware-tools-distrib/
    11. Downloads/ Public/
    12. linux@linux:~$ cd /home/linux/Desktop/
    13. linux@linux:~/Desktop$ ls
    14. a.out core gdb.c
    15. linux@linux:~/Desktop$ gdb a.out -p 4849
    16. GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
    17. Copyright (C) 2014 Free Software Foundation, Inc.
    18. License GPLv3+: GNU GPL version 3 or later //gnu.org/licenses/gpl.html>
    19. This is free software: you are free to change and redistribute it.
    20. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
    21. and "show warranty" for details.
    22. This GDB was configured as "i686-linux-gnu".
    23. Type "show configuration" for configuration details.
    24. For bug reporting instructions, please see:
    25. //www.gnu.org/software/gdb/bugs/>.
    26. Find the GDB manual and other documentation resources online at:
    27. //www.gnu.org/software/gdb/documentation/>.
    28. For help, type "help".
    29. Type "apropos word" to search for commands related to "word"...
    30. Reading symbols from a.out...done.
    31. Attaching to program: /home/linux/Desktop/a.out, process 4849
    32. warning: unable to open /proc file '/proc/4849/status'
    33. warning: unable to open /proc file '/proc/4849/status'
    34. ptrace: No such process.
    35. (gdb) b main
    36. Breakpoint 1 at 0x8048456: file gdb.c, line 9.
    37. (gdb) n
    38. The program is not being run.
    39. (gdb) r
    40. Starting program: /home/linux/Desktop/a.out
    41. Breakpoint 1, main (argc=1, argv=0xbffff0f4) at gdb.c:9
    42. 9 i++;
    43. (gdb) n
    44. 10 printf("%d\n",i);
    45. (gdb) n
    46. -1208209407
    47. 11 sleep(1);
    48. (gdb) n
    49. 12 }
    50. (gdb) q
    51. A debugging session is active.
    52. Inferior 1 [process 6317] will be killed.
    53. Quit anyway? (y or n) y
    54. linux@linux:~/Desktop$

  • 相关阅读:
    Vue介绍&如何安装vue&Vue生命周期钩子&MVVM
    小学期实训-智慧电子时钟
    【中秋国庆不断更】XML在HarmonyOS中的生成,解析与转换(上)
    多线程设计模式【线程安全、 Future 设计模式、Master-Worker 设计模式 】(一)-全面详解(学习总结---从入门到深化)
    微服务从代码到k8s部署应有尽有系列(二、网关)
    Verilog:【6】PWM调制器(pwm_modulator.sv)
    C++指针危险的原因
    基于连续小波变换的厄尔尼诺海平面周期变化数据集分析
    PTA 6-1 删除字符串中所有*
    Java基于SpringBoot+Vue的图书管理系统
  • 原文地址:https://blog.csdn.net/m0_60718520/article/details/132748005