View / print function code from GDB

I am trying to develop a simple text user interface that runs some gdb commands. I want the user to be able to set and break a trace point in a specific area of ​​code and execute some debugging commands.

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

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 get this:

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

then I tried to specify linenum:

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

So the behavior is similar to what you are describing, but "list filename: linenum" still doesn't work

Thanks!

+4
source share
1 answer

Using:

 (gdb) list FUNCTION 

For more information, see the list command online help:

 (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 are likely to fall into this case:

 $ 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 multiple function definitions in a code base. In the above case of printf() unloaded overhead C function has two definitions. One of them is defined in stdio2.h . Then you can use the list FILE:LINENUM to indicate 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 that you see, you need to tell GDB where to look for the source code. See GDB Handbook: Source Path . Obviously, you will need to have the source code for the function that you want to list on your computer.

+6
source

Source: https://habr.com/ru/post/1479781/


All Articles