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