From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Subject: [PATCH 09/16] gdb: make inferior::m_args an std::string
Date: Wed, 14 Jul 2021 00:55:13 -0400 [thread overview]
Message-ID: <20210714045520.1623120-10-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20210714045520.1623120-1-simon.marchi@polymtl.ca>
With the current code, both a NULL pointer and an empty string can mean
"no arguments". We don't need this distinction. Changing to a string
has the advantage that there is now a single state for that (an empty
string), which makes the code a bit simpler in my opinion.
Change-Id: Icdc622820f7869478791dbaa84b4a1c7fec21ced
---
gdb/elf-none-tdep.c | 6 +++---
gdb/fbsd-tdep.c | 6 +++---
gdb/infcmd.c | 4 ++--
gdb/inferior.h | 26 ++++++++------------------
gdb/linux-tdep.c | 6 +++---
gdb/procfs.c | 10 ++++------
6 files changed, 23 insertions(+), 35 deletions(-)
diff --git a/gdb/elf-none-tdep.c b/gdb/elf-none-tdep.c
index 0d94dc430403..1802ffb1ac09 100644
--- a/gdb/elf-none-tdep.c
+++ b/gdb/elf-none-tdep.c
@@ -49,9 +49,9 @@ elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
fname = lbasename (exe);
psargs = std::string (exe);
- const char *infargs = current_inferior ()->args ();
- if (infargs != nullptr)
- psargs += " " + std::string (infargs);
+ const std::string &infargs = current_inferior ()->args ();
+ if (!infargs.empty ())
+ psargs += ' ' + infargs;
/* All existing targets that handle writing out prpsinfo expect the
fname and psargs strings to be at least 16 and 80 characters long
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index 141d23662838..07cd844c818f 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -684,9 +684,9 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
const char *fname = lbasename (get_exec_file (0));
std::string psargs = fname;
- const char *infargs = current_inferior ()->args ();
- if (infargs != NULL)
- psargs = psargs + " " + infargs;
+ const std::string &infargs = current_inferior ()->args ();
+ if (!infargs.empty ())
+ psargs += ' ' + infargs;
note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
note_size, fname,
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index be6031d29364..2d0d6cc3e965 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -129,7 +129,7 @@ set_inferior_args_vector (int argc, char **argv)
{
gdb::array_view<char * const> args (argv, argc);
std::string n = construct_inferior_arguments (args);
- current_inferior ()->set_args (n.c_str ());
+ current_inferior ()->set_args (std::move (n));
}
/* Notice when `set args' is run. */
@@ -151,7 +151,7 @@ show_args_command (struct ui_file *file, int from_tty,
/* Note that we ignore the passed-in value in favor of computing it
directly. */
deprecated_show_value_hack (file, from_tty, c,
- current_inferior ()->args ());
+ current_inferior ()->args ().c_str ());
}
/* See gdbsupport/common-inferior.h. */
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 0b28e7e4766d..6c1e08a7671b 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -444,26 +444,18 @@ class inferior : public refcounted_object,
/* Set the argument string to use when running this inferior.
- Either nullptr or an empty string can be used to represent "no
- arguments". */
- void set_args (const char *args)
+ An empty string can be used to represent "no arguments". */
+ void set_args (std::string args)
{
- if (args != nullptr && args[0] != '\0')
- m_args = make_unique_xstrdup (args);
- else
- m_args.reset ();
+ m_args = std::move (args);
};
/* Get the argument string to use when running this inferior.
- The return value is always non-nullptr. No arguments is represented by
- an empty string. */
- const char *args () const
+ No arguments is represented by an empty string. */
+ const std::string &args () const
{
- if (m_args == nullptr)
- return "";
-
- return m_args.get ();
+ return m_args;
}
/* Set the inferior current working directory.
@@ -602,10 +594,8 @@ class inferior : public refcounted_object,
/* The list of continuations. */
std::list<std::function<void ()>> m_continuations;
- /* The arguments string to use when running.
-
- This is nullptr when there are not args. */
- gdb::unique_xmalloc_ptr<char> m_args;
+ /* The arguments string to use when running. */
+ std::string m_args;
/* The current working directory that will be used when starting
this inferior. */
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 99e868eed6e6..637d3d36a0be 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1847,12 +1847,12 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
- const char *infargs = current_inferior ()->args ();
+ const std::string &infargs = current_inferior ()->args ();
/* The arguments of the program. */
std::string psargs = fname.get ();
- if (infargs != NULL)
- psargs = psargs + " " + infargs;
+ if (!infargs.empty ())
+ psargs += ' ' + infargs;
strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
diff --git a/gdb/procfs.c b/gdb/procfs.c
index d764c95d422a..aa441284c7b8 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3603,7 +3603,6 @@ procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
char psargs[80] = {'\0'};
procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
gdb::unique_xmalloc_ptr<char> note_data;
- const char *inf_args;
enum gdb_signal stop_signal;
if (get_exec_file (0))
@@ -3613,14 +3612,13 @@ procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
strncpy (psargs, get_exec_file (0), sizeof (psargs));
psargs[sizeof (psargs) - 1] = 0;
- inf_args = current_inferior ()->args ();
- if (inf_args && *inf_args
- && (strlen (inf_args)
- < ((int) sizeof (psargs) - (int) strlen (psargs))))
+ const std::string &inf_args = current_inferior ()->args ();
+ if (!inf_args.empty () &&
+ inf_args.length () < ((int) sizeof (psargs) - (int) strlen (psargs)))
{
strncat (psargs, " ",
sizeof (psargs) - strlen (psargs));
- strncat (psargs, inf_args,
+ strncat (psargs, inf_args.c_str (),
sizeof (psargs) - strlen (psargs));
}
}
--
2.32.0
next prev parent reply other threads:[~2021-07-14 4:57 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-14 4:55 [PATCH 00/16] Bunch of commands related cleanups Simon Marchi
2021-07-14 4:55 ` [PATCH 01/16] gdb/testsuite: split gdb.python/py-parameter.exp in procs Simon Marchi
2021-07-14 4:55 ` [PATCH 02/16] gdb.base/setshow.exp: use save_vars to save/restore gdb_prompt Simon Marchi
2021-07-14 4:55 ` [PATCH 03/16] gdb.base/setshow.exp: split in procs Simon Marchi
2021-07-14 4:55 ` [PATCH 04/16] gdb.base/setshow.exp: fix duplicate test name Simon Marchi
2021-07-14 4:55 ` [PATCH 05/16] gdb: un-share set_inferior_cwd declaration Simon Marchi
2021-07-14 4:55 ` [PATCH 06/16] gdb: remove inferior::{argc,argv} Simon Marchi
2021-07-14 4:55 ` [PATCH 07/16] gdb: add setter/getter for inferior arguments Simon Marchi
2021-07-14 4:55 ` [PATCH 08/16] gdb: add setter/getter for inferior cwd Simon Marchi
2021-07-14 4:55 ` Simon Marchi [this message]
2021-07-14 4:55 ` [PATCH 10/16] gdb: make inferior::m_cwd an std::string Simon Marchi
2021-07-14 4:55 ` [PATCH 11/16] gdb: make inferior::m_terminal " Simon Marchi
2021-07-14 4:55 ` [PATCH 12/16] gdb: rename cfunc to simple_func Simon Marchi
2021-07-14 4:55 ` [PATCH 13/16] gdb: remove cmd_list_element::function::sfunc Simon Marchi
2021-07-28 19:10 ` Tom Tromey
2021-07-28 21:17 ` Simon Marchi
2021-07-29 17:33 ` Tom Tromey
2021-07-14 4:55 ` [PATCH 14/16] gdb/testsuite: test get/set value of unregistered Guile parameter Simon Marchi
2021-07-23 19:42 ` Simon Marchi
2021-07-14 4:55 ` [PATCH 15/16] gdb: make cmd_list_element var an optional union Simon Marchi
2021-07-14 12:08 ` Lancelot SIX
2021-07-14 17:12 ` Lancelot SIX
2021-07-14 19:22 ` Simon Marchi
2021-07-14 23:22 ` Lancelot SIX
2021-07-19 14:32 ` Simon Marchi
2021-07-19 19:52 ` Simon Marchi
2021-07-20 23:03 ` Lancelot SIX
2021-07-23 19:56 ` Simon Marchi
2021-07-23 20:46 ` Lancelot SIX
2021-07-23 21:15 ` Simon Marchi
2021-07-23 22:55 ` Lancelot SIX
2021-07-24 2:04 ` Simon Marchi
2021-07-28 20:13 ` Tom Tromey
2021-07-28 20:45 ` Lancelot SIX
2021-07-29 17:47 ` Tom Tromey
2021-07-29 20:12 ` Lancelot SIX
2021-07-30 2:09 ` Simon Marchi
2021-07-30 17:47 ` Lancelot SIX
2021-07-18 15:44 ` Lancelot SIX
2021-07-19 14:19 ` Simon Marchi
2021-07-19 20:58 ` Lancelot SIX
2021-07-28 19:47 ` Tom Tromey
2021-07-28 20:59 ` Simon Marchi
2021-07-29 17:41 ` Tom Tromey
2021-07-29 17:44 ` Simon Marchi
2021-07-29 17:49 ` Tom Tromey
2021-07-29 18:00 ` Simon Marchi
2021-07-14 4:55 ` [PATCH 16/16] gdb: make string-like set show commands use std::string variable Simon Marchi
2021-07-28 20:27 ` Tom Tromey
2021-07-28 21:03 ` Simon Marchi
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=20210714045520.1623120-10-simon.marchi@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
/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).