1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2015 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/>. */
23 #include "linux-low.h"
25 extern int debug_threads
;
27 static int thread_db_use_events
;
29 #include "gdb_proc_service.h"
30 #include "nat/gdb_thread_db.h"
32 #include "nat/linux-procfs.h"
34 #ifndef USE_LIBTHREAD_DB_DIRECTLY
42 /* Structure that identifies the child process for the
43 <proc_service.h> interface. */
44 struct ps_prochandle proc_handle
;
46 /* Connection to the libthread_db library. */
47 td_thragent_t
*thread_agent
;
49 /* If this flag has been set, we've already asked GDB for all
50 symbols we might need; assume symbol cache misses are
52 int all_symbols_looked_up
;
54 #ifndef USE_LIBTHREAD_DB_DIRECTLY
55 /* Handle of the libthread_db from dlopen. */
59 /* Thread creation event breakpoint. The code at this location in
60 the child process will be called by the pthread library whenever
61 a new thread is created. By setting a special breakpoint at this
62 location, GDB can detect when a new thread is created. We obtain
63 this location via the td_ta_event_addr call. Note that if the
64 running kernel supports tracing clones, then we don't need to use
65 (and in fact don't use) this magic thread event breakpoint to
66 learn about threads. */
67 struct breakpoint
*td_create_bp
;
69 /* Addresses of libthread_db functions. */
70 td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
, td_thragent_t
**ta
);
71 td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
73 td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
74 td_thr_events_t
*event
);
75 td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
76 td_event_e event
, td_notify_t
*ptr
);
77 td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
, lwpid_t lwpid
,
79 td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
81 td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
, int event
);
82 td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
83 td_thr_iter_f
*callback
, void *cbdata_p
,
84 td_thr_state_e state
, int ti_pri
,
85 sigset_t
*ti_sigmask_p
,
86 unsigned int ti_user_flags
);
87 td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
89 size_t offset
, psaddr_t
*address
);
90 td_err_e (*td_thr_tlsbase_p
) (const td_thrhandle_t
*th
,
91 unsigned long int modid
,
93 const char ** (*td_symbol_list_p
) (void);
96 static char *libthread_db_search_path
;
98 static int find_one_thread (ptid_t
);
99 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
102 thread_db_err_str (td_err_e err
)
109 return "generic 'call succeeded'";
111 return "generic error";
113 return "no thread to satisfy query";
115 return "no sync handle to satisfy query";
117 return "no LWP to satisfy query";
119 return "invalid process handle";
121 return "invalid thread handle";
123 return "invalid synchronization handle";
125 return "invalid thread agent";
127 return "invalid key";
129 return "no event message for getmsg";
131 return "FPU register set not available";
133 return "application not linked with libthread";
135 return "requested event is not supported";
137 return "capability not available";
139 return "debugger service failed";
141 return "operation not applicable to";
143 return "no thread-specific data for this thread";
145 return "malloc failed";
147 return "only part of register set was written/read";
149 return "X register set not available for this thread";
150 #ifdef HAVE_TD_VERSION
152 return "version mismatch between libthread_db and libpthread";
155 xsnprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
162 thread_db_state_str (td_thr_state_e state
)
169 return "stopped by debugger";
178 case TD_THR_STOPPED_ASLEEP
:
179 return "stopped by debugger AND blocked";
181 xsnprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
188 thread_db_create_event (CORE_ADDR where
)
192 struct lwp_info
*lwp
;
193 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
195 gdb_assert (thread_db
->td_ta_event_getmsg_p
!= NULL
);
198 debug_printf ("Thread creation event.\n");
200 /* FIXME: This assumes we don't get another event.
201 In the LinuxThreads implementation, this is safe,
202 because all events come from the manager thread
203 (except for its own creation, of course). */
204 err
= thread_db
->td_ta_event_getmsg_p (thread_db
->thread_agent
, &msg
);
206 fprintf (stderr
, "thread getmsg err: %s\n",
207 thread_db_err_str (err
));
209 /* If we do not know about the main thread yet, this would be a good time to
210 find it. We need to do this to pick up the main thread before any newly
212 lwp
= get_thread_lwp (current_thread
);
213 if (lwp
->thread_known
== 0)
214 find_one_thread (current_thread
->entry
.id
);
216 /* msg.event == TD_EVENT_CREATE */
218 find_new_threads_callback (msg
.th_p
, NULL
);
224 thread_db_enable_reporting (void)
226 td_thr_events_t events
;
229 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
231 if (thread_db
->td_ta_set_event_p
== NULL
232 || thread_db
->td_ta_event_addr_p
== NULL
233 || thread_db
->td_ta_event_getmsg_p
== NULL
)
234 /* This libthread_db is missing required support. */
237 /* Set the process wide mask saying which events we're interested in. */
238 td_event_emptyset (&events
);
239 td_event_addset (&events
, TD_CREATE
);
241 err
= thread_db
->td_ta_set_event_p (thread_db
->thread_agent
, &events
);
244 warning ("Unable to set global thread event mask: %s",
245 thread_db_err_str (err
));
249 /* Get address for thread creation breakpoint. */
250 err
= thread_db
->td_ta_event_addr_p (thread_db
->thread_agent
, TD_CREATE
,
254 warning ("Unable to get location for thread creation breakpoint: %s",
255 thread_db_err_str (err
));
258 thread_db
->td_create_bp
259 = set_breakpoint_at ((CORE_ADDR
) (unsigned long) notify
.u
.bptaddr
,
260 thread_db_create_event
);
266 find_one_thread (ptid_t ptid
)
271 struct thread_info
*inferior
;
272 struct lwp_info
*lwp
;
273 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
274 int lwpid
= ptid_get_lwp (ptid
);
276 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
277 lwp
= get_thread_lwp (inferior
);
278 if (lwp
->thread_known
)
281 /* Get information about this thread. */
282 err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
, &th
);
284 error ("Cannot get thread handle for LWP %d: %s",
285 lwpid
, thread_db_err_str (err
));
287 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
289 error ("Cannot get thread info for LWP %d: %s",
290 lwpid
, thread_db_err_str (err
));
293 debug_printf ("Found thread %ld (LWP %d)\n",
294 ti
.ti_tid
, ti
.ti_lid
);
296 if (lwpid
!= ti
.ti_lid
)
298 warning ("PID mismatch! Expected %ld, got %ld",
299 (long) lwpid
, (long) ti
.ti_lid
);
303 if (thread_db_use_events
)
305 err
= thread_db
->td_thr_event_enable_p (&th
, 1);
307 error ("Cannot enable thread event reporting for %d: %s",
308 ti
.ti_lid
, thread_db_err_str (err
));
311 /* If the new thread ID is zero, a final thread ID will be available
312 later. Do not enable thread debugging yet. */
316 lwp
->thread_known
= 1;
322 /* Attach a thread. Return true on success. */
325 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
327 struct process_info
*proc
= current_process ();
328 int pid
= pid_of (proc
);
329 ptid_t ptid
= ptid_build (pid
, ti_p
->ti_lid
, 0);
330 struct lwp_info
*lwp
;
334 debug_printf ("Attaching to thread %ld (LWP %d)\n",
335 ti_p
->ti_tid
, ti_p
->ti_lid
);
336 err
= linux_attach_lwp (ptid
);
339 warning ("Could not attach to thread %ld (LWP %d): %s\n",
340 ti_p
->ti_tid
, ti_p
->ti_lid
,
341 linux_ptrace_attach_fail_reason_string (ptid
, err
));
345 lwp
= find_lwp_pid (ptid
);
346 gdb_assert (lwp
!= NULL
);
347 lwp
->thread_known
= 1;
350 if (thread_db_use_events
)
353 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
355 err
= thread_db
->td_thr_event_enable_p (th_p
, 1);
357 error ("Cannot enable thread event reporting for %d: %s",
358 ti_p
->ti_lid
, thread_db_err_str (err
));
364 /* Attach thread if we haven't seen it yet.
365 Increment *COUNTER if we have attached a new thread.
366 Return false on failure. */
369 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
372 struct lwp_info
*lwp
;
374 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
378 if (!attach_thread (th_p
, ti_p
))
388 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
392 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
394 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
396 error ("Cannot get thread info: %s", thread_db_err_str (err
));
400 /* A thread with kernel thread ID -1 is either a thread that
401 exited and was joined, or a thread that is being created but
402 hasn't started yet, and that is reusing the tcb/stack of a
403 thread that previously exited and was joined. (glibc marks
404 terminated and joined threads with kernel thread ID -1. See
407 debug_printf ("thread_db: skipping exited and "
408 "joined thread (0x%lx)\n", ti
.ti_tid
);
412 /* Check for zombies. */
413 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
416 if (!maybe_attach_thread (th_p
, &ti
, (int *) data
))
418 /* Terminate iteration early: we might be looking at stale data in
419 the inferior. The thread_db_find_new_threads will retry. */
427 thread_db_find_new_threads (void)
430 ptid_t ptid
= current_ptid
;
431 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
434 /* This function is only called when we first initialize thread_db.
435 First locate the initial thread. If it is not ready for
436 debugging yet, then stop. */
437 if (find_one_thread (ptid
) == 0)
440 /* Require 4 successive iterations which do not find any new threads.
441 The 4 is a heuristic: there is an inherent race here, and I have
442 seen that 2 iterations in a row are not always sufficient to
443 "capture" all threads. */
444 for (loop
= 0, iteration
= 0; loop
< 4; ++loop
, ++iteration
)
446 int new_thread_count
= 0;
448 /* Iterate over all user-space threads to discover new threads. */
449 err
= thread_db
->td_ta_thr_iter_p (thread_db
->thread_agent
,
450 find_new_threads_callback
,
453 TD_THR_LOWEST_PRIORITY
,
454 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
456 debug_printf ("Found %d threads in iteration %d.\n",
457 new_thread_count
, iteration
);
459 if (new_thread_count
!= 0)
461 /* Found new threads. Restart iteration from beginning. */
466 error ("Cannot find new threads: %s", thread_db_err_str (err
));
469 /* Cache all future symbols that thread_db might request. We can not
470 request symbols at arbitrary states in the remote protocol, only
471 when the client tells us that new symbols are available. So when
472 we load the thread library, make sure to check the entire list. */
475 thread_db_look_up_symbols (void)
477 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
478 const char **sym_list
;
481 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
482 look_up_one_symbol (*sym_list
, &unused
, 1);
484 /* We're not interested in any other libraries loaded after this
485 point, only in symbols in libpthread.so. */
486 thread_db
->all_symbols_looked_up
= 1;
490 thread_db_look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
492 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
493 int may_ask_gdb
= !thread_db
->all_symbols_looked_up
;
495 /* If we've passed the call to thread_db_look_up_symbols, then
496 anything not in the cache must not exist; we're not interested
497 in any libraries loaded after that point, only in symbols in
498 libpthread.so. It might not be an appropriate time to look
499 up a symbol, e.g. while we're trying to fetch registers. */
500 return look_up_one_symbol (name
, addrp
, may_ask_gdb
);
504 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
505 CORE_ADDR load_module
, CORE_ADDR
*address
)
509 struct lwp_info
*lwp
;
510 struct thread_info
*saved_thread
;
511 struct process_info
*proc
;
512 struct thread_db
*thread_db
;
514 proc
= get_thread_process (thread
);
515 thread_db
= proc
->priv
->thread_db
;
517 /* If the thread layer is not (yet) initialized, fail. */
518 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
521 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
523 if (thread_db
->td_thr_tls_get_addr_p
== NULL
524 || (load_module
== 0 && thread_db
->td_thr_tlsbase_p
== NULL
))
527 lwp
= get_thread_lwp (thread
);
528 if (!lwp
->thread_known
)
529 find_one_thread (thread
->entry
.id
);
530 if (!lwp
->thread_known
)
533 saved_thread
= current_thread
;
534 current_thread
= thread
;
536 if (load_module
!= 0)
538 /* Note the cast through uintptr_t: this interface only works if
539 a target address fits in a psaddr_t, which is a host pointer.
540 So a 32-bit debugger can not access 64-bit TLS through this. */
541 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
542 (psaddr_t
) (uintptr_t) load_module
,
547 /* This code path handles the case of -static -pthread executables:
548 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
549 For older GNU libc r_debug.r_map is NULL. For GNU libc after
550 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
551 The constant number 1 depends on GNU __libc_setup_tls
552 initialization of l_tls_modid to 1. */
553 err
= thread_db
->td_thr_tlsbase_p (&lwp
->th
, 1, &addr
);
554 addr
= (char *) addr
+ offset
;
557 current_thread
= saved_thread
;
560 *address
= (CORE_ADDR
) (uintptr_t) addr
;
567 #ifdef USE_LIBTHREAD_DB_DIRECTLY
570 thread_db_load_search (void)
573 struct thread_db
*tdb
;
574 struct process_info
*proc
= current_process ();
576 gdb_assert (proc
->priv
->thread_db
== NULL
);
578 tdb
= xcalloc (1, sizeof (*tdb
));
579 proc
->priv
->thread_db
= tdb
;
581 tdb
->td_ta_new_p
= &td_ta_new
;
583 /* Attempt to open a connection to the thread library. */
584 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
588 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
590 proc
->priv
->thread_db
= NULL
;
594 tdb
->td_ta_map_lwp2thr_p
= &td_ta_map_lwp2thr
;
595 tdb
->td_thr_get_info_p
= &td_thr_get_info
;
596 tdb
->td_ta_thr_iter_p
= &td_ta_thr_iter
;
597 tdb
->td_symbol_list_p
= &td_symbol_list
;
599 /* This is required only when thread_db_use_events is on. */
600 tdb
->td_thr_event_enable_p
= &td_thr_event_enable
;
602 /* These are not essential. */
603 tdb
->td_ta_event_addr_p
= &td_ta_event_addr
;
604 tdb
->td_ta_set_event_p
= &td_ta_set_event
;
605 tdb
->td_ta_event_getmsg_p
= &td_ta_event_getmsg
;
606 tdb
->td_thr_tls_get_addr_p
= &td_thr_tls_get_addr
;
607 tdb
->td_thr_tlsbase_p
= &td_thr_tlsbase
;
615 try_thread_db_load_1 (void *handle
)
618 struct thread_db
*tdb
;
619 struct process_info
*proc
= current_process ();
621 gdb_assert (proc
->priv
->thread_db
== NULL
);
623 tdb
= xcalloc (1, sizeof (*tdb
));
624 proc
->priv
->thread_db
= tdb
;
626 tdb
->handle
= handle
;
628 /* Initialize pointers to the dynamic library functions we will use.
629 Essential functions first. */
631 #define CHK(required, a) \
637 debug_printf ("dlsym: %s\n", dlerror ()); \
641 proc->priv->thread_db = NULL; \
648 CHK (1, tdb
->td_ta_new_p
= dlsym (handle
, "td_ta_new"));
650 /* Attempt to open a connection to the thread library. */
651 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
655 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
657 proc
->priv
->thread_db
= NULL
;
661 CHK (1, tdb
->td_ta_map_lwp2thr_p
= dlsym (handle
, "td_ta_map_lwp2thr"));
662 CHK (1, tdb
->td_thr_get_info_p
= dlsym (handle
, "td_thr_get_info"));
663 CHK (1, tdb
->td_ta_thr_iter_p
= dlsym (handle
, "td_ta_thr_iter"));
664 CHK (1, tdb
->td_symbol_list_p
= dlsym (handle
, "td_symbol_list"));
666 /* This is required only when thread_db_use_events is on. */
667 CHK (thread_db_use_events
,
668 tdb
->td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable"));
670 /* These are not essential. */
671 CHK (0, tdb
->td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr"));
672 CHK (0, tdb
->td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event"));
673 CHK (0, tdb
->td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg"));
674 CHK (0, tdb
->td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr"));
675 CHK (0, tdb
->td_thr_tlsbase_p
= dlsym (handle
, "td_thr_tlsbase"));
684 /* Lookup a library in which given symbol resides.
685 Note: this is looking in the GDBSERVER process, not in the inferior.
686 Returns library name, or NULL. */
689 dladdr_to_soname (const void *addr
)
693 if (dladdr (addr
, &info
) != 0)
694 return info
.dli_fname
;
701 try_thread_db_load (const char *library
)
706 debug_printf ("Trying host libthread_db library: %s.\n",
708 handle
= dlopen (library
, RTLD_NOW
);
712 debug_printf ("dlopen failed: %s.\n", dlerror ());
717 if (debug_threads
&& strchr (library
, '/') == NULL
)
721 td_init
= dlsym (handle
, "td_init");
724 const char *const libpath
= dladdr_to_soname (td_init
);
727 fprintf (stderr
, "Host %s resolved to: %s.\n",
733 if (try_thread_db_load_1 (handle
))
736 /* This library "refused" to work on current inferior. */
741 /* Handle $sdir in libthread-db-search-path.
742 Look for libthread_db in the system dirs, or wherever a plain
743 dlopen(file_without_path) will look.
744 The result is true for success. */
747 try_thread_db_load_from_sdir (void)
749 return try_thread_db_load (LIBTHREAD_DB_SO
);
752 /* Try to load libthread_db from directory DIR of length DIR_LEN.
753 The result is true for success. */
756 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
760 if (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
762 char *cp
= xmalloc (dir_len
+ 1);
764 memcpy (cp
, dir
, dir_len
);
766 warning (_("libthread-db-search-path component too long,"
767 " ignored: %s."), cp
);
772 memcpy (path
, dir
, dir_len
);
774 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
775 return try_thread_db_load (path
);
778 /* Search libthread_db_search_path for libthread_db which "agrees"
779 to work on current inferior.
780 The result is true for success. */
783 thread_db_load_search (void)
785 VEC (char_ptr
) *dir_vec
;
789 if (libthread_db_search_path
== NULL
)
790 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
792 dir_vec
= dirnames_to_char_ptr_vec (libthread_db_search_path
);
794 for (i
= 0; VEC_iterate (char_ptr
, dir_vec
, i
, this_dir
); ++i
)
796 const int pdir_len
= sizeof ("$pdir") - 1;
799 this_dir_len
= strlen (this_dir
);
801 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
802 && (this_dir
[pdir_len
] == '\0'
803 || this_dir
[pdir_len
] == '/'))
805 /* We don't maintain a list of loaded libraries so we don't know
806 where libpthread lives. We *could* fetch the info, but we don't
807 do that yet. Ignore it. */
809 else if (strcmp (this_dir
, "$sdir") == 0)
811 if (try_thread_db_load_from_sdir ())
819 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
827 free_char_ptr_vec (dir_vec
);
829 debug_printf ("thread_db_load_search returning %d\n", rc
);
833 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
836 thread_db_init (int use_events
)
838 struct process_info
*proc
= current_process ();
840 /* FIXME drow/2004-10-16: This is the "overall process ID", which
841 GNU/Linux calls tgid, "thread group ID". When we support
842 attaching to threads, the original thread may not be the correct
843 thread. We would have to get the process ID from /proc for NPTL.
844 For LinuxThreads we could do something similar: follow the chain
845 of parent processes until we find the highest one we're attached
846 to, and use its tgid.
848 This isn't the only place in gdbserver that assumes that the first
849 process in the list is the thread group leader. */
851 thread_db_use_events
= use_events
;
853 if (thread_db_load_search ())
855 if (use_events
&& thread_db_enable_reporting () == 0)
857 /* Keep trying; maybe event reporting will work later. */
858 thread_db_mourn (proc
);
862 /* It's best to avoid td_ta_thr_iter if possible. That walks
863 data structures in the inferior's address space that may be
864 corrupted, or, if the target is running, the list may change
865 while we walk it. In the latter case, it's possible that a
866 thread exits just at the exact time that causes GDBserver to
867 get stuck in an infinite loop. If the kernel supports clone
868 events, and /proc/PID/task/ exits, then we already know about
869 all threads in the process. When we need info out of
870 thread_db on a given thread (e.g., for TLS), we'll use
871 find_one_thread then. That uses thread_db entry points that
872 do not walk libpthread's thread list, so should be safe, as
873 well as more efficient. */
875 || !linux_proc_task_list_dir_exists (pid_of (proc
)))
876 thread_db_find_new_threads ();
877 thread_db_look_up_symbols ();
885 any_thread_of (struct inferior_list_entry
*entry
, void *args
)
889 if (ptid_get_pid (entry
->id
) == *pid_p
)
896 switch_to_process (struct process_info
*proc
)
898 int pid
= pid_of (proc
);
901 (struct thread_info
*) find_inferior (&all_threads
,
902 any_thread_of
, &pid
);
905 /* Disconnect from libthread_db and free resources. */
908 disable_thread_event_reporting (struct process_info
*proc
)
910 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
913 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
914 td_thr_events_t
*event
);
916 #ifndef USE_LIBTHREAD_DB_DIRECTLY
917 td_ta_clear_event_p
= dlsym (thread_db
->handle
, "td_ta_clear_event");
919 td_ta_clear_event_p
= &td_ta_clear_event
;
922 if (td_ta_clear_event_p
!= NULL
)
924 struct thread_info
*saved_thread
= current_thread
;
925 td_thr_events_t events
;
927 switch_to_process (proc
);
929 /* Set the process wide mask saying we aren't interested
930 in any events anymore. */
931 td_event_fillset (&events
);
932 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
934 current_thread
= saved_thread
;
940 remove_thread_event_breakpoints (struct process_info
*proc
)
942 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
944 if (thread_db
->td_create_bp
!= NULL
)
946 struct thread_info
*saved_thread
= current_thread
;
948 switch_to_process (proc
);
950 delete_breakpoint (thread_db
->td_create_bp
);
951 thread_db
->td_create_bp
= NULL
;
953 current_thread
= saved_thread
;
958 thread_db_detach (struct process_info
*proc
)
960 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
964 disable_thread_event_reporting (proc
);
965 remove_thread_event_breakpoints (proc
);
969 /* Disconnect from libthread_db and free resources. */
972 thread_db_mourn (struct process_info
*proc
)
974 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
977 td_err_e (*td_ta_delete_p
) (td_thragent_t
*);
979 #ifndef USE_LIBTHREAD_DB_DIRECTLY
980 td_ta_delete_p
= dlsym (thread_db
->handle
, "td_ta_delete");
982 td_ta_delete_p
= &td_ta_delete
;
985 if (td_ta_delete_p
!= NULL
)
986 (*td_ta_delete_p
) (thread_db
->thread_agent
);
988 #ifndef USE_LIBTHREAD_DB_DIRECTLY
989 dlclose (thread_db
->handle
);
990 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
993 proc
->priv
->thread_db
= NULL
;
997 /* Handle "set libthread-db-search-path" monitor command and return 1.
998 For any other command, return 0. */
1001 thread_db_handle_monitor_command (char *mon
)
1003 const char *cmd
= "set libthread-db-search-path";
1004 size_t cmd_len
= strlen (cmd
);
1006 if (strncmp (mon
, cmd
, cmd_len
) == 0
1007 && (mon
[cmd_len
] == '\0'
1008 || mon
[cmd_len
] == ' '))
1010 const char *cp
= mon
+ cmd_len
;
1012 if (libthread_db_search_path
!= NULL
)
1013 free (libthread_db_search_path
);
1015 /* Skip leading space (if any). */
1016 while (isspace (*cp
))
1020 cp
= LIBTHREAD_DB_SEARCH_PATH
;
1021 libthread_db_search_path
= xstrdup (cp
);
1023 monitor_output ("libthread-db-search-path set to `");
1024 monitor_output (libthread_db_search_path
);
1025 monitor_output ("'\n");
1029 /* Tell server.c to perform default processing. */