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 46F0F397201D for ; Wed, 14 Jul 2021 04:57:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 46F0F397201D X-ASG-Debug-ID: 1626238641-0c856e67e219ef320001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id DZIe1BytgcwkmP0t (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 BFA96441D65; 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 10/16] gdb: make inferior::m_cwd an std::string Date: Wed, 14 Jul 2021 00:55:14 -0400 X-ASG-Orig-Subj: [PATCH 10/16] gdb: make inferior::m_cwd an std::string Message-Id: <20210714045520.1623120-11-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-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 9340 X-Barracuda-BRTS-Status: 1 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:25 -0000 Same idea as the previous patch, but for m_cwd. To keep things consistent across the board, change get_inferior_cwd as well, which is shared with GDBserver. So update the related GDBserver code too. Change-Id: Ia2c047fda738d45f3d18bc999eb67ceb8400ce4e --- gdb/infcmd.c | 14 ++++++-------- gdb/inferior.h | 18 ++++++++---------- gdb/nat/fork-inferior.c | 15 ++++++--------- gdb/remote.c | 9 +++++---- gdbserver/inferiors.cc | 16 +++++++--------- gdbserver/inferiors.h | 4 ++-- gdbserver/server.cc | 6 +++--- gdbsupport/common-inferior.h | 7 ++++--- 8 files changed, 41 insertions(+), 48 deletions(-) diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 2d0d6cc3e965..c02717631643 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -156,7 +156,7 @@ show_args_command (struct ui_file *file, int from_tty, /* See gdbsupport/common-inferior.h. */ -const char * +const std::string & get_inferior_cwd () { return current_inferior ()->cwd (); @@ -167,10 +167,7 @@ get_inferior_cwd () static void set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c) { - if (*inferior_cwd_scratch == '\0') - current_inferior ()->set_cwd (nullptr); - else - current_inferior ()->set_cwd (inferior_cwd_scratch); + current_inferior ()->set_cwd (inferior_cwd_scratch); } /* Handle the 'show cwd' command. */ @@ -179,9 +176,9 @@ static void show_cwd_command (struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value) { - const char *cwd = current_inferior ()->cwd (); + const std::string &cwd = current_inferior ()->cwd (); - if (cwd == NULL) + if (cwd.empty ()) fprintf_filtered (gdb_stdout, _("\ You have not set the inferior's current working directory.\n\ @@ -190,7 +187,8 @@ server's cwd if remote debugging.\n")); else fprintf_filtered (gdb_stdout, _("Current working directory that will be used " - "when starting the inferior is \"%s\".\n"), cwd); + "when starting the inferior is \"%s\".\n"), + cwd.c_str ()); } diff --git a/gdb/inferior.h b/gdb/inferior.h index 6c1e08a7671b..c52c38caa93e 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -460,21 +460,19 @@ class inferior : public refcounted_object, /* Set the inferior current working directory. - If CWD is NULL, unset the directory. */ - void set_cwd (const char *cwd) + If CWD is empty, unset the directory. */ + void set_cwd (std::string cwd) { - if (cwd == NULL) - m_cwd.reset (); - else - m_cwd.reset (xstrdup (cwd)); + m_cwd = std::move (cwd); } /* Get the inferior current working directory. - Return nullptr if the current working directory is not specified. */ - const char *cwd () const + Return an empty string if the current working directory is not + specified. */ + const std::string &cwd () const { - return m_cwd.get (); + return m_cwd; } /* Convenient handle (GDB inferior id). Unique across all @@ -599,7 +597,7 @@ class inferior : public refcounted_object, /* The current working directory that will be used when starting this inferior. */ - gdb::unique_xmalloc_ptr m_cwd; + std::string m_cwd; }; /* Keep a registry of per-inferior data-pointers required by other GDB diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c index d280e1120ccd..c88cf4cf7165 100644 --- a/gdb/nat/fork-inferior.c +++ b/gdb/nat/fork-inferior.c @@ -281,8 +281,6 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, char **save_our_env; int i; int save_errno; - const char *inferior_cwd; - std::string expanded_inferior_cwd; /* If no exec file handed to us, get it from the exec-file command -- with a good, common error message if none is specified. */ @@ -326,14 +324,13 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, /* Check if the user wants to set a different working directory for the inferior. */ - inferior_cwd = get_inferior_cwd (); + std::string inferior_cwd = get_inferior_cwd (); - if (inferior_cwd != NULL) + if (!inferior_cwd.empty ()) { /* Expand before forking because between fork and exec, the child process may only execute async-signal-safe operations. */ - expanded_inferior_cwd = gdb_tilde_expand (inferior_cwd); - inferior_cwd = expanded_inferior_cwd.c_str (); + inferior_cwd = gdb_tilde_expand (inferior_cwd.c_str ()); } /* If there's any initialization of the target layers that must @@ -373,10 +370,10 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, /* Change to the requested working directory if the user requested it. */ - if (inferior_cwd != NULL) + if (!inferior_cwd.empty ()) { - if (chdir (inferior_cwd) < 0) - trace_start_error_with_name (inferior_cwd); + if (chdir (inferior_cwd.c_str ()) < 0) + trace_start_error_with_name (inferior_cwd.c_str ()); } if (debug_fork) diff --git a/gdb/remote.c b/gdb/remote.c index a0726a00f05c..e2a08c9a113d 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -10397,13 +10397,14 @@ remote_target::extended_remote_set_inferior_cwd () { if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE) { - const char *inferior_cwd = current_inferior ()->cwd (); + const std::string &inferior_cwd = current_inferior ()->cwd (); remote_state *rs = get_remote_state (); - if (inferior_cwd != NULL) + if (!inferior_cwd.empty ()) { - std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd, - strlen (inferior_cwd)); + std::string hexpath + = bin2hex ((const gdb_byte *) inferior_cwd.data (), + inferior_cwd.size ()); xsnprintf (rs->buf.data (), get_remote_packet_size (), "QSetWorkingDir:%s", hexpath.c_str ()); diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc index 0a09de79071d..32f847812e60 100644 --- a/gdbserver/inferiors.cc +++ b/gdbserver/inferiors.cc @@ -28,8 +28,10 @@ std::list all_threads; struct thread_info *current_thread; -/* The current working directory used to start the inferior. */ -static const char *current_inferior_cwd = NULL; +/* The current working directory used to start the inferior. + + Empty if not specified. */ +static std::string current_inferior_cwd; struct thread_info * add_thread (ptid_t thread_id, void *target_data) @@ -235,7 +237,7 @@ switch_to_process (process_info *proc) /* See gdbsupport/common-inferior.h. */ -const char * +const std::string & get_inferior_cwd () { return current_inferior_cwd; @@ -244,11 +246,7 @@ get_inferior_cwd () /* See inferiors.h. */ void -set_inferior_cwd (const char *cwd) +set_inferior_cwd (std::string cwd) { - xfree ((void *) current_inferior_cwd); - if (cwd != NULL) - current_inferior_cwd = xstrdup (cwd); - else - current_inferior_cwd = NULL; + current_inferior_cwd = std::move (cwd); } diff --git a/gdbserver/inferiors.h b/gdbserver/inferiors.h index 3b8959a28cf7..1e0fe67033cb 100644 --- a/gdbserver/inferiors.h +++ b/gdbserver/inferiors.h @@ -154,8 +154,8 @@ void *thread_target_data (struct thread_info *); struct regcache *thread_regcache_data (struct thread_info *); void set_thread_regcache_data (struct thread_info *, struct regcache *); -/* Set the inferior current working directory. If CWD is NULL, unset +/* Set the inferior current working directory. If CWD is empty, unset the directory. */ -void set_inferior_cwd (const char *cwd); +void set_inferior_cwd (std::string cwd); #endif /* GDBSERVER_INFERIORS_H */ diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 32dcc05924e1..193c3d9d7d17 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -949,17 +949,17 @@ handle_general_set (char *own_buf) { std::string path = hex2str (p); - set_inferior_cwd (path.c_str ()); - if (remote_debug) debug_printf (_("[Set the inferior's current directory to %s]\n"), path.c_str ()); + + set_inferior_cwd (std::move (path)); } else { /* An empty argument means that we should clear out any previously set cwd for the inferior. */ - set_inferior_cwd (NULL); + set_inferior_cwd (""); if (remote_debug) debug_printf (_("\ diff --git a/gdbsupport/common-inferior.h b/gdbsupport/common-inferior.h index 5e1221277b87..92c880e12123 100644 --- a/gdbsupport/common-inferior.h +++ b/gdbsupport/common-inferior.h @@ -32,9 +32,10 @@ extern const char *get_exec_wrapper (); otherwise return 0 in that case. */ extern const char *get_exec_file (int err); -/* Return the inferior's current working directory. If nothing has - been set, then return NULL. */ -extern const char *get_inferior_cwd (); +/* Return the inferior's current working directory. + + If it is not set, the string is empty. */ +extern const std::string &get_inferior_cwd (); /* Whether to start up the debuggee under a shell. -- 2.32.0