[RFC] convert a host address to a string

Joel Brobecker
2009-01-07T12:19:22Z

[-- 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;
}