* Initial import of r_sys_crash_handler ()

- Launch 'gdb --pid %d' when crashing
  - Enabled when R_DEBUG is set
* List environment variables in help message
This commit is contained in:
pancake 2011-05-15 04:20:59 +02:00
parent db61c1640a
commit 6277d904d9
3 changed files with 82 additions and 15 deletions

View File

@ -11,21 +11,26 @@ static struct r_core_t r;
static int main_help(int line) {
printf ("Usage: radare2 [-dwnLuvV] [-p prj] [-s addr] [-b bsz] [-e k=v] [file]\n");
if (!line) printf (
" -d use 'file' as a program to debug\n"
" -w open file in write mode\n"
" -n do not run ~/.radare2rc\n"
" -v nonverbose mode (no prompt)\n"
" -f block size = file size\n"
" -p [prj] set project file\n"
" -s [addr] initial seek\n"
" -b [size] initial block size\n"
" -i [file] run script file\n"
" -V show radare2 version\n"
" -l [lib] load plugin file\n"
//" -t load rabin2 info in thread\n"
" -L list supported IO plugins\n"
" -u unknown file size\n"
" -e k=v evaluate config var\n");
" -d use 'file' as a program to debug\n"
" -w open file in write mode\n"
" -n do not run ~/.radare2rc\n"
" -v nonverbose mode (no prompt)\n"
" -f block size = file size\n"
" -p [prj] set project file\n"
" -s [addr] initial seek\n"
" -b [size] initial block size\n"
" -i [file] run script file\n"
" -V show radare2 version\n"
" -l [lib] load plugin file\n"
//" -t load rabin2 info in thread\n"
" -L list supported IO plugins\n"
" -u unknown file size\n"
" -e k=v evaluate config var\n"
"Environment:\n"
" R_DEBUG handle crash signal\n"
" LIBR_PLUGINS path to plugins directory\n"
" VAPIDIR path to extra vapi directory\n"
);
return 0;
}
@ -87,6 +92,9 @@ int main(int argc, char **argv) {
char *cmdfile = NULL;
int is_gdb = R_FALSE;
if (r_sys_getenv ("R_DEBUG"))
r_sys_crash_handler ("gdb --pid %d");
if (argc<2)
return main_help (1);
r_core_init (&r);

View File

@ -301,6 +301,7 @@ R_API int r_file_mkstemp(const char *prefix, char **oname);
R_API const char *r_file_tmpdir();
R_API ut64 r_sys_now();
R_API int r_sys_crash_handler(const char *cmd);
R_API const char *r_sys_arch_str(int arch);
R_API int r_sys_arch_id(const char *arch);
R_API RList *r_sys_dir(const char *path);

View File

@ -123,6 +123,64 @@ R_API int r_sys_setenv(const char *key, const char *value) {
#endif
}
static char *crash_handler_cmd = NULL;
static void signal_handler(int signum) {
int len;
char *cmd;
if (!crash_handler_cmd)
return;
len = strlen (crash_handler_cmd)+32;
cmd = malloc (len);
snprintf (cmd, len, crash_handler_cmd, getpid ());
r_sys_backtrace ();
system (cmd);
exit (1);
}
static int checkcmd(const char *c) {
char oc = 0;
for (;*c;c++) {
if (oc == '%')
if (*c!='d' && *c!='%')
return 0;
oc = *c;
}
return 1;
}
R_API int r_sys_crash_handler(const char *cmd) {
#if __UNIX__
struct sigaction sigact;
if (!checkcmd (cmd))
return R_FALSE;
free (crash_handler_cmd);
crash_handler_cmd = strdup (cmd);
sigact.sa_handler = signal_handler;
sigemptyset (&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction (SIGINT, &sigact, (struct sigaction *)NULL);
sigaddset (&sigact.sa_mask, SIGSEGV);
sigaction (SIGSEGV, &sigact, (struct sigaction *)NULL);
sigaddset (&sigact.sa_mask, SIGBUS);
sigaction (SIGBUS, &sigact, (struct sigaction *)NULL);
sigaddset (&sigact.sa_mask, SIGQUIT);
sigaction (SIGQUIT, &sigact, (struct sigaction *)NULL);
sigaddset (&sigact.sa_mask, SIGHUP);
sigaction (SIGHUP, &sigact, (struct sigaction *)NULL);
sigaddset (&sigact.sa_mask, SIGKILL);
sigaction (SIGKILL, &sigact, (struct sigaction *)NULL);
return R_TRUE;
#else
return R_FALSE;
#endif
}
#if __WINDOWS__
R_API const char *r_sys_getenv(const char *key) {
static char envbuf[1024];