16

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!

1

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, when i run this command on a simple file (HelloWorld.exe) it works no problem. However, I am trying to use gdb to debug a large live system. The function I want to view is in one of many c files, which are compiled into a .elf file and when I run it on there I get: (gdb) list FUNCTION 941 in directory/directory_etc/code.c
@user2342775, I've amended my answer for the case when gdb's list command shows several definitions for a function name. Is this what you're seeing?
I've edited my original question to try to make it more readable, thank you!
Is there by any chance a way to ask gdb where the function is defined?
@x-yuri, yes! Try "(gdb) info line MY_FUNCTION". I've written some notes here about using GDB to help navigate the code of the Linux kernel docs.google.com/document/d/… To really use these features, you'll want to read "Chapter 9 Examining Source Files" of the GDB manual.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.