I was just wondering if it is possible to modify the source code directly from gdb (with gdb -tui for example)?
with gdb -tui for example
No, gdb -tui is a read only view of source code. But you can use edit command to start your favorite editor and edit source code:
$ EDITOR=vim gdb -q a.out
Reading symbols from a.out...done.
(gdb)
...
...
...
(gdb) edit
See also builtin help:
(gdb) help edit
Edit specified file or function.
With no argument, edits file containing most recent line listed.
Editing targets can be specified in these ways:
FILE:LINENUM, to edit at that line in that file,
FUNCTION, to edit at the beginning of that function,
FILE:FUNCTION, to distinguish among like-named static functions.
*ADDRESS, to edit at the line containing that address.
Uses EDITOR environment variable contents as editor (or ex as default).
Related
I have a crash dump that I want to analyse with GDB on different computer. Crashed application uses several shared libraries (*.so files). I want GDB to load symbols from some of them but I can't put all of them in the original path.
Adding LD_LIBRARY_PATH to the environment doesn't help when working with dumps. When I type info shared it shows full (non-relative) paths:
(gdb) info shared
From To Syms Read Shared Object Library
0x00007fad4fb7f220 0x00007fad4fb80179 Yes /lib/x86_64-linux-gnu/libdl.so.2
...
No /opt/app/libXXX.so
...
How to specify different path for the example libXXX.so (from gdb command line or command prompt)?
The set solib-search-path option is for that purpose. Documentation says it is ment for using with gdbserver but also works with dump analysis. By default, it is set to ., so the directory that gdb is run from.
BUT: for some reason it doesn't work out of the box (at least with gdb 10.2). I don't know the reason, but I know a workaround: just type set solib-search-path . as a first command after entering gdb prompt. The output is following for me:
(gdb) set solib-search-path .
Reading symbols from /home/example/path/libXXX.so...
Of course one can type any path she want instead of ..
To make gdb make use of a single source file, you can store it in the same directory where gdb is run. How about a complete folder ?
E.g. I have a folder called "bootup" with multiple subfolders and files in it. It generates a binary called "bootup" which I need to debug for a crash and also to understand code flow.
If i just have the bootup folder (containing the source code) in the path it doesn't seem to be enough. When i set a breakpoint and debug it still only shows the file as follows but not the source code.
(gdb)directory /root/bootup
Source directories searched: /root/bootup:/root:$cdir:$cwd
(gdb) n
141 in RecFns.cpp
(gdb)
142 in RecFns.cpp
RecFns.cpp is under /root/bootup/dir1/dir2/dir3/RecFns.cpp
What should I do to be able to get gdb printing the source file contents as I debug along.
As per http://sourceware.org/gdb/onlinedocs/gdb/Source-Path.html, directory xyz should be able to make gdb search in the directory specified. But this isn't working for me.
gdb find . -type d | xargs printf " -d %s" prog
worked for me eventually. This supplies the directories recursively to gdb.
Found it here - https://titanwolf.org/Network/Articles/Article?AID=e61837e2-2ccc-4cce-80c8-3c8555aeacbd#gsc.tab=0
Now i am able to see the source code like how i wanted to :-)
How do I save my current GDB session? and How do I load it again on GDB startup later. There is a brief discussion on .gdbinit in Art of debugging , Chapter 1. But I really don't get the saving part. Is it an autosave?
The .gdbinit file saves some configurations and user-defined commands. When gdb starts, it will try to search .gdbinit file according to some sequences (For the sequences, you can refer https://sourceware.org/gdb/current/onlinedocs/gdb/Mode-Options.html#Mode-Options). For .gdbinit file, you can refer this as an example:https://github.com/gdbinit/Gdbinit/blob/master/gdbinit.
I think you want to store the whole memory dump into the file, then restart gdb, it will reload it. I have search the invoking gdb manual(https://sourceware.org/gdb/current/onlinedocs/gdb/Invoking-GDB.html#Invoking-GDB), but can't find gdb can do it.
Personally, I think this file you request is very similar to core dump file.
I think this may have been asked earlier but i can't find one that satisfied my requirements.
I am debugging(infact trying to understand) a large project by trying to analyze the code flow in various testsuites. But when i try to set breakpoints at some files, i get the error "no source file named filename found".
So my question is:
Can gdb only accept breakpoints for the source files where the code flow enters.?
Can I set breakpoints over entire lines of a file with something like b filename:*
Will a breakpoint at header file be accepted as header files are just appended at compile time?
Any insights are more than welcome.
Edit
I checked these issues with some hello world code and found same results as pointed out in one of the answers.but my issue in the actual project still remains on. I still get the same error even when i can see the edited output of the same line which is not accepted as a breakpoint.
Edit 2
I got it working but don't understand how and why it works..??
(gdb) b /home/neeraj/BTP/trunk/include/header.h:872
No source file named /home/neeraj/BTP/trunk/include/header.h:872
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) b /home/neeraj/BTP/trunk/src/driver.cpp:2
Breakpoint 1 at 0x806c61a: file ../../../trunk/src/driver.cpp, line 2.
(gdb) b /home/neeraj/BTP/trunk/include/header.h:872
Breakpoint 2 at 0x8052fa0: file ../../../trunk/include/header.h:872, line 872.
(gdb)
Any deeper insights..?
No.
No.
Yes.
Make sure you compile with -g (debug) option. Make sure the sourcepaths are set correctly. Use directory, show directories and dir commands to see/set.
The other thing to beware of besides shared libraries is that gdb source file names are relative to the directory where the code was compiled. If you haven't compiled with absolute pathnames, use the dir command to add the compilation directory to the list of places gdb searches for source code.
And a hint: I find I am wildly more productive when I use the Data Display Debugger (DDD) graphical front end to gdb.
I can set a breakpoint in main and debug the code with the correct source code, but I don't know where GDB is taking the source code from.
The source code is not present in CWD (current working directory).
How do I find from which location GDB is taking the code?
You can use the GDB command:
info source
Sample output:
Current source file is a.c
Compilation directory is /home/user/test
Located in /home/user/test/a.c
Contains 17 lines.
Source language is c.
Compiled with DWARF 2 debugging format.
Includes preprocessor macro info.
Use
(gdb) show directories
If you don't know where those directories are set, check your .gdbinit file to see if there are statements like
directory /path/to/source
See also this other Stack Overflow question about GDB.
This information is kept in the binary in the DWARF2 format. So, in order to see the DWARF2 information, you can use the dwarfdump utility. Needed information is kept in the DW_AT_comp_dir field.
The binary is probably compiled with "-g" - i.e. debugging.
Use the GDB "show directories" command to see the source search path.