From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id kMPXH2uDTWLSMAAAWB0awg (envelope-from ) for ; Wed, 06 Apr 2022 08:11:23 -0400 Received: by simark.ca (Postfix, from userid 112) id 7FD371F344; Wed, 6 Apr 2022 08:11:23 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 5C4251E787 for ; Wed, 6 Apr 2022 08:11:22 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 01C203857820 for ; Wed, 6 Apr 2022 12:11:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 01C203857820 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1649247082; bh=g+wF9B5yxCRfPYClXPSKwRnXt0g6QUAFW/lYwy4qYkM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=aGViHktwxeWhayVGkaW32rxSQIqfzn2h3gt+oQz+esakRq66I3OsbQ5zppeDMzFIO Ru1jmKFlCICluyP3p/J7D9UFp90brLbQ29I/4T7LcPY247xF/CPlvQt3K9DOZpwXDS bqVrlyFe003/UTNtwvr7DD0Nj+XdNkZRknK14NiA= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id F414B3857836 for ; Wed, 6 Apr 2022 12:05:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F414B3857836 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-107-_LU9wNC4MeqjEs78kxQjeg-1; Wed, 06 Apr 2022 08:05:15 -0400 X-MC-Unique: _LU9wNC4MeqjEs78kxQjeg-1 Received: by mail-wr1-f72.google.com with SMTP id k20-20020adfc714000000b001e305cd1597so424400wrg.19 for ; Wed, 06 Apr 2022 05:05:15 -0700 (PDT) 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=g+wF9B5yxCRfPYClXPSKwRnXt0g6QUAFW/lYwy4qYkM=; b=6FQMKBBIm9/gxPZu9TLHGXP5ZU+MunmBabkHswW2cN1KD1BDi/YQ0Ktv5hgZKpCXR8 4wByYZMCIHt43B0RLp4TFpVTc4SjbVB8OIumjpgUu2cOefMoiQaLIJEQkY9nQxVOKJTX zzdvK3n/5N4hu8bGGnvXHA7WlpU743Nf7BW9kec8rBeMNfwPbJ7r0ihlscHiIUuOQM3I Ifud/GwWOgYnNq47PQPGaurtUAjs6xhWHkPaBjJ+Ze061jCapT7IW2nwjQnPf8ZX574N JF69XGj8qHoQ4Jnk25novwfv+PO6TeDHd4Ab6LXFWkT2NGQeaxbZPTKfEgWZi1N1dgR3 AfHQ== X-Gm-Message-State: AOAM530aArdPyoLjQpVOwP5dbwqYTt0uA1V+l1Vqz0ppWGQ/HDa7QKkH 4XwiisH0hVBh+IrjihhYTNHXianhf7LW7NA+RFGARyNmsTFD4UNgFHdcEU6+FB9BX8xYcQBEFtP XlOYxBXSuuxhMWMD9QJ67WIFaChwNYEMVw2ZoeSEIyd1RgAe17gSPZw7V+Nqu0Pj5OVygGfUi3A == X-Received: by 2002:a05:6000:1545:b0:205:a82d:8007 with SMTP id 5-20020a056000154500b00205a82d8007mr6570291wry.88.1649246713825; Wed, 06 Apr 2022 05:05:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzArMjXAEwpzP8aarD/MZgKotLcolc0Lwxs0uzlVFFarAvF/guRSIahy+qgSE9UpgFhDudPpw== X-Received: by 2002:a05:6000:1545:b0:205:a82d:8007 with SMTP id 5-20020a056000154500b00205a82d8007mr6570237wry.88.1649246713174; Wed, 06 Apr 2022 05:05:13 -0700 (PDT) Received: from localhost (host86-169-131-113.range86-169.btcentralplus.com. [86.169.131.113]) by smtp.gmail.com with ESMTPSA id h8-20020a05600c350800b0038cc9096507sm5239057wmq.3.2022.04.06.05.05.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:05:12 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCHv2 11/16] gdb: remove reggroup_next and reggroup_prev Date: Wed, 6 Apr 2022 13:04:44 +0100 Message-Id: <3a1ef96c90157a4193af462837b63e90c0fcd6c8.1649246539.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: 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-default=true 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: , From: Andrew Burgess via Gdb-patches Reply-To: Andrew Burgess Cc: Andrew Burgess Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Add a new function gdbarch_reggroups that returns a reference to a vector containing all the reggroups for an architecture. Make use of this function throughout GDB instead of the existing reggroup_next and reggroup_prev functions. Finally, delete the reggroup_next and reggroup_prev functions. Most of these changes are pretty straight forward, using range based for loops instead of the old style look using reggroup_next. There are two places where the changes are less straight forward. In gdb/python/py-registers.c, the register group iterator needed to change slightly. As the iterator is tightly coupled to the gdbarch, I just fetch the register group vector from the gdbarch when needed, and use an index counter to find the next item from the vector when needed. In gdb/tui/tui-regs.c the tui_reg_next and tui_reg_prev functions are just wrappers around reggroup_next and reggroup_prev respectively. I've just inlined the logic of the old functions into the tui functions. As the tui function had its own special twist (wrap around behaviour) I think this is OK. There should be no user visible changes after this commit. --- gdb/completer.c | 6 +- gdb/infcmd.c | 14 ++--- gdb/python/py-registers.c | 17 +++--- gdb/regcache-dump.c | 6 +- gdb/reggroups.c | 118 ++++++++------------------------------ gdb/reggroups.h | 12 ++-- gdb/tui/tui-regs.c | 65 ++++++++++++--------- 7 files changed, 84 insertions(+), 154 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index 63b4cd22d24..f4b5917095d 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -1819,11 +1819,7 @@ reg_or_group_completer_1 (completion_tracker &tracker, if ((targets & complete_reggroup_names) != 0) { - struct reggroup *group; - - for (group = reggroup_next (gdbarch, NULL); - group != NULL; - group = reggroup_next (gdbarch, group)) + for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) { name = reggroup_name (group); if (strncmp (word, name, len) == 0) diff --git a/gdb/infcmd.c b/gdb/infcmd.c index b76345d8426..9abb91f2faa 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -2295,17 +2295,17 @@ registers_info (const char *addr_exp, int fpregs) /* A register group? */ { - struct reggroup *group; - - for (group = reggroup_next (gdbarch, NULL); - group != NULL; - group = reggroup_next (gdbarch, group)) + const struct reggroup *group = nullptr; + for (const struct reggroup *g : gdbarch_reggroups (gdbarch)) { /* Don't bother with a length check. Should the user enter a short register group name, go with the first group that matches. */ - if (strncmp (start, reggroup_name (group), end - start) == 0) - break; + if (strncmp (start, reggroup_name (g), end - start) == 0) + { + group = g; + break; + } } if (group != NULL) { diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index 58d196c37b4..7a9d2e22403 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -65,8 +65,8 @@ extern PyTypeObject register_descriptor_object_type struct reggroup_iterator_object { PyObject_HEAD - /* The last register group returned. Initially this will be NULL. */ - const struct reggroup *reggroup; + /* The index into GROUPS for the next group to return. */ + std::vector::size_type index; /* Pointer back to the architecture we're finding registers for. */ struct gdbarch *gdbarch; @@ -225,17 +225,18 @@ gdbpy_reggroup_iter_next (PyObject *self) { reggroup_iterator_object *iter_obj = (reggroup_iterator_object *) self; - struct gdbarch *gdbarch = iter_obj->gdbarch; - const reggroup *next_group = reggroup_next (gdbarch, iter_obj->reggroup); - if (next_group == NULL) + const std::vector &groups + = gdbarch_reggroups (iter_obj->gdbarch); + if (iter_obj->index >= groups.size ()) { PyErr_SetString (PyExc_StopIteration, _("No more groups")); return NULL; } - iter_obj->reggroup = next_group; - return gdbpy_get_reggroup (iter_obj->reggroup).release (); + const reggroup *group = groups[iter_obj->index]; + iter_obj->index++; + return gdbpy_get_reggroup (group).release (); } /* Return a new gdb.RegisterGroupsIterator over all the register groups in @@ -252,7 +253,7 @@ gdbpy_new_reggroup_iterator (struct gdbarch *gdbarch) ®group_iterator_object_type); if (iter == NULL) return NULL; - iter->reggroup = NULL; + iter->index = 0; iter->gdbarch = gdbarch; return (PyObject *) iter; } diff --git a/gdb/regcache-dump.c b/gdb/regcache-dump.c index 2a2578558f7..409a868be76 100644 --- a/gdb/regcache-dump.c +++ b/gdb/regcache-dump.c @@ -192,11 +192,7 @@ class register_dump_groups : public register_dump else { const char *sep = ""; - struct reggroup *group; - - for (group = reggroup_next (m_gdbarch, NULL); - group != NULL; - group = reggroup_next (m_gdbarch, group)) + for (const struct reggroup *group : gdbarch_reggroups (m_gdbarch)) { if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group)) { diff --git a/gdb/reggroups.c b/gdb/reggroups.c index 262bf49cea8..4607c4b674c 100644 --- a/gdb/reggroups.c +++ b/gdb/reggroups.c @@ -100,7 +100,7 @@ struct reggroups /* Return a reference to the list of all groups. */ - const std::vector & + const std::vector & groups () const { return m_groups; @@ -108,7 +108,7 @@ struct reggroups private: /* The register groups. */ - std::vector m_groups; + std::vector m_groups; }; static struct gdbarch_data *reggroups_data; @@ -146,62 +146,15 @@ reggroups_init (struct obstack *obstack) return groups; } -/* A register group iterator. */ - -struct reggroup * -reggroup_next (struct gdbarch *gdbarch, const struct reggroup *last) -{ - /* Don't allow this function to be called during architecture - creation. If there are no groups, use the default groups list. */ - struct reggroups *groups - = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data); - gdb_assert (groups != nullptr); - gdb_assert (groups->size () > 0); - - /* Return the first/next reggroup. */ - if (last == nullptr) - return groups->groups ().front (); - for (int i = 0; i < groups->size (); ++i) - { - if (groups->groups ()[i] == last) - { - if (i + 1 < groups->size ()) - return groups->groups ()[i + 1]; - else - return nullptr; - } - } - - return nullptr; -} - /* See reggroups.h. */ - -struct reggroup * -reggroup_prev (struct gdbarch *gdbarch, const struct reggroup *curr) +const std::vector +gdbarch_reggroups (struct gdbarch *gdbarch) { - /* Don't allow this function to be called during architecture - creation. If there are no groups, use the default groups list. */ struct reggroups *groups = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data); gdb_assert (groups != nullptr); gdb_assert (groups->size () > 0); - - /* Return the first/next reggroup. */ - if (curr == nullptr) - return groups->groups ().back (); - for (int i = groups->size () - 1; i >= 0; --i) - { - if (groups->groups ()[i] == curr) - { - if (i - 1 >= 0) - return groups->groups ()[i - 1]; - else - return nullptr; - } - } - - return nullptr; + return groups->groups (); } /* Is REGNUM a member of REGGROUP? */ @@ -239,11 +192,7 @@ default_register_reggroup_p (struct gdbarch *gdbarch, int regnum, const reggroup * reggroup_find (struct gdbarch *gdbarch, const char *name) { - struct reggroup *group; - - for (group = reggroup_next (gdbarch, NULL); - group != NULL; - group = reggroup_next (gdbarch, group)) + for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) { if (strcmp (name, reggroup_name (group)) == 0) return group; @@ -256,52 +205,35 @@ reggroup_find (struct gdbarch *gdbarch, const char *name) static void reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file) { - struct reggroup *group = NULL; + static constexpr const char *fmt = " %-10s %-10s\n"; - do + gdb_printf (file, fmt, "Group", "Type"); + + for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) { /* Group name. */ - { - const char *name; - - if (group == NULL) - name = "Group"; - else - name = reggroup_name (group); - gdb_printf (file, " %-10s", name); - } + const char *name = reggroup_name (group); /* Group type. */ - { - const char *type; - - if (group == NULL) - type = "Type"; - else - { - switch (reggroup_type (group)) - { - case USER_REGGROUP: - type = "user"; - break; - case INTERNAL_REGGROUP: - type = "internal"; - break; - default: - internal_error (__FILE__, __LINE__, _("bad switch")); - } - } - gdb_printf (file, " %-10s", type); - } + const char *type; + + switch (reggroup_type (group)) + { + case USER_REGGROUP: + type = "user"; + break; + case INTERNAL_REGGROUP: + type = "internal"; + break; + default: + internal_error (__FILE__, __LINE__, _("bad switch")); + } /* Note: If you change this, be sure to also update the documentation. */ - - gdb_printf (file, "\n"); - group = reggroup_next (gdbarch, group); + gdb_printf (file, fmt, name, type); } - while (group != NULL); } static void diff --git a/gdb/reggroups.h b/gdb/reggroups.h index bc87a7325f0..959191c4180 100644 --- a/gdb/reggroups.h +++ b/gdb/reggroups.h @@ -53,14 +53,10 @@ extern void reggroup_add (struct gdbarch *gdbarch, struct reggroup *group); extern const char *reggroup_name (const struct reggroup *reggroup); extern enum reggroup_type reggroup_type (const struct reggroup *reggroup); -/* Iterators for the architecture's register groups. Pass in NULL, returns - the first (for next), or last (for prev) group. Pass in a group, - returns the next or previous group, or NULL when either the end or the - beginning of the group list is reached. */ -extern struct reggroup *reggroup_next (struct gdbarch *gdbarch, - const struct reggroup *last); -extern struct reggroup *reggroup_prev (struct gdbarch *gdbarch, - const struct reggroup *curr); +/* Return the list of all register groups for GDBARCH. */ +extern const std::vector + gdbarch_reggroups (struct gdbarch *gdbarch); + /* Find a reggroup by name. */ extern const reggroup *reggroup_find (struct gdbarch *gdbarch, const char *name); diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c index b968947fa1c..acffc75238a 100644 --- a/gdb/tui/tui-regs.c +++ b/gdb/tui/tui-regs.c @@ -514,34 +514,48 @@ tui_data_item_window::rerender (WINDOW *handle, int field_width) (void) wstandend (handle); } -/* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap - around behaviour. Will never return nullptr. If CURRENT_GROUP is - nullptr (e.g. if the tui register window has only just been displayed - and has no current group selected), then the first register group will - be returned. */ +/* Helper for "tui reg next", returns the next register group after + CURRENT_GROUP in the register group list for GDBARCH, with wrap around + behaviour. + + If CURRENT_GROUP is nullptr (e.g. if the tui register window has only + just been displayed and has no current group selected) or the currently + selected register group can't be found (e.g. if the architecture has + changed since the register window was last updated), then the last + register group will be returned. */ static const reggroup * tui_reg_next (const reggroup *current_group, struct gdbarch *gdbarch) { - const reggroup *group = reggroup_next (gdbarch, current_group); - if (group == NULL) - group = reggroup_next (gdbarch, NULL); - return group; + const std::vector &groups = gdbarch_reggroups (gdbarch); + auto it = std::find (groups.begin (), groups.end (), current_group); + if (it != groups.end ()) + it++; + if (it == groups.end ()) + return groups.front (); + return *it; } -/* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap - around behaviour. Will never return nullptr. If CURRENT_GROUP is - nullptr (e.g. if the tui register window has only just been displayed - and has no current group selected), then the last register group will - be returned. */ +/* Helper for "tui reg prev", returns the register group previous to + CURRENT_GROUP in the register group list for GDBARCH, with wrap around + behaviour. + + If CURRENT_GROUP is nullptr (e.g. if the tui register window has only + just been displayed and has no current group selected) or the currently + selected register group can't be found (e.g. if the architecture has + changed since the register window was last updated), then the last + register group will be returned. */ static const reggroup * tui_reg_prev (const reggroup *current_group, struct gdbarch *gdbarch) { - const reggroup *group = reggroup_prev (gdbarch, current_group); - if (group == NULL) - group = reggroup_prev (gdbarch, NULL); - return group; + const std::vector &groups = gdbarch_reggroups (gdbarch); + auto it = std::find (groups.rbegin (), groups.rend (), current_group); + if (it != groups.rend ()) + it++; + if (it == groups.rend ()) + return groups.back (); + return *it; } /* Implement the 'tui reg' command. Changes the register group displayed @@ -555,7 +569,6 @@ tui_reg_command (const char *args, int from_tty) if (args != NULL) { - const reggroup *group, *match = NULL; size_t len = strlen (args); /* Make sure the curses mode is enabled. */ @@ -569,6 +582,7 @@ tui_reg_command (const char *args, int from_tty) if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->is_visible ()) tui_regs_layout (); + const reggroup *match = nullptr; const reggroup *current_group = TUI_DATA_WIN->get_current_group (); if (strncmp (args, "next", len) == 0) match = tui_reg_next (current_group, gdbarch); @@ -579,9 +593,7 @@ tui_reg_command (const char *args, int from_tty) /* This loop matches on the initial part of a register group name. If this initial part in ARGS matches only one register group then the switch is made. */ - for (group = reggroup_next (gdbarch, NULL); - group != NULL; - group = reggroup_next (gdbarch, group)) + for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) { if (strncmp (reggroup_name (group), args, len) == 0) { @@ -599,19 +611,16 @@ tui_reg_command (const char *args, int from_tty) } else { - const reggroup *group; - int first; - gdb_printf (_("\"tui reg\" must be followed by the name of " "either a register group,\nor one of 'next' " "or 'prev'. Known register groups are:\n")); - for (first = 1, group = reggroup_next (gdbarch, NULL); - group != NULL; - first = 0, group = reggroup_next (gdbarch, group)) + bool first = true; + for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) { if (!first) gdb_printf (", "); + first = false; gdb_printf ("%s", reggroup_name (group)); } -- 2.25.4