aboutsummaryrefslogtreecommitdiff
path: root/gdb/thread.c
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-09-01 12:19:30 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-09-24 17:25:55 -0400
commit25558938d07b61ad628952a7bdc0a793d27f1b71 (patch)
tree1cce292b3623c2f8cecc2426bb2011dfe1716fba /gdb/thread.c
parent7ebaa5f7821682c40e79ee1fdfe43528b7d87376 (diff)
downloadgdb-25558938d07b61ad628952a7bdc0a793d27f1b71.zip
gdb-25558938d07b61ad628952a7bdc0a793d27f1b71.tar.gz
gdb-25558938d07b61ad628952a7bdc0a793d27f1b71.tar.bz2
gdb: change thread_info::name to unique_xmalloc_ptr, add helper function
This started out as changing thread_info::name to a unique_xmalloc_ptr. That showed that almost all users of that field had the same logic to get a thread's name: use thread_info::name if non-nullptr, else ask the target. Factor out this logic in a new thread_name free function. Make the field private (rename to m_name) and add some accessors. Change-Id: Iebdd95f4cd21fbefc505249bd1d05befc466a2fc
Diffstat (limited to 'gdb/thread.c')
-rw-r--r--gdb/thread.c36
1 files changed, 23 insertions, 13 deletions
diff --git a/gdb/thread.c b/gdb/thread.c
index 10c3dcd..ebe2d78 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -302,11 +302,6 @@ thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
this->m_suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
}
-thread_info::~thread_info ()
-{
- xfree (this->name);
-}
-
/* See gdbthread.h. */
bool
@@ -998,7 +993,7 @@ thread_target_id_str (thread_info *tp)
{
std::string target_id = target_pid_to_str (tp->ptid);
const char *extra_info = target_extra_thread_info (tp);
- const char *name = tp->name != nullptr ? tp->name : target_thread_name (tp);
+ const char *name = thread_name (tp);
if (extra_info != nullptr && name != nullptr)
return string_printf ("%s \"%s\" (%s)", target_id.c_str (), name,
@@ -1140,9 +1135,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
if (extra_info != nullptr)
uiout->field_string ("details", extra_info);
- const char *name = (tp->name != nullptr
- ? tp->name
- : target_thread_name (tp));
+ const char *name = thread_name (tp);
if (name != NULL)
uiout->field_string ("name", name);
}
@@ -1835,8 +1828,7 @@ thread_name_command (const char *arg, int from_tty)
arg = skip_spaces (arg);
info = inferior_thread ();
- xfree (info->name);
- info->name = arg ? xstrdup (arg) : NULL;
+ info->set_name (arg != nullptr ? make_unique_xstrdup (arg) : nullptr);
}
/* Find thread ids with a name, target pid, or extra info matching ARG. */
@@ -1863,10 +1855,10 @@ thread_find_command (const char *arg, int from_tty)
{
switch_to_inferior_no_thread (tp->inf);
- if (tp->name != NULL && re_exec (tp->name))
+ if (tp->name () != nullptr && re_exec (tp->name ()))
{
printf_filtered (_("Thread %s has name '%s'\n"),
- print_thread_id (tp), tp->name);
+ print_thread_id (tp), tp->name ());
match++;
}
@@ -2010,6 +2002,24 @@ update_thread_list (void)
update_threads_executing ();
}
+/* See gdbthread.h. */
+
+const char *
+thread_name (thread_info *thread)
+{
+ /* Use the manually set name if there is one. */
+ const char *name = thread->name ();
+ if (name != nullptr)
+ return name;
+
+ /* Otherwise, ask the target. Ensure we query the right target stack. */
+ scoped_restore_current_thread restore_thread;
+ if (thread->inf != current_inferior ())
+ switch_to_inferior_no_thread (thread->inf);
+
+ return target_thread_name (thread);
+}
+
/* Return a new value for the selected thread's id. Return a value of
0 if no thread is selected. If GLOBAL is true, return the thread's
global number. Otherwise return the per-inferior number. */