1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright 2001, 2002, 2003, 2004 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "gdbthread.h"
37 #include <sys/param.h> /* for MAXPATHLEN */
38 #include <sys/procfs.h> /* for elf_gregset etc. */
39 #include "elf-bfd.h" /* for elfcore_write_* */
40 #include "gregset.h" /* for gregset */
41 #include "gdbcore.h" /* for get_exec_file */
42 #include <ctype.h> /* for isdigit */
43 #include "gdbthread.h" /* for struct thread_info etc. */
44 #include "gdb_stat.h" /* for struct stat */
45 #include <fcntl.h> /* for O_RDONLY */
51 /* If the system headers did not provide the constants, hard-code the normal
53 #ifndef PTRACE_EVENT_FORK
55 #define PTRACE_SETOPTIONS 0x4200
56 #define PTRACE_GETEVENTMSG 0x4201
58 /* options set using PTRACE_SETOPTIONS */
59 #define PTRACE_O_TRACESYSGOOD 0x00000001
60 #define PTRACE_O_TRACEFORK 0x00000002
61 #define PTRACE_O_TRACEVFORK 0x00000004
62 #define PTRACE_O_TRACECLONE 0x00000008
63 #define PTRACE_O_TRACEEXEC 0x00000010
64 #define PTRACE_O_TRACEVFORKDONE 0x00000020
65 #define PTRACE_O_TRACEEXIT 0x00000040
67 /* Wait extended result codes for the above trace options. */
68 #define PTRACE_EVENT_FORK 1
69 #define PTRACE_EVENT_VFORK 2
70 #define PTRACE_EVENT_CLONE 3
71 #define PTRACE_EVENT_EXEC 4
72 #define PTRACE_EVENT_VFORK_DONE 5
73 #define PTRACE_EVENT_EXIT 6
75 #endif /* PTRACE_EVENT_FORK */
77 /* We can't always assume that this flag is available, but all systems
78 with the ptrace event handlers also have __WALL, so it's safe to use
81 #define __WALL 0x40000000 /* Wait for any child. */
84 static int debug_linux_nat
;
86 static int linux_parent_pid
;
88 struct simple_pid_list
91 struct simple_pid_list
*next
;
93 struct simple_pid_list
*stopped_pids
;
95 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
96 can not be used, 1 if it can. */
98 static int linux_supports_tracefork_flag
= -1;
100 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
101 PTRACE_O_TRACEVFORKDONE. */
103 static int linux_supports_tracevforkdone_flag
= -1;
106 /* Trivial list manipulation functions to keep track of a list of
107 new stopped processes. */
109 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
111 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
113 new_pid
->next
= *listp
;
118 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
120 struct simple_pid_list
**p
;
122 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
123 if ((*p
)->pid
== pid
)
125 struct simple_pid_list
*next
= (*p
)->next
;
134 linux_record_stopped_pid (int pid
)
136 add_to_pid_list (&stopped_pids
, pid
);
140 /* A helper function for linux_test_for_tracefork, called after fork (). */
143 linux_tracefork_child (void)
147 ptrace (PTRACE_TRACEME
, 0, 0, 0);
148 kill (getpid (), SIGSTOP
);
153 /* Wrapper function for waitpid which handles EINTR. */
156 my_waitpid (int pid
, int *status
, int flags
)
161 ret
= waitpid (pid
, status
, flags
);
163 while (ret
== -1 && errno
== EINTR
);
168 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
170 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
171 we know that the feature is not available. This may change the tracing
172 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
174 However, if it succeeds, we don't know for sure that the feature is
175 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
176 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
177 fork tracing, and let it fork. If the process exits, we assume that we
178 can't use TRACEFORK; if we get the fork notification, and we can extract
179 the new child's PID, then we assume that we can. */
182 linux_test_for_tracefork (int original_pid
)
184 int child_pid
, ret
, status
;
187 linux_supports_tracefork_flag
= 0;
188 linux_supports_tracevforkdone_flag
= 0;
190 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
196 perror_with_name ("linux_test_for_tracefork: fork");
199 linux_tracefork_child ();
201 ret
= my_waitpid (child_pid
, &status
, 0);
203 perror_with_name ("linux_test_for_tracefork: waitpid");
204 else if (ret
!= child_pid
)
205 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
206 if (! WIFSTOPPED (status
))
207 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
209 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
212 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
215 warning ("linux_test_for_tracefork: failed to kill child");
219 ret
= my_waitpid (child_pid
, &status
, 0);
220 if (ret
!= child_pid
)
221 warning ("linux_test_for_tracefork: failed to wait for killed child");
222 else if (!WIFSIGNALED (status
))
223 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
224 "killed child", status
);
229 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
230 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
231 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
232 linux_supports_tracevforkdone_flag
= (ret
== 0);
234 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
236 warning ("linux_test_for_tracefork: failed to resume child");
238 ret
= my_waitpid (child_pid
, &status
, 0);
240 if (ret
== child_pid
&& WIFSTOPPED (status
)
241 && status
>> 16 == PTRACE_EVENT_FORK
)
244 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
245 if (ret
== 0 && second_pid
!= 0)
249 linux_supports_tracefork_flag
= 1;
250 my_waitpid (second_pid
, &second_status
, 0);
251 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
253 warning ("linux_test_for_tracefork: failed to kill second child");
257 warning ("linux_test_for_tracefork: unexpected result from waitpid "
258 "(%d, status 0x%x)", ret
, status
);
260 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
262 warning ("linux_test_for_tracefork: failed to kill child");
263 my_waitpid (child_pid
, &status
, 0);
266 /* Return non-zero iff we have tracefork functionality available.
267 This function also sets linux_supports_tracefork_flag. */
270 linux_supports_tracefork (int pid
)
272 if (linux_supports_tracefork_flag
== -1)
273 linux_test_for_tracefork (pid
);
274 return linux_supports_tracefork_flag
;
278 linux_supports_tracevforkdone (int pid
)
280 if (linux_supports_tracefork_flag
== -1)
281 linux_test_for_tracefork (pid
);
282 return linux_supports_tracevforkdone_flag
;
287 linux_enable_event_reporting (ptid_t ptid
)
289 int pid
= ptid_get_pid (ptid
);
292 if (! linux_supports_tracefork (pid
))
295 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
296 | PTRACE_O_TRACECLONE
;
297 if (linux_supports_tracevforkdone (pid
))
298 options
|= PTRACE_O_TRACEVFORKDONE
;
300 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
301 read-only process state. */
303 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
307 child_post_attach (int pid
)
309 linux_enable_event_reporting (pid_to_ptid (pid
));
313 linux_child_post_startup_inferior (ptid_t ptid
)
315 linux_enable_event_reporting (ptid
);
318 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
320 child_post_startup_inferior (ptid_t ptid
)
322 linux_child_post_startup_inferior (ptid
);
327 child_follow_fork (int follow_child
)
330 struct target_waitstatus last_status
;
332 int parent_pid
, child_pid
;
334 get_last_target_status (&last_ptid
, &last_status
);
335 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
336 parent_pid
= ptid_get_pid (last_ptid
);
337 child_pid
= last_status
.value
.related_pid
;
341 /* We're already attached to the parent, by default. */
343 /* Before detaching from the child, remove all breakpoints from
344 it. (This won't actually modify the breakpoint list, but will
345 physically remove the breakpoints from the child.) */
346 /* If we vforked this will remove the breakpoints from the parent
347 also, but they'll be reinserted below. */
348 detach_breakpoints (child_pid
);
350 fprintf_filtered (gdb_stdout
,
351 "Detaching after fork from child process %d.\n",
354 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
358 gdb_assert (linux_supports_tracefork_flag
>= 0);
359 if (linux_supports_tracevforkdone (0))
363 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
364 waitpid (parent_pid
, &status
, __WALL
);
365 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
366 warning ("Unexpected waitpid result %06x when waiting for "
367 "vfork-done", status
);
371 /* We can't insert breakpoints until the child has
372 finished with the shared memory region. We need to
373 wait until that happens. Ideal would be to just
375 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
376 - waitpid (parent_pid, &status, __WALL);
377 However, most architectures can't handle a syscall
378 being traced on the way out if it wasn't traced on
381 We might also think to loop, continuing the child
382 until it exits or gets a SIGTRAP. One problem is
383 that the child might call ptrace with PTRACE_TRACEME.
385 There's no simple and reliable way to figure out when
386 the vforked child will be done with its copy of the
387 shared memory. We could step it out of the syscall,
388 two instructions, let it go, and then single-step the
389 parent once. When we have hardware single-step, this
390 would work; with software single-step it could still
391 be made to work but we'd have to be able to insert
392 single-step breakpoints in the child, and we'd have
393 to insert -just- the single-step breakpoint in the
394 parent. Very awkward.
396 In the end, the best we can do is to make sure it
397 runs for a little while. Hopefully it will be out of
398 range of any breakpoints we reinsert. Usually this
399 is only the single-step breakpoint at vfork's return
405 /* Since we vforked, breakpoints were removed in the parent
406 too. Put them back. */
407 reattach_breakpoints (parent_pid
);
412 char child_pid_spelling
[40];
414 /* Needed to keep the breakpoint lists in sync. */
416 detach_breakpoints (child_pid
);
418 /* Before detaching from the parent, remove all breakpoints from it. */
419 remove_breakpoints ();
421 fprintf_filtered (gdb_stdout
,
422 "Attaching after fork to child process %d.\n",
425 /* If we're vforking, we may want to hold on to the parent until
426 the child exits or execs. At exec time we can remove the old
427 breakpoints from the parent and detach it; at exit time we
428 could do the same (or even, sneakily, resume debugging it - the
429 child's exec has failed, or something similar).
431 This doesn't clean up "properly", because we can't call
432 target_detach, but that's OK; if the current target is "child",
433 then it doesn't need any further cleanups, and lin_lwp will
434 generally not encounter vfork (vfork is defined to fork
437 The holding part is very easy if we have VFORKDONE events;
438 but keeping track of both processes is beyond GDB at the
439 moment. So we don't expose the parent to the rest of GDB.
440 Instead we quietly hold onto it until such time as we can
444 linux_parent_pid
= parent_pid
;
446 target_detach (NULL
, 0);
448 inferior_ptid
= pid_to_ptid (child_pid
);
449 push_target (&deprecated_child_ops
);
451 /* Reset breakpoints in the child as appropriate. */
452 follow_inferior_reset_breakpoints ();
459 linux_handle_extended_wait (int pid
, int status
,
460 struct target_waitstatus
*ourstatus
)
462 int event
= status
>> 16;
464 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
465 || event
== PTRACE_EVENT_CLONE
)
467 unsigned long new_pid
;
470 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
472 /* If we haven't already seen the new PID stop, wait for it now. */
473 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
475 /* The new child has a pending SIGSTOP. We can't affect it until it
476 hits the SIGSTOP, but we're already attached. */
478 ret
= waitpid (new_pid
, &status
,
479 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
480 } while (ret
== -1 && errno
== EINTR
);
482 perror_with_name ("waiting for new child");
483 else if (ret
!= new_pid
)
484 internal_error (__FILE__
, __LINE__
,
485 "wait returned unexpected PID %d", ret
);
486 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
487 internal_error (__FILE__
, __LINE__
,
488 "wait returned unexpected status 0x%x", status
);
491 if (event
== PTRACE_EVENT_FORK
)
492 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
493 else if (event
== PTRACE_EVENT_VFORK
)
494 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
496 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
498 ourstatus
->value
.related_pid
= new_pid
;
499 return inferior_ptid
;
502 if (event
== PTRACE_EVENT_EXEC
)
504 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
505 ourstatus
->value
.execd_pathname
506 = xstrdup (child_pid_to_exec_file (pid
));
508 if (linux_parent_pid
)
510 detach_breakpoints (linux_parent_pid
);
511 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
513 linux_parent_pid
= 0;
516 return inferior_ptid
;
519 internal_error (__FILE__
, __LINE__
,
520 "unknown ptrace event %d", event
);
525 child_insert_fork_catchpoint (int pid
)
527 if (! linux_supports_tracefork (pid
))
528 error ("Your system does not support fork catchpoints.");
534 child_insert_vfork_catchpoint (int pid
)
536 if (!linux_supports_tracefork (pid
))
537 error ("Your system does not support vfork catchpoints.");
543 child_insert_exec_catchpoint (int pid
)
545 if (!linux_supports_tracefork (pid
))
546 error ("Your system does not support exec catchpoints.");
555 int pid
= PIDGET (inferior_ptid
);
556 struct target_waitstatus last
;
563 /* If we're stopped while forking and we haven't followed yet, kill the
564 other task. We need to do this first because the parent will be
565 sleeping if this is a vfork. */
567 get_last_target_status (&last_ptid
, &last
);
569 if (last
.kind
== TARGET_WAITKIND_FORKED
570 || last
.kind
== TARGET_WAITKIND_VFORKED
)
572 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
576 /* Kill the current process. */
577 ptrace (PT_KILL
, pid
, 0, 0);
578 ret
= wait (&status
);
580 /* We might get a SIGCHLD instead of an exit status. This is
581 aggravated by the first kill above - a child has just died. */
583 while (ret
== pid
&& WIFSTOPPED (status
))
585 ptrace (PT_KILL
, pid
, 0, 0);
586 ret
= wait (&status
);
589 target_mourn_inferior ();
592 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
593 are processes sharing the same VM space. A multi-threaded process
594 is basically a group of such processes. However, such a grouping
595 is almost entirely a user-space issue; the kernel doesn't enforce
596 such a grouping at all (this might change in the future). In
597 general, we'll rely on the threads library (i.e. the GNU/Linux
598 Threads library) to provide such a grouping.
600 It is perfectly well possible to write a multi-threaded application
601 without the assistance of a threads library, by using the clone
602 system call directly. This module should be able to give some
603 rudimentary support for debugging such applications if developers
604 specify the CLONE_PTRACE flag in the clone system call, and are
605 using the Linux kernel 2.4 or above.
607 Note that there are some peculiarities in GNU/Linux that affect
610 - In general one should specify the __WCLONE flag to waitpid in
611 order to make it report events for any of the cloned processes
612 (and leave it out for the initial process). However, if a cloned
613 process has exited the exit status is only reported if the
614 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
615 we cannot use it since GDB must work on older systems too.
617 - When a traced, cloned process exits and is waited for by the
618 debugger, the kernel reassigns it to the original parent and
619 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
620 library doesn't notice this, which leads to the "zombie problem":
621 When debugged a multi-threaded process that spawns a lot of
622 threads will run out of processes, even if the threads exit,
623 because the "zombies" stay around. */
625 /* List of known LWPs. */
626 static struct lwp_info
*lwp_list
;
628 /* Number of LWPs in the list. */
631 /* Non-zero if we're running in "threaded" mode. */
635 #define GET_LWP(ptid) ptid_get_lwp (ptid)
636 #define GET_PID(ptid) ptid_get_pid (ptid)
637 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
638 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
640 /* If the last reported event was a SIGTRAP, this variable is set to
641 the process id of the LWP/thread that got it. */
645 /* This module's target-specific operations. */
646 static struct target_ops linux_nat_ops
;
648 /* Since we cannot wait (in linux_nat_wait) for the initial process and
649 any cloned processes with a single call to waitpid, we have to use
650 the WNOHANG flag and call waitpid in a loop. To optimize
651 things a bit we use `sigsuspend' to wake us up when a process has
652 something to report (it will send us a SIGCHLD if it has). To make
653 this work we have to juggle with the signal mask. We save the
654 original signal mask such that we can restore it before creating a
655 new process in order to avoid blocking certain signals in the
656 inferior. We then block SIGCHLD during the waitpid/sigsuspend
659 /* Original signal mask. */
660 static sigset_t normal_mask
;
662 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
663 _initialize_linux_nat. */
664 static sigset_t suspend_mask
;
666 /* Signals to block to make that sigsuspend work. */
667 static sigset_t blocked_mask
;
670 /* Prototypes for local functions. */
671 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
672 static int linux_nat_thread_alive (ptid_t ptid
);
674 /* Convert wait status STATUS to a string. Used for printing debug
678 status_to_str (int status
)
682 if (WIFSTOPPED (status
))
683 snprintf (buf
, sizeof (buf
), "%s (stopped)",
684 strsignal (WSTOPSIG (status
)));
685 else if (WIFSIGNALED (status
))
686 snprintf (buf
, sizeof (buf
), "%s (terminated)",
687 strsignal (WSTOPSIG (status
)));
689 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
694 /* Initialize the list of LWPs. Note that this module, contrary to
695 what GDB's generic threads layer does for its thread list,
696 re-initializes the LWP lists whenever we mourn or detach (which
697 doesn't involve mourning) the inferior. */
702 struct lwp_info
*lp
, *lpnext
;
704 for (lp
= lwp_list
; lp
; lp
= lpnext
)
715 /* Add the LWP specified by PID to the list. If this causes the
716 number of LWPs to become larger than one, go into "threaded" mode.
717 Return a pointer to the 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
;
742 /* Remove the LWP specified by PID from the list. */
745 delete_lwp (ptid_t ptid
)
747 struct lwp_info
*lp
, *lpprev
;
751 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
752 if (ptid_equal (lp
->ptid
, ptid
))
758 /* We don't go back to "non-threaded" mode if the number of threads
759 becomes less than two. */
763 lpprev
->next
= lp
->next
;
770 /* Return a pointer to the structure describing the LWP corresponding
771 to PID. If no corresponding LWP could be found, return NULL. */
773 static struct lwp_info
*
774 find_lwp_pid (ptid_t ptid
)
780 lwp
= GET_LWP (ptid
);
782 lwp
= GET_PID (ptid
);
784 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
785 if (lwp
== GET_LWP (lp
->ptid
))
791 /* Call CALLBACK with its second argument set to DATA for every LWP in
792 the list. If CALLBACK returns 1 for a particular LWP, return a
793 pointer to the structure describing that LWP immediately.
794 Otherwise return NULL. */
797 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
799 struct lwp_info
*lp
, *lpnext
;
801 for (lp
= lwp_list
; lp
; lp
= lpnext
)
804 if ((*callback
) (lp
, data
))
811 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
812 a message telling the user that a new LWP has been added to the
816 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
818 struct lwp_info
*lp
, *found_lp
;
820 gdb_assert (is_lwp (ptid
));
822 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
823 to interrupt either the ptrace() or waitpid() calls below. */
824 if (!sigismember (&blocked_mask
, SIGCHLD
))
826 sigaddset (&blocked_mask
, SIGCHLD
);
827 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
831 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
833 found_lp
= lp
= find_lwp_pid (ptid
);
837 /* We assume that we're already attached to any LWP that has an id
838 equal to the overall process id, and to any LWP that is already
839 in our list of LWPs. If we're not seeing exit events from threads
840 and we've had PID wraparound since we last tried to stop all threads,
841 this assumption might be wrong; fortunately, this is very unlikely
843 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
848 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
849 error ("Can't attach %s: %s", target_pid_to_str (ptid
),
850 safe_strerror (errno
));
853 fprintf_unfiltered (gdb_stdlog
,
854 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
855 target_pid_to_str (ptid
));
857 pid
= waitpid (GET_LWP (ptid
), &status
, 0);
858 if (pid
== -1 && errno
== ECHILD
)
860 /* Try again with __WCLONE to check cloned processes. */
861 pid
= waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
865 gdb_assert (pid
== GET_LWP (ptid
)
866 && WIFSTOPPED (status
) && WSTOPSIG (status
));
868 child_post_attach (pid
);
874 fprintf_unfiltered (gdb_stdlog
,
875 "LLAL: waitpid %s received %s\n",
876 target_pid_to_str (ptid
),
877 status_to_str (status
));
882 /* We assume that the LWP representing the original process is
883 already stopped. Mark it as stopped in the data structure
884 that the linux ptrace layer uses to keep track of threads.
885 Note that this won't have already been done since the main
886 thread will have, we assume, been stopped by an attach from a
893 linux_nat_attach (char *args
, int from_tty
)
899 /* FIXME: We should probably accept a list of process id's, and
900 attach all of them. */
901 deprecated_child_ops
.to_attach (args
, from_tty
);
903 /* Add the initial process as the first LWP to the list. */
904 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
906 /* Make sure the initial process is stopped. The user-level threads
907 layer might want to poke around in the inferior, and that won't
908 work if things haven't stabilized yet. */
909 pid
= waitpid (GET_PID (inferior_ptid
), &status
, 0);
910 if (pid
== -1 && errno
== ECHILD
)
912 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid
));
914 /* Try again with __WCLONE to check cloned processes. */
915 pid
= waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
919 gdb_assert (pid
== GET_PID (inferior_ptid
)
920 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
924 /* Fake the SIGSTOP that core GDB expects. */
925 lp
->status
= W_STOPCODE (SIGSTOP
);
929 fprintf_unfiltered (gdb_stdlog
,
930 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
935 detach_callback (struct lwp_info
*lp
, void *data
)
937 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
939 if (debug_linux_nat
&& lp
->status
)
940 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
941 strsignal (WSTOPSIG (lp
->status
)),
942 target_pid_to_str (lp
->ptid
));
944 while (lp
->signalled
&& lp
->stopped
)
947 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
948 WSTOPSIG (lp
->status
)) < 0)
949 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
950 safe_strerror (errno
));
953 fprintf_unfiltered (gdb_stdlog
,
954 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
955 target_pid_to_str (lp
->ptid
),
956 status_to_str (lp
->status
));
961 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
962 here. But since lp->signalled was cleared above,
963 stop_wait_callback didn't do anything; the process was left
964 running. Shouldn't we be waiting for it to stop?
965 I've removed the call, since stop_wait_callback now does do
966 something when called with lp->signalled == 0. */
968 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
971 /* We don't actually detach from the LWP that has an id equal to the
972 overall process id just yet. */
973 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
976 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
977 WSTOPSIG (lp
->status
)) < 0)
978 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
979 safe_strerror (errno
));
982 fprintf_unfiltered (gdb_stdlog
,
983 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
984 target_pid_to_str (lp
->ptid
),
985 strsignal (WSTOPSIG (lp
->status
)));
987 delete_lwp (lp
->ptid
);
994 linux_nat_detach (char *args
, int from_tty
)
996 iterate_over_lwps (detach_callback
, NULL
);
998 /* Only the initial process should be left right now. */
999 gdb_assert (num_lwps
== 1);
1001 trap_ptid
= null_ptid
;
1003 /* Destroy LWP info; it's no longer valid. */
1006 /* Restore the original signal mask. */
1007 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1008 sigemptyset (&blocked_mask
);
1010 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
1011 deprecated_child_ops
.to_detach (args
, from_tty
);
1017 resume_callback (struct lwp_info
*lp
, void *data
)
1019 if (lp
->stopped
&& lp
->status
== 0)
1021 struct thread_info
*tp
;
1023 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
1024 if (debug_linux_nat
)
1025 fprintf_unfiltered (gdb_stdlog
,
1026 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1027 target_pid_to_str (lp
->ptid
));
1036 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1043 resume_set_callback (struct lwp_info
*lp
, void *data
)
1050 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1052 struct lwp_info
*lp
;
1055 /* A specific PTID means `step only this process id'. */
1056 resume_all
= (PIDGET (ptid
) == -1);
1059 iterate_over_lwps (resume_set_callback
, NULL
);
1061 iterate_over_lwps (resume_clear_callback
, NULL
);
1063 /* If PID is -1, it's the current inferior that should be
1064 handled specially. */
1065 if (PIDGET (ptid
) == -1)
1066 ptid
= inferior_ptid
;
1068 lp
= find_lwp_pid (ptid
);
1071 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1073 /* Remember if we're stepping. */
1076 /* Mark this LWP as resumed. */
1079 /* If we have a pending wait status for this thread, there is no
1080 point in resuming the process. */
1083 /* FIXME: What should we do if we are supposed to continue
1084 this thread with a signal? */
1085 gdb_assert (signo
== TARGET_SIGNAL_0
);
1089 /* Mark LWP as not stopped to prevent it from being continued by
1095 iterate_over_lwps (resume_callback
, NULL
);
1097 child_resume (ptid
, step
, signo
);
1098 if (debug_linux_nat
)
1099 fprintf_unfiltered (gdb_stdlog
,
1100 "LLR: %s %s, %s (resume event thread)\n",
1101 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1102 target_pid_to_str (ptid
),
1103 signo
? strsignal (signo
) : "0");
1106 /* Issue kill to specified lwp. */
1108 static int tkill_failed
;
1111 kill_lwp (int lwpid
, int signo
)
1115 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1116 fails, then we are not using nptl threads and we should be using kill. */
1118 #ifdef HAVE_TKILL_SYSCALL
1121 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1122 if (errno
!= ENOSYS
)
1129 return kill (lwpid
, signo
);
1132 /* Handle a GNU/Linux extended wait response. Most of the work we
1133 just pass off to linux_handle_extended_wait, but if it reports a
1134 clone event we need to add the new LWP to our list (and not report
1135 the trap to higher layers). This function returns non-zero if
1136 the event should be ignored and we should wait again. */
1139 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1141 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1144 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1145 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1147 struct lwp_info
*new_lp
;
1148 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1149 GET_PID (inferior_ptid
)));
1151 new_lp
->stopped
= 1;
1153 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1155 if (debug_linux_nat
)
1156 fprintf_unfiltered (gdb_stdlog
,
1157 "LLHE: Got clone event from LWP %ld, resuming\n",
1158 GET_LWP (lp
->ptid
));
1159 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1167 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1171 wait_lwp (struct lwp_info
*lp
)
1175 int thread_dead
= 0;
1177 gdb_assert (!lp
->stopped
);
1178 gdb_assert (lp
->status
== 0);
1180 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1181 if (pid
== -1 && errno
== ECHILD
)
1183 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1184 if (pid
== -1 && errno
== ECHILD
)
1186 /* The thread has previously exited. We need to delete it
1187 now because, for some vendor 2.4 kernels with NPTL
1188 support backported, there won't be an exit event unless
1189 it is the main thread. 2.6 kernels will report an exit
1190 event for each thread that exits, as expected. */
1192 if (debug_linux_nat
)
1193 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1194 target_pid_to_str (lp
->ptid
));
1200 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1202 if (debug_linux_nat
)
1204 fprintf_unfiltered (gdb_stdlog
,
1205 "WL: waitpid %s received %s\n",
1206 target_pid_to_str (lp
->ptid
),
1207 status_to_str (status
));
1211 /* Check if the thread has exited. */
1212 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1215 if (debug_linux_nat
)
1216 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1217 target_pid_to_str (lp
->ptid
));
1222 if (in_thread_list (lp
->ptid
))
1224 /* Core GDB cannot deal with us deleting the current thread. */
1225 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1226 delete_thread (lp
->ptid
);
1227 printf_unfiltered ("[%s exited]\n",
1228 target_pid_to_str (lp
->ptid
));
1231 delete_lwp (lp
->ptid
);
1235 gdb_assert (WIFSTOPPED (status
));
1237 /* Handle GNU/Linux's extended waitstatus for trace events. */
1238 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1240 if (debug_linux_nat
)
1241 fprintf_unfiltered (gdb_stdlog
,
1242 "WL: Handling extended status 0x%06x\n",
1244 if (linux_nat_handle_extended (lp
, status
))
1245 return wait_lwp (lp
);
1251 /* Send a SIGSTOP to LP. */
1254 stop_callback (struct lwp_info
*lp
, void *data
)
1256 if (!lp
->stopped
&& !lp
->signalled
)
1260 if (debug_linux_nat
)
1262 fprintf_unfiltered (gdb_stdlog
,
1263 "SC: kill %s **<SIGSTOP>**\n",
1264 target_pid_to_str (lp
->ptid
));
1267 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1268 if (debug_linux_nat
)
1270 fprintf_unfiltered (gdb_stdlog
,
1271 "SC: lwp kill %d %s\n",
1273 errno
? safe_strerror (errno
) : "ERRNO-OK");
1277 gdb_assert (lp
->status
== 0);
1283 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1284 a pointer to a set of signals to be flushed immediately. */
1287 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1289 sigset_t
*flush_mask
= data
;
1295 status
= wait_lwp (lp
);
1299 /* Ignore any signals in FLUSH_MASK. */
1300 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1309 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1310 if (debug_linux_nat
)
1311 fprintf_unfiltered (gdb_stdlog
,
1312 "PTRACE_CONT %s, 0, 0 (%s)\n",
1313 target_pid_to_str (lp
->ptid
),
1314 errno
? safe_strerror (errno
) : "OK");
1316 return stop_wait_callback (lp
, flush_mask
);
1319 if (WSTOPSIG (status
) != SIGSTOP
)
1321 if (WSTOPSIG (status
) == SIGTRAP
)
1323 /* If a LWP other than the LWP that we're reporting an
1324 event for has hit a GDB breakpoint (as opposed to
1325 some random trap signal), then just arrange for it to
1326 hit it again later. We don't keep the SIGTRAP status
1327 and don't forward the SIGTRAP signal to the LWP. We
1328 will handle the current event, eventually we will
1329 resume all LWPs, and this one will get its breakpoint
1332 If we do not do this, then we run the risk that the
1333 user will delete or disable the breakpoint, but the
1334 thread will have already tripped on it. */
1336 /* Now resume this LWP and get the SIGSTOP event. */
1338 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1339 if (debug_linux_nat
)
1341 fprintf_unfiltered (gdb_stdlog
,
1342 "PTRACE_CONT %s, 0, 0 (%s)\n",
1343 target_pid_to_str (lp
->ptid
),
1344 errno
? safe_strerror (errno
) : "OK");
1346 fprintf_unfiltered (gdb_stdlog
,
1347 "SWC: Candidate SIGTRAP event in %s\n",
1348 target_pid_to_str (lp
->ptid
));
1350 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1351 stop_wait_callback (lp
, data
);
1352 /* If there's another event, throw it back into the queue. */
1355 if (debug_linux_nat
)
1357 fprintf_unfiltered (gdb_stdlog
,
1358 "SWC: kill %s, %s\n",
1359 target_pid_to_str (lp
->ptid
),
1360 status_to_str ((int) status
));
1362 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1364 /* Save the sigtrap event. */
1365 lp
->status
= status
;
1370 /* The thread was stopped with a signal other than
1371 SIGSTOP, and didn't accidentally trip a breakpoint. */
1373 if (debug_linux_nat
)
1375 fprintf_unfiltered (gdb_stdlog
,
1376 "SWC: Pending event %s in %s\n",
1377 status_to_str ((int) status
),
1378 target_pid_to_str (lp
->ptid
));
1380 /* Now resume this LWP and get the SIGSTOP event. */
1382 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1383 if (debug_linux_nat
)
1384 fprintf_unfiltered (gdb_stdlog
,
1385 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1386 target_pid_to_str (lp
->ptid
),
1387 errno
? safe_strerror (errno
) : "OK");
1389 /* Hold this event/waitstatus while we check to see if
1390 there are any more (we still want to get that SIGSTOP). */
1391 stop_wait_callback (lp
, data
);
1392 /* If the lp->status field is still empty, use it to hold
1393 this event. If not, then this event must be returned
1394 to the event queue of the LWP. */
1395 if (lp
->status
== 0)
1396 lp
->status
= status
;
1399 if (debug_linux_nat
)
1401 fprintf_unfiltered (gdb_stdlog
,
1402 "SWC: kill %s, %s\n",
1403 target_pid_to_str (lp
->ptid
),
1404 status_to_str ((int) status
));
1406 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1413 /* We caught the SIGSTOP that we intended to catch, so
1414 there's no SIGSTOP pending. */
1423 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1424 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1427 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1429 sigset_t blocked
, ignored
;
1432 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1437 for (i
= 1; i
< NSIG
; i
++)
1438 if (sigismember (pending
, i
))
1439 if (!sigismember (flush_mask
, i
)
1440 || sigismember (&blocked
, i
)
1441 || sigismember (&ignored
, i
))
1442 sigdelset (pending
, i
);
1444 if (sigisemptyset (pending
))
1450 /* DATA is interpreted as a mask of signals to flush. If LP has
1451 signals pending, and they are all in the flush mask, then arrange
1452 to flush them. LP should be stopped, as should all other threads
1453 it might share a signal queue with. */
1456 flush_callback (struct lwp_info
*lp
, void *data
)
1458 sigset_t
*flush_mask
= data
;
1459 sigset_t pending
, intersection
, blocked
, ignored
;
1462 /* Normally, when an LWP exits, it is removed from the LWP list. The
1463 last LWP isn't removed till later, however. So if there is only
1464 one LWP on the list, make sure it's alive. */
1465 if (lwp_list
== lp
&& lp
->next
== NULL
)
1466 if (!linux_nat_thread_alive (lp
->ptid
))
1469 /* Just because the LWP is stopped doesn't mean that new signals
1470 can't arrive from outside, so this function must be careful of
1471 race conditions. However, because all threads are stopped, we
1472 can assume that the pending mask will not shrink unless we resume
1473 the LWP, and that it will then get another signal. We can't
1474 control which one, however. */
1478 if (debug_linux_nat
)
1479 printf_unfiltered ("FC: LP has pending status %06x\n", lp
->status
);
1480 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1484 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1489 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1490 if (debug_linux_nat
)
1491 fprintf_unfiltered (gdb_stderr
,
1492 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1495 stop_wait_callback (lp
, flush_mask
);
1496 if (debug_linux_nat
)
1497 fprintf_unfiltered (gdb_stderr
,
1498 "FC: Wait finished; saved status is %d\n",
1505 /* Return non-zero if LP has a wait status pending. */
1508 status_callback (struct lwp_info
*lp
, void *data
)
1510 /* Only report a pending wait status if we pretend that this has
1511 indeed been resumed. */
1512 return (lp
->status
!= 0 && lp
->resumed
);
1515 /* Return non-zero if LP isn't stopped. */
1518 running_callback (struct lwp_info
*lp
, void *data
)
1520 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1523 /* Count the LWP's that have had events. */
1526 count_events_callback (struct lwp_info
*lp
, void *data
)
1530 gdb_assert (count
!= NULL
);
1532 /* Count only LWPs that have a SIGTRAP event pending. */
1534 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1540 /* Select the LWP (if any) that is currently being single-stepped. */
1543 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1545 if (lp
->step
&& lp
->status
!= 0)
1551 /* Select the Nth LWP that has had a SIGTRAP event. */
1554 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1556 int *selector
= data
;
1558 gdb_assert (selector
!= NULL
);
1560 /* Select only LWPs that have a SIGTRAP event pending. */
1562 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1563 if ((*selector
)-- == 0)
1570 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1572 struct lwp_info
*event_lp
= data
;
1574 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1578 /* If a LWP other than the LWP that we're reporting an event for has
1579 hit a GDB breakpoint (as opposed to some random trap signal),
1580 then just arrange for it to hit it again later. We don't keep
1581 the SIGTRAP status and don't forward the SIGTRAP signal to the
1582 LWP. We will handle the current event, eventually we will resume
1583 all LWPs, and this one will get its breakpoint trap again.
1585 If we do not do this, then we run the risk that the user will
1586 delete or disable the breakpoint, but the LWP will have already
1590 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1591 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1592 DECR_PC_AFTER_BREAK
))
1594 if (debug_linux_nat
)
1595 fprintf_unfiltered (gdb_stdlog
,
1596 "CBC: Push back breakpoint for %s\n",
1597 target_pid_to_str (lp
->ptid
));
1599 /* Back up the PC if necessary. */
1600 if (DECR_PC_AFTER_BREAK
)
1601 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1603 /* Throw away the SIGTRAP. */
1610 /* Select one LWP out of those that have events pending. */
1613 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1616 int random_selector
;
1617 struct lwp_info
*event_lp
;
1619 /* Record the wait status for the origional LWP. */
1620 (*orig_lp
)->status
= *status
;
1622 /* Give preference to any LWP that is being single-stepped. */
1623 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1624 if (event_lp
!= NULL
)
1626 if (debug_linux_nat
)
1627 fprintf_unfiltered (gdb_stdlog
,
1628 "SEL: Select single-step %s\n",
1629 target_pid_to_str (event_lp
->ptid
));
1633 /* No single-stepping LWP. Select one at random, out of those
1634 which have had SIGTRAP events. */
1636 /* First see how many SIGTRAP events we have. */
1637 iterate_over_lwps (count_events_callback
, &num_events
);
1639 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1640 random_selector
= (int)
1641 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1643 if (debug_linux_nat
&& num_events
> 1)
1644 fprintf_unfiltered (gdb_stdlog
,
1645 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1646 num_events
, random_selector
);
1648 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1652 if (event_lp
!= NULL
)
1654 /* Switch the event LWP. */
1655 *orig_lp
= event_lp
;
1656 *status
= event_lp
->status
;
1659 /* Flush the wait status for the event LWP. */
1660 (*orig_lp
)->status
= 0;
1663 /* Return non-zero if LP has been resumed. */
1666 resumed_callback (struct lwp_info
*lp
, void *data
)
1673 /* We need to override child_wait to support attaching to cloned
1674 processes, since a normal wait (as done by the default version)
1675 ignores those processes. */
1677 /* Wait for child PTID to do something. Return id of the child,
1678 minus_one_ptid in case of error; store status into *OURSTATUS. */
1681 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1687 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1691 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1692 attached process. */
1695 pid
= waitpid (GET_PID (ptid
), &status
, 0);
1696 if (pid
== -1 && errno
== ECHILD
)
1697 /* Try again with __WCLONE to check cloned processes. */
1698 pid
= waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1700 if (debug_linux_nat
)
1702 fprintf_unfiltered (gdb_stdlog
,
1703 "CW: waitpid %ld received %s\n",
1704 (long) pid
, status_to_str (status
));
1709 /* Make sure we don't report an event for the exit of the
1710 original program, if we've detached from it. */
1711 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1717 /* Check for stop events reported by a process we didn't already
1718 know about - in this case, anything other than inferior_ptid.
1720 If we're expecting to receive stopped processes after fork,
1721 vfork, and clone events, then we'll just add the new one to
1722 our list and go back to waiting for the event to be reported
1723 - the stopped process might be returned from waitpid before
1724 or after the event is. If we want to handle debugging of
1725 CLONE_PTRACE processes we need to do more here, i.e. switch
1726 to multi-threaded mode. */
1727 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1728 && pid
!= GET_PID (inferior_ptid
))
1730 linux_record_stopped_pid (pid
);
1735 /* Handle GNU/Linux's extended waitstatus for trace events. */
1736 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1737 && status
>> 16 != 0)
1739 linux_handle_extended_wait (pid
, status
, ourstatus
);
1741 /* If we see a clone event, detach the child, and don't
1742 report the event. It would be nice to offer some way to
1743 switch into a non-thread-db based threaded mode at this
1745 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1747 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1748 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1749 ptrace (PTRACE_CONT
, pid
, 0, 0);
1755 clear_sigio_trap ();
1756 clear_sigint_trap ();
1758 while (pid
== -1 && save_errno
== EINTR
);
1762 warning ("Child process unexpectedly missing: %s",
1763 safe_strerror (errno
));
1765 /* Claim it exited with unknown signal. */
1766 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1767 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1768 return minus_one_ptid
;
1771 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1772 store_waitstatus (ourstatus
, status
);
1774 return pid_to_ptid (pid
);
1779 /* Stop an active thread, verify it still exists, then resume it. */
1782 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1784 struct lwp_info
*ptr
;
1786 if (!lp
->stopped
&& !lp
->signalled
)
1788 stop_callback (lp
, NULL
);
1789 stop_wait_callback (lp
, NULL
);
1790 /* Resume if the lwp still exists. */
1791 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1794 resume_callback (lp
, NULL
);
1795 resume_set_callback (lp
, NULL
);
1802 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1804 struct lwp_info
*lp
= NULL
;
1807 pid_t pid
= PIDGET (ptid
);
1808 sigset_t flush_mask
;
1810 sigemptyset (&flush_mask
);
1812 /* Make sure SIGCHLD is blocked. */
1813 if (!sigismember (&blocked_mask
, SIGCHLD
))
1815 sigaddset (&blocked_mask
, SIGCHLD
);
1816 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1821 /* Make sure there is at least one LWP that has been resumed, at
1822 least if there are any LWPs at all. */
1823 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1825 /* First check if there is a LWP with a wait status pending. */
1828 /* Any LWP that's been resumed will do. */
1829 lp
= iterate_over_lwps (status_callback
, NULL
);
1832 status
= lp
->status
;
1835 if (debug_linux_nat
&& status
)
1836 fprintf_unfiltered (gdb_stdlog
,
1837 "LLW: Using pending wait status %s for %s.\n",
1838 status_to_str (status
),
1839 target_pid_to_str (lp
->ptid
));
1842 /* But if we don't fine one, we'll have to wait, and check both
1843 cloned and uncloned processes. We start with the cloned
1845 options
= __WCLONE
| WNOHANG
;
1847 else if (is_lwp (ptid
))
1849 if (debug_linux_nat
)
1850 fprintf_unfiltered (gdb_stdlog
,
1851 "LLW: Waiting for specific LWP %s.\n",
1852 target_pid_to_str (ptid
));
1854 /* We have a specific LWP to check. */
1855 lp
= find_lwp_pid (ptid
);
1857 status
= lp
->status
;
1860 if (debug_linux_nat
&& status
)
1861 fprintf_unfiltered (gdb_stdlog
,
1862 "LLW: Using pending wait status %s for %s.\n",
1863 status_to_str (status
),
1864 target_pid_to_str (lp
->ptid
));
1866 /* If we have to wait, take into account whether PID is a cloned
1867 process or not. And we have to convert it to something that
1868 the layer beneath us can understand. */
1869 options
= lp
->cloned
? __WCLONE
: 0;
1870 pid
= GET_LWP (ptid
);
1873 if (status
&& lp
->signalled
)
1875 /* A pending SIGSTOP may interfere with the normal stream of
1876 events. In a typical case where interference is a problem,
1877 we have a SIGSTOP signal pending for LWP A while
1878 single-stepping it, encounter an event in LWP B, and take the
1879 pending SIGSTOP while trying to stop LWP A. After processing
1880 the event in LWP B, LWP A is continued, and we'll never see
1881 the SIGTRAP associated with the last time we were
1882 single-stepping LWP A. */
1884 /* Resume the thread. It should halt immediately returning the
1886 registers_changed ();
1887 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1889 if (debug_linux_nat
)
1890 fprintf_unfiltered (gdb_stdlog
,
1891 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1892 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1893 target_pid_to_str (lp
->ptid
));
1895 gdb_assert (lp
->resumed
);
1897 /* This should catch the pending SIGSTOP. */
1898 stop_wait_callback (lp
, NULL
);
1901 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1902 attached process. */
1909 lwpid
= waitpid (pid
, &status
, options
);
1912 gdb_assert (pid
== -1 || lwpid
== pid
);
1914 if (debug_linux_nat
)
1916 fprintf_unfiltered (gdb_stdlog
,
1917 "LLW: waitpid %ld received %s\n",
1918 (long) lwpid
, status_to_str (status
));
1921 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1923 /* Check for stop events reported by a process we didn't
1924 already know about - anything not already in our LWP
1927 If we're expecting to receive stopped processes after
1928 fork, vfork, and clone events, then we'll just add the
1929 new one to our list and go back to waiting for the event
1930 to be reported - the stopped process might be returned
1931 from waitpid before or after the event is. */
1932 if (WIFSTOPPED (status
) && !lp
)
1934 linux_record_stopped_pid (lwpid
);
1939 /* Make sure we don't report an event for the exit of an LWP not in
1940 our list, i.e. not part of the current process. This can happen
1941 if we detach from a program we original forked and then it
1943 if (!WIFSTOPPED (status
) && !lp
)
1949 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1950 CLONE_PTRACE processes which do not use the thread library -
1951 otherwise we wouldn't find the new LWP this way. That doesn't
1952 currently work, and the following code is currently unreachable
1953 due to the two blocks above. If it's fixed some day, this code
1954 should be broken out into a function so that we can also pick up
1955 LWPs from the new interface. */
1958 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1959 if (options
& __WCLONE
)
1964 gdb_assert (WIFSTOPPED (status
)
1965 && WSTOPSIG (status
) == SIGSTOP
);
1968 if (!in_thread_list (inferior_ptid
))
1970 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1971 GET_PID (inferior_ptid
));
1972 add_thread (inferior_ptid
);
1975 add_thread (lp
->ptid
);
1976 printf_unfiltered ("[New %s]\n",
1977 target_pid_to_str (lp
->ptid
));
1981 /* Handle GNU/Linux's extended waitstatus for trace events. */
1982 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1984 if (debug_linux_nat
)
1985 fprintf_unfiltered (gdb_stdlog
,
1986 "LLW: Handling extended status 0x%06x\n",
1988 if (linux_nat_handle_extended (lp
, status
))
1995 /* Check if the thread has exited. */
1996 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1998 if (in_thread_list (lp
->ptid
))
2000 /* Core GDB cannot deal with us deleting the current
2002 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2003 delete_thread (lp
->ptid
);
2004 printf_unfiltered ("[%s exited]\n",
2005 target_pid_to_str (lp
->ptid
));
2008 /* If this is the main thread, we must stop all threads and
2009 verify if they are still alive. This is because in the nptl
2010 thread model, there is no signal issued for exiting LWPs
2011 other than the main thread. We only get the main thread
2012 exit signal once all child threads have already exited.
2013 If we stop all the threads and use the stop_wait_callback
2014 to check if they have exited we can determine whether this
2015 signal should be ignored or whether it means the end of the
2016 debugged application, regardless of which threading model
2018 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2021 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2024 if (debug_linux_nat
)
2025 fprintf_unfiltered (gdb_stdlog
,
2026 "LLW: %s exited.\n",
2027 target_pid_to_str (lp
->ptid
));
2029 delete_lwp (lp
->ptid
);
2031 /* If there is at least one more LWP, then the exit signal
2032 was not the end of the debugged application and should be
2036 /* Make sure there is at least one thread running. */
2037 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2039 /* Discard the event. */
2045 /* Check if the current LWP has previously exited. In the nptl
2046 thread model, LWPs other than the main thread do not issue
2047 signals when they exit so we must check whenever the thread
2048 has stopped. A similar check is made in stop_wait_callback(). */
2049 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2051 if (in_thread_list (lp
->ptid
))
2053 /* Core GDB cannot deal with us deleting the current
2055 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2056 delete_thread (lp
->ptid
);
2057 printf_unfiltered ("[%s exited]\n",
2058 target_pid_to_str (lp
->ptid
));
2060 if (debug_linux_nat
)
2061 fprintf_unfiltered (gdb_stdlog
,
2062 "LLW: %s exited.\n",
2063 target_pid_to_str (lp
->ptid
));
2065 delete_lwp (lp
->ptid
);
2067 /* Make sure there is at least one thread running. */
2068 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2070 /* Discard the event. */
2075 /* Make sure we don't report a SIGSTOP that we sent
2076 ourselves in an attempt to stop an LWP. */
2078 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2080 if (debug_linux_nat
)
2081 fprintf_unfiltered (gdb_stdlog
,
2082 "LLW: Delayed SIGSTOP caught for %s.\n",
2083 target_pid_to_str (lp
->ptid
));
2085 /* This is a delayed SIGSTOP. */
2088 registers_changed ();
2089 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2091 if (debug_linux_nat
)
2092 fprintf_unfiltered (gdb_stdlog
,
2093 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2095 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2096 target_pid_to_str (lp
->ptid
));
2099 gdb_assert (lp
->resumed
);
2101 /* Discard the event. */
2111 /* Alternate between checking cloned and uncloned processes. */
2112 options
^= __WCLONE
;
2114 /* And suspend every time we have checked both. */
2115 if (options
& __WCLONE
)
2116 sigsuspend (&suspend_mask
);
2119 /* We shouldn't end up here unless we want to try again. */
2120 gdb_assert (status
== 0);
2123 clear_sigio_trap ();
2124 clear_sigint_trap ();
2128 /* Don't report signals that GDB isn't interested in, such as
2129 signals that are neither printed nor stopped upon. Stopping all
2130 threads can be a bit time-consuming so if we want decent
2131 performance with heavily multi-threaded programs, especially when
2132 they're using a high frequency timer, we'd better avoid it if we
2135 if (WIFSTOPPED (status
))
2137 int signo
= target_signal_from_host (WSTOPSIG (status
));
2139 if (signal_stop_state (signo
) == 0
2140 && signal_print_state (signo
) == 0
2141 && signal_pass_state (signo
) == 1)
2143 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2144 here? It is not clear we should. GDB may not expect
2145 other threads to run. On the other hand, not resuming
2146 newly attached threads may cause an unwanted delay in
2147 getting them running. */
2148 registers_changed ();
2149 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2150 if (debug_linux_nat
)
2151 fprintf_unfiltered (gdb_stdlog
,
2152 "LLW: %s %s, %s (preempt 'handle')\n",
2154 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2155 target_pid_to_str (lp
->ptid
),
2156 signo
? strsignal (signo
) : "0");
2162 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2164 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2165 forwarded to the entire process group, that is, all LWP's
2166 will receive it. Since we only want to report it once,
2167 we try to flush it from all LWPs except this one. */
2168 sigaddset (&flush_mask
, SIGINT
);
2172 /* This LWP is stopped now. */
2175 if (debug_linux_nat
)
2176 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2177 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2179 /* Now stop all other LWP's ... */
2180 iterate_over_lwps (stop_callback
, NULL
);
2182 /* ... and wait until all of them have reported back that they're no
2184 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2185 iterate_over_lwps (flush_callback
, &flush_mask
);
2187 /* If we're not waiting for a specific LWP, choose an event LWP from
2188 among those that have had events. Giving equal priority to all
2189 LWPs that have had events helps prevent starvation. */
2191 select_event_lwp (&lp
, &status
);
2193 /* Now that we've selected our final event LWP, cancel any
2194 breakpoints in other LWPs that have hit a GDB breakpoint. See
2195 the comment in cancel_breakpoints_callback to find out why. */
2196 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2198 /* If we're not running in "threaded" mode, we'll report the bare
2201 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2203 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2204 if (debug_linux_nat
)
2205 fprintf_unfiltered (gdb_stdlog
,
2206 "LLW: trap_ptid is %s.\n",
2207 target_pid_to_str (trap_ptid
));
2210 trap_ptid
= null_ptid
;
2212 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2214 *ourstatus
= lp
->waitstatus
;
2215 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2218 store_waitstatus (ourstatus
, status
);
2220 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2224 kill_callback (struct lwp_info
*lp
, void *data
)
2227 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2228 if (debug_linux_nat
)
2229 fprintf_unfiltered (gdb_stdlog
,
2230 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2231 target_pid_to_str (lp
->ptid
),
2232 errno
? safe_strerror (errno
) : "OK");
2238 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2242 /* We must make sure that there are no pending events (delayed
2243 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2244 program doesn't interfere with any following debugging session. */
2246 /* For cloned processes we must check both with __WCLONE and
2247 without, since the exit status of a cloned process isn't reported
2253 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2254 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2256 fprintf_unfiltered (gdb_stdlog
,
2257 "KWC: wait %s received unknown.\n",
2258 target_pid_to_str (lp
->ptid
));
2261 while (pid
== GET_LWP (lp
->ptid
));
2263 gdb_assert (pid
== -1 && errno
== ECHILD
);
2268 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2269 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2271 fprintf_unfiltered (gdb_stdlog
,
2272 "KWC: wait %s received unk.\n",
2273 target_pid_to_str (lp
->ptid
));
2276 while (pid
== GET_LWP (lp
->ptid
));
2278 gdb_assert (pid
== -1 && errno
== ECHILD
);
2283 linux_nat_kill (void)
2285 /* Kill all LWP's ... */
2286 iterate_over_lwps (kill_callback
, NULL
);
2288 /* ... and wait until we've flushed all events. */
2289 iterate_over_lwps (kill_wait_callback
, NULL
);
2291 target_mourn_inferior ();
2295 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2298 deprecated_child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2302 linux_nat_mourn_inferior (void)
2304 trap_ptid
= null_ptid
;
2306 /* Destroy LWP info; it's no longer valid. */
2309 /* Restore the original signal mask. */
2310 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2311 sigemptyset (&blocked_mask
);
2313 deprecated_child_ops
.to_mourn_inferior ();
2317 linux_nat_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2318 struct mem_attrib
*attrib
, struct target_ops
*target
)
2320 struct cleanup
*old_chain
= save_inferior_ptid ();
2323 if (is_lwp (inferior_ptid
))
2324 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2326 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2328 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2330 do_cleanups (old_chain
);
2335 linux_nat_thread_alive (ptid_t ptid
)
2337 gdb_assert (is_lwp (ptid
));
2340 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2341 if (debug_linux_nat
)
2342 fprintf_unfiltered (gdb_stdlog
,
2343 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2344 target_pid_to_str (ptid
),
2345 errno
? safe_strerror (errno
) : "OK");
2353 linux_nat_pid_to_str (ptid_t ptid
)
2355 static char buf
[64];
2359 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2363 return normal_pid_to_str (ptid
);
2367 init_linux_nat_ops (void)
2370 linux_nat_ops
.to_open
= linux_nat_open
;
2372 linux_nat_ops
.to_shortname
= "lwp-layer";
2373 linux_nat_ops
.to_longname
= "lwp-layer";
2374 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2375 linux_nat_ops
.to_attach
= linux_nat_attach
;
2376 linux_nat_ops
.to_detach
= linux_nat_detach
;
2377 linux_nat_ops
.to_resume
= linux_nat_resume
;
2378 linux_nat_ops
.to_wait
= linux_nat_wait
;
2379 /* fetch_inferior_registers and store_inferior_registers will
2380 honor the LWP id, so we can use them directly. */
2381 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2382 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2383 linux_nat_ops
.deprecated_xfer_memory
= linux_nat_xfer_memory
;
2384 linux_nat_ops
.to_kill
= linux_nat_kill
;
2385 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2386 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2387 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2388 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2389 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2390 linux_nat_ops
.to_post_attach
= child_post_attach
;
2391 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2392 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2393 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2395 linux_nat_ops
.to_stratum
= thread_stratum
;
2396 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2397 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2401 sigchld_handler (int signo
)
2403 /* Do nothing. The only reason for this handler is that it allows
2404 us to use sigsuspend in linux_nat_wait above to wait for the
2405 arrival of a SIGCHLD. */
2408 /* Accepts an integer PID; Returns a string representing a file that
2409 can be opened to get the symbols for the child process. */
2412 child_pid_to_exec_file (int pid
)
2414 char *name1
, *name2
;
2416 name1
= xmalloc (MAXPATHLEN
);
2417 name2
= xmalloc (MAXPATHLEN
);
2418 make_cleanup (xfree
, name1
);
2419 make_cleanup (xfree
, name2
);
2420 memset (name2
, 0, MAXPATHLEN
);
2422 sprintf (name1
, "/proc/%d/exe", pid
);
2423 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2429 /* Service function for corefiles and info proc. */
2432 read_mapping (FILE *mapfile
,
2437 char *device
, long long *inode
, char *filename
)
2439 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2440 addr
, endaddr
, permissions
, offset
, device
, inode
);
2442 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
2444 /* Eat everything up to EOL for the filename. This will prevent
2445 weird filenames (such as one with embedded whitespace) from
2446 confusing this code. It also makes this code more robust in
2447 respect to annotations the kernel may add after the filename.
2449 Note the filename is used for informational purposes
2451 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2455 filename
[0] = '\0'; /* no filename */
2456 fscanf (mapfile
, "\n");
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\n", 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
);
2939 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
2940 struct mem_attrib
*attrib
, struct target_ops
*target
)
2948 /* Don't bother for one word. */
2949 if (len
< 3 * sizeof (long))
2952 /* We could keep this file open and cache it - possibly one per
2953 thread. That requires some juggling, but is even faster. */
2954 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2955 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2959 /* If pread64 is available, use it. It's faster if the kernel
2960 supports it (only one syscall), and it's 64-bit safe even on
2961 32-bit platforms (for instance, SPARC debugging a SPARC64
2964 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2966 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2976 /* Parse LINE as a signal set and add its set bits to SIGS. */
2979 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2981 int len
= strlen (line
) - 1;
2985 if (line
[len
] != '\n')
2986 error ("Could not parse signal set: %s", line
);
2994 if (*p
>= '0' && *p
<= '9')
2996 else if (*p
>= 'a' && *p
<= 'f')
2997 digit
= *p
- 'a' + 10;
2999 error ("Could not parse signal set: %s", line
);
3004 sigaddset (sigs
, signum
+ 1);
3006 sigaddset (sigs
, signum
+ 2);
3008 sigaddset (sigs
, signum
+ 3);
3010 sigaddset (sigs
, signum
+ 4);
3016 /* Find process PID's pending signals from /proc/pid/status and set
3020 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3023 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3026 sigemptyset (pending
);
3027 sigemptyset (blocked
);
3028 sigemptyset (ignored
);
3029 sprintf (fname
, "/proc/%d/status", pid
);
3030 procfile
= fopen (fname
, "r");
3031 if (procfile
== NULL
)
3032 error ("Could not open %s", fname
);
3034 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3036 /* Normal queued signals are on the SigPnd line in the status
3037 file. However, 2.6 kernels also have a "shared" pending
3038 queue for delivering signals to a thread group, so check for
3041 Unfortunately some Red Hat kernels include the shared pending
3042 queue but not the ShdPnd status field. */
3044 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3045 add_line_to_sigset (buffer
+ 8, pending
);
3046 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3047 add_line_to_sigset (buffer
+ 8, pending
);
3048 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3049 add_line_to_sigset (buffer
+ 8, blocked
);
3050 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3051 add_line_to_sigset (buffer
+ 8, ignored
);
3058 _initialize_linux_nat (void)
3060 struct sigaction action
;
3061 extern void thread_db_init (struct target_ops
*);
3063 deprecated_child_ops
.to_find_memory_regions
= linux_nat_find_memory_regions
;
3064 deprecated_child_ops
.to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3066 add_info ("proc", linux_nat_info_proc_cmd
,
3067 "Show /proc process information about any running process.\n\
3068 Specify any process id, or use the program being debugged by default.\n\
3069 Specify any of the following keywords for detailed info:\n\
3070 mappings -- list of mapped memory regions.\n\
3071 stat -- list a bunch of random process info.\n\
3072 status -- list a different bunch of random process info.\n\
3073 all -- list all available /proc info.");
3075 init_linux_nat_ops ();
3076 add_target (&linux_nat_ops
);
3077 thread_db_init (&linux_nat_ops
);
3079 /* Save the original signal mask. */
3080 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3082 action
.sa_handler
= sigchld_handler
;
3083 sigemptyset (&action
.sa_mask
);
3084 action
.sa_flags
= 0;
3085 sigaction (SIGCHLD
, &action
, NULL
);
3087 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3088 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3089 sigdelset (&suspend_mask
, SIGCHLD
);
3091 sigemptyset (&blocked_mask
);
3093 deprecated_add_show_from_set
3094 (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
3095 (char *) &debug_linux_nat
,
3096 "Set debugging of GNU/Linux lwp module.\n\
3097 Enables printf debugging output.\n", &setdebuglist
), &showdebuglist
);
3101 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3102 the GNU/Linux Threads library and therefore doesn't really belong
3105 /* Read variable NAME in the target and return its value if found.
3106 Otherwise return zero. It is assumed that the type of the variable
3110 get_signo (const char *name
)
3112 struct minimal_symbol
*ms
;
3115 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3119 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
3120 sizeof (signo
)) != 0)
3126 /* Return the set of signals used by the threads library in *SET. */
3129 lin_thread_get_thread_signals (sigset_t
*set
)
3131 struct sigaction action
;
3132 int restart
, cancel
;
3136 restart
= get_signo ("__pthread_sig_restart");
3140 cancel
= get_signo ("__pthread_sig_cancel");
3144 sigaddset (set
, restart
);
3145 sigaddset (set
, cancel
);
3147 /* The GNU/Linux Threads library makes terminating threads send a
3148 special "cancel" signal instead of SIGCHLD. Make sure we catch
3149 those (to prevent them from terminating GDB itself, which is
3150 likely to be their default action) and treat them the same way as
3153 action
.sa_handler
= sigchld_handler
;
3154 sigemptyset (&action
.sa_mask
);
3155 action
.sa_flags
= 0;
3156 sigaction (cancel
, &action
, NULL
);
3158 /* We block the "cancel" signal throughout this code ... */
3159 sigaddset (&blocked_mask
, cancel
);
3160 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3162 /* ... except during a sigsuspend. */
3163 sigdelset (&suspend_mask
, cancel
);