* alphanbsd-tdep.c (_initialize_alphanbsd_tdep): Register OS/ABI
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
CommitLineData
3993f6b1 1/* GNU/Linux native-dependent code common to multiple platforms.
dba24537
AC
2
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3993f6b1
DJ
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "inferior.h"
24#include "target.h"
d6b0e80f 25#include "gdb_string.h"
3993f6b1 26#include "gdb_wait.h"
d6b0e80f
AC
27#include "gdb_assert.h"
28#ifdef HAVE_TKILL_SYSCALL
29#include <unistd.h>
30#include <sys/syscall.h>
31#endif
3993f6b1 32#include <sys/ptrace.h>
0274a8ce 33#include "linux-nat.h"
d6b0e80f
AC
34#include "gdbthread.h"
35#include "gdbcmd.h"
36#include "regcache.h"
dba24537
AC
37#include <sys/param.h> /* for MAXPATHLEN */
38#include <sys/procfs.h> /* for elf_gregset etc. */
39#include "elf-bfd.h" /* for elfcore_write_* */
40#include "gregset.h" /* for gregset */
41#include "gdbcore.h" /* for get_exec_file */
42#include <ctype.h> /* for isdigit */
43#include "gdbthread.h" /* for struct thread_info etc. */
44#include "gdb_stat.h" /* for struct stat */
45#include <fcntl.h> /* for O_RDONLY */
46
47#ifndef O_LARGEFILE
48#define O_LARGEFILE 0
49#endif
0274a8ce 50
3993f6b1
DJ
51/* If the system headers did not provide the constants, hard-code the normal
52 values. */
53#ifndef PTRACE_EVENT_FORK
54
55#define PTRACE_SETOPTIONS 0x4200
56#define PTRACE_GETEVENTMSG 0x4201
57
58/* options set using PTRACE_SETOPTIONS */
59#define PTRACE_O_TRACESYSGOOD 0x00000001
60#define PTRACE_O_TRACEFORK 0x00000002
61#define PTRACE_O_TRACEVFORK 0x00000004
62#define PTRACE_O_TRACECLONE 0x00000008
63#define PTRACE_O_TRACEEXEC 0x00000010
9016a515
DJ
64#define PTRACE_O_TRACEVFORKDONE 0x00000020
65#define PTRACE_O_TRACEEXIT 0x00000040
3993f6b1
DJ
66
67/* Wait extended result codes for the above trace options. */
68#define PTRACE_EVENT_FORK 1
69#define PTRACE_EVENT_VFORK 2
70#define PTRACE_EVENT_CLONE 3
71#define PTRACE_EVENT_EXEC 4
9016a515
DJ
72#define PTRACE_EVENT_VFORKDONE 5
73#define PTRACE_EVENT_EXIT 6
3993f6b1
DJ
74
75#endif /* PTRACE_EVENT_FORK */
76
77/* We can't always assume that this flag is available, but all systems
78 with the ptrace event handlers also have __WALL, so it's safe to use
79 here. */
80#ifndef __WALL
81#define __WALL 0x40000000 /* Wait for any child. */
82#endif
83
d6b0e80f
AC
84static int debug_linux_nat;
85
4de4c07c
DJ
86extern struct target_ops child_ops;
87
9016a515
DJ
88static int linux_parent_pid;
89
ae087d01
DJ
90struct simple_pid_list
91{
92 int pid;
93 struct simple_pid_list *next;
94};
95struct simple_pid_list *stopped_pids;
96
3993f6b1
DJ
97/* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
98 can not be used, 1 if it can. */
99
100static int linux_supports_tracefork_flag = -1;
101
9016a515
DJ
102/* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
103 PTRACE_O_TRACEVFORKDONE. */
104
105static int linux_supports_tracevforkdone_flag = -1;
106
ae087d01
DJ
107\f
108/* Trivial list manipulation functions to keep track of a list of
109 new stopped processes. */
110static void
111add_to_pid_list (struct simple_pid_list **listp, int pid)
112{
113 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
114 new_pid->pid = pid;
115 new_pid->next = *listp;
116 *listp = new_pid;
117}
118
119static int
120pull_pid_from_list (struct simple_pid_list **listp, int pid)
121{
122 struct simple_pid_list **p;
123
124 for (p = listp; *p != NULL; p = &(*p)->next)
125 if ((*p)->pid == pid)
126 {
127 struct simple_pid_list *next = (*p)->next;
128 xfree (*p);
129 *p = next;
130 return 1;
131 }
132 return 0;
133}
134
135void
136linux_record_stopped_pid (int pid)
137{
138 add_to_pid_list (&stopped_pids, pid);
139}
140
3993f6b1
DJ
141\f
142/* A helper function for linux_test_for_tracefork, called after fork (). */
143
144static void
145linux_tracefork_child (void)
146{
147 int ret;
148
149 ptrace (PTRACE_TRACEME, 0, 0, 0);
150 kill (getpid (), SIGSTOP);
151 fork ();
152 exit (0);
153}
154
155/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
156 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
157 fork tracing, and let it fork. If the process exits, we assume that
158 we can't use TRACEFORK; if we get the fork notification, and we can
159 extract the new child's PID, then we assume that we can. */
160
161static void
162linux_test_for_tracefork (void)
163{
164 int child_pid, ret, status;
165 long second_pid;
166
167 child_pid = fork ();
168 if (child_pid == -1)
169 perror_with_name ("linux_test_for_tracefork: fork");
170
171 if (child_pid == 0)
172 linux_tracefork_child ();
173
174 ret = waitpid (child_pid, &status, 0);
175 if (ret == -1)
176 perror_with_name ("linux_test_for_tracefork: waitpid");
177 else if (ret != child_pid)
178 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
179 if (! WIFSTOPPED (status))
180 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
181
182 linux_supports_tracefork_flag = 0;
183
184 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
185 if (ret != 0)
186 {
187 ptrace (PTRACE_KILL, child_pid, 0, 0);
188 waitpid (child_pid, &status, 0);
189 return;
190 }
191
9016a515
DJ
192 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
193 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
194 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
195 linux_supports_tracevforkdone_flag = (ret == 0);
196
3993f6b1
DJ
197 ptrace (PTRACE_CONT, child_pid, 0, 0);
198 ret = waitpid (child_pid, &status, 0);
199 if (ret == child_pid && WIFSTOPPED (status)
200 && status >> 16 == PTRACE_EVENT_FORK)
201 {
202 second_pid = 0;
203 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
204 if (ret == 0 && second_pid != 0)
205 {
206 int second_status;
207
208 linux_supports_tracefork_flag = 1;
209 waitpid (second_pid, &second_status, 0);
210 ptrace (PTRACE_DETACH, second_pid, 0, 0);
211 }
212 }
213
214 if (WIFSTOPPED (status))
215 {
216 ptrace (PTRACE_DETACH, child_pid, 0, 0);
217 waitpid (child_pid, &status, 0);
218 }
219}
220
221/* Return non-zero iff we have tracefork functionality available.
222 This function also sets linux_supports_tracefork_flag. */
223
224static int
225linux_supports_tracefork (void)
226{
227 if (linux_supports_tracefork_flag == -1)
228 linux_test_for_tracefork ();
229 return linux_supports_tracefork_flag;
230}
231
9016a515
DJ
232static int
233linux_supports_tracevforkdone (void)
234{
235 if (linux_supports_tracefork_flag == -1)
236 linux_test_for_tracefork ();
237 return linux_supports_tracevforkdone_flag;
238}
239
3993f6b1 240\f
4de4c07c
DJ
241void
242linux_enable_event_reporting (ptid_t ptid)
243{
244 int pid = ptid_get_pid (ptid);
245 int options;
246
247 if (! linux_supports_tracefork ())
248 return;
249
a2f23071
DJ
250 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
251 | PTRACE_O_TRACECLONE;
9016a515
DJ
252 if (linux_supports_tracevforkdone ())
253 options |= PTRACE_O_TRACEVFORKDONE;
254
255 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
256 read-only process state. */
4de4c07c
DJ
257
258 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
259}
260
261void
262child_post_attach (int pid)
263{
264 linux_enable_event_reporting (pid_to_ptid (pid));
265}
266
267void
268linux_child_post_startup_inferior (ptid_t ptid)
269{
270 linux_enable_event_reporting (ptid);
271}
272
273#ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
274void
275child_post_startup_inferior (ptid_t ptid)
276{
277 linux_child_post_startup_inferior (ptid);
278}
279#endif
280
3993f6b1 281int
4de4c07c 282child_follow_fork (int follow_child)
3993f6b1 283{
4de4c07c
DJ
284 ptid_t last_ptid;
285 struct target_waitstatus last_status;
9016a515 286 int has_vforked;
4de4c07c
DJ
287 int parent_pid, child_pid;
288
289 get_last_target_status (&last_ptid, &last_status);
9016a515 290 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
4de4c07c
DJ
291 parent_pid = ptid_get_pid (last_ptid);
292 child_pid = last_status.value.related_pid;
293
294 if (! follow_child)
295 {
296 /* We're already attached to the parent, by default. */
297
298 /* Before detaching from the child, remove all breakpoints from
299 it. (This won't actually modify the breakpoint list, but will
300 physically remove the breakpoints from the child.) */
9016a515
DJ
301 /* If we vforked this will remove the breakpoints from the parent
302 also, but they'll be reinserted below. */
4de4c07c
DJ
303 detach_breakpoints (child_pid);
304
305 fprintf_filtered (gdb_stdout,
306 "Detaching after fork from child process %d.\n",
307 child_pid);
308
309 ptrace (PTRACE_DETACH, child_pid, 0, 0);
9016a515
DJ
310
311 if (has_vforked)
312 {
313 if (linux_supports_tracevforkdone ())
314 {
315 int status;
316
317 ptrace (PTRACE_CONT, parent_pid, 0, 0);
318 waitpid (parent_pid, &status, __WALL);
319 if ((status >> 16) != PTRACE_EVENT_VFORKDONE)
320 warning ("Unexpected waitpid result %06x when waiting for "
321 "vfork-done", status);
322 }
323 else
324 {
325 /* We can't insert breakpoints until the child has
326 finished with the shared memory region. We need to
327 wait until that happens. Ideal would be to just
328 call:
329 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
330 - waitpid (parent_pid, &status, __WALL);
331 However, most architectures can't handle a syscall
332 being traced on the way out if it wasn't traced on
333 the way in.
334
335 We might also think to loop, continuing the child
336 until it exits or gets a SIGTRAP. One problem is
337 that the child might call ptrace with PTRACE_TRACEME.
338
339 There's no simple and reliable way to figure out when
340 the vforked child will be done with its copy of the
341 shared memory. We could step it out of the syscall,
342 two instructions, let it go, and then single-step the
343 parent once. When we have hardware single-step, this
344 would work; with software single-step it could still
345 be made to work but we'd have to be able to insert
346 single-step breakpoints in the child, and we'd have
347 to insert -just- the single-step breakpoint in the
348 parent. Very awkward.
349
350 In the end, the best we can do is to make sure it
351 runs for a little while. Hopefully it will be out of
352 range of any breakpoints we reinsert. Usually this
353 is only the single-step breakpoint at vfork's return
354 point. */
355
356 usleep (10000);
357 }
358
359 /* Since we vforked, breakpoints were removed in the parent
360 too. Put them back. */
361 reattach_breakpoints (parent_pid);
362 }
4de4c07c 363 }
3993f6b1 364 else
4de4c07c
DJ
365 {
366 char child_pid_spelling[40];
367
368 /* Needed to keep the breakpoint lists in sync. */
9016a515
DJ
369 if (! has_vforked)
370 detach_breakpoints (child_pid);
4de4c07c
DJ
371
372 /* Before detaching from the parent, remove all breakpoints from it. */
373 remove_breakpoints ();
374
375 fprintf_filtered (gdb_stdout,
376 "Attaching after fork to child process %d.\n",
377 child_pid);
378
9016a515
DJ
379 /* If we're vforking, we may want to hold on to the parent until
380 the child exits or execs. At exec time we can remove the old
381 breakpoints from the parent and detach it; at exit time we
382 could do the same (or even, sneakily, resume debugging it - the
383 child's exec has failed, or something similar).
384
385 This doesn't clean up "properly", because we can't call
386 target_detach, but that's OK; if the current target is "child",
387 then it doesn't need any further cleanups, and lin_lwp will
388 generally not encounter vfork (vfork is defined to fork
389 in libpthread.so).
390
391 The holding part is very easy if we have VFORKDONE events;
392 but keeping track of both processes is beyond GDB at the
393 moment. So we don't expose the parent to the rest of GDB.
394 Instead we quietly hold onto it until such time as we can
395 safely resume it. */
396
397 if (has_vforked)
398 linux_parent_pid = parent_pid;
399 else
400 target_detach (NULL, 0);
4de4c07c
DJ
401
402 inferior_ptid = pid_to_ptid (child_pid);
403 push_target (&child_ops);
404
405 /* Reset breakpoints in the child as appropriate. */
406 follow_inferior_reset_breakpoints ();
407 }
408
409 return 0;
410}
411
412ptid_t
413linux_handle_extended_wait (int pid, int status,
414 struct target_waitstatus *ourstatus)
415{
416 int event = status >> 16;
417
a2f23071
DJ
418 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
419 || event == PTRACE_EVENT_CLONE)
4de4c07c
DJ
420 {
421 unsigned long new_pid;
422 int ret;
423
424 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
425
426 /* If we haven't already seen the new PID stop, wait for it now. */
427 if (! pull_pid_from_list (&stopped_pids, new_pid))
428 {
429 /* The new child has a pending SIGSTOP. We can't affect it until it
a2f23071 430 hits the SIGSTOP, but we're already attached. */
4de4c07c 431 do {
a2f23071
DJ
432 ret = waitpid (new_pid, &status,
433 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
4de4c07c
DJ
434 } while (ret == -1 && errno == EINTR);
435 if (ret == -1)
436 perror_with_name ("waiting for new child");
437 else if (ret != new_pid)
438 internal_error (__FILE__, __LINE__,
439 "wait returned unexpected PID %d", ret);
440 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
441 internal_error (__FILE__, __LINE__,
442 "wait returned unexpected status 0x%x", status);
443 }
444
a2f23071
DJ
445 if (event == PTRACE_EVENT_FORK)
446 ourstatus->kind = TARGET_WAITKIND_FORKED;
447 else if (event == PTRACE_EVENT_VFORK)
448 ourstatus->kind = TARGET_WAITKIND_VFORKED;
449 else
450 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
451
4de4c07c
DJ
452 ourstatus->value.related_pid = new_pid;
453 return inferior_ptid;
454 }
455
9016a515
DJ
456 if (event == PTRACE_EVENT_EXEC)
457 {
458 ourstatus->kind = TARGET_WAITKIND_EXECD;
459 ourstatus->value.execd_pathname
460 = xstrdup (child_pid_to_exec_file (pid));
461
462 if (linux_parent_pid)
463 {
464 detach_breakpoints (linux_parent_pid);
465 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
466
467 linux_parent_pid = 0;
468 }
469
470 return inferior_ptid;
471 }
472
4de4c07c
DJ
473 internal_error (__FILE__, __LINE__,
474 "unknown ptrace event %d", event);
475}
476
477\f
478int
479child_insert_fork_catchpoint (int pid)
480{
481 if (! linux_supports_tracefork ())
3993f6b1 482 error ("Your system does not support fork catchpoints.");
4de4c07c
DJ
483
484 return 0;
3993f6b1
DJ
485}
486
487int
488child_insert_vfork_catchpoint (int pid)
489{
9016a515 490 if (!linux_supports_tracefork ())
3993f6b1 491 error ("Your system does not support vfork catchpoints.");
9016a515
DJ
492
493 return 0;
3993f6b1
DJ
494}
495
496int
497child_insert_exec_catchpoint (int pid)
498{
9016a515 499 if (!linux_supports_tracefork ())
3993f6b1 500 error ("Your system does not support exec catchpoints.");
9016a515
DJ
501
502 return 0;
3993f6b1
DJ
503}
504
4de4c07c
DJ
505void
506kill_inferior (void)
507{
508 int status;
509 int pid = PIDGET (inferior_ptid);
510 struct target_waitstatus last;
511 ptid_t last_ptid;
512 int ret;
513
514 if (pid == 0)
515 return;
516
517 /* If we're stopped while forking and we haven't followed yet, kill the
518 other task. We need to do this first because the parent will be
519 sleeping if this is a vfork. */
520
521 get_last_target_status (&last_ptid, &last);
3993f6b1 522
4de4c07c
DJ
523 if (last.kind == TARGET_WAITKIND_FORKED
524 || last.kind == TARGET_WAITKIND_VFORKED)
525 {
de9a9e51 526 ptrace (PT_KILL, last.value.related_pid, 0, 0);
4de4c07c
DJ
527 ptrace_wait (null_ptid, &status);
528 }
529
530 /* Kill the current process. */
de9a9e51 531 ptrace (PT_KILL, pid, 0, 0);
4de4c07c
DJ
532 ret = ptrace_wait (null_ptid, &status);
533
534 /* We might get a SIGCHLD instead of an exit status. This is
535 aggravated by the first kill above - a child has just died. */
536
537 while (ret == pid && WIFSTOPPED (status))
538 {
de9a9e51 539 ptrace (PT_KILL, pid, 0, 0);
4de4c07c
DJ
540 ret = ptrace_wait (null_ptid, &status);
541 }
542
543 target_mourn_inferior ();
544}
d6b0e80f
AC
545
546/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
547 are processes sharing the same VM space. A multi-threaded process
548 is basically a group of such processes. However, such a grouping
549 is almost entirely a user-space issue; the kernel doesn't enforce
550 such a grouping at all (this might change in the future). In
551 general, we'll rely on the threads library (i.e. the GNU/Linux
552 Threads library) to provide such a grouping.
553
554 It is perfectly well possible to write a multi-threaded application
555 without the assistance of a threads library, by using the clone
556 system call directly. This module should be able to give some
557 rudimentary support for debugging such applications if developers
558 specify the CLONE_PTRACE flag in the clone system call, and are
559 using the Linux kernel 2.4 or above.
560
561 Note that there are some peculiarities in GNU/Linux that affect
562 this code:
563
564 - In general one should specify the __WCLONE flag to waitpid in
565 order to make it report events for any of the cloned processes
566 (and leave it out for the initial process). However, if a cloned
567 process has exited the exit status is only reported if the
568 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
569 we cannot use it since GDB must work on older systems too.
570
571 - When a traced, cloned process exits and is waited for by the
572 debugger, the kernel reassigns it to the original parent and
573 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
574 library doesn't notice this, which leads to the "zombie problem":
575 When debugged a multi-threaded process that spawns a lot of
576 threads will run out of processes, even if the threads exit,
577 because the "zombies" stay around. */
578
579/* List of known LWPs. */
580static struct lwp_info *lwp_list;
581
582/* Number of LWPs in the list. */
583static int num_lwps;
584
585/* Non-zero if we're running in "threaded" mode. */
586static int threaded;
587\f
588
589#define GET_LWP(ptid) ptid_get_lwp (ptid)
590#define GET_PID(ptid) ptid_get_pid (ptid)
591#define is_lwp(ptid) (GET_LWP (ptid) != 0)
592#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
593
594/* If the last reported event was a SIGTRAP, this variable is set to
595 the process id of the LWP/thread that got it. */
596ptid_t trap_ptid;
597\f
598
599/* This module's target-specific operations. */
600static struct target_ops linux_nat_ops;
601
602/* The standard child operations. */
603extern struct target_ops child_ops;
604
605/* Since we cannot wait (in linux_nat_wait) for the initial process and
606 any cloned processes with a single call to waitpid, we have to use
607 the WNOHANG flag and call waitpid in a loop. To optimize
608 things a bit we use `sigsuspend' to wake us up when a process has
609 something to report (it will send us a SIGCHLD if it has). To make
610 this work we have to juggle with the signal mask. We save the
611 original signal mask such that we can restore it before creating a
612 new process in order to avoid blocking certain signals in the
613 inferior. We then block SIGCHLD during the waitpid/sigsuspend
614 loop. */
615
616/* Original signal mask. */
617static sigset_t normal_mask;
618
619/* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
620 _initialize_linux_nat. */
621static sigset_t suspend_mask;
622
623/* Signals to block to make that sigsuspend work. */
624static sigset_t blocked_mask;
625\f
626
627/* Prototypes for local functions. */
628static int stop_wait_callback (struct lwp_info *lp, void *data);
629static int linux_nat_thread_alive (ptid_t ptid);
630\f
631/* Convert wait status STATUS to a string. Used for printing debug
632 messages only. */
633
634static char *
635status_to_str (int status)
636{
637 static char buf[64];
638
639 if (WIFSTOPPED (status))
640 snprintf (buf, sizeof (buf), "%s (stopped)",
641 strsignal (WSTOPSIG (status)));
642 else if (WIFSIGNALED (status))
643 snprintf (buf, sizeof (buf), "%s (terminated)",
644 strsignal (WSTOPSIG (status)));
645 else
646 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
647
648 return buf;
649}
650
651/* Initialize the list of LWPs. Note that this module, contrary to
652 what GDB's generic threads layer does for its thread list,
653 re-initializes the LWP lists whenever we mourn or detach (which
654 doesn't involve mourning) the inferior. */
655
656static void
657init_lwp_list (void)
658{
659 struct lwp_info *lp, *lpnext;
660
661 for (lp = lwp_list; lp; lp = lpnext)
662 {
663 lpnext = lp->next;
664 xfree (lp);
665 }
666
667 lwp_list = NULL;
668 num_lwps = 0;
669 threaded = 0;
670}
671
672/* Add the LWP specified by PID to the list. If this causes the
673 number of LWPs to become larger than one, go into "threaded" mode.
674 Return a pointer to the structure describing the new LWP. */
675
676static struct lwp_info *
677add_lwp (ptid_t ptid)
678{
679 struct lwp_info *lp;
680
681 gdb_assert (is_lwp (ptid));
682
683 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
684
685 memset (lp, 0, sizeof (struct lwp_info));
686
687 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
688
689 lp->ptid = ptid;
690
691 lp->next = lwp_list;
692 lwp_list = lp;
693 if (++num_lwps > 1)
694 threaded = 1;
695
696 return lp;
697}
698
699/* Remove the LWP specified by PID from the list. */
700
701static void
702delete_lwp (ptid_t ptid)
703{
704 struct lwp_info *lp, *lpprev;
705
706 lpprev = NULL;
707
708 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
709 if (ptid_equal (lp->ptid, ptid))
710 break;
711
712 if (!lp)
713 return;
714
715 /* We don't go back to "non-threaded" mode if the number of threads
716 becomes less than two. */
717 num_lwps--;
718
719 if (lpprev)
720 lpprev->next = lp->next;
721 else
722 lwp_list = lp->next;
723
724 xfree (lp);
725}
726
727/* Return a pointer to the structure describing the LWP corresponding
728 to PID. If no corresponding LWP could be found, return NULL. */
729
730static struct lwp_info *
731find_lwp_pid (ptid_t ptid)
732{
733 struct lwp_info *lp;
734 int lwp;
735
736 if (is_lwp (ptid))
737 lwp = GET_LWP (ptid);
738 else
739 lwp = GET_PID (ptid);
740
741 for (lp = lwp_list; lp; lp = lp->next)
742 if (lwp == GET_LWP (lp->ptid))
743 return lp;
744
745 return NULL;
746}
747
748/* Call CALLBACK with its second argument set to DATA for every LWP in
749 the list. If CALLBACK returns 1 for a particular LWP, return a
750 pointer to the structure describing that LWP immediately.
751 Otherwise return NULL. */
752
753struct lwp_info *
754iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
755{
756 struct lwp_info *lp, *lpnext;
757
758 for (lp = lwp_list; lp; lp = lpnext)
759 {
760 lpnext = lp->next;
761 if ((*callback) (lp, data))
762 return lp;
763 }
764
765 return NULL;
766}
767
768/* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
769 a message telling the user that a new LWP has been added to the
770 process. */
771
772void
773lin_lwp_attach_lwp (ptid_t ptid, int verbose)
774{
775 struct lwp_info *lp, *found_lp;
776
777 gdb_assert (is_lwp (ptid));
778
779 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
780 to interrupt either the ptrace() or waitpid() calls below. */
781 if (!sigismember (&blocked_mask, SIGCHLD))
782 {
783 sigaddset (&blocked_mask, SIGCHLD);
784 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
785 }
786
787 if (verbose)
788 printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
789
790 found_lp = lp = find_lwp_pid (ptid);
791 if (lp == NULL)
792 lp = add_lwp (ptid);
793
794 /* We assume that we're already attached to any LWP that has an id
795 equal to the overall process id, and to any LWP that is already
796 in our list of LWPs. If we're not seeing exit events from threads
797 and we've had PID wraparound since we last tried to stop all threads,
798 this assumption might be wrong; fortunately, this is very unlikely
799 to happen. */
800 if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL)
801 {
802 pid_t pid;
803 int status;
804
805 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
806 error ("Can't attach %s: %s", target_pid_to_str (ptid),
807 safe_strerror (errno));
808
809 if (debug_linux_nat)
810 fprintf_unfiltered (gdb_stdlog,
811 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
812 target_pid_to_str (ptid));
813
814 pid = waitpid (GET_LWP (ptid), &status, 0);
815 if (pid == -1 && errno == ECHILD)
816 {
817 /* Try again with __WCLONE to check cloned processes. */
818 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
819 lp->cloned = 1;
820 }
821
822 gdb_assert (pid == GET_LWP (ptid)
823 && WIFSTOPPED (status) && WSTOPSIG (status));
824
825 child_post_attach (pid);
826
827 lp->stopped = 1;
828
829 if (debug_linux_nat)
830 {
831 fprintf_unfiltered (gdb_stdlog,
832 "LLAL: waitpid %s received %s\n",
833 target_pid_to_str (ptid),
834 status_to_str (status));
835 }
836 }
837 else
838 {
839 /* We assume that the LWP representing the original process is
840 already stopped. Mark it as stopped in the data structure
841 that the linux ptrace layer uses to keep track of threads.
842 Note that this won't have already been done since the main
843 thread will have, we assume, been stopped by an attach from a
844 different layer. */
845 lp->stopped = 1;
846 }
847}
848
849static void
850linux_nat_attach (char *args, int from_tty)
851{
852 struct lwp_info *lp;
853 pid_t pid;
854 int status;
855
856 /* FIXME: We should probably accept a list of process id's, and
857 attach all of them. */
858 child_ops.to_attach (args, from_tty);
859
860 /* Add the initial process as the first LWP to the list. */
861 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
862
863 /* Make sure the initial process is stopped. The user-level threads
864 layer might want to poke around in the inferior, and that won't
865 work if things haven't stabilized yet. */
866 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
867 if (pid == -1 && errno == ECHILD)
868 {
869 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
870
871 /* Try again with __WCLONE to check cloned processes. */
872 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
873 lp->cloned = 1;
874 }
875
876 gdb_assert (pid == GET_PID (inferior_ptid)
877 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
878
879 lp->stopped = 1;
880
881 /* Fake the SIGSTOP that core GDB expects. */
882 lp->status = W_STOPCODE (SIGSTOP);
883 lp->resumed = 1;
884 if (debug_linux_nat)
885 {
886 fprintf_unfiltered (gdb_stdlog,
887 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
888 }
889}
890
891static int
892detach_callback (struct lwp_info *lp, void *data)
893{
894 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
895
896 if (debug_linux_nat && lp->status)
897 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
898 strsignal (WSTOPSIG (lp->status)),
899 target_pid_to_str (lp->ptid));
900
901 while (lp->signalled && lp->stopped)
902 {
903 errno = 0;
904 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
905 WSTOPSIG (lp->status)) < 0)
906 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
907 safe_strerror (errno));
908
909 if (debug_linux_nat)
910 fprintf_unfiltered (gdb_stdlog,
911 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
912 target_pid_to_str (lp->ptid),
913 status_to_str (lp->status));
914
915 lp->stopped = 0;
916 lp->signalled = 0;
917 lp->status = 0;
918 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
919 here. But since lp->signalled was cleared above,
920 stop_wait_callback didn't do anything; the process was left
921 running. Shouldn't we be waiting for it to stop?
922 I've removed the call, since stop_wait_callback now does do
923 something when called with lp->signalled == 0. */
924
925 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
926 }
927
928 /* We don't actually detach from the LWP that has an id equal to the
929 overall process id just yet. */
930 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
931 {
932 errno = 0;
933 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
934 WSTOPSIG (lp->status)) < 0)
935 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
936 safe_strerror (errno));
937
938 if (debug_linux_nat)
939 fprintf_unfiltered (gdb_stdlog,
940 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
941 target_pid_to_str (lp->ptid),
942 strsignal (WSTOPSIG (lp->status)));
943
944 delete_lwp (lp->ptid);
945 }
946
947 return 0;
948}
949
950static void
951linux_nat_detach (char *args, int from_tty)
952{
953 iterate_over_lwps (detach_callback, NULL);
954
955 /* Only the initial process should be left right now. */
956 gdb_assert (num_lwps == 1);
957
958 trap_ptid = null_ptid;
959
960 /* Destroy LWP info; it's no longer valid. */
961 init_lwp_list ();
962
963 /* Restore the original signal mask. */
964 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
965 sigemptyset (&blocked_mask);
966
967 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
968 child_ops.to_detach (args, from_tty);
969}
970
971/* Resume LP. */
972
973static int
974resume_callback (struct lwp_info *lp, void *data)
975{
976 if (lp->stopped && lp->status == 0)
977 {
978 struct thread_info *tp;
979
980 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
981 if (debug_linux_nat)
982 fprintf_unfiltered (gdb_stdlog,
983 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
984 target_pid_to_str (lp->ptid));
985 lp->stopped = 0;
986 lp->step = 0;
987 }
988
989 return 0;
990}
991
992static int
993resume_clear_callback (struct lwp_info *lp, void *data)
994{
995 lp->resumed = 0;
996 return 0;
997}
998
999static int
1000resume_set_callback (struct lwp_info *lp, void *data)
1001{
1002 lp->resumed = 1;
1003 return 0;
1004}
1005
1006static void
1007linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
1008{
1009 struct lwp_info *lp;
1010 int resume_all;
1011
1012 /* A specific PTID means `step only this process id'. */
1013 resume_all = (PIDGET (ptid) == -1);
1014
1015 if (resume_all)
1016 iterate_over_lwps (resume_set_callback, NULL);
1017 else
1018 iterate_over_lwps (resume_clear_callback, NULL);
1019
1020 /* If PID is -1, it's the current inferior that should be
1021 handled specially. */
1022 if (PIDGET (ptid) == -1)
1023 ptid = inferior_ptid;
1024
1025 lp = find_lwp_pid (ptid);
1026 if (lp)
1027 {
1028 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1029
1030 /* Remember if we're stepping. */
1031 lp->step = step;
1032
1033 /* Mark this LWP as resumed. */
1034 lp->resumed = 1;
1035
1036 /* If we have a pending wait status for this thread, there is no
1037 point in resuming the process. */
1038 if (lp->status)
1039 {
1040 /* FIXME: What should we do if we are supposed to continue
1041 this thread with a signal? */
1042 gdb_assert (signo == TARGET_SIGNAL_0);
1043 return;
1044 }
1045
1046 /* Mark LWP as not stopped to prevent it from being continued by
1047 resume_callback. */
1048 lp->stopped = 0;
1049 }
1050
1051 if (resume_all)
1052 iterate_over_lwps (resume_callback, NULL);
1053
1054 child_resume (ptid, step, signo);
1055 if (debug_linux_nat)
1056 fprintf_unfiltered (gdb_stdlog,
1057 "LLR: %s %s, %s (resume event thread)\n",
1058 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1059 target_pid_to_str (ptid),
1060 signo ? strsignal (signo) : "0");
1061}
1062
1063/* Issue kill to specified lwp. */
1064
1065static int tkill_failed;
1066
1067static int
1068kill_lwp (int lwpid, int signo)
1069{
1070 errno = 0;
1071
1072/* Use tkill, if possible, in case we are using nptl threads. If tkill
1073 fails, then we are not using nptl threads and we should be using kill. */
1074
1075#ifdef HAVE_TKILL_SYSCALL
1076 if (!tkill_failed)
1077 {
1078 int ret = syscall (__NR_tkill, lwpid, signo);
1079 if (errno != ENOSYS)
1080 return ret;
1081 errno = 0;
1082 tkill_failed = 1;
1083 }
1084#endif
1085
1086 return kill (lwpid, signo);
1087}
1088
1089/* Handle a GNU/Linux extended wait response. Most of the work we
1090 just pass off to linux_handle_extended_wait, but if it reports a
1091 clone event we need to add the new LWP to our list (and not report
1092 the trap to higher layers). This function returns non-zero if
1093 the event should be ignored and we should wait again. */
1094
1095static int
1096linux_nat_handle_extended (struct lwp_info *lp, int status)
1097{
1098 linux_handle_extended_wait (GET_LWP (lp->ptid), status,
1099 &lp->waitstatus);
1100
1101 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1102 if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS)
1103 {
1104 struct lwp_info *new_lp;
1105 new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid,
1106 GET_PID (inferior_ptid)));
1107 new_lp->cloned = 1;
1108 new_lp->stopped = 1;
1109
1110 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1111
1112 if (debug_linux_nat)
1113 fprintf_unfiltered (gdb_stdlog,
1114 "LLHE: Got clone event from LWP %ld, resuming\n",
1115 GET_LWP (lp->ptid));
1116 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1117
1118 return 1;
1119 }
1120
1121 return 0;
1122}
1123
1124/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1125 exited. */
1126
1127static int
1128wait_lwp (struct lwp_info *lp)
1129{
1130 pid_t pid;
1131 int status;
1132 int thread_dead = 0;
1133
1134 gdb_assert (!lp->stopped);
1135 gdb_assert (lp->status == 0);
1136
1137 pid = waitpid (GET_LWP (lp->ptid), &status, 0);
1138 if (pid == -1 && errno == ECHILD)
1139 {
1140 pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
1141 if (pid == -1 && errno == ECHILD)
1142 {
1143 /* The thread has previously exited. We need to delete it
1144 now because, for some vendor 2.4 kernels with NPTL
1145 support backported, there won't be an exit event unless
1146 it is the main thread. 2.6 kernels will report an exit
1147 event for each thread that exits, as expected. */
1148 thread_dead = 1;
1149 if (debug_linux_nat)
1150 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
1151 target_pid_to_str (lp->ptid));
1152 }
1153 }
1154
1155 if (!thread_dead)
1156 {
1157 gdb_assert (pid == GET_LWP (lp->ptid));
1158
1159 if (debug_linux_nat)
1160 {
1161 fprintf_unfiltered (gdb_stdlog,
1162 "WL: waitpid %s received %s\n",
1163 target_pid_to_str (lp->ptid),
1164 status_to_str (status));
1165 }
1166 }
1167
1168 /* Check if the thread has exited. */
1169 if (WIFEXITED (status) || WIFSIGNALED (status))
1170 {
1171 thread_dead = 1;
1172 if (debug_linux_nat)
1173 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
1174 target_pid_to_str (lp->ptid));
1175 }
1176
1177 if (thread_dead)
1178 {
1179 if (in_thread_list (lp->ptid))
1180 {
1181 /* Core GDB cannot deal with us deleting the current thread. */
1182 if (!ptid_equal (lp->ptid, inferior_ptid))
1183 delete_thread (lp->ptid);
1184 printf_unfiltered ("[%s exited]\n",
1185 target_pid_to_str (lp->ptid));
1186 }
1187
1188 delete_lwp (lp->ptid);
1189 return 0;
1190 }
1191
1192 gdb_assert (WIFSTOPPED (status));
1193
1194 /* Handle GNU/Linux's extended waitstatus for trace events. */
1195 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1196 {
1197 if (debug_linux_nat)
1198 fprintf_unfiltered (gdb_stdlog,
1199 "WL: Handling extended status 0x%06x\n",
1200 status);
1201 if (linux_nat_handle_extended (lp, status))
1202 return wait_lwp (lp);
1203 }
1204
1205 return status;
1206}
1207
1208/* Send a SIGSTOP to LP. */
1209
1210static int
1211stop_callback (struct lwp_info *lp, void *data)
1212{
1213 if (!lp->stopped && !lp->signalled)
1214 {
1215 int ret;
1216
1217 if (debug_linux_nat)
1218 {
1219 fprintf_unfiltered (gdb_stdlog,
1220 "SC: kill %s **<SIGSTOP>**\n",
1221 target_pid_to_str (lp->ptid));
1222 }
1223 errno = 0;
1224 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
1225 if (debug_linux_nat)
1226 {
1227 fprintf_unfiltered (gdb_stdlog,
1228 "SC: lwp kill %d %s\n",
1229 ret,
1230 errno ? safe_strerror (errno) : "ERRNO-OK");
1231 }
1232
1233 lp->signalled = 1;
1234 gdb_assert (lp->status == 0);
1235 }
1236
1237 return 0;
1238}
1239
1240/* Wait until LP is stopped. If DATA is non-null it is interpreted as
1241 a pointer to a set of signals to be flushed immediately. */
1242
1243static int
1244stop_wait_callback (struct lwp_info *lp, void *data)
1245{
1246 sigset_t *flush_mask = data;
1247
1248 if (!lp->stopped)
1249 {
1250 int status;
1251
1252 status = wait_lwp (lp);
1253 if (status == 0)
1254 return 0;
1255
1256 /* Ignore any signals in FLUSH_MASK. */
1257 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
1258 {
1259 if (!lp->signalled)
1260 {
1261 lp->stopped = 1;
1262 return 0;
1263 }
1264
1265 errno = 0;
1266 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1267 if (debug_linux_nat)
1268 fprintf_unfiltered (gdb_stdlog,
1269 "PTRACE_CONT %s, 0, 0 (%s)\n",
1270 target_pid_to_str (lp->ptid),
1271 errno ? safe_strerror (errno) : "OK");
1272
1273 return stop_wait_callback (lp, flush_mask);
1274 }
1275
1276 if (WSTOPSIG (status) != SIGSTOP)
1277 {
1278 if (WSTOPSIG (status) == SIGTRAP)
1279 {
1280 /* If a LWP other than the LWP that we're reporting an
1281 event for has hit a GDB breakpoint (as opposed to
1282 some random trap signal), then just arrange for it to
1283 hit it again later. We don't keep the SIGTRAP status
1284 and don't forward the SIGTRAP signal to the LWP. We
1285 will handle the current event, eventually we will
1286 resume all LWPs, and this one will get its breakpoint
1287 trap again.
1288
1289 If we do not do this, then we run the risk that the
1290 user will delete or disable the breakpoint, but the
1291 thread will have already tripped on it. */
1292
1293 /* Now resume this LWP and get the SIGSTOP event. */
1294 errno = 0;
1295 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1296 if (debug_linux_nat)
1297 {
1298 fprintf_unfiltered (gdb_stdlog,
1299 "PTRACE_CONT %s, 0, 0 (%s)\n",
1300 target_pid_to_str (lp->ptid),
1301 errno ? safe_strerror (errno) : "OK");
1302
1303 fprintf_unfiltered (gdb_stdlog,
1304 "SWC: Candidate SIGTRAP event in %s\n",
1305 target_pid_to_str (lp->ptid));
1306 }
1307 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1308 stop_wait_callback (lp, data);
1309 /* If there's another event, throw it back into the queue. */
1310 if (lp->status)
1311 {
1312 if (debug_linux_nat)
1313 {
1314 fprintf_unfiltered (gdb_stdlog,
1315 "SWC: kill %s, %s\n",
1316 target_pid_to_str (lp->ptid),
1317 status_to_str ((int) status));
1318 }
1319 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
1320 }
1321 /* Save the sigtrap event. */
1322 lp->status = status;
1323 return 0;
1324 }
1325 else
1326 {
1327 /* The thread was stopped with a signal other than
1328 SIGSTOP, and didn't accidentally trip a breakpoint. */
1329
1330 if (debug_linux_nat)
1331 {
1332 fprintf_unfiltered (gdb_stdlog,
1333 "SWC: Pending event %s in %s\n",
1334 status_to_str ((int) status),
1335 target_pid_to_str (lp->ptid));
1336 }
1337 /* Now resume this LWP and get the SIGSTOP event. */
1338 errno = 0;
1339 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1340 if (debug_linux_nat)
1341 fprintf_unfiltered (gdb_stdlog,
1342 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1343 target_pid_to_str (lp->ptid),
1344 errno ? safe_strerror (errno) : "OK");
1345
1346 /* Hold this event/waitstatus while we check to see if
1347 there are any more (we still want to get that SIGSTOP). */
1348 stop_wait_callback (lp, data);
1349 /* If the lp->status field is still empty, use it to hold
1350 this event. If not, then this event must be returned
1351 to the event queue of the LWP. */
1352 if (lp->status == 0)
1353 lp->status = status;
1354 else
1355 {
1356 if (debug_linux_nat)
1357 {
1358 fprintf_unfiltered (gdb_stdlog,
1359 "SWC: kill %s, %s\n",
1360 target_pid_to_str (lp->ptid),
1361 status_to_str ((int) status));
1362 }
1363 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
1364 }
1365 return 0;
1366 }
1367 }
1368 else
1369 {
1370 /* We caught the SIGSTOP that we intended to catch, so
1371 there's no SIGSTOP pending. */
1372 lp->stopped = 1;
1373 lp->signalled = 0;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
1380/* Check whether PID has any pending signals in FLUSH_MASK. If so set
1381 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1382
1383static int
1384linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
1385{
1386 sigset_t blocked, ignored;
1387 int i;
1388
1389 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
1390
1391 if (!flush_mask)
1392 return 0;
1393
1394 for (i = 1; i < NSIG; i++)
1395 if (sigismember (pending, i))
1396 if (!sigismember (flush_mask, i)
1397 || sigismember (&blocked, i)
1398 || sigismember (&ignored, i))
1399 sigdelset (pending, i);
1400
1401 if (sigisemptyset (pending))
1402 return 0;
1403
1404 return 1;
1405}
1406
1407/* DATA is interpreted as a mask of signals to flush. If LP has
1408 signals pending, and they are all in the flush mask, then arrange
1409 to flush them. LP should be stopped, as should all other threads
1410 it might share a signal queue with. */
1411
1412static int
1413flush_callback (struct lwp_info *lp, void *data)
1414{
1415 sigset_t *flush_mask = data;
1416 sigset_t pending, intersection, blocked, ignored;
1417 int pid, status;
1418
1419 /* Normally, when an LWP exits, it is removed from the LWP list. The
1420 last LWP isn't removed till later, however. So if there is only
1421 one LWP on the list, make sure it's alive. */
1422 if (lwp_list == lp && lp->next == NULL)
1423 if (!linux_nat_thread_alive (lp->ptid))
1424 return 0;
1425
1426 /* Just because the LWP is stopped doesn't mean that new signals
1427 can't arrive from outside, so this function must be careful of
1428 race conditions. However, because all threads are stopped, we
1429 can assume that the pending mask will not shrink unless we resume
1430 the LWP, and that it will then get another signal. We can't
1431 control which one, however. */
1432
1433 if (lp->status)
1434 {
1435 if (debug_linux_nat)
1436 printf_unfiltered ("FC: LP has pending status %06x\n", lp->status);
1437 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
1438 lp->status = 0;
1439 }
1440
1441 while (linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
1442 {
1443 int ret;
1444
1445 errno = 0;
1446 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1447 if (debug_linux_nat)
1448 fprintf_unfiltered (gdb_stderr,
1449 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
1450
1451 lp->stopped = 0;
1452 stop_wait_callback (lp, flush_mask);
1453 if (debug_linux_nat)
1454 fprintf_unfiltered (gdb_stderr,
1455 "FC: Wait finished; saved status is %d\n",
1456 lp->status);
1457 }
1458
1459 return 0;
1460}
1461
1462/* Return non-zero if LP has a wait status pending. */
1463
1464static int
1465status_callback (struct lwp_info *lp, void *data)
1466{
1467 /* Only report a pending wait status if we pretend that this has
1468 indeed been resumed. */
1469 return (lp->status != 0 && lp->resumed);
1470}
1471
1472/* Return non-zero if LP isn't stopped. */
1473
1474static int
1475running_callback (struct lwp_info *lp, void *data)
1476{
1477 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
1478}
1479
1480/* Count the LWP's that have had events. */
1481
1482static int
1483count_events_callback (struct lwp_info *lp, void *data)
1484{
1485 int *count = data;
1486
1487 gdb_assert (count != NULL);
1488
1489 /* Count only LWPs that have a SIGTRAP event pending. */
1490 if (lp->status != 0
1491 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1492 (*count)++;
1493
1494 return 0;
1495}
1496
1497/* Select the LWP (if any) that is currently being single-stepped. */
1498
1499static int
1500select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1501{
1502 if (lp->step && lp->status != 0)
1503 return 1;
1504 else
1505 return 0;
1506}
1507
1508/* Select the Nth LWP that has had a SIGTRAP event. */
1509
1510static int
1511select_event_lwp_callback (struct lwp_info *lp, void *data)
1512{
1513 int *selector = data;
1514
1515 gdb_assert (selector != NULL);
1516
1517 /* Select only LWPs that have a SIGTRAP event pending. */
1518 if (lp->status != 0
1519 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1520 if ((*selector)-- == 0)
1521 return 1;
1522
1523 return 0;
1524}
1525
1526static int
1527cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1528{
1529 struct lwp_info *event_lp = data;
1530
1531 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1532 if (lp == event_lp)
1533 return 0;
1534
1535 /* If a LWP other than the LWP that we're reporting an event for has
1536 hit a GDB breakpoint (as opposed to some random trap signal),
1537 then just arrange for it to hit it again later. We don't keep
1538 the SIGTRAP status and don't forward the SIGTRAP signal to the
1539 LWP. We will handle the current event, eventually we will resume
1540 all LWPs, and this one will get its breakpoint trap again.
1541
1542 If we do not do this, then we run the risk that the user will
1543 delete or disable the breakpoint, but the LWP will have already
1544 tripped on it. */
1545
1546 if (lp->status != 0
1547 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1548 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1549 DECR_PC_AFTER_BREAK))
1550 {
1551 if (debug_linux_nat)
1552 fprintf_unfiltered (gdb_stdlog,
1553 "CBC: Push back breakpoint for %s\n",
1554 target_pid_to_str (lp->ptid));
1555
1556 /* Back up the PC if necessary. */
1557 if (DECR_PC_AFTER_BREAK)
1558 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1559
1560 /* Throw away the SIGTRAP. */
1561 lp->status = 0;
1562 }
1563
1564 return 0;
1565}
1566
1567/* Select one LWP out of those that have events pending. */
1568
1569static void
1570select_event_lwp (struct lwp_info **orig_lp, int *status)
1571{
1572 int num_events = 0;
1573 int random_selector;
1574 struct lwp_info *event_lp;
1575
1576 /* Record the wait status for the origional LWP. */
1577 (*orig_lp)->status = *status;
1578
1579 /* Give preference to any LWP that is being single-stepped. */
1580 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1581 if (event_lp != NULL)
1582 {
1583 if (debug_linux_nat)
1584 fprintf_unfiltered (gdb_stdlog,
1585 "SEL: Select single-step %s\n",
1586 target_pid_to_str (event_lp->ptid));
1587 }
1588 else
1589 {
1590 /* No single-stepping LWP. Select one at random, out of those
1591 which have had SIGTRAP events. */
1592
1593 /* First see how many SIGTRAP events we have. */
1594 iterate_over_lwps (count_events_callback, &num_events);
1595
1596 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1597 random_selector = (int)
1598 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1599
1600 if (debug_linux_nat && num_events > 1)
1601 fprintf_unfiltered (gdb_stdlog,
1602 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1603 num_events, random_selector);
1604
1605 event_lp = iterate_over_lwps (select_event_lwp_callback,
1606 &random_selector);
1607 }
1608
1609 if (event_lp != NULL)
1610 {
1611 /* Switch the event LWP. */
1612 *orig_lp = event_lp;
1613 *status = event_lp->status;
1614 }
1615
1616 /* Flush the wait status for the event LWP. */
1617 (*orig_lp)->status = 0;
1618}
1619
1620/* Return non-zero if LP has been resumed. */
1621
1622static int
1623resumed_callback (struct lwp_info *lp, void *data)
1624{
1625 return lp->resumed;
1626}
1627
1628#ifdef CHILD_WAIT
1629
1630/* We need to override child_wait to support attaching to cloned
1631 processes, since a normal wait (as done by the default version)
1632 ignores those processes. */
1633
1634/* Wait for child PTID to do something. Return id of the child,
1635 minus_one_ptid in case of error; store status into *OURSTATUS. */
1636
1637ptid_t
1638child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1639{
1640 int save_errno;
1641 int status;
1642 pid_t pid;
1643
1644 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1645
1646 do
1647 {
1648 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1649 attached process. */
1650 set_sigio_trap ();
1651
1652 pid = waitpid (GET_PID (ptid), &status, 0);
1653 if (pid == -1 && errno == ECHILD)
1654 /* Try again with __WCLONE to check cloned processes. */
1655 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1656
1657 if (debug_linux_nat)
1658 {
1659 fprintf_unfiltered (gdb_stdlog,
1660 "CW: waitpid %ld received %s\n",
1661 (long) pid, status_to_str (status));
1662 }
1663
1664 save_errno = errno;
1665
1666 /* Make sure we don't report an event for the exit of the
1667 original program, if we've detached from it. */
1668 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1669 {
1670 pid = -1;
1671 save_errno = EINTR;
1672 }
1673
1674 /* Check for stop events reported by a process we didn't already
1675 know about - in this case, anything other than inferior_ptid.
1676
1677 If we're expecting to receive stopped processes after fork,
1678 vfork, and clone events, then we'll just add the new one to
1679 our list and go back to waiting for the event to be reported
1680 - the stopped process might be returned from waitpid before
1681 or after the event is. If we want to handle debugging of
1682 CLONE_PTRACE processes we need to do more here, i.e. switch
1683 to multi-threaded mode. */
1684 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1685 && pid != GET_PID (inferior_ptid))
1686 {
1687 linux_record_stopped_pid (pid);
1688 pid = -1;
1689 save_errno = EINTR;
1690 }
1691
1692 /* Handle GNU/Linux's extended waitstatus for trace events. */
1693 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
1694 && status >> 16 != 0)
1695 {
1696 linux_handle_extended_wait (pid, status, ourstatus);
1697
1698 /* If we see a clone event, detach the child, and don't
1699 report the event. It would be nice to offer some way to
1700 switch into a non-thread-db based threaded mode at this
1701 point. */
1702 if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS)
1703 {
1704 ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0);
1705 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1706 ptrace (PTRACE_CONT, pid, 0, 0);
1707 pid = -1;
1708 save_errno = EINTR;
1709 }
1710 }
1711
1712 clear_sigio_trap ();
1713 clear_sigint_trap ();
1714 }
1715 while (pid == -1 && save_errno == EINTR);
1716
1717 if (pid == -1)
1718 {
1719 warning ("Child process unexpectedly missing: %s",
1720 safe_strerror (errno));
1721
1722 /* Claim it exited with unknown signal. */
1723 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1724 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1725 return minus_one_ptid;
1726 }
1727
1728 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1729 store_waitstatus (ourstatus, status);
1730
1731 return pid_to_ptid (pid);
1732}
1733
1734#endif
1735
1736/* Stop an active thread, verify it still exists, then resume it. */
1737
1738static int
1739stop_and_resume_callback (struct lwp_info *lp, void *data)
1740{
1741 struct lwp_info *ptr;
1742
1743 if (!lp->stopped && !lp->signalled)
1744 {
1745 stop_callback (lp, NULL);
1746 stop_wait_callback (lp, NULL);
1747 /* Resume if the lwp still exists. */
1748 for (ptr = lwp_list; ptr; ptr = ptr->next)
1749 if (lp == ptr)
1750 {
1751 resume_callback (lp, NULL);
1752 resume_set_callback (lp, NULL);
1753 }
1754 }
1755 return 0;
1756}
1757
1758static ptid_t
1759linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1760{
1761 struct lwp_info *lp = NULL;
1762 int options = 0;
1763 int status = 0;
1764 pid_t pid = PIDGET (ptid);
1765 sigset_t flush_mask;
1766
1767 sigemptyset (&flush_mask);
1768
1769 /* Make sure SIGCHLD is blocked. */
1770 if (!sigismember (&blocked_mask, SIGCHLD))
1771 {
1772 sigaddset (&blocked_mask, SIGCHLD);
1773 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1774 }
1775
1776retry:
1777
1778 /* Make sure there is at least one LWP that has been resumed, at
1779 least if there are any LWPs at all. */
1780 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
1781
1782 /* First check if there is a LWP with a wait status pending. */
1783 if (pid == -1)
1784 {
1785 /* Any LWP that's been resumed will do. */
1786 lp = iterate_over_lwps (status_callback, NULL);
1787 if (lp)
1788 {
1789 status = lp->status;
1790 lp->status = 0;
1791
1792 if (debug_linux_nat && status)
1793 fprintf_unfiltered (gdb_stdlog,
1794 "LLW: Using pending wait status %s for %s.\n",
1795 status_to_str (status),
1796 target_pid_to_str (lp->ptid));
1797 }
1798
1799 /* But if we don't fine one, we'll have to wait, and check both
1800 cloned and uncloned processes. We start with the cloned
1801 processes. */
1802 options = __WCLONE | WNOHANG;
1803 }
1804 else if (is_lwp (ptid))
1805 {
1806 if (debug_linux_nat)
1807 fprintf_unfiltered (gdb_stdlog,
1808 "LLW: Waiting for specific LWP %s.\n",
1809 target_pid_to_str (ptid));
1810
1811 /* We have a specific LWP to check. */
1812 lp = find_lwp_pid (ptid);
1813 gdb_assert (lp);
1814 status = lp->status;
1815 lp->status = 0;
1816
1817 if (debug_linux_nat && status)
1818 fprintf_unfiltered (gdb_stdlog,
1819 "LLW: Using pending wait status %s for %s.\n",
1820 status_to_str (status),
1821 target_pid_to_str (lp->ptid));
1822
1823 /* If we have to wait, take into account whether PID is a cloned
1824 process or not. And we have to convert it to something that
1825 the layer beneath us can understand. */
1826 options = lp->cloned ? __WCLONE : 0;
1827 pid = GET_LWP (ptid);
1828 }
1829
1830 if (status && lp->signalled)
1831 {
1832 /* A pending SIGSTOP may interfere with the normal stream of
1833 events. In a typical case where interference is a problem,
1834 we have a SIGSTOP signal pending for LWP A while
1835 single-stepping it, encounter an event in LWP B, and take the
1836 pending SIGSTOP while trying to stop LWP A. After processing
1837 the event in LWP B, LWP A is continued, and we'll never see
1838 the SIGTRAP associated with the last time we were
1839 single-stepping LWP A. */
1840
1841 /* Resume the thread. It should halt immediately returning the
1842 pending SIGSTOP. */
1843 registers_changed ();
1844 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1845 TARGET_SIGNAL_0);
1846 if (debug_linux_nat)
1847 fprintf_unfiltered (gdb_stdlog,
1848 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1849 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1850 target_pid_to_str (lp->ptid));
1851 lp->stopped = 0;
1852 gdb_assert (lp->resumed);
1853
1854 /* This should catch the pending SIGSTOP. */
1855 stop_wait_callback (lp, NULL);
1856 }
1857
1858 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1859 attached process. */
1860 set_sigio_trap ();
1861
1862 while (status == 0)
1863 {
1864 pid_t lwpid;
1865
1866 lwpid = waitpid (pid, &status, options);
1867 if (lwpid > 0)
1868 {
1869 gdb_assert (pid == -1 || lwpid == pid);
1870
1871 if (debug_linux_nat)
1872 {
1873 fprintf_unfiltered (gdb_stdlog,
1874 "LLW: waitpid %ld received %s\n",
1875 (long) lwpid, status_to_str (status));
1876 }
1877
1878 lp = find_lwp_pid (pid_to_ptid (lwpid));
1879
1880 /* Check for stop events reported by a process we didn't
1881 already know about - anything not already in our LWP
1882 list.
1883
1884 If we're expecting to receive stopped processes after
1885 fork, vfork, and clone events, then we'll just add the
1886 new one to our list and go back to waiting for the event
1887 to be reported - the stopped process might be returned
1888 from waitpid before or after the event is. */
1889 if (WIFSTOPPED (status) && !lp)
1890 {
1891 linux_record_stopped_pid (lwpid);
1892 status = 0;
1893 continue;
1894 }
1895
1896 /* Make sure we don't report an event for the exit of an LWP not in
1897 our list, i.e. not part of the current process. This can happen
1898 if we detach from a program we original forked and then it
1899 exits. */
1900 if (!WIFSTOPPED (status) && !lp)
1901 {
1902 status = 0;
1903 continue;
1904 }
1905
1906 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1907 CLONE_PTRACE processes which do not use the thread library -
1908 otherwise we wouldn't find the new LWP this way. That doesn't
1909 currently work, and the following code is currently unreachable
1910 due to the two blocks above. If it's fixed some day, this code
1911 should be broken out into a function so that we can also pick up
1912 LWPs from the new interface. */
1913 if (!lp)
1914 {
1915 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
1916 if (options & __WCLONE)
1917 lp->cloned = 1;
1918
1919 if (threaded)
1920 {
1921 gdb_assert (WIFSTOPPED (status)
1922 && WSTOPSIG (status) == SIGSTOP);
1923 lp->signalled = 1;
1924
1925 if (!in_thread_list (inferior_ptid))
1926 {
1927 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1928 GET_PID (inferior_ptid));
1929 add_thread (inferior_ptid);
1930 }
1931
1932 add_thread (lp->ptid);
1933 printf_unfiltered ("[New %s]\n",
1934 target_pid_to_str (lp->ptid));
1935 }
1936 }
1937
1938 /* Handle GNU/Linux's extended waitstatus for trace events. */
1939 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1940 {
1941 if (debug_linux_nat)
1942 fprintf_unfiltered (gdb_stdlog,
1943 "LLW: Handling extended status 0x%06x\n",
1944 status);
1945 if (linux_nat_handle_extended (lp, status))
1946 {
1947 status = 0;
1948 continue;
1949 }
1950 }
1951
1952 /* Check if the thread has exited. */
1953 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1954 {
1955 if (in_thread_list (lp->ptid))
1956 {
1957 /* Core GDB cannot deal with us deleting the current
1958 thread. */
1959 if (!ptid_equal (lp->ptid, inferior_ptid))
1960 delete_thread (lp->ptid);
1961 printf_unfiltered ("[%s exited]\n",
1962 target_pid_to_str (lp->ptid));
1963 }
1964
1965 /* If this is the main thread, we must stop all threads and
1966 verify if they are still alive. This is because in the nptl
1967 thread model, there is no signal issued for exiting LWPs
1968 other than the main thread. We only get the main thread
1969 exit signal once all child threads have already exited.
1970 If we stop all the threads and use the stop_wait_callback
1971 to check if they have exited we can determine whether this
1972 signal should be ignored or whether it means the end of the
1973 debugged application, regardless of which threading model
1974 is being used. */
1975 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
1976 {
1977 lp->stopped = 1;
1978 iterate_over_lwps (stop_and_resume_callback, NULL);
1979 }
1980
1981 if (debug_linux_nat)
1982 fprintf_unfiltered (gdb_stdlog,
1983 "LLW: %s exited.\n",
1984 target_pid_to_str (lp->ptid));
1985
1986 delete_lwp (lp->ptid);
1987
1988 /* If there is at least one more LWP, then the exit signal
1989 was not the end of the debugged application and should be
1990 ignored. */
1991 if (num_lwps > 0)
1992 {
1993 /* Make sure there is at least one thread running. */
1994 gdb_assert (iterate_over_lwps (running_callback, NULL));
1995
1996 /* Discard the event. */
1997 status = 0;
1998 continue;
1999 }
2000 }
2001
2002 /* Check if the current LWP has previously exited. In the nptl
2003 thread model, LWPs other than the main thread do not issue
2004 signals when they exit so we must check whenever the thread
2005 has stopped. A similar check is made in stop_wait_callback(). */
2006 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
2007 {
2008 if (in_thread_list (lp->ptid))
2009 {
2010 /* Core GDB cannot deal with us deleting the current
2011 thread. */
2012 if (!ptid_equal (lp->ptid, inferior_ptid))
2013 delete_thread (lp->ptid);
2014 printf_unfiltered ("[%s exited]\n",
2015 target_pid_to_str (lp->ptid));
2016 }
2017 if (debug_linux_nat)
2018 fprintf_unfiltered (gdb_stdlog,
2019 "LLW: %s exited.\n",
2020 target_pid_to_str (lp->ptid));
2021
2022 delete_lwp (lp->ptid);
2023
2024 /* Make sure there is at least one thread running. */
2025 gdb_assert (iterate_over_lwps (running_callback, NULL));
2026
2027 /* Discard the event. */
2028 status = 0;
2029 continue;
2030 }
2031
2032 /* Make sure we don't report a SIGSTOP that we sent
2033 ourselves in an attempt to stop an LWP. */
2034 if (lp->signalled
2035 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2036 {
2037 if (debug_linux_nat)
2038 fprintf_unfiltered (gdb_stdlog,
2039 "LLW: Delayed SIGSTOP caught for %s.\n",
2040 target_pid_to_str (lp->ptid));
2041
2042 /* This is a delayed SIGSTOP. */
2043 lp->signalled = 0;
2044
2045 registers_changed ();
2046 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
2047 TARGET_SIGNAL_0);
2048 if (debug_linux_nat)
2049 fprintf_unfiltered (gdb_stdlog,
2050 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2051 lp->step ?
2052 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2053 target_pid_to_str (lp->ptid));
2054
2055 lp->stopped = 0;
2056 gdb_assert (lp->resumed);
2057
2058 /* Discard the event. */
2059 status = 0;
2060 continue;
2061 }
2062
2063 break;
2064 }
2065
2066 if (pid == -1)
2067 {
2068 /* Alternate between checking cloned and uncloned processes. */
2069 options ^= __WCLONE;
2070
2071 /* And suspend every time we have checked both. */
2072 if (options & __WCLONE)
2073 sigsuspend (&suspend_mask);
2074 }
2075
2076 /* We shouldn't end up here unless we want to try again. */
2077 gdb_assert (status == 0);
2078 }
2079
2080 clear_sigio_trap ();
2081 clear_sigint_trap ();
2082
2083 gdb_assert (lp);
2084
2085 /* Don't report signals that GDB isn't interested in, such as
2086 signals that are neither printed nor stopped upon. Stopping all
2087 threads can be a bit time-consuming so if we want decent
2088 performance with heavily multi-threaded programs, especially when
2089 they're using a high frequency timer, we'd better avoid it if we
2090 can. */
2091
2092 if (WIFSTOPPED (status))
2093 {
2094 int signo = target_signal_from_host (WSTOPSIG (status));
2095
2096 if (signal_stop_state (signo) == 0
2097 && signal_print_state (signo) == 0
2098 && signal_pass_state (signo) == 1)
2099 {
2100 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2101 here? It is not clear we should. GDB may not expect
2102 other threads to run. On the other hand, not resuming
2103 newly attached threads may cause an unwanted delay in
2104 getting them running. */
2105 registers_changed ();
2106 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
2107 if (debug_linux_nat)
2108 fprintf_unfiltered (gdb_stdlog,
2109 "LLW: %s %s, %s (preempt 'handle')\n",
2110 lp->step ?
2111 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2112 target_pid_to_str (lp->ptid),
2113 signo ? strsignal (signo) : "0");
2114 lp->stopped = 0;
2115 status = 0;
2116 goto retry;
2117 }
2118
2119 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
2120 {
2121 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2122 forwarded to the entire process group, that is, all LWP's
2123 will receive it. Since we only want to report it once,
2124 we try to flush it from all LWPs except this one. */
2125 sigaddset (&flush_mask, SIGINT);
2126 }
2127 }
2128
2129 /* This LWP is stopped now. */
2130 lp->stopped = 1;
2131
2132 if (debug_linux_nat)
2133 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
2134 status_to_str (status), target_pid_to_str (lp->ptid));
2135
2136 /* Now stop all other LWP's ... */
2137 iterate_over_lwps (stop_callback, NULL);
2138
2139 /* ... and wait until all of them have reported back that they're no
2140 longer running. */
2141 iterate_over_lwps (stop_wait_callback, &flush_mask);
2142 iterate_over_lwps (flush_callback, &flush_mask);
2143
2144 /* If we're not waiting for a specific LWP, choose an event LWP from
2145 among those that have had events. Giving equal priority to all
2146 LWPs that have had events helps prevent starvation. */
2147 if (pid == -1)
2148 select_event_lwp (&lp, &status);
2149
2150 /* Now that we've selected our final event LWP, cancel any
2151 breakpoints in other LWPs that have hit a GDB breakpoint. See
2152 the comment in cancel_breakpoints_callback to find out why. */
2153 iterate_over_lwps (cancel_breakpoints_callback, lp);
2154
2155 /* If we're not running in "threaded" mode, we'll report the bare
2156 process id. */
2157
2158 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2159 {
2160 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2161 if (debug_linux_nat)
2162 fprintf_unfiltered (gdb_stdlog,
2163 "LLW: trap_ptid is %s.\n",
2164 target_pid_to_str (trap_ptid));
2165 }
2166 else
2167 trap_ptid = null_ptid;
2168
2169 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2170 {
2171 *ourstatus = lp->waitstatus;
2172 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
2173 }
2174 else
2175 store_waitstatus (ourstatus, status);
2176
2177 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2178}
2179
2180static int
2181kill_callback (struct lwp_info *lp, void *data)
2182{
2183 errno = 0;
2184 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
2185 if (debug_linux_nat)
2186 fprintf_unfiltered (gdb_stdlog,
2187 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2188 target_pid_to_str (lp->ptid),
2189 errno ? safe_strerror (errno) : "OK");
2190
2191 return 0;
2192}
2193
2194static int
2195kill_wait_callback (struct lwp_info *lp, void *data)
2196{
2197 pid_t pid;
2198
2199 /* We must make sure that there are no pending events (delayed
2200 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2201 program doesn't interfere with any following debugging session. */
2202
2203 /* For cloned processes we must check both with __WCLONE and
2204 without, since the exit status of a cloned process isn't reported
2205 with __WCLONE. */
2206 if (lp->cloned)
2207 {
2208 do
2209 {
2210 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
2211 if (pid != (pid_t) -1 && debug_linux_nat)
2212 {
2213 fprintf_unfiltered (gdb_stdlog,
2214 "KWC: wait %s received unknown.\n",
2215 target_pid_to_str (lp->ptid));
2216 }
2217 }
2218 while (pid == GET_LWP (lp->ptid));
2219
2220 gdb_assert (pid == -1 && errno == ECHILD);
2221 }
2222
2223 do
2224 {
2225 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
2226 if (pid != (pid_t) -1 && debug_linux_nat)
2227 {
2228 fprintf_unfiltered (gdb_stdlog,
2229 "KWC: wait %s received unk.\n",
2230 target_pid_to_str (lp->ptid));
2231 }
2232 }
2233 while (pid == GET_LWP (lp->ptid));
2234
2235 gdb_assert (pid == -1 && errno == ECHILD);
2236 return 0;
2237}
2238
2239static void
2240linux_nat_kill (void)
2241{
2242 /* Kill all LWP's ... */
2243 iterate_over_lwps (kill_callback, NULL);
2244
2245 /* ... and wait until we've flushed all events. */
2246 iterate_over_lwps (kill_wait_callback, NULL);
2247
2248 target_mourn_inferior ();
2249}
2250
2251static void
2252linux_nat_create_inferior (char *exec_file, char *allargs, char **env,
2253 int from_tty)
2254{
2255 child_ops.to_create_inferior (exec_file, allargs, env, from_tty);
2256}
2257
2258static void
2259linux_nat_mourn_inferior (void)
2260{
2261 trap_ptid = null_ptid;
2262
2263 /* Destroy LWP info; it's no longer valid. */
2264 init_lwp_list ();
2265
2266 /* Restore the original signal mask. */
2267 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
2268 sigemptyset (&blocked_mask);
2269
2270 child_ops.to_mourn_inferior ();
2271}
2272
2273static int
2274linux_nat_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2275 struct mem_attrib *attrib, struct target_ops *target)
2276{
2277 struct cleanup *old_chain = save_inferior_ptid ();
2278 int xfer;
2279
2280 if (is_lwp (inferior_ptid))
2281 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
2282
2283 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2284 if (xfer == 0)
2285 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
2286
2287 do_cleanups (old_chain);
2288 return xfer;
2289}
2290
2291static int
2292linux_nat_thread_alive (ptid_t ptid)
2293{
2294 gdb_assert (is_lwp (ptid));
2295
2296 errno = 0;
2297 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
2298 if (debug_linux_nat)
2299 fprintf_unfiltered (gdb_stdlog,
2300 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2301 target_pid_to_str (ptid),
2302 errno ? safe_strerror (errno) : "OK");
2303 if (errno)
2304 return 0;
2305
2306 return 1;
2307}
2308
2309static char *
2310linux_nat_pid_to_str (ptid_t ptid)
2311{
2312 static char buf[64];
2313
2314 if (is_lwp (ptid))
2315 {
2316 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
2317 return buf;
2318 }
2319
2320 return normal_pid_to_str (ptid);
2321}
2322
2323static void
2324init_linux_nat_ops (void)
2325{
2326#if 0
2327 linux_nat_ops.to_open = linux_nat_open;
2328#endif
2329 linux_nat_ops.to_shortname = "lwp-layer";
2330 linux_nat_ops.to_longname = "lwp-layer";
2331 linux_nat_ops.to_doc = "Low level threads support (LWP layer)";
2332 linux_nat_ops.to_attach = linux_nat_attach;
2333 linux_nat_ops.to_detach = linux_nat_detach;
2334 linux_nat_ops.to_resume = linux_nat_resume;
2335 linux_nat_ops.to_wait = linux_nat_wait;
2336 /* fetch_inferior_registers and store_inferior_registers will
2337 honor the LWP id, so we can use them directly. */
2338 linux_nat_ops.to_fetch_registers = fetch_inferior_registers;
2339 linux_nat_ops.to_store_registers = store_inferior_registers;
2340 linux_nat_ops.to_xfer_memory = linux_nat_xfer_memory;
2341 linux_nat_ops.to_kill = linux_nat_kill;
2342 linux_nat_ops.to_create_inferior = linux_nat_create_inferior;
2343 linux_nat_ops.to_mourn_inferior = linux_nat_mourn_inferior;
2344 linux_nat_ops.to_thread_alive = linux_nat_thread_alive;
2345 linux_nat_ops.to_pid_to_str = linux_nat_pid_to_str;
2346 linux_nat_ops.to_post_startup_inferior = child_post_startup_inferior;
2347 linux_nat_ops.to_post_attach = child_post_attach;
2348 linux_nat_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
2349 linux_nat_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
2350 linux_nat_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
2351
2352 linux_nat_ops.to_stratum = thread_stratum;
2353 linux_nat_ops.to_has_thread_control = tc_schedlock;
2354 linux_nat_ops.to_magic = OPS_MAGIC;
2355}
2356
2357static void
2358sigchld_handler (int signo)
2359{
2360 /* Do nothing. The only reason for this handler is that it allows
2361 us to use sigsuspend in linux_nat_wait above to wait for the
2362 arrival of a SIGCHLD. */
2363}
2364
dba24537
AC
2365/* Accepts an integer PID; Returns a string representing a file that
2366 can be opened to get the symbols for the child process. */
2367
2368char *
2369child_pid_to_exec_file (int pid)
2370{
2371 char *name1, *name2;
2372
2373 name1 = xmalloc (MAXPATHLEN);
2374 name2 = xmalloc (MAXPATHLEN);
2375 make_cleanup (xfree, name1);
2376 make_cleanup (xfree, name2);
2377 memset (name2, 0, MAXPATHLEN);
2378
2379 sprintf (name1, "/proc/%d/exe", pid);
2380 if (readlink (name1, name2, MAXPATHLEN) > 0)
2381 return name2;
2382 else
2383 return name1;
2384}
2385
2386/* Service function for corefiles and info proc. */
2387
2388static int
2389read_mapping (FILE *mapfile,
2390 long long *addr,
2391 long long *endaddr,
2392 char *permissions,
2393 long long *offset,
2394 char *device, long long *inode, char *filename)
2395{
2396 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
2397 addr, endaddr, permissions, offset, device, inode);
2398
2399 if (ret > 0 && ret != EOF && *inode != 0)
2400 {
2401 /* Eat everything up to EOL for the filename. This will prevent
2402 weird filenames (such as one with embedded whitespace) from
2403 confusing this code. It also makes this code more robust in
2404 respect to annotations the kernel may add after the filename.
2405
2406 Note the filename is used for informational purposes
2407 only. */
2408 ret += fscanf (mapfile, "%[^\n]\n", filename);
2409 }
2410 else
2411 {
2412 filename[0] = '\0'; /* no filename */
2413 fscanf (mapfile, "\n");
2414 }
2415 return (ret != 0 && ret != EOF);
2416}
2417
2418/* Fills the "to_find_memory_regions" target vector. Lists the memory
2419 regions in the inferior for a corefile. */
2420
2421static int
2422linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
2423 unsigned long,
2424 int, int, int, void *), void *obfd)
2425{
2426 long long pid = PIDGET (inferior_ptid);
2427 char mapsfilename[MAXPATHLEN];
2428 FILE *mapsfile;
2429 long long addr, endaddr, size, offset, inode;
2430 char permissions[8], device[8], filename[MAXPATHLEN];
2431 int read, write, exec;
2432 int ret;
2433
2434 /* Compose the filename for the /proc memory map, and open it. */
2435 sprintf (mapsfilename, "/proc/%lld/maps", pid);
2436 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
2437 error ("Could not open %s\n", mapsfilename);
2438
2439 if (info_verbose)
2440 fprintf_filtered (gdb_stdout,
2441 "Reading memory regions from %s\n", mapsfilename);
2442
2443 /* Now iterate until end-of-file. */
2444 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
2445 &offset, &device[0], &inode, &filename[0]))
2446 {
2447 size = endaddr - addr;
2448
2449 /* Get the segment's permissions. */
2450 read = (strchr (permissions, 'r') != 0);
2451 write = (strchr (permissions, 'w') != 0);
2452 exec = (strchr (permissions, 'x') != 0);
2453
2454 if (info_verbose)
2455 {
2456 fprintf_filtered (gdb_stdout,
2457 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2458 size, paddr_nz (addr),
2459 read ? 'r' : ' ',
2460 write ? 'w' : ' ', exec ? 'x' : ' ');
2461 if (filename && filename[0])
2462 fprintf_filtered (gdb_stdout, " for %s", filename);
2463 fprintf_filtered (gdb_stdout, "\n");
2464 }
2465
2466 /* Invoke the callback function to create the corefile
2467 segment. */
2468 func (addr, size, read, write, exec, obfd);
2469 }
2470 fclose (mapsfile);
2471 return 0;
2472}
2473
2474/* Records the thread's register state for the corefile note
2475 section. */
2476
2477static char *
2478linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
2479 char *note_data, int *note_size)
2480{
2481 gdb_gregset_t gregs;
2482 gdb_fpregset_t fpregs;
2483#ifdef FILL_FPXREGSET
2484 gdb_fpxregset_t fpxregs;
2485#endif
2486 unsigned long lwp = ptid_get_lwp (ptid);
2487
2488 fill_gregset (&gregs, -1);
2489 note_data = (char *) elfcore_write_prstatus (obfd,
2490 note_data,
2491 note_size,
2492 lwp,
2493 stop_signal, &gregs);
2494
2495 fill_fpregset (&fpregs, -1);
2496 note_data = (char *) elfcore_write_prfpreg (obfd,
2497 note_data,
2498 note_size,
2499 &fpregs, sizeof (fpregs));
2500#ifdef FILL_FPXREGSET
2501 fill_fpxregset (&fpxregs, -1);
2502 note_data = (char *) elfcore_write_prxfpreg (obfd,
2503 note_data,
2504 note_size,
2505 &fpxregs, sizeof (fpxregs));
2506#endif
2507 return note_data;
2508}
2509
2510struct linux_nat_corefile_thread_data
2511{
2512 bfd *obfd;
2513 char *note_data;
2514 int *note_size;
2515 int num_notes;
2516};
2517
2518/* Called by gdbthread.c once per thread. Records the thread's
2519 register state for the corefile note section. */
2520
2521static int
2522linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
2523{
2524 struct linux_nat_corefile_thread_data *args = data;
2525 ptid_t saved_ptid = inferior_ptid;
2526
2527 inferior_ptid = ti->ptid;
2528 registers_changed ();
2529 target_fetch_registers (-1); /* FIXME should not be necessary;
2530 fill_gregset should do it automatically. */
2531 args->note_data = linux_nat_do_thread_registers (args->obfd,
2532 ti->ptid,
2533 args->note_data,
2534 args->note_size);
2535 args->num_notes++;
2536 inferior_ptid = saved_ptid;
2537 registers_changed ();
2538 target_fetch_registers (-1); /* FIXME should not be necessary;
2539 fill_gregset should do it automatically. */
2540 return 0;
2541}
2542
2543/* Records the register state for the corefile note section. */
2544
2545static char *
2546linux_nat_do_registers (bfd *obfd, ptid_t ptid,
2547 char *note_data, int *note_size)
2548{
2549 registers_changed ();
2550 target_fetch_registers (-1); /* FIXME should not be necessary;
2551 fill_gregset should do it automatically. */
2552 return linux_nat_do_thread_registers (obfd,
2553 ptid_build (ptid_get_pid (inferior_ptid),
2554 ptid_get_pid (inferior_ptid),
2555 0),
2556 note_data, note_size);
2557 return note_data;
2558}
2559
2560/* Fills the "to_make_corefile_note" target vector. Builds the note
2561 section for a corefile, and returns it in a malloc buffer. */
2562
2563static char *
2564linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
2565{
2566 struct linux_nat_corefile_thread_data thread_args;
2567 struct cleanup *old_chain;
2568 char fname[16] = { '\0' };
2569 char psargs[80] = { '\0' };
2570 char *note_data = NULL;
2571 ptid_t current_ptid = inferior_ptid;
2572 char *auxv;
2573 int auxv_len;
2574
2575 if (get_exec_file (0))
2576 {
2577 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
2578 strncpy (psargs, get_exec_file (0), sizeof (psargs));
2579 if (get_inferior_args ())
2580 {
2581 strncat (psargs, " ", sizeof (psargs) - strlen (psargs));
2582 strncat (psargs, get_inferior_args (),
2583 sizeof (psargs) - strlen (psargs));
2584 }
2585 note_data = (char *) elfcore_write_prpsinfo (obfd,
2586 note_data,
2587 note_size, fname, psargs);
2588 }
2589
2590 /* Dump information for threads. */
2591 thread_args.obfd = obfd;
2592 thread_args.note_data = note_data;
2593 thread_args.note_size = note_size;
2594 thread_args.num_notes = 0;
2595 iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args);
2596 if (thread_args.num_notes == 0)
2597 {
2598 /* iterate_over_threads didn't come up with any threads; just
2599 use inferior_ptid. */
2600 note_data = linux_nat_do_registers (obfd, inferior_ptid,
2601 note_data, note_size);
2602 }
2603 else
2604 {
2605 note_data = thread_args.note_data;
2606 }
2607
2608 auxv_len = target_auxv_read (&current_target, &auxv);
2609 if (auxv_len > 0)
2610 {
2611 note_data = elfcore_write_note (obfd, note_data, note_size,
2612 "CORE", NT_AUXV, auxv, auxv_len);
2613 xfree (auxv);
2614 }
2615
2616 make_cleanup (xfree, note_data);
2617 return note_data;
2618}
2619
2620/* Implement the "info proc" command. */
2621
2622static void
2623linux_nat_info_proc_cmd (char *args, int from_tty)
2624{
2625 long long pid = PIDGET (inferior_ptid);
2626 FILE *procfile;
2627 char **argv = NULL;
2628 char buffer[MAXPATHLEN];
2629 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
2630 int cmdline_f = 1;
2631 int cwd_f = 1;
2632 int exe_f = 1;
2633 int mappings_f = 0;
2634 int environ_f = 0;
2635 int status_f = 0;
2636 int stat_f = 0;
2637 int all = 0;
2638 struct stat dummy;
2639
2640 if (args)
2641 {
2642 /* Break up 'args' into an argv array. */
2643 if ((argv = buildargv (args)) == NULL)
2644 nomem (0);
2645 else
2646 make_cleanup_freeargv (argv);
2647 }
2648 while (argv != NULL && *argv != NULL)
2649 {
2650 if (isdigit (argv[0][0]))
2651 {
2652 pid = strtoul (argv[0], NULL, 10);
2653 }
2654 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
2655 {
2656 mappings_f = 1;
2657 }
2658 else if (strcmp (argv[0], "status") == 0)
2659 {
2660 status_f = 1;
2661 }
2662 else if (strcmp (argv[0], "stat") == 0)
2663 {
2664 stat_f = 1;
2665 }
2666 else if (strcmp (argv[0], "cmd") == 0)
2667 {
2668 cmdline_f = 1;
2669 }
2670 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
2671 {
2672 exe_f = 1;
2673 }
2674 else if (strcmp (argv[0], "cwd") == 0)
2675 {
2676 cwd_f = 1;
2677 }
2678 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
2679 {
2680 all = 1;
2681 }
2682 else
2683 {
2684 /* [...] (future options here) */
2685 }
2686 argv++;
2687 }
2688 if (pid == 0)
2689 error ("No current process: you must name one.");
2690
2691 sprintf (fname1, "/proc/%lld", pid);
2692 if (stat (fname1, &dummy) != 0)
2693 error ("No /proc directory: '%s'", fname1);
2694
2695 printf_filtered ("process %lld\n", pid);
2696 if (cmdline_f || all)
2697 {
2698 sprintf (fname1, "/proc/%lld/cmdline", pid);
2699 if ((procfile = fopen (fname1, "r")) > 0)
2700 {
2701 fgets (buffer, sizeof (buffer), procfile);
2702 printf_filtered ("cmdline = '%s'\n", buffer);
2703 fclose (procfile);
2704 }
2705 else
2706 warning ("unable to open /proc file '%s'", fname1);
2707 }
2708 if (cwd_f || all)
2709 {
2710 sprintf (fname1, "/proc/%lld/cwd", pid);
2711 memset (fname2, 0, sizeof (fname2));
2712 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2713 printf_filtered ("cwd = '%s'\n", fname2);
2714 else
2715 warning ("unable to read link '%s'", fname1);
2716 }
2717 if (exe_f || all)
2718 {
2719 sprintf (fname1, "/proc/%lld/exe", pid);
2720 memset (fname2, 0, sizeof (fname2));
2721 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2722 printf_filtered ("exe = '%s'\n", fname2);
2723 else
2724 warning ("unable to read link '%s'", fname1);
2725 }
2726 if (mappings_f || all)
2727 {
2728 sprintf (fname1, "/proc/%lld/maps", pid);
2729 if ((procfile = fopen (fname1, "r")) > 0)
2730 {
2731 long long addr, endaddr, size, offset, inode;
2732 char permissions[8], device[8], filename[MAXPATHLEN];
2733
2734 printf_filtered ("Mapped address spaces:\n\n");
2735 if (TARGET_ADDR_BIT == 32)
2736 {
2737 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2738 "Start Addr",
2739 " End Addr",
2740 " Size", " Offset", "objfile");
2741 }
2742 else
2743 {
2744 printf_filtered (" %18s %18s %10s %10s %7s\n",
2745 "Start Addr",
2746 " End Addr",
2747 " Size", " Offset", "objfile");
2748 }
2749
2750 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
2751 &offset, &device[0], &inode, &filename[0]))
2752 {
2753 size = endaddr - addr;
2754
2755 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2756 calls here (and possibly above) should be abstracted
2757 out into their own functions? Andrew suggests using
2758 a generic local_address_string instead to print out
2759 the addresses; that makes sense to me, too. */
2760
2761 if (TARGET_ADDR_BIT == 32)
2762 {
2763 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2764 (unsigned long) addr, /* FIXME: pr_addr */
2765 (unsigned long) endaddr,
2766 (int) size,
2767 (unsigned int) offset,
2768 filename[0] ? filename : "");
2769 }
2770 else
2771 {
2772 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2773 (unsigned long) addr, /* FIXME: pr_addr */
2774 (unsigned long) endaddr,
2775 (int) size,
2776 (unsigned int) offset,
2777 filename[0] ? filename : "");
2778 }
2779 }
2780
2781 fclose (procfile);
2782 }
2783 else
2784 warning ("unable to open /proc file '%s'", fname1);
2785 }
2786 if (status_f || all)
2787 {
2788 sprintf (fname1, "/proc/%lld/status", pid);
2789 if ((procfile = fopen (fname1, "r")) > 0)
2790 {
2791 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
2792 puts_filtered (buffer);
2793 fclose (procfile);
2794 }
2795 else
2796 warning ("unable to open /proc file '%s'", fname1);
2797 }
2798 if (stat_f || all)
2799 {
2800 sprintf (fname1, "/proc/%lld/stat", pid);
2801 if ((procfile = fopen (fname1, "r")) > 0)
2802 {
2803 int itmp;
2804 char ctmp;
2805
2806 if (fscanf (procfile, "%d ", &itmp) > 0)
2807 printf_filtered ("Process: %d\n", itmp);
2808 if (fscanf (procfile, "%s ", &buffer[0]) > 0)
2809 printf_filtered ("Exec file: %s\n", buffer);
2810 if (fscanf (procfile, "%c ", &ctmp) > 0)
2811 printf_filtered ("State: %c\n", ctmp);
2812 if (fscanf (procfile, "%d ", &itmp) > 0)
2813 printf_filtered ("Parent process: %d\n", itmp);
2814 if (fscanf (procfile, "%d ", &itmp) > 0)
2815 printf_filtered ("Process group: %d\n", itmp);
2816 if (fscanf (procfile, "%d ", &itmp) > 0)
2817 printf_filtered ("Session id: %d\n", itmp);
2818 if (fscanf (procfile, "%d ", &itmp) > 0)
2819 printf_filtered ("TTY: %d\n", itmp);
2820 if (fscanf (procfile, "%d ", &itmp) > 0)
2821 printf_filtered ("TTY owner process group: %d\n", itmp);
2822 if (fscanf (procfile, "%u ", &itmp) > 0)
2823 printf_filtered ("Flags: 0x%x\n", itmp);
2824 if (fscanf (procfile, "%u ", &itmp) > 0)
2825 printf_filtered ("Minor faults (no memory page): %u\n",
2826 (unsigned int) itmp);
2827 if (fscanf (procfile, "%u ", &itmp) > 0)
2828 printf_filtered ("Minor faults, children: %u\n",
2829 (unsigned int) itmp);
2830 if (fscanf (procfile, "%u ", &itmp) > 0)
2831 printf_filtered ("Major faults (memory page faults): %u\n",
2832 (unsigned int) itmp);
2833 if (fscanf (procfile, "%u ", &itmp) > 0)
2834 printf_filtered ("Major faults, children: %u\n",
2835 (unsigned int) itmp);
2836 if (fscanf (procfile, "%d ", &itmp) > 0)
2837 printf_filtered ("utime: %d\n", itmp);
2838 if (fscanf (procfile, "%d ", &itmp) > 0)
2839 printf_filtered ("stime: %d\n", itmp);
2840 if (fscanf (procfile, "%d ", &itmp) > 0)
2841 printf_filtered ("utime, children: %d\n", itmp);
2842 if (fscanf (procfile, "%d ", &itmp) > 0)
2843 printf_filtered ("stime, children: %d\n", itmp);
2844 if (fscanf (procfile, "%d ", &itmp) > 0)
2845 printf_filtered ("jiffies remaining in current time slice: %d\n",
2846 itmp);
2847 if (fscanf (procfile, "%d ", &itmp) > 0)
2848 printf_filtered ("'nice' value: %d\n", itmp);
2849 if (fscanf (procfile, "%u ", &itmp) > 0)
2850 printf_filtered ("jiffies until next timeout: %u\n",
2851 (unsigned int) itmp);
2852 if (fscanf (procfile, "%u ", &itmp) > 0)
2853 printf_filtered ("jiffies until next SIGALRM: %u\n",
2854 (unsigned int) itmp);
2855 if (fscanf (procfile, "%d ", &itmp) > 0)
2856 printf_filtered ("start time (jiffies since system boot): %d\n",
2857 itmp);
2858 if (fscanf (procfile, "%u ", &itmp) > 0)
2859 printf_filtered ("Virtual memory size: %u\n",
2860 (unsigned int) itmp);
2861 if (fscanf (procfile, "%u ", &itmp) > 0)
2862 printf_filtered ("Resident set size: %u\n", (unsigned int) itmp);
2863 if (fscanf (procfile, "%u ", &itmp) > 0)
2864 printf_filtered ("rlim: %u\n", (unsigned int) itmp);
2865 if (fscanf (procfile, "%u ", &itmp) > 0)
2866 printf_filtered ("Start of text: 0x%x\n", itmp);
2867 if (fscanf (procfile, "%u ", &itmp) > 0)
2868 printf_filtered ("End of text: 0x%x\n", itmp);
2869 if (fscanf (procfile, "%u ", &itmp) > 0)
2870 printf_filtered ("Start of stack: 0x%x\n", itmp);
2871#if 0 /* Don't know how architecture-dependent the rest is...
2872 Anyway the signal bitmap info is available from "status". */
2873 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2874 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp);
2875 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2876 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp);
2877 if (fscanf (procfile, "%d ", &itmp) > 0)
2878 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp);
2879 if (fscanf (procfile, "%d ", &itmp) > 0)
2880 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp);
2881 if (fscanf (procfile, "%d ", &itmp) > 0)
2882 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp);
2883 if (fscanf (procfile, "%d ", &itmp) > 0)
2884 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp);
2885 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
2886 printf_filtered ("wchan (system call): 0x%x\n", itmp);
2887#endif
2888 fclose (procfile);
2889 }
2890 else
2891 warning ("unable to open /proc file '%s'", fname1);
2892 }
2893}
2894
2895int
2896linux_proc_xfer_memory (CORE_ADDR addr, char *myaddr, int len, int write,
2897 struct mem_attrib *attrib, struct target_ops *target)
2898{
2899 int fd, ret;
2900 char filename[64];
2901
2902 if (write)
2903 return 0;
2904
2905 /* Don't bother for one word. */
2906 if (len < 3 * sizeof (long))
2907 return 0;
2908
2909 /* We could keep this file open and cache it - possibly one per
2910 thread. That requires some juggling, but is even faster. */
2911 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
2912 fd = open (filename, O_RDONLY | O_LARGEFILE);
2913 if (fd == -1)
2914 return 0;
2915
2916 /* If pread64 is available, use it. It's faster if the kernel
2917 supports it (only one syscall), and it's 64-bit safe even on
2918 32-bit platforms (for instance, SPARC debugging a SPARC64
2919 application). */
2920#ifdef HAVE_PREAD64
2921 if (pread64 (fd, myaddr, len, addr) != len)
2922#else
2923 if (lseek (fd, addr, SEEK_SET) == -1 || read (fd, myaddr, len) != len)
2924#endif
2925 ret = 0;
2926 else
2927 ret = len;
2928
2929 close (fd);
2930 return ret;
2931}
2932
2933/* Parse LINE as a signal set and add its set bits to SIGS. */
2934
2935static void
2936add_line_to_sigset (const char *line, sigset_t *sigs)
2937{
2938 int len = strlen (line) - 1;
2939 const char *p;
2940 int signum;
2941
2942 if (line[len] != '\n')
2943 error ("Could not parse signal set: %s", line);
2944
2945 p = line;
2946 signum = len * 4;
2947 while (len-- > 0)
2948 {
2949 int digit;
2950
2951 if (*p >= '0' && *p <= '9')
2952 digit = *p - '0';
2953 else if (*p >= 'a' && *p <= 'f')
2954 digit = *p - 'a' + 10;
2955 else
2956 error ("Could not parse signal set: %s", line);
2957
2958 signum -= 4;
2959
2960 if (digit & 1)
2961 sigaddset (sigs, signum + 1);
2962 if (digit & 2)
2963 sigaddset (sigs, signum + 2);
2964 if (digit & 4)
2965 sigaddset (sigs, signum + 3);
2966 if (digit & 8)
2967 sigaddset (sigs, signum + 4);
2968
2969 p++;
2970 }
2971}
2972
2973/* Find process PID's pending signals from /proc/pid/status and set
2974 SIGS to match. */
2975
2976void
2977linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
2978{
2979 FILE *procfile;
2980 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
2981 int signum;
2982
2983 sigemptyset (pending);
2984 sigemptyset (blocked);
2985 sigemptyset (ignored);
2986 sprintf (fname, "/proc/%d/status", pid);
2987 procfile = fopen (fname, "r");
2988 if (procfile == NULL)
2989 error ("Could not open %s", fname);
2990
2991 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
2992 {
2993 /* Normal queued signals are on the SigPnd line in the status
2994 file. However, 2.6 kernels also have a "shared" pending
2995 queue for delivering signals to a thread group, so check for
2996 a ShdPnd line also.
2997
2998 Unfortunately some Red Hat kernels include the shared pending
2999 queue but not the ShdPnd status field. */
3000
3001 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
3002 add_line_to_sigset (buffer + 8, pending);
3003 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
3004 add_line_to_sigset (buffer + 8, pending);
3005 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
3006 add_line_to_sigset (buffer + 8, blocked);
3007 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
3008 add_line_to_sigset (buffer + 8, ignored);
3009 }
3010
3011 fclose (procfile);
3012}
3013
d6b0e80f
AC
3014void
3015_initialize_linux_nat (void)
3016{
3017 struct sigaction action;
d6b0e80f 3018 extern void thread_db_init (struct target_ops *);
dba24537
AC
3019 extern void inftarg_set_find_memory_regions ();
3020 extern void inftarg_set_make_corefile_notes ();
3021
3022 inftarg_set_find_memory_regions (linux_nat_find_memory_regions);
3023 inftarg_set_make_corefile_notes (linux_nat_make_corefile_notes);
3024
3025 add_info ("proc", linux_nat_info_proc_cmd,
3026 "Show /proc process information about any running process.\n\
3027Specify any process id, or use the program being debugged by default.\n\
3028Specify any of the following keywords for detailed info:\n\
3029 mappings -- list of mapped memory regions.\n\
3030 stat -- list a bunch of random process info.\n\
3031 status -- list a different bunch of random process info.\n\
3032 all -- list all available /proc info.");
d6b0e80f
AC
3033
3034 init_linux_nat_ops ();
3035 add_target (&linux_nat_ops);
3036 thread_db_init (&linux_nat_ops);
3037
3038 /* Save the original signal mask. */
3039 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
3040
3041 action.sa_handler = sigchld_handler;
3042 sigemptyset (&action.sa_mask);
3043 action.sa_flags = 0;
3044 sigaction (SIGCHLD, &action, NULL);
3045
3046 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3047 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
3048 sigdelset (&suspend_mask, SIGCHLD);
3049
3050 sigemptyset (&blocked_mask);
3051
3052 deprecated_add_show_from_set
3053 (add_set_cmd ("lin-lwp", no_class, var_zinteger,
3054 (char *) &debug_linux_nat,
3055 "Set debugging of GNU/Linux lwp module.\n\
3056Enables printf debugging output.\n", &setdebuglist), &showdebuglist);
3057}
3058\f
3059
3060/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3061 the GNU/Linux Threads library and therefore doesn't really belong
3062 here. */
3063
3064/* Read variable NAME in the target and return its value if found.
3065 Otherwise return zero. It is assumed that the type of the variable
3066 is `int'. */
3067
3068static int
3069get_signo (const char *name)
3070{
3071 struct minimal_symbol *ms;
3072 int signo;
3073
3074 ms = lookup_minimal_symbol (name, NULL, NULL);
3075 if (ms == NULL)
3076 return 0;
3077
3078 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
3079 sizeof (signo)) != 0)
3080 return 0;
3081
3082 return signo;
3083}
3084
3085/* Return the set of signals used by the threads library in *SET. */
3086
3087void
3088lin_thread_get_thread_signals (sigset_t *set)
3089{
3090 struct sigaction action;
3091 int restart, cancel;
3092
3093 sigemptyset (set);
3094
3095 restart = get_signo ("__pthread_sig_restart");
3096 if (restart == 0)
3097 return;
3098
3099 cancel = get_signo ("__pthread_sig_cancel");
3100 if (cancel == 0)
3101 return;
3102
3103 sigaddset (set, restart);
3104 sigaddset (set, cancel);
3105
3106 /* The GNU/Linux Threads library makes terminating threads send a
3107 special "cancel" signal instead of SIGCHLD. Make sure we catch
3108 those (to prevent them from terminating GDB itself, which is
3109 likely to be their default action) and treat them the same way as
3110 SIGCHLD. */
3111
3112 action.sa_handler = sigchld_handler;
3113 sigemptyset (&action.sa_mask);
3114 action.sa_flags = 0;
3115 sigaction (cancel, &action, NULL);
3116
3117 /* We block the "cancel" signal throughout this code ... */
3118 sigaddset (&blocked_mask, cancel);
3119 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
3120
3121 /* ... except during a sigsuspend. */
3122 sigdelset (&suspend_mask, cancel);
3123}
This page took 0.255749 seconds and 4 git commands to generate.