From: Andrew Burgess <andrew.burgess@embecosm.com>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/6] gdb: make use of std::string in utils.c
Date: Tue, 7 Sep 2021 15:12:21 +0100 [thread overview]
Message-ID: <20210907141221.GS2581@embecosm.com> (raw)
In-Reply-To: <692bd212-465d-aa25-338f-ce239d0c57e2@polymtl.ca>
* Simon Marchi <simon.marchi@polymtl.ca> [2021-08-19 14:41:41 -0400]:
>
>
> On 2021-08-19 5:49 a.m., Andrew Burgess wrote:
> > Replace use of manual string management (malloc/free) with std::string
> > when creating commands in utils.c. Things are a little bit messy as
> > creating the prefix commands (using add_basic_prefix_cmd and
> > add_show_prefix_cmd), don't copy the doc string, while creating the
> > actual set/show commands does copy the doc string, this explains the
> > extra xstrdup when creating the prefix commands.
>
> Indeed, all of this is really not consistent. I guess we would need to
> create versions of the add_*_cmd that take a
> gdb::unique_xmalloc_pointer<char> and set
> cmd_list_element::doc_allocated.
>
> > There should be no user visible changes after this commit.
> > ---
> > gdb/utils.c | 58 ++++++++++++++++++++++++-----------------------------
> > 1 file changed, 26 insertions(+), 32 deletions(-)
> >
> > diff --git a/gdb/utils.c b/gdb/utils.c
> > index 3916ae5a1c9..143c2eddd70 100644
> > --- a/gdb/utils.c
> > +++ b/gdb/utils.c
> > @@ -508,72 +508,66 @@ add_internal_problem_command (struct internal_problem *problem)
> > {
> > struct cmd_list_element **set_cmd_list;
> > struct cmd_list_element **show_cmd_list;
> > - char *set_doc;
> > - char *show_doc;
> >
> > set_cmd_list = XNEW (struct cmd_list_element *);
> > show_cmd_list = XNEW (struct cmd_list_element *);
> > *set_cmd_list = NULL;
> > *show_cmd_list = NULL;
> >
> > - set_doc = xstrprintf (_("Configure what GDB does when %s is detected."),
> > - problem->name);
> > + std::string set_doc
> > + = string_printf (_("Configure what GDB does when %s is detected."),
> > + problem->name);
> >
> > - show_doc = xstrprintf (_("Show what GDB does when %s is detected."),
> > - problem->name);
> > + std::string show_doc
> > + = string_printf (_("Show what GDB does when %s is detected."),
> > + problem->name);
> >
> > - add_basic_prefix_cmd (problem->name, class_maintenance, set_doc,
> > - set_cmd_list,
> > + add_basic_prefix_cmd (problem->name, class_maintenance,
> > + xstrdup (set_doc.c_str ()), set_cmd_list,
> > 0/*allow-unknown*/, &maintenance_set_cmdlist);
> >
> > - add_show_prefix_cmd (problem->name, class_maintenance, show_doc,
> > - show_cmd_list,
> > + add_show_prefix_cmd (problem->name, class_maintenance,
> > + xstrdup (show_doc.c_str ()), show_cmd_list,
> > 0/*allow-unknown*/, &maintenance_show_cmdlist);
>
> I guess you could keep using xstrprintf for these to avoid the extra
> copy... but what you have LGTM either way.
I've taken your advice. Below is what I pushed.
Thanks,
Andrew
--
commit 747656685b3e8477868478cd927fbb2834937aff
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date: Tue Aug 17 13:29:22 2021 +0100
gdb: make use of std::string in utils.c
Replace some of the manual string management (malloc/free) with
std::string when creating commands in utils.c.
Things are a little bit messy as, creating the prefix commands (using
add_basic_prefix_cmd and add_show_prefix_cmd), doesn't copy the doc
string, while creating the actual set/show commands (using
add_setshow_enum_cmd) does copy the doc string.
As a result, I have retained the use of xstrprintf when creating the
prefix command doc strings, but switched to using std::string when
creating the actual set/show commands.
There should be no user visible changes after this commit.
diff --git a/gdb/utils.c b/gdb/utils.c
index 0009cb10d87..0a7c270b40d 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -508,19 +508,21 @@ add_internal_problem_command (struct internal_problem *problem)
{
struct cmd_list_element **set_cmd_list;
struct cmd_list_element **show_cmd_list;
- char *set_doc;
- char *show_doc;
set_cmd_list = XNEW (struct cmd_list_element *);
show_cmd_list = XNEW (struct cmd_list_element *);
*set_cmd_list = NULL;
*show_cmd_list = NULL;
- set_doc = xstrprintf (_("Configure what GDB does when %s is detected."),
- problem->name);
-
- show_doc = xstrprintf (_("Show what GDB does when %s is detected."),
- problem->name);
+ /* The add_basic_prefix_cmd and add_show_prefix_cmd functions take
+ ownership of the string passed in, which is why we don't need to free
+ set_doc and show_doc in this function. */
+ const char *set_doc
+ = xstrprintf (_("Configure what GDB does when %s is detected."),
+ problem->name);
+ const char *show_doc
+ = xstrprintf (_("Show what GDB does when %s is detected."),
+ problem->name);
add_basic_prefix_cmd (problem->name, class_maintenance, set_doc,
set_cmd_list,
@@ -532,48 +534,42 @@ add_internal_problem_command (struct internal_problem *problem)
if (problem->user_settable_should_quit)
{
- set_doc = xstrprintf (_("Set whether GDB should quit "
- "when an %s is detected."),
- problem->name);
- show_doc = xstrprintf (_("Show whether GDB will quit "
- "when an %s is detected."),
- problem->name);
+ std::string set_quit_doc
+ = string_printf (_("Set whether GDB should quit when an %s is "
+ "detected."), problem->name);
+ std::string show_quit_doc
+ = string_printf (_("Show whether GDB will quit when an %s is "
+ "detected."), problem->name);
add_setshow_enum_cmd ("quit", class_maintenance,
internal_problem_modes,
&problem->should_quit,
- set_doc,
- show_doc,
+ set_quit_doc.c_str (),
+ show_quit_doc.c_str (),
NULL, /* help_doc */
NULL, /* setfunc */
NULL, /* showfunc */
set_cmd_list,
show_cmd_list);
-
- xfree (set_doc);
- xfree (show_doc);
}
if (problem->user_settable_should_dump_core)
{
- set_doc = xstrprintf (_("Set whether GDB should create a core "
- "file of GDB when %s is detected."),
- problem->name);
- show_doc = xstrprintf (_("Show whether GDB will create a core "
- "file of GDB when %s is detected."),
- problem->name);
+ std::string set_core_doc
+ = string_printf (_("Set whether GDB should create a core file of "
+ "GDB when %s is detected."), problem->name);
+ std::string show_core_doc
+ = string_printf (_("Show whether GDB will create a core file of "
+ "GDB when %s is detected."), problem->name);
add_setshow_enum_cmd ("corefile", class_maintenance,
internal_problem_modes,
&problem->should_dump_core,
- set_doc,
- show_doc,
+ set_core_doc.c_str (),
+ show_core_doc.c_str (),
NULL, /* help_doc */
NULL, /* setfunc */
NULL, /* showfunc */
set_cmd_list,
show_cmd_list);
-
- xfree (set_doc);
- xfree (show_doc);
}
}
next prev parent reply other threads:[~2021-09-07 14:12 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-19 9:49 [PATCH 0/6] Display GDB backtrace for internal errors Andrew Burgess
2021-08-19 9:49 ` [PATCH 1/6] gdb: use bool instead of int in struct internal_problem Andrew Burgess
2021-08-19 18:33 ` Simon Marchi
2021-09-07 14:11 ` Andrew Burgess
2021-08-19 9:49 ` [PATCH 2/6] gdb: make use of std::string in utils.c Andrew Burgess
2021-08-19 18:41 ` Simon Marchi
2021-09-07 14:12 ` Andrew Burgess [this message]
2021-08-19 9:49 ` [PATCH 3/6] gdb: Add a dependency between gdb and libbacktrace Andrew Burgess
2021-08-19 18:43 ` Simon Marchi
2021-08-27 17:44 ` Tom Tromey
2021-08-30 20:33 ` Andrew Burgess
2021-08-19 9:49 ` [PATCH 4/6] Copy in libbacktrace from gcc Andrew Burgess
2021-08-27 17:46 ` Tom Tromey
2021-08-30 20:34 ` Andrew Burgess
2021-08-19 9:49 ` [PATCH 5/6] gdb: use libbacktrace to create a better backtrace for fatal signals Andrew Burgess
2021-08-19 18:58 ` Simon Marchi
2021-08-19 9:49 ` [PATCH 6/6] gdb: print backtrace for internal error/warning Andrew Burgess
2021-08-19 19:01 ` Simon Marchi
2021-08-30 14:16 ` [PATCH 0/6] Display GDB backtrace for internal errors Tom de Vries
2021-08-30 20:35 ` Andrew Burgess
2021-08-31 11:17 ` Florian Weimer
2021-09-28 11:26 ` [PUSHED " Andrew Burgess
2021-09-28 11:26 ` [PUSHED 1/6] top-level configure: setup target_configdirs based on repository Andrew Burgess
2021-09-28 11:26 ` [PUSHED 2/6] gdb: Add a dependency between gdb and libbacktrace Andrew Burgess
2021-09-28 11:26 ` [PUSHED 4/6] src-release.sh: add libbacktrace to GDB_SUPPORT_DIRS Andrew Burgess
2021-09-28 11:26 ` [PUSHED 5/6] gdb: use libbacktrace to create a better backtrace for fatal signals Andrew Burgess
2021-09-28 18:55 ` Pedro Alves
2021-09-29 8:21 ` Andrew Burgess
2021-09-29 3:09 ` Simon Marchi
2021-09-29 9:56 ` Andrew Burgess
2021-09-28 11:26 ` [PUSHED 6/6] gdb: print backtrace for internal error/warning Andrew Burgess
2021-09-28 12:26 ` Eli Zaretskii
2021-09-29 8:34 ` Andrew Burgess
2021-09-28 12:20 ` [PUSHED 3/6] Copy in libbacktrace from gcc Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210907141221.GS2581@embecosm.com \
--to=andrew.burgess@embecosm.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@polymtl.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).