GDB copyright headers update after running GDB's copyright.py script.
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
1 /* GNU/Linux native-dependent code common to multiple platforms.
2
3 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "infrun.h"
23 #include "target.h"
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
26 #include "gdb_wait.h"
27 #include <unistd.h>
28 #include <sys/syscall.h>
29 #include "nat/gdb_ptrace.h"
30 #include "linux-nat.h"
31 #include "nat/linux-ptrace.h"
32 #include "nat/linux-procfs.h"
33 #include "nat/linux-personality.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
36 #include "gdbcmd.h"
37 #include "regcache.h"
38 #include "regset.h"
39 #include "inf-child.h"
40 #include "inf-ptrace.h"
41 #include "auxv.h"
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include <sys/stat.h> /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
49 #include "inf-loop.h"
50 #include "event-loop.h"
51 #include "event-top.h"
52 #include <pwd.h>
53 #include <sys/types.h>
54 #include <dirent.h>
55 #include "xml-support.h"
56 #include <sys/vfs.h>
57 #include "solib.h"
58 #include "nat/linux-osdata.h"
59 #include "linux-tdep.h"
60 #include "symfile.h"
61 #include "agent.h"
62 #include "tracepoint.h"
63 #include "buffer.h"
64 #include "target-descriptions.h"
65 #include "filestuff.h"
66 #include "objfiles.h"
67 #include "nat/linux-namespaces.h"
68 #include "fileio.h"
69
70 #ifndef SPUFS_MAGIC
71 #define SPUFS_MAGIC 0x23c9b64e
72 #endif
73
74 /* This comment documents high-level logic of this file.
75
76 Waiting for events in sync mode
77 ===============================
78
79 When waiting for an event in a specific thread, we just use waitpid,
80 passing the specific pid, and not passing WNOHANG.
81
82 When waiting for an event in all threads, waitpid is not quite good:
83
84 - If the thread group leader exits while other threads in the thread
85 group still exist, waitpid(TGID, ...) hangs. That waitpid won't
86 return an exit status until the other threads in the group are
87 reaped.
88
89 - When a non-leader thread execs, that thread just vanishes without
90 reporting an exit (so we'd hang if we waited for it explicitly in
91 that case). The exec event is instead reported to the TGID pid.
92
93 The solution is to always use -1 and WNOHANG, together with
94 sigsuspend.
95
96 First, we use non-blocking waitpid to check for events. If nothing is
97 found, we use sigsuspend to wait for SIGCHLD. When SIGCHLD arrives,
98 it means something happened to a child process. As soon as we know
99 there's an event, we get back to calling nonblocking waitpid.
100
101 Note that SIGCHLD should be blocked between waitpid and sigsuspend
102 calls, so that we don't miss a signal. If SIGCHLD arrives in between,
103 when it's blocked, the signal becomes pending and sigsuspend
104 immediately notices it and returns.
105
106 Waiting for events in async mode (TARGET_WNOHANG)
107 =================================================
108
109 In async mode, GDB should always be ready to handle both user input
110 and target events, so neither blocking waitpid nor sigsuspend are
111 viable options. Instead, we should asynchronously notify the GDB main
112 event loop whenever there's an unprocessed event from the target. We
113 detect asynchronous target events by handling SIGCHLD signals. To
114 notify the event loop about target events, the self-pipe trick is used
115 --- a pipe is registered as waitable event source in the event loop,
116 the event loop select/poll's on the read end of this pipe (as well on
117 other event sources, e.g., stdin), and the SIGCHLD handler writes a
118 byte to this pipe. This is more portable than relying on
119 pselect/ppoll, since on kernels that lack those syscalls, libc
120 emulates them with select/poll+sigprocmask, and that is racy
121 (a.k.a. plain broken).
122
123 Obviously, if we fail to notify the event loop if there's a target
124 event, it's bad. OTOH, if we notify the event loop when there's no
125 event from the target, linux_nat_wait will detect that there's no real
126 event to report, and return event of type TARGET_WAITKIND_IGNORE.
127 This is mostly harmless, but it will waste time and is better avoided.
128
129 The main design point is that every time GDB is outside linux-nat.c,
130 we have a SIGCHLD handler installed that is called when something
131 happens to the target and notifies the GDB event loop. Whenever GDB
132 core decides to handle the event, and calls into linux-nat.c, we
133 process things as in sync mode, except that the we never block in
134 sigsuspend.
135
136 While processing an event, we may end up momentarily blocked in
137 waitpid calls. Those waitpid calls, while blocking, are guarantied to
138 return quickly. E.g., in all-stop mode, before reporting to the core
139 that an LWP hit a breakpoint, all LWPs are stopped by sending them
140 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
141 Note that this is different from blocking indefinitely waiting for the
142 next event --- here, we're already handling an event.
143
144 Use of signals
145 ==============
146
147 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
148 signal is not entirely significant; we just need for a signal to be delivered,
149 so that we can intercept it. SIGSTOP's advantage is that it can not be
150 blocked. A disadvantage is that it is not a real-time signal, so it can only
151 be queued once; we do not keep track of other sources of SIGSTOP.
152
153 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
154 use them, because they have special behavior when the signal is generated -
155 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
156 kills the entire thread group.
157
158 A delivered SIGSTOP would stop the entire thread group, not just the thread we
159 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
160 cancel it (by PTRACE_CONT without passing SIGSTOP).
161
162 We could use a real-time signal instead. This would solve those problems; we
163 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
164 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
165 generates it, and there are races with trying to find a signal that is not
166 blocked.
167
168 Exec events
169 ===========
170
171 The case of a thread group (process) with 3 or more threads, and a
172 thread other than the leader execs is worth detailing:
173
174 On an exec, the Linux kernel destroys all threads except the execing
175 one in the thread group, and resets the execing thread's tid to the
176 tgid. No exit notification is sent for the execing thread -- from the
177 ptracer's perspective, it appears as though the execing thread just
178 vanishes. Until we reap all other threads except the leader and the
179 execing thread, the leader will be zombie, and the execing thread will
180 be in `D (disc sleep)' state. As soon as all other threads are
181 reaped, the execing thread changes its tid to the tgid, and the
182 previous (zombie) leader vanishes, giving place to the "new"
183 leader. */
184
185 #ifndef O_LARGEFILE
186 #define O_LARGEFILE 0
187 #endif
188
189 /* Does the current host support PTRACE_GETREGSET? */
190 enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
191
192 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
193 the use of the multi-threaded target. */
194 static struct target_ops *linux_ops;
195 static struct target_ops linux_ops_saved;
196
197 /* The method to call, if any, when a new thread is attached. */
198 static void (*linux_nat_new_thread) (struct lwp_info *);
199
200 /* The method to call, if any, when a new fork is attached. */
201 static linux_nat_new_fork_ftype *linux_nat_new_fork;
202
203 /* The method to call, if any, when a process is no longer
204 attached. */
205 static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
206
207 /* Hook to call prior to resuming a thread. */
208 static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
209
210 /* The method to call, if any, when the siginfo object needs to be
211 converted between the layout returned by ptrace, and the layout in
212 the architecture of the inferior. */
213 static int (*linux_nat_siginfo_fixup) (siginfo_t *,
214 gdb_byte *,
215 int);
216
217 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
218 Called by our to_xfer_partial. */
219 static target_xfer_partial_ftype *super_xfer_partial;
220
221 /* The saved to_close method, inherited from inf-ptrace.c.
222 Called by our to_close. */
223 static void (*super_close) (struct target_ops *);
224
225 static unsigned int debug_linux_nat;
226 static void
227 show_debug_linux_nat (struct ui_file *file, int from_tty,
228 struct cmd_list_element *c, const char *value)
229 {
230 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
231 value);
232 }
233
234 struct simple_pid_list
235 {
236 int pid;
237 int status;
238 struct simple_pid_list *next;
239 };
240 struct simple_pid_list *stopped_pids;
241
242 /* Async mode support. */
243
244 /* The read/write ends of the pipe registered as waitable file in the
245 event loop. */
246 static int linux_nat_event_pipe[2] = { -1, -1 };
247
248 /* True if we're currently in async mode. */
249 #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
250
251 /* Flush the event pipe. */
252
253 static void
254 async_file_flush (void)
255 {
256 int ret;
257 char buf;
258
259 do
260 {
261 ret = read (linux_nat_event_pipe[0], &buf, 1);
262 }
263 while (ret >= 0 || (ret == -1 && errno == EINTR));
264 }
265
266 /* Put something (anything, doesn't matter what, or how much) in event
267 pipe, so that the select/poll in the event-loop realizes we have
268 something to process. */
269
270 static void
271 async_file_mark (void)
272 {
273 int ret;
274
275 /* It doesn't really matter what the pipe contains, as long we end
276 up with something in it. Might as well flush the previous
277 left-overs. */
278 async_file_flush ();
279
280 do
281 {
282 ret = write (linux_nat_event_pipe[1], "+", 1);
283 }
284 while (ret == -1 && errno == EINTR);
285
286 /* Ignore EAGAIN. If the pipe is full, the event loop will already
287 be awakened anyway. */
288 }
289
290 static int kill_lwp (int lwpid, int signo);
291
292 static int stop_callback (struct lwp_info *lp, void *data);
293 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
294
295 static void block_child_signals (sigset_t *prev_mask);
296 static void restore_child_signals_mask (sigset_t *prev_mask);
297
298 struct lwp_info;
299 static struct lwp_info *add_lwp (ptid_t ptid);
300 static void purge_lwp_list (int pid);
301 static void delete_lwp (ptid_t ptid);
302 static struct lwp_info *find_lwp_pid (ptid_t ptid);
303
304 static int lwp_status_pending_p (struct lwp_info *lp);
305
306 static int check_stopped_by_breakpoint (struct lwp_info *lp);
307 static int sigtrap_is_event (int status);
308 static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
309
310 \f
311 /* LWP accessors. */
312
313 /* See nat/linux-nat.h. */
314
315 ptid_t
316 ptid_of_lwp (struct lwp_info *lwp)
317 {
318 return lwp->ptid;
319 }
320
321 /* See nat/linux-nat.h. */
322
323 void
324 lwp_set_arch_private_info (struct lwp_info *lwp,
325 struct arch_lwp_info *info)
326 {
327 lwp->arch_private = info;
328 }
329
330 /* See nat/linux-nat.h. */
331
332 struct arch_lwp_info *
333 lwp_arch_private_info (struct lwp_info *lwp)
334 {
335 return lwp->arch_private;
336 }
337
338 /* See nat/linux-nat.h. */
339
340 int
341 lwp_is_stopped (struct lwp_info *lwp)
342 {
343 return lwp->stopped;
344 }
345
346 /* See nat/linux-nat.h. */
347
348 enum target_stop_reason
349 lwp_stop_reason (struct lwp_info *lwp)
350 {
351 return lwp->stop_reason;
352 }
353
354 \f
355 /* Trivial list manipulation functions to keep track of a list of
356 new stopped processes. */
357 static void
358 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
359 {
360 struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
361
362 new_pid->pid = pid;
363 new_pid->status = status;
364 new_pid->next = *listp;
365 *listp = new_pid;
366 }
367
368 static int
369 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
370 {
371 struct simple_pid_list **p;
372
373 for (p = listp; *p != NULL; p = &(*p)->next)
374 if ((*p)->pid == pid)
375 {
376 struct simple_pid_list *next = (*p)->next;
377
378 *statusp = (*p)->status;
379 xfree (*p);
380 *p = next;
381 return 1;
382 }
383 return 0;
384 }
385
386 /* Return the ptrace options that we want to try to enable. */
387
388 static int
389 linux_nat_ptrace_options (int attached)
390 {
391 int options = 0;
392
393 if (!attached)
394 options |= PTRACE_O_EXITKILL;
395
396 options |= (PTRACE_O_TRACESYSGOOD
397 | PTRACE_O_TRACEVFORKDONE
398 | PTRACE_O_TRACEVFORK
399 | PTRACE_O_TRACEFORK
400 | PTRACE_O_TRACEEXEC);
401
402 return options;
403 }
404
405 /* Initialize ptrace warnings and check for supported ptrace
406 features given PID.
407
408 ATTACHED should be nonzero iff we attached to the inferior. */
409
410 static void
411 linux_init_ptrace (pid_t pid, int attached)
412 {
413 int options = linux_nat_ptrace_options (attached);
414
415 linux_enable_event_reporting (pid, options);
416 linux_ptrace_init_warnings ();
417 }
418
419 static void
420 linux_child_post_attach (struct target_ops *self, int pid)
421 {
422 linux_init_ptrace (pid, 1);
423 }
424
425 static void
426 linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
427 {
428 linux_init_ptrace (ptid_get_pid (ptid), 0);
429 }
430
431 /* Return the number of known LWPs in the tgid given by PID. */
432
433 static int
434 num_lwps (int pid)
435 {
436 int count = 0;
437 struct lwp_info *lp;
438
439 for (lp = lwp_list; lp; lp = lp->next)
440 if (ptid_get_pid (lp->ptid) == pid)
441 count++;
442
443 return count;
444 }
445
446 /* Call delete_lwp with prototype compatible for make_cleanup. */
447
448 static void
449 delete_lwp_cleanup (void *lp_voidp)
450 {
451 struct lwp_info *lp = (struct lwp_info *) lp_voidp;
452
453 delete_lwp (lp->ptid);
454 }
455
456 /* Target hook for follow_fork. On entry inferior_ptid must be the
457 ptid of the followed inferior. At return, inferior_ptid will be
458 unchanged. */
459
460 static int
461 linux_child_follow_fork (struct target_ops *ops, int follow_child,
462 int detach_fork)
463 {
464 if (!follow_child)
465 {
466 struct lwp_info *child_lp = NULL;
467 int status = W_STOPCODE (0);
468 struct cleanup *old_chain;
469 int has_vforked;
470 ptid_t parent_ptid, child_ptid;
471 int parent_pid, child_pid;
472
473 has_vforked = (inferior_thread ()->pending_follow.kind
474 == TARGET_WAITKIND_VFORKED);
475 parent_ptid = inferior_ptid;
476 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
477 parent_pid = ptid_get_lwp (parent_ptid);
478 child_pid = ptid_get_lwp (child_ptid);
479
480 /* We're already attached to the parent, by default. */
481 old_chain = save_inferior_ptid ();
482 inferior_ptid = child_ptid;
483 child_lp = add_lwp (inferior_ptid);
484 child_lp->stopped = 1;
485 child_lp->last_resume_kind = resume_stop;
486
487 /* Detach new forked process? */
488 if (detach_fork)
489 {
490 make_cleanup (delete_lwp_cleanup, child_lp);
491
492 if (linux_nat_prepare_to_resume != NULL)
493 linux_nat_prepare_to_resume (child_lp);
494
495 /* When debugging an inferior in an architecture that supports
496 hardware single stepping on a kernel without commit
497 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
498 process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
499 set if the parent process had them set.
500 To work around this, single step the child process
501 once before detaching to clear the flags. */
502
503 if (!gdbarch_software_single_step_p (target_thread_architecture
504 (child_lp->ptid)))
505 {
506 linux_disable_event_reporting (child_pid);
507 if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
508 perror_with_name (_("Couldn't do single step"));
509 if (my_waitpid (child_pid, &status, 0) < 0)
510 perror_with_name (_("Couldn't wait vfork process"));
511 }
512
513 if (WIFSTOPPED (status))
514 {
515 int signo;
516
517 signo = WSTOPSIG (status);
518 if (signo != 0
519 && !signal_pass_state (gdb_signal_from_host (signo)))
520 signo = 0;
521 ptrace (PTRACE_DETACH, child_pid, 0, signo);
522 }
523
524 /* Resets value of inferior_ptid to parent ptid. */
525 do_cleanups (old_chain);
526 }
527 else
528 {
529 /* Let the thread_db layer learn about this new process. */
530 check_for_thread_db ();
531 }
532
533 do_cleanups (old_chain);
534
535 if (has_vforked)
536 {
537 struct lwp_info *parent_lp;
538
539 parent_lp = find_lwp_pid (parent_ptid);
540 gdb_assert (linux_supports_tracefork () >= 0);
541
542 if (linux_supports_tracevforkdone ())
543 {
544 if (debug_linux_nat)
545 fprintf_unfiltered (gdb_stdlog,
546 "LCFF: waiting for VFORK_DONE on %d\n",
547 parent_pid);
548 parent_lp->stopped = 1;
549
550 /* We'll handle the VFORK_DONE event like any other
551 event, in target_wait. */
552 }
553 else
554 {
555 /* We can't insert breakpoints until the child has
556 finished with the shared memory region. We need to
557 wait until that happens. Ideal would be to just
558 call:
559 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
560 - waitpid (parent_pid, &status, __WALL);
561 However, most architectures can't handle a syscall
562 being traced on the way out if it wasn't traced on
563 the way in.
564
565 We might also think to loop, continuing the child
566 until it exits or gets a SIGTRAP. One problem is
567 that the child might call ptrace with PTRACE_TRACEME.
568
569 There's no simple and reliable way to figure out when
570 the vforked child will be done with its copy of the
571 shared memory. We could step it out of the syscall,
572 two instructions, let it go, and then single-step the
573 parent once. When we have hardware single-step, this
574 would work; with software single-step it could still
575 be made to work but we'd have to be able to insert
576 single-step breakpoints in the child, and we'd have
577 to insert -just- the single-step breakpoint in the
578 parent. Very awkward.
579
580 In the end, the best we can do is to make sure it
581 runs for a little while. Hopefully it will be out of
582 range of any breakpoints we reinsert. Usually this
583 is only the single-step breakpoint at vfork's return
584 point. */
585
586 if (debug_linux_nat)
587 fprintf_unfiltered (gdb_stdlog,
588 "LCFF: no VFORK_DONE "
589 "support, sleeping a bit\n");
590
591 usleep (10000);
592
593 /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
594 and leave it pending. The next linux_nat_resume call
595 will notice a pending event, and bypasses actually
596 resuming the inferior. */
597 parent_lp->status = 0;
598 parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
599 parent_lp->stopped = 1;
600
601 /* If we're in async mode, need to tell the event loop
602 there's something here to process. */
603 if (target_is_async_p ())
604 async_file_mark ();
605 }
606 }
607 }
608 else
609 {
610 struct lwp_info *child_lp;
611
612 child_lp = add_lwp (inferior_ptid);
613 child_lp->stopped = 1;
614 child_lp->last_resume_kind = resume_stop;
615
616 /* Let the thread_db layer learn about this new process. */
617 check_for_thread_db ();
618 }
619
620 return 0;
621 }
622
623 \f
624 static int
625 linux_child_insert_fork_catchpoint (struct target_ops *self, int pid)
626 {
627 return !linux_supports_tracefork ();
628 }
629
630 static int
631 linux_child_remove_fork_catchpoint (struct target_ops *self, int pid)
632 {
633 return 0;
634 }
635
636 static int
637 linux_child_insert_vfork_catchpoint (struct target_ops *self, int pid)
638 {
639 return !linux_supports_tracefork ();
640 }
641
642 static int
643 linux_child_remove_vfork_catchpoint (struct target_ops *self, int pid)
644 {
645 return 0;
646 }
647
648 static int
649 linux_child_insert_exec_catchpoint (struct target_ops *self, int pid)
650 {
651 return !linux_supports_tracefork ();
652 }
653
654 static int
655 linux_child_remove_exec_catchpoint (struct target_ops *self, int pid)
656 {
657 return 0;
658 }
659
660 static int
661 linux_child_set_syscall_catchpoint (struct target_ops *self,
662 int pid, int needed, int any_count,
663 int table_size, int *table)
664 {
665 if (!linux_supports_tracesysgood ())
666 return 1;
667
668 /* On GNU/Linux, we ignore the arguments. It means that we only
669 enable the syscall catchpoints, but do not disable them.
670
671 Also, we do not use the `table' information because we do not
672 filter system calls here. We let GDB do the logic for us. */
673 return 0;
674 }
675
676 /* List of known LWPs. */
677 struct lwp_info *lwp_list;
678 \f
679
680 /* Original signal mask. */
681 static sigset_t normal_mask;
682
683 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
684 _initialize_linux_nat. */
685 static sigset_t suspend_mask;
686
687 /* Signals to block to make that sigsuspend work. */
688 static sigset_t blocked_mask;
689
690 /* SIGCHLD action. */
691 struct sigaction sigchld_action;
692
693 /* Block child signals (SIGCHLD and linux threads signals), and store
694 the previous mask in PREV_MASK. */
695
696 static void
697 block_child_signals (sigset_t *prev_mask)
698 {
699 /* Make sure SIGCHLD is blocked. */
700 if (!sigismember (&blocked_mask, SIGCHLD))
701 sigaddset (&blocked_mask, SIGCHLD);
702
703 sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
704 }
705
706 /* Restore child signals mask, previously returned by
707 block_child_signals. */
708
709 static void
710 restore_child_signals_mask (sigset_t *prev_mask)
711 {
712 sigprocmask (SIG_SETMASK, prev_mask, NULL);
713 }
714
715 /* Mask of signals to pass directly to the inferior. */
716 static sigset_t pass_mask;
717
718 /* Update signals to pass to the inferior. */
719 static void
720 linux_nat_pass_signals (struct target_ops *self,
721 int numsigs, unsigned char *pass_signals)
722 {
723 int signo;
724
725 sigemptyset (&pass_mask);
726
727 for (signo = 1; signo < NSIG; signo++)
728 {
729 int target_signo = gdb_signal_from_host (signo);
730 if (target_signo < numsigs && pass_signals[target_signo])
731 sigaddset (&pass_mask, signo);
732 }
733 }
734
735 \f
736
737 /* Prototypes for local functions. */
738 static int stop_wait_callback (struct lwp_info *lp, void *data);
739 static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
740 static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
741
742 \f
743
744 /* Destroy and free LP. */
745
746 static void
747 lwp_free (struct lwp_info *lp)
748 {
749 xfree (lp->arch_private);
750 xfree (lp);
751 }
752
753 /* Remove all LWPs belong to PID from the lwp list. */
754
755 static void
756 purge_lwp_list (int pid)
757 {
758 struct lwp_info *lp, *lpprev, *lpnext;
759
760 lpprev = NULL;
761
762 for (lp = lwp_list; lp; lp = lpnext)
763 {
764 lpnext = lp->next;
765
766 if (ptid_get_pid (lp->ptid) == pid)
767 {
768 if (lp == lwp_list)
769 lwp_list = lp->next;
770 else
771 lpprev->next = lp->next;
772
773 lwp_free (lp);
774 }
775 else
776 lpprev = lp;
777 }
778 }
779
780 /* Add the LWP specified by PTID to the list. PTID is the first LWP
781 in the process. Return a pointer to the structure describing the
782 new LWP.
783
784 This differs from add_lwp in that we don't let the arch specific
785 bits know about this new thread. Current clients of this callback
786 take the opportunity to install watchpoints in the new thread, and
787 we shouldn't do that for the first thread. If we're spawning a
788 child ("run"), the thread executes the shell wrapper first, and we
789 shouldn't touch it until it execs the program we want to debug.
790 For "attach", it'd be okay to call the callback, but it's not
791 necessary, because watchpoints can't yet have been inserted into
792 the inferior. */
793
794 static struct lwp_info *
795 add_initial_lwp (ptid_t ptid)
796 {
797 struct lwp_info *lp;
798
799 gdb_assert (ptid_lwp_p (ptid));
800
801 lp = XNEW (struct lwp_info);
802
803 memset (lp, 0, sizeof (struct lwp_info));
804
805 lp->last_resume_kind = resume_continue;
806 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
807
808 lp->ptid = ptid;
809 lp->core = -1;
810
811 lp->next = lwp_list;
812 lwp_list = lp;
813
814 return lp;
815 }
816
817 /* Add the LWP specified by PID to the list. Return a pointer to the
818 structure describing the new LWP. The LWP should already be
819 stopped. */
820
821 static struct lwp_info *
822 add_lwp (ptid_t ptid)
823 {
824 struct lwp_info *lp;
825
826 lp = add_initial_lwp (ptid);
827
828 /* Let the arch specific bits know about this new thread. Current
829 clients of this callback take the opportunity to install
830 watchpoints in the new thread. We don't do this for the first
831 thread though. See add_initial_lwp. */
832 if (linux_nat_new_thread != NULL)
833 linux_nat_new_thread (lp);
834
835 return lp;
836 }
837
838 /* Remove the LWP specified by PID from the list. */
839
840 static void
841 delete_lwp (ptid_t ptid)
842 {
843 struct lwp_info *lp, *lpprev;
844
845 lpprev = NULL;
846
847 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
848 if (ptid_equal (lp->ptid, ptid))
849 break;
850
851 if (!lp)
852 return;
853
854 if (lpprev)
855 lpprev->next = lp->next;
856 else
857 lwp_list = lp->next;
858
859 lwp_free (lp);
860 }
861
862 /* Return a pointer to the structure describing the LWP corresponding
863 to PID. If no corresponding LWP could be found, return NULL. */
864
865 static struct lwp_info *
866 find_lwp_pid (ptid_t ptid)
867 {
868 struct lwp_info *lp;
869 int lwp;
870
871 if (ptid_lwp_p (ptid))
872 lwp = ptid_get_lwp (ptid);
873 else
874 lwp = ptid_get_pid (ptid);
875
876 for (lp = lwp_list; lp; lp = lp->next)
877 if (lwp == ptid_get_lwp (lp->ptid))
878 return lp;
879
880 return NULL;
881 }
882
883 /* See nat/linux-nat.h. */
884
885 struct lwp_info *
886 iterate_over_lwps (ptid_t filter,
887 iterate_over_lwps_ftype callback,
888 void *data)
889 {
890 struct lwp_info *lp, *lpnext;
891
892 for (lp = lwp_list; lp; lp = lpnext)
893 {
894 lpnext = lp->next;
895
896 if (ptid_match (lp->ptid, filter))
897 {
898 if ((*callback) (lp, data) != 0)
899 return lp;
900 }
901 }
902
903 return NULL;
904 }
905
906 /* Update our internal state when changing from one checkpoint to
907 another indicated by NEW_PTID. We can only switch single-threaded
908 applications, so we only create one new LWP, and the previous list
909 is discarded. */
910
911 void
912 linux_nat_switch_fork (ptid_t new_ptid)
913 {
914 struct lwp_info *lp;
915
916 purge_lwp_list (ptid_get_pid (inferior_ptid));
917
918 lp = add_lwp (new_ptid);
919 lp->stopped = 1;
920
921 /* This changes the thread's ptid while preserving the gdb thread
922 num. Also changes the inferior pid, while preserving the
923 inferior num. */
924 thread_change_ptid (inferior_ptid, new_ptid);
925
926 /* We've just told GDB core that the thread changed target id, but,
927 in fact, it really is a different thread, with different register
928 contents. */
929 registers_changed ();
930 }
931
932 /* Handle the exit of a single thread LP. */
933
934 static void
935 exit_lwp (struct lwp_info *lp)
936 {
937 struct thread_info *th = find_thread_ptid (lp->ptid);
938
939 if (th)
940 {
941 if (print_thread_events)
942 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
943
944 delete_thread (lp->ptid);
945 }
946
947 delete_lwp (lp->ptid);
948 }
949
950 /* Wait for the LWP specified by LP, which we have just attached to.
951 Returns a wait status for that LWP, to cache. */
952
953 static int
954 linux_nat_post_attach_wait (ptid_t ptid, int first, int *signalled)
955 {
956 pid_t new_pid, pid = ptid_get_lwp (ptid);
957 int status;
958
959 if (linux_proc_pid_is_stopped (pid))
960 {
961 if (debug_linux_nat)
962 fprintf_unfiltered (gdb_stdlog,
963 "LNPAW: Attaching to a stopped process\n");
964
965 /* The process is definitely stopped. It is in a job control
966 stop, unless the kernel predates the TASK_STOPPED /
967 TASK_TRACED distinction, in which case it might be in a
968 ptrace stop. Make sure it is in a ptrace stop; from there we
969 can kill it, signal it, et cetera.
970
971 First make sure there is a pending SIGSTOP. Since we are
972 already attached, the process can not transition from stopped
973 to running without a PTRACE_CONT; so we know this signal will
974 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
975 probably already in the queue (unless this kernel is old
976 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
977 is not an RT signal, it can only be queued once. */
978 kill_lwp (pid, SIGSTOP);
979
980 /* Finally, resume the stopped process. This will deliver the SIGSTOP
981 (or a higher priority signal, just like normal PTRACE_ATTACH). */
982 ptrace (PTRACE_CONT, pid, 0, 0);
983 }
984
985 /* Make sure the initial process is stopped. The user-level threads
986 layer might want to poke around in the inferior, and that won't
987 work if things haven't stabilized yet. */
988 new_pid = my_waitpid (pid, &status, __WALL);
989 gdb_assert (pid == new_pid);
990
991 if (!WIFSTOPPED (status))
992 {
993 /* The pid we tried to attach has apparently just exited. */
994 if (debug_linux_nat)
995 fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
996 pid, status_to_str (status));
997 return status;
998 }
999
1000 if (WSTOPSIG (status) != SIGSTOP)
1001 {
1002 *signalled = 1;
1003 if (debug_linux_nat)
1004 fprintf_unfiltered (gdb_stdlog,
1005 "LNPAW: Received %s after attaching\n",
1006 status_to_str (status));
1007 }
1008
1009 return status;
1010 }
1011
1012 static void
1013 linux_nat_create_inferior (struct target_ops *ops,
1014 char *exec_file, char *allargs, char **env,
1015 int from_tty)
1016 {
1017 struct cleanup *restore_personality
1018 = maybe_disable_address_space_randomization (disable_randomization);
1019
1020 /* The fork_child mechanism is synchronous and calls target_wait, so
1021 we have to mask the async mode. */
1022
1023 /* Make sure we report all signals during startup. */
1024 linux_nat_pass_signals (ops, 0, NULL);
1025
1026 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1027
1028 do_cleanups (restore_personality);
1029 }
1030
1031 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1032 already attached. Returns true if a new LWP is found, false
1033 otherwise. */
1034
1035 static int
1036 attach_proc_task_lwp_callback (ptid_t ptid)
1037 {
1038 struct lwp_info *lp;
1039
1040 /* Ignore LWPs we're already attached to. */
1041 lp = find_lwp_pid (ptid);
1042 if (lp == NULL)
1043 {
1044 int lwpid = ptid_get_lwp (ptid);
1045
1046 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1047 {
1048 int err = errno;
1049
1050 /* Be quiet if we simply raced with the thread exiting.
1051 EPERM is returned if the thread's task still exists, and
1052 is marked as exited or zombie, as well as other
1053 conditions, so in that case, confirm the status in
1054 /proc/PID/status. */
1055 if (err == ESRCH
1056 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1057 {
1058 if (debug_linux_nat)
1059 {
1060 fprintf_unfiltered (gdb_stdlog,
1061 "Cannot attach to lwp %d: "
1062 "thread is gone (%d: %s)\n",
1063 lwpid, err, safe_strerror (err));
1064 }
1065 }
1066 else
1067 {
1068 warning (_("Cannot attach to lwp %d: %s"),
1069 lwpid,
1070 linux_ptrace_attach_fail_reason_string (ptid,
1071 err));
1072 }
1073 }
1074 else
1075 {
1076 if (debug_linux_nat)
1077 fprintf_unfiltered (gdb_stdlog,
1078 "PTRACE_ATTACH %s, 0, 0 (OK)\n",
1079 target_pid_to_str (ptid));
1080
1081 lp = add_lwp (ptid);
1082
1083 /* The next time we wait for this LWP we'll see a SIGSTOP as
1084 PTRACE_ATTACH brings it to a halt. */
1085 lp->signalled = 1;
1086
1087 /* We need to wait for a stop before being able to make the
1088 next ptrace call on this LWP. */
1089 lp->must_set_ptrace_flags = 1;
1090 }
1091
1092 return 1;
1093 }
1094 return 0;
1095 }
1096
1097 static void
1098 linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
1099 {
1100 struct lwp_info *lp;
1101 int status;
1102 ptid_t ptid;
1103
1104 /* Make sure we report all signals during attach. */
1105 linux_nat_pass_signals (ops, 0, NULL);
1106
1107 TRY
1108 {
1109 linux_ops->to_attach (ops, args, from_tty);
1110 }
1111 CATCH (ex, RETURN_MASK_ERROR)
1112 {
1113 pid_t pid = parse_pid_to_attach (args);
1114 struct buffer buffer;
1115 char *message, *buffer_s;
1116
1117 message = xstrdup (ex.message);
1118 make_cleanup (xfree, message);
1119
1120 buffer_init (&buffer);
1121 linux_ptrace_attach_fail_reason (pid, &buffer);
1122
1123 buffer_grow_str0 (&buffer, "");
1124 buffer_s = buffer_finish (&buffer);
1125 make_cleanup (xfree, buffer_s);
1126
1127 if (*buffer_s != '\0')
1128 throw_error (ex.error, "warning: %s\n%s", buffer_s, message);
1129 else
1130 throw_error (ex.error, "%s", message);
1131 }
1132 END_CATCH
1133
1134 /* The ptrace base target adds the main thread with (pid,0,0)
1135 format. Decorate it with lwp info. */
1136 ptid = ptid_build (ptid_get_pid (inferior_ptid),
1137 ptid_get_pid (inferior_ptid),
1138 0);
1139 thread_change_ptid (inferior_ptid, ptid);
1140
1141 /* Add the initial process as the first LWP to the list. */
1142 lp = add_initial_lwp (ptid);
1143
1144 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->signalled);
1145 if (!WIFSTOPPED (status))
1146 {
1147 if (WIFEXITED (status))
1148 {
1149 int exit_code = WEXITSTATUS (status);
1150
1151 target_terminal_ours ();
1152 target_mourn_inferior ();
1153 if (exit_code == 0)
1154 error (_("Unable to attach: program exited normally."));
1155 else
1156 error (_("Unable to attach: program exited with code %d."),
1157 exit_code);
1158 }
1159 else if (WIFSIGNALED (status))
1160 {
1161 enum gdb_signal signo;
1162
1163 target_terminal_ours ();
1164 target_mourn_inferior ();
1165
1166 signo = gdb_signal_from_host (WTERMSIG (status));
1167 error (_("Unable to attach: program terminated with signal "
1168 "%s, %s."),
1169 gdb_signal_to_name (signo),
1170 gdb_signal_to_string (signo));
1171 }
1172
1173 internal_error (__FILE__, __LINE__,
1174 _("unexpected status %d for PID %ld"),
1175 status, (long) ptid_get_lwp (ptid));
1176 }
1177
1178 lp->stopped = 1;
1179
1180 /* Save the wait status to report later. */
1181 lp->resumed = 1;
1182 if (debug_linux_nat)
1183 fprintf_unfiltered (gdb_stdlog,
1184 "LNA: waitpid %ld, saving status %s\n",
1185 (long) ptid_get_pid (lp->ptid), status_to_str (status));
1186
1187 lp->status = status;
1188
1189 /* We must attach to every LWP. If /proc is mounted, use that to
1190 find them now. The inferior may be using raw clone instead of
1191 using pthreads. But even if it is using pthreads, thread_db
1192 walks structures in the inferior's address space to find the list
1193 of threads/LWPs, and those structures may well be corrupted.
1194 Note that once thread_db is loaded, we'll still use it to list
1195 threads and associate pthread info with each LWP. */
1196 linux_proc_attach_tgid_threads (ptid_get_pid (lp->ptid),
1197 attach_proc_task_lwp_callback);
1198
1199 if (target_can_async_p ())
1200 target_async (1);
1201 }
1202
1203 /* Get pending status of LP. */
1204 static int
1205 get_pending_status (struct lwp_info *lp, int *status)
1206 {
1207 enum gdb_signal signo = GDB_SIGNAL_0;
1208
1209 /* If we paused threads momentarily, we may have stored pending
1210 events in lp->status or lp->waitstatus (see stop_wait_callback),
1211 and GDB core hasn't seen any signal for those threads.
1212 Otherwise, the last signal reported to the core is found in the
1213 thread object's stop_signal.
1214
1215 There's a corner case that isn't handled here at present. Only
1216 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1217 stop_signal make sense as a real signal to pass to the inferior.
1218 Some catchpoint related events, like
1219 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1220 to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1221 those traps are debug API (ptrace in our case) related and
1222 induced; the inferior wouldn't see them if it wasn't being
1223 traced. Hence, we should never pass them to the inferior, even
1224 when set to pass state. Since this corner case isn't handled by
1225 infrun.c when proceeding with a signal, for consistency, neither
1226 do we handle it here (or elsewhere in the file we check for
1227 signal pass state). Normally SIGTRAP isn't set to pass state, so
1228 this is really a corner case. */
1229
1230 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1231 signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1232 else if (lp->status)
1233 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1234 else if (target_is_non_stop_p () && !is_executing (lp->ptid))
1235 {
1236 struct thread_info *tp = find_thread_ptid (lp->ptid);
1237
1238 signo = tp->suspend.stop_signal;
1239 }
1240 else if (!target_is_non_stop_p ())
1241 {
1242 struct target_waitstatus last;
1243 ptid_t last_ptid;
1244
1245 get_last_target_status (&last_ptid, &last);
1246
1247 if (ptid_get_lwp (lp->ptid) == ptid_get_lwp (last_ptid))
1248 {
1249 struct thread_info *tp = find_thread_ptid (lp->ptid);
1250
1251 signo = tp->suspend.stop_signal;
1252 }
1253 }
1254
1255 *status = 0;
1256
1257 if (signo == GDB_SIGNAL_0)
1258 {
1259 if (debug_linux_nat)
1260 fprintf_unfiltered (gdb_stdlog,
1261 "GPT: lwp %s has no pending signal\n",
1262 target_pid_to_str (lp->ptid));
1263 }
1264 else if (!signal_pass_state (signo))
1265 {
1266 if (debug_linux_nat)
1267 fprintf_unfiltered (gdb_stdlog,
1268 "GPT: lwp %s had signal %s, "
1269 "but it is in no pass state\n",
1270 target_pid_to_str (lp->ptid),
1271 gdb_signal_to_string (signo));
1272 }
1273 else
1274 {
1275 *status = W_STOPCODE (gdb_signal_to_host (signo));
1276
1277 if (debug_linux_nat)
1278 fprintf_unfiltered (gdb_stdlog,
1279 "GPT: lwp %s has pending signal %s\n",
1280 target_pid_to_str (lp->ptid),
1281 gdb_signal_to_string (signo));
1282 }
1283
1284 return 0;
1285 }
1286
1287 static int
1288 detach_callback (struct lwp_info *lp, void *data)
1289 {
1290 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1291
1292 if (debug_linux_nat && lp->status)
1293 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1294 strsignal (WSTOPSIG (lp->status)),
1295 target_pid_to_str (lp->ptid));
1296
1297 /* If there is a pending SIGSTOP, get rid of it. */
1298 if (lp->signalled)
1299 {
1300 if (debug_linux_nat)
1301 fprintf_unfiltered (gdb_stdlog,
1302 "DC: Sending SIGCONT to %s\n",
1303 target_pid_to_str (lp->ptid));
1304
1305 kill_lwp (ptid_get_lwp (lp->ptid), SIGCONT);
1306 lp->signalled = 0;
1307 }
1308
1309 /* We don't actually detach from the LWP that has an id equal to the
1310 overall process id just yet. */
1311 if (ptid_get_lwp (lp->ptid) != ptid_get_pid (lp->ptid))
1312 {
1313 int status = 0;
1314
1315 /* Pass on any pending signal for this LWP. */
1316 get_pending_status (lp, &status);
1317
1318 if (linux_nat_prepare_to_resume != NULL)
1319 linux_nat_prepare_to_resume (lp);
1320 errno = 0;
1321 if (ptrace (PTRACE_DETACH, ptid_get_lwp (lp->ptid), 0,
1322 WSTOPSIG (status)) < 0)
1323 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1324 safe_strerror (errno));
1325
1326 if (debug_linux_nat)
1327 fprintf_unfiltered (gdb_stdlog,
1328 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1329 target_pid_to_str (lp->ptid),
1330 strsignal (WSTOPSIG (status)));
1331
1332 delete_lwp (lp->ptid);
1333 }
1334
1335 return 0;
1336 }
1337
1338 static void
1339 linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
1340 {
1341 int pid;
1342 int status;
1343 struct lwp_info *main_lwp;
1344
1345 pid = ptid_get_pid (inferior_ptid);
1346
1347 /* Don't unregister from the event loop, as there may be other
1348 inferiors running. */
1349
1350 /* Stop all threads before detaching. ptrace requires that the
1351 thread is stopped to sucessfully detach. */
1352 iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1353 /* ... and wait until all of them have reported back that
1354 they're no longer running. */
1355 iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1356
1357 iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1358
1359 /* Only the initial process should be left right now. */
1360 gdb_assert (num_lwps (ptid_get_pid (inferior_ptid)) == 1);
1361
1362 main_lwp = find_lwp_pid (pid_to_ptid (pid));
1363
1364 /* Pass on any pending signal for the last LWP. */
1365 if ((args == NULL || *args == '\0')
1366 && get_pending_status (main_lwp, &status) != -1
1367 && WIFSTOPPED (status))
1368 {
1369 char *tem;
1370
1371 /* Put the signal number in ARGS so that inf_ptrace_detach will
1372 pass it along with PTRACE_DETACH. */
1373 tem = (char *) alloca (8);
1374 xsnprintf (tem, 8, "%d", (int) WSTOPSIG (status));
1375 args = tem;
1376 if (debug_linux_nat)
1377 fprintf_unfiltered (gdb_stdlog,
1378 "LND: Sending signal %s to %s\n",
1379 args,
1380 target_pid_to_str (main_lwp->ptid));
1381 }
1382
1383 if (linux_nat_prepare_to_resume != NULL)
1384 linux_nat_prepare_to_resume (main_lwp);
1385 delete_lwp (main_lwp->ptid);
1386
1387 if (forks_exist_p ())
1388 {
1389 /* Multi-fork case. The current inferior_ptid is being detached
1390 from, but there are other viable forks to debug. Detach from
1391 the current fork, and context-switch to the first
1392 available. */
1393 linux_fork_detach (args, from_tty);
1394 }
1395 else
1396 linux_ops->to_detach (ops, args, from_tty);
1397 }
1398
1399 /* Resume execution of the inferior process. If STEP is nonzero,
1400 single-step it. If SIGNAL is nonzero, give it that signal. */
1401
1402 static void
1403 linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
1404 enum gdb_signal signo)
1405 {
1406 lp->step = step;
1407
1408 /* stop_pc doubles as the PC the LWP had when it was last resumed.
1409 We only presently need that if the LWP is stepped though (to
1410 handle the case of stepping a breakpoint instruction). */
1411 if (step)
1412 {
1413 struct regcache *regcache = get_thread_regcache (lp->ptid);
1414
1415 lp->stop_pc = regcache_read_pc (regcache);
1416 }
1417 else
1418 lp->stop_pc = 0;
1419
1420 if (linux_nat_prepare_to_resume != NULL)
1421 linux_nat_prepare_to_resume (lp);
1422 linux_ops->to_resume (linux_ops, lp->ptid, step, signo);
1423
1424 /* Successfully resumed. Clear state that no longer makes sense,
1425 and mark the LWP as running. Must not do this before resuming
1426 otherwise if that fails other code will be confused. E.g., we'd
1427 later try to stop the LWP and hang forever waiting for a stop
1428 status. Note that we must not throw after this is cleared,
1429 otherwise handle_zombie_lwp_error would get confused. */
1430 lp->stopped = 0;
1431 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1432 registers_changed_ptid (lp->ptid);
1433 }
1434
1435 /* Called when we try to resume a stopped LWP and that errors out. If
1436 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
1437 or about to become), discard the error, clear any pending status
1438 the LWP may have, and return true (we'll collect the exit status
1439 soon enough). Otherwise, return false. */
1440
1441 static int
1442 check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
1443 {
1444 /* If we get an error after resuming the LWP successfully, we'd
1445 confuse !T state for the LWP being gone. */
1446 gdb_assert (lp->stopped);
1447
1448 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
1449 because even if ptrace failed with ESRCH, the tracee may be "not
1450 yet fully dead", but already refusing ptrace requests. In that
1451 case the tracee has 'R (Running)' state for a little bit
1452 (observed in Linux 3.18). See also the note on ESRCH in the
1453 ptrace(2) man page. Instead, check whether the LWP has any state
1454 other than ptrace-stopped. */
1455
1456 /* Don't assume anything if /proc/PID/status can't be read. */
1457 if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp->ptid)) == 0)
1458 {
1459 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
1460 lp->status = 0;
1461 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1462 return 1;
1463 }
1464 return 0;
1465 }
1466
1467 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
1468 disappears while we try to resume it. */
1469
1470 static void
1471 linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1472 {
1473 TRY
1474 {
1475 linux_resume_one_lwp_throw (lp, step, signo);
1476 }
1477 CATCH (ex, RETURN_MASK_ERROR)
1478 {
1479 if (!check_ptrace_stopped_lwp_gone (lp))
1480 throw_exception (ex);
1481 }
1482 END_CATCH
1483 }
1484
1485 /* Resume LP. */
1486
1487 static void
1488 resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1489 {
1490 if (lp->stopped)
1491 {
1492 struct inferior *inf = find_inferior_ptid (lp->ptid);
1493
1494 if (inf->vfork_child != NULL)
1495 {
1496 if (debug_linux_nat)
1497 fprintf_unfiltered (gdb_stdlog,
1498 "RC: Not resuming %s (vfork parent)\n",
1499 target_pid_to_str (lp->ptid));
1500 }
1501 else if (!lwp_status_pending_p (lp))
1502 {
1503 if (debug_linux_nat)
1504 fprintf_unfiltered (gdb_stdlog,
1505 "RC: Resuming sibling %s, %s, %s\n",
1506 target_pid_to_str (lp->ptid),
1507 (signo != GDB_SIGNAL_0
1508 ? strsignal (gdb_signal_to_host (signo))
1509 : "0"),
1510 step ? "step" : "resume");
1511
1512 linux_resume_one_lwp (lp, step, signo);
1513 }
1514 else
1515 {
1516 if (debug_linux_nat)
1517 fprintf_unfiltered (gdb_stdlog,
1518 "RC: Not resuming sibling %s (has pending)\n",
1519 target_pid_to_str (lp->ptid));
1520 }
1521 }
1522 else
1523 {
1524 if (debug_linux_nat)
1525 fprintf_unfiltered (gdb_stdlog,
1526 "RC: Not resuming sibling %s (not stopped)\n",
1527 target_pid_to_str (lp->ptid));
1528 }
1529 }
1530
1531 /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
1532 Resume LWP with the last stop signal, if it is in pass state. */
1533
1534 static int
1535 linux_nat_resume_callback (struct lwp_info *lp, void *except)
1536 {
1537 enum gdb_signal signo = GDB_SIGNAL_0;
1538
1539 if (lp == except)
1540 return 0;
1541
1542 if (lp->stopped)
1543 {
1544 struct thread_info *thread;
1545
1546 thread = find_thread_ptid (lp->ptid);
1547 if (thread != NULL)
1548 {
1549 signo = thread->suspend.stop_signal;
1550 thread->suspend.stop_signal = GDB_SIGNAL_0;
1551 }
1552 }
1553
1554 resume_lwp (lp, 0, signo);
1555 return 0;
1556 }
1557
1558 static int
1559 resume_clear_callback (struct lwp_info *lp, void *data)
1560 {
1561 lp->resumed = 0;
1562 lp->last_resume_kind = resume_stop;
1563 return 0;
1564 }
1565
1566 static int
1567 resume_set_callback (struct lwp_info *lp, void *data)
1568 {
1569 lp->resumed = 1;
1570 lp->last_resume_kind = resume_continue;
1571 return 0;
1572 }
1573
1574 static void
1575 linux_nat_resume (struct target_ops *ops,
1576 ptid_t ptid, int step, enum gdb_signal signo)
1577 {
1578 struct lwp_info *lp;
1579 int resume_many;
1580
1581 if (debug_linux_nat)
1582 fprintf_unfiltered (gdb_stdlog,
1583 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1584 step ? "step" : "resume",
1585 target_pid_to_str (ptid),
1586 (signo != GDB_SIGNAL_0
1587 ? strsignal (gdb_signal_to_host (signo)) : "0"),
1588 target_pid_to_str (inferior_ptid));
1589
1590 /* A specific PTID means `step only this process id'. */
1591 resume_many = (ptid_equal (minus_one_ptid, ptid)
1592 || ptid_is_pid (ptid));
1593
1594 /* Mark the lwps we're resuming as resumed. */
1595 iterate_over_lwps (ptid, resume_set_callback, NULL);
1596
1597 /* See if it's the current inferior that should be handled
1598 specially. */
1599 if (resume_many)
1600 lp = find_lwp_pid (inferior_ptid);
1601 else
1602 lp = find_lwp_pid (ptid);
1603 gdb_assert (lp != NULL);
1604
1605 /* Remember if we're stepping. */
1606 lp->last_resume_kind = step ? resume_step : resume_continue;
1607
1608 /* If we have a pending wait status for this thread, there is no
1609 point in resuming the process. But first make sure that
1610 linux_nat_wait won't preemptively handle the event - we
1611 should never take this short-circuit if we are going to
1612 leave LP running, since we have skipped resuming all the
1613 other threads. This bit of code needs to be synchronized
1614 with linux_nat_wait. */
1615
1616 if (lp->status && WIFSTOPPED (lp->status))
1617 {
1618 if (!lp->step
1619 && WSTOPSIG (lp->status)
1620 && sigismember (&pass_mask, WSTOPSIG (lp->status)))
1621 {
1622 if (debug_linux_nat)
1623 fprintf_unfiltered (gdb_stdlog,
1624 "LLR: Not short circuiting for ignored "
1625 "status 0x%x\n", lp->status);
1626
1627 /* FIXME: What should we do if we are supposed to continue
1628 this thread with a signal? */
1629 gdb_assert (signo == GDB_SIGNAL_0);
1630 signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1631 lp->status = 0;
1632 }
1633 }
1634
1635 if (lwp_status_pending_p (lp))
1636 {
1637 /* FIXME: What should we do if we are supposed to continue
1638 this thread with a signal? */
1639 gdb_assert (signo == GDB_SIGNAL_0);
1640
1641 if (debug_linux_nat)
1642 fprintf_unfiltered (gdb_stdlog,
1643 "LLR: Short circuiting for status 0x%x\n",
1644 lp->status);
1645
1646 if (target_can_async_p ())
1647 {
1648 target_async (1);
1649 /* Tell the event loop we have something to process. */
1650 async_file_mark ();
1651 }
1652 return;
1653 }
1654
1655 if (resume_many)
1656 iterate_over_lwps (ptid, linux_nat_resume_callback, lp);
1657
1658 if (debug_linux_nat)
1659 fprintf_unfiltered (gdb_stdlog,
1660 "LLR: %s %s, %s (resume event thread)\n",
1661 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1662 target_pid_to_str (lp->ptid),
1663 (signo != GDB_SIGNAL_0
1664 ? strsignal (gdb_signal_to_host (signo)) : "0"));
1665
1666 linux_resume_one_lwp (lp, step, signo);
1667
1668 if (target_can_async_p ())
1669 target_async (1);
1670 }
1671
1672 /* Send a signal to an LWP. */
1673
1674 static int
1675 kill_lwp (int lwpid, int signo)
1676 {
1677 int ret;
1678
1679 errno = 0;
1680 ret = syscall (__NR_tkill, lwpid, signo);
1681 if (errno == ENOSYS)
1682 {
1683 /* If tkill fails, then we are not using nptl threads, a
1684 configuration we no longer support. */
1685 perror_with_name (("tkill"));
1686 }
1687 return ret;
1688 }
1689
1690 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1691 event, check if the core is interested in it: if not, ignore the
1692 event, and keep waiting; otherwise, we need to toggle the LWP's
1693 syscall entry/exit status, since the ptrace event itself doesn't
1694 indicate it, and report the trap to higher layers. */
1695
1696 static int
1697 linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
1698 {
1699 struct target_waitstatus *ourstatus = &lp->waitstatus;
1700 struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
1701 int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
1702
1703 if (stopping)
1704 {
1705 /* If we're stopping threads, there's a SIGSTOP pending, which
1706 makes it so that the LWP reports an immediate syscall return,
1707 followed by the SIGSTOP. Skip seeing that "return" using
1708 PTRACE_CONT directly, and let stop_wait_callback collect the
1709 SIGSTOP. Later when the thread is resumed, a new syscall
1710 entry event. If we didn't do this (and returned 0), we'd
1711 leave a syscall entry pending, and our caller, by using
1712 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1713 itself. Later, when the user re-resumes this LWP, we'd see
1714 another syscall entry event and we'd mistake it for a return.
1715
1716 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1717 (leaving immediately with LWP->signalled set, without issuing
1718 a PTRACE_CONT), it would still be problematic to leave this
1719 syscall enter pending, as later when the thread is resumed,
1720 it would then see the same syscall exit mentioned above,
1721 followed by the delayed SIGSTOP, while the syscall didn't
1722 actually get to execute. It seems it would be even more
1723 confusing to the user. */
1724
1725 if (debug_linux_nat)
1726 fprintf_unfiltered (gdb_stdlog,
1727 "LHST: ignoring syscall %d "
1728 "for LWP %ld (stopping threads), "
1729 "resuming with PTRACE_CONT for SIGSTOP\n",
1730 syscall_number,
1731 ptid_get_lwp (lp->ptid));
1732
1733 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1734 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
1735 lp->stopped = 0;
1736 return 1;
1737 }
1738
1739 /* Always update the entry/return state, even if this particular
1740 syscall isn't interesting to the core now. In async mode,
1741 the user could install a new catchpoint for this syscall
1742 between syscall enter/return, and we'll need to know to
1743 report a syscall return if that happens. */
1744 lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1745 ? TARGET_WAITKIND_SYSCALL_RETURN
1746 : TARGET_WAITKIND_SYSCALL_ENTRY);
1747
1748 if (catch_syscall_enabled ())
1749 {
1750 if (catching_syscall_number (syscall_number))
1751 {
1752 /* Alright, an event to report. */
1753 ourstatus->kind = lp->syscall_state;
1754 ourstatus->value.syscall_number = syscall_number;
1755
1756 if (debug_linux_nat)
1757 fprintf_unfiltered (gdb_stdlog,
1758 "LHST: stopping for %s of syscall %d"
1759 " for LWP %ld\n",
1760 lp->syscall_state
1761 == TARGET_WAITKIND_SYSCALL_ENTRY
1762 ? "entry" : "return",
1763 syscall_number,
1764 ptid_get_lwp (lp->ptid));
1765 return 0;
1766 }
1767
1768 if (debug_linux_nat)
1769 fprintf_unfiltered (gdb_stdlog,
1770 "LHST: ignoring %s of syscall %d "
1771 "for LWP %ld\n",
1772 lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1773 ? "entry" : "return",
1774 syscall_number,
1775 ptid_get_lwp (lp->ptid));
1776 }
1777 else
1778 {
1779 /* If we had been syscall tracing, and hence used PT_SYSCALL
1780 before on this LWP, it could happen that the user removes all
1781 syscall catchpoints before we get to process this event.
1782 There are two noteworthy issues here:
1783
1784 - When stopped at a syscall entry event, resuming with
1785 PT_STEP still resumes executing the syscall and reports a
1786 syscall return.
1787
1788 - Only PT_SYSCALL catches syscall enters. If we last
1789 single-stepped this thread, then this event can't be a
1790 syscall enter. If we last single-stepped this thread, this
1791 has to be a syscall exit.
1792
1793 The points above mean that the next resume, be it PT_STEP or
1794 PT_CONTINUE, can not trigger a syscall trace event. */
1795 if (debug_linux_nat)
1796 fprintf_unfiltered (gdb_stdlog,
1797 "LHST: caught syscall event "
1798 "with no syscall catchpoints."
1799 " %d for LWP %ld, ignoring\n",
1800 syscall_number,
1801 ptid_get_lwp (lp->ptid));
1802 lp->syscall_state = TARGET_WAITKIND_IGNORE;
1803 }
1804
1805 /* The core isn't interested in this event. For efficiency, avoid
1806 stopping all threads only to have the core resume them all again.
1807 Since we're not stopping threads, if we're still syscall tracing
1808 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1809 subsequent syscall. Simply resume using the inf-ptrace layer,
1810 which knows when to use PT_SYSCALL or PT_CONTINUE. */
1811
1812 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
1813 return 1;
1814 }
1815
1816 /* Handle a GNU/Linux extended wait response. If we see a clone
1817 event, we need to add the new LWP to our list (and not report the
1818 trap to higher layers). This function returns non-zero if the
1819 event should be ignored and we should wait again. If STOPPING is
1820 true, the new LWP remains stopped, otherwise it is continued. */
1821
1822 static int
1823 linux_handle_extended_wait (struct lwp_info *lp, int status)
1824 {
1825 int pid = ptid_get_lwp (lp->ptid);
1826 struct target_waitstatus *ourstatus = &lp->waitstatus;
1827 int event = linux_ptrace_get_extended_event (status);
1828
1829 /* All extended events we currently use are mid-syscall. Only
1830 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
1831 you have to be using PTRACE_SEIZE to get that. */
1832 lp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
1833
1834 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
1835 || event == PTRACE_EVENT_CLONE)
1836 {
1837 unsigned long new_pid;
1838 int ret;
1839
1840 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
1841
1842 /* If we haven't already seen the new PID stop, wait for it now. */
1843 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
1844 {
1845 /* The new child has a pending SIGSTOP. We can't affect it until it
1846 hits the SIGSTOP, but we're already attached. */
1847 ret = my_waitpid (new_pid, &status, __WALL);
1848 if (ret == -1)
1849 perror_with_name (_("waiting for new child"));
1850 else if (ret != new_pid)
1851 internal_error (__FILE__, __LINE__,
1852 _("wait returned unexpected PID %d"), ret);
1853 else if (!WIFSTOPPED (status))
1854 internal_error (__FILE__, __LINE__,
1855 _("wait returned unexpected status 0x%x"), status);
1856 }
1857
1858 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
1859
1860 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
1861 {
1862 /* The arch-specific native code may need to know about new
1863 forks even if those end up never mapped to an
1864 inferior. */
1865 if (linux_nat_new_fork != NULL)
1866 linux_nat_new_fork (lp, new_pid);
1867 }
1868
1869 if (event == PTRACE_EVENT_FORK
1870 && linux_fork_checkpointing_p (ptid_get_pid (lp->ptid)))
1871 {
1872 /* Handle checkpointing by linux-fork.c here as a special
1873 case. We don't want the follow-fork-mode or 'catch fork'
1874 to interfere with this. */
1875
1876 /* This won't actually modify the breakpoint list, but will
1877 physically remove the breakpoints from the child. */
1878 detach_breakpoints (ptid_build (new_pid, new_pid, 0));
1879
1880 /* Retain child fork in ptrace (stopped) state. */
1881 if (!find_fork_pid (new_pid))
1882 add_fork (new_pid);
1883
1884 /* Report as spurious, so that infrun doesn't want to follow
1885 this fork. We're actually doing an infcall in
1886 linux-fork.c. */
1887 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1888
1889 /* Report the stop to the core. */
1890 return 0;
1891 }
1892
1893 if (event == PTRACE_EVENT_FORK)
1894 ourstatus->kind = TARGET_WAITKIND_FORKED;
1895 else if (event == PTRACE_EVENT_VFORK)
1896 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1897 else if (event == PTRACE_EVENT_CLONE)
1898 {
1899 struct lwp_info *new_lp;
1900
1901 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1902
1903 if (debug_linux_nat)
1904 fprintf_unfiltered (gdb_stdlog,
1905 "LHEW: Got clone event "
1906 "from LWP %d, new child is LWP %ld\n",
1907 pid, new_pid);
1908
1909 new_lp = add_lwp (ptid_build (ptid_get_pid (lp->ptid), new_pid, 0));
1910 new_lp->stopped = 1;
1911 new_lp->resumed = 1;
1912
1913 /* If the thread_db layer is active, let it record the user
1914 level thread id and status, and add the thread to GDB's
1915 list. */
1916 if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
1917 {
1918 /* The process is not using thread_db. Add the LWP to
1919 GDB's list. */
1920 target_post_attach (ptid_get_lwp (new_lp->ptid));
1921 add_thread (new_lp->ptid);
1922 }
1923
1924 /* Even if we're stopping the thread for some reason
1925 internal to this module, from the perspective of infrun
1926 and the user/frontend, this new thread is running until
1927 it next reports a stop. */
1928 set_running (new_lp->ptid, 1);
1929 set_executing (new_lp->ptid, 1);
1930
1931 if (WSTOPSIG (status) != SIGSTOP)
1932 {
1933 /* This can happen if someone starts sending signals to
1934 the new thread before it gets a chance to run, which
1935 have a lower number than SIGSTOP (e.g. SIGUSR1).
1936 This is an unlikely case, and harder to handle for
1937 fork / vfork than for clone, so we do not try - but
1938 we handle it for clone events here. */
1939
1940 new_lp->signalled = 1;
1941
1942 /* We created NEW_LP so it cannot yet contain STATUS. */
1943 gdb_assert (new_lp->status == 0);
1944
1945 /* Save the wait status to report later. */
1946 if (debug_linux_nat)
1947 fprintf_unfiltered (gdb_stdlog,
1948 "LHEW: waitpid of new LWP %ld, "
1949 "saving status %s\n",
1950 (long) ptid_get_lwp (new_lp->ptid),
1951 status_to_str (status));
1952 new_lp->status = status;
1953 }
1954
1955 return 1;
1956 }
1957
1958 return 0;
1959 }
1960
1961 if (event == PTRACE_EVENT_EXEC)
1962 {
1963 if (debug_linux_nat)
1964 fprintf_unfiltered (gdb_stdlog,
1965 "LHEW: Got exec event from LWP %ld\n",
1966 ptid_get_lwp (lp->ptid));
1967
1968 ourstatus->kind = TARGET_WAITKIND_EXECD;
1969 ourstatus->value.execd_pathname
1970 = xstrdup (linux_child_pid_to_exec_file (NULL, pid));
1971
1972 /* The thread that execed must have been resumed, but, when a
1973 thread execs, it changes its tid to the tgid, and the old
1974 tgid thread might have not been resumed. */
1975 lp->resumed = 1;
1976 return 0;
1977 }
1978
1979 if (event == PTRACE_EVENT_VFORK_DONE)
1980 {
1981 if (current_inferior ()->waiting_for_vfork_done)
1982 {
1983 if (debug_linux_nat)
1984 fprintf_unfiltered (gdb_stdlog,
1985 "LHEW: Got expected PTRACE_EVENT_"
1986 "VFORK_DONE from LWP %ld: stopping\n",
1987 ptid_get_lwp (lp->ptid));
1988
1989 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1990 return 0;
1991 }
1992
1993 if (debug_linux_nat)
1994 fprintf_unfiltered (gdb_stdlog,
1995 "LHEW: Got PTRACE_EVENT_VFORK_DONE "
1996 "from LWP %ld: ignoring\n",
1997 ptid_get_lwp (lp->ptid));
1998 return 1;
1999 }
2000
2001 internal_error (__FILE__, __LINE__,
2002 _("unknown ptrace event %d"), event);
2003 }
2004
2005 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2006 exited. */
2007
2008 static int
2009 wait_lwp (struct lwp_info *lp)
2010 {
2011 pid_t pid;
2012 int status = 0;
2013 int thread_dead = 0;
2014 sigset_t prev_mask;
2015
2016 gdb_assert (!lp->stopped);
2017 gdb_assert (lp->status == 0);
2018
2019 /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2020 block_child_signals (&prev_mask);
2021
2022 for (;;)
2023 {
2024 pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, __WALL | WNOHANG);
2025 if (pid == -1 && errno == ECHILD)
2026 {
2027 /* The thread has previously exited. We need to delete it
2028 now because if this was a non-leader thread execing, we
2029 won't get an exit event. See comments on exec events at
2030 the top of the file. */
2031 thread_dead = 1;
2032 if (debug_linux_nat)
2033 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2034 target_pid_to_str (lp->ptid));
2035 }
2036 if (pid != 0)
2037 break;
2038
2039 /* Bugs 10970, 12702.
2040 Thread group leader may have exited in which case we'll lock up in
2041 waitpid if there are other threads, even if they are all zombies too.
2042 Basically, we're not supposed to use waitpid this way.
2043 tkill(pid,0) cannot be used here as it gets ESRCH for both
2044 for zombie and running processes.
2045
2046 As a workaround, check if we're waiting for the thread group leader and
2047 if it's a zombie, and avoid calling waitpid if it is.
2048
2049 This is racy, what if the tgl becomes a zombie right after we check?
2050 Therefore always use WNOHANG with sigsuspend - it is equivalent to
2051 waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2052
2053 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid)
2054 && linux_proc_pid_is_zombie (ptid_get_lwp (lp->ptid)))
2055 {
2056 thread_dead = 1;
2057 if (debug_linux_nat)
2058 fprintf_unfiltered (gdb_stdlog,
2059 "WL: Thread group leader %s vanished.\n",
2060 target_pid_to_str (lp->ptid));
2061 break;
2062 }
2063
2064 /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2065 get invoked despite our caller had them intentionally blocked by
2066 block_child_signals. This is sensitive only to the loop of
2067 linux_nat_wait_1 and there if we get called my_waitpid gets called
2068 again before it gets to sigsuspend so we can safely let the handlers
2069 get executed here. */
2070
2071 if (debug_linux_nat)
2072 fprintf_unfiltered (gdb_stdlog, "WL: about to sigsuspend\n");
2073 sigsuspend (&suspend_mask);
2074 }
2075
2076 restore_child_signals_mask (&prev_mask);
2077
2078 if (!thread_dead)
2079 {
2080 gdb_assert (pid == ptid_get_lwp (lp->ptid));
2081
2082 if (debug_linux_nat)
2083 {
2084 fprintf_unfiltered (gdb_stdlog,
2085 "WL: waitpid %s received %s\n",
2086 target_pid_to_str (lp->ptid),
2087 status_to_str (status));
2088 }
2089
2090 /* Check if the thread has exited. */
2091 if (WIFEXITED (status) || WIFSIGNALED (status))
2092 {
2093 if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
2094 {
2095 if (debug_linux_nat)
2096 fprintf_unfiltered (gdb_stdlog, "WL: Process %d exited.\n",
2097 ptid_get_pid (lp->ptid));
2098
2099 /* This is the leader exiting, it means the whole
2100 process is gone. Store the status to report to the
2101 core. Store it in lp->waitstatus, because lp->status
2102 would be ambiguous (W_EXITCODE(0,0) == 0). */
2103 store_waitstatus (&lp->waitstatus, status);
2104 return 0;
2105 }
2106
2107 thread_dead = 1;
2108 if (debug_linux_nat)
2109 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2110 target_pid_to_str (lp->ptid));
2111 }
2112 }
2113
2114 if (thread_dead)
2115 {
2116 exit_lwp (lp);
2117 return 0;
2118 }
2119
2120 gdb_assert (WIFSTOPPED (status));
2121 lp->stopped = 1;
2122
2123 if (lp->must_set_ptrace_flags)
2124 {
2125 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2126 int options = linux_nat_ptrace_options (inf->attach_flag);
2127
2128 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
2129 lp->must_set_ptrace_flags = 0;
2130 }
2131
2132 /* Handle GNU/Linux's syscall SIGTRAPs. */
2133 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2134 {
2135 /* No longer need the sysgood bit. The ptrace event ends up
2136 recorded in lp->waitstatus if we care for it. We can carry
2137 on handling the event like a regular SIGTRAP from here
2138 on. */
2139 status = W_STOPCODE (SIGTRAP);
2140 if (linux_handle_syscall_trap (lp, 1))
2141 return wait_lwp (lp);
2142 }
2143 else
2144 {
2145 /* Almost all other ptrace-stops are known to be outside of system
2146 calls, with further exceptions in linux_handle_extended_wait. */
2147 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2148 }
2149
2150 /* Handle GNU/Linux's extended waitstatus for trace events. */
2151 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
2152 && linux_is_extended_waitstatus (status))
2153 {
2154 if (debug_linux_nat)
2155 fprintf_unfiltered (gdb_stdlog,
2156 "WL: Handling extended status 0x%06x\n",
2157 status);
2158 linux_handle_extended_wait (lp, status);
2159 return 0;
2160 }
2161
2162 return status;
2163 }
2164
2165 /* Send a SIGSTOP to LP. */
2166
2167 static int
2168 stop_callback (struct lwp_info *lp, void *data)
2169 {
2170 if (!lp->stopped && !lp->signalled)
2171 {
2172 int ret;
2173
2174 if (debug_linux_nat)
2175 {
2176 fprintf_unfiltered (gdb_stdlog,
2177 "SC: kill %s **<SIGSTOP>**\n",
2178 target_pid_to_str (lp->ptid));
2179 }
2180 errno = 0;
2181 ret = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
2182 if (debug_linux_nat)
2183 {
2184 fprintf_unfiltered (gdb_stdlog,
2185 "SC: lwp kill %d %s\n",
2186 ret,
2187 errno ? safe_strerror (errno) : "ERRNO-OK");
2188 }
2189
2190 lp->signalled = 1;
2191 gdb_assert (lp->status == 0);
2192 }
2193
2194 return 0;
2195 }
2196
2197 /* Request a stop on LWP. */
2198
2199 void
2200 linux_stop_lwp (struct lwp_info *lwp)
2201 {
2202 stop_callback (lwp, NULL);
2203 }
2204
2205 /* See linux-nat.h */
2206
2207 void
2208 linux_stop_and_wait_all_lwps (void)
2209 {
2210 /* Stop all LWP's ... */
2211 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
2212
2213 /* ... and wait until all of them have reported back that
2214 they're no longer running. */
2215 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
2216 }
2217
2218 /* See linux-nat.h */
2219
2220 void
2221 linux_unstop_all_lwps (void)
2222 {
2223 iterate_over_lwps (minus_one_ptid,
2224 resume_stopped_resumed_lwps, &minus_one_ptid);
2225 }
2226
2227 /* Return non-zero if LWP PID has a pending SIGINT. */
2228
2229 static int
2230 linux_nat_has_pending_sigint (int pid)
2231 {
2232 sigset_t pending, blocked, ignored;
2233
2234 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2235
2236 if (sigismember (&pending, SIGINT)
2237 && !sigismember (&ignored, SIGINT))
2238 return 1;
2239
2240 return 0;
2241 }
2242
2243 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2244
2245 static int
2246 set_ignore_sigint (struct lwp_info *lp, void *data)
2247 {
2248 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2249 flag to consume the next one. */
2250 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2251 && WSTOPSIG (lp->status) == SIGINT)
2252 lp->status = 0;
2253 else
2254 lp->ignore_sigint = 1;
2255
2256 return 0;
2257 }
2258
2259 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2260 This function is called after we know the LWP has stopped; if the LWP
2261 stopped before the expected SIGINT was delivered, then it will never have
2262 arrived. Also, if the signal was delivered to a shared queue and consumed
2263 by a different thread, it will never be delivered to this LWP. */
2264
2265 static void
2266 maybe_clear_ignore_sigint (struct lwp_info *lp)
2267 {
2268 if (!lp->ignore_sigint)
2269 return;
2270
2271 if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp->ptid)))
2272 {
2273 if (debug_linux_nat)
2274 fprintf_unfiltered (gdb_stdlog,
2275 "MCIS: Clearing bogus flag for %s\n",
2276 target_pid_to_str (lp->ptid));
2277 lp->ignore_sigint = 0;
2278 }
2279 }
2280
2281 /* Fetch the possible triggered data watchpoint info and store it in
2282 LP.
2283
2284 On some archs, like x86, that use debug registers to set
2285 watchpoints, it's possible that the way to know which watched
2286 address trapped, is to check the register that is used to select
2287 which address to watch. Problem is, between setting the watchpoint
2288 and reading back which data address trapped, the user may change
2289 the set of watchpoints, and, as a consequence, GDB changes the
2290 debug registers in the inferior. To avoid reading back a stale
2291 stopped-data-address when that happens, we cache in LP the fact
2292 that a watchpoint trapped, and the corresponding data address, as
2293 soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2294 registers meanwhile, we have the cached data we can rely on. */
2295
2296 static int
2297 check_stopped_by_watchpoint (struct lwp_info *lp)
2298 {
2299 struct cleanup *old_chain;
2300
2301 if (linux_ops->to_stopped_by_watchpoint == NULL)
2302 return 0;
2303
2304 old_chain = save_inferior_ptid ();
2305 inferior_ptid = lp->ptid;
2306
2307 if (linux_ops->to_stopped_by_watchpoint (linux_ops))
2308 {
2309 lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2310
2311 if (linux_ops->to_stopped_data_address != NULL)
2312 lp->stopped_data_address_p =
2313 linux_ops->to_stopped_data_address (&current_target,
2314 &lp->stopped_data_address);
2315 else
2316 lp->stopped_data_address_p = 0;
2317 }
2318
2319 do_cleanups (old_chain);
2320
2321 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2322 }
2323
2324 /* Called when the LWP stopped for a trap that could be explained by a
2325 watchpoint or a breakpoint. */
2326
2327 static void
2328 save_sigtrap (struct lwp_info *lp)
2329 {
2330 gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
2331 gdb_assert (lp->status != 0);
2332
2333 /* Check first if this was a SW/HW breakpoint before checking
2334 watchpoints, because at least s390 can't tell the data address of
2335 hardware watchpoint hits, and the kernel returns
2336 stopped-by-watchpoint as long as there's a watchpoint set. */
2337 if (linux_nat_status_is_event (lp->status))
2338 check_stopped_by_breakpoint (lp);
2339
2340 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2341 or hardware watchpoint. Check which is which if we got
2342 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2343 if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON
2344 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2345 check_stopped_by_watchpoint (lp);
2346 }
2347
2348 /* Returns true if the LWP had stopped for a watchpoint. */
2349
2350 static int
2351 linux_nat_stopped_by_watchpoint (struct target_ops *ops)
2352 {
2353 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2354
2355 gdb_assert (lp != NULL);
2356
2357 return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2358 }
2359
2360 static int
2361 linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2362 {
2363 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2364
2365 gdb_assert (lp != NULL);
2366
2367 *addr_p = lp->stopped_data_address;
2368
2369 return lp->stopped_data_address_p;
2370 }
2371
2372 /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2373
2374 static int
2375 sigtrap_is_event (int status)
2376 {
2377 return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2378 }
2379
2380 /* Set alternative SIGTRAP-like events recognizer. If
2381 breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2382 applied. */
2383
2384 void
2385 linux_nat_set_status_is_event (struct target_ops *t,
2386 int (*status_is_event) (int status))
2387 {
2388 linux_nat_status_is_event = status_is_event;
2389 }
2390
2391 /* Wait until LP is stopped. */
2392
2393 static int
2394 stop_wait_callback (struct lwp_info *lp, void *data)
2395 {
2396 struct inferior *inf = find_inferior_ptid (lp->ptid);
2397
2398 /* If this is a vfork parent, bail out, it is not going to report
2399 any SIGSTOP until the vfork is done with. */
2400 if (inf->vfork_child != NULL)
2401 return 0;
2402
2403 if (!lp->stopped)
2404 {
2405 int status;
2406
2407 status = wait_lwp (lp);
2408 if (status == 0)
2409 return 0;
2410
2411 if (lp->ignore_sigint && WIFSTOPPED (status)
2412 && WSTOPSIG (status) == SIGINT)
2413 {
2414 lp->ignore_sigint = 0;
2415
2416 errno = 0;
2417 ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
2418 lp->stopped = 0;
2419 if (debug_linux_nat)
2420 fprintf_unfiltered (gdb_stdlog,
2421 "PTRACE_CONT %s, 0, 0 (%s) "
2422 "(discarding SIGINT)\n",
2423 target_pid_to_str (lp->ptid),
2424 errno ? safe_strerror (errno) : "OK");
2425
2426 return stop_wait_callback (lp, NULL);
2427 }
2428
2429 maybe_clear_ignore_sigint (lp);
2430
2431 if (WSTOPSIG (status) != SIGSTOP)
2432 {
2433 /* The thread was stopped with a signal other than SIGSTOP. */
2434
2435 if (debug_linux_nat)
2436 fprintf_unfiltered (gdb_stdlog,
2437 "SWC: Pending event %s in %s\n",
2438 status_to_str ((int) status),
2439 target_pid_to_str (lp->ptid));
2440
2441 /* Save the sigtrap event. */
2442 lp->status = status;
2443 gdb_assert (lp->signalled);
2444 save_sigtrap (lp);
2445 }
2446 else
2447 {
2448 /* We caught the SIGSTOP that we intended to catch, so
2449 there's no SIGSTOP pending. */
2450
2451 if (debug_linux_nat)
2452 fprintf_unfiltered (gdb_stdlog,
2453 "SWC: Expected SIGSTOP caught for %s.\n",
2454 target_pid_to_str (lp->ptid));
2455
2456 /* Reset SIGNALLED only after the stop_wait_callback call
2457 above as it does gdb_assert on SIGNALLED. */
2458 lp->signalled = 0;
2459 }
2460 }
2461
2462 return 0;
2463 }
2464
2465 /* Return non-zero if LP has a wait status pending. Discard the
2466 pending event and resume the LWP if the event that originally
2467 caused the stop became uninteresting. */
2468
2469 static int
2470 status_callback (struct lwp_info *lp, void *data)
2471 {
2472 /* Only report a pending wait status if we pretend that this has
2473 indeed been resumed. */
2474 if (!lp->resumed)
2475 return 0;
2476
2477 if (!lwp_status_pending_p (lp))
2478 return 0;
2479
2480 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
2481 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
2482 {
2483 struct regcache *regcache = get_thread_regcache (lp->ptid);
2484 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2485 CORE_ADDR pc;
2486 int discard = 0;
2487
2488 pc = regcache_read_pc (regcache);
2489
2490 if (pc != lp->stop_pc)
2491 {
2492 if (debug_linux_nat)
2493 fprintf_unfiltered (gdb_stdlog,
2494 "SC: PC of %s changed. was=%s, now=%s\n",
2495 target_pid_to_str (lp->ptid),
2496 paddress (target_gdbarch (), lp->stop_pc),
2497 paddress (target_gdbarch (), pc));
2498 discard = 1;
2499 }
2500
2501 #if !USE_SIGTRAP_SIGINFO
2502 else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2503 {
2504 if (debug_linux_nat)
2505 fprintf_unfiltered (gdb_stdlog,
2506 "SC: previous breakpoint of %s, at %s gone\n",
2507 target_pid_to_str (lp->ptid),
2508 paddress (target_gdbarch (), lp->stop_pc));
2509
2510 discard = 1;
2511 }
2512 #endif
2513
2514 if (discard)
2515 {
2516 if (debug_linux_nat)
2517 fprintf_unfiltered (gdb_stdlog,
2518 "SC: pending event of %s cancelled.\n",
2519 target_pid_to_str (lp->ptid));
2520
2521 lp->status = 0;
2522 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
2523 return 0;
2524 }
2525 }
2526
2527 return 1;
2528 }
2529
2530 /* Count the LWP's that have had events. */
2531
2532 static int
2533 count_events_callback (struct lwp_info *lp, void *data)
2534 {
2535 int *count = (int *) data;
2536
2537 gdb_assert (count != NULL);
2538
2539 /* Select only resumed LWPs that have an event pending. */
2540 if (lp->resumed && lwp_status_pending_p (lp))
2541 (*count)++;
2542
2543 return 0;
2544 }
2545
2546 /* Select the LWP (if any) that is currently being single-stepped. */
2547
2548 static int
2549 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2550 {
2551 if (lp->last_resume_kind == resume_step
2552 && lp->status != 0)
2553 return 1;
2554 else
2555 return 0;
2556 }
2557
2558 /* Returns true if LP has a status pending. */
2559
2560 static int
2561 lwp_status_pending_p (struct lwp_info *lp)
2562 {
2563 /* We check for lp->waitstatus in addition to lp->status, because we
2564 can have pending process exits recorded in lp->status and
2565 W_EXITCODE(0,0) happens to be 0. */
2566 return lp->status != 0 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE;
2567 }
2568
2569 /* Select the Nth LWP that has had an event. */
2570
2571 static int
2572 select_event_lwp_callback (struct lwp_info *lp, void *data)
2573 {
2574 int *selector = (int *) data;
2575
2576 gdb_assert (selector != NULL);
2577
2578 /* Select only resumed LWPs that have an event pending. */
2579 if (lp->resumed && lwp_status_pending_p (lp))
2580 if ((*selector)-- == 0)
2581 return 1;
2582
2583 return 0;
2584 }
2585
2586 /* Called when the LWP got a signal/trap that could be explained by a
2587 software or hardware breakpoint. */
2588
2589 static int
2590 check_stopped_by_breakpoint (struct lwp_info *lp)
2591 {
2592 /* Arrange for a breakpoint to be hit again later. We don't keep
2593 the SIGTRAP status and don't forward the SIGTRAP signal to the
2594 LWP. We will handle the current event, eventually we will resume
2595 this LWP, and this breakpoint will trap again.
2596
2597 If we do not do this, then we run the risk that the user will
2598 delete or disable the breakpoint, but the LWP will have already
2599 tripped on it. */
2600
2601 struct regcache *regcache = get_thread_regcache (lp->ptid);
2602 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2603 CORE_ADDR pc;
2604 CORE_ADDR sw_bp_pc;
2605 #if USE_SIGTRAP_SIGINFO
2606 siginfo_t siginfo;
2607 #endif
2608
2609 pc = regcache_read_pc (regcache);
2610 sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
2611
2612 #if USE_SIGTRAP_SIGINFO
2613 if (linux_nat_get_siginfo (lp->ptid, &siginfo))
2614 {
2615 if (siginfo.si_signo == SIGTRAP)
2616 {
2617 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
2618 {
2619 if (debug_linux_nat)
2620 fprintf_unfiltered (gdb_stdlog,
2621 "CSBB: %s stopped by software "
2622 "breakpoint\n",
2623 target_pid_to_str (lp->ptid));
2624
2625 /* Back up the PC if necessary. */
2626 if (pc != sw_bp_pc)
2627 regcache_write_pc (regcache, sw_bp_pc);
2628
2629 lp->stop_pc = sw_bp_pc;
2630 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2631 return 1;
2632 }
2633 else if (siginfo.si_code == TRAP_HWBKPT)
2634 {
2635 if (debug_linux_nat)
2636 fprintf_unfiltered (gdb_stdlog,
2637 "CSBB: %s stopped by hardware "
2638 "breakpoint/watchpoint\n",
2639 target_pid_to_str (lp->ptid));
2640
2641 lp->stop_pc = pc;
2642 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2643 return 1;
2644 }
2645 else if (siginfo.si_code == TRAP_TRACE)
2646 {
2647 if (debug_linux_nat)
2648 fprintf_unfiltered (gdb_stdlog,
2649 "CSBB: %s stopped by trace\n",
2650 target_pid_to_str (lp->ptid));
2651 }
2652 }
2653 }
2654 #else
2655 if ((!lp->step || lp->stop_pc == sw_bp_pc)
2656 && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache),
2657 sw_bp_pc))
2658 {
2659 /* The LWP was either continued, or stepped a software
2660 breakpoint instruction. */
2661 if (debug_linux_nat)
2662 fprintf_unfiltered (gdb_stdlog,
2663 "CSBB: %s stopped by software breakpoint\n",
2664 target_pid_to_str (lp->ptid));
2665
2666 /* Back up the PC if necessary. */
2667 if (pc != sw_bp_pc)
2668 regcache_write_pc (regcache, sw_bp_pc);
2669
2670 lp->stop_pc = sw_bp_pc;
2671 lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
2672 return 1;
2673 }
2674
2675 if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2676 {
2677 if (debug_linux_nat)
2678 fprintf_unfiltered (gdb_stdlog,
2679 "CSBB: stopped by hardware breakpoint %s\n",
2680 target_pid_to_str (lp->ptid));
2681
2682 lp->stop_pc = pc;
2683 lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
2684 return 1;
2685 }
2686 #endif
2687
2688 return 0;
2689 }
2690
2691
2692 /* Returns true if the LWP had stopped for a software breakpoint. */
2693
2694 static int
2695 linux_nat_stopped_by_sw_breakpoint (struct target_ops *ops)
2696 {
2697 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2698
2699 gdb_assert (lp != NULL);
2700
2701 return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
2702 }
2703
2704 /* Implement the supports_stopped_by_sw_breakpoint method. */
2705
2706 static int
2707 linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
2708 {
2709 return USE_SIGTRAP_SIGINFO;
2710 }
2711
2712 /* Returns true if the LWP had stopped for a hardware
2713 breakpoint/watchpoint. */
2714
2715 static int
2716 linux_nat_stopped_by_hw_breakpoint (struct target_ops *ops)
2717 {
2718 struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2719
2720 gdb_assert (lp != NULL);
2721
2722 return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
2723 }
2724
2725 /* Implement the supports_stopped_by_hw_breakpoint method. */
2726
2727 static int
2728 linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
2729 {
2730 return USE_SIGTRAP_SIGINFO;
2731 }
2732
2733 /* Select one LWP out of those that have events pending. */
2734
2735 static void
2736 select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
2737 {
2738 int num_events = 0;
2739 int random_selector;
2740 struct lwp_info *event_lp = NULL;
2741
2742 /* Record the wait status for the original LWP. */
2743 (*orig_lp)->status = *status;
2744
2745 /* In all-stop, give preference to the LWP that is being
2746 single-stepped. There will be at most one, and it will be the
2747 LWP that the core is most interested in. If we didn't do this,
2748 then we'd have to handle pending step SIGTRAPs somehow in case
2749 the core later continues the previously-stepped thread, as
2750 otherwise we'd report the pending SIGTRAP then, and the core, not
2751 having stepped the thread, wouldn't understand what the trap was
2752 for, and therefore would report it to the user as a random
2753 signal. */
2754 if (!target_is_non_stop_p ())
2755 {
2756 event_lp = iterate_over_lwps (filter,
2757 select_singlestep_lwp_callback, NULL);
2758 if (event_lp != NULL)
2759 {
2760 if (debug_linux_nat)
2761 fprintf_unfiltered (gdb_stdlog,
2762 "SEL: Select single-step %s\n",
2763 target_pid_to_str (event_lp->ptid));
2764 }
2765 }
2766
2767 if (event_lp == NULL)
2768 {
2769 /* Pick one at random, out of those which have had events. */
2770
2771 /* First see how many events we have. */
2772 iterate_over_lwps (filter, count_events_callback, &num_events);
2773 gdb_assert (num_events > 0);
2774
2775 /* Now randomly pick a LWP out of those that have had
2776 events. */
2777 random_selector = (int)
2778 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2779
2780 if (debug_linux_nat && num_events > 1)
2781 fprintf_unfiltered (gdb_stdlog,
2782 "SEL: Found %d events, selecting #%d\n",
2783 num_events, random_selector);
2784
2785 event_lp = iterate_over_lwps (filter,
2786 select_event_lwp_callback,
2787 &random_selector);
2788 }
2789
2790 if (event_lp != NULL)
2791 {
2792 /* Switch the event LWP. */
2793 *orig_lp = event_lp;
2794 *status = event_lp->status;
2795 }
2796
2797 /* Flush the wait status for the event LWP. */
2798 (*orig_lp)->status = 0;
2799 }
2800
2801 /* Return non-zero if LP has been resumed. */
2802
2803 static int
2804 resumed_callback (struct lwp_info *lp, void *data)
2805 {
2806 return lp->resumed;
2807 }
2808
2809 /* Check if we should go on and pass this event to common code.
2810 Return the affected lwp if we are, or NULL otherwise. */
2811
2812 static struct lwp_info *
2813 linux_nat_filter_event (int lwpid, int status)
2814 {
2815 struct lwp_info *lp;
2816 int event = linux_ptrace_get_extended_event (status);
2817
2818 lp = find_lwp_pid (pid_to_ptid (lwpid));
2819
2820 /* Check for stop events reported by a process we didn't already
2821 know about - anything not already in our LWP list.
2822
2823 If we're expecting to receive stopped processes after
2824 fork, vfork, and clone events, then we'll just add the
2825 new one to our list and go back to waiting for the event
2826 to be reported - the stopped process might be returned
2827 from waitpid before or after the event is.
2828
2829 But note the case of a non-leader thread exec'ing after the
2830 leader having exited, and gone from our lists. The non-leader
2831 thread changes its tid to the tgid. */
2832
2833 if (WIFSTOPPED (status) && lp == NULL
2834 && (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC))
2835 {
2836 /* A multi-thread exec after we had seen the leader exiting. */
2837 if (debug_linux_nat)
2838 fprintf_unfiltered (gdb_stdlog,
2839 "LLW: Re-adding thread group leader LWP %d.\n",
2840 lwpid);
2841
2842 lp = add_lwp (ptid_build (lwpid, lwpid, 0));
2843 lp->stopped = 1;
2844 lp->resumed = 1;
2845 add_thread (lp->ptid);
2846 }
2847
2848 if (WIFSTOPPED (status) && !lp)
2849 {
2850 if (debug_linux_nat)
2851 fprintf_unfiltered (gdb_stdlog,
2852 "LHEW: saving LWP %ld status %s in stopped_pids list\n",
2853 (long) lwpid, status_to_str (status));
2854 add_to_pid_list (&stopped_pids, lwpid, status);
2855 return NULL;
2856 }
2857
2858 /* Make sure we don't report an event for the exit of an LWP not in
2859 our list, i.e. not part of the current process. This can happen
2860 if we detach from a program we originally forked and then it
2861 exits. */
2862 if (!WIFSTOPPED (status) && !lp)
2863 return NULL;
2864
2865 /* This LWP is stopped now. (And if dead, this prevents it from
2866 ever being continued.) */
2867 lp->stopped = 1;
2868
2869 if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
2870 {
2871 struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2872 int options = linux_nat_ptrace_options (inf->attach_flag);
2873
2874 linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
2875 lp->must_set_ptrace_flags = 0;
2876 }
2877
2878 /* Handle GNU/Linux's syscall SIGTRAPs. */
2879 if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2880 {
2881 /* No longer need the sysgood bit. The ptrace event ends up
2882 recorded in lp->waitstatus if we care for it. We can carry
2883 on handling the event like a regular SIGTRAP from here
2884 on. */
2885 status = W_STOPCODE (SIGTRAP);
2886 if (linux_handle_syscall_trap (lp, 0))
2887 return NULL;
2888 }
2889 else
2890 {
2891 /* Almost all other ptrace-stops are known to be outside of system
2892 calls, with further exceptions in linux_handle_extended_wait. */
2893 lp->syscall_state = TARGET_WAITKIND_IGNORE;
2894 }
2895
2896 /* Handle GNU/Linux's extended waitstatus for trace events. */
2897 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
2898 && linux_is_extended_waitstatus (status))
2899 {
2900 if (debug_linux_nat)
2901 fprintf_unfiltered (gdb_stdlog,
2902 "LLW: Handling extended status 0x%06x\n",
2903 status);
2904 if (linux_handle_extended_wait (lp, status))
2905 return NULL;
2906 }
2907
2908 /* Check if the thread has exited. */
2909 if (WIFEXITED (status) || WIFSIGNALED (status))
2910 {
2911 if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
2912 {
2913 if (debug_linux_nat)
2914 fprintf_unfiltered (gdb_stdlog,
2915 "LLW: %s exited.\n",
2916 target_pid_to_str (lp->ptid));
2917
2918 /* If there is at least one more LWP, then the exit signal
2919 was not the end of the debugged application and should be
2920 ignored. */
2921 exit_lwp (lp);
2922 return NULL;
2923 }
2924
2925 /* Note that even if the leader was ptrace-stopped, it can still
2926 exit, if e.g., some other thread brings down the whole
2927 process (calls `exit'). So don't assert that the lwp is
2928 resumed. */
2929 if (debug_linux_nat)
2930 fprintf_unfiltered (gdb_stdlog,
2931 "Process %ld exited (resumed=%d)\n",
2932 ptid_get_lwp (lp->ptid), lp->resumed);
2933
2934 /* This was the last lwp in the process. Since events are
2935 serialized to GDB core, we may not be able report this one
2936 right now, but GDB core and the other target layers will want
2937 to be notified about the exit code/signal, leave the status
2938 pending for the next time we're able to report it. */
2939
2940 /* Dead LWP's aren't expected to reported a pending sigstop. */
2941 lp->signalled = 0;
2942
2943 /* Store the pending event in the waitstatus, because
2944 W_EXITCODE(0,0) == 0. */
2945 store_waitstatus (&lp->waitstatus, status);
2946 return lp;
2947 }
2948
2949 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2950 an attempt to stop an LWP. */
2951 if (lp->signalled
2952 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2953 {
2954 lp->signalled = 0;
2955
2956 if (lp->last_resume_kind == resume_stop)
2957 {
2958 if (debug_linux_nat)
2959 fprintf_unfiltered (gdb_stdlog,
2960 "LLW: resume_stop SIGSTOP caught for %s.\n",
2961 target_pid_to_str (lp->ptid));
2962 }
2963 else
2964 {
2965 /* This is a delayed SIGSTOP. Filter out the event. */
2966
2967 if (debug_linux_nat)
2968 fprintf_unfiltered (gdb_stdlog,
2969 "LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2970 lp->step ?
2971 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2972 target_pid_to_str (lp->ptid));
2973
2974 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
2975 gdb_assert (lp->resumed);
2976 return NULL;
2977 }
2978 }
2979
2980 /* Make sure we don't report a SIGINT that we have already displayed
2981 for another thread. */
2982 if (lp->ignore_sigint
2983 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
2984 {
2985 if (debug_linux_nat)
2986 fprintf_unfiltered (gdb_stdlog,
2987 "LLW: Delayed SIGINT caught for %s.\n",
2988 target_pid_to_str (lp->ptid));
2989
2990 /* This is a delayed SIGINT. */
2991 lp->ignore_sigint = 0;
2992
2993 linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
2994 if (debug_linux_nat)
2995 fprintf_unfiltered (gdb_stdlog,
2996 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2997 lp->step ?
2998 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2999 target_pid_to_str (lp->ptid));
3000 gdb_assert (lp->resumed);
3001
3002 /* Discard the event. */
3003 return NULL;
3004 }
3005
3006 /* Don't report signals that GDB isn't interested in, such as
3007 signals that are neither printed nor stopped upon. Stopping all
3008 threads can be a bit time-consuming so if we want decent
3009 performance with heavily multi-threaded programs, especially when
3010 they're using a high frequency timer, we'd better avoid it if we
3011 can. */
3012 if (WIFSTOPPED (status))
3013 {
3014 enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
3015
3016 if (!target_is_non_stop_p ())
3017 {
3018 /* Only do the below in all-stop, as we currently use SIGSTOP
3019 to implement target_stop (see linux_nat_stop) in
3020 non-stop. */
3021 if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
3022 {
3023 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3024 forwarded to the entire process group, that is, all LWPs
3025 will receive it - unless they're using CLONE_THREAD to
3026 share signals. Since we only want to report it once, we
3027 mark it as ignored for all LWPs except this one. */
3028 iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3029 set_ignore_sigint, NULL);
3030 lp->ignore_sigint = 0;
3031 }
3032 else
3033 maybe_clear_ignore_sigint (lp);
3034 }
3035
3036 /* When using hardware single-step, we need to report every signal.
3037 Otherwise, signals in pass_mask may be short-circuited
3038 except signals that might be caused by a breakpoint. */
3039 if (!lp->step
3040 && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
3041 && !linux_wstatus_maybe_breakpoint (status))
3042 {
3043 linux_resume_one_lwp (lp, lp->step, signo);
3044 if (debug_linux_nat)
3045 fprintf_unfiltered (gdb_stdlog,
3046 "LLW: %s %s, %s (preempt 'handle')\n",
3047 lp->step ?
3048 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3049 target_pid_to_str (lp->ptid),
3050 (signo != GDB_SIGNAL_0
3051 ? strsignal (gdb_signal_to_host (signo))
3052 : "0"));
3053 return NULL;
3054 }
3055 }
3056
3057 /* An interesting event. */
3058 gdb_assert (lp);
3059 lp->status = status;
3060 save_sigtrap (lp);
3061 return lp;
3062 }
3063
3064 /* Detect zombie thread group leaders, and "exit" them. We can't reap
3065 their exits until all other threads in the group have exited. */
3066
3067 static void
3068 check_zombie_leaders (void)
3069 {
3070 struct inferior *inf;
3071
3072 ALL_INFERIORS (inf)
3073 {
3074 struct lwp_info *leader_lp;
3075
3076 if (inf->pid == 0)
3077 continue;
3078
3079 leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3080 if (leader_lp != NULL
3081 /* Check if there are other threads in the group, as we may
3082 have raced with the inferior simply exiting. */
3083 && num_lwps (inf->pid) > 1
3084 && linux_proc_pid_is_zombie (inf->pid))
3085 {
3086 if (debug_linux_nat)
3087 fprintf_unfiltered (gdb_stdlog,
3088 "CZL: Thread group leader %d zombie "
3089 "(it exited, or another thread execd).\n",
3090 inf->pid);
3091
3092 /* A leader zombie can mean one of two things:
3093
3094 - It exited, and there's an exit status pending
3095 available, or only the leader exited (not the whole
3096 program). In the latter case, we can't waitpid the
3097 leader's exit status until all other threads are gone.
3098
3099 - There are 3 or more threads in the group, and a thread
3100 other than the leader exec'd. See comments on exec
3101 events at the top of the file. We could try
3102 distinguishing the exit and exec cases, by waiting once
3103 more, and seeing if something comes out, but it doesn't
3104 sound useful. The previous leader _does_ go away, and
3105 we'll re-add the new one once we see the exec event
3106 (which is just the same as what would happen if the
3107 previous leader did exit voluntarily before some other
3108 thread execs). */
3109
3110 if (debug_linux_nat)
3111 fprintf_unfiltered (gdb_stdlog,
3112 "CZL: Thread group leader %d vanished.\n",
3113 inf->pid);
3114 exit_lwp (leader_lp);
3115 }
3116 }
3117 }
3118
3119 static ptid_t
3120 linux_nat_wait_1 (struct target_ops *ops,
3121 ptid_t ptid, struct target_waitstatus *ourstatus,
3122 int target_options)
3123 {
3124 sigset_t prev_mask;
3125 enum resume_kind last_resume_kind;
3126 struct lwp_info *lp;
3127 int status;
3128
3129 if (debug_linux_nat)
3130 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3131
3132 /* The first time we get here after starting a new inferior, we may
3133 not have added it to the LWP list yet - this is the earliest
3134 moment at which we know its PID. */
3135 if (ptid_is_pid (inferior_ptid))
3136 {
3137 /* Upgrade the main thread's ptid. */
3138 thread_change_ptid (inferior_ptid,
3139 ptid_build (ptid_get_pid (inferior_ptid),
3140 ptid_get_pid (inferior_ptid), 0));
3141
3142 lp = add_initial_lwp (inferior_ptid);
3143 lp->resumed = 1;
3144 }
3145
3146 /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3147 block_child_signals (&prev_mask);
3148
3149 /* First check if there is a LWP with a wait status pending. */
3150 lp = iterate_over_lwps (ptid, status_callback, NULL);
3151 if (lp != NULL)
3152 {
3153 if (debug_linux_nat)
3154 fprintf_unfiltered (gdb_stdlog,
3155 "LLW: Using pending wait status %s for %s.\n",
3156 status_to_str (lp->status),
3157 target_pid_to_str (lp->ptid));
3158 }
3159
3160 /* But if we don't find a pending event, we'll have to wait. Always
3161 pull all events out of the kernel. We'll randomly select an
3162 event LWP out of all that have events, to prevent starvation. */
3163
3164 while (lp == NULL)
3165 {
3166 pid_t lwpid;
3167
3168 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3169 quirks:
3170
3171 - If the thread group leader exits while other threads in the
3172 thread group still exist, waitpid(TGID, ...) hangs. That
3173 waitpid won't return an exit status until the other threads
3174 in the group are reapped.
3175
3176 - When a non-leader thread execs, that thread just vanishes
3177 without reporting an exit (so we'd hang if we waited for it
3178 explicitly in that case). The exec event is reported to
3179 the TGID pid. */
3180
3181 errno = 0;
3182 lwpid = my_waitpid (-1, &status, __WALL | WNOHANG);
3183
3184 if (debug_linux_nat)
3185 fprintf_unfiltered (gdb_stdlog,
3186 "LNW: waitpid(-1, ...) returned %d, %s\n",
3187 lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3188
3189 if (lwpid > 0)
3190 {
3191 if (debug_linux_nat)
3192 {
3193 fprintf_unfiltered (gdb_stdlog,
3194 "LLW: waitpid %ld received %s\n",
3195 (long) lwpid, status_to_str (status));
3196 }
3197
3198 linux_nat_filter_event (lwpid, status);
3199 /* Retry until nothing comes out of waitpid. A single
3200 SIGCHLD can indicate more than one child stopped. */
3201 continue;
3202 }
3203
3204 /* Now that we've pulled all events out of the kernel, resume
3205 LWPs that don't have an interesting event to report. */
3206 iterate_over_lwps (minus_one_ptid,
3207 resume_stopped_resumed_lwps, &minus_one_ptid);
3208
3209 /* ... and find an LWP with a status to report to the core, if
3210 any. */
3211 lp = iterate_over_lwps (ptid, status_callback, NULL);
3212 if (lp != NULL)
3213 break;
3214
3215 /* Check for zombie thread group leaders. Those can't be reaped
3216 until all other threads in the thread group are. */
3217 check_zombie_leaders ();
3218
3219 /* If there are no resumed children left, bail. We'd be stuck
3220 forever in the sigsuspend call below otherwise. */
3221 if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3222 {
3223 if (debug_linux_nat)
3224 fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3225
3226 ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3227
3228 restore_child_signals_mask (&prev_mask);
3229 return minus_one_ptid;
3230 }
3231
3232 /* No interesting event to report to the core. */
3233
3234 if (target_options & TARGET_WNOHANG)
3235 {
3236 if (debug_linux_nat)
3237 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3238
3239 ourstatus->kind = TARGET_WAITKIND_IGNORE;
3240 restore_child_signals_mask (&prev_mask);
3241 return minus_one_ptid;
3242 }
3243
3244 /* We shouldn't end up here unless we want to try again. */
3245 gdb_assert (lp == NULL);
3246
3247 /* Block until we get an event reported with SIGCHLD. */
3248 if (debug_linux_nat)
3249 fprintf_unfiltered (gdb_stdlog, "LNW: about to sigsuspend\n");
3250 sigsuspend (&suspend_mask);
3251 }
3252
3253 gdb_assert (lp);
3254
3255 status = lp->status;
3256 lp->status = 0;
3257
3258 if (!target_is_non_stop_p ())
3259 {
3260 /* Now stop all other LWP's ... */
3261 iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3262
3263 /* ... and wait until all of them have reported back that
3264 they're no longer running. */
3265 iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3266 }
3267
3268 /* If we're not waiting for a specific LWP, choose an event LWP from
3269 among those that have had events. Giving equal priority to all
3270 LWPs that have had events helps prevent starvation. */
3271 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3272 select_event_lwp (ptid, &lp, &status);
3273
3274 gdb_assert (lp != NULL);
3275
3276 /* Now that we've selected our final event LWP, un-adjust its PC if
3277 it was a software breakpoint, and we can't reliably support the
3278 "stopped by software breakpoint" stop reason. */
3279 if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3280 && !USE_SIGTRAP_SIGINFO)
3281 {
3282 struct regcache *regcache = get_thread_regcache (lp->ptid);
3283 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3284 int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
3285
3286 if (decr_pc != 0)
3287 {
3288 CORE_ADDR pc;
3289
3290 pc = regcache_read_pc (regcache);
3291 regcache_write_pc (regcache, pc + decr_pc);
3292 }
3293 }
3294
3295 /* We'll need this to determine whether to report a SIGSTOP as
3296 GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
3297 clears it. */
3298 last_resume_kind = lp->last_resume_kind;
3299
3300 if (!target_is_non_stop_p ())
3301 {
3302 /* In all-stop, from the core's perspective, all LWPs are now
3303 stopped until a new resume action is sent over. */
3304 iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3305 }
3306 else
3307 {
3308 resume_clear_callback (lp, NULL);
3309 }
3310
3311 if (linux_nat_status_is_event (status))
3312 {
3313 if (debug_linux_nat)
3314 fprintf_unfiltered (gdb_stdlog,
3315 "LLW: trap ptid is %s.\n",
3316 target_pid_to_str (lp->ptid));
3317 }
3318
3319 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3320 {
3321 *ourstatus = lp->waitstatus;
3322 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3323 }
3324 else
3325 store_waitstatus (ourstatus, status);
3326
3327 if (debug_linux_nat)
3328 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3329
3330 restore_child_signals_mask (&prev_mask);
3331
3332 if (last_resume_kind == resume_stop
3333 && ourstatus->kind == TARGET_WAITKIND_STOPPED
3334 && WSTOPSIG (status) == SIGSTOP)
3335 {
3336 /* A thread that has been requested to stop by GDB with
3337 target_stop, and it stopped cleanly, so report as SIG0. The
3338 use of SIGSTOP is an implementation detail. */
3339 ourstatus->value.sig = GDB_SIGNAL_0;
3340 }
3341
3342 if (ourstatus->kind == TARGET_WAITKIND_EXITED
3343 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
3344 lp->core = -1;
3345 else
3346 lp->core = linux_common_core_of_thread (lp->ptid);
3347
3348 return lp->ptid;
3349 }
3350
3351 /* Resume LWPs that are currently stopped without any pending status
3352 to report, but are resumed from the core's perspective. */
3353
3354 static int
3355 resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
3356 {
3357 ptid_t *wait_ptid_p = (ptid_t *) data;
3358
3359 if (!lp->stopped)
3360 {
3361 if (debug_linux_nat)
3362 fprintf_unfiltered (gdb_stdlog,
3363 "RSRL: NOT resuming LWP %s, not stopped\n",
3364 target_pid_to_str (lp->ptid));
3365 }
3366 else if (!lp->resumed)
3367 {
3368 if (debug_linux_nat)
3369 fprintf_unfiltered (gdb_stdlog,
3370 "RSRL: NOT resuming LWP %s, not resumed\n",
3371 target_pid_to_str (lp->ptid));
3372 }
3373 else if (lwp_status_pending_p (lp))
3374 {
3375 if (debug_linux_nat)
3376 fprintf_unfiltered (gdb_stdlog,
3377 "RSRL: NOT resuming LWP %s, has pending status\n",
3378 target_pid_to_str (lp->ptid));
3379 }
3380 else
3381 {
3382 struct regcache *regcache = get_thread_regcache (lp->ptid);
3383 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3384
3385 TRY
3386 {
3387 CORE_ADDR pc = regcache_read_pc (regcache);
3388 int leave_stopped = 0;
3389
3390 /* Don't bother if there's a breakpoint at PC that we'd hit
3391 immediately, and we're not waiting for this LWP. */
3392 if (!ptid_match (lp->ptid, *wait_ptid_p))
3393 {
3394 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3395 leave_stopped = 1;
3396 }
3397
3398 if (!leave_stopped)
3399 {
3400 if (debug_linux_nat)
3401 fprintf_unfiltered (gdb_stdlog,
3402 "RSRL: resuming stopped-resumed LWP %s at "
3403 "%s: step=%d\n",
3404 target_pid_to_str (lp->ptid),
3405 paddress (gdbarch, pc),
3406 lp->step);
3407
3408 linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
3409 }
3410 }
3411 CATCH (ex, RETURN_MASK_ERROR)
3412 {
3413 if (!check_ptrace_stopped_lwp_gone (lp))
3414 throw_exception (ex);
3415 }
3416 END_CATCH
3417 }
3418
3419 return 0;
3420 }
3421
3422 static ptid_t
3423 linux_nat_wait (struct target_ops *ops,
3424 ptid_t ptid, struct target_waitstatus *ourstatus,
3425 int target_options)
3426 {
3427 ptid_t event_ptid;
3428
3429 if (debug_linux_nat)
3430 {
3431 char *options_string;
3432
3433 options_string = target_options_to_string (target_options);
3434 fprintf_unfiltered (gdb_stdlog,
3435 "linux_nat_wait: [%s], [%s]\n",
3436 target_pid_to_str (ptid),
3437 options_string);
3438 xfree (options_string);
3439 }
3440
3441 /* Flush the async file first. */
3442 if (target_is_async_p ())
3443 async_file_flush ();
3444
3445 /* Resume LWPs that are currently stopped without any pending status
3446 to report, but are resumed from the core's perspective. LWPs get
3447 in this state if we find them stopping at a time we're not
3448 interested in reporting the event (target_wait on a
3449 specific_process, for example, see linux_nat_wait_1), and
3450 meanwhile the event became uninteresting. Don't bother resuming
3451 LWPs we're not going to wait for if they'd stop immediately. */
3452 if (target_is_non_stop_p ())
3453 iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
3454
3455 event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3456
3457 /* If we requested any event, and something came out, assume there
3458 may be more. If we requested a specific lwp or process, also
3459 assume there may be more. */
3460 if (target_is_async_p ()
3461 && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
3462 && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
3463 || !ptid_equal (ptid, minus_one_ptid)))
3464 async_file_mark ();
3465
3466 return event_ptid;
3467 }
3468
3469 static int
3470 kill_callback (struct lwp_info *lp, void *data)
3471 {
3472 /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3473
3474 errno = 0;
3475 kill_lwp (ptid_get_lwp (lp->ptid), SIGKILL);
3476 if (debug_linux_nat)
3477 {
3478 int save_errno = errno;
3479
3480 fprintf_unfiltered (gdb_stdlog,
3481 "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3482 target_pid_to_str (lp->ptid),
3483 save_errno ? safe_strerror (save_errno) : "OK");
3484 }
3485
3486 /* Some kernels ignore even SIGKILL for processes under ptrace. */
3487
3488 errno = 0;
3489 ptrace (PTRACE_KILL, ptid_get_lwp (lp->ptid), 0, 0);
3490 if (debug_linux_nat)
3491 {
3492 int save_errno = errno;
3493
3494 fprintf_unfiltered (gdb_stdlog,
3495 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3496 target_pid_to_str (lp->ptid),
3497 save_errno ? safe_strerror (save_errno) : "OK");
3498 }
3499
3500 return 0;
3501 }
3502
3503 static int
3504 kill_wait_callback (struct lwp_info *lp, void *data)
3505 {
3506 pid_t pid;
3507
3508 /* We must make sure that there are no pending events (delayed
3509 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3510 program doesn't interfere with any following debugging session. */
3511
3512 do
3513 {
3514 pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, __WALL);
3515 if (pid != (pid_t) -1)
3516 {
3517 if (debug_linux_nat)
3518 fprintf_unfiltered (gdb_stdlog,
3519 "KWC: wait %s received unknown.\n",
3520 target_pid_to_str (lp->ptid));
3521 /* The Linux kernel sometimes fails to kill a thread
3522 completely after PTRACE_KILL; that goes from the stop
3523 point in do_fork out to the one in get_signal_to_deliver
3524 and waits again. So kill it again. */
3525 kill_callback (lp, NULL);
3526 }
3527 }
3528 while (pid == ptid_get_lwp (lp->ptid));
3529
3530 gdb_assert (pid == -1 && errno == ECHILD);
3531 return 0;
3532 }
3533
3534 static void
3535 linux_nat_kill (struct target_ops *ops)
3536 {
3537 struct target_waitstatus last;
3538 ptid_t last_ptid;
3539 int status;
3540
3541 /* If we're stopped while forking and we haven't followed yet,
3542 kill the other task. We need to do this first because the
3543 parent will be sleeping if this is a vfork. */
3544
3545 get_last_target_status (&last_ptid, &last);
3546
3547 if (last.kind == TARGET_WAITKIND_FORKED
3548 || last.kind == TARGET_WAITKIND_VFORKED)
3549 {
3550 ptrace (PT_KILL, ptid_get_pid (last.value.related_pid), 0, 0);
3551 wait (&status);
3552
3553 /* Let the arch-specific native code know this process is
3554 gone. */
3555 linux_nat_forget_process (ptid_get_pid (last.value.related_pid));
3556 }
3557
3558 if (forks_exist_p ())
3559 linux_fork_killall ();
3560 else
3561 {
3562 ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
3563
3564 /* Stop all threads before killing them, since ptrace requires
3565 that the thread is stopped to sucessfully PTRACE_KILL. */
3566 iterate_over_lwps (ptid, stop_callback, NULL);
3567 /* ... and wait until all of them have reported back that
3568 they're no longer running. */
3569 iterate_over_lwps (ptid, stop_wait_callback, NULL);
3570
3571 /* Kill all LWP's ... */
3572 iterate_over_lwps (ptid, kill_callback, NULL);
3573
3574 /* ... and wait until we've flushed all events. */
3575 iterate_over_lwps (ptid, kill_wait_callback, NULL);
3576 }
3577
3578 target_mourn_inferior ();
3579 }
3580
3581 static void
3582 linux_nat_mourn_inferior (struct target_ops *ops)
3583 {
3584 int pid = ptid_get_pid (inferior_ptid);
3585
3586 purge_lwp_list (pid);
3587
3588 if (! forks_exist_p ())
3589 /* Normal case, no other forks available. */
3590 linux_ops->to_mourn_inferior (ops);
3591 else
3592 /* Multi-fork case. The current inferior_ptid has exited, but
3593 there are other viable forks to debug. Delete the exiting
3594 one and context-switch to the first available. */
3595 linux_fork_mourn_inferior ();
3596
3597 /* Let the arch-specific native code know this process is gone. */
3598 linux_nat_forget_process (pid);
3599 }
3600
3601 /* Convert a native/host siginfo object, into/from the siginfo in the
3602 layout of the inferiors' architecture. */
3603
3604 static void
3605 siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
3606 {
3607 int done = 0;
3608
3609 if (linux_nat_siginfo_fixup != NULL)
3610 done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
3611
3612 /* If there was no callback, or the callback didn't do anything,
3613 then just do a straight memcpy. */
3614 if (!done)
3615 {
3616 if (direction == 1)
3617 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
3618 else
3619 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
3620 }
3621 }
3622
3623 static enum target_xfer_status
3624 linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
3625 const char *annex, gdb_byte *readbuf,
3626 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
3627 ULONGEST *xfered_len)
3628 {
3629 int pid;
3630 siginfo_t siginfo;
3631 gdb_byte inf_siginfo[sizeof (siginfo_t)];
3632
3633 gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
3634 gdb_assert (readbuf || writebuf);
3635
3636 pid = ptid_get_lwp (inferior_ptid);
3637 if (pid == 0)
3638 pid = ptid_get_pid (inferior_ptid);
3639
3640 if (offset > sizeof (siginfo))
3641 return TARGET_XFER_E_IO;
3642
3643 errno = 0;
3644 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3645 if (errno != 0)
3646 return TARGET_XFER_E_IO;
3647
3648 /* When GDB is built as a 64-bit application, ptrace writes into
3649 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3650 inferior with a 64-bit GDB should look the same as debugging it
3651 with a 32-bit GDB, we need to convert it. GDB core always sees
3652 the converted layout, so any read/write will have to be done
3653 post-conversion. */
3654 siginfo_fixup (&siginfo, inf_siginfo, 0);
3655
3656 if (offset + len > sizeof (siginfo))
3657 len = sizeof (siginfo) - offset;
3658
3659 if (readbuf != NULL)
3660 memcpy (readbuf, inf_siginfo + offset, len);
3661 else
3662 {
3663 memcpy (inf_siginfo + offset, writebuf, len);
3664
3665 /* Convert back to ptrace layout before flushing it out. */
3666 siginfo_fixup (&siginfo, inf_siginfo, 1);
3667
3668 errno = 0;
3669 ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3670 if (errno != 0)
3671 return TARGET_XFER_E_IO;
3672 }
3673
3674 *xfered_len = len;
3675 return TARGET_XFER_OK;
3676 }
3677
3678 static enum target_xfer_status
3679 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3680 const char *annex, gdb_byte *readbuf,
3681 const gdb_byte *writebuf,
3682 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
3683 {
3684 struct cleanup *old_chain;
3685 enum target_xfer_status xfer;
3686
3687 if (object == TARGET_OBJECT_SIGNAL_INFO)
3688 return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
3689 offset, len, xfered_len);
3690
3691 /* The target is connected but no live inferior is selected. Pass
3692 this request down to a lower stratum (e.g., the executable
3693 file). */
3694 if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
3695 return TARGET_XFER_EOF;
3696
3697 old_chain = save_inferior_ptid ();
3698
3699 if (ptid_lwp_p (inferior_ptid))
3700 inferior_ptid = pid_to_ptid (ptid_get_lwp (inferior_ptid));
3701
3702 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
3703 offset, len, xfered_len);
3704
3705 do_cleanups (old_chain);
3706 return xfer;
3707 }
3708
3709 static int
3710 linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
3711 {
3712 /* As long as a PTID is in lwp list, consider it alive. */
3713 return find_lwp_pid (ptid) != NULL;
3714 }
3715
3716 /* Implement the to_update_thread_list target method for this
3717 target. */
3718
3719 static void
3720 linux_nat_update_thread_list (struct target_ops *ops)
3721 {
3722 struct lwp_info *lwp;
3723
3724 /* We add/delete threads from the list as clone/exit events are
3725 processed, so just try deleting exited threads still in the
3726 thread list. */
3727 delete_exited_threads ();
3728
3729 /* Update the processor core that each lwp/thread was last seen
3730 running on. */
3731 ALL_LWPS (lwp)
3732 lwp->core = linux_common_core_of_thread (lwp->ptid);
3733 }
3734
3735 static char *
3736 linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
3737 {
3738 static char buf[64];
3739
3740 if (ptid_lwp_p (ptid)
3741 && (ptid_get_pid (ptid) != ptid_get_lwp (ptid)
3742 || num_lwps (ptid_get_pid (ptid)) > 1))
3743 {
3744 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
3745 return buf;
3746 }
3747
3748 return normal_pid_to_str (ptid);
3749 }
3750
3751 static const char *
3752 linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
3753 {
3754 return linux_proc_tid_get_name (thr->ptid);
3755 }
3756
3757 /* Accepts an integer PID; Returns a string representing a file that
3758 can be opened to get the symbols for the child process. */
3759
3760 static char *
3761 linux_child_pid_to_exec_file (struct target_ops *self, int pid)
3762 {
3763 return linux_proc_pid_to_exec_file (pid);
3764 }
3765
3766 /* Implement the to_xfer_partial interface for memory reads using the /proc
3767 filesystem. Because we can use a single read() call for /proc, this
3768 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3769 but it doesn't support writes. */
3770
3771 static enum target_xfer_status
3772 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
3773 const char *annex, gdb_byte *readbuf,
3774 const gdb_byte *writebuf,
3775 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
3776 {
3777 LONGEST ret;
3778 int fd;
3779 char filename[64];
3780
3781 if (object != TARGET_OBJECT_MEMORY || !readbuf)
3782 return TARGET_XFER_EOF;
3783
3784 /* Don't bother for one word. */
3785 if (len < 3 * sizeof (long))
3786 return TARGET_XFER_EOF;
3787
3788 /* We could keep this file open and cache it - possibly one per
3789 thread. That requires some juggling, but is even faster. */
3790 xsnprintf (filename, sizeof filename, "/proc/%d/mem",
3791 ptid_get_pid (inferior_ptid));
3792 fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
3793 if (fd == -1)
3794 return TARGET_XFER_EOF;
3795
3796 /* If pread64 is available, use it. It's faster if the kernel
3797 supports it (only one syscall), and it's 64-bit safe even on
3798 32-bit platforms (for instance, SPARC debugging a SPARC64
3799 application). */
3800 #ifdef HAVE_PREAD64
3801 if (pread64 (fd, readbuf, len, offset) != len)
3802 #else
3803 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
3804 #endif
3805 ret = 0;
3806 else
3807 ret = len;
3808
3809 close (fd);
3810
3811 if (ret == 0)
3812 return TARGET_XFER_EOF;
3813 else
3814 {
3815 *xfered_len = ret;
3816 return TARGET_XFER_OK;
3817 }
3818 }
3819
3820
3821 /* Enumerate spufs IDs for process PID. */
3822 static LONGEST
3823 spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, ULONGEST len)
3824 {
3825 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
3826 LONGEST pos = 0;
3827 LONGEST written = 0;
3828 char path[128];
3829 DIR *dir;
3830 struct dirent *entry;
3831
3832 xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
3833 dir = opendir (path);
3834 if (!dir)
3835 return -1;
3836
3837 rewinddir (dir);
3838 while ((entry = readdir (dir)) != NULL)
3839 {
3840 struct stat st;
3841 struct statfs stfs;
3842 int fd;
3843
3844 fd = atoi (entry->d_name);
3845 if (!fd)
3846 continue;
3847
3848 xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
3849 if (stat (path, &st) != 0)
3850 continue;
3851 if (!S_ISDIR (st.st_mode))
3852 continue;
3853
3854 if (statfs (path, &stfs) != 0)
3855 continue;
3856 if (stfs.f_type != SPUFS_MAGIC)
3857 continue;
3858
3859 if (pos >= offset && pos + 4 <= offset + len)
3860 {
3861 store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
3862 written += 4;
3863 }
3864 pos += 4;
3865 }
3866
3867 closedir (dir);
3868 return written;
3869 }
3870
3871 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
3872 object type, using the /proc file system. */
3873
3874 static enum target_xfer_status
3875 linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
3876 const char *annex, gdb_byte *readbuf,
3877 const gdb_byte *writebuf,
3878 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
3879 {
3880 char buf[128];
3881 int fd = 0;
3882 int ret = -1;
3883 int pid = ptid_get_pid (inferior_ptid);
3884
3885 if (!annex)
3886 {
3887 if (!readbuf)
3888 return TARGET_XFER_E_IO;
3889 else
3890 {
3891 LONGEST l = spu_enumerate_spu_ids (pid, readbuf, offset, len);
3892
3893 if (l < 0)
3894 return TARGET_XFER_E_IO;
3895 else if (l == 0)
3896 return TARGET_XFER_EOF;
3897 else
3898 {
3899 *xfered_len = (ULONGEST) l;
3900 return TARGET_XFER_OK;
3901 }
3902 }
3903 }
3904
3905 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
3906 fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
3907 if (fd <= 0)
3908 return TARGET_XFER_E_IO;
3909
3910 if (offset != 0
3911 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
3912 {
3913 close (fd);
3914 return TARGET_XFER_EOF;
3915 }
3916
3917 if (writebuf)
3918 ret = write (fd, writebuf, (size_t) len);
3919 else if (readbuf)
3920 ret = read (fd, readbuf, (size_t) len);
3921
3922 close (fd);
3923
3924 if (ret < 0)
3925 return TARGET_XFER_E_IO;
3926 else if (ret == 0)
3927 return TARGET_XFER_EOF;
3928 else
3929 {
3930 *xfered_len = (ULONGEST) ret;
3931 return TARGET_XFER_OK;
3932 }
3933 }
3934
3935
3936 /* Parse LINE as a signal set and add its set bits to SIGS. */
3937
3938 static void
3939 add_line_to_sigset (const char *line, sigset_t *sigs)
3940 {
3941 int len = strlen (line) - 1;
3942 const char *p;
3943 int signum;
3944
3945 if (line[len] != '\n')
3946 error (_("Could not parse signal set: %s"), line);
3947
3948 p = line;
3949 signum = len * 4;
3950 while (len-- > 0)
3951 {
3952 int digit;
3953
3954 if (*p >= '0' && *p <= '9')
3955 digit = *p - '0';
3956 else if (*p >= 'a' && *p <= 'f')
3957 digit = *p - 'a' + 10;
3958 else
3959 error (_("Could not parse signal set: %s"), line);
3960
3961 signum -= 4;
3962
3963 if (digit & 1)
3964 sigaddset (sigs, signum + 1);
3965 if (digit & 2)
3966 sigaddset (sigs, signum + 2);
3967 if (digit & 4)
3968 sigaddset (sigs, signum + 3);
3969 if (digit & 8)
3970 sigaddset (sigs, signum + 4);
3971
3972 p++;
3973 }
3974 }
3975
3976 /* Find process PID's pending signals from /proc/pid/status and set
3977 SIGS to match. */
3978
3979 void
3980 linux_proc_pending_signals (int pid, sigset_t *pending,
3981 sigset_t *blocked, sigset_t *ignored)
3982 {
3983 FILE *procfile;
3984 char buffer[PATH_MAX], fname[PATH_MAX];
3985 struct cleanup *cleanup;
3986
3987 sigemptyset (pending);
3988 sigemptyset (blocked);
3989 sigemptyset (ignored);
3990 xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
3991 procfile = gdb_fopen_cloexec (fname, "r");
3992 if (procfile == NULL)
3993 error (_("Could not open %s"), fname);
3994 cleanup = make_cleanup_fclose (procfile);
3995
3996 while (fgets (buffer, PATH_MAX, procfile) != NULL)
3997 {
3998 /* Normal queued signals are on the SigPnd line in the status
3999 file. However, 2.6 kernels also have a "shared" pending
4000 queue for delivering signals to a thread group, so check for
4001 a ShdPnd line also.
4002
4003 Unfortunately some Red Hat kernels include the shared pending
4004 queue but not the ShdPnd status field. */
4005
4006 if (startswith (buffer, "SigPnd:\t"))
4007 add_line_to_sigset (buffer + 8, pending);
4008 else if (startswith (buffer, "ShdPnd:\t"))
4009 add_line_to_sigset (buffer + 8, pending);
4010 else if (startswith (buffer, "SigBlk:\t"))
4011 add_line_to_sigset (buffer + 8, blocked);
4012 else if (startswith (buffer, "SigIgn:\t"))
4013 add_line_to_sigset (buffer + 8, ignored);
4014 }
4015
4016 do_cleanups (cleanup);
4017 }
4018
4019 static enum target_xfer_status
4020 linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4021 const char *annex, gdb_byte *readbuf,
4022 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4023 ULONGEST *xfered_len)
4024 {
4025 gdb_assert (object == TARGET_OBJECT_OSDATA);
4026
4027 *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len);
4028 if (*xfered_len == 0)
4029 return TARGET_XFER_EOF;
4030 else
4031 return TARGET_XFER_OK;
4032 }
4033
4034 static enum target_xfer_status
4035 linux_xfer_partial (struct target_ops *ops, enum target_object object,
4036 const char *annex, gdb_byte *readbuf,
4037 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
4038 ULONGEST *xfered_len)
4039 {
4040 enum target_xfer_status xfer;
4041
4042 if (object == TARGET_OBJECT_AUXV)
4043 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4044 offset, len, xfered_len);
4045
4046 if (object == TARGET_OBJECT_OSDATA)
4047 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4048 offset, len, xfered_len);
4049
4050 if (object == TARGET_OBJECT_SPU)
4051 return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4052 offset, len, xfered_len);
4053
4054 /* GDB calculates all the addresses in possibly larget width of the address.
4055 Address width needs to be masked before its final use - either by
4056 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4057
4058 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4059
4060 if (object == TARGET_OBJECT_MEMORY)
4061 {
4062 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
4063
4064 if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4065 offset &= ((ULONGEST) 1 << addr_bit) - 1;
4066 }
4067
4068 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4069 offset, len, xfered_len);
4070 if (xfer != TARGET_XFER_EOF)
4071 return xfer;
4072
4073 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4074 offset, len, xfered_len);
4075 }
4076
4077 static void
4078 cleanup_target_stop (void *arg)
4079 {
4080 ptid_t *ptid = (ptid_t *) arg;
4081
4082 gdb_assert (arg != NULL);
4083
4084 /* Unpause all */
4085 target_resume (*ptid, 0, GDB_SIGNAL_0);
4086 }
4087
4088 static VEC(static_tracepoint_marker_p) *
4089 linux_child_static_tracepoint_markers_by_strid (struct target_ops *self,
4090 const char *strid)
4091 {
4092 char s[IPA_CMD_BUF_SIZE];
4093 struct cleanup *old_chain;
4094 int pid = ptid_get_pid (inferior_ptid);
4095 VEC(static_tracepoint_marker_p) *markers = NULL;
4096 struct static_tracepoint_marker *marker = NULL;
4097 char *p = s;
4098 ptid_t ptid = ptid_build (pid, 0, 0);
4099
4100 /* Pause all */
4101 target_stop (ptid);
4102
4103 memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4104 s[sizeof ("qTfSTM")] = 0;
4105
4106 agent_run_command (pid, s, strlen (s) + 1);
4107
4108 old_chain = make_cleanup (free_current_marker, &marker);
4109 make_cleanup (cleanup_target_stop, &ptid);
4110
4111 while (*p++ == 'm')
4112 {
4113 if (marker == NULL)
4114 marker = XCNEW (struct static_tracepoint_marker);
4115
4116 do
4117 {
4118 parse_static_tracepoint_marker_definition (p, &p, marker);
4119
4120 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4121 {
4122 VEC_safe_push (static_tracepoint_marker_p,
4123 markers, marker);
4124 marker = NULL;
4125 }
4126 else
4127 {
4128 release_static_tracepoint_marker (marker);
4129 memset (marker, 0, sizeof (*marker));
4130 }
4131 }
4132 while (*p++ == ','); /* comma-separated list */
4133
4134 memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4135 s[sizeof ("qTsSTM")] = 0;
4136 agent_run_command (pid, s, strlen (s) + 1);
4137 p = s;
4138 }
4139
4140 do_cleanups (old_chain);
4141
4142 return markers;
4143 }
4144
4145 /* Create a prototype generic GNU/Linux target. The client can override
4146 it with local methods. */
4147
4148 static void
4149 linux_target_install_ops (struct target_ops *t)
4150 {
4151 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4152 t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4153 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4154 t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4155 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4156 t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4157 t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4158 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4159 t->to_post_startup_inferior = linux_child_post_startup_inferior;
4160 t->to_post_attach = linux_child_post_attach;
4161 t->to_follow_fork = linux_child_follow_fork;
4162
4163 super_xfer_partial = t->to_xfer_partial;
4164 t->to_xfer_partial = linux_xfer_partial;
4165
4166 t->to_static_tracepoint_markers_by_strid
4167 = linux_child_static_tracepoint_markers_by_strid;
4168 }
4169
4170 struct target_ops *
4171 linux_target (void)
4172 {
4173 struct target_ops *t;
4174
4175 t = inf_ptrace_target ();
4176 linux_target_install_ops (t);
4177
4178 return t;
4179 }
4180
4181 struct target_ops *
4182 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4183 {
4184 struct target_ops *t;
4185
4186 t = inf_ptrace_trad_target (register_u_offset);
4187 linux_target_install_ops (t);
4188
4189 return t;
4190 }
4191
4192 /* target_is_async_p implementation. */
4193
4194 static int
4195 linux_nat_is_async_p (struct target_ops *ops)
4196 {
4197 return linux_is_async_p ();
4198 }
4199
4200 /* target_can_async_p implementation. */
4201
4202 static int
4203 linux_nat_can_async_p (struct target_ops *ops)
4204 {
4205 /* NOTE: palves 2008-03-21: We're only async when the user requests
4206 it explicitly with the "set target-async" command.
4207 Someday, linux will always be async. */
4208 return target_async_permitted;
4209 }
4210
4211 static int
4212 linux_nat_supports_non_stop (struct target_ops *self)
4213 {
4214 return 1;
4215 }
4216
4217 /* to_always_non_stop_p implementation. */
4218
4219 static int
4220 linux_nat_always_non_stop_p (struct target_ops *self)
4221 {
4222 return 1;
4223 }
4224
4225 /* True if we want to support multi-process. To be removed when GDB
4226 supports multi-exec. */
4227
4228 int linux_multi_process = 1;
4229
4230 static int
4231 linux_nat_supports_multi_process (struct target_ops *self)
4232 {
4233 return linux_multi_process;
4234 }
4235
4236 static int
4237 linux_nat_supports_disable_randomization (struct target_ops *self)
4238 {
4239 #ifdef HAVE_PERSONALITY
4240 return 1;
4241 #else
4242 return 0;
4243 #endif
4244 }
4245
4246 static int async_terminal_is_ours = 1;
4247
4248 /* target_terminal_inferior implementation.
4249
4250 This is a wrapper around child_terminal_inferior to add async support. */
4251
4252 static void
4253 linux_nat_terminal_inferior (struct target_ops *self)
4254 {
4255 child_terminal_inferior (self);
4256
4257 /* Calls to target_terminal_*() are meant to be idempotent. */
4258 if (!async_terminal_is_ours)
4259 return;
4260
4261 delete_file_handler (input_fd);
4262 async_terminal_is_ours = 0;
4263 set_sigint_trap ();
4264 }
4265
4266 /* target_terminal_ours implementation.
4267
4268 This is a wrapper around child_terminal_ours to add async support (and
4269 implement the target_terminal_ours vs target_terminal_ours_for_output
4270 distinction). child_terminal_ours is currently no different than
4271 child_terminal_ours_for_output.
4272 We leave target_terminal_ours_for_output alone, leaving it to
4273 child_terminal_ours_for_output. */
4274
4275 static void
4276 linux_nat_terminal_ours (struct target_ops *self)
4277 {
4278 /* GDB should never give the terminal to the inferior if the
4279 inferior is running in the background (run&, continue&, etc.),
4280 but claiming it sure should. */
4281 child_terminal_ours (self);
4282
4283 if (async_terminal_is_ours)
4284 return;
4285
4286 clear_sigint_trap ();
4287 add_file_handler (input_fd, stdin_event_handler, 0);
4288 async_terminal_is_ours = 1;
4289 }
4290
4291 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4292 so we notice when any child changes state, and notify the
4293 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4294 above to wait for the arrival of a SIGCHLD. */
4295
4296 static void
4297 sigchld_handler (int signo)
4298 {
4299 int old_errno = errno;
4300
4301 if (debug_linux_nat)
4302 ui_file_write_async_safe (gdb_stdlog,
4303 "sigchld\n", sizeof ("sigchld\n") - 1);
4304
4305 if (signo == SIGCHLD
4306 && linux_nat_event_pipe[0] != -1)
4307 async_file_mark (); /* Let the event loop know that there are
4308 events to handle. */
4309
4310 errno = old_errno;
4311 }
4312
4313 /* Callback registered with the target events file descriptor. */
4314
4315 static void
4316 handle_target_event (int error, gdb_client_data client_data)
4317 {
4318 inferior_event_handler (INF_REG_EVENT, NULL);
4319 }
4320
4321 /* Create/destroy the target events pipe. Returns previous state. */
4322
4323 static int
4324 linux_async_pipe (int enable)
4325 {
4326 int previous = linux_is_async_p ();
4327
4328 if (previous != enable)
4329 {
4330 sigset_t prev_mask;
4331
4332 /* Block child signals while we create/destroy the pipe, as
4333 their handler writes to it. */
4334 block_child_signals (&prev_mask);
4335
4336 if (enable)
4337 {
4338 if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
4339 internal_error (__FILE__, __LINE__,
4340 "creating event pipe failed.");
4341
4342 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4343 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4344 }
4345 else
4346 {
4347 close (linux_nat_event_pipe[0]);
4348 close (linux_nat_event_pipe[1]);
4349 linux_nat_event_pipe[0] = -1;
4350 linux_nat_event_pipe[1] = -1;
4351 }
4352
4353 restore_child_signals_mask (&prev_mask);
4354 }
4355
4356 return previous;
4357 }
4358
4359 /* target_async implementation. */
4360
4361 static void
4362 linux_nat_async (struct target_ops *ops, int enable)
4363 {
4364 if (enable)
4365 {
4366 if (!linux_async_pipe (1))
4367 {
4368 add_file_handler (linux_nat_event_pipe[0],
4369 handle_target_event, NULL);
4370 /* There may be pending events to handle. Tell the event loop
4371 to poll them. */
4372 async_file_mark ();
4373 }
4374 }
4375 else
4376 {
4377 delete_file_handler (linux_nat_event_pipe[0]);
4378 linux_async_pipe (0);
4379 }
4380 return;
4381 }
4382
4383 /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4384 event came out. */
4385
4386 static int
4387 linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4388 {
4389 if (!lwp->stopped)
4390 {
4391 if (debug_linux_nat)
4392 fprintf_unfiltered (gdb_stdlog,
4393 "LNSL: running -> suspending %s\n",
4394 target_pid_to_str (lwp->ptid));
4395
4396
4397 if (lwp->last_resume_kind == resume_stop)
4398 {
4399 if (debug_linux_nat)
4400 fprintf_unfiltered (gdb_stdlog,
4401 "linux-nat: already stopping LWP %ld at "
4402 "GDB's request\n",
4403 ptid_get_lwp (lwp->ptid));
4404 return 0;
4405 }
4406
4407 stop_callback (lwp, NULL);
4408 lwp->last_resume_kind = resume_stop;
4409 }
4410 else
4411 {
4412 /* Already known to be stopped; do nothing. */
4413
4414 if (debug_linux_nat)
4415 {
4416 if (find_thread_ptid (lwp->ptid)->stop_requested)
4417 fprintf_unfiltered (gdb_stdlog,
4418 "LNSL: already stopped/stop_requested %s\n",
4419 target_pid_to_str (lwp->ptid));
4420 else
4421 fprintf_unfiltered (gdb_stdlog,
4422 "LNSL: already stopped/no "
4423 "stop_requested yet %s\n",
4424 target_pid_to_str (lwp->ptid));
4425 }
4426 }
4427 return 0;
4428 }
4429
4430 static void
4431 linux_nat_stop (struct target_ops *self, ptid_t ptid)
4432 {
4433 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4434 }
4435
4436 static void
4437 linux_nat_interrupt (struct target_ops *self, ptid_t ptid)
4438 {
4439 if (non_stop)
4440 iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4441 else
4442 linux_ops->to_interrupt (linux_ops, ptid);
4443 }
4444
4445 static void
4446 linux_nat_close (struct target_ops *self)
4447 {
4448 /* Unregister from the event loop. */
4449 if (linux_nat_is_async_p (self))
4450 linux_nat_async (self, 0);
4451
4452 if (linux_ops->to_close)
4453 linux_ops->to_close (linux_ops);
4454
4455 super_close (self);
4456 }
4457
4458 /* When requests are passed down from the linux-nat layer to the
4459 single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4460 used. The address space pointer is stored in the inferior object,
4461 but the common code that is passed such ptid can't tell whether
4462 lwpid is a "main" process id or not (it assumes so). We reverse
4463 look up the "main" process id from the lwp here. */
4464
4465 static struct address_space *
4466 linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
4467 {
4468 struct lwp_info *lwp;
4469 struct inferior *inf;
4470 int pid;
4471
4472 if (ptid_get_lwp (ptid) == 0)
4473 {
4474 /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4475 tgid. */
4476 lwp = find_lwp_pid (ptid);
4477 pid = ptid_get_pid (lwp->ptid);
4478 }
4479 else
4480 {
4481 /* A (pid,lwpid,0) ptid. */
4482 pid = ptid_get_pid (ptid);
4483 }
4484
4485 inf = find_inferior_pid (pid);
4486 gdb_assert (inf != NULL);
4487 return inf->aspace;
4488 }
4489
4490 /* Return the cached value of the processor core for thread PTID. */
4491
4492 static int
4493 linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
4494 {
4495 struct lwp_info *info = find_lwp_pid (ptid);
4496
4497 if (info)
4498 return info->core;
4499 return -1;
4500 }
4501
4502 /* Implementation of to_filesystem_is_local. */
4503
4504 static int
4505 linux_nat_filesystem_is_local (struct target_ops *ops)
4506 {
4507 struct inferior *inf = current_inferior ();
4508
4509 if (inf->fake_pid_p || inf->pid == 0)
4510 return 1;
4511
4512 return linux_ns_same (inf->pid, LINUX_NS_MNT);
4513 }
4514
4515 /* Convert the INF argument passed to a to_fileio_* method
4516 to a process ID suitable for passing to its corresponding
4517 linux_mntns_* function. If INF is non-NULL then the
4518 caller is requesting the filesystem seen by INF. If INF
4519 is NULL then the caller is requesting the filesystem seen
4520 by the GDB. We fall back to GDB's filesystem in the case
4521 that INF is non-NULL but its PID is unknown. */
4522
4523 static pid_t
4524 linux_nat_fileio_pid_of (struct inferior *inf)
4525 {
4526 if (inf == NULL || inf->fake_pid_p || inf->pid == 0)
4527 return getpid ();
4528 else
4529 return inf->pid;
4530 }
4531
4532 /* Implementation of to_fileio_open. */
4533
4534 static int
4535 linux_nat_fileio_open (struct target_ops *self,
4536 struct inferior *inf, const char *filename,
4537 int flags, int mode, int warn_if_slow,
4538 int *target_errno)
4539 {
4540 int nat_flags;
4541 mode_t nat_mode;
4542 int fd;
4543
4544 if (fileio_to_host_openflags (flags, &nat_flags) == -1
4545 || fileio_to_host_mode (mode, &nat_mode) == -1)
4546 {
4547 *target_errno = FILEIO_EINVAL;
4548 return -1;
4549 }
4550
4551 fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf),
4552 filename, nat_flags, nat_mode);
4553 if (fd == -1)
4554 *target_errno = host_to_fileio_error (errno);
4555
4556 return fd;
4557 }
4558
4559 /* Implementation of to_fileio_readlink. */
4560
4561 static char *
4562 linux_nat_fileio_readlink (struct target_ops *self,
4563 struct inferior *inf, const char *filename,
4564 int *target_errno)
4565 {
4566 char buf[PATH_MAX];
4567 int len;
4568 char *ret;
4569
4570 len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf),
4571 filename, buf, sizeof (buf));
4572 if (len < 0)
4573 {
4574 *target_errno = host_to_fileio_error (errno);
4575 return NULL;
4576 }
4577
4578 ret = (char *) xmalloc (len + 1);
4579 memcpy (ret, buf, len);
4580 ret[len] = '\0';
4581 return ret;
4582 }
4583
4584 /* Implementation of to_fileio_unlink. */
4585
4586 static int
4587 linux_nat_fileio_unlink (struct target_ops *self,
4588 struct inferior *inf, const char *filename,
4589 int *target_errno)
4590 {
4591 int ret;
4592
4593 ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf),
4594 filename);
4595 if (ret == -1)
4596 *target_errno = host_to_fileio_error (errno);
4597
4598 return ret;
4599 }
4600
4601 void
4602 linux_nat_add_target (struct target_ops *t)
4603 {
4604 /* Save the provided single-threaded target. We save this in a separate
4605 variable because another target we've inherited from (e.g. inf-ptrace)
4606 may have saved a pointer to T; we want to use it for the final
4607 process stratum target. */
4608 linux_ops_saved = *t;
4609 linux_ops = &linux_ops_saved;
4610
4611 /* Override some methods for multithreading. */
4612 t->to_create_inferior = linux_nat_create_inferior;
4613 t->to_attach = linux_nat_attach;
4614 t->to_detach = linux_nat_detach;
4615 t->to_resume = linux_nat_resume;
4616 t->to_wait = linux_nat_wait;
4617 t->to_pass_signals = linux_nat_pass_signals;
4618 t->to_xfer_partial = linux_nat_xfer_partial;
4619 t->to_kill = linux_nat_kill;
4620 t->to_mourn_inferior = linux_nat_mourn_inferior;
4621 t->to_thread_alive = linux_nat_thread_alive;
4622 t->to_update_thread_list = linux_nat_update_thread_list;
4623 t->to_pid_to_str = linux_nat_pid_to_str;
4624 t->to_thread_name = linux_nat_thread_name;
4625 t->to_has_thread_control = tc_schedlock;
4626 t->to_thread_address_space = linux_nat_thread_address_space;
4627 t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
4628 t->to_stopped_data_address = linux_nat_stopped_data_address;
4629 t->to_stopped_by_sw_breakpoint = linux_nat_stopped_by_sw_breakpoint;
4630 t->to_supports_stopped_by_sw_breakpoint = linux_nat_supports_stopped_by_sw_breakpoint;
4631 t->to_stopped_by_hw_breakpoint = linux_nat_stopped_by_hw_breakpoint;
4632 t->to_supports_stopped_by_hw_breakpoint = linux_nat_supports_stopped_by_hw_breakpoint;
4633
4634 t->to_can_async_p = linux_nat_can_async_p;
4635 t->to_is_async_p = linux_nat_is_async_p;
4636 t->to_supports_non_stop = linux_nat_supports_non_stop;
4637 t->to_always_non_stop_p = linux_nat_always_non_stop_p;
4638 t->to_async = linux_nat_async;
4639 t->to_terminal_inferior = linux_nat_terminal_inferior;
4640 t->to_terminal_ours = linux_nat_terminal_ours;
4641
4642 super_close = t->to_close;
4643 t->to_close = linux_nat_close;
4644
4645 t->to_stop = linux_nat_stop;
4646 t->to_interrupt = linux_nat_interrupt;
4647
4648 t->to_supports_multi_process = linux_nat_supports_multi_process;
4649
4650 t->to_supports_disable_randomization
4651 = linux_nat_supports_disable_randomization;
4652
4653 t->to_core_of_thread = linux_nat_core_of_thread;
4654
4655 t->to_filesystem_is_local = linux_nat_filesystem_is_local;
4656 t->to_fileio_open = linux_nat_fileio_open;
4657 t->to_fileio_readlink = linux_nat_fileio_readlink;
4658 t->to_fileio_unlink = linux_nat_fileio_unlink;
4659
4660 /* We don't change the stratum; this target will sit at
4661 process_stratum and thread_db will set at thread_stratum. This
4662 is a little strange, since this is a multi-threaded-capable
4663 target, but we want to be on the stack below thread_db, and we
4664 also want to be used for single-threaded processes. */
4665
4666 add_target (t);
4667 }
4668
4669 /* Register a method to call whenever a new thread is attached. */
4670 void
4671 linux_nat_set_new_thread (struct target_ops *t,
4672 void (*new_thread) (struct lwp_info *))
4673 {
4674 /* Save the pointer. We only support a single registered instance
4675 of the GNU/Linux native target, so we do not need to map this to
4676 T. */
4677 linux_nat_new_thread = new_thread;
4678 }
4679
4680 /* See declaration in linux-nat.h. */
4681
4682 void
4683 linux_nat_set_new_fork (struct target_ops *t,
4684 linux_nat_new_fork_ftype *new_fork)
4685 {
4686 /* Save the pointer. */
4687 linux_nat_new_fork = new_fork;
4688 }
4689
4690 /* See declaration in linux-nat.h. */
4691
4692 void
4693 linux_nat_set_forget_process (struct target_ops *t,
4694 linux_nat_forget_process_ftype *fn)
4695 {
4696 /* Save the pointer. */
4697 linux_nat_forget_process_hook = fn;
4698 }
4699
4700 /* See declaration in linux-nat.h. */
4701
4702 void
4703 linux_nat_forget_process (pid_t pid)
4704 {
4705 if (linux_nat_forget_process_hook != NULL)
4706 linux_nat_forget_process_hook (pid);
4707 }
4708
4709 /* Register a method that converts a siginfo object between the layout
4710 that ptrace returns, and the layout in the architecture of the
4711 inferior. */
4712 void
4713 linux_nat_set_siginfo_fixup (struct target_ops *t,
4714 int (*siginfo_fixup) (siginfo_t *,
4715 gdb_byte *,
4716 int))
4717 {
4718 /* Save the pointer. */
4719 linux_nat_siginfo_fixup = siginfo_fixup;
4720 }
4721
4722 /* Register a method to call prior to resuming a thread. */
4723
4724 void
4725 linux_nat_set_prepare_to_resume (struct target_ops *t,
4726 void (*prepare_to_resume) (struct lwp_info *))
4727 {
4728 /* Save the pointer. */
4729 linux_nat_prepare_to_resume = prepare_to_resume;
4730 }
4731
4732 /* See linux-nat.h. */
4733
4734 int
4735 linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
4736 {
4737 int pid;
4738
4739 pid = ptid_get_lwp (ptid);
4740 if (pid == 0)
4741 pid = ptid_get_pid (ptid);
4742
4743 errno = 0;
4744 ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
4745 if (errno != 0)
4746 {
4747 memset (siginfo, 0, sizeof (*siginfo));
4748 return 0;
4749 }
4750 return 1;
4751 }
4752
4753 /* See nat/linux-nat.h. */
4754
4755 ptid_t
4756 current_lwp_ptid (void)
4757 {
4758 gdb_assert (ptid_lwp_p (inferior_ptid));
4759 return inferior_ptid;
4760 }
4761
4762 /* Provide a prototype to silence -Wmissing-prototypes. */
4763 extern initialize_file_ftype _initialize_linux_nat;
4764
4765 void
4766 _initialize_linux_nat (void)
4767 {
4768 add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
4769 &debug_linux_nat, _("\
4770 Set debugging of GNU/Linux lwp module."), _("\
4771 Show debugging of GNU/Linux lwp module."), _("\
4772 Enables printf debugging output."),
4773 NULL,
4774 show_debug_linux_nat,
4775 &setdebuglist, &showdebuglist);
4776
4777 add_setshow_boolean_cmd ("linux-namespaces", class_maintenance,
4778 &debug_linux_namespaces, _("\
4779 Set debugging of GNU/Linux namespaces module."), _("\
4780 Show debugging of GNU/Linux namespaces module."), _("\
4781 Enables printf debugging output."),
4782 NULL,
4783 NULL,
4784 &setdebuglist, &showdebuglist);
4785
4786 /* Save this mask as the default. */
4787 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
4788
4789 /* Install a SIGCHLD handler. */
4790 sigchld_action.sa_handler = sigchld_handler;
4791 sigemptyset (&sigchld_action.sa_mask);
4792 sigchld_action.sa_flags = SA_RESTART;
4793
4794 /* Make it the default. */
4795 sigaction (SIGCHLD, &sigchld_action, NULL);
4796
4797 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4798 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
4799 sigdelset (&suspend_mask, SIGCHLD);
4800
4801 sigemptyset (&blocked_mask);
4802 }
4803 \f
4804
4805 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4806 the GNU/Linux Threads library and therefore doesn't really belong
4807 here. */
4808
4809 /* Return the set of signals used by the threads library in *SET. */
4810
4811 void
4812 lin_thread_get_thread_signals (sigset_t *set)
4813 {
4814 sigemptyset (set);
4815
4816 /* NPTL reserves the first two RT signals, but does not provide any
4817 way for the debugger to query the signal numbers - fortunately
4818 they don't change. */
4819 sigaddset (set, __SIGRTMIN);
4820 sigaddset (set, __SIGRTMIN + 1);
4821 }
This page took 0.200896 seconds and 5 git commands to generate.