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