Core dump
From Attie's Wiki
(Difference between revisions)
m |
m |
||
Line 22: | Line 22: | ||
setrlimit(RLIMIT_CORE,&rlim); | setrlimit(RLIMIT_CORE,&rlim); | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | After you call <code>coredump_enable()</code>, if your program dies for any reason (Segfault, etc) then a core dump will be taken. | ||
+ | It will often be stored to <code>./code</code> but the naming schema can be changed. | ||
+ | '''BEWARE''': once a dump has been taken, if the naming schema does not result in a unique name, then an existing dump (or file) will NOT be overwritten. | ||
+ | |||
+ | ==Naming the core dumps== | ||
+ | Run the following to see your current setting: | ||
+ | <source lang="bash"> | ||
+ | cat /proc/sys/kernel/core_pattern | ||
+ | <source> | ||
+ | |||
+ | And run the following (as root) to update it: | ||
+ | <source lang="bash"> | ||
+ | echo "%e.core.%s.%p" > /proc/sys/kernel/core_pattern | ||
+ | </source> | ||
+ | |||
+ | ===Pattern replacement=== | ||
+ | {| | ||
+ | | %p || pid | ||
+ | |- | ||
+ | | %% || output one '%' | ||
+ | |- | ||
+ | | %u || uid | ||
+ | |- | ||
+ | | %g || gid | ||
+ | |- | ||
+ | | %s || signal number | ||
+ | |- | ||
+ | | %t || UNIX time of dump | ||
+ | |- | ||
+ | | %h || hostname | ||
+ | |- | ||
+ | | %e || executable filename | ||
+ | |- | ||
+ | | %<OTHER> || both are dropped | ||
+ | |- | ||
+ | | %<NUL> || '%' is dropped | ||
+ | |} | ||
+ | |||
+ | ==Analyzing the dump later== | ||
+ | Take your executable in the left hand, and the core in the right, and shove them at gdb: | ||
+ | <source lang="bash"> | ||
+ | gdb ${EXECUTABLE} ${COREDUMP} | ||
</source> | </source> |
Revision as of 18:18, 14 June 2012
To enable a core dumps, do this in a shell:
ulimit -c unlimited
Or this from C:
#include <sys/time.h> #include <sys/resource.h> void coredump_enable(void) { struct rlimit rlim; getrlimit(RLIMIT_CORE,&rlim); rlim.rlim_cur = rlim.rlim_max; setrlimit(RLIMIT_CORE,&rlim); } void coredump_disable(void) { struct rlimit rlim; getrlimit(RLIMIT_CORE,&rlim); rlim.rlim_cur = 0; setrlimit(RLIMIT_CORE,&rlim); }
After you call coredump_enable()
, if your program dies for any reason (Segfault, etc) then a core dump will be taken.
It will often be stored to ./code
but the naming schema can be changed.
BEWARE: once a dump has been taken, if the naming schema does not result in a unique name, then an existing dump (or file) will NOT be overwritten.
Naming the core dumps
Run the following to see your current setting:
cat /proc/sys/kernel/core_pattern <source> And run the following (as root) to update it: <source lang="bash"> echo "%e.core.%s.%p" > /proc/sys/kernel/core_pattern
Pattern replacement
%p | pid |
%% | output one '%' |
%u | uid |
%g | gid |
%s | signal number |
%t | UNIX time of dump |
%h | hostname |
%e | executable filename |
%<OTHER> | both are dropped |
%<NUL> | '%' is dropped |
Analyzing the dump later
Take your executable in the left hand, and the core in the right, and shove them at gdb:
gdb ${EXECUTABLE} ${COREDUMP}