Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Kai Tietz" <ktietz70@googlemail.com>
To: "Joel Brobecker" <brobecker@adacore.com>
Cc: "Kai Tietz" <Kai.Tietz@onevision.com>,
	gdb-patches@sourceware.org,
	 	"Mark Kettenis" <mark.kettenis@xs4all.nl>
Subject: Re: [RFC] convert a host address to a string
Date: Sat, 10 Jan 2009 13:31:00 -0000	[thread overview]
Message-ID: <90baa01f0901100530t6590599bucfaa12aea8898c57@mail.gmail.com> (raw)
In-Reply-To: <20090110071137.GN24105@adacore.com>

[-- Attachment #1: Type: text/plain, Size: 1345 bytes --]

2009/1/10 Joel Brobecker <brobecker@adacore.com>:
>> I think it is a sub-optimal solution to have just support for Vista64, but
>> not for XP.
>
> To me, the question is not about whether to support XP64 or not.
> I agree it would be nice to support XP64 as well.  It's about who
> has the time and energy to drive the discussion to find an accepted
> solution.  I decided to drop XP64, because it's not in the list of
> things I'm interested in while I'm sensing that it's going to take
> a bit of effort to reach a consensus. You already made a very nice
> contribution in the coff/pe reader, why not send another patch to
> further improve host_address_to_string for XP64?
>
>> On a second thought, I remembered, that bfd does things right ;) There
>> is the macro sprintf_vma in bfd.h, which handles things pretty well
>> and can be used here in utils.c, too.
>
> The problem with that routine is that it is designed to print target
> addresses, not host addresses.
>
> --
> Joel
>

ok, so I sugget the following patch instead. It is able to generate
addresses for XP64 without the use of any vendor specific printf
formatters, and uses for targets where sizeof(long) == sizeof(void*)
the long variant.

Cheers,
Kai


-- 
|  (\_/) This is Bunny. Copy and paste
| (='.'=) Bunny into your signature to help
| (")_(") him gain world domination

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: host_addr_to_string.diff --]
[-- Type: text/x-c; name=host_addr_to_string.diff, Size: 2815 bytes --]

Index: src/gdb/utils.c
===================================================================
--- src.orig/gdb/utils.c
+++ src/gdb/utils.c
@@ -3071,11 +3071,54 @@ host_address_to_string (const void *addr
 {
   char *str = get_cell ();
 
-  /* We could use the %p conversion specifier to sprintf if we had any
-     way of knowing whether this host supports it.  But the following
-     should work on the Alpha and on 32 bit machines.  */
-  sprintf (str, "0x%lx", (unsigned long) addr);
+  /* We do not use the %p conversion specifier, because the resulting
+     image can vary from implementation to implementation.  For instance,
+     some implementations format the pointer value with a leading "0x"
+     whereas others don't (Solaris, for instance).  Also, it is unspecified
+     whether the alphabetical digits are printed using uppercase letters
+     or not (in GDB, we want lowercase).
+
+     So we use the %x type instead.  This, however, introduces
+     a couple of issues:
+
+       1. The %x type expects an integer value, not a pointer.
+          So we first need to cast our pointer to an integer type
+          whose size is identical to the size of our pointer.
+          We use uintptr_t for that.
+
+       2. The %x type alone expects and int, which is not always
+          large enough to hold an address.  Usually, type "long"
+          has the same size as pointers, but certain ABIs define
+          the size of pointers to be larger than the size of long
+          (64bit Windows is one such example).
+
+          So, to be certain to have a type that's large enough
+          to hold an address, we need to use "long long".  But
+          the trick is that not all printf implementations support
+          the "ll" modifier.  On those platforms where the "ll"
+          modifier is not available, we'll assume that type "long"
+          can be used to print an address.
+
+          To make sure that the type we pass to sprintf matches
+          the type we specified in our expression, we perform
+          a second cast to "unsigned long long" if we used "%llx",
+          or "unsigned long" if we used "%lx".  */
+
+#if defined(PRINTF_HAS_LONG_LONG) && BITSIZEOF_SIZE_T == 64 && \
+  SIZEOF_LONG == 4
+  sprintf (str, "0x%llx", (unsigned long long) (uintptr_t) addr);
+#elif BITSIZEOF_SIZE_T == 64 && SIZEOF_LONG == 4
+  unsigned long long val = (unsigned long) (uintptr_t) addr;
+  if ((val & ~0xffffffffull) != 0)
+    sprintf (str, "0x%lx%08lx",
+             (unsigned long) (val >> 32), (unsigned long) val);
+  else
+    sprintf (str, "0x%lx", (unsigned long) (uintptr_t) val);
+#else
+  sprintf (str, "0x%lx", (unsigned long) (uintptr_t) addr);
+#endif
   return str;
+  BITSIZEOF_SIZE_T SIZEOF_LONG
 }
 
 char *

  reply	other threads:[~2009-01-10 13:31 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-07 12:19 Joel Brobecker
2009-01-07 16:17 ` Mark Kettenis
2009-01-08 10:19   ` Joel Brobecker
2009-01-08 10:25     ` Kai Tietz
2009-01-08 10:48       ` Joel Brobecker
2009-01-08 11:02         ` Kai Tietz
2009-01-08 11:25           ` Joel Brobecker
2009-01-08 11:31             ` Kai Tietz
2009-01-08 12:49           ` Mark Kettenis
2009-01-08 12:54             ` Joel Brobecker
2009-01-08 13:04               ` Kai Tietz
2009-01-08 13:12               ` Mark Kettenis
2009-01-08 13:26     ` Mark Kettenis
2009-01-08 13:35       ` Kai Tietz
2009-01-08 13:42         ` Joel Brobecker
2009-01-08 14:04           ` Kai Tietz
2009-01-08 16:18         ` Mark Kettenis
2009-01-08 16:23           ` Kai Tietz
2009-01-09  9:57             ` Joel Brobecker
2009-01-09 10:05               ` Kai Tietz
2009-01-09 13:12       ` Joel Brobecker
2009-01-09 14:28         ` Kai Tietz
2009-01-10  7:12           ` Joel Brobecker
2009-01-10 13:31             ` Kai Tietz [this message]
2009-01-10 13:34               ` Kai Tietz
2009-01-10 13:58               ` Mark Kettenis
2009-01-10 14:04               ` Mark Kettenis
2009-01-10 14:15                 ` Kai Tietz
2009-01-10 14:22                   ` Mark Kettenis
2009-01-10 14:25                     ` Kai Tietz
2009-01-11 13:31                     ` Joel Brobecker
2009-01-11 13:53                       ` Mark Kettenis
2009-01-13 12:09                     ` Joel Brobecker

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=90baa01f0901100530t6590599bucfaa12aea8898c57@mail.gmail.com \
    --to=ktietz70@googlemail.com \
    --cc=Kai.Tietz@onevision.com \
    --cc=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    --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