From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id oMWpGo68/mmXuyUAWB0awg (envelope-from ) for ; Sat, 09 May 2026 00:48:14 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=NIRpCO+7; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 580871E093; Sat, 09 May 2026 00:48:14 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.9 required=5.0 tests=ARC_SIGNED,ARC_VALID, BAYES_00,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id F27F71E093 for ; Sat, 09 May 2026 00:48:12 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5FF6E4BA2E14 for ; Sat, 9 May 2026 04:48:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5FF6E4BA2E14 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=NIRpCO+7 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 5E42B4BA543C for ; Sat, 9 May 2026 04:47:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E42B4BA543C Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E42B4BA543C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::82e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778302054; cv=none; b=YQ1JjdYt6MuW5bCOexsNlEtXaq8oJCElzajrZyfNQCQXgM8p7w347Z9+poDkeBqDsnLk04NFJTXsuYAdIdEI2JVR5zZ5uHqyPBUvushPmkLUSE7BcSEe4fRzmocQtRriyJikVEf3jYD3HpEgT83bZRefqVANZmt1QZX9SAEGYD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778302054; c=relaxed/simple; bh=K1F2M+iXUKE3KEL7+rgFDgKRJubayvERFkeWFfi2bEo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vWrn2odnc729+ppiQaf2HMIgeFJNvHNtRDR9EVE9MWVl/UudmVu7vhd6RsSBQyxqzM4dzRlWNmjDosqzbLcvzIHEcDROiBFwmituG9G83yeqz1VmVGsKofzdarOfQB9drYZu6u1y1h3k47Lf4BitD9/62qbV57x2bwd7Gkm2RlU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=NIRpCO+7 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E42B4BA543C Received: by mail-qt1-x82e.google.com with SMTP id d75a77b69052e-50d6b393d60so3651cf.0 for ; Fri, 08 May 2026 21:47:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778302054; x=1778906854; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Ojw5k+1bI1PUKjo8sMSB3ijd8kE+NUm5hR1HKmGKXHA=; b=NIRpCO+7EDxeBezlj93CrsxQYmLDzzmpj+5uRxT62tMloutn0l8KAVTlntxa9d+ZdC pilY+VVK6rCa4SuBKavkrDaYR8jUpw7i4YcZFnsmHbzJ+0q8ax+zP2Jmem0Z3RET4ZxK +c0VYepZjRhLaWliKWAJ482elq6Mk0GrWgL6Rl1ZJ0gx8mCCtCZAoL8S4BtOLZAiJ8Ro U5fWYX7X1sSNZ1s5LVz7/o5Joq6G48aQZdt7opNpD6N1H+/zN+T/K248X+Dkdd4sgp1n obzeu3ULHWjJ5rm8KRx+9TIwDL1gxJpWmcDBmySEScHLkWJYyq1ug/CNH3zsIKJ8RUTR 38iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778302054; x=1778906854; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Ojw5k+1bI1PUKjo8sMSB3ijd8kE+NUm5hR1HKmGKXHA=; b=jtvT8qvjJuJ/M57MvvR0Dw/eyLEjDsGHQ0cvo9VUjMd44Jl5s5DA8UG7din2pAwYDV 0HGyeooL3ZZHFqlu3tULKqcvFp98txXRLjlSMJ0C2mv1xx0mw7y8zqVkVvP3K/Z7i0T0 LI0/cpra3PKA55A9aF8xqEb6ZYoKSPvDhGZmaIBjKNgyY//5/IE2s05hyVlRTdbxgkSw diJBVs5hF8LLjwjYfuFP4jALKCn46rhTFu7soaguFnFsZcK1HMfq9UwHuI3hpKvk0Kis 0JSqX5vK1BnZA0cMTLn1G0O6gKlCC9jv6yrliPqybLiv0EaJJuuahMZPBRnClDVxjh+1 X2NQ== X-Gm-Message-State: AOJu0YwahHQUwdE/KVhhgw2r6h06VpkzfC5meSC/6j8i4Vdu5+dG2NE/ ApNC6Ht6rpLt52TTodv0chAVE6VRuuanJyJWfh9WAhU9yj4pgONmdhiV5U0UuMmOd9IOme7flq/ LnxkpY0PH X-Gm-Gg: AeBDievgktiPXCbA4/Rc8MKgWTZ2DEAiVzWB6iSyuunfGN/qvf/Dmp5NjCYuSF3NuzM MPCdEzzfKI7EytE947IHaWKpZFCwe4r1gTkXrngK1ePUpCPNc+2SBtQ9oX5r5AlgXuPXgGObskv prvLrxLb15frQxyRROmavVZf3OJaO89b1EbyY740kNexU7Pyinaof8+E0bUbTgawrb0xKTpeO/K blNh6zkqRuYa92PNS9G+ljqBPbWWdaoJhtJqTiZs2ZytY38hB9qfbbbuabO8toMAL3V0Cl/rrDR cnj3aR4s9SPSseCB6W627wRlDM6c6I0zd+se+0TdM0ToMHXS6fczI2iTo12cAkLPwPJhAdVQ0Af Vd9/CQH9TGC0yPWah5cAYMiBb2ow8gai6uPFd7EUk6cvp0VG3yRR2U+FBZlg/Ww2g/D5nLHJ4na rspZhNKzstuucRdV8X02GrFImpK4jmgb8iUC+f1K6Du34RhOoQgnoW0/wxH9UAJ3W4+SS3p0kTI N8nDACwS83OMkINbHkRD4cbCP7ZDypRVOU= X-Received: by 2002:ac8:5705:0:b0:50f:d03e:6482 with SMTP id d75a77b69052e-5149f2aa522mr7818301cf.4.1778302053264; Fri, 08 May 2026 21:47:33 -0700 (PDT) Received: from ztop.w.home.arpa (ool-45706b36.dyn.optonline.net. [69.112.107.54]) by smtp.gmail.com with ESMTPSA id af79cd13be357-907b986c0dfsm391204385a.3.2026.05.08.21.47.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2026 21:47:32 -0700 (PDT) From: Zander Work To: gdb-patches@sourceware.org Cc: guinevere@redhat.com, tom@tromey.com, Zander Work Subject: [PATCH v2] Use "output-radix" setting to format function offsets Date: Fri, 8 May 2026 21:45:44 -0700 Message-ID: <20260509044543.558625-2-zdw@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Updates since v1: * Addressed feedback in the test suite by more robustly handling the GDB I/O and improved the regex patterns used to match on the offset values. * Updated the accompanying test program to have a minimum level of complexity to avoid compiler optimizations trimming out too much function body, and fixed the formatting and copyright date * Added a comment on the `format_pc_offset()` implementation and addressed comments in that function. I believe the only open items are: * Should MI consumers have the string radix-formatted offset value, or continue having an int value (this is the current impl in the patch)? * I still need to do an FSF copyright assignment. Please let me know if I missed anything else to address. Thanks! --- gdb/disasm.c | 18 ++++++++++--- gdb/printcmd.c | 2 +- gdb/testsuite/gdb.base/radix.c | 35 ++++++++++++++++++++++++++ gdb/testsuite/gdb.base/radix.exp | 43 ++++++++++++++++++++++++++++++++ gdb/valprint.c | 13 ++++++++++ gdb/valprint.h | 6 +++++ 6 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 gdb/testsuite/gdb.base/radix.c diff --git a/gdb/disasm.c b/gdb/disasm.c index 81c466c188a..5d19dea72b6 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -375,10 +375,20 @@ gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn m_uiout->field_string ("func-name", name, function_name_style.style ()); /* For negative offsets, avoid displaying them as +-N; the sign of - the offset takes the place of the "+" here. */ - if (offset >= 0) - m_uiout->text ("+"); - m_uiout->field_signed ("offset", offset); + the offset takes the place of the "+" here. For MI consumers, + emit the integer value; otherwise, print the formatted offset based + on the current 'output-radix'. */ + if (m_uiout->is_mi_like_p ()) + { + if (offset >= 0) + m_uiout->text ("+"); + m_uiout->field_signed ("offset", offset); + } + else + { + std::string s = format_pc_offset (offset); + m_uiout->field_string ("offset", s.c_str ()); + } m_uiout->text (">:\t"); } else diff --git a/gdb/printcmd.c b/gdb/printcmd.c index ae498395436..5fb8c666447 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -567,7 +567,7 @@ print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr, gdb_puts ("<", stream); fputs_styled (name.c_str (), function_name_style.style (), stream); if (offset != 0) - gdb_printf (stream, "%+d", offset); + gdb_puts (format_pc_offset (offset).c_str (), stream); /* Append source filename and line number if desired. Give specific line # of this addr, if we have it; else line # of the nearest symbol. */ diff --git a/gdb/testsuite/gdb.base/radix.c b/gdb/testsuite/gdb.base/radix.c new file mode 100644 index 00000000000..8a4d1287208 --- /dev/null +++ b/gdb/testsuite/gdb.base/radix.c @@ -0,0 +1,35 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include + +static int v; + +int +main (void) +{ + v = 0; + + puts ("hello world"); + + printf ("this is another string\n"); + + v += 3; + + return v; +} diff --git a/gdb/testsuite/gdb.base/radix.exp b/gdb/testsuite/gdb.base/radix.exp index 7a4320bbf36..4b8e2d74b49 100644 --- a/gdb/testsuite/gdb.base/radix.exp +++ b/gdb/testsuite/gdb.base/radix.exp @@ -17,6 +17,11 @@ # This file was written by Fred Fish. (fnf@cygnus.com) # And rewritten by Michael Chastain (mec.gnu@mindspring.com) +standard_testfile + +if {[build_executable "failed to prepare" $testfile $srcfile debug]} { + return -1 +} # Start with a fresh gdb. @@ -189,3 +194,41 @@ gdb_test "set radix 7" \ gdb_test "show output-radix" \ "Default output radix for printing of values is 10\\." \ "output radix unchanged after rejection through set radix command" + +with_test_prefix "pc offset radix" { + clean_restart $testfile + + if { ![runto_main] } { + return -1 + } + + proc test_pc_offset_radix { oradix offset_re } { + global gdb_prompt + + gdb_test "set output-radix $oradix" \ + "Output radix now set to decimal $oradix.*\\." + + set test "x/i with output-radix $oradix" + + gdb_test_multiple "x/i \$pc" "$test 1" { + -re -wrap ":.*" { + pass $gdb_test_name + } + } + + gdb_test "ni 3" "\[0-9\]+.*" "Next instruction for radix $oradix" + + gdb_test_multiple "x/i \$pc" "$test 2" { + -re -wrap ":.*" { + pass $gdb_test_name + } + } + } + + test_pc_offset_radix 8 {0[0-7]+} + test_pc_offset_radix 10 {[1-9][0-9]*} + test_pc_offset_radix 16 {0x[0-9a-f]+} + + gdb_test "set output-radix 10" "Output radix now set to decimal 10.*\\." \ + "restore output-radix" +} diff --git a/gdb/valprint.c b/gdb/valprint.c index 62b1b33bb66..d0d0472ca7a 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -171,6 +171,19 @@ show_output_radix (struct ui_file *file, int from_tty, value); } +/* See valprint.h. */ + +std::string +format_pc_offset (int offset) +{ + std::string sign = (offset < 0) ? "-" : "+"; + + std::string body = int_string (offset < 0 ? -offset : offset, output_radix, + 0, 0, 1); + + return sign + body; +} + /* By default we print arrays without printing the index of each element in the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ diff --git a/gdb/valprint.h b/gdb/valprint.h index 0ce3e0781f6..5511707cba3 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -320,6 +320,12 @@ extern int build_address_symbolic (struct gdbarch *, int *line, int *unmapped); +/* Format OFFSET, the offset portion of a "" display, as + a string with an explicit sign prefix ("+" or "-"). The numeric + portion is rendered using the current "output-radix". */ + +extern std::string format_pc_offset (int offset); + /* Check to see if RECURSE is greater than or equal to the allowed printing max-depth (see 'set print max-depth'). If it is then print an ellipsis expression to STREAM and return true, otherwise return false. -- 2.54.0.563.g4f69b47b94-goog