From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by sourceware.org (Postfix) with ESMTPS id 887DB3858D35 for ; Fri, 24 Jul 2020 23:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 887DB3858D35 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-x441.google.com with SMTP id r4so6681861wrx.9 for ; Fri, 24 Jul 2020 16:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=bHjh3PFmn+M0ODf0/WrlT7lIrskHIoI3ByzNkKeeJ0U=; b=Cu70iFswoI+rJiLSrtHR9IwRgSEvr/WwjoJaIoFCaaKiadyjWcKywaIzNvfzopp+jj FClNHTyXLHY8pjnxM1pQyrMEW1DjnvTuw0vYFYVtRQfm33B0EV3bVo75M4e/SzRmf/mU hyxtN8Eo930yy9x/7K9AvQCUjtGn8HZoLA+lmL96u8ZjGMXpaWkdv71/S2Ec71njsYML pTS7zMoVvdQJ0yxFP9LsU6k1oVni+E7x6fFZajUhxx2pet+tMktOvembsuWt1ulUAEC3 ggERgOj3atmjQI3rbetb5vnOPfspyp58ZMJwLHJVoRhhwqqRPPWu6xXQQhnyirWlJLET CReQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=bHjh3PFmn+M0ODf0/WrlT7lIrskHIoI3ByzNkKeeJ0U=; b=Zt8H8ROejUFDqtUmLvy4AyBlysRG2EbaISLNkAhnWhbxO5AnvAAu6+47fBqCfr/ZvZ 1o//VCsMmGRj3ffWSOpt7l54WqSCkZD5/8/dwVrusC19Xl6/0Bv6c5O3JESVkBVeyW2/ UdLN30bUPnADjcpdCXOVSDTyM7dltFagg1pOEheS9B3gIe9kBu04DzBIfjXuOYomQaUA dfcuridTHhXWzqmonGKoMKfbSTWAy/PQH26rWFmKiI9mL2oK76Qn4YwluwDMwGQ97ODP uCjtZ1vMdiRF12nI5Is405QVp8xBG9hF9x2J004AerNQEslmOFy2idj+CO9MB2l2xdcq 50Yw== X-Gm-Message-State: AOAM530WR3cCJlLjMnKTqL4Tcd/3+njTp+2nsyW+Uh92tkNzESz2o6+I 6LNSkgyHBzaHZqUSmIk4Yh1M97VnxbU= X-Google-Smtp-Source: ABdhPJzRkaIzYWCg31ejjxJ5ZFTFlmukaZEAJQtxPhtskyBqsuczsEIRY7VSihnwK/0bqblUT16AWg== X-Received: by 2002:adf:e690:: with SMTP id r16mr11313307wrm.249.1595632627573; Fri, 24 Jul 2020 16:17:07 -0700 (PDT) Received: from localhost (host86-134-151-238.range86-134.btcentralplus.com. [86.134.151.238]) by smtp.gmail.com with ESMTPSA id e23sm8504646wme.35.2020.07.24.16.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 16:17:06 -0700 (PDT) Date: Sat, 25 Jul 2020 00:17:05 +0100 From: Andrew Burgess To: Eli Zaretskii Cc: gdb-patches@sourceware.org Subject: Re: [PATCH 1/2] gdb: Add a find method for RegisterDescriptorIterator Message-ID: <20200724231705.GQ853475@embecosm.com> References: <83mu3pdwzy.fsf@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <83mu3pdwzy.fsf@gnu.org> X-Operating-System: Linux/5.6.15-200.fc31.x86_64 (x86_64) X-Uptime: 00:16:26 up 6 days, 8:31, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] 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 23:17:10 -0000 * Eli Zaretskii [2020-07-24 13:32:17 +0300]: > > From: Andrew Burgess > > Date: Fri, 24 Jul 2020 10:57:51 +0100 > > > > +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} ) > ^ ^ > Please remove these extra blanks, they are against our coding > conventions. > > > +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 > > Is it necessary or useful to say what kind of data type is NAME? Or > will this be obvious to any Python programmer? Thanks for the feedback. The patch below includes updated documentation. How's this? Thanks, Andrew -- commit efe6a55ba25e088c67cde777bc8d524281356bb2 Author: Andrew Burgess Date: Wed Jul 22 14:02:30 2020 +0100 gdb: Add a find method for RegisterDescriptorIterator 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. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4fb994ca6d9..c9dc1ff3b71 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}) +Takes @var{name} as an argument, which must be a string, and returns a +@code{gdb.RegisterDescriptor} for the register with that 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