Commit | Line | Data |
---|---|---|
ed9a39eb JM |
1 | /* Multi-threaded debugging support for the thread_db interface, |
2 | used on operating systems such as Solaris and Linux. | |
b6ba6518 | 3 | Copyright 1999, 2000, 2001 Free Software Foundation, Inc. |
ed9a39eb JM |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | /* This module implements a thread_stratum target that sits on top of | |
23 | a normal process_stratum target (such as procfs or ptrace). The | |
24 | process_stratum target must install this thread_stratum target when | |
25 | it detects the presence of the thread_db shared library. | |
26 | ||
27 | This module will then use the thread_db API to add thread-awareness | |
28 | to the functionality provided by the process_stratum target (or in | |
29 | some cases, to add user-level thread awareness on top of the | |
30 | kernel-level thread awareness that is already provided by the | |
31 | process_stratum target). | |
32 | ||
33 | Solaris threads (for instance) are a multi-level thread implementation; | |
34 | the kernel provides a Light Weight Process (LWP) which the procfs | |
35 | process_stratum module is aware of. This module must then mediate | |
36 | the relationship between kernel LWP threads and user (eg. posix) | |
37 | threads. | |
38 | ||
39 | Linux threads are likely to be different -- but the thread_db | |
40 | library API should make the difference largely transparent to GDB. | |
41 | ||
42 | */ | |
43 | ||
44 | /* The thread_db API provides a number of functions that give the caller | |
45 | access to the inner workings of the child process's thread library. | |
46 | We will be using the following (others may be added): | |
47 | ||
48 | td_thr_validate Confirm valid "live" thread | |
49 | td_thr_get_info Get info about a thread | |
50 | td_thr_getgregs Get thread's general registers | |
51 | td_thr_getfpregs Get thread's floating point registers | |
52 | td_thr_setgregs Set thread's general registers | |
53 | td_thr_setfpregs Set thread's floating point registers | |
54 | td_ta_map_id2thr Get thread handle from thread id | |
55 | td_ta_map_lwp2thr Get thread handle from LWP id | |
56 | td_ta_thr_iter Iterate over all threads (with callback) | |
57 | ||
58 | In return, the debugger has to provide certain services to the | |
59 | thread_db library. Some of these aren't actually required to do | |
60 | anything in practice. For instance, the thread_db expects to be | |
61 | able to stop the child process and start it again: but in our | |
62 | context, the child process will always be stopped already when we | |
63 | invoke the thread_db library, so the functions that we provide for | |
64 | the library to stop and start the child process are no-ops. | |
65 | ||
66 | Here is the list of functions which we export to the thread_db | |
67 | library, divided into no-op functions vs. functions that actually | |
68 | have to do something: | |
69 | ||
70 | No-op functions: | |
71 | ||
72 | ps_pstop Stop the child process | |
73 | ps_pcontinue Continue the child process | |
74 | ps_lstop Stop a specific LWP (kernel thread) | |
75 | ps_lcontinue Continue an LWP | |
76 | ps_lgetxregsize Get size of LWP's xregs (sparc) | |
77 | ps_lgetxregs Get LWP's xregs (sparc) | |
78 | ps_lsetxregs Set LWP's xregs (sparc) | |
79 | ||
80 | Functions that have to do useful work: | |
81 | ||
82 | ps_pglobal_lookup Get the address of a global symbol | |
83 | ps_pdread Read memory, data segment | |
84 | ps_ptread Read memory, text segment | |
85 | ps_pdwrite Write memory, data segment | |
86 | ps_ptwrite Write memory, text segment | |
87 | ps_lgetregs Get LWP's general registers | |
88 | ps_lgetfpregs Get LWP's floating point registers | |
89 | ps_lsetregs Set LWP's general registers | |
90 | ps_lsetfpregs Set LWP's floating point registers | |
91 | ps_lgetLDT Get LWP's Local Descriptor Table (x86) | |
92 | ||
93 | Thus, if we ask the thread_db library to give us the general registers | |
94 | for user thread X, thread_db may figure out that user thread X is | |
95 | actually mapped onto kernel thread Y. Thread_db does not know how | |
96 | to obtain the registers for kernel thread Y, but GDB does, so thread_db | |
97 | turns the request right back to us via the ps_lgetregs callback. */ | |
98 | ||
99 | #include "defs.h" | |
100 | #include "gdbthread.h" | |
101 | #include "target.h" | |
102 | #include "inferior.h" | |
103 | #include "gdbcmd.h" | |
4e052eda | 104 | #include "regcache.h" |
ed9a39eb | 105 | |
03f2053f | 106 | #include "gdb_wait.h" |
ed9a39eb JM |
107 | |
108 | #include <time.h> | |
109 | ||
110 | #if defined(USE_PROC_FS) || defined(HAVE_GREGSET_T) | |
111 | #include <sys/procfs.h> | |
112 | #endif | |
113 | ||
ed9a39eb | 114 | #include "gdb_proc_service.h" |
ed9a39eb JM |
115 | |
116 | #if defined HAVE_STDINT_H /* Pre-5.2 systems don't have this header */ | |
117 | #if defined (HAVE_THREAD_DB_H) | |
118 | #include <thread_db.h> /* defines outgoing API (td_thr_* calls) */ | |
119 | #else | |
120 | #include "gdb_thread_db.h" | |
121 | #endif | |
122 | ||
123 | #include <dlfcn.h> /* dynamic library interface */ | |
124 | ||
c60c0f5f MS |
125 | /* Prototypes for supply_gregset etc. */ |
126 | #include "gregset.h" | |
127 | ||
ed9a39eb JM |
128 | #ifndef TIDGET |
129 | #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16) | |
39f77062 KB |
130 | #define PIDGET0(PID) (((PID) & 0xffff)) |
131 | #define PIDGET(PID) ((PIDGET0 (PID) == 0xffff) ? -1 : PIDGET0 (PID)) | |
ed9a39eb JM |
132 | #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16)) |
133 | #endif | |
134 | ||
39f77062 | 135 | /* Macros for superimposing PID and TID into inferior_ptid. */ |
ed9a39eb JM |
136 | #define THREAD_FLAG 0x80000000 |
137 | #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0) | |
138 | #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0) | |
139 | #define GET_LWP(PID) TIDGET (PID) | |
140 | #define GET_THREAD(PID) TIDGET (PID) | |
141 | #define BUILD_LWP(TID, PID) MERGEPID (PID, TID) | |
142 | #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG) | |
143 | ||
144 | /* | |
145 | * target_beneath is a pointer to the target_ops underlying this one. | |
146 | */ | |
147 | ||
148 | static struct target_ops *target_beneath; | |
149 | ||
150 | ||
151 | /* | |
152 | * target vector defined in this module: | |
153 | */ | |
154 | ||
155 | static struct target_ops thread_db_ops; | |
156 | ||
157 | /* | |
158 | * Typedefs required to resolve differences between the thread_db | |
159 | * and proc_service API defined on different versions of Solaris: | |
160 | */ | |
161 | ||
162 | #if defined(PROC_SERVICE_IS_OLD) | |
163 | typedef const struct ps_prochandle *gdb_ps_prochandle_t; | |
164 | typedef char *gdb_ps_read_buf_t; | |
165 | typedef char *gdb_ps_write_buf_t; | |
166 | typedef int gdb_ps_size_t; | |
167 | #else | |
168 | typedef struct ps_prochandle *gdb_ps_prochandle_t; | |
169 | typedef void *gdb_ps_read_buf_t; | |
170 | typedef const void *gdb_ps_write_buf_t; | |
171 | typedef size_t gdb_ps_size_t; | |
172 | #endif | |
173 | ||
174 | /* | |
175 | * proc_service callback functions, called by thread_db. | |
176 | */ | |
177 | ||
178 | ps_err_e | |
179 | ps_pstop (gdb_ps_prochandle_t ph) /* Process stop */ | |
180 | { | |
181 | return PS_OK; | |
182 | } | |
183 | ||
184 | ps_err_e | |
185 | ps_pcontinue (gdb_ps_prochandle_t ph) /* Process continue */ | |
186 | { | |
187 | return PS_OK; | |
188 | } | |
189 | ||
190 | ps_err_e | |
191 | ps_lstop (gdb_ps_prochandle_t ph, /* LWP stop */ | |
192 | lwpid_t lwpid) | |
193 | { | |
194 | return PS_OK; | |
195 | } | |
196 | ||
197 | ps_err_e | |
198 | ps_lcontinue (gdb_ps_prochandle_t ph, /* LWP continue */ | |
199 | lwpid_t lwpid) | |
200 | { | |
201 | return PS_OK; | |
202 | } | |
203 | ||
204 | ps_err_e | |
205 | ps_lgetxregsize (gdb_ps_prochandle_t ph, /* Get XREG size */ | |
206 | lwpid_t lwpid, | |
207 | int *xregsize) | |
208 | { | |
209 | return PS_OK; | |
210 | } | |
211 | ||
212 | ps_err_e | |
213 | ps_lgetxregs (gdb_ps_prochandle_t ph, /* Get XREGS */ | |
214 | lwpid_t lwpid, | |
215 | caddr_t xregset) | |
216 | { | |
217 | return PS_OK; | |
218 | } | |
219 | ||
220 | ps_err_e | |
221 | ps_lsetxregs (gdb_ps_prochandle_t ph, /* Set XREGS */ | |
222 | lwpid_t lwpid, | |
223 | caddr_t xregset) | |
224 | { | |
225 | return PS_OK; | |
226 | } | |
227 | ||
228 | void | |
229 | ps_plog (const char *fmt, ...) | |
230 | { | |
231 | va_list args; | |
232 | ||
233 | va_start (args, fmt); | |
234 | vfprintf_filtered (gdb_stderr, fmt, args); | |
235 | } | |
236 | ||
237 | /* Look up a symbol in GDB's global symbol table. | |
238 | Return the symbol's address. | |
239 | FIXME: it would be more correct to look up the symbol in the context | |
240 | of the LD_OBJECT_NAME provided. However we're probably fairly safe | |
241 | as long as there aren't name conflicts with other libraries. */ | |
242 | ||
243 | ps_err_e | |
244 | ps_pglobal_lookup (gdb_ps_prochandle_t ph, | |
245 | const char *ld_object_name, /* the library name */ | |
246 | const char *ld_symbol_name, /* the symbol name */ | |
247 | paddr_t *ld_symbol_addr) /* return the symbol addr */ | |
248 | { | |
249 | struct minimal_symbol *ms; | |
250 | ||
251 | ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL); | |
252 | ||
253 | if (!ms) | |
254 | return PS_NOSYM; | |
255 | ||
256 | *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms); | |
257 | ||
258 | return PS_OK; | |
259 | } | |
260 | ||
261 | /* Worker function for all memory reads and writes: */ | |
262 | static ps_err_e rw_common (const struct ps_prochandle *ph, | |
263 | paddr_t addr, | |
264 | char *buf, | |
265 | int size, | |
266 | int write_p); | |
267 | ||
268 | /* target_xfer_memory direction consts */ | |
269 | enum {PS_READ = 0, PS_WRITE = 1}; | |
270 | ||
271 | ps_err_e | |
272 | ps_pdread (gdb_ps_prochandle_t ph, /* read from data segment */ | |
273 | paddr_t addr, | |
274 | gdb_ps_read_buf_t buf, | |
275 | gdb_ps_size_t size) | |
276 | { | |
277 | return rw_common (ph, addr, buf, size, PS_READ); | |
278 | } | |
279 | ||
280 | ps_err_e | |
281 | ps_pdwrite (gdb_ps_prochandle_t ph, /* write to data segment */ | |
282 | paddr_t addr, | |
283 | gdb_ps_write_buf_t buf, | |
284 | gdb_ps_size_t size) | |
285 | { | |
286 | return rw_common (ph, addr, (char *) buf, size, PS_WRITE); | |
287 | } | |
288 | ||
289 | ps_err_e | |
290 | ps_ptread (gdb_ps_prochandle_t ph, /* read from text segment */ | |
291 | paddr_t addr, | |
292 | gdb_ps_read_buf_t buf, | |
293 | gdb_ps_size_t size) | |
294 | { | |
295 | return rw_common (ph, addr, buf, size, PS_READ); | |
296 | } | |
297 | ||
298 | ps_err_e | |
299 | ps_ptwrite (gdb_ps_prochandle_t ph, /* write to text segment */ | |
300 | paddr_t addr, | |
301 | gdb_ps_write_buf_t buf, | |
302 | gdb_ps_size_t size) | |
303 | { | |
304 | return rw_common (ph, addr, (char *) buf, size, PS_WRITE); | |
305 | } | |
306 | ||
ed9a39eb JM |
307 | static char *thr_err_string (td_err_e); |
308 | static char *thr_state_string (td_thr_state_e); | |
309 | ||
ed9a39eb JM |
310 | struct ps_prochandle main_prochandle; |
311 | td_thragent_t * main_threadagent; | |
312 | ||
313 | /* | |
314 | * Common proc_service routine for reading and writing memory. | |
315 | */ | |
316 | ||
39f77062 | 317 | /* FIXME: once we've munged the inferior_ptid, why can't we |
ed9a39eb JM |
318 | simply call target_read/write_memory and return? */ |
319 | ||
ed9a39eb JM |
320 | static ps_err_e |
321 | rw_common (const struct ps_prochandle *ph, | |
322 | paddr_t addr, | |
323 | char *buf, | |
324 | int size, | |
325 | int write_p) | |
326 | { | |
39f77062 | 327 | struct cleanup *old_chain = save_inferior_ptid (); |
ed9a39eb JM |
328 | int to_do = size; |
329 | int done = 0; | |
330 | ||
39f77062 | 331 | inferior_ptid = pid_to_ptid (main_prochandle.pid); |
ed9a39eb JM |
332 | |
333 | while (to_do > 0) | |
334 | { | |
335 | done = current_target.to_xfer_memory (addr, buf, size, write_p, | |
336 | ¤t_target); | |
337 | if (done <= 0) | |
338 | { | |
339 | if (write_p == PS_READ) | |
340 | print_sys_errmsg ("rw_common (): read", errno); | |
341 | else | |
342 | print_sys_errmsg ("rw_common (): write", errno); | |
343 | ||
344 | return PS_ERR; | |
345 | } | |
346 | to_do -= done; | |
347 | buf += done; | |
348 | } | |
349 | do_cleanups (old_chain); | |
350 | return PS_OK; | |
351 | } | |
352 | ||
353 | /* Cleanup functions used by the register callbacks | |
39f77062 | 354 | (which have to manipulate the global inferior_ptid). */ |
ed9a39eb JM |
355 | |
356 | ps_err_e | |
357 | ps_lgetregs (gdb_ps_prochandle_t ph, /* Get LWP general regs */ | |
358 | lwpid_t lwpid, | |
359 | prgregset_t gregset) | |
360 | { | |
39f77062 | 361 | struct cleanup *old_chain = save_inferior_ptid (); |
ed9a39eb | 362 | |
39f77062 | 363 | inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid); |
ed9a39eb JM |
364 | current_target.to_fetch_registers (-1); |
365 | ||
39f77062 | 366 | fill_gregset ((gdb_gregset_t *) gregset, -1); |
ed9a39eb JM |
367 | do_cleanups (old_chain); |
368 | ||
369 | return PS_OK; | |
370 | } | |
371 | ||
372 | ps_err_e | |
373 | ps_lsetregs (gdb_ps_prochandle_t ph, /* Set LWP general regs */ | |
374 | lwpid_t lwpid, | |
375 | const prgregset_t gregset) | |
376 | { | |
39f77062 | 377 | struct cleanup *old_chain = save_inferior_ptid (); |
ed9a39eb | 378 | |
39f77062 KB |
379 | inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid); |
380 | supply_gregset ((gdb_gregset_t *) gregset); | |
ed9a39eb JM |
381 | current_target.to_store_registers (-1); |
382 | do_cleanups (old_chain); | |
383 | return PS_OK; | |
384 | } | |
385 | ||
386 | ps_err_e | |
387 | ps_lgetfpregs (gdb_ps_prochandle_t ph, /* Get LWP float regs */ | |
388 | lwpid_t lwpid, | |
d84dd0c5 | 389 | gdb_prfpregset_t *fpregset) |
ed9a39eb | 390 | { |
39f77062 | 391 | struct cleanup *old_chain = save_inferior_ptid (); |
ed9a39eb | 392 | |
39f77062 | 393 | inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid); |
ed9a39eb JM |
394 | current_target.to_fetch_registers (-1); |
395 | fill_fpregset (fpregset, -1); | |
396 | do_cleanups (old_chain); | |
397 | return PS_OK; | |
398 | } | |
399 | ||
400 | ps_err_e | |
401 | ps_lsetfpregs (gdb_ps_prochandle_t ph, /* Set LWP float regs */ | |
402 | lwpid_t lwpid, | |
d84dd0c5 | 403 | const gdb_prfpregset_t *fpregset) |
ed9a39eb | 404 | { |
39f77062 | 405 | struct cleanup *old_chain = save_inferior_ptid (); |
ed9a39eb | 406 | |
39f77062 | 407 | inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid); |
ed9a39eb JM |
408 | supply_fpregset (fpregset); |
409 | current_target.to_store_registers (-1); | |
410 | do_cleanups (old_chain); | |
411 | return PS_OK; | |
412 | } | |
413 | ||
414 | /* | |
415 | * ps_getpid | |
416 | * | |
417 | * return the main pid for the child process | |
418 | * (special for Linux -- not used on Solaris) | |
419 | */ | |
420 | ||
421 | pid_t | |
422 | ps_getpid (gdb_ps_prochandle_t ph) | |
423 | { | |
424 | return ph->pid; | |
425 | } | |
426 | ||
427 | #ifdef TM_I386SOL2_H | |
428 | ||
429 | /* Reads the local descriptor table of a LWP. */ | |
430 | ||
431 | ps_err_e | |
432 | ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid, | |
433 | struct ssd *pldt) | |
434 | { | |
435 | /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */ | |
436 | extern struct ssd *procfs_find_LDT_entry (int); | |
437 | struct ssd *ret; | |
438 | ||
439 | ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, | |
440 | PIDGET (main_prochandle.pid))); | |
441 | if (ret) | |
442 | { | |
443 | memcpy (pldt, ret, sizeof (struct ssd)); | |
444 | return PS_OK; | |
445 | } | |
446 | else /* LDT not found. */ | |
447 | return PS_ERR; | |
448 | } | |
449 | #endif /* TM_I386SOL2_H */ | |
450 | ||
451 | /* | |
452 | * Pointers to thread_db functions: | |
453 | * | |
454 | * These are a dynamic library mechanism. | |
455 | * The dlfcn.h interface will be used to initialize these | |
456 | * so that they point to the appropriate functions in the | |
457 | * thread_db dynamic library. This is done dynamically | |
458 | * so that GDB can still run on systems that lack thread_db. | |
459 | */ | |
460 | ||
461 | static td_err_e (*p_td_init) (void); | |
462 | ||
463 | static td_err_e (*p_td_ta_new) (const struct ps_prochandle *ph_p, | |
464 | td_thragent_t **ta_pp); | |
465 | ||
466 | static td_err_e (*p_td_ta_delete) (td_thragent_t *ta_p); | |
467 | ||
468 | static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p, | |
469 | int *nthread_p); | |
470 | ||
471 | ||
472 | static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t *ta_p, | |
473 | td_thr_iter_f *cb, | |
474 | void *cbdata_p, | |
475 | td_thr_state_e state, | |
476 | int ti_pri, | |
477 | sigset_t *ti_sigmask_p, | |
478 | unsigned ti_user_flags); | |
479 | ||
480 | static td_err_e (*p_td_ta_event_addr) (const td_thragent_t *ta_p, | |
481 | u_long event, | |
482 | td_notify_t *notify_p); | |
483 | ||
484 | static td_err_e (*p_td_ta_event_getmsg) (const td_thragent_t *ta_p, | |
485 | td_event_msg_t *msg); | |
486 | ||
487 | static td_err_e (*p_td_ta_set_event) (const td_thragent_t *ta_p, | |
488 | td_thr_events_t *events); | |
489 | ||
490 | static td_err_e (*p_td_thr_validate) (const td_thrhandle_t *th_p); | |
491 | ||
492 | static td_err_e (*p_td_thr_event_enable) (const td_thrhandle_t *th_p, | |
493 | int on_off); | |
494 | ||
495 | static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t *th_p, | |
496 | td_thrinfo_t *ti_p); | |
497 | ||
498 | static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t *th_p, | |
499 | prgregset_t regset); | |
500 | ||
501 | static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t *th_p, | |
502 | const prgregset_t regset); | |
503 | ||
504 | static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p, | |
d84dd0c5 | 505 | gdb_prfpregset_t *fpregset); |
ed9a39eb JM |
506 | |
507 | static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p, | |
d84dd0c5 | 508 | const gdb_prfpregset_t *fpregset); |
ed9a39eb JM |
509 | |
510 | static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p, | |
511 | thread_t tid, | |
512 | td_thrhandle_t *th_p); | |
513 | ||
514 | static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p, | |
515 | lwpid_t lwpid, | |
516 | td_thrhandle_t *th_p); | |
517 | ||
518 | /* | |
519 | * API and target vector initialization function: thread_db_initialize. | |
520 | * | |
521 | * NOTE: this function is deliberately NOT named with the GDB convention | |
522 | * of module initializer function names that begin with "_initialize". | |
523 | * This module is NOT intended to be auto-initialized at GDB startup. | |
524 | * Rather, it will only be initialized when a multi-threaded child | |
525 | * process is detected. | |
526 | * | |
527 | */ | |
528 | ||
529 | /* | |
530 | * Initializer for thread_db library interface. | |
531 | * This function does the dynamic library stuff (dlopen, dlsym), | |
532 | * and then calls the thread_db library's one-time initializer | |
533 | * function (td_init). If everything succeeds, this function | |
534 | * returns true; otherwise it returns false, and this module | |
535 | * cannot be used. | |
536 | */ | |
537 | ||
538 | static int | |
fba45db2 | 539 | init_thread_db_library (void) |
ed9a39eb JM |
540 | { |
541 | void *dlhandle; | |
542 | td_err_e ret; | |
543 | ||
544 | /* Open a handle to the "thread_db" dynamic library. */ | |
545 | if ((dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW)) == NULL) | |
546 | return 0; /* fail */ | |
547 | ||
548 | /* Initialize pointers to the dynamic library functions we will use. | |
549 | * Note that we are not calling the functions here -- we are only | |
550 | * establishing pointers to them. | |
551 | */ | |
552 | ||
553 | /* td_init: initialize thread_db library. */ | |
554 | if ((p_td_init = dlsym (dlhandle, "td_init")) == NULL) | |
555 | return 0; /* fail */ | |
556 | /* td_ta_new: register a target process with thread_db. */ | |
557 | if ((p_td_ta_new = dlsym (dlhandle, "td_ta_new")) == NULL) | |
558 | return 0; /* fail */ | |
559 | /* td_ta_delete: un-register a target process with thread_db. */ | |
560 | if ((p_td_ta_delete = dlsym (dlhandle, "td_ta_delete")) == NULL) | |
561 | return 0; /* fail */ | |
562 | ||
563 | /* td_ta_map_id2thr: get thread handle from thread id. */ | |
564 | if ((p_td_ta_map_id2thr = dlsym (dlhandle, "td_ta_map_id2thr")) == NULL) | |
565 | return 0; /* fail */ | |
566 | /* td_ta_map_lwp2thr: get thread handle from lwp id. */ | |
567 | if ((p_td_ta_map_lwp2thr = dlsym (dlhandle, "td_ta_map_lwp2thr")) == NULL) | |
568 | return 0; /* fail */ | |
569 | /* td_ta_get_nthreads: get number of threads in target process. */ | |
570 | if ((p_td_ta_get_nthreads = dlsym (dlhandle, "td_ta_get_nthreads")) == NULL) | |
571 | return 0; /* fail */ | |
572 | /* td_ta_thr_iter: iterate over all thread handles. */ | |
573 | if ((p_td_ta_thr_iter = dlsym (dlhandle, "td_ta_thr_iter")) == NULL) | |
574 | return 0; /* fail */ | |
575 | ||
576 | /* td_thr_validate: make sure a thread handle is real and alive. */ | |
577 | if ((p_td_thr_validate = dlsym (dlhandle, "td_thr_validate")) == NULL) | |
578 | return 0; /* fail */ | |
579 | /* td_thr_get_info: get a bunch of info about a thread. */ | |
580 | if ((p_td_thr_get_info = dlsym (dlhandle, "td_thr_get_info")) == NULL) | |
581 | return 0; /* fail */ | |
582 | /* td_thr_getgregs: get general registers for thread. */ | |
583 | if ((p_td_thr_getgregs = dlsym (dlhandle, "td_thr_getgregs")) == NULL) | |
584 | return 0; /* fail */ | |
585 | /* td_thr_setgregs: set general registers for thread. */ | |
586 | if ((p_td_thr_setgregs = dlsym (dlhandle, "td_thr_setgregs")) == NULL) | |
587 | return 0; /* fail */ | |
588 | /* td_thr_getfpregs: get floating point registers for thread. */ | |
589 | if ((p_td_thr_getfpregs = dlsym (dlhandle, "td_thr_getfpregs")) == NULL) | |
590 | return 0; /* fail */ | |
591 | /* td_thr_setfpregs: set floating point registers for thread. */ | |
592 | if ((p_td_thr_setfpregs = dlsym (dlhandle, "td_thr_setfpregs")) == NULL) | |
593 | return 0; /* fail */ | |
594 | ||
595 | ret = p_td_init (); | |
596 | if (ret != TD_OK) | |
597 | { | |
598 | warning ("init_thread_db: td_init: %s", thr_err_string (ret)); | |
599 | return 0; | |
600 | } | |
601 | ||
602 | /* Optional functions: | |
603 | We can still debug even if the following functions are not found. */ | |
604 | ||
605 | /* td_ta_event_addr: get the breakpoint address for specified event. */ | |
606 | p_td_ta_event_addr = dlsym (dlhandle, "td_ta_event_addr"); | |
607 | ||
608 | /* td_ta_event_getmsg: get the next event message for the process. */ | |
609 | p_td_ta_event_getmsg = dlsym (dlhandle, "td_ta_event_getmsg"); | |
610 | ||
611 | /* td_ta_set_event: request notification of an event. */ | |
612 | p_td_ta_set_event = dlsym (dlhandle, "td_ta_set_event"); | |
613 | ||
614 | /* td_thr_event_enable: enable event reporting in a thread. */ | |
615 | p_td_thr_event_enable = dlsym (dlhandle, "td_thr_event_enable"); | |
616 | ||
617 | return 1; /* success */ | |
618 | } | |
619 | ||
620 | /* | |
621 | * Local utility functions: | |
622 | */ | |
623 | ||
ed9a39eb JM |
624 | /* |
625 | ||
626 | LOCAL FUNCTION | |
627 | ||
628 | thr_err_string - Convert a thread_db error code to a string | |
629 | ||
630 | SYNOPSIS | |
631 | ||
632 | char * thr_err_string (errcode) | |
633 | ||
634 | DESCRIPTION | |
635 | ||
636 | Return a string description of the thread_db errcode. If errcode | |
637 | is unknown, then return an <unknown> message. | |
638 | ||
639 | */ | |
640 | ||
641 | static char * | |
fba45db2 | 642 | thr_err_string (td_err_e errcode) |
ed9a39eb JM |
643 | { |
644 | static char buf[50]; | |
645 | ||
646 | switch (errcode) { | |
647 | case TD_OK: return "generic 'call succeeded'"; | |
648 | case TD_ERR: return "generic error"; | |
649 | case TD_NOTHR: return "no thread to satisfy query"; | |
650 | case TD_NOSV: return "no sync handle to satisfy query"; | |
651 | case TD_NOLWP: return "no lwp to satisfy query"; | |
652 | case TD_BADPH: return "invalid process handle"; | |
653 | case TD_BADTH: return "invalid thread handle"; | |
654 | case TD_BADSH: return "invalid synchronization handle"; | |
655 | case TD_BADTA: return "invalid thread agent"; | |
656 | case TD_BADKEY: return "invalid key"; | |
657 | case TD_NOMSG: return "no event message for getmsg"; | |
658 | case TD_NOFPREGS: return "FPU register set not available"; | |
659 | case TD_NOLIBTHREAD: return "application not linked with libthread"; | |
660 | case TD_NOEVENT: return "requested event is not supported"; | |
661 | case TD_NOCAPAB: return "capability not available"; | |
662 | case TD_DBERR: return "debugger service failed"; | |
663 | case TD_NOAPLIC: return "operation not applicable to"; | |
664 | case TD_NOTSD: return "no thread-specific data for this thread"; | |
665 | case TD_MALLOC: return "malloc failed"; | |
666 | case TD_PARTIALREG: return "only part of register set was written/read"; | |
667 | case TD_NOXREGS: return "X register set not available for this thread"; | |
668 | default: | |
669 | sprintf (buf, "unknown thread_db error '%d'", errcode); | |
670 | return buf; | |
671 | } | |
672 | } | |
673 | ||
674 | /* | |
675 | ||
676 | LOCAL FUNCTION | |
677 | ||
678 | thr_state_string - Convert a thread_db state code to a string | |
679 | ||
680 | SYNOPSIS | |
681 | ||
682 | char *thr_state_string (statecode) | |
683 | ||
684 | DESCRIPTION | |
685 | ||
686 | Return the thread_db state string associated with statecode. | |
687 | If statecode is unknown, then return an <unknown> message. | |
688 | ||
689 | */ | |
690 | ||
691 | static char * | |
fba45db2 | 692 | thr_state_string (td_thr_state_e statecode) |
ed9a39eb JM |
693 | { |
694 | static char buf[50]; | |
695 | ||
696 | switch (statecode) { | |
697 | case TD_THR_STOPPED: return "stopped by debugger"; | |
698 | case TD_THR_RUN: return "runnable"; | |
699 | case TD_THR_ACTIVE: return "active"; | |
700 | case TD_THR_ZOMBIE: return "zombie"; | |
701 | case TD_THR_SLEEP: return "sleeping"; | |
702 | case TD_THR_STOPPED_ASLEEP: return "stopped by debugger AND blocked"; | |
703 | default: | |
704 | sprintf (buf, "unknown thread_db state %d", statecode); | |
705 | return buf; | |
706 | } | |
707 | } | |
708 | ||
709 | /* | |
710 | * Local thread/event list. | |
711 | * This data structure will be used to hold a list of threads and | |
712 | * pending/deliverable events. | |
713 | */ | |
714 | ||
715 | typedef struct THREADINFO { | |
716 | thread_t tid; /* thread ID */ | |
717 | pid_t lid; /* process/lwp ID */ | |
718 | td_thr_state_e state; /* thread state (a la thread_db) */ | |
719 | td_thr_type_e type; /* thread type (a la thread_db) */ | |
720 | int pending; /* true if holding a pending event */ | |
721 | int status; /* wait status of any interesting event */ | |
722 | } threadinfo; | |
723 | ||
724 | threadinfo * threadlist; | |
725 | int threadlist_max = 0; /* current size of table */ | |
726 | int threadlist_top = 0; /* number of threads now in table */ | |
727 | #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */ | |
728 | ||
729 | static threadinfo * | |
fba45db2 | 730 | insert_thread (int tid, int lid, td_thr_state_e state, td_thr_type_e type) |
ed9a39eb JM |
731 | { |
732 | if (threadlist_top >= threadlist_max) | |
733 | { | |
734 | threadlist_max += THREADLIST_ALLOC; | |
0e52036f AC |
735 | threadlist = xrealloc (threadlist, |
736 | threadlist_max * sizeof (threadinfo)); | |
ed9a39eb JM |
737 | if (threadlist == NULL) |
738 | return NULL; | |
739 | } | |
740 | threadlist[threadlist_top].tid = tid; | |
741 | threadlist[threadlist_top].lid = lid; | |
742 | threadlist[threadlist_top].state = state; | |
743 | threadlist[threadlist_top].type = type; | |
744 | threadlist[threadlist_top].pending = 0; | |
745 | threadlist[threadlist_top].status = 0; | |
746 | ||
747 | return &threadlist[threadlist_top++]; | |
748 | } | |
749 | ||
750 | static void | |
fba45db2 | 751 | empty_threadlist (void) |
ed9a39eb JM |
752 | { |
753 | threadlist_top = 0; | |
754 | } | |
755 | ||
756 | static threadinfo * | |
fba45db2 | 757 | next_pending_event (void) |
ed9a39eb JM |
758 | { |
759 | int i; | |
760 | ||
761 | for (i = 0; i < threadlist_top; i++) | |
762 | if (threadlist[i].pending) | |
763 | return &threadlist[i]; | |
764 | ||
765 | return NULL; | |
766 | } | |
767 | ||
768 | static void | |
064002de KB |
769 | threadlist_iter (int (*func) (), void *data, td_thr_state_e state, |
770 | td_thr_type_e type) | |
ed9a39eb JM |
771 | { |
772 | int i; | |
773 | ||
774 | for (i = 0; i < threadlist_top; i++) | |
775 | if ((state == TD_THR_ANY_STATE || state == threadlist[i].state) && | |
776 | (type == TD_THR_ANY_TYPE || type == threadlist[i].type)) | |
777 | if ((*func) (&threadlist[i], data) != 0) | |
778 | break; | |
779 | ||
780 | return; | |
781 | } | |
782 | ||
783 | /* | |
784 | * Global state | |
785 | * | |
786 | * Here we keep state information all collected in one place. | |
787 | */ | |
788 | ||
789 | /* This flag is set when we activate, so that we don't do it twice. | |
790 | Defined in linux-thread.c and used for inter-target syncronization. */ | |
791 | extern int using_thread_db; | |
792 | ||
793 | /* The process id for which we've stopped. | |
794 | * This is only set when we actually stop all threads. | |
795 | * Otherwise it's zero. | |
796 | */ | |
797 | static int event_pid; | |
798 | ||
799 | /* | |
800 | * The process id for a new thread to which we've just attached. | |
801 | * This process needs special handling at resume time. | |
802 | */ | |
803 | static int attach_pid; | |
804 | ||
805 | ||
806 | /* | |
807 | * thread_db event handling: | |
808 | * | |
809 | * The mechanism for event notification via the thread_db API. | |
810 | * These events are implemented as breakpoints. The thread_db | |
811 | * library gives us an address where we can set a breakpoint. | |
812 | * When the breakpoint is hit, it represents an event of interest | |
813 | * such as: | |
814 | * Thread creation | |
815 | * Thread death | |
816 | * Thread reap | |
817 | */ | |
818 | ||
819 | /* Location of the thread creation event breakpoint. The code at this | |
820 | location in the child process will be called by the pthread library | |
821 | whenever a new thread is created. By setting a special breakpoint | |
822 | at this location, GDB can detect when a new thread is created. We | |
823 | obtain this location via the td_ta_event_addr call. */ | |
824 | ||
825 | static CORE_ADDR thread_creation_bkpt_address; | |
826 | ||
827 | /* Location of the thread death event breakpoint. The code at this | |
828 | location in the child process will be called by the pthread library | |
829 | whenever a thread is destroyed. By setting a special breakpoint at | |
830 | this location, GDB can detect when a new thread is created. We | |
831 | obtain this location via the td_ta_event_addr call. */ | |
832 | ||
833 | static CORE_ADDR thread_death_bkpt_address; | |
834 | ||
835 | /* This function handles the global parts of enabling thread events. | |
836 | The thread-specific enabling is handled per-thread elsewhere. */ | |
837 | ||
838 | static void | |
fba45db2 | 839 | enable_thread_event_reporting (td_thragent_t *ta) |
ed9a39eb JM |
840 | { |
841 | td_thr_events_t events; | |
842 | td_notify_t notify; | |
843 | CORE_ADDR addr; | |
844 | ||
845 | if (p_td_ta_set_event == NULL || | |
846 | p_td_ta_event_addr == NULL || | |
847 | p_td_ta_event_getmsg == NULL || | |
848 | p_td_thr_event_enable == NULL) | |
849 | return; /* can't do thread event reporting without these funcs */ | |
850 | ||
851 | /* set process wide mask saying which events we are interested in */ | |
852 | td_event_emptyset (&events); | |
853 | td_event_addset (&events, TD_CREATE); | |
854 | td_event_addset (&events, TD_DEATH); | |
855 | ||
856 | if (p_td_ta_set_event (ta, &events) != TD_OK) | |
857 | { | |
858 | warning ("unable to set global thread event mask"); | |
859 | return; | |
860 | } | |
861 | ||
862 | /* Delete previous thread event breakpoints, if any. */ | |
863 | remove_thread_event_breakpoints (); | |
864 | ||
865 | /* create breakpoints -- thread creation and death */ | |
866 | /* thread creation */ | |
867 | /* get breakpoint location */ | |
868 | if (p_td_ta_event_addr (ta, TD_CREATE, ¬ify) != TD_OK) | |
869 | { | |
870 | warning ("unable to get location for thread creation breakpoint"); | |
871 | return; | |
872 | } | |
873 | ||
874 | /* Set up the breakpoint. */ | |
39f77062 | 875 | create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr); |
ed9a39eb JM |
876 | |
877 | /* Save it's location. */ | |
39f77062 | 878 | thread_creation_bkpt_address = (CORE_ADDR) notify.u.bptaddr; |
ed9a39eb JM |
879 | |
880 | /* thread death */ | |
881 | /* get breakpoint location */ | |
882 | if (p_td_ta_event_addr (ta, TD_DEATH, ¬ify) != TD_OK) | |
883 | { | |
884 | warning ("unable to get location for thread death breakpoint"); | |
885 | return; | |
886 | } | |
887 | /* Set up the breakpoint. */ | |
39f77062 | 888 | create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr); |
ed9a39eb JM |
889 | |
890 | /* Save it's location. */ | |
39f77062 | 891 | thread_death_bkpt_address = (CORE_ADDR) notify.u.bptaddr; |
ed9a39eb JM |
892 | } |
893 | ||
894 | /* This function handles the global parts of disabling thread events. | |
895 | The thread-specific enabling is handled per-thread elsewhere. */ | |
896 | ||
897 | static void | |
fba45db2 | 898 | disable_thread_event_reporting (td_thragent_t *ta) |
ed9a39eb JM |
899 | { |
900 | td_thr_events_t events; | |
901 | ||
902 | /* set process wide mask saying we aren't interested in any events */ | |
903 | td_event_emptyset (&events); | |
904 | p_td_ta_set_event (main_threadagent, &events); | |
905 | ||
906 | /* Delete thread event breakpoints, if any. */ | |
907 | remove_thread_event_breakpoints (); | |
908 | thread_creation_bkpt_address = 0; | |
909 | thread_death_bkpt_address = 0; | |
910 | } | |
911 | ||
912 | /* check_for_thread_event | |
913 | ||
914 | if it's a thread event we recognize (currently | |
915 | we only recognize creation and destruction | |
916 | events), return 1; else return 0. */ | |
917 | ||
918 | ||
919 | static int | |
920 | check_for_thread_event (struct target_waitstatus *tws, int event_pid) | |
921 | { | |
922 | /* FIXME: to be more efficient, we should keep a static | |
923 | list of threads, and update it only here (with td_ta_thr_iter). */ | |
39f77062 | 924 | return 0; |
ed9a39eb JM |
925 | } |
926 | ||
927 | static void | |
928 | thread_db_push_target (void) | |
929 | { | |
930 | /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */ | |
931 | ||
932 | /* Push this target vector */ | |
933 | push_target (&thread_db_ops); | |
934 | /* Find the underlying process-layer target for calling later. */ | |
935 | target_beneath = find_target_beneath (&thread_db_ops); | |
936 | using_thread_db = 1; | |
937 | /* Turn on thread_db event-reporting API. */ | |
938 | enable_thread_event_reporting (main_threadagent); | |
939 | } | |
940 | ||
941 | static void | |
942 | thread_db_unpush_target (void) | |
943 | { | |
944 | /* Must be called whenever we remove ourself from the target stack! */ | |
945 | ||
946 | using_thread_db = 0; | |
947 | target_beneath = NULL; | |
948 | ||
949 | /* delete local list of threads */ | |
950 | empty_threadlist (); | |
951 | /* Turn off the thread_db API. */ | |
952 | p_td_ta_delete (main_threadagent); | |
953 | /* Unpush this target vector */ | |
954 | unpush_target (&thread_db_ops); | |
955 | /* Reset linuxthreads module. */ | |
956 | linuxthreads_discard_global_state (); | |
957 | } | |
958 | ||
959 | /* | |
960 | * New objfile hook function: | |
961 | * Called for each new objfile (image, shared lib) in the target process. | |
962 | * | |
963 | * The purpose of this function is to detect that the target process | |
964 | * is linked with the (appropriate) thread library. So every time a | |
965 | * new target shared library is detected, we will call td_ta_new. | |
966 | * If it succeeds, we know we have a multi-threaded target process | |
967 | * that we can debug using the thread_db API. | |
968 | */ | |
969 | ||
970 | /* | |
971 | * new_objfile function: | |
972 | * | |
973 | * connected to target_new_objfile_hook, this function gets called | |
974 | * every time a new binary image is loaded. | |
975 | * | |
976 | * At each call, we attempt to open the thread_db connection to the | |
977 | * child process. If it succeeds, we know we have a libthread process | |
978 | * and we can debug it with this target vector. Therefore we push | |
979 | * ourself onto the target stack. | |
980 | */ | |
981 | ||
982 | static void (*target_new_objfile_chain) (struct objfile *objfile); | |
983 | static int stop_or_attach_thread_callback (const td_thrhandle_t *th, | |
984 | void *data); | |
985 | static int wait_thread_callback (const td_thrhandle_t *th, | |
986 | void *data); | |
987 | ||
988 | static void | |
989 | thread_db_new_objfile (struct objfile *objfile) | |
990 | { | |
991 | td_err_e ret; | |
992 | ||
993 | if (using_thread_db) /* libthread already detected, and */ | |
994 | goto quit; /* thread target vector activated. */ | |
995 | ||
996 | if (objfile == NULL) | |
997 | goto quit; /* un-interesting object file */ | |
998 | ||
999 | /* Initialize our "main prochandle" with the main inferior pid. */ | |
39f77062 | 1000 | main_prochandle.pid = PIDGET (inferior_ptid); |
ed9a39eb JM |
1001 | |
1002 | /* Now attempt to open a thread_db connection to the | |
1003 | thread library running in the child process. */ | |
1004 | ret = p_td_ta_new (&main_prochandle, &main_threadagent); | |
1005 | switch (ret) { | |
1006 | default: | |
1007 | warning ("Unexpected error initializing thread_db: %s", | |
1008 | thr_err_string (ret)); | |
1009 | break; | |
1010 | case TD_NOLIBTHREAD: /* expected: no libthread in child process (yet) */ | |
1011 | break; | |
1012 | case TD_OK: /* libthread detected in child: we go live now! */ | |
1013 | thread_db_push_target (); | |
39f77062 | 1014 | event_pid = PIDGET (inferior_ptid); /* for resume */ |
ed9a39eb JM |
1015 | |
1016 | /* Now stop everyone else, and attach any new threads you find. */ | |
1017 | p_td_ta_thr_iter (main_threadagent, | |
1018 | stop_or_attach_thread_callback, | |
1019 | (void *) 0, | |
1020 | TD_THR_ANY_STATE, | |
1021 | TD_THR_LOWEST_PRIORITY, | |
1022 | TD_SIGNO_MASK, | |
1023 | TD_THR_ANY_USER_FLAGS); | |
1024 | ||
1025 | /* Now go call wait on all the threads you've stopped: | |
1026 | This allows us to absorb the SIGKILL event, and to make sure | |
1027 | that the thread knows that it is stopped (Linux peculiarity). */ | |
1028 | p_td_ta_thr_iter (main_threadagent, | |
1029 | wait_thread_callback, | |
1030 | (void *) 0, | |
1031 | TD_THR_ANY_STATE, | |
1032 | TD_THR_LOWEST_PRIORITY, | |
1033 | TD_SIGNO_MASK, | |
1034 | TD_THR_ANY_USER_FLAGS); | |
1035 | ||
1036 | break; | |
1037 | } | |
1038 | quit: | |
1039 | if (target_new_objfile_chain) | |
1040 | target_new_objfile_chain (objfile); | |
1041 | } | |
1042 | ||
1043 | ||
1044 | /* | |
1045 | ||
1046 | LOCAL FUNCTION | |
1047 | ||
1048 | thread_db_alive - test thread for "aliveness" | |
1049 | ||
1050 | SYNOPSIS | |
1051 | ||
1052 | static bool thread_db_alive (int pid); | |
1053 | ||
1054 | DESCRIPTION | |
1055 | ||
1056 | returns true if thread still active in inferior. | |
1057 | ||
1058 | */ | |
1059 | ||
1060 | static int | |
39f77062 | 1061 | thread_db_alive (ptid_t ptid) |
ed9a39eb | 1062 | { |
39f77062 | 1063 | if (is_thread (ptid)) /* user-space (non-kernel) thread */ |
ed9a39eb JM |
1064 | { |
1065 | td_thrhandle_t th; | |
1066 | td_err_e ret; | |
39f77062 | 1067 | int pid = GET_THREAD (ptid); |
ed9a39eb | 1068 | |
ed9a39eb JM |
1069 | if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK) |
1070 | return 0; /* thread not found */ | |
1071 | if ((ret = p_td_thr_validate (&th)) != TD_OK) | |
1072 | return 0; /* thread not valid */ | |
1073 | return 1; /* known thread: return true */ | |
1074 | } | |
1075 | else if (target_beneath->to_thread_alive) | |
39f77062 | 1076 | return target_beneath->to_thread_alive (ptid); |
ed9a39eb JM |
1077 | else |
1078 | return 0; /* default to "not alive" (shouldn't happen anyway) */ | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * get_lwp_from_thread_handle | |
1083 | */ | |
1084 | ||
1085 | static int /* lwpid_t or pid_t */ | |
fba45db2 | 1086 | get_lwp_from_thread_handle (td_thrhandle_t *th) |
ed9a39eb JM |
1087 | { |
1088 | td_thrinfo_t ti; | |
1089 | td_err_e ret; | |
1090 | ||
1091 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1092 | error ("get_lwp_from_thread_handle: thr_get_info failed: %s", | |
1093 | thr_err_string (ret)); | |
1094 | ||
1095 | return ti.ti_lid; | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * get_lwp_from_thread_id | |
1100 | */ | |
1101 | ||
1102 | static int /* lwpid_t or pid_t */ | |
064002de | 1103 | get_lwp_from_thread_id (int tid /* thread_t? */) |
ed9a39eb JM |
1104 | { |
1105 | td_thrhandle_t th; | |
1106 | td_err_e ret; | |
1107 | ||
1108 | if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK) | |
1109 | error ("get_lwp_from_thread_id: map_id2thr failed: %s", | |
1110 | thr_err_string (ret)); | |
1111 | ||
1112 | return get_lwp_from_thread_handle (&th); | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * pid_to_str has to handle user-space threads. | |
1117 | * If not a user-space thread, then pass the request on to the | |
1118 | * underlying stratum if it can handle it: else call normal_pid_to_str. | |
1119 | */ | |
1120 | ||
1121 | static char * | |
39f77062 | 1122 | thread_db_pid_to_str (ptid_t ptid) |
ed9a39eb JM |
1123 | { |
1124 | static char buf[100]; | |
1125 | td_thrhandle_t th; | |
1126 | td_thrinfo_t ti; | |
1127 | td_err_e ret; | |
1128 | ||
39f77062 | 1129 | if (is_thread (ptid)) |
ed9a39eb JM |
1130 | { |
1131 | if ((ret = p_td_ta_map_id2thr (main_threadagent, | |
39f77062 | 1132 | GET_THREAD (ptid), |
ed9a39eb JM |
1133 | &th)) != TD_OK) |
1134 | error ("thread_db: map_id2thr failed: %s", thr_err_string (ret)); | |
1135 | ||
1136 | if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK) | |
1137 | error ("thread_db: thr_get_info failed: %s", thr_err_string (ret)); | |
1138 | ||
1139 | if (ti.ti_state == TD_THR_ACTIVE && | |
1140 | ti.ti_lid != 0) | |
39f77062 | 1141 | sprintf (buf, "Thread %ld (LWP %d)", ti.ti_tid, ti.ti_lid); |
ed9a39eb | 1142 | else |
39f77062 | 1143 | sprintf (buf, "Thread %ld (%s)", ti.ti_tid, |
ed9a39eb JM |
1144 | thr_state_string (ti.ti_state)); |
1145 | } | |
39f77062 KB |
1146 | else if (GET_LWP (ptid)) |
1147 | sprintf (buf, "LWP %ld", GET_LWP (ptid)); | |
1148 | else return normal_pid_to_str (ptid); | |
ed9a39eb JM |
1149 | |
1150 | return buf; | |
1151 | } | |
1152 | ||
1153 | /* | |
1154 | * thread_db target vector functions: | |
1155 | */ | |
1156 | ||
1157 | static void | |
1158 | thread_db_files_info (struct target_ops *tgt_vector) | |
1159 | { | |
1160 | /* This function will be unnecessary in real life. */ | |
1161 | printf_filtered ("thread_db stratum:\n"); | |
1162 | target_beneath->to_files_info (tgt_vector); | |
1163 | } | |
1164 | ||
1165 | /* | |
39f77062 | 1166 | * xfer_memory has to munge the inferior_ptid before passing the call |
ed9a39eb JM |
1167 | * down to the target layer. |
1168 | */ | |
1169 | ||
1170 | static int | |
064002de | 1171 | thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite, |
043780a1 | 1172 | struct mem_attrib *attrib, |
064002de | 1173 | struct target_ops *target) |
ed9a39eb JM |
1174 | { |
1175 | struct cleanup *old_chain; | |
1176 | int ret; | |
1177 | ||
39f77062 | 1178 | old_chain = save_inferior_ptid (); |
ed9a39eb | 1179 | |
39f77062 KB |
1180 | if (is_thread (inferior_ptid) || |
1181 | !target_thread_alive (inferior_ptid)) | |
ed9a39eb JM |
1182 | { |
1183 | /* FIXME: use the LID/LWP, so that underlying process layer | |
1184 | can read memory from specific threads? */ | |
39f77062 | 1185 | inferior_ptid = pid_to_ptid (main_prochandle.pid); |
ed9a39eb JM |
1186 | } |
1187 | ||
1188 | ret = target_beneath->to_xfer_memory (memaddr, myaddr, len, | |
043780a1 | 1189 | dowrite, attrib, target); |
ed9a39eb JM |
1190 | do_cleanups (old_chain); |
1191 | return ret; | |
1192 | } | |
1193 | ||
1194 | /* | |
39f77062 | 1195 | * fetch_registers has to determine if inferior_ptid is a user-space thread. |
ed9a39eb JM |
1196 | * If so, we use the thread_db API to get the registers. |
1197 | * And if not, we call the underlying process stratum. | |
1198 | */ | |
1199 | ||
1200 | static void | |
fba45db2 | 1201 | thread_db_fetch_registers (int regno) |
ed9a39eb JM |
1202 | { |
1203 | td_thrhandle_t thandle; | |
d84dd0c5 | 1204 | gdb_prfpregset_t fpregset; |
ed9a39eb JM |
1205 | prgregset_t gregset; |
1206 | thread_t thread; | |
1207 | td_err_e ret; | |
1208 | ||
39f77062 | 1209 | if (!is_thread (inferior_ptid)) /* kernel thread */ |
ed9a39eb JM |
1210 | { /* pass the request on to the target underneath. */ |
1211 | target_beneath->to_fetch_registers (regno); | |
1212 | return; | |
1213 | } | |
1214 | ||
39f77062 | 1215 | /* convert inferior_ptid into a td_thrhandle_t */ |
ed9a39eb | 1216 | |
39f77062 | 1217 | if ((thread = GET_THREAD (inferior_ptid)) == 0) |
ed9a39eb JM |
1218 | error ("fetch_registers: thread == 0"); |
1219 | ||
1220 | if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK) | |
1221 | error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret)); | |
1222 | ||
1223 | /* Get the integer regs: | |
1224 | For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, | |
1225 | pc and sp are saved (by a thread context switch). */ | |
1226 | if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK && | |
1227 | ret != TD_PARTIALREG) | |
1228 | error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret)); | |
1229 | ||
1230 | /* And, now the fp regs */ | |
1231 | if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK && | |
1232 | ret != TD_NOFPREGS) | |
1233 | error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret)); | |
1234 | ||
1235 | /* Note that we must call supply_{g fp}regset *after* calling the td routines | |
1236 | because the td routines call ps_lget* which affect the values stored in the | |
1237 | registers array. */ | |
1238 | ||
39f77062 | 1239 | supply_gregset ((gdb_gregset_t *) gregset); |
ed9a39eb JM |
1240 | supply_fpregset (&fpregset); |
1241 | ||
1242 | } | |
1243 | ||
1244 | /* | |
39f77062 | 1245 | * store_registers has to determine if inferior_ptid is a user-space thread. |
ed9a39eb JM |
1246 | * If so, we use the thread_db API to get the registers. |
1247 | * And if not, we call the underlying process stratum. | |
1248 | */ | |
1249 | ||
1250 | static void | |
fba45db2 | 1251 | thread_db_store_registers (int regno) |
ed9a39eb JM |
1252 | { |
1253 | td_thrhandle_t thandle; | |
d84dd0c5 | 1254 | gdb_prfpregset_t fpregset; |
ed9a39eb JM |
1255 | prgregset_t gregset; |
1256 | thread_t thread; | |
1257 | td_err_e ret; | |
1258 | ||
39f77062 | 1259 | if (!is_thread (inferior_ptid)) /* Kernel thread: */ |
ed9a39eb JM |
1260 | { /* pass the request on to the underlying target vector. */ |
1261 | target_beneath->to_store_registers (regno); | |
1262 | return; | |
1263 | } | |
1264 | ||
39f77062 | 1265 | /* convert inferior_ptid into a td_thrhandle_t */ |
ed9a39eb | 1266 | |
39f77062 | 1267 | if ((thread = GET_THREAD (inferior_ptid)) == 0) |
ed9a39eb JM |
1268 | error ("store_registers: thread == 0"); |
1269 | ||
1270 | if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK) | |
1271 | error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret)); | |
1272 | ||
1273 | if (regno != -1) | |
1274 | { /* Not writing all the regs */ | |
1275 | /* save new register value */ | |
1276 | /* MVS: I don't understand this... */ | |
1277 | char old_value[REGISTER_SIZE]; | |
1278 | ||
1279 | memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE); | |
1280 | ||
1281 | if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK) | |
1282 | error ("store_registers: td_thr_getgregs %s", thr_err_string (ret)); | |
1283 | if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK) | |
1284 | error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret)); | |
1285 | ||
1286 | /* restore new register value */ | |
1287 | memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE); | |
1288 | ||
1289 | } | |
1290 | ||
39f77062 | 1291 | fill_gregset ((gdb_gregset_t *) gregset, regno); |
ed9a39eb JM |
1292 | fill_fpregset (&fpregset, regno); |
1293 | ||
1294 | if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK) | |
1295 | error ("store_registers: td_thr_setgregs %s", thr_err_string (ret)); | |
1296 | if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK && | |
1297 | ret != TD_NOFPREGS) | |
1298 | error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret)); | |
1299 | } | |
1300 | ||
1301 | static void | |
fba45db2 KB |
1302 | handle_new_thread (int tid, /* user thread id */ |
1303 | int lid, /* kernel thread id */ | |
1304 | int verbose) | |
ed9a39eb | 1305 | { |
39f77062 | 1306 | ptid_t gdb_ptid = BUILD_THREAD (tid, main_prochandle.pid); |
ed9a39eb JM |
1307 | int wait_pid, wait_status; |
1308 | ||
1309 | if (verbose) | |
39f77062 KB |
1310 | printf_filtered ("[New %s]\n", target_pid_to_str (gdb_ptid)); |
1311 | add_thread (gdb_ptid); | |
ed9a39eb JM |
1312 | |
1313 | if (lid != main_prochandle.pid) | |
1314 | { | |
1315 | attach_thread (lid); | |
1316 | /* According to the Eric Paire model, we now have to send | |
1317 | the restart signal to the new thread -- however, empirically, | |
1318 | I do not find that to be necessary. */ | |
1319 | attach_pid = lid; | |
1320 | } | |
1321 | } | |
1322 | ||
1323 | static void | |
fba45db2 | 1324 | test_for_new_thread (int tid, int lid, int verbose) |
ed9a39eb JM |
1325 | { |
1326 | if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid))) | |
1327 | handle_new_thread (tid, lid, verbose); | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * Callback function that gets called once per USER thread | |
1332 | * (i.e., not kernel) thread by td_ta_thr_iter. | |
1333 | */ | |
1334 | ||
1335 | static int | |
fba45db2 | 1336 | find_new_threads_callback (const td_thrhandle_t *th, void *ignored) |
ed9a39eb JM |
1337 | { |
1338 | td_thrinfo_t ti; | |
1339 | td_err_e ret; | |
1340 | ||
1341 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1342 | { | |
1343 | warning ("find_new_threads_callback: %s", thr_err_string (ret)); | |
1344 | return -1; /* bail out, get_info failed. */ | |
1345 | } | |
1346 | ||
1347 | /* FIXME: | |
1348 | As things now stand, this should never detect a new thread. | |
1349 | But if it does, we could be in trouble because we aren't calling | |
1350 | wait_thread_callback for it. */ | |
1351 | test_for_new_thread (ti.ti_tid, ti.ti_lid, 0); | |
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * find_new_threads uses the thread_db iterator function to discover | |
1357 | * user-space threads. Then if the underlying process stratum has a | |
1358 | * find_new_threads method, we call that too. | |
1359 | */ | |
1360 | ||
1361 | static void | |
fba45db2 | 1362 | thread_db_find_new_threads (void) |
ed9a39eb | 1363 | { |
39f77062 | 1364 | if (PIDGET (inferior_ptid) == -1) /* FIXME: still necessary? */ |
ed9a39eb JM |
1365 | { |
1366 | printf_filtered ("No process.\n"); | |
1367 | return; | |
1368 | } | |
1369 | p_td_ta_thr_iter (main_threadagent, | |
1370 | find_new_threads_callback, | |
1371 | (void *) 0, | |
1372 | TD_THR_ANY_STATE, | |
1373 | TD_THR_LOWEST_PRIORITY, | |
1374 | TD_SIGNO_MASK, | |
1375 | TD_THR_ANY_USER_FLAGS); | |
1376 | if (target_beneath->to_find_new_threads) | |
1377 | target_beneath->to_find_new_threads (); | |
1378 | } | |
1379 | ||
1380 | /* | |
1381 | * Resume all threads, or resume a single thread. | |
1382 | * If step is true, then single-step the appropriate thread | |
39f77062 | 1383 | * (or single-step inferior_ptid, but continue everyone else). |
ed9a39eb JM |
1384 | * If signo is true, then send that signal to at least one thread. |
1385 | */ | |
1386 | ||
1387 | /* | |
1388 | * This function is called once for each thread before resuming. | |
1389 | * It sends continue (no step, and no signal) to each thread except | |
1390 | * the main thread, and | |
1391 | * the event thread (the one that stopped at a breakpoint etc.) | |
1392 | * | |
1393 | * The event thread is handled separately so that it can be sent | |
1394 | * the stepping and signal args with which target_resume was called. | |
1395 | * | |
1396 | * The main thread is resumed last, so that the thread_db proc_service | |
1397 | * callbacks will still work during the iterator function. | |
1398 | */ | |
1399 | ||
1400 | static int | |
fba45db2 | 1401 | resume_thread_callback (const td_thrhandle_t *th, void *data) |
ed9a39eb JM |
1402 | { |
1403 | td_thrinfo_t ti; | |
1404 | td_err_e ret; | |
1405 | ||
1406 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1407 | { | |
1408 | warning ("resume_thread_callback: %s", thr_err_string (ret)); | |
1409 | return -1; /* bail out, get_info failed. */ | |
1410 | } | |
1411 | /* FIXME: | |
1412 | As things now stand, this should never detect a new thread. | |
1413 | But if it does, we could be in trouble because we aren't calling | |
1414 | wait_thread_callback for it. */ | |
1415 | test_for_new_thread (ti.ti_tid, ti.ti_lid, 1); | |
1416 | ||
1417 | if (ti.ti_lid != main_prochandle.pid && | |
1418 | ti.ti_lid != event_pid) | |
1419 | { | |
1420 | /* Unconditionally continue the thread with no signal. | |
1421 | Only the event thread will get a signal of any kind. */ | |
1422 | ||
39f77062 | 1423 | target_beneath->to_resume (pid_to_ptid (ti.ti_lid), 0, 0); |
ed9a39eb JM |
1424 | } |
1425 | return 0; | |
1426 | } | |
1427 | ||
1428 | static int | |
fba45db2 | 1429 | new_resume_thread_callback (threadinfo *thread, void *data) |
ed9a39eb JM |
1430 | { |
1431 | if (thread->lid != event_pid && | |
1432 | thread->lid != main_prochandle.pid) | |
1433 | { | |
1434 | /* Unconditionally continue the thread with no signal (for now). */ | |
1435 | ||
39f77062 | 1436 | target_beneath->to_resume (pid_to_ptid (thread->lid), 0, 0); |
ed9a39eb JM |
1437 | } |
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static int last_resume_pid; | |
1442 | static int last_resume_step; | |
1443 | static int last_resume_signo; | |
1444 | ||
1445 | static void | |
39f77062 | 1446 | thread_db_resume (ptid_t ptid, int step, enum target_signal signo) |
ed9a39eb | 1447 | { |
39f77062 | 1448 | last_resume_pid = PIDGET (ptid); |
ed9a39eb JM |
1449 | last_resume_step = step; |
1450 | last_resume_signo = signo; | |
1451 | ||
1452 | /* resuming a specific pid? */ | |
39f77062 | 1453 | if (PIDGET (ptid) != -1) |
ed9a39eb | 1454 | { |
39f77062 KB |
1455 | if (is_thread (ptid)) |
1456 | ptid = pid_to_ptid (get_lwp_from_thread_id (GET_THREAD (ptid))); | |
1457 | else if (GET_LWP (ptid)) | |
1458 | ptid = pid_to_ptid (GET_LWP (ptid)); | |
ed9a39eb JM |
1459 | } |
1460 | ||
1461 | /* Apparently the interpretation of 'pid' is dependent on 'step': | |
1462 | If step is true, then a specific pid means 'step only this pid'. | |
1463 | But if step is not true, then pid means 'continue ALL pids, but | |
1464 | give the signal only to this one'. */ | |
39f77062 | 1465 | if (PIDGET (ptid) != -1 && step) |
ed9a39eb JM |
1466 | { |
1467 | /* FIXME: is this gonna work in all circumstances? */ | |
39f77062 | 1468 | target_beneath->to_resume (ptid, step, signo); |
ed9a39eb JM |
1469 | } |
1470 | else | |
1471 | { | |
1472 | /* 1) Continue all threads except the event thread and the main thread. | |
1473 | 2) resume the event thread with step and signo. | |
1474 | 3) If event thread != main thread, continue the main thread. | |
1475 | ||
1476 | Note: order of 2 and 3 may need to be reversed. */ | |
1477 | ||
1478 | threadlist_iter (new_resume_thread_callback, | |
1479 | (void *) 0, | |
1480 | TD_THR_ANY_STATE, | |
1481 | TD_THR_ANY_TYPE); | |
1482 | /* now resume event thread, and if necessary also main thread. */ | |
1483 | if (event_pid) | |
1484 | { | |
39f77062 | 1485 | target_beneath->to_resume (pid_to_ptid (event_pid), step, signo); |
ed9a39eb JM |
1486 | } |
1487 | if (event_pid != main_prochandle.pid) | |
1488 | { | |
39f77062 | 1489 | target_beneath->to_resume (pid_to_ptid (main_prochandle.pid), 0, 0); |
ed9a39eb JM |
1490 | } |
1491 | } | |
1492 | } | |
1493 | ||
1494 | /* All new threads will be attached. | |
1495 | All previously known threads will be stopped using kill (SIGKILL). */ | |
1496 | ||
1497 | static int | |
1498 | stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data) | |
1499 | { | |
1500 | td_thrinfo_t ti; | |
1501 | td_err_e ret; | |
39f77062 | 1502 | ptid_t gdb_ptid; |
ed9a39eb JM |
1503 | int on_off = 1; |
1504 | ||
1505 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1506 | { | |
1507 | warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret)); | |
1508 | return -1; /* bail out, get_info failed. */ | |
1509 | } | |
1510 | ||
1511 | /* First add it to our internal list. | |
1512 | We build this list anew at every wait event. */ | |
1513 | insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type); | |
1514 | /* Now: if we've already seen it, stop it, else add it and attach it. */ | |
39f77062 KB |
1515 | gdb_ptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid); |
1516 | if (!in_thread_list (gdb_ptid)) /* new thread */ | |
ed9a39eb JM |
1517 | { |
1518 | handle_new_thread (ti.ti_tid, ti.ti_lid, 1); | |
1519 | /* Enable thread events */ | |
1520 | if (p_td_thr_event_enable) | |
1521 | if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK) | |
1522 | warning ("stop_or_attach_thread: %s", thr_err_string (ret)); | |
1523 | } | |
1524 | else if (ti.ti_lid != event_pid && | |
1525 | ti.ti_lid != main_prochandle.pid) | |
1526 | { | |
1527 | ret = (td_err_e) kill (ti.ti_lid, SIGSTOP); | |
1528 | } | |
1529 | ||
1530 | return 0; | |
1531 | } | |
1532 | ||
1533 | /* | |
1534 | * Wait for signal N from pid PID. | |
1535 | * If wait returns any other signals, put them back before returning. | |
1536 | */ | |
1537 | ||
1538 | static void | |
fba45db2 | 1539 | wait_for_stop (int pid) |
ed9a39eb JM |
1540 | { |
1541 | int i; | |
1542 | int retpid; | |
1543 | int status; | |
1544 | ||
1545 | /* Array of wait/signal status */ | |
1546 | /* FIXME: wrong data structure, we need a queue. | |
1547 | Realtime signals may be delivered more than once. | |
1548 | And at that, we really can't handle them (see below). */ | |
1549 | #if defined (NSIG) | |
1550 | static int wstatus [NSIG]; | |
1551 | #elif defined (_NSIG) | |
1552 | static int wstatus [_NSIG]; | |
1553 | #else | |
1554 | #error No definition for number of signals! | |
1555 | #endif | |
1556 | ||
1557 | /* clear wait/status list */ | |
1558 | memset (&wstatus, 0, sizeof (wstatus)); | |
1559 | ||
1560 | /* Now look for SIGSTOP event on all threads except event thread. */ | |
1561 | do { | |
1562 | errno = 0; | |
1563 | if (pid == main_prochandle.pid) | |
1564 | retpid = waitpid (pid, &status, 0); | |
1565 | else | |
1566 | retpid = waitpid (pid, &status, __WCLONE); | |
1567 | ||
1568 | if (retpid > 0) | |
1569 | if (WSTOPSIG (status) == SIGSTOP) | |
1570 | { | |
1571 | /* Got the SIGSTOP event we're looking for. | |
1572 | Throw it away, and throw any other events back! */ | |
1573 | for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++) | |
1574 | if (wstatus[i]) | |
1575 | if (i != SIGSTOP) | |
1576 | { | |
1577 | kill (retpid, i); | |
1578 | } | |
1579 | break; /* all done */ | |
1580 | } | |
1581 | else | |
1582 | { | |
1583 | int signo; | |
1584 | /* Oops, got an event other than SIGSTOP. | |
1585 | Save it, and throw it back after we find the SIGSTOP event. */ | |
1586 | ||
1587 | /* FIXME (how?) This method is going to fail for realtime | |
1588 | signals, which cannot be put back simply by using kill. */ | |
1589 | ||
1590 | if (WIFEXITED (status)) | |
1591 | error ("Ack! Thread Exited event. What do I do now???"); | |
1592 | else if (WIFSTOPPED (status)) | |
1593 | signo = WSTOPSIG (status); | |
1594 | else | |
1595 | signo = WTERMSIG (status); | |
1596 | ||
1597 | /* If a thread other than the event thread has hit a GDB | |
1598 | breakpoint (as opposed to some random trap signal), then | |
1599 | just arrange for it to hit it again later. Back up the | |
1600 | PC if necessary. Don't forward the SIGTRAP signal to | |
1601 | the thread. We will handle the current event, eventually | |
1602 | we will resume all the threads, and this one will get | |
1603 | it's breakpoint trap again. | |
1604 | ||
1605 | If we do not do this, then we run the risk that the user | |
1606 | will delete or disable the breakpoint, but the thread will | |
1607 | have already tripped on it. */ | |
1608 | ||
1609 | if (retpid != event_pid && | |
1610 | signo == SIGTRAP && | |
39f77062 | 1611 | breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (retpid)) - |
ed9a39eb JM |
1612 | DECR_PC_AFTER_BREAK)) |
1613 | { | |
1614 | /* Set the pc to before the trap and DO NOT re-send the signal */ | |
1615 | if (DECR_PC_AFTER_BREAK) | |
39f77062 KB |
1616 | write_pc_pid (read_pc_pid (pid_to_ptid (retpid)) |
1617 | - DECR_PC_AFTER_BREAK, | |
1618 | pid_to_ptid (retpid)); | |
ed9a39eb JM |
1619 | } |
1620 | ||
1621 | /* Since SIGINT gets forwarded to the entire process group | |
1622 | (in the case where ^C is typed at the tty / console), | |
1623 | just ignore all SIGINTs from other than the event thread. */ | |
1624 | else if (retpid != event_pid && signo == SIGINT) | |
1625 | { /* do nothing. Signal will disappear into oblivion! */ | |
1626 | ; | |
1627 | } | |
1628 | ||
1629 | else /* This is some random signal other than a breakpoint. */ | |
1630 | { | |
1631 | wstatus [signo] = 1; | |
1632 | } | |
39f77062 | 1633 | child_resume (pid_to_ptid (retpid), 0, TARGET_SIGNAL_0); |
ed9a39eb JM |
1634 | continue; |
1635 | } | |
1636 | ||
1637 | } while (errno == 0 || errno == EINTR); | |
1638 | } | |
1639 | ||
1640 | /* | |
1641 | * wait_thread_callback | |
1642 | * | |
1643 | * Calls waitpid for each thread, repeatedly if necessary, until | |
1644 | * SIGSTOP is returned. Afterward, if any other signals were returned | |
1645 | * by waitpid, return them to the thread's pending queue by calling kill. | |
1646 | */ | |
1647 | ||
1648 | static int | |
1649 | wait_thread_callback (const td_thrhandle_t *th, void *data) | |
1650 | { | |
1651 | td_thrinfo_t ti; | |
1652 | td_err_e ret; | |
1653 | ||
1654 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1655 | { | |
1656 | warning ("wait_thread_callback: %s", thr_err_string (ret)); | |
1657 | return -1; /* bail out, get_info failed. */ | |
1658 | } | |
1659 | ||
1660 | /* This callback to act on all threads except the event thread: */ | |
1661 | if (ti.ti_lid == event_pid || /* no need to wait (no sigstop) */ | |
1662 | ti.ti_lid == main_prochandle.pid) /* no need to wait (already waited) */ | |
1663 | return 0; /* don't wait on the event thread. */ | |
1664 | ||
1665 | wait_for_stop (ti.ti_lid); | |
1666 | return 0; /* finished: next thread. */ | |
1667 | } | |
1668 | ||
1669 | static int | |
fba45db2 | 1670 | new_wait_thread_callback (threadinfo *thread, void *data) |
ed9a39eb JM |
1671 | { |
1672 | /* don't wait on the event thread -- it's already stopped and waited. | |
1673 | Ditto the main thread. */ | |
1674 | if (thread->lid != event_pid && | |
1675 | thread->lid != main_prochandle.pid) | |
1676 | { | |
1677 | wait_for_stop (thread->lid); | |
1678 | } | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | /* | |
1683 | * Wait for any thread to stop, by calling the underlying wait method. | |
1684 | * The PID returned by the underlying target may be a kernel thread, | |
1685 | * in which case we will want to convert it to the corresponding | |
1686 | * user-space thread. | |
1687 | */ | |
1688 | ||
39f77062 KB |
1689 | static ptid_t |
1690 | thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
ed9a39eb JM |
1691 | { |
1692 | td_thrhandle_t thandle; | |
1693 | td_thrinfo_t ti; | |
1694 | td_err_e ret; | |
1695 | lwpid_t lwp; | |
1696 | int retpid; | |
39f77062 | 1697 | ptid_t retptid; |
ed9a39eb JM |
1698 | int status; |
1699 | int save_errno; | |
1700 | ||
1701 | /* OK, we're about to wait for an event from the running inferior. | |
1702 | Make sure we're ignoring the right signals. */ | |
1703 | ||
1704 | check_all_signal_numbers (); /* see if magic signals changed. */ | |
1705 | ||
1706 | event_pid = 0; | |
1707 | attach_pid = 0; | |
1708 | ||
1709 | /* FIXME: should I do the wait right here inline? */ | |
1710 | #if 0 | |
39f77062 | 1711 | if (PIDGET (ptid) == -1) |
ed9a39eb JM |
1712 | lwp = -1; |
1713 | else | |
39f77062 | 1714 | lwp = get_lwp_from_thread_id (GET_THREAD (ptid)); |
ed9a39eb JM |
1715 | #endif |
1716 | ||
1717 | ||
1718 | save_errno = linux_child_wait (-1, &retpid, &status); | |
1719 | store_waitstatus (ourstatus, status); | |
1720 | ||
1721 | /* Thread ID is irrelevant if the target process exited. | |
1722 | FIXME: do I have any killing to do? | |
1723 | Can I get this event mistakenly from a thread? */ | |
1724 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) | |
39f77062 | 1725 | return pid_to_ptid (retpid); |
ed9a39eb JM |
1726 | |
1727 | /* OK, we got an event of interest. | |
1728 | Go stop all threads and look for new ones. | |
1729 | FIXME: maybe don't do this for the restart signal? Optimization... */ | |
1730 | event_pid = retpid; | |
1731 | ||
1732 | /* If the last call to resume was for a specific thread, then we don't | |
1733 | need to stop everyone else: they should already be stopped. */ | |
1734 | if (last_resume_step == 0 || last_resume_pid == -1) | |
1735 | { | |
1736 | /* Main thread must be stopped before calling the iterator. */ | |
1737 | if (retpid != main_prochandle.pid) | |
1738 | { | |
1739 | kill (main_prochandle.pid, SIGSTOP); | |
1740 | wait_for_stop (main_prochandle.pid); | |
1741 | } | |
1742 | ||
1743 | empty_threadlist (); | |
1744 | /* Now stop everyone else, and attach any new threads you find. */ | |
1745 | p_td_ta_thr_iter (main_threadagent, | |
1746 | stop_or_attach_thread_callback, | |
1747 | (void *) 0, | |
1748 | TD_THR_ANY_STATE, | |
1749 | TD_THR_LOWEST_PRIORITY, | |
1750 | TD_SIGNO_MASK, | |
1751 | TD_THR_ANY_USER_FLAGS); | |
1752 | ||
1753 | /* Now go call wait on all the threads we've stopped: | |
1754 | This allows us to absorb the SIGKILL event, and to make sure | |
1755 | that the thread knows that it is stopped (Linux peculiarity). */ | |
1756 | ||
1757 | threadlist_iter (new_wait_thread_callback, | |
1758 | (void *) 0, | |
1759 | TD_THR_ANY_STATE, | |
1760 | TD_THR_ANY_TYPE); | |
1761 | } | |
1762 | ||
1763 | /* Convert the kernel thread id to the corresponding thread id. */ | |
1764 | ||
1765 | /* If the process layer does not furnish an lwp, | |
1766 | then perhaps the returned pid IS the lwp... */ | |
39f77062 KB |
1767 | #if 0 /* Always true (if it'd compile...) */ |
1768 | if ((lwp = GET_LWP (pid_to_ptid (retpid))) == 0) | |
1769 | #endif | |
ed9a39eb JM |
1770 | lwp = retpid; |
1771 | ||
1772 | if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK) | |
39f77062 | 1773 | return pid_to_ptid (retpid); /* LWP is not mapped onto a user-space thread. */ |
ed9a39eb JM |
1774 | |
1775 | if ((ret = p_td_thr_validate (&thandle)) != TD_OK) | |
39f77062 | 1776 | return pid_to_ptid (retpid); /* LWP is not mapped onto a valid thread. */ |
ed9a39eb JM |
1777 | |
1778 | if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK) | |
1779 | { | |
1780 | warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret)); | |
39f77062 | 1781 | return pid_to_ptid (retpid); |
ed9a39eb JM |
1782 | } |
1783 | ||
39f77062 | 1784 | retptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid); |
ed9a39eb | 1785 | /* If this is a new user thread, notify GDB about it. */ |
39f77062 | 1786 | if (!in_thread_list (retptid)) |
ed9a39eb | 1787 | { |
39f77062 KB |
1788 | printf_filtered ("[New %s]\n", target_pid_to_str (retptid)); |
1789 | add_thread (retptid); | |
ed9a39eb JM |
1790 | } |
1791 | ||
1792 | #if 0 | |
1793 | /* Now detect if this is a thread creation/deletion event: */ | |
1794 | check_for_thread_event (ourstatus, retpid); | |
1795 | #endif | |
39f77062 | 1796 | return retptid; |
ed9a39eb JM |
1797 | } |
1798 | ||
1799 | /* | |
1800 | * kill has to call the underlying kill. | |
39f77062 KB |
1801 | * FIXME: I'm not sure if it's necessary to check inferior_ptid any more, |
1802 | * but we might need to fix inferior_ptid up if it's a user thread. | |
ed9a39eb JM |
1803 | */ |
1804 | ||
1805 | static int | |
39f77062 | 1806 | kill_thread_callback (const td_thrhandle_t *th, void *data) |
ed9a39eb JM |
1807 | { |
1808 | td_thrinfo_t ti; | |
1809 | td_err_e ret; | |
1810 | ||
1811 | /* Fixme: | |
1812 | For Linux, threads may need to be waited. */ | |
1813 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1814 | { | |
1815 | warning ("kill_thread_callback: %s", thr_err_string (ret)); | |
1816 | return -1; /* bail out, get_info failed. */ | |
1817 | } | |
1818 | ||
1819 | if (ti.ti_lid != main_prochandle.pid) | |
1820 | { | |
1821 | kill (ti.ti_lid, SIGKILL); | |
1822 | } | |
1823 | return 0; | |
1824 | } | |
1825 | ||
1826 | ||
1827 | static void thread_db_kill (void) | |
1828 | { | |
1829 | int rpid; | |
1830 | int status; | |
1831 | ||
1832 | /* Fixme: | |
1833 | For Linux, threads may need to be waited. */ | |
39f77062 | 1834 | if (! ptid_equal (inferior_ptid, null_ptid)) |
ed9a39eb JM |
1835 | { |
1836 | /* Go kill the children first. Save the main thread for last. */ | |
1837 | p_td_ta_thr_iter (main_threadagent, | |
1838 | kill_thread_callback, | |
1839 | (void *) 0, | |
1840 | TD_THR_ANY_STATE, | |
1841 | TD_THR_LOWEST_PRIORITY, | |
1842 | TD_SIGNO_MASK, | |
1843 | TD_THR_ANY_USER_FLAGS); | |
1844 | ||
1845 | /* Turn off thread_db event-reporting API *before* killing the | |
1846 | main thread, since this operation requires child memory access. | |
1847 | Can't move this into thread_db_unpush target because then | |
1848 | detach would not work. */ | |
1849 | disable_thread_event_reporting (main_threadagent); | |
1850 | ||
39f77062 | 1851 | inferior_ptid = pid_to_ptid (main_prochandle.pid); |
ed9a39eb JM |
1852 | |
1853 | /* | |
1854 | * Since both procfs_kill and ptrace_kill call target_mourn, | |
1855 | * it should be sufficient for me to call one of them. | |
1856 | * That will result in my mourn being called, which will both | |
1857 | * unpush me and call the underlying mourn. | |
1858 | */ | |
1859 | target_beneath->to_kill (); | |
1860 | } | |
1861 | ||
1862 | /* Wait for all threads. */ | |
1863 | /* FIXME: need a universal wait_for_signal func? */ | |
1864 | do | |
1865 | { | |
1866 | rpid = waitpid (-1, &status, __WCLONE | WNOHANG); | |
1867 | } | |
1868 | while (rpid > 0 || errno == EINTR); | |
1869 | ||
1870 | do | |
1871 | { | |
1872 | rpid = waitpid (-1, &status, WNOHANG); | |
1873 | } | |
1874 | while (rpid > 0 || errno == EINTR); | |
1875 | } | |
1876 | ||
1877 | /* | |
1878 | * Mourn has to remove us from the target stack, | |
1879 | * and then call the underlying mourn. | |
1880 | */ | |
1881 | ||
1882 | static void thread_db_mourn_inferior (void) | |
1883 | { | |
1884 | thread_db_unpush_target (); | |
1885 | target_mourn_inferior (); /* call the underlying mourn */ | |
1886 | } | |
1887 | ||
1888 | /* | |
1889 | * Detach has to remove us from the target stack, | |
1890 | * and then call the underlying detach. | |
1891 | * | |
1892 | * But first, it has to detach all the cloned threads! | |
1893 | */ | |
1894 | ||
1895 | static int | |
39f77062 | 1896 | detach_thread_callback (const td_thrhandle_t *th, void *data) |
ed9a39eb JM |
1897 | { |
1898 | /* Called once per thread. */ | |
1899 | td_thrinfo_t ti; | |
1900 | td_err_e ret; | |
1901 | ||
1902 | if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK) | |
1903 | { | |
1904 | warning ("detach_thread_callback: %s", thr_err_string (ret)); | |
1905 | return -1; /* bail out, get_info failed. */ | |
1906 | } | |
1907 | ||
1908 | if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid))) | |
1909 | return 0; /* apparently we don't know this one. */ | |
1910 | ||
1911 | /* Save main thread for last, or the iterator will fail! */ | |
1912 | if (ti.ti_lid != main_prochandle.pid) | |
1913 | { | |
1914 | struct cleanup *old_chain; | |
1915 | int off = 0; | |
1916 | ||
1917 | /* Time to detach this thread. | |
1918 | First disable thread_db event reporting for the thread. */ | |
1919 | if (p_td_thr_event_enable && | |
1920 | (ret = p_td_thr_event_enable (th, off)) != TD_OK) | |
1921 | { | |
1922 | warning ("detach_thread_callback: %s\n", thr_err_string (ret)); | |
1923 | return 0; | |
1924 | } | |
1925 | ||
1926 | /* Now cancel any pending SIGTRAPS. FIXME! */ | |
1927 | ||
1928 | /* Call underlying detach method. FIXME just detach it. */ | |
39f77062 KB |
1929 | old_chain = save_inferior_ptid (); |
1930 | inferior_ptid = pid_to_ptid (ti.ti_lid); | |
ed9a39eb JM |
1931 | detach (TARGET_SIGNAL_0); |
1932 | do_cleanups (old_chain); | |
1933 | } | |
1934 | return 0; | |
1935 | } | |
1936 | ||
1937 | static void | |
1938 | thread_db_detach (char *args, int from_tty) | |
1939 | { | |
1940 | td_err_e ret; | |
1941 | ||
1942 | if ((ret = p_td_ta_thr_iter (main_threadagent, | |
1943 | detach_thread_callback, | |
1944 | (void *) 0, | |
1945 | TD_THR_ANY_STATE, | |
1946 | TD_THR_LOWEST_PRIORITY, | |
1947 | TD_SIGNO_MASK, | |
1948 | TD_THR_ANY_USER_FLAGS)) | |
1949 | != TD_OK) | |
1950 | warning ("detach (thr_iter): %s", thr_err_string (ret)); | |
1951 | ||
1952 | /* Turn off thread_db event-reporting API | |
1953 | (before detaching the main thread) */ | |
1954 | disable_thread_event_reporting (main_threadagent); | |
1955 | ||
1956 | thread_db_unpush_target (); | |
1957 | ||
1958 | /* above call nullifies target_beneath, so don't use that! */ | |
39f77062 | 1959 | inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid)); |
ed9a39eb JM |
1960 | target_detach (args, from_tty); |
1961 | } | |
1962 | ||
1963 | ||
1964 | /* | |
1965 | * We never want to actually create the inferior! | |
1966 | * | |
1967 | * If this is ever called, it means we were on the target stack | |
1968 | * when the user said "run". But we don't want to be on the new | |
1969 | * inferior's target stack until the thread_db / libthread | |
1970 | * connection is ready to be made. | |
1971 | * | |
1972 | * So, what shall we do? | |
1973 | * Unpush ourselves from the stack, and then invoke | |
1974 | * find_default_create_inferior, which will invoke the | |
1975 | * appropriate process_stratum target to do the create. | |
1976 | */ | |
1977 | ||
1978 | static void | |
fba45db2 | 1979 | thread_db_create_inferior (char *exec_file, char *allargs, char **env) |
ed9a39eb JM |
1980 | { |
1981 | thread_db_unpush_target (); | |
1982 | find_default_create_inferior (exec_file, allargs, env); | |
1983 | } | |
1984 | ||
1985 | /* | |
1986 | * Thread_db target vector initializer. | |
1987 | */ | |
1988 | ||
1989 | void | |
fba45db2 | 1990 | init_thread_db_ops (void) |
ed9a39eb JM |
1991 | { |
1992 | thread_db_ops.to_shortname = "multi-thread"; | |
1993 | thread_db_ops.to_longname = "multi-threaded child process."; | |
1994 | thread_db_ops.to_doc = "Threads and pthreads support."; | |
1995 | thread_db_ops.to_files_info = thread_db_files_info; | |
1996 | thread_db_ops.to_create_inferior = thread_db_create_inferior; | |
1997 | thread_db_ops.to_detach = thread_db_detach; | |
1998 | thread_db_ops.to_wait = thread_db_wait; | |
1999 | thread_db_ops.to_resume = thread_db_resume; | |
2000 | thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior; | |
2001 | thread_db_ops.to_kill = thread_db_kill; | |
2002 | thread_db_ops.to_xfer_memory = thread_db_xfer_memory; | |
2003 | thread_db_ops.to_fetch_registers = thread_db_fetch_registers; | |
2004 | thread_db_ops.to_store_registers = thread_db_store_registers; | |
2005 | thread_db_ops.to_thread_alive = thread_db_alive; | |
2006 | thread_db_ops.to_find_new_threads = thread_db_find_new_threads; | |
2007 | thread_db_ops.to_pid_to_str = thread_db_pid_to_str; | |
2008 | thread_db_ops.to_stratum = thread_stratum; | |
2009 | thread_db_ops.to_has_thread_control = tc_schedlock; | |
2010 | thread_db_ops.to_magic = OPS_MAGIC; | |
2011 | } | |
2012 | #endif /* HAVE_STDINT_H */ | |
2013 | ||
2014 | /* | |
2015 | * Module constructor / initializer function. | |
2016 | * If connection to thread_db dynamic library is successful, | |
2017 | * then initialize this module's target vectors and the | |
2018 | * new_objfile hook. | |
2019 | */ | |
2020 | ||
2021 | ||
2022 | void | |
fba45db2 | 2023 | _initialize_thread_db (void) |
ed9a39eb JM |
2024 | { |
2025 | #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */ | |
2026 | if (init_thread_db_library ()) | |
2027 | { | |
2028 | init_thread_db_ops (); | |
2029 | add_target (&thread_db_ops); | |
2030 | /* | |
2031 | * Hook up to the new_objfile event. | |
2032 | * If someone is already there, arrange for him to be called | |
2033 | * after we are. | |
2034 | */ | |
2035 | target_new_objfile_chain = target_new_objfile_hook; | |
2036 | target_new_objfile_hook = thread_db_new_objfile; | |
2037 | } | |
2038 | #endif /* HAVE_STDINT_H */ | |
2039 | } | |
2040 |