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