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