gdb: make inferior::m_args an std::string

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
This commit is contained in:
Simon Marchi
2021-06-25 17:54:55 -04:00
parent 90cc31c9e5
commit fd2dec2a45
6 changed files with 23 additions and 35 deletions

View File

@ -49,9 +49,9 @@ elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
fname = lbasename (exe); fname = lbasename (exe);
psargs = std::string (exe); psargs = std::string (exe);
const char *infargs = current_inferior ()->args (); const std::string &infargs = current_inferior ()->args ();
if (infargs != nullptr) if (!infargs.empty ())
psargs += " " + std::string (infargs); psargs += ' ' + infargs;
/* All existing targets that handle writing out prpsinfo expect the /* All existing targets that handle writing out prpsinfo expect the
fname and psargs strings to be at least 16 and 80 characters long fname and psargs strings to be at least 16 and 80 characters long

View File

@ -684,9 +684,9 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
const char *fname = lbasename (get_exec_file (0)); const char *fname = lbasename (get_exec_file (0));
std::string psargs = fname; std::string psargs = fname;
const char *infargs = current_inferior ()->args (); const std::string &infargs = current_inferior ()->args ();
if (infargs != NULL) if (!infargs.empty ())
psargs = psargs + " " + infargs; psargs += ' ' + infargs;
note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (), note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
note_size, fname, note_size, fname,

View File

@ -129,7 +129,7 @@ set_inferior_args_vector (int argc, char **argv)
{ {
gdb::array_view<char * const> args (argv, argc); gdb::array_view<char * const> args (argv, argc);
std::string n = construct_inferior_arguments (args); 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. */ /* 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 /* Note that we ignore the passed-in value in favor of computing it
directly. */ directly. */
deprecated_show_value_hack (file, from_tty, c, deprecated_show_value_hack (file, from_tty, c,
current_inferior ()->args ()); current_inferior ()->args ().c_str ());
} }
/* See gdbsupport/common-inferior.h. */ /* See gdbsupport/common-inferior.h. */

View File

@ -444,26 +444,18 @@ public:
/* Set the argument string to use when running this inferior. /* Set the argument string to use when running this inferior.
Either nullptr or an empty string can be used to represent "no An empty string can be used to represent "no arguments". */
arguments". */ void set_args (std::string args)
void set_args (const char *args)
{ {
if (args != nullptr && args[0] != '\0') m_args = std::move (args);
m_args = make_unique_xstrdup (args);
else
m_args.reset ();
}; };
/* Get the argument string to use when running this inferior. /* Get the argument string to use when running this inferior.
The return value is always non-nullptr. No arguments is represented by No arguments is represented by an empty string. */
an empty string. */ const std::string &args () const
const char *args () const
{ {
if (m_args == nullptr) return m_args;
return "";
return m_args.get ();
} }
/* Set the inferior current working directory. /* Set the inferior current working directory.
@ -602,10 +594,8 @@ private:
/* The list of continuations. */ /* The list of continuations. */
std::list<std::function<void ()>> m_continuations; std::list<std::function<void ()>> m_continuations;
/* The arguments string to use when running. /* The arguments string to use when running. */
std::string m_args;
This is nullptr when there are not args. */
gdb::unique_xmalloc_ptr<char> m_args;
/* The current working directory that will be used when starting /* The current working directory that will be used when starting
this inferior. */ this inferior. */

View File

@ -1847,12 +1847,12 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1); strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
p->pr_fname[sizeof (p->pr_fname) - 1] = '\0'; 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. */ /* The arguments of the program. */
std::string psargs = fname.get (); std::string psargs = fname.get ();
if (infargs != NULL) if (!infargs.empty ())
psargs = psargs + " " + infargs; psargs += ' ' + infargs;
strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1); strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0'; p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';

View File

@ -3603,7 +3603,6 @@ procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
char psargs[80] = {'\0'}; char psargs[80] = {'\0'};
procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0); procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
gdb::unique_xmalloc_ptr<char> note_data; gdb::unique_xmalloc_ptr<char> note_data;
const char *inf_args;
enum gdb_signal stop_signal; enum gdb_signal stop_signal;
if (get_exec_file (0)) 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)); strncpy (psargs, get_exec_file (0), sizeof (psargs));
psargs[sizeof (psargs) - 1] = 0; psargs[sizeof (psargs) - 1] = 0;
inf_args = current_inferior ()->args (); const std::string &inf_args = current_inferior ()->args ();
if (inf_args && *inf_args if (!inf_args.empty () &&
&& (strlen (inf_args) inf_args.length () < ((int) sizeof (psargs) - (int) strlen (psargs)))
< ((int) sizeof (psargs) - (int) strlen (psargs))))
{ {
strncat (psargs, " ", strncat (psargs, " ",
sizeof (psargs) - strlen (psargs)); sizeof (psargs) - strlen (psargs));
strncat (psargs, inf_args, strncat (psargs, inf_args.c_str (),
sizeof (psargs) - strlen (psargs)); sizeof (psargs) - strlen (psargs));
} }
} }