调试程序
**1 #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <string.h>
void dump(int signo) { char buf; char cmd; FILE *fh;
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", getpid());
if(!(fh = fopen(buf, "r")))
exit(0);
if(!fgets(buf, sizeof(buf), fh))
exit(0);
fclose(fh);
if(buf == '\n')
buf = '\0';
snprintf(cmd, sizeof(cmd), "gdb %s %d", buf, getpid());
system(cmd);
exit(0);
}
void
dummy_function (void) { unsigned char *ptr = 0x00; *ptr = 0x00; }
int
main (void) { signal(SIGSEGV, &dump); dummy_function ();
return 0;
} ~
******************2
#include <execinfo.h> #include <stdio.h> #include <stdlib.h> #include <signal.h>
/* A dummy function to make the backtrace more interesting. */ void dummy_function (void) { unsigned char *ptr = 0x00; *ptr = 0x00; }
void dump(int signo) { void *array; size_t size; char **strings; size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf ("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
printf ("%s\n", strings*);
free (strings);
exit(0);
}
int
main (void) { signal(SIGSEGV, &dump); dummy_function ();
return 0;
}