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