]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
GDB-attach cleanups. Have our own system() so we don't have to use
authorJulian Seward <jseward@acm.org>
Sun, 21 Apr 2002 22:03:07 +0000 (22:03 +0000)
committerJulian Seward <jseward@acm.org>
Sun, 21 Apr 2002 22:03:07 +0000 (22:03 +0000)
glibc's, and tell the user if starting GDB failed for some reason.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@115

coregrind/vg_include.h
coregrind/vg_main.c
coregrind/vg_mylibc.c
vg_include.h
vg_main.c
vg_mylibc.c

index ee7adfae1158404be78fa2168593dc006b98a043..13e3f01ff28bd6b3d135fd8766f9bb9b1614030b 100644 (file)
@@ -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
index 97f17d3554dd88afc01ae7d2cdc9bd2ede1162e4..47ea5be07315cac6bac0d44e23d67252c9a868be 100644 (file)
@@ -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.");
 }
 
 
index 2b92a8b66468353fea0fb977a7e8c4dcd9b5cf62..a728f4239969b619a6272c949cbfd54e45edffcc 100644 (file)
@@ -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.
    ------------------------------------------------------------------ */
index ee7adfae1158404be78fa2168593dc006b98a043..13e3f01ff28bd6b3d135fd8766f9bb9b1614030b 100644 (file)
@@ -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
index 97f17d3554dd88afc01ae7d2cdc9bd2ede1162e4..47ea5be07315cac6bac0d44e23d67252c9a868be 100644 (file)
--- 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.");
 }
 
 
index 2b92a8b66468353fea0fb977a7e8c4dcd9b5cf62..a728f4239969b619a6272c949cbfd54e45edffcc 100644 (file)
@@ -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.
    ------------------------------------------------------------------ */