linux調試工具gdb具體步驟(GDB調試入門)

2023-08-15 09:26:57 來源:嵌入式Linux充電站

本篇講解使用GDB調試Linux應用程序,下面以hellowld.c為例,簡單介紹一下 GDB 的調試入門。

1、編寫代碼


(資料圖片僅供參考)

#includeintmain(intargc,char**argv){inti;intresult=0;if(1>=argc){printf("Helloworld.");}printf("Hello World%s!",argv[1]);for(i=1;i<=100;i++){result+=i;}printf("result=%d",result);return0;}

編譯時加上-g參數:

gcc helloworld.c-o hellowrld-g

2、啟動調試

$gdb helloWorldGNU gdb(GDB)Red Hat Enterprise Linux 8.2-12.el8Copyright(C)2018 Free Software Foundation,Inc.License GPLv3+:GNU GPL version 3 or laterThis 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"fordetails.This GDB was configured as"x86_64-redhat-linux-gnu".Type"show configuration"forconfiguration details.For bug reporting instructions,please see:.Find the GDB manual and other documentation resources online at:.Forhelp,type"help".Type"apropos word"to searchforcommands related to"word"...Reading symbols from helloworld...done.(gdb)run<-----------------------------不帶參數運行Starting program:/home/zhuzhg/helloworldMissing separate debuginfos,use:yum debuginfo-install glibc-2.28-101.el8.x86_64helloworld.result=5050[Inferior 1(process 1069013)exited normally](gdb)run China<-----------------------------帶參數運行Starting program:/home/zhuzhg/helloworld ChinaHello World China!result=5050[Inferior 1(process 1071086)exited normally](gdb)

3、斷點

設置斷點

文件行號斷點:break hellowrld.c:9

函數斷點:break main

條件斷點:break helloworld.c:17 if c == 10

臨時斷點, 假設某處的斷點只想生效一次,那么可以設置臨時斷點,這樣斷點后面就不復存在了:tbreak helleworld.c:9

禁用或啟動斷點:

disable#禁用所有斷點disable bnum#禁用標號為bnum的斷點enable#啟用所有斷點enable bnum#啟用標號為bnum的斷點enabledeletebnum#啟動標號為bnum的斷點,并且在此之后刪除該斷點

斷點清除:

clear#刪除當前行所有breakpointsclear function#刪除函數名為function處的斷點clear filename:function#刪除文件filename中函數function處的斷點clear lineNum#刪除行號為lineNum處的斷點clear flineNum#刪除文件filename中行號為lineNum處的斷點delete#刪除所有breakpoints,watchpoints和catchpointsdeletebnum#刪除斷點號為bnum的斷點

4、變量查看

b 字節

h 半字,即雙字節

w 字,即四字節

g 八字節

n 表示要顯示的內存單元數,默認值為1

f 表示要打印的格式,前面已經提到了格式控制字符

u 要打印的單元長度

addr 內存地址

變量查看:最常見的使用便是使用print(可簡寫為p)打印變量內容。

以上述程序為例:

gdb helloworldbreakhelloworld.c:17ifi==0(gdb)runStarting program:/home/book/helloworldhelloworld.Breakpoint2,main(argc=1,argv=0x7fffffffdca8)at helloworld.c:1717result+=i;(gdb)print i<------------------查看變量i當前的值$1=10(gdb)print result<------------------查看變量result當前的值$2=45(gdb)print argc<------------------查看變量argc當前的值$3=1(gdb)print str$4=0x4006c8"Hello World"<------------------查看變量str當前的值

查看內存:examine(簡寫為x)可以用來查看內存地址中的值。語法如下:

x/[n][f][u]addr

其中:

單元類型常見有如下:

示例:

(gdb)x/4bstr0x4006c8:01001000011001010110110001101100

可以看到,變量 str 的四個字節都以二進制的方式打印出來了。

查看寄存器內容:info registers

ra0x3ff7ef22820x3ff7ef2282<__libc_start_main+160>sp0x3ffffffaa00x3ffffffaa0gp0x2aaaaac8000x2aaaaac800tp0x3ff7fdd2500x3ff7fdd250t00x3ff7ed60b0274742468784t10x3ff7ef21e2274742583778t20x2aaaaac4f0183251944688fp0x3ffffffab00x3ffffffab0s10x00a00x11a10x3ffffffc28274877905960a20x3ffffffc38274877905976a30x00a40x3ffffffad8274877905624a50x00a60x3ff7fd88a8274743527592(內容過多未顯示完全)

5、單步調試

單步執行-next:

next命令(可簡寫為n)用于在程序斷住后,繼續執行下一條語句,假設已經啟動調試,并在第12行停住,如果要繼續執行,則使用n執行下一條語句,如果后面跟上數字num,則表示執行該命令num次,就達到繼續執行n行的效果了:

gdb helloworld<-------------------------------加載程序(gdb)breakhelloworld.c:18<-------------------------------設置斷點(gdb)run<-------------------------------啟動調試The program being debugged has been started already.Start it from the beginning?(yorn)yStarting program:/home/book/helloworldHelleo World.Breakpoint2,main(argc=1,argv=0x7fffffffdca8)at helloworld.c:18<--------程序在18行暫停18result+=i;Breakpoint2,main(argc=1,argv=0x7fffffffdca8)at helloworld.c:1818result+=i;(gdb)next<--------單步執行17for(i=1;i<=100;i++){Breakpoint2,main(argc=1,argv=0x7fffffffdca8)at helloworld.c:1818result+=i;(gdb)next2<--------執行兩次Breakpoint2,main(argc=1,argv=0x7fffffffdca8)at helloworld.c:1818result+=i;

單步進入-step:

如果我們想跟蹤函數內部的情況,可以使用step命令(可簡寫為s),它可以單步跟蹤到函數內部,但前提是該函數有調試信息并且有源碼信息。

斷點繼續-continue:

continue命令(可簡寫為c),它會繼續執行程序,直到再次遇到斷點處。

編輯:黃飛

標簽:

上一篇:TL494引腳圖和電路圖講解 TL494開關電源芯片的工作原理和應用電路
下一篇:最后一頁