Reoder functions to eliminate most of the static function prototypes.
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
CommitLineData
3993f6b1 1/* GNU/Linux native-dependent code common to multiple platforms.
a2f23071 2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3993f6b1
DJ
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#include "inferior.h"
23#include "target.h"
d6b0e80f 24#include "gdb_string.h"
3993f6b1 25#include "gdb_wait.h"
d6b0e80f
AC
26#include "gdb_assert.h"
27#ifdef HAVE_TKILL_SYSCALL
28#include <unistd.h>
29#include <sys/syscall.h>
30#endif
3993f6b1 31#include <sys/ptrace.h>
0274a8ce 32#include "linux-nat.h"
d6b0e80f
AC
33#include "gdbthread.h"
34#include "gdbcmd.h"
35#include "regcache.h"
0274a8ce 36
3993f6b1
DJ
37/* If the system headers did not provide the constants, hard-code the normal
38 values. */
39#ifndef PTRACE_EVENT_FORK
40
41#define PTRACE_SETOPTIONS 0x4200
42#define PTRACE_GETEVENTMSG 0x4201
43
44/* options set using PTRACE_SETOPTIONS */
45#define PTRACE_O_TRACESYSGOOD 0x00000001
46#define PTRACE_O_TRACEFORK 0x00000002
47#define PTRACE_O_TRACEVFORK 0x00000004
48#define PTRACE_O_TRACECLONE 0x00000008
49#define PTRACE_O_TRACEEXEC 0x00000010
9016a515
DJ
50#define PTRACE_O_TRACEVFORKDONE 0x00000020
51#define PTRACE_O_TRACEEXIT 0x00000040
3993f6b1
DJ
52
53/* Wait extended result codes for the above trace options. */
54#define PTRACE_EVENT_FORK 1
55#define PTRACE_EVENT_VFORK 2
56#define PTRACE_EVENT_CLONE 3
57#define PTRACE_EVENT_EXEC 4
9016a515
DJ
58#define PTRACE_EVENT_VFORKDONE 5
59#define PTRACE_EVENT_EXIT 6
3993f6b1
DJ
60
61#endif /* PTRACE_EVENT_FORK */
62
63/* We can't always assume that this flag is available, but all systems
64 with the ptrace event handlers also have __WALL, so it's safe to use
65 here. */
66#ifndef __WALL
67#define __WALL 0x40000000 /* Wait for any child. */
68#endif
69
d6b0e80f
AC
70static int debug_linux_nat;
71
4de4c07c
DJ
72extern struct target_ops child_ops;
73
9016a515
DJ
74static int linux_parent_pid;
75
ae087d01
DJ
76struct simple_pid_list
77{
78 int pid;
79 struct simple_pid_list *next;
80};
81struct simple_pid_list *stopped_pids;
82
3993f6b1
DJ
83/* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
84 can not be used, 1 if it can. */
85
86static int linux_supports_tracefork_flag = -1;
87
9016a515
DJ
88/* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
89 PTRACE_O_TRACEVFORKDONE. */
90
91static int linux_supports_tracevforkdone_flag = -1;
92
ae087d01
DJ
93\f
94/* Trivial list manipulation functions to keep track of a list of
95 new stopped processes. */
96static void
97add_to_pid_list (struct simple_pid_list **listp, int pid)
98{
99 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
100 new_pid->pid = pid;
101 new_pid->next = *listp;
102 *listp = new_pid;
103}
104
105static int
106pull_pid_from_list (struct simple_pid_list **listp, int pid)
107{
108 struct simple_pid_list **p;
109
110 for (p = listp; *p != NULL; p = &(*p)->next)
111 if ((*p)->pid == pid)
112 {
113 struct simple_pid_list *next = (*p)->next;
114 xfree (*p);
115 *p = next;
116 return 1;
117 }
118 return 0;
119}
120
121void
122linux_record_stopped_pid (int pid)
123{
124 add_to_pid_list (&stopped_pids, pid);
125}
126
3993f6b1
DJ
127\f
128/* A helper function for linux_test_for_tracefork, called after fork (). */
129
130static void
131linux_tracefork_child (void)
132{
133 int ret;
134
135 ptrace (PTRACE_TRACEME, 0, 0, 0);
136 kill (getpid (), SIGSTOP);
137 fork ();
138 exit (0);
139}
140
141/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
142 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
143 fork tracing, and let it fork. If the process exits, we assume that
144 we can't use TRACEFORK; if we get the fork notification, and we can
145 extract the new child's PID, then we assume that we can. */
146
147static void
148linux_test_for_tracefork (void)
149{
150 int child_pid, ret, status;
151 long second_pid;
152
153 child_pid = fork ();
154 if (child_pid == -1)
155 perror_with_name ("linux_test_for_tracefork: fork");
156
157 if (child_pid == 0)
158 linux_tracefork_child ();
159
160 ret = waitpid (child_pid, &status, 0);
161 if (ret == -1)
162 perror_with_name ("linux_test_for_tracefork: waitpid");
163 else if (ret != child_pid)
164 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
165 if (! WIFSTOPPED (status))
166 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
167
168 linux_supports_tracefork_flag = 0;
169
170 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
171 if (ret != 0)
172 {
173 ptrace (PTRACE_KILL, child_pid, 0, 0);
174 waitpid (child_pid, &status, 0);
175 return;
176 }
177
9016a515
DJ
178 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
179 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
180 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
181 linux_supports_tracevforkdone_flag = (ret == 0);
182
3993f6b1
DJ
183 ptrace (PTRACE_CONT, child_pid, 0, 0);
184 ret = waitpid (child_pid, &status, 0);
185 if (ret == child_pid && WIFSTOPPED (status)
186 && status >> 16 == PTRACE_EVENT_FORK)
187 {
188 second_pid = 0;
189 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
190 if (ret == 0 && second_pid != 0)
191 {
192 int second_status;
193
194 linux_supports_tracefork_flag = 1;
195 waitpid (second_pid, &second_status, 0);
196 ptrace (PTRACE_DETACH, second_pid, 0, 0);
197 }
198 }
199
200 if (WIFSTOPPED (status))
201 {
202 ptrace (PTRACE_DETACH, child_pid, 0, 0);
203 waitpid (child_pid, &status, 0);
204 }
205}
206
207/* Return non-zero iff we have tracefork functionality available.
208 This function also sets linux_supports_tracefork_flag. */
209
210static int
211linux_supports_tracefork (void)
212{
213 if (linux_supports_tracefork_flag == -1)
214 linux_test_for_tracefork ();
215 return linux_supports_tracefork_flag;
216}
217
9016a515
DJ
218static int
219linux_supports_tracevforkdone (void)
220{
221 if (linux_supports_tracefork_flag == -1)
222 linux_test_for_tracefork ();
223 return linux_supports_tracevforkdone_flag;
224}
225
3993f6b1 226\f
4de4c07c
DJ
227void
228linux_enable_event_reporting (ptid_t ptid)
229{
230 int pid = ptid_get_pid (ptid);
231 int options;
232
233 if (! linux_supports_tracefork ())
234 return;
235
a2f23071
DJ
236 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
237 | PTRACE_O_TRACECLONE;
9016a515
DJ
238 if (linux_supports_tracevforkdone ())
239 options |= PTRACE_O_TRACEVFORKDONE;
240
241 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
242 read-only process state. */
4de4c07c
DJ
243
244 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
245}
246
247void
248child_post_attach (int pid)
249{
250 linux_enable_event_reporting (pid_to_ptid (pid));
251}
252
253void
254linux_child_post_startup_inferior (ptid_t ptid)
255{
256 linux_enable_event_reporting (ptid);
257}
258
259#ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
260void
261child_post_startup_inferior (ptid_t ptid)
262{
263 linux_child_post_startup_inferior (ptid);
264}
265#endif
266
3993f6b1 267int
4de4c07c 268child_follow_fork (int follow_child)
3993f6b1 269{
4de4c07c
DJ
270 ptid_t last_ptid;
271 struct target_waitstatus last_status;
9016a515 272 int has_vforked;
4de4c07c
DJ
273 int parent_pid, child_pid;
274
275 get_last_target_status (&last_ptid, &last_status);
9016a515 276 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
4de4c07c
DJ
277 parent_pid = ptid_get_pid (last_ptid);
278 child_pid = last_status.value.related_pid;
279
280 if (! follow_child)
281 {
282 /* We're already attached to the parent, by default. */
283
284 /* Before detaching from the child, remove all breakpoints from
285 it. (This won't actually modify the breakpoint list, but will
286 physically remove the breakpoints from the child.) */
9016a515
DJ
287 /* If we vforked this will remove the breakpoints from the parent
288 also, but they'll be reinserted below. */
4de4c07c
DJ
289 detach_breakpoints (child_pid);
290
291 fprintf_filtered (gdb_stdout,
292 "Detaching after fork from child process %d.\n",
293 child_pid);
294
295 ptrace (PTRACE_DETACH, child_pid, 0, 0);
9016a515
DJ
296
297 if (has_vforked)
298 {
299 if (linux_supports_tracevforkdone ())
300 {
301 int status;
302
303 ptrace (PTRACE_CONT, parent_pid, 0, 0);
304 waitpid (parent_pid, &status, __WALL);
305 if ((status >> 16) != PTRACE_EVENT_VFORKDONE)
306 warning ("Unexpected waitpid result %06x when waiting for "
307 "vfork-done", status);
308 }
309 else
310 {
311 /* We can't insert breakpoints until the child has
312 finished with the shared memory region. We need to
313 wait until that happens. Ideal would be to just
314 call:
315 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
316 - waitpid (parent_pid, &status, __WALL);
317 However, most architectures can't handle a syscall
318 being traced on the way out if it wasn't traced on
319 the way in.
320
321 We might also think to loop, continuing the child
322 until it exits or gets a SIGTRAP. One problem is
323 that the child might call ptrace with PTRACE_TRACEME.
324
325 There's no simple and reliable way to figure out when
326 the vforked child will be done with its copy of the
327 shared memory. We could step it out of the syscall,
328 two instructions, let it go, and then single-step the
329 parent once. When we have hardware single-step, this
330 would work; with software single-step it could still
331 be made to work but we'd have to be able to insert
332 single-step breakpoints in the child, and we'd have
333 to insert -just- the single-step breakpoint in the
334 parent. Very awkward.
335
336 In the end, the best we can do is to make sure it
337 runs for a little while. Hopefully it will be out of
338 range of any breakpoints we reinsert. Usually this
339 is only the single-step breakpoint at vfork's return
340 point. */
341
342 usleep (10000);
343 }
344
345 /* Since we vforked, breakpoints were removed in the parent
346 too. Put them back. */
347 reattach_breakpoints (parent_pid);
348 }
4de4c07c 349 }
3993f6b1 350 else
4de4c07c
DJ
351 {
352 char child_pid_spelling[40];
353
354 /* Needed to keep the breakpoint lists in sync. */
9016a515
DJ
355 if (! has_vforked)
356 detach_breakpoints (child_pid);
4de4c07c
DJ
357
358 /* Before detaching from the parent, remove all breakpoints from it. */
359 remove_breakpoints ();
360
361 fprintf_filtered (gdb_stdout,
362 "Attaching after fork to child process %d.\n",
363 child_pid);
364
9016a515
DJ
365 /* If we're vforking, we may want to hold on to the parent until
366 the child exits or execs. At exec time we can remove the old
367 breakpoints from the parent and detach it; at exit time we
368 could do the same (or even, sneakily, resume debugging it - the
369 child's exec has failed, or something similar).
370
371 This doesn't clean up "properly", because we can't call
372 target_detach, but that's OK; if the current target is "child",
373 then it doesn't need any further cleanups, and lin_lwp will
374 generally not encounter vfork (vfork is defined to fork
375 in libpthread.so).
376
377 The holding part is very easy if we have VFORKDONE events;
378 but keeping track of both processes is beyond GDB at the
379 moment. So we don't expose the parent to the rest of GDB.
380 Instead we quietly hold onto it until such time as we can
381 safely resume it. */
382
383 if (has_vforked)
384 linux_parent_pid = parent_pid;
385 else
386 target_detach (NULL, 0);
4de4c07c
DJ
387
388 inferior_ptid = pid_to_ptid (child_pid);
389 push_target (&child_ops);
390
391 /* Reset breakpoints in the child as appropriate. */
392 follow_inferior_reset_breakpoints ();
393 }
394
395 return 0;
396}
397
398ptid_t
399linux_handle_extended_wait (int pid, int status,
400 struct target_waitstatus *ourstatus)
401{
402 int event = status >> 16;
403
a2f23071
DJ
404 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
405 || event == PTRACE_EVENT_CLONE)
4de4c07c
DJ
406 {
407 unsigned long new_pid;
408 int ret;
409
410 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
411
412 /* If we haven't already seen the new PID stop, wait for it now. */
413 if (! pull_pid_from_list (&stopped_pids, new_pid))
414 {
415 /* The new child has a pending SIGSTOP. We can't affect it until it
a2f23071 416 hits the SIGSTOP, but we're already attached. */
4de4c07c 417 do {
a2f23071
DJ
418 ret = waitpid (new_pid, &status,
419 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
4de4c07c
DJ
420 } while (ret == -1 && errno == EINTR);
421 if (ret == -1)
422 perror_with_name ("waiting for new child");
423 else if (ret != new_pid)
424 internal_error (__FILE__, __LINE__,
425 "wait returned unexpected PID %d", ret);
426 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
427 internal_error (__FILE__, __LINE__,
428 "wait returned unexpected status 0x%x", status);
429 }
430
a2f23071
DJ
431 if (event == PTRACE_EVENT_FORK)
432 ourstatus->kind = TARGET_WAITKIND_FORKED;
433 else if (event == PTRACE_EVENT_VFORK)
434 ourstatus->kind = TARGET_WAITKIND_VFORKED;
435 else
436 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
437
4de4c07c
DJ
438 ourstatus->value.related_pid = new_pid;
439 return inferior_ptid;
440 }
441
9016a515
DJ
442 if (event == PTRACE_EVENT_EXEC)
443 {
444 ourstatus->kind = TARGET_WAITKIND_EXECD;
445 ourstatus->value.execd_pathname
446 = xstrdup (child_pid_to_exec_file (pid));
447
448 if (linux_parent_pid)
449 {
450 detach_breakpoints (linux_parent_pid);
451 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
452
453 linux_parent_pid = 0;
454 }
455
456 return inferior_ptid;
457 }
458
4de4c07c
DJ
459 internal_error (__FILE__, __LINE__,
460 "unknown ptrace event %d", event);
461}
462
463\f
464int
465child_insert_fork_catchpoint (int pid)
466{
467 if (! linux_supports_tracefork ())
3993f6b1 468 error ("Your system does not support fork catchpoints.");
4de4c07c
DJ
469
470 return 0;
3993f6b1
DJ
471}
472
473int
474child_insert_vfork_catchpoint (int pid)
475{
9016a515 476 if (!linux_supports_tracefork ())
3993f6b1 477 error ("Your system does not support vfork catchpoints.");
9016a515
DJ
478
479 return 0;
3993f6b1
DJ
480}
481
482int
483child_insert_exec_catchpoint (int pid)
484{
9016a515 485 if (!linux_supports_tracefork ())
3993f6b1 486 error ("Your system does not support exec catchpoints.");
9016a515
DJ
487
488 return 0;
3993f6b1
DJ
489}
490
4de4c07c
DJ
491void
492kill_inferior (void)
493{
494 int status;
495 int pid = PIDGET (inferior_ptid);
496 struct target_waitstatus last;
497 ptid_t last_ptid;
498 int ret;
499
500 if (pid == 0)
501 return;
502
503 /* If we're stopped while forking and we haven't followed yet, kill the
504 other task. We need to do this first because the parent will be
505 sleeping if this is a vfork. */
506
507 get_last_target_status (&last_ptid, &last);
3993f6b1 508
4de4c07c
DJ
509 if (last.kind == TARGET_WAITKIND_FORKED
510 || last.kind == TARGET_WAITKIND_VFORKED)
511 {
de9a9e51 512 ptrace (PT_KILL, last.value.related_pid, 0, 0);
4de4c07c
DJ
513 ptrace_wait (null_ptid, &status);
514 }
515
516 /* Kill the current process. */
de9a9e51 517 ptrace (PT_KILL, pid, 0, 0);
4de4c07c
DJ
518 ret = ptrace_wait (null_ptid, &status);
519
520 /* We might get a SIGCHLD instead of an exit status. This is
521 aggravated by the first kill above - a child has just died. */
522
523 while (ret == pid && WIFSTOPPED (status))
524 {
de9a9e51 525 ptrace (PT_KILL, pid, 0, 0);
4de4c07c
DJ
526 ret = ptrace_wait (null_ptid, &status);
527 }
528
529 target_mourn_inferior ();
530}
d6b0e80f
AC
531
532/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
533 are processes sharing the same VM space. A multi-threaded process
534 is basically a group of such processes. However, such a grouping
535 is almost entirely a user-space issue; the kernel doesn't enforce
536 such a grouping at all (this might change in the future). In
537 general, we'll rely on the threads library (i.e. the GNU/Linux
538 Threads library) to provide such a grouping.
539
540 It is perfectly well possible to write a multi-threaded application
541 without the assistance of a threads library, by using the clone
542 system call directly. This module should be able to give some
543 rudimentary support for debugging such applications if developers
544 specify the CLONE_PTRACE flag in the clone system call, and are
545 using the Linux kernel 2.4 or above.
546
547 Note that there are some peculiarities in GNU/Linux that affect
548 this code:
549
550 - In general one should specify the __WCLONE flag to waitpid in
551 order to make it report events for any of the cloned processes
552 (and leave it out for the initial process). However, if a cloned
553 process has exited the exit status is only reported if the
554 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
555 we cannot use it since GDB must work on older systems too.
556
557 - When a traced, cloned process exits and is waited for by the
558 debugger, the kernel reassigns it to the original parent and
559 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
560 library doesn't notice this, which leads to the "zombie problem":
561 When debugged a multi-threaded process that spawns a lot of
562 threads will run out of processes, even if the threads exit,
563 because the "zombies" stay around. */
564
565/* List of known LWPs. */
566static struct lwp_info *lwp_list;
567
568/* Number of LWPs in the list. */
569static int num_lwps;
570
571/* Non-zero if we're running in "threaded" mode. */
572static int threaded;
573\f
574
575#define GET_LWP(ptid) ptid_get_lwp (ptid)
576#define GET_PID(ptid) ptid_get_pid (ptid)
577#define is_lwp(ptid) (GET_LWP (ptid) != 0)
578#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
579
580/* If the last reported event was a SIGTRAP, this variable is set to
581 the process id of the LWP/thread that got it. */
582ptid_t trap_ptid;
583\f
584
585/* This module's target-specific operations. */
586static struct target_ops linux_nat_ops;
587
588/* The standard child operations. */
589extern struct target_ops child_ops;
590
591/* Since we cannot wait (in linux_nat_wait) for the initial process and
592 any cloned processes with a single call to waitpid, we have to use
593 the WNOHANG flag and call waitpid in a loop. To optimize
594 things a bit we use `sigsuspend' to wake us up when a process has
595 something to report (it will send us a SIGCHLD if it has). To make
596 this work we have to juggle with the signal mask. We save the
597 original signal mask such that we can restore it before creating a
598 new process in order to avoid blocking certain signals in the
599 inferior. We then block SIGCHLD during the waitpid/sigsuspend
600 loop. */
601
602/* Original signal mask. */
603static sigset_t normal_mask;
604
605/* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
606 _initialize_linux_nat. */
607static sigset_t suspend_mask;
608
609/* Signals to block to make that sigsuspend work. */
610static sigset_t blocked_mask;
611\f
612
613/* Prototypes for local functions. */
614static int stop_wait_callback (struct lwp_info *lp, void *data);
615static int linux_nat_thread_alive (ptid_t ptid);
616\f
617/* Convert wait status STATUS to a string. Used for printing debug
618 messages only. */
619
620static char *
621status_to_str (int status)
622{
623 static char buf[64];
624
625 if (WIFSTOPPED (status))
626 snprintf (buf, sizeof (buf), "%s (stopped)",
627 strsignal (WSTOPSIG (status)));
628 else if (WIFSIGNALED (status))
629 snprintf (buf, sizeof (buf), "%s (terminated)",
630 strsignal (WSTOPSIG (status)));
631 else
632 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
633
634 return buf;
635}
636
637/* Initialize the list of LWPs. Note that this module, contrary to
638 what GDB's generic threads layer does for its thread list,
639 re-initializes the LWP lists whenever we mourn or detach (which
640 doesn't involve mourning) the inferior. */
641
642static void
643init_lwp_list (void)
644{
645 struct lwp_info *lp, *lpnext;
646
647 for (lp = lwp_list; lp; lp = lpnext)
648 {
649 lpnext = lp->next;
650 xfree (lp);
651 }
652
653 lwp_list = NULL;
654 num_lwps = 0;
655 threaded = 0;
656}
657
658/* Add the LWP specified by PID to the list. If this causes the
659 number of LWPs to become larger than one, go into "threaded" mode.
660 Return a pointer to the structure describing the new LWP. */
661
662static struct lwp_info *
663add_lwp (ptid_t ptid)
664{
665 struct lwp_info *lp;
666
667 gdb_assert (is_lwp (ptid));
668
669 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
670
671 memset (lp, 0, sizeof (struct lwp_info));
672
673 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
674
675 lp->ptid = ptid;
676
677 lp->next = lwp_list;
678 lwp_list = lp;
679 if (++num_lwps > 1)
680 threaded = 1;
681
682 return lp;
683}
684
685/* Remove the LWP specified by PID from the list. */
686
687static void
688delete_lwp (ptid_t ptid)
689{
690 struct lwp_info *lp, *lpprev;
691
692 lpprev = NULL;
693
694 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
695 if (ptid_equal (lp->ptid, ptid))
696 break;
697
698 if (!lp)
699 return;
700
701 /* We don't go back to "non-threaded" mode if the number of threads
702 becomes less than two. */
703 num_lwps--;
704
705 if (lpprev)
706 lpprev->next = lp->next;
707 else
708 lwp_list = lp->next;
709
710 xfree (lp);
711}
712
713/* Return a pointer to the structure describing the LWP corresponding
714 to PID. If no corresponding LWP could be found, return NULL. */
715
716static struct lwp_info *
717find_lwp_pid (ptid_t ptid)
718{
719 struct lwp_info *lp;
720 int lwp;
721
722 if (is_lwp (ptid))
723 lwp = GET_LWP (ptid);
724 else
725 lwp = GET_PID (ptid);
726
727 for (lp = lwp_list; lp; lp = lp->next)
728 if (lwp == GET_LWP (lp->ptid))
729 return lp;
730
731 return NULL;
732}
733
734/* Call CALLBACK with its second argument set to DATA for every LWP in
735 the list. If CALLBACK returns 1 for a particular LWP, return a
736 pointer to the structure describing that LWP immediately.
737 Otherwise return NULL. */
738
739struct lwp_info *
740iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
741{
742 struct lwp_info *lp, *lpnext;
743
744 for (lp = lwp_list; lp; lp = lpnext)
745 {
746 lpnext = lp->next;
747 if ((*callback) (lp, data))
748 return lp;
749 }
750
751 return NULL;
752}
753
754/* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
755 a message telling the user that a new LWP has been added to the
756 process. */
757
758void
759lin_lwp_attach_lwp (ptid_t ptid, int verbose)
760{
761 struct lwp_info *lp, *found_lp;
762
763 gdb_assert (is_lwp (ptid));
764
765 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
766 to interrupt either the ptrace() or waitpid() calls below. */
767 if (!sigismember (&blocked_mask, SIGCHLD))
768 {
769 sigaddset (&blocked_mask, SIGCHLD);
770 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
771 }
772
773 if (verbose)
774 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
775
776 found_lp = lp = find_lwp_pid (ptid);
777 if (lp == NULL)
778 lp = add_lwp (ptid);
779
780 /* We assume that we're already attached to any LWP that has an id
781 equal to the overall process id, and to any LWP that is already
782 in our list of LWPs. If we're not seeing exit events from threads
783 and we've had PID wraparound since we last tried to stop all threads,
784 this assumption might be wrong; fortunately, this is very unlikely
785 to happen. */
786 if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL)
787 {
788 pid_t pid;
789 int status;
790
791 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
792 error ("Can't attach %s: %s", target_pid_to_str (ptid),
793 safe_strerror (errno));
794
795 if (debug_linux_nat)
796 fprintf_unfiltered (gdb_stdlog,
797 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
798 target_pid_to_str (ptid));
799
800 pid = waitpid (GET_LWP (ptid), &status, 0);
801 if (pid == -1 && errno == ECHILD)
802 {
803 /* Try again with __WCLONE to check cloned processes. */
804 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
805 lp->cloned = 1;
806 }
807
808 gdb_assert (pid == GET_LWP (ptid)
809 && WIFSTOPPED (status) && WSTOPSIG (status));
810
811 child_post_attach (pid);
812
813 lp->stopped = 1;
814
815 if (debug_linux_nat)
816 {
817 fprintf_unfiltered (gdb_stdlog,
818 "LLAL: waitpid %s received %s\n",
819 target_pid_to_str (ptid),
820 status_to_str (status));
821 }
822 }
823 else
824 {
825 /* We assume that the LWP representing the original process is
826 already stopped. Mark it as stopped in the data structure
827 that the linux ptrace layer uses to keep track of threads.
828 Note that this won't have already been done since the main
829 thread will have, we assume, been stopped by an attach from a
830 different layer. */
831 lp->stopped = 1;
832 }
833}
834
835static void
836linux_nat_attach (char *args, int from_tty)
837{
838 struct lwp_info *lp;
839 pid_t pid;
840 int status;
841
842 /* FIXME: We should probably accept a list of process id's, and
843 attach all of them. */
844 child_ops.to_attach (args, from_tty);
845
846 /* Add the initial process as the first LWP to the list. */
847 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
848
849 /* Make sure the initial process is stopped. The user-level threads
850 layer might want to poke around in the inferior, and that won't
851 work if things haven't stabilized yet. */
852 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
853 if (pid == -1 && errno == ECHILD)
854 {
855 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
856
857 /* Try again with __WCLONE to check cloned processes. */
858 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
859 lp->cloned = 1;
860 }
861
862 gdb_assert (pid == GET_PID (inferior_ptid)
863 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
864
865 lp->stopped = 1;
866
867 /* Fake the SIGSTOP that core GDB expects. */
868 lp->status = W_STOPCODE (SIGSTOP);
869 lp->resumed = 1;
870 if (debug_linux_nat)
871 {
872 fprintf_unfiltered (gdb_stdlog,
873 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
874 }
875}
876
877static int
878detach_callback (struct lwp_info *lp, void *data)
879{
880 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
881
882 if (debug_linux_nat && lp->status)
883 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
884 strsignal (WSTOPSIG (lp->status)),
885 target_pid_to_str (lp->ptid));
886
887 while (lp->signalled && lp->stopped)
888 {
889 errno = 0;
890 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
891 WSTOPSIG (lp->status)) < 0)
892 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
893 safe_strerror (errno));
894
895 if (debug_linux_nat)
896 fprintf_unfiltered (gdb_stdlog,
897 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
898 target_pid_to_str (lp->ptid),
899 status_to_str (lp->status));
900
901 lp->stopped = 0;
902 lp->signalled = 0;
903 lp->status = 0;
904 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
905 here. But since lp->signalled was cleared above,
906 stop_wait_callback didn't do anything; the process was left
907 running. Shouldn't we be waiting for it to stop?
908 I've removed the call, since stop_wait_callback now does do
909 something when called with lp->signalled == 0. */
910
911 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
912 }
913
914 /* We don't actually detach from the LWP that has an id equal to the
915 overall process id just yet. */
916 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
917 {
918 errno = 0;
919 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
920 WSTOPSIG (lp->status)) < 0)
921 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
922 safe_strerror (errno));
923
924 if (debug_linux_nat)
925 fprintf_unfiltered (gdb_stdlog,
926 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
927 target_pid_to_str (lp->ptid),
928 strsignal (WSTOPSIG (lp->status)));
929
930 delete_lwp (lp->ptid);
931 }
932
933 return 0;
934}
935
936static void
937linux_nat_detach (char *args, int from_tty)
938{
939 iterate_over_lwps (detach_callback, NULL);
940
941 /* Only the initial process should be left right now. */
942 gdb_assert (num_lwps == 1);
943
944 trap_ptid = null_ptid;
945
946 /* Destroy LWP info; it's no longer valid. */
947 init_lwp_list ();
948
949 /* Restore the original signal mask. */
950 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
951 sigemptyset (&blocked_mask);
952
953 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
954 child_ops.to_detach (args, from_tty);
955}
956
957/* Resume LP. */
958
959static int
960resume_callback (struct lwp_info *lp, void *data)
961{
962 if (lp->stopped && lp->status == 0)
963 {
964 struct thread_info *tp;
965
966 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
967 if (debug_linux_nat)
968 fprintf_unfiltered (gdb_stdlog,
969 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
970 target_pid_to_str (lp->ptid));
971 lp->stopped = 0;
972 lp->step = 0;
973 }
974
975 return 0;
976}
977
978static int
979resume_clear_callback (struct lwp_info *lp, void *data)
980{
981 lp->resumed = 0;
982 return 0;
983}
984
985static int
986resume_set_callback (struct lwp_info *lp, void *data)
987{
988 lp->resumed = 1;
989 return 0;
990}
991
992static void
993linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
994{
995 struct lwp_info *lp;
996 int resume_all;
997
998 /* A specific PTID means `step only this process id'. */
999 resume_all = (PIDGET (ptid) == -1);
1000
1001 if (resume_all)
1002 iterate_over_lwps (resume_set_callback, NULL);
1003 else
1004 iterate_over_lwps (resume_clear_callback, NULL);
1005
1006 /* If PID is -1, it's the current inferior that should be
1007 handled specially. */
1008 if (PIDGET (ptid) == -1)
1009 ptid = inferior_ptid;
1010
1011 lp = find_lwp_pid (ptid);
1012 if (lp)
1013 {
1014 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1015
1016 /* Remember if we're stepping. */
1017 lp->step = step;
1018
1019 /* Mark this LWP as resumed. */
1020 lp->resumed = 1;
1021
1022 /* If we have a pending wait status for this thread, there is no
1023 point in resuming the process. */
1024 if (lp->status)
1025 {
1026 /* FIXME: What should we do if we are supposed to continue
1027 this thread with a signal? */
1028 gdb_assert (signo == TARGET_SIGNAL_0);
1029 return;
1030 }
1031
1032 /* Mark LWP as not stopped to prevent it from being continued by
1033 resume_callback. */
1034 lp->stopped = 0;
1035 }
1036
1037 if (resume_all)
1038 iterate_over_lwps (resume_callback, NULL);
1039
1040 child_resume (ptid, step, signo);
1041 if (debug_linux_nat)
1042 fprintf_unfiltered (gdb_stdlog,
1043 "LLR: %s %s, %s (resume event thread)\n",
1044 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1045 target_pid_to_str (ptid),
1046 signo ? strsignal (signo) : "0");
1047}
1048
1049/* Issue kill to specified lwp. */
1050
1051static int tkill_failed;
1052
1053static int
1054kill_lwp (int lwpid, int signo)
1055{
1056 errno = 0;
1057
1058/* Use tkill, if possible, in case we are using nptl threads. If tkill
1059 fails, then we are not using nptl threads and we should be using kill. */
1060
1061#ifdef HAVE_TKILL_SYSCALL
1062 if (!tkill_failed)
1063 {
1064 int ret = syscall (__NR_tkill, lwpid, signo);
1065 if (errno != ENOSYS)
1066 return ret;
1067 errno = 0;
1068 tkill_failed = 1;
1069 }
1070#endif
1071
1072 return kill (lwpid, signo);
1073}
1074
1075/* Handle a GNU/Linux extended wait response. Most of the work we
1076 just pass off to linux_handle_extended_wait, but if it reports a
1077 clone event we need to add the new LWP to our list (and not report
1078 the trap to higher layers). This function returns non-zero if
1079 the event should be ignored and we should wait again. */
1080
1081static int
1082linux_nat_handle_extended (struct lwp_info *lp, int status)
1083{
1084 linux_handle_extended_wait (GET_LWP (lp->ptid), status,
1085 &lp->waitstatus);
1086
1087 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1088 if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS)
1089 {
1090 struct lwp_info *new_lp;
1091 new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid,
1092 GET_PID (inferior_ptid)));
1093 new_lp->cloned = 1;
1094 new_lp->stopped = 1;
1095
1096 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1097
1098 if (debug_linux_nat)
1099 fprintf_unfiltered (gdb_stdlog,
1100 "LLHE: Got clone event from LWP %ld, resuming\n",
1101 GET_LWP (lp->ptid));
1102 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1103
1104 return 1;
1105 }
1106
1107 return 0;
1108}
1109
1110/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1111 exited. */
1112
1113static int
1114wait_lwp (struct lwp_info *lp)
1115{
1116 pid_t pid;
1117 int status;
1118 int thread_dead = 0;
1119
1120 gdb_assert (!lp->stopped);
1121 gdb_assert (lp->status == 0);
1122
1123 pid = waitpid (GET_LWP (lp->ptid), &status, 0);
1124 if (pid == -1 && errno == ECHILD)
1125 {
1126 pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
1127 if (pid == -1 && errno == ECHILD)
1128 {
1129 /* The thread has previously exited. We need to delete it
1130 now because, for some vendor 2.4 kernels with NPTL
1131 support backported, there won't be an exit event unless
1132 it is the main thread. 2.6 kernels will report an exit
1133 event for each thread that exits, as expected. */
1134 thread_dead = 1;
1135 if (debug_linux_nat)
1136 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
1137 target_pid_to_str (lp->ptid));
1138 }
1139 }
1140
1141 if (!thread_dead)
1142 {
1143 gdb_assert (pid == GET_LWP (lp->ptid));
1144
1145 if (debug_linux_nat)
1146 {
1147 fprintf_unfiltered (gdb_stdlog,
1148 "WL: waitpid %s received %s\n",
1149 target_pid_to_str (lp->ptid),
1150 status_to_str (status));
1151 }
1152 }
1153
1154 /* Check if the thread has exited. */
1155 if (WIFEXITED (status) || WIFSIGNALED (status))
1156 {
1157 thread_dead = 1;
1158 if (debug_linux_nat)
1159 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
1160 target_pid_to_str (lp->ptid));
1161 }
1162
1163 if (thread_dead)
1164 {
1165 if (in_thread_list (lp->ptid))
1166 {
1167 /* Core GDB cannot deal with us deleting the current thread. */
1168 if (!ptid_equal (lp->ptid, inferior_ptid))
1169 delete_thread (lp->ptid);
1170 printf_unfiltered ("[%s exited]\n",
1171 target_pid_to_str (lp->ptid));
1172 }
1173
1174 delete_lwp (lp->ptid);
1175 return 0;
1176 }
1177
1178 gdb_assert (WIFSTOPPED (status));
1179
1180 /* Handle GNU/Linux's extended waitstatus for trace events. */
1181 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1182 {
1183 if (debug_linux_nat)
1184 fprintf_unfiltered (gdb_stdlog,
1185 "WL: Handling extended status 0x%06x\n",
1186 status);
1187 if (linux_nat_handle_extended (lp, status))
1188 return wait_lwp (lp);
1189 }
1190
1191 return status;
1192}
1193
1194/* Send a SIGSTOP to LP. */
1195
1196static int
1197stop_callback (struct lwp_info *lp, void *data)
1198{
1199 if (!lp->stopped && !lp->signalled)
1200 {
1201 int ret;
1202
1203 if (debug_linux_nat)
1204 {
1205 fprintf_unfiltered (gdb_stdlog,
1206 "SC: kill %s **<SIGSTOP>**\n",
1207 target_pid_to_str (lp->ptid));
1208 }
1209 errno = 0;
1210 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
1211 if (debug_linux_nat)
1212 {
1213 fprintf_unfiltered (gdb_stdlog,
1214 "SC: lwp kill %d %s\n",
1215 ret,
1216 errno ? safe_strerror (errno) : "ERRNO-OK");
1217 }
1218
1219 lp->signalled = 1;
1220 gdb_assert (lp->status == 0);
1221 }
1222
1223 return 0;
1224}
1225
1226/* Wait until LP is stopped. If DATA is non-null it is interpreted as
1227 a pointer to a set of signals to be flushed immediately. */
1228
1229static int
1230stop_wait_callback (struct lwp_info *lp, void *data)
1231{
1232 sigset_t *flush_mask = data;
1233
1234 if (!lp->stopped)
1235 {
1236 int status;
1237
1238 status = wait_lwp (lp);
1239 if (status == 0)
1240 return 0;
1241
1242 /* Ignore any signals in FLUSH_MASK. */
1243 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
1244 {
1245 if (!lp->signalled)
1246 {
1247 lp->stopped = 1;
1248 return 0;
1249 }
1250
1251 errno = 0;
1252 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1253 if (debug_linux_nat)
1254 fprintf_unfiltered (gdb_stdlog,
1255 "PTRACE_CONT %s, 0, 0 (%s)\n",
1256 target_pid_to_str (lp->ptid),
1257 errno ? safe_strerror (errno) : "OK");
1258
1259 return stop_wait_callback (lp, flush_mask);
1260 }
1261
1262 if (WSTOPSIG (status) != SIGSTOP)
1263 {
1264 if (WSTOPSIG (status) == SIGTRAP)
1265 {
1266 /* If a LWP other than the LWP that we're reporting an
1267 event for has hit a GDB breakpoint (as opposed to
1268 some random trap signal), then just arrange for it to
1269 hit it again later. We don't keep the SIGTRAP status
1270 and don't forward the SIGTRAP signal to the LWP. We
1271 will handle the current event, eventually we will
1272 resume all LWPs, and this one will get its breakpoint
1273 trap again.
1274
1275 If we do not do this, then we run the risk that the
1276 user will delete or disable the breakpoint, but the
1277 thread will have already tripped on it. */
1278
1279 /* Now resume this LWP and get the SIGSTOP event. */
1280 errno = 0;
1281 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1282 if (debug_linux_nat)
1283 {
1284 fprintf_unfiltered (gdb_stdlog,
1285 "PTRACE_CONT %s, 0, 0 (%s)\n",
1286 target_pid_to_str (lp->ptid),
1287 errno ? safe_strerror (errno) : "OK");
1288
1289 fprintf_unfiltered (gdb_stdlog,
1290 "SWC: Candidate SIGTRAP event in %s\n",
1291 target_pid_to_str (lp->ptid));
1292 }
1293 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1294 stop_wait_callback (lp, data);
1295 /* If there's another event, throw it back into the queue. */
1296 if (lp->status)
1297 {
1298 if (debug_linux_nat)
1299 {
1300 fprintf_unfiltered (gdb_stdlog,
1301 "SWC: kill %s, %s\n",
1302 target_pid_to_str (lp->ptid),
1303 status_to_str ((int) status));
1304 }
1305 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
1306 }
1307 /* Save the sigtrap event. */
1308 lp->status = status;
1309 return 0;
1310 }
1311 else
1312 {
1313 /* The thread was stopped with a signal other than
1314 SIGSTOP, and didn't accidentally trip a breakpoint. */
1315
1316 if (debug_linux_nat)
1317 {
1318 fprintf_unfiltered (gdb_stdlog,
1319 "SWC: Pending event %s in %s\n",
1320 status_to_str ((int) status),
1321 target_pid_to_str (lp->ptid));
1322 }
1323 /* Now resume this LWP and get the SIGSTOP event. */
1324 errno = 0;
1325 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1326 if (debug_linux_nat)
1327 fprintf_unfiltered (gdb_stdlog,
1328 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1329 target_pid_to_str (lp->ptid),
1330 errno ? safe_strerror (errno) : "OK");
1331
1332 /* Hold this event/waitstatus while we check to see if
1333 there are any more (we still want to get that SIGSTOP). */
1334 stop_wait_callback (lp, data);
1335 /* If the lp->status field is still empty, use it to hold
1336 this event. If not, then this event must be returned
1337 to the event queue of the LWP. */
1338 if (lp->status == 0)
1339 lp->status = status;
1340 else
1341 {
1342 if (debug_linux_nat)
1343 {
1344 fprintf_unfiltered (gdb_stdlog,
1345 "SWC: kill %s, %s\n",
1346 target_pid_to_str (lp->ptid),
1347 status_to_str ((int) status));
1348 }
1349 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
1350 }
1351 return 0;
1352 }
1353 }
1354 else
1355 {
1356 /* We caught the SIGSTOP that we intended to catch, so
1357 there's no SIGSTOP pending. */
1358 lp->stopped = 1;
1359 lp->signalled = 0;
1360 }
1361 }
1362
1363 return 0;
1364}
1365
1366/* Check whether PID has any pending signals in FLUSH_MASK. If so set
1367 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1368
1369static int
1370linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
1371{
1372 sigset_t blocked, ignored;
1373 int i;
1374
1375 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
1376
1377 if (!flush_mask)
1378 return 0;
1379
1380 for (i = 1; i < NSIG; i++)
1381 if (sigismember (pending, i))
1382 if (!sigismember (flush_mask, i)
1383 || sigismember (&blocked, i)
1384 || sigismember (&ignored, i))
1385 sigdelset (pending, i);
1386
1387 if (sigisemptyset (pending))
1388 return 0;
1389
1390 return 1;
1391}
1392
1393/* DATA is interpreted as a mask of signals to flush. If LP has
1394 signals pending, and they are all in the flush mask, then arrange
1395 to flush them. LP should be stopped, as should all other threads
1396 it might share a signal queue with. */
1397
1398static int
1399flush_callback (struct lwp_info *lp, void *data)
1400{
1401 sigset_t *flush_mask = data;
1402 sigset_t pending, intersection, blocked, ignored;
1403 int pid, status;
1404
1405 /* Normally, when an LWP exits, it is removed from the LWP list. The
1406 last LWP isn't removed till later, however. So if there is only
1407 one LWP on the list, make sure it's alive. */
1408 if (lwp_list == lp && lp->next == NULL)
1409 if (!linux_nat_thread_alive (lp->ptid))
1410 return 0;
1411
1412 /* Just because the LWP is stopped doesn't mean that new signals
1413 can't arrive from outside, so this function must be careful of
1414 race conditions. However, because all threads are stopped, we
1415 can assume that the pending mask will not shrink unless we resume
1416 the LWP, and that it will then get another signal. We can't
1417 control which one, however. */
1418
1419 if (lp->status)
1420 {
1421 if (debug_linux_nat)
1422 printf_unfiltered ("FC: LP has pending status %06x\n", lp->status);
1423 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
1424 lp->status = 0;
1425 }
1426
1427 while (linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
1428 {
1429 int ret;
1430
1431 errno = 0;
1432 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1433 if (debug_linux_nat)
1434 fprintf_unfiltered (gdb_stderr,
1435 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
1436
1437 lp->stopped = 0;
1438 stop_wait_callback (lp, flush_mask);
1439 if (debug_linux_nat)
1440 fprintf_unfiltered (gdb_stderr,
1441 "FC: Wait finished; saved status is %d\n",
1442 lp->status);
1443 }
1444
1445 return 0;
1446}
1447
1448/* Return non-zero if LP has a wait status pending. */
1449
1450static int
1451status_callback (struct lwp_info *lp, void *data)
1452{
1453 /* Only report a pending wait status if we pretend that this has
1454 indeed been resumed. */
1455 return (lp->status != 0 && lp->resumed);
1456}
1457
1458/* Return non-zero if LP isn't stopped. */
1459
1460static int
1461running_callback (struct lwp_info *lp, void *data)
1462{
1463 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
1464}
1465
1466/* Count the LWP's that have had events. */
1467
1468static int
1469count_events_callback (struct lwp_info *lp, void *data)
1470{
1471 int *count = data;
1472
1473 gdb_assert (count != NULL);
1474
1475 /* Count only LWPs that have a SIGTRAP event pending. */
1476 if (lp->status != 0
1477 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1478 (*count)++;
1479
1480 return 0;
1481}
1482
1483/* Select the LWP (if any) that is currently being single-stepped. */
1484
1485static int
1486select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1487{
1488 if (lp->step && lp->status != 0)
1489 return 1;
1490 else
1491 return 0;
1492}
1493
1494/* Select the Nth LWP that has had a SIGTRAP event. */
1495
1496static int
1497select_event_lwp_callback (struct lwp_info *lp, void *data)
1498{
1499 int *selector = data;
1500
1501 gdb_assert (selector != NULL);
1502
1503 /* Select only LWPs that have a SIGTRAP event pending. */
1504 if (lp->status != 0
1505 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1506 if ((*selector)-- == 0)
1507 return 1;
1508
1509 return 0;
1510}
1511
1512static int
1513cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1514{
1515 struct lwp_info *event_lp = data;
1516
1517 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1518 if (lp == event_lp)
1519 return 0;
1520
1521 /* If a LWP other than the LWP that we're reporting an event for has
1522 hit a GDB breakpoint (as opposed to some random trap signal),
1523 then just arrange for it to hit it again later. We don't keep
1524 the SIGTRAP status and don't forward the SIGTRAP signal to the
1525 LWP. We will handle the current event, eventually we will resume
1526 all LWPs, and this one will get its breakpoint trap again.
1527
1528 If we do not do this, then we run the risk that the user will
1529 delete or disable the breakpoint, but the LWP will have already
1530 tripped on it. */
1531
1532 if (lp->status != 0
1533 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1534 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1535 DECR_PC_AFTER_BREAK))
1536 {
1537 if (debug_linux_nat)
1538 fprintf_unfiltered (gdb_stdlog,
1539 "CBC: Push back breakpoint for %s\n",
1540 target_pid_to_str (lp->ptid));
1541
1542 /* Back up the PC if necessary. */
1543 if (DECR_PC_AFTER_BREAK)
1544 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1545
1546 /* Throw away the SIGTRAP. */
1547 lp->status = 0;
1548 }
1549
1550 return 0;
1551}
1552
1553/* Select one LWP out of those that have events pending. */
1554
1555static void
1556select_event_lwp (struct lwp_info **orig_lp, int *status)
1557{
1558 int num_events = 0;
1559 int random_selector;
1560 struct lwp_info *event_lp;
1561
1562 /* Record the wait status for the origional LWP. */
1563 (*orig_lp)->status = *status;
1564
1565 /* Give preference to any LWP that is being single-stepped. */
1566 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1567 if (event_lp != NULL)
1568 {
1569 if (debug_linux_nat)
1570 fprintf_unfiltered (gdb_stdlog,
1571 "SEL: Select single-step %s\n",
1572 target_pid_to_str (event_lp->ptid));
1573 }
1574 else
1575 {
1576 /* No single-stepping LWP. Select one at random, out of those
1577 which have had SIGTRAP events. */
1578
1579 /* First see how many SIGTRAP events we have. */
1580 iterate_over_lwps (count_events_callback, &num_events);
1581
1582 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1583 random_selector = (int)
1584 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1585
1586 if (debug_linux_nat && num_events > 1)
1587 fprintf_unfiltered (gdb_stdlog,
1588 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1589 num_events, random_selector);
1590
1591 event_lp = iterate_over_lwps (select_event_lwp_callback,
1592 &random_selector);
1593 }
1594
1595 if (event_lp != NULL)
1596 {
1597 /* Switch the event LWP. */
1598 *orig_lp = event_lp;
1599 *status = event_lp->status;
1600 }
1601
1602 /* Flush the wait status for the event LWP. */
1603 (*orig_lp)->status = 0;
1604}
1605
1606/* Return non-zero if LP has been resumed. */
1607
1608static int
1609resumed_callback (struct lwp_info *lp, void *data)
1610{
1611 return lp->resumed;
1612}
1613
1614#ifdef CHILD_WAIT
1615
1616/* We need to override child_wait to support attaching to cloned
1617 processes, since a normal wait (as done by the default version)
1618 ignores those processes. */
1619
1620/* Wait for child PTID to do something. Return id of the child,
1621 minus_one_ptid in case of error; store status into *OURSTATUS. */
1622
1623ptid_t
1624child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1625{
1626 int save_errno;
1627 int status;
1628 pid_t pid;
1629
1630 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1631
1632 do
1633 {
1634 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1635 attached process. */
1636 set_sigio_trap ();
1637
1638 pid = waitpid (GET_PID (ptid), &status, 0);
1639 if (pid == -1 && errno == ECHILD)
1640 /* Try again with __WCLONE to check cloned processes. */
1641 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1642
1643 if (debug_linux_nat)
1644 {
1645 fprintf_unfiltered (gdb_stdlog,
1646 "CW: waitpid %ld received %s\n",
1647 (long) pid, status_to_str (status));
1648 }
1649
1650 save_errno = errno;
1651
1652 /* Make sure we don't report an event for the exit of the
1653 original program, if we've detached from it. */
1654 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1655 {
1656 pid = -1;
1657 save_errno = EINTR;
1658 }
1659
1660 /* Check for stop events reported by a process we didn't already
1661 know about - in this case, anything other than inferior_ptid.
1662
1663 If we're expecting to receive stopped processes after fork,
1664 vfork, and clone events, then we'll just add the new one to
1665 our list and go back to waiting for the event to be reported
1666 - the stopped process might be returned from waitpid before
1667 or after the event is. If we want to handle debugging of
1668 CLONE_PTRACE processes we need to do more here, i.e. switch
1669 to multi-threaded mode. */
1670 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1671 && pid != GET_PID (inferior_ptid))
1672 {
1673 linux_record_stopped_pid (pid);
1674 pid = -1;
1675 save_errno = EINTR;
1676 }
1677
1678 /* Handle GNU/Linux's extended waitstatus for trace events. */
1679 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
1680 && status >> 16 != 0)
1681 {
1682 linux_handle_extended_wait (pid, status, ourstatus);
1683
1684 /* If we see a clone event, detach the child, and don't
1685 report the event. It would be nice to offer some way to
1686 switch into a non-thread-db based threaded mode at this
1687 point. */
1688 if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS)
1689 {
1690 ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0);
1691 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1692 ptrace (PTRACE_CONT, pid, 0, 0);
1693 pid = -1;
1694 save_errno = EINTR;
1695 }
1696 }
1697
1698 clear_sigio_trap ();
1699 clear_sigint_trap ();
1700 }
1701 while (pid == -1 && save_errno == EINTR);
1702
1703 if (pid == -1)
1704 {
1705 warning ("Child process unexpectedly missing: %s",
1706 safe_strerror (errno));
1707
1708 /* Claim it exited with unknown signal. */
1709 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1710 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1711 return minus_one_ptid;
1712 }
1713
1714 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1715 store_waitstatus (ourstatus, status);
1716
1717 return pid_to_ptid (pid);
1718}
1719
1720#endif
1721
1722/* Stop an active thread, verify it still exists, then resume it. */
1723
1724static int
1725stop_and_resume_callback (struct lwp_info *lp, void *data)
1726{
1727 struct lwp_info *ptr;
1728
1729 if (!lp->stopped && !lp->signalled)
1730 {
1731 stop_callback (lp, NULL);
1732 stop_wait_callback (lp, NULL);
1733 /* Resume if the lwp still exists. */
1734 for (ptr = lwp_list; ptr; ptr = ptr->next)
1735 if (lp == ptr)
1736 {
1737 resume_callback (lp, NULL);
1738 resume_set_callback (lp, NULL);
1739 }
1740 }
1741 return 0;
1742}
1743
1744static ptid_t
1745linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1746{
1747 struct lwp_info *lp = NULL;
1748 int options = 0;
1749 int status = 0;
1750 pid_t pid = PIDGET (ptid);
1751 sigset_t flush_mask;
1752
1753 sigemptyset (&flush_mask);
1754
1755 /* Make sure SIGCHLD is blocked. */
1756 if (!sigismember (&blocked_mask, SIGCHLD))
1757 {
1758 sigaddset (&blocked_mask, SIGCHLD);
1759 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1760 }
1761
1762retry:
1763
1764 /* Make sure there is at least one LWP that has been resumed, at
1765 least if there are any LWPs at all. */
1766 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1767
1768 /* First check if there is a LWP with a wait status pending. */
1769 if (pid == -1)
1770 {
1771 /* Any LWP that's been resumed will do. */
1772 lp = iterate_over_lwps (status_callback, NULL);
1773 if (lp)
1774 {
1775 status = lp->status;
1776 lp->status = 0;
1777
1778 if (debug_linux_nat && status)
1779 fprintf_unfiltered (gdb_stdlog,
1780 "LLW: Using pending wait status %s for %s.\n",
1781 status_to_str (status),
1782 target_pid_to_str (lp->ptid));
1783 }
1784
1785 /* But if we don't fine one, we'll have to wait, and check both
1786 cloned and uncloned processes. We start with the cloned
1787 processes. */
1788 options = __WCLONE | WNOHANG;
1789 }
1790 else if (is_lwp (ptid))
1791 {
1792 if (debug_linux_nat)
1793 fprintf_unfiltered (gdb_stdlog,
1794 "LLW: Waiting for specific LWP %s.\n",
1795 target_pid_to_str (ptid));
1796
1797 /* We have a specific LWP to check. */
1798 lp = find_lwp_pid (ptid);
1799 gdb_assert (lp);
1800 status = lp->status;
1801 lp->status = 0;
1802
1803 if (debug_linux_nat && status)
1804 fprintf_unfiltered (gdb_stdlog,
1805 "LLW: Using pending wait status %s for %s.\n",
1806 status_to_str (status),
1807 target_pid_to_str (lp->ptid));
1808
1809 /* If we have to wait, take into account whether PID is a cloned
1810 process or not. And we have to convert it to something that
1811 the layer beneath us can understand. */
1812 options = lp->cloned ? __WCLONE : 0;
1813 pid = GET_LWP (ptid);
1814 }
1815
1816 if (status && lp->signalled)
1817 {
1818 /* A pending SIGSTOP may interfere with the normal stream of
1819 events. In a typical case where interference is a problem,
1820 we have a SIGSTOP signal pending for LWP A while
1821 single-stepping it, encounter an event in LWP B, and take the
1822 pending SIGSTOP while trying to stop LWP A. After processing
1823 the event in LWP B, LWP A is continued, and we'll never see
1824 the SIGTRAP associated with the last time we were
1825 single-stepping LWP A. */
1826
1827 /* Resume the thread. It should halt immediately returning the
1828 pending SIGSTOP. */
1829 registers_changed ();
1830 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1831 TARGET_SIGNAL_0);
1832 if (debug_linux_nat)
1833 fprintf_unfiltered (gdb_stdlog,
1834 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1835 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1836 target_pid_to_str (lp->ptid));
1837 lp->stopped = 0;
1838 gdb_assert (lp->resumed);
1839
1840 /* This should catch the pending SIGSTOP. */
1841 stop_wait_callback (lp, NULL);
1842 }
1843
1844 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1845 attached process. */
1846 set_sigio_trap ();
1847
1848 while (status == 0)
1849 {
1850 pid_t lwpid;
1851
1852 lwpid = waitpid (pid, &status, options);
1853 if (lwpid > 0)
1854 {
1855 gdb_assert (pid == -1 || lwpid == pid);
1856
1857 if (debug_linux_nat)
1858 {
1859 fprintf_unfiltered (gdb_stdlog,
1860 "LLW: waitpid %ld received %s\n",
1861 (long) lwpid, status_to_str (status));
1862 }
1863
1864 lp = find_lwp_pid (pid_to_ptid (lwpid));
1865
1866 /* Check for stop events reported by a process we didn't
1867 already know about - anything not already in our LWP
1868 list.
1869
1870 If we're expecting to receive stopped processes after
1871 fork, vfork, and clone events, then we'll just add the
1872 new one to our list and go back to waiting for the event
1873 to be reported - the stopped process might be returned
1874 from waitpid before or after the event is. */
1875 if (WIFSTOPPED (status) && !lp)
1876 {
1877 linux_record_stopped_pid (lwpid);
1878 status = 0;
1879 continue;
1880 }
1881
1882 /* Make sure we don't report an event for the exit of an LWP not in
1883 our list, i.e. not part of the current process. This can happen
1884 if we detach from a program we original forked and then it
1885 exits. */
1886 if (!WIFSTOPPED (status) && !lp)
1887 {
1888 status = 0;
1889 continue;
1890 }
1891
1892 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1893 CLONE_PTRACE processes which do not use the thread library -
1894 otherwise we wouldn't find the new LWP this way. That doesn't
1895 currently work, and the following code is currently unreachable
1896 due to the two blocks above. If it's fixed some day, this code
1897 should be broken out into a function so that we can also pick up
1898 LWPs from the new interface. */
1899 if (!lp)
1900 {
1901 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1902 if (options & __WCLONE)
1903 lp->cloned = 1;
1904
1905 if (threaded)
1906 {
1907 gdb_assert (WIFSTOPPED (status)
1908 && WSTOPSIG (status) == SIGSTOP);
1909 lp->signalled = 1;
1910
1911 if (!in_thread_list (inferior_ptid))
1912 {
1913 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1914 GET_PID (inferior_ptid));
1915 add_thread (inferior_ptid);
1916 }
1917
1918 add_thread (lp->ptid);
1919 printf_unfiltered ("[New %s]\n",
1920 target_pid_to_str (lp->ptid));
1921 }
1922 }
1923
1924 /* Handle GNU/Linux's extended waitstatus for trace events. */
1925 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1926 {
1927 if (debug_linux_nat)
1928 fprintf_unfiltered (gdb_stdlog,
1929 "LLW: Handling extended status 0x%06x\n",
1930 status);
1931 if (linux_nat_handle_extended (lp, status))
1932 {
1933 status = 0;
1934 continue;
1935 }
1936 }
1937
1938 /* Check if the thread has exited. */
1939 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1940 {
1941 if (in_thread_list (lp->ptid))
1942 {
1943 /* Core GDB cannot deal with us deleting the current
1944 thread. */
1945 if (!ptid_equal (lp->ptid, inferior_ptid))
1946 delete_thread (lp->ptid);
1947 printf_unfiltered ("[%s exited]\n",
1948 target_pid_to_str (lp->ptid));
1949 }
1950
1951 /* If this is the main thread, we must stop all threads and
1952 verify if they are still alive. This is because in the nptl
1953 thread model, there is no signal issued for exiting LWPs
1954 other than the main thread. We only get the main thread
1955 exit signal once all child threads have already exited.
1956 If we stop all the threads and use the stop_wait_callback
1957 to check if they have exited we can determine whether this
1958 signal should be ignored or whether it means the end of the
1959 debugged application, regardless of which threading model
1960 is being used. */
1961 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
1962 {
1963 lp->stopped = 1;
1964 iterate_over_lwps (stop_and_resume_callback, NULL);
1965 }
1966
1967 if (debug_linux_nat)
1968 fprintf_unfiltered (gdb_stdlog,
1969 "LLW: %s exited.\n",
1970 target_pid_to_str (lp->ptid));
1971
1972 delete_lwp (lp->ptid);
1973
1974 /* If there is at least one more LWP, then the exit signal
1975 was not the end of the debugged application and should be
1976 ignored. */
1977 if (num_lwps > 0)
1978 {
1979 /* Make sure there is at least one thread running. */
1980 gdb_assert (iterate_over_lwps (running_callback, NULL));
1981
1982 /* Discard the event. */
1983 status = 0;
1984 continue;
1985 }
1986 }
1987
1988 /* Check if the current LWP has previously exited. In the nptl
1989 thread model, LWPs other than the main thread do not issue
1990 signals when they exit so we must check whenever the thread
1991 has stopped. A similar check is made in stop_wait_callback(). */
1992 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
1993 {
1994 if (in_thread_list (lp->ptid))
1995 {
1996 /* Core GDB cannot deal with us deleting the current
1997 thread. */
1998 if (!ptid_equal (lp->ptid, inferior_ptid))
1999 delete_thread (lp->ptid);
2000 printf_unfiltered ("[%s exited]\n",
2001 target_pid_to_str (lp->ptid));
2002 }
2003 if (debug_linux_nat)
2004 fprintf_unfiltered (gdb_stdlog,
2005 "LLW: %s exited.\n",
2006 target_pid_to_str (lp->ptid));
2007
2008 delete_lwp (lp->ptid);
2009
2010 /* Make sure there is at least one thread running. */
2011 gdb_assert (iterate_over_lwps (running_callback, NULL));
2012
2013 /* Discard the event. */
2014 status = 0;
2015 continue;
2016 }
2017
2018 /* Make sure we don't report a SIGSTOP that we sent
2019 ourselves in an attempt to stop an LWP. */
2020 if (lp->signalled
2021 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2022 {
2023 if (debug_linux_nat)
2024 fprintf_unfiltered (gdb_stdlog,
2025 "LLW: Delayed SIGSTOP caught for %s.\n",
2026 target_pid_to_str (lp->ptid));
2027
2028 /* This is a delayed SIGSTOP. */
2029 lp->signalled = 0;
2030
2031 registers_changed ();
2032 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
2033 TARGET_SIGNAL_0);
2034 if (debug_linux_nat)
2035 fprintf_unfiltered (gdb_stdlog,
2036 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2037 lp->step ?
2038 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2039 target_pid_to_str (lp->ptid));
2040
2041 lp->stopped = 0;
2042 gdb_assert (lp->resumed);
2043
2044 /* Discard the event. */
2045 status = 0;
2046 continue;
2047 }
2048
2049 break;
2050 }
2051
2052 if (pid == -1)
2053 {
2054 /* Alternate between checking cloned and uncloned processes. */
2055 options ^= __WCLONE;
2056
2057 /* And suspend every time we have checked both. */
2058 if (options & __WCLONE)
2059 sigsuspend (&suspend_mask);
2060 }
2061
2062 /* We shouldn't end up here unless we want to try again. */
2063 gdb_assert (status == 0);
2064 }
2065
2066 clear_sigio_trap ();
2067 clear_sigint_trap ();
2068
2069 gdb_assert (lp);
2070
2071 /* Don't report signals that GDB isn't interested in, such as
2072 signals that are neither printed nor stopped upon. Stopping all
2073 threads can be a bit time-consuming so if we want decent
2074 performance with heavily multi-threaded programs, especially when
2075 they're using a high frequency timer, we'd better avoid it if we
2076 can. */
2077
2078 if (WIFSTOPPED (status))
2079 {
2080 int signo = target_signal_from_host (WSTOPSIG (status));
2081
2082 if (signal_stop_state (signo) == 0
2083 && signal_print_state (signo) == 0
2084 && signal_pass_state (signo) == 1)
2085 {
2086 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2087 here? It is not clear we should. GDB may not expect
2088 other threads to run. On the other hand, not resuming
2089 newly attached threads may cause an unwanted delay in
2090 getting them running. */
2091 registers_changed ();
2092 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
2093 if (debug_linux_nat)
2094 fprintf_unfiltered (gdb_stdlog,
2095 "LLW: %s %s, %s (preempt 'handle')\n",
2096 lp->step ?
2097 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2098 target_pid_to_str (lp->ptid),
2099 signo ? strsignal (signo) : "0");
2100 lp->stopped = 0;
2101 status = 0;
2102 goto retry;
2103 }
2104
2105 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
2106 {
2107 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2108 forwarded to the entire process group, that is, all LWP's
2109 will receive it. Since we only want to report it once,
2110 we try to flush it from all LWPs except this one. */
2111 sigaddset (&flush_mask, SIGINT);
2112 }
2113 }
2114
2115 /* This LWP is stopped now. */
2116 lp->stopped = 1;
2117
2118 if (debug_linux_nat)
2119 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
2120 status_to_str (status), target_pid_to_str (lp->ptid));
2121
2122 /* Now stop all other LWP's ... */
2123 iterate_over_lwps (stop_callback, NULL);
2124
2125 /* ... and wait until all of them have reported back that they're no
2126 longer running. */
2127 iterate_over_lwps (stop_wait_callback, &flush_mask);
2128 iterate_over_lwps (flush_callback, &flush_mask);
2129
2130 /* If we're not waiting for a specific LWP, choose an event LWP from
2131 among those that have had events. Giving equal priority to all
2132 LWPs that have had events helps prevent starvation. */
2133 if (pid == -1)
2134 select_event_lwp (&lp, &status);
2135
2136 /* Now that we've selected our final event LWP, cancel any
2137 breakpoints in other LWPs that have hit a GDB breakpoint. See
2138 the comment in cancel_breakpoints_callback to find out why. */
2139 iterate_over_lwps (cancel_breakpoints_callback, lp);
2140
2141 /* If we're not running in "threaded" mode, we'll report the bare
2142 process id. */
2143
2144 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2145 {
2146 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2147 if (debug_linux_nat)
2148 fprintf_unfiltered (gdb_stdlog,
2149 "LLW: trap_ptid is %s.\n",
2150 target_pid_to_str (trap_ptid));
2151 }
2152 else
2153 trap_ptid = null_ptid;
2154
2155 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2156 {
2157 *ourstatus = lp->waitstatus;
2158 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
2159 }
2160 else
2161 store_waitstatus (ourstatus, status);
2162
2163 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2164}
2165
2166static int
2167kill_callback (struct lwp_info *lp, void *data)
2168{
2169 errno = 0;
2170 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
2171 if (debug_linux_nat)
2172 fprintf_unfiltered (gdb_stdlog,
2173 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2174 target_pid_to_str (lp->ptid),
2175 errno ? safe_strerror (errno) : "OK");
2176
2177 return 0;
2178}
2179
2180static int
2181kill_wait_callback (struct lwp_info *lp, void *data)
2182{
2183 pid_t pid;
2184
2185 /* We must make sure that there are no pending events (delayed
2186 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2187 program doesn't interfere with any following debugging session. */
2188
2189 /* For cloned processes we must check both with __WCLONE and
2190 without, since the exit status of a cloned process isn't reported
2191 with __WCLONE. */
2192 if (lp->cloned)
2193 {
2194 do
2195 {
2196 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
2197 if (pid != (pid_t) -1 && debug_linux_nat)
2198 {
2199 fprintf_unfiltered (gdb_stdlog,
2200 "KWC: wait %s received unknown.\n",
2201 target_pid_to_str (lp->ptid));
2202 }
2203 }
2204 while (pid == GET_LWP (lp->ptid));
2205
2206 gdb_assert (pid == -1 && errno == ECHILD);
2207 }
2208
2209 do
2210 {
2211 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
2212 if (pid != (pid_t) -1 && debug_linux_nat)
2213 {
2214 fprintf_unfiltered (gdb_stdlog,
2215 "KWC: wait %s received unk.\n",
2216 target_pid_to_str (lp->ptid));
2217 }
2218 }
2219 while (pid == GET_LWP (lp->ptid));
2220
2221 gdb_assert (pid == -1 && errno == ECHILD);
2222 return 0;
2223}
2224
2225static void
2226linux_nat_kill (void)
2227{
2228 /* Kill all LWP's ... */
2229 iterate_over_lwps (kill_callback, NULL);
2230
2231 /* ... and wait until we've flushed all events. */
2232 iterate_over_lwps (kill_wait_callback, NULL);
2233
2234 target_mourn_inferior ();
2235}
2236
2237static void
2238linux_nat_create_inferior (char *exec_file, char *allargs, char **env,
2239 int from_tty)
2240{
2241 child_ops.to_create_inferior (exec_file, allargs, env, from_tty);
2242}
2243
2244static void
2245linux_nat_mourn_inferior (void)
2246{
2247 trap_ptid = null_ptid;
2248
2249 /* Destroy LWP info; it's no longer valid. */
2250 init_lwp_list ();
2251
2252 /* Restore the original signal mask. */
2253 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
2254 sigemptyset (&blocked_mask);
2255
2256 child_ops.to_mourn_inferior ();
2257}
2258
2259static int
2260linux_nat_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2261 struct mem_attrib *attrib, struct target_ops *target)
2262{
2263 struct cleanup *old_chain = save_inferior_ptid ();
2264 int xfer;
2265
2266 if (is_lwp (inferior_ptid))
2267 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
2268
2269 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2270 if (xfer == 0)
2271 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2272
2273 do_cleanups (old_chain);
2274 return xfer;
2275}
2276
2277static int
2278linux_nat_thread_alive (ptid_t ptid)
2279{
2280 gdb_assert (is_lwp (ptid));
2281
2282 errno = 0;
2283 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
2284 if (debug_linux_nat)
2285 fprintf_unfiltered (gdb_stdlog,
2286 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2287 target_pid_to_str (ptid),
2288 errno ? safe_strerror (errno) : "OK");
2289 if (errno)
2290 return 0;
2291
2292 return 1;
2293}
2294
2295static char *
2296linux_nat_pid_to_str (ptid_t ptid)
2297{
2298 static char buf[64];
2299
2300 if (is_lwp (ptid))
2301 {
2302 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
2303 return buf;
2304 }
2305
2306 return normal_pid_to_str (ptid);
2307}
2308
2309static void
2310init_linux_nat_ops (void)
2311{
2312#if 0
2313 linux_nat_ops.to_open = linux_nat_open;
2314#endif
2315 linux_nat_ops.to_shortname = "lwp-layer";
2316 linux_nat_ops.to_longname = "lwp-layer";
2317 linux_nat_ops.to_doc = "Low level threads support (LWP layer)";
2318 linux_nat_ops.to_attach = linux_nat_attach;
2319 linux_nat_ops.to_detach = linux_nat_detach;
2320 linux_nat_ops.to_resume = linux_nat_resume;
2321 linux_nat_ops.to_wait = linux_nat_wait;
2322 /* fetch_inferior_registers and store_inferior_registers will
2323 honor the LWP id, so we can use them directly. */
2324 linux_nat_ops.to_fetch_registers = fetch_inferior_registers;
2325 linux_nat_ops.to_store_registers = store_inferior_registers;
2326 linux_nat_ops.to_xfer_memory = linux_nat_xfer_memory;
2327 linux_nat_ops.to_kill = linux_nat_kill;
2328 linux_nat_ops.to_create_inferior = linux_nat_create_inferior;
2329 linux_nat_ops.to_mourn_inferior = linux_nat_mourn_inferior;
2330 linux_nat_ops.to_thread_alive = linux_nat_thread_alive;
2331 linux_nat_ops.to_pid_to_str = linux_nat_pid_to_str;
2332 linux_nat_ops.to_post_startup_inferior = child_post_startup_inferior;
2333 linux_nat_ops.to_post_attach = child_post_attach;
2334 linux_nat_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
2335 linux_nat_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
2336 linux_nat_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
2337
2338 linux_nat_ops.to_stratum = thread_stratum;
2339 linux_nat_ops.to_has_thread_control = tc_schedlock;
2340 linux_nat_ops.to_magic = OPS_MAGIC;
2341}
2342
2343static void
2344sigchld_handler (int signo)
2345{
2346 /* Do nothing. The only reason for this handler is that it allows
2347 us to use sigsuspend in linux_nat_wait above to wait for the
2348 arrival of a SIGCHLD. */
2349}
2350
2351void
2352_initialize_linux_nat (void)
2353{
2354 struct sigaction action;
2355
2356 extern void thread_db_init (struct target_ops *);
2357
2358 init_linux_nat_ops ();
2359 add_target (&linux_nat_ops);
2360 thread_db_init (&linux_nat_ops);
2361
2362 /* Save the original signal mask. */
2363 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
2364
2365 action.sa_handler = sigchld_handler;
2366 sigemptyset (&action.sa_mask);
2367 action.sa_flags = 0;
2368 sigaction (SIGCHLD, &action, NULL);
2369
2370 /* Make sure we don't block SIGCHLD during a sigsuspend. */
2371 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
2372 sigdelset (&suspend_mask, SIGCHLD);
2373
2374 sigemptyset (&blocked_mask);
2375
2376 deprecated_add_show_from_set
2377 (add_set_cmd ("lin-lwp", no_class, var_zinteger,
2378 (char *) &debug_linux_nat,
2379 "Set debugging of GNU/Linux lwp module.\n\
2380Enables printf debugging output.\n", &setdebuglist), &showdebuglist);
2381}
2382\f
2383
2384/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
2385 the GNU/Linux Threads library and therefore doesn't really belong
2386 here. */
2387
2388/* Read variable NAME in the target and return its value if found.
2389 Otherwise return zero. It is assumed that the type of the variable
2390 is `int'. */
2391
2392static int
2393get_signo (const char *name)
2394{
2395 struct minimal_symbol *ms;
2396 int signo;
2397
2398 ms = lookup_minimal_symbol (name, NULL, NULL);
2399 if (ms == NULL)
2400 return 0;
2401
2402 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
2403 sizeof (signo)) != 0)
2404 return 0;
2405
2406 return signo;
2407}
2408
2409/* Return the set of signals used by the threads library in *SET. */
2410
2411void
2412lin_thread_get_thread_signals (sigset_t *set)
2413{
2414 struct sigaction action;
2415 int restart, cancel;
2416
2417 sigemptyset (set);
2418
2419 restart = get_signo ("__pthread_sig_restart");
2420 if (restart == 0)
2421 return;
2422
2423 cancel = get_signo ("__pthread_sig_cancel");
2424 if (cancel == 0)
2425 return;
2426
2427 sigaddset (set, restart);
2428 sigaddset (set, cancel);
2429
2430 /* The GNU/Linux Threads library makes terminating threads send a
2431 special "cancel" signal instead of SIGCHLD. Make sure we catch
2432 those (to prevent them from terminating GDB itself, which is
2433 likely to be their default action) and treat them the same way as
2434 SIGCHLD. */
2435
2436 action.sa_handler = sigchld_handler;
2437 sigemptyset (&action.sa_mask);
2438 action.sa_flags = 0;
2439 sigaction (cancel, &action, NULL);
2440
2441 /* We block the "cancel" signal throughout this code ... */
2442 sigaddset (&blocked_mask, cancel);
2443 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
2444
2445 /* ... except during a sigsuspend. */
2446 sigdelset (&suspend_mask, cancel);
2447}
This page took 0.233748 seconds and 4 git commands to generate.