Pass the whole commandline.
authorJan Kratochvil <jan.kratochvil@redhat.com>
Fri, 10 Aug 2012 15:17:36 +0000 (17:17 +0200)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Fri, 10 Aug 2012 15:17:36 +0000 (17:17 +0200)
libmigdb/src/connect.c
main.c

index 9d61e57..e9f26b7 100644 (file)
@@ -31,6 +31,7 @@ in a row.
 #include <signal.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <assert.h>
 #include "mi_gdb.h"
 
 #ifndef TEMP_FAILURE_RETRY
@@ -362,6 +363,9 @@ similar to a "FILE *" for stdio.
   
 ***************************************************************************/
 
+char **mi_gdb_argv;
+void (*mi_gdb_start_hook) (mi_h *h);
+
 mi_h *mi_connect_local()
 {
  mi_h *h;
@@ -412,7 +416,7 @@ mi_h *mi_connect_local()
     argv[2]="--quiet";
     argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
     argv[4]=0;
-    execvp(argv[0],argv);
+    execvp(argv[0],mi_gdb_argv ? mi_gdb_argv : argv);
     /* We get here only if exec failed. */
     _exit(127);
    }
@@ -429,10 +433,18 @@ mi_h *mi_connect_local()
     mi_free_h(&h);
     return NULL;
    }
- /* Wait for the prompt. */
- mi_get_response_blk(h);
- /* Send the start-up commands */
- mi_send_commands(h,gdb_start);
+ if (mi_gdb_start_hook)
+  {
+   mi_gdb_start_hook (h);
+   assert (gdb_start == NULL);
+  }
+ else
+  {
+   /* Wait for the prompt. */
+   mi_get_response_blk(h);
+   /* Send the start-up commands */
+   mi_send_commands(h,gdb_start);
+  }
 
  return h;
 }
diff --git a/main.c b/main.c
index 040c6f1..2980031 100644 (file)
--- a/main.c
+++ b/main.c
@@ -349,58 +349,42 @@ completion_entry_function (const char *text, int matches)
     gdb_assert_not_reached ("too many matches");
 }
 
+extern char **mi_gdb_argv;
+extern void (*mi_gdb_start_hook) (mi_h *h);
+
+static void
+start_hook (mi_h *h)
+{
+  on_exit (h_disconnect, h);
+  mi_set_console_cb (h, console_cb, NULL);
+//  mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
+//  mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
+}
+
 int
 main (int argc, char **argv)
 {
   mi_h *h;
-  char *cmd;
-  const char *ex[argc], *arg_file = NULL, *arg_core = NULL, *arg_pid = NULL;
-  unsigned ex_count = 0, ex_used = 0;
+  mi_output *res;
 
   setbuf (stdout, NULL);
 
-  while (*++argv != NULL)
-    {
-      if (strcmp (*argv, "-ex") == 0 && argv[1] != NULL)
-       ex[ex_count++] = *++argv;
-      else if (strcmp (*argv, "-c") == 0 && argv[1] != NULL)
-       arg_core = *++argv;
-      else if (strncmp (*argv, "--core=", strlen ("--core=")) == 0)
-       arg_core = xstrdup (&(*argv)[strlen ("--core=")]);
-      else if (strcmp (*argv, "-p") == 0 && argv[1] != NULL)
-       arg_pid = *++argv;
-      else if (strncmp (*argv, "--pid=", strlen ("--pid=")) == 0)
-       arg_pid = xstrdup (&(*argv)[strlen ("--pid=")]);
-      else if (arg_file == NULL)
-       arg_file = *argv;
-      else if (arg_core == NULL && arg_pid == NULL)
-       {
-         char *end = NULL;
-
-         strtol (*argv, &end, 0);
-         if (isdigit ((*argv)[0]) && (end == NULL || *end == '\0'))
-           arg_pid = *argv;
-         else
-           arg_core = *argv;
-       }
-      else
-       fatal (_("Excess command line argument: %s"), *argv);
-    }
-  if (arg_core != NULL && arg_pid != NULL)
-    fatal (_("Can't attach to process and specify a core file "
-            "at the same time."));
+  mi_gdb_argv = xmalloc ((argc + 2) * sizeof (*mi_gdb_argv));
+  memcpy (&mi_gdb_argv[2], &argv[1], argc * sizeof (*mi_gdb_argv));
+  mi_gdb_argv[0] = "gdb";
+  mi_gdb_argv[1] = "--interpreter=mi";
 
-  mi_set_workaround (MI_PSYM_SEARCH, 0);
+  mi_gdb_start_hook = start_hook;
 
   h = mi_connect_local ();
   if (h == NULL)
     fatal ("Cannot connect to GDB");
 
-  on_exit (h_disconnect, h);
-
-  mi_set_console_cb (h, console_cb, NULL);
-//  mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
-//  mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
+  /* First eat the prompt.  Then run empty command so that additional results
+     from -ex or -x during mi_connect_local are flushed.  */
+  res = mi_get_response_blk (h);
+  gdb_assert (res == NULL);
+  default_command (h, "echo");
 
   completion_entry_function_h = h;
   rl_completion_entry_function = completion_entry_function;
@@ -420,31 +404,13 @@ main (int argc, char **argv)
 
   for (;;)
     {
-      if (ex_used < ex_count)
-       cmd = xstrdup (ex[ex_used++]);
-      else if (arg_file != NULL)
-       {
-         cmd = xstrprintf ("file %s", arg_file);
-         arg_file = NULL;
-       }
-      else if (arg_core != NULL)
-       {
-         cmd = xstrprintf ("core-file %s", arg_core);
-         arg_core = NULL;
-       }
-      else if (arg_pid != NULL)
-       {
-         cmd = xstrprintf ("attach %s", arg_pid);
-         arg_pid = NULL;
-       }
+      char *cmd = readline ("(gdb) ");
+
+      if (cmd == NULL)
+       cmd = xstrdup ("quit");
       else
-       {
-         cmd = readline ("(gdb) ");
-         if (cmd == NULL)
-           cmd = xstrdup ("quit");
-         else
-           add_history (cmd);
-       }
+       add_history (cmd);
+
       executecommand (h, cmd);
       xfree (cmd);
     }