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
/* See gdbsupport/common-inferior.h. */
-const char *
+const std::string &
get_inferior_cwd ()
{
return current_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. */
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\
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 ());
}
/* 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
/* 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
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. */
/* 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
/* 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)
{
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 ());
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)
/* See gdbsupport/common-inferior.h. */
-const char *
+const std::string &
get_inferior_cwd ()
{
return current_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);
}
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 */
{
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 (_("\
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.