Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] target_wait/linux_nat_wait: Dump the passed in target options.
@ 2012-07-20 15:49 Pedro Alves
  2012-07-20 16:27 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2012-07-20 15:49 UTC (permalink / raw)
  To: gdb-patches

I found myself staring at "set debug lin-lwp 1" debug output wondering
whether the bug I was seeing was related to the target_options passed
to target_wait.  It would have been easier if the options were dumped
in the debug output.  This patch makes it so.

E.g., before, with set debug target, and set debug lin-lwp:

	target_wait (-1, status) = 18774,   status->kind = stopped, signal = SIGTRAP
	...
	linux_nat_wait: [process -1]


after, no options:

	target_wait (-1, status, options={}) = 21554,   status->kind = stopped, signal = SIGTRAP
	linux_nat_wait: [process -1], []


after, w/ one (the only current) option:

	target_wait (-1, status, options={TARGET_WNOHANG}) = 21584,   status->kind = stopped, signal = SIGTRAP
	linux_nat_wait: [process -1], [TARGET_WNOHANG]


Tested on x86_64 Fedora 17.

Applying in a moment.

2012-07-20  Pedro Alves  <palves@redhat.com>

	* linux-nat.c (linux_nat_wait): Dump the passed in target options.
	* target.c (target_wait): Likewise.
	(str_comma_list_concat_elem, do_option, target_options_to_string):
	New functions.
	* target.h (target_options_to_string): Declare.
---
 gdb/linux-nat.c |   12 ++++++++++--
 gdb/target.c    |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 gdb/target.h    |    4 ++++
 3 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index d2a529a..c25f155 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3929,8 +3929,16 @@ linux_nat_wait (struct target_ops *ops,
   ptid_t event_ptid;
 
   if (debug_linux_nat)
-    fprintf_unfiltered (gdb_stdlog,
-			"linux_nat_wait: [%s]\n", target_pid_to_str (ptid));
+    {
+      char *options_string;
+
+      options_string = target_options_to_string (target_options);
+      fprintf_unfiltered (gdb_stdlog,
+			  "linux_nat_wait: [%s], [%s]\n",
+			  target_pid_to_str (ptid),
+			  options_string);
+      xfree (options_string);
+    }
 
   /* Flush the async file first.  */
   if (target_can_async_p ())
diff --git a/gdb/target.c b/gdb/target.c
index bb8eae8..c916ab8 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2629,13 +2629,17 @@ target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
 	  if (targetdebug)
 	    {
 	      char *status_string;
+	      char *options_string;
 
 	      status_string = target_waitstatus_to_string (status);
+	      options_string = target_options_to_string (options);
 	      fprintf_unfiltered (gdb_stdlog,
-				  "target_wait (%d, status) = %d,   %s\n",
-				  PIDGET (ptid), PIDGET (retval),
-				  status_string);
+				  "target_wait (%d, status, options={%s})"
+				  " = %d,   %s\n",
+				  PIDGET (ptid), options_string,
+				  PIDGET (retval), status_string);
 	      xfree (status_string);
+	      xfree (options_string);
 	    }
 
 	  return retval;
@@ -3885,6 +3889,53 @@ target_waitstatus_to_string (const struct target_waitstatus *ws)
     }
 }
 
+/* Concatenate ELEM to LIST, a comma separate list, and return the
+   result.  */
+
+static char *
+str_comma_list_concat_elem (char *list, const char *elem)
+{
+  if (list == NULL)
+    return xstrdup (elem);
+  else
+    return concat (list, ", ", elem, (char *) NULL);
+}
+
+/* Helper for target_options_to_string.  If OPT is present in
+   TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
+   Returns the new resulting string.  */
+
+static char *
+do_option (int *target_options, char *ret,
+	   int opt, char *opt_str)
+{
+  if ((*target_options & opt) != 0)
+    {
+      ret = str_comma_list_concat_elem (ret, opt_str);
+      *target_options &= ~opt;
+    }
+
+  return ret;
+}
+
+char *
+target_options_to_string (int target_options)
+{
+  char *ret = NULL;
+
+#define DO_TARG_OPTION(OPT) \
+  ret = do_option (&target_options, ret, OPT, #OPT)
+
+  DO_TARG_OPTION (TARGET_WNOHANG);
+
+  if (target_options != 0)
+    ret = str_comma_list_concat_elem (ret, "unknown???");
+
+  if (ret == NULL)
+    ret = xstrdup ("");
+  return ret;
+}
+
 static void
 debug_print_register (const char * func,
 		      struct regcache *regcache, int regno)
diff --git a/gdb/target.h b/gdb/target.h
index 54c58d6..95cfbe2 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -198,6 +198,10 @@ struct syscall
    Space for the result is malloc'd, caller must free.  */
 extern char *target_waitstatus_to_string (const struct target_waitstatus *);
 
+/* Return a pretty printed form of TARGET_OPTIONS.
+   Space for the result is malloc'd, caller must free.  */
+extern char *target_options_to_string (int target_options);
+
 /* Possible types of events that the inferior handler will have to
    deal with.  */
 enum inferior_event_type


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] target_wait/linux_nat_wait: Dump the passed in target options.
  2012-07-20 15:49 [PATCH] target_wait/linux_nat_wait: Dump the passed in target options Pedro Alves
@ 2012-07-20 16:27 ` Tom Tromey
  2012-07-20 16:59   ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2012-07-20 16:27 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> +  if (list == NULL)
Pedro> +    return xstrdup (elem);
Pedro> +  else
Pedro> +    return concat (list, ", ", elem, (char *) NULL);

If list is non-NULL and allocated by malloc, then it will be leaked.

Pedro> +#define DO_TARG_OPTION(OPT) \
Pedro> +  ret = do_option (&target_options, ret, OPT, #OPT)
Pedro> +
Pedro> +  DO_TARG_OPTION (TARGET_WNOHANG);
Pedro> +
Pedro> +  if (target_options != 0)
Pedro> +    ret = str_comma_list_concat_elem (ret, "unknown???");

That could conceivably happen here.

I think the fix is just to use

 return reconcat (list, list, ", ", elem, (char *) NULL);

Tom


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] target_wait/linux_nat_wait: Dump the passed in target options.
  2012-07-20 16:27 ` Tom Tromey
@ 2012-07-20 16:59   ` Pedro Alves
  0 siblings, 0 replies; 3+ messages in thread
From: Pedro Alves @ 2012-07-20 16:59 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 07/20/2012 05:27 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> +  if (list == NULL)
> Pedro> +    return xstrdup (elem);
> Pedro> +  else
> Pedro> +    return concat (list, ", ", elem, (char *) NULL);
> 
> If list is non-NULL and allocated by malloc, then it will be leaked.

Whoops.  I somehow confused concat with reconcat.

> 
> Pedro> +#define DO_TARG_OPTION(OPT) \
> Pedro> +  ret = do_option (&target_options, ret, OPT, #OPT)
> Pedro> +
> Pedro> +  DO_TARG_OPTION (TARGET_WNOHANG);
> Pedro> +
> Pedro> +  if (target_options != 0)
> Pedro> +    ret = str_comma_list_concat_elem (ret, "unknown???");
> 
> That could conceivably happen here.
> 
> I think the fix is just to use
> 
>  return reconcat (list, list, ", ", elem, (char *) NULL);

That was indeed the intent.  Thanks for spotting this.

Here's the version I checked in.  Also extended the comments a bit.

2012-07-20  Pedro Alves  <palves@redhat.com>

	* linux-nat.c (linux_nat_wait): Dump the passed in target options.
	* target.c (target_wait): Likewise.
	(str_comma_list_concat_elem, do_option, target_options_to_string):
	New functions.
	* target.h (target_options_to_string): Declare.
---
 gdb/linux-nat.c |   12 +++++++++--
 gdb/target.c    |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 gdb/target.h    |    4 ++++
 3 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index d2a529a..c25f155 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3929,8 +3929,16 @@ linux_nat_wait (struct target_ops *ops,
   ptid_t event_ptid;

   if (debug_linux_nat)
-    fprintf_unfiltered (gdb_stdlog,
-			"linux_nat_wait: [%s]\n", target_pid_to_str (ptid));
+    {
+      char *options_string;
+
+      options_string = target_options_to_string (target_options);
+      fprintf_unfiltered (gdb_stdlog,
+			  "linux_nat_wait: [%s], [%s]\n",
+			  target_pid_to_str (ptid),
+			  options_string);
+      xfree (options_string);
+    }

   /* Flush the async file first.  */
   if (target_can_async_p ())
diff --git a/gdb/target.c b/gdb/target.c
index bb8eae8..ae31415 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2629,13 +2629,17 @@ target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
 	  if (targetdebug)
 	    {
 	      char *status_string;
+	      char *options_string;

 	      status_string = target_waitstatus_to_string (status);
+	      options_string = target_options_to_string (options);
 	      fprintf_unfiltered (gdb_stdlog,
-				  "target_wait (%d, status) = %d,   %s\n",
-				  PIDGET (ptid), PIDGET (retval),
-				  status_string);
+				  "target_wait (%d, status, options={%s})"
+				  " = %d,   %s\n",
+				  PIDGET (ptid), options_string,
+				  PIDGET (retval), status_string);
 	      xfree (status_string);
+	      xfree (options_string);
 	    }

 	  return retval;
@@ -3885,6 +3889,54 @@ target_waitstatus_to_string (const struct target_waitstatus *ws)
     }
 }

+/* Concatenate ELEM to LIST, a comma separate list, and return the
+   result.  The LIST incoming argument is released.  */
+
+static char *
+str_comma_list_concat_elem (char *list, const char *elem)
+{
+  if (list == NULL)
+    return xstrdup (elem);
+  else
+    return reconcat (list, list, ", ", elem, (char *) NULL);
+}
+
+/* Helper for target_options_to_string.  If OPT is present in
+   TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
+   Returns the new resulting string.  OPT is removed from
+   TARGET_OPTIONS.  */
+
+static char *
+do_option (int *target_options, char *ret,
+	   int opt, char *opt_str)
+{
+  if ((*target_options & opt) != 0)
+    {
+      ret = str_comma_list_concat_elem (ret, opt_str);
+      *target_options &= ~opt;
+    }
+
+  return ret;
+}
+
+char *
+target_options_to_string (int target_options)
+{
+  char *ret = NULL;
+
+#define DO_TARG_OPTION(OPT) \
+  ret = do_option (&target_options, ret, OPT, #OPT)
+
+  DO_TARG_OPTION (TARGET_WNOHANG);
+
+  if (target_options != 0)
+    ret = str_comma_list_concat_elem (ret, "unknown???");
+
+  if (ret == NULL)
+    ret = xstrdup ("");
+  return ret;
+}
+
 static void
 debug_print_register (const char * func,
 		      struct regcache *regcache, int regno)
diff --git a/gdb/target.h b/gdb/target.h
index 54c58d6..95cfbe2 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -198,6 +198,10 @@ struct syscall
    Space for the result is malloc'd, caller must free.  */
 extern char *target_waitstatus_to_string (const struct target_waitstatus *);

+/* Return a pretty printed form of TARGET_OPTIONS.
+   Space for the result is malloc'd, caller must free.  */
+extern char *target_options_to_string (int target_options);
+
 /* Possible types of events that the inferior handler will have to
    deal with.  */
 enum inferior_event_type


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-07-20 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-20 15:49 [PATCH] target_wait/linux_nat_wait: Dump the passed in target options Pedro Alves
2012-07-20 16:27 ` Tom Tromey
2012-07-20 16:59   ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox