Can I give objdump an address and have it disassemble the containing function?

Viewed 17225

I'm finding it really annoying to have to disassemble large swathes of library code just to get enough context to see what is causing a crash. Is there any way that I can just hand objdump an address, and have it find the boundaries of the containing function for me?

EDIT: Better yet, can I have it disassemble an entire stack trace for me?

3 Answers

GDB disassemble

gdb -batch -ex "file $EXECUTABLE" -ex "disassemble/rs $ADDRESS"

For example:

a.c

#include <assert.h>

int myfunc(int i) {
    i = i + 2;
    i = i * 2;
    return i;
}

int main(void) {
    assert(myfunc(1) == 6);
    assert(myfunc(2) == 8);
    return 0;
}

Compile and disassemble myfunc to find an address:

gcc -std=c99 -O0 -g a.c
gdb -batch -ex 'file a.out' -ex "disassemble/rs myfunc"

Output:

Dump of assembler code for function myfunc:
a.c:
3   int myfunc(int i) {
   0x000000000000064a <+0>: 55  push   %rbp
   0x000000000000064b <+1>: 48 89 e5    mov    %rsp,%rbp
   0x000000000000064e <+4>: 89 7d fc    mov    %edi,-0x4(%rbp)

4       i = i + 2;
   0x0000000000000651 <+7>: 83 45 fc 02 addl   $0x2,-0x4(%rbp)

5       i = i * 2;
   0x0000000000000655 <+11>:    d1 65 fc    shll   -0x4(%rbp)

6       return i;
   0x0000000000000658 <+14>:    8b 45 fc    mov    -0x4(%rbp),%eax

7   }
   0x000000000000065b <+17>:    5d  pop    %rbp
   0x000000000000065c <+18>:    c3  retq   
End of assembler dump.

OK, so 0x0000000000000655 is in myfunc, let's confirm it works:

gdb -batch -ex 'file a.out' -ex 'disassemble/rs 0x0000000000000655'

Output: same as previous disassembly.

See also: How to disassemble one single function using objdump?

Tested on Ubuntu 18.04, GDB 8.1.

Related