Improve readability in the failed case.
[debugger.git] / threadtest.c
index c3afa7a..971e940 100644 (file)
@@ -171,9 +171,9 @@ pid_t ps_getpid (struct ps_prochandle *proc_handle)
 
 struct getmodules_callback_arg
   {
-    const char *object_name;
-    Dwfl_Module *module_main;  /* first */
-    Dwfl_Module *module_libpthread;
+    const char *sym_name;
+    psaddr_t *sym_addr;
+    ps_err_e retval;
   };
 
 static int getmodules_callback (Dwfl_Module *module,
@@ -182,51 +182,8 @@ static int getmodules_callback (Dwfl_Module *module,
                                Dwarf_Addr module_low_addr, void *arg)
 {
   struct getmodules_callback_arg *getmodules_callback_arg = arg;
-  const char *basename;
-
-  if (getmodules_callback_arg->module_main == NULL)
-    getmodules_callback_arg->module_main = module;
-
-  basename = strrchr (module_name, '/');
-  if (basename != NULL)
-    basename++;
-  else
-    basename = module_name;
-
-  /* FIXME */
-  if (strcmp (basename, "libpthread-2.5.so") == 0)
-    basename = "libpthread.so.0";
-
-  if (strcmp (basename, getmodules_callback_arg->object_name) != 0)
-    return DWARF_CB_OK;
-
-  getmodules_callback_arg->module_libpthread = module;
-  return DWARF_CB_ABORT;
-}
-
-/* Look up the named symbol in the named DSO in the symbol tables
-   associated with the process being debugged, filling in *SYM_ADDR
-   with the corresponding run-time address.  */
-ps_err_e ps_pglobal_lookup (struct ps_prochandle *proc_handle,
-                           const char *object_name, const char *sym_name,
-                           psaddr_t *sym_addr)
-{
-  Dwfl *dwfl = get_dwfl (proc_handle);
-  struct getmodules_callback_arg getmodules_callback_arg;
   int sym_count, ndx;
   GElf_Sym sym;
-  Dwfl_Module *module;
-
-  getmodules_callback_arg.object_name = object_name;
-  getmodules_callback_arg.module_main = NULL;
-  getmodules_callback_arg.module_libpthread = NULL;
-  dwfl_getmodules (dwfl, getmodules_callback, &getmodules_callback_arg, 0);
-  if (getmodules_callback_arg.module_libpthread != NULL)
-    module = getmodules_callback_arg.module_libpthread;
-  else if (getmodules_callback_arg.module_main != NULL)
-    module = getmodules_callback_arg.module_main;
-  else
-    return PS_NOSYM;
 
   sym_count = dwfl_module_getsymtab (module);
   assert (sym_count >= 0);
@@ -237,14 +194,38 @@ ps_err_e ps_pglobal_lookup (struct ps_prochandle *proc_handle,
       name_got = dwfl_module_getsym (module, ndx, &sym,
                                     NULL);
       assert (name_got != NULL);
-      if (strcmp (name_got, sym_name) == 0)
+      if (strcmp (name_got, getmodules_callback_arg->sym_name) == 0)
         break;
     }
   if (ndx == sym_count)
-    return PS_NOSYM;
+    return DWARF_CB_OK;
 
-  *sym_addr = (psaddr_t) sym.st_value;
-  return PS_OK;
+  *getmodules_callback_arg->sym_addr = (psaddr_t) sym.st_value;
+  getmodules_callback_arg->retval = PS_OK;
+
+  return DWARF_CB_OK;
+}
+
+/* Look up the named symbol in the named DSO in the symbol tables
+   associated with the process being debugged, filling in *SYM_ADDR
+   with the corresponding run-time address.  */
+ps_err_e ps_pglobal_lookup (struct ps_prochandle *proc_handle,
+                           const char *object_name, const char *sym_name,
+                           psaddr_t *sym_addr)
+{
+  Dwfl *dwfl = get_dwfl (proc_handle);
+  struct getmodules_callback_arg getmodules_callback_arg;
+  ptrdiff_t err_ptrdiff;
+
+  /* FIXME: `object_name' ignored due to missing unresolving of shared
+     libraries symbolic links.  */
+  getmodules_callback_arg.sym_name = sym_name;
+  getmodules_callback_arg.sym_addr = sym_addr;
+  getmodules_callback_arg.retval = PS_NOSYM;
+  err_ptrdiff = dwfl_getmodules (dwfl, getmodules_callback,
+                                &getmodules_callback_arg, 0);
+  assert (err_ptrdiff == 0);
+  return getmodules_callback_arg.retval;
 }
 
 #ifdef __x86_64__
@@ -298,7 +279,11 @@ static void thread_test (td_thragent_t *thread_agent)
   err = td_ta_thr_iter (thread_agent, find_new_threads_callback, NULL,
                        TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
                        TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
-  assert (err == TD_OK);
+  if (err != TD_OK)
+    {
+      fprintf (stderr, "err = %d\n", err);
+      abort ();
+    }
 }
 
 static void attach (pid_t pid)
@@ -314,11 +299,17 @@ static void attach (pid_t pid)
   proc_handle_local.dwfl = NULL;
   err = td_ta_new (&proc_handle_local, &thread_agent);
   assert (err == TD_OK || err == TD_NOLIBTHREAD);
-  if (err == TD_OK)
-    puts ("multithreaded");
-  else
+  if (err == TD_NOLIBTHREAD)
     puts ("singlethreaded");
-  thread_test (thread_agent);
+  else
+    {
+      puts ("multithreaded");
+      thread_test (thread_agent);
+      err = td_ta_delete (thread_agent);
+      assert (err == TD_OK);
+    }
+  if (proc_handle_local.dwfl != NULL);
+    dwfl_end (proc_handle_local.dwfl);
 }
 
 static void *start (void *arg)