From 11bd012ed256688f43d71dcc531c2f105a9c55dd Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 25 Jun 2021 18:07:54 -0400 Subject: [PATCH] gdb: make inferior::m_cwd an std::string 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 2d0d6cc3e96..c0271763164 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 72f2c29af7e..5cd36f4df3d 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -460,21 +460,19 @@ public: /* 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 @@ private: /* 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 d280e1120cc..c88cf4cf716 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 dc3948ba67e..552481fc551 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -10391,13 +10391,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 0a09de79071..32f847812e6 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 3b8959a28cf..1e0fe67033c 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 32dcc05924e..193c3d9d7d1 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 5e1221277b8..92c880e1212 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.30.2