Handle array- and string-like values in no-op pretty printers
[binutils-gdb.git] / gdbserver / thread-db.cc
1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include "server.h"
22
23 #include "linux-low.h"
24
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"
31
32 #ifndef USE_LIBTHREAD_DB_DIRECTLY
33 #include <dlfcn.h>
34 #endif
35 #include <limits.h>
36 #include <ctype.h>
37
38 struct thread_db
39 {
40 /* Structure that identifies the child process for the
41 <proc_service.h> interface. */
42 struct ps_prochandle proc_handle;
43
44 /* Connection to the libthread_db library. */
45 td_thragent_t *thread_agent;
46
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;
51
52 #ifndef USE_LIBTHREAD_DB_DIRECTLY
53 /* Handle of the libthread_db from dlopen. */
54 void *handle;
55 #endif
56
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;
65 };
66
67 static char *libthread_db_search_path;
68
69 static int find_one_thread (ptid_t);
70 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
71
72 static const char *
73 thread_db_err_str (td_err_e err)
74 {
75 static char buf[64];
76
77 switch (err)
78 {
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;
128 }
129 }
130
131 #if 0
132 static char *
133 thread_db_state_str (td_thr_state_e state)
134 {
135 static char buf[64];
136
137 switch (state)
138 {
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;
154 }
155 }
156 #endif
157
158 /* Get thread info about PTID. */
159
160 static int
161 find_one_thread (ptid_t ptid)
162 {
163 thread_info *thread = find_thread_ptid (ptid);
164 lwp_info *lwp = get_thread_lwp (thread);
165 if (lwp->thread_known)
166 return 1;
167
168 /* Get information about this thread. libthread_db will need to read some
169 memory, which will be done on the current process, so make PTID's process
170 the current one. */
171 process_info *proc = find_process_pid (ptid.pid ());
172 gdb_assert (proc != nullptr);
173
174 scoped_restore_current_thread restore_thread;
175 switch_to_process (proc);
176
177 thread_db *thread_db = proc->priv->thread_db;
178 td_thrhandle_t th;
179 int lwpid = ptid.lwp ();
180 td_err_e err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid,
181 &th);
182 if (err != TD_OK)
183 error ("Cannot get thread handle for LWP %d: %s",
184 lwpid, thread_db_err_str (err));
185
186 td_thrinfo_t ti;
187 err = thread_db->td_thr_get_info_p (&th, &ti);
188 if (err != TD_OK)
189 error ("Cannot get thread info for LWP %d: %s",
190 lwpid, thread_db_err_str (err));
191
192 threads_debug_printf ("Found thread %ld (LWP %d)",
193 (unsigned long) ti.ti_tid, ti.ti_lid);
194
195 if (lwpid != ti.ti_lid)
196 {
197 warning ("PID mismatch! Expected %ld, got %ld",
198 (long) lwpid, (long) ti.ti_lid);
199 return 0;
200 }
201
202 /* If the new thread ID is zero, a final thread ID will be available
203 later. Do not enable thread debugging yet. */
204 if (ti.ti_tid == 0)
205 return 0;
206
207 lwp->thread_known = 1;
208 lwp->th = th;
209 lwp->thread_handle = ti.ti_tid;
210
211 return 1;
212 }
213
214 /* Attach a thread. Return true on success. */
215
216 static int
217 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
218 {
219 struct process_info *proc = current_process ();
220 int pid = pid_of (proc);
221 ptid_t ptid = ptid_t (pid, ti_p->ti_lid);
222 struct lwp_info *lwp;
223 int err;
224
225 threads_debug_printf ("Attaching to thread %ld (LWP %d)",
226 (unsigned long) ti_p->ti_tid, ti_p->ti_lid);
227 err = the_linux_target->attach_lwp (ptid);
228 if (err != 0)
229 {
230 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
231
232 warning ("Could not attach to thread %ld (LWP %d): %s",
233 (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
234
235 return 0;
236 }
237
238 lwp = find_lwp_pid (ptid);
239 gdb_assert (lwp != NULL);
240 lwp->thread_known = 1;
241 lwp->th = *th_p;
242 lwp->thread_handle = ti_p->ti_tid;
243
244 return 1;
245 }
246
247 /* Attach thread if we haven't seen it yet.
248 Increment *COUNTER if we have attached a new thread.
249 Return false on failure. */
250
251 static int
252 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
253 int *counter)
254 {
255 struct lwp_info *lwp;
256
257 lwp = find_lwp_pid (ptid_t (ti_p->ti_lid));
258 if (lwp != NULL)
259 return 1;
260
261 if (!attach_thread (th_p, ti_p))
262 return 0;
263
264 if (counter != NULL)
265 *counter += 1;
266
267 return 1;
268 }
269
270 static int
271 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
272 {
273 td_thrinfo_t ti;
274 td_err_e err;
275 struct thread_db *thread_db = current_process ()->priv->thread_db;
276
277 err = thread_db->td_thr_get_info_p (th_p, &ti);
278 if (err != TD_OK)
279 error ("Cannot get thread info: %s", thread_db_err_str (err));
280
281 if (ti.ti_lid == -1)
282 {
283 /* A thread with kernel thread ID -1 is either a thread that
284 exited and was joined, or a thread that is being created but
285 hasn't started yet, and that is reusing the tcb/stack of a
286 thread that previously exited and was joined. (glibc marks
287 terminated and joined threads with kernel thread ID -1. See
288 glibc PR17707. */
289 threads_debug_printf ("thread_db: skipping exited and "
290 "joined thread (0x%lx)",
291 (unsigned long) ti.ti_tid);
292 return 0;
293 }
294
295 /* Check for zombies. */
296 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
297 return 0;
298
299 if (!maybe_attach_thread (th_p, &ti, (int *) data))
300 {
301 /* Terminate iteration early: we might be looking at stale data in
302 the inferior. The thread_db_find_new_threads will retry. */
303 return 1;
304 }
305
306 return 0;
307 }
308
309 static void
310 thread_db_find_new_threads (void)
311 {
312 td_err_e err;
313 ptid_t ptid = current_ptid;
314 struct thread_db *thread_db = current_process ()->priv->thread_db;
315 int loop, iteration;
316
317 /* This function is only called when we first initialize thread_db.
318 First locate the initial thread. If it is not ready for
319 debugging yet, then stop. */
320 if (find_one_thread (ptid) == 0)
321 return;
322
323 /* Require 4 successive iterations which do not find any new threads.
324 The 4 is a heuristic: there is an inherent race here, and I have
325 seen that 2 iterations in a row are not always sufficient to
326 "capture" all threads. */
327 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
328 {
329 int new_thread_count = 0;
330
331 /* Iterate over all user-space threads to discover new threads. */
332 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
333 find_new_threads_callback,
334 &new_thread_count,
335 TD_THR_ANY_STATE,
336 TD_THR_LOWEST_PRIORITY,
337 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
338 threads_debug_printf ("Found %d threads in iteration %d.",
339 new_thread_count, iteration);
340
341 if (new_thread_count != 0)
342 {
343 /* Found new threads. Restart iteration from beginning. */
344 loop = -1;
345 }
346 }
347 if (err != TD_OK)
348 error ("Cannot find new threads: %s", thread_db_err_str (err));
349 }
350
351 /* Cache all future symbols that thread_db might request. We can not
352 request symbols at arbitrary states in the remote protocol, only
353 when the client tells us that new symbols are available. So when
354 we load the thread library, make sure to check the entire list. */
355
356 static void
357 thread_db_look_up_symbols (void)
358 {
359 struct thread_db *thread_db = current_process ()->priv->thread_db;
360 const char **sym_list;
361 CORE_ADDR unused;
362
363 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
364 look_up_one_symbol (*sym_list, &unused, 1);
365
366 /* We're not interested in any other libraries loaded after this
367 point, only in symbols in libpthread.so. */
368 thread_db->all_symbols_looked_up = 1;
369 }
370
371 int
372 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
373 {
374 struct thread_db *thread_db = current_process ()->priv->thread_db;
375 int may_ask_gdb = !thread_db->all_symbols_looked_up;
376
377 /* If we've passed the call to thread_db_look_up_symbols, then
378 anything not in the cache must not exist; we're not interested
379 in any libraries loaded after that point, only in symbols in
380 libpthread.so. It might not be an appropriate time to look
381 up a symbol, e.g. while we're trying to fetch registers. */
382 return look_up_one_symbol (name, addrp, may_ask_gdb);
383 }
384
385 int
386 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
387 CORE_ADDR load_module, CORE_ADDR *address)
388 {
389 psaddr_t addr;
390 td_err_e err;
391 struct lwp_info *lwp;
392 struct process_info *proc;
393 struct thread_db *thread_db;
394
395 proc = get_thread_process (thread);
396 thread_db = proc->priv->thread_db;
397
398 /* If the thread layer is not (yet) initialized, fail. */
399 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
400 return TD_ERR;
401
402 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
403 could work. */
404 if (thread_db->td_thr_tls_get_addr_p == NULL
405 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
406 return -1;
407
408 lwp = get_thread_lwp (thread);
409 if (!lwp->thread_known)
410 find_one_thread (thread->id);
411 if (!lwp->thread_known)
412 return TD_NOTHR;
413
414 scoped_restore_current_thread restore_thread;
415 switch_to_thread (thread);
416
417 if (load_module != 0)
418 {
419 /* Note the cast through uintptr_t: this interface only works if
420 a target address fits in a psaddr_t, which is a host pointer.
421 So a 32-bit debugger can not access 64-bit TLS through this. */
422 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
423 (psaddr_t) (uintptr_t) load_module,
424 offset, &addr);
425 }
426 else
427 {
428 /* This code path handles the case of -static -pthread executables:
429 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
430 For older GNU libc r_debug.r_map is NULL. For GNU libc after
431 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
432 The constant number 1 depends on GNU __libc_setup_tls
433 initialization of l_tls_modid to 1. */
434 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
435 addr = (char *) addr + offset;
436 }
437
438 if (err == TD_OK)
439 {
440 *address = (CORE_ADDR) (uintptr_t) addr;
441 return 0;
442 }
443 else
444 return err;
445 }
446
447 /* See linux-low.h. */
448
449 bool
450 thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len)
451 {
452 struct thread_db *thread_db;
453 struct lwp_info *lwp;
454 thread_info *thread = find_thread_ptid (ptid);
455
456 if (thread == NULL)
457 return false;
458
459 thread_db = get_thread_process (thread)->priv->thread_db;
460
461 if (thread_db == NULL)
462 return false;
463
464 lwp = get_thread_lwp (thread);
465
466 if (!lwp->thread_known && !find_one_thread (thread->id))
467 return false;
468
469 gdb_assert (lwp->thread_known);
470
471 *handle = (gdb_byte *) &lwp->thread_handle;
472 *handle_len = sizeof (lwp->thread_handle);
473 return true;
474 }
475
476 #ifdef USE_LIBTHREAD_DB_DIRECTLY
477
478 static int
479 thread_db_load_search (void)
480 {
481 td_err_e err;
482 struct thread_db *tdb;
483 struct process_info *proc = current_process ();
484
485 gdb_assert (proc->priv->thread_db == NULL);
486
487 tdb = XCNEW (struct thread_db);
488 proc->priv->thread_db = tdb;
489
490 tdb->td_ta_new_p = &td_ta_new;
491
492 /* Attempt to open a connection to the thread library. */
493 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
494 if (err != TD_OK)
495 {
496 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
497 free (tdb);
498 proc->priv->thread_db = NULL;
499 return 0;
500 }
501
502 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
503 tdb->td_thr_get_info_p = &td_thr_get_info;
504 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
505 tdb->td_symbol_list_p = &td_symbol_list;
506
507 /* These are not essential. */
508 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
509 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
510
511 return 1;
512 }
513
514 #else
515
516 static int
517 try_thread_db_load_1 (void *handle)
518 {
519 td_err_e err;
520 struct thread_db *tdb;
521 struct process_info *proc = current_process ();
522
523 gdb_assert (proc->priv->thread_db == NULL);
524
525 tdb = XCNEW (struct thread_db);
526 proc->priv->thread_db = tdb;
527
528 tdb->handle = handle;
529
530 /* Initialize pointers to the dynamic library functions we will use.
531 Essential functions first. */
532
533 #define CHK(required, a) \
534 do \
535 { \
536 if ((a) == NULL) \
537 { \
538 threads_debug_printf ("dlsym: %s", dlerror ()); \
539 if (required) \
540 { \
541 free (tdb); \
542 proc->priv->thread_db = NULL; \
543 return 0; \
544 } \
545 } \
546 } \
547 while (0)
548
549 #define TDB_DLSYM(tdb, func) \
550 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
551
552 CHK (1, TDB_DLSYM (tdb, td_ta_new));
553
554 /* Attempt to open a connection to the thread library. */
555 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
556 if (err != TD_OK)
557 {
558 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
559 free (tdb);
560 proc->priv->thread_db = NULL;
561 return 0;
562 }
563
564 CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
565 CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
566 CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
567 CHK (1, TDB_DLSYM (tdb, td_symbol_list));
568
569 /* These are not essential. */
570 CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
571 CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
572
573 #undef CHK
574 #undef TDB_DLSYM
575
576 return 1;
577 }
578
579 #ifdef HAVE_DLADDR
580
581 /* Lookup a library in which given symbol resides.
582 Note: this is looking in the GDBSERVER process, not in the inferior.
583 Returns library name, or NULL. */
584
585 static const char *
586 dladdr_to_soname (const void *addr)
587 {
588 Dl_info info;
589
590 if (dladdr (addr, &info) != 0)
591 return info.dli_fname;
592 return NULL;
593 }
594
595 #endif
596
597 static int
598 try_thread_db_load (const char *library)
599 {
600 void *handle;
601
602 threads_debug_printf ("Trying host libthread_db library: %s.",
603 library);
604 handle = dlopen (library, RTLD_NOW);
605 if (handle == NULL)
606 {
607 threads_debug_printf ("dlopen failed: %s.", dlerror ());
608 return 0;
609 }
610
611 #ifdef HAVE_DLADDR
612 if (debug_threads && strchr (library, '/') == NULL)
613 {
614 void *td_init;
615
616 td_init = dlsym (handle, "td_init");
617 if (td_init != NULL)
618 {
619 const char *const libpath = dladdr_to_soname (td_init);
620
621 if (libpath != NULL)
622 threads_debug_printf ("Host %s resolved to: %s.", library, libpath);
623 }
624 }
625 #endif
626
627 if (try_thread_db_load_1 (handle))
628 return 1;
629
630 /* This library "refused" to work on current inferior. */
631 dlclose (handle);
632 return 0;
633 }
634
635 /* Handle $sdir in libthread-db-search-path.
636 Look for libthread_db in the system dirs, or wherever a plain
637 dlopen(file_without_path) will look.
638 The result is true for success. */
639
640 static int
641 try_thread_db_load_from_sdir (void)
642 {
643 return try_thread_db_load (LIBTHREAD_DB_SO);
644 }
645
646 /* Try to load libthread_db from directory DIR of length DIR_LEN.
647 The result is true for success. */
648
649 static int
650 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
651 {
652 char path[PATH_MAX];
653
654 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
655 {
656 char *cp = (char *) xmalloc (dir_len + 1);
657
658 memcpy (cp, dir, dir_len);
659 cp[dir_len] = '\0';
660 warning (_("libthread-db-search-path component too long,"
661 " ignored: %s."), cp);
662 free (cp);
663 return 0;
664 }
665
666 memcpy (path, dir, dir_len);
667 path[dir_len] = '/';
668 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
669 return try_thread_db_load (path);
670 }
671
672 /* Search libthread_db_search_path for libthread_db which "agrees"
673 to work on current inferior.
674 The result is true for success. */
675
676 static int
677 thread_db_load_search (void)
678 {
679 int rc = 0;
680
681 if (libthread_db_search_path == NULL)
682 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
683
684 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
685 = dirnames_to_char_ptr_vec (libthread_db_search_path);
686
687 for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
688 {
689 char *this_dir = this_dir_up.get ();
690 const int pdir_len = sizeof ("$pdir") - 1;
691 size_t this_dir_len;
692
693 this_dir_len = strlen (this_dir);
694
695 if (strncmp (this_dir, "$pdir", pdir_len) == 0
696 && (this_dir[pdir_len] == '\0'
697 || this_dir[pdir_len] == '/'))
698 {
699 /* We don't maintain a list of loaded libraries so we don't know
700 where libpthread lives. We *could* fetch the info, but we don't
701 do that yet. Ignore it. */
702 }
703 else if (strcmp (this_dir, "$sdir") == 0)
704 {
705 if (try_thread_db_load_from_sdir ())
706 {
707 rc = 1;
708 break;
709 }
710 }
711 else
712 {
713 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
714 {
715 rc = 1;
716 break;
717 }
718 }
719 }
720
721 threads_debug_printf ("thread_db_load_search returning %d", rc);
722 return rc;
723 }
724
725 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
726
727 int
728 thread_db_init (void)
729 {
730 struct process_info *proc = current_process ();
731
732 /* FIXME drow/2004-10-16: This is the "overall process ID", which
733 GNU/Linux calls tgid, "thread group ID". When we support
734 attaching to threads, the original thread may not be the correct
735 thread. We would have to get the process ID from /proc for NPTL.
736
737 This isn't the only place in gdbserver that assumes that the first
738 process in the list is the thread group leader. */
739
740 if (thread_db_load_search ())
741 {
742 /* It's best to avoid td_ta_thr_iter if possible. That walks
743 data structures in the inferior's address space that may be
744 corrupted, or, if the target is running, the list may change
745 while we walk it. In the latter case, it's possible that a
746 thread exits just at the exact time that causes GDBserver to
747 get stuck in an infinite loop. As the kernel supports clone
748 events and /proc/PID/task/ exists, then we already know about
749 all threads in the process. When we need info out of
750 thread_db on a given thread (e.g., for TLS), we'll use
751 find_one_thread then. That uses thread_db entry points that
752 do not walk libpthread's thread list, so should be safe, as
753 well as more efficient. */
754 if (!linux_proc_task_list_dir_exists (pid_of (proc)))
755 thread_db_find_new_threads ();
756 thread_db_look_up_symbols ();
757 return 1;
758 }
759
760 return 0;
761 }
762
763 /* Disconnect from libthread_db and free resources. */
764
765 static void
766 disable_thread_event_reporting (struct process_info *proc)
767 {
768 struct thread_db *thread_db = proc->priv->thread_db;
769 if (thread_db)
770 {
771 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
772 td_thr_events_t *event);
773
774 #ifndef USE_LIBTHREAD_DB_DIRECTLY
775 td_ta_clear_event_p
776 = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
777 "td_ta_clear_event");
778 #else
779 td_ta_clear_event_p = &td_ta_clear_event;
780 #endif
781
782 if (td_ta_clear_event_p != NULL)
783 {
784 scoped_restore_current_thread restore_thread;
785 td_thr_events_t events;
786
787 switch_to_process (proc);
788
789 /* Set the process wide mask saying we aren't interested
790 in any events anymore. */
791 td_event_fillset (&events);
792 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
793 }
794 }
795 }
796
797 void
798 thread_db_detach (struct process_info *proc)
799 {
800 struct thread_db *thread_db = proc->priv->thread_db;
801
802 if (thread_db)
803 {
804 disable_thread_event_reporting (proc);
805 }
806 }
807
808 /* Disconnect from libthread_db and free resources. */
809
810 void
811 thread_db_mourn (struct process_info *proc)
812 {
813 struct thread_db *thread_db = proc->priv->thread_db;
814 if (thread_db)
815 {
816 td_ta_delete_ftype *td_ta_delete_p;
817
818 #ifndef USE_LIBTHREAD_DB_DIRECTLY
819 td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
820 #else
821 td_ta_delete_p = &td_ta_delete;
822 #endif
823
824 if (td_ta_delete_p != NULL)
825 (*td_ta_delete_p) (thread_db->thread_agent);
826
827 #ifndef USE_LIBTHREAD_DB_DIRECTLY
828 dlclose (thread_db->handle);
829 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
830
831 free (thread_db);
832 proc->priv->thread_db = NULL;
833 }
834 }
835
836 /* Handle "set libthread-db-search-path" monitor command and return 1.
837 For any other command, return 0. */
838
839 int
840 thread_db_handle_monitor_command (char *mon)
841 {
842 const char *cmd = "set libthread-db-search-path";
843 size_t cmd_len = strlen (cmd);
844
845 if (strncmp (mon, cmd, cmd_len) == 0
846 && (mon[cmd_len] == '\0'
847 || mon[cmd_len] == ' '))
848 {
849 const char *cp = mon + cmd_len;
850
851 if (libthread_db_search_path != NULL)
852 free (libthread_db_search_path);
853
854 /* Skip leading space (if any). */
855 while (isspace (*cp))
856 ++cp;
857
858 if (*cp == '\0')
859 cp = LIBTHREAD_DB_SEARCH_PATH;
860 libthread_db_search_path = xstrdup (cp);
861
862 monitor_output ("libthread-db-search-path set to `");
863 monitor_output (libthread_db_search_path);
864 monitor_output ("'\n");
865 return 1;
866 }
867
868 /* Tell server.c to perform default processing. */
869 return 0;
870 }
871
872 /* See linux-low.h. */
873
874 void
875 thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid)
876 {
877 process_info *parent_proc = get_thread_process (parent_thr);
878 struct thread_db *thread_db = parent_proc->priv->thread_db;
879
880 /* If the thread layer isn't initialized, return. It may just
881 be that the program uses clone, but does not use libthread_db. */
882 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
883 return;
884
885 /* find_one_thread calls into libthread_db which accesses memory via
886 the current thread. Temporarily switch to a thread we know is
887 stopped. */
888 scoped_restore_current_thread restore_thread;
889 switch_to_thread (parent_thr);
890
891 if (!find_one_thread (child_ptid))
892 warning ("Cannot find thread after clone.");
893 }