user2342775
user2342775

Reputation: 245

View/Print function code from within GDB

I am trying to develop a simple text based user interface which runs some gdb commands. I want to user to be able to set and break/trace point at a certain area of the code and run some debug commands.

I want to user to enter the function which needs to be debugged. I then take this function name and print out the source code of the function, then ask the user to select which line of code at which to set the break/trace point. At the moment, using the disassemble command I can print out the memory addresses for the user, but i want to print the actual source code instead.

Can this be done in gdb?

Currently:

Dump of assembler code for function test_function:
   0x03800f70 <test_function+0>:  push   %ebp
   0x03800f71 <test_function+1>:  mov    %esp,%ebp
   0x03800f73 <test_function+3>:  sub    $0x48,%esp

What I want:

void main()
{
  printf("Hello World\n");
}

Thanks!

EDIT: I'm getting this:

(gdb) list myFunction
941     directory/directory_etc/sourcefile.c: No such file or directory.
        in directory/directory_etc/sourcefile.c

then i tried specifying linenum:

(gdb) list directory/directory_etc/sourcefile.c:941
936     in directory/directory_etc/sourcefile.c

So the behaviour is similar to what you are describing, but "list filename:linenum" still isnt working

Thank you!

Upvotes: 16

Views: 77268

Answers (3)

Mohammad Rahimi
Mohammad Rahimi

Reputation: 1123

Another way to open and edit files while using gdb is by running an external command. To do this:

  1. Inside the gdb prompt, type the following command to open the file in vim:

    (gdb) !vim <file>
    
  2. Once you're in vim, enable line numbers by typing:

    :set number
    
  3. To return to the gdb prompt, exit vim by typing:

    :q!
    

This will bring you back to gdb where you left off.

Upvotes: 0

user5840129
user5840129

Reputation:

For gcc:

Add a debugging option flag -g with the compilation of your source:

gcc -g test.c

To test, use:

gdb ./a.out
(gdb) list

For even more functionality, check out the man pages:

man gcc
man gdb

Upvotes: 1

scottt
scottt

Reputation: 7248

Use:

(gdb) list FUNCTION

See the online help of the list command for details:

(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
  LINENUM, to list around that line in current file,
  FILE:LINENUM, to list around that line in that file,
  FUNCTION, to list around beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.

For any non-toy projects, you'll probably hit a case like this:

$ gdb /bin/true
<...>
(gdb) start
<...>
(gdb) list printf
file: "/usr/include/bits/stdio2.h", line number: 104
file: "printf.c", line number: 29

Which lists the multiple definitions of a function in a code base. In the printf() case above, the non-overloaded pure C function has two definitions. One defined in stdio2.h. You can then use the list FILE:LINENUM form to specify which one you want to list:

(gdb) list printf.c:29
24  
25  /* Write formatted output to stdout from the format string FORMAT.  */
26  /* VARARGS1 */
27  int
28  __printf (const char *format, ...)
29  {
30    va_list arg;
31    int done;
32  
33    va_start (arg, format);

For the "sourcefile.c: No such file or directory" errors you're seeing, you need to tell GDB where to look for the source code. See GDB Manual: Source Path. Obviously you'll need to actually have the source code for the function you want to list on your machine.

Upvotes: 16

Related Questions