3

When i execute the following commands i get different address of function()
(gdb) break function()
Breakpoint 1 at function() 0x804834a.
(gdb) print function()
Breakpoint 1 at function() 0x8048344.
Why there is difference in both address?

0

2 Answers 2

4

This output can't be correct, it would be if you did something as:

int func(void) {
  int a = 10;
  printf("%d\n", a);
  return 1;
}

after loading it into the gdb:

(gdb) p func
$1 = {int (void)} 0x4016b0 <func>
(gdb) b func
Breakpoint 1 at 0x4016b6: file file.c, line 4.
(gdb) disassemble func
Dump of assembler code for function func:
   0x004016b0 <+0>:     push   %ebp
   0x004016b1 <+1>:     mov    %esp,%ebp
   0x004016b3 <+3>:     sub    $0x28,%esp
   0x004016b6 <+6>:     movl   $0xa,-0xc(%ebp)
   0x004016bd <+13>:    mov    -0xc(%ebp),%eax
   0x004016c0 <+16>:    mov    %eax,0x4(%esp)
   0x004016c4 <+20>:    movl   $0x405064,(%esp)
   0x004016cb <+27>:    call   0x403678 <printf>
   0x004016d0 <+32>:    mov    $0x1,%eax
   0x004016d5 <+37>:    leave
   0x004016d6 <+38>:    ret
End of assembler dump.
(gdb)

Here func points to the exact first instruction in the function, push %ebp, but when you setup a break point, gdb sets it after stack frame initialization instructions:

   0x004016b0 <+0>:     push   %ebp
   0x004016b1 <+1>:     mov    %esp,%ebp
   0x004016b3 <+3>:     sub    $0x28,%esp

at where the instructions of the function actually begins:

=> 0x004016b6 <+6>:     movl   $0xa,-0xc(%ebp)
   0x004016bd <+13>:    mov    -0xc(%ebp),%eax
   0x004016c0 <+16>:    mov    %eax,0x4(%esp)
   0x004016c4 <+20>:    movl   $0x405064,(%esp)
   0x004016cb <+27>:    call   0x403678 <printf>
   0x004016d0 <+32>:    mov    $0x1,%eax
   0x004016d5 <+37>:    leave
   0x004016d6 <+38>:    ret

here this instruction:

movl   $0xa,-0xc(%ebp) ; 0xa = 10

is this part:

int a = 10;
Sign up to request clarification or add additional context in comments.

Comments

3

Gdb sets a breakpoint after function prologue, as before the things are properly set up it could not show the expected state like local variables, etc.

Break therefor sets breakpoint and prints address of first instruction after prologue, whereas print prints the address of actual first instruction in function.

You can set a breakpoint to actual first instruction by doing break *0x8048344, then observe the value of local variables there and after prologue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.