Line by line c - c++ code debugging in Linux ubuntu [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am coding using gedit in ubuntu and running program in terminal. While working in windows using Turboc or netbeans we can debug code line by line. How can we do it in ubuntu terminal? or any other option?

gdb (The Gnu debugger) is best choice
apt-get install gdb
man gdb
1. cc -g file.c // compile your program ,this will generate a.out file with required debugging information
2. gdb a.out // start with gdb
3. b main // to set break point at main
4. run // run now , and it will stop at break point main
5. s // option s is to step single line and even step into functions
6. n // option n is to execute next line and step over functions
7. p variable name // to print the value of variable at that particular instance very helpful
man gdb will give more info
All useful gdb commands and an example with simple cpp program are given Here
GDB Documentation

I find GDB (Gnu DeBugger) to be the best tool for c/c++. It's probably already installed on your system if you have gcc installed.
To use it, make sure you compile your program with the -g flag:
gcc -g myprog.c -o myprog
And then launch the debugger with
gdb ./myprog
Here are some basic commands to get you going:
b lineno - set a break point at line 'lineno'
b srcfile:lineno - set a break point in source file 'srcfile' at line 'lineno'
r - run the program
s - step through the next line of code
c - continue execution up to the next breakpoint
p varname - print the value of the variable 'varname'

You can use gdb for this.
Install gdb if it isn't already installed.
sudo apt-get install gdb
Then you can debug the executable of choice as follows
gdb <executable name>
You get a complete interactive debug session.

You can use an IDE(http://en.wikipedia.org/wiki/Integrated_development_environment) which provides code management, highlighting, debugging facilities. You may try any of these.
QTCreator(http://qt-project.org/wiki/Category:Tools::QtCreator)
KDevelop(http://www.kdevelop.org/)
Eclipse(http://www.eclipse.org/)
or you may choose to use gdb(https://www.gnu.org/software/gdb/) directly from the command line.

Related

walking through executable [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am coding using gedit in ubuntu and running program in terminal. While working in windows using Turboc or netbeans we can debug code line by line. How can we do it in ubuntu terminal? or any other option?
gdb (The Gnu debugger) is best choice
apt-get install gdb
man gdb
1. cc -g file.c // compile your program ,this will generate a.out file with required debugging information
2. gdb a.out // start with gdb
3. b main // to set break point at main
4. run // run now , and it will stop at break point main
5. s // option s is to step single line and even step into functions
6. n // option n is to execute next line and step over functions
7. p variable name // to print the value of variable at that particular instance very helpful
man gdb will give more info
All useful gdb commands and an example with simple cpp program are given Here
GDB Documentation
I find GDB (Gnu DeBugger) to be the best tool for c/c++. It's probably already installed on your system if you have gcc installed.
To use it, make sure you compile your program with the -g flag:
gcc -g myprog.c -o myprog
And then launch the debugger with
gdb ./myprog
Here are some basic commands to get you going:
b lineno - set a break point at line 'lineno'
b srcfile:lineno - set a break point in source file 'srcfile' at line 'lineno'
r - run the program
s - step through the next line of code
c - continue execution up to the next breakpoint
p varname - print the value of the variable 'varname'
You can use gdb for this.
Install gdb if it isn't already installed.
sudo apt-get install gdb
Then you can debug the executable of choice as follows
gdb <executable name>
You get a complete interactive debug session.
You can use an IDE(http://en.wikipedia.org/wiki/Integrated_development_environment) which provides code management, highlighting, debugging facilities. You may try any of these.
QTCreator(http://qt-project.org/wiki/Category:Tools::QtCreator)
KDevelop(http://www.kdevelop.org/)
Eclipse(http://www.eclipse.org/)
or you may choose to use gdb(https://www.gnu.org/software/gdb/) directly from the command line.

Stack Trace in Codeblock

I am using Codeblocks for the first time to run a cpp program. While compling the program an error occurs, I want to know the line number from the program where the error is evoking or in other words I want to see the stack trace of the program.
How can achieve this?
You can also use gdb. To debug, compile with g++ and -g at the end of the command and then run your program with gdb (in linux, gdb ./NameOfYourProgram). Then, you type r to run it, and when an error occurs, just type where and you get the stack. You can also set breakpoints and perform steps with gdb to examine the bug further.

gdb segmentation fault line number missing with c++11 option [duplicate]

Is there any gcc option I can set that will give me the line number of the segmentation fault?
I know I can:
Debug line by line
Put printfs in the code to narrow down.
Edits:
bt / where on gdb give No stack.
Helpful suggestion
I don't know of a gcc option, but you should be able to run the application with gdb and then when it crashes, type where to take a look at the stack when it exited, which should get you close.
$ gdb blah
(gdb) run
(gdb) where
Edit for completeness:
You should also make sure to build the application with debug flags on using the -g gcc option to include line numbers in the executable.
Another option is to use the bt (backtrace) command.
Here's a complete shell/gdb session
$ gcc -ggdb myproj.c
$ gdb a.out
gdb> run --some-option=foo --other-option=bar
(gdb will say your program hit a segfault)
gdb> bt
(gdb prints a stack trace)
gdb> q
[are you sure, your program is still running]? y
$ emacs myproj.c # heh, I know what the error is now...
Happy hacking :-)
You can get gcc to print you a stacktrace when your program gets a SEGV signal, similar to how Java and other friendlier languages handle null pointer exceptions. See my answer here for more details:
how to generate a stacktace when my C++ app crashes ( using gcc compiler )
The nice thing about this is you can just leave it in your code; you don't need to run things through gdb to get the nice debug output.
If you compile with -g and follow the instructions there, you can use a command-line tool like addr2line to get file/line information from the output.
Run it under valgrind.
you also need to build with debug flags on -g
You can also open the core dump with gdb (you need -g though).
If all the preceding suggestions to compile with debugging (-g) and run under a debugger (gdb, run, bt) are not working for you, then:
Elementary: Maybe you're not running under the debugger, you're just trying to analyze the postmortem core dump. (If you start a debug session, but don't run the program, or if it exits, then when you ask for a backtrace, gdb will say "No stack" -- because there's no running program at all. Don't forget to type "run".) If it segfaulted, don't forget to add the third argument (core) when you run gdb, otherwise you start in the same state, not attached to any particular process or memory image.
Difficult: If your program is/was really running but your gdb is saying "No stack" perhaps your stack pointer is badly smashed. In which case, you may be a buffer overflow problem somewhere, severe enough to mash your runtime state entirely. GCC 4.1 supports the ProPolice "Stack Smashing Protector" that is enabled with -fstack-protector-all. It can be added to GCC 3.x with a patch.
There is no method for GCC to provide this information, you'll have to rely on an external program like GDB.
GDB can give you the line where a crash occurred with the "bt" (short for "backtrace") command after the program has seg faulted. This will give you not only the line of the crash, but the whole stack of the program (so you can see what called the function where the crash happened).
The No stack problem seems to happen when the program exit successfully.
For the record, I had this problem because I had forgotten a return in my code, which made my program exit with failure code.

GDB and NS2: how to stop program at some Function call

I am using gdb to debug NS-2 which is a simulator for network protocols. It takes an .tcl file as input and interpret it. [I think it is an interpreter.]
Some of the code is written in tcl (events and creation of network components) and some in C++ (especially Packet Formats, Agents etc.).
I have created an Agent in C++ and i want to stop it at some function call so that i can see the stack trace and find which other classes have been called before it.
This is what i have done:
There was some error in one of my MyAgent::function and it was giving Segmentation Fault and gdb was stopping there automatically. I could then see the stack trace. I rectified the error.
Now when i run
gdb ./ns
b MyAgent::function()
/*
When i press TAB after writing "b MyA" it gives me all functions
of my class :). when i press enter after above command --
it asks me "Breakpoint on future shared library load" and i say Yes.
I hope this is ok ??
*/
r myfiles/myWireless.tcl
Now it runs and do not stop anywhere. :(
I am sure that this function is being called, because when that Segmentation fault was occuring, it was stopping at that function.
Thanks
You can add a breakpoint in that function:
(gdb) break MyAgent::function()
You must make sure to compile with whatever options are necessary to get debug symbols. On GCC, use the -g or -ggdb options.
You need the -args option to specify the tcl script that will be executed.
Run gdb like this:
gdb -args ./ns path/to/tcl/script.tcl
To enable debug flag to c++ code, if have not done it already, re-configure your ns2 instalation with:
./configure --enable-debug ;# plus any other flags you use for configuring
make clean
make -j 3 ;# -j for faster compiling
make install ;# optional
You can also use the --with-tcldebug=..., for debugging tcl code (You need to install tcldebug first for this option)

How do I use the MinGW gdb debugger to debug a C++ program in Windows?

I have looked for documentation on this and found nothing. I have MinGW installed and it works great. I just don't know how to use the debugger.
Given some simple code, say in a file called "mycode.cpp":
int main()
{
int temp = 0;
for (int i = 0; i < 5; ++i)
temp += i;
return 0;
}
...how would I debug this. What are the commands that I use to debug code with MinGW and GDB in windows? Can I step through the code via the command line like in Visual Studio? If so what commands do I use to do that?
Are there any tutorials for using GDB out there? I couldn't find any, but if anyone could direct me to one that would be great too. I'm tired of writing tons of std::cout statements to debug complex code.
The first step is to compile your program with -g to include debugging information within the executable:
g++ -g -o myprog.exe mycode.cpp
Then the program can be loaded into gdb:
gdb myprog.exe
A few commands to get you started:
break main will cause the debugger to break when main is called. You can also break on lines of code with break FILENAME:LINENO. For example, break mycode.cpp:4 breaks execution whenever the program reaches line 4 of mycode.cpp.
start starts the program. In your case, you need to set breakpoints before starting the program because it exits quickly.
At a breakpoint:
print VARNAME. That's how you print values of variables, whether local, static, or global. For example, at the for loop, you can type print temp to print out the value of the temp variable.
step This is equivalent to "step into".
next or adv +1 Advance to the next line (like "step over"). You can also advance to a specific line of a specific file with, for example, adv mycode.cpp:8.
bt Print a backtrace. This is a stack trace, essentially.
continue Exactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.
The best thing to read is the GDB users' manual.
There are a few gdb guis for windows in this question windows version of the GDB frontend DDD
Although DDD hasn't been ported