Change embedded documentation to use consistent indentation and to split up
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
1 /* Multi-threaded debugging support for GNU/Linux (LWP layer).
2 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
25 #include <errno.h>
26 #include <signal.h>
27 #include <sys/ptrace.h>
28 #include "gdb_wait.h"
29
30 #include "gdbthread.h"
31 #include "inferior.h"
32 #include "target.h"
33 #include "regcache.h"
34 #include "gdbcmd.h"
35
36 static int debug_lin_lwp;
37 extern char *strsignal (int sig);
38
39 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
40 are processes sharing the same VM space. A multi-threaded process
41 is basically a group of such processes. However, such a grouping
42 is almost entirely a user-space issue; the kernel doesn't enforce
43 such a grouping at all (this might change in the future). In
44 general, we'll rely on the threads library (i.e. the GNU/Linux
45 Threads library) to provide such a grouping.
46
47 It is perfectly well possible to write a multi-threaded application
48 without the assistance of a threads library, by using the clone
49 system call directly. This module should be able to give some
50 rudimentary support for debugging such applications if developers
51 specify the CLONE_PTRACE flag in the clone system call, and are
52 using the Linux kernel 2.4 or above.
53
54 Note that there are some peculiarities in GNU/Linux that affect
55 this code:
56
57 - In general one should specify the __WCLONE flag to waitpid in
58 order to make it report events for any of the cloned processes
59 (and leave it out for the initial process). However, if a cloned
60 process has exited the exit status is only reported if the
61 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
62 we cannot use it since GDB must work on older systems too.
63
64 - When a traced, cloned process exits and is waited for by the
65 debugger, the kernel reassigns it to the original parent and
66 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
67 library doesn't notice this, which leads to the "zombie problem":
68 When debugged a multi-threaded process that spawns a lot of
69 threads will run out of processes, even if the threads exit,
70 because the "zombies" stay around. */
71
72 /* Structure describing a LWP. */
73 struct lwp_info
74 {
75 /* The process id of the LWP. This is a combination of the LWP id
76 and overall process id. */
77 ptid_t ptid;
78
79 /* Non-zero if this LWP is cloned. In this context "cloned" means
80 that the LWP is reporting to its parent using a signal other than
81 SIGCHLD. */
82 int cloned;
83
84 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
85 it back yet). */
86 int signalled;
87
88 /* Non-zero if this LWP is stopped. */
89 int stopped;
90
91 /* Non-zero if this LWP will be/has been resumed. Note that an LWP
92 can be marked both as stopped and resumed at the same time. This
93 happens if we try to resume an LWP that has a wait status
94 pending. We shouldn't let the LWP run until that wait status has
95 been processed, but we should not report that wait status if GDB
96 didn't try to let the LWP run. */
97 int resumed;
98
99 /* If non-zero, a pending wait status. */
100 int status;
101
102 /* Non-zero if we were stepping this LWP. */
103 int step;
104
105 /* Next LWP in list. */
106 struct lwp_info *next;
107 };
108
109 /* List of known LWPs. */
110 static struct lwp_info *lwp_list;
111
112 /* Number of LWPs in the list. */
113 static int num_lwps;
114
115 /* Non-zero if we're running in "threaded" mode. */
116 static int threaded;
117 \f
118
119 #define GET_LWP(ptid) ptid_get_lwp (ptid)
120 #define GET_PID(ptid) ptid_get_pid (ptid)
121 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
122 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
123
124 /* If the last reported event was a SIGTRAP, this variable is set to
125 the process id of the LWP/thread that got it. */
126 ptid_t trap_ptid;
127 \f
128
129 /* This module's target-specific operations. */
130 static struct target_ops lin_lwp_ops;
131
132 /* The standard child operations. */
133 extern struct target_ops child_ops;
134
135 /* Since we cannot wait (in lin_lwp_wait) for the initial process and
136 any cloned processes with a single call to waitpid, we have to use
137 the WNOHANG flag and call waitpid in a loop. To optimize
138 things a bit we use `sigsuspend' to wake us up when a process has
139 something to report (it will send us a SIGCHLD if it has). To make
140 this work we have to juggle with the signal mask. We save the
141 original signal mask such that we can restore it before creating a
142 new process in order to avoid blocking certain signals in the
143 inferior. We then block SIGCHLD during the waitpid/sigsuspend
144 loop. */
145
146 /* Original signal mask. */
147 static sigset_t normal_mask;
148
149 /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
150 _initialize_lin_lwp. */
151 static sigset_t suspend_mask;
152
153 /* Signals to block to make that sigsuspend work. */
154 static sigset_t blocked_mask;
155 \f
156
157 /* Prototypes for local functions. */
158 static int stop_wait_callback (struct lwp_info *lp, void *data);
159 \f
160 /* Convert wait status STATUS to a string. Used for printing debug
161 messages only. */
162
163 static char *
164 status_to_str (int status)
165 {
166 static char buf[64];
167
168 if (WIFSTOPPED (status))
169 snprintf (buf, sizeof (buf), "%s (stopped)",
170 strsignal (WSTOPSIG (status)));
171 else if (WIFSIGNALED (status))
172 snprintf (buf, sizeof (buf), "%s (terminated)",
173 strsignal (WSTOPSIG (status)));
174 else
175 snprintf (buf, sizeof (buf), "%d (exited)",
176 WEXITSTATUS (status));
177
178 return buf;
179 }
180 \f
181 /* Initialize the list of LWPs. Note that this module, contrary to
182 what GDB's generic threads layer does for its thread list,
183 re-initializes the LWP lists whenever we mourn or detach (which
184 doesn't involve mourning) the inferior. */
185
186 static void
187 init_lwp_list (void)
188 {
189 struct lwp_info *lp, *lpnext;
190
191 for (lp = lwp_list; lp; lp = lpnext)
192 {
193 lpnext = lp->next;
194 xfree (lp);
195 }
196
197 lwp_list = NULL;
198 num_lwps = 0;
199 threaded = 0;
200 }
201
202 /* Add the LWP specified by PID to the list. If this causes the
203 number of LWPs to become larger than one, go into "threaded" mode.
204 Return a pointer to the structure describing the new LWP. */
205
206 static struct lwp_info *
207 add_lwp (ptid_t ptid)
208 {
209 struct lwp_info *lp;
210
211 gdb_assert (is_lwp (ptid));
212
213 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
214
215 memset (lp, 0, sizeof (struct lwp_info));
216
217 lp->ptid = ptid;
218
219 lp->next = lwp_list;
220 lwp_list = lp;
221 if (++num_lwps > 1)
222 threaded = 1;
223
224 return lp;
225 }
226
227 /* Remove the LWP specified by PID from the list. */
228
229 static void
230 delete_lwp (ptid_t ptid)
231 {
232 struct lwp_info *lp, *lpprev;
233
234 lpprev = NULL;
235
236 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
237 if (ptid_equal (lp->ptid, ptid))
238 break;
239
240 if (!lp)
241 return;
242
243 /* We don't go back to "non-threaded" mode if the number of threads
244 becomes less than two. */
245 num_lwps--;
246
247 if (lpprev)
248 lpprev->next = lp->next;
249 else
250 lwp_list = lp->next;
251
252 xfree (lp);
253 }
254
255 /* Return a pointer to the structure describing the LWP corresponding
256 to PID. If no corresponding LWP could be found, return NULL. */
257
258 static struct lwp_info *
259 find_lwp_pid (ptid_t ptid)
260 {
261 struct lwp_info *lp;
262 int lwp;
263
264 if (is_lwp (ptid))
265 lwp = GET_LWP (ptid);
266 else
267 lwp = GET_PID (ptid);
268
269 for (lp = lwp_list; lp; lp = lp->next)
270 if (lwp == GET_LWP (lp->ptid))
271 return lp;
272
273 return NULL;
274 }
275
276 /* Call CALLBACK with its second argument set to DATA for every LWP in
277 the list. If CALLBACK returns 1 for a particular LWP, return a
278 pointer to the structure describing that LWP immediately.
279 Otherwise return NULL. */
280
281 struct lwp_info *
282 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
283 {
284 struct lwp_info *lp, *lpnext;
285
286 for (lp = lwp_list; lp; lp = lpnext)
287 {
288 lpnext = lp->next;
289 if ((*callback) (lp, data))
290 return lp;
291 }
292
293 return NULL;
294 }
295 \f
296
297 /* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP
298 layer.
299
300 Note that this implementation is potentially redundant now that
301 default_prepare_to_proceed() has been added.
302
303 FIXME This may not support switching threads after Ctrl-C
304 correctly. The default implementation does support this. */
305
306 int
307 lin_lwp_prepare_to_proceed (void)
308 {
309 if (! ptid_equal (trap_ptid, null_ptid)
310 && ! ptid_equal (inferior_ptid, trap_ptid))
311 {
312 /* Switched over from TRAP_PID. */
313 CORE_ADDR stop_pc = read_pc ();
314 CORE_ADDR trap_pc;
315
316 /* Avoid switching where it wouldn't do any good, i.e. if both
317 threads are at the same breakpoint. */
318 trap_pc = read_pc_pid (trap_ptid);
319 if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
320 {
321 /* User hasn't deleted the breakpoint. Return non-zero, and
322 switch back to TRAP_PID. */
323 inferior_ptid = trap_ptid;
324
325 /* FIXME: Is this stuff really necessary? */
326 flush_cached_frames ();
327 registers_changed ();
328
329 return 1;
330 }
331 }
332
333 return 0;
334 }
335 \f
336
337 #if 0
338 static void
339 lin_lwp_open (char *args, int from_tty)
340 {
341 push_target (&lin_lwp_ops);
342 }
343 #endif
344
345 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
346 a message telling the user that a new LWP has been added to the
347 process. */
348
349 void
350 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
351 {
352 struct lwp_info *lp;
353
354 gdb_assert (is_lwp (ptid));
355
356 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
357 to interrupt either the ptrace() or waitpid() calls below. */
358 if (! sigismember (&blocked_mask, SIGCHLD))
359 {
360 sigaddset (&blocked_mask, SIGCHLD);
361 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
362 }
363
364 if (verbose)
365 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
366
367 lp = find_lwp_pid (ptid);
368 if (lp == NULL)
369 lp = add_lwp (ptid);
370
371 /* We assume that we're already attached to any LWP that has an
372 id equal to the overall process id. */
373 if (GET_LWP (ptid) != GET_PID (ptid))
374 {
375 pid_t pid;
376 int status;
377
378 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
379 error ("Can't attach %s: %s", target_pid_to_str (ptid),
380 safe_strerror (errno));
381
382 if (debug_lin_lwp)
383 fprintf_unfiltered (gdb_stdlog,
384 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
385 target_pid_to_str (ptid));
386
387 pid = waitpid (GET_LWP (ptid), &status, 0);
388 if (pid == -1 && errno == ECHILD)
389 {
390 /* Try again with __WCLONE to check cloned processes. */
391 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
392 lp->cloned = 1;
393 }
394
395 gdb_assert (pid == GET_LWP (ptid)
396 && WIFSTOPPED (status) && WSTOPSIG (status));
397
398 lp->stopped = 1;
399
400 if (debug_lin_lwp)
401 {
402 fprintf_unfiltered (gdb_stdlog,
403 "LLAL: waitpid %s received %s\n",
404 target_pid_to_str (ptid),
405 status_to_str (status));
406 }
407 }
408 else
409 {
410 /* We assume that the LWP representing the original process
411 is already stopped. Mark it as stopped in the data structure
412 that the lin-lwp layer uses to keep track of threads. Note
413 that this won't have already been done since the main thread
414 will have, we assume, been stopped by an attach from a
415 different layer. */
416 lp->stopped = 1;
417 }
418 }
419
420 static void
421 lin_lwp_attach (char *args, int from_tty)
422 {
423 struct lwp_info *lp;
424 pid_t pid;
425 int status;
426
427 /* FIXME: We should probably accept a list of process id's, and
428 attach all of them. */
429 child_ops.to_attach (args, from_tty);
430
431 /* Add the initial process as the first LWP to the list. */
432 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
433
434 /* Make sure the initial process is stopped. The user-level threads
435 layer might want to poke around in the inferior, and that won't
436 work if things haven't stabilized yet. */
437 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
438 if (pid == -1 && errno == ECHILD)
439 {
440 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
441
442 /* Try again with __WCLONE to check cloned processes. */
443 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
444 lp->cloned = 1;
445 }
446
447 gdb_assert (pid == GET_PID (inferior_ptid)
448 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
449
450 lp->stopped = 1;
451
452 /* Fake the SIGSTOP that core GDB expects. */
453 lp->status = W_STOPCODE (SIGSTOP);
454 lp->resumed = 1;
455 if (debug_lin_lwp)
456 {
457 fprintf_unfiltered (gdb_stdlog,
458 "LLA: waitpid %ld, faking SIGSTOP\n",
459 (long) pid);
460 }
461 }
462
463 static int
464 detach_callback (struct lwp_info *lp, void *data)
465 {
466 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
467
468 if (debug_lin_lwp && lp->status)
469 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
470 strsignal (WSTOPSIG (lp->status)),
471 target_pid_to_str (lp->ptid));
472
473 while (lp->signalled && lp->stopped)
474 {
475 errno = 0;
476 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
477 WSTOPSIG (lp->status)) < 0)
478 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
479 safe_strerror (errno));
480
481 if (debug_lin_lwp)
482 fprintf_unfiltered (gdb_stdlog,
483 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
484 target_pid_to_str (lp->ptid),
485 status_to_str (lp->status));
486
487 lp->stopped = 0;
488 lp->signalled = 0;
489 lp->status = 0;
490 stop_wait_callback (lp, NULL);
491
492 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
493 }
494
495 /* We don't actually detach from the LWP that has an id equal to the
496 overall process id just yet. */
497 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
498 {
499 errno = 0;
500 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
501 WSTOPSIG (lp->status)) < 0)
502 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
503 safe_strerror (errno));
504
505 if (debug_lin_lwp)
506 fprintf_unfiltered (gdb_stdlog,
507 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
508 target_pid_to_str (lp->ptid),
509 strsignal (WSTOPSIG (lp->status)));
510
511 delete_lwp (lp->ptid);
512 }
513
514 return 0;
515 }
516
517 static void
518 lin_lwp_detach (char *args, int from_tty)
519 {
520 iterate_over_lwps (detach_callback, NULL);
521
522 /* Only the initial process should be left right now. */
523 gdb_assert (num_lwps == 1);
524
525 trap_ptid = null_ptid;
526
527 /* Destroy LWP info; it's no longer valid. */
528 init_lwp_list ();
529
530 /* Restore the original signal mask. */
531 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
532 sigemptyset (&blocked_mask);
533
534 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
535 child_ops.to_detach (args, from_tty);
536 }
537 \f
538
539 /* Resume LP. */
540
541 static int
542 resume_callback (struct lwp_info *lp, void *data)
543 {
544 if (lp->stopped && lp->status == 0)
545 {
546 struct thread_info *tp;
547
548 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
549 if (debug_lin_lwp)
550 fprintf_unfiltered (gdb_stdlog,
551 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
552 target_pid_to_str (lp->ptid));
553 lp->stopped = 0;
554 lp->step = 0;
555 }
556
557 return 0;
558 }
559
560 static int
561 resume_clear_callback (struct lwp_info *lp, void *data)
562 {
563 lp->resumed = 0;
564 return 0;
565 }
566
567 static int
568 resume_set_callback (struct lwp_info *lp, void *data)
569 {
570 lp->resumed = 1;
571 return 0;
572 }
573
574 static void
575 lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
576 {
577 struct lwp_info *lp;
578 int resume_all;
579
580 /* A specific PTID means `step only this process id'. */
581 resume_all = (PIDGET (ptid) == -1);
582
583 if (resume_all)
584 iterate_over_lwps (resume_set_callback, NULL);
585 else
586 iterate_over_lwps (resume_clear_callback, NULL);
587
588 /* If PID is -1, it's the current inferior that should be
589 handled specially. */
590 if (PIDGET (ptid) == -1)
591 ptid = inferior_ptid;
592
593 lp = find_lwp_pid (ptid);
594 if (lp)
595 {
596 ptid = pid_to_ptid (GET_LWP (lp->ptid));
597
598 /* Remember if we're stepping. */
599 lp->step = step;
600
601 /* Mark this LWP as resumed. */
602 lp->resumed = 1;
603
604 /* If we have a pending wait status for this thread, there is no
605 point in resuming the process. */
606 if (lp->status)
607 {
608 /* FIXME: What should we do if we are supposed to continue
609 this thread with a signal? */
610 gdb_assert (signo == TARGET_SIGNAL_0);
611 return;
612 }
613
614 /* Mark LWP as not stopped to prevent it from being continued by
615 resume_callback. */
616 lp->stopped = 0;
617 }
618
619 if (resume_all)
620 iterate_over_lwps (resume_callback, NULL);
621
622 child_resume (ptid, step, signo);
623 if (debug_lin_lwp)
624 fprintf_unfiltered (gdb_stdlog,
625 "LLR: %s %s, %s (resume event thread)\n",
626 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
627 target_pid_to_str (ptid),
628 signo ? strsignal (signo) : "0");
629 }
630 \f
631
632 /* Send a SIGSTOP to LP. */
633
634 static int
635 stop_callback (struct lwp_info *lp, void *data)
636 {
637 if (! lp->stopped && ! lp->signalled)
638 {
639 int ret;
640
641 if (debug_lin_lwp)
642 {
643 fprintf_unfiltered (gdb_stdlog,
644 "SC: kill %s **<SIGSTOP>**\n",
645 target_pid_to_str (lp->ptid));
646 }
647 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
648 gdb_assert (ret == 0);
649
650 lp->signalled = 1;
651 gdb_assert (lp->status == 0);
652 }
653
654 return 0;
655 }
656
657 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
658 a pointer to a set of signals to be flushed immediately. */
659
660 static int
661 stop_wait_callback (struct lwp_info *lp, void *data)
662 {
663 sigset_t *flush_mask = data;
664
665 if (! lp->stopped && lp->signalled)
666 {
667 pid_t pid;
668 int status;
669
670 gdb_assert (lp->status == 0);
671
672 pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
673 if (pid == -1 && errno == ECHILD)
674 /* OK, the proccess has disappeared. We'll catch the actual
675 exit event in lin_lwp_wait. */
676 return 0;
677
678 gdb_assert (pid == GET_LWP (lp->ptid));
679
680 if (debug_lin_lwp)
681 {
682 fprintf_unfiltered (gdb_stdlog,
683 "SWC: waitpid %s received %s\n",
684 target_pid_to_str (lp->ptid),
685 status_to_str (status));
686 }
687
688 if (WIFEXITED (status) || WIFSIGNALED (status))
689 {
690 gdb_assert (num_lwps > 1);
691
692 if (in_thread_list (lp->ptid))
693 {
694 /* Core GDB cannot deal with us deleting the current
695 thread. */
696 if (!ptid_equal (lp->ptid, inferior_ptid))
697 delete_thread (lp->ptid);
698 printf_unfiltered ("[%s exited]\n",
699 target_pid_to_str (lp->ptid));
700 }
701 if (debug_lin_lwp)
702 fprintf_unfiltered (gdb_stdlog, "SWC: %s exited.\n",
703 target_pid_to_str (lp->ptid));
704
705 delete_lwp (lp->ptid);
706 return 0;
707 }
708
709 gdb_assert (WIFSTOPPED (status));
710
711 /* Ignore any signals in FLUSH_MASK. */
712 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
713 {
714 errno = 0;
715 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
716 if (debug_lin_lwp)
717 fprintf_unfiltered (gdb_stdlog,
718 "PTRACE_CONT %s, 0, 0 (%s)\n",
719 target_pid_to_str (lp->ptid),
720 errno ? safe_strerror (errno) : "OK");
721
722 return stop_wait_callback (lp, flush_mask);
723 }
724
725 if (WSTOPSIG (status) != SIGSTOP)
726 {
727 if (WSTOPSIG (status) == SIGTRAP)
728 {
729 /* If a LWP other than the LWP that we're reporting an
730 event for has hit a GDB breakpoint (as opposed to
731 some random trap signal), then just arrange for it to
732 hit it again later. We don't keep the SIGTRAP status
733 and don't forward the SIGTRAP signal to the LWP. We
734 will handle the current event, eventually we will
735 resume all LWPs, and this one will get its breakpoint
736 trap again.
737
738 If we do not do this, then we run the risk that the
739 user will delete or disable the breakpoint, but the
740 thread will have already tripped on it. */
741
742 /* Now resume this LWP and get the SIGSTOP event. */
743 errno = 0;
744 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
745 if (debug_lin_lwp)
746 {
747 fprintf_unfiltered (gdb_stdlog,
748 "PTRACE_CONT %s, 0, 0 (%s)\n",
749 target_pid_to_str (lp->ptid),
750 errno ? safe_strerror (errno) : "OK");
751
752 fprintf_unfiltered (gdb_stdlog,
753 "SWC: Candidate SIGTRAP event in %s\n",
754 target_pid_to_str (lp->ptid));
755 }
756 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
757 stop_wait_callback (lp, data);
758 /* If there's another event, throw it back into the queue. */
759 if (lp->status)
760 {
761 kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
762 }
763 /* Save the sigtrap event. */
764 lp->status = status;
765 return 0;
766 }
767 else
768 {
769 /* The thread was stopped with a signal other than
770 SIGSTOP, and didn't accidentally trip a breakpoint. */
771
772 if (debug_lin_lwp)
773 {
774 fprintf_unfiltered (gdb_stdlog,
775 "SWC: Pending event %s in %s\n",
776 status_to_str ((int) status),
777 target_pid_to_str (lp->ptid));
778 }
779 /* Now resume this LWP and get the SIGSTOP event. */
780 errno = 0;
781 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
782 if (debug_lin_lwp)
783 fprintf_unfiltered (gdb_stdlog,
784 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
785 target_pid_to_str (lp->ptid),
786 errno ? safe_strerror (errno) : "OK");
787
788 /* Hold this event/waitstatus while we check to see if
789 there are any more (we still want to get that SIGSTOP). */
790 stop_wait_callback (lp, data);
791 /* If the lp->status field is still empty, use it to hold
792 this event. If not, then this event must be returned
793 to the event queue of the LWP. */
794 if (lp->status == 0)
795 lp->status = status;
796 else
797 {
798 if (debug_lin_lwp)
799 {
800 fprintf_unfiltered (gdb_stdlog,
801 "SWC: kill %s, %s\n",
802 target_pid_to_str (lp->ptid),
803 status_to_str ((int) status));
804 }
805 kill (GET_LWP (lp->ptid), WSTOPSIG (status));
806 }
807 return 0;
808 }
809 }
810 else
811 {
812 /* We caught the SIGSTOP that we intended to catch, so
813 there's no SIGSTOP pending. */
814 lp->stopped = 1;
815 lp->signalled = 0;
816 }
817 }
818
819 return 0;
820 }
821
822 /* Return non-zero if LP has a wait status pending. */
823
824 static int
825 status_callback (struct lwp_info *lp, void *data)
826 {
827 /* Only report a pending wait status if we pretend that this has
828 indeed been resumed. */
829 return (lp->status != 0 && lp->resumed);
830 }
831
832 /* Return non-zero if LP isn't stopped. */
833
834 static int
835 running_callback (struct lwp_info *lp, void *data)
836 {
837 return (lp->stopped == 0);
838 }
839
840 /* Count the LWP's that have had events. */
841
842 static int
843 count_events_callback (struct lwp_info *lp, void *data)
844 {
845 int *count = data;
846
847 gdb_assert (count != NULL);
848
849 /* Count only LWPs that have a SIGTRAP event pending. */
850 if (lp->status != 0
851 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
852 (*count)++;
853
854 return 0;
855 }
856
857 /* Select the LWP (if any) that is currently being single-stepped. */
858
859 static int
860 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
861 {
862 if (lp->step && lp->status != 0)
863 return 1;
864 else
865 return 0;
866 }
867
868 /* Select the Nth LWP that has had a SIGTRAP event. */
869
870 static int
871 select_event_lwp_callback (struct lwp_info *lp, void *data)
872 {
873 int *selector = data;
874
875 gdb_assert (selector != NULL);
876
877 /* Select only LWPs that have a SIGTRAP event pending. */
878 if (lp->status != 0
879 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
880 if ((*selector)-- == 0)
881 return 1;
882
883 return 0;
884 }
885
886 static int
887 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
888 {
889 struct lwp_info *event_lp = data;
890
891 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
892 if (lp == event_lp)
893 return 0;
894
895 /* If a LWP other than the LWP that we're reporting an event for has
896 hit a GDB breakpoint (as opposed to some random trap signal),
897 then just arrange for it to hit it again later. We don't keep
898 the SIGTRAP status and don't forward the SIGTRAP signal to the
899 LWP. We will handle the current event, eventually we will resume
900 all LWPs, and this one will get its breakpoint trap again.
901
902 If we do not do this, then we run the risk that the user will
903 delete or disable the breakpoint, but the LWP will have already
904 tripped on it. */
905
906 if (lp->status != 0
907 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
908 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
909 DECR_PC_AFTER_BREAK))
910 {
911 if (debug_lin_lwp)
912 fprintf_unfiltered (gdb_stdlog,
913 "CBC: Push back breakpoint for %s\n",
914 target_pid_to_str (lp->ptid));
915
916 /* Back up the PC if necessary. */
917 if (DECR_PC_AFTER_BREAK)
918 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
919
920 /* Throw away the SIGTRAP. */
921 lp->status = 0;
922 }
923
924 return 0;
925 }
926
927 /* Select one LWP out of those that have events pending. */
928
929 static void
930 select_event_lwp (struct lwp_info **orig_lp, int *status)
931 {
932 int num_events = 0;
933 int random_selector;
934 struct lwp_info *event_lp;
935
936 /* Record the wait status for the origional LWP. */
937 (*orig_lp)->status = *status;
938
939 /* Give preference to any LWP that is being single-stepped. */
940 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
941 if (event_lp != NULL)
942 {
943 if (debug_lin_lwp)
944 fprintf_unfiltered (gdb_stdlog,
945 "SEL: Select single-step %s\n",
946 target_pid_to_str (event_lp->ptid));
947 }
948 else
949 {
950 /* No single-stepping LWP. Select one at random, out of those
951 which have had SIGTRAP events. */
952
953 /* First see how many SIGTRAP events we have. */
954 iterate_over_lwps (count_events_callback, &num_events);
955
956 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
957 random_selector = (int)
958 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
959
960 if (debug_lin_lwp && num_events > 1)
961 fprintf_unfiltered (gdb_stdlog,
962 "SEL: Found %d SIGTRAP events, selecting #%d\n",
963 num_events, random_selector);
964
965 event_lp = iterate_over_lwps (select_event_lwp_callback,
966 &random_selector);
967 }
968
969 if (event_lp != NULL)
970 {
971 /* Switch the event LWP. */
972 *orig_lp = event_lp;
973 *status = event_lp->status;
974 }
975
976 /* Flush the wait status for the event LWP. */
977 (*orig_lp)->status = 0;
978 }
979
980 /* Return non-zero if LP has been resumed. */
981
982 static int
983 resumed_callback (struct lwp_info *lp, void *data)
984 {
985 return lp->resumed;
986 }
987
988 #ifdef CHILD_WAIT
989
990 /* We need to override child_wait to support attaching to cloned
991 processes, since a normal wait (as done by the default version)
992 ignores those processes. */
993
994 /* Wait for child PTID to do something. Return id of the child,
995 minus_one_ptid in case of error; store status into *OURSTATUS. */
996
997 ptid_t
998 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
999 {
1000 int save_errno;
1001 int status;
1002 pid_t pid;
1003
1004 do
1005 {
1006 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1007 attached process. */
1008 set_sigio_trap ();
1009
1010 pid = waitpid (GET_PID (ptid), &status, 0);
1011 if (pid == -1 && errno == ECHILD)
1012 /* Try again with __WCLONE to check cloned processes. */
1013 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1014
1015 if (debug_lin_lwp)
1016 {
1017 fprintf_unfiltered (gdb_stdlog,
1018 "CW: waitpid %ld received %s\n",
1019 (long) pid,
1020 status_to_str (status));
1021 }
1022
1023 save_errno = errno;
1024
1025 /* Make sure we don't report an event for the exit of the
1026 original program, if we've detached from it. */
1027 if (pid != -1 && ! WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1028 {
1029 pid = -1;
1030 save_errno = EINTR;
1031 }
1032
1033 clear_sigio_trap ();
1034 clear_sigint_trap ();
1035 }
1036 while (pid == -1 && save_errno == EINTR);
1037
1038 if (pid == -1)
1039 {
1040 warning ("Child process unexpectedly missing: %s",
1041 safe_strerror (errno));
1042
1043 /* Claim it exited with unknown signal. */
1044 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1045 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1046 return minus_one_ptid;
1047 }
1048
1049 store_waitstatus (ourstatus, status);
1050 return pid_to_ptid (pid);
1051 }
1052
1053 #endif
1054
1055 static ptid_t
1056 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1057 {
1058 struct lwp_info *lp = NULL;
1059 int options = 0;
1060 int status = 0;
1061 pid_t pid = PIDGET (ptid);
1062 sigset_t flush_mask;
1063
1064 sigemptyset (&flush_mask);
1065
1066 /* Make sure SIGCHLD is blocked. */
1067 if (! sigismember (&blocked_mask, SIGCHLD))
1068 {
1069 sigaddset (&blocked_mask, SIGCHLD);
1070 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1071 }
1072
1073 retry:
1074
1075 /* Make sure there is at least one LWP that has been resumed, at
1076 least if there are any LWPs at all. */
1077 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1078
1079 /* First check if there is a LWP with a wait status pending. */
1080 if (pid == -1)
1081 {
1082 /* Any LWP that's been resumed will do. */
1083 lp = iterate_over_lwps (status_callback, NULL);
1084 if (lp)
1085 {
1086 status = lp->status;
1087 lp->status = 0;
1088
1089 if (debug_lin_lwp && status)
1090 fprintf_unfiltered (gdb_stdlog,
1091 "LLW: Using pending wait status %s for %s.\n",
1092 status_to_str (status),
1093 target_pid_to_str (lp->ptid));
1094 }
1095
1096 /* But if we don't fine one, we'll have to wait, and check both
1097 cloned and uncloned processes. We start with the cloned
1098 processes. */
1099 options = __WCLONE | WNOHANG;
1100 }
1101 else if (is_lwp (ptid))
1102 {
1103 if (debug_lin_lwp)
1104 fprintf_unfiltered (gdb_stdlog,
1105 "LLW: Waiting for specific LWP %s.\n",
1106 target_pid_to_str (ptid));
1107
1108 /* We have a specific LWP to check. */
1109 lp = find_lwp_pid (ptid);
1110 gdb_assert (lp);
1111 status = lp->status;
1112 lp->status = 0;
1113
1114 if (debug_lin_lwp && status)
1115 fprintf_unfiltered (gdb_stdlog,
1116 "LLW: Using pending wait status %s for %s.\n",
1117 status_to_str (status),
1118 target_pid_to_str (lp->ptid));
1119
1120 /* If we have to wait, take into account whether PID is a cloned
1121 process or not. And we have to convert it to something that
1122 the layer beneath us can understand. */
1123 options = lp->cloned ? __WCLONE : 0;
1124 pid = GET_LWP (ptid);
1125 }
1126
1127 if (status && lp->signalled)
1128 {
1129 /* A pending SIGSTOP may interfere with the normal stream of
1130 events. In a typical case where interference is a problem,
1131 we have a SIGSTOP signal pending for LWP A while
1132 single-stepping it, encounter an event in LWP B, and take the
1133 pending SIGSTOP while trying to stop LWP A. After processing
1134 the event in LWP B, LWP A is continued, and we'll never see
1135 the SIGTRAP associated with the last time we were
1136 single-stepping LWP A. */
1137
1138 /* Resume the thread. It should halt immediately returning the
1139 pending SIGSTOP. */
1140 registers_changed ();
1141 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1142 TARGET_SIGNAL_0);
1143 if (debug_lin_lwp)
1144 fprintf_unfiltered (gdb_stdlog,
1145 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1146 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1147 target_pid_to_str (lp->ptid));
1148 lp->stopped = 0;
1149 gdb_assert (lp->resumed);
1150
1151 /* This should catch the pending SIGSTOP. */
1152 stop_wait_callback (lp, NULL);
1153 }
1154
1155 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1156 attached process. */
1157 set_sigio_trap ();
1158
1159 while (status == 0)
1160 {
1161 pid_t lwpid;
1162
1163 lwpid = waitpid (pid, &status, options);
1164 if (lwpid > 0)
1165 {
1166 gdb_assert (pid == -1 || lwpid == pid);
1167
1168 if (debug_lin_lwp)
1169 {
1170 fprintf_unfiltered (gdb_stdlog,
1171 "LLW: waitpid %ld received %s\n",
1172 (long) lwpid,
1173 status_to_str (status));
1174 }
1175
1176 lp = find_lwp_pid (pid_to_ptid (lwpid));
1177
1178 /* Make sure we don't report an event for the exit of an LWP not in
1179 our list, i.e. not part of the current process. This can happen
1180 if we detach from a program we original forked and then it
1181 exits. */
1182 if (! WIFSTOPPED (status) && ! lp)
1183 {
1184 status = 0;
1185 continue;
1186 }
1187
1188 if (! lp)
1189 {
1190 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1191 if (options & __WCLONE)
1192 lp->cloned = 1;
1193
1194 if (threaded)
1195 {
1196 gdb_assert (WIFSTOPPED (status)
1197 && WSTOPSIG (status) == SIGSTOP);
1198 lp->signalled = 1;
1199
1200 if (! in_thread_list (inferior_ptid))
1201 {
1202 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1203 GET_PID (inferior_ptid));
1204 add_thread (inferior_ptid);
1205 }
1206
1207 add_thread (lp->ptid);
1208 printf_unfiltered ("[New %s]\n",
1209 target_pid_to_str (lp->ptid));
1210 }
1211 }
1212
1213 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1214 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1215 left in the process. */
1216 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1217 {
1218 if (in_thread_list (lp->ptid))
1219 {
1220 /* Core GDB cannot deal with us deleting the current
1221 thread. */
1222 if (! ptid_equal (lp->ptid, inferior_ptid))
1223 delete_thread (lp->ptid);
1224 printf_unfiltered ("[%s exited]\n",
1225 target_pid_to_str (lp->ptid));
1226 }
1227 if (debug_lin_lwp)
1228 fprintf_unfiltered (gdb_stdlog,
1229 "LLW: %s exited.\n",
1230 target_pid_to_str (lp->ptid));
1231
1232 delete_lwp (lp->ptid);
1233
1234 /* Make sure there is at least one thread running. */
1235 gdb_assert (iterate_over_lwps (running_callback, NULL));
1236
1237 /* Discard the event. */
1238 status = 0;
1239 continue;
1240 }
1241
1242 /* Make sure we don't report a SIGSTOP that we sent
1243 ourselves in an attempt to stop an LWP. */
1244 if (lp->signalled
1245 && WIFSTOPPED (status)
1246 && WSTOPSIG (status) == SIGSTOP)
1247 {
1248 if (debug_lin_lwp)
1249 fprintf_unfiltered (gdb_stdlog,
1250 "LLW: Delayed SIGSTOP caught for %s.\n",
1251 target_pid_to_str (lp->ptid));
1252
1253 /* This is a delayed SIGSTOP. */
1254 lp->signalled = 0;
1255
1256 registers_changed ();
1257 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1258 TARGET_SIGNAL_0);
1259 if (debug_lin_lwp)
1260 fprintf_unfiltered (gdb_stdlog,
1261 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
1262 lp->step ?
1263 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1264 target_pid_to_str (lp->ptid));
1265
1266 lp->stopped = 0;
1267 gdb_assert (lp->resumed);
1268
1269 /* Discard the event. */
1270 status = 0;
1271 continue;
1272 }
1273
1274 break;
1275 }
1276
1277 if (pid == -1)
1278 {
1279 /* Alternate between checking cloned and uncloned processes. */
1280 options ^= __WCLONE;
1281
1282 /* And suspend every time we have checked both. */
1283 if (options & __WCLONE)
1284 sigsuspend (&suspend_mask);
1285 }
1286
1287 /* We shouldn't end up here unless we want to try again. */
1288 gdb_assert (status == 0);
1289 }
1290
1291 clear_sigio_trap ();
1292 clear_sigint_trap ();
1293
1294 gdb_assert (lp);
1295
1296 /* Don't report signals that GDB isn't interested in, such as
1297 signals that are neither printed nor stopped upon. Stopping all
1298 threads can be a bit time-consuming so if we want decent
1299 performance with heavily multi-threaded programs, especially when
1300 they're using a high frequency timer, we'd better avoid it if we
1301 can. */
1302
1303 if (WIFSTOPPED (status))
1304 {
1305 int signo = target_signal_from_host (WSTOPSIG (status));
1306
1307 if (signal_stop_state (signo) == 0
1308 && signal_print_state (signo) == 0
1309 && signal_pass_state (signo) == 1)
1310 {
1311 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1312 here? It is not clear we should. GDB may not expect
1313 other threads to run. On the other hand, not resuming
1314 newly attached threads may cause an unwanted delay in
1315 getting them running. */
1316 registers_changed ();
1317 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
1318 if (debug_lin_lwp)
1319 fprintf_unfiltered (gdb_stdlog,
1320 "LLW: %s %s, %s (preempt 'handle')\n",
1321 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1322 target_pid_to_str (lp->ptid),
1323 signo ? strsignal (signo) : "0");
1324 lp->stopped = 0;
1325 status = 0;
1326 goto retry;
1327 }
1328
1329 if (signo == TARGET_SIGNAL_INT
1330 && signal_pass_state (signo) == 0)
1331 {
1332 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1333 forwarded to the entire process group, that is, all LWP's
1334 will receive it. Since we only want to report it once,
1335 we try to flush it from all LWPs except this one. */
1336 sigaddset (&flush_mask, SIGINT);
1337 }
1338 }
1339
1340 /* This LWP is stopped now. */
1341 lp->stopped = 1;
1342
1343 if (debug_lin_lwp)
1344 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
1345 status_to_str (status),
1346 target_pid_to_str (lp->ptid));
1347
1348 /* Now stop all other LWP's ... */
1349 iterate_over_lwps (stop_callback, NULL);
1350
1351 /* ... and wait until all of them have reported back that they're no
1352 longer running. */
1353 iterate_over_lwps (stop_wait_callback, &flush_mask);
1354
1355 /* If we're not waiting for a specific LWP, choose an event LWP from
1356 among those that have had events. Giving equal priority to all
1357 LWPs that have had events helps prevent starvation. */
1358 if (pid == -1)
1359 select_event_lwp (&lp, &status);
1360
1361 /* Now that we've selected our final event LWP, cancel any
1362 breakpoints in other LWPs that have hit a GDB breakpoint. See
1363 the comment in cancel_breakpoints_callback to find out why. */
1364 iterate_over_lwps (cancel_breakpoints_callback, lp);
1365
1366 /* If we're not running in "threaded" mode, we'll report the bare
1367 process id. */
1368
1369 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
1370 {
1371 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1372 if (debug_lin_lwp)
1373 fprintf_unfiltered (gdb_stdlog,
1374 "LLW: trap_ptid is %s.\n",
1375 target_pid_to_str (trap_ptid));
1376 }
1377 else
1378 trap_ptid = null_ptid;
1379
1380 store_waitstatus (ourstatus, status);
1381 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1382 }
1383
1384 static int
1385 kill_callback (struct lwp_info *lp, void *data)
1386 {
1387 errno = 0;
1388 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
1389 if (debug_lin_lwp)
1390 fprintf_unfiltered (gdb_stdlog,
1391 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
1392 target_pid_to_str (lp->ptid),
1393 errno ? safe_strerror (errno) : "OK");
1394
1395 return 0;
1396 }
1397
1398 static int
1399 kill_wait_callback (struct lwp_info *lp, void *data)
1400 {
1401 pid_t pid;
1402
1403 /* We must make sure that there are no pending events (delayed
1404 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1405 program doesn't interfere with any following debugging session. */
1406
1407 /* For cloned processes we must check both with __WCLONE and
1408 without, since the exit status of a cloned process isn't reported
1409 with __WCLONE. */
1410 if (lp->cloned)
1411 {
1412 do
1413 {
1414 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
1415 if (pid != (pid_t) -1 && debug_lin_lwp)
1416 {
1417 fprintf_unfiltered (gdb_stdlog,
1418 "KWC: wait %s received unknown.\n",
1419 target_pid_to_str (lp->ptid));
1420 }
1421 }
1422 while (pid == GET_LWP (lp->ptid));
1423
1424 gdb_assert (pid == -1 && errno == ECHILD);
1425 }
1426
1427 do
1428 {
1429 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
1430 if (pid != (pid_t) -1 && debug_lin_lwp)
1431 {
1432 fprintf_unfiltered (gdb_stdlog,
1433 "KWC: wait %s received unk.\n",
1434 target_pid_to_str (lp->ptid));
1435 }
1436 }
1437 while (pid == GET_LWP (lp->ptid));
1438
1439 gdb_assert (pid == -1 && errno == ECHILD);
1440 return 0;
1441 }
1442
1443 static void
1444 lin_lwp_kill (void)
1445 {
1446 /* Kill all LWP's ... */
1447 iterate_over_lwps (kill_callback, NULL);
1448
1449 /* ... and wait until we've flushed all events. */
1450 iterate_over_lwps (kill_wait_callback, NULL);
1451
1452 target_mourn_inferior ();
1453 }
1454
1455 static void
1456 lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1457 {
1458 child_ops.to_create_inferior (exec_file, allargs, env);
1459 }
1460
1461 static void
1462 lin_lwp_mourn_inferior (void)
1463 {
1464 trap_ptid = null_ptid;
1465
1466 /* Destroy LWP info; it's no longer valid. */
1467 init_lwp_list ();
1468
1469 /* Restore the original signal mask. */
1470 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1471 sigemptyset (&blocked_mask);
1472
1473 child_ops.to_mourn_inferior ();
1474 }
1475
1476 static int
1477 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1478 struct mem_attrib *attrib,
1479 struct target_ops *target)
1480 {
1481 struct cleanup *old_chain = save_inferior_ptid ();
1482 int xfer;
1483
1484 if (is_lwp (inferior_ptid))
1485 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
1486
1487 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1488 if (xfer == 0)
1489 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1490
1491 do_cleanups (old_chain);
1492 return xfer;
1493 }
1494
1495 static int
1496 lin_lwp_thread_alive (ptid_t ptid)
1497 {
1498 gdb_assert (is_lwp (ptid));
1499
1500 errno = 0;
1501 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1502 if (debug_lin_lwp)
1503 fprintf_unfiltered (gdb_stdlog,
1504 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
1505 target_pid_to_str (ptid),
1506 errno ? safe_strerror (errno) : "OK");
1507 if (errno)
1508 return 0;
1509
1510 return 1;
1511 }
1512
1513 static char *
1514 lin_lwp_pid_to_str (ptid_t ptid)
1515 {
1516 static char buf[64];
1517
1518 if (is_lwp (ptid))
1519 {
1520 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
1521 return buf;
1522 }
1523
1524 return normal_pid_to_str (ptid);
1525 }
1526
1527 static void
1528 init_lin_lwp_ops (void)
1529 {
1530 #if 0
1531 lin_lwp_ops.to_open = lin_lwp_open;
1532 #endif
1533 lin_lwp_ops.to_shortname = "lwp-layer";
1534 lin_lwp_ops.to_longname = "lwp-layer";
1535 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1536 lin_lwp_ops.to_attach = lin_lwp_attach;
1537 lin_lwp_ops.to_detach = lin_lwp_detach;
1538 lin_lwp_ops.to_resume = lin_lwp_resume;
1539 lin_lwp_ops.to_wait = lin_lwp_wait;
1540 /* fetch_inferior_registers and store_inferior_registers will
1541 honor the LWP id, so we can use them directly. */
1542 lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
1543 lin_lwp_ops.to_store_registers = store_inferior_registers;
1544 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1545 lin_lwp_ops.to_kill = lin_lwp_kill;
1546 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1547 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1548 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1549 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1550 lin_lwp_ops.to_stratum = thread_stratum;
1551 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1552 lin_lwp_ops.to_magic = OPS_MAGIC;
1553 }
1554
1555 static void
1556 sigchld_handler (int signo)
1557 {
1558 /* Do nothing. The only reason for this handler is that it allows
1559 us to use sigsuspend in lin_lwp_wait above to wait for the
1560 arrival of a SIGCHLD. */
1561 }
1562
1563 void
1564 _initialize_lin_lwp (void)
1565 {
1566 struct sigaction action;
1567
1568 extern void thread_db_init (struct target_ops *);
1569
1570 init_lin_lwp_ops ();
1571 add_target (&lin_lwp_ops);
1572 thread_db_init (&lin_lwp_ops);
1573
1574 /* Save the original signal mask. */
1575 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1576
1577 action.sa_handler = sigchld_handler;
1578 sigemptyset (&action.sa_mask);
1579 action.sa_flags = 0;
1580 sigaction (SIGCHLD, &action, NULL);
1581
1582 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1583 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
1584 sigdelset (&suspend_mask, SIGCHLD);
1585
1586 sigemptyset (&blocked_mask);
1587
1588 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1589 (char *) &debug_lin_lwp,
1590 "Set debugging of GNU/Linux lwp module.\n\
1591 Enables printf debugging output.\n",
1592 &setdebuglist),
1593 &showdebuglist);
1594 }
1595 \f
1596
1597 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
1598 the GNU/Linux Threads library and therefore doesn't really belong
1599 here. */
1600
1601 /* Read variable NAME in the target and return its value if found.
1602 Otherwise return zero. It is assumed that the type of the variable
1603 is `int'. */
1604
1605 static int
1606 get_signo (const char *name)
1607 {
1608 struct minimal_symbol *ms;
1609 int signo;
1610
1611 ms = lookup_minimal_symbol (name, NULL, NULL);
1612 if (ms == NULL)
1613 return 0;
1614
1615 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1616 sizeof (signo)) != 0)
1617 return 0;
1618
1619 return signo;
1620 }
1621
1622 /* Return the set of signals used by the threads library in *SET. */
1623
1624 void
1625 lin_thread_get_thread_signals (sigset_t *set)
1626 {
1627 struct sigaction action;
1628 int restart, cancel;
1629
1630 sigemptyset (set);
1631
1632 restart = get_signo ("__pthread_sig_restart");
1633 if (restart == 0)
1634 return;
1635
1636 cancel = get_signo ("__pthread_sig_cancel");
1637 if (cancel == 0)
1638 return;
1639
1640 sigaddset (set, restart);
1641 sigaddset (set, cancel);
1642
1643 /* The GNU/Linux Threads library makes terminating threads send a
1644 special "cancel" signal instead of SIGCHLD. Make sure we catch
1645 those (to prevent them from terminating GDB itself, which is
1646 likely to be their default action) and treat them the same way as
1647 SIGCHLD. */
1648
1649 action.sa_handler = sigchld_handler;
1650 sigemptyset (&action.sa_mask);
1651 action.sa_flags = 0;
1652 sigaction (cancel, &action, NULL);
1653
1654 /* We block the "cancel" signal throughout this code ... */
1655 sigaddset (&blocked_mask, cancel);
1656 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1657
1658 /* ... except during a sigsuspend. */
1659 sigdelset (&suspend_mask, cancel);
1660 }
This page took 0.062236 seconds and 4 git commands to generate.