]>
Commit | Line | Data |
---|---|---|
fb0e1ba7 | 1 | /* libthread_db assisted debugging support, generic parts. |
1bac305b | 2 | |
6aba47ca | 3 | Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007 |
10d6c8cd | 4 | Free Software Foundation, Inc. |
fb0e1ba7 MK |
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 2 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, write to the Free Software | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
fb0e1ba7 MK |
22 | |
23 | #include "defs.h" | |
24 | ||
25 | #include "gdb_assert.h" | |
26 | #include <dlfcn.h> | |
27 | #include "gdb_proc_service.h" | |
28 | #include "gdb_thread_db.h" | |
29 | ||
bda9cb72 | 30 | #include "bfd.h" |
93ad78a7 | 31 | #include "exceptions.h" |
fb0e1ba7 MK |
32 | #include "gdbthread.h" |
33 | #include "inferior.h" | |
bda9cb72 MK |
34 | #include "symfile.h" |
35 | #include "objfiles.h" | |
fb0e1ba7 | 36 | #include "target.h" |
4e052eda | 37 | #include "regcache.h" |
3f47be5c | 38 | #include "solib-svr4.h" |
16451949 | 39 | #include "gdbcore.h" |
0ec9a092 | 40 | #include "linux-nat.h" |
fb0e1ba7 | 41 | |
979894f2 NR |
42 | #include <signal.h> |
43 | ||
a2f23071 DJ |
44 | #ifdef HAVE_GNU_LIBC_VERSION_H |
45 | #include <gnu/libc-version.h> | |
46 | #endif | |
47 | ||
fb0e1ba7 MK |
48 | #ifndef LIBTHREAD_DB_SO |
49 | #define LIBTHREAD_DB_SO "libthread_db.so.1" | |
50 | #endif | |
51 | ||
8605d56e AC |
52 | /* If we're running on GNU/Linux, we must explicitly attach to any new |
53 | threads. */ | |
fb0e1ba7 MK |
54 | |
55 | /* FIXME: There is certainly some room for improvements: | |
56 | - Cache LWP ids. | |
57 | - Bypass libthread_db when fetching or storing registers for | |
58 | threads bound to a LWP. */ | |
59 | ||
60 | /* This module's target vector. */ | |
61 | static struct target_ops thread_db_ops; | |
62 | ||
63 | /* The target vector that we call for things this module can't handle. */ | |
64 | static struct target_ops *target_beneath; | |
65 | ||
66 | /* Pointer to the next function on the objfile event chain. */ | |
b4acd559 | 67 | static void (*target_new_objfile_chain) (struct objfile * objfile); |
fb0e1ba7 MK |
68 | |
69 | /* Non-zero if we're using this module's target vector. */ | |
70 | static int using_thread_db; | |
71 | ||
72 | /* Non-zero if we have determined the signals used by the threads | |
73 | library. */ | |
74 | static int thread_signals; | |
75 | static sigset_t thread_stop_set; | |
76 | static sigset_t thread_print_set; | |
77 | ||
78 | /* Structure that identifies the child process for the | |
79 | <proc_service.h> interface. */ | |
80 | static struct ps_prochandle proc_handle; | |
81 | ||
82 | /* Connection to the libthread_db library. */ | |
83 | static td_thragent_t *thread_agent; | |
84 | ||
85 | /* Pointers to the libthread_db functions. */ | |
86 | ||
87 | static td_err_e (*td_init_p) (void); | |
88 | ||
b4acd559 JJ |
89 | static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, |
90 | td_thragent_t **ta); | |
fb0e1ba7 MK |
91 | static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, |
92 | td_thrhandle_t *__th); | |
b4acd559 JJ |
93 | static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, |
94 | lwpid_t lwpid, td_thrhandle_t *th); | |
fb0e1ba7 | 95 | static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, |
b4acd559 JJ |
96 | td_thr_iter_f *callback, void *cbdata_p, |
97 | td_thr_state_e state, int ti_pri, | |
98 | sigset_t *ti_sigmask_p, | |
fb0e1ba7 MK |
99 | unsigned int ti_user_flags); |
100 | static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, | |
101 | td_event_e event, td_notify_t *ptr); | |
102 | static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, | |
103 | td_thr_events_t *event); | |
104 | static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, | |
105 | td_event_msg_t *msg); | |
106 | ||
107 | static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th); | |
108 | static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, | |
109 | td_thrinfo_t *infop); | |
b4acd559 JJ |
110 | static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, |
111 | int event); | |
fb0e1ba7 | 112 | |
3f47be5c | 113 | static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, |
b4acd559 JJ |
114 | void *map_address, |
115 | size_t offset, void **address); | |
3f47be5c | 116 | |
fb0e1ba7 MK |
117 | /* Location of the thread creation event breakpoint. The code at this |
118 | location in the child process will be called by the pthread library | |
119 | whenever a new thread is created. By setting a special breakpoint | |
120 | at this location, GDB can detect when a new thread is created. We | |
121 | obtain this location via the td_ta_event_addr call. */ | |
122 | static CORE_ADDR td_create_bp_addr; | |
123 | ||
124 | /* Location of the thread death event breakpoint. */ | |
125 | static CORE_ADDR td_death_bp_addr; | |
126 | ||
127 | /* Prototypes for local functions. */ | |
bda9cb72 | 128 | static void thread_db_find_new_threads (void); |
5365276c DJ |
129 | static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
130 | const td_thrinfo_t *ti_p, int verbose); | |
a2f23071 | 131 | static void detach_thread (ptid_t ptid, int verbose); |
fb0e1ba7 MK |
132 | \f |
133 | ||
134 | /* Building process ids. */ | |
135 | ||
ca6724c1 KB |
136 | #define GET_PID(ptid) ptid_get_pid (ptid) |
137 | #define GET_LWP(ptid) ptid_get_lwp (ptid) | |
138 | #define GET_THREAD(ptid) ptid_get_tid (ptid) | |
fb0e1ba7 | 139 | |
ca6724c1 KB |
140 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) |
141 | #define is_thread(ptid) (GET_THREAD (ptid) != 0) | |
fb0e1ba7 | 142 | |
ca6724c1 | 143 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) |
fb0e1ba7 MK |
144 | \f |
145 | ||
5365276c DJ |
146 | /* Use "struct private_thread_info" to cache thread state. This is |
147 | a substantial optimization. */ | |
148 | ||
fb0e1ba7 MK |
149 | struct private_thread_info |
150 | { | |
a2f23071 DJ |
151 | /* Flag set when we see a TD_DEATH event for this thread. */ |
152 | unsigned int dying:1; | |
153 | ||
5365276c | 154 | /* Cached thread state. */ |
b4acd559 JJ |
155 | unsigned int th_valid:1; |
156 | unsigned int ti_valid:1; | |
5365276c DJ |
157 | |
158 | td_thrhandle_t th; | |
159 | td_thrinfo_t ti; | |
fb0e1ba7 | 160 | }; |
fb0e1ba7 | 161 | \f |
21bf60fe | 162 | |
fb0e1ba7 MK |
163 | static char * |
164 | thread_db_err_str (td_err_e err) | |
165 | { | |
166 | static char buf[64]; | |
167 | ||
168 | switch (err) | |
169 | { | |
170 | case TD_OK: | |
171 | return "generic 'call succeeded'"; | |
172 | case TD_ERR: | |
173 | return "generic error"; | |
174 | case TD_NOTHR: | |
175 | return "no thread to satisfy query"; | |
176 | case TD_NOSV: | |
177 | return "no sync handle to satisfy query"; | |
178 | case TD_NOLWP: | |
179 | return "no LWP to satisfy query"; | |
180 | case TD_BADPH: | |
181 | return "invalid process handle"; | |
182 | case TD_BADTH: | |
183 | return "invalid thread handle"; | |
184 | case TD_BADSH: | |
185 | return "invalid synchronization handle"; | |
186 | case TD_BADTA: | |
187 | return "invalid thread agent"; | |
188 | case TD_BADKEY: | |
189 | return "invalid key"; | |
190 | case TD_NOMSG: | |
191 | return "no event message for getmsg"; | |
192 | case TD_NOFPREGS: | |
193 | return "FPU register set not available"; | |
194 | case TD_NOLIBTHREAD: | |
195 | return "application not linked with libthread"; | |
196 | case TD_NOEVENT: | |
197 | return "requested event is not supported"; | |
198 | case TD_NOCAPAB: | |
199 | return "capability not available"; | |
200 | case TD_DBERR: | |
201 | return "debugger service failed"; | |
202 | case TD_NOAPLIC: | |
203 | return "operation not applicable to"; | |
204 | case TD_NOTSD: | |
205 | return "no thread-specific data for this thread"; | |
206 | case TD_MALLOC: | |
207 | return "malloc failed"; | |
208 | case TD_PARTIALREG: | |
209 | return "only part of register set was written/read"; | |
210 | case TD_NOXREGS: | |
211 | return "X register set not available for this thread"; | |
59f80f10 DJ |
212 | #ifdef THREAD_DB_HAS_TD_NOTALLOC |
213 | case TD_NOTALLOC: | |
214 | return "thread has not yet allocated TLS for given module"; | |
215 | #endif | |
216 | #ifdef THREAD_DB_HAS_TD_VERSION | |
217 | case TD_VERSION: | |
218 | return "versions of libpthread and libthread_db do not match"; | |
219 | #endif | |
220 | #ifdef THREAD_DB_HAS_TD_NOTLS | |
221 | case TD_NOTLS: | |
222 | return "there is no TLS segment in the given module"; | |
223 | #endif | |
fb0e1ba7 MK |
224 | default: |
225 | snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); | |
226 | return buf; | |
227 | } | |
228 | } | |
fb0e1ba7 | 229 | \f |
5365276c | 230 | /* A callback function for td_ta_thr_iter, which we use to map all |
cdbc0b18 | 231 | threads to LWPs. |
5365276c DJ |
232 | |
233 | THP is a handle to the current thread; if INFOP is not NULL, the | |
234 | struct thread_info associated with this thread is returned in | |
b9b5d7ea JJ |
235 | *INFOP. |
236 | ||
237 | If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise, | |
238 | zero is returned to indicate success. */ | |
5365276c DJ |
239 | |
240 | static int | |
241 | thread_get_info_callback (const td_thrhandle_t *thp, void *infop) | |
242 | { | |
243 | td_thrinfo_t ti; | |
244 | td_err_e err; | |
245 | struct thread_info *thread_info; | |
246 | ptid_t thread_ptid; | |
247 | ||
248 | err = td_thr_get_info_p (thp, &ti); | |
249 | if (err != TD_OK) | |
8a3fe4f8 | 250 | error (_("thread_get_info_callback: cannot get thread info: %s"), |
5365276c DJ |
251 | thread_db_err_str (err)); |
252 | ||
253 | /* Fill the cache. */ | |
1bac0d4d | 254 | thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid); |
5365276c DJ |
255 | thread_info = find_thread_pid (thread_ptid); |
256 | ||
b9b5d7ea | 257 | /* In the case of a zombie thread, don't continue. We don't want to |
f90ef764 | 258 | attach to it thinking it is a new thread. */ |
b9b5d7ea JJ |
259 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
260 | { | |
261 | if (infop != NULL) | |
262 | *(struct thread_info **) infop = thread_info; | |
f90ef764 JJ |
263 | if (thread_info != NULL) |
264 | { | |
265 | memcpy (&thread_info->private->th, thp, sizeof (*thp)); | |
266 | thread_info->private->th_valid = 1; | |
267 | memcpy (&thread_info->private->ti, &ti, sizeof (ti)); | |
268 | thread_info->private->ti_valid = 1; | |
269 | } | |
b9b5d7ea JJ |
270 | return TD_THR_ZOMBIE; |
271 | } | |
272 | ||
5365276c DJ |
273 | if (thread_info == NULL) |
274 | { | |
275 | /* New thread. Attach to it now (why wait?). */ | |
276 | attach_thread (thread_ptid, thp, &ti, 1); | |
277 | thread_info = find_thread_pid (thread_ptid); | |
278 | gdb_assert (thread_info != NULL); | |
279 | } | |
280 | ||
281 | memcpy (&thread_info->private->th, thp, sizeof (*thp)); | |
282 | thread_info->private->th_valid = 1; | |
283 | memcpy (&thread_info->private->ti, &ti, sizeof (ti)); | |
284 | thread_info->private->ti_valid = 1; | |
285 | ||
286 | if (infop != NULL) | |
287 | *(struct thread_info **) infop = thread_info; | |
288 | ||
289 | return 0; | |
290 | } | |
291 | ||
292 | /* Accessor functions for the thread_db information, with caching. */ | |
fb0e1ba7 | 293 | |
5365276c DJ |
294 | static void |
295 | thread_db_map_id2thr (struct thread_info *thread_info, int fatal) | |
296 | { | |
297 | td_err_e err; | |
298 | ||
299 | if (thread_info->private->th_valid) | |
300 | return; | |
301 | ||
302 | err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid), | |
303 | &thread_info->private->th); | |
304 | if (err != TD_OK) | |
305 | { | |
306 | if (fatal) | |
8a3fe4f8 | 307 | error (_("Cannot find thread %ld: %s"), |
b4acd559 JJ |
308 | (long) GET_THREAD (thread_info->ptid), |
309 | thread_db_err_str (err)); | |
5365276c DJ |
310 | } |
311 | else | |
312 | thread_info->private->th_valid = 1; | |
313 | } | |
5365276c | 314 | \f |
fb0e1ba7 MK |
315 | /* Convert between user-level thread ids and LWP ids. */ |
316 | ||
39f77062 KB |
317 | static ptid_t |
318 | thread_from_lwp (ptid_t ptid) | |
fb0e1ba7 | 319 | { |
fb0e1ba7 MK |
320 | td_thrhandle_t th; |
321 | td_err_e err; | |
5365276c DJ |
322 | struct thread_info *thread_info; |
323 | ptid_t thread_ptid; | |
fb0e1ba7 | 324 | |
39f77062 KB |
325 | if (GET_LWP (ptid) == 0) |
326 | ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid)); | |
fb0e1ba7 | 327 | |
39f77062 | 328 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 | 329 | |
39f77062 | 330 | err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); |
fb0e1ba7 | 331 | if (err != TD_OK) |
8a3fe4f8 | 332 | error (_("Cannot find user-level thread for LWP %ld: %s"), |
39f77062 | 333 | GET_LWP (ptid), thread_db_err_str (err)); |
fb0e1ba7 | 334 | |
5365276c | 335 | thread_info = NULL; |
b9b5d7ea JJ |
336 | |
337 | /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the | |
338 | event thread has already died. If another gdb interface has called | |
339 | thread_alive() previously, the thread won't be found on the thread list | |
340 | anymore. In that case, we don't want to process this ptid anymore | |
341 | to avoid the possibility of later treating it as a newly | |
342 | discovered thread id that we should add to the list. Thus, | |
343 | we return a -1 ptid which is also how the thread list marks a | |
344 | dead thread. */ | |
345 | if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE | |
346 | && thread_info == NULL) | |
347 | return pid_to_ptid (-1); | |
348 | ||
5365276c | 349 | gdb_assert (thread_info && thread_info->private->ti_valid); |
fb0e1ba7 | 350 | |
1bac0d4d DJ |
351 | return ptid_build (GET_PID (ptid), GET_LWP (ptid), |
352 | thread_info->private->ti.ti_tid); | |
fb0e1ba7 MK |
353 | } |
354 | ||
39f77062 KB |
355 | static ptid_t |
356 | lwp_from_thread (ptid_t ptid) | |
fb0e1ba7 | 357 | { |
1bac0d4d | 358 | return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid)); |
fb0e1ba7 MK |
359 | } |
360 | \f | |
361 | ||
362 | void | |
363 | thread_db_init (struct target_ops *target) | |
364 | { | |
365 | target_beneath = target; | |
366 | } | |
367 | ||
5220ea4c AC |
368 | static void * |
369 | verbose_dlsym (void *handle, const char *name) | |
370 | { | |
371 | void *sym = dlsym (handle, name); | |
372 | if (sym == NULL) | |
8a3fe4f8 | 373 | warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ()); |
5220ea4c AC |
374 | return sym; |
375 | } | |
376 | ||
fb0e1ba7 MK |
377 | static int |
378 | thread_db_load (void) | |
379 | { | |
380 | void *handle; | |
381 | td_err_e err; | |
382 | ||
383 | handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW); | |
384 | if (handle == NULL) | |
f7c1e0f3 | 385 | { |
b4acd559 | 386 | fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n", |
f7c1e0f3 | 387 | LIBTHREAD_DB_SO, dlerror ()); |
b4acd559 | 388 | fprintf_filtered (gdb_stderr, |
f7c1e0f3 MS |
389 | "GDB will not be able to debug pthreads.\n\n"); |
390 | return 0; | |
391 | } | |
fb0e1ba7 MK |
392 | |
393 | /* Initialize pointers to the dynamic library functions we will use. | |
394 | Essential functions first. */ | |
395 | ||
5220ea4c | 396 | td_init_p = verbose_dlsym (handle, "td_init"); |
fb0e1ba7 MK |
397 | if (td_init_p == NULL) |
398 | return 0; | |
399 | ||
5220ea4c | 400 | td_ta_new_p = verbose_dlsym (handle, "td_ta_new"); |
fb0e1ba7 MK |
401 | if (td_ta_new_p == NULL) |
402 | return 0; | |
403 | ||
5220ea4c | 404 | td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr"); |
fb0e1ba7 MK |
405 | if (td_ta_map_id2thr_p == NULL) |
406 | return 0; | |
407 | ||
5220ea4c | 408 | td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr"); |
fb0e1ba7 MK |
409 | if (td_ta_map_lwp2thr_p == NULL) |
410 | return 0; | |
411 | ||
5220ea4c | 412 | td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter"); |
fb0e1ba7 MK |
413 | if (td_ta_thr_iter_p == NULL) |
414 | return 0; | |
415 | ||
5220ea4c | 416 | td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate"); |
fb0e1ba7 MK |
417 | if (td_thr_validate_p == NULL) |
418 | return 0; | |
419 | ||
5220ea4c | 420 | td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info"); |
fb0e1ba7 MK |
421 | if (td_thr_get_info_p == NULL) |
422 | return 0; | |
423 | ||
fb0e1ba7 MK |
424 | /* Initialize the library. */ |
425 | err = td_init_p (); | |
426 | if (err != TD_OK) | |
427 | { | |
8a3fe4f8 | 428 | warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err)); |
fb0e1ba7 MK |
429 | return 0; |
430 | } | |
431 | ||
432 | /* These are not essential. */ | |
433 | td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"); | |
434 | td_ta_set_event_p = dlsym (handle, "td_ta_set_event"); | |
435 | td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"); | |
436 | td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"); | |
3f47be5c | 437 | td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"); |
fb0e1ba7 MK |
438 | |
439 | return 1; | |
440 | } | |
441 | ||
cdbc0b18 | 442 | static td_err_e |
24557e30 AC |
443 | enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp) |
444 | { | |
445 | td_notify_t notify; | |
cdbc0b18 | 446 | td_err_e err; |
24557e30 AC |
447 | |
448 | /* Get the breakpoint address for thread EVENT. */ | |
449 | err = td_ta_event_addr_p (thread_agent, event, ¬ify); | |
450 | if (err != TD_OK) | |
cdbc0b18 | 451 | return err; |
24557e30 AC |
452 | |
453 | /* Set up the breakpoint. */ | |
16451949 AS |
454 | gdb_assert (exec_bfd); |
455 | (*bp) = (gdbarch_convert_from_func_ptr_addr | |
456 | (current_gdbarch, | |
457 | /* Do proper sign extension for the target. */ | |
458 | (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
459 | ? (CORE_ADDR) (intptr_t) notify.u.bptaddr | |
460 | : (CORE_ADDR) (uintptr_t) notify.u.bptaddr), | |
461 | ¤t_target)); | |
24557e30 AC |
462 | create_thread_event_breakpoint ((*bp)); |
463 | ||
cdbc0b18 | 464 | return TD_OK; |
24557e30 AC |
465 | } |
466 | ||
fb0e1ba7 MK |
467 | static void |
468 | enable_thread_event_reporting (void) | |
469 | { | |
470 | td_thr_events_t events; | |
471 | td_notify_t notify; | |
472 | td_err_e err; | |
a2f23071 DJ |
473 | #ifdef HAVE_GNU_LIBC_VERSION_H |
474 | const char *libc_version; | |
475 | int libc_major, libc_minor; | |
476 | #endif | |
fb0e1ba7 MK |
477 | |
478 | /* We cannot use the thread event reporting facility if these | |
479 | functions aren't available. */ | |
480 | if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL | |
481 | || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL) | |
482 | return; | |
483 | ||
484 | /* Set the process wide mask saying which events we're interested in. */ | |
485 | td_event_emptyset (&events); | |
486 | td_event_addset (&events, TD_CREATE); | |
a2f23071 DJ |
487 | |
488 | #ifdef HAVE_GNU_LIBC_VERSION_H | |
fb0e1ba7 MK |
489 | /* FIXME: kettenis/2000-04-23: The event reporting facility is |
490 | broken for TD_DEATH events in glibc 2.1.3, so don't enable it for | |
491 | now. */ | |
a2f23071 DJ |
492 | libc_version = gnu_get_libc_version (); |
493 | if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2 | |
494 | && (libc_major > 2 || (libc_major == 2 && libc_minor > 1))) | |
fb0e1ba7 | 495 | #endif |
a2f23071 | 496 | td_event_addset (&events, TD_DEATH); |
fb0e1ba7 MK |
497 | |
498 | err = td_ta_set_event_p (thread_agent, &events); | |
499 | if (err != TD_OK) | |
500 | { | |
8a3fe4f8 | 501 | warning (_("Unable to set global thread event mask: %s"), |
fb0e1ba7 MK |
502 | thread_db_err_str (err)); |
503 | return; | |
504 | } | |
505 | ||
506 | /* Delete previous thread event breakpoints, if any. */ | |
507 | remove_thread_event_breakpoints (); | |
24557e30 AC |
508 | td_create_bp_addr = 0; |
509 | td_death_bp_addr = 0; | |
fb0e1ba7 | 510 | |
24557e30 | 511 | /* Set up the thread creation event. */ |
cdbc0b18 RM |
512 | err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr); |
513 | if (err != TD_OK) | |
fb0e1ba7 | 514 | { |
8a3fe4f8 | 515 | warning (_("Unable to get location for thread creation breakpoint: %s"), |
fb0e1ba7 MK |
516 | thread_db_err_str (err)); |
517 | return; | |
518 | } | |
519 | ||
24557e30 | 520 | /* Set up the thread death event. */ |
cdbc0b18 RM |
521 | err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr); |
522 | if (err != TD_OK) | |
fb0e1ba7 | 523 | { |
8a3fe4f8 | 524 | warning (_("Unable to get location for thread death breakpoint: %s"), |
fb0e1ba7 MK |
525 | thread_db_err_str (err)); |
526 | return; | |
527 | } | |
fb0e1ba7 MK |
528 | } |
529 | ||
530 | static void | |
531 | disable_thread_event_reporting (void) | |
532 | { | |
533 | td_thr_events_t events; | |
534 | ||
535 | /* Set the process wide mask saying we aren't interested in any | |
536 | events anymore. */ | |
537 | td_event_emptyset (&events); | |
538 | td_ta_set_event_p (thread_agent, &events); | |
539 | ||
540 | /* Delete thread event breakpoints, if any. */ | |
541 | remove_thread_event_breakpoints (); | |
542 | td_create_bp_addr = 0; | |
543 | td_death_bp_addr = 0; | |
544 | } | |
545 | ||
546 | static void | |
547 | check_thread_signals (void) | |
548 | { | |
549 | #ifdef GET_THREAD_SIGNALS | |
21bf60fe | 550 | if (!thread_signals) |
fb0e1ba7 MK |
551 | { |
552 | sigset_t mask; | |
553 | int i; | |
554 | ||
555 | GET_THREAD_SIGNALS (&mask); | |
556 | sigemptyset (&thread_stop_set); | |
557 | sigemptyset (&thread_print_set); | |
558 | ||
b9569773 | 559 | for (i = 1; i < NSIG; i++) |
fb0e1ba7 MK |
560 | { |
561 | if (sigismember (&mask, i)) | |
562 | { | |
563 | if (signal_stop_update (target_signal_from_host (i), 0)) | |
564 | sigaddset (&thread_stop_set, i); | |
565 | if (signal_print_update (target_signal_from_host (i), 0)) | |
566 | sigaddset (&thread_print_set, i); | |
567 | thread_signals = 1; | |
568 | } | |
569 | } | |
570 | } | |
571 | #endif | |
572 | } | |
573 | ||
0ec9a092 DJ |
574 | /* Check whether thread_db is usable. This function is called when |
575 | an inferior is created (or otherwise acquired, e.g. attached to) | |
576 | and when new shared libraries are loaded into a running process. */ | |
577 | ||
578 | void | |
579 | check_for_thread_db (void) | |
fb0e1ba7 MK |
580 | { |
581 | td_err_e err; | |
0ec9a092 | 582 | static int already_loaded; |
fb0e1ba7 | 583 | |
5220ea4c AC |
584 | /* First time through, report that libthread_db was successfuly |
585 | loaded. Can't print this in in thread_db_load as, at that stage, | |
0ec9a092 | 586 | the interpreter and it's console haven't started. */ |
5220ea4c | 587 | |
0ec9a092 | 588 | if (!already_loaded) |
bda9cb72 | 589 | { |
0ec9a092 DJ |
590 | Dl_info info; |
591 | const char *library = NULL; | |
592 | if (dladdr ((*td_ta_new_p), &info) != 0) | |
593 | library = info.dli_fname; | |
594 | ||
595 | /* Try dlinfo? */ | |
596 | ||
597 | if (library == NULL) | |
598 | /* Paranoid - don't let a NULL path slip through. */ | |
599 | library = LIBTHREAD_DB_SO; | |
c194fbe1 | 600 | |
0ec9a092 DJ |
601 | printf_unfiltered (_("Using host libthread_db library \"%s\".\n"), |
602 | library); | |
603 | already_loaded = 1; | |
bda9cb72 MK |
604 | } |
605 | ||
fb0e1ba7 MK |
606 | if (using_thread_db) |
607 | /* Nothing to do. The thread library was already detected and the | |
608 | target vector was already activated. */ | |
0ec9a092 | 609 | return; |
fb0e1ba7 | 610 | |
0ec9a092 DJ |
611 | /* Don't attempt to use thread_db on targets which can not run |
612 | (executables not running yet, core files) for now. */ | |
613 | if (!target_has_execution) | |
614 | return; | |
615 | ||
f86172a5 UW |
616 | /* Don't attempt to use thread_db for remote targets. */ |
617 | if (!target_can_run (¤t_target)) | |
618 | return; | |
619 | ||
0ec9a092 | 620 | /* Initialize the structure that identifies the child process. */ |
39f77062 | 621 | proc_handle.pid = GET_PID (inferior_ptid); |
fb0e1ba7 | 622 | |
bda9cb72 | 623 | /* Now attempt to open a connection to the thread library. */ |
fb0e1ba7 MK |
624 | err = td_ta_new_p (&proc_handle, &thread_agent); |
625 | switch (err) | |
626 | { | |
627 | case TD_NOLIBTHREAD: | |
bda9cb72 | 628 | /* No thread library was detected. */ |
fb0e1ba7 MK |
629 | break; |
630 | ||
631 | case TD_OK: | |
a3f17187 | 632 | printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n")); |
5220ea4c | 633 | |
bda9cb72 MK |
634 | /* The thread library was detected. Activate the thread_db target. */ |
635 | push_target (&thread_db_ops); | |
636 | using_thread_db = 1; | |
637 | ||
95575b2e AC |
638 | enable_thread_event_reporting (); |
639 | thread_db_find_new_threads (); | |
fb0e1ba7 MK |
640 | break; |
641 | ||
642 | default: | |
8a3fe4f8 | 643 | warning (_("Cannot initialize thread debugging library: %s"), |
fb0e1ba7 MK |
644 | thread_db_err_str (err)); |
645 | break; | |
646 | } | |
0ec9a092 DJ |
647 | } |
648 | ||
649 | static void | |
650 | thread_db_new_objfile (struct objfile *objfile) | |
651 | { | |
652 | if (objfile != NULL) | |
653 | check_for_thread_db (); | |
fb0e1ba7 | 654 | |
540af400 MS |
655 | if (target_new_objfile_chain) |
656 | target_new_objfile_chain (objfile); | |
fb0e1ba7 MK |
657 | } |
658 | ||
a2f23071 DJ |
659 | /* Attach to a new thread. This function is called when we receive a |
660 | TD_CREATE event or when we iterate over all threads and find one | |
661 | that wasn't already in our list. */ | |
662 | ||
fb0e1ba7 | 663 | static void |
39f77062 | 664 | attach_thread (ptid_t ptid, const td_thrhandle_t *th_p, |
fb0e1ba7 MK |
665 | const td_thrinfo_t *ti_p, int verbose) |
666 | { | |
667 | struct thread_info *tp; | |
668 | td_err_e err; | |
669 | ||
a2f23071 DJ |
670 | /* If we're being called after a TD_CREATE event, we may already |
671 | know about this thread. There are two ways this can happen. We | |
672 | may have iterated over all threads between the thread creation | |
673 | and the TD_CREATE event, for instance when the user has issued | |
674 | the `info threads' command before the SIGTRAP for hitting the | |
675 | thread creation breakpoint was reported. Alternatively, the | |
676 | thread may have exited and a new one been created with the same | |
677 | thread ID. In the first case we don't need to do anything; in | |
678 | the second case we should discard information about the dead | |
679 | thread and attach to the new one. */ | |
680 | if (in_thread_list (ptid)) | |
681 | { | |
682 | tp = find_thread_pid (ptid); | |
683 | gdb_assert (tp != NULL); | |
684 | ||
685 | if (!tp->private->dying) | |
686 | return; | |
687 | ||
688 | delete_thread (ptid); | |
689 | } | |
690 | ||
fb0e1ba7 MK |
691 | check_thread_signals (); |
692 | ||
9ee57c33 DJ |
693 | if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE) |
694 | return; /* A zombie thread -- do not attach. */ | |
695 | ||
696 | /* Under GNU/Linux, we have to attach to each and every thread. */ | |
697 | if (lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0) < 0) | |
698 | return; | |
699 | ||
fb0e1ba7 | 700 | /* Add the thread to GDB's thread list. */ |
39f77062 | 701 | tp = add_thread (ptid); |
fb0e1ba7 | 702 | tp->private = xmalloc (sizeof (struct private_thread_info)); |
5365276c DJ |
703 | memset (tp->private, 0, sizeof (struct private_thread_info)); |
704 | ||
705 | if (verbose) | |
a3f17187 | 706 | printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid)); |
fb0e1ba7 | 707 | |
fb0e1ba7 MK |
708 | /* Enable thread event reporting for this thread. */ |
709 | err = td_thr_event_enable_p (th_p, 1); | |
710 | if (err != TD_OK) | |
8a3fe4f8 | 711 | error (_("Cannot enable thread event reporting for %s: %s"), |
39f77062 | 712 | target_pid_to_str (ptid), thread_db_err_str (err)); |
fb0e1ba7 MK |
713 | } |
714 | ||
c194fbe1 MK |
715 | static void |
716 | thread_db_attach (char *args, int from_tty) | |
717 | { | |
718 | target_beneath->to_attach (args, from_tty); | |
719 | ||
720 | /* Destroy thread info; it's no longer valid. */ | |
721 | init_thread_list (); | |
722 | ||
723 | /* The child process is now the actual multi-threaded | |
724 | program. Snatch its process ID... */ | |
725 | proc_handle.pid = GET_PID (inferior_ptid); | |
726 | ||
727 | /* ...and perform the remaining initialization steps. */ | |
728 | enable_thread_event_reporting (); | |
b4acd559 | 729 | thread_db_find_new_threads (); |
c194fbe1 MK |
730 | } |
731 | ||
fb0e1ba7 | 732 | static void |
39f77062 | 733 | detach_thread (ptid_t ptid, int verbose) |
fb0e1ba7 | 734 | { |
a2f23071 DJ |
735 | struct thread_info *thread_info; |
736 | ||
fb0e1ba7 | 737 | if (verbose) |
a3f17187 | 738 | printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid)); |
a2f23071 DJ |
739 | |
740 | /* Don't delete the thread now, because it still reports as active | |
741 | until it has executed a few instructions after the event | |
742 | breakpoint - if we deleted it now, "info threads" would cause us | |
743 | to re-attach to it. Just mark it as having had a TD_DEATH | |
744 | event. This means that we won't delete it from our thread list | |
745 | until we notice that it's dead (via prune_threads), or until | |
746 | something re-uses its thread ID. */ | |
747 | thread_info = find_thread_pid (ptid); | |
748 | gdb_assert (thread_info != NULL); | |
749 | thread_info->private->dying = 1; | |
fb0e1ba7 MK |
750 | } |
751 | ||
752 | static void | |
753 | thread_db_detach (char *args, int from_tty) | |
754 | { | |
755 | disable_thread_event_reporting (); | |
c194fbe1 MK |
756 | |
757 | /* There's no need to save & restore inferior_ptid here, since the | |
758 | inferior is supposed to be survive this function call. */ | |
759 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
760 | ||
761 | /* Forget about the child's process ID. We shouldn't need it | |
762 | anymore. */ | |
763 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
764 | |
765 | target_beneath->to_detach (args, from_tty); | |
766 | } | |
767 | ||
5365276c DJ |
768 | static int |
769 | clear_lwpid_callback (struct thread_info *thread, void *dummy) | |
770 | { | |
771 | /* If we know that our thread implementation is 1-to-1, we could save | |
772 | a certain amount of information; it's not clear how much, so we | |
773 | are always conservative. */ | |
774 | ||
775 | thread->private->th_valid = 0; | |
776 | thread->private->ti_valid = 0; | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
fb0e1ba7 | 781 | static void |
39f77062 | 782 | thread_db_resume (ptid_t ptid, int step, enum target_signal signo) |
fb0e1ba7 | 783 | { |
39f77062 | 784 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 | 785 | |
39f77062 KB |
786 | if (GET_PID (ptid) == -1) |
787 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
788 | else if (is_thread (ptid)) | |
789 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 790 | |
5365276c DJ |
791 | /* Clear cached data which may not be valid after the resume. */ |
792 | iterate_over_threads (clear_lwpid_callback, NULL); | |
793 | ||
39f77062 | 794 | target_beneath->to_resume (ptid, step, signo); |
fb0e1ba7 MK |
795 | |
796 | do_cleanups (old_chain); | |
797 | } | |
798 | ||
799 | /* Check if PID is currently stopped at the location of a thread event | |
800 | breakpoint location. If it is, read the event message and act upon | |
801 | the event. */ | |
802 | ||
803 | static void | |
39f77062 | 804 | check_event (ptid_t ptid) |
fb0e1ba7 MK |
805 | { |
806 | td_event_msg_t msg; | |
807 | td_thrinfo_t ti; | |
808 | td_err_e err; | |
809 | CORE_ADDR stop_pc; | |
4d9850d3 | 810 | int loop = 0; |
fb0e1ba7 MK |
811 | |
812 | /* Bail out early if we're not at a thread event breakpoint. */ | |
39f77062 | 813 | stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK; |
fb0e1ba7 MK |
814 | if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr) |
815 | return; | |
816 | ||
4d9850d3 JJ |
817 | /* If we are at a create breakpoint, we do not know what new lwp |
818 | was created and cannot specifically locate the event message for it. | |
819 | We have to call td_ta_event_getmsg() to get | |
820 | the latest message. Since we have no way of correlating whether | |
cdbc0b18 | 821 | the event message we get back corresponds to our breakpoint, we must |
4d9850d3 | 822 | loop and read all event messages, processing them appropriately. |
cdbc0b18 RM |
823 | This guarantees we will process the correct message before continuing |
824 | from the breakpoint. | |
4d9850d3 JJ |
825 | |
826 | Currently, death events are not enabled. If they are enabled, | |
827 | the death event can use the td_thr_event_getmsg() interface to | |
828 | get the message specifically for that lwp and avoid looping | |
829 | below. */ | |
830 | ||
831 | loop = 1; | |
832 | ||
833 | do | |
fb0e1ba7 | 834 | { |
4d9850d3 JJ |
835 | err = td_ta_event_getmsg_p (thread_agent, &msg); |
836 | if (err != TD_OK) | |
837 | { | |
838 | if (err == TD_NOMSG) | |
839 | return; | |
fb0e1ba7 | 840 | |
8a3fe4f8 | 841 | error (_("Cannot get thread event message: %s"), |
4d9850d3 JJ |
842 | thread_db_err_str (err)); |
843 | } | |
fb0e1ba7 | 844 | |
4d9850d3 JJ |
845 | err = td_thr_get_info_p (msg.th_p, &ti); |
846 | if (err != TD_OK) | |
8a3fe4f8 | 847 | error (_("Cannot get thread info: %s"), thread_db_err_str (err)); |
fb0e1ba7 | 848 | |
1bac0d4d | 849 | ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid); |
fb0e1ba7 | 850 | |
4d9850d3 JJ |
851 | switch (msg.event) |
852 | { | |
853 | case TD_CREATE: | |
a2f23071 DJ |
854 | /* Call attach_thread whether or not we already know about a |
855 | thread with this thread ID. */ | |
856 | attach_thread (ptid, msg.th_p, &ti, 1); | |
fb0e1ba7 | 857 | |
4d9850d3 | 858 | break; |
fb0e1ba7 | 859 | |
4d9850d3 | 860 | case TD_DEATH: |
fb0e1ba7 | 861 | |
4d9850d3 | 862 | if (!in_thread_list (ptid)) |
8a3fe4f8 | 863 | error (_("Spurious thread death event.")); |
fb0e1ba7 | 864 | |
4d9850d3 | 865 | detach_thread (ptid, 1); |
fb0e1ba7 | 866 | |
4d9850d3 | 867 | break; |
fb0e1ba7 | 868 | |
4d9850d3 | 869 | default: |
8a3fe4f8 | 870 | error (_("Spurious thread event.")); |
4d9850d3 | 871 | } |
fb0e1ba7 | 872 | } |
4d9850d3 | 873 | while (loop); |
fb0e1ba7 MK |
874 | } |
875 | ||
39f77062 KB |
876 | static ptid_t |
877 | thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
fb0e1ba7 | 878 | { |
39f77062 | 879 | extern ptid_t trap_ptid; |
fb0e1ba7 | 880 | |
39f77062 KB |
881 | if (GET_PID (ptid) != -1 && is_thread (ptid)) |
882 | ptid = lwp_from_thread (ptid); | |
fb0e1ba7 | 883 | |
39f77062 | 884 | ptid = target_beneath->to_wait (ptid, ourstatus); |
fb0e1ba7 | 885 | |
bda9cb72 MK |
886 | if (proc_handle.pid == 0) |
887 | /* The current child process isn't the actual multi-threaded | |
888 | program yet, so don't try to do any special thread-specific | |
889 | post-processing and bail out early. */ | |
39f77062 | 890 | return ptid; |
bda9cb72 | 891 | |
1111f4aa NR |
892 | if (ourstatus->kind == TARGET_WAITKIND_EXITED |
893 | || ourstatus->kind == TARGET_WAITKIND_SIGNALLED) | |
39f77062 | 894 | return pid_to_ptid (-1); |
fb0e1ba7 | 895 | |
3f64f7b1 DJ |
896 | if (ourstatus->kind == TARGET_WAITKIND_EXECD) |
897 | { | |
898 | remove_thread_event_breakpoints (); | |
899 | unpush_target (&thread_db_ops); | |
900 | using_thread_db = 0; | |
901 | ||
902 | return pid_to_ptid (GET_PID (ptid)); | |
903 | } | |
904 | ||
fb0e1ba7 MK |
905 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED |
906 | && ourstatus->value.sig == TARGET_SIGNAL_TRAP) | |
907 | /* Check for a thread event. */ | |
39f77062 | 908 | check_event (ptid); |
fb0e1ba7 | 909 | |
39f77062 KB |
910 | if (!ptid_equal (trap_ptid, null_ptid)) |
911 | trap_ptid = thread_from_lwp (trap_ptid); | |
fb0e1ba7 | 912 | |
b9b5d7ea JJ |
913 | /* Change the ptid back into the higher level PID + TID format. |
914 | If the thread is dead and no longer on the thread list, we will | |
915 | get back a dead ptid. This can occur if the thread death event | |
916 | gets postponed by other simultaneous events. In such a case, | |
917 | we want to just ignore the event and continue on. */ | |
918 | ptid = thread_from_lwp (ptid); | |
919 | if (GET_PID (ptid) == -1) | |
920 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
921 | ||
922 | return ptid; | |
fb0e1ba7 MK |
923 | } |
924 | ||
10d6c8cd DJ |
925 | static LONGEST |
926 | thread_db_xfer_partial (struct target_ops *ops, enum target_object object, | |
927 | const char *annex, gdb_byte *readbuf, | |
928 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
fb0e1ba7 | 929 | { |
39f77062 | 930 | struct cleanup *old_chain = save_inferior_ptid (); |
10d6c8cd | 931 | LONGEST xfer; |
fb0e1ba7 | 932 | |
39f77062 | 933 | if (is_thread (inferior_ptid)) |
fb0e1ba7 MK |
934 | { |
935 | /* FIXME: This seems to be necessary to make sure breakpoints | |
936 | are removed. */ | |
21bf60fe | 937 | if (!target_thread_alive (inferior_ptid)) |
39f77062 | 938 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
fb0e1ba7 | 939 | else |
39f77062 | 940 | inferior_ptid = lwp_from_thread (inferior_ptid); |
fb0e1ba7 MK |
941 | } |
942 | ||
10d6c8cd DJ |
943 | xfer = target_beneath->to_xfer_partial (ops, object, annex, |
944 | readbuf, writebuf, offset, len); | |
fb0e1ba7 MK |
945 | |
946 | do_cleanups (old_chain); | |
947 | return xfer; | |
948 | } | |
949 | ||
fb0e1ba7 MK |
950 | static void |
951 | thread_db_kill (void) | |
952 | { | |
c194fbe1 MK |
953 | /* There's no need to save & restore inferior_ptid here, since the |
954 | inferior isn't supposed to survive this function call. */ | |
955 | inferior_ptid = lwp_from_thread (inferior_ptid); | |
fb0e1ba7 MK |
956 | target_beneath->to_kill (); |
957 | } | |
958 | ||
959 | static void | |
c27cda74 AC |
960 | thread_db_create_inferior (char *exec_file, char *allargs, char **env, |
961 | int from_tty) | |
fb0e1ba7 | 962 | { |
b26a6851 AC |
963 | unpush_target (&thread_db_ops); |
964 | using_thread_db = 0; | |
c27cda74 | 965 | target_beneath->to_create_inferior (exec_file, allargs, env, from_tty); |
bda9cb72 | 966 | } |
fb0e1ba7 | 967 | |
bda9cb72 | 968 | static void |
39f77062 | 969 | thread_db_post_startup_inferior (ptid_t ptid) |
bda9cb72 MK |
970 | { |
971 | if (proc_handle.pid == 0) | |
972 | { | |
973 | /* The child process is now the actual multi-threaded | |
974 | program. Snatch its process ID... */ | |
39f77062 | 975 | proc_handle.pid = GET_PID (ptid); |
fb0e1ba7 | 976 | |
bda9cb72 MK |
977 | /* ...and perform the remaining initialization steps. */ |
978 | enable_thread_event_reporting (); | |
21bf60fe | 979 | thread_db_find_new_threads (); |
bda9cb72 | 980 | } |
fb0e1ba7 MK |
981 | } |
982 | ||
983 | static void | |
984 | thread_db_mourn_inferior (void) | |
985 | { | |
c194fbe1 MK |
986 | /* Forget about the child's process ID. We shouldn't need it |
987 | anymore. */ | |
988 | proc_handle.pid = 0; | |
fb0e1ba7 MK |
989 | |
990 | target_beneath->to_mourn_inferior (); | |
043b2f77 | 991 | |
e23fc6de DJ |
992 | /* Delete the old thread event breakpoints. Do this after mourning |
993 | the inferior, so that we don't try to uninsert them. */ | |
994 | remove_thread_event_breakpoints (); | |
995 | ||
b26a6851 AC |
996 | /* Detach thread_db target ops. */ |
997 | unpush_target (&thread_db_ops); | |
998 | using_thread_db = 0; | |
fb0e1ba7 MK |
999 | } |
1000 | ||
fb0e1ba7 MK |
1001 | static int |
1002 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
1003 | { | |
1004 | td_thrinfo_t ti; | |
1005 | td_err_e err; | |
39f77062 | 1006 | ptid_t ptid; |
fb0e1ba7 MK |
1007 | |
1008 | err = td_thr_get_info_p (th_p, &ti); | |
1009 | if (err != TD_OK) | |
8a3fe4f8 | 1010 | error (_("find_new_threads_callback: cannot get thread info: %s"), |
3197744f | 1011 | thread_db_err_str (err)); |
fb0e1ba7 | 1012 | |
21bf60fe MK |
1013 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
1014 | return 0; /* A zombie -- ignore. */ | |
5fd913cc | 1015 | |
1bac0d4d | 1016 | ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid); |
fb0e1ba7 | 1017 | |
21bf60fe | 1018 | if (!in_thread_list (ptid)) |
39f77062 | 1019 | attach_thread (ptid, th_p, &ti, 1); |
fb0e1ba7 MK |
1020 | |
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static void | |
1025 | thread_db_find_new_threads (void) | |
1026 | { | |
1027 | td_err_e err; | |
1028 | ||
1029 | /* Iterate over all user-space threads to discover new threads. */ | |
1030 | err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, | |
1031 | TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, | |
1032 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); | |
1033 | if (err != TD_OK) | |
8a3fe4f8 | 1034 | error (_("Cannot find new threads: %s"), thread_db_err_str (err)); |
fb0e1ba7 MK |
1035 | } |
1036 | ||
1037 | static char * | |
39f77062 | 1038 | thread_db_pid_to_str (ptid_t ptid) |
fb0e1ba7 | 1039 | { |
39f77062 | 1040 | if (is_thread (ptid)) |
fb0e1ba7 MK |
1041 | { |
1042 | static char buf[64]; | |
5365276c | 1043 | struct thread_info *thread_info; |
fb0e1ba7 | 1044 | |
5365276c | 1045 | thread_info = find_thread_pid (ptid); |
28b17333 DJ |
1046 | if (thread_info == NULL) |
1047 | snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld) (Missing)", | |
1048 | GET_THREAD (ptid), GET_LWP (ptid)); | |
fb0e1ba7 | 1049 | else |
28b17333 DJ |
1050 | snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld)", |
1051 | GET_THREAD (ptid), GET_LWP (ptid)); | |
fb0e1ba7 MK |
1052 | |
1053 | return buf; | |
1054 | } | |
1055 | ||
39f77062 KB |
1056 | if (target_beneath->to_pid_to_str (ptid)) |
1057 | return target_beneath->to_pid_to_str (ptid); | |
fb0e1ba7 | 1058 | |
39f77062 | 1059 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1060 | } |
1061 | ||
28b17333 DJ |
1062 | /* Return a string describing the state of the thread specified by |
1063 | INFO. */ | |
1064 | ||
1065 | static char * | |
1066 | thread_db_extra_thread_info (struct thread_info *info) | |
1067 | { | |
1068 | if (info->private->dying) | |
1069 | return "Exiting"; | |
1070 | ||
1071 | return NULL; | |
1072 | } | |
1073 | ||
b2756930 KB |
1074 | /* Get the address of the thread local variable in load module LM which |
1075 | is stored at OFFSET within the thread local storage for thread PTID. */ | |
3f47be5c EZ |
1076 | |
1077 | static CORE_ADDR | |
b2756930 KB |
1078 | thread_db_get_thread_local_address (ptid_t ptid, |
1079 | CORE_ADDR lm, | |
b4acd559 | 1080 | CORE_ADDR offset) |
3f47be5c EZ |
1081 | { |
1082 | if (is_thread (ptid)) | |
1083 | { | |
3f47be5c | 1084 | td_err_e err; |
3f47be5c | 1085 | void *address; |
5365276c | 1086 | struct thread_info *thread_info; |
3f47be5c EZ |
1087 | |
1088 | /* glibc doesn't provide the needed interface. */ | |
b4acd559 | 1089 | if (!td_thr_tls_get_addr_p) |
109c3e39 AC |
1090 | throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR, |
1091 | _("No TLS library support")); | |
3f47be5c | 1092 | |
b2756930 KB |
1093 | /* Caller should have verified that lm != 0. */ |
1094 | gdb_assert (lm != 0); | |
3f47be5c EZ |
1095 | |
1096 | /* Get info about the thread. */ | |
5365276c DJ |
1097 | thread_info = find_thread_pid (ptid); |
1098 | thread_db_map_id2thr (thread_info, 1); | |
1099 | ||
3f47be5c | 1100 | /* Finally, get the address of the variable. */ |
87177905 TS |
1101 | err = td_thr_tls_get_addr_p (&thread_info->private->th, |
1102 | (void *)(size_t) lm, | |
5365276c | 1103 | offset, &address); |
3f47be5c EZ |
1104 | |
1105 | #ifdef THREAD_DB_HAS_TD_NOTALLOC | |
1106 | /* The memory hasn't been allocated, yet. */ | |
1107 | if (err == TD_NOTALLOC) | |
b4acd559 JJ |
1108 | /* Now, if libthread_db provided the initialization image's |
1109 | address, we *could* try to build a non-lvalue value from | |
1110 | the initialization image. */ | |
109c3e39 AC |
1111 | throw_error (TLS_NOT_ALLOCATED_YET_ERROR, |
1112 | _("TLS not allocated yet")); | |
3f47be5c EZ |
1113 | #endif |
1114 | ||
1115 | /* Something else went wrong. */ | |
1116 | if (err != TD_OK) | |
109c3e39 AC |
1117 | throw_error (TLS_GENERIC_ERROR, |
1118 | (("%s")), thread_db_err_str (err)); | |
3f47be5c EZ |
1119 | |
1120 | /* Cast assuming host == target. Joy. */ | |
16451949 AS |
1121 | /* Do proper sign extension for the target. */ |
1122 | gdb_assert (exec_bfd); | |
1123 | return (bfd_get_sign_extend_vma (exec_bfd) > 0 | |
1124 | ? (CORE_ADDR) (intptr_t) address | |
1125 | : (CORE_ADDR) (uintptr_t) address); | |
3f47be5c EZ |
1126 | } |
1127 | ||
1128 | if (target_beneath->to_get_thread_local_address) | |
b2756930 | 1129 | return target_beneath->to_get_thread_local_address (ptid, lm, offset); |
93ad78a7 | 1130 | else |
109c3e39 AC |
1131 | throw_error (TLS_GENERIC_ERROR, |
1132 | _("TLS not supported on this target")); | |
3f47be5c EZ |
1133 | } |
1134 | ||
fb0e1ba7 MK |
1135 | static void |
1136 | init_thread_db_ops (void) | |
1137 | { | |
1138 | thread_db_ops.to_shortname = "multi-thread"; | |
1139 | thread_db_ops.to_longname = "multi-threaded child process."; | |
1140 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
c194fbe1 | 1141 | thread_db_ops.to_attach = thread_db_attach; |
fb0e1ba7 MK |
1142 | thread_db_ops.to_detach = thread_db_detach; |
1143 | thread_db_ops.to_resume = thread_db_resume; | |
1144 | thread_db_ops.to_wait = thread_db_wait; | |
10d6c8cd | 1145 | thread_db_ops.to_xfer_partial = thread_db_xfer_partial; |
fb0e1ba7 MK |
1146 | thread_db_ops.to_kill = thread_db_kill; |
1147 | thread_db_ops.to_create_inferior = thread_db_create_inferior; | |
bda9cb72 | 1148 | thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior; |
fb0e1ba7 | 1149 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; |
fb0e1ba7 MK |
1150 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; |
1151 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
1152 | thread_db_ops.to_stratum = thread_stratum; | |
1153 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
3f47be5c EZ |
1154 | thread_db_ops.to_get_thread_local_address |
1155 | = thread_db_get_thread_local_address; | |
28b17333 | 1156 | thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info; |
fb0e1ba7 MK |
1157 | thread_db_ops.to_magic = OPS_MAGIC; |
1158 | } | |
1159 | ||
1160 | void | |
1161 | _initialize_thread_db (void) | |
1162 | { | |
1163 | /* Only initialize the module if we can load libthread_db. */ | |
1164 | if (thread_db_load ()) | |
1165 | { | |
1166 | init_thread_db_ops (); | |
1167 | add_target (&thread_db_ops); | |
1168 | ||
1169 | /* Add ourselves to objfile event chain. */ | |
9a4105ab AC |
1170 | target_new_objfile_chain = deprecated_target_new_objfile_hook; |
1171 | deprecated_target_new_objfile_hook = thread_db_new_objfile; | |
fb0e1ba7 MK |
1172 | } |
1173 | } |