Automatic date update in version.in
[binutils-gdb.git] / gdbserver / thread-db.cc
blob6e0e2228a5f982e943e5cf7356d0550f64c53a98
1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "server.h"
23 #include "linux-low.h"
25 #include "debug.h"
26 #include "gdb_proc_service.h"
27 #include "nat/gdb_thread_db.h"
28 #include "gdbsupport/gdb_vecs.h"
29 #include "nat/linux-procfs.h"
30 #include "gdbsupport/scoped_restore.h"
32 #ifndef USE_LIBTHREAD_DB_DIRECTLY
33 #include <dlfcn.h>
34 #endif
35 #include <limits.h>
36 #include <ctype.h>
38 struct thread_db
40 /* Structure that identifies the child process for the
41 <proc_service.h> interface. */
42 struct ps_prochandle proc_handle;
44 /* Connection to the libthread_db library. */
45 td_thragent_t *thread_agent;
47 /* If this flag has been set, we've already asked GDB for all
48 symbols we might need; assume symbol cache misses are
49 failures. */
50 int all_symbols_looked_up;
52 #ifndef USE_LIBTHREAD_DB_DIRECTLY
53 /* Handle of the libthread_db from dlopen. */
54 void *handle;
55 #endif
57 /* Addresses of libthread_db functions. */
58 td_ta_new_ftype *td_ta_new_p;
59 td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
60 td_thr_get_info_ftype *td_thr_get_info_p;
61 td_ta_thr_iter_ftype *td_ta_thr_iter_p;
62 td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
63 td_thr_tlsbase_ftype *td_thr_tlsbase_p;
64 td_symbol_list_ftype *td_symbol_list_p;
67 static char *libthread_db_search_path;
69 static int find_one_thread (ptid_t);
70 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
72 static const char *
73 thread_db_err_str (td_err_e err)
75 static char buf[64];
77 switch (err)
79 case TD_OK:
80 return "generic 'call succeeded'";
81 case TD_ERR:
82 return "generic error";
83 case TD_NOTHR:
84 return "no thread to satisfy query";
85 case TD_NOSV:
86 return "no sync handle to satisfy query";
87 case TD_NOLWP:
88 return "no LWP to satisfy query";
89 case TD_BADPH:
90 return "invalid process handle";
91 case TD_BADTH:
92 return "invalid thread handle";
93 case TD_BADSH:
94 return "invalid synchronization handle";
95 case TD_BADTA:
96 return "invalid thread agent";
97 case TD_BADKEY:
98 return "invalid key";
99 case TD_NOMSG:
100 return "no event message for getmsg";
101 case TD_NOFPREGS:
102 return "FPU register set not available";
103 case TD_NOLIBTHREAD:
104 return "application not linked with libthread";
105 case TD_NOEVENT:
106 return "requested event is not supported";
107 case TD_NOCAPAB:
108 return "capability not available";
109 case TD_DBERR:
110 return "debugger service failed";
111 case TD_NOAPLIC:
112 return "operation not applicable to";
113 case TD_NOTSD:
114 return "no thread-specific data for this thread";
115 case TD_MALLOC:
116 return "malloc failed";
117 case TD_PARTIALREG:
118 return "only part of register set was written/read";
119 case TD_NOXREGS:
120 return "X register set not available for this thread";
121 #ifdef HAVE_TD_VERSION
122 case TD_VERSION:
123 return "version mismatch between libthread_db and libpthread";
124 #endif
125 default:
126 xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
127 return buf;
131 #if 0
132 static char *
133 thread_db_state_str (td_thr_state_e state)
135 static char buf[64];
137 switch (state)
139 case TD_THR_STOPPED:
140 return "stopped by debugger";
141 case TD_THR_RUN:
142 return "runnable";
143 case TD_THR_ACTIVE:
144 return "active";
145 case TD_THR_ZOMBIE:
146 return "zombie";
147 case TD_THR_SLEEP:
148 return "sleeping";
149 case TD_THR_STOPPED_ASLEEP:
150 return "stopped by debugger AND blocked";
151 default:
152 xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
153 return buf;
156 #endif
158 /* Get thread info about PTID, accessing memory via the current
159 thread. */
161 static int
162 find_one_thread (ptid_t ptid)
164 td_thrhandle_t th;
165 td_thrinfo_t ti;
166 td_err_e err;
167 struct lwp_info *lwp;
168 struct thread_db *thread_db = current_process ()->priv->thread_db;
169 int lwpid = ptid.lwp ();
171 thread_info *thread = find_thread_ptid (ptid);
172 lwp = get_thread_lwp (thread);
173 if (lwp->thread_known)
174 return 1;
176 /* Get information about this thread. */
177 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
178 if (err != TD_OK)
179 error ("Cannot get thread handle for LWP %d: %s",
180 lwpid, thread_db_err_str (err));
182 err = thread_db->td_thr_get_info_p (&th, &ti);
183 if (err != TD_OK)
184 error ("Cannot get thread info for LWP %d: %s",
185 lwpid, thread_db_err_str (err));
187 threads_debug_printf ("Found thread %ld (LWP %d)",
188 (unsigned long) ti.ti_tid, ti.ti_lid);
190 if (lwpid != ti.ti_lid)
192 warning ("PID mismatch! Expected %ld, got %ld",
193 (long) lwpid, (long) ti.ti_lid);
194 return 0;
197 /* If the new thread ID is zero, a final thread ID will be available
198 later. Do not enable thread debugging yet. */
199 if (ti.ti_tid == 0)
200 return 0;
202 lwp->thread_known = 1;
203 lwp->th = th;
204 lwp->thread_handle = ti.ti_tid;
206 return 1;
209 /* Attach a thread. Return true on success. */
211 static int
212 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
214 struct process_info *proc = current_process ();
215 int pid = pid_of (proc);
216 ptid_t ptid = ptid_t (pid, ti_p->ti_lid);
217 struct lwp_info *lwp;
218 int err;
220 threads_debug_printf ("Attaching to thread %ld (LWP %d)",
221 (unsigned long) ti_p->ti_tid, ti_p->ti_lid);
222 err = the_linux_target->attach_lwp (ptid);
223 if (err != 0)
225 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
227 warning ("Could not attach to thread %ld (LWP %d): %s",
228 (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
230 return 0;
233 lwp = find_lwp_pid (ptid);
234 gdb_assert (lwp != NULL);
235 lwp->thread_known = 1;
236 lwp->th = *th_p;
237 lwp->thread_handle = ti_p->ti_tid;
239 return 1;
242 /* Attach thread if we haven't seen it yet.
243 Increment *COUNTER if we have attached a new thread.
244 Return false on failure. */
246 static int
247 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
248 int *counter)
250 struct lwp_info *lwp;
252 lwp = find_lwp_pid (ptid_t (ti_p->ti_lid));
253 if (lwp != NULL)
254 return 1;
256 if (!attach_thread (th_p, ti_p))
257 return 0;
259 if (counter != NULL)
260 *counter += 1;
262 return 1;
265 static int
266 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
268 td_thrinfo_t ti;
269 td_err_e err;
270 struct thread_db *thread_db = current_process ()->priv->thread_db;
272 err = thread_db->td_thr_get_info_p (th_p, &ti);
273 if (err != TD_OK)
274 error ("Cannot get thread info: %s", thread_db_err_str (err));
276 if (ti.ti_lid == -1)
278 /* A thread with kernel thread ID -1 is either a thread that
279 exited and was joined, or a thread that is being created but
280 hasn't started yet, and that is reusing the tcb/stack of a
281 thread that previously exited and was joined. (glibc marks
282 terminated and joined threads with kernel thread ID -1. See
283 glibc PR17707. */
284 threads_debug_printf ("thread_db: skipping exited and "
285 "joined thread (0x%lx)",
286 (unsigned long) ti.ti_tid);
287 return 0;
290 /* Check for zombies. */
291 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
292 return 0;
294 if (!maybe_attach_thread (th_p, &ti, (int *) data))
296 /* Terminate iteration early: we might be looking at stale data in
297 the inferior. The thread_db_find_new_threads will retry. */
298 return 1;
301 return 0;
304 static void
305 thread_db_find_new_threads (void)
307 td_err_e err;
308 ptid_t ptid = current_ptid;
309 struct thread_db *thread_db = current_process ()->priv->thread_db;
310 int loop, iteration;
312 /* This function is only called when we first initialize thread_db.
313 First locate the initial thread. If it is not ready for
314 debugging yet, then stop. */
315 if (find_one_thread (ptid) == 0)
316 return;
318 /* Require 4 successive iterations which do not find any new threads.
319 The 4 is a heuristic: there is an inherent race here, and I have
320 seen that 2 iterations in a row are not always sufficient to
321 "capture" all threads. */
322 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
324 int new_thread_count = 0;
326 /* Iterate over all user-space threads to discover new threads. */
327 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
328 find_new_threads_callback,
329 &new_thread_count,
330 TD_THR_ANY_STATE,
331 TD_THR_LOWEST_PRIORITY,
332 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
333 threads_debug_printf ("Found %d threads in iteration %d.",
334 new_thread_count, iteration);
336 if (new_thread_count != 0)
338 /* Found new threads. Restart iteration from beginning. */
339 loop = -1;
342 if (err != TD_OK)
343 error ("Cannot find new threads: %s", thread_db_err_str (err));
346 /* Cache all future symbols that thread_db might request. We can not
347 request symbols at arbitrary states in the remote protocol, only
348 when the client tells us that new symbols are available. So when
349 we load the thread library, make sure to check the entire list. */
351 static void
352 thread_db_look_up_symbols (void)
354 struct thread_db *thread_db = current_process ()->priv->thread_db;
355 const char **sym_list;
356 CORE_ADDR unused;
358 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
359 look_up_one_symbol (*sym_list, &unused, 1);
361 /* We're not interested in any other libraries loaded after this
362 point, only in symbols in libpthread.so. */
363 thread_db->all_symbols_looked_up = 1;
367 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
369 struct thread_db *thread_db = current_process ()->priv->thread_db;
370 int may_ask_gdb = !thread_db->all_symbols_looked_up;
372 /* If we've passed the call to thread_db_look_up_symbols, then
373 anything not in the cache must not exist; we're not interested
374 in any libraries loaded after that point, only in symbols in
375 libpthread.so. It might not be an appropriate time to look
376 up a symbol, e.g. while we're trying to fetch registers. */
377 return look_up_one_symbol (name, addrp, may_ask_gdb);
381 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
382 CORE_ADDR load_module, CORE_ADDR *address)
384 psaddr_t addr;
385 td_err_e err;
386 struct lwp_info *lwp;
387 struct process_info *proc;
388 struct thread_db *thread_db;
390 proc = get_thread_process (thread);
391 thread_db = proc->priv->thread_db;
393 /* If the thread layer is not (yet) initialized, fail. */
394 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
395 return TD_ERR;
397 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
398 could work. */
399 if (thread_db->td_thr_tls_get_addr_p == NULL
400 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
401 return -1;
403 lwp = get_thread_lwp (thread);
404 if (!lwp->thread_known)
405 find_one_thread (thread->id);
406 if (!lwp->thread_known)
407 return TD_NOTHR;
409 scoped_restore_current_thread restore_thread;
410 switch_to_thread (thread);
412 if (load_module != 0)
414 /* Note the cast through uintptr_t: this interface only works if
415 a target address fits in a psaddr_t, which is a host pointer.
416 So a 32-bit debugger can not access 64-bit TLS through this. */
417 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
418 (psaddr_t) (uintptr_t) load_module,
419 offset, &addr);
421 else
423 /* This code path handles the case of -static -pthread executables:
424 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
425 For older GNU libc r_debug.r_map is NULL. For GNU libc after
426 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
427 The constant number 1 depends on GNU __libc_setup_tls
428 initialization of l_tls_modid to 1. */
429 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
430 addr = (char *) addr + offset;
433 if (err == TD_OK)
435 *address = (CORE_ADDR) (uintptr_t) addr;
436 return 0;
438 else
439 return err;
442 /* See linux-low.h. */
444 bool
445 thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len)
447 struct thread_db *thread_db;
448 struct lwp_info *lwp;
449 thread_info *thread = find_thread_ptid (ptid);
451 if (thread == NULL)
452 return false;
454 thread_db = get_thread_process (thread)->priv->thread_db;
456 if (thread_db == NULL)
457 return false;
459 lwp = get_thread_lwp (thread);
461 if (!lwp->thread_known && !find_one_thread (thread->id))
462 return false;
464 gdb_assert (lwp->thread_known);
466 *handle = (gdb_byte *) &lwp->thread_handle;
467 *handle_len = sizeof (lwp->thread_handle);
468 return true;
471 #ifdef USE_LIBTHREAD_DB_DIRECTLY
473 static int
474 thread_db_load_search (void)
476 td_err_e err;
477 struct thread_db *tdb;
478 struct process_info *proc = current_process ();
480 gdb_assert (proc->priv->thread_db == NULL);
482 tdb = XCNEW (struct thread_db);
483 proc->priv->thread_db = tdb;
485 tdb->td_ta_new_p = &td_ta_new;
487 /* Attempt to open a connection to the thread library. */
488 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
489 if (err != TD_OK)
491 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
492 free (tdb);
493 proc->priv->thread_db = NULL;
494 return 0;
497 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
498 tdb->td_thr_get_info_p = &td_thr_get_info;
499 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
500 tdb->td_symbol_list_p = &td_symbol_list;
502 /* These are not essential. */
503 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
504 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
506 return 1;
509 #else
511 static int
512 try_thread_db_load_1 (void *handle)
514 td_err_e err;
515 struct thread_db *tdb;
516 struct process_info *proc = current_process ();
518 gdb_assert (proc->priv->thread_db == NULL);
520 tdb = XCNEW (struct thread_db);
521 proc->priv->thread_db = tdb;
523 tdb->handle = handle;
525 /* Initialize pointers to the dynamic library functions we will use.
526 Essential functions first. */
528 #define CHK(required, a) \
529 do \
531 if ((a) == NULL) \
533 threads_debug_printf ("dlsym: %s", dlerror ()); \
534 if (required) \
536 free (tdb); \
537 proc->priv->thread_db = NULL; \
538 return 0; \
542 while (0)
544 #define TDB_DLSYM(tdb, func) \
545 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
547 CHK (1, TDB_DLSYM (tdb, td_ta_new));
549 /* Attempt to open a connection to the thread library. */
550 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
551 if (err != TD_OK)
553 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
554 free (tdb);
555 proc->priv->thread_db = NULL;
556 return 0;
559 CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
560 CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
561 CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
562 CHK (1, TDB_DLSYM (tdb, td_symbol_list));
564 /* These are not essential. */
565 CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
566 CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
568 #undef CHK
569 #undef TDB_DLSYM
571 return 1;
574 #ifdef HAVE_DLADDR
576 /* Lookup a library in which given symbol resides.
577 Note: this is looking in the GDBSERVER process, not in the inferior.
578 Returns library name, or NULL. */
580 static const char *
581 dladdr_to_soname (const void *addr)
583 Dl_info info;
585 if (dladdr (addr, &info) != 0)
586 return info.dli_fname;
587 return NULL;
590 #endif
592 static int
593 try_thread_db_load (const char *library)
595 void *handle;
597 threads_debug_printf ("Trying host libthread_db library: %s.",
598 library);
599 handle = dlopen (library, RTLD_NOW);
600 if (handle == NULL)
602 threads_debug_printf ("dlopen failed: %s.", dlerror ());
603 return 0;
606 #ifdef HAVE_DLADDR
607 if (debug_threads && strchr (library, '/') == NULL)
609 void *td_init;
611 td_init = dlsym (handle, "td_init");
612 if (td_init != NULL)
614 const char *const libpath = dladdr_to_soname (td_init);
616 if (libpath != NULL)
617 threads_debug_printf ("Host %s resolved to: %s.", library, libpath);
620 #endif
622 if (try_thread_db_load_1 (handle))
623 return 1;
625 /* This library "refused" to work on current inferior. */
626 dlclose (handle);
627 return 0;
630 /* Handle $sdir in libthread-db-search-path.
631 Look for libthread_db in the system dirs, or wherever a plain
632 dlopen(file_without_path) will look.
633 The result is true for success. */
635 static int
636 try_thread_db_load_from_sdir (void)
638 return try_thread_db_load (LIBTHREAD_DB_SO);
641 /* Try to load libthread_db from directory DIR of length DIR_LEN.
642 The result is true for success. */
644 static int
645 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
647 char path[PATH_MAX];
649 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
651 char *cp = (char *) xmalloc (dir_len + 1);
653 memcpy (cp, dir, dir_len);
654 cp[dir_len] = '\0';
655 warning (_("libthread-db-search-path component too long,"
656 " ignored: %s."), cp);
657 free (cp);
658 return 0;
661 memcpy (path, dir, dir_len);
662 path[dir_len] = '/';
663 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
664 return try_thread_db_load (path);
667 /* Search libthread_db_search_path for libthread_db which "agrees"
668 to work on current inferior.
669 The result is true for success. */
671 static int
672 thread_db_load_search (void)
674 int rc = 0;
676 if (libthread_db_search_path == NULL)
677 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
679 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
680 = dirnames_to_char_ptr_vec (libthread_db_search_path);
682 for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
684 char *this_dir = this_dir_up.get ();
685 const int pdir_len = sizeof ("$pdir") - 1;
686 size_t this_dir_len;
688 this_dir_len = strlen (this_dir);
690 if (strncmp (this_dir, "$pdir", pdir_len) == 0
691 && (this_dir[pdir_len] == '\0'
692 || this_dir[pdir_len] == '/'))
694 /* We don't maintain a list of loaded libraries so we don't know
695 where libpthread lives. We *could* fetch the info, but we don't
696 do that yet. Ignore it. */
698 else if (strcmp (this_dir, "$sdir") == 0)
700 if (try_thread_db_load_from_sdir ())
702 rc = 1;
703 break;
706 else
708 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
710 rc = 1;
711 break;
716 threads_debug_printf ("thread_db_load_search returning %d", rc);
717 return rc;
720 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
723 thread_db_init (void)
725 struct process_info *proc = current_process ();
727 /* FIXME drow/2004-10-16: This is the "overall process ID", which
728 GNU/Linux calls tgid, "thread group ID". When we support
729 attaching to threads, the original thread may not be the correct
730 thread. We would have to get the process ID from /proc for NPTL.
732 This isn't the only place in gdbserver that assumes that the first
733 process in the list is the thread group leader. */
735 if (thread_db_load_search ())
737 /* It's best to avoid td_ta_thr_iter if possible. That walks
738 data structures in the inferior's address space that may be
739 corrupted, or, if the target is running, the list may change
740 while we walk it. In the latter case, it's possible that a
741 thread exits just at the exact time that causes GDBserver to
742 get stuck in an infinite loop. As the kernel supports clone
743 events and /proc/PID/task/ exists, then we already know about
744 all threads in the process. When we need info out of
745 thread_db on a given thread (e.g., for TLS), we'll use
746 find_one_thread then. That uses thread_db entry points that
747 do not walk libpthread's thread list, so should be safe, as
748 well as more efficient. */
749 if (!linux_proc_task_list_dir_exists (pid_of (proc)))
750 thread_db_find_new_threads ();
751 thread_db_look_up_symbols ();
752 return 1;
755 return 0;
758 /* Disconnect from libthread_db and free resources. */
760 static void
761 disable_thread_event_reporting (struct process_info *proc)
763 struct thread_db *thread_db = proc->priv->thread_db;
764 if (thread_db)
766 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
767 td_thr_events_t *event);
769 #ifndef USE_LIBTHREAD_DB_DIRECTLY
770 td_ta_clear_event_p
771 = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
772 "td_ta_clear_event");
773 #else
774 td_ta_clear_event_p = &td_ta_clear_event;
775 #endif
777 if (td_ta_clear_event_p != NULL)
779 scoped_restore_current_thread restore_thread;
780 td_thr_events_t events;
782 switch_to_process (proc);
784 /* Set the process wide mask saying we aren't interested
785 in any events anymore. */
786 td_event_fillset (&events);
787 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
792 void
793 thread_db_detach (struct process_info *proc)
795 struct thread_db *thread_db = proc->priv->thread_db;
797 if (thread_db)
799 disable_thread_event_reporting (proc);
803 /* Disconnect from libthread_db and free resources. */
805 void
806 thread_db_mourn (struct process_info *proc)
808 struct thread_db *thread_db = proc->priv->thread_db;
809 if (thread_db)
811 td_ta_delete_ftype *td_ta_delete_p;
813 #ifndef USE_LIBTHREAD_DB_DIRECTLY
814 td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
815 #else
816 td_ta_delete_p = &td_ta_delete;
817 #endif
819 if (td_ta_delete_p != NULL)
820 (*td_ta_delete_p) (thread_db->thread_agent);
822 #ifndef USE_LIBTHREAD_DB_DIRECTLY
823 dlclose (thread_db->handle);
824 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
826 free (thread_db);
827 proc->priv->thread_db = NULL;
831 /* Handle "set libthread-db-search-path" monitor command and return 1.
832 For any other command, return 0. */
835 thread_db_handle_monitor_command (char *mon)
837 const char *cmd = "set libthread-db-search-path";
838 size_t cmd_len = strlen (cmd);
840 if (strncmp (mon, cmd, cmd_len) == 0
841 && (mon[cmd_len] == '\0'
842 || mon[cmd_len] == ' '))
844 const char *cp = mon + cmd_len;
846 if (libthread_db_search_path != NULL)
847 free (libthread_db_search_path);
849 /* Skip leading space (if any). */
850 while (isspace (*cp))
851 ++cp;
853 if (*cp == '\0')
854 cp = LIBTHREAD_DB_SEARCH_PATH;
855 libthread_db_search_path = xstrdup (cp);
857 monitor_output ("libthread-db-search-path set to `");
858 monitor_output (libthread_db_search_path);
859 monitor_output ("'\n");
860 return 1;
863 /* Tell server.c to perform default processing. */
864 return 0;
867 /* See linux-low.h. */
869 void
870 thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid)
872 process_info *parent_proc = get_thread_process (parent_thr);
873 struct thread_db *thread_db = parent_proc->priv->thread_db;
875 /* If the thread layer isn't initialized, return. It may just
876 be that the program uses clone, but does not use libthread_db. */
877 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
878 return;
880 /* find_one_thread calls into libthread_db which accesses memory via
881 the current thread. Temporarily switch to a thread we know is
882 stopped. */
883 scoped_restore_current_thread restore_thread;
884 switch_to_thread (parent_thr);
886 if (!find_one_thread (child_ptid))
887 warning ("Cannot find thread after clone.");