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