]>
Commit | Line | Data |
---|---|---|
1 | /* Low level interface for debugging UnixWare user-mode threads for | |
2 | GDB, the GNU debugger. | |
3 | ||
4 | Copyright 1999, 2000 Free Software Foundation, Inc. | |
5 | Written by Nick Duffek <[email protected]>. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | ||
25 | /* Like many systems, UnixWare implements two classes of threads: | |
26 | kernel-mode threads, which are scheduled by the kernel; and | |
27 | user-mode threads, which are scheduled by a library. UnixWare | |
28 | calls these two classes lightweight processes (LWPs) and threads, | |
29 | respectively. | |
30 | ||
31 | This module deals with user-mode threads. It calls procfs_ops | |
32 | functions to deal with LWPs and processes and core_ops functions to | |
33 | deal with core files. | |
34 | ||
35 | As of this writing, the user-mode thread debugging interface is not | |
36 | documented beyond the comments in <thread.h>. The following | |
37 | description has been gleaned from experience and from information | |
38 | provided by SCO. | |
39 | ||
40 | libthread.so, against which all UnixWare user-mode thread programs | |
41 | link, provides a global thread_debug structure named _thr_debug. | |
42 | It has three fields: | |
43 | ||
44 | (1) thr_map is a pointer to a pointer to an element of a | |
45 | thread_map ring. A thread_map contains a single thread's id | |
46 | number, state, LWP pointer, recent register state, and other | |
47 | useful information. | |
48 | ||
49 | (2) thr_brk is a pointer to a stub function that libthread.so | |
50 | calls when it changes a thread's state, e.g. by creating it, | |
51 | switching it to an LWP, or causing it to exit. | |
52 | ||
53 | (3) thr_debug_on controls whether libthread.so calls thr_brk(). | |
54 | ||
55 | Debuggers are able to track thread activity by setting a private | |
56 | breakpoint on thr_brk() and setting thr_debug_on to 1. | |
57 | ||
58 | thr_brk() receives two arguments: | |
59 | ||
60 | (1) a pointer to a thread_map describing the thread being | |
61 | changed; and | |
62 | ||
63 | (2) an enum thread_change specifying one of the following | |
64 | changes: | |
65 | ||
66 | invalid unknown | |
67 | thread_create thread has just been created | |
68 | thread_exit thread has just exited | |
69 | switch_begin thread will be switched to an LWP | |
70 | switch_complete thread has been switched to an LWP | |
71 | cancel_complete thread wasn't switched to an LWP | |
72 | thread_suspend thread has been thr_suspend()ed | |
73 | thread_suspend_pending thread will be thr_suspend()ed | |
74 | thread_continue thread has been thr_continue()d | |
75 | ||
76 | The thread_map argument to thr_brk() is NULL under the following | |
77 | circumstances: | |
78 | ||
79 | - The main thread is being acted upon. The main thread always | |
80 | has id 1, so its thread_map is easy to find by scanning through | |
81 | _thr_debug.thr_map. | |
82 | ||
83 | - A "switch_complete" change is occurring, which means that the | |
84 | thread specified in the most recent "switch_begin" change has | |
85 | moved to an LWP. | |
86 | ||
87 | - A "cancel_complete" change is occurring, which means that the | |
88 | thread specified in the most recent "switch_begin" change has | |
89 | not moved to an LWP after all. | |
90 | ||
91 | - A spurious "switch_begin" change is occurring after a | |
92 | "thread_exit" change. | |
93 | ||
94 | Between switch_begin and switch_complete or cancel_complete, the | |
95 | affected thread's LWP pointer is not reliable. It is possible that | |
96 | other parts of the thread's thread_map are also unreliable during | |
97 | that time. */ | |
98 | ||
99 | ||
100 | #include "defs.h" | |
101 | #include "gdbthread.h" | |
102 | #include "target.h" | |
103 | #include "inferior.h" | |
104 | #include <fcntl.h> | |
105 | ||
106 | /* | |
107 | * <thread.h> includes <sys/priocntl.h>, which requires boolean_t from | |
108 | * <sys/types.h>, which doesn't typedef boolean_t with gcc. | |
109 | */ | |
110 | #define boolean_t int | |
111 | #include <thread.h> | |
112 | #undef boolean_t | |
113 | ||
114 | #include <synch.h> /* for UnixWare 2.x */ | |
115 | ||
116 | ||
117 | /* | |
118 | * Whether to emit debugging output. | |
119 | */ | |
120 | #define DEBUG 0 | |
121 | ||
122 | /* | |
123 | * Default debugging output file, overridden by envvar UWTHR_DEBUG. | |
124 | */ | |
125 | #define DEBUG_FILE "/dev/tty" | |
126 | ||
127 | /* | |
128 | * #if DEBUG, write string S to the debugging output channel. | |
129 | */ | |
130 | #if !DEBUG | |
131 | # define DBG(fmt_and_args) | |
132 | # define DBG2(fmt_and_args) | |
133 | #else | |
134 | # define DBG(fmt_and_args) dbg fmt_and_args | |
135 | # define DBG2(fmt_and_args) | |
136 | #endif | |
137 | ||
138 | /* | |
139 | * Back end to CALL_BASE() and TRY_BASE(): evaluate CALL, then convert | |
140 | * inferior_pid to a composite thread/process id. | |
141 | */ | |
142 | #define CALL_BASE_1(call) \ | |
143 | do { \ | |
144 | DBG2(("CALL_BASE(" #call ")")); \ | |
145 | call; \ | |
146 | do_cleanups (infpid_cleanup); \ | |
147 | } while (0) | |
148 | ||
149 | /* | |
150 | * If inferior_pid can be converted to a composite lwp/process id, do so, | |
151 | * evaluate base_ops function CALL, and then convert inferior_pid back to a | |
152 | * composite thread/process id. | |
153 | * | |
154 | * Otherwise, issue an error message and return nonlocally. | |
155 | */ | |
156 | #define CALL_BASE(call) \ | |
157 | do { \ | |
158 | if (!lwp_infpid ()) \ | |
159 | error ("uw-thread: no lwp"); \ | |
160 | CALL_BASE_1 (call); \ | |
161 | } while (0) | |
162 | ||
163 | /* | |
164 | * Like CALL_BASE(), but instead of returning nonlocally on error, set | |
165 | * *CALLED to whether the inferior_pid conversion was successful. | |
166 | */ | |
167 | #define TRY_BASE(call, called) \ | |
168 | do { \ | |
169 | if ((*(called) = lwp_infpid ())) \ | |
170 | CALL_BASE_1 (call); \ | |
171 | } while (0) | |
172 | ||
173 | /* | |
174 | * Information passed by thread_iter() to its callback parameter. | |
175 | */ | |
176 | typedef struct { | |
177 | struct thread_map map; | |
178 | __lwp_desc_t lwp; | |
179 | CORE_ADDR mapp; | |
180 | } iter_t; | |
181 | ||
182 | /* | |
183 | * Private thread data for the thread_info struct. | |
184 | */ | |
185 | struct private_thread_info { | |
186 | int stable; /* 0 if libthread.so is modifying thread map */ | |
187 | int thrid; /* thread id assigned by libthread.so */ | |
188 | int lwpid; /* thread's lwp if .stable, 0 means no lwp */ | |
189 | CORE_ADDR mapp; /* address of thread's map structure */ | |
190 | }; | |
191 | ||
192 | ||
193 | /* procfs.c's target-specific operations. */ | |
194 | extern struct target_ops procfs_ops; | |
195 | ||
196 | /* Flag to prevent procfs.c from starting inferior processes. */ | |
197 | extern int procfs_suppress_run; | |
198 | ||
199 | /* This module's target-specific operations. */ | |
200 | static struct target_ops uw_thread_ops; | |
201 | ||
202 | /* Copy of the target over which uw_thread_ops is pushed. This is | |
203 | more convenient than a pointer to procfs_ops or core_ops, because | |
204 | they lack current_target's default callbacks. */ | |
205 | static struct target_ops base_ops; | |
206 | ||
207 | /* Saved pointer to previous owner of target_new_objfile_hook. */ | |
208 | static void (*target_new_objfile_chain)(struct objfile *); | |
209 | ||
210 | /* Whether we are debugging a user-space thread program. This isn't | |
211 | set until after libthread.so is loaded by the program being | |
212 | debugged. | |
213 | ||
214 | Except for module one-time intialization and where otherwise | |
215 | documented, no functions in this module get called when | |
216 | !uw_thread_active. */ | |
217 | static int uw_thread_active; | |
218 | ||
219 | /* For efficiency, cache the addresses of libthread.so's _thr_debug | |
220 | structure, its thr_brk stub function, and the main thread's map. */ | |
221 | static CORE_ADDR thr_debug_addr; | |
222 | static CORE_ADDR thr_brk_addr; | |
223 | static CORE_ADDR thr_map_main; | |
224 | ||
225 | /* Remember the thread most recently marked as switching. Necessary because | |
226 | libthread.so passes null map when calling stub with tc_*_complete. */ | |
227 | static struct thread_info *switchto_thread; | |
228 | ||
229 | /* Cleanup chain for safely restoring inferior_pid after CALL_BASE. */ | |
230 | static struct cleanup *infpid_cleanup; | |
231 | ||
232 | ||
233 | #if DEBUG | |
234 | /* | |
235 | * Helper function for DBG() macro: if printf-style FMT is non-null, format it | |
236 | * with args and display the result on the debugging output channel. | |
237 | */ | |
238 | static void | |
239 | dbg (char *fmt, ...) | |
240 | { | |
241 | static int fd = -1, len; | |
242 | va_list args; | |
243 | char buf[1024]; | |
244 | char *path; | |
245 | ||
246 | if (!fmt) | |
247 | return; | |
248 | ||
249 | if (fd < 0) | |
250 | { | |
251 | path = getenv ("UWTHR_DEBUG"); | |
252 | if (!path) | |
253 | path = DEBUG_FILE; | |
254 | if ((fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0) | |
255 | error ("can't open %s\n", path); | |
256 | } | |
257 | ||
258 | va_start (args, fmt); | |
259 | vsprintf (buf, fmt, args); | |
260 | va_end (args); | |
261 | ||
262 | len = strlen (buf); | |
263 | buf[len] = '\n'; | |
264 | (void)write (fd, buf, len + 1); | |
265 | } | |
266 | ||
267 | #if 0 | |
268 | /* | |
269 | * Return a string representing composite PID's components. | |
270 | */ | |
271 | static char * | |
272 | dbgpid (int pid) | |
273 | { | |
274 | static char *buf, buf1[80], buf2[80]; | |
275 | if (!buf || buf == buf2) | |
276 | buf = buf1; | |
277 | else | |
278 | buf = buf2; | |
279 | ||
280 | if (pid <= 0) | |
281 | sprintf (buf, "%d", pid); | |
282 | else | |
283 | sprintf (buf, "%s %d/%d", ISTID (pid) ? "thr" : "lwp", | |
284 | TIDGET (pid), PIDGET (pid)); | |
285 | ||
286 | return buf; | |
287 | } | |
288 | ||
289 | /* | |
290 | * Return a string representing thread state CHANGE. | |
291 | */ | |
292 | static char * | |
293 | dbgchange (enum thread_change change) | |
294 | { | |
295 | switch (change) { | |
296 | case tc_invalid: return "invalid"; | |
297 | case tc_thread_create: return "thread_create"; | |
298 | case tc_thread_exit: return "thread_exit"; | |
299 | case tc_switch_begin: return "switch_begin"; | |
300 | case tc_switch_complete: return "switch_complete"; | |
301 | case tc_cancel_complete: return "cancel_complete"; | |
302 | case tc_thread_suspend: return "thread_suspend"; | |
303 | case tc_thread_suspend_pending: return "thread_suspend_pending"; | |
304 | case tc_thread_continue: return "thread_continue"; | |
305 | default: return "unknown"; | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | * Return a string representing thread STATE. | |
311 | */ | |
312 | static char * | |
313 | dbgstate (int state) | |
314 | { | |
315 | switch (state) { | |
316 | case TS_ONPROC: return "running"; | |
317 | case TS_SLEEP: return "sleeping"; | |
318 | case TS_RUNNABLE: return "runnable"; | |
319 | case TS_ZOMBIE: return "zombie"; | |
320 | case TS_SUSPENDED: return "suspended"; | |
321 | #ifdef TS_FORK | |
322 | case TS_FORK: return "forking"; | |
323 | #endif | |
324 | default: return "confused"; | |
325 | } | |
326 | } | |
327 | #endif /* 0 */ | |
328 | #endif /* DEBUG */ | |
329 | ||
330 | ||
331 | /* | |
332 | * Read the contents of _thr_debug into *DEBUGP. Return success. | |
333 | */ | |
334 | static int | |
335 | read_thr_debug (struct thread_debug *debugp) | |
336 | { | |
337 | return base_ops.to_xfer_memory (thr_debug_addr, (char *)debugp, | |
338 | sizeof (*debugp), 0, &base_ops); | |
339 | } | |
340 | ||
341 | /* | |
342 | * Read into MAP the contents of the thread map at inferior process address | |
343 | * MAPP. Return success. | |
344 | */ | |
345 | static int | |
346 | read_map (CORE_ADDR mapp, struct thread_map *map) | |
347 | { | |
348 | return base_ops.to_xfer_memory ((CORE_ADDR)THR_MAP (mapp), (char *)map, | |
349 | sizeof (*map), 0, &base_ops); | |
350 | } | |
351 | ||
352 | /* | |
353 | * Read into LWP the contents of the lwp decriptor at inferior process address | |
354 | * LWPP. Return success. | |
355 | */ | |
356 | static int | |
357 | read_lwp (CORE_ADDR lwpp, __lwp_desc_t *lwp) | |
358 | { | |
359 | return base_ops.to_xfer_memory (lwpp, (char *)lwp, | |
360 | sizeof (*lwp), 0, &base_ops); | |
361 | } | |
362 | ||
363 | /* | |
364 | * Iterate through all user threads, applying FUNC(<map>, <lwp>, DATA) until | |
365 | * (a) FUNC returns nonzero, | |
366 | * (b) FUNC has been applied to all threads, or | |
367 | * (c) an error occurs, | |
368 | * where <map> is the thread's struct thread_map and <lwp> if non-null is the | |
369 | * thread's current __lwp_desc_t. | |
370 | * | |
371 | * If a call to FUNC returns nonzero, return that value; otherwise, return 0. | |
372 | */ | |
373 | static int | |
374 | thread_iter (int (*func)(iter_t *, void *), void *data) | |
375 | { | |
376 | struct thread_debug debug; | |
377 | CORE_ADDR first, mapp; | |
378 | iter_t iter; | |
379 | int ret; | |
380 | ||
381 | if (!read_thr_debug (&debug)) | |
382 | return 0; | |
383 | if (!base_ops.to_xfer_memory ((CORE_ADDR)debug.thr_map, (char *)&mapp, | |
384 | sizeof (mapp), 0, &base_ops)) | |
385 | return 0; | |
386 | if (!mapp) | |
387 | return 0; | |
388 | ||
389 | for (first = mapp;;) | |
390 | { | |
391 | if (!read_map (mapp, &iter.map)) | |
392 | return 0; | |
393 | ||
394 | if (iter.map.thr_lwpp) | |
395 | if (!read_lwp ((CORE_ADDR)iter.map.thr_lwpp, &iter.lwp)) | |
396 | return 0; | |
397 | ||
398 | iter.mapp = mapp; | |
399 | if ((ret = func (&iter, data))) | |
400 | return ret; | |
401 | ||
402 | mapp = (CORE_ADDR)iter.map.thr_next; | |
403 | if (mapp == first) | |
404 | return 0; | |
405 | } | |
406 | } | |
407 | ||
408 | /* | |
409 | * Deactivate user-mode thread support. | |
410 | */ | |
411 | static void | |
412 | deactivate_uw_thread (void) | |
413 | { | |
414 | uw_thread_active = 0; | |
415 | unpush_target (&uw_thread_ops); | |
416 | } | |
417 | ||
418 | /* | |
419 | * Return the composite lwp/process id corresponding to composite | |
420 | * id PID. If PID is a thread with no lwp, return 0. | |
421 | */ | |
422 | static int | |
423 | thr_to_lwp (int pid) | |
424 | { | |
425 | struct thread_info *info; | |
426 | int lid; | |
427 | ||
428 | if (!ISTID (pid)) | |
429 | lid = pid; | |
430 | else if (!(info = find_thread_pid (pid))) | |
431 | lid = 0; | |
432 | else if (!info->private->lwpid) | |
433 | lid = 0; | |
434 | else | |
435 | lid = MKLID (pid, info->private->lwpid); | |
436 | ||
437 | DBG2((" thr_to_lwp(%s) = %s", dbgpid (pid), dbgpid (lid))); | |
438 | return lid; | |
439 | } | |
440 | ||
441 | /* | |
442 | * find_thread_lwp() callback: return whether TP describes a thread | |
443 | * associated with lwp id DATA. | |
444 | */ | |
445 | static int | |
446 | find_thread_lwp_callback (struct thread_info *tp, void *data) | |
447 | { | |
448 | int lwpid = (int)data; | |
449 | ||
450 | if (!ISTID (tp->pid)) | |
451 | return 0; | |
452 | if (!tp->private->stable) | |
453 | return 0; | |
454 | if (lwpid != tp->private->lwpid) | |
455 | return 0; | |
456 | ||
457 | /* match */ | |
458 | return 1; | |
459 | } | |
460 | ||
461 | /* | |
462 | * If a thread is associated with lwp id LWPID, return the corresponding | |
463 | * member of the global thread list; otherwise, return null. | |
464 | */ | |
465 | static struct thread_info * | |
466 | find_thread_lwp (int lwpid) | |
467 | { | |
468 | return iterate_over_threads (find_thread_lwp_callback, (void *)lwpid); | |
469 | } | |
470 | ||
471 | /* | |
472 | * Return the composite thread/process id corresponding to composite | |
473 | * id PID. If PID is an lwp with no thread, return PID. | |
474 | */ | |
475 | static int | |
476 | lwp_to_thr (int pid) | |
477 | { | |
478 | struct thread_info *info; | |
479 | int tid = pid, lwpid; | |
480 | ||
481 | if (ISTID (pid)) | |
482 | goto done; | |
483 | if (!(lwpid = LIDGET (pid))) | |
484 | goto done; | |
485 | if (!(info = find_thread_lwp (lwpid))) | |
486 | goto done; | |
487 | tid = MKTID (pid, info->private->thrid); | |
488 | ||
489 | done: | |
490 | DBG2((ISTID (tid) ? NULL : "lwp_to_thr: no thr for %s", dbgpid (pid))); | |
491 | return tid; | |
492 | } | |
493 | ||
494 | /* | |
495 | * do_cleanups() callback: convert inferior_pid to a composite | |
496 | * thread/process id after having made a procfs call. | |
497 | */ | |
498 | static void | |
499 | thr_infpid (void *unused) | |
500 | { | |
501 | int pid = lwp_to_thr (inferior_pid); | |
502 | DBG2((" inferior_pid from procfs: %s => %s", | |
503 | dbgpid (inferior_pid), dbgpid (pid))); | |
504 | inferior_pid = pid; | |
505 | } | |
506 | ||
507 | /* | |
508 | * If possible, convert inferior_pid to a composite lwp/process id in | |
509 | * preparation for making a procfs call. Return success. | |
510 | */ | |
511 | static int | |
512 | lwp_infpid (void) | |
513 | { | |
514 | int pid = thr_to_lwp (inferior_pid); | |
515 | DBG2((" inferior_pid to procfs: %s => %s", | |
516 | dbgpid (inferior_pid), dbgpid (pid))); | |
517 | ||
518 | if (!pid) | |
519 | return 0; | |
520 | ||
521 | inferior_pid = pid; | |
522 | infpid_cleanup = make_cleanup (thr_infpid, NULL); | |
523 | return 1; | |
524 | } | |
525 | ||
526 | /* | |
527 | * Add to the global thread list a new user-mode thread with system id THRID, | |
528 | * lwp id LWPID, map address MAPP, and composite thread/process PID. | |
529 | */ | |
530 | static void | |
531 | add_thread_uw (int thrid, int lwpid, CORE_ADDR mapp, int pid) | |
532 | { | |
533 | struct thread_info *newthread; | |
534 | ||
535 | if ((newthread = add_thread (pid)) == NULL) | |
536 | error ("failed to create new thread structure"); | |
537 | ||
538 | newthread->private = xmalloc (sizeof (struct private_thread_info)); | |
539 | newthread->private->stable = 1; | |
540 | newthread->private->thrid = thrid; | |
541 | newthread->private->lwpid = lwpid; | |
542 | newthread->private->mapp = mapp; | |
543 | ||
544 | if (target_has_execution) | |
545 | printf_unfiltered ("[New %s]\n", target_pid_to_str (pid)); | |
546 | } | |
547 | ||
548 | /* | |
549 | * notice_threads() and find_main() callback: if the thread list doesn't | |
550 | * already contain the thread described by ITER, add it if it's the main | |
551 | * thread or if !DATA. | |
552 | */ | |
553 | static int | |
554 | notice_thread (iter_t *iter, void *data) | |
555 | { | |
556 | int thrid = iter->map.thr_tid; | |
557 | int lwpid = !iter->map.thr_lwpp ? 0 : iter->lwp.lwp_id; | |
558 | int pid = MKTID (inferior_pid, thrid); | |
559 | ||
560 | if (!find_thread_pid (pid) && (!data || thrid == 1)) | |
561 | add_thread_uw (thrid, lwpid, iter->mapp, pid); | |
562 | ||
563 | return 0; | |
564 | } | |
565 | ||
566 | /* | |
567 | * Add to the thread list any threads it doesn't already contain. | |
568 | */ | |
569 | static void | |
570 | notice_threads (void) | |
571 | { | |
572 | thread_iter (notice_thread, NULL); | |
573 | } | |
574 | ||
575 | /* | |
576 | * Return the address of the main thread's map. On error, return 0. | |
577 | */ | |
578 | static CORE_ADDR | |
579 | find_main (void) | |
580 | { | |
581 | if (!thr_map_main) | |
582 | { | |
583 | struct thread_info *info; | |
584 | thread_iter (notice_thread, (void *)1); | |
585 | if ((info = find_thread_pid (MKTID (inferior_pid, 1)))) | |
586 | thr_map_main = info->private->mapp; | |
587 | } | |
588 | return thr_map_main; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Attach to process specified by ARGS, then initialize for debugging it | |
593 | * and wait for the trace-trap that results from attaching. | |
594 | * | |
595 | * This function only gets called with uw_thread_active == 0. | |
596 | */ | |
597 | static void | |
598 | uw_thread_attach (char *args, int from_tty) | |
599 | { | |
600 | procfs_ops.to_attach (args, from_tty); | |
601 | if (uw_thread_active) | |
602 | thr_infpid (NULL); | |
603 | } | |
604 | ||
605 | /* | |
606 | * Detach from the process attached to by uw_thread_attach(). | |
607 | */ | |
608 | static void | |
609 | uw_thread_detach (char *args, int from_tty) | |
610 | { | |
611 | deactivate_uw_thread (); | |
612 | base_ops.to_detach (args, from_tty); | |
613 | } | |
614 | ||
615 | /* | |
616 | * Tell the inferior process to continue running thread PID if >= 0 | |
617 | * and all threads otherwise. | |
618 | */ | |
619 | static void | |
620 | uw_thread_resume (int pid, int step, enum target_signal signo) | |
621 | { | |
622 | if (pid > 0 && !(pid = thr_to_lwp (pid))) | |
623 | pid = -1; | |
624 | ||
625 | CALL_BASE (base_ops.to_resume (pid, step, signo)); | |
626 | } | |
627 | ||
628 | /* | |
629 | * If the trap we just received from lwp PID was due to a breakpoint | |
630 | * on the libthread.so debugging stub, update this module's state | |
631 | * accordingly. | |
632 | */ | |
633 | static void | |
634 | libthread_stub (int pid) | |
635 | { | |
636 | CORE_ADDR sp, mapp, mapp_main; | |
637 | enum thread_change change; | |
638 | struct thread_map map; | |
639 | __lwp_desc_t lwp; | |
640 | int tid = 0, lwpid; | |
641 | struct thread_info *info; | |
642 | ||
643 | /* Check for stub breakpoint. */ | |
644 | if (read_pc_pid (pid) - DECR_PC_AFTER_BREAK != thr_brk_addr) | |
645 | return; | |
646 | ||
647 | /* Retrieve stub args. */ | |
648 | sp = read_register_pid (SP_REGNUM, pid); | |
649 | if (!base_ops.to_xfer_memory (sp + SP_ARG0, (char *)&mapp, | |
650 | sizeof (mapp), 0, &base_ops)) | |
651 | goto err; | |
652 | if (!base_ops.to_xfer_memory (sp + SP_ARG0 + sizeof (mapp), (char *)&change, | |
653 | sizeof (change), 0, &base_ops)) | |
654 | goto err; | |
655 | ||
656 | /* create_inferior() may not have finished yet, so notice the main | |
657 | thread to ensure that it's displayed first by add_thread(). */ | |
658 | mapp_main = find_main (); | |
659 | ||
660 | /* Notice thread creation, deletion, or stability change. */ | |
661 | switch (change) { | |
662 | case tc_switch_begin: | |
663 | if (!mapp) /* usually means main thread */ | |
664 | mapp = mapp_main; | |
665 | /* fall through */ | |
666 | ||
667 | case tc_thread_create: | |
668 | case tc_thread_exit: | |
669 | if (!mapp) | |
670 | break; | |
671 | if (!read_map (mapp, &map)) | |
672 | goto err; | |
673 | tid = MKTID (pid, map.thr_tid); | |
674 | ||
675 | switch (change) { | |
676 | case tc_thread_create: /* new thread */ | |
677 | if (!map.thr_lwpp) | |
678 | lwpid = 0; | |
679 | else if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp)) | |
680 | goto err; | |
681 | else | |
682 | lwpid = lwp.lwp_id; | |
683 | add_thread_uw (map.thr_tid, lwpid, mapp, tid); | |
684 | break; | |
685 | ||
686 | case tc_thread_exit: /* thread has exited */ | |
687 | printf_unfiltered ("[Exited %s]\n", target_pid_to_str (tid)); | |
688 | delete_thread (tid); | |
689 | if (tid == inferior_pid) | |
690 | inferior_pid = pid; | |
691 | break; | |
692 | ||
693 | case tc_switch_begin: /* lwp is switching threads */ | |
694 | if (switchto_thread) | |
695 | goto err; | |
696 | if (!(switchto_thread = find_thread_pid (tid))) | |
697 | goto err; | |
698 | switchto_thread->private->stable = 0; | |
699 | break; | |
700 | ||
701 | default: | |
702 | break; | |
703 | } | |
704 | break; | |
705 | ||
706 | case tc_switch_complete: /* lwp has switched threads */ | |
707 | case tc_cancel_complete: /* lwp didn't switch threads */ | |
708 | if (!switchto_thread) | |
709 | goto err; | |
710 | ||
711 | if (change == tc_switch_complete) | |
712 | { | |
713 | /* | |
714 | * If switchto_thread is the main thread, then (a) the corresponding | |
715 | * tc_switch_begin probably received a null map argument and therefore | |
716 | * (b) it may have been a spurious switch following a tc_thread_exit. | |
717 | * | |
718 | * Therefore, explicitly query the thread's lwp before caching it in | |
719 | * its thread list entry. | |
720 | */ | |
721 | if (!read_map (switchto_thread->private->mapp, &map)) | |
722 | goto err; | |
723 | if (map.thr_lwpp) | |
724 | { | |
725 | if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp)) | |
726 | goto err; | |
727 | if ((info = find_thread_lwp (lwp.lwp_id))) | |
728 | info->private->lwpid = 0; | |
729 | switchto_thread->private->lwpid = lwp.lwp_id; | |
730 | } | |
731 | } | |
732 | ||
733 | switchto_thread->private->stable = 1; | |
734 | switchto_thread = NULL; | |
735 | break; | |
736 | ||
737 | case tc_invalid: | |
738 | case tc_thread_suspend: | |
739 | case tc_thread_suspend_pending: | |
740 | case tc_thread_continue: | |
741 | err: | |
742 | DBG(("unexpected condition in libthread_stub()")); | |
743 | break; | |
744 | } | |
745 | ||
746 | DBG2(("libthread_stub(%s): %s %s %s", dbgpid (pid), dbgpid (tid), | |
747 | dbgchange (change), tid ? dbgstate (map.thr_state) : "")); | |
748 | } | |
749 | ||
750 | /* | |
751 | * Wait for thread/lwp/process ID if >= 0 or for any thread otherwise. | |
752 | */ | |
753 | static int | |
754 | uw_thread_wait (int pid, struct target_waitstatus *status) | |
755 | { | |
756 | if (pid > 0) | |
757 | pid = thr_to_lwp (pid); | |
758 | CALL_BASE (pid = base_ops.to_wait (pid > 0 ? pid : -1, status)); | |
759 | ||
760 | if (status->kind == TARGET_WAITKIND_STOPPED && | |
761 | status->value.sig == TARGET_SIGNAL_TRAP) | |
762 | libthread_stub (pid); | |
763 | ||
764 | return lwp_to_thr (pid); | |
765 | } | |
766 | ||
767 | /* | |
768 | * Tell gdb about the registers in the thread/lwp/process specified by | |
769 | * inferior_pid. | |
770 | */ | |
771 | static void | |
772 | uw_thread_fetch_registers (int regno) | |
773 | { | |
774 | int called; | |
775 | struct thread_info *info; | |
776 | struct thread_map map; | |
777 | ||
778 | TRY_BASE (base_ops.to_fetch_registers (regno), &called); | |
779 | if (called) | |
780 | return; | |
781 | ||
782 | if (!(info = find_thread_pid (inferior_pid))) | |
783 | return; | |
784 | if (!read_map (info->private->mapp, &map)) | |
785 | return; | |
786 | ||
787 | supply_gregset (&map.thr_ucontext.uc_mcontext.gregs); | |
788 | supply_fpregset (&map.thr_ucontext.uc_mcontext.fpregs); | |
789 | } | |
790 | ||
791 | /* | |
792 | * Store gdb's current view of the register set into the thread/lwp/process | |
793 | * specified by inferior_pid. | |
794 | */ | |
795 | static void | |
796 | uw_thread_store_registers (int regno) | |
797 | { | |
798 | CALL_BASE (base_ops.to_store_registers (regno)); | |
799 | } | |
800 | ||
801 | /* | |
802 | * Prepare to modify the registers array. | |
803 | */ | |
804 | static void | |
805 | uw_thread_prepare_to_store (void) | |
806 | { | |
807 | CALL_BASE (base_ops.to_prepare_to_store ()); | |
808 | } | |
809 | ||
810 | /* | |
811 | * Fork an inferior process and start debugging it. | |
812 | * | |
813 | * This function only gets called with uw_thread_active == 0. | |
814 | */ | |
815 | static void | |
816 | uw_thread_create_inferior (char *exec_file, char *allargs, char **env) | |
817 | { | |
818 | if (uw_thread_active) | |
819 | deactivate_uw_thread (); | |
820 | ||
821 | procfs_ops.to_create_inferior (exec_file, allargs, env); | |
822 | if (uw_thread_active) | |
823 | { | |
824 | find_main (); | |
825 | thr_infpid (NULL); | |
826 | } | |
827 | } | |
828 | ||
829 | /* | |
830 | * Kill and forget about the inferior process. | |
831 | */ | |
832 | static void | |
833 | uw_thread_kill (void) | |
834 | { | |
835 | base_ops.to_kill (); | |
836 | } | |
837 | ||
838 | /* | |
839 | * Clean up after the inferior exits. | |
840 | */ | |
841 | static void | |
842 | uw_thread_mourn_inferior (void) | |
843 | { | |
844 | remove_thread_event_breakpoints (); | |
845 | deactivate_uw_thread (); | |
846 | base_ops.to_mourn_inferior (); | |
847 | } | |
848 | ||
849 | /* | |
850 | * Return whether this module can attach to and run processes. | |
851 | * | |
852 | * This function only gets called with uw_thread_active == 0. | |
853 | */ | |
854 | static int | |
855 | uw_thread_can_run (void) | |
856 | { | |
857 | return procfs_suppress_run; | |
858 | } | |
859 | ||
860 | /* | |
861 | * Return whether thread PID is still valid. | |
862 | */ | |
863 | static int | |
864 | uw_thread_alive (int pid) | |
865 | { | |
866 | if (!ISTID (pid)) | |
867 | return base_ops.to_thread_alive (pid); | |
868 | ||
869 | /* If it's in the thread list, it's valid, because otherwise | |
870 | libthread_stub() would have deleted it. */ | |
871 | return in_thread_list (pid); | |
872 | } | |
873 | ||
874 | /* | |
875 | * Add to the thread list any threads and lwps it doesn't already contain. | |
876 | */ | |
877 | static void | |
878 | uw_thread_find_new_threads (void) | |
879 | { | |
880 | CALL_BASE (if (base_ops.to_find_new_threads) | |
881 | base_ops.to_find_new_threads ()); | |
882 | notice_threads (); | |
883 | } | |
884 | ||
885 | /* | |
886 | * Return a string for pretty-printing PID in "info threads" output. | |
887 | * This may be called by either procfs.c or by generic gdb. | |
888 | */ | |
889 | static char * | |
890 | uw_thread_pid_to_str (int pid) | |
891 | { | |
892 | #define FMT "Thread %d" | |
893 | static char buf[sizeof (FMT) + 3 * sizeof (pid)]; | |
894 | ||
895 | if (!ISTID (pid)) | |
896 | /* core_ops says "process foo", so call procfs_ops explicitly. */ | |
897 | return procfs_ops.to_pid_to_str (pid); | |
898 | ||
899 | sprintf (buf, FMT, TIDGET (pid)); | |
900 | #undef FMT | |
901 | return buf; | |
902 | } | |
903 | ||
904 | /* | |
905 | * Return a string displaying INFO state information in "info threads" | |
906 | * output. | |
907 | */ | |
908 | static char * | |
909 | uw_extra_thread_info (struct thread_info *info) | |
910 | { | |
911 | static char buf[80]; | |
912 | struct thread_map map; | |
913 | __lwp_desc_t lwp; | |
914 | int lwpid; | |
915 | char *name; | |
916 | ||
917 | if (!ISTID (info->pid)) | |
918 | return NULL; | |
919 | ||
920 | if (!info->private->stable) | |
921 | return "switching"; | |
922 | ||
923 | if (!read_map (info->private->mapp, &map)) | |
924 | return NULL; | |
925 | ||
926 | if (!map.thr_lwpp || !read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp)) | |
927 | lwpid = 0; | |
928 | else | |
929 | lwpid = lwp.lwp_id; | |
930 | ||
931 | switch (map.thr_state) { | |
932 | case TS_ONPROC: name = "running"; break; | |
933 | case TS_SLEEP: name = "sleeping"; break; | |
934 | case TS_RUNNABLE: name = "runnable"; break; | |
935 | case TS_ZOMBIE: name = "zombie"; break; | |
936 | case TS_SUSPENDED: name = "suspended"; break; | |
937 | #ifdef TS_FORK | |
938 | case TS_FORK: name = "forking"; break; | |
939 | #endif | |
940 | default: name = "confused"; break; | |
941 | } | |
942 | ||
943 | if (!lwpid) | |
944 | return name; | |
945 | ||
946 | sprintf (buf, "%s, LWP %d", name, lwpid); | |
947 | return buf; | |
948 | } | |
949 | ||
950 | /* | |
951 | * Check whether libthread.so has just been loaded, and if so, try to | |
952 | * initialize user-space thread debugging support. | |
953 | * | |
954 | * libthread.so loading happens while (a) an inferior process is being | |
955 | * started by procfs and (b) a core image is being loaded. | |
956 | * | |
957 | * This function often gets called with uw_thread_active == 0. | |
958 | */ | |
959 | static void | |
960 | libthread_init (void) | |
961 | { | |
962 | struct minimal_symbol *ms; | |
963 | struct thread_debug debug; | |
964 | CORE_ADDR onp; | |
965 | struct breakpoint *b; | |
966 | int one = 1; | |
967 | ||
968 | /* Don't initialize twice. */ | |
969 | if (uw_thread_active) | |
970 | return; | |
971 | ||
972 | /* Check whether libthread.so has been loaded. */ | |
973 | if (!(ms = lookup_minimal_symbol ("_thr_debug", NULL, NULL))) | |
974 | return; | |
975 | ||
976 | /* Cache _thr_debug's address. */ | |
977 | if (!(thr_debug_addr = SYMBOL_VALUE_ADDRESS (ms))) | |
978 | return; | |
979 | ||
980 | /* Initialize base_ops.to_xfer_memory(). */ | |
981 | base_ops = current_target; | |
982 | ||
983 | /* Load _thr_debug's current contents. */ | |
984 | if (!read_thr_debug (&debug)) | |
985 | return; | |
986 | ||
987 | /* User code (e.g. my test programs) may dereference _thr_debug, | |
988 | making it availble to GDB before shared libs are loaded. */ | |
989 | if (!debug.thr_map) | |
990 | return; | |
991 | ||
992 | /* libthread.so has been loaded, and the current_target should now | |
993 | reflect core_ops or procfs_ops. */ | |
994 | push_target (&uw_thread_ops); /* must precede notice_threads() */ | |
995 | uw_thread_active = 1; | |
996 | ||
997 | if (!target_has_execution) | |
998 | ||
999 | /* Locate threads in core file. */ | |
1000 | notice_threads (); | |
1001 | ||
1002 | else | |
1003 | { | |
1004 | /* Set a breakpoint on the stub function provided by libthread.so. */ | |
1005 | thr_brk_addr = (CORE_ADDR)debug.thr_brk; | |
1006 | if (!(b = create_thread_event_breakpoint (thr_brk_addr))) | |
1007 | goto err; | |
1008 | ||
1009 | /* Activate the stub function. */ | |
1010 | onp = (CORE_ADDR)&((struct thread_debug *)thr_debug_addr)->thr_debug_on; | |
1011 | if (!base_ops.to_xfer_memory ((CORE_ADDR)onp, (char *)&one, | |
1012 | sizeof (one), 1, &base_ops)) | |
1013 | { | |
1014 | delete_breakpoint (b); | |
1015 | goto err; | |
1016 | } | |
1017 | ||
1018 | /* Prepare for finding the main thread, which doesn't yet exist. */ | |
1019 | thr_map_main = 0; | |
1020 | } | |
1021 | ||
1022 | return; | |
1023 | ||
1024 | err: | |
1025 | warning ("uw-thread: unable to initialize user-mode thread debugging\n"); | |
1026 | deactivate_uw_thread (); | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * target_new_objfile_hook callback. | |
1031 | * | |
1032 | * If OBJFILE is non-null, check whether libthread.so was just loaded, | |
1033 | * and if so, prepare for user-mode thread debugging. | |
1034 | * | |
1035 | * If OBJFILE is null, libthread.so has gone away, so stop debugging | |
1036 | * user-mode threads. | |
1037 | * | |
1038 | * This function often gets called with uw_thread_active == 0. | |
1039 | */ | |
1040 | static void | |
1041 | uw_thread_new_objfile (struct objfile *objfile) | |
1042 | { | |
1043 | if (objfile) | |
1044 | libthread_init (); | |
1045 | ||
1046 | else if (uw_thread_active) | |
1047 | deactivate_uw_thread (); | |
1048 | ||
1049 | if (target_new_objfile_chain) | |
1050 | target_new_objfile_chain (objfile); | |
1051 | } | |
1052 | ||
1053 | /* | |
1054 | * Initialize uw_thread_ops. | |
1055 | */ | |
1056 | static void | |
1057 | init_uw_thread_ops (void) | |
1058 | { | |
1059 | uw_thread_ops.to_shortname = "unixware-threads"; | |
1060 | uw_thread_ops.to_longname = "UnixWare threads and pthread."; | |
1061 | uw_thread_ops.to_doc = "UnixWare threads and pthread support."; | |
1062 | uw_thread_ops.to_attach = uw_thread_attach; | |
1063 | uw_thread_ops.to_detach = uw_thread_detach; | |
1064 | uw_thread_ops.to_resume = uw_thread_resume; | |
1065 | uw_thread_ops.to_wait = uw_thread_wait; | |
1066 | uw_thread_ops.to_fetch_registers = uw_thread_fetch_registers; | |
1067 | uw_thread_ops.to_store_registers = uw_thread_store_registers; | |
1068 | uw_thread_ops.to_prepare_to_store = uw_thread_prepare_to_store; | |
1069 | uw_thread_ops.to_create_inferior = uw_thread_create_inferior; | |
1070 | uw_thread_ops.to_kill = uw_thread_kill; | |
1071 | uw_thread_ops.to_mourn_inferior = uw_thread_mourn_inferior; | |
1072 | uw_thread_ops.to_can_run = uw_thread_can_run; | |
1073 | uw_thread_ops.to_thread_alive = uw_thread_alive; | |
1074 | uw_thread_ops.to_find_new_threads = uw_thread_find_new_threads; | |
1075 | uw_thread_ops.to_pid_to_str = uw_thread_pid_to_str; | |
1076 | uw_thread_ops.to_extra_thread_info = uw_extra_thread_info; | |
1077 | uw_thread_ops.to_stratum = thread_stratum; | |
1078 | uw_thread_ops.to_magic = OPS_MAGIC; | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * Module startup initialization function, automagically called by | |
1083 | * init.c. | |
1084 | */ | |
1085 | void | |
1086 | _initialize_uw_thread (void) | |
1087 | { | |
1088 | init_uw_thread_ops (); | |
1089 | add_target (&uw_thread_ops); | |
1090 | ||
1091 | procfs_suppress_run = 1; | |
1092 | ||
1093 | /* Notice when libthread.so gets loaded. */ | |
1094 | target_new_objfile_chain = target_new_objfile_hook; | |
1095 | target_new_objfile_hook = uw_thread_new_objfile; | |
1096 | } |