Use -ex option for GDB rather than stdin
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 19 Nov 2014 14:57:47 +0000 (15:57 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 19 Nov 2014 14:57:47 +0000 (15:57 +0100)
src/backtrace.c

index b8f4183..0f69255 100644 (file)
@@ -73,6 +73,7 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz)
     {
         pid_t pid;
         int fds[2];
+
         pipe(fds);
 
         pid = fork();
@@ -83,7 +84,7 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz)
         }
         else if (pid == 0)
         {   /* child */
-            char *arg[10];
+            char *arg[20];
             int arg_no = 0;
             char pidstr[40];
             const char *cp = "backtrace: could not exec gdb";
@@ -103,6 +104,11 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz)
             }
             arg[arg_no++] = "/usr/bin/gdb";
             arg[arg_no++] = "-n";
+            arg[arg_no++] = "-batch";
+            arg[arg_no++] = "-ex";
+            arg[arg_no++] = "info threads";
+            arg[arg_no++] = "-ex";
+            arg[arg_no++] = "thread apply all bt";
             arg[arg_no++] = static_progname;
             sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid());
             arg[arg_no++] = pidstr;
@@ -114,20 +120,11 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz)
         else
         {  /* parent */
 
-            char *dbg_commands = "info threads\nthread apply all bt\n";
-            int off = 0;
             int sec = 0;
 
             close(fds[0]);
-            while (off < strlen(dbg_commands))
-            {
-                ssize_t r = write(fds[1], dbg_commands + off,
-                                  strlen(dbg_commands) - off);
-                if (r == (ssize_t) (-1))
-                    break;
-                off += r;
-            }
-            close(fds[1]);
+
+            write(fds[1], "quit\n", 5);
             while (1)
             {
                 int status;
@@ -141,8 +138,10 @@ static void yaz_invoke_backtrace(char *buf, int buf_sz)
                 if (sec == 4)
                     break;
                 sleep(1);
+                write(fds[1], "quit\n", 5);
                 sec++;
             }
+            close(fds[1]);
         }
     }
 #else