walking through executable [duplicate] - 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

Cannot access memory when debugging xv6 user program using gdb

I am currently self-studying 2020 MIT 6.S081: Operating System Engineering https://pdos.csail.mit.edu/6.828/2020/schedule.html. I have followed all the steps for MAC OS to set up the environment correctly.
When I launched gdb and qemu together, I was able to break points normally when debugging the kernel executable.
However, when I attempted to do the same thing for the user executables, I was unable to break any points with the error:
Cannot access memory at address 0x...
It turned out I can set break points for some particular lines, but when I hit continue, another error has shown in the screenshot above.
Any way to get around this? Thank you!
I'm also self-studying 6.S081 and have had the same issue. I solved this just now so try this.
In Makefile, add -gdwarf-2 option to CFLAGS variable:
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
In my Makefile it's on line 94, and I didn't change other options.
I find this solution from here:
http://staff.ustc.edu.cn/~bjhua/courses/ats/2014/hw/hw-interface.html (search 'error reading variable' in the page)
https://www.reddit.com/r/RISCV/comments/plgwyk/comment/hcalnf1/?utm_source=share&utm_medium=web2x&context=3
and one more thing: If you successfully solve the problem with this but fail to watch variables in gdb, try chainging -O option for CFLAGS to -O0. It would prevent your code from being optimized (ref). (For me, this doesn't work. my code start to stuck frequently after changing this option so I just quit watching variables)
You can check out whether your gdb has warned you can't use file .gdbinit to start gdb? If not, you can simply add a line to .gdbinit: set riscv use-compressed-breakpoints yes,like this:
set confirm off
set architecture riscv:rv64
target remote 127.0.0.1:26000
symbol-file kernel/kernel
set disassemble-next-line auto
set riscv use-compressed-breakpoints yes
then use riscv64-unknown-elf-gdb to start gdb.You will see it work well.
But if you have the warning, then you should use
riscv64-unknown-elf-gdb -iex 'add-auto-load-safe-path .' to start gdb additionally.

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.

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

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.

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)