Examining Source Files

Posted by Beetle B. on Thu 16 January 2020

Printing Source Lines

Note: You can use GDB with Emacs, and viewing the source via Emacs is convenient.

list prints 10 lines. To change the number of lines printed, do set listsize COUNT. To see the current size, do show listsize.

list N prints around line \(N\).

list function prints around the beginning of the function.

Multiple list commands end up printing more lines.

list - means print the lines prior to the currently printed lines. There is also a list + which goes in the opposite direction.

Pressing Enter is like pressing list or list - (i.e. discards the argument).

list A,B prints lines from A to B.

list ,LAST prints lines ending at LAST

list FIRST, prints lines starting at FIRST

Specifying a Location

Linespec

  • Line number
  • Offsets from current line
  • Filename:Linenum
  • Function
  • Function:Label
  • Filename:Function
  • Label
  • Probe points

Explicit Locations

Using these may be faster for large programs.

  • Source file name
  • Function
  • Qualified function name
  • Label
  • Line

Address Locations

What you’d expect.

Editing Source Files

You can edit a given line in your favorite editor.

Searching Source Files

You can search for a regexp.

Specifying Source Directories

As the title suggests.

Source and Machine Code

You can use the command info line to map source lines to program addresses (and vice versa), and the command disassemble to display a range of addresses as machine instructions.

For programs that were dynamically linked and use shared libraries, instructions that call functions or branch to locations in the shared libraries might show a seemingly bogus location–it’s actually a location of the relocation table. On some architectures, GDB might be able to resolve these to actual function names.

tags : gdb