I just started learning C, and I am looking for a simple tool for debugging in gcc environment. Such tool would print a stack trace, and indicate where a segmentation fault occurs.
Try gdb; or a frontend like ddd or kdgb.
Compile with the -g flag.
GDB does it all - you need to compile your program with debug information (use -g switch) and then open it with GDB. To print stack trace use command bt.
To investigate segfaults you need to pass path to core file to GDB as well, like this:
gdb yourprogram core
If your system by default doesn't generate core files in case of segfault, you can switch it on using command:
ulimit -c unlimited
GDB is what you are looking for. In particular, the backtrace command in GDB will show you a stack trace. http://www.cs.cmu.edu/~gilpin/tutorial/
If you use X try "ddd"
Whether you use the command line gdb, or one of the front-ends such as DDD, you should definitely take a look at the gdb manual, which is also (like many GNU manuals) a very good tutorial.
I would also advise to look at valgrind.
There's also a nice integration of gdb in kdevelop (the emacs binding is fine too..)
Related
Using gdb 7.12-6 - (on debian linux in a virtualbox) I have the problems described below using step
strangely using gdb 7.4.1-debian everything seems to be fine and there are no issues (debian linux in an older virtualbox on a different computer)
I am trying to step through code. I don't want to use next because I want to go into the subroutines I have written.
I am having a problem with it going into malloc.c, s_sin.c and a whole host of other library routines.
I have checked that step-mode is off
I have tried using skip /usr/include/*, skip /usr/include/*/*, skip /usr/include/*/*/*
I already looked at the questions below and didn't find the answer to my question
Preventing GDB from stepping into a function (or file)
gdb - skip further step in of certain file by predefined rule?
Any help would be much appreciated
[I am compiling with gcc -g -o program program.c -lm]
I made my code as a standalone .c file and read that, in order to debug, the file must be in a project. So I made an empty project and added my file to it, set some breakpoints and, when i run the debugger, I get this on console:
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.6.1
Child process PID: 13112
Error in re-setting breakpoint 2: PC register is not available
Error in re-setting breakpoint -3: PC register is not available
In ()
Tried some tutorials and whatched some videos without success. Does somebody knows a fix for that? Is there a simpler way to debug a .c file?
For linux system you could use gdb as debugger in this way:
$ vim hello.c
# include <stdio.h>
int main()
{
printf("hello \n");
}
$ gcc -o hello hello.c
$ ./hello
$ gdb hello
(gdb) break main
(gdb) run
and then you can use:
c or continue
n or next
s or step
For more details see this.
Updated MinGW downloading it from its sourceforge repositor.
Downloaded the 6.2.0 version that is available in this link.
Then I unziped it to C:\ and modified the environment variable Path to add the new C:\MinGW\bin folder. To know if you made it correctly just open CMD and type gcc --version.
After that, I modified the compiller and debugger settings of Code::Blocks to use the new version of MinGW and its executables.
Now it is compiling and debugging properly.
According to Free Pascal's GDB Debugger Tips the problem is with GDB and they cite Bug 14018.
It appears you should use a different version of GDB. They suggest downgrading to 7.2. Now I believe other versions are now available, like 8.0. I don't know if GDB 8.0 suffers it too.
I'm trying to compile a simple C program on mac However I can't ever seem to get Xcode to work so I want something else. I also want to check the code for errors while I'm running it anyone got a good idea?
open Terminal, cd to your source directory,
then type gcc -Wall yourProgram.c -o YourProgram and if everything is good, you will be able to execute the program typing ./YourProgram
Also, if you are just learning C, then do not use IDEs just yet. get used to the terminal and as your experience builds, move to an IDE.]
Also there are tools like lint that analyze your code statically. Use gdb and if available, use valgrind as well for runtime debugging. I am not sure if valgrind is available on mac. Have a look at dtrace and how to use it.
I complied my program with only -g option, and add some libs like -lpthread
But when I use gdb to debug my program, using step it will step into some system functions, like fork and memcpy.
Is there a way to avoid that? It happens after I install valgrind on my computer.
Kernel:2.6.38-13
You need to run the following from a gdb prompt..
(gdb) set auto-solib-add off
It prevents gdb from loading symbols from libraries.
I am asked to debug a C code containing a number of .c files and is complied using a Makefile on a Redhat Linux system. I want to debug that whole code. How to go about it? What changes do i need to make in the Makefile?
I am using gcc as the compiler.
If you're using GCC as compiler add to the CFLAGS variable the -g option. Then you'll be able to debug the resulting executable using the gdb command.
First you need to add the flag "-g" during compilation(make this change in the makefile.if already present then no need). this will open up the symbols for debugging the code.
use the debugging tools available like gdb, dbx, mdb which ever is available in your system.
many resources are available for debugging. one of them is here
Add -g option in your makefile in order to invoke gdb debugger in the future.The way you debug on gdb is like : gdb yourprogram,then you will be redirected to another interface.type "run yourarguments" to start off debugging