From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87650 invoked by alias); 2 Jun 2017 19:37:05 -0000 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 Received: (qmail 87542 invoked by uid 89); 2 Jun 2017 19:37:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.3 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:4151, H*m:3173, H*F:U*tom, H*RU:cmgw2 X-HELO: gproxy10.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 02 Jun 2017 19:36:57 +0000 Received: from cmgw2 (unknown [10.0.90.83]) by gproxy10.mail.unifiedlayer.com (Postfix) with ESMTP id 80CE6140554 for ; Fri, 2 Jun 2017 13:37:00 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id Tvcx1v00A2f2jeq01vd0jj; Fri, 02 Jun 2017 13:37:00 -0600 X-Authority-Analysis: v=2.2 cv=Ibz3YSia c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=LWSFodeU3zMA:10 a=zstS-IiYAAAA:8 a=5hbzxG5bqXrQ7DImFQYA:9 a=GYmLNYRCbkI10zIs:21 a=jVpJHBJ5O6Wg4IJD:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-29-39-24.hlrn.qwest.net ([174.29.39.24]:55096 helo=pokyo.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dGsNU-0003ge-T3; Fri, 02 Jun 2017 13:36:56 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 2/5] Let print_decimal_chars handle signed values Date: Fri, 02 Jun 2017 19:37:00 -0000 Message-Id: <20170602193651.3173-3-tom@tromey.com> In-Reply-To: <20170602193651.3173-1-tom@tromey.com> References: <20170602193651.3173-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dGsNU-0003ge-T3 X-Source-Sender: 174-29-39-24.hlrn.qwest.net (pokyo.Home) [174.29.39.24]:55096 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-SW-Source: 2017-06/txt/msg00071.txt.bz2 This changes print_decimal_chars to handle signed values. gdb/ChangeLog 2017-06-02 Tom Tromey PR exp/16225: * valprint.h (print_decimal_chars): Update. * valprint.c (maybe_negate_by_bytes): New function. (print_decimal_chars): Add "is_signed" argument. * printcmd.c (print_scalar_formatted): Update. --- gdb/ChangeLog | 8 ++++++++ gdb/printcmd.c | 3 ++- gdb/valprint.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- gdb/valprint.h | 2 +- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index bc2ac04..a529694 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,6 +1,14 @@ 2017-06-02 Tom Tromey PR exp/16225: + * valprint.h (print_decimal_chars): Update. + * valprint.c (maybe_negate_by_bytes): New function. + (print_decimal_chars): Add "is_signed" argument. + * printcmd.c (print_scalar_formatted): Update. + +2017-06-02 Tom Tromey + + PR exp/16225: * valprint.h (print_binary_chars, print_hex_chars): Update. * valprint.c (val_print_type_code_int): Update. (print_binary_chars): Add "zero_pad" argument. diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 66395d5..84f41f5 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -374,7 +374,8 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type, return; case 'u': case 'd': - print_decimal_chars (stream, valaddr, len, byte_order); + print_decimal_chars (stream, valaddr, len, !TYPE_UNSIGNED (type), + byte_order); return; case 't': print_binary_chars (stream, valaddr, len, byte_order, size > 0); diff --git a/gdb/valprint.c b/gdb/valprint.c index aa34b68..fcd74f1 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -1763,12 +1763,58 @@ print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr, } +/* Possibly negate the integer represented by BYTES. It contains LEN + bytes in the specified byte order. If the integer is negative, + copy it into OUT_VEC, negate it, and return true. Otherwise, do + nothing and return false. */ + +static bool +maybe_negate_by_bytes (const gdb_byte *bytes, unsigned len, + enum bfd_endian byte_order, + std::vector *out_vec) +{ + gdb_byte sign_byte; + if (byte_order == BFD_ENDIAN_BIG) + sign_byte = bytes[0]; + else + sign_byte = bytes[len - 1]; + if ((sign_byte & 0x80) == 0) + return false; + + out_vec->reserve (len); + + /* Compute -x == 1 + ~x. */ + if (byte_order == BFD_ENDIAN_LITTLE) + { + unsigned carry = 1; + for (unsigned i = 0; i < len; ++i) + { + unsigned tem = (0xff & ~bytes[i]) + carry; + (*out_vec)[i] = tem & 0xff; + carry = tem / 256; + } + } + else + { + unsigned carry = 1; + for (unsigned i = len; i > 0; --i) + { + unsigned tem = (0xff & ~bytes[i - 1]) + carry; + (*out_vec)[i - 1] = tem & 0xff; + carry = tem / 256; + } + } + + return true; +} + /* VALADDR points to an integer of LEN bytes. Print it in decimal on stream or format it in buf. */ void print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr, - unsigned len, enum bfd_endian byte_order) + unsigned len, bool is_signed, + enum bfd_endian byte_order) { #define TEN 10 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */ @@ -1784,6 +1830,14 @@ print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr, int dummy; int flip; + std::vector negated_bytes; + if (is_signed + && maybe_negate_by_bytes (valaddr, len, byte_order, &negated_bytes)) + { + fputs_filtered ("-", stream); + valaddr = negated_bytes.data (); + } + /* Base-ten number is less than twice as many digits as the base 16 number, which is 2 digits per byte. */ diff --git a/gdb/valprint.h b/gdb/valprint.h index 8bfad21..f71d4ab 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -138,7 +138,7 @@ extern void print_octal_chars (struct ui_file *, const gdb_byte *, unsigned int, enum bfd_endian); extern void print_decimal_chars (struct ui_file *, const gdb_byte *, - unsigned int, enum bfd_endian); + unsigned int, bool, enum bfd_endian); extern void print_hex_chars (struct ui_file *, const gdb_byte *, unsigned int, enum bfd_endian, bool); -- 2.9.3