GDB

GDB - Gnome debugger

Many developers may be fed up with fixing the logical errors in programs which causes core dump etc.

Gnome debugger(GDB) is useful to debug the C++ programs. Its used to analyse the core files also. core is the state of memory at specif time caused by program crash.

Lets see this program which causes core dump.

debugging Sample program with GDB
Note: all programs here assume that necessary header files are included.

#include"iostream"
using namespace std;
void func();
int main()
{

func();
return 1;
}
void func()
{
char *p;
strcpy(p,"hello,world");
cout < <>$g++ -g prog.cpp –o prog.o
$ ./prog.o
u will get output like this

Program received signal SIGSEGV, Segmentation fault core dumped .

Core file will be created as core.pid where pid is process id.
Suppose core.1431 gets created here

Note: If core file is not created then check core file size using

$ulimit –c
if it gives 0 , then core files cant be created. So increase the size by giving the

$ulimit –c unlimited

use the command $gdb 'prog_name' 'core_filename' .

here executable is prog.o

$gdb prog.o core.1431

Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
Cannot access memory at address 0xb7600744
#0 0xb743a9b6 in ?? ()

(gdb) run
Starting program: /home/edarav/plusplus/a.out
starting the func Program received signal SIGSEGV, Segmentation fault.0xb743a9b6 in strcpy () from /lib/tls/libc.so.6

the last few lines say that core dumped has come from strcpy in our code. we didnt allocated any space for char *p and we re trying to write into that memory. so the *p has to be initialised with enough memory.

No comments: