JACK M
JACK M

Reputation: 2841

How can I know how the exit() function work?

I wrote a small program to find how the exit() function works in Linux.

#include <unistd.h>

int main()

{
    exit(0);
}

And then I compiled the program with gcc.

gcc -o  example -g -static example.c

In gdb, when I set a breakpoint, I got these lines.

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]

The program does not stop at the breakpoint. Why? I use -static to compile the program, why does the breakpoint pend until the library loads into the memory?

Upvotes: 2

Views: 331

Answers (3)

Sandeep
Sandeep

Reputation: 19452

0x080495a3 is an address of the line on which you are willing to apply break point. But the format for gdb is b (function name or line number). So You have 2 ways to do this.

1) do an l after your gdb session has started. It will list you the code in C. And then apply a break point using the line number else

2) if you want to use the address, use b *0x080495a3 way to set a break point.

This way you will be able to halt at line

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

Upvotes: 1

David Z.
David Z.

Reputation: 5701

It looks like that you're trying to set a breakpoint at a function named 0x080495a3. Instead try b *0x080495a3 to indicate to GDB that you want to break at a specific address.

Upvotes: 4

Timothy Jones
Timothy Jones

Reputation: 22125

You're asking gdb to break on a function called 0x080495a3. You'll need to use b *0x080495a3 instead.

(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.

As the help says, The * tells gdb it's an address you want to break on.

From your example:

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

The "pending" means that the breakpoint is waiting until a function called 0x080495a3 is loaded from a shared library.


You might also be interested in 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).

Upvotes: 5

Related Questions