CIsForCookies
CIsForCookies

Reputation: 12817

how to make gdb show line numbers with respect to function's head?

this this is my gdb output. How can I make it write the line numbers, instead of ...227 to be main+1, as it shows when I disassemble it?

Upvotes: 1

Views: 1714

Answers (1)

Clifford
Clifford

Reputation: 93476

It is not clear exactly what you are asking since machine instruction address and source-code line number are not directly related. Possibly suited to your need is to use mixed source/disassembly. For example:

(gdb) disassemble /m main
Dump of assembler code for function main:
5       {
   0x08048330 <+0>:    push   %ebp
   0x08048331 <+1>:    mov    %esp,%ebp
   0x08048333 <+3>:    sub    $0x8,%esp
   0x08048336 <+6>:    and    $0xfffffff0,%esp
   0x08048339 <+9>:    sub    $0x10,%esp

6         printf ("Hello.\n");
   0x0804833c <+12>:   movl   $0x8048440,(%esp)
   0x08048343 <+19>:   call   0x8048284 <puts@plt>

7         return 0;
8       }
   0x08048348 <+24>:   mov    $0x0,%eax
   0x0804834d <+29>:   leave
   0x0804834e <+30>:   ret

End of assembler dump.

This shows each line of source code ahead of the machine code disassembly associates with it. Both the source line numbers and instruction addresses and offsets are shown. Note that it is likely to be far less comprehensible if you apply optimisation as often code is eliminated or re-ordered such that it no longer has a direct correspondence to the source code order.

If rather you want to show the current program counter address/offset as you step, then that can be done with the display /i $pc command:

(gdb) display /i $pc
(gdb) run                                                                                         
Starting program: /home/a.out                                                                     

Breakpoint 2, main () at main.c:13                                                                
13          printf("Hello World");                                                                
1: x/i $pc                                                                                        
=> 0x40053a <main+4>:   mov    $0x4005d4,%edi                                                     
(gdb) step                                                                                        
__printf (format=0x4005d4 "Hello World") at printf.c:28                                           
28      printf.c: No such file or directory.                                                      
1: x/i $pc                                                                                        
=> 0x7ffff7a686b0 <__printf>:   sub    $0xd8,%rsp                                                 
(gdb)

Upvotes: 2

Related Questions