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