在linnux下编程,gdb是一个非常重要的**【才不会告诉你是调试利器】
在使用gdb调试前,所调试的文件必须加上-g参数生成调试信息
gcc -g helloworld.c -o hello #生成可调试的信息以及可执行文件
调试:
下面我们来介绍下gdb的使用方法:
退出 gdb:
(gdb) quit |(gdb) q
进入gdb:
$ gdb hello
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb) list #显示源码
1 #include
2
3 void main(){
4 printf("Hello,World\n\n\n");
5 }
错误指示: 没有符号表被读取。请使用 "file" 命令。
!!!如果出现以上文字,那么请注意在编译文件时是否使用了-g参数!!!
是不是发现list这样按顺序列出程序源代码是不是有点不太方便?设计师当然 也想到了这个问题,于是乎设计出了搜索特定内容的命令
$ (gdb) search printf
4 printf("Hello,world!"); #search只会显示一个符合条件的行,再次回车找到匹配的下一行代码,并且值得注意的是,search只能向前(朝文件尾)搜索
若是向向后(朝文件头)搜索,可以使用reverse-search
#search和reverse-search支持使用正则达式进行搜索。