1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #ifdef HAVE_TKILL_SYSCALL
30 #include <sys/syscall.h>
32 #include <sys/ptrace.h>
33 #include "linux-nat.h"
34 #include "linux-fork.h"
35 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
54 /* If the system headers did not provide the constants, hard-code the normal
56 #ifndef PTRACE_EVENT_FORK
58 #define PTRACE_SETOPTIONS 0x4200
59 #define PTRACE_GETEVENTMSG 0x4201
61 /* options set using PTRACE_SETOPTIONS */
62 #define PTRACE_O_TRACESYSGOOD 0x00000001
63 #define PTRACE_O_TRACEFORK 0x00000002
64 #define PTRACE_O_TRACEVFORK 0x00000004
65 #define PTRACE_O_TRACECLONE 0x00000008
66 #define PTRACE_O_TRACEEXEC 0x00000010
67 #define PTRACE_O_TRACEVFORKDONE 0x00000020
68 #define PTRACE_O_TRACEEXIT 0x00000040
70 /* Wait extended result codes for the above trace options. */
71 #define PTRACE_EVENT_FORK 1
72 #define PTRACE_EVENT_VFORK 2
73 #define PTRACE_EVENT_CLONE 3
74 #define PTRACE_EVENT_EXEC 4
75 #define PTRACE_EVENT_VFORK_DONE 5
76 #define PTRACE_EVENT_EXIT 6
78 #endif /* PTRACE_EVENT_FORK */
80 /* We can't always assume that this flag is available, but all systems
81 with the ptrace event handlers also have __WALL, so it's safe to use
84 #define __WALL 0x40000000 /* Wait for any child. */
87 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
88 the use of the multi-threaded target. */
89 static struct target_ops
*linux_ops
;
91 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
92 Called by our to_xfer_partial. */
93 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
95 const char *, gdb_byte
*,
99 /* The saved to_mourn_inferior method, inherited from inf-ptrace.c.
100 Called by our to_mourn_inferior. */
101 static void (*super_mourn_inferior
) (void);
103 static int debug_linux_nat
;
105 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
106 struct cmd_list_element
*c
, const char *value
)
108 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
112 static int linux_parent_pid
;
114 struct simple_pid_list
117 struct simple_pid_list
*next
;
119 struct simple_pid_list
*stopped_pids
;
121 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
122 can not be used, 1 if it can. */
124 static int linux_supports_tracefork_flag
= -1;
126 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
127 PTRACE_O_TRACEVFORKDONE. */
129 static int linux_supports_tracevforkdone_flag
= -1;
132 /* Trivial list manipulation functions to keep track of a list of
133 new stopped processes. */
135 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
137 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
139 new_pid
->next
= *listp
;
144 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
146 struct simple_pid_list
**p
;
148 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
149 if ((*p
)->pid
== pid
)
151 struct simple_pid_list
*next
= (*p
)->next
;
160 linux_record_stopped_pid (int pid
)
162 add_to_pid_list (&stopped_pids
, pid
);
166 /* A helper function for linux_test_for_tracefork, called after fork (). */
169 linux_tracefork_child (void)
173 ptrace (PTRACE_TRACEME
, 0, 0, 0);
174 kill (getpid (), SIGSTOP
);
179 /* Wrapper function for waitpid which handles EINTR. */
182 my_waitpid (int pid
, int *status
, int flags
)
187 ret
= waitpid (pid
, status
, flags
);
189 while (ret
== -1 && errno
== EINTR
);
194 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
196 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
197 we know that the feature is not available. This may change the tracing
198 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
200 However, if it succeeds, we don't know for sure that the feature is
201 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
202 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
203 fork tracing, and let it fork. If the process exits, we assume that we
204 can't use TRACEFORK; if we get the fork notification, and we can extract
205 the new child's PID, then we assume that we can. */
208 linux_test_for_tracefork (int original_pid
)
210 int child_pid
, ret
, status
;
213 linux_supports_tracefork_flag
= 0;
214 linux_supports_tracevforkdone_flag
= 0;
216 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
222 perror_with_name (("fork"));
225 linux_tracefork_child ();
227 ret
= my_waitpid (child_pid
, &status
, 0);
229 perror_with_name (("waitpid"));
230 else if (ret
!= child_pid
)
231 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
232 if (! WIFSTOPPED (status
))
233 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
235 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
238 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
241 warning (_("linux_test_for_tracefork: failed to kill child"));
245 ret
= my_waitpid (child_pid
, &status
, 0);
246 if (ret
!= child_pid
)
247 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
248 else if (!WIFSIGNALED (status
))
249 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
250 "killed child"), status
);
255 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
256 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
257 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
258 linux_supports_tracevforkdone_flag
= (ret
== 0);
260 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
262 warning (_("linux_test_for_tracefork: failed to resume child"));
264 ret
= my_waitpid (child_pid
, &status
, 0);
266 if (ret
== child_pid
&& WIFSTOPPED (status
)
267 && status
>> 16 == PTRACE_EVENT_FORK
)
270 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
271 if (ret
== 0 && second_pid
!= 0)
275 linux_supports_tracefork_flag
= 1;
276 my_waitpid (second_pid
, &second_status
, 0);
277 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
279 warning (_("linux_test_for_tracefork: failed to kill second child"));
283 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
284 "(%d, status 0x%x)"), ret
, status
);
286 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
288 warning (_("linux_test_for_tracefork: failed to kill child"));
289 my_waitpid (child_pid
, &status
, 0);
292 /* Return non-zero iff we have tracefork functionality available.
293 This function also sets linux_supports_tracefork_flag. */
296 linux_supports_tracefork (int pid
)
298 if (linux_supports_tracefork_flag
== -1)
299 linux_test_for_tracefork (pid
);
300 return linux_supports_tracefork_flag
;
304 linux_supports_tracevforkdone (int pid
)
306 if (linux_supports_tracefork_flag
== -1)
307 linux_test_for_tracefork (pid
);
308 return linux_supports_tracevforkdone_flag
;
313 linux_enable_event_reporting (ptid_t ptid
)
315 int pid
= ptid_get_lwp (ptid
);
319 pid
= ptid_get_pid (ptid
);
321 if (! linux_supports_tracefork (pid
))
324 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
325 | PTRACE_O_TRACECLONE
;
326 if (linux_supports_tracevforkdone (pid
))
327 options
|= PTRACE_O_TRACEVFORKDONE
;
329 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
330 read-only process state. */
332 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
336 child_post_attach (int pid
)
338 linux_enable_event_reporting (pid_to_ptid (pid
));
342 linux_child_post_startup_inferior (ptid_t ptid
)
344 linux_enable_event_reporting (ptid
);
348 child_follow_fork (struct target_ops
*ops
, int follow_child
)
351 struct target_waitstatus last_status
;
353 int parent_pid
, child_pid
;
355 get_last_target_status (&last_ptid
, &last_status
);
356 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
357 parent_pid
= ptid_get_lwp (last_ptid
);
359 parent_pid
= ptid_get_pid (last_ptid
);
360 child_pid
= last_status
.value
.related_pid
;
364 /* We're already attached to the parent, by default. */
366 /* Before detaching from the child, remove all breakpoints from
367 it. (This won't actually modify the breakpoint list, but will
368 physically remove the breakpoints from the child.) */
369 /* If we vforked this will remove the breakpoints from the parent
370 also, but they'll be reinserted below. */
371 detach_breakpoints (child_pid
);
373 /* Detach new forked process? */
378 target_terminal_ours ();
379 fprintf_filtered (gdb_stdlog
,
380 "Detaching after fork from child process %d.\n",
384 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
388 struct fork_info
*fp
;
389 /* Retain child fork in ptrace (stopped) state. */
390 fp
= find_fork_pid (child_pid
);
392 fp
= add_fork (child_pid
);
393 fork_save_infrun_state (fp
, 0);
398 gdb_assert (linux_supports_tracefork_flag
>= 0);
399 if (linux_supports_tracevforkdone (0))
403 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
404 my_waitpid (parent_pid
, &status
, __WALL
);
405 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
406 warning (_("Unexpected waitpid result %06x when waiting for "
407 "vfork-done"), status
);
411 /* We can't insert breakpoints until the child has
412 finished with the shared memory region. We need to
413 wait until that happens. Ideal would be to just
415 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
416 - waitpid (parent_pid, &status, __WALL);
417 However, most architectures can't handle a syscall
418 being traced on the way out if it wasn't traced on
421 We might also think to loop, continuing the child
422 until it exits or gets a SIGTRAP. One problem is
423 that the child might call ptrace with PTRACE_TRACEME.
425 There's no simple and reliable way to figure out when
426 the vforked child will be done with its copy of the
427 shared memory. We could step it out of the syscall,
428 two instructions, let it go, and then single-step the
429 parent once. When we have hardware single-step, this
430 would work; with software single-step it could still
431 be made to work but we'd have to be able to insert
432 single-step breakpoints in the child, and we'd have
433 to insert -just- the single-step breakpoint in the
434 parent. Very awkward.
436 In the end, the best we can do is to make sure it
437 runs for a little while. Hopefully it will be out of
438 range of any breakpoints we reinsert. Usually this
439 is only the single-step breakpoint at vfork's return
445 /* Since we vforked, breakpoints were removed in the parent
446 too. Put them back. */
447 reattach_breakpoints (parent_pid
);
452 char child_pid_spelling
[40];
454 /* Needed to keep the breakpoint lists in sync. */
456 detach_breakpoints (child_pid
);
458 /* Before detaching from the parent, remove all breakpoints from it. */
459 remove_breakpoints ();
463 target_terminal_ours ();
464 fprintf_filtered (gdb_stdlog
,
465 "Attaching after fork to child process %d.\n",
469 /* If we're vforking, we may want to hold on to the parent until
470 the child exits or execs. At exec time we can remove the old
471 breakpoints from the parent and detach it; at exit time we
472 could do the same (or even, sneakily, resume debugging it - the
473 child's exec has failed, or something similar).
475 This doesn't clean up "properly", because we can't call
476 target_detach, but that's OK; if the current target is "child",
477 then it doesn't need any further cleanups, and lin_lwp will
478 generally not encounter vfork (vfork is defined to fork
481 The holding part is very easy if we have VFORKDONE events;
482 but keeping track of both processes is beyond GDB at the
483 moment. So we don't expose the parent to the rest of GDB.
484 Instead we quietly hold onto it until such time as we can
488 linux_parent_pid
= parent_pid
;
489 else if (!detach_fork
)
491 struct fork_info
*fp
;
492 /* Retain parent fork in ptrace (stopped) state. */
493 fp
= find_fork_pid (parent_pid
);
495 fp
= add_fork (parent_pid
);
496 fork_save_infrun_state (fp
, 0);
500 target_detach (NULL
, 0);
503 inferior_ptid
= pid_to_ptid (child_pid
);
505 /* Reinstall ourselves, since we might have been removed in
506 target_detach (which does other necessary cleanup). */
510 /* Reset breakpoints in the child as appropriate. */
511 follow_inferior_reset_breakpoints ();
518 linux_handle_extended_wait (int pid
, int status
,
519 struct target_waitstatus
*ourstatus
)
521 int event
= status
>> 16;
523 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
524 || event
== PTRACE_EVENT_CLONE
)
526 unsigned long new_pid
;
529 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
531 /* If we haven't already seen the new PID stop, wait for it now. */
532 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
534 /* The new child has a pending SIGSTOP. We can't affect it until it
535 hits the SIGSTOP, but we're already attached. */
536 ret
= my_waitpid (new_pid
, &status
,
537 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
539 perror_with_name (_("waiting for new child"));
540 else if (ret
!= new_pid
)
541 internal_error (__FILE__
, __LINE__
,
542 _("wait returned unexpected PID %d"), ret
);
543 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
544 internal_error (__FILE__
, __LINE__
,
545 _("wait returned unexpected status 0x%x"), status
);
548 if (event
== PTRACE_EVENT_FORK
)
549 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
550 else if (event
== PTRACE_EVENT_VFORK
)
551 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
553 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
555 ourstatus
->value
.related_pid
= new_pid
;
556 return inferior_ptid
;
559 if (event
== PTRACE_EVENT_EXEC
)
561 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
562 ourstatus
->value
.execd_pathname
563 = xstrdup (child_pid_to_exec_file (pid
));
565 if (linux_parent_pid
)
567 detach_breakpoints (linux_parent_pid
);
568 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
570 linux_parent_pid
= 0;
573 return inferior_ptid
;
576 internal_error (__FILE__
, __LINE__
,
577 _("unknown ptrace event %d"), event
);
582 child_insert_fork_catchpoint (int pid
)
584 if (! linux_supports_tracefork (pid
))
585 error (_("Your system does not support fork catchpoints."));
589 child_insert_vfork_catchpoint (int pid
)
591 if (!linux_supports_tracefork (pid
))
592 error (_("Your system does not support vfork catchpoints."));
596 child_insert_exec_catchpoint (int pid
)
598 if (!linux_supports_tracefork (pid
))
599 error (_("Your system does not support exec catchpoints."));
606 int pid
= PIDGET (inferior_ptid
);
607 struct target_waitstatus last
;
614 /* First cut -- let's crudely do everything inline. */
615 if (forks_exist_p ())
617 linux_fork_killall ();
619 generic_mourn_inferior ();
623 /* If we're stopped while forking and we haven't followed yet,
624 kill the other task. We need to do this first because the
625 parent will be sleeping if this is a vfork. */
627 get_last_target_status (&last_ptid
, &last
);
629 if (last
.kind
== TARGET_WAITKIND_FORKED
630 || last
.kind
== TARGET_WAITKIND_VFORKED
)
632 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
636 /* Kill the current process. */
637 ptrace (PT_KILL
, pid
, 0, 0);
638 ret
= wait (&status
);
640 /* We might get a SIGCHLD instead of an exit status. This is
641 aggravated by the first kill above - a child has just died. */
643 while (ret
== pid
&& WIFSTOPPED (status
))
645 ptrace (PT_KILL
, pid
, 0, 0);
646 ret
= wait (&status
);
648 target_mourn_inferior ();
652 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
653 are processes sharing the same VM space. A multi-threaded process
654 is basically a group of such processes. However, such a grouping
655 is almost entirely a user-space issue; the kernel doesn't enforce
656 such a grouping at all (this might change in the future). In
657 general, we'll rely on the threads library (i.e. the GNU/Linux
658 Threads library) to provide such a grouping.
660 It is perfectly well possible to write a multi-threaded application
661 without the assistance of a threads library, by using the clone
662 system call directly. This module should be able to give some
663 rudimentary support for debugging such applications if developers
664 specify the CLONE_PTRACE flag in the clone system call, and are
665 using the Linux kernel 2.4 or above.
667 Note that there are some peculiarities in GNU/Linux that affect
670 - In general one should specify the __WCLONE flag to waitpid in
671 order to make it report events for any of the cloned processes
672 (and leave it out for the initial process). However, if a cloned
673 process has exited the exit status is only reported if the
674 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
675 we cannot use it since GDB must work on older systems too.
677 - When a traced, cloned process exits and is waited for by the
678 debugger, the kernel reassigns it to the original parent and
679 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
680 library doesn't notice this, which leads to the "zombie problem":
681 When debugged a multi-threaded process that spawns a lot of
682 threads will run out of processes, even if the threads exit,
683 because the "zombies" stay around. */
685 /* List of known LWPs. */
686 static struct lwp_info
*lwp_list
;
688 /* Number of LWPs in the list. */
691 /* Non-zero if we're running in "threaded" mode. */
695 #define GET_LWP(ptid) ptid_get_lwp (ptid)
696 #define GET_PID(ptid) ptid_get_pid (ptid)
697 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
698 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
700 /* If the last reported event was a SIGTRAP, this variable is set to
701 the process id of the LWP/thread that got it. */
705 /* This module's target-specific operations. */
706 static struct target_ops linux_nat_ops
;
708 /* Since we cannot wait (in linux_nat_wait) for the initial process and
709 any cloned processes with a single call to waitpid, we have to use
710 the WNOHANG flag and call waitpid in a loop. To optimize
711 things a bit we use `sigsuspend' to wake us up when a process has
712 something to report (it will send us a SIGCHLD if it has). To make
713 this work we have to juggle with the signal mask. We save the
714 original signal mask such that we can restore it before creating a
715 new process in order to avoid blocking certain signals in the
716 inferior. We then block SIGCHLD during the waitpid/sigsuspend
719 /* Original signal mask. */
720 static sigset_t normal_mask
;
722 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
723 _initialize_linux_nat. */
724 static sigset_t suspend_mask
;
726 /* Signals to block to make that sigsuspend work. */
727 static sigset_t blocked_mask
;
730 /* Prototypes for local functions. */
731 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
732 static int linux_nat_thread_alive (ptid_t ptid
);
734 /* Convert wait status STATUS to a string. Used for printing debug
738 status_to_str (int status
)
742 if (WIFSTOPPED (status
))
743 snprintf (buf
, sizeof (buf
), "%s (stopped)",
744 strsignal (WSTOPSIG (status
)));
745 else if (WIFSIGNALED (status
))
746 snprintf (buf
, sizeof (buf
), "%s (terminated)",
747 strsignal (WSTOPSIG (status
)));
749 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
754 /* Initialize the list of LWPs. Note that this module, contrary to
755 what GDB's generic threads layer does for its thread list,
756 re-initializes the LWP lists whenever we mourn or detach (which
757 doesn't involve mourning) the inferior. */
762 struct lwp_info
*lp
, *lpnext
;
764 for (lp
= lwp_list
; lp
; lp
= lpnext
)
775 /* Add the LWP specified by PID to the list. If this causes the
776 number of LWPs to become larger than one, go into "threaded" mode.
777 Return a pointer to the structure describing the new LWP. */
779 static struct lwp_info
*
780 add_lwp (ptid_t ptid
)
784 gdb_assert (is_lwp (ptid
));
786 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
788 memset (lp
, 0, sizeof (struct lwp_info
));
790 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
802 /* Remove the LWP specified by PID from the list. */
805 delete_lwp (ptid_t ptid
)
807 struct lwp_info
*lp
, *lpprev
;
811 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
812 if (ptid_equal (lp
->ptid
, ptid
))
818 /* We don't go back to "non-threaded" mode if the number of threads
819 becomes less than two. */
823 lpprev
->next
= lp
->next
;
830 /* Return a pointer to the structure describing the LWP corresponding
831 to PID. If no corresponding LWP could be found, return NULL. */
833 static struct lwp_info
*
834 find_lwp_pid (ptid_t ptid
)
840 lwp
= GET_LWP (ptid
);
842 lwp
= GET_PID (ptid
);
844 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
845 if (lwp
== GET_LWP (lp
->ptid
))
851 /* Call CALLBACK with its second argument set to DATA for every LWP in
852 the list. If CALLBACK returns 1 for a particular LWP, return a
853 pointer to the structure describing that LWP immediately.
854 Otherwise return NULL. */
857 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
859 struct lwp_info
*lp
, *lpnext
;
861 for (lp
= lwp_list
; lp
; lp
= lpnext
)
864 if ((*callback
) (lp
, data
))
871 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
872 a message telling the user that a new LWP has been added to the
876 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
878 struct lwp_info
*lp
, *found_lp
;
880 gdb_assert (is_lwp (ptid
));
882 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
883 to interrupt either the ptrace() or waitpid() calls below. */
884 if (!sigismember (&blocked_mask
, SIGCHLD
))
886 sigaddset (&blocked_mask
, SIGCHLD
);
887 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
891 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
893 found_lp
= lp
= find_lwp_pid (ptid
);
897 /* We assume that we're already attached to any LWP that has an id
898 equal to the overall process id, and to any LWP that is already
899 in our list of LWPs. If we're not seeing exit events from threads
900 and we've had PID wraparound since we last tried to stop all threads,
901 this assumption might be wrong; fortunately, this is very unlikely
903 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
908 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
909 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
910 safe_strerror (errno
));
913 fprintf_unfiltered (gdb_stdlog
,
914 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
915 target_pid_to_str (ptid
));
917 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
918 if (pid
== -1 && errno
== ECHILD
)
920 /* Try again with __WCLONE to check cloned processes. */
921 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
925 gdb_assert (pid
== GET_LWP (ptid
)
926 && WIFSTOPPED (status
) && WSTOPSIG (status
));
928 child_post_attach (pid
);
934 fprintf_unfiltered (gdb_stdlog
,
935 "LLAL: waitpid %s received %s\n",
936 target_pid_to_str (ptid
),
937 status_to_str (status
));
942 /* We assume that the LWP representing the original process is
943 already stopped. Mark it as stopped in the data structure
944 that the linux ptrace layer uses to keep track of threads.
945 Note that this won't have already been done since the main
946 thread will have, we assume, been stopped by an attach from a
953 linux_nat_attach (char *args
, int from_tty
)
959 /* FIXME: We should probably accept a list of process id's, and
960 attach all of them. */
961 linux_ops
->to_attach (args
, from_tty
);
963 /* Add the initial process as the first LWP to the list. */
964 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
966 /* Make sure the initial process is stopped. The user-level threads
967 layer might want to poke around in the inferior, and that won't
968 work if things haven't stabilized yet. */
969 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
970 if (pid
== -1 && errno
== ECHILD
)
972 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
974 /* Try again with __WCLONE to check cloned processes. */
975 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
979 gdb_assert (pid
== GET_PID (inferior_ptid
)
980 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
984 /* Fake the SIGSTOP that core GDB expects. */
985 lp
->status
= W_STOPCODE (SIGSTOP
);
989 fprintf_unfiltered (gdb_stdlog
,
990 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
995 detach_callback (struct lwp_info
*lp
, void *data
)
997 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
999 if (debug_linux_nat
&& lp
->status
)
1000 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1001 strsignal (WSTOPSIG (lp
->status
)),
1002 target_pid_to_str (lp
->ptid
));
1004 while (lp
->signalled
&& lp
->stopped
)
1007 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
1008 WSTOPSIG (lp
->status
)) < 0)
1009 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
1010 safe_strerror (errno
));
1012 if (debug_linux_nat
)
1013 fprintf_unfiltered (gdb_stdlog
,
1014 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
1015 target_pid_to_str (lp
->ptid
),
1016 status_to_str (lp
->status
));
1021 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
1022 here. But since lp->signalled was cleared above,
1023 stop_wait_callback didn't do anything; the process was left
1024 running. Shouldn't we be waiting for it to stop?
1025 I've removed the call, since stop_wait_callback now does do
1026 something when called with lp->signalled == 0. */
1028 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1031 /* We don't actually detach from the LWP that has an id equal to the
1032 overall process id just yet. */
1033 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1036 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1037 WSTOPSIG (lp
->status
)) < 0)
1038 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1039 safe_strerror (errno
));
1041 if (debug_linux_nat
)
1042 fprintf_unfiltered (gdb_stdlog
,
1043 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1044 target_pid_to_str (lp
->ptid
),
1045 strsignal (WSTOPSIG (lp
->status
)));
1047 delete_lwp (lp
->ptid
);
1054 linux_nat_detach (char *args
, int from_tty
)
1056 iterate_over_lwps (detach_callback
, NULL
);
1058 /* Only the initial process should be left right now. */
1059 gdb_assert (num_lwps
== 1);
1061 trap_ptid
= null_ptid
;
1063 /* Destroy LWP info; it's no longer valid. */
1066 /* Restore the original signal mask. */
1067 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1068 sigemptyset (&blocked_mask
);
1070 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1071 linux_ops
->to_detach (args
, from_tty
);
1077 resume_callback (struct lwp_info
*lp
, void *data
)
1079 if (lp
->stopped
&& lp
->status
== 0)
1081 struct thread_info
*tp
;
1083 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1084 0, TARGET_SIGNAL_0
);
1085 if (debug_linux_nat
)
1086 fprintf_unfiltered (gdb_stdlog
,
1087 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1088 target_pid_to_str (lp
->ptid
));
1097 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1104 resume_set_callback (struct lwp_info
*lp
, void *data
)
1111 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1113 struct lwp_info
*lp
;
1116 if (debug_linux_nat
)
1117 fprintf_unfiltered (gdb_stdlog
,
1118 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1119 step
? "step" : "resume",
1120 target_pid_to_str (ptid
),
1121 signo
? strsignal (signo
) : "0",
1122 target_pid_to_str (inferior_ptid
));
1124 /* A specific PTID means `step only this process id'. */
1125 resume_all
= (PIDGET (ptid
) == -1);
1128 iterate_over_lwps (resume_set_callback
, NULL
);
1130 iterate_over_lwps (resume_clear_callback
, NULL
);
1132 /* If PID is -1, it's the current inferior that should be
1133 handled specially. */
1134 if (PIDGET (ptid
) == -1)
1135 ptid
= inferior_ptid
;
1137 lp
= find_lwp_pid (ptid
);
1140 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1142 /* Remember if we're stepping. */
1145 /* Mark this LWP as resumed. */
1148 /* If we have a pending wait status for this thread, there is no
1149 point in resuming the process. But first make sure that
1150 linux_nat_wait won't preemptively handle the event - we
1151 should never take this short-circuit if we are going to
1152 leave LP running, since we have skipped resuming all the
1153 other threads. This bit of code needs to be synchronized
1154 with linux_nat_wait. */
1156 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1158 int saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1160 if (signal_stop_state (saved_signo
) == 0
1161 && signal_print_state (saved_signo
) == 0
1162 && signal_pass_state (saved_signo
) == 1)
1164 if (debug_linux_nat
)
1165 fprintf_unfiltered (gdb_stdlog
,
1166 "LLR: Not short circuiting for ignored "
1167 "status 0x%x\n", lp
->status
);
1169 /* FIXME: What should we do if we are supposed to continue
1170 this thread with a signal? */
1171 gdb_assert (signo
== TARGET_SIGNAL_0
);
1172 signo
= saved_signo
;
1179 /* FIXME: What should we do if we are supposed to continue
1180 this thread with a signal? */
1181 gdb_assert (signo
== TARGET_SIGNAL_0
);
1183 if (debug_linux_nat
)
1184 fprintf_unfiltered (gdb_stdlog
,
1185 "LLR: Short circuiting for status 0x%x\n",
1191 /* Mark LWP as not stopped to prevent it from being continued by
1197 iterate_over_lwps (resume_callback
, NULL
);
1199 linux_ops
->to_resume (ptid
, step
, signo
);
1200 if (debug_linux_nat
)
1201 fprintf_unfiltered (gdb_stdlog
,
1202 "LLR: %s %s, %s (resume event thread)\n",
1203 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1204 target_pid_to_str (ptid
),
1205 signo
? strsignal (signo
) : "0");
1208 /* Issue kill to specified lwp. */
1210 static int tkill_failed
;
1213 kill_lwp (int lwpid
, int signo
)
1217 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1218 fails, then we are not using nptl threads and we should be using kill. */
1220 #ifdef HAVE_TKILL_SYSCALL
1223 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1224 if (errno
!= ENOSYS
)
1231 return kill (lwpid
, signo
);
1234 /* Handle a GNU/Linux extended wait response. Most of the work we
1235 just pass off to linux_handle_extended_wait, but if it reports a
1236 clone event we need to add the new LWP to our list (and not report
1237 the trap to higher layers). This function returns non-zero if
1238 the event should be ignored and we should wait again. */
1241 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1243 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1246 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1247 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1249 struct lwp_info
*new_lp
;
1250 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1251 GET_PID (inferior_ptid
)));
1253 new_lp
->stopped
= 1;
1255 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1257 if (debug_linux_nat
)
1258 fprintf_unfiltered (gdb_stdlog
,
1259 "LLHE: Got clone event from LWP %ld, resuming\n",
1260 GET_LWP (lp
->ptid
));
1261 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1269 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1273 wait_lwp (struct lwp_info
*lp
)
1277 int thread_dead
= 0;
1279 gdb_assert (!lp
->stopped
);
1280 gdb_assert (lp
->status
== 0);
1282 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1283 if (pid
== -1 && errno
== ECHILD
)
1285 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1286 if (pid
== -1 && errno
== ECHILD
)
1288 /* The thread has previously exited. We need to delete it
1289 now because, for some vendor 2.4 kernels with NPTL
1290 support backported, there won't be an exit event unless
1291 it is the main thread. 2.6 kernels will report an exit
1292 event for each thread that exits, as expected. */
1294 if (debug_linux_nat
)
1295 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1296 target_pid_to_str (lp
->ptid
));
1302 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1304 if (debug_linux_nat
)
1306 fprintf_unfiltered (gdb_stdlog
,
1307 "WL: waitpid %s received %s\n",
1308 target_pid_to_str (lp
->ptid
),
1309 status_to_str (status
));
1313 /* Check if the thread has exited. */
1314 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1317 if (debug_linux_nat
)
1318 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1319 target_pid_to_str (lp
->ptid
));
1324 if (in_thread_list (lp
->ptid
))
1326 /* Core GDB cannot deal with us deleting the current thread. */
1327 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1328 delete_thread (lp
->ptid
);
1329 printf_unfiltered (_("[%s exited]\n"),
1330 target_pid_to_str (lp
->ptid
));
1333 delete_lwp (lp
->ptid
);
1337 gdb_assert (WIFSTOPPED (status
));
1339 /* Handle GNU/Linux's extended waitstatus for trace events. */
1340 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1342 if (debug_linux_nat
)
1343 fprintf_unfiltered (gdb_stdlog
,
1344 "WL: Handling extended status 0x%06x\n",
1346 if (linux_nat_handle_extended (lp
, status
))
1347 return wait_lwp (lp
);
1353 /* Send a SIGSTOP to LP. */
1356 stop_callback (struct lwp_info
*lp
, void *data
)
1358 if (!lp
->stopped
&& !lp
->signalled
)
1362 if (debug_linux_nat
)
1364 fprintf_unfiltered (gdb_stdlog
,
1365 "SC: kill %s **<SIGSTOP>**\n",
1366 target_pid_to_str (lp
->ptid
));
1369 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1370 if (debug_linux_nat
)
1372 fprintf_unfiltered (gdb_stdlog
,
1373 "SC: lwp kill %d %s\n",
1375 errno
? safe_strerror (errno
) : "ERRNO-OK");
1379 gdb_assert (lp
->status
== 0);
1385 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1386 a pointer to a set of signals to be flushed immediately. */
1389 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1391 sigset_t
*flush_mask
= data
;
1397 status
= wait_lwp (lp
);
1401 /* Ignore any signals in FLUSH_MASK. */
1402 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1411 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1412 if (debug_linux_nat
)
1413 fprintf_unfiltered (gdb_stdlog
,
1414 "PTRACE_CONT %s, 0, 0 (%s)\n",
1415 target_pid_to_str (lp
->ptid
),
1416 errno
? safe_strerror (errno
) : "OK");
1418 return stop_wait_callback (lp
, flush_mask
);
1421 if (WSTOPSIG (status
) != SIGSTOP
)
1423 if (WSTOPSIG (status
) == SIGTRAP
)
1425 /* If a LWP other than the LWP that we're reporting an
1426 event for has hit a GDB breakpoint (as opposed to
1427 some random trap signal), then just arrange for it to
1428 hit it again later. We don't keep the SIGTRAP status
1429 and don't forward the SIGTRAP signal to the LWP. We
1430 will handle the current event, eventually we will
1431 resume all LWPs, and this one will get its breakpoint
1434 If we do not do this, then we run the risk that the
1435 user will delete or disable the breakpoint, but the
1436 thread will have already tripped on it. */
1438 /* Now resume this LWP and get the SIGSTOP event. */
1440 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1441 if (debug_linux_nat
)
1443 fprintf_unfiltered (gdb_stdlog
,
1444 "PTRACE_CONT %s, 0, 0 (%s)\n",
1445 target_pid_to_str (lp
->ptid
),
1446 errno
? safe_strerror (errno
) : "OK");
1448 fprintf_unfiltered (gdb_stdlog
,
1449 "SWC: Candidate SIGTRAP event in %s\n",
1450 target_pid_to_str (lp
->ptid
));
1452 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1453 stop_wait_callback (lp
, data
);
1454 /* If there's another event, throw it back into the queue. */
1457 if (debug_linux_nat
)
1459 fprintf_unfiltered (gdb_stdlog
,
1460 "SWC: kill %s, %s\n",
1461 target_pid_to_str (lp
->ptid
),
1462 status_to_str ((int) status
));
1464 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1466 /* Save the sigtrap event. */
1467 lp
->status
= status
;
1472 /* The thread was stopped with a signal other than
1473 SIGSTOP, and didn't accidentally trip a breakpoint. */
1475 if (debug_linux_nat
)
1477 fprintf_unfiltered (gdb_stdlog
,
1478 "SWC: Pending event %s in %s\n",
1479 status_to_str ((int) status
),
1480 target_pid_to_str (lp
->ptid
));
1482 /* Now resume this LWP and get the SIGSTOP event. */
1484 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1485 if (debug_linux_nat
)
1486 fprintf_unfiltered (gdb_stdlog
,
1487 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1488 target_pid_to_str (lp
->ptid
),
1489 errno
? safe_strerror (errno
) : "OK");
1491 /* Hold this event/waitstatus while we check to see if
1492 there are any more (we still want to get that SIGSTOP). */
1493 stop_wait_callback (lp
, data
);
1494 /* If the lp->status field is still empty, use it to hold
1495 this event. If not, then this event must be returned
1496 to the event queue of the LWP. */
1497 if (lp
->status
== 0)
1498 lp
->status
= status
;
1501 if (debug_linux_nat
)
1503 fprintf_unfiltered (gdb_stdlog
,
1504 "SWC: kill %s, %s\n",
1505 target_pid_to_str (lp
->ptid
),
1506 status_to_str ((int) status
));
1508 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1515 /* We caught the SIGSTOP that we intended to catch, so
1516 there's no SIGSTOP pending. */
1525 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1526 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1529 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1531 sigset_t blocked
, ignored
;
1534 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1539 for (i
= 1; i
< NSIG
; i
++)
1540 if (sigismember (pending
, i
))
1541 if (!sigismember (flush_mask
, i
)
1542 || sigismember (&blocked
, i
)
1543 || sigismember (&ignored
, i
))
1544 sigdelset (pending
, i
);
1546 if (sigisemptyset (pending
))
1552 /* DATA is interpreted as a mask of signals to flush. If LP has
1553 signals pending, and they are all in the flush mask, then arrange
1554 to flush them. LP should be stopped, as should all other threads
1555 it might share a signal queue with. */
1558 flush_callback (struct lwp_info
*lp
, void *data
)
1560 sigset_t
*flush_mask
= data
;
1561 sigset_t pending
, intersection
, blocked
, ignored
;
1564 /* Normally, when an LWP exits, it is removed from the LWP list. The
1565 last LWP isn't removed till later, however. So if there is only
1566 one LWP on the list, make sure it's alive. */
1567 if (lwp_list
== lp
&& lp
->next
== NULL
)
1568 if (!linux_nat_thread_alive (lp
->ptid
))
1571 /* Just because the LWP is stopped doesn't mean that new signals
1572 can't arrive from outside, so this function must be careful of
1573 race conditions. However, because all threads are stopped, we
1574 can assume that the pending mask will not shrink unless we resume
1575 the LWP, and that it will then get another signal. We can't
1576 control which one, however. */
1580 if (debug_linux_nat
)
1581 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1582 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1586 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1591 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1592 if (debug_linux_nat
)
1593 fprintf_unfiltered (gdb_stderr
,
1594 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1597 stop_wait_callback (lp
, flush_mask
);
1598 if (debug_linux_nat
)
1599 fprintf_unfiltered (gdb_stderr
,
1600 "FC: Wait finished; saved status is %d\n",
1607 /* Return non-zero if LP has a wait status pending. */
1610 status_callback (struct lwp_info
*lp
, void *data
)
1612 /* Only report a pending wait status if we pretend that this has
1613 indeed been resumed. */
1614 return (lp
->status
!= 0 && lp
->resumed
);
1617 /* Return non-zero if LP isn't stopped. */
1620 running_callback (struct lwp_info
*lp
, void *data
)
1622 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1625 /* Count the LWP's that have had events. */
1628 count_events_callback (struct lwp_info
*lp
, void *data
)
1632 gdb_assert (count
!= NULL
);
1634 /* Count only LWPs that have a SIGTRAP event pending. */
1636 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1642 /* Select the LWP (if any) that is currently being single-stepped. */
1645 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1647 if (lp
->step
&& lp
->status
!= 0)
1653 /* Select the Nth LWP that has had a SIGTRAP event. */
1656 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1658 int *selector
= data
;
1660 gdb_assert (selector
!= NULL
);
1662 /* Select only LWPs that have a SIGTRAP event pending. */
1664 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1665 if ((*selector
)-- == 0)
1672 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1674 struct lwp_info
*event_lp
= data
;
1676 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1680 /* If a LWP other than the LWP that we're reporting an event for has
1681 hit a GDB breakpoint (as opposed to some random trap signal),
1682 then just arrange for it to hit it again later. We don't keep
1683 the SIGTRAP status and don't forward the SIGTRAP signal to the
1684 LWP. We will handle the current event, eventually we will resume
1685 all LWPs, and this one will get its breakpoint trap again.
1687 If we do not do this, then we run the risk that the user will
1688 delete or disable the breakpoint, but the LWP will have already
1692 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1693 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1694 DECR_PC_AFTER_BREAK
))
1696 if (debug_linux_nat
)
1697 fprintf_unfiltered (gdb_stdlog
,
1698 "CBC: Push back breakpoint for %s\n",
1699 target_pid_to_str (lp
->ptid
));
1701 /* Back up the PC if necessary. */
1702 if (DECR_PC_AFTER_BREAK
)
1703 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1705 /* Throw away the SIGTRAP. */
1712 /* Select one LWP out of those that have events pending. */
1715 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1718 int random_selector
;
1719 struct lwp_info
*event_lp
;
1721 /* Record the wait status for the original LWP. */
1722 (*orig_lp
)->status
= *status
;
1724 /* Give preference to any LWP that is being single-stepped. */
1725 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1726 if (event_lp
!= NULL
)
1728 if (debug_linux_nat
)
1729 fprintf_unfiltered (gdb_stdlog
,
1730 "SEL: Select single-step %s\n",
1731 target_pid_to_str (event_lp
->ptid
));
1735 /* No single-stepping LWP. Select one at random, out of those
1736 which have had SIGTRAP events. */
1738 /* First see how many SIGTRAP events we have. */
1739 iterate_over_lwps (count_events_callback
, &num_events
);
1741 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1742 random_selector
= (int)
1743 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1745 if (debug_linux_nat
&& num_events
> 1)
1746 fprintf_unfiltered (gdb_stdlog
,
1747 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1748 num_events
, random_selector
);
1750 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1754 if (event_lp
!= NULL
)
1756 /* Switch the event LWP. */
1757 *orig_lp
= event_lp
;
1758 *status
= event_lp
->status
;
1761 /* Flush the wait status for the event LWP. */
1762 (*orig_lp
)->status
= 0;
1765 /* Return non-zero if LP has been resumed. */
1768 resumed_callback (struct lwp_info
*lp
, void *data
)
1773 /* Local mourn_inferior -- we need to override mourn_inferior
1774 so that we can do something clever if one of several forks
1778 child_mourn_inferior (void)
1782 if (! forks_exist_p ())
1784 /* Normal case, no other forks available. */
1785 super_mourn_inferior ();
1790 /* Multi-fork case. The current inferior_ptid has exited, but
1791 there are other viable forks to debug. Delete the exiting
1792 one and context-switch to the first available. */
1793 linux_fork_mourn_inferior ();
1797 /* We need to override child_wait to support attaching to cloned
1798 processes, since a normal wait (as done by the default version)
1799 ignores those processes. */
1801 /* Wait for child PTID to do something. Return id of the child,
1802 minus_one_ptid in case of error; store status into *OURSTATUS. */
1805 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1811 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1815 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1816 attached process. */
1819 pid
= my_waitpid (GET_PID (ptid
), &status
, 0);
1820 if (pid
== -1 && errno
== ECHILD
)
1821 /* Try again with __WCLONE to check cloned processes. */
1822 pid
= my_waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1824 if (debug_linux_nat
)
1826 fprintf_unfiltered (gdb_stdlog
,
1827 "CW: waitpid %ld received %s\n",
1828 (long) pid
, status_to_str (status
));
1833 /* Make sure we don't report an event for the exit of the
1834 original program, if we've detached from it. */
1835 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1841 /* Check for stop events reported by a process we didn't already
1842 know about - in this case, anything other than inferior_ptid.
1844 If we're expecting to receive stopped processes after fork,
1845 vfork, and clone events, then we'll just add the new one to
1846 our list and go back to waiting for the event to be reported
1847 - the stopped process might be returned from waitpid before
1848 or after the event is. If we want to handle debugging of
1849 CLONE_PTRACE processes we need to do more here, i.e. switch
1850 to multi-threaded mode. */
1851 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1852 && pid
!= GET_PID (inferior_ptid
))
1854 linux_record_stopped_pid (pid
);
1859 /* Handle GNU/Linux's extended waitstatus for trace events. */
1860 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1861 && status
>> 16 != 0)
1863 linux_handle_extended_wait (pid
, status
, ourstatus
);
1865 /* If we see a clone event, detach the child, and don't
1866 report the event. It would be nice to offer some way to
1867 switch into a non-thread-db based threaded mode at this
1869 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1871 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1872 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1873 ptrace (PTRACE_CONT
, pid
, 0, 0);
1879 clear_sigio_trap ();
1880 clear_sigint_trap ();
1882 while (pid
== -1 && save_errno
== EINTR
);
1886 warning (_("Child process unexpectedly missing: %s"),
1887 safe_strerror (errno
));
1889 /* Claim it exited with unknown signal. */
1890 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1891 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1892 return minus_one_ptid
;
1895 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1896 store_waitstatus (ourstatus
, status
);
1898 return pid_to_ptid (pid
);
1901 /* Stop an active thread, verify it still exists, then resume it. */
1904 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1906 struct lwp_info
*ptr
;
1908 if (!lp
->stopped
&& !lp
->signalled
)
1910 stop_callback (lp
, NULL
);
1911 stop_wait_callback (lp
, NULL
);
1912 /* Resume if the lwp still exists. */
1913 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1916 resume_callback (lp
, NULL
);
1917 resume_set_callback (lp
, NULL
);
1924 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1926 struct lwp_info
*lp
= NULL
;
1929 pid_t pid
= PIDGET (ptid
);
1930 sigset_t flush_mask
;
1932 sigemptyset (&flush_mask
);
1934 /* Make sure SIGCHLD is blocked. */
1935 if (!sigismember (&blocked_mask
, SIGCHLD
))
1937 sigaddset (&blocked_mask
, SIGCHLD
);
1938 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1943 /* Make sure there is at least one LWP that has been resumed, at
1944 least if there are any LWPs at all. */
1945 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1947 /* First check if there is a LWP with a wait status pending. */
1950 /* Any LWP that's been resumed will do. */
1951 lp
= iterate_over_lwps (status_callback
, NULL
);
1954 status
= lp
->status
;
1957 if (debug_linux_nat
&& status
)
1958 fprintf_unfiltered (gdb_stdlog
,
1959 "LLW: Using pending wait status %s for %s.\n",
1960 status_to_str (status
),
1961 target_pid_to_str (lp
->ptid
));
1964 /* But if we don't fine one, we'll have to wait, and check both
1965 cloned and uncloned processes. We start with the cloned
1967 options
= __WCLONE
| WNOHANG
;
1969 else if (is_lwp (ptid
))
1971 if (debug_linux_nat
)
1972 fprintf_unfiltered (gdb_stdlog
,
1973 "LLW: Waiting for specific LWP %s.\n",
1974 target_pid_to_str (ptid
));
1976 /* We have a specific LWP to check. */
1977 lp
= find_lwp_pid (ptid
);
1979 status
= lp
->status
;
1982 if (debug_linux_nat
&& status
)
1983 fprintf_unfiltered (gdb_stdlog
,
1984 "LLW: Using pending wait status %s for %s.\n",
1985 status_to_str (status
),
1986 target_pid_to_str (lp
->ptid
));
1988 /* If we have to wait, take into account whether PID is a cloned
1989 process or not. And we have to convert it to something that
1990 the layer beneath us can understand. */
1991 options
= lp
->cloned
? __WCLONE
: 0;
1992 pid
= GET_LWP (ptid
);
1995 if (status
&& lp
->signalled
)
1997 /* A pending SIGSTOP may interfere with the normal stream of
1998 events. In a typical case where interference is a problem,
1999 we have a SIGSTOP signal pending for LWP A while
2000 single-stepping it, encounter an event in LWP B, and take the
2001 pending SIGSTOP while trying to stop LWP A. After processing
2002 the event in LWP B, LWP A is continued, and we'll never see
2003 the SIGTRAP associated with the last time we were
2004 single-stepping LWP A. */
2006 /* Resume the thread. It should halt immediately returning the
2008 registers_changed ();
2009 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2010 lp
->step
, TARGET_SIGNAL_0
);
2011 if (debug_linux_nat
)
2012 fprintf_unfiltered (gdb_stdlog
,
2013 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2014 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2015 target_pid_to_str (lp
->ptid
));
2017 gdb_assert (lp
->resumed
);
2019 /* This should catch the pending SIGSTOP. */
2020 stop_wait_callback (lp
, NULL
);
2023 set_sigint_trap (); /* Causes SIGINT to be passed on to the
2024 attached process. */
2031 lwpid
= my_waitpid (pid
, &status
, options
);
2034 gdb_assert (pid
== -1 || lwpid
== pid
);
2036 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
,
2039 "LLW: waitpid %ld received %s\n",
2040 (long) lwpid
, status_to_str (status
));
2043 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2045 /* Check for stop events reported by a process we didn't
2046 already know about - anything not already in our LWP
2049 If we're expecting to receive stopped processes after
2050 fork, vfork, and clone events, then we'll just add the
2051 new one to our list and go back to waiting for the event
2052 to be reported - the stopped process might be returned
2053 from waitpid before or after the event is. */
2054 if (WIFSTOPPED (status
) && !lp
)
2056 linux_record_stopped_pid (lwpid
);
2061 /* Make sure we don't report an event for the exit of an LWP not in
2062 our list, i.e. not part of the current process. This can happen
2063 if we detach from a program we original forked and then it
2065 if (!WIFSTOPPED (status
) && !lp
)
2071 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2072 CLONE_PTRACE processes which do not use the thread library -
2073 otherwise we wouldn't find the new LWP this way. That doesn't
2074 currently work, and the following code is currently unreachable
2075 due to the two blocks above. If it's fixed some day, this code
2076 should be broken out into a function so that we can also pick up
2077 LWPs from the new interface. */
2080 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2081 if (options
& __WCLONE
)
2086 gdb_assert (WIFSTOPPED (status
)
2087 && WSTOPSIG (status
) == SIGSTOP
);
2090 if (!in_thread_list (inferior_ptid
))
2092 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2093 GET_PID (inferior_ptid
));
2094 add_thread (inferior_ptid
);
2097 add_thread (lp
->ptid
);
2098 printf_unfiltered (_("[New %s]\n"),
2099 target_pid_to_str (lp
->ptid
));
2103 /* Handle GNU/Linux's extended waitstatus for trace events. */
2104 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2106 if (debug_linux_nat
)
2107 fprintf_unfiltered (gdb_stdlog
,
2108 "LLW: Handling extended status 0x%06x\n",
2110 if (linux_nat_handle_extended (lp
, status
))
2117 /* Check if the thread has exited. */
2118 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2120 if (in_thread_list (lp
->ptid
))
2122 /* Core GDB cannot deal with us deleting the current
2124 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2125 delete_thread (lp
->ptid
);
2126 printf_unfiltered (_("[%s exited]\n"),
2127 target_pid_to_str (lp
->ptid
));
2130 /* If this is the main thread, we must stop all threads and
2131 verify if they are still alive. This is because in the nptl
2132 thread model, there is no signal issued for exiting LWPs
2133 other than the main thread. We only get the main thread
2134 exit signal once all child threads have already exited.
2135 If we stop all the threads and use the stop_wait_callback
2136 to check if they have exited we can determine whether this
2137 signal should be ignored or whether it means the end of the
2138 debugged application, regardless of which threading model
2140 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2143 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2146 if (debug_linux_nat
)
2147 fprintf_unfiltered (gdb_stdlog
,
2148 "LLW: %s exited.\n",
2149 target_pid_to_str (lp
->ptid
));
2151 delete_lwp (lp
->ptid
);
2153 /* If there is at least one more LWP, then the exit signal
2154 was not the end of the debugged application and should be
2158 /* Make sure there is at least one thread running. */
2159 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2161 /* Discard the event. */
2167 /* Check if the current LWP has previously exited. In the nptl
2168 thread model, LWPs other than the main thread do not issue
2169 signals when they exit so we must check whenever the thread
2170 has stopped. A similar check is made in stop_wait_callback(). */
2171 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2173 if (in_thread_list (lp
->ptid
))
2175 /* Core GDB cannot deal with us deleting the current
2177 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2178 delete_thread (lp
->ptid
);
2179 printf_unfiltered (_("[%s exited]\n"),
2180 target_pid_to_str (lp
->ptid
));
2182 if (debug_linux_nat
)
2183 fprintf_unfiltered (gdb_stdlog
,
2184 "LLW: %s exited.\n",
2185 target_pid_to_str (lp
->ptid
));
2187 delete_lwp (lp
->ptid
);
2189 /* Make sure there is at least one thread running. */
2190 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2192 /* Discard the event. */
2197 /* Make sure we don't report a SIGSTOP that we sent
2198 ourselves in an attempt to stop an LWP. */
2200 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2202 if (debug_linux_nat
)
2203 fprintf_unfiltered (gdb_stdlog
,
2204 "LLW: Delayed SIGSTOP caught for %s.\n",
2205 target_pid_to_str (lp
->ptid
));
2207 /* This is a delayed SIGSTOP. */
2210 registers_changed ();
2211 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2212 lp
->step
, TARGET_SIGNAL_0
);
2213 if (debug_linux_nat
)
2214 fprintf_unfiltered (gdb_stdlog
,
2215 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2217 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2218 target_pid_to_str (lp
->ptid
));
2221 gdb_assert (lp
->resumed
);
2223 /* Discard the event. */
2233 /* Alternate between checking cloned and uncloned processes. */
2234 options
^= __WCLONE
;
2236 /* And suspend every time we have checked both. */
2237 if (options
& __WCLONE
)
2238 sigsuspend (&suspend_mask
);
2241 /* We shouldn't end up here unless we want to try again. */
2242 gdb_assert (status
== 0);
2245 clear_sigio_trap ();
2246 clear_sigint_trap ();
2250 /* Don't report signals that GDB isn't interested in, such as
2251 signals that are neither printed nor stopped upon. Stopping all
2252 threads can be a bit time-consuming so if we want decent
2253 performance with heavily multi-threaded programs, especially when
2254 they're using a high frequency timer, we'd better avoid it if we
2257 if (WIFSTOPPED (status
))
2259 int signo
= target_signal_from_host (WSTOPSIG (status
));
2261 if (signal_stop_state (signo
) == 0
2262 && signal_print_state (signo
) == 0
2263 && signal_pass_state (signo
) == 1)
2265 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2266 here? It is not clear we should. GDB may not expect
2267 other threads to run. On the other hand, not resuming
2268 newly attached threads may cause an unwanted delay in
2269 getting them running. */
2270 registers_changed ();
2271 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2273 if (debug_linux_nat
)
2274 fprintf_unfiltered (gdb_stdlog
,
2275 "LLW: %s %s, %s (preempt 'handle')\n",
2277 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2278 target_pid_to_str (lp
->ptid
),
2279 signo
? strsignal (signo
) : "0");
2285 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2287 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2288 forwarded to the entire process group, that is, all LWP's
2289 will receive it. Since we only want to report it once,
2290 we try to flush it from all LWPs except this one. */
2291 sigaddset (&flush_mask
, SIGINT
);
2295 /* This LWP is stopped now. */
2298 if (debug_linux_nat
)
2299 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2300 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2302 /* Now stop all other LWP's ... */
2303 iterate_over_lwps (stop_callback
, NULL
);
2305 /* ... and wait until all of them have reported back that they're no
2307 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2308 iterate_over_lwps (flush_callback
, &flush_mask
);
2310 /* If we're not waiting for a specific LWP, choose an event LWP from
2311 among those that have had events. Giving equal priority to all
2312 LWPs that have had events helps prevent starvation. */
2314 select_event_lwp (&lp
, &status
);
2316 /* Now that we've selected our final event LWP, cancel any
2317 breakpoints in other LWPs that have hit a GDB breakpoint. See
2318 the comment in cancel_breakpoints_callback to find out why. */
2319 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2321 /* If we're not running in "threaded" mode, we'll report the bare
2324 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2326 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2327 if (debug_linux_nat
)
2328 fprintf_unfiltered (gdb_stdlog
,
2329 "LLW: trap_ptid is %s.\n",
2330 target_pid_to_str (trap_ptid
));
2333 trap_ptid
= null_ptid
;
2335 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2337 *ourstatus
= lp
->waitstatus
;
2338 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2341 store_waitstatus (ourstatus
, status
);
2343 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2347 kill_callback (struct lwp_info
*lp
, void *data
)
2350 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2351 if (debug_linux_nat
)
2352 fprintf_unfiltered (gdb_stdlog
,
2353 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2354 target_pid_to_str (lp
->ptid
),
2355 errno
? safe_strerror (errno
) : "OK");
2361 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2365 /* We must make sure that there are no pending events (delayed
2366 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2367 program doesn't interfere with any following debugging session. */
2369 /* For cloned processes we must check both with __WCLONE and
2370 without, since the exit status of a cloned process isn't reported
2376 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2377 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2379 fprintf_unfiltered (gdb_stdlog
,
2380 "KWC: wait %s received unknown.\n",
2381 target_pid_to_str (lp
->ptid
));
2384 while (pid
== GET_LWP (lp
->ptid
));
2386 gdb_assert (pid
== -1 && errno
== ECHILD
);
2391 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2392 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2394 fprintf_unfiltered (gdb_stdlog
,
2395 "KWC: wait %s received unk.\n",
2396 target_pid_to_str (lp
->ptid
));
2399 while (pid
== GET_LWP (lp
->ptid
));
2401 gdb_assert (pid
== -1 && errno
== ECHILD
);
2406 linux_nat_kill (void)
2408 /* Kill all LWP's ... */
2409 iterate_over_lwps (kill_callback
, NULL
);
2411 /* ... and wait until we've flushed all events. */
2412 iterate_over_lwps (kill_wait_callback
, NULL
);
2414 target_mourn_inferior ();
2418 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2421 linux_ops
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2425 linux_nat_mourn_inferior (void)
2427 trap_ptid
= null_ptid
;
2429 /* Destroy LWP info; it's no longer valid. */
2432 /* Restore the original signal mask. */
2433 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2434 sigemptyset (&blocked_mask
);
2436 linux_ops
->to_mourn_inferior ();
2440 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2441 const char *annex
, gdb_byte
*readbuf
,
2442 const gdb_byte
*writebuf
,
2443 ULONGEST offset
, LONGEST len
)
2445 struct cleanup
*old_chain
= save_inferior_ptid ();
2448 if (is_lwp (inferior_ptid
))
2449 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2451 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
2454 do_cleanups (old_chain
);
2459 linux_nat_thread_alive (ptid_t ptid
)
2461 gdb_assert (is_lwp (ptid
));
2464 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2465 if (debug_linux_nat
)
2466 fprintf_unfiltered (gdb_stdlog
,
2467 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2468 target_pid_to_str (ptid
),
2469 errno
? safe_strerror (errno
) : "OK");
2477 linux_nat_pid_to_str (ptid_t ptid
)
2479 static char buf
[64];
2483 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2487 return normal_pid_to_str (ptid
);
2491 linux_nat_fetch_registers (int regnum
)
2493 /* to_fetch_registers will honor the LWP ID, so we can use it directly. */
2494 linux_ops
->to_fetch_registers (regnum
);
2498 linux_nat_store_registers (int regnum
)
2500 /* to_store_registers will honor the LWP ID, so we can use it directly. */
2501 linux_ops
->to_store_registers (regnum
);
2505 linux_nat_child_post_startup_inferior (ptid_t ptid
)
2507 linux_ops
->to_post_startup_inferior (ptid
);
2511 init_linux_nat_ops (void)
2514 linux_nat_ops
.to_open
= linux_nat_open
;
2516 linux_nat_ops
.to_shortname
= "lwp-layer";
2517 linux_nat_ops
.to_longname
= "lwp-layer";
2518 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2519 linux_nat_ops
.to_attach
= linux_nat_attach
;
2520 linux_nat_ops
.to_detach
= linux_nat_detach
;
2521 linux_nat_ops
.to_resume
= linux_nat_resume
;
2522 linux_nat_ops
.to_wait
= linux_nat_wait
;
2523 linux_nat_ops
.to_fetch_registers
= linux_nat_fetch_registers
;
2524 linux_nat_ops
.to_store_registers
= linux_nat_store_registers
;
2525 linux_nat_ops
.to_xfer_partial
= linux_nat_xfer_partial
;
2526 linux_nat_ops
.to_kill
= linux_nat_kill
;
2527 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2528 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2529 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2530 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2531 linux_nat_ops
.to_post_startup_inferior
2532 = linux_nat_child_post_startup_inferior
;
2533 linux_nat_ops
.to_post_attach
= child_post_attach
;
2534 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2535 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2536 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2538 linux_nat_ops
.to_stratum
= thread_stratum
;
2539 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2540 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2544 sigchld_handler (int signo
)
2546 /* Do nothing. The only reason for this handler is that it allows
2547 us to use sigsuspend in linux_nat_wait above to wait for the
2548 arrival of a SIGCHLD. */
2551 /* Accepts an integer PID; Returns a string representing a file that
2552 can be opened to get the symbols for the child process. */
2555 child_pid_to_exec_file (int pid
)
2557 char *name1
, *name2
;
2559 name1
= xmalloc (MAXPATHLEN
);
2560 name2
= xmalloc (MAXPATHLEN
);
2561 make_cleanup (xfree
, name1
);
2562 make_cleanup (xfree
, name2
);
2563 memset (name2
, 0, MAXPATHLEN
);
2565 sprintf (name1
, "/proc/%d/exe", pid
);
2566 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2572 /* Service function for corefiles and info proc. */
2575 read_mapping (FILE *mapfile
,
2580 char *device
, long long *inode
, char *filename
)
2582 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2583 addr
, endaddr
, permissions
, offset
, device
, inode
);
2586 if (ret
> 0 && ret
!= EOF
)
2588 /* Eat everything up to EOL for the filename. This will prevent
2589 weird filenames (such as one with embedded whitespace) from
2590 confusing this code. It also makes this code more robust in
2591 respect to annotations the kernel may add after the filename.
2593 Note the filename is used for informational purposes
2595 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2598 return (ret
!= 0 && ret
!= EOF
);
2601 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2602 regions in the inferior for a corefile. */
2605 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2607 int, int, int, void *), void *obfd
)
2609 long long pid
= PIDGET (inferior_ptid
);
2610 char mapsfilename
[MAXPATHLEN
];
2612 long long addr
, endaddr
, size
, offset
, inode
;
2613 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2614 int read
, write
, exec
;
2617 /* Compose the filename for the /proc memory map, and open it. */
2618 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2619 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2620 error (_("Could not open %s."), mapsfilename
);
2623 fprintf_filtered (gdb_stdout
,
2624 "Reading memory regions from %s\n", mapsfilename
);
2626 /* Now iterate until end-of-file. */
2627 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2628 &offset
, &device
[0], &inode
, &filename
[0]))
2630 size
= endaddr
- addr
;
2632 /* Get the segment's permissions. */
2633 read
= (strchr (permissions
, 'r') != 0);
2634 write
= (strchr (permissions
, 'w') != 0);
2635 exec
= (strchr (permissions
, 'x') != 0);
2639 fprintf_filtered (gdb_stdout
,
2640 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2641 size
, paddr_nz (addr
),
2643 write
? 'w' : ' ', exec
? 'x' : ' ');
2644 if (filename
&& filename
[0])
2645 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2646 fprintf_filtered (gdb_stdout
, "\n");
2649 /* Invoke the callback function to create the corefile
2651 func (addr
, size
, read
, write
, exec
, obfd
);
2657 /* Records the thread's register state for the corefile note
2661 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2662 char *note_data
, int *note_size
)
2664 gdb_gregset_t gregs
;
2665 gdb_fpregset_t fpregs
;
2666 #ifdef FILL_FPXREGSET
2667 gdb_fpxregset_t fpxregs
;
2669 unsigned long lwp
= ptid_get_lwp (ptid
);
2671 fill_gregset (&gregs
, -1);
2672 note_data
= (char *) elfcore_write_prstatus (obfd
,
2676 stop_signal
, &gregs
);
2678 fill_fpregset (&fpregs
, -1);
2679 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2682 &fpregs
, sizeof (fpregs
));
2683 #ifdef FILL_FPXREGSET
2684 fill_fpxregset (&fpxregs
, -1);
2685 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2688 &fpxregs
, sizeof (fpxregs
));
2693 struct linux_nat_corefile_thread_data
2701 /* Called by gdbthread.c once per thread. Records the thread's
2702 register state for the corefile note section. */
2705 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2707 struct linux_nat_corefile_thread_data
*args
= data
;
2708 ptid_t saved_ptid
= inferior_ptid
;
2710 inferior_ptid
= ti
->ptid
;
2711 registers_changed ();
2712 target_fetch_registers (-1); /* FIXME should not be necessary;
2713 fill_gregset should do it automatically. */
2714 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2719 inferior_ptid
= saved_ptid
;
2720 registers_changed ();
2721 target_fetch_registers (-1); /* FIXME should not be necessary;
2722 fill_gregset should do it automatically. */
2726 /* Records the register state for the corefile note section. */
2729 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2730 char *note_data
, int *note_size
)
2732 registers_changed ();
2733 target_fetch_registers (-1); /* FIXME should not be necessary;
2734 fill_gregset should do it automatically. */
2735 return linux_nat_do_thread_registers (obfd
,
2736 ptid_build (ptid_get_pid (inferior_ptid
),
2737 ptid_get_pid (inferior_ptid
),
2739 note_data
, note_size
);
2743 /* Fills the "to_make_corefile_note" target vector. Builds the note
2744 section for a corefile, and returns it in a malloc buffer. */
2747 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2749 struct linux_nat_corefile_thread_data thread_args
;
2750 struct cleanup
*old_chain
;
2751 char fname
[16] = { '\0' };
2752 char psargs
[80] = { '\0' };
2753 char *note_data
= NULL
;
2754 ptid_t current_ptid
= inferior_ptid
;
2758 if (get_exec_file (0))
2760 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2761 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2762 if (get_inferior_args ())
2764 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2765 strncat (psargs
, get_inferior_args (),
2766 sizeof (psargs
) - strlen (psargs
));
2768 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2770 note_size
, fname
, psargs
);
2773 /* Dump information for threads. */
2774 thread_args
.obfd
= obfd
;
2775 thread_args
.note_data
= note_data
;
2776 thread_args
.note_size
= note_size
;
2777 thread_args
.num_notes
= 0;
2778 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2779 if (thread_args
.num_notes
== 0)
2781 /* iterate_over_threads didn't come up with any threads; just
2782 use inferior_ptid. */
2783 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2784 note_data
, note_size
);
2788 note_data
= thread_args
.note_data
;
2791 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2794 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2795 "CORE", NT_AUXV
, auxv
, auxv_len
);
2799 make_cleanup (xfree
, note_data
);
2803 /* Implement the "info proc" command. */
2806 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2808 long long pid
= PIDGET (inferior_ptid
);
2811 char buffer
[MAXPATHLEN
];
2812 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2825 /* Break up 'args' into an argv array. */
2826 if ((argv
= buildargv (args
)) == NULL
)
2829 make_cleanup_freeargv (argv
);
2831 while (argv
!= NULL
&& *argv
!= NULL
)
2833 if (isdigit (argv
[0][0]))
2835 pid
= strtoul (argv
[0], NULL
, 10);
2837 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2841 else if (strcmp (argv
[0], "status") == 0)
2845 else if (strcmp (argv
[0], "stat") == 0)
2849 else if (strcmp (argv
[0], "cmd") == 0)
2853 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2857 else if (strcmp (argv
[0], "cwd") == 0)
2861 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2867 /* [...] (future options here) */
2872 error (_("No current process: you must name one."));
2874 sprintf (fname1
, "/proc/%lld", pid
);
2875 if (stat (fname1
, &dummy
) != 0)
2876 error (_("No /proc directory: '%s'"), fname1
);
2878 printf_filtered (_("process %lld\n"), pid
);
2879 if (cmdline_f
|| all
)
2881 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2882 if ((procfile
= fopen (fname1
, "r")) > 0)
2884 fgets (buffer
, sizeof (buffer
), procfile
);
2885 printf_filtered ("cmdline = '%s'\n", buffer
);
2889 warning (_("unable to open /proc file '%s'"), fname1
);
2893 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2894 memset (fname2
, 0, sizeof (fname2
));
2895 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2896 printf_filtered ("cwd = '%s'\n", fname2
);
2898 warning (_("unable to read link '%s'"), fname1
);
2902 sprintf (fname1
, "/proc/%lld/exe", pid
);
2903 memset (fname2
, 0, sizeof (fname2
));
2904 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2905 printf_filtered ("exe = '%s'\n", fname2
);
2907 warning (_("unable to read link '%s'"), fname1
);
2909 if (mappings_f
|| all
)
2911 sprintf (fname1
, "/proc/%lld/maps", pid
);
2912 if ((procfile
= fopen (fname1
, "r")) > 0)
2914 long long addr
, endaddr
, size
, offset
, inode
;
2915 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2917 printf_filtered (_("Mapped address spaces:\n\n"));
2918 if (TARGET_ADDR_BIT
== 32)
2920 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2923 " Size", " Offset", "objfile");
2927 printf_filtered (" %18s %18s %10s %10s %7s\n",
2930 " Size", " Offset", "objfile");
2933 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2934 &offset
, &device
[0], &inode
, &filename
[0]))
2936 size
= endaddr
- addr
;
2938 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2939 calls here (and possibly above) should be abstracted
2940 out into their own functions? Andrew suggests using
2941 a generic local_address_string instead to print out
2942 the addresses; that makes sense to me, too. */
2944 if (TARGET_ADDR_BIT
== 32)
2946 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2947 (unsigned long) addr
, /* FIXME: pr_addr */
2948 (unsigned long) endaddr
,
2950 (unsigned int) offset
,
2951 filename
[0] ? filename
: "");
2955 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2956 (unsigned long) addr
, /* FIXME: pr_addr */
2957 (unsigned long) endaddr
,
2959 (unsigned int) offset
,
2960 filename
[0] ? filename
: "");
2967 warning (_("unable to open /proc file '%s'"), fname1
);
2969 if (status_f
|| all
)
2971 sprintf (fname1
, "/proc/%lld/status", pid
);
2972 if ((procfile
= fopen (fname1
, "r")) > 0)
2974 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2975 puts_filtered (buffer
);
2979 warning (_("unable to open /proc file '%s'"), fname1
);
2983 sprintf (fname1
, "/proc/%lld/stat", pid
);
2984 if ((procfile
= fopen (fname1
, "r")) > 0)
2989 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2990 printf_filtered (_("Process: %d\n"), itmp
);
2991 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2992 printf_filtered (_("Exec file: %s\n"), buffer
);
2993 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2994 printf_filtered (_("State: %c\n"), ctmp
);
2995 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2996 printf_filtered (_("Parent process: %d\n"), itmp
);
2997 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2998 printf_filtered (_("Process group: %d\n"), itmp
);
2999 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3000 printf_filtered (_("Session id: %d\n"), itmp
);
3001 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3002 printf_filtered (_("TTY: %d\n"), itmp
);
3003 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3004 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3005 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3006 printf_filtered (_("Flags: 0x%x\n"), itmp
);
3007 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3008 printf_filtered (_("Minor faults (no memory page): %u\n"),
3009 (unsigned int) itmp
);
3010 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3011 printf_filtered (_("Minor faults, children: %u\n"),
3012 (unsigned int) itmp
);
3013 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3014 printf_filtered (_("Major faults (memory page faults): %u\n"),
3015 (unsigned int) itmp
);
3016 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3017 printf_filtered (_("Major faults, children: %u\n"),
3018 (unsigned int) itmp
);
3019 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3020 printf_filtered ("utime: %d\n", itmp
);
3021 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3022 printf_filtered ("stime: %d\n", itmp
);
3023 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3024 printf_filtered ("utime, children: %d\n", itmp
);
3025 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3026 printf_filtered ("stime, children: %d\n", itmp
);
3027 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3028 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
3030 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3031 printf_filtered ("'nice' value: %d\n", itmp
);
3032 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3033 printf_filtered (_("jiffies until next timeout: %u\n"),
3034 (unsigned int) itmp
);
3035 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3036 printf_filtered ("jiffies until next SIGALRM: %u\n",
3037 (unsigned int) itmp
);
3038 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3039 printf_filtered (_("start time (jiffies since system boot): %d\n"),
3041 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3042 printf_filtered (_("Virtual memory size: %u\n"),
3043 (unsigned int) itmp
);
3044 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3045 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
3046 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3047 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
3048 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3049 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
3050 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3051 printf_filtered (_("End of text: 0x%x\n"), itmp
);
3052 if (fscanf (procfile
, "%u ", &itmp
) > 0)
3053 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
3054 #if 0 /* Don't know how architecture-dependent the rest is...
3055 Anyway the signal bitmap info is available from "status". */
3056 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3057 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
3058 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3059 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
3060 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3061 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
3062 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3063 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
3064 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3065 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
3066 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3067 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
3068 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
3069 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
3074 warning (_("unable to open /proc file '%s'"), fname1
);
3078 /* Implement the to_xfer_partial interface for memory reads using the /proc
3079 filesystem. Because we can use a single read() call for /proc, this
3080 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3081 but it doesn't support writes. */
3084 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3085 const char *annex
, gdb_byte
*readbuf
,
3086 const gdb_byte
*writebuf
,
3087 ULONGEST offset
, LONGEST len
)
3093 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3096 /* Don't bother for one word. */
3097 if (len
< 3 * sizeof (long))
3100 /* We could keep this file open and cache it - possibly one per
3101 thread. That requires some juggling, but is even faster. */
3102 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3103 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3107 /* If pread64 is available, use it. It's faster if the kernel
3108 supports it (only one syscall), and it's 64-bit safe even on
3109 32-bit platforms (for instance, SPARC debugging a SPARC64
3112 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3114 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3124 /* Parse LINE as a signal set and add its set bits to SIGS. */
3127 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3129 int len
= strlen (line
) - 1;
3133 if (line
[len
] != '\n')
3134 error (_("Could not parse signal set: %s"), line
);
3142 if (*p
>= '0' && *p
<= '9')
3144 else if (*p
>= 'a' && *p
<= 'f')
3145 digit
= *p
- 'a' + 10;
3147 error (_("Could not parse signal set: %s"), line
);
3152 sigaddset (sigs
, signum
+ 1);
3154 sigaddset (sigs
, signum
+ 2);
3156 sigaddset (sigs
, signum
+ 3);
3158 sigaddset (sigs
, signum
+ 4);
3164 /* Find process PID's pending signals from /proc/pid/status and set
3168 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3171 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3174 sigemptyset (pending
);
3175 sigemptyset (blocked
);
3176 sigemptyset (ignored
);
3177 sprintf (fname
, "/proc/%d/status", pid
);
3178 procfile
= fopen (fname
, "r");
3179 if (procfile
== NULL
)
3180 error (_("Could not open %s"), fname
);
3182 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3184 /* Normal queued signals are on the SigPnd line in the status
3185 file. However, 2.6 kernels also have a "shared" pending
3186 queue for delivering signals to a thread group, so check for
3189 Unfortunately some Red Hat kernels include the shared pending
3190 queue but not the ShdPnd status field. */
3192 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3193 add_line_to_sigset (buffer
+ 8, pending
);
3194 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3195 add_line_to_sigset (buffer
+ 8, pending
);
3196 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3197 add_line_to_sigset (buffer
+ 8, blocked
);
3198 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3199 add_line_to_sigset (buffer
+ 8, ignored
);
3206 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3207 const char *annex
, gdb_byte
*readbuf
,
3208 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3212 if (object
== TARGET_OBJECT_AUXV
)
3213 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3216 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3221 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3225 #ifndef FETCH_INFERIOR_REGISTERS
3227 /* Return the address in the core dump or inferior of register
3231 linux_register_u_offset (int regno
)
3233 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
3234 away. This requires disentangling the various definitions of it
3235 (particularly alpha-nat.c's). */
3236 return register_addr (regno
, 0);
3241 /* Create a prototype generic Linux target. The client can override
3242 it with local methods. */
3247 struct target_ops
*t
;
3249 #ifdef FETCH_INFERIOR_REGISTERS
3250 t
= inf_ptrace_target ();
3252 t
= inf_ptrace_trad_target (linux_register_u_offset
);
3254 t
->to_wait
= child_wait
;
3255 t
->to_kill
= kill_inferior
;
3256 t
->to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
3257 t
->to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
3258 t
->to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
3259 t
->to_pid_to_exec_file
= child_pid_to_exec_file
;
3260 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3261 t
->to_post_attach
= child_post_attach
;
3262 t
->to_follow_fork
= child_follow_fork
;
3263 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3264 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3266 super_xfer_partial
= t
->to_xfer_partial
;
3267 t
->to_xfer_partial
= linux_xfer_partial
;
3269 super_mourn_inferior
= t
->to_mourn_inferior
;
3270 t
->to_mourn_inferior
= child_mourn_inferior
;
3277 _initialize_linux_nat (void)
3279 struct sigaction action
;
3280 extern void thread_db_init (struct target_ops
*);
3282 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3283 Show /proc process information about any running process.\n\
3284 Specify any process id, or use the program being debugged by default.\n\
3285 Specify any of the following keywords for detailed info:\n\
3286 mappings -- list of mapped memory regions.\n\
3287 stat -- list a bunch of random process info.\n\
3288 status -- list a different bunch of random process info.\n\
3289 all -- list all available /proc info."));
3291 init_linux_nat_ops ();
3292 add_target (&linux_nat_ops
);
3293 thread_db_init (&linux_nat_ops
);
3295 /* Save the original signal mask. */
3296 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3298 action
.sa_handler
= sigchld_handler
;
3299 sigemptyset (&action
.sa_mask
);
3300 action
.sa_flags
= SA_RESTART
;
3301 sigaction (SIGCHLD
, &action
, NULL
);
3303 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3304 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3305 sigdelset (&suspend_mask
, SIGCHLD
);
3307 sigemptyset (&blocked_mask
);
3309 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3310 Set debugging of GNU/Linux lwp module."), _("\
3311 Show debugging of GNU/Linux lwp module."), _("\
3312 Enables printf debugging output."),
3314 show_debug_linux_nat
,
3315 &setdebuglist
, &showdebuglist
);
3319 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3320 the GNU/Linux Threads library and therefore doesn't really belong
3323 /* Read variable NAME in the target and return its value if found.
3324 Otherwise return zero. It is assumed that the type of the variable
3328 get_signo (const char *name
)
3330 struct minimal_symbol
*ms
;
3333 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3337 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
3338 sizeof (signo
)) != 0)
3344 /* Return the set of signals used by the threads library in *SET. */
3347 lin_thread_get_thread_signals (sigset_t
*set
)
3349 struct sigaction action
;
3350 int restart
, cancel
;
3354 restart
= get_signo ("__pthread_sig_restart");
3358 cancel
= get_signo ("__pthread_sig_cancel");
3362 sigaddset (set
, restart
);
3363 sigaddset (set
, cancel
);
3365 /* The GNU/Linux Threads library makes terminating threads send a
3366 special "cancel" signal instead of SIGCHLD. Make sure we catch
3367 those (to prevent them from terminating GDB itself, which is
3368 likely to be their default action) and treat them the same way as
3371 action
.sa_handler
= sigchld_handler
;
3372 sigemptyset (&action
.sa_mask
);
3373 action
.sa_flags
= SA_RESTART
;
3374 sigaction (cancel
, &action
, NULL
);
3376 /* We block the "cancel" signal throughout this code ... */
3377 sigaddset (&blocked_mask
, cancel
);
3378 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3380 /* ... except during a sigsuspend. */
3381 sigdelset (&suspend_mask
, cancel
);