From: Julian Seward Date: Sun, 21 Apr 2002 22:03:07 +0000 (+0000) Subject: GDB-attach cleanups. Have our own system() so we don't have to use X-Git-Tag: svn/VALGRIND_1_0_3~336 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1eea9acdeaad3df10b8922d3fb50a157ae6ca3f9;p=thirdparty%2Fvalgrind.git GDB-attach cleanups. Have our own system() so we don't have to use glibc's, and tell the user if starting GDB failed for some reason. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@115 --- diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h index ee7adfae11..13e3f01ff2 100644 --- a/coregrind/vg_include.h +++ b/coregrind/vg_include.h @@ -716,7 +716,11 @@ extern void VG_(panic) ( Char* str ) __attribute__ ((__noreturn__)); /* Get memory by anonymous mmap. */ -void* VG_(get_memory_from_mmap) ( Int nBytes ); +extern void* VG_(get_memory_from_mmap) ( Int nBytes ); + +/* Crude stand-in for the glibc system() call. */ +extern Int VG_(system) ( Char* cmd ); + /* Signal stuff. Note that these use the vk_ (kernel) structure definitions, which are different in places from those that glibc diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c index 97f17d3554..47ea5be073 100644 --- a/coregrind/vg_main.c +++ b/coregrind/vg_main.c @@ -1111,20 +1111,21 @@ void VG_(mash_LD_PRELOAD_string)( Char* ld_preload_str ) continue the program, though; to continue, quit GDB. */ extern void VG_(start_GDB_whilst_on_client_stack) ( void ) { + Int res; UChar buf[100]; VG_(sprintf)(buf, "/usr/bin/gdb -nw /proc/%d/exe %d", VG_(getpid)(), VG_(getpid)()); - VG_(printf)("starting GDB with cmd: %s\n", buf); - VG_(mash_LD_PRELOAD_string)(VG_(getenv)("LD_PRELOAD")); - { /* HACK ALERT */ - extern int system ( const char * ); - system(buf); - /* end of HACK ALERT */ + VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf); + res = VG_(system)(buf); + if (res == 0) { + VG_(message)(Vg_UserMsg, ""); + VG_(message)(Vg_UserMsg, + "GDB has detached. Valgrind regains control. We continue."); + } else { + VG_(message)(Vg_UserMsg, "Apparently failed!"); + VG_(message)(Vg_UserMsg, ""); } - VG_(message)(Vg_UserMsg, ""); - VG_(message)(Vg_UserMsg, - "GDB has detached. Valgrind regains control. We continue."); } diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c index 2b92a8b664..a728f42399 100644 --- a/coregrind/vg_mylibc.c +++ b/coregrind/vg_mylibc.c @@ -947,6 +947,40 @@ ULong VG_(read_microsecond_timer)( void ) return (1000000ULL * (ULong)(tv.tv_sec)) + (ULong)(tv.tv_usec); } +/* Return -1 if error, else 0. NOTE does not indicate return code of + child! */ +Int VG_(system) ( Char* cmd ) +{ + Int pid, res; + void* environ[1] = { NULL }; + if (cmd == NULL) + return 1; + pid = vg_do_syscall0(__NR_fork); + if (VG_(is_kerror)(pid)) + return -1; + if (pid == 0) { + /* child */ + Char* argv[4]; + argv[0] = "/bin/sh"; + argv[1] = "-c"; + argv[2] = cmd; + argv[3] = 0; + (void)vg_do_syscall3(__NR_execve, + (UInt)"/bin/sh", (UInt)argv, (UInt)&environ); + /* If we're still alive here, execve failed. */ + return -1; + } else { + /* parent */ + res = vg_do_syscall3(__NR_waitpid, pid, (UInt)NULL, 0); + if (VG_(is_kerror)(res)) { + return -1; + } else { + return 0; + } + } +} + + /* --------------------------------------------------------------------- Primitive support for bagging memory via mmap. ------------------------------------------------------------------ */ diff --git a/vg_include.h b/vg_include.h index ee7adfae11..13e3f01ff2 100644 --- a/vg_include.h +++ b/vg_include.h @@ -716,7 +716,11 @@ extern void VG_(panic) ( Char* str ) __attribute__ ((__noreturn__)); /* Get memory by anonymous mmap. */ -void* VG_(get_memory_from_mmap) ( Int nBytes ); +extern void* VG_(get_memory_from_mmap) ( Int nBytes ); + +/* Crude stand-in for the glibc system() call. */ +extern Int VG_(system) ( Char* cmd ); + /* Signal stuff. Note that these use the vk_ (kernel) structure definitions, which are different in places from those that glibc diff --git a/vg_main.c b/vg_main.c index 97f17d3554..47ea5be073 100644 --- a/vg_main.c +++ b/vg_main.c @@ -1111,20 +1111,21 @@ void VG_(mash_LD_PRELOAD_string)( Char* ld_preload_str ) continue the program, though; to continue, quit GDB. */ extern void VG_(start_GDB_whilst_on_client_stack) ( void ) { + Int res; UChar buf[100]; VG_(sprintf)(buf, "/usr/bin/gdb -nw /proc/%d/exe %d", VG_(getpid)(), VG_(getpid)()); - VG_(printf)("starting GDB with cmd: %s\n", buf); - VG_(mash_LD_PRELOAD_string)(VG_(getenv)("LD_PRELOAD")); - { /* HACK ALERT */ - extern int system ( const char * ); - system(buf); - /* end of HACK ALERT */ + VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf); + res = VG_(system)(buf); + if (res == 0) { + VG_(message)(Vg_UserMsg, ""); + VG_(message)(Vg_UserMsg, + "GDB has detached. Valgrind regains control. We continue."); + } else { + VG_(message)(Vg_UserMsg, "Apparently failed!"); + VG_(message)(Vg_UserMsg, ""); } - VG_(message)(Vg_UserMsg, ""); - VG_(message)(Vg_UserMsg, - "GDB has detached. Valgrind regains control. We continue."); } diff --git a/vg_mylibc.c b/vg_mylibc.c index 2b92a8b664..a728f42399 100644 --- a/vg_mylibc.c +++ b/vg_mylibc.c @@ -947,6 +947,40 @@ ULong VG_(read_microsecond_timer)( void ) return (1000000ULL * (ULong)(tv.tv_sec)) + (ULong)(tv.tv_usec); } +/* Return -1 if error, else 0. NOTE does not indicate return code of + child! */ +Int VG_(system) ( Char* cmd ) +{ + Int pid, res; + void* environ[1] = { NULL }; + if (cmd == NULL) + return 1; + pid = vg_do_syscall0(__NR_fork); + if (VG_(is_kerror)(pid)) + return -1; + if (pid == 0) { + /* child */ + Char* argv[4]; + argv[0] = "/bin/sh"; + argv[1] = "-c"; + argv[2] = cmd; + argv[3] = 0; + (void)vg_do_syscall3(__NR_execve, + (UInt)"/bin/sh", (UInt)argv, (UInt)&environ); + /* If we're still alive here, execve failed. */ + return -1; + } else { + /* parent */ + res = vg_do_syscall3(__NR_waitpid, pid, (UInt)NULL, 0); + if (VG_(is_kerror)(res)) { + return -1; + } else { + return 0; + } + } +} + + /* --------------------------------------------------------------------- Primitive support for bagging memory via mmap. ------------------------------------------------------------------ */