Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'reload' source files in GDB

Is there a command in gdb that I can use to (re)load / "refresh" source files? (As far as I can see, gdb works only with source directories, according to Debugging with GDB: Source - and there is no specific command to "refresh")

Background about my problem:

I use a virtual machine with a debug kernel, so I can connect to a local instance of gdb, and can debug kernel modules. The modules are compiled with debug info on, and this specifies folders where the source of the modules is kept (Instruct GDB 6.5 to use source embedded in object file - Stack Overflow). I have the source directories in the same path(s) in both VM and local machine.

The problem is this - I need to do quite a bit of steps in order to get the module to segfault - and the remote gdb to go into the stack. Then I do a backtrace, and I can see the source files referenced, i.e.

#0  0xc0132a13 in ?? () #1  0xc056e551 in ?? () #2  0xc056e506 in ?? () #3  0xd8be53f3 in mymodule_func1 (var1=0xd79f9b44, var2=0x0, var3=825269148)     at /media/src/mymodule.h:954 #4  0xd8be53d0 in mymodule_func2 (data=3617561412)     at /media/src/mymodule.h:936 #5  0xc014fe87 in ?? () #6  0xc0151478 in ?? () 

Then I try to do say, list /media/src/mymodule.h:954 - and I realize that I have changed stuff on the local version of mymodule.h file!!

So I undo the changes - but unfortunately, GDB does not see these changes! And, of course, I don't want to restart GDB - because that means I have to restart the VM, and go through the entire procedure in order to get the kernel module to segfault again :( !!

Then I try to do something like this:

(gdb) show symbol-reloading Dynamic symbol table reloading multiple times in one run is off. (gdb) set symbol-reloading on (gdb) add-symbol-file ~/mymodule.o 0xd8be4000 add symbol table from file "/media/src/mymodule.o" at     .text_addr = 0xd8be4000 (y or n) y Reading symbols from /media/src/mymodule.o...done. 

... in hope that it will somehow "reload" the source files - but unfortunately, list /media/src/mymodule.h:954 shows that it doesn't, nothing is changed - even though gdb does recognize that something has changed, as in warning: Source file is more recent than executable.... (so, for the time being, I have to restart entire VM and gdb as well :( :( )

like image 604
sdaau Avatar asked Nov 07 '10 14:11

sdaau


People also ask

Can you recompile in GDB?

A key point is that you should not exit gdb before recompiling. After recompiling, when you issue the r command to rerun your program, gdb will notice that the source file is now newer than the binary executable file which it had been using, and thus will automatically reload the new binary before the rerun.

How do I restart GDB?

If you want gdb to resume normal execution, type "continue" or "c". gdb will run until your program ends, your program crashes, or gdb encounters a breakpoint.

How do I add a source file to GDB?

When you start GDB, its source path includes only ' $cdir ' and ' $cwd ', in that order. To add other directories, use the directory command. The search path is used to find both program source files and GDB script files (read using the ' -command ' option and ' source ' command).

How does GDB find source files?

GDB has a list of directories to search for source files; this is called the source path. Each time GDB wants a source file, it tries all the directories in the list, in the order they are present in the list, until it finds a file with the desired name.


2 Answers

Resetting the directory list using the directory command appears to have the desired effect.

like image 174
nandhp Avatar answered Oct 02 '22 08:10

nandhp


From https://www.cs.rochester.edu/~nelson/courses/csc_173/review/gdb.html:

After changing program, reload executable with file command

  (gdb) file gdbprog   A program is being debugged already.  Kill it? (y or n) y    Load new symbol table from "gdbprog"? (y or n) y   Reading symbols from gdbprog...   done.   Breakpoint 1 at 0x2298: file gdbprog.cc, line 10.     (gdb) run   Starting program: gdbprog     Breakpoint 1, InitArrays (array=0x18be8)         at gdbprog.cc:10   10                for(i = 0;i < 10;i++) 
like image 40
Eduardo Avatar answered Oct 02 '22 08:10

Eduardo