How to debug specific file with gdb? - gdb

I have to cpp files (main and functions) and I make them to build a exe file (code) and two object files (main.o and functions.o).
How can I debug specific file "functions.cpp" from gdb command line?

You need to compile your files with gcc's -g3 option. After this start gdb <exename>. You can then set breakpoint in your file inside gdb by something like b functions.cpp:36 if you want the exe to break on line 36 of functions.cpp. You can set breakpoints to particular function calls as well, such as b func(). Then run the program using r <options that exename takes>.

Related

How to get GDB to show the source code while debugging?

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 :-)

Running a .cpp file on a GPU paperspace machine

I have a paperspace account and I loaded a .cpp file I want to run since it will be faster than the machine I am using. I need to use c++11 thus I put this command into the paperspace terminal:
g++ -std=c++11 your_file.cpp -o main
But I do not get any output it just goes to the next line. I will screenshot what I mean here:
screenshot
Does anyone know how to fix this issue?
The command you enter in to the terminal only compiles your code and outputs an executable file.
(The outputted file is usually called 'a.out' but in your case it will be called 'main' as that is what you specified with the -o flag)
In order to run the executable you will have to type: The path to the executable followed by a slash followed by the executables name. For example:
/my/cpp/dir/main
A quick way to run an executable that is in your current directory is by simply typing:
./main

windows cant find dll even thought its in a directory in the PATH

I have cpp file called funner.cpp which i compiled down to a dll file by using the following command: (using microsofts CL compiler, btw)
cl /LD funner.cpp
that generated the following files: funner.lib, funner.dll.
i then created another cpp file called mainer.cpp which calls a function within the dll.
i compiled that file using this command:
cl mainer.cpp /link funner.lib
that, then, generated an executable file, called mainer.exe.
When i run mainer.exe the program runs as expected and i get no errors what so ever. (as long as the funner.dll file is in the same directory)
however i would like to move the funner.dll to another directory somewhere else, say:
c:\my_dlls\
so i did that and then i added the folder to my system PATH variable.
i then tried running the executable but it threw me an error saying that the dll was missing?
but doesn't windows search all the directories in the PATH variable for dlls ?
why cant it find my dll?
The command line does not receive the updated PATH until you close and reopen it.
Open a new command line and call the executable from there.

Not able to set gdb 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).

breakpoints in GDB

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.