1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 #include "gdb_string.h"
28 #include "gdb_assert.h"
29 #ifdef HAVE_TKILL_SYSCALL
31 #include <sys/syscall.h>
33 #include <sys/ptrace.h>
34 #include "linux-nat.h"
35 #include "linux-fork.h"
36 #include "gdbthread.h"
39 #include "inf-ptrace.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
55 /* If the system headers did not provide the constants, hard-code the normal
57 #ifndef PTRACE_EVENT_FORK
59 #define PTRACE_SETOPTIONS 0x4200
60 #define PTRACE_GETEVENTMSG 0x4201
62 /* options set using PTRACE_SETOPTIONS */
63 #define PTRACE_O_TRACESYSGOOD 0x00000001
64 #define PTRACE_O_TRACEFORK 0x00000002
65 #define PTRACE_O_TRACEVFORK 0x00000004
66 #define PTRACE_O_TRACECLONE 0x00000008
67 #define PTRACE_O_TRACEEXEC 0x00000010
68 #define PTRACE_O_TRACEVFORKDONE 0x00000020
69 #define PTRACE_O_TRACEEXIT 0x00000040
71 /* Wait extended result codes for the above trace options. */
72 #define PTRACE_EVENT_FORK 1
73 #define PTRACE_EVENT_VFORK 2
74 #define PTRACE_EVENT_CLONE 3
75 #define PTRACE_EVENT_EXEC 4
76 #define PTRACE_EVENT_VFORK_DONE 5
77 #define PTRACE_EVENT_EXIT 6
79 #endif /* PTRACE_EVENT_FORK */
81 /* We can't always assume that this flag is available, but all systems
82 with the ptrace event handlers also have __WALL, so it's safe to use
85 #define __WALL 0x40000000 /* Wait for any child. */
88 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
89 the use of the multi-threaded target. */
90 static struct target_ops
*linux_ops
;
91 static struct target_ops linux_ops_saved
;
93 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
94 Called by our to_xfer_partial. */
95 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
97 const char *, gdb_byte
*,
101 static int debug_linux_nat
;
103 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
104 struct cmd_list_element
*c
, const char *value
)
106 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
110 static int linux_parent_pid
;
112 struct simple_pid_list
115 struct simple_pid_list
*next
;
117 struct simple_pid_list
*stopped_pids
;
119 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
120 can not be used, 1 if it can. */
122 static int linux_supports_tracefork_flag
= -1;
124 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
125 PTRACE_O_TRACEVFORKDONE. */
127 static int linux_supports_tracevforkdone_flag
= -1;
130 /* Trivial list manipulation functions to keep track of a list of
131 new stopped processes. */
133 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
135 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
137 new_pid
->next
= *listp
;
142 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
144 struct simple_pid_list
**p
;
146 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
147 if ((*p
)->pid
== pid
)
149 struct simple_pid_list
*next
= (*p
)->next
;
158 linux_record_stopped_pid (int pid
)
160 add_to_pid_list (&stopped_pids
, pid
);
164 /* A helper function for linux_test_for_tracefork, called after fork (). */
167 linux_tracefork_child (void)
171 ptrace (PTRACE_TRACEME
, 0, 0, 0);
172 kill (getpid (), SIGSTOP
);
177 /* Wrapper function for waitpid which handles EINTR. */
180 my_waitpid (int pid
, int *status
, int flags
)
185 ret
= waitpid (pid
, status
, flags
);
187 while (ret
== -1 && errno
== EINTR
);
192 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
194 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
195 we know that the feature is not available. This may change the tracing
196 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
198 However, if it succeeds, we don't know for sure that the feature is
199 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
200 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
201 fork tracing, and let it fork. If the process exits, we assume that we
202 can't use TRACEFORK; if we get the fork notification, and we can extract
203 the new child's PID, then we assume that we can. */
206 linux_test_for_tracefork (int original_pid
)
208 int child_pid
, ret
, status
;
211 linux_supports_tracefork_flag
= 0;
212 linux_supports_tracevforkdone_flag
= 0;
214 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
220 perror_with_name (("fork"));
223 linux_tracefork_child ();
225 ret
= my_waitpid (child_pid
, &status
, 0);
227 perror_with_name (("waitpid"));
228 else if (ret
!= child_pid
)
229 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
230 if (! WIFSTOPPED (status
))
231 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
233 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
236 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
239 warning (_("linux_test_for_tracefork: failed to kill child"));
243 ret
= my_waitpid (child_pid
, &status
, 0);
244 if (ret
!= child_pid
)
245 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
246 else if (!WIFSIGNALED (status
))
247 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
248 "killed child"), status
);
253 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
254 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
255 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
256 linux_supports_tracevforkdone_flag
= (ret
== 0);
258 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
260 warning (_("linux_test_for_tracefork: failed to resume child"));
262 ret
= my_waitpid (child_pid
, &status
, 0);
264 if (ret
== child_pid
&& WIFSTOPPED (status
)
265 && status
>> 16 == PTRACE_EVENT_FORK
)
268 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
269 if (ret
== 0 && second_pid
!= 0)
273 linux_supports_tracefork_flag
= 1;
274 my_waitpid (second_pid
, &second_status
, 0);
275 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
277 warning (_("linux_test_for_tracefork: failed to kill second child"));
281 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
282 "(%d, status 0x%x)"), ret
, status
);
284 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
286 warning (_("linux_test_for_tracefork: failed to kill child"));
287 my_waitpid (child_pid
, &status
, 0);
290 /* Return non-zero iff we have tracefork functionality available.
291 This function also sets linux_supports_tracefork_flag. */
294 linux_supports_tracefork (int pid
)
296 if (linux_supports_tracefork_flag
== -1)
297 linux_test_for_tracefork (pid
);
298 return linux_supports_tracefork_flag
;
302 linux_supports_tracevforkdone (int pid
)
304 if (linux_supports_tracefork_flag
== -1)
305 linux_test_for_tracefork (pid
);
306 return linux_supports_tracevforkdone_flag
;
311 linux_enable_event_reporting (ptid_t ptid
)
313 int pid
= ptid_get_lwp (ptid
);
317 pid
= ptid_get_pid (ptid
);
319 if (! linux_supports_tracefork (pid
))
322 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
323 | PTRACE_O_TRACECLONE
;
324 if (linux_supports_tracevforkdone (pid
))
325 options
|= PTRACE_O_TRACEVFORKDONE
;
327 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
328 read-only process state. */
330 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
334 child_post_attach (int pid
)
336 linux_enable_event_reporting (pid_to_ptid (pid
));
340 linux_child_post_startup_inferior (ptid_t ptid
)
342 linux_enable_event_reporting (ptid
);
346 child_follow_fork (struct target_ops
*ops
, int follow_child
)
349 struct target_waitstatus last_status
;
351 int parent_pid
, child_pid
;
353 get_last_target_status (&last_ptid
, &last_status
);
354 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
355 parent_pid
= ptid_get_lwp (last_ptid
);
357 parent_pid
= ptid_get_pid (last_ptid
);
358 child_pid
= last_status
.value
.related_pid
;
362 /* We're already attached to the parent, by default. */
364 /* Before detaching from the child, remove all breakpoints from
365 it. (This won't actually modify the breakpoint list, but will
366 physically remove the breakpoints from the child.) */
367 /* If we vforked this will remove the breakpoints from the parent
368 also, but they'll be reinserted below. */
369 detach_breakpoints (child_pid
);
371 /* Detach new forked process? */
376 target_terminal_ours ();
377 fprintf_filtered (gdb_stdlog
,
378 "Detaching after fork from child process %d.\n",
382 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
386 struct fork_info
*fp
;
387 /* Retain child fork in ptrace (stopped) state. */
388 fp
= find_fork_pid (child_pid
);
390 fp
= add_fork (child_pid
);
391 fork_save_infrun_state (fp
, 0);
396 gdb_assert (linux_supports_tracefork_flag
>= 0);
397 if (linux_supports_tracevforkdone (0))
401 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
402 my_waitpid (parent_pid
, &status
, __WALL
);
403 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
404 warning (_("Unexpected waitpid result %06x when waiting for "
405 "vfork-done"), status
);
409 /* We can't insert breakpoints until the child has
410 finished with the shared memory region. We need to
411 wait until that happens. Ideal would be to just
413 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
414 - waitpid (parent_pid, &status, __WALL);
415 However, most architectures can't handle a syscall
416 being traced on the way out if it wasn't traced on
419 We might also think to loop, continuing the child
420 until it exits or gets a SIGTRAP. One problem is
421 that the child might call ptrace with PTRACE_TRACEME.
423 There's no simple and reliable way to figure out when
424 the vforked child will be done with its copy of the
425 shared memory. We could step it out of the syscall,
426 two instructions, let it go, and then single-step the
427 parent once. When we have hardware single-step, this
428 would work; with software single-step it could still
429 be made to work but we'd have to be able to insert
430 single-step breakpoints in the child, and we'd have
431 to insert -just- the single-step breakpoint in the
432 parent. Very awkward.
434 In the end, the best we can do is to make sure it
435 runs for a little while. Hopefully it will be out of
436 range of any breakpoints we reinsert. Usually this
437 is only the single-step breakpoint at vfork's return
443 /* Since we vforked, breakpoints were removed in the parent
444 too. Put them back. */
445 reattach_breakpoints (parent_pid
);
450 char child_pid_spelling
[40];
452 /* Needed to keep the breakpoint lists in sync. */
454 detach_breakpoints (child_pid
);
456 /* Before detaching from the parent, remove all breakpoints from it. */
457 remove_breakpoints ();
461 target_terminal_ours ();
462 fprintf_filtered (gdb_stdlog
,
463 "Attaching after fork to child process %d.\n",
467 /* If we're vforking, we may want to hold on to the parent until
468 the child exits or execs. At exec time we can remove the old
469 breakpoints from the parent and detach it; at exit time we
470 could do the same (or even, sneakily, resume debugging it - the
471 child's exec has failed, or something similar).
473 This doesn't clean up "properly", because we can't call
474 target_detach, but that's OK; if the current target is "child",
475 then it doesn't need any further cleanups, and lin_lwp will
476 generally not encounter vfork (vfork is defined to fork
479 The holding part is very easy if we have VFORKDONE events;
480 but keeping track of both processes is beyond GDB at the
481 moment. So we don't expose the parent to the rest of GDB.
482 Instead we quietly hold onto it until such time as we can
486 linux_parent_pid
= parent_pid
;
487 else if (!detach_fork
)
489 struct fork_info
*fp
;
490 /* Retain parent fork in ptrace (stopped) state. */
491 fp
= find_fork_pid (parent_pid
);
493 fp
= add_fork (parent_pid
);
494 fork_save_infrun_state (fp
, 0);
498 target_detach (NULL
, 0);
501 inferior_ptid
= pid_to_ptid (child_pid
);
503 /* Reinstall ourselves, since we might have been removed in
504 target_detach (which does other necessary cleanup). */
508 /* Reset breakpoints in the child as appropriate. */
509 follow_inferior_reset_breakpoints ();
516 linux_handle_extended_wait (int pid
, int status
,
517 struct target_waitstatus
*ourstatus
)
519 int event
= status
>> 16;
521 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
522 || event
== PTRACE_EVENT_CLONE
)
524 unsigned long new_pid
;
527 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
529 /* If we haven't already seen the new PID stop, wait for it now. */
530 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
532 /* The new child has a pending SIGSTOP. We can't affect it until it
533 hits the SIGSTOP, but we're already attached. */
534 ret
= my_waitpid (new_pid
, &status
,
535 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
537 perror_with_name (_("waiting for new child"));
538 else if (ret
!= new_pid
)
539 internal_error (__FILE__
, __LINE__
,
540 _("wait returned unexpected PID %d"), ret
);
541 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
542 internal_error (__FILE__
, __LINE__
,
543 _("wait returned unexpected status 0x%x"), status
);
546 if (event
== PTRACE_EVENT_FORK
)
547 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
548 else if (event
== PTRACE_EVENT_VFORK
)
549 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
551 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
553 ourstatus
->value
.related_pid
= new_pid
;
554 return inferior_ptid
;
557 if (event
== PTRACE_EVENT_EXEC
)
559 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
560 ourstatus
->value
.execd_pathname
561 = xstrdup (child_pid_to_exec_file (pid
));
563 if (linux_parent_pid
)
565 detach_breakpoints (linux_parent_pid
);
566 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
568 linux_parent_pid
= 0;
571 return inferior_ptid
;
574 internal_error (__FILE__
, __LINE__
,
575 _("unknown ptrace event %d"), event
);
580 child_insert_fork_catchpoint (int pid
)
582 if (! linux_supports_tracefork (pid
))
583 error (_("Your system does not support fork catchpoints."));
587 child_insert_vfork_catchpoint (int pid
)
589 if (!linux_supports_tracefork (pid
))
590 error (_("Your system does not support vfork catchpoints."));
594 child_insert_exec_catchpoint (int pid
)
596 if (!linux_supports_tracefork (pid
))
597 error (_("Your system does not support exec catchpoints."));
600 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
601 are processes sharing the same VM space. A multi-threaded process
602 is basically a group of such processes. However, such a grouping
603 is almost entirely a user-space issue; the kernel doesn't enforce
604 such a grouping at all (this might change in the future). In
605 general, we'll rely on the threads library (i.e. the GNU/Linux
606 Threads library) to provide such a grouping.
608 It is perfectly well possible to write a multi-threaded application
609 without the assistance of a threads library, by using the clone
610 system call directly. This module should be able to give some
611 rudimentary support for debugging such applications if developers
612 specify the CLONE_PTRACE flag in the clone system call, and are
613 using the Linux kernel 2.4 or above.
615 Note that there are some peculiarities in GNU/Linux that affect
618 - In general one should specify the __WCLONE flag to waitpid in
619 order to make it report events for any of the cloned processes
620 (and leave it out for the initial process). However, if a cloned
621 process has exited the exit status is only reported if the
622 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
623 we cannot use it since GDB must work on older systems too.
625 - When a traced, cloned process exits and is waited for by the
626 debugger, the kernel reassigns it to the original parent and
627 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
628 library doesn't notice this, which leads to the "zombie problem":
629 When debugged a multi-threaded process that spawns a lot of
630 threads will run out of processes, even if the threads exit,
631 because the "zombies" stay around. */
633 /* List of known LWPs. */
634 static struct lwp_info
*lwp_list
;
636 /* Number of LWPs in the list. */
640 #define GET_LWP(ptid) ptid_get_lwp (ptid)
641 #define GET_PID(ptid) ptid_get_pid (ptid)
642 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
643 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
645 /* If the last reported event was a SIGTRAP, this variable is set to
646 the process id of the LWP/thread that got it. */
650 /* Since we cannot wait (in linux_nat_wait) for the initial process and
651 any cloned processes with a single call to waitpid, we have to use
652 the WNOHANG flag and call waitpid in a loop. To optimize
653 things a bit we use `sigsuspend' to wake us up when a process has
654 something to report (it will send us a SIGCHLD if it has). To make
655 this work we have to juggle with the signal mask. We save the
656 original signal mask such that we can restore it before creating a
657 new process in order to avoid blocking certain signals in the
658 inferior. We then block SIGCHLD during the waitpid/sigsuspend
661 /* Original signal mask. */
662 static sigset_t normal_mask
;
664 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
665 _initialize_linux_nat. */
666 static sigset_t suspend_mask
;
668 /* Signals to block to make that sigsuspend work. */
669 static sigset_t blocked_mask
;
672 /* Prototypes for local functions. */
673 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
674 static int linux_nat_thread_alive (ptid_t ptid
);
676 /* Convert wait status STATUS to a string. Used for printing debug
680 status_to_str (int status
)
684 if (WIFSTOPPED (status
))
685 snprintf (buf
, sizeof (buf
), "%s (stopped)",
686 strsignal (WSTOPSIG (status
)));
687 else if (WIFSIGNALED (status
))
688 snprintf (buf
, sizeof (buf
), "%s (terminated)",
689 strsignal (WSTOPSIG (status
)));
691 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
696 /* Initialize the list of LWPs. Note that this module, contrary to
697 what GDB's generic threads layer does for its thread list,
698 re-initializes the LWP lists whenever we mourn or detach (which
699 doesn't involve mourning) the inferior. */
704 struct lwp_info
*lp
, *lpnext
;
706 for (lp
= lwp_list
; lp
; lp
= lpnext
)
716 /* Add the LWP specified by PID to the list. Return a pointer to the
717 structure describing the new LWP. */
719 static struct lwp_info
*
720 add_lwp (ptid_t ptid
)
724 gdb_assert (is_lwp (ptid
));
726 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
728 memset (lp
, 0, sizeof (struct lwp_info
));
730 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
741 /* Remove the LWP specified by PID from the list. */
744 delete_lwp (ptid_t ptid
)
746 struct lwp_info
*lp
, *lpprev
;
750 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
751 if (ptid_equal (lp
->ptid
, ptid
))
760 lpprev
->next
= lp
->next
;
767 /* Return a pointer to the structure describing the LWP corresponding
768 to PID. If no corresponding LWP could be found, return NULL. */
770 static struct lwp_info
*
771 find_lwp_pid (ptid_t ptid
)
777 lwp
= GET_LWP (ptid
);
779 lwp
= GET_PID (ptid
);
781 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
782 if (lwp
== GET_LWP (lp
->ptid
))
788 /* Call CALLBACK with its second argument set to DATA for every LWP in
789 the list. If CALLBACK returns 1 for a particular LWP, return a
790 pointer to the structure describing that LWP immediately.
791 Otherwise return NULL. */
794 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
796 struct lwp_info
*lp
, *lpnext
;
798 for (lp
= lwp_list
; lp
; lp
= lpnext
)
801 if ((*callback
) (lp
, data
))
808 /* Update our internal state when changing from one fork (checkpoint,
809 et cetera) to another indicated by NEW_PTID. We can only switch
810 single-threaded applications, so we only create one new LWP, and
811 the previous list is discarded. */
814 linux_nat_switch_fork (ptid_t new_ptid
)
819 lp
= add_lwp (new_ptid
);
823 /* Record a PTID for later deletion. */
828 struct saved_ptids
*next
;
830 static struct saved_ptids
*threads_to_delete
;
833 record_dead_thread (ptid_t ptid
)
835 struct saved_ptids
*p
= xmalloc (sizeof (struct saved_ptids
));
837 p
->next
= threads_to_delete
;
838 threads_to_delete
= p
;
841 /* Delete any dead threads which are not the current thread. */
846 struct saved_ptids
**p
= &threads_to_delete
;
849 if (! ptid_equal ((*p
)->ptid
, inferior_ptid
))
851 struct saved_ptids
*tmp
= *p
;
852 delete_thread (tmp
->ptid
);
860 /* Callback for iterate_over_threads that finds a thread corresponding
864 find_thread_from_lwp (struct thread_info
*thr
, void *dummy
)
866 ptid_t
*ptid_p
= dummy
;
868 if (GET_LWP (thr
->ptid
) && GET_LWP (thr
->ptid
) == GET_LWP (*ptid_p
))
874 /* Handle the exit of a single thread LP. */
877 exit_lwp (struct lwp_info
*lp
)
879 if (in_thread_list (lp
->ptid
))
881 /* Core GDB cannot deal with us deleting the current thread. */
882 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
883 delete_thread (lp
->ptid
);
885 record_dead_thread (lp
->ptid
);
886 printf_unfiltered (_("[%s exited]\n"),
887 target_pid_to_str (lp
->ptid
));
891 /* Even if LP->PTID is not in the global GDB thread list, the
892 LWP may be - with an additional thread ID. We don't need
893 to print anything in this case; thread_db is in use and
894 already took care of that. But it didn't delete the thread
895 in order to handle zombies correctly. */
897 struct thread_info
*thr
;
899 thr
= iterate_over_threads (find_thread_from_lwp
, &lp
->ptid
);
900 if (thr
&& !ptid_equal (thr
->ptid
, inferior_ptid
))
901 delete_thread (thr
->ptid
);
903 record_dead_thread (thr
->ptid
);
906 delete_lwp (lp
->ptid
);
909 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
910 a message telling the user that a new LWP has been added to the
914 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
916 struct lwp_info
*lp
, *found_lp
;
918 gdb_assert (is_lwp (ptid
));
920 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
921 to interrupt either the ptrace() or waitpid() calls below. */
922 if (!sigismember (&blocked_mask
, SIGCHLD
))
924 sigaddset (&blocked_mask
, SIGCHLD
);
925 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
929 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
931 found_lp
= lp
= find_lwp_pid (ptid
);
935 /* We assume that we're already attached to any LWP that has an id
936 equal to the overall process id, and to any LWP that is already
937 in our list of LWPs. If we're not seeing exit events from threads
938 and we've had PID wraparound since we last tried to stop all threads,
939 this assumption might be wrong; fortunately, this is very unlikely
941 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
946 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
947 error (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
948 safe_strerror (errno
));
951 fprintf_unfiltered (gdb_stdlog
,
952 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
953 target_pid_to_str (ptid
));
955 pid
= my_waitpid (GET_LWP (ptid
), &status
, 0);
956 if (pid
== -1 && errno
== ECHILD
)
958 /* Try again with __WCLONE to check cloned processes. */
959 pid
= my_waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
963 gdb_assert (pid
== GET_LWP (ptid
)
964 && WIFSTOPPED (status
) && WSTOPSIG (status
));
966 child_post_attach (pid
);
972 fprintf_unfiltered (gdb_stdlog
,
973 "LLAL: waitpid %s received %s\n",
974 target_pid_to_str (ptid
),
975 status_to_str (status
));
980 /* We assume that the LWP representing the original process is
981 already stopped. Mark it as stopped in the data structure
982 that the linux ptrace layer uses to keep track of threads.
983 Note that this won't have already been done since the main
984 thread will have, we assume, been stopped by an attach from a
991 linux_nat_attach (char *args
, int from_tty
)
997 /* FIXME: We should probably accept a list of process id's, and
998 attach all of them. */
999 linux_ops
->to_attach (args
, from_tty
);
1001 /* Add the initial process as the first LWP to the list. */
1002 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1003 lp
= add_lwp (inferior_ptid
);
1005 /* Make sure the initial process is stopped. The user-level threads
1006 layer might want to poke around in the inferior, and that won't
1007 work if things haven't stabilized yet. */
1008 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, 0);
1009 if (pid
== -1 && errno
== ECHILD
)
1011 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid
));
1013 /* Try again with __WCLONE to check cloned processes. */
1014 pid
= my_waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
1018 gdb_assert (pid
== GET_PID (inferior_ptid
)
1019 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
1023 /* Fake the SIGSTOP that core GDB expects. */
1024 lp
->status
= W_STOPCODE (SIGSTOP
);
1026 if (debug_linux_nat
)
1028 fprintf_unfiltered (gdb_stdlog
,
1029 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
1034 detach_callback (struct lwp_info
*lp
, void *data
)
1036 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1038 if (debug_linux_nat
&& lp
->status
)
1039 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1040 strsignal (WSTOPSIG (lp
->status
)),
1041 target_pid_to_str (lp
->ptid
));
1043 while (lp
->signalled
&& lp
->stopped
)
1046 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
1047 WSTOPSIG (lp
->status
)) < 0)
1048 error (_("Can't continue %s: %s"), target_pid_to_str (lp
->ptid
),
1049 safe_strerror (errno
));
1051 if (debug_linux_nat
)
1052 fprintf_unfiltered (gdb_stdlog
,
1053 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
1054 target_pid_to_str (lp
->ptid
),
1055 status_to_str (lp
->status
));
1060 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
1061 here. But since lp->signalled was cleared above,
1062 stop_wait_callback didn't do anything; the process was left
1063 running. Shouldn't we be waiting for it to stop?
1064 I've removed the call, since stop_wait_callback now does do
1065 something when called with lp->signalled == 0. */
1067 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1070 /* We don't actually detach from the LWP that has an id equal to the
1071 overall process id just yet. */
1072 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1075 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1076 WSTOPSIG (lp
->status
)) < 0)
1077 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1078 safe_strerror (errno
));
1080 if (debug_linux_nat
)
1081 fprintf_unfiltered (gdb_stdlog
,
1082 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1083 target_pid_to_str (lp
->ptid
),
1084 strsignal (WSTOPSIG (lp
->status
)));
1086 delete_lwp (lp
->ptid
);
1093 linux_nat_detach (char *args
, int from_tty
)
1095 iterate_over_lwps (detach_callback
, NULL
);
1097 /* Only the initial process should be left right now. */
1098 gdb_assert (num_lwps
== 1);
1100 trap_ptid
= null_ptid
;
1102 /* Destroy LWP info; it's no longer valid. */
1105 /* Restore the original signal mask. */
1106 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1107 sigemptyset (&blocked_mask
);
1109 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1110 linux_ops
->to_detach (args
, from_tty
);
1116 resume_callback (struct lwp_info
*lp
, void *data
)
1118 if (lp
->stopped
&& lp
->status
== 0)
1120 struct thread_info
*tp
;
1122 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1123 0, TARGET_SIGNAL_0
);
1124 if (debug_linux_nat
)
1125 fprintf_unfiltered (gdb_stdlog
,
1126 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1127 target_pid_to_str (lp
->ptid
));
1136 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1143 resume_set_callback (struct lwp_info
*lp
, void *data
)
1150 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1152 struct lwp_info
*lp
;
1155 if (debug_linux_nat
)
1156 fprintf_unfiltered (gdb_stdlog
,
1157 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1158 step
? "step" : "resume",
1159 target_pid_to_str (ptid
),
1160 signo
? strsignal (signo
) : "0",
1161 target_pid_to_str (inferior_ptid
));
1165 /* A specific PTID means `step only this process id'. */
1166 resume_all
= (PIDGET (ptid
) == -1);
1169 iterate_over_lwps (resume_set_callback
, NULL
);
1171 iterate_over_lwps (resume_clear_callback
, NULL
);
1173 /* If PID is -1, it's the current inferior that should be
1174 handled specially. */
1175 if (PIDGET (ptid
) == -1)
1176 ptid
= inferior_ptid
;
1178 lp
= find_lwp_pid (ptid
);
1181 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1183 /* Remember if we're stepping. */
1186 /* Mark this LWP as resumed. */
1189 /* If we have a pending wait status for this thread, there is no
1190 point in resuming the process. But first make sure that
1191 linux_nat_wait won't preemptively handle the event - we
1192 should never take this short-circuit if we are going to
1193 leave LP running, since we have skipped resuming all the
1194 other threads. This bit of code needs to be synchronized
1195 with linux_nat_wait. */
1197 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1199 int saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1201 if (signal_stop_state (saved_signo
) == 0
1202 && signal_print_state (saved_signo
) == 0
1203 && signal_pass_state (saved_signo
) == 1)
1205 if (debug_linux_nat
)
1206 fprintf_unfiltered (gdb_stdlog
,
1207 "LLR: Not short circuiting for ignored "
1208 "status 0x%x\n", lp
->status
);
1210 /* FIXME: What should we do if we are supposed to continue
1211 this thread with a signal? */
1212 gdb_assert (signo
== TARGET_SIGNAL_0
);
1213 signo
= saved_signo
;
1220 /* FIXME: What should we do if we are supposed to continue
1221 this thread with a signal? */
1222 gdb_assert (signo
== TARGET_SIGNAL_0
);
1224 if (debug_linux_nat
)
1225 fprintf_unfiltered (gdb_stdlog
,
1226 "LLR: Short circuiting for status 0x%x\n",
1232 /* Mark LWP as not stopped to prevent it from being continued by
1238 iterate_over_lwps (resume_callback
, NULL
);
1240 linux_ops
->to_resume (ptid
, step
, signo
);
1241 if (debug_linux_nat
)
1242 fprintf_unfiltered (gdb_stdlog
,
1243 "LLR: %s %s, %s (resume event thread)\n",
1244 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1245 target_pid_to_str (ptid
),
1246 signo
? strsignal (signo
) : "0");
1249 /* Issue kill to specified lwp. */
1251 static int tkill_failed
;
1254 kill_lwp (int lwpid
, int signo
)
1258 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1259 fails, then we are not using nptl threads and we should be using kill. */
1261 #ifdef HAVE_TKILL_SYSCALL
1264 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1265 if (errno
!= ENOSYS
)
1272 return kill (lwpid
, signo
);
1275 /* Handle a GNU/Linux extended wait response. Most of the work we
1276 just pass off to linux_handle_extended_wait, but if it reports a
1277 clone event we need to add the new LWP to our list (and not report
1278 the trap to higher layers). This function returns non-zero if
1279 the event should be ignored and we should wait again. */
1282 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1284 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1287 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1288 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1290 struct lwp_info
*new_lp
;
1291 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1292 GET_PID (inferior_ptid
)));
1294 new_lp
->stopped
= 1;
1296 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1298 if (debug_linux_nat
)
1299 fprintf_unfiltered (gdb_stdlog
,
1300 "LLHE: Got clone event from LWP %ld, resuming\n",
1301 GET_LWP (lp
->ptid
));
1302 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1310 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1314 wait_lwp (struct lwp_info
*lp
)
1318 int thread_dead
= 0;
1320 gdb_assert (!lp
->stopped
);
1321 gdb_assert (lp
->status
== 0);
1323 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1324 if (pid
== -1 && errno
== ECHILD
)
1326 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1327 if (pid
== -1 && errno
== ECHILD
)
1329 /* The thread has previously exited. We need to delete it
1330 now because, for some vendor 2.4 kernels with NPTL
1331 support backported, there won't be an exit event unless
1332 it is the main thread. 2.6 kernels will report an exit
1333 event for each thread that exits, as expected. */
1335 if (debug_linux_nat
)
1336 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1337 target_pid_to_str (lp
->ptid
));
1343 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1345 if (debug_linux_nat
)
1347 fprintf_unfiltered (gdb_stdlog
,
1348 "WL: waitpid %s received %s\n",
1349 target_pid_to_str (lp
->ptid
),
1350 status_to_str (status
));
1354 /* Check if the thread has exited. */
1355 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1358 if (debug_linux_nat
)
1359 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1360 target_pid_to_str (lp
->ptid
));
1369 gdb_assert (WIFSTOPPED (status
));
1371 /* Handle GNU/Linux's extended waitstatus for trace events. */
1372 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1374 if (debug_linux_nat
)
1375 fprintf_unfiltered (gdb_stdlog
,
1376 "WL: Handling extended status 0x%06x\n",
1378 if (linux_nat_handle_extended (lp
, status
))
1379 return wait_lwp (lp
);
1385 /* Send a SIGSTOP to LP. */
1388 stop_callback (struct lwp_info
*lp
, void *data
)
1390 if (!lp
->stopped
&& !lp
->signalled
)
1394 if (debug_linux_nat
)
1396 fprintf_unfiltered (gdb_stdlog
,
1397 "SC: kill %s **<SIGSTOP>**\n",
1398 target_pid_to_str (lp
->ptid
));
1401 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1402 if (debug_linux_nat
)
1404 fprintf_unfiltered (gdb_stdlog
,
1405 "SC: lwp kill %d %s\n",
1407 errno
? safe_strerror (errno
) : "ERRNO-OK");
1411 gdb_assert (lp
->status
== 0);
1417 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1418 a pointer to a set of signals to be flushed immediately. */
1421 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1423 sigset_t
*flush_mask
= data
;
1429 status
= wait_lwp (lp
);
1433 /* Ignore any signals in FLUSH_MASK. */
1434 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1443 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1444 if (debug_linux_nat
)
1445 fprintf_unfiltered (gdb_stdlog
,
1446 "PTRACE_CONT %s, 0, 0 (%s)\n",
1447 target_pid_to_str (lp
->ptid
),
1448 errno
? safe_strerror (errno
) : "OK");
1450 return stop_wait_callback (lp
, flush_mask
);
1453 if (WSTOPSIG (status
) != SIGSTOP
)
1455 if (WSTOPSIG (status
) == SIGTRAP
)
1457 /* If a LWP other than the LWP that we're reporting an
1458 event for has hit a GDB breakpoint (as opposed to
1459 some random trap signal), then just arrange for it to
1460 hit it again later. We don't keep the SIGTRAP status
1461 and don't forward the SIGTRAP signal to the LWP. We
1462 will handle the current event, eventually we will
1463 resume all LWPs, and this one will get its breakpoint
1466 If we do not do this, then we run the risk that the
1467 user will delete or disable the breakpoint, but the
1468 thread will have already tripped on it. */
1470 /* Now resume this LWP and get the SIGSTOP event. */
1472 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1473 if (debug_linux_nat
)
1475 fprintf_unfiltered (gdb_stdlog
,
1476 "PTRACE_CONT %s, 0, 0 (%s)\n",
1477 target_pid_to_str (lp
->ptid
),
1478 errno
? safe_strerror (errno
) : "OK");
1480 fprintf_unfiltered (gdb_stdlog
,
1481 "SWC: Candidate SIGTRAP event in %s\n",
1482 target_pid_to_str (lp
->ptid
));
1484 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1485 stop_wait_callback (lp
, data
);
1486 /* If there's another event, throw it back into the queue. */
1489 if (debug_linux_nat
)
1491 fprintf_unfiltered (gdb_stdlog
,
1492 "SWC: kill %s, %s\n",
1493 target_pid_to_str (lp
->ptid
),
1494 status_to_str ((int) status
));
1496 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1498 /* Save the sigtrap event. */
1499 lp
->status
= status
;
1504 /* The thread was stopped with a signal other than
1505 SIGSTOP, and didn't accidentally trip a breakpoint. */
1507 if (debug_linux_nat
)
1509 fprintf_unfiltered (gdb_stdlog
,
1510 "SWC: Pending event %s in %s\n",
1511 status_to_str ((int) status
),
1512 target_pid_to_str (lp
->ptid
));
1514 /* Now resume this LWP and get the SIGSTOP event. */
1516 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1517 if (debug_linux_nat
)
1518 fprintf_unfiltered (gdb_stdlog
,
1519 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1520 target_pid_to_str (lp
->ptid
),
1521 errno
? safe_strerror (errno
) : "OK");
1523 /* Hold this event/waitstatus while we check to see if
1524 there are any more (we still want to get that SIGSTOP). */
1525 stop_wait_callback (lp
, data
);
1526 /* If the lp->status field is still empty, use it to hold
1527 this event. If not, then this event must be returned
1528 to the event queue of the LWP. */
1529 if (lp
->status
== 0)
1530 lp
->status
= status
;
1533 if (debug_linux_nat
)
1535 fprintf_unfiltered (gdb_stdlog
,
1536 "SWC: kill %s, %s\n",
1537 target_pid_to_str (lp
->ptid
),
1538 status_to_str ((int) status
));
1540 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1547 /* We caught the SIGSTOP that we intended to catch, so
1548 there's no SIGSTOP pending. */
1557 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1558 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1561 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1563 sigset_t blocked
, ignored
;
1566 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1571 for (i
= 1; i
< NSIG
; i
++)
1572 if (sigismember (pending
, i
))
1573 if (!sigismember (flush_mask
, i
)
1574 || sigismember (&blocked
, i
)
1575 || sigismember (&ignored
, i
))
1576 sigdelset (pending
, i
);
1578 if (sigisemptyset (pending
))
1584 /* DATA is interpreted as a mask of signals to flush. If LP has
1585 signals pending, and they are all in the flush mask, then arrange
1586 to flush them. LP should be stopped, as should all other threads
1587 it might share a signal queue with. */
1590 flush_callback (struct lwp_info
*lp
, void *data
)
1592 sigset_t
*flush_mask
= data
;
1593 sigset_t pending
, intersection
, blocked
, ignored
;
1596 /* Normally, when an LWP exits, it is removed from the LWP list. The
1597 last LWP isn't removed till later, however. So if there is only
1598 one LWP on the list, make sure it's alive. */
1599 if (lwp_list
== lp
&& lp
->next
== NULL
)
1600 if (!linux_nat_thread_alive (lp
->ptid
))
1603 /* Just because the LWP is stopped doesn't mean that new signals
1604 can't arrive from outside, so this function must be careful of
1605 race conditions. However, because all threads are stopped, we
1606 can assume that the pending mask will not shrink unless we resume
1607 the LWP, and that it will then get another signal. We can't
1608 control which one, however. */
1612 if (debug_linux_nat
)
1613 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
1614 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1618 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1623 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1624 if (debug_linux_nat
)
1625 fprintf_unfiltered (gdb_stderr
,
1626 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1629 stop_wait_callback (lp
, flush_mask
);
1630 if (debug_linux_nat
)
1631 fprintf_unfiltered (gdb_stderr
,
1632 "FC: Wait finished; saved status is %d\n",
1639 /* Return non-zero if LP has a wait status pending. */
1642 status_callback (struct lwp_info
*lp
, void *data
)
1644 /* Only report a pending wait status if we pretend that this has
1645 indeed been resumed. */
1646 return (lp
->status
!= 0 && lp
->resumed
);
1649 /* Return non-zero if LP isn't stopped. */
1652 running_callback (struct lwp_info
*lp
, void *data
)
1654 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1657 /* Count the LWP's that have had events. */
1660 count_events_callback (struct lwp_info
*lp
, void *data
)
1664 gdb_assert (count
!= NULL
);
1666 /* Count only LWPs that have a SIGTRAP event pending. */
1668 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1674 /* Select the LWP (if any) that is currently being single-stepped. */
1677 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1679 if (lp
->step
&& lp
->status
!= 0)
1685 /* Select the Nth LWP that has had a SIGTRAP event. */
1688 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1690 int *selector
= data
;
1692 gdb_assert (selector
!= NULL
);
1694 /* Select only LWPs that have a SIGTRAP event pending. */
1696 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1697 if ((*selector
)-- == 0)
1704 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1706 struct lwp_info
*event_lp
= data
;
1708 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1712 /* If a LWP other than the LWP that we're reporting an event for has
1713 hit a GDB breakpoint (as opposed to some random trap signal),
1714 then just arrange for it to hit it again later. We don't keep
1715 the SIGTRAP status and don't forward the SIGTRAP signal to the
1716 LWP. We will handle the current event, eventually we will resume
1717 all LWPs, and this one will get its breakpoint trap again.
1719 If we do not do this, then we run the risk that the user will
1720 delete or disable the breakpoint, but the LWP will have already
1724 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1725 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1726 DECR_PC_AFTER_BREAK
))
1728 if (debug_linux_nat
)
1729 fprintf_unfiltered (gdb_stdlog
,
1730 "CBC: Push back breakpoint for %s\n",
1731 target_pid_to_str (lp
->ptid
));
1733 /* Back up the PC if necessary. */
1734 if (DECR_PC_AFTER_BREAK
)
1735 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1737 /* Throw away the SIGTRAP. */
1744 /* Select one LWP out of those that have events pending. */
1747 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1750 int random_selector
;
1751 struct lwp_info
*event_lp
;
1753 /* Record the wait status for the original LWP. */
1754 (*orig_lp
)->status
= *status
;
1756 /* Give preference to any LWP that is being single-stepped. */
1757 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1758 if (event_lp
!= NULL
)
1760 if (debug_linux_nat
)
1761 fprintf_unfiltered (gdb_stdlog
,
1762 "SEL: Select single-step %s\n",
1763 target_pid_to_str (event_lp
->ptid
));
1767 /* No single-stepping LWP. Select one at random, out of those
1768 which have had SIGTRAP events. */
1770 /* First see how many SIGTRAP events we have. */
1771 iterate_over_lwps (count_events_callback
, &num_events
);
1773 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1774 random_selector
= (int)
1775 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1777 if (debug_linux_nat
&& num_events
> 1)
1778 fprintf_unfiltered (gdb_stdlog
,
1779 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1780 num_events
, random_selector
);
1782 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1786 if (event_lp
!= NULL
)
1788 /* Switch the event LWP. */
1789 *orig_lp
= event_lp
;
1790 *status
= event_lp
->status
;
1793 /* Flush the wait status for the event LWP. */
1794 (*orig_lp
)->status
= 0;
1797 /* Return non-zero if LP has been resumed. */
1800 resumed_callback (struct lwp_info
*lp
, void *data
)
1805 /* Stop an active thread, verify it still exists, then resume it. */
1808 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1810 struct lwp_info
*ptr
;
1812 if (!lp
->stopped
&& !lp
->signalled
)
1814 stop_callback (lp
, NULL
);
1815 stop_wait_callback (lp
, NULL
);
1816 /* Resume if the lwp still exists. */
1817 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1820 resume_callback (lp
, NULL
);
1821 resume_set_callback (lp
, NULL
);
1828 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1830 struct lwp_info
*lp
= NULL
;
1833 pid_t pid
= PIDGET (ptid
);
1834 sigset_t flush_mask
;
1836 /* The first time we get here after starting a new inferior, we may
1837 not have added it to the LWP list yet - this is the earliest
1838 moment at which we know its PID. */
1841 gdb_assert (!is_lwp (inferior_ptid
));
1843 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1844 GET_PID (inferior_ptid
));
1845 lp
= add_lwp (inferior_ptid
);
1849 sigemptyset (&flush_mask
);
1851 /* Make sure SIGCHLD is blocked. */
1852 if (!sigismember (&blocked_mask
, SIGCHLD
))
1854 sigaddset (&blocked_mask
, SIGCHLD
);
1855 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1860 /* Make sure there is at least one LWP that has been resumed. */
1861 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
1863 /* First check if there is a LWP with a wait status pending. */
1866 /* Any LWP that's been resumed will do. */
1867 lp
= iterate_over_lwps (status_callback
, NULL
);
1870 status
= lp
->status
;
1873 if (debug_linux_nat
&& status
)
1874 fprintf_unfiltered (gdb_stdlog
,
1875 "LLW: Using pending wait status %s for %s.\n",
1876 status_to_str (status
),
1877 target_pid_to_str (lp
->ptid
));
1880 /* But if we don't fine one, we'll have to wait, and check both
1881 cloned and uncloned processes. We start with the cloned
1883 options
= __WCLONE
| WNOHANG
;
1885 else if (is_lwp (ptid
))
1887 if (debug_linux_nat
)
1888 fprintf_unfiltered (gdb_stdlog
,
1889 "LLW: Waiting for specific LWP %s.\n",
1890 target_pid_to_str (ptid
));
1892 /* We have a specific LWP to check. */
1893 lp
= find_lwp_pid (ptid
);
1895 status
= lp
->status
;
1898 if (debug_linux_nat
&& status
)
1899 fprintf_unfiltered (gdb_stdlog
,
1900 "LLW: Using pending wait status %s for %s.\n",
1901 status_to_str (status
),
1902 target_pid_to_str (lp
->ptid
));
1904 /* If we have to wait, take into account whether PID is a cloned
1905 process or not. And we have to convert it to something that
1906 the layer beneath us can understand. */
1907 options
= lp
->cloned
? __WCLONE
: 0;
1908 pid
= GET_LWP (ptid
);
1911 if (status
&& lp
->signalled
)
1913 /* A pending SIGSTOP may interfere with the normal stream of
1914 events. In a typical case where interference is a problem,
1915 we have a SIGSTOP signal pending for LWP A while
1916 single-stepping it, encounter an event in LWP B, and take the
1917 pending SIGSTOP while trying to stop LWP A. After processing
1918 the event in LWP B, LWP A is continued, and we'll never see
1919 the SIGTRAP associated with the last time we were
1920 single-stepping LWP A. */
1922 /* Resume the thread. It should halt immediately returning the
1924 registers_changed ();
1925 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1926 lp
->step
, TARGET_SIGNAL_0
);
1927 if (debug_linux_nat
)
1928 fprintf_unfiltered (gdb_stdlog
,
1929 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1930 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1931 target_pid_to_str (lp
->ptid
));
1933 gdb_assert (lp
->resumed
);
1935 /* This should catch the pending SIGSTOP. */
1936 stop_wait_callback (lp
, NULL
);
1939 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1940 attached process. */
1947 lwpid
= my_waitpid (pid
, &status
, options
);
1950 gdb_assert (pid
== -1 || lwpid
== pid
);
1952 if (debug_linux_nat
)
1954 fprintf_unfiltered (gdb_stdlog
,
1955 "LLW: waitpid %ld received %s\n",
1956 (long) lwpid
, status_to_str (status
));
1959 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1961 /* Check for stop events reported by a process we didn't
1962 already know about - anything not already in our LWP
1965 If we're expecting to receive stopped processes after
1966 fork, vfork, and clone events, then we'll just add the
1967 new one to our list and go back to waiting for the event
1968 to be reported - the stopped process might be returned
1969 from waitpid before or after the event is. */
1970 if (WIFSTOPPED (status
) && !lp
)
1972 linux_record_stopped_pid (lwpid
);
1977 /* Make sure we don't report an event for the exit of an LWP not in
1978 our list, i.e. not part of the current process. This can happen
1979 if we detach from a program we original forked and then it
1981 if (!WIFSTOPPED (status
) && !lp
)
1987 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1988 CLONE_PTRACE processes which do not use the thread library -
1989 otherwise we wouldn't find the new LWP this way. That doesn't
1990 currently work, and the following code is currently unreachable
1991 due to the two blocks above. If it's fixed some day, this code
1992 should be broken out into a function so that we can also pick up
1993 LWPs from the new interface. */
1996 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1997 if (options
& __WCLONE
)
2000 gdb_assert (WIFSTOPPED (status
)
2001 && WSTOPSIG (status
) == SIGSTOP
);
2004 if (!in_thread_list (inferior_ptid
))
2006 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2007 GET_PID (inferior_ptid
));
2008 add_thread (inferior_ptid
);
2011 add_thread (lp
->ptid
);
2012 printf_unfiltered (_("[New %s]\n"),
2013 target_pid_to_str (lp
->ptid
));
2016 /* Handle GNU/Linux's extended waitstatus for trace events. */
2017 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2019 if (debug_linux_nat
)
2020 fprintf_unfiltered (gdb_stdlog
,
2021 "LLW: Handling extended status 0x%06x\n",
2023 if (linux_nat_handle_extended (lp
, status
))
2030 /* Check if the thread has exited. */
2031 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2033 /* If this is the main thread, we must stop all threads and
2034 verify if they are still alive. This is because in the nptl
2035 thread model, there is no signal issued for exiting LWPs
2036 other than the main thread. We only get the main thread
2037 exit signal once all child threads have already exited.
2038 If we stop all the threads and use the stop_wait_callback
2039 to check if they have exited we can determine whether this
2040 signal should be ignored or whether it means the end of the
2041 debugged application, regardless of which threading model
2043 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2046 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2049 if (debug_linux_nat
)
2050 fprintf_unfiltered (gdb_stdlog
,
2051 "LLW: %s exited.\n",
2052 target_pid_to_str (lp
->ptid
));
2056 /* If there is at least one more LWP, then the exit signal
2057 was not the end of the debugged application and should be
2061 /* Make sure there is at least one thread running. */
2062 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2064 /* Discard the event. */
2070 /* Check if the current LWP has previously exited. In the nptl
2071 thread model, LWPs other than the main thread do not issue
2072 signals when they exit so we must check whenever the thread
2073 has stopped. A similar check is made in stop_wait_callback(). */
2074 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2076 if (debug_linux_nat
)
2077 fprintf_unfiltered (gdb_stdlog
,
2078 "LLW: %s exited.\n",
2079 target_pid_to_str (lp
->ptid
));
2083 /* Make sure there is at least one thread running. */
2084 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2086 /* Discard the event. */
2091 /* Make sure we don't report a SIGSTOP that we sent
2092 ourselves in an attempt to stop an LWP. */
2094 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2096 if (debug_linux_nat
)
2097 fprintf_unfiltered (gdb_stdlog
,
2098 "LLW: Delayed SIGSTOP caught for %s.\n",
2099 target_pid_to_str (lp
->ptid
));
2101 /* This is a delayed SIGSTOP. */
2104 registers_changed ();
2105 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2106 lp
->step
, TARGET_SIGNAL_0
);
2107 if (debug_linux_nat
)
2108 fprintf_unfiltered (gdb_stdlog
,
2109 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2111 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2112 target_pid_to_str (lp
->ptid
));
2115 gdb_assert (lp
->resumed
);
2117 /* Discard the event. */
2127 /* Alternate between checking cloned and uncloned processes. */
2128 options
^= __WCLONE
;
2130 /* And suspend every time we have checked both. */
2131 if (options
& __WCLONE
)
2132 sigsuspend (&suspend_mask
);
2135 /* We shouldn't end up here unless we want to try again. */
2136 gdb_assert (status
== 0);
2139 clear_sigio_trap ();
2140 clear_sigint_trap ();
2144 /* Don't report signals that GDB isn't interested in, such as
2145 signals that are neither printed nor stopped upon. Stopping all
2146 threads can be a bit time-consuming so if we want decent
2147 performance with heavily multi-threaded programs, especially when
2148 they're using a high frequency timer, we'd better avoid it if we
2151 if (WIFSTOPPED (status
))
2153 int signo
= target_signal_from_host (WSTOPSIG (status
));
2155 if (signal_stop_state (signo
) == 0
2156 && signal_print_state (signo
) == 0
2157 && signal_pass_state (signo
) == 1)
2159 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2160 here? It is not clear we should. GDB may not expect
2161 other threads to run. On the other hand, not resuming
2162 newly attached threads may cause an unwanted delay in
2163 getting them running. */
2164 registers_changed ();
2165 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2167 if (debug_linux_nat
)
2168 fprintf_unfiltered (gdb_stdlog
,
2169 "LLW: %s %s, %s (preempt 'handle')\n",
2171 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2172 target_pid_to_str (lp
->ptid
),
2173 signo
? strsignal (signo
) : "0");
2179 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2181 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2182 forwarded to the entire process group, that is, all LWP's
2183 will receive it. Since we only want to report it once,
2184 we try to flush it from all LWPs except this one. */
2185 sigaddset (&flush_mask
, SIGINT
);
2189 /* This LWP is stopped now. */
2192 if (debug_linux_nat
)
2193 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2194 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2196 /* Now stop all other LWP's ... */
2197 iterate_over_lwps (stop_callback
, NULL
);
2199 /* ... and wait until all of them have reported back that they're no
2201 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2202 iterate_over_lwps (flush_callback
, &flush_mask
);
2204 /* If we're not waiting for a specific LWP, choose an event LWP from
2205 among those that have had events. Giving equal priority to all
2206 LWPs that have had events helps prevent starvation. */
2208 select_event_lwp (&lp
, &status
);
2210 /* Now that we've selected our final event LWP, cancel any
2211 breakpoints in other LWPs that have hit a GDB breakpoint. See
2212 the comment in cancel_breakpoints_callback to find out why. */
2213 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2215 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2217 trap_ptid
= lp
->ptid
;
2218 if (debug_linux_nat
)
2219 fprintf_unfiltered (gdb_stdlog
,
2220 "LLW: trap_ptid is %s.\n",
2221 target_pid_to_str (trap_ptid
));
2224 trap_ptid
= null_ptid
;
2226 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2228 *ourstatus
= lp
->waitstatus
;
2229 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2232 store_waitstatus (ourstatus
, status
);
2238 kill_callback (struct lwp_info
*lp
, void *data
)
2241 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2242 if (debug_linux_nat
)
2243 fprintf_unfiltered (gdb_stdlog
,
2244 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2245 target_pid_to_str (lp
->ptid
),
2246 errno
? safe_strerror (errno
) : "OK");
2252 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2256 /* We must make sure that there are no pending events (delayed
2257 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2258 program doesn't interfere with any following debugging session. */
2260 /* For cloned processes we must check both with __WCLONE and
2261 without, since the exit status of a cloned process isn't reported
2267 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2268 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2270 fprintf_unfiltered (gdb_stdlog
,
2271 "KWC: wait %s received unknown.\n",
2272 target_pid_to_str (lp
->ptid
));
2275 while (pid
== GET_LWP (lp
->ptid
));
2277 gdb_assert (pid
== -1 && errno
== ECHILD
);
2282 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2283 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2285 fprintf_unfiltered (gdb_stdlog
,
2286 "KWC: wait %s received unk.\n",
2287 target_pid_to_str (lp
->ptid
));
2290 while (pid
== GET_LWP (lp
->ptid
));
2292 gdb_assert (pid
== -1 && errno
== ECHILD
);
2297 linux_nat_kill (void)
2299 struct target_waitstatus last
;
2303 /* If we're stopped while forking and we haven't followed yet,
2304 kill the other task. We need to do this first because the
2305 parent will be sleeping if this is a vfork. */
2307 get_last_target_status (&last_ptid
, &last
);
2309 if (last
.kind
== TARGET_WAITKIND_FORKED
2310 || last
.kind
== TARGET_WAITKIND_VFORKED
)
2312 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
2316 if (forks_exist_p ())
2317 linux_fork_killall ();
2320 /* Kill all LWP's ... */
2321 iterate_over_lwps (kill_callback
, NULL
);
2323 /* ... and wait until we've flushed all events. */
2324 iterate_over_lwps (kill_wait_callback
, NULL
);
2327 target_mourn_inferior ();
2331 linux_nat_mourn_inferior (void)
2333 trap_ptid
= null_ptid
;
2335 /* Destroy LWP info; it's no longer valid. */
2338 /* Restore the original signal mask. */
2339 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2340 sigemptyset (&blocked_mask
);
2342 if (! forks_exist_p ())
2343 /* Normal case, no other forks available. */
2344 linux_ops
->to_mourn_inferior ();
2346 /* Multi-fork case. The current inferior_ptid has exited, but
2347 there are other viable forks to debug. Delete the exiting
2348 one and context-switch to the first available. */
2349 linux_fork_mourn_inferior ();
2353 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2354 const char *annex
, gdb_byte
*readbuf
,
2355 const gdb_byte
*writebuf
,
2356 ULONGEST offset
, LONGEST len
)
2358 struct cleanup
*old_chain
= save_inferior_ptid ();
2361 if (is_lwp (inferior_ptid
))
2362 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2364 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
2367 do_cleanups (old_chain
);
2372 linux_nat_thread_alive (ptid_t ptid
)
2374 gdb_assert (is_lwp (ptid
));
2377 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2378 if (debug_linux_nat
)
2379 fprintf_unfiltered (gdb_stdlog
,
2380 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2381 target_pid_to_str (ptid
),
2382 errno
? safe_strerror (errno
) : "OK");
2390 linux_nat_pid_to_str (ptid_t ptid
)
2392 static char buf
[64];
2394 if (lwp_list
&& lwp_list
->next
&& is_lwp (ptid
))
2396 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2400 return normal_pid_to_str (ptid
);
2404 sigchld_handler (int signo
)
2406 /* Do nothing. The only reason for this handler is that it allows
2407 us to use sigsuspend in linux_nat_wait above to wait for the
2408 arrival of a SIGCHLD. */
2411 /* Accepts an integer PID; Returns a string representing a file that
2412 can be opened to get the symbols for the child process. */
2415 child_pid_to_exec_file (int pid
)
2417 char *name1
, *name2
;
2419 name1
= xmalloc (MAXPATHLEN
);
2420 name2
= xmalloc (MAXPATHLEN
);
2421 make_cleanup (xfree
, name1
);
2422 make_cleanup (xfree
, name2
);
2423 memset (name2
, 0, MAXPATHLEN
);
2425 sprintf (name1
, "/proc/%d/exe", pid
);
2426 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2432 /* Service function for corefiles and info proc. */
2435 read_mapping (FILE *mapfile
,
2440 char *device
, long long *inode
, char *filename
)
2442 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2443 addr
, endaddr
, permissions
, offset
, device
, inode
);
2446 if (ret
> 0 && ret
!= EOF
)
2448 /* Eat everything up to EOL for the filename. This will prevent
2449 weird filenames (such as one with embedded whitespace) from
2450 confusing this code. It also makes this code more robust in
2451 respect to annotations the kernel may add after the filename.
2453 Note the filename is used for informational purposes
2455 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2458 return (ret
!= 0 && ret
!= EOF
);
2461 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2462 regions in the inferior for a corefile. */
2465 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2467 int, int, int, void *), void *obfd
)
2469 long long pid
= PIDGET (inferior_ptid
);
2470 char mapsfilename
[MAXPATHLEN
];
2472 long long addr
, endaddr
, size
, offset
, inode
;
2473 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2474 int read
, write
, exec
;
2477 /* Compose the filename for the /proc memory map, and open it. */
2478 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2479 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2480 error (_("Could not open %s."), mapsfilename
);
2483 fprintf_filtered (gdb_stdout
,
2484 "Reading memory regions from %s\n", mapsfilename
);
2486 /* Now iterate until end-of-file. */
2487 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2488 &offset
, &device
[0], &inode
, &filename
[0]))
2490 size
= endaddr
- addr
;
2492 /* Get the segment's permissions. */
2493 read
= (strchr (permissions
, 'r') != 0);
2494 write
= (strchr (permissions
, 'w') != 0);
2495 exec
= (strchr (permissions
, 'x') != 0);
2499 fprintf_filtered (gdb_stdout
,
2500 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2501 size
, paddr_nz (addr
),
2503 write
? 'w' : ' ', exec
? 'x' : ' ');
2504 if (filename
&& filename
[0])
2505 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2506 fprintf_filtered (gdb_stdout
, "\n");
2509 /* Invoke the callback function to create the corefile
2511 func (addr
, size
, read
, write
, exec
, obfd
);
2517 /* Records the thread's register state for the corefile note
2521 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2522 char *note_data
, int *note_size
)
2524 gdb_gregset_t gregs
;
2525 gdb_fpregset_t fpregs
;
2526 #ifdef FILL_FPXREGSET
2527 gdb_fpxregset_t fpxregs
;
2529 unsigned long lwp
= ptid_get_lwp (ptid
);
2531 fill_gregset (&gregs
, -1);
2532 note_data
= (char *) elfcore_write_prstatus (obfd
,
2536 stop_signal
, &gregs
);
2538 fill_fpregset (&fpregs
, -1);
2539 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2542 &fpregs
, sizeof (fpregs
));
2543 #ifdef FILL_FPXREGSET
2544 fill_fpxregset (&fpxregs
, -1);
2545 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2548 &fpxregs
, sizeof (fpxregs
));
2553 struct linux_nat_corefile_thread_data
2561 /* Called by gdbthread.c once per thread. Records the thread's
2562 register state for the corefile note section. */
2565 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2567 struct linux_nat_corefile_thread_data
*args
= data
;
2568 ptid_t saved_ptid
= inferior_ptid
;
2570 inferior_ptid
= ti
->ptid
;
2571 registers_changed ();
2572 target_fetch_registers (-1); /* FIXME should not be necessary;
2573 fill_gregset should do it automatically. */
2574 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2579 inferior_ptid
= saved_ptid
;
2580 registers_changed ();
2581 target_fetch_registers (-1); /* FIXME should not be necessary;
2582 fill_gregset should do it automatically. */
2586 /* Records the register state for the corefile note section. */
2589 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2590 char *note_data
, int *note_size
)
2592 registers_changed ();
2593 target_fetch_registers (-1); /* FIXME should not be necessary;
2594 fill_gregset should do it automatically. */
2595 return linux_nat_do_thread_registers (obfd
,
2596 ptid_build (ptid_get_pid (inferior_ptid
),
2597 ptid_get_pid (inferior_ptid
),
2599 note_data
, note_size
);
2603 /* Fills the "to_make_corefile_note" target vector. Builds the note
2604 section for a corefile, and returns it in a malloc buffer. */
2607 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2609 struct linux_nat_corefile_thread_data thread_args
;
2610 struct cleanup
*old_chain
;
2611 char fname
[16] = { '\0' };
2612 char psargs
[80] = { '\0' };
2613 char *note_data
= NULL
;
2614 ptid_t current_ptid
= inferior_ptid
;
2618 if (get_exec_file (0))
2620 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2621 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2622 if (get_inferior_args ())
2624 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2625 strncat (psargs
, get_inferior_args (),
2626 sizeof (psargs
) - strlen (psargs
));
2628 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2630 note_size
, fname
, psargs
);
2633 /* Dump information for threads. */
2634 thread_args
.obfd
= obfd
;
2635 thread_args
.note_data
= note_data
;
2636 thread_args
.note_size
= note_size
;
2637 thread_args
.num_notes
= 0;
2638 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2639 if (thread_args
.num_notes
== 0)
2641 /* iterate_over_threads didn't come up with any threads; just
2642 use inferior_ptid. */
2643 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2644 note_data
, note_size
);
2648 note_data
= thread_args
.note_data
;
2651 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2654 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2655 "CORE", NT_AUXV
, auxv
, auxv_len
);
2659 make_cleanup (xfree
, note_data
);
2663 /* Implement the "info proc" command. */
2666 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2668 long long pid
= PIDGET (inferior_ptid
);
2671 char buffer
[MAXPATHLEN
];
2672 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2685 /* Break up 'args' into an argv array. */
2686 if ((argv
= buildargv (args
)) == NULL
)
2689 make_cleanup_freeargv (argv
);
2691 while (argv
!= NULL
&& *argv
!= NULL
)
2693 if (isdigit (argv
[0][0]))
2695 pid
= strtoul (argv
[0], NULL
, 10);
2697 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2701 else if (strcmp (argv
[0], "status") == 0)
2705 else if (strcmp (argv
[0], "stat") == 0)
2709 else if (strcmp (argv
[0], "cmd") == 0)
2713 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2717 else if (strcmp (argv
[0], "cwd") == 0)
2721 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2727 /* [...] (future options here) */
2732 error (_("No current process: you must name one."));
2734 sprintf (fname1
, "/proc/%lld", pid
);
2735 if (stat (fname1
, &dummy
) != 0)
2736 error (_("No /proc directory: '%s'"), fname1
);
2738 printf_filtered (_("process %lld\n"), pid
);
2739 if (cmdline_f
|| all
)
2741 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2742 if ((procfile
= fopen (fname1
, "r")) > 0)
2744 fgets (buffer
, sizeof (buffer
), procfile
);
2745 printf_filtered ("cmdline = '%s'\n", buffer
);
2749 warning (_("unable to open /proc file '%s'"), fname1
);
2753 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2754 memset (fname2
, 0, sizeof (fname2
));
2755 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2756 printf_filtered ("cwd = '%s'\n", fname2
);
2758 warning (_("unable to read link '%s'"), fname1
);
2762 sprintf (fname1
, "/proc/%lld/exe", pid
);
2763 memset (fname2
, 0, sizeof (fname2
));
2764 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2765 printf_filtered ("exe = '%s'\n", fname2
);
2767 warning (_("unable to read link '%s'"), fname1
);
2769 if (mappings_f
|| all
)
2771 sprintf (fname1
, "/proc/%lld/maps", pid
);
2772 if ((procfile
= fopen (fname1
, "r")) > 0)
2774 long long addr
, endaddr
, size
, offset
, inode
;
2775 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2777 printf_filtered (_("Mapped address spaces:\n\n"));
2778 if (TARGET_ADDR_BIT
== 32)
2780 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2783 " Size", " Offset", "objfile");
2787 printf_filtered (" %18s %18s %10s %10s %7s\n",
2790 " Size", " Offset", "objfile");
2793 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2794 &offset
, &device
[0], &inode
, &filename
[0]))
2796 size
= endaddr
- addr
;
2798 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2799 calls here (and possibly above) should be abstracted
2800 out into their own functions? Andrew suggests using
2801 a generic local_address_string instead to print out
2802 the addresses; that makes sense to me, too. */
2804 if (TARGET_ADDR_BIT
== 32)
2806 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2807 (unsigned long) addr
, /* FIXME: pr_addr */
2808 (unsigned long) endaddr
,
2810 (unsigned int) offset
,
2811 filename
[0] ? filename
: "");
2815 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2816 (unsigned long) addr
, /* FIXME: pr_addr */
2817 (unsigned long) endaddr
,
2819 (unsigned int) offset
,
2820 filename
[0] ? filename
: "");
2827 warning (_("unable to open /proc file '%s'"), fname1
);
2829 if (status_f
|| all
)
2831 sprintf (fname1
, "/proc/%lld/status", pid
);
2832 if ((procfile
= fopen (fname1
, "r")) > 0)
2834 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2835 puts_filtered (buffer
);
2839 warning (_("unable to open /proc file '%s'"), fname1
);
2843 sprintf (fname1
, "/proc/%lld/stat", pid
);
2844 if ((procfile
= fopen (fname1
, "r")) > 0)
2849 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2850 printf_filtered (_("Process: %d\n"), itmp
);
2851 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2852 printf_filtered (_("Exec file: %s\n"), buffer
);
2853 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2854 printf_filtered (_("State: %c\n"), ctmp
);
2855 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2856 printf_filtered (_("Parent process: %d\n"), itmp
);
2857 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2858 printf_filtered (_("Process group: %d\n"), itmp
);
2859 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2860 printf_filtered (_("Session id: %d\n"), itmp
);
2861 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2862 printf_filtered (_("TTY: %d\n"), itmp
);
2863 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2864 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
2865 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2866 printf_filtered (_("Flags: 0x%x\n"), itmp
);
2867 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2868 printf_filtered (_("Minor faults (no memory page): %u\n"),
2869 (unsigned int) itmp
);
2870 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2871 printf_filtered (_("Minor faults, children: %u\n"),
2872 (unsigned int) itmp
);
2873 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2874 printf_filtered (_("Major faults (memory page faults): %u\n"),
2875 (unsigned int) itmp
);
2876 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2877 printf_filtered (_("Major faults, children: %u\n"),
2878 (unsigned int) itmp
);
2879 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2880 printf_filtered ("utime: %d\n", itmp
);
2881 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2882 printf_filtered ("stime: %d\n", itmp
);
2883 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2884 printf_filtered ("utime, children: %d\n", itmp
);
2885 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2886 printf_filtered ("stime, children: %d\n", itmp
);
2887 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2888 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
2890 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2891 printf_filtered ("'nice' value: %d\n", itmp
);
2892 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2893 printf_filtered (_("jiffies until next timeout: %u\n"),
2894 (unsigned int) itmp
);
2895 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2896 printf_filtered ("jiffies until next SIGALRM: %u\n",
2897 (unsigned int) itmp
);
2898 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2899 printf_filtered (_("start time (jiffies since system boot): %d\n"),
2901 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2902 printf_filtered (_("Virtual memory size: %u\n"),
2903 (unsigned int) itmp
);
2904 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2905 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp
);
2906 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2907 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2908 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2909 printf_filtered (_("Start of text: 0x%x\n"), itmp
);
2910 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2911 printf_filtered (_("End of text: 0x%x\n"), itmp
);
2912 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2913 printf_filtered (_("Start of stack: 0x%x\n"), itmp
);
2914 #if 0 /* Don't know how architecture-dependent the rest is...
2915 Anyway the signal bitmap info is available from "status". */
2916 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2917 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp
);
2918 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2919 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp
);
2920 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2921 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp
);
2922 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2923 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp
);
2924 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2925 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp
);
2926 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2927 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp
);
2928 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2929 printf_filtered (_("wchan (system call): 0x%x\n"), itmp
);
2934 warning (_("unable to open /proc file '%s'"), fname1
);
2938 /* Implement the to_xfer_partial interface for memory reads using the /proc
2939 filesystem. Because we can use a single read() call for /proc, this
2940 can be much more efficient than banging away at PTRACE_PEEKTEXT,
2941 but it doesn't support writes. */
2944 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2945 const char *annex
, gdb_byte
*readbuf
,
2946 const gdb_byte
*writebuf
,
2947 ULONGEST offset
, LONGEST len
)
2953 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
2956 /* Don't bother for one word. */
2957 if (len
< 3 * sizeof (long))
2960 /* We could keep this file open and cache it - possibly one per
2961 thread. That requires some juggling, but is even faster. */
2962 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2963 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2967 /* If pread64 is available, use it. It's faster if the kernel
2968 supports it (only one syscall), and it's 64-bit safe even on
2969 32-bit platforms (for instance, SPARC debugging a SPARC64
2972 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
2974 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
2984 /* Parse LINE as a signal set and add its set bits to SIGS. */
2987 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2989 int len
= strlen (line
) - 1;
2993 if (line
[len
] != '\n')
2994 error (_("Could not parse signal set: %s"), line
);
3002 if (*p
>= '0' && *p
<= '9')
3004 else if (*p
>= 'a' && *p
<= 'f')
3005 digit
= *p
- 'a' + 10;
3007 error (_("Could not parse signal set: %s"), line
);
3012 sigaddset (sigs
, signum
+ 1);
3014 sigaddset (sigs
, signum
+ 2);
3016 sigaddset (sigs
, signum
+ 3);
3018 sigaddset (sigs
, signum
+ 4);
3024 /* Find process PID's pending signals from /proc/pid/status and set
3028 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3031 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3034 sigemptyset (pending
);
3035 sigemptyset (blocked
);
3036 sigemptyset (ignored
);
3037 sprintf (fname
, "/proc/%d/status", pid
);
3038 procfile
= fopen (fname
, "r");
3039 if (procfile
== NULL
)
3040 error (_("Could not open %s"), fname
);
3042 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3044 /* Normal queued signals are on the SigPnd line in the status
3045 file. However, 2.6 kernels also have a "shared" pending
3046 queue for delivering signals to a thread group, so check for
3049 Unfortunately some Red Hat kernels include the shared pending
3050 queue but not the ShdPnd status field. */
3052 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3053 add_line_to_sigset (buffer
+ 8, pending
);
3054 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3055 add_line_to_sigset (buffer
+ 8, pending
);
3056 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3057 add_line_to_sigset (buffer
+ 8, blocked
);
3058 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3059 add_line_to_sigset (buffer
+ 8, ignored
);
3066 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3067 const char *annex
, gdb_byte
*readbuf
,
3068 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3072 if (object
== TARGET_OBJECT_AUXV
)
3073 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3076 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3081 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3085 #ifndef FETCH_INFERIOR_REGISTERS
3087 /* Return the address in the core dump or inferior of register
3091 linux_register_u_offset (int regno
)
3093 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
3094 away. This requires disentangling the various definitions of it
3095 (particularly alpha-nat.c's). */
3096 return register_addr (regno
, 0);
3101 /* Create a prototype generic Linux target. The client can override
3102 it with local methods. */
3107 struct target_ops
*t
;
3109 #ifdef FETCH_INFERIOR_REGISTERS
3110 t
= inf_ptrace_target ();
3112 t
= inf_ptrace_trad_target (linux_register_u_offset
);
3114 t
->to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
3115 t
->to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
3116 t
->to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
3117 t
->to_pid_to_exec_file
= child_pid_to_exec_file
;
3118 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3119 t
->to_post_attach
= child_post_attach
;
3120 t
->to_follow_fork
= child_follow_fork
;
3121 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3122 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3124 super_xfer_partial
= t
->to_xfer_partial
;
3125 t
->to_xfer_partial
= linux_xfer_partial
;
3131 linux_nat_add_target (struct target_ops
*t
)
3133 extern void thread_db_init (struct target_ops
*);
3135 /* Save the provided single-threaded target. We save this in a separate
3136 variable because another target we've inherited from (e.g. inf-ptrace)
3137 may have saved a pointer to T; we want to use it for the final
3138 process stratum target. */
3139 linux_ops_saved
= *t
;
3140 linux_ops
= &linux_ops_saved
;
3142 /* Override some methods for multithreading. */
3143 t
->to_attach
= linux_nat_attach
;
3144 t
->to_detach
= linux_nat_detach
;
3145 t
->to_resume
= linux_nat_resume
;
3146 t
->to_wait
= linux_nat_wait
;
3147 t
->to_xfer_partial
= linux_nat_xfer_partial
;
3148 t
->to_kill
= linux_nat_kill
;
3149 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
3150 t
->to_thread_alive
= linux_nat_thread_alive
;
3151 t
->to_pid_to_str
= linux_nat_pid_to_str
;
3152 t
->to_has_thread_control
= tc_schedlock
;
3154 /* We don't change the stratum; this target will sit at
3155 process_stratum and thread_db will set at thread_stratum. This
3156 is a little strange, since this is a multi-threaded-capable
3157 target, but we want to be on the stack below thread_db, and we
3158 also want to be used for single-threaded processes. */
3162 /* TODO: Eliminate this and have libthread_db use
3163 find_target_beneath. */
3168 _initialize_linux_nat (void)
3170 struct sigaction action
;
3172 add_info ("proc", linux_nat_info_proc_cmd
, _("\
3173 Show /proc process information about any running process.\n\
3174 Specify any process id, or use the program being debugged by default.\n\
3175 Specify any of the following keywords for detailed info:\n\
3176 mappings -- list of mapped memory regions.\n\
3177 stat -- list a bunch of random process info.\n\
3178 status -- list a different bunch of random process info.\n\
3179 all -- list all available /proc info."));
3181 /* Save the original signal mask. */
3182 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3184 action
.sa_handler
= sigchld_handler
;
3185 sigemptyset (&action
.sa_mask
);
3186 action
.sa_flags
= SA_RESTART
;
3187 sigaction (SIGCHLD
, &action
, NULL
);
3189 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3190 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3191 sigdelset (&suspend_mask
, SIGCHLD
);
3193 sigemptyset (&blocked_mask
);
3195 add_setshow_zinteger_cmd ("lin-lwp", no_class
, &debug_linux_nat
, _("\
3196 Set debugging of GNU/Linux lwp module."), _("\
3197 Show debugging of GNU/Linux lwp module."), _("\
3198 Enables printf debugging output."),
3200 show_debug_linux_nat
,
3201 &setdebuglist
, &showdebuglist
);
3205 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3206 the GNU/Linux Threads library and therefore doesn't really belong
3209 /* Read variable NAME in the target and return its value if found.
3210 Otherwise return zero. It is assumed that the type of the variable
3214 get_signo (const char *name
)
3216 struct minimal_symbol
*ms
;
3219 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3223 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
3224 sizeof (signo
)) != 0)
3230 /* Return the set of signals used by the threads library in *SET. */
3233 lin_thread_get_thread_signals (sigset_t
*set
)
3235 struct sigaction action
;
3236 int restart
, cancel
;
3240 restart
= get_signo ("__pthread_sig_restart");
3241 cancel
= get_signo ("__pthread_sig_cancel");
3243 /* LinuxThreads normally uses the first two RT signals, but in some legacy
3244 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
3245 not provide any way for the debugger to query the signal numbers -
3246 fortunately they don't change! */
3249 restart
= __SIGRTMIN
;
3252 cancel
= __SIGRTMIN
+ 1;
3254 sigaddset (set
, restart
);
3255 sigaddset (set
, cancel
);
3257 /* The GNU/Linux Threads library makes terminating threads send a
3258 special "cancel" signal instead of SIGCHLD. Make sure we catch
3259 those (to prevent them from terminating GDB itself, which is
3260 likely to be their default action) and treat them the same way as
3263 action
.sa_handler
= sigchld_handler
;
3264 sigemptyset (&action
.sa_mask
);
3265 action
.sa_flags
= SA_RESTART
;
3266 sigaction (cancel
, &action
, NULL
);
3268 /* We block the "cancel" signal throughout this code ... */
3269 sigaddset (&blocked_mask
, cancel
);
3270 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3272 /* ... except during a sigsuspend. */
3273 sigdelset (&suspend_mask
, cancel
);