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 07808385840E for ; Fri, 11 Feb 2022 16:17:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 07808385840E Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-632-IE7614cENl2tPwMbgDNZtA-1; Fri, 11 Feb 2022 11:17:25 -0500 X-MC-Unique: IE7614cENl2tPwMbgDNZtA-1 Received: by mail-wm1-f69.google.com with SMTP id n3-20020a05600c294300b0037bc685b717so2703247wmd.8 for ; Fri, 11 Feb 2022 08:17:25 -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:mime-version :content-transfer-encoding; bh=pHOhrqqVx7R9EPAUhEhVwPJMP7q+1S3FnUGLffljRSs=; b=zoLkOIvtNVAp68f8HDfg2zgnWR0d8FkeZyN/1F2ZJtdmVGtLT7EobNxrcIMhzdeArT HfZlk4GrYXMlyk67UUEbT92ruoIEUfAPNMtf9kkQPfD7eFuwO7ZLzVNSBt3Oh7/49HOz Ic8X7p1jdz0WFHf0eG0B9m02C7MmMvcD3ZSJBhkbuR/mchm8uLRdO8p2umWGaf8udcrz upWg9y70RXp3GTivqLsU0nYup8LHjbV4Sm6AJUm63B8N+XCNpXTGT4d16MB/iQl3RlwG Ky1o+dLNODY2aO9ojnkywmpFEC8jxU0wIJps9mufimWuPTcyaU1onBsZcomOILjgtVFa FGFQ== X-Gm-Message-State: AOAM532cRYMgv6Ds5wIBfh28JiLJ6zuaItNKH0ziHxObk0LBX9l7NR8Y K31XIX6ngIr17GOIcT+F8JGvbgCser6UTEHpB7UPadJ4U72Qlw4U7TLAOJKzEPem2s0qD0u6aZZ oDLGkkio0M3cTOzGsA2w/SDWy7w/2S1sgIj3TyFALTdMOammz8lOOXVYSBjUfkCBcc95Y1hEgbw == X-Received: by 2002:a5d:48c9:: with SMTP id p9mr1872128wrs.1.1644596243809; Fri, 11 Feb 2022 08:17:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJw/Zm7wY/2I35JR+tX4zChVNkFy/D9MxQHXmO+lYvl8wco5asnI7Rdm/WDRLT6CNoQjgg9BaA== X-Received: by 2002:a5d:48c9:: with SMTP id p9mr1872104wrs.1.1644596243420; Fri, 11 Feb 2022 08:17:23 -0800 (PST) Received: from localhost (host86-134-151-224.range86-134.btcentralplus.com. [86.134.151.224]) by smtp.gmail.com with ESMTPSA id b7sm11068077wrp.23.2022.02.11.08.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Feb 2022 08:17:23 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/python: add gdb.Architecture.format_address Date: Fri, 11 Feb 2022 16:17:21 +0000 Message-Id: <20220211161721.3252422-1-aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 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=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, 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, 11 Feb 2022 16:17:28 -0000 From: Andrew Burgess 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 that represents the address within the currently selected inferior's address space, formatted like this '0x.... '. If there's no symbol to associate with the address then the returned string just contains the address (this is just print_address behaviour). 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. --- gdb/NEWS | 5 +++++ gdb/doc/python.texi | 19 +++++++++++++++++ gdb/python/py-arch.c | 31 ++++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-arch.exp | 15 ++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index e173d38c3a1..4f4f0c2af6d 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -187,6 +187,11 @@ GNU/Linux/LoongArch loongarch*-*-linux* GNU/Linux/OpenRISC or1k*-*-linux* + ** New function gdb.Architecture.format_address(ADDRESS), which + takes an address in the currently selected inferior's address + space, and returns a string representing the address. The format + of the returned string is '0x.... '. + *** Changes in GDB 11 * The 'set disassembler-options' command now supports specifying options diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index c1a3f5f2a7e..50443f7b704 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6016,6 +6016,25 @@ @code{gdb.Architecture}. @end defun +@defun Architecture.format_address (@var{address}) +Returns @var{address}, an address within the currently selected +inferior's address space, formatted as a string. When a suitable +symbol can be found to associate with @var{address} this will be +included in the returned string, formatted like this: + +@smallexample +0x00001042 +@end smallexample + +If there is no symbol that @value{GDBN} can find to associate with +@var{address} then the returned string will just contain +@var{address}. + +If @var{address} is not accessible within the current inferior's +address space, this function will still return a string containing +@var{address}. +@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..c4854033d8c 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -127,3 +127,18 @@ 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"] +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" +set next_addr [format 0x%x [expr $main_addr + 1]] +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