Implement attach/detach for multi-threaded programs on Linux.
[binutils-gdb.git] / gdb / thread-db.c
index 8c1bdba4411c70fc43b90cb46f9f262bed0fc6e7..ab94429bb8e37c2ac74885f32336aedb1dc55caa 100644 (file)
@@ -56,7 +56,8 @@ static void (*target_new_objfile_chain) (struct objfile *objfile);
 /* Non-zero if we're using this module's target vector.  */
 static int using_thread_db;
 
-/* Non-zero if we musn't deactivate this module's target vector.  */
+/* Non-zero if we have to keep this module's target vector active
+   across re-runs.  */
 static int keep_thread_db;
 
 /* Non-zero if we have determined the signals used by the threads
@@ -506,20 +507,6 @@ disable_thread_signals (void)
 #endif
 }
 
-static void
-deactivate_target (void)
-{
-  /* Forget about the child's process ID.  We shouldn't need it
-     anymore.  */
-  proc_handle.pid = 0;
-
-  if (! keep_thread_db)
-    {
-      using_thread_db = 0;
-      unpush_target (&thread_db_ops);
-    }
-}
-
 static void
 thread_db_new_objfile (struct objfile *objfile)
 {
@@ -528,11 +515,15 @@ thread_db_new_objfile (struct objfile *objfile)
   if (objfile == NULL)
     {
       /* All symbols have been discarded.  If the thread_db target is
-         active, deactivate it now, even if the application was linked
-         statically against the thread library.  */
-      keep_thread_db = 0;
+         active, deactivate it now.  */
       if (using_thread_db)
-       deactivate_target ();
+       {
+         gdb_assert (proc_handle.pid == 0);
+         unpush_target (&thread_db_ops);
+         using_thread_db = 0;
+       }
+
+      keep_thread_db = 0;
 
       goto quit;
     }
@@ -612,8 +603,7 @@ attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
 
   /* Under Linux, we have to attach to each and every thread.  */
 #ifdef ATTACH_LWP
-  if (ti_p->ti_lid != GET_PID (ptid))
-    ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0);
+  ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0);
 #endif
 
   /* Enable thread event reporting for this thread.  */
@@ -623,6 +613,23 @@ attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
           target_pid_to_str (ptid), thread_db_err_str (err));
 }
 
+static void
+thread_db_attach (char *args, int from_tty)
+{
+  target_beneath->to_attach (args, from_tty);
+
+  /* Destroy thread info; it's no longer valid.  */
+  init_thread_list ();
+
+  /* The child process is now the actual multi-threaded
+     program.  Snatch its process ID...  */
+  proc_handle.pid = GET_PID (inferior_ptid);
+
+  /* ...and perform the remaining initialization steps.  */
+  enable_thread_event_reporting ();
+  thread_db_find_new_threads();
+}
+
 static void
 detach_thread (ptid_t ptid, int verbose)
 {
@@ -634,7 +641,14 @@ static void
 thread_db_detach (char *args, int from_tty)
 {
   disable_thread_event_reporting ();
-  deactivate_target ();
+
+  /* There's no need to save & restore inferior_ptid here, since the
+     inferior is supposed to be survive this function call.  */
+  inferior_ptid = lwp_from_thread (inferior_ptid);
+
+  /* Forget about the child's process ID.  We shouldn't need it
+     anymore.  */
+  proc_handle.pid = 0;
 
   target_beneath->to_detach (args, from_tty);
 }
@@ -860,12 +874,21 @@ thread_db_store_registers (int regno)
 static void
 thread_db_kill (void)
 {
+  /* There's no need to save & restore inferior_ptid here, since the
+     inferior isn't supposed to survive this function call.  */
+  inferior_ptid = lwp_from_thread (inferior_ptid);
   target_beneath->to_kill ();
 }
 
 static void
 thread_db_create_inferior (char *exec_file, char *allargs, char **env)
 {
+  if (! keep_thread_db)
+    {
+      unpush_target (&thread_db_ops);
+      using_thread_db = 0;
+    }
+
   target_beneath->to_create_inferior (exec_file, allargs, env);
 }
 
@@ -888,7 +911,10 @@ static void
 thread_db_mourn_inferior (void)
 {
   remove_thread_event_breakpoints ();
-  deactivate_target ();
+
+  /* Forget about the child's process ID.  We shouldn't need it
+     anymore.  */
+  proc_handle.pid = 0;
 
   target_beneath->to_mourn_inferior ();
 }
@@ -996,6 +1022,7 @@ init_thread_db_ops (void)
   thread_db_ops.to_shortname = "multi-thread";
   thread_db_ops.to_longname = "multi-threaded child process.";
   thread_db_ops.to_doc = "Threads and pthreads support.";
+  thread_db_ops.to_attach = thread_db_attach;
   thread_db_ops.to_detach = thread_db_detach;
   thread_db_ops.to_resume = thread_db_resume;
   thread_db_ops.to_wait = thread_db_wait;