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
if (show_thread_that_caused_stop ())
{
- const char *name;
struct thread_info *thr = inferior_thread ();
uiout->text ("Thread ");
uiout->field_string ("thread-id", print_thread_id (thr));
- name = thr->name != NULL ? thr->name : target_thread_name (thr);
+ const char *name = thread_name (thr);
if (name != NULL)
{
uiout->text (" \"");
{
public:
explicit thread_info (inferior *inf, ptid_t ptid);
- ~thread_info ();
bool deletable () const;
/* The inferior this thread belongs to. */
struct inferior *inf;
- /* The name of the thread, as specified by the user. This is NULL
- if the thread does not have a user-given name. */
- char *name = NULL;
+ /* The user-given name of the thread.
+
+ Returns nullptr if the thread does not have a user-given name. */
+ const char *name () const
+ {
+ return m_name.get ();
+ }
+
+ /* Set the user-given name of the thread.
+
+ Pass nullptr to clear the name. */
+ void set_name (gdb::unique_xmalloc_ptr<char> name)
+ {
+ m_name = std::move (name);
+ }
bool executing () const
{ return m_executing; }
/* State of inferior thread to restore after GDB is done with an inferior
call. See `struct thread_suspend_state'. */
thread_suspend_state m_suspend;
+
+ /* The user-given name of the thread.
+
+ Nullptr if the thread does not have a user-given name. */
+ gdb::unique_xmalloc_ptr<char> m_name;
};
using thread_info_resumed_with_pending_wait_status_node
alive anymore. */
extern void thread_select (const char *tidstr, class thread_info *thr);
+/* Return THREAD's name.
+
+ If THREAD has a user-given name, return it. Otherwise, query the thread's
+ target to get the name. May return nullptr. */
+extern const char *thread_name (thread_info *thread);
+
#endif /* GDBTHREAD_H */
;
else if (show_thread_that_caused_stop ())
{
- const char *name;
-
uiout->text ("\nThread ");
uiout->field_string ("thread-id", print_thread_id (thr));
- name = thr->name != NULL ? thr->name : target_thread_name (thr);
+ const char *name = thread_name (thr);
if (name != NULL)
{
uiout->text (" \"");
thpy_get_name (PyObject *self, void *ignore)
{
thread_object *thread_obj = (thread_object *) self;
- const char *name;
THPY_REQUIRE_VALID (thread_obj);
- name = thread_obj->thread->name;
- if (name == NULL)
- name = target_thread_name (thread_obj->thread);
-
+ const char *name = thread_name (thread_obj->thread);
if (name == NULL)
Py_RETURN_NONE;
return -1;
}
- xfree (thread_obj->thread->name);
- thread_obj->thread->name = name.release ();
+ thread_obj->thread->set_name (std::move (name));
return 0;
}
extern const char *target_extra_thread_info (thread_info *tp);
/* Return the thread's name, or NULL if the target is unable to determine it.
- The returned value must not be freed by the caller. */
+ The returned value must not be freed by the caller.
+
+ You likely don't want to call this function, but use the thread_name
+ function instead, which prefers the user-given thread name, if set. */
extern const char *target_thread_name (struct thread_info *);
this->m_suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
}
-thread_info::~thread_info ()
-{
- xfree (this->name);
-}
-
/* See gdbthread.h. */
bool
{
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,
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);
}
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. */
{
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++;
}
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. */