Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging disassembled libraries with gdb

in Linux and Mac OS X I can use stepi and nexti to debug an application without debugging information.

On Mac OS X gdb shows the functions that are called inside the library, although sometimes advancing several assembler instructions in each stepi instruction.

On Linux, when I step into a dynamic library gdb gets lost. For instance, with puts() there are three assembler instructions inside puts(), once gdb reaches the jump at 0x080482bf, it fails with the message "No function contains program counter for selected frame".

0x080482ba in puts@plt ()
(gdb) disassemble
Dump of assembler code for function puts@plt:
0x080482b4 <puts@plt+0>:        jmp    *0x8049580
0x080482ba <puts@plt+6>:        push   $0x10
0x080482bf <puts@plt+11>:       jmp    0x8048284 <_init+48>
End of assembler dump.
(gdb) stepi
0x080482bf in puts@plt ()
(gdb) stepi
0x08048284 in ?? ()
(gdb) disassemble
No function contains program counter for selected frame.

Do you know how to debug these library calls with gdb.

like image 411
Freeman Avatar asked Oct 18 '09 16:10

Freeman


People also ask

How do you debug disassembly?

To enable the Disassembly window, under Tools > Options > Debugging, select Enable address-level debugging. To open the Disassembly window during debugging, select Windows > Disassembly or press Alt+8.

Can GDB debug assembly?

gdb will work in an ordinary terminal window, and this is fine for debugging assembly code. For use with higher-level source code, it is more convenient to use gdb from within the emacs editor (a good one to learn!) or using a graphical front-end like ddd.

What is disassemble GDB?

Function. Specifies the function to disassemble. If specified, the disassemble command will produce the disassembly output of the entire function. Address. Specifies the address inside a function to disassemble.

How do I add a breakpoint in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.


1 Answers

If GDB does not have debug symbols for the function you are trying to debug, GDB will not be able to determine the range of memory addresses to disassemble. To work around this, you can pass the range into the disassemble command. For example:

(gdb) p $pc
$4 = (void (*)()) 0x70c72d <_IO_puts+29>
(gdb) disassemble 0x70c72d 0x70c740
Dump of assembler code from 0x70c72d to 0x70c740:
0x0070c72d <_IO_puts+29>:   mov    %eax,(%esp)
0x0070c730 <_IO_puts+32>:   call   0x721f10 <strlen>
0x0070c735 <_IO_puts+37>:   mov    0x84c(%ebx),%edx
0x0070c73b <_IO_puts+43>:   cmpw   $0x0,(%edx)
0x0070c73f <_IO_puts+47>:   mov    %edx,-0x10(%ebp)
End of assembler dump.

There may be a way to install debug symbols. On my Ubuntu system, I installed the package libc6-dbg, which allows me to step into functions in the standard library.

like image 106
mpontillo Avatar answered Sep 24 '22 23:09

mpontillo