Friday, December 18, 2009

Core Dump : how to enable & Debug

1. find out if core will be dumped.

nemesis@nemesis-laptop:~/test_code$ ulimit -c
0

This means that core will NOT be dumped. ulimit specifies the maximum size of the core file.

2. Now set the value of ulimit to whatever you want it to be

ulimit -c [size]

3.  you can also specify the directory in which core files will be placed
root@nemesis-laptop:/home/nemesis/test_code# echo "/tmp/corefiles/core" > /proc/sys/kernel/core_pattern

4. small program to cause the segmentation fault

###########################################
#include

void func2(void)
{
    int *p = NULL;
    *p = 0xdeadcafe;
}

void func1(void)
{
    func2();
}


int main (int argc, char **argv)
{
    func1();
    int *p = NULL;
    *p = 0xdeadcafe;

}

##############################################


nemesis@nemesis-laptop:~/test_code$ gcc -g -o crashcourse crash_core.c







5. now generate the core file
nemesis@nemesis-laptop:~/test_code$ ./corefile
Segmentation fault (core dumped)

6. a bit more details from the file command [Tells you the program which generated the core dump]

nemesis@nemesis-laptop:~/test_code$ file /tmp/corefiles/core
/tmp/corefiles/core: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from './corefile'


7. gdb [executable_file] [core_file]

##################################################
nemesis@nemesis-laptop:~/test_code$ gdb crashcourse /tmp/corefiles/core
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/nemesis/test_code/crashcourse...done.

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/tls/i686/cmov/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/tls/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./crashcourse'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c4 in func2 () at crash_core.c:6
6           *p = 0xdeadcafe;

##################################################

8. Get the backtrace


(gdb) bt
#0  0x080483c4 in func2 () at crash_core.c:6
#1  0x080483d4 in func1 () at crash_core.c:11
#2  0x080483e1 in main (argc=1, argv=0xbfe81b44) at crash_core.c:17





9. use the "up" and "down" commands to go through the code. (you can't run the code remember :) )

==============================================
UP
==============================================
(gdb) up
#1  0x080483d4 in func1 () at crash_core.c:11
11          func2();
(gdb) up
#2  0x080483e1 in main (argc=1, argv=0xbfe81b44) at crash_core.c:17
17          func1();
(gdb) up
Initial frame selected; you cannot go up.

===============================================
DOWN
===============================================
(gdb) down
#1  0x080483d4 in func1 () at crash_core.c:11
11          func2();
(gdb) down
#0  0x080483c4 in func2 () at crash_core.c:6
6           *p = 0xdeadcafe;


=========================================
NOTE: What if somebody handed you a core file and you don't the crashing process ? Happens a lot of times when the test team would pass you the core file and wash their hands off the issue altogether :)

Answer:
1. from the (file) following command

file [CORE-FILE]


nemesis@nemesis-laptop:~/test_code$ file /tmp/corefiles/core
/tmp/corefiles/core: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from './corefile'


2. just load the core file in gdb with "any" program name.

nemesis@nemesis-laptop:~/test_code$ gdb /bin/ps /tmp/corefiles/core
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /bin/ps...(no debugging symbols found)...done.

warning: core file may not match specified executable file.
Core was generated by `./crashcourse'.

Program terminated with signal 11, Segmentation fault.
#0  0x080483c4 in ?? ()


GDB is intelligent enough to catch your bluff but the good that comes out of it is that it tells you the correct name of the program that core-d. (GDB can't tell if your bluffing or plain ignorant :))

No comments: