Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Daniel Jacobowitz <drow@false.org>
To: Mark Kettenis <mark.kettenis@xs4all.nl>
Cc: jan.kratochvil@redhat.com, gdb-patches@sourceware.org
Subject: Re: [patch] printf "%p" gdb internal error fix
Date: Tue, 04 Sep 2007 14:19:00 -0000	[thread overview]
Message-ID: <20070904141926.GA27477@caradoc.them.org> (raw)
In-Reply-To: <200609101931.k8AJVF4m026090@elgar.sibelius.xs4all.nl>

On Sun, Sep 10, 2006 at 09:31:15PM +0200, Mark Kettenis wrote:
> > Date: Sun, 10 Sep 2006 19:20:37 +0200
> > From: Jan Kratochvil <jan.kratochvil@redhat.com>
> > 
> > Hi,
> > 
> > (gdb) printf "%p\n", (void *) 7
> > internal error in printf_command

> Hmm, this will not do the right thing when you try to print a 64-bit
> pointer on a 32-bit host.

Better late than never...

Here is a patch for %p which does not rely on the size of a host
pointer.  Instead of using the underlying system's printf, it chooses
the same implementation-defined behavior chosen by glibc (%#x for
non-zero pointers and "(nil)" for null pointers).

I tested this on x86_64-linux and checked it in.

-- 
Daniel Jacobowitz
CodeSourcery

2007-09-04  Daniel Jacobowitz  <dan@codesourcery.com>

	* printcmd.c (printf_command): Handle ptr_arg.  Correct typo
	in internal error message.

2007-09-04  Daniel Jacobowitz  <dan@codesourcery.com>

	* gdb.base/display.exp: Add tests for printf %p.

Index: printcmd.c
===================================================================
RCS file: /cvs/src/src/gdb/printcmd.c,v
retrieving revision 1.109
diff -u -p -r1.109 printcmd.c
--- printcmd.c	23 Aug 2007 18:08:36 -0000	1.109
+++ printcmd.c	4 Sep 2007 13:59:44 -0000
@@ -2079,9 +2079,68 @@ printf_command (char *arg, int from_tty)
 	      printf_filtered (current_substring, val);
 	      break;
 	    }
+	  case ptr_arg:
+	    {
+	      /* We avoid the host's %p because pointers are too
+		 likely to be the wrong size.  The only interesting
+		 modifier for %p is a width; extract that, and then
+		 handle %p as glibc would: %#x or a literal "(nil)".  */
+
+	      char *p, *fmt, *fmt_p;
+#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
+	      long long val = value_as_long (val_args[i]);
+#else
+	      long val = value_as_long (val_args[i]);
+#endif
+
+	      fmt = alloca (strlen (current_substring) + 5);
+
+	      /* Copy up to the leading %.  */
+	      p = current_substring;
+	      fmt_p = fmt;
+	      while (*p)
+		{
+		  int is_percent = (*p == '%');
+		  *fmt_p++ = *p++;
+		  if (is_percent)
+		    {
+		      if (*p == '%')
+			*fmt_p++ = *p++;
+		      else
+			break;
+		    }
+		}
+
+	      if (val != 0)
+		*fmt_p++ = '#';
+
+	      /* Copy any width.  */
+	      while (*p >= '0' && *p < '9')
+		*fmt_p++ = *p++;
+
+	      gdb_assert (*p == 'p' && *(p + 1) == '\0');
+	      if (val != 0)
+		{
+#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
+		  *fmt_p++ = 'l';
+#endif
+		  *fmt_p++ = 'l';
+		  *fmt_p++ = 'x';
+		  *fmt_p++ = '\0';
+		  printf_filtered (fmt, val);
+		}
+	      else
+		{
+		  *fmt_p++ = 's';
+		  *fmt_p++ = '\0';
+		  printf_filtered (fmt, "(nil)");
+		}
+
+	      break;
+	    }
 	  default:
 	    internal_error (__FILE__, __LINE__,
-			    _("failed internal consitency check"));
+			    _("failed internal consistency check"));
 	  }
 	/* Skip to the next substring.  */
 	current_substring += strlen (current_substring) + 1;
Index: testsuite/gdb.base/display.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/display.exp,v
retrieving revision 1.11
diff -u -p -r1.11 display.exp
--- testsuite/gdb.base/display.exp	23 Aug 2007 18:14:16 -0000	1.11
+++ testsuite/gdb.base/display.exp	4 Sep 2007 13:59:45 -0000
@@ -178,6 +178,8 @@ gdb_test "printf \"\\\\!\\a\\f\\r\\t\\v\
 gdb_test "printf \"\"" ".*" "re-set term"
 gdb_test "printf \"\\w\"" ".*Unrecognized escape character.*"
 gdb_test "printf \"%d\" j" ".*Invalid argument syntax.*"
+gdb_test "printf \"%p\\n\", 0" "\\(nil\\)"
+gdb_test "printf \"%p\\n\", 1" "0x1"
 
 # play with "print", too
 #


  parent reply	other threads:[~2007-09-04 14:19 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-10 17:20 Jan Kratochvil
2006-09-10 19:31 ` Mark Kettenis
2006-09-10 22:00   ` Daniel Jacobowitz
2007-09-04 14:19   ` Daniel Jacobowitz [this message]
2007-09-04 20:43     ` Eli Zaretskii
2007-09-04 20:53       ` Daniel Jacobowitz
2007-09-15  9:00         ` Eli Zaretskii
2007-09-15 13:52           ` Daniel Jacobowitz
2007-09-15 14:55             ` Andreas Schwab
2007-09-15 15:16               ` Eli Zaretskii
2007-09-15 15:18             ` Eli Zaretskii
2007-09-15 16:12               ` Daniel Jacobowitz
2007-09-15 18:12                 ` Eli Zaretskii
2007-09-15 18:40                   ` Daniel Jacobowitz
2007-09-15 21:59                     ` Eli Zaretskii
2007-09-15 22:17                       ` Daniel Jacobowitz
2007-09-15 16:21               ` Joseph S. Myers

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=20070904141926.GA27477@caradoc.them.org \
    --to=drow@false.org \
    --cc=gdb-patches@sourceware.org \
    --cc=jan.kratochvil@redhat.com \
    --cc=mark.kettenis@xs4all.nl \
    /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