2

Linuxでexit()関数がどのように機能するかを調べるための小さなプログラムを作成しました。

#include <unistd.h>

int main()

{
    exit(0);
}

そして、gccでプログラムをコンパイルしました。

gcc -o  example -g -static example.c

gdbでブレークポイントを設定すると、これらの行が表示されます。

Dump of assembler code for function exit:
0x080495a0 <+0>:    sub    $0x1c,%esp
0x080495a3 <+3>:    mov    0x20(%esp),%eax
0x080495a7 <+7>:    movl   $0x1,0x8(%esp)
0x080495af <+15>:   movl   $0x80d602c,0x4(%esp)
0x080495b7 <+23>:   mov    %eax,(%esp)
0x080495ba <+26>:   call   0x80494b0 <__run_exit_handlers>
End of assembler dump.

(gdb) b 0x080495a3
Function "0x080495a3" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (0x080495a3) pending.

(gdb) run
Starting program: /home/jack/Documents/overflow/example
[Inferior 1 (process 2299) exited normally]

プログラムはブレークポイントで停止しません。なんで?-staticを使用してプログラムをコンパイルしますが、ライブラリがメモリにロードされるまでブレークポイントが保留されるのはなぜですか?

4

3 に答える 3

5

と呼ばれる関数を中断するようにgdbに要求しています0x080495a3b *0x080495a3代わりに使用する必要があります。

(gdb) help break
Set breakpoint at specified line or function.
break [LOCATION] [thread THREADNUM] [if CONDITION]
LOCATION may be a line number, function name, or "*" and an address.

ヘルプが言うように、The*はgdbにそれがあなたが壊したいアドレスであることを伝えます。

あなたの例から:

Function "0x080495a3" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (0x080495a3) pending.

「保留中」とは、呼び出された関数0x080495a3が共有ライブラリからロードされるまでブレークポイントが待機していることを意味します。


あなたも興味があるかもしれませんbreak-range

(gdb) help break-range
Set a breakpoint for an address range.
break-range START-LOCATION, END-LOCATION
where START-LOCATION and END-LOCATION can be one of the following:
 LINENUM, for that line in the current file,
 FILE:LINENUM, for that line in that file,
 +OFFSET, for that number of lines after the current line
         or the start of the range
 FUNCTION, for the first line in that function,
 FILE:FUNCTION, to distinguish among like-named static functions.
 *ADDRESS, for the instruction at that address.

The breakpoint will stop execution of the inferior whenever it executes
an instruction at any address within the [START-LOCATION, END-LOCATION]
range (including START-LOCATION and END-LOCATION).
于 2012-04-18T07:12:43.340 に答える
4

。という名前の関数にブレークポイントを設定しようとしているようです0x080495a3。代わりb *0x080495a3に、特定のアドレスで中断することをGDBに示すようにしてください。

于 2012-04-18T07:12:29.583 に答える
1

0x080495a3は、ブレークポイントを適用することをいとわない回線のアドレスです。ただし、gdbの形式はb(関数名または行番号)です。したがって、これを行うには2つの方法があります。

1)gdbセッションの開始後にlを実行します。Cのコードが一覧表示されます。次に、行番号elseを使用してブレークポイントを適用します。

2)アドレスを使用する場合は、b*0x080495a3の方法を使用してブレークポイントを設定します。

このようにして、ラインで停止することができます

0x080495a3 <+3>:mov 0x20(%esp)、%eax

于 2012-04-18T07:33:00.630 に答える