From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id DC4A83972023 for ; Wed, 14 Jul 2021 04:57:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DC4A83972023 X-ASG-Debug-ID: 1626238641-0c856e67e219ef310001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id tXeHjG6Adi7MOUlv (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Jul 2021 00:57:21 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.localdomain (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) by smtp.ebox.ca (Postfix) with ESMTP id AE2EA441D64; Wed, 14 Jul 2021 00:57:21 -0400 (EDT) From: Simon Marchi X-Barracuda-RBL-IP: 192.222.157.6 X-Barracuda-Effective-Source-IP: 192-222-157-6.qc.cable.ebox.net[192.222.157.6] X-Barracuda-Apparent-Source-IP: 192.222.157.6 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 X-ASG-Orig-Subj: [PATCH 09/16] gdb: make inferior::m_args an std::string Message-Id: <20210714045520.1623120-10-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210714045520.1623120-1-simon.marchi@polymtl.ca> References: <20210714045520.1623120-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1626238641 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-BRTS-Status: 1 X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 6468 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.91193 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-Spam-Status: No, score=-16.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Wed, 14 Jul 2021 04:57:24 -0000 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 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> m_continuations; - /* The arguments string to use when running. - - This is nullptr when there are not args. */ - gdb::unique_xmalloc_ptr 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 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