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