From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 93C15385840B for ; Wed, 13 Oct 2021 21:59:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 93C15385840B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42e.google.com with SMTP id g25so13027607wrb.2 for ; Wed, 13 Oct 2021 14:59:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+yDKqc13S8U+SM6aK+lu1suYQ6t8kPrqvT5JPHwCQFs=; b=hMD8qGouCHAvGZFzhnUYHXBI4iRp34Zf9VKe3+yBEXYVM8OoIxeJwRM0fUTDfSNgOW 951J4aly2C+FKBqQ4JgYheJEdoHv4mkuTluGJjs+8SBlVT3dz2/wOw7oxenCAbwGEdM7 pL1+2FaHCRua3lOPhokvgpeqIBFm5vJwfk+ssxriaGcOG4VoMQ760m3m6INBipE7x3p+ D8wwZFJUHFMSg01YHvt/6MB0yPopQE1NgTwAlXz0FqmBLhVHa/DwL2gXKgBRHNTOC7zi I+mQ9stXTb2vSWIAcDBmuGwGMy6lSBhgyQqWyqA8/RiKEc6bXGZUmAKdDezTJh+TGmbO JdaQ== 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=+yDKqc13S8U+SM6aK+lu1suYQ6t8kPrqvT5JPHwCQFs=; b=l3wNXnsyEkflY6ZHbpVAt/26TTFd1ihZ46w4sW3dHg7rD0u+DQR8kVb9kRjVWTmeZi boMPITv8w/Fkmw7ziAsiL5B3h014LvfEEk2g4qOIaNWRSo6GG8nE2bhtAWxHdZ4D2/b6 hDxVo0Ae0D6idHWDwu7Uj5LeeBkImajxm5XFbNFJKjgIRt/q/5jU9r5w9vJnD2FRr6Qi u2s144kuZJpeChvNzN+DZ5+M0bOwvJikDbhidEIMAC6Z1i+V++rW0ygM4kbaOyPbReya VH7X4xQ8E1LCN7epmrEQXh/kTD3cresNdmQmxcr9M7OXSVfTtT/ZKjmLXPp7MqgGMOIP nn+g== X-Gm-Message-State: AOAM530iYUy8DnvOJeFoFPImZBnxBpSUw76z26whHoYJBRbCfrdhrMhO rF7Y0pmXAcE/0BTAdPSRrFf2hJfDFageOw== X-Google-Smtp-Source: ABdhPJyBOZcWkMbnx7wjGxQjqugrwKbwvP+lzzgHd9cQgJCXfVtjIVhAaqs9YofyNwp3M5tans38Lg== X-Received: by 2002:a05:600c:2303:: with SMTP id 3mr15519008wmo.123.1634162355408; Wed, 13 Oct 2021 14:59:15 -0700 (PDT) Received: from localhost (host212-140-123-151.range212-140.btcentralplus.com. [212.140.123.151]) by smtp.gmail.com with ESMTPSA id s1sm491680wmc.47.2021.10.13.14.59.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Oct 2021 14:59:15 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Subject: [PATCH 2/5] gdb/python: new gdb.architecture_names function Date: Wed, 13 Oct 2021 22:59:07 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: Wed, 13 Oct 2021 21:59:19 -0000 Add a new function to the Python API, gdb.architecture_names(). This function returns a list containing all of the supported architecture names within the current build of GDB. The values returned in this list are all of the possible values that can be returned from gdb.Architecture.name(). --- gdb/NEWS | 4 +++ gdb/doc/python.texi | 9 +++++ gdb/python/py-arch.c | 23 +++++++++++++ gdb/python/python-internal.h | 1 + gdb/python/python.c | 4 +++ gdb/testsuite/gdb.python/py-arch.exp | 51 ++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index bd26d2b1ec2..d001a03145d 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -45,6 +45,10 @@ maint show internal-warning backtrace event is triggered once GDB decides it is going to exit, but before GDB starts to clean up its internal state. + ** New function gdb.architecture_names(), which returns a list + containing all of the possible Architecture.name() values. Each + entry is a string. + *** 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 15bf9dc3e21..04192f906c8 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -557,6 +557,14 @@ related prompts are prohibited from being changed. @end defun +@defun gdb.architecture_names () +Return a list containing all of the architecture names that the +current build of @value{GDBN} supports. Each architecture name is a +string. The names returned in this list are the same names as are +returned from @code{gdb.Architecture.name ()} +(@pxref{gdbpy_architecture_name,,Architecture.name ()}). +@end defun + @node Exception Handling @subsubsection Exception Handling @cindex python exceptions @@ -5834,6 +5842,7 @@ A @code{gdb.Architecture} class has the following methods: +@anchor{gdbpy_architecture_name} @defun Architecture.name () Return the name (string value) of the architecture. @end defun diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index 66f2d28b94a..3e7970ab764 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -271,6 +271,29 @@ archpy_register_groups (PyObject *self, PyObject *args) return gdbpy_new_reggroup_iterator (gdbarch); } +/* Implementation of gdb.architecture_names(). Return a list of all the + BFD architecture names that GDB understands. */ + +PyObject * +gdbpy_all_architecture_names (PyObject *self, PyObject *args) +{ + gdbpy_ref<> list (PyList_New (0)); + if (list == nullptr) + return nullptr; + + std::vector name_list = gdbarch_printable_names (); + for (const char *name : name_list) + { + gdbpy_ref <> py_name (PyString_FromString (name)); + if (py_name == nullptr) + return nullptr; + if (PyList_Append (list.get (), py_name.get ()) < 0) + return nullptr; + } + + return list.release (); +} + void _initialize_py_arch (); void _initialize_py_arch () diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 022d4a67172..2ad3bc944a7 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -469,6 +469,7 @@ PyObject *objfpy_get_xmethods (PyObject *, void *); PyObject *gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw); PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch); +PyObject *gdbpy_all_architecture_names (PyObject *self, PyObject *args); PyObject *gdbpy_new_register_descriptor_iterator (struct gdbarch *gdbarch, const char *group_name); diff --git a/gdb/python/python.c b/gdb/python/python.c index 264f7c88ed6..6c1baa167d9 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2299,6 +2299,10 @@ Set the value of the convenience variable $NAME." }, Register a TUI window constructor." }, #endif /* TUI */ + { "architecture_names", gdbpy_all_architecture_names, METH_NOARGS, + "architecture_names () -> List.\n\ +Return a list of all the architecture names GDB understands." }, + {NULL, NULL, 0, NULL} }; diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp index 4f971127197..415fbd475b0 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -62,3 +62,54 @@ if { ![is_address_zero_readable] } { gdb_test "python arch.disassemble(0, 0)" ".*gdb\.MemoryError.*" \ "test bad memory access" } + +# Test for gdb.architecture_names(). First we're going to grab the +# complete list of architecture names using the 'complete' command. +set arch_names [] +gdb_test_no_output "set max-completions unlimited" +gdb_test_multiple "complete set architecture " "" { + -re "complete set architecture\[^\r\n\]+\r\n" { + exp_continue + } + -re "^set architecture \(\[^\r\n\]+\)\r\n" { + set arch $expect_out(1,string) + if { "$arch" != "auto" } { + set arch_names [lappend arch_names $arch] + } + exp_continue + } + -re "^$gdb_prompt $" { + gdb_assert { [llength $arch_names] > 0 } + } +} + +# Now find all of the architecture names using Python. +set py_arch_names [] +gdb_test_no_output "python all_arch = gdb.architecture_names()" +gdb_test_no_output "python all_arch.sort()" +gdb_test_multiple "python print(\"\\n\".join((\"Arch: %s\" % a) for a in all_arch))" "" { + -re "python \[^\r\n\]+\r\n" { + exp_continue + } + -re "^Arch: \(\[^\r\n\]+\)\r\n" { + set arch $expect_out(1,string) + set py_arch_names [lappend py_arch_names $arch] + exp_continue + } + -re "$gdb_prompt $" { + gdb_assert { [llength $py_arch_names] > 0 } + } +} + +# Check the two lists of architecture names are the same length, and +# that the list contents all match. +gdb_assert { [llength $arch_names] == [llength $py_arch_names] } +set lists_match true +foreach a $arch_names b $py_arch_names { + if { $a != $b } { + set lists_match false + verbose -log "Mismatch is architecture list '$a' != '$b'" + break + } +} +gdb_assert { $lists_match } -- 2.25.4