Remove support for thread events without PTRACE_EVENT_CLONE in GDBServer.
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2015 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 extern int debug_threads;
26
27 #include "gdb_proc_service.h"
28 #include "nat/gdb_thread_db.h"
29 #include "gdb_vecs.h"
30 #include "nat/linux-procfs.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_event_getmsg_ftype * td_ta_event_getmsg_p;
60 td_ta_set_event_ftype *td_ta_set_event_p;
61 td_ta_event_addr_ftype *td_ta_event_addr_p;
62 td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
63 td_thr_get_info_ftype *td_thr_get_info_p;
64 td_ta_thr_iter_ftype *td_ta_thr_iter_p;
65 td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
66 td_thr_tlsbase_ftype *td_thr_tlsbase_p;
67 td_symbol_list_ftype *td_symbol_list_p;
68 };
69
70 static char *libthread_db_search_path;
71
72 static int find_one_thread (ptid_t);
73 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
74
75 static const char *
76 thread_db_err_str (td_err_e err)
77 {
78 static char buf[64];
79
80 switch (err)
81 {
82 case TD_OK:
83 return "generic 'call succeeded'";
84 case TD_ERR:
85 return "generic error";
86 case TD_NOTHR:
87 return "no thread to satisfy query";
88 case TD_NOSV:
89 return "no sync handle to satisfy query";
90 case TD_NOLWP:
91 return "no LWP to satisfy query";
92 case TD_BADPH:
93 return "invalid process handle";
94 case TD_BADTH:
95 return "invalid thread handle";
96 case TD_BADSH:
97 return "invalid synchronization handle";
98 case TD_BADTA:
99 return "invalid thread agent";
100 case TD_BADKEY:
101 return "invalid key";
102 case TD_NOMSG:
103 return "no event message for getmsg";
104 case TD_NOFPREGS:
105 return "FPU register set not available";
106 case TD_NOLIBTHREAD:
107 return "application not linked with libthread";
108 case TD_NOEVENT:
109 return "requested event is not supported";
110 case TD_NOCAPAB:
111 return "capability not available";
112 case TD_DBERR:
113 return "debugger service failed";
114 case TD_NOAPLIC:
115 return "operation not applicable to";
116 case TD_NOTSD:
117 return "no thread-specific data for this thread";
118 case TD_MALLOC:
119 return "malloc failed";
120 case TD_PARTIALREG:
121 return "only part of register set was written/read";
122 case TD_NOXREGS:
123 return "X register set not available for this thread";
124 #ifdef HAVE_TD_VERSION
125 case TD_VERSION:
126 return "version mismatch between libthread_db and libpthread";
127 #endif
128 default:
129 xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
130 return buf;
131 }
132 }
133
134 #if 0
135 static char *
136 thread_db_state_str (td_thr_state_e state)
137 {
138 static char buf[64];
139
140 switch (state)
141 {
142 case TD_THR_STOPPED:
143 return "stopped by debugger";
144 case TD_THR_RUN:
145 return "runnable";
146 case TD_THR_ACTIVE:
147 return "active";
148 case TD_THR_ZOMBIE:
149 return "zombie";
150 case TD_THR_SLEEP:
151 return "sleeping";
152 case TD_THR_STOPPED_ASLEEP:
153 return "stopped by debugger AND blocked";
154 default:
155 xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
156 return buf;
157 }
158 }
159 #endif
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 thread_info *inferior;
168 struct lwp_info *lwp;
169 struct thread_db *thread_db = current_process ()->priv->thread_db;
170 int lwpid = ptid_get_lwp (ptid);
171
172 inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
173 lwp = get_thread_lwp (inferior);
174 if (lwp->thread_known)
175 return 1;
176
177 /* Get information about this thread. */
178 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
179 if (err != TD_OK)
180 error ("Cannot get thread handle for LWP %d: %s",
181 lwpid, thread_db_err_str (err));
182
183 err = thread_db->td_thr_get_info_p (&th, &ti);
184 if (err != TD_OK)
185 error ("Cannot get thread info for LWP %d: %s",
186 lwpid, thread_db_err_str (err));
187
188 if (debug_threads)
189 debug_printf ("Found thread %ld (LWP %d)\n",
190 (unsigned long) ti.ti_tid, ti.ti_lid);
191
192 if (lwpid != ti.ti_lid)
193 {
194 warning ("PID mismatch! Expected %ld, got %ld",
195 (long) lwpid, (long) ti.ti_lid);
196 return 0;
197 }
198
199 /* If the new thread ID is zero, a final thread ID will be available
200 later. Do not enable thread debugging yet. */
201 if (ti.ti_tid == 0)
202 return 0;
203
204 lwp->thread_known = 1;
205 lwp->th = th;
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_build (pid, ti_p->ti_lid, 0);
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 = linux_attach_lwp (ptid);
225 if (err != 0)
226 {
227 warning ("Could not attach to thread %ld (LWP %d): %s\n",
228 (unsigned long) ti_p->ti_tid, ti_p->ti_lid,
229 linux_ptrace_attach_fail_reason_string (ptid, err));
230 return 0;
231 }
232
233 lwp = find_lwp_pid (ptid);
234 gdb_assert (lwp != NULL);
235 lwp->thread_known = 1;
236 lwp->th = *th_p;
237
238 return 1;
239 }
240
241 /* Attach thread if we haven't seen it yet.
242 Increment *COUNTER if we have attached a new thread.
243 Return false on failure. */
244
245 static int
246 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
247 int *counter)
248 {
249 struct lwp_info *lwp;
250
251 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
252 if (lwp != NULL)
253 return 1;
254
255 if (!attach_thread (th_p, ti_p))
256 return 0;
257
258 if (counter != NULL)
259 *counter += 1;
260
261 return 1;
262 }
263
264 static int
265 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
266 {
267 td_thrinfo_t ti;
268 td_err_e err;
269 struct thread_db *thread_db = current_process ()->priv->thread_db;
270
271 err = thread_db->td_thr_get_info_p (th_p, &ti);
272 if (err != TD_OK)
273 error ("Cannot get thread info: %s", thread_db_err_str (err));
274
275 if (ti.ti_lid == -1)
276 {
277 /* A thread with kernel thread ID -1 is either a thread that
278 exited and was joined, or a thread that is being created but
279 hasn't started yet, and that is reusing the tcb/stack of a
280 thread that previously exited and was joined. (glibc marks
281 terminated and joined threads with kernel thread ID -1. See
282 glibc PR17707. */
283 if (debug_threads)
284 debug_printf ("thread_db: skipping exited and "
285 "joined thread (0x%lx)\n",
286 (unsigned long) ti.ti_tid);
287 return 0;
288 }
289
290 /* Check for zombies. */
291 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
292 return 0;
293
294 if (!maybe_attach_thread (th_p, &ti, (int *) data))
295 {
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;
299 }
300
301 return 0;
302 }
303
304 static void
305 thread_db_find_new_threads (void)
306 {
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;
311
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;
317
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)
323 {
324 int new_thread_count = 0;
325
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 if (debug_threads)
334 debug_printf ("Found %d threads in iteration %d.\n",
335 new_thread_count, iteration);
336
337 if (new_thread_count != 0)
338 {
339 /* Found new threads. Restart iteration from beginning. */
340 loop = -1;
341 }
342 }
343 if (err != TD_OK)
344 error ("Cannot find new threads: %s", thread_db_err_str (err));
345 }
346
347 /* Cache all future symbols that thread_db might request. We can not
348 request symbols at arbitrary states in the remote protocol, only
349 when the client tells us that new symbols are available. So when
350 we load the thread library, make sure to check the entire list. */
351
352 static void
353 thread_db_look_up_symbols (void)
354 {
355 struct thread_db *thread_db = current_process ()->priv->thread_db;
356 const char **sym_list;
357 CORE_ADDR unused;
358
359 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
360 look_up_one_symbol (*sym_list, &unused, 1);
361
362 /* We're not interested in any other libraries loaded after this
363 point, only in symbols in libpthread.so. */
364 thread_db->all_symbols_looked_up = 1;
365 }
366
367 int
368 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
369 {
370 struct thread_db *thread_db = current_process ()->priv->thread_db;
371 int may_ask_gdb = !thread_db->all_symbols_looked_up;
372
373 /* If we've passed the call to thread_db_look_up_symbols, then
374 anything not in the cache must not exist; we're not interested
375 in any libraries loaded after that point, only in symbols in
376 libpthread.so. It might not be an appropriate time to look
377 up a symbol, e.g. while we're trying to fetch registers. */
378 return look_up_one_symbol (name, addrp, may_ask_gdb);
379 }
380
381 int
382 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
383 CORE_ADDR load_module, CORE_ADDR *address)
384 {
385 psaddr_t addr;
386 td_err_e err;
387 struct lwp_info *lwp;
388 struct thread_info *saved_thread;
389 struct process_info *proc;
390 struct thread_db *thread_db;
391
392 proc = get_thread_process (thread);
393 thread_db = proc->priv->thread_db;
394
395 /* If the thread layer is not (yet) initialized, fail. */
396 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
397 return TD_ERR;
398
399 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
400 could work. */
401 if (thread_db->td_thr_tls_get_addr_p == NULL
402 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
403 return -1;
404
405 lwp = get_thread_lwp (thread);
406 if (!lwp->thread_known)
407 find_one_thread (thread->entry.id);
408 if (!lwp->thread_known)
409 return TD_NOTHR;
410
411 saved_thread = current_thread;
412 current_thread = thread;
413
414 if (load_module != 0)
415 {
416 /* Note the cast through uintptr_t: this interface only works if
417 a target address fits in a psaddr_t, which is a host pointer.
418 So a 32-bit debugger can not access 64-bit TLS through this. */
419 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
420 (psaddr_t) (uintptr_t) load_module,
421 offset, &addr);
422 }
423 else
424 {
425 /* This code path handles the case of -static -pthread executables:
426 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
427 For older GNU libc r_debug.r_map is NULL. For GNU libc after
428 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
429 The constant number 1 depends on GNU __libc_setup_tls
430 initialization of l_tls_modid to 1. */
431 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
432 addr = (char *) addr + offset;
433 }
434
435 current_thread = saved_thread;
436 if (err == TD_OK)
437 {
438 *address = (CORE_ADDR) (uintptr_t) addr;
439 return 0;
440 }
441 else
442 return err;
443 }
444
445 #ifdef USE_LIBTHREAD_DB_DIRECTLY
446
447 static int
448 thread_db_load_search (void)
449 {
450 td_err_e err;
451 struct thread_db *tdb;
452 struct process_info *proc = current_process ();
453
454 gdb_assert (proc->priv->thread_db == NULL);
455
456 tdb = XCNEW (struct thread_db);
457 proc->priv->thread_db = tdb;
458
459 tdb->td_ta_new_p = &td_ta_new;
460
461 /* Attempt to open a connection to the thread library. */
462 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
463 if (err != TD_OK)
464 {
465 if (debug_threads)
466 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
467 free (tdb);
468 proc->priv->thread_db = NULL;
469 return 0;
470 }
471
472 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
473 tdb->td_thr_get_info_p = &td_thr_get_info;
474 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
475 tdb->td_symbol_list_p = &td_symbol_list;
476
477 /* These are not essential. */
478 tdb->td_ta_event_addr_p = &td_ta_event_addr;
479 tdb->td_ta_set_event_p = &td_ta_set_event;
480 tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
481 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
482 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
483
484 return 1;
485 }
486
487 #else
488
489 static int
490 try_thread_db_load_1 (void *handle)
491 {
492 td_err_e err;
493 struct thread_db *tdb;
494 struct process_info *proc = current_process ();
495
496 gdb_assert (proc->priv->thread_db == NULL);
497
498 tdb = XCNEW (struct thread_db);
499 proc->priv->thread_db = tdb;
500
501 tdb->handle = handle;
502
503 /* Initialize pointers to the dynamic library functions we will use.
504 Essential functions first. */
505
506 #define CHK(required, a) \
507 do \
508 { \
509 if ((a) == NULL) \
510 { \
511 if (debug_threads) \
512 debug_printf ("dlsym: %s\n", dlerror ()); \
513 if (required) \
514 { \
515 free (tdb); \
516 proc->priv->thread_db = NULL; \
517 return 0; \
518 } \
519 } \
520 } \
521 while (0)
522
523 #define TDB_DLSYM(tdb, func) \
524 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
525
526 CHK (1, TDB_DLSYM (tdb, td_ta_new));
527
528 /* Attempt to open a connection to the thread library. */
529 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
530 if (err != TD_OK)
531 {
532 if (debug_threads)
533 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
534 free (tdb);
535 proc->priv->thread_db = NULL;
536 return 0;
537 }
538
539 CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
540 CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
541 CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
542 CHK (1, TDB_DLSYM (tdb, td_symbol_list));
543
544 /* These are not essential. */
545 CHK (0, TDB_DLSYM (tdb, td_ta_event_addr));
546 CHK (0, TDB_DLSYM (tdb, td_ta_set_event));
547 CHK (0, TDB_DLSYM (tdb, td_ta_event_getmsg));
548 CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
549 CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
550
551 #undef CHK
552 #undef TDB_DLSYM
553
554 return 1;
555 }
556
557 #ifdef HAVE_DLADDR
558
559 /* Lookup a library in which given symbol resides.
560 Note: this is looking in the GDBSERVER process, not in the inferior.
561 Returns library name, or NULL. */
562
563 static const char *
564 dladdr_to_soname (const void *addr)
565 {
566 Dl_info info;
567
568 if (dladdr (addr, &info) != 0)
569 return info.dli_fname;
570 return NULL;
571 }
572
573 #endif
574
575 static int
576 try_thread_db_load (const char *library)
577 {
578 void *handle;
579
580 if (debug_threads)
581 debug_printf ("Trying host libthread_db library: %s.\n",
582 library);
583 handle = dlopen (library, RTLD_NOW);
584 if (handle == NULL)
585 {
586 if (debug_threads)
587 debug_printf ("dlopen failed: %s.\n", dlerror ());
588 return 0;
589 }
590
591 #ifdef HAVE_DLADDR
592 if (debug_threads && strchr (library, '/') == NULL)
593 {
594 void *td_init;
595
596 td_init = dlsym (handle, "td_init");
597 if (td_init != NULL)
598 {
599 const char *const libpath = dladdr_to_soname (td_init);
600
601 if (libpath != NULL)
602 fprintf (stderr, "Host %s resolved to: %s.\n",
603 library, libpath);
604 }
605 }
606 #endif
607
608 if (try_thread_db_load_1 (handle))
609 return 1;
610
611 /* This library "refused" to work on current inferior. */
612 dlclose (handle);
613 return 0;
614 }
615
616 /* Handle $sdir in libthread-db-search-path.
617 Look for libthread_db in the system dirs, or wherever a plain
618 dlopen(file_without_path) will look.
619 The result is true for success. */
620
621 static int
622 try_thread_db_load_from_sdir (void)
623 {
624 return try_thread_db_load (LIBTHREAD_DB_SO);
625 }
626
627 /* Try to load libthread_db from directory DIR of length DIR_LEN.
628 The result is true for success. */
629
630 static int
631 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
632 {
633 char path[PATH_MAX];
634
635 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
636 {
637 char *cp = (char *) xmalloc (dir_len + 1);
638
639 memcpy (cp, dir, dir_len);
640 cp[dir_len] = '\0';
641 warning (_("libthread-db-search-path component too long,"
642 " ignored: %s."), cp);
643 free (cp);
644 return 0;
645 }
646
647 memcpy (path, dir, dir_len);
648 path[dir_len] = '/';
649 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
650 return try_thread_db_load (path);
651 }
652
653 /* Search libthread_db_search_path for libthread_db which "agrees"
654 to work on current inferior.
655 The result is true for success. */
656
657 static int
658 thread_db_load_search (void)
659 {
660 VEC (char_ptr) *dir_vec;
661 char *this_dir;
662 int i, rc = 0;
663
664 if (libthread_db_search_path == NULL)
665 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
666
667 dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
668
669 for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
670 {
671 const int pdir_len = sizeof ("$pdir") - 1;
672 size_t this_dir_len;
673
674 this_dir_len = strlen (this_dir);
675
676 if (strncmp (this_dir, "$pdir", pdir_len) == 0
677 && (this_dir[pdir_len] == '\0'
678 || this_dir[pdir_len] == '/'))
679 {
680 /* We don't maintain a list of loaded libraries so we don't know
681 where libpthread lives. We *could* fetch the info, but we don't
682 do that yet. Ignore it. */
683 }
684 else if (strcmp (this_dir, "$sdir") == 0)
685 {
686 if (try_thread_db_load_from_sdir ())
687 {
688 rc = 1;
689 break;
690 }
691 }
692 else
693 {
694 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
695 {
696 rc = 1;
697 break;
698 }
699 }
700 }
701
702 free_char_ptr_vec (dir_vec);
703 if (debug_threads)
704 debug_printf ("thread_db_load_search returning %d\n", rc);
705 return rc;
706 }
707
708 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
709
710 int
711 thread_db_init (void)
712 {
713 struct process_info *proc = current_process ();
714
715 /* FIXME drow/2004-10-16: This is the "overall process ID", which
716 GNU/Linux calls tgid, "thread group ID". When we support
717 attaching to threads, the original thread may not be the correct
718 thread. We would have to get the process ID from /proc for NPTL.
719 For LinuxThreads we could do something similar: follow the chain
720 of parent processes until we find the highest one we're attached
721 to, and use its tgid.
722
723 This isn't the only place in gdbserver that assumes that the first
724 process in the list is the thread group leader. */
725
726 if (thread_db_load_search ())
727 {
728 /* It's best to avoid td_ta_thr_iter if possible. That walks
729 data structures in the inferior's address space that may be
730 corrupted, or, if the target is running, the list may change
731 while we walk it. In the latter case, it's possible that a
732 thread exits just at the exact time that causes GDBserver to
733 get stuck in an infinite loop. As the kernel supports clone
734 events and /proc/PID/task/ exists, then we already know about
735 all threads in the process. When we need info out of
736 thread_db on a given thread (e.g., for TLS), we'll use
737 find_one_thread then. That uses thread_db entry points that
738 do not walk libpthread's thread list, so should be safe, as
739 well as more efficient. */
740 if (!linux_proc_task_list_dir_exists (pid_of (proc)))
741 thread_db_find_new_threads ();
742 thread_db_look_up_symbols ();
743 return 1;
744 }
745
746 return 0;
747 }
748
749 static int
750 any_thread_of (struct inferior_list_entry *entry, void *args)
751 {
752 int *pid_p = (int *) args;
753
754 if (ptid_get_pid (entry->id) == *pid_p)
755 return 1;
756
757 return 0;
758 }
759
760 static void
761 switch_to_process (struct process_info *proc)
762 {
763 int pid = pid_of (proc);
764
765 current_thread =
766 (struct thread_info *) find_inferior (&all_threads,
767 any_thread_of, &pid);
768 }
769
770 /* Disconnect from libthread_db and free resources. */
771
772 static void
773 disable_thread_event_reporting (struct process_info *proc)
774 {
775 struct thread_db *thread_db = proc->priv->thread_db;
776 if (thread_db)
777 {
778 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
779 td_thr_events_t *event);
780
781 #ifndef USE_LIBTHREAD_DB_DIRECTLY
782 td_ta_clear_event_p
783 = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
784 "td_ta_clear_event");
785 #else
786 td_ta_clear_event_p = &td_ta_clear_event;
787 #endif
788
789 if (td_ta_clear_event_p != NULL)
790 {
791 struct thread_info *saved_thread = current_thread;
792 td_thr_events_t events;
793
794 switch_to_process (proc);
795
796 /* Set the process wide mask saying we aren't interested
797 in any events anymore. */
798 td_event_fillset (&events);
799 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
800
801 current_thread = saved_thread;
802 }
803 }
804 }
805
806 void
807 thread_db_detach (struct process_info *proc)
808 {
809 struct thread_db *thread_db = proc->priv->thread_db;
810
811 if (thread_db)
812 {
813 disable_thread_event_reporting (proc);
814 }
815 }
816
817 /* Disconnect from libthread_db and free resources. */
818
819 void
820 thread_db_mourn (struct process_info *proc)
821 {
822 struct thread_db *thread_db = proc->priv->thread_db;
823 if (thread_db)
824 {
825 td_ta_delete_ftype *td_ta_delete_p;
826
827 #ifndef USE_LIBTHREAD_DB_DIRECTLY
828 td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
829 #else
830 td_ta_delete_p = &td_ta_delete;
831 #endif
832
833 if (td_ta_delete_p != NULL)
834 (*td_ta_delete_p) (thread_db->thread_agent);
835
836 #ifndef USE_LIBTHREAD_DB_DIRECTLY
837 dlclose (thread_db->handle);
838 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
839
840 free (thread_db);
841 proc->priv->thread_db = NULL;
842 }
843 }
844
845 /* Handle "set libthread-db-search-path" monitor command and return 1.
846 For any other command, return 0. */
847
848 int
849 thread_db_handle_monitor_command (char *mon)
850 {
851 const char *cmd = "set libthread-db-search-path";
852 size_t cmd_len = strlen (cmd);
853
854 if (strncmp (mon, cmd, cmd_len) == 0
855 && (mon[cmd_len] == '\0'
856 || mon[cmd_len] == ' '))
857 {
858 const char *cp = mon + cmd_len;
859
860 if (libthread_db_search_path != NULL)
861 free (libthread_db_search_path);
862
863 /* Skip leading space (if any). */
864 while (isspace (*cp))
865 ++cp;
866
867 if (*cp == '\0')
868 cp = LIBTHREAD_DB_SEARCH_PATH;
869 libthread_db_search_path = xstrdup (cp);
870
871 monitor_output ("libthread-db-search-path set to `");
872 monitor_output (libthread_db_search_path);
873 monitor_output ("'\n");
874 return 1;
875 }
876
877 /* Tell server.c to perform default processing. */
878 return 0;
879 }
This page took 0.047638 seconds and 4 git commands to generate.