This is the mail archive of the gdb@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

What should we use to format hexadecimal numbers?


Peter Schauer recently pointed me at the following bit of code in
solib.c:

	  printf_unfiltered ("%-*s", addr_width,
			     so->textsection != NULL 
			       ? local_hex_string_custom (
			           (unsigned long) so->textsection->addr,
	                           addr_fmt)
			       : "");

The part that needs to be fixed (well, one of them anyway) is the
call to local_hex_string_custom().  It's a really bad idea to coerce
something which is a CORE_ADDR to an unsigned long since the latter
type could well be shorter than the former.

Peter suggested using paddr() in utils.c to format hex strings along
with strlen_paddr() to determine the length of strings returned by
paddr().  But I also noticed an underutilized function called
longest_local_hex_string_custom() which could be employed for the
same purpose.  (Both of these functions take a LONGEST as the
parameter representing the number to format into a hex string.)

So what is the difference between these functions?

paddr() is lean and mean and I understand how it works without
straining my brain too much.  It knows about how big pointers are for
the target that you happen to be debugging and will return a "bare"
hex string of the appropriate length.  By "bare", I mean that it won't
have a prefix (like "0x" in C) nor a suffix (like "H" in Modula 2).

local_hex_string_custom() and longest_local_hex_string_custom() differ
in that they require that you pass in the portion of the printf format
that occurs between the "%" and the "x", i.e. potentially a field
width and a length modifier.  More importantly, depending upon your
current language, they will format your hex string with prefixes and
suffixes specified by the current language.  E.g, as noted above, the
C language would use a "0x" prefix and the Modula 2 language uses "H"
as a suffix.  The current language also affects whether or not the A-F
digits in hexadecimal numbers are capitalized or not.

So the real issue in choosing between paddr() and
longest_local_hex_string_custom() is the concern over the way that the
number ends up being formatted.  I.e, should it be formatted according
to the style of the language that we're debugging?  Or should it be in
some other "standard" format?

The code that I mention above is in info_sharedlibrary_command(). 
As written, the addresses printed by info_shared_library_command()
will be different depending upon the language you have set.  Is this
desirable?

Kevin

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]