Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: Tom Tromey <tromey@redhat.com>
Cc: gdb-patches@sourceware.org, toolchain-devel@blackfin.uclinux.org
Subject: [PATCH/RFC] new argv handlers to help with sim argv building
Date: Wed, 01 Jun 2011 16:18:00 -0000	[thread overview]
Message-ID: <201106011217.55915.vapier@gentoo.org> (raw)
In-Reply-To: <m3boyovxws.fsf@fleche.redhat.com>

[-- Attachment #1: Type: Text/Plain, Size: 4228 bytes --]

On Friday, May 27, 2011 13:46:59 Tom Tromey wrote:
> Offhand it seems like it would be better if this code directly built
> sim_argv rather than building a string and converting it, but I don't
> know if any odd issue is lurking.

i slapped this together.  what do you think (ignoring missing docs) ?
-mike

--- libiberty/argv.c	13 Aug 2010 11:36:10 -0000	1.22
+++ libiberty/argv.c	1 Jun 2011 16:17:06 -0000
@@ -301,6 +301,56 @@ char **buildargv (const char *input)
   return (argv);
 }
 
+char **appendargv (char **argv, const char *arg, ...)
+{
+  va_list ap;
+  int argc;
+
+  for (argc = 0; argv[argc]; ++argc)
+    continue;
+
+  va_start (ap, arg);
+  while (arg)
+    {
+      argv = (char **) realloc (argv, sizeof (*argv) * (argc + 2));
+      argv[argc] = strdup (arg);
+      ++argc;
+      arg = va_arg (ap, const char *);
+    }
+  va_end (ap);
+
+  if (argv)
+    argv[argc] = NULL;
+
+  return argv;
+}
+
+char **mergeargv (char **iargv, ...)
+{
+  va_list ap;
+  char **argv = NULL;
+  int i, argc = 0;
+
+  va_start (ap, iargv);
+  while (iargv)
+    {
+      for (i = 0; iargv[i]; ++i)
+	{
+	  argv = (char **) realloc (argv, sizeof (*argv) * (argc + 2));
+	  argv[argc] = strdup (iargv[i]);
+	  ++argc;
+	}
+
+      iargv = va_arg (ap, char **);
+    }
+  va_end (ap);
+
+  if (argv)
+    argv[argc] = NULL;
+
+  return argv;
+}
+
 /*
 
 @deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file})
--- include/libiberty.h	3 Jan 2011 21:05:50 -0000	1.63
+++ include/libiberty.h	1 Jun 2011 16:17:06 -0000
@@ -74,6 +74,9 @@ extern FILE *freopen_unlocked (const cha
 
 extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
 
+extern char **appendargv (char **, const char *, ...) ATTRIBUTE_MALLOC;
+extern char **mergeargv (char **iargv, ...) ATTRIBUTE_MALLOC;
+
 /* Free a vector returned by buildargv.  */
 
 extern void freeargv (char **);
--- gdb/remote-sim.c	1 Jun 2011 15:29:07 -0000	1.103
+++ gdb/remote-sim.c	1 Jun 2011 16:17:06 -0000
@@ -664,7 +664,7 @@ static void
 gdbsim_open (char *args, int from_tty)
 {
   int len;
-  char *arg_buf;
+  char **argv;
   struct sim_inferior_data *sim_data;
   SIM_DESC gdbsim_desc;
 
@@ -681,44 +681,43 @@ gdbsim_open (char *args, int from_tty)
   if (gdbsim_is_open)
     unpush_target (&gdbsim_ops);
 
-  len = (7 + 1			/* gdbsim */
-	 + strlen (" -E little")
-	 + strlen (" --architecture=xxxxxxxxxx")
-	 + strlen (" --sysroot=") + strlen (gdb_sysroot) +
-	 + (args ? strlen (args) : 0)
-	 + 50) /* slack */ ;
-  arg_buf = (char *) alloca (len);
-  strcpy (arg_buf, "gdbsim");	/* 7 */
+  argv = buildargv ("gdbsim");
+
   /* Specify the byte order for the target when it is explicitly
      specified by the user (not auto detected).  */
   switch (selected_byte_order ())
     {
     case BFD_ENDIAN_BIG:
-      strcat (arg_buf, " -E big");
+      argv = appendargv (argv, "-E", "big", NULL);
       break;
     case BFD_ENDIAN_LITTLE:
-      strcat (arg_buf, " -E little");
+      argv = appendargv (argv, "-E", "little", NULL);
       break;
     case BFD_ENDIAN_UNKNOWN:
       break;
     }
+
   /* Specify the architecture of the target when it has been
      explicitly specified */
   if (selected_architecture_name () != NULL)
-    {
-      strcat (arg_buf, " --architecture=");
-      strcat (arg_buf, selected_architecture_name ());
-    }
+    argv = appendargv (argv, "--architecture",
+			selected_architecture_name (), NULL);
+
   /* Pass along gdb's concept of the sysroot.  */
-  strcat (arg_buf, " --sysroot=");
-  strcat (arg_buf, gdb_sysroot);
+  argv = appendargv (argv, "--sysroot", gdb_sysroot, NULL);
+
   /* finally, any explicit args */
   if (args)
     {
-      strcat (arg_buf, " ");	/* 1 */
-      strcat (arg_buf, args);
+      char **uargv;
+
+      uargv = gdb_buildargv (args);
+      sim_argv = mergeargv (argv, uargv, NULL);
+      freeargv (argv);
+      freeargv (uargv);
     }
-  sim_argv = gdb_buildargv (arg_buf);
+  else
+    sim_argv = argv;
 
   init_callbacks ();
   gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

  parent reply	other threads:[~2011-06-01 16:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1306440200-25087-1-git-send-email-vapier__8251.52545371584$1306440225$gmane$org@gentoo.org>
2011-05-27 17:47 ` [PATCH] gdb: sim: automatically pass down sysroot Tom Tromey
2011-05-27 18:37   ` Mike Frysinger
2011-06-01 16:18   ` Mike Frysinger [this message]
2011-06-03 16:56     ` [PATCH/RFC] new argv handlers to help with sim argv building Tom Tromey
2011-06-03 18:34       ` Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201106011217.55915.vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=gdb-patches@sourceware.org \
    --cc=toolchain-devel@blackfin.uclinux.org \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox