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