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!
To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".
Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).
The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).
Use the "file" command. You must compile the program with debug information in order to see the source code while debugging.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With