From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2610 invoked by alias); 8 Jan 2009 13:35:28 -0000 Received: (qmail 2599 invoked by uid 22791); 8 Jan 2009 13:35:27 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from outdoor.onevision.de (HELO outdoor.onevision.de) (212.77.172.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 08 Jan 2009 13:35:23 +0000 Received: from sanders.onevision.de (moonrace [212.77.172.62]) by outdoor.onevision.de (8.14.3/8.13.7/ROSCH/DDB) with ESMTP id n08DZFw6017817; Thu, 8 Jan 2009 14:35:20 +0100 In-Reply-To: <200901081326.n08DQEgY002357@brahms.sibelius.xs4all.nl> To: Mark Kettenis Cc: brobecker@adacore.com, gdb-patches@sourceware.org Subject: Re: [RFC] convert a host address to a string MIME-Version: 1.0 Message-ID: From: Kai Tietz Date: Thu, 08 Jan 2009 13:35:00 -0000 Content-Type: text/plain; charset="US-ASCII" X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-01/txt/msg00157.txt.bz2 gdb-patches-owner@sourceware.org wrote on 08.01.2009 14:26:15: > > Date: Thu, 8 Jan 2009 14:19:11 +0400 > > From: Joel Brobecker > > > > > An option would be to use the strategy used by phex_nz() to print host > > > addresses. Or we could use PRINTF_HAS_LONG_LONG, and always use %llx > > > if it's available. > > > > Unfortunately, I don't know how this could be made to work. > > The problem is that GCC insists that the integer type that we > > use to cast the host address to must have the same size. > > At one point, hoping that GCC would kill the wrong branch, > > I even tried: > > > > if (sizeof (void *) == sizeof (long)) > > printf ("0x%lx", (long) address); > > else > > printf ("0x%llx", (long long) address); > > > > But this didn't work, because GCC complained about the cast > > in the "if" branch. > > Ah, GCC is being a bit too helpful here. Damn! > > > Actually, it's only after writing the entire email that I realized > > that we have another option. See option (3) below. > > > > > I'd really like to avoid introducing another macro dealing with > > > type-size issues if possible. I especially dislike HOST_IS_LLP64 > > > since I fear its existence encourages people to write unportable code. > > > > I can see several solutions: > > > > 1. Use %p. To overcome the problem with 0x, we could use > > two alternatives: > > > > a. Import printf from gnulib. I looked at this a while ago, > > for some other issue, and I immediately stopped, as it > > looked like it might be a lot of work to do so (printf > > doesn't come alone, there's a bunch of other routines > > that printf uses which we probably want). > > I'm not very excited about this option. And if the gnulib printf > doesn't actually implement the Microsoft-invented non-standard format > specifiers it may even cause us more grief. > > > b. Strip the leading "0x" if %p already provides it. In other > > words: > > > > fprintf (buf, "0x%p", address); > > if (buf[2] == '0' && buf[3] == 'x') > > buf = buf + 2; > > return buf; > > > > There is no memory management issue in this case, because > > the buffer we return is more or less static. It's part > > of a bunch of buffers we cycle through each time we call > > this routine. The caller never frees the memory we return. > > Ugh, this is a bit ugly. And we can't even be sure that there are > even more variations on the format that %p generates. I wouldn't be > surprised at all if some platforms would use upper case for the hex > digits for example. > > > 2. Avoid the HOST_IS_LLP64 macro, but still do something similar > > inside host_address_to_string. Something like: > > > > #if defined(WIN64_) > > fprintf (buf, "0x%llx", (unsigned long long) address); > > #else > > fprintf (buf, "0x%lx", (unsigned long) address); > > #endif > > > > This eliminates the likeliness of re-using the HOST_IS_LLP64 > > macro to write non-portable code. > > Not really excited about this one either. > > > 3. Work through uintptr_t. > > > > #ifdef PRINTF_HAS_LONG_LONG > > fprintf (buf, "0x%llx", (unsigned long long) (uintptr_t) address); > > #else > > fprintf (buf, "0x%lx", (unsigned long) (uintptr_t) address); > > #endif > > This wouldn't be the first place where we'd use a double cast in > connection with intptr_t/uintptr_t. So I'd say that while this is a > bit ugly, it's certainly acceptable. It's by far the simplest way to > fix things. > > > I kinda like option 1b as being simple and avoiding the need to > > cast the address to an integer. Option (3) is my next favorite, > > but I don't like the fact that we end up doing an unnecessary > > integer promotion on the 32bit targets. > > I'm not really worried about the integer promotion. Printing host > addresses is a fairly rare operation, and certainly not time critical. > Please be aware that %llx isn't valid for x86_64 windows (as it isn't for 32-bit standard). The options 'I' or 'I64' have to be used for those targets (at least for _WIN64). Cheers, Kai | (\_/) This is Bunny. Copy and paste Bunny | (='.'=) into your signature to help him gain | (")_(") world domination.