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