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