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