Make linux_ptrace_attach_fail_reason return an std::string

This patch makes linux_ptrace_attach_fail_reason and
linux_ptrace_attach_fail_reason_string return std::string.  It also
replaces usages of struct buffer with std::string.  This allows getting
rid of a cleanup in in linux_ptrace_attach_fail_reason_string and
simplifies the code in general.

Something that looks odd to me is that in
linux_ptrace_attach_fail_reason, if the two messages are appended, there
is no separating space or \n, so the result won't be very nice.  I left
it as-is for now though.

gdb/ChangeLog:

	* nat/linux-ptrace.h (linux_ptrace_attach_fail_reason): Return
	std::string.
	(linux_ptrace_attach_fail_reason_string): Likewise.
	* nat/linux-ptrace.c (linux_ptrace_attach_fail_reason):
	Likewise.
	(linux_ptrace_attach_fail_reason_string): Likewise.
	* linux-nat.c (attach_proc_task_lwp_callback): Adjust.

gdb/gdbserver/ChangeLog:

	* linux-low.c (attach_proc_task_lwp_callback): Adjust to
	linux_ptrace_attach_fail_reason_string now returning an
	std::string.
	(linux_attach): Likewise.
	* thread-db.c (attach_thread): Likewise.
This commit is contained in:
Simon Marchi
2018-01-17 12:33:57 -05:00
parent a7b2d0fbeb
commit 4d9b86e175
7 changed files with 63 additions and 57 deletions

View File

@ -1,3 +1,13 @@
2018-01-17 Simon Marchi <simon.marchi@ericsson.com>
* nat/linux-ptrace.h (linux_ptrace_attach_fail_reason): Return
std::string.
(linux_ptrace_attach_fail_reason_string): Likewise.
* nat/linux-ptrace.c (linux_ptrace_attach_fail_reason):
Likewise.
(linux_ptrace_attach_fail_reason_string): Likewise.
* linux-nat.c (attach_proc_task_lwp_callback): Adjust.
2018-01-17 Simon Marchi <simon.marchi@ericsson.com> 2018-01-17 Simon Marchi <simon.marchi@ericsson.com>
* linux-nat.c (linux_nat_attach): Remove xstrdup. * linux-nat.c (linux_nat_attach): Remove xstrdup.

View File

@ -1,3 +1,11 @@
2018-01-17 Simon Marchi <simon.marchi@ericsson.com>
* linux-low.c (attach_proc_task_lwp_callback): Adjust to
linux_ptrace_attach_fail_reason_string now returning an
std::string.
(linux_attach): Likewise.
* thread-db.c (attach_thread): Likewise.
2018-01-17 Eldar Abusalimov <eldar.abusalimov@jetbrains.com> 2018-01-17 Eldar Abusalimov <eldar.abusalimov@jetbrains.com>
PR gdb/21559 PR gdb/21559

View File

@ -1159,9 +1159,10 @@ attach_proc_task_lwp_callback (ptid_t ptid)
} }
else if (err != 0) else if (err != 0)
{ {
warning (_("Cannot attach to lwp %d: %s"), std::string reason
lwpid, = linux_ptrace_attach_fail_reason_string (ptid, err);
linux_ptrace_attach_fail_reason_string (ptid, err));
warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
} }
return 1; return 1;
@ -1186,8 +1187,11 @@ linux_attach (unsigned long pid)
soon. */ soon. */
err = linux_attach_lwp (ptid); err = linux_attach_lwp (ptid);
if (err != 0) if (err != 0)
error ("Cannot attach to process %ld: %s", {
pid, linux_ptrace_attach_fail_reason_string (ptid, err)); std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
}
proc = linux_add_process (pid, 1); proc = linux_add_process (pid, 1);

View File

@ -225,9 +225,11 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
err = linux_attach_lwp (ptid); err = linux_attach_lwp (ptid);
if (err != 0) if (err != 0)
{ {
std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
warning ("Could not attach to thread %ld (LWP %d): %s\n", warning ("Could not attach to thread %ld (LWP %d): %s\n",
(unsigned long) ti_p->ti_tid, ti_p->ti_lid, (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
linux_ptrace_attach_fail_reason_string (ptid, err));
return 0; return 0;
} }

View File

@ -1167,10 +1167,11 @@ attach_proc_task_lwp_callback (ptid_t ptid)
} }
else else
{ {
std::string reason
= linux_ptrace_attach_fail_reason_string (ptid, err);
warning (_("Cannot attach to lwp %d: %s"), warning (_("Cannot attach to lwp %d: %s"),
lwpid, lwpid, reason.c_str ());
linux_ptrace_attach_fail_reason_string (ptid,
err));
} }
} }
else else
@ -1223,18 +1224,10 @@ linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
CATCH (ex, RETURN_MASK_ERROR) CATCH (ex, RETURN_MASK_ERROR)
{ {
pid_t pid = parse_pid_to_attach (args); pid_t pid = parse_pid_to_attach (args);
struct buffer buffer; std::string reason = linux_ptrace_attach_fail_reason (pid);
char *buffer_s;
buffer_init (&buffer); if (!reason.empty ())
linux_ptrace_attach_fail_reason (pid, &buffer); throw_error (ex.error, "warning: %s\n%s", reason.c_str (), ex.message);
buffer_grow_str0 (&buffer, "");
buffer_s = buffer_finish (&buffer);
make_cleanup (xfree, buffer_s);
if (*buffer_s != '\0')
throw_error (ex.error, "warning: %s\n%s", buffer_s, ex.message);
else else
throw_error (ex.error, "%s", ex.message); throw_error (ex.error, "%s", ex.message);
} }

View File

@ -32,51 +32,42 @@
of 0 means there are no supported features. */ of 0 means there are no supported features. */
static int supported_ptrace_options = -1; static int supported_ptrace_options = -1;
/* Find all possible reasons we could fail to attach PID and append /* Find all possible reasons we could fail to attach PID and return these
these as strings to the already initialized BUFFER. '\0' as a string. An empty string is returned if we didn't find any reason. */
termination of BUFFER must be done by the caller. */
void std::string
linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer) linux_ptrace_attach_fail_reason (pid_t pid)
{ {
pid_t tracerpid; pid_t tracerpid = linux_proc_get_tracerpid_nowarn (pid);
std::string result;
tracerpid = linux_proc_get_tracerpid_nowarn (pid);
if (tracerpid > 0) if (tracerpid > 0)
buffer_xml_printf (buffer, _("process %d is already traced " string_appendf (result,
"by process %d"), _("process %d is already traced by process %d"),
(int) pid, (int) tracerpid); (int) pid, (int) tracerpid);
if (linux_proc_pid_is_zombie_nowarn (pid)) if (linux_proc_pid_is_zombie_nowarn (pid))
buffer_xml_printf (buffer, _("process %d is a zombie " string_appendf (result,
"- the process has already terminated"), _("process %d is a zombie - the process has already "
(int) pid); "terminated"),
(int) pid);
return result;
} }
/* See linux-ptrace.h. */ /* See linux-ptrace.h. */
char * std::string
linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err) linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err)
{ {
static char *reason_string; long lwpid = ptid.lwp ();
struct buffer buffer; std::string reason = linux_ptrace_attach_fail_reason (lwpid);
char *warnings;
long lwpid = ptid_get_lwp (ptid);
xfree (reason_string); if (!reason.empty ())
return string_printf ("%s (%d), %s", safe_strerror (err), err,
buffer_init (&buffer); reason.c_str ());
linux_ptrace_attach_fail_reason (lwpid, &buffer);
buffer_grow_str0 (&buffer, "");
warnings = buffer_finish (&buffer);
if (warnings[0] != '\0')
reason_string = xstrprintf ("%s (%d), %s",
safe_strerror (err), err, warnings);
else else
reason_string = xstrprintf ("%s (%d)", return string_printf ("%s (%d)", safe_strerror (err), err);
safe_strerror (err), err);
xfree (warnings);
return reason_string;
} }
#if defined __i386__ || defined __x86_64__ #if defined __i386__ || defined __x86_64__

View File

@ -180,14 +180,12 @@ struct buffer;
# define TRAP_HWBKPT 4 # define TRAP_HWBKPT 4
#endif #endif
extern void linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer); extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
/* Find all possible reasons we could have failed to attach to PTID /* Find all possible reasons we could have failed to attach to PTID
and return them as a string. ERR is the error PTRACE_ATTACH failed and return them as a string. ERR is the error PTRACE_ATTACH failed
with (an errno). The result is stored in a static buffer. This with (an errno). */
string should be copied into a buffer by the client if the string extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
will not be immediately used, or if it must persist. */
extern char *linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
extern void linux_ptrace_init_warnings (void); extern void linux_ptrace_init_warnings (void);
extern void linux_check_ptrace_features (void); extern void linux_check_ptrace_features (void);