public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
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	[thread overview]
Message-ID: <20210714045520.1623120-11-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20210714045520.1623120-1-simon.marchi@polymtl.ca>

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<char> 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<thread_info *> 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


  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 ` [PATCH 09/16] gdb: make inferior::m_args an std::string Simon Marchi
2021-07-14  4:55 ` Simon Marchi [this message]
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-11-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).