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.
Related
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 :-)
I have an executable, which I can read symbols from (so it seems.) My problem is this: when it comes time to run, I get the following error:
(gdb) run
Starting program: /home/usr/src/etcetera/etcetera/bin/theExecutable.exe
Cannot exec -c exec /home/usr/src/etcetera/etcetera/bin/theExecutable.exe.
Error: No such file or directory
During startup program exited with code 127
obviously, I have edited the directories here. I searched how to fix this on SO, and tried some of the following solutions:
GDB cannot see source file
GDB can't find source file
GDB won't load source file
got onto this link:
https://sourceware.org/gdb/download/onlinedocs/gdb/Source-Path.html#Source-Path
and am trying to change the source file directory. (The source files are not in the same location as the executable, but instead are spread over a range of different places.) Unless I am mistaken, the way of doing this is to go:
(gdb) directory /home/usr/src/etcetera/etcetera/rootDirectoryForSourcefiles
and have the GDB search this directory. I have even tried changing directory into the source directory, and then running but still, it wants to try where the executable lives.
Am I completely missing the mark here in an obvious way, or is this likely to be quite obscure?
You are barking up the wrong tree. You problem has ~nothing to do with source files, and everything to do with your executable file.
It may be related to something in your ~/.gdbinit, or your ~/.bashrc, or the way you invoked GDB.
You should start by doing a basic sanity check:
env SHELL=/bin/sh gdb -nx /bin/date
(gdb) run
If that doesn't work, your GDB installation is screwed up.
If that does work, one of the three things I mentioned above is very likely the cause of your troubles.
I had this problem and it turned out that the shell wasn't set correctly in the /etc/passwd file.
To solve it, I opened the file with
sudo vipw
and added /bin/bash to the my account's data there.
Try to:
export SHELL=/bin/sh
before running gdb
I had met same problem. When my
SHELL=/usr/local/bin/tcsh
but I have only the file .cshrc, gdb reports the same error.
When I change SHELL:
setenv SHELL /bin/csh
Then everything goes fine.
My C++ project folder structure is as shown below.
I am trying to debug the program using gdb in my linux machine. My main function is in g2o.cpp which is inside g2o_cli folder. I am able to put breakpoints in the files in this folder by
break g2o.cpp:<line_number>
But I am not able to put breakpoints in the files in other folders, for example, a file optimizer.cpp in the 'core' folder. I tried giving the absolute path from my home directory
break ~/HOME/g2o/core/optimizer.cpp:<line_number>
but it is giving an error
No source file named ~/HOME/g2o/core/optimizer.cpp
I also tried ../../core/optimizer.cpp instead of the absolute path. Still it did not work.
I saw a similar question here. But none of the possible reasons mentioned in the answer is applicable in my case. The file optimizer.cpp is not a shared library and the entire project was compiled using cmake.
How does gdb take folder paths? How can I give the relative folder path?
A dirty hack you can use on x86 is to use int3. Just use the statement asm volatile ("int 3"); in the code where you want the breakpoint.
I work on program with multiple C++ files. I have run the executable through gdb for debugging segmentation fault. Later, gdb backtrace provided the list of functions before segmentation fault. Later, I tried to set a break point in a file on a particular line-number. (The path specified is absolute path)
(gdb) break /aia/r015/home/sathish/zfs_amr/src/zfslbminterfaced2q9.cpp:100
However, gdb gives the following message:
No source file named /aia/r015/home/sathish/zfs_amr/src/zfslbminterfaced2q9.cpp.
However, this particular does exist in the location. What really the message means?
What really the message means?
The message means that GDB does not know about any source file named /aia/r015/home/sathish/zfs_amr/src/zfslbminterfaced2q9.cpp.
There are multiple reasons this could be the case:
The debug info for this file is missing, either because that file is compiled without -g, or because the debug info was (possibly inadvertantly) stripped later on,
There are some symbolic links in the above path, and GDB knows that file by fully-resolved pathname instead,
The file is actually not linked into the executable at all,
The file part of a shared library, and symbols for that shared library haven't been loaded yet,
Etc.
As Pat suggested, setting breakpoint on zfslbminterfaced2q9.cpp:100 is more likely to work.
If that doesn't work, info sources will tell you which files GDB does know about.
Update:
info sources gives blank
This means that the application doesn't have any debug info at all.
Usually this happens for one of two reasons:
You neglected to specify -g on the link line (some platforms require -g both at compile and link time),
You have a "stray" -s somewhere on your link line (which strips the final executable).
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.