GDB调试工具的使用方法.ppt_第1页
GDB调试工具的使用方法.ppt_第2页
GDB调试工具的使用方法.ppt_第3页
GDB调试工具的使用方法.ppt_第4页
GDB调试工具的使用方法.ppt_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、2020/7/22,第1页,GDB调试工具的使用方法 周鸣,2020/7/22,第2页,GDB的介绍 GDB的安装和使用 GDB的使用帮助说明 GDB的基本使用 GDB使用技巧,基本内容,2020/7/22,第3页,GDB的介绍,调试器(如GDB)的用途是在程序运行时让你看到程序“内部”发生了什么或者程序无故退出时其他程序做了什么。 一般来说,GDB主要帮忙你完成下面四个方面的功能: 1、启动你的程式,能按照你的自定义的需求随心所欲的运行程式。2、可让被调试的程式在你所指定的调置的断点处停住。(断点能是条件表达式)3、当程式被停住时,能检查此时你的程式中所发生的事。4、动态的改动你程式的执行环

2、境。,2020/7/22,第4页,GDB的安装和使用,GDB可以运行在不同的操作系统上,下面以Linux操作系统为例来说明一下安装方法: 1、首先要安装gcc。 2、在安装gdb的rpm包。 3、设置gdb的path,一般gdb安装后会在/usr/bin目录下,只要指定PATH=/usr/bin即可。 4、在任何路径下执行gdb就可以运行了。,2020/7/22,第5页,GDB的使用帮助说明,在gdb执行后,就可以运行命令了,输入help命令后,显示如下: List of classes of commands: aliases - Aliases of other commands brea

3、kpoints - Making program stop at certain points data - Examining data files - Specifying and examining files internals - Maintenance commands obscure - Obscure features running - Running the program stack - Examining the stack status - Status inquiries support - Support facilities tracepoints - Trac

4、ing of program execution without stopping the program user-defined - User-defined commands,2020/7/22,第6页,执行help aliases后,就会显示如下: delete breakpoints - Delete some breakpoints or auto-display expressions disable breakpoints - Disable some breakpoints ni - Step one instruction si - Step one instruction

5、 exactly stepping - Specify single-stepping behavior at a tracepoint tp - Set a tracepoint at a specified line or function or address where - Print backtrace of all stack frames ws - Specify single-stepping behavior at a tracepoint 以上是gdb一些命令的别名,GDB的使用帮助说明,2020/7/22,第7页,GDB的使用帮助说明,执行help breakpoints

6、后,显示如下: break - Set breakpoint at specified line or function catch - Set catchpoints to catch events watch - Set a watchpoint for an expression clear - Clear breakpoint at specified line or function delete - Delete some breakpoints or auto-display expressions disable - Disable some breakpoints enabl

7、e - Enable some breakpoints 以上是gdb设置断点的一些基本命令,还有一些命令没有在这里描述。,2020/7/22,第8页,执行help data后,显示如下: call - Call a function in the program print - Print value of expression EXP whatis - Print data type of expression EXP 以上是gdb 数据命令的几条常用命令,还有很多没有在此描述。,GDB的使用帮助说明,2020/7/22,第9页,GDB的使用帮助说明,执行help files命令后,显示如下

8、: add-shared-symbol-files - Load the symbols from shared objects in the dynamic linkers link map cd - Set working directory to DIR for debugger and program being debugged core-file - Use FILE as core dump for examining memory and registers directory - Add directory DIR to beginning of search path fo

9、r source files path - Add directory DIR(s) to beginning of search path for object files 以上是gdb的文件命令的一部分,还有很多没有在此描述。,2020/7/22,第10页,执行help internals命令后,显示如下: maintenance - Commands for use by GDB maintainers maintenance agent - Translate an expression into remote agent bytecode maintenance check-symt

10、abs - Check consistency of psymtabs and symtabs maintenance cplus - C+ maintenance commands 以上命令是gdb的维护命令的一部分,我们一般不会用到这些命令。,GDB的使用帮助说明,2020/7/22,第11页,GDB的使用帮助说明,执行help obscure命令后,显示如下: set annotate - Set annotation_level set backtrace limit - Set an upper bound on the number of backtrace levels set

11、backtrace past-main - Set whether backtraces should continue past main set backtrace past-zero-pc - Set whether backtraces should continue past a zero pc value set coerce-float-to-double - Set coercion of floats to doubles when calling functions 以上命令是gdb的一些设置命令,我们用户日常使用也比较少。,2020/7/22,第12页,执行help ru

12、nning 命令后,显示如下: continue - Continue program being debugged kill - Kill execution of program being debugged next - Step program nexti - Step one instruction run - Start debugged program start - Run the debugged program until the beginning of the main procedure step - Step program until it reaches a d

13、ifferent source line stepi - Step one instruction exactly thread - Use this command to switch between threads 以上是gdb的运行命令的一部分,还有一些没有在此描述。,GDB的使用帮助说明,2020/7/22,第13页,执行help stack命令后,显示入下: backtrace - Print backtrace of all stack frames bt - Print backtrace of all stack frames down - Select and print s

14、tack frame called by this one frame - Select and print a stack frame return - Make selected stack frame return to its caller select-frame - Select a stack frame without printing anything up - Select and print stack frame that called this one 以上命令是gdb的堆栈命令,一般用于调试core文件。,GDB的使用帮助说明,2020/7/22,第14页,GDB的

15、使用帮助说明,执行help status命令后,显示如下: info - Generic command for showing things about the program being debugged macro - Prefix for commands dealing with C preprocessor macros show - Generic command for showing things about the debugger 以上是gdb调试程序的一些基本命令显示和一些基本信息。,2020/7/22,第15页,GDB的基本使用,下面以举例的方式来说明一下的gdb的常

16、用命令 main.c 1 #include 2 void *helloworld(char *argc); 3 4 int main(int argc,int argv) 5 6 int error; 7 int *temptr; 8 int i = 0; 9 pthread_t thread_id; /麓忙麓垄脧脽鲁脤 ID 10 pthread_create( 19 ,2020/7/22,第16页,GDB的基本使用,Hellothread.c 1 2 3 void *helloworld(char *argc) 4 5 int i = 0; 6 while(i10000) 7 8 prin

17、tf(the message is %sn,argc); 9 printf(the main id is %dn,pthread_self(); 10 print1(); 11 i+; 12 sleep(1); 13 14 return; 15 16,2020/7/22,第17页,GDB的基本使用,17 int print1() 18 19 printf(hello world); 20 int i = 1; 21 i = i/i; 22 print2(i); 23 return 0; 24 25 26 int print2(int i) 27 28 int a5 = 1,2,3,4,5; 2

18、9 30 printf(param i = %dn, i); 31 32 printf(a0 = %dn, a0); 33 34 return 0; 35 ,2020/7/22,第18页,上面两个文件一个是主线程文件main.c,一个是子线程文件hellothread.c,该文件在maintest目录下。 编译文件: gcc -g -o main main.c maintest/hellothread.c -lpthread 一定要加上-g参数,加上了-g参数才能用gdb进行调试。 开始调试main程序了: 用gdb调试main程序有两种进入方法: 方法1:gdb ./main 方法2: 1、

19、运行gdb 2、(gdb) file main list命令使用: (gdb) list 执行这个命令后,会显示从源文件开始第一行到第十行。 显示如下:,GDB的基本使用,2020/7/22,第19页,GDB的基本使用,1 #include 2 void *helloworld(char *argc); 3 4 int main(int argc,int argv) 5 6 int error; 7 int *temptr; 8 int i = 0; 9 pthread_t thread_id; /存储线程 ID 10 pthread_create(,2020/7/22,第20页,GDB的基本

20、使用,(gdb) list 5 , 10 执行命令后显示为: 5 6 int error; 7 int *temptr; 8 int i = 0; 9 pthread_t thread_id; /存储线程 ID 10 pthread_create( 显示源文件第5行到第10行内容,2020/7/22,第21页,GDB的基本使用,设置断点: 设置默认文件断点 (gdb) break 10 Breakpoint 1 at 0 x8048483: file main.c, line 10. (gdb) break 设置指定文件断点 (gdb) break maintest/hellothread.c

21、:6 Breakpoint 1 at 0 x80484cd: file maintest/hellothread.c, line 6. 设置路径后再设置断点: (gdb) dir maintest Source directories searched: /home/mzhou/maintest:$cdir:$cwd 这是再设置断点就不用带路径了 (gdb) break hellothread.c:6,2020/7/22,第22页,GDB的基本使用,显示断点信息命令: (gdb) info break Num Type Disp Enb Address What 1 breakpoint ke

22、ep y 0 x080484cd in helloworld at maintest/hellothread.c:6 2 breakpoint keep y 0 x080484cd in helloworld at maintest/hellothread.c:6 breakpoint keep y 0 x08048483 in main at main.c:10 删除断点信息命令: (gdb) delete 1 (gdb) info break Num Type Disp Enb Address What 2 breakpoint keep y 0 x080484cd in hellowor

23、ld at maintest/hellothread.c:6 breakpoint keep y 0 x08048483 in main at main.c:10 删除所有断点: (gdb) delete 清除原文件中某一代码行上的断点命令: (gdb) clear maintest/hellothread.c:6 Deleted breakpoint 2,2020/7/22,第23页,GDB的基本使用,开始运行程序命令: start命令: (gdb) start 该命令执行后会程序会停止在最开始的地方。 run命令 (gdb) run 该命令执行后会程序会停止最开始的一个断点上。 Break

24、point 1, main (argc=1, argv=0 xbff95844) at main.c:10 pthread_create( 单步执行命令 (gdb) next 进入的单步执行 step 进入一个函数的单步执行,2020/7/22,第24页,GDB的基本使用,设置断点 (gdb) break hellothread.c:10 Breakpoint 1 at 0 x8048505: file maintest/hellothread.c, line 10. (gdb) run Breakpoint 1, helloworld (argc=0 x8048620 helloworld)

25、 at maintest/hellothread.c:10 10 print1(); (gdb)step /进入print1函数进行调试 print1 () at maintest/hellothread.c:19 19 printf(hello world);,2020/7/22,第25页,GDB的基本使用,Run命令还可以带参数,参数为执行程序的参数。 (gdb) run 3 hello gdb调试的程序可以先运行,再设置断点。 具体操作如下: Gdb ./main (gdb) run 按Ctrl c 再设置断点 最后执行(gdb)continue /继续执行程序命令,2020/7/22,

26、第26页,GDB的基本使用,线程命令: Main程序运行后有两个线程,我们可以定位到指定线程进行调试。 显示线程信息命令: (gdb) info thread 2 Thread -1208869984 (LWP 3764) 0 x003177a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 1 Thread -1208867136 (LWP 3761) 0 x003177a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 线程1有个*号表示gdb在当前线程。 切换线程命令: (gdb) thr

27、ead 2 Switching to thread 2 (Thread -1208869984 (LWP 3764)#0 0 x003177a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 这是如果再执行info thread后,会发现*号已经指定到了线程2上了。,2020/7/22,第27页,GDB的基本使用,变量的检查和赋值 命令: whatis识别数组或变量的类型 (gdb) whatis i type = int ptype:比whatis的功能更强,他可以提供一个结构的定义 print显示一个变量的值 或设置一个变量 (gdb) p

28、rint i $1 = 6 (gdb) print i = 10 $1 = 10,2020/7/22,第28页,GDB的基本使用,带条件的设置断点: (gdb) break 15 if i = 15 Breakpoint 2 at 0 x80484a8: file main.c, line 15. (gdb) run 程序会在15行,并且满足i=15停住。 设置监视点 当你调试一个很大的程序,并且在跟踪一个关键的变量时,发现这个变量不知在哪儿被改动过,如何才能找到改动它的地方。这时你可以使用watch命令。 watch expression (gdb) watch i 20 Hardware

29、watchpoint 3: i 20 (gdb) c 它会在i=21时停住,它和break的区别是只会停止一次,下一次不会再执行过来时不会停住。,2020/7/22,第29页,GDB的基本使用,Core文件调试 int print1() printf(hello world); int i = 0; i = i/i; return 0; 这个函数调用时会core掉。 gdb ./main core,2020/7/22,第30页,GDB的基本使用,(gdb) bt #0 0 x0804854c in print1 () at maintest/hellothread.c:21 #1 0 x080

30、4850a in helloworld (argc=0 x8048640 helloworld) at maintest/hellothread.c:10 #2 0 x005703cc in start_thread () from /lib/tls/libpthread.so.0 #3 0 x003f9c3e in clone () from /lib/tls/libc.so.6 执行bt命令后,打印当前的函数调用栈的所有信息,会显示最后core掉的语句上,hellothread.c:21是 “i=i/i”语句。,2020/7/22,第31页,GDB的基本使用,frame命令是用来选择函数堆

31、栈号的。 (gdb) frame 1 (gdb) frame 1 #1 0 x0804850a in helloworld (argc=0 x8048640 helloworld) at maintest/hellothread.c:10 print1(); 执行该命令后就会把函数堆栈定在第一层。 Info local命令是显示该函数堆栈的局部变量。 (gdb) info local i = 0 显示局部变量i为0,所以可以看出core的问题了。 info args命令是显示该函数的参数值。 (gdb) info args argc = 0 x8048640 helloworld 显示该函数参

32、数值为“helloworld”。,2020/7/22,第32页,GDB的基本使用,Call 函数调用命令 (gdb) break hellothread.c:22 (gdb) call print2(2) hello worldmain thread sleep 1 second param i = 2 a0 = 1 $1 = 0 Print2函数调用后,会执行函数的过程,$1是函数返回值。 Finish 命令, finish 运行程序,直到当前函数完成 (gdb) finsih,2020/7/22,第33页,GDB的基本使用,输出数组: (gdb) s main thread sleep 1

33、 second print2 (i=1) at maintest/hellothread.c:28 28 int a5 = 1,2,3,4,5; (gdb) n main thread sleep 1 second 30 printf(param i = %dn, i); (gdb) p a1 /输出数组第1号元素的值 $2 = 2 (gdb) p a /输出数组的所有元素的值 $3 = 1, 2, 3, 4, 5 (gdb) p a + 1 /输出数组第1号元素的指针 $4 = (int *) 0 xb7f4f364 (gdb) p *(a+1) /输出数组的第1号元素的值 $5 = 2,2

34、020/7/22,第34页,GDB的基本使用,进程运行后,我们可以用attach命令来调试该进程。 mzhourh-15 $ ./main 首先运行main程序 mzhourh-15 maintest$ ps -ef|grep main mzhou 5755 1893 0 16:08 pts/2 00:00:00 ./main (gdb) attach 5755 Attaching to process 5755 这是可以对main进程进行调试了,进程main被阻塞。 (gdb) break hellothread.c:30 Breakpoint 1 at 0 x8048586: file m

35、aintest/hellothread.c, line 30. 设置断点 (gdb) c Continuing. Switching to Thread -1208423520 (LWP 5756) Breakpoint 1, print2 (i=1) at maintest/hellothread.c:30 30 printf(param i = %dn, i); 进程在断点处停住,我们这是可以对该程序进行调试了,,2020/7/22,第35页,GDB的基本使用,Detach 命令 ,释放GDB对进程的控制。 (gdb) detach Detaching from program: /hom

36、e/mzhou/main, process 5755 执行该命令后,main进程就会打开阻塞正常运行了。 List 命令高级用法: (Gdb) list hellothread.c: 1,20 指定某个文件显示从第一行到20行 Quit命令 (gdb) quit 退出gdb,2020/7/22,第36页,GDB的基本使用,Shell cmd 命令 在gdb环境中,你能执行UNIX的shell的命令,使用gdb的shell命令来完成: (gdb) shell ./test5 command 命令 我们能使用GDB提供的command命令来设置停止点的运行命令。也就是说,当 运行的程式在被停止住时,我们能让其自动运行一些别的命令,这非常有利行自 动化调试。对基于GDB的自动化调试是个强大的支持。 (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论