Introduce target_ops method thread_info_to_thread_handle
authorKevin Buettner <kevinb@redhat.com>
Fri, 8 Feb 2019 02:16:04 +0000 (19:16 -0700)
committerKevin Buettner <kevinb@redhat.com>
Tue, 9 Apr 2019 03:09:37 +0000 (20:09 -0700)
This patch adds a thread_info_to_thread_handle method to the target_ops
struct.  It also implements this functionality for remote targets and
linux native threads.

gdb/ChangeLog:

* gdbthread.h (thread_to_thread_handle): Declare.
* thread.c (gdbtypes.h): Include.
(thread_to_thread_handle): New function.

* target.h (struct target_ops): Add thread_info_to_thread_handle.
(target_thread_info_to_thread_handle): Declare.
* target.c (target_thread_info_to_thread_handle): New function.
* target-debug.h (target_debug_print_gdb_byte_vector): Define.
* target-delegates.c: Regenerate.

* linux-thread-db.c (class thread_db_target): Add method
thread_info_to_thread_handle.
(thread_db_target::thread_info_to_thread_handle): Define.
* remote.c (class remote_target): Add new method
thread_info_to_thread_handle.
(remote_target::thread_info_to_thread_handle): Define.

gdb/ChangeLog
gdb/linux-thread-db.c
gdb/remote.c
gdb/target-debug.h
gdb/target-delegates.c
gdb/target.c
gdb/target.h

index 9910952..ea89249 100644 (file)
@@ -1,3 +1,22 @@
+2019-04-08  Kevin Buettner  <kevinb@redhat.com>
+
+       * gdbthread.h (thread_to_thread_handle): Declare.
+       * thread.c (gdbtypes.h): Include.
+       (thread_to_thread_handle): New function.
+
+       * target.h (struct target_ops): Add thread_info_to_thread_handle.
+       (target_thread_info_to_thread_handle): Declare.
+       * target.c (target_thread_info_to_thread_handle): New function.
+       * target-debug.h (target_debug_print_gdb_byte_vector): Define.
+       * target-delegates.c: Regenerate.
+
+       * linux-thread-db.c (class thread_db_target): Add method
+       thread_info_to_thread_handle.
+       (thread_db_target::thread_info_to_thread_handle): Define.
+       * remote.c (class remote_target): Add new method
+       thread_info_to_thread_handle.
+       (remote_target::thread_info_to_thread_handle): Define.
+
 2019-04-08  Pedro Alves  <palves@redhat.com>
 
        * common/common-exceptions.c (throw_exception): Don't create
index b3e1a24..a67f9b9 100644 (file)
@@ -105,6 +105,7 @@ public:
   thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle,
                                             int handle_len,
                                             inferior *inf) override;
+  gdb::byte_vector thread_info_to_thread_handle (struct thread_info *) override;
 };
 
 static char *libthread_db_search_path;
@@ -1692,6 +1693,24 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
   return NULL;
 }
 
+/* Return the thread handle associated the thread_info pointer TP.  */
+
+gdb::byte_vector
+thread_db_target::thread_info_to_thread_handle (struct thread_info *tp)
+{
+  thread_db_thread_info *priv = get_thread_db_thread_info (tp);
+
+  if (priv == NULL)
+    return gdb::byte_vector ();
+
+  int handle_size = sizeof (priv->tid);
+  gdb::byte_vector rv (handle_size);
+
+  memcpy (rv.data (), &priv->tid, handle_size);
+
+  return rv;
+}
+
 /* Get the address of the thread local variable in load module LM which
    is stored at OFFSET within the thread local storage for thread PTID.  */
 
index 767c694..9bbf458 100644 (file)
@@ -485,6 +485,9 @@ public:
                                             int handle_len,
                                             inferior *inf) override;
 
+  gdb::byte_vector thread_info_to_thread_handle (struct thread_info *tp)
+                                                override;
+
   void stop (ptid_t) override;
 
   void interrupt () override;
@@ -13998,6 +14001,13 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
   return NULL;
 }
 
+gdb::byte_vector
+remote_target::thread_info_to_thread_handle (struct thread_info *tp)
+{
+  remote_thread_info *priv = get_remote_thread_info (tp);
+  return priv->thread_handle;
+}
+
 bool
 remote_target::can_async_p ()
 {
index bef8a7f..aa2adce 100644 (file)
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_std_string(X) \
   target_debug_do_print ((X).c_str ())
+#define target_debug_print_gdb_byte_vector(X)  \
+  target_debug_do_print (host_address_to_string (X.data ()))
 
 static void
 target_debug_print_struct_target_waitstatus_p (struct target_waitstatus *status)
index 3654f02..cfc0ce0 100644 (file)
@@ -70,6 +70,7 @@ struct dummy_target : public target_ops
   const char *extra_thread_info (thread_info *arg0) override;
   const char *thread_name (thread_info *arg0) override;
   thread_info *thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, inferior *arg2) override;
+  gdb::byte_vector thread_info_to_thread_handle (struct thread_info *arg0) override;
   void stop (ptid_t arg0) override;
   void interrupt () override;
   void pass_ctrlc () override;
@@ -237,6 +238,7 @@ struct debug_target : public target_ops
   const char *extra_thread_info (thread_info *arg0) override;
   const char *thread_name (thread_info *arg0) override;
   thread_info *thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, inferior *arg2) override;
+  gdb::byte_vector thread_info_to_thread_handle (struct thread_info *arg0) override;
   void stop (ptid_t arg0) override;
   void interrupt () override;
   void pass_ctrlc () override;
@@ -1854,6 +1856,32 @@ debug_target::thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, infe
   return result;
 }
 
+gdb::byte_vector
+target_ops::thread_info_to_thread_handle (struct thread_info *arg0)
+{
+  return this->beneath ()->thread_info_to_thread_handle (arg0);
+}
+
+gdb::byte_vector
+dummy_target::thread_info_to_thread_handle (struct thread_info *arg0)
+{
+  return gdb::byte_vector ();
+}
+
+gdb::byte_vector
+debug_target::thread_info_to_thread_handle (struct thread_info *arg0)
+{
+  gdb::byte_vector result;
+  fprintf_unfiltered (gdb_stdlog, "-> %s->thread_info_to_thread_handle (...)\n", this->beneath ()->shortname ());
+  result = this->beneath ()->thread_info_to_thread_handle (arg0);
+  fprintf_unfiltered (gdb_stdlog, "<- %s->thread_info_to_thread_handle (", this->beneath ()->shortname ());
+  target_debug_print_struct_thread_info_p (arg0);
+  fputs_unfiltered (") = ", gdb_stdlog);
+  target_debug_print_gdb_byte_vector (result);
+  fputs_unfiltered ("\n", gdb_stdlog);
+  return result;
+}
+
 void
 target_ops::stop (ptid_t arg0)
 {
index 3ab5671..f492102 100644 (file)
@@ -2097,6 +2097,14 @@ target_thread_handle_to_thread_info (const gdb_byte *thread_handle,
                                                     handle_len, inf);
 }
 
+/* See target.h.  */
+
+gdb::byte_vector
+target_thread_info_to_thread_handle (struct thread_info *tip)
+{
+  return current_top_target ()->thread_info_to_thread_handle (tip);
+}
+
 void
 target_resume (ptid_t ptid, int step, enum gdb_signal signal)
 {
index 4f7a43e..9078a42 100644 (file)
@@ -659,6 +659,9 @@ struct target_ops
                                                       int,
                                                       inferior *inf)
       TARGET_DEFAULT_RETURN (NULL);
+    /* See target_thread_info_to_thread_handle.  */
+    virtual gdb::byte_vector thread_info_to_thread_handle (struct thread_info *)
+      TARGET_DEFAULT_RETURN (gdb::byte_vector ());
     virtual void stop (ptid_t)
       TARGET_DEFAULT_IGNORE ();
     virtual void interrupt ()
@@ -1851,6 +1854,12 @@ extern const char *target_thread_name (struct thread_info *);
 extern struct thread_info *target_thread_handle_to_thread_info
   (const gdb_byte *thread_handle, int handle_len, struct inferior *inf);
 
+/* Given a thread, return the thread handle, a target-specific sequence of
+   bytes which serves as a thread identifier within the program being
+   debugged.  */
+extern gdb::byte_vector target_thread_info_to_thread_handle
+  (struct thread_info *);
+
 /* Attempts to find the pathname of the executable file
    that was run to create a specified process.