簡體   English   中英

附加到另一個進程時如何在gdb中設置斷點

[英]How to set breakpoint in gdb when attach to another process

我有一個C程序,編寫了一個非常復雜的腳本來運行它。 我需要使用gdb調試該程序。 我試圖運行腳本並將gdb附加到它的進程中,但是后來我無法設置所需的斷點:

$ gdb median.o 27944
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from median.o...done.
Attaching to program: median.o, process 27944
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fc376e9cbfa in ?? ()
(gdb) break median.c:10
Cannot access memory at address 0x40059d

我也試過這個:

$gdb -p 28303
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 28303
Reading symbols from /bin/dash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.19.so...done.
done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fe918e50bfa in wait4 () at ../sysdeps/unix/syscall-template.S:81
81  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) break median.c:10
No source file named median.c.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (median.c:10) pending.
(gdb) continue
Continuing.
Please enter 3 numbers separated by spaces > 6 is the median
[Inferior 1 (process 28303) exited normally]

因此它繼續運行而不會在斷點處停止。 值得一提的是,如果我從gdb本身調用run median.o < input ,它就可以工作。

如何在由腳本運行的C程序上設置斷點?

這是人們使用gdb時遇到的經典問題。 它是如此普遍,以至於您會以為它有一個方便的名稱!

有一些解決問題的方法,有些經過了時間的考驗,而有些則相對更具實驗性。

  • 如果要調試的程序(在gdb術語中為“劣等”)是長時間運行的(例如,GUI或某種類型的服務器),那么最簡單的方法是僅運行腳本,等待劣等開始,然后附加到它。 您可以使用PID進行附加,既可以使用gdb -p PID進行attach PID ,也可以在gdb提示符下使用attach PID

  • 如果程序是短命的,那么另一種經典方法是在程序啟動時提早添加sleep調用。 也就是說,作為第一線main 然后,繼續執行attach計划。

這些是經典方式。 但是,現在讓我們談談更有趣的東西。

gdb具有多種劣質模式,可以同時調試多個進程。 IME這種模式仍然有點脆弱,但是我已經取得了一些成功。

首先,將gdb設置為正確的模式:

set detach-on-fork off
set non-stop off
set pagination off

(如果您有較舊的gdb,則還需要set target-async on )。

現在您可以調試shell ,例如:

$ gdb --args /bin/sh /path/to/my/script
(gdb) [... set the mode as above ...]
(gdb) break some_function_in_my_inferior

現在run應該啟動腳本,並自動將gdb附加到創建的每個子進程中; 最終在斷點處停止。

還有另一種方法。 很久以前,有一個內核補丁程序可以添加“全局斷點”,還有一個gdb補丁程序可以使用此功能。 據我所知,這些都沒有合並。 但是,我在gdb幫助程序項目中編寫了一個變體。

那里有一個稱為preattach的新命令。 它所做的是使用的SystemTap看一個exec指定的程序的; 然后在gdb附加到該程序時暫停該程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM