Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFC] convert a host address to a string
@ 2009-01-07 12:19 Joel Brobecker
  2009-01-07 16:17 ` Mark Kettenis
  0 siblings, 1 reply; 33+ messages in thread
From: Joel Brobecker @ 2009-01-07 12:19 UTC (permalink / raw)
  To: gdb-patches

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

The host_address_to_string function converts the host address to
a string using sprintf after having converted the address to an
unsigned long.

Unfortunately for us, on x86_64-windows, unsigned long is not big
enough to hold an address. My initial approach was to detect this case
by using a configure check which defines HOST_IS_LLP64 if sizeof (void*)
is 8 while sizeof (long) is 4.

Mark's reaction was that we should be able to use something more elegant.
One of the things we could do, perhaps, is use %p, which is mentioned by
the comment inside the function.  I checked the C90 draft, and %p is
definitely mentioned, so I suspect it's fine to use it assuming we
require C90. The problem is, are we going to break the build on some
targets if we do?

2009-01-07  Joel Brobecker  <brobecker@adacore.com>

        * configure.ac: Define HOST_IS_LLP64 if the host uses the LLP64 ABI.
        * configure, config.in: Regenerate.
        * utils.c (host_address_to_string): Use "long long" to print
        addresses rather than "long" if HOST_IS_LLP64 is defined.

Tested on x86-linux, x86-windows and x86_64-windows.

Thoughts?

-- 
Joel

[-- Attachment #2: print_addr2.diff --]
[-- Type: text/plain, Size: 1831 bytes --]

diff --git a/gdb/configure.ac b/gdb/configure.ac
index e970234..8774309 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1189,6 +1189,20 @@ if test $gdb_cv_printf_has_long_long = yes; then
             [Define to 1 if the "%ll" format works to print long longs.])
 fi
 
+# Check if the host is LLP64 (pointers are 64bits and "long" is only 32bits).
+
+AC_CACHE_CHECK([host is LLP64],
+               gdb_cv_host_is_llp64,
+               [AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
+[[return !(sizeof (void *) == 8 && sizeof (long) == 4);]])],
+                              gdb_cv_host_is_llp64=yes,
+                              gdb_cv_host_is_llp64=no,
+                              gdb_cv_host_is_llp64=no)])
+if test $gdb_cv_host_is_llp64 = yes; then
+  AC_DEFINE(HOST_IS_LLP64, 1,
+            [Define to 1 if host is LLP64 (pointers is 64bits and "long" is32bits)])
+fi
+
 # Check if the compiler and runtime support printing decfloats.
 
 AC_CACHE_CHECK([for decfloat support in printf],
diff --git a/gdb/utils.c b/gdb/utils.c
index 2549a3c..d289509 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3066,9 +3066,15 @@ 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.  */
+     way of knowing whether this host supports it.  */
+#ifdef HOST_IS_LLP64
+  /* On LLP64 hosts, longs are only 32bits whereas pointers are 64bit.
+     Use a "long long" to print our address.  */
+  sprintf (str, "0x%llx", (unsigned long long) addr);
+#else
+  /* The following should work on the Alpha and on 32 bit machines.  */
   sprintf (str, "0x%lx", (unsigned long) addr);
+#endif
   return str;
 }
 

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

end of thread, other threads:[~2009-01-13 12:09 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-07 12:19 [RFC] convert a host address to a string 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
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

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