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