From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2EE743858D39 for ; Fri, 4 Mar 2022 10:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2EE743858D39 Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-204-4O9q_M7ON_mwd31QM5qBMg-1; Fri, 04 Mar 2022 05:50:36 -0500 X-MC-Unique: 4O9q_M7ON_mwd31QM5qBMg-1 Received: by mail-wr1-f71.google.com with SMTP id p9-20020adf9589000000b001e333885ac1so3207633wrp.10 for ; Fri, 04 Mar 2022 02:50:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FkKbayy+yiMoTfgs+XD/9+ubICuCXqEvPBGC+qVAJSk=; b=BXei1re5ytaQmm52ryeXhqSSB9CuioKvMvdV2ePCeN1w5X+fbA1ruRB3VubHDVSJ0z 51WyLE0rUd/RfeMAzMMQCp5ohAOe5/+L6djmqnuUnSFjFo6Kci8zFVoFdngeofdLAPgo 7bBpjqe+F0nszvhOcYgcqVMm/6Y0uWGqDRTGCOVflvAzzjkEztTP9tHCyGMd4OnGPohn 5O3Nm/xje8FjzLn2bvWb0XT4j1/x5f6JBn0EdWcOfRQd3ObXqej9Ij+u78qgPAY4h4KD T7ulWw02XM/jOUQKDvKxHzmwhTDoAdmWr9A6pK+GFWIt1SCQaUqIbuMkhYl053mZtSJd NnbA== X-Gm-Message-State: AOAM530sj6G21LZFw02kHfN+dLmaPAa9IQhAjfsoe4aBy/KIKZTcsM/2 SgH3PnH0cHWKCyVPEUEeqML/KtGZyoIT18xUr45HhwudHl0xfi8CtmJ0iyOmDVjft6PfmTtgUAa mz85WlhMvDubKFVKQfe/WLIUAO2dHrHme4dItFPNDCBFKulw5/nuitTqvl2KBRUuFWN4YOVA2+g == X-Received: by 2002:adf:d0c8:0:b0:1f0:771f:20d8 with SMTP id z8-20020adfd0c8000000b001f0771f20d8mr1798726wrh.210.1646391035180; Fri, 04 Mar 2022 02:50:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzmYKBSGoYloVTDrSdKMw1pPY9Ktwu/LT4FWNgZT9hsMqxW+9216TYCNEwk24hRn03ON6ZQnQ== X-Received: by 2002:adf:d0c8:0:b0:1f0:771f:20d8 with SMTP id z8-20020adfd0c8000000b001f0771f20d8mr1798701wrh.210.1646391034697; Fri, 04 Mar 2022 02:50:34 -0800 (PST) Received: from localhost (host86-169-131-29.range86-169.btcentralplus.com. [86.169.131.29]) by smtp.gmail.com with ESMTPSA id p12-20020a056000018c00b001f079518150sm1118527wrx.93.2022.03.04.02.50.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 02:50:34 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2] gdb/python: add gdb.Architecture.format_address Date: Fri, 4 Mar 2022 10:50:31 +0000 Message-Id: <20220304105031.2706582-1-aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20220211161721.3252422-1-aburgess@redhat.com> References: <20220211161721.3252422-1-aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Mar 2022 10:50:40 -0000 From: Andrew Burgess Since v1: - All the doc changes have been addressed (these have already been approved), - Rebased to current master, - Added an additional test for when 'set print symbol-filename' is on. Thanks, Andrew --- Add a new method gdb.Architecture.format_address, which is a wrapper around GDB's print_address function. This method takes an address, and returns a string with the format: ADDRESS Where, ADDRESS is the original address, formatted as hexadecimal, and padded with zeros on the left up to the width of an address in the current architecture. SYMBOL is a symbol whose address range covers ADDRESS, and OFFSET is the offset from SYMBOL to ADDRESS in decimal. If there's no SYMBOL whose address range covers ADDRESS, then the part is not included. This is useful if a user wants to write a Python script that pretty-print addresses, the user no longer needs to do manual symbol lookup, and additionally, things like the zero padding on addresses will be consistent with the builtin GDB behaviour. --- gdb/NEWS | 5 ++++ gdb/doc/python.texi | 36 ++++++++++++++++++++++++++++ gdb/python/py-arch.c | 31 ++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-arch.exp | 28 ++++++++++++++++++++++ 4 files changed, 100 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index dc2cac1871b..d3883febf82 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -206,6 +206,11 @@ GNU/Linux/LoongArch loongarch*-*-linux* state information, or None, if there is no such additional information. + ** New function gdb.Architecture.format_address(ADDRESS), that + formats ADDRESS as 'address ', this is the same + format that GDB uses when printing address, symbol, and offset + information from the disassembler. + * New features in the GDB remote stub, GDBserver ** GDBserver is now supported on OpenRISC GNU/Linux. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4d9e77bf12c..0ec08f4994e 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6033,6 +6033,42 @@ @code{gdb.Architecture}. @end defun +@defun Architecture.format_address (@var{address}) +Return a string in the format @samp{@var{addr} +<@var{symbol}+@var{offset}>}, where @var{addr} is @var{address} +formatted in hexadecimal, @var{symbol} is the symbol whose address is +the nearest to @var{address} and below it in memory, and @var{offset} +is the offset from @var{symbol} to @var{address} in decimal. + +If no suitable @var{symbol} was found, then the +<@var{symbol}+@var{offset}> part is not included in the returned +string, instead the returned string will just contain the +@var{address} formatted as hexadecimal. How far @value{GDBN} looks +back for a suitable symbol can be controlled with @kbd{set print +max-symbolic-offset} (@pxref{Print Settings}). + +Additionally, the returned string can include file name and line +number information when @kbd{set print symbol-filename on} +(@pxref{Print Settings}), in this case the format of the returned +string is @samp{@var{addr} <@var{symbol}+@var{offset}> at +@var{filename}:@var{line-number}}. + +In all cases, the @var{addr} component will be padded with leading +zeros based on the width of an address for the current architecture. + +This method uses the same mechanism for formatting address, symbol, +and offset information as core @value{GDBN} does in commands such as +@kbd{disassemble}. + +Here are some examples of the possible string formats: + +@smallexample +0x00001042 +0x00001042 +0x00001042 +@end smallexample +@end defun + @node Registers In Python @subsubsection Registers In Python @cindex Registers In Python diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index 0f273b344e4..95ae931e73e 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -348,6 +348,31 @@ gdbpy_all_architecture_names (PyObject *self, PyObject *args) return list.release (); } +/* Implement gdb.architecture.format_address(ADDR). Provide access to + GDB's print_address function from Python. The returned address will + have the format '0x..... '. */ + +static PyObject * +archpy_format_address (PyObject *self, PyObject *args, PyObject *kw) +{ + static const char *keywords[] = { "address", nullptr }; + PyObject *addr_obj; + CORE_ADDR addr; + struct gdbarch *gdbarch = nullptr; + + ARCHPY_REQUIRE_VALID (self, gdbarch); + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &addr_obj)) + return nullptr; + + if (get_addr_from_python (addr_obj, &addr) < 0) + return nullptr; + + string_file buf; + print_address (gdbarch, addr, &buf); + return PyString_FromString (buf.c_str ()); +} + void _initialize_py_arch (); void _initialize_py_arch () @@ -391,6 +416,12 @@ group GROUP-NAME." }, METH_NOARGS, "register_groups () -> Iterator.\n\ Return an iterator over all of the register groups in this architecture." }, + { "format_address", (PyCFunction) archpy_format_address, + METH_VARARGS | METH_KEYWORDS, + "format_address (ADDRESS) -> String.\n\ +Format ADDRESS, an address within the currently selected inferior's\n\ +address space, as a string. The format of the returned string is\n\ +'ADDRESS ' without the quotes." }, {NULL} /* Sentinel */ }; diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp index b55778b0b72..5272d9c2173 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -127,3 +127,31 @@ foreach a $arch_names b $py_arch_names { } } gdb_assert { $lists_match } + +# Check the gdb.Architecture.format_address method. +set main_addr [get_hexadecimal_valueof "&main" "UNKNOWN"] +set next_addr [format 0x%x [expr $main_addr + 1]] + +foreach_with_prefix symbol_filename { on off } { + gdb_test_no_output "set print symbol-filename ${symbol_filename}" + + if { $symbol_filename == "on" } { + set filename_pattern " at \[^\r\n\]+/py-arch.c:$decimal" + } else { + set filename_pattern "" + } + + gdb_test "python print(\"Got: \" + gdb.selected_inferior().architecture().format_address($main_addr))" \ + "Got: $main_addr " \ + "gdb.Architecture.format_address, result should have no offset" + + gdb_test "python print(\"Got: \" + gdb.selected_inferior().architecture().format_address($next_addr))" \ + "Got: $next_addr " \ + "gdb.Architecture.format_address, result should have an offset" +} + +if {![is_address_zero_readable]} { + gdb_test "python print(\"Got: \" + gdb.selected_inferior().architecture().format_address(0))" \ + "Got: 0x0" \ + "gdb.Architecture.format_address for address 0" +} -- 2.25.4