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