]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/linux-nat.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / linux-nat.c
CommitLineData
3993f6b1 1/* GNU/Linux native-dependent code common to multiple platforms.
dba24537 2
0fb0cc75 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
e26af52f 4 Free Software Foundation, Inc.
3993f6b1
DJ
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
3993f6b1
DJ
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3993f6b1
DJ
20
21#include "defs.h"
22#include "inferior.h"
23#include "target.h"
d6b0e80f 24#include "gdb_string.h"
3993f6b1 25#include "gdb_wait.h"
d6b0e80f
AC
26#include "gdb_assert.h"
27#ifdef HAVE_TKILL_SYSCALL
28#include <unistd.h>
29#include <sys/syscall.h>
30#endif
3993f6b1 31#include <sys/ptrace.h>
0274a8ce 32#include "linux-nat.h"
ac264b3b 33#include "linux-fork.h"
d6b0e80f
AC
34#include "gdbthread.h"
35#include "gdbcmd.h"
36#include "regcache.h"
4f844a66 37#include "regset.h"
10d6c8cd
DJ
38#include "inf-ptrace.h"
39#include "auxv.h"
dba24537
AC
40#include <sys/param.h> /* for MAXPATHLEN */
41#include <sys/procfs.h> /* for elf_gregset etc. */
42#include "elf-bfd.h" /* for elfcore_write_* */
43#include "gregset.h" /* for gregset */
44#include "gdbcore.h" /* for get_exec_file */
45#include <ctype.h> /* for isdigit */
46#include "gdbthread.h" /* for struct thread_info etc. */
47#include "gdb_stat.h" /* for struct stat */
48#include <fcntl.h> /* for O_RDONLY */
b84876c2
PA
49#include "inf-loop.h"
50#include "event-loop.h"
51#include "event-top.h"
07e059b5
VP
52#include <pwd.h>
53#include <sys/types.h>
54#include "gdb_dirent.h"
55#include "xml-support.h"
dba24537 56
10568435
JK
57#ifdef HAVE_PERSONALITY
58# include <sys/personality.h>
59# if !HAVE_DECL_ADDR_NO_RANDOMIZE
60# define ADDR_NO_RANDOMIZE 0x0040000
61# endif
62#endif /* HAVE_PERSONALITY */
63
8a77dff3
VP
64/* This comment documents high-level logic of this file.
65
66Waiting for events in sync mode
67===============================
68
69When waiting for an event in a specific thread, we just use waitpid, passing
70the specific pid, and not passing WNOHANG.
71
72When waiting for an event in all threads, waitpid is not quite good. Prior to
73version 2.4, Linux can either wait for event in main thread, or in secondary
74threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
75miss an event. The solution is to use non-blocking waitpid, together with
76sigsuspend. First, we use non-blocking waitpid to get an event in the main
77process, if any. Second, we use non-blocking waitpid with the __WCLONED
78flag to check for events in cloned processes. If nothing is found, we use
79sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
80happened to a child process -- and SIGCHLD will be delivered both for events
81in main debugged process and in cloned processes. As soon as we know there's
82an event, we get back to calling nonblocking waitpid with and without __WCLONED.
83
84Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
85so that we don't miss a signal. If SIGCHLD arrives in between, when it's
86blocked, the signal becomes pending and sigsuspend immediately
87notices it and returns.
88
89Waiting for events in async mode
90================================
91
92In async mode, GDB should always be ready to handle both user input and target
93events, so neither blocking waitpid nor sigsuspend are viable
94options. Instead, we should notify the GDB main event loop whenever there's
95unprocessed event from the target. The only way to notify this event loop is
96to make it wait on input from a pipe, and write something to the pipe whenever
97there's event. Obviously, if we fail to notify the event loop if there's
98target event, it's bad. If we notify the event loop when there's no event
99from target, linux-nat.c will detect that there's no event, actually, and
100report event of type TARGET_WAITKIND_IGNORE, but it will waste time and
101better avoided.
102
103The main design point is that every time GDB is outside linux-nat.c, we have a
104SIGCHLD handler installed that is called when something happens to the target
105and notifies the GDB event loop. Also, the event is extracted from the target
106using waitpid and stored for future use. Whenever GDB core decides to handle
107the event, and calls into linux-nat.c, we disable SIGCHLD and process things
108as in sync mode, except that before waitpid call we check if there are any
109previously read events.
110
111It could happen that during event processing, we'll try to get more events
112than there are events in the local queue, which will result to waitpid call.
113Those waitpid calls, while blocking, are guarantied to always have
114something for waitpid to return. E.g., stopping a thread with SIGSTOP, and
115waiting for the lwp to stop.
116
117The event loop is notified about new events using a pipe. SIGCHLD handler does
118waitpid and writes the results in to a pipe. GDB event loop has the other end
119of the pipe among the sources. When event loop starts to process the event
120and calls a function in linux-nat.c, all events from the pipe are transferred
121into a local queue and SIGCHLD is blocked. Further processing goes as in sync
122mode. Before we return from linux_nat_wait, we transfer all unprocessed events
123from local queue back to the pipe, so that when we get back to event loop,
124event loop will notice there's something more to do.
125
126SIGCHLD is blocked when we're inside target_wait, so that should we actually
127want to wait for some more events, SIGCHLD handler does not steal them from
128us. Technically, it would be possible to add new events to the local queue but
129it's about the same amount of work as blocking SIGCHLD.
130
131This moving of events from pipe into local queue and back into pipe when we
132enter/leave linux-nat.c is somewhat ugly. Unfortunately, GDB event loop is
133home-grown and incapable to wait on any queue.
134
135Use of signals
136==============
137
138We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
139signal is not entirely significant; we just need for a signal to be delivered,
140so that we can intercept it. SIGSTOP's advantage is that it can not be
141blocked. A disadvantage is that it is not a real-time signal, so it can only
142be queued once; we do not keep track of other sources of SIGSTOP.
143
144Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
145use them, because they have special behavior when the signal is generated -
146not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
147kills the entire thread group.
148
149A delivered SIGSTOP would stop the entire thread group, not just the thread we
150tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
151cancel it (by PTRACE_CONT without passing SIGSTOP).
152
153We could use a real-time signal instead. This would solve those problems; we
154could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
155But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
156generates it, and there are races with trying to find a signal that is not
157blocked. */
a0ef4274 158
dba24537
AC
159#ifndef O_LARGEFILE
160#define O_LARGEFILE 0
161#endif
0274a8ce 162
3993f6b1
DJ
163/* If the system headers did not provide the constants, hard-code the normal
164 values. */
165#ifndef PTRACE_EVENT_FORK
166
167#define PTRACE_SETOPTIONS 0x4200
168#define PTRACE_GETEVENTMSG 0x4201
169
170/* options set using PTRACE_SETOPTIONS */
171#define PTRACE_O_TRACESYSGOOD 0x00000001
172#define PTRACE_O_TRACEFORK 0x00000002
173#define PTRACE_O_TRACEVFORK 0x00000004
174#define PTRACE_O_TRACECLONE 0x00000008
175#define PTRACE_O_TRACEEXEC 0x00000010
9016a515
DJ
176#define PTRACE_O_TRACEVFORKDONE 0x00000020
177#define PTRACE_O_TRACEEXIT 0x00000040
3993f6b1
DJ
178
179/* Wait extended result codes for the above trace options. */
180#define PTRACE_EVENT_FORK 1
181#define PTRACE_EVENT_VFORK 2
182#define PTRACE_EVENT_CLONE 3
183#define PTRACE_EVENT_EXEC 4
c874c7fc 184#define PTRACE_EVENT_VFORK_DONE 5
9016a515 185#define PTRACE_EVENT_EXIT 6
3993f6b1
DJ
186
187#endif /* PTRACE_EVENT_FORK */
188
189/* We can't always assume that this flag is available, but all systems
190 with the ptrace event handlers also have __WALL, so it's safe to use
191 here. */
192#ifndef __WALL
193#define __WALL 0x40000000 /* Wait for any child. */
194#endif
195
02d3ff8c
UW
196#ifndef PTRACE_GETSIGINFO
197#define PTRACE_GETSIGINFO 0x4202
198#endif
199
10d6c8cd
DJ
200/* The single-threaded native GNU/Linux target_ops. We save a pointer for
201 the use of the multi-threaded target. */
202static struct target_ops *linux_ops;
f973ed9c 203static struct target_ops linux_ops_saved;
10d6c8cd 204
9f0bdab8
DJ
205/* The method to call, if any, when a new thread is attached. */
206static void (*linux_nat_new_thread) (ptid_t);
207
ac264b3b
MS
208/* The saved to_xfer_partial method, inherited from inf-ptrace.c.
209 Called by our to_xfer_partial. */
210static LONGEST (*super_xfer_partial) (struct target_ops *,
211 enum target_object,
212 const char *, gdb_byte *,
213 const gdb_byte *,
10d6c8cd
DJ
214 ULONGEST, LONGEST);
215
d6b0e80f 216static int debug_linux_nat;
920d2a44
AC
217static void
218show_debug_linux_nat (struct ui_file *file, int from_tty,
219 struct cmd_list_element *c, const char *value)
220{
221 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
222 value);
223}
d6b0e80f 224
b84876c2
PA
225static int debug_linux_nat_async = 0;
226static void
227show_debug_linux_nat_async (struct ui_file *file, int from_tty,
228 struct cmd_list_element *c, const char *value)
229{
230 fprintf_filtered (file, _("Debugging of GNU/Linux async lwp module is %s.\n"),
231 value);
232}
233
10568435
JK
234static int disable_randomization = 1;
235
236static void
237show_disable_randomization (struct ui_file *file, int from_tty,
238 struct cmd_list_element *c, const char *value)
239{
240#ifdef HAVE_PERSONALITY
241 fprintf_filtered (file, _("\
242Disabling randomization of debuggee's virtual address space is %s.\n"),
243 value);
244#else /* !HAVE_PERSONALITY */
245 fputs_filtered (_("\
246Disabling randomization of debuggee's virtual address space is unsupported on\n\
247this platform.\n"), file);
248#endif /* !HAVE_PERSONALITY */
249}
250
251static void
252set_disable_randomization (char *args, int from_tty, struct cmd_list_element *c)
253{
254#ifndef HAVE_PERSONALITY
255 error (_("\
256Disabling randomization of debuggee's virtual address space is unsupported on\n\
257this platform."));
258#endif /* !HAVE_PERSONALITY */
259}
260
9016a515
DJ
261static int linux_parent_pid;
262
ae087d01
DJ
263struct simple_pid_list
264{
265 int pid;
3d799a95 266 int status;
ae087d01
DJ
267 struct simple_pid_list *next;
268};
269struct simple_pid_list *stopped_pids;
270
3993f6b1
DJ
271/* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
272 can not be used, 1 if it can. */
273
274static int linux_supports_tracefork_flag = -1;
275
9016a515
DJ
276/* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
277 PTRACE_O_TRACEVFORKDONE. */
278
279static int linux_supports_tracevforkdone_flag = -1;
280
b84876c2
PA
281/* Async mode support */
282
b84876c2
PA
283/* Zero if the async mode, although enabled, is masked, which means
284 linux_nat_wait should behave as if async mode was off. */
285static int linux_nat_async_mask_value = 1;
286
287/* The read/write ends of the pipe registered as waitable file in the
288 event loop. */
289static int linux_nat_event_pipe[2] = { -1, -1 };
290
291/* Number of queued events in the pipe. */
292static volatile int linux_nat_num_queued_events;
293
84e46146 294/* The possible SIGCHLD handling states. */
b84876c2 295
84e46146
PA
296enum sigchld_state
297{
298 /* SIGCHLD disabled, with action set to sigchld_handler, for the
299 sigsuspend in linux_nat_wait. */
300 sigchld_sync,
301 /* SIGCHLD enabled, with action set to async_sigchld_handler. */
302 sigchld_async,
303 /* Set SIGCHLD to default action. Used while creating an
304 inferior. */
305 sigchld_default
306};
307
308/* The current SIGCHLD handling state. */
309static enum sigchld_state linux_nat_async_events_state;
310
311static enum sigchld_state linux_nat_async_events (enum sigchld_state enable);
b84876c2
PA
312static void pipe_to_local_event_queue (void);
313static void local_event_queue_to_pipe (void);
314static void linux_nat_event_pipe_push (int pid, int status, int options);
315static int linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options);
316static void linux_nat_set_async_mode (int on);
317static void linux_nat_async (void (*callback)
318 (enum inferior_event_type event_type, void *context),
319 void *context);
320static int linux_nat_async_mask (int mask);
a0ef4274 321static int kill_lwp (int lwpid, int signo);
b84876c2 322
4c28f408
PA
323static int stop_callback (struct lwp_info *lp, void *data);
324
b84876c2
PA
325/* Captures the result of a successful waitpid call, along with the
326 options used in that call. */
327struct waitpid_result
328{
329 int pid;
330 int status;
331 int options;
332 struct waitpid_result *next;
333};
334
335/* A singly-linked list of the results of the waitpid calls performed
336 in the async SIGCHLD handler. */
337static struct waitpid_result *waitpid_queue = NULL;
338
252fbfc8
PA
339/* Similarly to `waitpid', but check the local event queue instead of
340 querying the kernel queue. If PEEK, don't remove the event found
341 from the queue. */
342
b84876c2 343static int
252fbfc8 344queued_waitpid_1 (int pid, int *status, int flags, int peek)
b84876c2
PA
345{
346 struct waitpid_result *msg = waitpid_queue, *prev = NULL;
347
348 if (debug_linux_nat_async)
349 fprintf_unfiltered (gdb_stdlog,
350 "\
84e46146
PA
351QWPID: linux_nat_async_events_state(%d), linux_nat_num_queued_events(%d)\n",
352 linux_nat_async_events_state,
b84876c2
PA
353 linux_nat_num_queued_events);
354
355 if (flags & __WALL)
356 {
357 for (; msg; prev = msg, msg = msg->next)
358 if (pid == -1 || pid == msg->pid)
359 break;
360 }
361 else if (flags & __WCLONE)
362 {
363 for (; msg; prev = msg, msg = msg->next)
364 if (msg->options & __WCLONE
365 && (pid == -1 || pid == msg->pid))
366 break;
367 }
368 else
369 {
370 for (; msg; prev = msg, msg = msg->next)
371 if ((msg->options & __WCLONE) == 0
372 && (pid == -1 || pid == msg->pid))
373 break;
374 }
375
376 if (msg)
377 {
378 int pid;
379
b84876c2
PA
380 if (status)
381 *status = msg->status;
382 pid = msg->pid;
383
384 if (debug_linux_nat_async)
385 fprintf_unfiltered (gdb_stdlog, "QWPID: pid(%d), status(%x)\n",
386 pid, msg->status);
252fbfc8
PA
387
388 if (!peek)
389 {
390 if (prev)
391 prev->next = msg->next;
392 else
393 waitpid_queue = msg->next;
394
395 msg->next = NULL;
396 xfree (msg);
397 }
b84876c2
PA
398
399 return pid;
400 }
401
402 if (debug_linux_nat_async)
403 fprintf_unfiltered (gdb_stdlog, "QWPID: miss\n");
404
405 if (status)
406 *status = 0;
407 return -1;
408}
409
252fbfc8
PA
410/* Similarly to `waitpid', but check the local event queue. */
411
412static int
413queued_waitpid (int pid, int *status, int flags)
414{
415 return queued_waitpid_1 (pid, status, flags, 0);
416}
417
b84876c2
PA
418static void
419push_waitpid (int pid, int status, int options)
420{
421 struct waitpid_result *event, *new_event;
422
423 new_event = xmalloc (sizeof (*new_event));
424 new_event->pid = pid;
425 new_event->status = status;
426 new_event->options = options;
427 new_event->next = NULL;
428
429 if (waitpid_queue)
430 {
431 for (event = waitpid_queue;
432 event && event->next;
433 event = event->next)
434 ;
435
436 event->next = new_event;
437 }
438 else
439 waitpid_queue = new_event;
440}
441
710151dd 442/* Drain all queued events of PID. If PID is -1, the effect is of
b84876c2
PA
443 draining all events. */
444static void
445drain_queued_events (int pid)
446{
447 while (queued_waitpid (pid, NULL, __WALL) != -1)
448 ;
449}
450
ae087d01
DJ
451\f
452/* Trivial list manipulation functions to keep track of a list of
453 new stopped processes. */
454static void
3d799a95 455add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
ae087d01
DJ
456{
457 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
458 new_pid->pid = pid;
3d799a95 459 new_pid->status = status;
ae087d01
DJ
460 new_pid->next = *listp;
461 *listp = new_pid;
462}
463
464static int
3d799a95 465pull_pid_from_list (struct simple_pid_list **listp, int pid, int *status)
ae087d01
DJ
466{
467 struct simple_pid_list **p;
468
469 for (p = listp; *p != NULL; p = &(*p)->next)
470 if ((*p)->pid == pid)
471 {
472 struct simple_pid_list *next = (*p)->next;
3d799a95 473 *status = (*p)->status;
ae087d01
DJ
474 xfree (*p);
475 *p = next;
476 return 1;
477 }
478 return 0;
479}
480
3d799a95
DJ
481static void
482linux_record_stopped_pid (int pid, int status)
ae087d01 483{
3d799a95 484 add_to_pid_list (&stopped_pids, pid, status);
ae087d01
DJ
485}
486
3993f6b1
DJ
487\f
488/* A helper function for linux_test_for_tracefork, called after fork (). */
489
490static void
491linux_tracefork_child (void)
492{
493 int ret;
494
495 ptrace (PTRACE_TRACEME, 0, 0, 0);
496 kill (getpid (), SIGSTOP);
497 fork ();
48bb3cce 498 _exit (0);
3993f6b1
DJ
499}
500
b84876c2
PA
501/* Wrapper function for waitpid which handles EINTR, and checks for
502 locally queued events. */
b957e937
DJ
503
504static int
505my_waitpid (int pid, int *status, int flags)
506{
507 int ret;
b84876c2
PA
508
509 /* There should be no concurrent calls to waitpid. */
84e46146 510 gdb_assert (linux_nat_async_events_state == sigchld_sync);
b84876c2
PA
511
512 ret = queued_waitpid (pid, status, flags);
513 if (ret != -1)
514 return ret;
515
b957e937
DJ
516 do
517 {
518 ret = waitpid (pid, status, flags);
519 }
520 while (ret == -1 && errno == EINTR);
521
522 return ret;
523}
524
525/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
526
527 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
528 we know that the feature is not available. This may change the tracing
529 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
530
531 However, if it succeeds, we don't know for sure that the feature is
532 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
3993f6b1 533 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
b957e937
DJ
534 fork tracing, and let it fork. If the process exits, we assume that we
535 can't use TRACEFORK; if we get the fork notification, and we can extract
536 the new child's PID, then we assume that we can. */
3993f6b1
DJ
537
538static void
b957e937 539linux_test_for_tracefork (int original_pid)
3993f6b1
DJ
540{
541 int child_pid, ret, status;
542 long second_pid;
4c28f408
PA
543 enum sigchld_state async_events_original_state;
544
545 async_events_original_state = linux_nat_async_events (sigchld_sync);
3993f6b1 546
b957e937
DJ
547 linux_supports_tracefork_flag = 0;
548 linux_supports_tracevforkdone_flag = 0;
549
550 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
551 if (ret != 0)
552 return;
553
3993f6b1
DJ
554 child_pid = fork ();
555 if (child_pid == -1)
e2e0b3e5 556 perror_with_name (("fork"));
3993f6b1
DJ
557
558 if (child_pid == 0)
559 linux_tracefork_child ();
560
b957e937 561 ret = my_waitpid (child_pid, &status, 0);
3993f6b1 562 if (ret == -1)
e2e0b3e5 563 perror_with_name (("waitpid"));
3993f6b1 564 else if (ret != child_pid)
8a3fe4f8 565 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
3993f6b1 566 if (! WIFSTOPPED (status))
8a3fe4f8 567 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status);
3993f6b1 568
3993f6b1
DJ
569 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
570 if (ret != 0)
571 {
b957e937
DJ
572 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
573 if (ret != 0)
574 {
8a3fe4f8 575 warning (_("linux_test_for_tracefork: failed to kill child"));
4c28f408 576 linux_nat_async_events (async_events_original_state);
b957e937
DJ
577 return;
578 }
579
580 ret = my_waitpid (child_pid, &status, 0);
581 if (ret != child_pid)
8a3fe4f8 582 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
b957e937 583 else if (!WIFSIGNALED (status))
8a3fe4f8
AC
584 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
585 "killed child"), status);
b957e937 586
4c28f408 587 linux_nat_async_events (async_events_original_state);
3993f6b1
DJ
588 return;
589 }
590
9016a515
DJ
591 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
592 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
593 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
594 linux_supports_tracevforkdone_flag = (ret == 0);
595
b957e937
DJ
596 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
597 if (ret != 0)
8a3fe4f8 598 warning (_("linux_test_for_tracefork: failed to resume child"));
b957e937
DJ
599
600 ret = my_waitpid (child_pid, &status, 0);
601
3993f6b1
DJ
602 if (ret == child_pid && WIFSTOPPED (status)
603 && status >> 16 == PTRACE_EVENT_FORK)
604 {
605 second_pid = 0;
606 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
607 if (ret == 0 && second_pid != 0)
608 {
609 int second_status;
610
611 linux_supports_tracefork_flag = 1;
b957e937
DJ
612 my_waitpid (second_pid, &second_status, 0);
613 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
614 if (ret != 0)
8a3fe4f8 615 warning (_("linux_test_for_tracefork: failed to kill second child"));
97725dc4 616 my_waitpid (second_pid, &status, 0);
3993f6b1
DJ
617 }
618 }
b957e937 619 else
8a3fe4f8
AC
620 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
621 "(%d, status 0x%x)"), ret, status);
3993f6b1 622
b957e937
DJ
623 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
624 if (ret != 0)
8a3fe4f8 625 warning (_("linux_test_for_tracefork: failed to kill child"));
b957e937 626 my_waitpid (child_pid, &status, 0);
4c28f408
PA
627
628 linux_nat_async_events (async_events_original_state);
3993f6b1
DJ
629}
630
631/* Return non-zero iff we have tracefork functionality available.
632 This function also sets linux_supports_tracefork_flag. */
633
634static int
b957e937 635linux_supports_tracefork (int pid)
3993f6b1
DJ
636{
637 if (linux_supports_tracefork_flag == -1)
b957e937 638 linux_test_for_tracefork (pid);
3993f6b1
DJ
639 return linux_supports_tracefork_flag;
640}
641
9016a515 642static int
b957e937 643linux_supports_tracevforkdone (int pid)
9016a515
DJ
644{
645 if (linux_supports_tracefork_flag == -1)
b957e937 646 linux_test_for_tracefork (pid);
9016a515
DJ
647 return linux_supports_tracevforkdone_flag;
648}
649
3993f6b1 650\f
4de4c07c
DJ
651void
652linux_enable_event_reporting (ptid_t ptid)
653{
d3587048 654 int pid = ptid_get_lwp (ptid);
4de4c07c
DJ
655 int options;
656
d3587048
DJ
657 if (pid == 0)
658 pid = ptid_get_pid (ptid);
659
b957e937 660 if (! linux_supports_tracefork (pid))
4de4c07c
DJ
661 return;
662
a2f23071
DJ
663 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
664 | PTRACE_O_TRACECLONE;
b957e937 665 if (linux_supports_tracevforkdone (pid))
9016a515
DJ
666 options |= PTRACE_O_TRACEVFORKDONE;
667
668 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
669 read-only process state. */
4de4c07c
DJ
670
671 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
672}
673
6d8fd2b7
UW
674static void
675linux_child_post_attach (int pid)
4de4c07c
DJ
676{
677 linux_enable_event_reporting (pid_to_ptid (pid));
0ec9a092 678 check_for_thread_db ();
4de4c07c
DJ
679}
680
10d6c8cd 681static void
4de4c07c
DJ
682linux_child_post_startup_inferior (ptid_t ptid)
683{
684 linux_enable_event_reporting (ptid);
0ec9a092 685 check_for_thread_db ();
4de4c07c
DJ
686}
687
6d8fd2b7
UW
688static int
689linux_child_follow_fork (struct target_ops *ops, int follow_child)
3993f6b1 690{
4de4c07c
DJ
691 ptid_t last_ptid;
692 struct target_waitstatus last_status;
9016a515 693 int has_vforked;
4de4c07c
DJ
694 int parent_pid, child_pid;
695
b84876c2
PA
696 if (target_can_async_p ())
697 target_async (NULL, 0);
698
4de4c07c 699 get_last_target_status (&last_ptid, &last_status);
9016a515 700 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
d3587048
DJ
701 parent_pid = ptid_get_lwp (last_ptid);
702 if (parent_pid == 0)
703 parent_pid = ptid_get_pid (last_ptid);
3a3e9ee3 704 child_pid = PIDGET (last_status.value.related_pid);
4de4c07c
DJ
705
706 if (! follow_child)
707 {
708 /* We're already attached to the parent, by default. */
709
710 /* Before detaching from the child, remove all breakpoints from
711 it. (This won't actually modify the breakpoint list, but will
712 physically remove the breakpoints from the child.) */
9016a515
DJ
713 /* If we vforked this will remove the breakpoints from the parent
714 also, but they'll be reinserted below. */
4de4c07c
DJ
715 detach_breakpoints (child_pid);
716
ac264b3b
MS
717 /* Detach new forked process? */
718 if (detach_fork)
f75c00e4 719 {
e85a822c 720 if (info_verbose || debug_linux_nat)
ac264b3b
MS
721 {
722 target_terminal_ours ();
723 fprintf_filtered (gdb_stdlog,
724 "Detaching after fork from child process %d.\n",
725 child_pid);
726 }
4de4c07c 727
ac264b3b
MS
728 ptrace (PTRACE_DETACH, child_pid, 0, 0);
729 }
730 else
731 {
732 struct fork_info *fp;
77435e4c 733 struct inferior *parent_inf, *child_inf;
7f9f62ba
PA
734
735 /* Add process to GDB's tables. */
77435e4c
PA
736 child_inf = add_inferior (child_pid);
737
738 parent_inf = find_inferior_pid (GET_PID (last_ptid));
739 child_inf->attach_flag = parent_inf->attach_flag;
7f9f62ba 740
ac264b3b
MS
741 /* Retain child fork in ptrace (stopped) state. */
742 fp = find_fork_pid (child_pid);
743 if (!fp)
744 fp = add_fork (child_pid);
745 fork_save_infrun_state (fp, 0);
746 }
9016a515
DJ
747
748 if (has_vforked)
749 {
b957e937
DJ
750 gdb_assert (linux_supports_tracefork_flag >= 0);
751 if (linux_supports_tracevforkdone (0))
9016a515
DJ
752 {
753 int status;
754
755 ptrace (PTRACE_CONT, parent_pid, 0, 0);
58aecb61 756 my_waitpid (parent_pid, &status, __WALL);
c874c7fc 757 if ((status >> 16) != PTRACE_EVENT_VFORK_DONE)
8a3fe4f8
AC
758 warning (_("Unexpected waitpid result %06x when waiting for "
759 "vfork-done"), status);
9016a515
DJ
760 }
761 else
762 {
763 /* We can't insert breakpoints until the child has
764 finished with the shared memory region. We need to
765 wait until that happens. Ideal would be to just
766 call:
767 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
768 - waitpid (parent_pid, &status, __WALL);
769 However, most architectures can't handle a syscall
770 being traced on the way out if it wasn't traced on
771 the way in.
772
773 We might also think to loop, continuing the child
774 until it exits or gets a SIGTRAP. One problem is
775 that the child might call ptrace with PTRACE_TRACEME.
776
777 There's no simple and reliable way to figure out when
778 the vforked child will be done with its copy of the
779 shared memory. We could step it out of the syscall,
780 two instructions, let it go, and then single-step the
781 parent once. When we have hardware single-step, this
782 would work; with software single-step it could still
783 be made to work but we'd have to be able to insert
784 single-step breakpoints in the child, and we'd have
785 to insert -just- the single-step breakpoint in the
786 parent. Very awkward.
787
788 In the end, the best we can do is to make sure it
789 runs for a little while. Hopefully it will be out of
790 range of any breakpoints we reinsert. Usually this
791 is only the single-step breakpoint at vfork's return
792 point. */
793
794 usleep (10000);
795 }
796
797 /* Since we vforked, breakpoints were removed in the parent
798 too. Put them back. */
799 reattach_breakpoints (parent_pid);
800 }
4de4c07c 801 }
3993f6b1 802 else
4de4c07c 803 {
4e1c45ea
PA
804 struct thread_info *last_tp = find_thread_pid (last_ptid);
805 struct thread_info *tp;
4de4c07c 806 char child_pid_spelling[40];
77435e4c 807 struct inferior *parent_inf, *child_inf;
4de4c07c 808
4e1c45ea
PA
809 /* Copy user stepping state to the new inferior thread. */
810 struct breakpoint *step_resume_breakpoint = last_tp->step_resume_breakpoint;
811 CORE_ADDR step_range_start = last_tp->step_range_start;
812 CORE_ADDR step_range_end = last_tp->step_range_end;
813 struct frame_id step_frame_id = last_tp->step_frame_id;
814
815 /* Otherwise, deleting the parent would get rid of this
816 breakpoint. */
817 last_tp->step_resume_breakpoint = NULL;
818
4de4c07c 819 /* Needed to keep the breakpoint lists in sync. */
9016a515
DJ
820 if (! has_vforked)
821 detach_breakpoints (child_pid);
4de4c07c
DJ
822
823 /* Before detaching from the parent, remove all breakpoints from it. */
824 remove_breakpoints ();
825
e85a822c 826 if (info_verbose || debug_linux_nat)
f75c00e4
DJ
827 {
828 target_terminal_ours ();
ac264b3b
MS
829 fprintf_filtered (gdb_stdlog,
830 "Attaching after fork to child process %d.\n",
831 child_pid);
f75c00e4 832 }
4de4c07c 833
7a7d3353
PA
834 /* Add the new inferior first, so that the target_detach below
835 doesn't unpush the target. */
836
77435e4c
PA
837 child_inf = add_inferior (child_pid);
838
839 parent_inf = find_inferior_pid (GET_PID (last_ptid));
840 child_inf->attach_flag = parent_inf->attach_flag;
7a7d3353 841
9016a515
DJ
842 /* If we're vforking, we may want to hold on to the parent until
843 the child exits or execs. At exec time we can remove the old
844 breakpoints from the parent and detach it; at exit time we
845 could do the same (or even, sneakily, resume debugging it - the
846 child's exec has failed, or something similar).
847
848 This doesn't clean up "properly", because we can't call
849 target_detach, but that's OK; if the current target is "child",
850 then it doesn't need any further cleanups, and lin_lwp will
851 generally not encounter vfork (vfork is defined to fork
852 in libpthread.so).
853
854 The holding part is very easy if we have VFORKDONE events;
855 but keeping track of both processes is beyond GDB at the
856 moment. So we don't expose the parent to the rest of GDB.
857 Instead we quietly hold onto it until such time as we can
858 safely resume it. */
859
860 if (has_vforked)
7f9f62ba
PA
861 {
862 linux_parent_pid = parent_pid;
863 detach_inferior (parent_pid);
864 }
ac264b3b
MS
865 else if (!detach_fork)
866 {
867 struct fork_info *fp;
868 /* Retain parent fork in ptrace (stopped) state. */
869 fp = find_fork_pid (parent_pid);
870 if (!fp)
871 fp = add_fork (parent_pid);
872 fork_save_infrun_state (fp, 0);
0d14fc63
PA
873
874 /* Also add an entry for the child fork. */
875 fp = find_fork_pid (child_pid);
876 if (!fp)
877 fp = add_fork (child_pid);
878 fork_save_infrun_state (fp, 0);
ac264b3b 879 }
9016a515 880 else
b84876c2 881 target_detach (NULL, 0);
4de4c07c 882
9f0bdab8 883 inferior_ptid = ptid_build (child_pid, child_pid, 0);
ac264b3b 884
9f0bdab8 885 linux_nat_switch_fork (inferior_ptid);
ef29ce1a 886 check_for_thread_db ();
4de4c07c 887
4e1c45ea
PA
888 tp = inferior_thread ();
889 tp->step_resume_breakpoint = step_resume_breakpoint;
890 tp->step_range_start = step_range_start;
891 tp->step_range_end = step_range_end;
892 tp->step_frame_id = step_frame_id;
893
4de4c07c
DJ
894 /* Reset breakpoints in the child as appropriate. */
895 follow_inferior_reset_breakpoints ();
896 }
897
b84876c2
PA
898 if (target_can_async_p ())
899 target_async (inferior_event_handler, 0);
900
4de4c07c
DJ
901 return 0;
902}
903
4de4c07c 904\f
6d8fd2b7
UW
905static void
906linux_child_insert_fork_catchpoint (int pid)
4de4c07c 907{
b957e937 908 if (! linux_supports_tracefork (pid))
8a3fe4f8 909 error (_("Your system does not support fork catchpoints."));
3993f6b1
DJ
910}
911
6d8fd2b7
UW
912static void
913linux_child_insert_vfork_catchpoint (int pid)
3993f6b1 914{
b957e937 915 if (!linux_supports_tracefork (pid))
8a3fe4f8 916 error (_("Your system does not support vfork catchpoints."));
3993f6b1
DJ
917}
918
6d8fd2b7
UW
919static void
920linux_child_insert_exec_catchpoint (int pid)
3993f6b1 921{
b957e937 922 if (!linux_supports_tracefork (pid))
8a3fe4f8 923 error (_("Your system does not support exec catchpoints."));
3993f6b1
DJ
924}
925
d6b0e80f
AC
926/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
927 are processes sharing the same VM space. A multi-threaded process
928 is basically a group of such processes. However, such a grouping
929 is almost entirely a user-space issue; the kernel doesn't enforce
930 such a grouping at all (this might change in the future). In
931 general, we'll rely on the threads library (i.e. the GNU/Linux
932 Threads library) to provide such a grouping.
933
934 It is perfectly well possible to write a multi-threaded application
935 without the assistance of a threads library, by using the clone
936 system call directly. This module should be able to give some
937 rudimentary support for debugging such applications if developers
938 specify the CLONE_PTRACE flag in the clone system call, and are
939 using the Linux kernel 2.4 or above.
940
941 Note that there are some peculiarities in GNU/Linux that affect
942 this code:
943
944 - In general one should specify the __WCLONE flag to waitpid in
945 order to make it report events for any of the cloned processes
946 (and leave it out for the initial process). However, if a cloned
947 process has exited the exit status is only reported if the
948 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
949 we cannot use it since GDB must work on older systems too.
950
951 - When a traced, cloned process exits and is waited for by the
952 debugger, the kernel reassigns it to the original parent and
953 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
954 library doesn't notice this, which leads to the "zombie problem":
955 When debugged a multi-threaded process that spawns a lot of
956 threads will run out of processes, even if the threads exit,
957 because the "zombies" stay around. */
958
959/* List of known LWPs. */
9f0bdab8 960struct lwp_info *lwp_list;
d6b0e80f
AC
961
962/* Number of LWPs in the list. */
963static int num_lwps;
d6b0e80f
AC
964\f
965
d6b0e80f
AC
966/* Original signal mask. */
967static sigset_t normal_mask;
968
969/* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
970 _initialize_linux_nat. */
971static sigset_t suspend_mask;
972
b84876c2
PA
973/* SIGCHLD action for synchronous mode. */
974struct sigaction sync_sigchld_action;
975
976/* SIGCHLD action for asynchronous mode. */
977static struct sigaction async_sigchld_action;
84e46146
PA
978
979/* SIGCHLD default action, to pass to new inferiors. */
980static struct sigaction sigchld_default_action;
d6b0e80f
AC
981\f
982
983/* Prototypes for local functions. */
984static int stop_wait_callback (struct lwp_info *lp, void *data);
985static int linux_nat_thread_alive (ptid_t ptid);
6d8fd2b7 986static char *linux_child_pid_to_exec_file (int pid);
710151dd
PA
987static int cancel_breakpoint (struct lwp_info *lp);
988
d6b0e80f
AC
989\f
990/* Convert wait status STATUS to a string. Used for printing debug
991 messages only. */
992
993static char *
994status_to_str (int status)
995{
996 static char buf[64];
997
998 if (WIFSTOPPED (status))
999 snprintf (buf, sizeof (buf), "%s (stopped)",
1000 strsignal (WSTOPSIG (status)));
1001 else if (WIFSIGNALED (status))
1002 snprintf (buf, sizeof (buf), "%s (terminated)",
1003 strsignal (WSTOPSIG (status)));
1004 else
1005 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
1006
1007 return buf;
1008}
1009
1010/* Initialize the list of LWPs. Note that this module, contrary to
1011 what GDB's generic threads layer does for its thread list,
1012 re-initializes the LWP lists whenever we mourn or detach (which
1013 doesn't involve mourning) the inferior. */
1014
1015static void
1016init_lwp_list (void)
1017{
1018 struct lwp_info *lp, *lpnext;
1019
1020 for (lp = lwp_list; lp; lp = lpnext)
1021 {
1022 lpnext = lp->next;
1023 xfree (lp);
1024 }
1025
1026 lwp_list = NULL;
1027 num_lwps = 0;
d6b0e80f
AC
1028}
1029
f973ed9c 1030/* Add the LWP specified by PID to the list. Return a pointer to the
9f0bdab8
DJ
1031 structure describing the new LWP. The LWP should already be stopped
1032 (with an exception for the very first LWP). */
d6b0e80f
AC
1033
1034static struct lwp_info *
1035add_lwp (ptid_t ptid)
1036{
1037 struct lwp_info *lp;
1038
1039 gdb_assert (is_lwp (ptid));
1040
1041 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
1042
1043 memset (lp, 0, sizeof (struct lwp_info));
1044
1045 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1046
1047 lp->ptid = ptid;
1048
1049 lp->next = lwp_list;
1050 lwp_list = lp;
f973ed9c 1051 ++num_lwps;
d6b0e80f 1052
9f0bdab8
DJ
1053 if (num_lwps > 1 && linux_nat_new_thread != NULL)
1054 linux_nat_new_thread (ptid);
1055
d6b0e80f
AC
1056 return lp;
1057}
1058
1059/* Remove the LWP specified by PID from the list. */
1060
1061static void
1062delete_lwp (ptid_t ptid)
1063{
1064 struct lwp_info *lp, *lpprev;
1065
1066 lpprev = NULL;
1067
1068 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
1069 if (ptid_equal (lp->ptid, ptid))
1070 break;
1071
1072 if (!lp)
1073 return;
1074
d6b0e80f
AC
1075 num_lwps--;
1076
1077 if (lpprev)
1078 lpprev->next = lp->next;
1079 else
1080 lwp_list = lp->next;
1081
1082 xfree (lp);
1083}
1084
1085/* Return a pointer to the structure describing the LWP corresponding
1086 to PID. If no corresponding LWP could be found, return NULL. */
1087
1088static struct lwp_info *
1089find_lwp_pid (ptid_t ptid)
1090{
1091 struct lwp_info *lp;
1092 int lwp;
1093
1094 if (is_lwp (ptid))
1095 lwp = GET_LWP (ptid);
1096 else
1097 lwp = GET_PID (ptid);
1098
1099 for (lp = lwp_list; lp; lp = lp->next)
1100 if (lwp == GET_LWP (lp->ptid))
1101 return lp;
1102
1103 return NULL;
1104}
1105
1106/* Call CALLBACK with its second argument set to DATA for every LWP in
1107 the list. If CALLBACK returns 1 for a particular LWP, return a
1108 pointer to the structure describing that LWP immediately.
1109 Otherwise return NULL. */
1110
1111struct lwp_info *
1112iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
1113{
1114 struct lwp_info *lp, *lpnext;
1115
1116 for (lp = lwp_list; lp; lp = lpnext)
1117 {
1118 lpnext = lp->next;
1119 if ((*callback) (lp, data))
1120 return lp;
1121 }
1122
1123 return NULL;
1124}
1125
f973ed9c
DJ
1126/* Update our internal state when changing from one fork (checkpoint,
1127 et cetera) to another indicated by NEW_PTID. We can only switch
1128 single-threaded applications, so we only create one new LWP, and
1129 the previous list is discarded. */
1130
1131void
1132linux_nat_switch_fork (ptid_t new_ptid)
1133{
1134 struct lwp_info *lp;
1135
1136 init_lwp_list ();
1137 lp = add_lwp (new_ptid);
1138 lp->stopped = 1;
e26af52f 1139
4f8d22e3
PA
1140 init_thread_list ();
1141 add_thread_silent (new_ptid);
e26af52f
DJ
1142}
1143
e26af52f
DJ
1144/* Handle the exit of a single thread LP. */
1145
1146static void
1147exit_lwp (struct lwp_info *lp)
1148{
063bfe2e
VP
1149 struct thread_info *th = find_thread_pid (lp->ptid);
1150
1151 if (th)
e26af52f 1152 {
17faa917
DJ
1153 if (print_thread_events)
1154 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
1155
4f8d22e3 1156 delete_thread (lp->ptid);
e26af52f
DJ
1157 }
1158
1159 delete_lwp (lp->ptid);
1160}
1161
a0ef4274
DJ
1162/* Detect `T (stopped)' in `/proc/PID/status'.
1163 Other states including `T (tracing stop)' are reported as false. */
1164
1165static int
1166pid_is_stopped (pid_t pid)
1167{
1168 FILE *status_file;
1169 char buf[100];
1170 int retval = 0;
1171
1172 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) pid);
1173 status_file = fopen (buf, "r");
1174 if (status_file != NULL)
1175 {
1176 int have_state = 0;
1177
1178 while (fgets (buf, sizeof (buf), status_file))
1179 {
1180 if (strncmp (buf, "State:", 6) == 0)
1181 {
1182 have_state = 1;
1183 break;
1184 }
1185 }
1186 if (have_state && strstr (buf, "T (stopped)") != NULL)
1187 retval = 1;
1188 fclose (status_file);
1189 }
1190 return retval;
1191}
1192
1193/* Wait for the LWP specified by LP, which we have just attached to.
1194 Returns a wait status for that LWP, to cache. */
1195
1196static int
1197linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
1198 int *signalled)
1199{
1200 pid_t new_pid, pid = GET_LWP (ptid);
1201 int status;
1202
1203 if (pid_is_stopped (pid))
1204 {
1205 if (debug_linux_nat)
1206 fprintf_unfiltered (gdb_stdlog,
1207 "LNPAW: Attaching to a stopped process\n");
1208
1209 /* The process is definitely stopped. It is in a job control
1210 stop, unless the kernel predates the TASK_STOPPED /
1211 TASK_TRACED distinction, in which case it might be in a
1212 ptrace stop. Make sure it is in a ptrace stop; from there we
1213 can kill it, signal it, et cetera.
1214
1215 First make sure there is a pending SIGSTOP. Since we are
1216 already attached, the process can not transition from stopped
1217 to running without a PTRACE_CONT; so we know this signal will
1218 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1219 probably already in the queue (unless this kernel is old
1220 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1221 is not an RT signal, it can only be queued once. */
1222 kill_lwp (pid, SIGSTOP);
1223
1224 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1225 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1226 ptrace (PTRACE_CONT, pid, 0, 0);
1227 }
1228
1229 /* Make sure the initial process is stopped. The user-level threads
1230 layer might want to poke around in the inferior, and that won't
1231 work if things haven't stabilized yet. */
1232 new_pid = my_waitpid (pid, &status, 0);
1233 if (new_pid == -1 && errno == ECHILD)
1234 {
1235 if (first)
1236 warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1237
1238 /* Try again with __WCLONE to check cloned processes. */
1239 new_pid = my_waitpid (pid, &status, __WCLONE);
1240 *cloned = 1;
1241 }
1242
1243 gdb_assert (pid == new_pid && WIFSTOPPED (status));
1244
1245 if (WSTOPSIG (status) != SIGSTOP)
1246 {
1247 *signalled = 1;
1248 if (debug_linux_nat)
1249 fprintf_unfiltered (gdb_stdlog,
1250 "LNPAW: Received %s after attaching\n",
1251 status_to_str (status));
1252 }
1253
1254 return status;
1255}
1256
1257/* Attach to the LWP specified by PID. Return 0 if successful or -1
1258 if the new LWP could not be attached. */
d6b0e80f 1259
9ee57c33 1260int
93815fbf 1261lin_lwp_attach_lwp (ptid_t ptid)
d6b0e80f 1262{
9ee57c33 1263 struct lwp_info *lp;
84e46146 1264 enum sigchld_state async_events_original_state;
d6b0e80f
AC
1265
1266 gdb_assert (is_lwp (ptid));
1267
84e46146 1268 async_events_original_state = linux_nat_async_events (sigchld_sync);
d6b0e80f 1269
9ee57c33 1270 lp = find_lwp_pid (ptid);
d6b0e80f
AC
1271
1272 /* We assume that we're already attached to any LWP that has an id
1273 equal to the overall process id, and to any LWP that is already
1274 in our list of LWPs. If we're not seeing exit events from threads
1275 and we've had PID wraparound since we last tried to stop all threads,
1276 this assumption might be wrong; fortunately, this is very unlikely
1277 to happen. */
9ee57c33 1278 if (GET_LWP (ptid) != GET_PID (ptid) && lp == NULL)
d6b0e80f 1279 {
a0ef4274 1280 int status, cloned = 0, signalled = 0;
d6b0e80f
AC
1281
1282 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
9ee57c33
DJ
1283 {
1284 /* If we fail to attach to the thread, issue a warning,
1285 but continue. One way this can happen is if thread
e9efe249 1286 creation is interrupted; as of Linux kernel 2.6.19, a
9ee57c33
DJ
1287 bug may place threads in the thread list and then fail
1288 to create them. */
1289 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1290 safe_strerror (errno));
1291 return -1;
1292 }
1293
d6b0e80f
AC
1294 if (debug_linux_nat)
1295 fprintf_unfiltered (gdb_stdlog,
1296 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1297 target_pid_to_str (ptid));
1298
a0ef4274
DJ
1299 status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1300 lp = add_lwp (ptid);
1301 lp->stopped = 1;
1302 lp->cloned = cloned;
1303 lp->signalled = signalled;
1304 if (WSTOPSIG (status) != SIGSTOP)
d6b0e80f 1305 {
a0ef4274
DJ
1306 lp->resumed = 1;
1307 lp->status = status;
d6b0e80f
AC
1308 }
1309
a0ef4274 1310 target_post_attach (GET_LWP (lp->ptid));
d6b0e80f
AC
1311
1312 if (debug_linux_nat)
1313 {
1314 fprintf_unfiltered (gdb_stdlog,
1315 "LLAL: waitpid %s received %s\n",
1316 target_pid_to_str (ptid),
1317 status_to_str (status));
1318 }
1319 }
1320 else
1321 {
1322 /* We assume that the LWP representing the original process is
1323 already stopped. Mark it as stopped in the data structure
155bd5d1
AC
1324 that the GNU/linux ptrace layer uses to keep track of
1325 threads. Note that this won't have already been done since
1326 the main thread will have, we assume, been stopped by an
1327 attach from a different layer. */
9ee57c33
DJ
1328 if (lp == NULL)
1329 lp = add_lwp (ptid);
d6b0e80f
AC
1330 lp->stopped = 1;
1331 }
9ee57c33 1332
84e46146 1333 linux_nat_async_events (async_events_original_state);
9ee57c33 1334 return 0;
d6b0e80f
AC
1335}
1336
b84876c2 1337static void
136d6dae
VP
1338linux_nat_create_inferior (struct target_ops *ops,
1339 char *exec_file, char *allargs, char **env,
b84876c2
PA
1340 int from_tty)
1341{
1342 int saved_async = 0;
10568435
JK
1343#ifdef HAVE_PERSONALITY
1344 int personality_orig = 0, personality_set = 0;
1345#endif /* HAVE_PERSONALITY */
b84876c2
PA
1346
1347 /* The fork_child mechanism is synchronous and calls target_wait, so
1348 we have to mask the async mode. */
1349
1350 if (target_can_async_p ())
84e46146
PA
1351 /* Mask async mode. Creating a child requires a loop calling
1352 wait_for_inferior currently. */
b84876c2
PA
1353 saved_async = linux_nat_async_mask (0);
1354 else
1355 {
1356 /* Restore the original signal mask. */
1357 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1358 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1359 suspend_mask = normal_mask;
1360 sigdelset (&suspend_mask, SIGCHLD);
1361 }
1362
84e46146
PA
1363 /* Set SIGCHLD to the default action, until after execing the child,
1364 since the inferior inherits the superior's signal mask. It will
1365 be blocked again in linux_nat_wait, which is only reached after
1366 the inferior execing. */
1367 linux_nat_async_events (sigchld_default);
1368
10568435
JK
1369#ifdef HAVE_PERSONALITY
1370 if (disable_randomization)
1371 {
1372 errno = 0;
1373 personality_orig = personality (0xffffffff);
1374 if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
1375 {
1376 personality_set = 1;
1377 personality (personality_orig | ADDR_NO_RANDOMIZE);
1378 }
1379 if (errno != 0 || (personality_set
1380 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
1381 warning (_("Error disabling address space randomization: %s"),
1382 safe_strerror (errno));
1383 }
1384#endif /* HAVE_PERSONALITY */
1385
136d6dae 1386 linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
b84876c2 1387
10568435
JK
1388#ifdef HAVE_PERSONALITY
1389 if (personality_set)
1390 {
1391 errno = 0;
1392 personality (personality_orig);
1393 if (errno != 0)
1394 warning (_("Error restoring address space randomization: %s"),
1395 safe_strerror (errno));
1396 }
1397#endif /* HAVE_PERSONALITY */
1398
b84876c2
PA
1399 if (saved_async)
1400 linux_nat_async_mask (saved_async);
1401}
1402
d6b0e80f 1403static void
136d6dae 1404linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
d6b0e80f
AC
1405{
1406 struct lwp_info *lp;
d6b0e80f 1407 int status;
af990527 1408 ptid_t ptid;
d6b0e80f
AC
1409
1410 /* FIXME: We should probably accept a list of process id's, and
1411 attach all of them. */
136d6dae 1412 linux_ops->to_attach (ops, args, from_tty);
d6b0e80f 1413
b84876c2
PA
1414 if (!target_can_async_p ())
1415 {
1416 /* Restore the original signal mask. */
1417 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1418 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1419 suspend_mask = normal_mask;
1420 sigdelset (&suspend_mask, SIGCHLD);
1421 }
1422
af990527
PA
1423 /* The ptrace base target adds the main thread with (pid,0,0)
1424 format. Decorate it with lwp info. */
1425 ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid));
1426 thread_change_ptid (inferior_ptid, ptid);
1427
9f0bdab8 1428 /* Add the initial process as the first LWP to the list. */
af990527 1429 lp = add_lwp (ptid);
a0ef4274
DJ
1430
1431 status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1432 &lp->signalled);
1433 lp->stopped = 1;
9f0bdab8 1434
a0ef4274 1435 /* Save the wait status to report later. */
d6b0e80f 1436 lp->resumed = 1;
a0ef4274
DJ
1437 if (debug_linux_nat)
1438 fprintf_unfiltered (gdb_stdlog,
1439 "LNA: waitpid %ld, saving status %s\n",
1440 (long) GET_PID (lp->ptid), status_to_str (status));
710151dd
PA
1441
1442 if (!target_can_async_p ())
a0ef4274 1443 lp->status = status;
710151dd
PA
1444 else
1445 {
1446 /* We already waited for this LWP, so put the wait result on the
1447 pipe. The event loop will wake up and gets us to handling
1448 this event. */
a0ef4274
DJ
1449 linux_nat_event_pipe_push (GET_PID (lp->ptid), status,
1450 lp->cloned ? __WCLONE : 0);
b84876c2
PA
1451 /* Register in the event loop. */
1452 target_async (inferior_event_handler, 0);
d6b0e80f
AC
1453 }
1454}
1455
a0ef4274
DJ
1456/* Get pending status of LP. */
1457static int
1458get_pending_status (struct lwp_info *lp, int *status)
1459{
1460 struct target_waitstatus last;
1461 ptid_t last_ptid;
1462
1463 get_last_target_status (&last_ptid, &last);
1464
1465 /* If this lwp is the ptid that GDB is processing an event from, the
1466 signal will be in stop_signal. Otherwise, in all-stop + sync
1467 mode, we may cache pending events in lp->status while trying to
1468 stop all threads (see stop_wait_callback). In async mode, the
1469 events are always cached in waitpid_queue. */
1470
1471 *status = 0;
4c28f408
PA
1472
1473 if (non_stop)
a0ef4274 1474 {
4c28f408
PA
1475 enum target_signal signo = TARGET_SIGNAL_0;
1476
1477 if (is_executing (lp->ptid))
1478 {
1479 /* If the core thought this lwp was executing --- e.g., the
1480 executing property hasn't been updated yet, but the
1481 thread has been stopped with a stop_callback /
1482 stop_wait_callback sequence (see linux_nat_detach for
1483 example) --- we can only have pending events in the local
1484 queue. */
1485 if (queued_waitpid (GET_LWP (lp->ptid), status, __WALL) != -1)
1486 {
8b8655b3
TJB
1487 if (WIFSTOPPED (*status))
1488 signo = target_signal_from_host (WSTOPSIG (*status));
4c28f408
PA
1489
1490 /* If not stopped, then the lwp is gone, no use in
1491 resending a signal. */
1492 }
1493 }
1494 else
1495 {
1496 /* If the core knows the thread is not executing, then we
1497 have the last signal recorded in
2020b7ab 1498 thread_info->stop_signal. */
4c28f408 1499
2020b7ab
PA
1500 struct thread_info *tp = find_thread_pid (lp->ptid);
1501 signo = tp->stop_signal;
4c28f408
PA
1502 }
1503
1504 if (signo != TARGET_SIGNAL_0
1505 && !signal_pass_state (signo))
1506 {
1507 if (debug_linux_nat)
1508 fprintf_unfiltered (gdb_stdlog, "\
1509GPT: lwp %s had signal %s, but it is in no pass state\n",
1510 target_pid_to_str (lp->ptid),
1511 target_signal_to_string (signo));
1512 }
1513 else
1514 {
1515 if (signo != TARGET_SIGNAL_0)
1516 *status = W_STOPCODE (target_signal_to_host (signo));
1517
1518 if (debug_linux_nat)
1519 fprintf_unfiltered (gdb_stdlog,
1520 "GPT: lwp %s as pending signal %s\n",
1521 target_pid_to_str (lp->ptid),
1522 target_signal_to_string (signo));
1523 }
a0ef4274 1524 }
a0ef4274 1525 else
4c28f408
PA
1526 {
1527 if (GET_LWP (lp->ptid) == GET_LWP (last_ptid))
1528 {
2020b7ab
PA
1529 struct thread_info *tp = find_thread_pid (lp->ptid);
1530 if (tp->stop_signal != TARGET_SIGNAL_0
1531 && signal_pass_state (tp->stop_signal))
1532 *status = W_STOPCODE (target_signal_to_host (tp->stop_signal));
4c28f408
PA
1533 }
1534 else if (target_can_async_p ())
1535 queued_waitpid (GET_LWP (lp->ptid), status, __WALL);
1536 else
1537 *status = lp->status;
1538 }
a0ef4274
DJ
1539
1540 return 0;
1541}
1542
d6b0e80f
AC
1543static int
1544detach_callback (struct lwp_info *lp, void *data)
1545{
1546 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1547
1548 if (debug_linux_nat && lp->status)
1549 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1550 strsignal (WSTOPSIG (lp->status)),
1551 target_pid_to_str (lp->ptid));
1552
a0ef4274
DJ
1553 /* If there is a pending SIGSTOP, get rid of it. */
1554 if (lp->signalled)
d6b0e80f 1555 {
d6b0e80f
AC
1556 if (debug_linux_nat)
1557 fprintf_unfiltered (gdb_stdlog,
a0ef4274
DJ
1558 "DC: Sending SIGCONT to %s\n",
1559 target_pid_to_str (lp->ptid));
d6b0e80f 1560
a0ef4274 1561 kill_lwp (GET_LWP (lp->ptid), SIGCONT);
d6b0e80f 1562 lp->signalled = 0;
d6b0e80f
AC
1563 }
1564
1565 /* We don't actually detach from the LWP that has an id equal to the
1566 overall process id just yet. */
1567 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1568 {
a0ef4274
DJ
1569 int status = 0;
1570
1571 /* Pass on any pending signal for this LWP. */
1572 get_pending_status (lp, &status);
1573
d6b0e80f
AC
1574 errno = 0;
1575 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
a0ef4274 1576 WSTOPSIG (status)) < 0)
8a3fe4f8 1577 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
d6b0e80f
AC
1578 safe_strerror (errno));
1579
1580 if (debug_linux_nat)
1581 fprintf_unfiltered (gdb_stdlog,
1582 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1583 target_pid_to_str (lp->ptid),
1584 strsignal (WSTOPSIG (lp->status)));
1585
1586 delete_lwp (lp->ptid);
1587 }
1588
1589 return 0;
1590}
1591
1592static void
136d6dae 1593linux_nat_detach (struct target_ops *ops, char *args, int from_tty)
d6b0e80f 1594{
b84876c2 1595 int pid;
a0ef4274
DJ
1596 int status;
1597 enum target_signal sig;
1598
b84876c2
PA
1599 if (target_can_async_p ())
1600 linux_nat_async (NULL, 0);
1601
4c28f408
PA
1602 /* Stop all threads before detaching. ptrace requires that the
1603 thread is stopped to sucessfully detach. */
1604 iterate_over_lwps (stop_callback, NULL);
1605 /* ... and wait until all of them have reported back that
1606 they're no longer running. */
1607 iterate_over_lwps (stop_wait_callback, NULL);
1608
d6b0e80f
AC
1609 iterate_over_lwps (detach_callback, NULL);
1610
1611 /* Only the initial process should be left right now. */
1612 gdb_assert (num_lwps == 1);
1613
a0ef4274
DJ
1614 /* Pass on any pending signal for the last LWP. */
1615 if ((args == NULL || *args == '\0')
1616 && get_pending_status (lwp_list, &status) != -1
1617 && WIFSTOPPED (status))
1618 {
1619 /* Put the signal number in ARGS so that inf_ptrace_detach will
1620 pass it along with PTRACE_DETACH. */
1621 args = alloca (8);
1622 sprintf (args, "%d", (int) WSTOPSIG (status));
1623 fprintf_unfiltered (gdb_stdlog,
1624 "LND: Sending signal %s to %s\n",
1625 args,
1626 target_pid_to_str (lwp_list->ptid));
1627 }
1628
d6b0e80f
AC
1629 /* Destroy LWP info; it's no longer valid. */
1630 init_lwp_list ();
1631
7a7d3353 1632 pid = ptid_get_pid (inferior_ptid);
b84876c2
PA
1633
1634 if (target_can_async_p ())
1635 drain_queued_events (pid);
7a7d3353
PA
1636
1637 if (forks_exist_p ())
1638 {
1639 /* Multi-fork case. The current inferior_ptid is being detached
1640 from, but there are other viable forks to debug. Detach from
1641 the current fork, and context-switch to the first
1642 available. */
1643 linux_fork_detach (args, from_tty);
1644
1645 if (non_stop && target_can_async_p ())
1646 target_async (inferior_event_handler, 0);
1647 }
1648 else
1649 linux_ops->to_detach (ops, args, from_tty);
d6b0e80f
AC
1650}
1651
1652/* Resume LP. */
1653
1654static int
1655resume_callback (struct lwp_info *lp, void *data)
1656{
1657 if (lp->stopped && lp->status == 0)
1658 {
10d6c8cd
DJ
1659 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
1660 0, TARGET_SIGNAL_0);
d6b0e80f
AC
1661 if (debug_linux_nat)
1662 fprintf_unfiltered (gdb_stdlog,
1663 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1664 target_pid_to_str (lp->ptid));
1665 lp->stopped = 0;
1666 lp->step = 0;
9f0bdab8 1667 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
d6b0e80f 1668 }
57380f4e
DJ
1669 else if (lp->stopped && debug_linux_nat)
1670 fprintf_unfiltered (gdb_stdlog, "RC: Not resuming sibling %s (has pending)\n",
1671 target_pid_to_str (lp->ptid));
1672 else if (debug_linux_nat)
1673 fprintf_unfiltered (gdb_stdlog, "RC: Not resuming sibling %s (not stopped)\n",
1674 target_pid_to_str (lp->ptid));
d6b0e80f
AC
1675
1676 return 0;
1677}
1678
1679static int
1680resume_clear_callback (struct lwp_info *lp, void *data)
1681{
1682 lp->resumed = 0;
1683 return 0;
1684}
1685
1686static int
1687resume_set_callback (struct lwp_info *lp, void *data)
1688{
1689 lp->resumed = 1;
1690 return 0;
1691}
1692
1693static void
1694linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
1695{
1696 struct lwp_info *lp;
1697 int resume_all;
1698
76f50ad1
DJ
1699 if (debug_linux_nat)
1700 fprintf_unfiltered (gdb_stdlog,
1701 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1702 step ? "step" : "resume",
1703 target_pid_to_str (ptid),
1704 signo ? strsignal (signo) : "0",
1705 target_pid_to_str (inferior_ptid));
1706
b84876c2
PA
1707 if (target_can_async_p ())
1708 /* Block events while we're here. */
84e46146 1709 linux_nat_async_events (sigchld_sync);
b84876c2 1710
d6b0e80f
AC
1711 /* A specific PTID means `step only this process id'. */
1712 resume_all = (PIDGET (ptid) == -1);
1713
4c28f408
PA
1714 if (non_stop && resume_all)
1715 internal_error (__FILE__, __LINE__,
1716 "can't resume all in non-stop mode");
1717
1718 if (!non_stop)
1719 {
1720 if (resume_all)
1721 iterate_over_lwps (resume_set_callback, NULL);
1722 else
1723 iterate_over_lwps (resume_clear_callback, NULL);
1724 }
d6b0e80f
AC
1725
1726 /* If PID is -1, it's the current inferior that should be
1727 handled specially. */
1728 if (PIDGET (ptid) == -1)
1729 ptid = inferior_ptid;
1730
1731 lp = find_lwp_pid (ptid);
9f0bdab8 1732 gdb_assert (lp != NULL);
d6b0e80f 1733
4c28f408 1734 /* Convert to something the lower layer understands. */
9f0bdab8 1735 ptid = pid_to_ptid (GET_LWP (lp->ptid));
d6b0e80f 1736
9f0bdab8
DJ
1737 /* Remember if we're stepping. */
1738 lp->step = step;
d6b0e80f 1739
9f0bdab8
DJ
1740 /* Mark this LWP as resumed. */
1741 lp->resumed = 1;
76f50ad1 1742
9f0bdab8
DJ
1743 /* If we have a pending wait status for this thread, there is no
1744 point in resuming the process. But first make sure that
1745 linux_nat_wait won't preemptively handle the event - we
1746 should never take this short-circuit if we are going to
1747 leave LP running, since we have skipped resuming all the
1748 other threads. This bit of code needs to be synchronized
1749 with linux_nat_wait. */
76f50ad1 1750
710151dd
PA
1751 /* In async mode, we never have pending wait status. */
1752 if (target_can_async_p () && lp->status)
1753 internal_error (__FILE__, __LINE__, "Pending status in async mode");
1754
9f0bdab8
DJ
1755 if (lp->status && WIFSTOPPED (lp->status))
1756 {
d6b48e9c
PA
1757 int saved_signo;
1758 struct inferior *inf;
76f50ad1 1759
d6b48e9c
PA
1760 inf = find_inferior_pid (ptid_get_pid (ptid));
1761 gdb_assert (inf);
1762 saved_signo = target_signal_from_host (WSTOPSIG (lp->status));
1763
1764 /* Defer to common code if we're gaining control of the
1765 inferior. */
1766 if (inf->stop_soon == NO_STOP_QUIETLY
1767 && signal_stop_state (saved_signo) == 0
9f0bdab8
DJ
1768 && signal_print_state (saved_signo) == 0
1769 && signal_pass_state (saved_signo) == 1)
d6b0e80f 1770 {
9f0bdab8
DJ
1771 if (debug_linux_nat)
1772 fprintf_unfiltered (gdb_stdlog,
1773 "LLR: Not short circuiting for ignored "
1774 "status 0x%x\n", lp->status);
1775
d6b0e80f
AC
1776 /* FIXME: What should we do if we are supposed to continue
1777 this thread with a signal? */
1778 gdb_assert (signo == TARGET_SIGNAL_0);
9f0bdab8
DJ
1779 signo = saved_signo;
1780 lp->status = 0;
1781 }
1782 }
76f50ad1 1783
9f0bdab8
DJ
1784 if (lp->status)
1785 {
1786 /* FIXME: What should we do if we are supposed to continue
1787 this thread with a signal? */
1788 gdb_assert (signo == TARGET_SIGNAL_0);
76f50ad1 1789
9f0bdab8
DJ
1790 if (debug_linux_nat)
1791 fprintf_unfiltered (gdb_stdlog,
1792 "LLR: Short circuiting for status 0x%x\n",
1793 lp->status);
d6b0e80f 1794
9f0bdab8 1795 return;
d6b0e80f
AC
1796 }
1797
9f0bdab8
DJ
1798 /* Mark LWP as not stopped to prevent it from being continued by
1799 resume_callback. */
1800 lp->stopped = 0;
1801
d6b0e80f
AC
1802 if (resume_all)
1803 iterate_over_lwps (resume_callback, NULL);
1804
10d6c8cd 1805 linux_ops->to_resume (ptid, step, signo);
9f0bdab8
DJ
1806 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
1807
d6b0e80f
AC
1808 if (debug_linux_nat)
1809 fprintf_unfiltered (gdb_stdlog,
1810 "LLR: %s %s, %s (resume event thread)\n",
1811 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1812 target_pid_to_str (ptid),
1813 signo ? strsignal (signo) : "0");
b84876c2
PA
1814
1815 if (target_can_async_p ())
8ea051c5 1816 target_async (inferior_event_handler, 0);
d6b0e80f
AC
1817}
1818
1819/* Issue kill to specified lwp. */
1820
1821static int tkill_failed;
1822
1823static int
1824kill_lwp (int lwpid, int signo)
1825{
1826 errno = 0;
1827
1828/* Use tkill, if possible, in case we are using nptl threads. If tkill
1829 fails, then we are not using nptl threads and we should be using kill. */
1830
1831#ifdef HAVE_TKILL_SYSCALL
1832 if (!tkill_failed)
1833 {
1834 int ret = syscall (__NR_tkill, lwpid, signo);
1835 if (errno != ENOSYS)
1836 return ret;
1837 errno = 0;
1838 tkill_failed = 1;
1839 }
1840#endif
1841
1842 return kill (lwpid, signo);
1843}
1844
3d799a95
DJ
1845/* Handle a GNU/Linux extended wait response. If we see a clone
1846 event, we need to add the new LWP to our list (and not report the
1847 trap to higher layers). This function returns non-zero if the
1848 event should be ignored and we should wait again. If STOPPING is
1849 true, the new LWP remains stopped, otherwise it is continued. */
d6b0e80f
AC
1850
1851static int
3d799a95
DJ
1852linux_handle_extended_wait (struct lwp_info *lp, int status,
1853 int stopping)
d6b0e80f 1854{
3d799a95
DJ
1855 int pid = GET_LWP (lp->ptid);
1856 struct target_waitstatus *ourstatus = &lp->waitstatus;
1857 struct lwp_info *new_lp = NULL;
1858 int event = status >> 16;
d6b0e80f 1859
3d799a95
DJ
1860 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
1861 || event == PTRACE_EVENT_CLONE)
d6b0e80f 1862 {
3d799a95
DJ
1863 unsigned long new_pid;
1864 int ret;
1865
1866 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
6fc19103 1867
3d799a95
DJ
1868 /* If we haven't already seen the new PID stop, wait for it now. */
1869 if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
1870 {
1871 /* The new child has a pending SIGSTOP. We can't affect it until it
1872 hits the SIGSTOP, but we're already attached. */
1873 ret = my_waitpid (new_pid, &status,
1874 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
1875 if (ret == -1)
1876 perror_with_name (_("waiting for new child"));
1877 else if (ret != new_pid)
1878 internal_error (__FILE__, __LINE__,
1879 _("wait returned unexpected PID %d"), ret);
1880 else if (!WIFSTOPPED (status))
1881 internal_error (__FILE__, __LINE__,
1882 _("wait returned unexpected status 0x%x"), status);
1883 }
1884
3a3e9ee3 1885 ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
3d799a95
DJ
1886
1887 if (event == PTRACE_EVENT_FORK)
1888 ourstatus->kind = TARGET_WAITKIND_FORKED;
1889 else if (event == PTRACE_EVENT_VFORK)
1890 ourstatus->kind = TARGET_WAITKIND_VFORKED;
6fc19103 1891 else
3d799a95 1892 {
4c28f408
PA
1893 struct cleanup *old_chain;
1894
3d799a95
DJ
1895 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1896 new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (inferior_ptid)));
1897 new_lp->cloned = 1;
4c28f408 1898 new_lp->stopped = 1;
d6b0e80f 1899
3d799a95
DJ
1900 if (WSTOPSIG (status) != SIGSTOP)
1901 {
1902 /* This can happen if someone starts sending signals to
1903 the new thread before it gets a chance to run, which
1904 have a lower number than SIGSTOP (e.g. SIGUSR1).
1905 This is an unlikely case, and harder to handle for
1906 fork / vfork than for clone, so we do not try - but
1907 we handle it for clone events here. We'll send
1908 the other signal on to the thread below. */
1909
1910 new_lp->signalled = 1;
1911 }
1912 else
1913 status = 0;
d6b0e80f 1914
4c28f408 1915 if (non_stop)
3d799a95 1916 {
4c28f408
PA
1917 /* Add the new thread to GDB's lists as soon as possible
1918 so that:
1919
1920 1) the frontend doesn't have to wait for a stop to
1921 display them, and,
1922
1923 2) we tag it with the correct running state. */
1924
1925 /* If the thread_db layer is active, let it know about
1926 this new thread, and add it to GDB's list. */
1927 if (!thread_db_attach_lwp (new_lp->ptid))
1928 {
1929 /* We're not using thread_db. Add it to GDB's
1930 list. */
1931 target_post_attach (GET_LWP (new_lp->ptid));
1932 add_thread (new_lp->ptid);
1933 }
1934
1935 if (!stopping)
1936 {
1937 set_running (new_lp->ptid, 1);
1938 set_executing (new_lp->ptid, 1);
1939 }
1940 }
1941
1942 if (!stopping)
1943 {
1944 new_lp->stopped = 0;
3d799a95 1945 new_lp->resumed = 1;
4c28f408 1946 ptrace (PTRACE_CONT, new_pid, 0,
3d799a95
DJ
1947 status ? WSTOPSIG (status) : 0);
1948 }
d6b0e80f 1949
3d799a95
DJ
1950 if (debug_linux_nat)
1951 fprintf_unfiltered (gdb_stdlog,
1952 "LHEW: Got clone event from LWP %ld, resuming\n",
1953 GET_LWP (lp->ptid));
1954 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1955
1956 return 1;
1957 }
1958
1959 return 0;
d6b0e80f
AC
1960 }
1961
3d799a95
DJ
1962 if (event == PTRACE_EVENT_EXEC)
1963 {
1964 ourstatus->kind = TARGET_WAITKIND_EXECD;
1965 ourstatus->value.execd_pathname
6d8fd2b7 1966 = xstrdup (linux_child_pid_to_exec_file (pid));
3d799a95
DJ
1967
1968 if (linux_parent_pid)
1969 {
1970 detach_breakpoints (linux_parent_pid);
1971 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
1972
1973 linux_parent_pid = 0;
1974 }
1975
25b22b0a
PA
1976 /* At this point, all inserted breakpoints are gone. Doing this
1977 as soon as we detect an exec prevents the badness of deleting
1978 a breakpoint writing the current "shadow contents" to lift
1979 the bp. That shadow is NOT valid after an exec.
1980
1981 Note that we have to do this after the detach_breakpoints
1982 call above, otherwise breakpoints wouldn't be lifted from the
1983 parent on a vfork, because detach_breakpoints would think
1984 that breakpoints are not inserted. */
1985 mark_breakpoints_out ();
3d799a95
DJ
1986 return 0;
1987 }
1988
1989 internal_error (__FILE__, __LINE__,
1990 _("unknown ptrace event %d"), event);
d6b0e80f
AC
1991}
1992
1993/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1994 exited. */
1995
1996static int
1997wait_lwp (struct lwp_info *lp)
1998{
1999 pid_t pid;
2000 int status;
2001 int thread_dead = 0;
2002
2003 gdb_assert (!lp->stopped);
2004 gdb_assert (lp->status == 0);
2005
58aecb61 2006 pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
d6b0e80f
AC
2007 if (pid == -1 && errno == ECHILD)
2008 {
58aecb61 2009 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
d6b0e80f
AC
2010 if (pid == -1 && errno == ECHILD)
2011 {
2012 /* The thread has previously exited. We need to delete it
2013 now because, for some vendor 2.4 kernels with NPTL
2014 support backported, there won't be an exit event unless
2015 it is the main thread. 2.6 kernels will report an exit
2016 event for each thread that exits, as expected. */
2017 thread_dead = 1;
2018 if (debug_linux_nat)
2019 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2020 target_pid_to_str (lp->ptid));
2021 }
2022 }
2023
2024 if (!thread_dead)
2025 {
2026 gdb_assert (pid == GET_LWP (lp->ptid));
2027
2028 if (debug_linux_nat)
2029 {
2030 fprintf_unfiltered (gdb_stdlog,
2031 "WL: waitpid %s received %s\n",
2032 target_pid_to_str (lp->ptid),
2033 status_to_str (status));
2034 }
2035 }
2036
2037 /* Check if the thread has exited. */
2038 if (WIFEXITED (status) || WIFSIGNALED (status))
2039 {
2040 thread_dead = 1;
2041 if (debug_linux_nat)
2042 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2043 target_pid_to_str (lp->ptid));
2044 }
2045
2046 if (thread_dead)
2047 {
e26af52f 2048 exit_lwp (lp);
d6b0e80f
AC
2049 return 0;
2050 }
2051
2052 gdb_assert (WIFSTOPPED (status));
2053
2054 /* Handle GNU/Linux's extended waitstatus for trace events. */
2055 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2056 {
2057 if (debug_linux_nat)
2058 fprintf_unfiltered (gdb_stdlog,
2059 "WL: Handling extended status 0x%06x\n",
2060 status);
3d799a95 2061 if (linux_handle_extended_wait (lp, status, 1))
d6b0e80f
AC
2062 return wait_lwp (lp);
2063 }
2064
2065 return status;
2066}
2067
9f0bdab8
DJ
2068/* Save the most recent siginfo for LP. This is currently only called
2069 for SIGTRAP; some ports use the si_addr field for
2070 target_stopped_data_address. In the future, it may also be used to
2071 restore the siginfo of requeued signals. */
2072
2073static void
2074save_siginfo (struct lwp_info *lp)
2075{
2076 errno = 0;
2077 ptrace (PTRACE_GETSIGINFO, GET_LWP (lp->ptid),
2078 (PTRACE_TYPE_ARG3) 0, &lp->siginfo);
2079
2080 if (errno != 0)
2081 memset (&lp->siginfo, 0, sizeof (lp->siginfo));
2082}
2083
d6b0e80f
AC
2084/* Send a SIGSTOP to LP. */
2085
2086static int
2087stop_callback (struct lwp_info *lp, void *data)
2088{
2089 if (!lp->stopped && !lp->signalled)
2090 {
2091 int ret;
2092
2093 if (debug_linux_nat)
2094 {
2095 fprintf_unfiltered (gdb_stdlog,
2096 "SC: kill %s **<SIGSTOP>**\n",
2097 target_pid_to_str (lp->ptid));
2098 }
2099 errno = 0;
2100 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
2101 if (debug_linux_nat)
2102 {
2103 fprintf_unfiltered (gdb_stdlog,
2104 "SC: lwp kill %d %s\n",
2105 ret,
2106 errno ? safe_strerror (errno) : "ERRNO-OK");
2107 }
2108
2109 lp->signalled = 1;
2110 gdb_assert (lp->status == 0);
2111 }
2112
2113 return 0;
2114}
2115
57380f4e 2116/* Return non-zero if LWP PID has a pending SIGINT. */
d6b0e80f
AC
2117
2118static int
57380f4e
DJ
2119linux_nat_has_pending_sigint (int pid)
2120{
2121 sigset_t pending, blocked, ignored;
2122 int i;
2123
2124 linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2125
2126 if (sigismember (&pending, SIGINT)
2127 && !sigismember (&ignored, SIGINT))
2128 return 1;
2129
2130 return 0;
2131}
2132
2133/* Set a flag in LP indicating that we should ignore its next SIGINT. */
2134
2135static int
2136set_ignore_sigint (struct lwp_info *lp, void *data)
d6b0e80f 2137{
57380f4e
DJ
2138 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2139 flag to consume the next one. */
2140 if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2141 && WSTOPSIG (lp->status) == SIGINT)
2142 lp->status = 0;
2143 else
2144 lp->ignore_sigint = 1;
2145
2146 return 0;
2147}
2148
2149/* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2150 This function is called after we know the LWP has stopped; if the LWP
2151 stopped before the expected SIGINT was delivered, then it will never have
2152 arrived. Also, if the signal was delivered to a shared queue and consumed
2153 by a different thread, it will never be delivered to this LWP. */
d6b0e80f 2154
57380f4e
DJ
2155static void
2156maybe_clear_ignore_sigint (struct lwp_info *lp)
2157{
2158 if (!lp->ignore_sigint)
2159 return;
2160
2161 if (!linux_nat_has_pending_sigint (GET_LWP (lp->ptid)))
2162 {
2163 if (debug_linux_nat)
2164 fprintf_unfiltered (gdb_stdlog,
2165 "MCIS: Clearing bogus flag for %s\n",
2166 target_pid_to_str (lp->ptid));
2167 lp->ignore_sigint = 0;
2168 }
2169}
2170
2171/* Wait until LP is stopped. */
2172
2173static int
2174stop_wait_callback (struct lwp_info *lp, void *data)
2175{
d6b0e80f
AC
2176 if (!lp->stopped)
2177 {
2178 int status;
2179
2180 status = wait_lwp (lp);
2181 if (status == 0)
2182 return 0;
2183
57380f4e
DJ
2184 if (lp->ignore_sigint && WIFSTOPPED (status)
2185 && WSTOPSIG (status) == SIGINT)
d6b0e80f 2186 {
57380f4e 2187 lp->ignore_sigint = 0;
d6b0e80f
AC
2188
2189 errno = 0;
2190 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2191 if (debug_linux_nat)
2192 fprintf_unfiltered (gdb_stdlog,
57380f4e 2193 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
d6b0e80f
AC
2194 target_pid_to_str (lp->ptid),
2195 errno ? safe_strerror (errno) : "OK");
2196
57380f4e 2197 return stop_wait_callback (lp, NULL);
d6b0e80f
AC
2198 }
2199
57380f4e
DJ
2200 maybe_clear_ignore_sigint (lp);
2201
d6b0e80f
AC
2202 if (WSTOPSIG (status) != SIGSTOP)
2203 {
2204 if (WSTOPSIG (status) == SIGTRAP)
2205 {
2206 /* If a LWP other than the LWP that we're reporting an
2207 event for has hit a GDB breakpoint (as opposed to
2208 some random trap signal), then just arrange for it to
2209 hit it again later. We don't keep the SIGTRAP status
2210 and don't forward the SIGTRAP signal to the LWP. We
2211 will handle the current event, eventually we will
2212 resume all LWPs, and this one will get its breakpoint
2213 trap again.
2214
2215 If we do not do this, then we run the risk that the
2216 user will delete or disable the breakpoint, but the
2217 thread will have already tripped on it. */
2218
9f0bdab8
DJ
2219 /* Save the trap's siginfo in case we need it later. */
2220 save_siginfo (lp);
2221
d6b0e80f
AC
2222 /* Now resume this LWP and get the SIGSTOP event. */
2223 errno = 0;
2224 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2225 if (debug_linux_nat)
2226 {
2227 fprintf_unfiltered (gdb_stdlog,
2228 "PTRACE_CONT %s, 0, 0 (%s)\n",
2229 target_pid_to_str (lp->ptid),
2230 errno ? safe_strerror (errno) : "OK");
2231
2232 fprintf_unfiltered (gdb_stdlog,
2233 "SWC: Candidate SIGTRAP event in %s\n",
2234 target_pid_to_str (lp->ptid));
2235 }
710151dd
PA
2236 /* Hold this event/waitstatus while we check to see if
2237 there are any more (we still want to get that SIGSTOP). */
57380f4e 2238 stop_wait_callback (lp, NULL);
710151dd
PA
2239
2240 if (target_can_async_p ())
d6b0e80f 2241 {
710151dd
PA
2242 /* Don't leave a pending wait status in async mode.
2243 Retrigger the breakpoint. */
2244 if (!cancel_breakpoint (lp))
d6b0e80f 2245 {
710151dd
PA
2246 /* There was no gdb breakpoint set at pc. Put
2247 the event back in the queue. */
2248 if (debug_linux_nat)
252fbfc8
PA
2249 fprintf_unfiltered (gdb_stdlog, "\
2250SWC: leaving SIGTRAP in local queue of %s\n", target_pid_to_str (lp->ptid));
2251 push_waitpid (GET_LWP (lp->ptid),
2252 W_STOPCODE (SIGTRAP),
2253 lp->cloned ? __WCLONE : 0);
710151dd
PA
2254 }
2255 }
2256 else
2257 {
2258 /* Hold the SIGTRAP for handling by
2259 linux_nat_wait. */
2260 /* If there's another event, throw it back into the
2261 queue. */
2262 if (lp->status)
2263 {
2264 if (debug_linux_nat)
2265 fprintf_unfiltered (gdb_stdlog,
2266 "SWC: kill %s, %s\n",
2267 target_pid_to_str (lp->ptid),
2268 status_to_str ((int) status));
2269 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
d6b0e80f 2270 }
710151dd
PA
2271 /* Save the sigtrap event. */
2272 lp->status = status;
d6b0e80f 2273 }
d6b0e80f
AC
2274 return 0;
2275 }
2276 else
2277 {
2278 /* The thread was stopped with a signal other than
2279 SIGSTOP, and didn't accidentally trip a breakpoint. */
2280
2281 if (debug_linux_nat)
2282 {
2283 fprintf_unfiltered (gdb_stdlog,
2284 "SWC: Pending event %s in %s\n",
2285 status_to_str ((int) status),
2286 target_pid_to_str (lp->ptid));
2287 }
2288 /* Now resume this LWP and get the SIGSTOP event. */
2289 errno = 0;
2290 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
2291 if (debug_linux_nat)
2292 fprintf_unfiltered (gdb_stdlog,
2293 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2294 target_pid_to_str (lp->ptid),
2295 errno ? safe_strerror (errno) : "OK");
2296
2297 /* Hold this event/waitstatus while we check to see if
2298 there are any more (we still want to get that SIGSTOP). */
57380f4e 2299 stop_wait_callback (lp, NULL);
710151dd
PA
2300
2301 /* If the lp->status field is still empty, use it to
2302 hold this event. If not, then this event must be
2303 returned to the event queue of the LWP. */
2304 if (lp->status || target_can_async_p ())
d6b0e80f
AC
2305 {
2306 if (debug_linux_nat)
2307 {
2308 fprintf_unfiltered (gdb_stdlog,
2309 "SWC: kill %s, %s\n",
2310 target_pid_to_str (lp->ptid),
2311 status_to_str ((int) status));
2312 }
2313 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
2314 }
710151dd
PA
2315 else
2316 lp->status = status;
d6b0e80f
AC
2317 return 0;
2318 }
2319 }
2320 else
2321 {
2322 /* We caught the SIGSTOP that we intended to catch, so
2323 there's no SIGSTOP pending. */
2324 lp->stopped = 1;
2325 lp->signalled = 0;
2326 }
2327 }
2328
2329 return 0;
2330}
2331
d6b0e80f
AC
2332/* Return non-zero if LP has a wait status pending. */
2333
2334static int
2335status_callback (struct lwp_info *lp, void *data)
2336{
2337 /* Only report a pending wait status if we pretend that this has
2338 indeed been resumed. */
2339 return (lp->status != 0 && lp->resumed);
2340}
2341
2342/* Return non-zero if LP isn't stopped. */
2343
2344static int
2345running_callback (struct lwp_info *lp, void *data)
2346{
2347 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
2348}
2349
2350/* Count the LWP's that have had events. */
2351
2352static int
2353count_events_callback (struct lwp_info *lp, void *data)
2354{
2355 int *count = data;
2356
2357 gdb_assert (count != NULL);
2358
e09490f1
DJ
2359 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2360 if (lp->status != 0 && lp->resumed
d6b0e80f
AC
2361 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
2362 (*count)++;
2363
2364 return 0;
2365}
2366
2367/* Select the LWP (if any) that is currently being single-stepped. */
2368
2369static int
2370select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2371{
2372 if (lp->step && lp->status != 0)
2373 return 1;
2374 else
2375 return 0;
2376}
2377
2378/* Select the Nth LWP that has had a SIGTRAP event. */
2379
2380static int
2381select_event_lwp_callback (struct lwp_info *lp, void *data)
2382{
2383 int *selector = data;
2384
2385 gdb_assert (selector != NULL);
2386
e09490f1
DJ
2387 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2388 if (lp->status != 0 && lp->resumed
d6b0e80f
AC
2389 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
2390 if ((*selector)-- == 0)
2391 return 1;
2392
2393 return 0;
2394}
2395
710151dd
PA
2396static int
2397cancel_breakpoint (struct lwp_info *lp)
2398{
2399 /* Arrange for a breakpoint to be hit again later. We don't keep
2400 the SIGTRAP status and don't forward the SIGTRAP signal to the
2401 LWP. We will handle the current event, eventually we will resume
2402 this LWP, and this breakpoint will trap again.
2403
2404 If we do not do this, then we run the risk that the user will
2405 delete or disable the breakpoint, but the LWP will have already
2406 tripped on it. */
2407
515630c5
UW
2408 struct regcache *regcache = get_thread_regcache (lp->ptid);
2409 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2410 CORE_ADDR pc;
2411
2412 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
2413 if (breakpoint_inserted_here_p (pc))
710151dd
PA
2414 {
2415 if (debug_linux_nat)
2416 fprintf_unfiltered (gdb_stdlog,
2417 "CB: Push back breakpoint for %s\n",
2418 target_pid_to_str (lp->ptid));
2419
2420 /* Back up the PC if necessary. */
515630c5
UW
2421 if (gdbarch_decr_pc_after_break (gdbarch))
2422 regcache_write_pc (regcache, pc);
2423
710151dd
PA
2424 return 1;
2425 }
2426 return 0;
2427}
2428
d6b0e80f
AC
2429static int
2430cancel_breakpoints_callback (struct lwp_info *lp, void *data)
2431{
2432 struct lwp_info *event_lp = data;
2433
2434 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2435 if (lp == event_lp)
2436 return 0;
2437
2438 /* If a LWP other than the LWP that we're reporting an event for has
2439 hit a GDB breakpoint (as opposed to some random trap signal),
2440 then just arrange for it to hit it again later. We don't keep
2441 the SIGTRAP status and don't forward the SIGTRAP signal to the
2442 LWP. We will handle the current event, eventually we will resume
2443 all LWPs, and this one will get its breakpoint trap again.
2444
2445 If we do not do this, then we run the risk that the user will
2446 delete or disable the breakpoint, but the LWP will have already
2447 tripped on it. */
2448
2449 if (lp->status != 0
2450 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
710151dd
PA
2451 && cancel_breakpoint (lp))
2452 /* Throw away the SIGTRAP. */
2453 lp->status = 0;
d6b0e80f
AC
2454
2455 return 0;
2456}
2457
2458/* Select one LWP out of those that have events pending. */
2459
2460static void
2461select_event_lwp (struct lwp_info **orig_lp, int *status)
2462{
2463 int num_events = 0;
2464 int random_selector;
2465 struct lwp_info *event_lp;
2466
ac264b3b 2467 /* Record the wait status for the original LWP. */
d6b0e80f
AC
2468 (*orig_lp)->status = *status;
2469
2470 /* Give preference to any LWP that is being single-stepped. */
2471 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
2472 if (event_lp != NULL)
2473 {
2474 if (debug_linux_nat)
2475 fprintf_unfiltered (gdb_stdlog,
2476 "SEL: Select single-step %s\n",
2477 target_pid_to_str (event_lp->ptid));
2478 }
2479 else
2480 {
2481 /* No single-stepping LWP. Select one at random, out of those
2482 which have had SIGTRAP events. */
2483
2484 /* First see how many SIGTRAP events we have. */
2485 iterate_over_lwps (count_events_callback, &num_events);
2486
2487 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2488 random_selector = (int)
2489 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2490
2491 if (debug_linux_nat && num_events > 1)
2492 fprintf_unfiltered (gdb_stdlog,
2493 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2494 num_events, random_selector);
2495
2496 event_lp = iterate_over_lwps (select_event_lwp_callback,
2497 &random_selector);
2498 }
2499
2500 if (event_lp != NULL)
2501 {
2502 /* Switch the event LWP. */
2503 *orig_lp = event_lp;
2504 *status = event_lp->status;
2505 }
2506
2507 /* Flush the wait status for the event LWP. */
2508 (*orig_lp)->status = 0;
2509}
2510
2511/* Return non-zero if LP has been resumed. */
2512
2513static int
2514resumed_callback (struct lwp_info *lp, void *data)
2515{
2516 return lp->resumed;
2517}
2518
d6b0e80f
AC
2519/* Stop an active thread, verify it still exists, then resume it. */
2520
2521static int
2522stop_and_resume_callback (struct lwp_info *lp, void *data)
2523{
2524 struct lwp_info *ptr;
2525
2526 if (!lp->stopped && !lp->signalled)
2527 {
2528 stop_callback (lp, NULL);
2529 stop_wait_callback (lp, NULL);
2530 /* Resume if the lwp still exists. */
2531 for (ptr = lwp_list; ptr; ptr = ptr->next)
2532 if (lp == ptr)
2533 {
2534 resume_callback (lp, NULL);
2535 resume_set_callback (lp, NULL);
2536 }
2537 }
2538 return 0;
2539}
2540
02f3fc28 2541/* Check if we should go on and pass this event to common code.
fa2c6a57 2542 Return the affected lwp if we are, or NULL otherwise. */
02f3fc28
PA
2543static struct lwp_info *
2544linux_nat_filter_event (int lwpid, int status, int options)
2545{
2546 struct lwp_info *lp;
2547
2548 lp = find_lwp_pid (pid_to_ptid (lwpid));
2549
2550 /* Check for stop events reported by a process we didn't already
2551 know about - anything not already in our LWP list.
2552
2553 If we're expecting to receive stopped processes after
2554 fork, vfork, and clone events, then we'll just add the
2555 new one to our list and go back to waiting for the event
2556 to be reported - the stopped process might be returned
2557 from waitpid before or after the event is. */
2558 if (WIFSTOPPED (status) && !lp)
2559 {
2560 linux_record_stopped_pid (lwpid, status);
2561 return NULL;
2562 }
2563
2564 /* Make sure we don't report an event for the exit of an LWP not in
2565 our list, i.e. not part of the current process. This can happen
2566 if we detach from a program we original forked and then it
2567 exits. */
2568 if (!WIFSTOPPED (status) && !lp)
2569 return NULL;
2570
2571 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2572 CLONE_PTRACE processes which do not use the thread library -
2573 otherwise we wouldn't find the new LWP this way. That doesn't
2574 currently work, and the following code is currently unreachable
2575 due to the two blocks above. If it's fixed some day, this code
2576 should be broken out into a function so that we can also pick up
2577 LWPs from the new interface. */
2578 if (!lp)
2579 {
2580 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
2581 if (options & __WCLONE)
2582 lp->cloned = 1;
2583
2584 gdb_assert (WIFSTOPPED (status)
2585 && WSTOPSIG (status) == SIGSTOP);
2586 lp->signalled = 1;
2587
2588 if (!in_thread_list (inferior_ptid))
2589 {
2590 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
2591 GET_PID (inferior_ptid));
2592 add_thread (inferior_ptid);
2593 }
2594
2595 add_thread (lp->ptid);
2596 }
2597
2598 /* Save the trap's siginfo in case we need it later. */
2599 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2600 save_siginfo (lp);
2601
2602 /* Handle GNU/Linux's extended waitstatus for trace events. */
2603 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2604 {
2605 if (debug_linux_nat)
2606 fprintf_unfiltered (gdb_stdlog,
2607 "LLW: Handling extended status 0x%06x\n",
2608 status);
2609 if (linux_handle_extended_wait (lp, status, 0))
2610 return NULL;
2611 }
2612
2613 /* Check if the thread has exited. */
2614 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
2615 {
2616 /* If this is the main thread, we must stop all threads and
2617 verify if they are still alive. This is because in the nptl
2618 thread model, there is no signal issued for exiting LWPs
2619 other than the main thread. We only get the main thread exit
2620 signal once all child threads have already exited. If we
2621 stop all the threads and use the stop_wait_callback to check
2622 if they have exited we can determine whether this signal
2623 should be ignored or whether it means the end of the debugged
2624 application, regardless of which threading model is being
2625 used. */
2626 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
2627 {
2628 lp->stopped = 1;
2629 iterate_over_lwps (stop_and_resume_callback, NULL);
2630 }
2631
2632 if (debug_linux_nat)
2633 fprintf_unfiltered (gdb_stdlog,
2634 "LLW: %s exited.\n",
2635 target_pid_to_str (lp->ptid));
2636
2637 exit_lwp (lp);
2638
2639 /* If there is at least one more LWP, then the exit signal was
2640 not the end of the debugged application and should be
2641 ignored. */
2642 if (num_lwps > 0)
4c28f408 2643 return NULL;
02f3fc28
PA
2644 }
2645
2646 /* Check if the current LWP has previously exited. In the nptl
2647 thread model, LWPs other than the main thread do not issue
2648 signals when they exit so we must check whenever the thread has
2649 stopped. A similar check is made in stop_wait_callback(). */
2650 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
2651 {
2652 if (debug_linux_nat)
2653 fprintf_unfiltered (gdb_stdlog,
2654 "LLW: %s exited.\n",
2655 target_pid_to_str (lp->ptid));
2656
2657 exit_lwp (lp);
2658
2659 /* Make sure there is at least one thread running. */
2660 gdb_assert (iterate_over_lwps (running_callback, NULL));
2661
2662 /* Discard the event. */
2663 return NULL;
2664 }
2665
2666 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2667 an attempt to stop an LWP. */
2668 if (lp->signalled
2669 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2670 {
2671 if (debug_linux_nat)
2672 fprintf_unfiltered (gdb_stdlog,
2673 "LLW: Delayed SIGSTOP caught for %s.\n",
2674 target_pid_to_str (lp->ptid));
2675
2676 /* This is a delayed SIGSTOP. */
2677 lp->signalled = 0;
2678
2679 registers_changed ();
2680
2681 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2682 lp->step, TARGET_SIGNAL_0);
2683 if (debug_linux_nat)
2684 fprintf_unfiltered (gdb_stdlog,
2685 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2686 lp->step ?
2687 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2688 target_pid_to_str (lp->ptid));
2689
2690 lp->stopped = 0;
2691 gdb_assert (lp->resumed);
2692
2693 /* Discard the event. */
2694 return NULL;
2695 }
2696
57380f4e
DJ
2697 /* Make sure we don't report a SIGINT that we have already displayed
2698 for another thread. */
2699 if (lp->ignore_sigint
2700 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
2701 {
2702 if (debug_linux_nat)
2703 fprintf_unfiltered (gdb_stdlog,
2704 "LLW: Delayed SIGINT caught for %s.\n",
2705 target_pid_to_str (lp->ptid));
2706
2707 /* This is a delayed SIGINT. */
2708 lp->ignore_sigint = 0;
2709
2710 registers_changed ();
2711 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2712 lp->step, TARGET_SIGNAL_0);
2713 if (debug_linux_nat)
2714 fprintf_unfiltered (gdb_stdlog,
2715 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2716 lp->step ?
2717 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2718 target_pid_to_str (lp->ptid));
2719
2720 lp->stopped = 0;
2721 gdb_assert (lp->resumed);
2722
2723 /* Discard the event. */
2724 return NULL;
2725 }
2726
02f3fc28
PA
2727 /* An interesting event. */
2728 gdb_assert (lp);
2729 return lp;
2730}
2731
b84876c2
PA
2732/* Get the events stored in the pipe into the local queue, so they are
2733 accessible to queued_waitpid. We need to do this, since it is not
2734 always the case that the event at the head of the pipe is the event
2735 we want. */
2736
2737static void
2738pipe_to_local_event_queue (void)
2739{
2740 if (debug_linux_nat_async)
2741 fprintf_unfiltered (gdb_stdlog,
2742 "PTLEQ: linux_nat_num_queued_events(%d)\n",
2743 linux_nat_num_queued_events);
2744 while (linux_nat_num_queued_events)
2745 {
2746 int lwpid, status, options;
b84876c2 2747 lwpid = linux_nat_event_pipe_pop (&status, &options);
b84876c2
PA
2748 gdb_assert (lwpid > 0);
2749 push_waitpid (lwpid, status, options);
2750 }
2751}
2752
2753/* Get the unprocessed events stored in the local queue back into the
2754 pipe, so the event loop realizes there's something else to
2755 process. */
2756
2757static void
2758local_event_queue_to_pipe (void)
2759{
2760 struct waitpid_result *w = waitpid_queue;
2761 while (w)
2762 {
2763 struct waitpid_result *next = w->next;
2764 linux_nat_event_pipe_push (w->pid,
2765 w->status,
2766 w->options);
2767 xfree (w);
2768 w = next;
2769 }
2770 waitpid_queue = NULL;
2771
2772 if (debug_linux_nat_async)
2773 fprintf_unfiltered (gdb_stdlog,
2774 "LEQTP: linux_nat_num_queued_events(%d)\n",
2775 linux_nat_num_queued_events);
2776}
2777
d6b0e80f 2778static ptid_t
117de6a9
PA
2779linux_nat_wait (struct target_ops *ops,
2780 ptid_t ptid, struct target_waitstatus *ourstatus)
d6b0e80f
AC
2781{
2782 struct lwp_info *lp = NULL;
2783 int options = 0;
2784 int status = 0;
2785 pid_t pid = PIDGET (ptid);
d6b0e80f 2786
b84876c2
PA
2787 if (debug_linux_nat_async)
2788 fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
2789
f973ed9c
DJ
2790 /* The first time we get here after starting a new inferior, we may
2791 not have added it to the LWP list yet - this is the earliest
2792 moment at which we know its PID. */
2793 if (num_lwps == 0)
2794 {
2795 gdb_assert (!is_lwp (inferior_ptid));
2796
27c9d204
PA
2797 /* Upgrade the main thread's ptid. */
2798 thread_change_ptid (inferior_ptid,
2799 BUILD_LWP (GET_PID (inferior_ptid),
2800 GET_PID (inferior_ptid)));
2801
f973ed9c
DJ
2802 lp = add_lwp (inferior_ptid);
2803 lp->resumed = 1;
2804 }
2805
84e46146
PA
2806 /* Block events while we're here. */
2807 linux_nat_async_events (sigchld_sync);
d6b0e80f
AC
2808
2809retry:
2810
f973ed9c
DJ
2811 /* Make sure there is at least one LWP that has been resumed. */
2812 gdb_assert (iterate_over_lwps (resumed_callback, NULL));
d6b0e80f
AC
2813
2814 /* First check if there is a LWP with a wait status pending. */
2815 if (pid == -1)
2816 {
2817 /* Any LWP that's been resumed will do. */
2818 lp = iterate_over_lwps (status_callback, NULL);
2819 if (lp)
2820 {
710151dd
PA
2821 if (target_can_async_p ())
2822 internal_error (__FILE__, __LINE__,
2823 "Found an LWP with a pending status in async mode.");
2824
d6b0e80f
AC
2825 status = lp->status;
2826 lp->status = 0;
2827
2828 if (debug_linux_nat && status)
2829 fprintf_unfiltered (gdb_stdlog,
2830 "LLW: Using pending wait status %s for %s.\n",
2831 status_to_str (status),
2832 target_pid_to_str (lp->ptid));
2833 }
2834
b84876c2 2835 /* But if we don't find one, we'll have to wait, and check both
d6b0e80f
AC
2836 cloned and uncloned processes. We start with the cloned
2837 processes. */
2838 options = __WCLONE | WNOHANG;
2839 }
2840 else if (is_lwp (ptid))
2841 {
2842 if (debug_linux_nat)
2843 fprintf_unfiltered (gdb_stdlog,
2844 "LLW: Waiting for specific LWP %s.\n",
2845 target_pid_to_str (ptid));
2846
2847 /* We have a specific LWP to check. */
2848 lp = find_lwp_pid (ptid);
2849 gdb_assert (lp);
2850 status = lp->status;
2851 lp->status = 0;
2852
2853 if (debug_linux_nat && status)
2854 fprintf_unfiltered (gdb_stdlog,
2855 "LLW: Using pending wait status %s for %s.\n",
2856 status_to_str (status),
2857 target_pid_to_str (lp->ptid));
2858
2859 /* If we have to wait, take into account whether PID is a cloned
2860 process or not. And we have to convert it to something that
2861 the layer beneath us can understand. */
2862 options = lp->cloned ? __WCLONE : 0;
2863 pid = GET_LWP (ptid);
2864 }
2865
2866 if (status && lp->signalled)
2867 {
2868 /* A pending SIGSTOP may interfere with the normal stream of
2869 events. In a typical case where interference is a problem,
2870 we have a SIGSTOP signal pending for LWP A while
2871 single-stepping it, encounter an event in LWP B, and take the
2872 pending SIGSTOP while trying to stop LWP A. After processing
2873 the event in LWP B, LWP A is continued, and we'll never see
2874 the SIGTRAP associated with the last time we were
2875 single-stepping LWP A. */
2876
2877 /* Resume the thread. It should halt immediately returning the
2878 pending SIGSTOP. */
2879 registers_changed ();
10d6c8cd
DJ
2880 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2881 lp->step, TARGET_SIGNAL_0);
d6b0e80f
AC
2882 if (debug_linux_nat)
2883 fprintf_unfiltered (gdb_stdlog,
2884 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2885 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2886 target_pid_to_str (lp->ptid));
2887 lp->stopped = 0;
2888 gdb_assert (lp->resumed);
2889
2890 /* This should catch the pending SIGSTOP. */
2891 stop_wait_callback (lp, NULL);
2892 }
2893
b84876c2
PA
2894 if (!target_can_async_p ())
2895 {
2896 /* Causes SIGINT to be passed on to the attached process. */
2897 set_sigint_trap ();
b84876c2 2898 }
d6b0e80f
AC
2899
2900 while (status == 0)
2901 {
2902 pid_t lwpid;
2903
b84876c2
PA
2904 if (target_can_async_p ())
2905 /* In async mode, don't ever block. Only look at the locally
2906 queued events. */
2907 lwpid = queued_waitpid (pid, &status, options);
2908 else
2909 lwpid = my_waitpid (pid, &status, options);
2910
d6b0e80f
AC
2911 if (lwpid > 0)
2912 {
2913 gdb_assert (pid == -1 || lwpid == pid);
2914
2915 if (debug_linux_nat)
2916 {
2917 fprintf_unfiltered (gdb_stdlog,
2918 "LLW: waitpid %ld received %s\n",
2919 (long) lwpid, status_to_str (status));
2920 }
2921
02f3fc28 2922 lp = linux_nat_filter_event (lwpid, status, options);
d6b0e80f
AC
2923 if (!lp)
2924 {
02f3fc28 2925 /* A discarded event. */
d6b0e80f
AC
2926 status = 0;
2927 continue;
2928 }
2929
2930 break;
2931 }
2932
2933 if (pid == -1)
2934 {
2935 /* Alternate between checking cloned and uncloned processes. */
2936 options ^= __WCLONE;
2937
b84876c2
PA
2938 /* And every time we have checked both:
2939 In async mode, return to event loop;
2940 In sync mode, suspend waiting for a SIGCHLD signal. */
d6b0e80f 2941 if (options & __WCLONE)
b84876c2
PA
2942 {
2943 if (target_can_async_p ())
2944 {
2945 /* No interesting event. */
2946 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2947
2948 /* Get ready for the next event. */
2949 target_async (inferior_event_handler, 0);
2950
2951 if (debug_linux_nat_async)
2952 fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
2953
2954 return minus_one_ptid;
2955 }
2956
2957 sigsuspend (&suspend_mask);
2958 }
d6b0e80f
AC
2959 }
2960
2961 /* We shouldn't end up here unless we want to try again. */
2962 gdb_assert (status == 0);
2963 }
2964
b84876c2 2965 if (!target_can_async_p ())
d26b5354 2966 clear_sigint_trap ();
d6b0e80f
AC
2967
2968 gdb_assert (lp);
2969
2970 /* Don't report signals that GDB isn't interested in, such as
2971 signals that are neither printed nor stopped upon. Stopping all
2972 threads can be a bit time-consuming so if we want decent
2973 performance with heavily multi-threaded programs, especially when
2974 they're using a high frequency timer, we'd better avoid it if we
2975 can. */
2976
2977 if (WIFSTOPPED (status))
2978 {
2979 int signo = target_signal_from_host (WSTOPSIG (status));
d6b48e9c
PA
2980 struct inferior *inf;
2981
2982 inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2983 gdb_assert (inf);
d6b0e80f 2984
d6b48e9c
PA
2985 /* Defer to common code if we get a signal while
2986 single-stepping, since that may need special care, e.g. to
2987 skip the signal handler, or, if we're gaining control of the
2988 inferior. */
d539ed7e 2989 if (!lp->step
d6b48e9c 2990 && inf->stop_soon == NO_STOP_QUIETLY
d539ed7e 2991 && signal_stop_state (signo) == 0
d6b0e80f
AC
2992 && signal_print_state (signo) == 0
2993 && signal_pass_state (signo) == 1)
2994 {
2995 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2996 here? It is not clear we should. GDB may not expect
2997 other threads to run. On the other hand, not resuming
2998 newly attached threads may cause an unwanted delay in
2999 getting them running. */
3000 registers_changed ();
10d6c8cd
DJ
3001 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
3002 lp->step, signo);
d6b0e80f
AC
3003 if (debug_linux_nat)
3004 fprintf_unfiltered (gdb_stdlog,
3005 "LLW: %s %s, %s (preempt 'handle')\n",
3006 lp->step ?
3007 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3008 target_pid_to_str (lp->ptid),
3009 signo ? strsignal (signo) : "0");
3010 lp->stopped = 0;
3011 status = 0;
3012 goto retry;
3013 }
3014
1ad15515 3015 if (!non_stop)
d6b0e80f 3016 {
1ad15515
PA
3017 /* Only do the below in all-stop, as we currently use SIGINT
3018 to implement target_stop (see linux_nat_stop) in
3019 non-stop. */
3020 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
3021 {
3022 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3023 forwarded to the entire process group, that is, all LWPs
3024 will receive it - unless they're using CLONE_THREAD to
3025 share signals. Since we only want to report it once, we
3026 mark it as ignored for all LWPs except this one. */
3027 iterate_over_lwps (set_ignore_sigint, NULL);
3028 lp->ignore_sigint = 0;
3029 }
3030 else
3031 maybe_clear_ignore_sigint (lp);
d6b0e80f
AC
3032 }
3033 }
3034
3035 /* This LWP is stopped now. */
3036 lp->stopped = 1;
3037
3038 if (debug_linux_nat)
3039 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
3040 status_to_str (status), target_pid_to_str (lp->ptid));
3041
4c28f408
PA
3042 if (!non_stop)
3043 {
3044 /* Now stop all other LWP's ... */
3045 iterate_over_lwps (stop_callback, NULL);
3046
3047 /* ... and wait until all of them have reported back that
3048 they're no longer running. */
57380f4e 3049 iterate_over_lwps (stop_wait_callback, NULL);
4c28f408
PA
3050
3051 /* If we're not waiting for a specific LWP, choose an event LWP
3052 from among those that have had events. Giving equal priority
3053 to all LWPs that have had events helps prevent
3054 starvation. */
3055 if (pid == -1)
3056 select_event_lwp (&lp, &status);
3057 }
d6b0e80f
AC
3058
3059 /* Now that we've selected our final event LWP, cancel any
3060 breakpoints in other LWPs that have hit a GDB breakpoint. See
3061 the comment in cancel_breakpoints_callback to find out why. */
3062 iterate_over_lwps (cancel_breakpoints_callback, lp);
3063
d6b0e80f
AC
3064 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
3065 {
d6b0e80f
AC
3066 if (debug_linux_nat)
3067 fprintf_unfiltered (gdb_stdlog,
4fdebdd0
PA
3068 "LLW: trap ptid is %s.\n",
3069 target_pid_to_str (lp->ptid));
d6b0e80f 3070 }
d6b0e80f
AC
3071
3072 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3073 {
3074 *ourstatus = lp->waitstatus;
3075 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3076 }
3077 else
3078 store_waitstatus (ourstatus, status);
3079
b84876c2
PA
3080 /* Get ready for the next event. */
3081 if (target_can_async_p ())
3082 target_async (inferior_event_handler, 0);
3083
3084 if (debug_linux_nat_async)
3085 fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3086
f973ed9c 3087 return lp->ptid;
d6b0e80f
AC
3088}
3089
3090static int
3091kill_callback (struct lwp_info *lp, void *data)
3092{
3093 errno = 0;
3094 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
3095 if (debug_linux_nat)
3096 fprintf_unfiltered (gdb_stdlog,
3097 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3098 target_pid_to_str (lp->ptid),
3099 errno ? safe_strerror (errno) : "OK");
3100
3101 return 0;
3102}
3103
3104static int
3105kill_wait_callback (struct lwp_info *lp, void *data)
3106{
3107 pid_t pid;
3108
3109 /* We must make sure that there are no pending events (delayed
3110 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3111 program doesn't interfere with any following debugging session. */
3112
3113 /* For cloned processes we must check both with __WCLONE and
3114 without, since the exit status of a cloned process isn't reported
3115 with __WCLONE. */
3116 if (lp->cloned)
3117 {
3118 do
3119 {
58aecb61 3120 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
e85a822c 3121 if (pid != (pid_t) -1)
d6b0e80f 3122 {
e85a822c
DJ
3123 if (debug_linux_nat)
3124 fprintf_unfiltered (gdb_stdlog,
3125 "KWC: wait %s received unknown.\n",
3126 target_pid_to_str (lp->ptid));
3127 /* The Linux kernel sometimes fails to kill a thread
3128 completely after PTRACE_KILL; that goes from the stop
3129 point in do_fork out to the one in
3130 get_signal_to_deliever and waits again. So kill it
3131 again. */
3132 kill_callback (lp, NULL);
d6b0e80f
AC
3133 }
3134 }
3135 while (pid == GET_LWP (lp->ptid));
3136
3137 gdb_assert (pid == -1 && errno == ECHILD);
3138 }
3139
3140 do
3141 {
58aecb61 3142 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
e85a822c 3143 if (pid != (pid_t) -1)
d6b0e80f 3144 {
e85a822c
DJ
3145 if (debug_linux_nat)
3146 fprintf_unfiltered (gdb_stdlog,
3147 "KWC: wait %s received unk.\n",
3148 target_pid_to_str (lp->ptid));
3149 /* See the call to kill_callback above. */
3150 kill_callback (lp, NULL);
d6b0e80f
AC
3151 }
3152 }
3153 while (pid == GET_LWP (lp->ptid));
3154
3155 gdb_assert (pid == -1 && errno == ECHILD);
3156 return 0;
3157}
3158
3159static void
3160linux_nat_kill (void)
3161{
f973ed9c
DJ
3162 struct target_waitstatus last;
3163 ptid_t last_ptid;
3164 int status;
d6b0e80f 3165
b84876c2
PA
3166 if (target_can_async_p ())
3167 target_async (NULL, 0);
3168
f973ed9c
DJ
3169 /* If we're stopped while forking and we haven't followed yet,
3170 kill the other task. We need to do this first because the
3171 parent will be sleeping if this is a vfork. */
d6b0e80f 3172
f973ed9c 3173 get_last_target_status (&last_ptid, &last);
d6b0e80f 3174
f973ed9c
DJ
3175 if (last.kind == TARGET_WAITKIND_FORKED
3176 || last.kind == TARGET_WAITKIND_VFORKED)
3177 {
3a3e9ee3 3178 ptrace (PT_KILL, PIDGET (last.value.related_pid), 0, 0);
f973ed9c
DJ
3179 wait (&status);
3180 }
3181
3182 if (forks_exist_p ())
b84876c2
PA
3183 {
3184 linux_fork_killall ();
3185 drain_queued_events (-1);
3186 }
f973ed9c
DJ
3187 else
3188 {
4c28f408
PA
3189 /* Stop all threads before killing them, since ptrace requires
3190 that the thread is stopped to sucessfully PTRACE_KILL. */
3191 iterate_over_lwps (stop_callback, NULL);
3192 /* ... and wait until all of them have reported back that
3193 they're no longer running. */
3194 iterate_over_lwps (stop_wait_callback, NULL);
3195
f973ed9c
DJ
3196 /* Kill all LWP's ... */
3197 iterate_over_lwps (kill_callback, NULL);
3198
3199 /* ... and wait until we've flushed all events. */
3200 iterate_over_lwps (kill_wait_callback, NULL);
3201 }
3202
3203 target_mourn_inferior ();
d6b0e80f
AC
3204}
3205
3206static void
136d6dae 3207linux_nat_mourn_inferior (struct target_ops *ops)
d6b0e80f 3208{
d6b0e80f
AC
3209 /* Destroy LWP info; it's no longer valid. */
3210 init_lwp_list ();
3211
f973ed9c 3212 if (! forks_exist_p ())
b84876c2
PA
3213 {
3214 /* Normal case, no other forks available. */
3215 if (target_can_async_p ())
3216 linux_nat_async (NULL, 0);
136d6dae 3217 linux_ops->to_mourn_inferior (ops);
b84876c2 3218 }
f973ed9c
DJ
3219 else
3220 /* Multi-fork case. The current inferior_ptid has exited, but
3221 there are other viable forks to debug. Delete the exiting
3222 one and context-switch to the first available. */
3223 linux_fork_mourn_inferior ();
d6b0e80f
AC
3224}
3225
10d6c8cd
DJ
3226static LONGEST
3227linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3228 const char *annex, gdb_byte *readbuf,
3229 const gdb_byte *writebuf,
3230 ULONGEST offset, LONGEST len)
d6b0e80f
AC
3231{
3232 struct cleanup *old_chain = save_inferior_ptid ();
10d6c8cd 3233 LONGEST xfer;
d6b0e80f
AC
3234
3235 if (is_lwp (inferior_ptid))
3236 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
3237
10d6c8cd
DJ
3238 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
3239 offset, len);
d6b0e80f
AC
3240
3241 do_cleanups (old_chain);
3242 return xfer;
3243}
3244
3245static int
3246linux_nat_thread_alive (ptid_t ptid)
3247{
4c28f408
PA
3248 int err;
3249
d6b0e80f
AC
3250 gdb_assert (is_lwp (ptid));
3251
4c28f408
PA
3252 /* Send signal 0 instead of anything ptrace, because ptracing a
3253 running thread errors out claiming that the thread doesn't
3254 exist. */
3255 err = kill_lwp (GET_LWP (ptid), 0);
3256
d6b0e80f
AC
3257 if (debug_linux_nat)
3258 fprintf_unfiltered (gdb_stdlog,
4c28f408 3259 "LLTA: KILL(SIG0) %s (%s)\n",
d6b0e80f 3260 target_pid_to_str (ptid),
4c28f408 3261 err ? safe_strerror (err) : "OK");
9c0dd46b 3262
4c28f408 3263 if (err != 0)
d6b0e80f
AC
3264 return 0;
3265
3266 return 1;
3267}
3268
3269static char *
117de6a9 3270linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
d6b0e80f
AC
3271{
3272 static char buf[64];
3273
a0ef4274
DJ
3274 if (is_lwp (ptid)
3275 && ((lwp_list && lwp_list->next)
3276 || GET_PID (ptid) != GET_LWP (ptid)))
d6b0e80f
AC
3277 {
3278 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
3279 return buf;
3280 }
3281
3282 return normal_pid_to_str (ptid);
3283}
3284
d6b0e80f
AC
3285static void
3286sigchld_handler (int signo)
3287{
c6ebd6cf 3288 if (target_async_permitted
84e46146 3289 && linux_nat_async_events_state != sigchld_sync
b84876c2
PA
3290 && signo == SIGCHLD)
3291 /* It is *always* a bug to hit this. */
3292 internal_error (__FILE__, __LINE__,
3293 "sigchld_handler called when async events are enabled");
3294
d6b0e80f
AC
3295 /* Do nothing. The only reason for this handler is that it allows
3296 us to use sigsuspend in linux_nat_wait above to wait for the
3297 arrival of a SIGCHLD. */
3298}
3299
dba24537
AC
3300/* Accepts an integer PID; Returns a string representing a file that
3301 can be opened to get the symbols for the child process. */
3302
6d8fd2b7
UW
3303static char *
3304linux_child_pid_to_exec_file (int pid)
dba24537
AC
3305{
3306 char *name1, *name2;
3307
3308 name1 = xmalloc (MAXPATHLEN);
3309 name2 = xmalloc (MAXPATHLEN);
3310 make_cleanup (xfree, name1);
3311 make_cleanup (xfree, name2);
3312 memset (name2, 0, MAXPATHLEN);
3313
3314 sprintf (name1, "/proc/%d/exe", pid);
3315 if (readlink (name1, name2, MAXPATHLEN) > 0)
3316 return name2;
3317 else
3318 return name1;
3319}
3320
3321/* Service function for corefiles and info proc. */
3322
3323static int
3324read_mapping (FILE *mapfile,
3325 long long *addr,
3326 long long *endaddr,
3327 char *permissions,
3328 long long *offset,
3329 char *device, long long *inode, char *filename)
3330{
3331 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
3332 addr, endaddr, permissions, offset, device, inode);
3333
2e14c2ea
MS
3334 filename[0] = '\0';
3335 if (ret > 0 && ret != EOF)
dba24537
AC
3336 {
3337 /* Eat everything up to EOL for the filename. This will prevent
3338 weird filenames (such as one with embedded whitespace) from
3339 confusing this code. It also makes this code more robust in
3340 respect to annotations the kernel may add after the filename.
3341
3342 Note the filename is used for informational purposes
3343 only. */
3344 ret += fscanf (mapfile, "%[^\n]\n", filename);
3345 }
2e14c2ea 3346
dba24537
AC
3347 return (ret != 0 && ret != EOF);
3348}
3349
3350/* Fills the "to_find_memory_regions" target vector. Lists the memory
3351 regions in the inferior for a corefile. */
3352
3353static int
3354linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
3355 unsigned long,
3356 int, int, int, void *), void *obfd)
3357{
3358 long long pid = PIDGET (inferior_ptid);
3359 char mapsfilename[MAXPATHLEN];
3360 FILE *mapsfile;
3361 long long addr, endaddr, size, offset, inode;
3362 char permissions[8], device[8], filename[MAXPATHLEN];
3363 int read, write, exec;
3364 int ret;
7c8a8b04 3365 struct cleanup *cleanup;
dba24537
AC
3366
3367 /* Compose the filename for the /proc memory map, and open it. */
3368 sprintf (mapsfilename, "/proc/%lld/maps", pid);
3369 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
8a3fe4f8 3370 error (_("Could not open %s."), mapsfilename);
7c8a8b04 3371 cleanup = make_cleanup_fclose (mapsfile);
dba24537
AC
3372
3373 if (info_verbose)
3374 fprintf_filtered (gdb_stdout,
3375 "Reading memory regions from %s\n", mapsfilename);
3376
3377 /* Now iterate until end-of-file. */
3378 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
3379 &offset, &device[0], &inode, &filename[0]))
3380 {
3381 size = endaddr - addr;
3382
3383 /* Get the segment's permissions. */
3384 read = (strchr (permissions, 'r') != 0);
3385 write = (strchr (permissions, 'w') != 0);
3386 exec = (strchr (permissions, 'x') != 0);
3387
3388 if (info_verbose)
3389 {
3390 fprintf_filtered (gdb_stdout,
3391 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3392 size, paddr_nz (addr),
3393 read ? 'r' : ' ',
3394 write ? 'w' : ' ', exec ? 'x' : ' ');
b260b6c1 3395 if (filename[0])
dba24537
AC
3396 fprintf_filtered (gdb_stdout, " for %s", filename);
3397 fprintf_filtered (gdb_stdout, "\n");
3398 }
3399
3400 /* Invoke the callback function to create the corefile
3401 segment. */
3402 func (addr, size, read, write, exec, obfd);
3403 }
7c8a8b04 3404 do_cleanups (cleanup);
dba24537
AC
3405 return 0;
3406}
3407
2020b7ab
PA
3408static int
3409find_signalled_thread (struct thread_info *info, void *data)
3410{
3411 if (info->stop_signal != TARGET_SIGNAL_0
3412 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
3413 return 1;
3414
3415 return 0;
3416}
3417
3418static enum target_signal
3419find_stop_signal (void)
3420{
3421 struct thread_info *info =
3422 iterate_over_threads (find_signalled_thread, NULL);
3423
3424 if (info)
3425 return info->stop_signal;
3426 else
3427 return TARGET_SIGNAL_0;
3428}
3429
dba24537
AC
3430/* Records the thread's register state for the corefile note
3431 section. */
3432
3433static char *
3434linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
2020b7ab
PA
3435 char *note_data, int *note_size,
3436 enum target_signal stop_signal)
dba24537
AC
3437{
3438 gdb_gregset_t gregs;
3439 gdb_fpregset_t fpregs;
dba24537 3440 unsigned long lwp = ptid_get_lwp (ptid);
594f7785
UW
3441 struct regcache *regcache = get_thread_regcache (ptid);
3442 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4f844a66 3443 const struct regset *regset;
55e969c1 3444 int core_regset_p;
594f7785 3445 struct cleanup *old_chain;
17ea7499
CES
3446 struct core_regset_section *sect_list;
3447 char *gdb_regset;
594f7785
UW
3448
3449 old_chain = save_inferior_ptid ();
3450 inferior_ptid = ptid;
3451 target_fetch_registers (regcache, -1);
3452 do_cleanups (old_chain);
4f844a66
DM
3453
3454 core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
17ea7499
CES
3455 sect_list = gdbarch_core_regset_sections (gdbarch);
3456
55e969c1
DM
3457 if (core_regset_p
3458 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
3459 sizeof (gregs))) != NULL
3460 && regset->collect_regset != NULL)
594f7785 3461 regset->collect_regset (regset, regcache, -1,
55e969c1 3462 &gregs, sizeof (gregs));
4f844a66 3463 else
594f7785 3464 fill_gregset (regcache, &gregs, -1);
4f844a66 3465
55e969c1
DM
3466 note_data = (char *) elfcore_write_prstatus (obfd,
3467 note_data,
3468 note_size,
3469 lwp,
3470 stop_signal, &gregs);
3471
17ea7499
CES
3472 /* The loop below uses the new struct core_regset_section, which stores
3473 the supported section names and sizes for the core file. Note that
3474 note PRSTATUS needs to be treated specially. But the other notes are
3475 structurally the same, so they can benefit from the new struct. */
3476 if (core_regset_p && sect_list != NULL)
3477 while (sect_list->sect_name != NULL)
3478 {
3479 /* .reg was already handled above. */
3480 if (strcmp (sect_list->sect_name, ".reg") == 0)
3481 {
3482 sect_list++;
3483 continue;
3484 }
3485 regset = gdbarch_regset_from_core_section (gdbarch,
3486 sect_list->sect_name,
3487 sect_list->size);
3488 gdb_assert (regset && regset->collect_regset);
3489 gdb_regset = xmalloc (sect_list->size);
3490 regset->collect_regset (regset, regcache, -1,
3491 gdb_regset, sect_list->size);
3492 note_data = (char *) elfcore_write_register_note (obfd,
3493 note_data,
3494 note_size,
3495 sect_list->sect_name,
3496 gdb_regset,
3497 sect_list->size);
3498 xfree (gdb_regset);
3499 sect_list++;
3500 }
dba24537 3501
17ea7499
CES
3502 /* For architectures that does not have the struct core_regset_section
3503 implemented, we use the old method. When all the architectures have
3504 the new support, the code below should be deleted. */
4f844a66 3505 else
17ea7499
CES
3506 {
3507 if (core_regset_p
3508 && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
3509 sizeof (fpregs))) != NULL
3510 && regset->collect_regset != NULL)
3511 regset->collect_regset (regset, regcache, -1,
3512 &fpregs, sizeof (fpregs));
3513 else
3514 fill_fpregset (regcache, &fpregs, -1);
3515
3516 note_data = (char *) elfcore_write_prfpreg (obfd,
3517 note_data,
3518 note_size,
3519 &fpregs, sizeof (fpregs));
3520 }
4f844a66 3521
dba24537
AC
3522 return note_data;
3523}
3524
3525struct linux_nat_corefile_thread_data
3526{
3527 bfd *obfd;
3528 char *note_data;
3529 int *note_size;
3530 int num_notes;
2020b7ab 3531 enum target_signal stop_signal;
dba24537
AC
3532};
3533
3534/* Called by gdbthread.c once per thread. Records the thread's
3535 register state for the corefile note section. */
3536
3537static int
3538linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
3539{
3540 struct linux_nat_corefile_thread_data *args = data;
dba24537 3541
dba24537
AC
3542 args->note_data = linux_nat_do_thread_registers (args->obfd,
3543 ti->ptid,
3544 args->note_data,
2020b7ab
PA
3545 args->note_size,
3546 args->stop_signal);
dba24537 3547 args->num_notes++;
56be3814 3548
dba24537
AC
3549 return 0;
3550}
3551
dba24537
AC
3552/* Fills the "to_make_corefile_note" target vector. Builds the note
3553 section for a corefile, and returns it in a malloc buffer. */
3554
3555static char *
3556linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
3557{
3558 struct linux_nat_corefile_thread_data thread_args;
3559 struct cleanup *old_chain;
d99148ef 3560 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
dba24537 3561 char fname[16] = { '\0' };
d99148ef 3562 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
dba24537
AC
3563 char psargs[80] = { '\0' };
3564 char *note_data = NULL;
3565 ptid_t current_ptid = inferior_ptid;
c6826062 3566 gdb_byte *auxv;
dba24537
AC
3567 int auxv_len;
3568
3569 if (get_exec_file (0))
3570 {
3571 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
3572 strncpy (psargs, get_exec_file (0), sizeof (psargs));
3573 if (get_inferior_args ())
3574 {
d99148ef
JK
3575 char *string_end;
3576 char *psargs_end = psargs + sizeof (psargs);
3577
3578 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3579 strings fine. */
3580 string_end = memchr (psargs, 0, sizeof (psargs));
3581 if (string_end != NULL)
3582 {
3583 *string_end++ = ' ';
3584 strncpy (string_end, get_inferior_args (),
3585 psargs_end - string_end);
3586 }
dba24537
AC
3587 }
3588 note_data = (char *) elfcore_write_prpsinfo (obfd,
3589 note_data,
3590 note_size, fname, psargs);
3591 }
3592
3593 /* Dump information for threads. */
3594 thread_args.obfd = obfd;
3595 thread_args.note_data = note_data;
3596 thread_args.note_size = note_size;
3597 thread_args.num_notes = 0;
2020b7ab 3598 thread_args.stop_signal = find_stop_signal ();
dba24537 3599 iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args);
2020b7ab
PA
3600 gdb_assert (thread_args.num_notes != 0);
3601 note_data = thread_args.note_data;
dba24537 3602
13547ab6
DJ
3603 auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
3604 NULL, &auxv);
dba24537
AC
3605 if (auxv_len > 0)
3606 {
3607 note_data = elfcore_write_note (obfd, note_data, note_size,
3608 "CORE", NT_AUXV, auxv, auxv_len);
3609 xfree (auxv);
3610 }
3611
3612 make_cleanup (xfree, note_data);
3613 return note_data;
3614}
3615
3616/* Implement the "info proc" command. */
3617
3618static void
3619linux_nat_info_proc_cmd (char *args, int from_tty)
3620{
3621 long long pid = PIDGET (inferior_ptid);
3622 FILE *procfile;
3623 char **argv = NULL;
3624 char buffer[MAXPATHLEN];
3625 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
3626 int cmdline_f = 1;
3627 int cwd_f = 1;
3628 int exe_f = 1;
3629 int mappings_f = 0;
3630 int environ_f = 0;
3631 int status_f = 0;
3632 int stat_f = 0;
3633 int all = 0;
3634 struct stat dummy;
3635
3636 if (args)
3637 {
3638 /* Break up 'args' into an argv array. */
d1a41061
PP
3639 argv = gdb_buildargv (args);
3640 make_cleanup_freeargv (argv);
dba24537
AC
3641 }
3642 while (argv != NULL && *argv != NULL)
3643 {
3644 if (isdigit (argv[0][0]))
3645 {
3646 pid = strtoul (argv[0], NULL, 10);
3647 }
3648 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
3649 {
3650 mappings_f = 1;
3651 }
3652 else if (strcmp (argv[0], "status") == 0)
3653 {
3654 status_f = 1;
3655 }
3656 else if (strcmp (argv[0], "stat") == 0)
3657 {
3658 stat_f = 1;
3659 }
3660 else if (strcmp (argv[0], "cmd") == 0)
3661 {
3662 cmdline_f = 1;
3663 }
3664 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
3665 {
3666 exe_f = 1;
3667 }
3668 else if (strcmp (argv[0], "cwd") == 0)
3669 {
3670 cwd_f = 1;
3671 }
3672 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
3673 {
3674 all = 1;
3675 }
3676 else
3677 {
3678 /* [...] (future options here) */
3679 }
3680 argv++;
3681 }
3682 if (pid == 0)
8a3fe4f8 3683 error (_("No current process: you must name one."));
dba24537
AC
3684
3685 sprintf (fname1, "/proc/%lld", pid);
3686 if (stat (fname1, &dummy) != 0)
8a3fe4f8 3687 error (_("No /proc directory: '%s'"), fname1);
dba24537 3688
a3f17187 3689 printf_filtered (_("process %lld\n"), pid);
dba24537
AC
3690 if (cmdline_f || all)
3691 {
3692 sprintf (fname1, "/proc/%lld/cmdline", pid);
d5d6fca5 3693 if ((procfile = fopen (fname1, "r")) != NULL)
dba24537 3694 {
7c8a8b04 3695 struct cleanup *cleanup = make_cleanup_fclose (procfile);
bf1d7d9c
JB
3696 if (fgets (buffer, sizeof (buffer), procfile))
3697 printf_filtered ("cmdline = '%s'\n", buffer);
3698 else
3699 warning (_("unable to read '%s'"), fname1);
7c8a8b04 3700 do_cleanups (cleanup);
dba24537
AC
3701 }
3702 else
8a3fe4f8 3703 warning (_("unable to open /proc file '%s'"), fname1);
dba24537
AC
3704 }
3705 if (cwd_f || all)
3706 {
3707 sprintf (fname1, "/proc/%lld/cwd", pid);
3708 memset (fname2, 0, sizeof (fname2));
3709 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
3710 printf_filtered ("cwd = '%s'\n", fname2);
3711 else
8a3fe4f8 3712 warning (_("unable to read link '%s'"), fname1);
dba24537
AC
3713 }
3714 if (exe_f || all)
3715 {
3716 sprintf (fname1, "/proc/%lld/exe", pid);
3717 memset (fname2, 0, sizeof (fname2));
3718 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
3719 printf_filtered ("exe = '%s'\n", fname2);
3720 else
8a3fe4f8 3721 warning (_("unable to read link '%s'"), fname1);
dba24537
AC
3722 }
3723 if (mappings_f || all)
3724 {
3725 sprintf (fname1, "/proc/%lld/maps", pid);
d5d6fca5 3726 if ((procfile = fopen (fname1, "r")) != NULL)
dba24537
AC
3727 {
3728 long long addr, endaddr, size, offset, inode;
3729 char permissions[8], device[8], filename[MAXPATHLEN];
7c8a8b04 3730 struct cleanup *cleanup;
dba24537 3731
7c8a8b04 3732 cleanup = make_cleanup_fclose (procfile);
a3f17187 3733 printf_filtered (_("Mapped address spaces:\n\n"));
17a912b6 3734 if (gdbarch_addr_bit (current_gdbarch) == 32)
dba24537
AC
3735 {
3736 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3737 "Start Addr",
3738 " End Addr",
3739 " Size", " Offset", "objfile");
3740 }
3741 else
3742 {
3743 printf_filtered (" %18s %18s %10s %10s %7s\n",
3744 "Start Addr",
3745 " End Addr",
3746 " Size", " Offset", "objfile");
3747 }
3748
3749 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
3750 &offset, &device[0], &inode, &filename[0]))
3751 {
3752 size = endaddr - addr;
3753
3754 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3755 calls here (and possibly above) should be abstracted
3756 out into their own functions? Andrew suggests using
3757 a generic local_address_string instead to print out
3758 the addresses; that makes sense to me, too. */
3759
17a912b6 3760 if (gdbarch_addr_bit (current_gdbarch) == 32)
dba24537
AC
3761 {
3762 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3763 (unsigned long) addr, /* FIXME: pr_addr */
3764 (unsigned long) endaddr,
3765 (int) size,
3766 (unsigned int) offset,
3767 filename[0] ? filename : "");
3768 }
3769 else
3770 {
3771 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3772 (unsigned long) addr, /* FIXME: pr_addr */
3773 (unsigned long) endaddr,
3774 (int) size,
3775 (unsigned int) offset,
3776 filename[0] ? filename : "");
3777 }
3778 }
3779
7c8a8b04 3780 do_cleanups (cleanup);
dba24537
AC
3781 }
3782 else
8a3fe4f8 3783 warning (_("unable to open /proc file '%s'"), fname1);
dba24537
AC
3784 }
3785 if (status_f || all)
3786 {
3787 sprintf (fname1, "/proc/%lld/status", pid);
d5d6fca5 3788 if ((procfile = fopen (fname1, "r")) != NULL)
dba24537 3789 {
7c8a8b04 3790 struct cleanup *cleanup = make_cleanup_fclose (procfile);
dba24537
AC
3791 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
3792 puts_filtered (buffer);
7c8a8b04 3793 do_cleanups (cleanup);
dba24537
AC
3794 }
3795 else
8a3fe4f8 3796 warning (_("unable to open /proc file '%s'"), fname1);
dba24537
AC
3797 }
3798 if (stat_f || all)
3799 {
3800 sprintf (fname1, "/proc/%lld/stat", pid);
d5d6fca5 3801 if ((procfile = fopen (fname1, "r")) != NULL)
dba24537
AC
3802 {
3803 int itmp;
3804 char ctmp;
a25694b4 3805 long ltmp;
7c8a8b04 3806 struct cleanup *cleanup = make_cleanup_fclose (procfile);
dba24537
AC
3807
3808 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3809 printf_filtered (_("Process: %d\n"), itmp);
a25694b4 3810 if (fscanf (procfile, "(%[^)]) ", &buffer[0]) > 0)
a3f17187 3811 printf_filtered (_("Exec file: %s\n"), buffer);
dba24537 3812 if (fscanf (procfile, "%c ", &ctmp) > 0)
a3f17187 3813 printf_filtered (_("State: %c\n"), ctmp);
dba24537 3814 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3815 printf_filtered (_("Parent process: %d\n"), itmp);
dba24537 3816 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3817 printf_filtered (_("Process group: %d\n"), itmp);
dba24537 3818 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3819 printf_filtered (_("Session id: %d\n"), itmp);
dba24537 3820 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3821 printf_filtered (_("TTY: %d\n"), itmp);
dba24537 3822 if (fscanf (procfile, "%d ", &itmp) > 0)
a3f17187 3823 printf_filtered (_("TTY owner process group: %d\n"), itmp);
a25694b4
AS
3824 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3825 printf_filtered (_("Flags: 0x%lx\n"), ltmp);
3826 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3827 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3828 (unsigned long) ltmp);
3829 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3830 printf_filtered (_("Minor faults, children: %lu\n"),
3831 (unsigned long) ltmp);
3832 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3833 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3834 (unsigned long) ltmp);
3835 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3836 printf_filtered (_("Major faults, children: %lu\n"),
3837 (unsigned long) ltmp);
3838 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3839 printf_filtered (_("utime: %ld\n"), ltmp);
3840 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3841 printf_filtered (_("stime: %ld\n"), ltmp);
3842 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3843 printf_filtered (_("utime, children: %ld\n"), ltmp);
3844 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3845 printf_filtered (_("stime, children: %ld\n"), ltmp);
3846 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3847 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3848 ltmp);
3849 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3850 printf_filtered (_("'nice' value: %ld\n"), ltmp);
3851 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3852 printf_filtered (_("jiffies until next timeout: %lu\n"),
3853 (unsigned long) ltmp);
3854 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3855 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3856 (unsigned long) ltmp);
3857 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3858 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3859 ltmp);
3860 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3861 printf_filtered (_("Virtual memory size: %lu\n"),
3862 (unsigned long) ltmp);
3863 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3864 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp);
3865 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3866 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp);
3867 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3868 printf_filtered (_("Start of text: 0x%lx\n"), ltmp);
3869 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3870 printf_filtered (_("End of text: 0x%lx\n"), ltmp);
3871 if (fscanf (procfile, "%lu ", &ltmp) > 0)
3872 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp);
dba24537
AC
3873#if 0 /* Don't know how architecture-dependent the rest is...
3874 Anyway the signal bitmap info is available from "status". */
a25694b4
AS
3875 if (fscanf (procfile, "%lu ", &ltmp) > 0) /* FIXME arch? */
3876 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp);
3877 if (fscanf (procfile, "%lu ", &ltmp) > 0) /* FIXME arch? */
3878 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp);
3879 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3880 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp);
3881 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3882 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp);
3883 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3884 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp);
3885 if (fscanf (procfile, "%ld ", &ltmp) > 0)
3886 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp);
3887 if (fscanf (procfile, "%lu ", &ltmp) > 0) /* FIXME arch? */
3888 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp);
dba24537 3889#endif
7c8a8b04 3890 do_cleanups (cleanup);
dba24537
AC
3891 }
3892 else
8a3fe4f8 3893 warning (_("unable to open /proc file '%s'"), fname1);
dba24537
AC
3894 }
3895}
3896
10d6c8cd
DJ
3897/* Implement the to_xfer_partial interface for memory reads using the /proc
3898 filesystem. Because we can use a single read() call for /proc, this
3899 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3900 but it doesn't support writes. */
3901
3902static LONGEST
3903linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
3904 const char *annex, gdb_byte *readbuf,
3905 const gdb_byte *writebuf,
3906 ULONGEST offset, LONGEST len)
dba24537 3907{
10d6c8cd
DJ
3908 LONGEST ret;
3909 int fd;
dba24537
AC
3910 char filename[64];
3911
10d6c8cd 3912 if (object != TARGET_OBJECT_MEMORY || !readbuf)
dba24537
AC
3913 return 0;
3914
3915 /* Don't bother for one word. */
3916 if (len < 3 * sizeof (long))
3917 return 0;
3918
3919 /* We could keep this file open and cache it - possibly one per
3920 thread. That requires some juggling, but is even faster. */
3921 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
3922 fd = open (filename, O_RDONLY | O_LARGEFILE);
3923 if (fd == -1)
3924 return 0;
3925
3926 /* If pread64 is available, use it. It's faster if the kernel
3927 supports it (only one syscall), and it's 64-bit safe even on
3928 32-bit platforms (for instance, SPARC debugging a SPARC64
3929 application). */
3930#ifdef HAVE_PREAD64
10d6c8cd 3931 if (pread64 (fd, readbuf, len, offset) != len)
dba24537 3932#else
10d6c8cd 3933 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
dba24537
AC
3934#endif
3935 ret = 0;
3936 else
3937 ret = len;
3938
3939 close (fd);
3940 return ret;
3941}
3942
3943/* Parse LINE as a signal set and add its set bits to SIGS. */
3944
3945static void
3946add_line_to_sigset (const char *line, sigset_t *sigs)
3947{
3948 int len = strlen (line) - 1;
3949 const char *p;
3950 int signum;
3951
3952 if (line[len] != '\n')
8a3fe4f8 3953 error (_("Could not parse signal set: %s"), line);
dba24537
AC
3954
3955 p = line;
3956 signum = len * 4;
3957 while (len-- > 0)
3958 {
3959 int digit;
3960
3961 if (*p >= '0' && *p <= '9')
3962 digit = *p - '0';
3963 else if (*p >= 'a' && *p <= 'f')
3964 digit = *p - 'a' + 10;
3965 else
8a3fe4f8 3966 error (_("Could not parse signal set: %s"), line);
dba24537
AC
3967
3968 signum -= 4;
3969
3970 if (digit & 1)
3971 sigaddset (sigs, signum + 1);
3972 if (digit & 2)
3973 sigaddset (sigs, signum + 2);
3974 if (digit & 4)
3975 sigaddset (sigs, signum + 3);
3976 if (digit & 8)
3977 sigaddset (sigs, signum + 4);
3978
3979 p++;
3980 }
3981}
3982
3983/* Find process PID's pending signals from /proc/pid/status and set
3984 SIGS to match. */
3985
3986void
3987linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
3988{
3989 FILE *procfile;
3990 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
3991 int signum;
7c8a8b04 3992 struct cleanup *cleanup;
dba24537
AC
3993
3994 sigemptyset (pending);
3995 sigemptyset (blocked);
3996 sigemptyset (ignored);
3997 sprintf (fname, "/proc/%d/status", pid);
3998 procfile = fopen (fname, "r");
3999 if (procfile == NULL)
8a3fe4f8 4000 error (_("Could not open %s"), fname);
7c8a8b04 4001 cleanup = make_cleanup_fclose (procfile);
dba24537
AC
4002
4003 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
4004 {
4005 /* Normal queued signals are on the SigPnd line in the status
4006 file. However, 2.6 kernels also have a "shared" pending
4007 queue for delivering signals to a thread group, so check for
4008 a ShdPnd line also.
4009
4010 Unfortunately some Red Hat kernels include the shared pending
4011 queue but not the ShdPnd status field. */
4012
4013 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
4014 add_line_to_sigset (buffer + 8, pending);
4015 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
4016 add_line_to_sigset (buffer + 8, pending);
4017 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
4018 add_line_to_sigset (buffer + 8, blocked);
4019 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
4020 add_line_to_sigset (buffer + 8, ignored);
4021 }
4022
7c8a8b04 4023 do_cleanups (cleanup);
dba24537
AC
4024}
4025
07e059b5
VP
4026static LONGEST
4027linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4028 const char *annex, gdb_byte *readbuf,
4029 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4030{
4031 /* We make the process list snapshot when the object starts to be
4032 read. */
4033 static const char *buf;
4034 static LONGEST len_avail = -1;
4035 static struct obstack obstack;
4036
4037 DIR *dirp;
4038
4039 gdb_assert (object == TARGET_OBJECT_OSDATA);
4040
4041 if (strcmp (annex, "processes") != 0)
4042 return 0;
4043
4044 gdb_assert (readbuf && !writebuf);
4045
4046 if (offset == 0)
4047 {
4048 if (len_avail != -1 && len_avail != 0)
4049 obstack_free (&obstack, NULL);
4050 len_avail = 0;
4051 buf = NULL;
4052 obstack_init (&obstack);
4053 obstack_grow_str (&obstack, "<osdata type=\"processes\">\n");
4054
4055 dirp = opendir ("/proc");
4056 if (dirp)
4057 {
4058 struct dirent *dp;
4059 while ((dp = readdir (dirp)) != NULL)
4060 {
4061 struct stat statbuf;
4062 char procentry[sizeof ("/proc/4294967295")];
4063
4064 if (!isdigit (dp->d_name[0])
4065 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
4066 continue;
4067
4068 sprintf (procentry, "/proc/%s", dp->d_name);
4069 if (stat (procentry, &statbuf) == 0
4070 && S_ISDIR (statbuf.st_mode))
4071 {
4072 char *pathname;
4073 FILE *f;
4074 char cmd[MAXPATHLEN + 1];
4075 struct passwd *entry;
4076
4077 pathname = xstrprintf ("/proc/%s/cmdline", dp->d_name);
4078 entry = getpwuid (statbuf.st_uid);
4079
4080 if ((f = fopen (pathname, "r")) != NULL)
4081 {
4082 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
4083 if (len > 0)
4084 {
4085 int i;
4086 for (i = 0; i < len; i++)
4087 if (cmd[i] == '\0')
4088 cmd[i] = ' ';
4089 cmd[len] = '\0';
4090
4091 obstack_xml_printf (
4092 &obstack,
4093 "<item>"
4094 "<column name=\"pid\">%s</column>"
4095 "<column name=\"user\">%s</column>"
4096 "<column name=\"command\">%s</column>"
4097 "</item>",
4098 dp->d_name,
4099 entry ? entry->pw_name : "?",
4100 cmd);
4101 }
4102 fclose (f);
4103 }
4104
4105 xfree (pathname);
4106 }
4107 }
4108
4109 closedir (dirp);
4110 }
4111
4112 obstack_grow_str0 (&obstack, "</osdata>\n");
4113 buf = obstack_finish (&obstack);
4114 len_avail = strlen (buf);
4115 }
4116
4117 if (offset >= len_avail)
4118 {
4119 /* Done. Get rid of the obstack. */
4120 obstack_free (&obstack, NULL);
4121 buf = NULL;
4122 len_avail = 0;
4123 return 0;
4124 }
4125
4126 if (len > len_avail - offset)
4127 len = len_avail - offset;
4128 memcpy (readbuf, buf + offset, len);
4129
4130 return len;
4131}
4132
10d6c8cd
DJ
4133static LONGEST
4134linux_xfer_partial (struct target_ops *ops, enum target_object object,
4135 const char *annex, gdb_byte *readbuf,
4136 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4137{
4138 LONGEST xfer;
4139
4140 if (object == TARGET_OBJECT_AUXV)
4141 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
4142 offset, len);
4143
07e059b5
VP
4144 if (object == TARGET_OBJECT_OSDATA)
4145 return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4146 offset, len);
4147
10d6c8cd
DJ
4148 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4149 offset, len);
4150 if (xfer != 0)
4151 return xfer;
4152
4153 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4154 offset, len);
4155}
4156
e9efe249 4157/* Create a prototype generic GNU/Linux target. The client can override
10d6c8cd
DJ
4158 it with local methods. */
4159
910122bf
UW
4160static void
4161linux_target_install_ops (struct target_ops *t)
10d6c8cd 4162{
6d8fd2b7
UW
4163 t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4164 t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4165 t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4166 t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
10d6c8cd 4167 t->to_post_startup_inferior = linux_child_post_startup_inferior;
6d8fd2b7
UW
4168 t->to_post_attach = linux_child_post_attach;
4169 t->to_follow_fork = linux_child_follow_fork;
10d6c8cd
DJ
4170 t->to_find_memory_regions = linux_nat_find_memory_regions;
4171 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
4172
4173 super_xfer_partial = t->to_xfer_partial;
4174 t->to_xfer_partial = linux_xfer_partial;
910122bf
UW
4175}
4176
4177struct target_ops *
4178linux_target (void)
4179{
4180 struct target_ops *t;
4181
4182 t = inf_ptrace_target ();
4183 linux_target_install_ops (t);
4184
4185 return t;
4186}
4187
4188struct target_ops *
7714d83a 4189linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
910122bf
UW
4190{
4191 struct target_ops *t;
4192
4193 t = inf_ptrace_trad_target (register_u_offset);
4194 linux_target_install_ops (t);
10d6c8cd 4195
10d6c8cd
DJ
4196 return t;
4197}
4198
b84876c2
PA
4199/* target_is_async_p implementation. */
4200
4201static int
4202linux_nat_is_async_p (void)
4203{
4204 /* NOTE: palves 2008-03-21: We're only async when the user requests
c6ebd6cf 4205 it explicitly with the "maintenance set target-async" command.
b84876c2 4206 Someday, linux will always be async. */
c6ebd6cf 4207 if (!target_async_permitted)
b84876c2
PA
4208 return 0;
4209
4210 return 1;
4211}
4212
4213/* target_can_async_p implementation. */
4214
4215static int
4216linux_nat_can_async_p (void)
4217{
4218 /* NOTE: palves 2008-03-21: We're only async when the user requests
c6ebd6cf 4219 it explicitly with the "maintenance set target-async" command.
b84876c2 4220 Someday, linux will always be async. */
c6ebd6cf 4221 if (!target_async_permitted)
b84876c2
PA
4222 return 0;
4223
4224 /* See target.h/target_async_mask. */
4225 return linux_nat_async_mask_value;
4226}
4227
9908b566
VP
4228static int
4229linux_nat_supports_non_stop (void)
4230{
4231 return 1;
4232}
4233
b84876c2
PA
4234/* target_async_mask implementation. */
4235
4236static int
4237linux_nat_async_mask (int mask)
4238{
4239 int current_state;
4240 current_state = linux_nat_async_mask_value;
4241
4242 if (current_state != mask)
4243 {
4244 if (mask == 0)
4245 {
4246 linux_nat_async (NULL, 0);
4247 linux_nat_async_mask_value = mask;
b84876c2
PA
4248 }
4249 else
4250 {
b84876c2
PA
4251 linux_nat_async_mask_value = mask;
4252 linux_nat_async (inferior_event_handler, 0);
4253 }
4254 }
4255
4256 return current_state;
4257}
4258
4259/* Pop an event from the event pipe. */
4260
4261static int
4262linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options)
4263{
4264 struct waitpid_result event = {0};
4265 int ret;
4266
4267 do
4268 {
4269 ret = read (linux_nat_event_pipe[0], &event, sizeof (event));
4270 }
4271 while (ret == -1 && errno == EINTR);
4272
4273 gdb_assert (ret == sizeof (event));
4274
4275 *ptr_status = event.status;
4276 *ptr_options = event.options;
4277
4278 linux_nat_num_queued_events--;
4279
4280 return event.pid;
4281}
4282
4283/* Push an event into the event pipe. */
4284
4285static void
4286linux_nat_event_pipe_push (int pid, int status, int options)
4287{
4288 int ret;
4289 struct waitpid_result event = {0};
4290 event.pid = pid;
4291 event.status = status;
4292 event.options = options;
4293
4294 do
4295 {
4296 ret = write (linux_nat_event_pipe[1], &event, sizeof (event));
4297 gdb_assert ((ret == -1 && errno == EINTR) || ret == sizeof (event));
4298 } while (ret == -1 && errno == EINTR);
4299
4300 linux_nat_num_queued_events++;
4301}
4302
4303static void
4304get_pending_events (void)
4305{
4306 int status, options, pid;
4307
c6ebd6cf 4308 if (!target_async_permitted
84e46146 4309 || linux_nat_async_events_state != sigchld_async)
b84876c2
PA
4310 internal_error (__FILE__, __LINE__,
4311 "get_pending_events called with async masked");
4312
4313 while (1)
4314 {
4315 status = 0;
4316 options = __WCLONE | WNOHANG;
4317
4318 do
4319 {
4320 pid = waitpid (-1, &status, options);
4321 }
4322 while (pid == -1 && errno == EINTR);
4323
4324 if (pid <= 0)
4325 {
4326 options = WNOHANG;
4327 do
4328 {
4329 pid = waitpid (-1, &status, options);
4330 }
4331 while (pid == -1 && errno == EINTR);
4332 }
4333
4334 if (pid <= 0)
4335 /* No more children reporting events. */
4336 break;
4337
4338 if (debug_linux_nat_async)
4339 fprintf_unfiltered (gdb_stdlog, "\
4340get_pending_events: pid(%d), status(%x), options (%x)\n",
4341 pid, status, options);
4342
4343 linux_nat_event_pipe_push (pid, status, options);
4344 }
4345
4346 if (debug_linux_nat_async)
4347 fprintf_unfiltered (gdb_stdlog, "\
4348get_pending_events: linux_nat_num_queued_events(%d)\n",
4349 linux_nat_num_queued_events);
4350}
4351
4352/* SIGCHLD handler for async mode. */
4353
4354static void
4355async_sigchld_handler (int signo)
4356{
4357 if (debug_linux_nat_async)
4358 fprintf_unfiltered (gdb_stdlog, "async_sigchld_handler\n");
4359
4360 get_pending_events ();
4361}
4362
84e46146 4363/* Set SIGCHLD handling state to STATE. Returns previous state. */
b84876c2 4364
84e46146
PA
4365static enum sigchld_state
4366linux_nat_async_events (enum sigchld_state state)
b84876c2 4367{
84e46146 4368 enum sigchld_state current_state = linux_nat_async_events_state;
b84876c2
PA
4369
4370 if (debug_linux_nat_async)
4371 fprintf_unfiltered (gdb_stdlog,
84e46146 4372 "LNAE: state(%d): linux_nat_async_events_state(%d), "
b84876c2 4373 "linux_nat_num_queued_events(%d)\n",
84e46146 4374 state, linux_nat_async_events_state,
b84876c2
PA
4375 linux_nat_num_queued_events);
4376
84e46146 4377 if (current_state != state)
b84876c2
PA
4378 {
4379 sigset_t mask;
4380 sigemptyset (&mask);
4381 sigaddset (&mask, SIGCHLD);
84e46146
PA
4382
4383 /* Always block before changing state. */
4384 sigprocmask (SIG_BLOCK, &mask, NULL);
4385
4386 /* Set new state. */
4387 linux_nat_async_events_state = state;
4388
4389 switch (state)
b84876c2 4390 {
84e46146
PA
4391 case sigchld_sync:
4392 {
4393 /* Block target events. */
4394 sigprocmask (SIG_BLOCK, &mask, NULL);
4395 sigaction (SIGCHLD, &sync_sigchld_action, NULL);
4396 /* Get events out of queue, and make them available to
4397 queued_waitpid / my_waitpid. */
4398 pipe_to_local_event_queue ();
4399 }
4400 break;
4401 case sigchld_async:
4402 {
4403 /* Unblock target events for async mode. */
4404
4405 sigprocmask (SIG_BLOCK, &mask, NULL);
4406
4407 /* Put events we already waited on, in the pipe first, so
4408 events are FIFO. */
4409 local_event_queue_to_pipe ();
4410 /* While in masked async, we may have not collected all
4411 the pending events. Get them out now. */
4412 get_pending_events ();
4413
4414 /* Let'em come. */
4415 sigaction (SIGCHLD, &async_sigchld_action, NULL);
4416 sigprocmask (SIG_UNBLOCK, &mask, NULL);
4417 }
4418 break;
4419 case sigchld_default:
4420 {
4421 /* SIGCHLD default mode. */
4422 sigaction (SIGCHLD, &sigchld_default_action, NULL);
4423
4424 /* Get events out of queue, and make them available to
4425 queued_waitpid / my_waitpid. */
4426 pipe_to_local_event_queue ();
4427
4428 /* Unblock SIGCHLD. */
4429 sigprocmask (SIG_UNBLOCK, &mask, NULL);
4430 }
4431 break;
b84876c2
PA
4432 }
4433 }
4434
4435 return current_state;
4436}
4437
4438static int async_terminal_is_ours = 1;
4439
4440/* target_terminal_inferior implementation. */
4441
4442static void
4443linux_nat_terminal_inferior (void)
4444{
4445 if (!target_is_async_p ())
4446 {
4447 /* Async mode is disabled. */
4448 terminal_inferior ();
4449 return;
4450 }
4451
4452 /* GDB should never give the terminal to the inferior, if the
4453 inferior is running in the background (run&, continue&, etc.).
4454 This check can be removed when the common code is fixed. */
4455 if (!sync_execution)
4456 return;
4457
4458 terminal_inferior ();
4459
4460 if (!async_terminal_is_ours)
4461 return;
4462
4463 delete_file_handler (input_fd);
4464 async_terminal_is_ours = 0;
4465 set_sigint_trap ();
4466}
4467
4468/* target_terminal_ours implementation. */
4469
4470void
4471linux_nat_terminal_ours (void)
4472{
4473 if (!target_is_async_p ())
4474 {
4475 /* Async mode is disabled. */
4476 terminal_ours ();
4477 return;
4478 }
4479
4480 /* GDB should never give the terminal to the inferior if the
4481 inferior is running in the background (run&, continue&, etc.),
4482 but claiming it sure should. */
4483 terminal_ours ();
4484
4485 if (!sync_execution)
4486 return;
4487
4488 if (async_terminal_is_ours)
4489 return;
4490
4491 clear_sigint_trap ();
4492 add_file_handler (input_fd, stdin_event_handler, 0);
4493 async_terminal_is_ours = 1;
4494}
4495
4496static void (*async_client_callback) (enum inferior_event_type event_type,
4497 void *context);
4498static void *async_client_context;
4499
4500static void
4501linux_nat_async_file_handler (int error, gdb_client_data client_data)
4502{
4503 async_client_callback (INF_REG_EVENT, async_client_context);
4504}
4505
4506/* target_async implementation. */
4507
4508static void
4509linux_nat_async (void (*callback) (enum inferior_event_type event_type,
4510 void *context), void *context)
4511{
c6ebd6cf 4512 if (linux_nat_async_mask_value == 0 || !target_async_permitted)
b84876c2
PA
4513 internal_error (__FILE__, __LINE__,
4514 "Calling target_async when async is masked");
4515
4516 if (callback != NULL)
4517 {
4518 async_client_callback = callback;
4519 async_client_context = context;
4520 add_file_handler (linux_nat_event_pipe[0],
4521 linux_nat_async_file_handler, NULL);
4522
84e46146 4523 linux_nat_async_events (sigchld_async);
b84876c2
PA
4524 }
4525 else
4526 {
4527 async_client_callback = callback;
4528 async_client_context = context;
4529
84e46146 4530 linux_nat_async_events (sigchld_sync);
b84876c2
PA
4531 delete_file_handler (linux_nat_event_pipe[0]);
4532 }
4533 return;
4534}
4535
252fbfc8
PA
4536/* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4537 event came out. */
4538
4c28f408 4539static int
252fbfc8 4540linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4c28f408 4541{
252fbfc8
PA
4542 ptid_t ptid = * (ptid_t *) data;
4543
4544 if (ptid_equal (lwp->ptid, ptid)
4545 || ptid_equal (minus_one_ptid, ptid)
4546 || (ptid_is_pid (ptid)
4547 && ptid_get_pid (ptid) == ptid_get_pid (lwp->ptid)))
4548 {
4549 if (!lwp->stopped)
4550 {
4551 int pid, status;
4552
4553 if (debug_linux_nat)
4554 fprintf_unfiltered (gdb_stdlog,
4555 "LNSL: running -> suspending %s\n",
4556 target_pid_to_str (lwp->ptid));
4557
4558 /* Peek once, to check if we've already waited for this
4559 LWP. */
4560 pid = queued_waitpid_1 (ptid_get_lwp (lwp->ptid), &status,
4561 lwp->cloned ? __WCLONE : 0, 1 /* peek */);
4562
4563 if (pid == -1)
4564 {
4565 ptid_t ptid = lwp->ptid;
4566
4567 stop_callback (lwp, NULL);
4568 stop_wait_callback (lwp, NULL);
4569
4570 /* If the lwp exits while we try to stop it, there's
4571 nothing else to do. */
4572 lwp = find_lwp_pid (ptid);
4573 if (lwp == NULL)
4574 return 0;
4575
4576 pid = queued_waitpid_1 (ptid_get_lwp (lwp->ptid), &status,
4577 lwp->cloned ? __WCLONE : 0,
4578 1 /* peek */);
4579 }
4580
4581 /* If we didn't collect any signal other than SIGSTOP while
4582 stopping the LWP, push a SIGNAL_0 event. In either case,
4583 the event-loop will end up calling target_wait which will
4584 collect these. */
4585 if (pid == -1)
4586 push_waitpid (ptid_get_lwp (lwp->ptid), W_STOPCODE (0),
4587 lwp->cloned ? __WCLONE : 0);
4588 }
4589 else
4590 {
4591 /* Already known to be stopped; do nothing. */
4592
4593 if (debug_linux_nat)
4594 {
4595 if (find_thread_pid (lwp->ptid)->stop_requested)
4596 fprintf_unfiltered (gdb_stdlog, "\
4597LNSL: already stopped/stop_requested %s\n",
4598 target_pid_to_str (lwp->ptid));
4599 else
4600 fprintf_unfiltered (gdb_stdlog, "\
4601LNSL: already stopped/no stop_requested yet %s\n",
4602 target_pid_to_str (lwp->ptid));
4603 }
4604 }
4605 }
4c28f408
PA
4606 return 0;
4607}
4608
4609static void
4610linux_nat_stop (ptid_t ptid)
4611{
4612 if (non_stop)
4613 {
252fbfc8
PA
4614 linux_nat_async_events (sigchld_sync);
4615 iterate_over_lwps (linux_nat_stop_lwp, &ptid);
4616 target_async (inferior_event_handler, 0);
4c28f408
PA
4617 }
4618 else
4619 linux_ops->to_stop (ptid);
4620}
4621
f973ed9c
DJ
4622void
4623linux_nat_add_target (struct target_ops *t)
4624{
f973ed9c
DJ
4625 /* Save the provided single-threaded target. We save this in a separate
4626 variable because another target we've inherited from (e.g. inf-ptrace)
4627 may have saved a pointer to T; we want to use it for the final
4628 process stratum target. */
4629 linux_ops_saved = *t;
4630 linux_ops = &linux_ops_saved;
4631
4632 /* Override some methods for multithreading. */
b84876c2 4633 t->to_create_inferior = linux_nat_create_inferior;
f973ed9c
DJ
4634 t->to_attach = linux_nat_attach;
4635 t->to_detach = linux_nat_detach;
4636 t->to_resume = linux_nat_resume;
4637 t->to_wait = linux_nat_wait;
4638 t->to_xfer_partial = linux_nat_xfer_partial;
4639 t->to_kill = linux_nat_kill;
4640 t->to_mourn_inferior = linux_nat_mourn_inferior;
4641 t->to_thread_alive = linux_nat_thread_alive;
4642 t->to_pid_to_str = linux_nat_pid_to_str;
4643 t->to_has_thread_control = tc_schedlock;
4644
b84876c2
PA
4645 t->to_can_async_p = linux_nat_can_async_p;
4646 t->to_is_async_p = linux_nat_is_async_p;
9908b566 4647 t->to_supports_non_stop = linux_nat_supports_non_stop;
b84876c2
PA
4648 t->to_async = linux_nat_async;
4649 t->to_async_mask = linux_nat_async_mask;
4650 t->to_terminal_inferior = linux_nat_terminal_inferior;
4651 t->to_terminal_ours = linux_nat_terminal_ours;
4652
4c28f408
PA
4653 /* Methods for non-stop support. */
4654 t->to_stop = linux_nat_stop;
4655
f973ed9c
DJ
4656 /* We don't change the stratum; this target will sit at
4657 process_stratum and thread_db will set at thread_stratum. This
4658 is a little strange, since this is a multi-threaded-capable
4659 target, but we want to be on the stack below thread_db, and we
4660 also want to be used for single-threaded processes. */
4661
4662 add_target (t);
f973ed9c
DJ
4663}
4664
9f0bdab8
DJ
4665/* Register a method to call whenever a new thread is attached. */
4666void
4667linux_nat_set_new_thread (struct target_ops *t, void (*new_thread) (ptid_t))
4668{
4669 /* Save the pointer. We only support a single registered instance
4670 of the GNU/Linux native target, so we do not need to map this to
4671 T. */
4672 linux_nat_new_thread = new_thread;
4673}
4674
4675/* Return the saved siginfo associated with PTID. */
4676struct siginfo *
4677linux_nat_get_siginfo (ptid_t ptid)
4678{
4679 struct lwp_info *lp = find_lwp_pid (ptid);
4680
4681 gdb_assert (lp != NULL);
4682
4683 return &lp->siginfo;
4684}
4685
c6ebd6cf
VP
4686/* Enable/Disable async mode. */
4687
4688static void
4689linux_nat_setup_async (void)
4690{
4691 if (pipe (linux_nat_event_pipe) == -1)
4692 internal_error (__FILE__, __LINE__,
4693 "creating event pipe failed.");
4694 fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4695 fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4696}
4697
d6b0e80f
AC
4698void
4699_initialize_linux_nat (void)
4700{
b84876c2 4701 sigset_t mask;
dba24537 4702
1bedd215
AC
4703 add_info ("proc", linux_nat_info_proc_cmd, _("\
4704Show /proc process information about any running process.\n\
dba24537
AC
4705Specify any process id, or use the program being debugged by default.\n\
4706Specify any of the following keywords for detailed info:\n\
4707 mappings -- list of mapped memory regions.\n\
4708 stat -- list a bunch of random process info.\n\
4709 status -- list a different bunch of random process info.\n\
1bedd215 4710 all -- list all available /proc info."));
d6b0e80f 4711
b84876c2
PA
4712 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance,
4713 &debug_linux_nat, _("\
4714Set debugging of GNU/Linux lwp module."), _("\
4715Show debugging of GNU/Linux lwp module."), _("\
4716Enables printf debugging output."),
4717 NULL,
4718 show_debug_linux_nat,
4719 &setdebuglist, &showdebuglist);
4720
4721 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance,
4722 &debug_linux_nat_async, _("\
4723Set debugging of GNU/Linux async lwp module."), _("\
4724Show debugging of GNU/Linux async lwp module."), _("\
4725Enables printf debugging output."),
4726 NULL,
4727 show_debug_linux_nat_async,
4728 &setdebuglist, &showdebuglist);
4729
84e46146
PA
4730 /* Get the default SIGCHLD action. Used while forking an inferior
4731 (see linux_nat_create_inferior/linux_nat_async_events). */
4732 sigaction (SIGCHLD, NULL, &sigchld_default_action);
4733
b84876c2
PA
4734 /* Block SIGCHLD by default. Doing this early prevents it getting
4735 unblocked if an exception is thrown due to an error while the
4736 inferior is starting (sigsetjmp/siglongjmp). */
4737 sigemptyset (&mask);
4738 sigaddset (&mask, SIGCHLD);
4739 sigprocmask (SIG_BLOCK, &mask, NULL);
4740
4741 /* Save this mask as the default. */
d6b0e80f
AC
4742 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
4743
b84876c2
PA
4744 /* The synchronous SIGCHLD handler. */
4745 sync_sigchld_action.sa_handler = sigchld_handler;
4746 sigemptyset (&sync_sigchld_action.sa_mask);
4747 sync_sigchld_action.sa_flags = SA_RESTART;
4748
4749 /* Make it the default. */
4750 sigaction (SIGCHLD, &sync_sigchld_action, NULL);
d6b0e80f
AC
4751
4752 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4753 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
4754 sigdelset (&suspend_mask, SIGCHLD);
4755
b84876c2
PA
4756 /* SIGCHLD handler for async mode. */
4757 async_sigchld_action.sa_handler = async_sigchld_handler;
4758 sigemptyset (&async_sigchld_action.sa_mask);
4759 async_sigchld_action.sa_flags = SA_RESTART;
d6b0e80f 4760
c6ebd6cf 4761 linux_nat_setup_async ();
10568435
JK
4762
4763 add_setshow_boolean_cmd ("disable-randomization", class_support,
4764 &disable_randomization, _("\
4765Set disabling of debuggee's virtual address space randomization."), _("\
4766Show disabling of debuggee's virtual address space randomization."), _("\
4767When this mode is on (which is the default), randomization of the virtual\n\
4768address space is disabled. Standalone programs run with the randomization\n\
4769enabled by default on some platforms."),
4770 &set_disable_randomization,
4771 &show_disable_randomization,
4772 &setlist, &showlist);
d6b0e80f
AC
4773}
4774\f
4775
4776/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4777 the GNU/Linux Threads library and therefore doesn't really belong
4778 here. */
4779
4780/* Read variable NAME in the target and return its value if found.
4781 Otherwise return zero. It is assumed that the type of the variable
4782 is `int'. */
4783
4784static int
4785get_signo (const char *name)
4786{
4787 struct minimal_symbol *ms;
4788 int signo;
4789
4790 ms = lookup_minimal_symbol (name, NULL, NULL);
4791 if (ms == NULL)
4792 return 0;
4793
8e70166d 4794 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
d6b0e80f
AC
4795 sizeof (signo)) != 0)
4796 return 0;
4797
4798 return signo;
4799}
4800
4801/* Return the set of signals used by the threads library in *SET. */
4802
4803void
4804lin_thread_get_thread_signals (sigset_t *set)
4805{
4806 struct sigaction action;
4807 int restart, cancel;
b84876c2 4808 sigset_t blocked_mask;
d6b0e80f 4809
b84876c2 4810 sigemptyset (&blocked_mask);
d6b0e80f
AC
4811 sigemptyset (set);
4812
4813 restart = get_signo ("__pthread_sig_restart");
17fbb0bd
DJ
4814 cancel = get_signo ("__pthread_sig_cancel");
4815
4816 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4817 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4818 not provide any way for the debugger to query the signal numbers -
4819 fortunately they don't change! */
4820
d6b0e80f 4821 if (restart == 0)
17fbb0bd 4822 restart = __SIGRTMIN;
d6b0e80f 4823
d6b0e80f 4824 if (cancel == 0)
17fbb0bd 4825 cancel = __SIGRTMIN + 1;
d6b0e80f
AC
4826
4827 sigaddset (set, restart);
4828 sigaddset (set, cancel);
4829
4830 /* The GNU/Linux Threads library makes terminating threads send a
4831 special "cancel" signal instead of SIGCHLD. Make sure we catch
4832 those (to prevent them from terminating GDB itself, which is
4833 likely to be their default action) and treat them the same way as
4834 SIGCHLD. */
4835
4836 action.sa_handler = sigchld_handler;
4837 sigemptyset (&action.sa_mask);
58aecb61 4838 action.sa_flags = SA_RESTART;
d6b0e80f
AC
4839 sigaction (cancel, &action, NULL);
4840
4841 /* We block the "cancel" signal throughout this code ... */
4842 sigaddset (&blocked_mask, cancel);
4843 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
4844
4845 /* ... except during a sigsuspend. */
4846 sigdelset (&suspend_mask, cancel);
4847}