日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

gdb调试若干问题

發(fā)布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gdb调试若干问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.若干命令速查

  • file <文件名>:加載被調(diào)試的可執(zhí)行程序文件。因?yàn)橐话愣荚诒徽{(diào)試程序所在目錄下執(zhí)行GDB,因而文本名不需要帶路徑。示例:(gdb) file gdb-sample

  • r:Run的簡寫,運(yùn)行被調(diào)試的程序,也是重啟程序運(yùn)行。如果此前沒有下過斷點(diǎn),則執(zhí)行完整個程序;如果有斷點(diǎn),則程序暫停在第一個可用斷點(diǎn)處。

  • c:Continue的簡寫,繼續(xù)執(zhí)行被調(diào)試程序,直至下一個斷點(diǎn)或程序結(jié)束。

  • b <行號>:? ?b <函數(shù)名稱>/? ?b *<函數(shù)名稱>/b *<代碼地址>,b: Breakpoint的簡寫,設(shè)置斷點(diǎn)。兩可以使用“行號”“函數(shù)名稱”“執(zhí)行地址”等方式指定斷點(diǎn)位置。其中在函數(shù)名稱前面加“*”符號表示將斷點(diǎn)設(shè)置在“由編譯器生成的prolog代碼處”。如果不了解匯編,可以不予理會此用法。

  • set args [parameter]:db調(diào)試的時候,設(shè)置命令行參數(shù)。

  • bt:bt可以去查看堆棧信息。down和up和一個一個打印堆棧信息。bt直接打印全部堆棧信息。

  • n:單步調(diào)試.執(zhí)行完后顯示的是下一條命令。

  • s單步進(jìn)入

  • finish:如果想讓程序執(zhí)行到當(dāng)前函數(shù)返回之后停止,用finish,當(dāng)前函數(shù)的剩余語句將會正常運(yùn)行。

  • 保存斷點(diǎn):save breakpoint fig8.3.bp;輸入斷點(diǎn):gdb fig8.3 -x fig8.3.bp(-x參數(shù))
  • gcc 編譯有源碼情況,調(diào)試帶源碼。gcc x.c -o x -g
  • nm可以查看程序函數(shù)固定偏移和符號表情況
  • gdb attach pid:attach已經(jīng)運(yùn)行的進(jìn)程。i locals 查看變量值。
  • readelf -a xx;查看xx內(nèi)存數(shù)據(jù)分布情況
  • 內(nèi)存映射: i proc m (info proc mappings 的簡寫)核查零是不是有效地址
  • gdb高級用法:http://blog.jobbole.com/107759/
  • gdb執(zhí)行文件中的命令(批處理):source -v file.txt
  • 查看pe映射:cat /proc/28124/maps。相當(dāng)于vmmap更全。
  • 解決gdb調(diào)試分頁問題:type return to continue,or q <return> to quit:set pagination off

2.查看程序安全特性:

  • peda命令:checksec,可以看到NX是否啟用。
  • linux系統(tǒng)查看是否開啟aslr:cat /proc/sys/kernel/randomize_va_space?2
  • 查看程序加載機(jī)制,或者分布圖,三種方法:
    • info sharedlibrary
    • peda:vmmap
    • pmap -x pid:更全面

1.gdbserver遠(yuǎn)程調(diào)試

gdbserver 192.168.16.1:7678 ./over//若有參數(shù),加上參數(shù)。

//這是gdbserver啟動程序運(yùn)行。另外一種是attach方式:./gdbserver :12345 --attach 3334 &//&表明后臺運(yùn)行
gdb ./over
target remote 192.168.16.1:7678
continue
info sharedlibrary?:查看加載的模塊

以調(diào)試lampp中apache httpd為例:

(1).gdbserver單進(jìn)程啟動httpd:gdbserver :12345 ./httpd -X

(2).gdb掛上:gdb ./httpd

(gdb) target remote 127.0.0.1:12345
Remote debugging using 127.0.0.1:12345
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading symbols from target:/lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/.build-id/e7/d1cfa2253c89a2f4ad9588f8db4f1c1fc58467.debug...done.
done.//這里可以看到是有符號文件的。
0x00007ffff7dd9cc0 in _start () from target:/lib64/ld-linux-x86-64.so.2

(gdb)?b ap_process_request

(gdb) continue

Reading /opt/lampp/lib/libpcre.so.0 from remote target...//注意gdb是從遠(yuǎn)程讀取so文件
Reading /opt/lampp/lib/libaprutil-1.so.0 from remote target...

(gdb) info sharedlibrary
From To Syms Read Shared Object Library
0x00007ffff7dd9ac0 0x00007ffff7df53c0 Yes target:/lib64/ld-linux-x86-64.so.2
0x00007ffff7bbd230 0x00007ffff7bd3ec8 Yes (*) target:/opt/lampp/lib/libpcre.so.0
0x00007ffff799f030 0x00007ffff79b50c8 Yes (*) target:/opt/lampp/lib/libaprutil-1.so.0
0x00007ffff776d9f0 0x00007ffff7789568 Yes (*) target:/opt/lampp/lib/libexpat.so.1
0x00007ffff7487370 0x00007ffff749c618 Yes (*) target:/opt/lampp/lib/libiconv.so.2
0x00007ffff7262f60 0x00007ffff727ca68 Yes (*) target:/opt/lampp/lib/libapr-1.so.0
0x00007ffff7051100 0x00007ffff7053ecf Yes target:/lib/x86_64-linux-gnu/librt.so.1

2.其他命令

nm - list symbols from object files

objdump ?-tT libgd.so//查看無符號情況

3.gdb斷點(diǎn)高級技巧

轉(zhuǎn):http://blog.csdn.net/zhangmiaoping23/article/details/41009053

條件斷點(diǎn)

設(shè)置一個條件斷點(diǎn),條件由cond指定;在gdb每次執(zhí)行到此斷點(diǎn)時,cond都被計(jì)算。當(dāng)cond的值為非零時,程序在斷點(diǎn)處停止。

用法:break [break-args] if (condition)

例如:

break main if argc > 1
break 180 if (string == NULL && i < 0)
break test.c:34 if (x & y) == 1
break myfunc if i % (j+3) != 0
break 44 if strlen(mystring) == 0
b 10 if ((int)$gdb_strcmp(a,"chinaunix") == 0)
b 10 if ((int)aa.find("dd",0) == 0)

condition

可以在我們設(shè)置的條件成立時,自動停止當(dāng)前的程序,先使用break(或者watch也可以)設(shè)置斷點(diǎn),然后用condition來修改這個斷點(diǎn)的停止(就是斷)的條件。

用法:condition <break_list> (conditon)

例如:
cond 3 i == 3

condition 2 ((int)strstr($r0,".plist") != 0)

ignore

如果我們不是想根據(jù)某一條件表達(dá)式來停止,而是想斷點(diǎn)自動忽略前面多少次的停止,從某一次開始才停止,這時ignore就很有用了。

用法:ignore <break_list> count。上面的命令行表示break_list所指定的斷點(diǎn)號將被忽略count次。例如:ignore 1 100,表示忽略斷點(diǎn)1的前100次停止

為斷點(diǎn)設(shè)置命令列表

設(shè)置一個斷點(diǎn)并且在上面中斷后,我們必須會查詢一些變量或者做一些其他動作。如果這些動作可以一起呵成,豈不妙哉!使用命令列表(commands)就能實(shí)現(xiàn)這個功能。

步驟:
1.建立斷點(diǎn)。2.使用commands命令

用法:
commands <break_list>

例如:
(gdb) commands 1
Type commands for when breakpoint 1 is hit,one per line.
End with a line saying just "end".
>silent
>print "n= %d \n",n
>continue
>end

文件記錄 :斷點(diǎn)2在open函數(shù)開頭

(gdb) commands 2
Type commands for when breakpoint?2 is hit,one per line.
End with a line saying just "end".
>x/s $r0
>continue
>end

如果遇到指針情況,一定要先轉(zhuǎn)化,例如push [ebp-0x24];

則為x/s *(int *)(ebp-0x24)

---------------------------------------------------------------------------

4.?GDB寄存器和內(nèi)存?:轉(zhuǎn):http://blog.chinaunix.net/uid-22315114-id-99972.html

  • 查看寄存器

(gdb) i r
(gdb) i r a?? ?? ?? ?? ?? ?? ?? # 查看所有寄存器(包括浮點(diǎn)、多媒體)
(gdb) i r esp
(gdb) i r pc

  • 查看內(nèi)存

(gdb) x /wx 0x80040000??? # 以16進(jìn)制顯示指定地址處的數(shù)據(jù)
(gdb) x /8x $esp
(gdb) x /16x $esp+12
(gdb) x /16s 0x86468700?? # 以字符串形式顯示指定地址處的數(shù)據(jù)
(gdb) x /24i 0x8048a51????? # 以指令形式顯示指定地址處的數(shù)據(jù)(24條)

  • 修改寄存器的值

(gdb) set $v0 = 0x004000000
(gdb) set $epc = 0xbfc00000

  • 修改內(nèi)存的值

(gdb) set {unsigned int}0x8048a51=0x0
(gdb) set *(unsigned int*)0x8048a54=0x55aa55aa

  • 內(nèi)存搜索

Usage:?find <start> <end> <count> <value>
(gdb) define find ??? ??? ??? ??? ??? ??? ????
set $ptr = $arg0
set $cnt = 0
while ( ($ptr<=$arg1) && ($cnt<$arg2) )
??? if ( *(unsigned int *)$ptr == $arg3 )
??????? x /wx $ptr
??????? set $cnt = $cnt + 1
??? end
??? set $ptr = $ptr + 4
end
end

  • 斷點(diǎn)、監(jiān)測點(diǎn)

(gdb) b *0x80400000
(gdb) watch *(unsigned int *)0xbffff400==0x90909090

  • gdb調(diào)試有參數(shù)的程序:下面可以使用兩種方法輸入命令行參數(shù)

? ? ? 1)run ?命令行參數(shù)

? ? ? 2)set args ?命令行參數(shù)

  • gdb查看main函數(shù):

    (gdb) b main
    Breakpoint 1 at 0x40072a
    (gdb) x/16i main

?

fork多進(jìn)程調(diào)試一般有一下3種方法:

轉(zhuǎn):http://blog.csdn.net/fingding/article/details/46459095

1. follow-fork-mode

用法:set follow-fork-mode [parent|child]? 進(jìn)入gdb后,直接設(shè)置,默認(rèn)是parent 所以如果想要調(diào)試子進(jìn)程,進(jìn)入gdb后設(shè)置set follow-fork-mode child,然后設(shè)置子進(jìn)程的斷點(diǎn) 可用使用show?follow-fork-mode 來查詢當(dāng)前fork模式 使用follow-fork-mode,只能調(diào)試一個進(jìn)程,不能同時調(diào)試父子進(jìn)程 2.?detach-on-fork mode 用法:set detach-on-fork [on|off] on:?只調(diào)試父進(jìn)程或子進(jìn)程的其中一個(根據(jù)follow-fork-mode來決定),這是默認(rèn)的模式。 off: 父子進(jìn)程都在gdb的控制之下,其中一個進(jìn)程正常調(diào)試(根據(jù)follow-fork-mode來決定),另一個進(jìn)程會被設(shè)置為暫停狀態(tài)。 如果設(shè)置了set detach-on-fork off且follow-fork-mode為parent,fork后子進(jìn)程并不運(yùn)行,而是處于暫停狀態(tài)。 注意,最好使用GDB 6.6或以上版本,如果你使用的是GDB6.4,就只有follow-fork-mode模式。 3. attach后多進(jìn)程調(diào)試 上述方法還是不能同時調(diào)試兩個進(jìn)程,假設(shè)調(diào)試主進(jìn)程,fork后,子進(jìn)程已經(jīng)不知道運(yùn)行到何處了,所以這就需要讓子進(jìn)程先暫停一下。暫停的方法有很多 1> 最簡單的就是sleep一下 else if(fpid == 0) ?{// child process sleep(10); // 給你足夠的時間去find子進(jìn)程id(pstree -ap | ?grep a.out),然后attach? } 2> 加一段等待代碼
  • void?debug_wait(char?*tag)??
  • {??
  • ????while(1)??
  • ????{??
  • ????????if?(tag存在)?<span?style="color:#ff0000;">//?tag可以是一個環(huán)境變量,也可以是一個文件等</span>??
  • ????????????睡眠一段時間;??
  • ????????else??
  • ????????????break;??
  • ????}??
  • }?
  • peda

    PEDA - Python Exploit Development Assistance for GDB

    Key Features:

    • Enhance the display of gdb: colorize and display disassembly codes, registers, memory information during debugging.
    • Add commands to support debugging and exploit development (for a full list of commands use?peda help):
      • aslr?-- Show/set ASLR setting of GDB
      • checksec?-- Check for various security options of binary
      • dumpargs?-- Display arguments passed to a function when stopped at a call instruction
      • dumprop?-- Dump all ROP gadgets in specific memory range
      • elfheader?-- Get headers information from debugged ELF file
      • elfsymbol?-- Get non-debugging symbol information from an ELF file
      • lookup?-- Search for all addresses/references to addresses which belong to a memory range
      • patch?-- Patch memory start at an address with string/hexstring/int
      • pattern?-- Generate, search, or write a cyclic pattern to memory
      • procinfo?-- Display various info from /proc/pid/
      • pshow?-- Show various PEDA options and other settings
      • pset?-- Set various PEDA options and other settings
      • readelf?-- Get headers information from an ELF file
      • ropgadget?-- Get common ROP gadgets of binary or library
      • ropsearch?-- Search for ROP gadgets in memory
      • searchmem|find?-- Search for a pattern in memory; support regex search
      • shellcode?-- Generate or download common shellcodes.
      • skeleton?-- Generate python exploit code template
      • vmmap?-- Get virtual mapping address ranges of section(s) in debugged process
      • xormem?-- XOR a memory region with a key

    Installation

    git clone https://github.com/longld/peda.git ~/peda echo "source ~/peda/peda.py" >> ~/.gdbinit echo "DONE! debug your program with gdb and enjoy"

    Screenshot

    轉(zhuǎn)載于:https://www.cnblogs.com/studyskill/p/6609530.html

    總結(jié)

    以上是生活随笔為你收集整理的gdb调试若干问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。