From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 2558C385701E for ; Fri, 24 Jul 2020 09:57:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2558C385701E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wr1-x42b.google.com with SMTP id r12so7712397wrj.13 for ; Fri, 24 Jul 2020 02:57:58 -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=OtQ4rDFDKc416QkebHahlV4a2tTPas6crtESc+uKh54=; b=gNtiRx/glUUJJ4RCxX+pSFjMLFVYWd3KrNwjVBHDPtLaIGXZoZQtGwbI0kXR88+U5j HgAVzMwfbcDqGhkwdYVdbGaXngqVWqXAWk9P/UTUUwEath3mA+nLiZBJ/9gidL/eD89j od77UswqGoKhvBj51lxWWp1Cb91+nZx3CQdJpmMpdp++08zkIBbJndHwWsb+x3VGouAF 9OUqdsRtkQ0MzHVg7fkGX2gLCHFTqwZkPesQl7uTmfFEyS6xt8oXOlyPI8UiWGrBAWZR 55zzxGLx8UhwfCH4avafI5nR4aiidpzGdK8u11LHzKqGaihx1BvRxcLbjvNwG0RYTJKE e4fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OtQ4rDFDKc416QkebHahlV4a2tTPas6crtESc+uKh54=; b=TCOLtqU9zI9HGO2/JYCOjUcWdBOex78WM9u0JMVbRv7hwJETPAk0uKv+cK2SihSGwi AnvE1a+whySmj5kgY0yr10Y+9zyIrISZmy89tJdL6/Y93ThItrGEMaue7Hr7yK9oAjgZ AeXPxC901SEvA2P75iN+0LUeo89moPFWZ7vxYy0dgiiJX6vGwMt6/391hp+pLTyjKjQp Y4BFVkWLzYKyOGvDsa0uNdSKzj08eBzhhKgzb0Epqqv6lYax+QwOTnVDaKwV4RSxAWf8 1HVfABUfjugfrTkf5eTjsmefCqGiZMjk2UAiXvSQMiEPuk8f60aGbzVLukbdUdd0vaKY L2Ug== X-Gm-Message-State: AOAM531NSGtY0QnUsl7Y46IMGN2GL9QJCrIZbpdSDkPD/h4o4PvLK2Eq 5cK2qOixTqod4qGeYq5rPomMIV2N8no= X-Google-Smtp-Source: ABdhPJyY6FGrhlp4+zphST4whlrfh6aCrWNu3Q83hl2uzUzXoz/Z0iZ+NDWTPpsz50z4F2Ktk7ANow== X-Received: by 2002:adf:eecb:: with SMTP id a11mr8011800wrp.339.1595584676824; Fri, 24 Jul 2020 02:57:56 -0700 (PDT) Received: from localhost (host86-134-151-238.range86-134.btcentralplus.com. [86.134.151.238]) by smtp.gmail.com with ESMTPSA id s4sm786474wre.53.2020.07.24.02.57.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 02:57:56 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Subject: [PATCH 1/2] gdb: Add a find method for RegisterDescriptorIterator Date: Fri, 24 Jul 2020 10:57:51 +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=-9.9 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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, 24 Jul 2020 09:57:59 -0000 Adds a new method 'find' to the gdb.RegisterDescriptorIterator class, this allows gdb.RegisterDescriptor objects to be looked up directly by register name rather than having to iterate over all registers. This will be of use for a later commit. I've documented the new function in the manual, but I don't think a NEWS entry is required here, as, since the last release, the whole register descriptor mechanism is new, and is already mentioned in the NEWS file. gdb/ChangeLog: * python/py-registers.c: Add 'user-regs.h' include. (register_descriptor_iter_find): New function. (register_descriptor_iterator_object_methods): New static global methods array. (register_descriptor_iterator_object_type): Add pointer to methods array. gdb/testsuite/ChangeLog: * gdb.python/py-arch-reg-names.exp: Add additional tests. gdb/doc/ChangeLog: * python.texi (Registers In Python): Document new find function. --- gdb/ChangeLog | 9 ++++ gdb/doc/ChangeLog | 4 ++ gdb/doc/python.texi | 9 ++++ gdb/python/py-registers.c | 44 ++++++++++++++++++- gdb/testsuite/ChangeLog | 4 ++ .../gdb.python/py-arch-reg-names.exp | 15 +++++++ 6 files changed, 84 insertions(+), 1 deletion(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4fb994ca6d9..4c0432372ab 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5755,6 +5755,15 @@ The name of this register. @end defvar +It is also possible to lookup a register descriptor based on its name +using the following @code{gdb.RegisterDescriptorIterator} function: + +@defun RegisterDescriptorIterator.find ( @var{name} ) +Returns a @code{gdb.RegisterDescriptor} for the register with name +@var{name}, or @code{None} if there is no register with that name. +@end defun + + Python code can also request from a @code{gdb.Architecture} information about the set of register groups available on a given architecture diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index f64ca3c401b..fffe3ecb1e6 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -23,6 +23,7 @@ #include "disasm.h" #include "reggroups.h" #include "python-internal.h" +#include "user-regs.h" #include /* Token to access per-gdbarch data related to register descriptors. */ @@ -337,6 +338,38 @@ gdbpy_register_descriptor_iter_next (PyObject *self) while (true); } +/* Implement: + + gdb.RegisterDescriptorIterator.find (self, name) -> gdb.RegisterDescriptor + + Look up a descriptor for register with NAME. If no matching register is + found then return None. */ + +static PyObject * +register_descriptor_iter_find (PyObject *self, PyObject *args, PyObject *kw) +{ + static const char *keywords[] = { "name", NULL }; + const char *register_name = NULL; + + register_descriptor_iterator_object *iter_obj + = (register_descriptor_iterator_object *) self; + struct gdbarch *gdbarch = iter_obj->gdbarch; + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, + ®ister_name)) + return NULL; + + if (register_name != NULL && *register_name != '\0') + { + int regnum = user_reg_map_name_to_regnum (gdbarch, register_name, + strlen (register_name)); + if (regnum >= 0) + return gdbpy_get_register_descriptor (gdbarch, regnum).release (); + } + + Py_RETURN_NONE; +} + /* Initializes the new Python classes from this file in the gdb module. */ int @@ -377,6 +410,15 @@ gdbpy_initialize_registers () (PyObject *) ®ister_descriptor_iterator_object_type)); } +static PyMethodDef register_descriptor_iterator_object_methods [] = { + { "find", (PyCFunction) register_descriptor_iter_find, + METH_VARARGS | METH_KEYWORDS, + "registers (name) -> gdb.RegisterDescriptor.\n\ +Return a register descriptor for the register NAME, or None if no register\n\ +with that name exists in this iterator." }, + {NULL} /* Sentinel */ +}; + PyTypeObject register_descriptor_iterator_object_type = { PyVarObject_HEAD_INIT (NULL, 0) "gdb.RegisterDescriptorIterator", /*tp_name*/ @@ -405,7 +447,7 @@ PyTypeObject register_descriptor_iterator_object_type = { 0, /*tp_weaklistoffset */ gdbpy_register_descriptor_iter, /*tp_iter */ gdbpy_register_descriptor_iter_next, /*tp_iternext */ - 0 /*tp_methods */ + register_descriptor_iterator_object_methods /*tp_methods */ }; static gdb_PyGetSetDef gdbpy_register_descriptor_getset[] = { diff --git a/gdb/testsuite/gdb.python/py-arch-reg-names.exp b/gdb/testsuite/gdb.python/py-arch-reg-names.exp index 8dd34ef5fd2..891da1b6af5 100644 --- a/gdb/testsuite/gdb.python/py-arch-reg-names.exp +++ b/gdb/testsuite/gdb.python/py-arch-reg-names.exp @@ -104,3 +104,18 @@ gdb_py_test_silent_cmd \ " raise gdb.GdbError (\"miss-matched objects\")" \ "\004" ] \ "check names and objects match" 1 + +# Ensure that the '.find' method on the iterator returns the same +# Python object as we got from the iterator's list of descriptors. +gdb_py_test_silent_cmd \ + [multi_line \ + "python" \ + "def check_regs (arch, regs):" \ + " for r in (regs):" \ + " if (arch.registers ().find (r.name) != r):" \ + " raise gdb.GdbError (\"object miss-match\")" \ + "end" ] \ + "build check_obj function" 0 +gdb_py_test_silent_cmd \ + "python check_regs (arch, arch.registers (\"general\"))" \ + "ensure find gets expected descriptors" 1 -- 2.25.4