linux-user: create coredumps from segfauling target binaries
cover linux-user: create coredumps from segfauling target binaries
From: Riku Voipio <[email protected]>
Instead of exiting with "qemu: uncaught target signal 11", give a coredump.
Mika Westerberg (2):
linux-user: implemented ELF coredump support for ARM target
linux-user: added x86 and x86_64 support for ELF coredump
cpu-all.h | 2 + elf.h | 19 +- exec.c | 69 +++-- linux-user/elfload.c | 985 +++++++++++++++++++++++++++++++++++++++++++++++- linux-user/linuxload.c | 50 ++-- linux-user/main.c | 42 ++- linux-user/qemu.h | 13 +- linux-user/signal.c | 33 ++- linux-user/syscall.c | 6 + 9 files changed, 1153 insertions(+), 66 deletions(-)
PATCH 1/2 linux-user: implemented ELF coredump support for ARM target
From: Mika Westerberg <[email protected]>
When target process is killed with signal (such signal that should dump core) a coredump file is created. This file is similar than coredump generated by Linux (there are few exceptions though).
From: Mika Westerberg <[email protected]>
--- cpu-all.h | 2 + elf.h | 19 +- exec.c | 69 +++-- linux-user/elfload.c | 904 +++++++++++++++++++++++++++++++++++++++++++++++- linux-user/linuxload.c | 50 ++-- linux-user/main.c | 42 ++- linux-user/qemu.h | 13 +- linux-user/signal.c | 33 ++- linux-user/syscall.c | 6 + 9 files changed, 1071 insertions(+), 67 deletions(-)
cpu-all.h +2 -0
@@ -739,6 +739,8 @@ extern unsigned long qemu_host_page_mask;#define PAGE_RESERVED 0x0020void page_dump (FILE *f); +int walk_memory_regions (void *, +int (*fn)(void *,unsigned long ,unsigned long ,unsigned long ));int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);int page_check_range (target_ulong start,target_ulong len,int flags);
elf.h +18 -1
@@ -1079,7 +1079,23 @@ typedef struct elf64_shdr {#define EI_CLASS 4#define EI_DATA 5#define EI_VERSION 6 -#define EI_PAD 7 +#define EI_OSABI 7 +#define EI_PAD 8 + +#define ELFOSABI_NONE 0/* UNIX System V ABI */ +#define ELFOSABI_SYSV 0/* Alias. */ +#define ELFOSABI_HPUX 1/* HP-UX */ +#define ELFOSABI_NETBSD 2/* NetBSD. */ +#define ELFOSABI_LINUX 3/* Linux. */ +#define ELFOSABI_SOLARIS 6/* Sun Solaris. */ +#define ELFOSABI_AIX 7/* IBM AIX. */ +#define ELFOSABI_IRIX 8/* SGI Irix. */ +#define ELFOSABI_FREEBSD 9/* FreeBSD. */ +#define ELFOSABI_TRU64 10/* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11/* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12/* OpenBSD. */ +#define ELFOSABI_ARM 97/* ARM */ +#define ELFOSABI_STANDALONE 255/* Standalone (embedded) application */ #define ELFMAG0 0x7f/* EI_MAG */ #define ELFMAG1 'E'
@@ -1106,6 +1122,7 @@ typedef struct elf64_shdr {#define NT_PRFPREG 2#define NT_PRPSINFO 3#define NT_TASKSTRUCT 4 +#define NT_AUXV 6#define NT_PRXFPREG 0x46e62b7f/* copied from gdb5.1/include/elf/common.h */
exec.c +46 -23
92 lines not on this page · raw message
linux-user/elfload.c +897 -7
989 lines not on this page · raw message
linux-user/linuxload.c +26 -24
97 lines not on this page · raw message
linux-user/main.c +35 -7
78 lines not on this page · raw message
linux-user/qemu.h +11 -2
@@ -18,6 +18,7 @@#include "syscall.h" #include "target_signal.h" #include "gdbstub.h" +#include "sys-queue.h" #if defined(USE_NPTL )#define THREAD __thread
@@ -44,6 +45,9 @@ struct image_info {abi_ulong entry;abi_ulong code_offset;abi_ulong data_offset; +abi_ulong saved_auxv; +abi_ulong arg_start; +abi_ulong arg_end;char **host_argv;int personality; };
@@ -87,7 +91,7 @@ struct emulated_sigtable {/* NOTE: we force a big alignment so that the stack stored after is aligned too */ typedef struct TaskState { - struct TaskState *next; + pid_t ts_tid; /* tid (or pid) of this task */ #ifdef TARGET_ARM /* FPA state */ FPA11 fpa;
@@ -114,6 +118,7 @@ typedef struct TaskState {#endif int used;/* non zero if used */ struct image_info *info; +struct linux_binprm *bprm;struct emulated_sigtable sigtab[TARGET_NSIG ];struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE ];/* siginfo queue */
@@ -125,6 +130,8 @@ typedef struct TaskState {extern char *exec_path;void init_task_state (TaskState *ts); +void task_settid (TaskState *); +void stop_all_tasks (void );extern const char *qemu_uname_release;/* ??? See if we can avoid exposing so much of the loader internals. */
@@ -149,13 +156,15 @@ struct linux_binprm {char **argv;char **envp;char * filename;/* Name of binary */ +int (*core_dump)(int ,const CPUState *);/* coredump routine */ };void do_init_thread (struct target_pt_regs *regs,struct image_info *infop);abi_ulong loader_build_argptr (int envc,int argc,abi_ulong sp,abi_ulong stringp,int push_ptr);int loader_exec (const char * filename,char ** argv,char ** envp, -struct target_pt_regs * regs,struct image_info *infop); +struct target_pt_regs * regs,struct image_info *infop, +struct linux_binprm *);int load_elf_binary (struct linux_binprm * bprm,struct target_pt_regs * regs,struct image_info * info);
linux-user/signal.c +30 -3
@@ -287,6 +287,23 @@ static int fatal_signal (int sig) } } +/* returns 1 if given signal should dump core if not handled */ +static int core_dump_signal (int sig) +{ +switch (sig) { +case TARGET_SIGABRT : +case TARGET_SIGFPE : +case TARGET_SIGILL : +case TARGET_SIGQUIT : +case TARGET_SIGSEGV : +case TARGET_SIGTRAP : +case TARGET_SIGBUS : +return (1 ); +default : +return (0 ); + } +} +void signal_init (void ) {struct sigaction act;
@@ -352,13 +369,23 @@ static inline void free_sigqueue(CPUState *env, struct sigqueue *q)/* abort execution with signal */ static void QEMU_NORETURN force_sig(int sig) { -int host_sig; +TaskState *ts = (TaskState *)thread_env->opaque; +int host_sig, core_dumped =0 ;struct sigaction act; host_sig =target_to_host_signal (sig); -fprintf (stderr,"qemu: uncaught target signal %d (%s) - exiting\n" , - sig,strsignal (host_sig));gdb_signalled (thread_env, sig); +/* dump core if supported by target binary format */ +if (core_dump_signal (sig) && (ts->bprm->core_dump !=NULL )) { +stop_all_tasks (); + core_dumped = + ((*ts->bprm->core_dump)(sig, thread_env) ==0 ); + } + + (void )fprintf (stderr,"qemu: uncaught target signal %d (%s) - %s\n" , + sig,strsignal (host_sig), + ((core_dumped) ?"core dumped" :"exiting" )); +/* The proper exit code for dieing from an uncaught signal is * -<signal>. The kernel doesn't allow exit() or _exit() to pass * a negative value. To get the proper exit code we need to
linux-user/syscall.c +6 -0
@@ -3379,10 +3379,13 @@ static void *clone_func(void *arg) {new_thread_info *info = arg;CPUState *env; +TaskState *ts; env = info->env; thread_env = env; + ts = (TaskState *)thread_env->opaque; info->tid =gettid (); +task_settid (ts);if (info->child_tidptr)put_user_u32 (info->tid, info->child_tidptr);if (info->parent_tidptr)
@@ -3434,6 +3437,7 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp, flags &= ~(CLONE_VFORK |CLONE_VM );if (flags &CLONE_VM ) { +TaskState *parent_ts = (TaskState *)env->opaque;#if defined(USE_NPTL )new_thread_info info;pthread_attr_t attr;
@@ -3446,6 +3450,8 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,/* Init regs that differ from the parent. */ cpu_clone_regs (new_env, newsp); new_env->opaque = ts; + ts->bprm = parent_ts->bprm; + ts->info = parent_ts->info;#if defined(USE_NPTL ) nptl_flags = flags; flags &= ~CLONE_NPTL_FLAGS2 ;
Signature · 1 line
1.6.2.1
PATCH 2/2 linux-user: added x86 and x86_64 support for ELF coredump
In reply to [email protected]From: Mika Westerberg <[email protected]>
Add the register definitions for these targets too.
From: Mika Westerberg <[email protected]>
--- linux-user/elfload.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 83 insertions(+), 0 deletions(-)
linux-user/elfload.c +83 -0
@@ -133,6 +133,52 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i regs->rip = infop->entry; } +typedef target_ulong elf_greg_t ; +typedef uint32_t target_uid_t ; +typedef uint32_t target_gid_t ; +typedef int32_t target_pid_t ; + +#define ELF_NREG 27 +typedef elf_greg_t elf_gregset_t [ELF_NREG ]; + +/* +* Note that ELF_NREG should be 29 as there should be place for +* TRAPNO and ERR "registers" as well but linux doesn't dump +* those. +* +* See linux kernel: arch/x86/include/asm/elf.h +*/ +static void elf_core_copy_regs (elf_gregset_t *regs,const CPUState *env) +{ + (*regs)[0 ] = env->regs[15 ]; + (*regs)[1 ] = env->regs[14 ]; + (*regs)[2 ] = env->regs[13 ]; + (*regs)[3 ] = env->regs[12 ]; + (*regs)[4 ] = env->regs[R_EBP ]; + (*regs)[5 ] = env->regs[R_EBX ]; + (*regs)[6 ] = env->regs[11 ]; + (*regs)[7 ] = env->regs[10 ]; + (*regs)[8 ] = env->regs[9 ]; + (*regs)[9 ] = env->regs[8 ]; + (*regs)[10 ] = env->regs[R_EAX ]; + (*regs)[11 ] = env->regs[R_ECX ]; + (*regs)[12 ] = env->regs[R_EDX ]; + (*regs)[13 ] = env->regs[R_ESI ]; + (*regs)[14 ] = env->regs[R_EDI ]; + (*regs)[15 ] = env->regs[R_EAX ];/* XXX */ + (*regs)[16 ] = env->eip; + (*regs)[17 ] = env->segs[R_CS ].selector &0xffff ; + (*regs)[18 ] = env->eflags; + (*regs)[19 ] = env->regs[R_ESP ]; + (*regs)[20 ] = env->segs[R_SS ].selector &0xffff ; + (*regs)[21 ] = env->segs[R_FS ].selector &0xffff ; + (*regs)[22 ] = env->segs[R_GS ].selector &0xffff ; + (*regs)[23 ] = env->segs[R_DS ].selector &0xffff ; + (*regs)[24 ] = env->segs[R_ES ].selector &0xffff ; + (*regs)[25 ] = env->segs[R_FS ].selector &0xffff ; + (*regs)[26 ] = env->segs[R_GS ].selector &0xffff ; +} +#else #define ELF_START_MMAP 0x80000000
@@ -163,8 +209,45 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *iA value of 0 tells we have no such handler. */ regs->edx =0 ; } + +typedef target_ulong elf_greg_t ; +typedef uint16_t target_uid_t ; +typedef uint16_t target_gid_t ; +typedef int32_t target_pid_t ; + +#define ELF_NREG 17 +typedef elf_greg_t elf_gregset_t [ELF_NREG ]; + +/* +* Note that ELF_NREG should be 19 as there should be place for +* TRAPNO and ERR "registers" as well but linux doesn't dump +* those. +* +* See linux kernel: arch/x86/include/asm/elf.h +*/ +static void elf_core_copy_regs (elf_gregset_t *regs,const CPUState *env) +{ + (*regs)[0 ] = env->regs[R_EBX ]; + (*regs)[1 ] = env->regs[R_ECX ]; + (*regs)[2 ] = env->regs[R_EDX ]; + (*regs)[3 ] = env->regs[R_ESI ]; + (*regs)[4 ] = env->regs[R_EDI ]; + (*regs)[5 ] = env->regs[R_EBP ]; + (*regs)[6 ] = env->regs[R_EAX ]; + (*regs)[7 ] = env->segs[R_DS ].selector &0xffff ; + (*regs)[8 ] = env->segs[R_ES ].selector &0xffff ; + (*regs)[9 ] = env->segs[R_FS ].selector &0xffff ; + (*regs)[10 ] = env->segs[R_GS ].selector &0xffff ; + (*regs)[11 ] = env->regs[R_EAX ];/* XXX */ + (*regs)[12 ] = env->eip; + (*regs)[13 ] = env->segs[R_CS ].selector &0xffff ; + (*regs)[14 ] = env->eflags; + (*regs)[15 ] = env->regs[R_ESP ]; + (*regs)[16 ] = env->segs[R_SS ].selector &0xffff ; +}#endif +#define USE_ELF_CORE_DUMP #define ELF_EXEC_PAGESIZE 4096#endif
Signature · 1 line
1.6.2.1
Quoted 3 lines · On Thursday 09 April 2009, [email protected] wrote:
From: Riku Voipio <[email protected]>
Instead of exiting with "qemu: uncaught target signal 11", give a coredump.
This needs to honor ulimit -c.
Paul
Quoted 6 lines · Paul Brook
Quoted 3 lines · On Thursday 09 April 2009, [email protected] wrote:
From: Riku Voipio <[email protected]>
Instead of exiting with "qemu: uncaught target signal 11", give a coredump.
This needs to honor ulimit -c.
Here you go:
Quoted 1 line
From
c9e2d50761357a902c249cf9febbfa23502f1764Mon Sep 17 00:00:00 2001
From: Mika Westerberg <[email protected]>
Date: Tue, 7 Apr 2009 09:57:11 +0300
Subject: [PATCH 10/13] linux-user: [v2] implemented ELF coredump support for ARM target
When target process is killed with signal (such signal that should dump core) a coredump file is created. This file is similar than coredump generated by Linux (there are few exceptions though).
Riku Voipio:
[v2]: add support for ulimit -c
--- cpu-all.h | 2 + elf.h | 19 +- exec.c | 69 +++-- linux-user/elfload.c | 927 +++++++++++++++++++++++++++++++++++++++++++++++- linux-user/linuxload.c | 50 ++-- linux-user/main.c | 42 ++- linux-user/qemu.h | 13 +- linux-user/signal.c | 40 ++- linux-user/syscall.c | 6 + 9 files changed, 1101 insertions(+), 67 deletions(-)
cpu-all.h +2 -0
@@ -739,6 +739,8 @@ extern unsigned long qemu_host_page_mask;#define PAGE_RESERVED 0x0020void page_dump (FILE *f); +int walk_memory_regions (void *, +int (*fn)(void *,unsigned long ,unsigned long ,unsigned long ));int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);int page_check_range (target_ulong start,target_ulong len,int flags);
elf.h +18 -1
@@ -1079,7 +1079,23 @@ typedef struct elf64_shdr {#define EI_CLASS 4#define EI_DATA 5#define EI_VERSION 6 -#define EI_PAD 7 +#define EI_OSABI 7 +#define EI_PAD 8 + +#define ELFOSABI_NONE 0/* UNIX System V ABI */ +#define ELFOSABI_SYSV 0/* Alias. */ +#define ELFOSABI_HPUX 1/* HP-UX */ +#define ELFOSABI_NETBSD 2/* NetBSD. */ +#define ELFOSABI_LINUX 3/* Linux. */ +#define ELFOSABI_SOLARIS 6/* Sun Solaris. */ +#define ELFOSABI_AIX 7/* IBM AIX. */ +#define ELFOSABI_IRIX 8/* SGI Irix. */ +#define ELFOSABI_FREEBSD 9/* FreeBSD. */ +#define ELFOSABI_TRU64 10/* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11/* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12/* OpenBSD. */ +#define ELFOSABI_ARM 97/* ARM */ +#define ELFOSABI_STANDALONE 255/* Standalone (embedded) application */ #define ELFMAG0 0x7f/* EI_MAG */ #define ELFMAG1 'E'
@@ -1106,6 +1122,7 @@ typedef struct elf64_shdr {#define NT_PRFPREG 2#define NT_PRPSINFO 3#define NT_TASKSTRUCT 4 +#define NT_AUXV 6#define NT_PRXFPREG 0x46e62b7f/* copied from gdb5.1/include/elf/common.h */
exec.c +46 -23
92 lines not on this page · raw message
linux-user/elfload.c +920 -7
1,014 lines not on this page · raw message
linux-user/linuxload.c +26 -24
97 lines not on this page · raw message
linux-user/main.c +35 -7
78 lines not on this page · raw message
linux-user/qemu.h +11 -2
@@ -18,6 +18,7 @@#include "syscall.h" #include "target_signal.h" #include "gdbstub.h" +#include "sys-queue.h" #if defined(USE_NPTL )#define THREAD __thread
@@ -44,6 +45,9 @@ struct image_info {abi_ulong entry;abi_ulong code_offset;abi_ulong data_offset; +abi_ulong saved_auxv; +abi_ulong arg_start; +abi_ulong arg_end;char **host_argv;int personality; };
@@ -87,7 +91,7 @@ struct emulated_sigtable {/* NOTE: we force a big alignment so that the stack stored after is aligned too */ typedef struct TaskState { - struct TaskState *next; + pid_t ts_tid; /* tid (or pid) of this task */ #ifdef TARGET_ARM /* FPA state */ FPA11 fpa;
@@ -114,6 +118,7 @@ typedef struct TaskState {#endif int used;/* non zero if used */ struct image_info *info; +struct linux_binprm *bprm;struct emulated_sigtable sigtab[TARGET_NSIG ];struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE ];/* siginfo queue */
@@ -125,6 +130,8 @@ typedef struct TaskState {extern char *exec_path;void init_task_state (TaskState *ts); +void task_settid (TaskState *); +void stop_all_tasks (void );extern const char *qemu_uname_release;/* ??? See if we can avoid exposing so much of the loader internals. */
@@ -149,13 +156,15 @@ struct linux_binprm {char **argv;char **envp;char * filename;/* Name of binary */ +int (*core_dump)(int ,const CPUState *);/* coredump routine */ };void do_init_thread (struct target_pt_regs *regs,struct image_info *infop);abi_ulong loader_build_argptr (int envc,int argc,abi_ulong sp,abi_ulong stringp,int push_ptr);int loader_exec (const char * filename,char ** argv,char ** envp, -struct target_pt_regs * regs,struct image_info *infop); +struct target_pt_regs * regs,struct image_info *infop, +struct linux_binprm *);int load_elf_binary (struct linux_binprm * bprm,struct target_pt_regs * regs,struct image_info * info);
linux-user/signal.c +37 -3
@@ -27,6 +27,7 @@#include <errno.h> #include <assert.h> #include <sys/ucontext.h> +#include <sys/resource.h> #include "qemu.h" #include "qemu-common.h"
@@ -287,6 +288,23 @@ static int fatal_signal (int sig) } } +/* returns 1 if given signal should dump core if not handled */ +static int core_dump_signal (int sig) +{ +switch (sig) { +case TARGET_SIGABRT : +case TARGET_SIGFPE : +case TARGET_SIGILL : +case TARGET_SIGQUIT : +case TARGET_SIGSEGV : +case TARGET_SIGTRAP : +case TARGET_SIGBUS : +return (1 ); +default : +return (0 ); + } +} +void signal_init (void ) {struct sigaction act;
@@ -352,13 +370,29 @@ static inline void free_sigqueue(CPUState *env, struct sigqueue *q)/* abort execution with signal */ static void QEMU_NORETURN force_sig(int sig) { -int host_sig; +TaskState *ts = (TaskState *)thread_env->opaque; +int host_sig, core_dumped =0 ;struct sigaction act; host_sig =target_to_host_signal (sig); -fprintf (stderr,"qemu: uncaught target signal %d (%s) - exiting\n" , - sig,strsignal (host_sig));gdb_signalled (thread_env, sig); +/* dump core if supported by target binary format */ +if (core_dump_signal (sig) && (ts->bprm->core_dump !=NULL )) { +stop_all_tasks (); + core_dumped = + ((*ts->bprm->core_dump)(sig, thread_env) ==0 ); + } +if (core_dumped) { +/* we already dumped the core of target process, we don't want +* a coredump of qemu itself */ +struct rlimit nodump; +getrlimit (RLIMIT_CORE , &nodump); + nodump.rlim_cur=0 ; +setrlimit (RLIMIT_CORE , &nodump); + (void )fprintf (stderr,"qemu: uncaught target signal %d (%s) - %s\n" , + sig,strsignal (host_sig),"core dumped" ); + } +/* The proper exit code for dieing from an uncaught signal is * -<signal>. The kernel doesn't allow exit() or _exit() to pass * a negative value. To get the proper exit code we need to
linux-user/syscall.c +6 -0
@@ -3379,10 +3379,13 @@ static void *clone_func(void *arg) {new_thread_info *info = arg;CPUState *env; +TaskState *ts; env = info->env; thread_env = env; + ts = (TaskState *)thread_env->opaque; info->tid =gettid (); +task_settid (ts);if (info->child_tidptr)put_user_u32 (info->tid, info->child_tidptr);if (info->parent_tidptr)
@@ -3434,6 +3437,7 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp, flags &= ~(CLONE_VFORK |CLONE_VM );if (flags &CLONE_VM ) { +TaskState *parent_ts = (TaskState *)env->opaque;#if defined(USE_NPTL )new_thread_info info;pthread_attr_t attr;
@@ -3446,6 +3450,8 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,/* Init regs that differ from the parent. */ cpu_clone_regs (new_env, newsp); new_env->opaque = ts; + ts->bprm = parent_ts->bprm; + ts->info = parent_ts->info;#if defined(USE_NPTL ) nptl_flags = flags; flags &= ~CLONE_NPTL_FLAGS2 ;
Signature · 6 lines
1.6.2.1 -- "rm -rf" only sounds scary if you don't have backups