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 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
154 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
155 fork tracing, and let it fork. If the process exits, we assume that
156 we can't use TRACEFORK; if we get the fork notification, and we can
157 extract the new child's PID, then we assume that we can. */
160 linux_test_for_tracefork (void)
162 int child_pid
, ret
, status
;
167 perror_with_name ("linux_test_for_tracefork: fork");
170 linux_tracefork_child ();
172 ret
= waitpid (child_pid
, &status
, 0);
174 perror_with_name ("linux_test_for_tracefork: waitpid");
175 else if (ret
!= child_pid
)
176 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
177 if (! WIFSTOPPED (status
))
178 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
180 linux_supports_tracefork_flag
= 0;
182 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
185 ptrace (PTRACE_KILL
, child_pid
, 0, 0);
186 waitpid (child_pid
, &status
, 0);
190 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
191 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
192 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
193 linux_supports_tracevforkdone_flag
= (ret
== 0);
195 ptrace (PTRACE_CONT
, child_pid
, 0, 0);
196 ret
= waitpid (child_pid
, &status
, 0);
197 if (ret
== child_pid
&& WIFSTOPPED (status
)
198 && status
>> 16 == PTRACE_EVENT_FORK
)
201 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
202 if (ret
== 0 && second_pid
!= 0)
206 linux_supports_tracefork_flag
= 1;
207 waitpid (second_pid
, &second_status
, 0);
208 ptrace (PTRACE_DETACH
, second_pid
, 0, 0);
212 if (WIFSTOPPED (status
))
214 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
215 waitpid (child_pid
, &status
, 0);
219 /* Return non-zero iff we have tracefork functionality available.
220 This function also sets linux_supports_tracefork_flag. */
223 linux_supports_tracefork (void)
225 if (linux_supports_tracefork_flag
== -1)
226 linux_test_for_tracefork ();
227 return linux_supports_tracefork_flag
;
231 linux_supports_tracevforkdone (void)
233 if (linux_supports_tracefork_flag
== -1)
234 linux_test_for_tracefork ();
235 return linux_supports_tracevforkdone_flag
;
240 linux_enable_event_reporting (ptid_t ptid
)
242 int pid
= ptid_get_pid (ptid
);
245 if (! linux_supports_tracefork ())
248 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
249 | PTRACE_O_TRACECLONE
;
250 if (linux_supports_tracevforkdone ())
251 options
|= PTRACE_O_TRACEVFORKDONE
;
253 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
254 read-only process state. */
256 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
260 child_post_attach (int pid
)
262 linux_enable_event_reporting (pid_to_ptid (pid
));
266 linux_child_post_startup_inferior (ptid_t ptid
)
268 linux_enable_event_reporting (ptid
);
271 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
273 child_post_startup_inferior (ptid_t ptid
)
275 linux_child_post_startup_inferior (ptid
);
280 child_follow_fork (int follow_child
)
283 struct target_waitstatus last_status
;
285 int parent_pid
, child_pid
;
287 get_last_target_status (&last_ptid
, &last_status
);
288 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
289 parent_pid
= ptid_get_pid (last_ptid
);
290 child_pid
= last_status
.value
.related_pid
;
294 /* We're already attached to the parent, by default. */
296 /* Before detaching from the child, remove all breakpoints from
297 it. (This won't actually modify the breakpoint list, but will
298 physically remove the breakpoints from the child.) */
299 /* If we vforked this will remove the breakpoints from the parent
300 also, but they'll be reinserted below. */
301 detach_breakpoints (child_pid
);
303 fprintf_filtered (gdb_stdout
,
304 "Detaching after fork from child process %d.\n",
307 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
311 if (linux_supports_tracevforkdone ())
315 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
316 waitpid (parent_pid
, &status
, __WALL
);
317 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
318 warning ("Unexpected waitpid result %06x when waiting for "
319 "vfork-done", status
);
323 /* We can't insert breakpoints until the child has
324 finished with the shared memory region. We need to
325 wait until that happens. Ideal would be to just
327 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
328 - waitpid (parent_pid, &status, __WALL);
329 However, most architectures can't handle a syscall
330 being traced on the way out if it wasn't traced on
333 We might also think to loop, continuing the child
334 until it exits or gets a SIGTRAP. One problem is
335 that the child might call ptrace with PTRACE_TRACEME.
337 There's no simple and reliable way to figure out when
338 the vforked child will be done with its copy of the
339 shared memory. We could step it out of the syscall,
340 two instructions, let it go, and then single-step the
341 parent once. When we have hardware single-step, this
342 would work; with software single-step it could still
343 be made to work but we'd have to be able to insert
344 single-step breakpoints in the child, and we'd have
345 to insert -just- the single-step breakpoint in the
346 parent. Very awkward.
348 In the end, the best we can do is to make sure it
349 runs for a little while. Hopefully it will be out of
350 range of any breakpoints we reinsert. Usually this
351 is only the single-step breakpoint at vfork's return
357 /* Since we vforked, breakpoints were removed in the parent
358 too. Put them back. */
359 reattach_breakpoints (parent_pid
);
364 char child_pid_spelling
[40];
366 /* Needed to keep the breakpoint lists in sync. */
368 detach_breakpoints (child_pid
);
370 /* Before detaching from the parent, remove all breakpoints from it. */
371 remove_breakpoints ();
373 fprintf_filtered (gdb_stdout
,
374 "Attaching after fork to child process %d.\n",
377 /* If we're vforking, we may want to hold on to the parent until
378 the child exits or execs. At exec time we can remove the old
379 breakpoints from the parent and detach it; at exit time we
380 could do the same (or even, sneakily, resume debugging it - the
381 child's exec has failed, or something similar).
383 This doesn't clean up "properly", because we can't call
384 target_detach, but that's OK; if the current target is "child",
385 then it doesn't need any further cleanups, and lin_lwp will
386 generally not encounter vfork (vfork is defined to fork
389 The holding part is very easy if we have VFORKDONE events;
390 but keeping track of both processes is beyond GDB at the
391 moment. So we don't expose the parent to the rest of GDB.
392 Instead we quietly hold onto it until such time as we can
396 linux_parent_pid
= parent_pid
;
398 target_detach (NULL
, 0);
400 inferior_ptid
= pid_to_ptid (child_pid
);
401 push_target (&deprecated_child_ops
);
403 /* Reset breakpoints in the child as appropriate. */
404 follow_inferior_reset_breakpoints ();
411 linux_handle_extended_wait (int pid
, int status
,
412 struct target_waitstatus
*ourstatus
)
414 int event
= status
>> 16;
416 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
417 || event
== PTRACE_EVENT_CLONE
)
419 unsigned long new_pid
;
422 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
424 /* If we haven't already seen the new PID stop, wait for it now. */
425 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
427 /* The new child has a pending SIGSTOP. We can't affect it until it
428 hits the SIGSTOP, but we're already attached. */
430 ret
= waitpid (new_pid
, &status
,
431 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
432 } while (ret
== -1 && errno
== EINTR
);
434 perror_with_name ("waiting for new child");
435 else if (ret
!= new_pid
)
436 internal_error (__FILE__
, __LINE__
,
437 "wait returned unexpected PID %d", ret
);
438 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
439 internal_error (__FILE__
, __LINE__
,
440 "wait returned unexpected status 0x%x", status
);
443 if (event
== PTRACE_EVENT_FORK
)
444 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
445 else if (event
== PTRACE_EVENT_VFORK
)
446 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
448 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
450 ourstatus
->value
.related_pid
= new_pid
;
451 return inferior_ptid
;
454 if (event
== PTRACE_EVENT_EXEC
)
456 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
457 ourstatus
->value
.execd_pathname
458 = xstrdup (child_pid_to_exec_file (pid
));
460 if (linux_parent_pid
)
462 detach_breakpoints (linux_parent_pid
);
463 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
465 linux_parent_pid
= 0;
468 return inferior_ptid
;
471 internal_error (__FILE__
, __LINE__
,
472 "unknown ptrace event %d", event
);
477 child_insert_fork_catchpoint (int pid
)
479 if (! linux_supports_tracefork ())
480 error ("Your system does not support fork catchpoints.");
486 child_insert_vfork_catchpoint (int pid
)
488 if (!linux_supports_tracefork ())
489 error ("Your system does not support vfork catchpoints.");
495 child_insert_exec_catchpoint (int pid
)
497 if (!linux_supports_tracefork ())
498 error ("Your system does not support exec catchpoints.");
507 int pid
= PIDGET (inferior_ptid
);
508 struct target_waitstatus last
;
515 /* If we're stopped while forking and we haven't followed yet, kill the
516 other task. We need to do this first because the parent will be
517 sleeping if this is a vfork. */
519 get_last_target_status (&last_ptid
, &last
);
521 if (last
.kind
== TARGET_WAITKIND_FORKED
522 || last
.kind
== TARGET_WAITKIND_VFORKED
)
524 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
528 /* Kill the current process. */
529 ptrace (PT_KILL
, pid
, 0, 0);
530 ret
= wait (&status
);
532 /* We might get a SIGCHLD instead of an exit status. This is
533 aggravated by the first kill above - a child has just died. */
535 while (ret
== pid
&& WIFSTOPPED (status
))
537 ptrace (PT_KILL
, pid
, 0, 0);
538 ret
= wait (&status
);
541 target_mourn_inferior ();
544 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
545 are processes sharing the same VM space. A multi-threaded process
546 is basically a group of such processes. However, such a grouping
547 is almost entirely a user-space issue; the kernel doesn't enforce
548 such a grouping at all (this might change in the future). In
549 general, we'll rely on the threads library (i.e. the GNU/Linux
550 Threads library) to provide such a grouping.
552 It is perfectly well possible to write a multi-threaded application
553 without the assistance of a threads library, by using the clone
554 system call directly. This module should be able to give some
555 rudimentary support for debugging such applications if developers
556 specify the CLONE_PTRACE flag in the clone system call, and are
557 using the Linux kernel 2.4 or above.
559 Note that there are some peculiarities in GNU/Linux that affect
562 - In general one should specify the __WCLONE flag to waitpid in
563 order to make it report events for any of the cloned processes
564 (and leave it out for the initial process). However, if a cloned
565 process has exited the exit status is only reported if the
566 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
567 we cannot use it since GDB must work on older systems too.
569 - When a traced, cloned process exits and is waited for by the
570 debugger, the kernel reassigns it to the original parent and
571 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
572 library doesn't notice this, which leads to the "zombie problem":
573 When debugged a multi-threaded process that spawns a lot of
574 threads will run out of processes, even if the threads exit,
575 because the "zombies" stay around. */
577 /* List of known LWPs. */
578 static struct lwp_info
*lwp_list
;
580 /* Number of LWPs in the list. */
583 /* Non-zero if we're running in "threaded" mode. */
587 #define GET_LWP(ptid) ptid_get_lwp (ptid)
588 #define GET_PID(ptid) ptid_get_pid (ptid)
589 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
590 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
592 /* If the last reported event was a SIGTRAP, this variable is set to
593 the process id of the LWP/thread that got it. */
597 /* This module's target-specific operations. */
598 static struct target_ops linux_nat_ops
;
600 /* Since we cannot wait (in linux_nat_wait) for the initial process and
601 any cloned processes with a single call to waitpid, we have to use
602 the WNOHANG flag and call waitpid in a loop. To optimize
603 things a bit we use `sigsuspend' to wake us up when a process has
604 something to report (it will send us a SIGCHLD if it has). To make
605 this work we have to juggle with the signal mask. We save the
606 original signal mask such that we can restore it before creating a
607 new process in order to avoid blocking certain signals in the
608 inferior. We then block SIGCHLD during the waitpid/sigsuspend
611 /* Original signal mask. */
612 static sigset_t normal_mask
;
614 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
615 _initialize_linux_nat. */
616 static sigset_t suspend_mask
;
618 /* Signals to block to make that sigsuspend work. */
619 static sigset_t blocked_mask
;
622 /* Prototypes for local functions. */
623 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
624 static int linux_nat_thread_alive (ptid_t ptid
);
626 /* Convert wait status STATUS to a string. Used for printing debug
630 status_to_str (int status
)
634 if (WIFSTOPPED (status
))
635 snprintf (buf
, sizeof (buf
), "%s (stopped)",
636 strsignal (WSTOPSIG (status
)));
637 else if (WIFSIGNALED (status
))
638 snprintf (buf
, sizeof (buf
), "%s (terminated)",
639 strsignal (WSTOPSIG (status
)));
641 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
646 /* Initialize the list of LWPs. Note that this module, contrary to
647 what GDB's generic threads layer does for its thread list,
648 re-initializes the LWP lists whenever we mourn or detach (which
649 doesn't involve mourning) the inferior. */
654 struct lwp_info
*lp
, *lpnext
;
656 for (lp
= lwp_list
; lp
; lp
= lpnext
)
667 /* Add the LWP specified by PID to the list. If this causes the
668 number of LWPs to become larger than one, go into "threaded" mode.
669 Return a pointer to the structure describing the new LWP. */
671 static struct lwp_info
*
672 add_lwp (ptid_t ptid
)
676 gdb_assert (is_lwp (ptid
));
678 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
680 memset (lp
, 0, sizeof (struct lwp_info
));
682 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
694 /* Remove the LWP specified by PID from the list. */
697 delete_lwp (ptid_t ptid
)
699 struct lwp_info
*lp
, *lpprev
;
703 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
704 if (ptid_equal (lp
->ptid
, ptid
))
710 /* We don't go back to "non-threaded" mode if the number of threads
711 becomes less than two. */
715 lpprev
->next
= lp
->next
;
722 /* Return a pointer to the structure describing the LWP corresponding
723 to PID. If no corresponding LWP could be found, return NULL. */
725 static struct lwp_info
*
726 find_lwp_pid (ptid_t ptid
)
732 lwp
= GET_LWP (ptid
);
734 lwp
= GET_PID (ptid
);
736 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
737 if (lwp
== GET_LWP (lp
->ptid
))
743 /* Call CALLBACK with its second argument set to DATA for every LWP in
744 the list. If CALLBACK returns 1 for a particular LWP, return a
745 pointer to the structure describing that LWP immediately.
746 Otherwise return NULL. */
749 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
751 struct lwp_info
*lp
, *lpnext
;
753 for (lp
= lwp_list
; lp
; lp
= lpnext
)
756 if ((*callback
) (lp
, data
))
763 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
764 a message telling the user that a new LWP has been added to the
768 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
770 struct lwp_info
*lp
, *found_lp
;
772 gdb_assert (is_lwp (ptid
));
774 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
775 to interrupt either the ptrace() or waitpid() calls below. */
776 if (!sigismember (&blocked_mask
, SIGCHLD
))
778 sigaddset (&blocked_mask
, SIGCHLD
);
779 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
783 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
785 found_lp
= lp
= find_lwp_pid (ptid
);
789 /* We assume that we're already attached to any LWP that has an id
790 equal to the overall process id, and to any LWP that is already
791 in our list of LWPs. If we're not seeing exit events from threads
792 and we've had PID wraparound since we last tried to stop all threads,
793 this assumption might be wrong; fortunately, this is very unlikely
795 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
800 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
801 error ("Can't attach %s: %s", target_pid_to_str (ptid
),
802 safe_strerror (errno
));
805 fprintf_unfiltered (gdb_stdlog
,
806 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
807 target_pid_to_str (ptid
));
809 pid
= waitpid (GET_LWP (ptid
), &status
, 0);
810 if (pid
== -1 && errno
== ECHILD
)
812 /* Try again with __WCLONE to check cloned processes. */
813 pid
= waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
817 gdb_assert (pid
== GET_LWP (ptid
)
818 && WIFSTOPPED (status
) && WSTOPSIG (status
));
820 child_post_attach (pid
);
826 fprintf_unfiltered (gdb_stdlog
,
827 "LLAL: waitpid %s received %s\n",
828 target_pid_to_str (ptid
),
829 status_to_str (status
));
834 /* We assume that the LWP representing the original process is
835 already stopped. Mark it as stopped in the data structure
836 that the linux ptrace layer uses to keep track of threads.
837 Note that this won't have already been done since the main
838 thread will have, we assume, been stopped by an attach from a
845 linux_nat_attach (char *args
, int from_tty
)
851 /* FIXME: We should probably accept a list of process id's, and
852 attach all of them. */
853 deprecated_child_ops
.to_attach (args
, from_tty
);
855 /* Add the initial process as the first LWP to the list. */
856 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
858 /* Make sure the initial process is stopped. The user-level threads
859 layer might want to poke around in the inferior, and that won't
860 work if things haven't stabilized yet. */
861 pid
= waitpid (GET_PID (inferior_ptid
), &status
, 0);
862 if (pid
== -1 && errno
== ECHILD
)
864 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid
));
866 /* Try again with __WCLONE to check cloned processes. */
867 pid
= waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
871 gdb_assert (pid
== GET_PID (inferior_ptid
)
872 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
876 /* Fake the SIGSTOP that core GDB expects. */
877 lp
->status
= W_STOPCODE (SIGSTOP
);
881 fprintf_unfiltered (gdb_stdlog
,
882 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
887 detach_callback (struct lwp_info
*lp
, void *data
)
889 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
891 if (debug_linux_nat
&& lp
->status
)
892 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
893 strsignal (WSTOPSIG (lp
->status
)),
894 target_pid_to_str (lp
->ptid
));
896 while (lp
->signalled
&& lp
->stopped
)
899 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
900 WSTOPSIG (lp
->status
)) < 0)
901 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
902 safe_strerror (errno
));
905 fprintf_unfiltered (gdb_stdlog
,
906 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
907 target_pid_to_str (lp
->ptid
),
908 status_to_str (lp
->status
));
913 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
914 here. But since lp->signalled was cleared above,
915 stop_wait_callback didn't do anything; the process was left
916 running. Shouldn't we be waiting for it to stop?
917 I've removed the call, since stop_wait_callback now does do
918 something when called with lp->signalled == 0. */
920 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
923 /* We don't actually detach from the LWP that has an id equal to the
924 overall process id just yet. */
925 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
928 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
929 WSTOPSIG (lp
->status
)) < 0)
930 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
931 safe_strerror (errno
));
934 fprintf_unfiltered (gdb_stdlog
,
935 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
936 target_pid_to_str (lp
->ptid
),
937 strsignal (WSTOPSIG (lp
->status
)));
939 delete_lwp (lp
->ptid
);
946 linux_nat_detach (char *args
, int from_tty
)
948 iterate_over_lwps (detach_callback
, NULL
);
950 /* Only the initial process should be left right now. */
951 gdb_assert (num_lwps
== 1);
953 trap_ptid
= null_ptid
;
955 /* Destroy LWP info; it's no longer valid. */
958 /* Restore the original signal mask. */
959 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
960 sigemptyset (&blocked_mask
);
962 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
963 deprecated_child_ops
.to_detach (args
, from_tty
);
969 resume_callback (struct lwp_info
*lp
, void *data
)
971 if (lp
->stopped
&& lp
->status
== 0)
973 struct thread_info
*tp
;
975 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
977 fprintf_unfiltered (gdb_stdlog
,
978 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
979 target_pid_to_str (lp
->ptid
));
988 resume_clear_callback (struct lwp_info
*lp
, void *data
)
995 resume_set_callback (struct lwp_info
*lp
, void *data
)
1002 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1004 struct lwp_info
*lp
;
1007 /* A specific PTID means `step only this process id'. */
1008 resume_all
= (PIDGET (ptid
) == -1);
1011 iterate_over_lwps (resume_set_callback
, NULL
);
1013 iterate_over_lwps (resume_clear_callback
, NULL
);
1015 /* If PID is -1, it's the current inferior that should be
1016 handled specially. */
1017 if (PIDGET (ptid
) == -1)
1018 ptid
= inferior_ptid
;
1020 lp
= find_lwp_pid (ptid
);
1023 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1025 /* Remember if we're stepping. */
1028 /* Mark this LWP as resumed. */
1031 /* If we have a pending wait status for this thread, there is no
1032 point in resuming the process. */
1035 /* FIXME: What should we do if we are supposed to continue
1036 this thread with a signal? */
1037 gdb_assert (signo
== TARGET_SIGNAL_0
);
1041 /* Mark LWP as not stopped to prevent it from being continued by
1047 iterate_over_lwps (resume_callback
, NULL
);
1049 child_resume (ptid
, step
, signo
);
1050 if (debug_linux_nat
)
1051 fprintf_unfiltered (gdb_stdlog
,
1052 "LLR: %s %s, %s (resume event thread)\n",
1053 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1054 target_pid_to_str (ptid
),
1055 signo
? strsignal (signo
) : "0");
1058 /* Issue kill to specified lwp. */
1060 static int tkill_failed
;
1063 kill_lwp (int lwpid
, int signo
)
1067 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1068 fails, then we are not using nptl threads and we should be using kill. */
1070 #ifdef HAVE_TKILL_SYSCALL
1073 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1074 if (errno
!= ENOSYS
)
1081 return kill (lwpid
, signo
);
1084 /* Handle a GNU/Linux extended wait response. Most of the work we
1085 just pass off to linux_handle_extended_wait, but if it reports a
1086 clone event we need to add the new LWP to our list (and not report
1087 the trap to higher layers). This function returns non-zero if
1088 the event should be ignored and we should wait again. */
1091 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1093 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1096 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1097 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1099 struct lwp_info
*new_lp
;
1100 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1101 GET_PID (inferior_ptid
)));
1103 new_lp
->stopped
= 1;
1105 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1107 if (debug_linux_nat
)
1108 fprintf_unfiltered (gdb_stdlog
,
1109 "LLHE: Got clone event from LWP %ld, resuming\n",
1110 GET_LWP (lp
->ptid
));
1111 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1119 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1123 wait_lwp (struct lwp_info
*lp
)
1127 int thread_dead
= 0;
1129 gdb_assert (!lp
->stopped
);
1130 gdb_assert (lp
->status
== 0);
1132 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1133 if (pid
== -1 && errno
== ECHILD
)
1135 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1136 if (pid
== -1 && errno
== ECHILD
)
1138 /* The thread has previously exited. We need to delete it
1139 now because, for some vendor 2.4 kernels with NPTL
1140 support backported, there won't be an exit event unless
1141 it is the main thread. 2.6 kernels will report an exit
1142 event for each thread that exits, as expected. */
1144 if (debug_linux_nat
)
1145 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1146 target_pid_to_str (lp
->ptid
));
1152 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1154 if (debug_linux_nat
)
1156 fprintf_unfiltered (gdb_stdlog
,
1157 "WL: waitpid %s received %s\n",
1158 target_pid_to_str (lp
->ptid
),
1159 status_to_str (status
));
1163 /* Check if the thread has exited. */
1164 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1167 if (debug_linux_nat
)
1168 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1169 target_pid_to_str (lp
->ptid
));
1174 if (in_thread_list (lp
->ptid
))
1176 /* Core GDB cannot deal with us deleting the current thread. */
1177 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1178 delete_thread (lp
->ptid
);
1179 printf_unfiltered ("[%s exited]\n",
1180 target_pid_to_str (lp
->ptid
));
1183 delete_lwp (lp
->ptid
);
1187 gdb_assert (WIFSTOPPED (status
));
1189 /* Handle GNU/Linux's extended waitstatus for trace events. */
1190 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1192 if (debug_linux_nat
)
1193 fprintf_unfiltered (gdb_stdlog
,
1194 "WL: Handling extended status 0x%06x\n",
1196 if (linux_nat_handle_extended (lp
, status
))
1197 return wait_lwp (lp
);
1203 /* Send a SIGSTOP to LP. */
1206 stop_callback (struct lwp_info
*lp
, void *data
)
1208 if (!lp
->stopped
&& !lp
->signalled
)
1212 if (debug_linux_nat
)
1214 fprintf_unfiltered (gdb_stdlog
,
1215 "SC: kill %s **<SIGSTOP>**\n",
1216 target_pid_to_str (lp
->ptid
));
1219 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1220 if (debug_linux_nat
)
1222 fprintf_unfiltered (gdb_stdlog
,
1223 "SC: lwp kill %d %s\n",
1225 errno
? safe_strerror (errno
) : "ERRNO-OK");
1229 gdb_assert (lp
->status
== 0);
1235 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1236 a pointer to a set of signals to be flushed immediately. */
1239 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1241 sigset_t
*flush_mask
= data
;
1247 status
= wait_lwp (lp
);
1251 /* Ignore any signals in FLUSH_MASK. */
1252 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1261 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1262 if (debug_linux_nat
)
1263 fprintf_unfiltered (gdb_stdlog
,
1264 "PTRACE_CONT %s, 0, 0 (%s)\n",
1265 target_pid_to_str (lp
->ptid
),
1266 errno
? safe_strerror (errno
) : "OK");
1268 return stop_wait_callback (lp
, flush_mask
);
1271 if (WSTOPSIG (status
) != SIGSTOP
)
1273 if (WSTOPSIG (status
) == SIGTRAP
)
1275 /* If a LWP other than the LWP that we're reporting an
1276 event for has hit a GDB breakpoint (as opposed to
1277 some random trap signal), then just arrange for it to
1278 hit it again later. We don't keep the SIGTRAP status
1279 and don't forward the SIGTRAP signal to the LWP. We
1280 will handle the current event, eventually we will
1281 resume all LWPs, and this one will get its breakpoint
1284 If we do not do this, then we run the risk that the
1285 user will delete or disable the breakpoint, but the
1286 thread will have already tripped on it. */
1288 /* Now resume this LWP and get the SIGSTOP event. */
1290 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1291 if (debug_linux_nat
)
1293 fprintf_unfiltered (gdb_stdlog
,
1294 "PTRACE_CONT %s, 0, 0 (%s)\n",
1295 target_pid_to_str (lp
->ptid
),
1296 errno
? safe_strerror (errno
) : "OK");
1298 fprintf_unfiltered (gdb_stdlog
,
1299 "SWC: Candidate SIGTRAP event in %s\n",
1300 target_pid_to_str (lp
->ptid
));
1302 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1303 stop_wait_callback (lp
, data
);
1304 /* If there's another event, throw it back into the queue. */
1307 if (debug_linux_nat
)
1309 fprintf_unfiltered (gdb_stdlog
,
1310 "SWC: kill %s, %s\n",
1311 target_pid_to_str (lp
->ptid
),
1312 status_to_str ((int) status
));
1314 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1316 /* Save the sigtrap event. */
1317 lp
->status
= status
;
1322 /* The thread was stopped with a signal other than
1323 SIGSTOP, and didn't accidentally trip a breakpoint. */
1325 if (debug_linux_nat
)
1327 fprintf_unfiltered (gdb_stdlog
,
1328 "SWC: Pending event %s in %s\n",
1329 status_to_str ((int) status
),
1330 target_pid_to_str (lp
->ptid
));
1332 /* Now resume this LWP and get the SIGSTOP event. */
1334 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1335 if (debug_linux_nat
)
1336 fprintf_unfiltered (gdb_stdlog
,
1337 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1338 target_pid_to_str (lp
->ptid
),
1339 errno
? safe_strerror (errno
) : "OK");
1341 /* Hold this event/waitstatus while we check to see if
1342 there are any more (we still want to get that SIGSTOP). */
1343 stop_wait_callback (lp
, data
);
1344 /* If the lp->status field is still empty, use it to hold
1345 this event. If not, then this event must be returned
1346 to the event queue of the LWP. */
1347 if (lp
->status
== 0)
1348 lp
->status
= status
;
1351 if (debug_linux_nat
)
1353 fprintf_unfiltered (gdb_stdlog
,
1354 "SWC: kill %s, %s\n",
1355 target_pid_to_str (lp
->ptid
),
1356 status_to_str ((int) status
));
1358 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1365 /* We caught the SIGSTOP that we intended to catch, so
1366 there's no SIGSTOP pending. */
1375 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1376 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1379 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1381 sigset_t blocked
, ignored
;
1384 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1389 for (i
= 1; i
< NSIG
; i
++)
1390 if (sigismember (pending
, i
))
1391 if (!sigismember (flush_mask
, i
)
1392 || sigismember (&blocked
, i
)
1393 || sigismember (&ignored
, i
))
1394 sigdelset (pending
, i
);
1396 if (sigisemptyset (pending
))
1402 /* DATA is interpreted as a mask of signals to flush. If LP has
1403 signals pending, and they are all in the flush mask, then arrange
1404 to flush them. LP should be stopped, as should all other threads
1405 it might share a signal queue with. */
1408 flush_callback (struct lwp_info
*lp
, void *data
)
1410 sigset_t
*flush_mask
= data
;
1411 sigset_t pending
, intersection
, blocked
, ignored
;
1414 /* Normally, when an LWP exits, it is removed from the LWP list. The
1415 last LWP isn't removed till later, however. So if there is only
1416 one LWP on the list, make sure it's alive. */
1417 if (lwp_list
== lp
&& lp
->next
== NULL
)
1418 if (!linux_nat_thread_alive (lp
->ptid
))
1421 /* Just because the LWP is stopped doesn't mean that new signals
1422 can't arrive from outside, so this function must be careful of
1423 race conditions. However, because all threads are stopped, we
1424 can assume that the pending mask will not shrink unless we resume
1425 the LWP, and that it will then get another signal. We can't
1426 control which one, however. */
1430 if (debug_linux_nat
)
1431 printf_unfiltered ("FC: LP has pending status %06x\n", lp
->status
);
1432 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1436 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1441 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1442 if (debug_linux_nat
)
1443 fprintf_unfiltered (gdb_stderr
,
1444 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1447 stop_wait_callback (lp
, flush_mask
);
1448 if (debug_linux_nat
)
1449 fprintf_unfiltered (gdb_stderr
,
1450 "FC: Wait finished; saved status is %d\n",
1457 /* Return non-zero if LP has a wait status pending. */
1460 status_callback (struct lwp_info
*lp
, void *data
)
1462 /* Only report a pending wait status if we pretend that this has
1463 indeed been resumed. */
1464 return (lp
->status
!= 0 && lp
->resumed
);
1467 /* Return non-zero if LP isn't stopped. */
1470 running_callback (struct lwp_info
*lp
, void *data
)
1472 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1475 /* Count the LWP's that have had events. */
1478 count_events_callback (struct lwp_info
*lp
, void *data
)
1482 gdb_assert (count
!= NULL
);
1484 /* Count only LWPs that have a SIGTRAP event pending. */
1486 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1492 /* Select the LWP (if any) that is currently being single-stepped. */
1495 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1497 if (lp
->step
&& lp
->status
!= 0)
1503 /* Select the Nth LWP that has had a SIGTRAP event. */
1506 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1508 int *selector
= data
;
1510 gdb_assert (selector
!= NULL
);
1512 /* Select only LWPs that have a SIGTRAP event pending. */
1514 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1515 if ((*selector
)-- == 0)
1522 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1524 struct lwp_info
*event_lp
= data
;
1526 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1530 /* If a LWP other than the LWP that we're reporting an event for has
1531 hit a GDB breakpoint (as opposed to some random trap signal),
1532 then just arrange for it to hit it again later. We don't keep
1533 the SIGTRAP status and don't forward the SIGTRAP signal to the
1534 LWP. We will handle the current event, eventually we will resume
1535 all LWPs, and this one will get its breakpoint trap again.
1537 If we do not do this, then we run the risk that the user will
1538 delete or disable the breakpoint, but the LWP will have already
1542 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1543 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1544 DECR_PC_AFTER_BREAK
))
1546 if (debug_linux_nat
)
1547 fprintf_unfiltered (gdb_stdlog
,
1548 "CBC: Push back breakpoint for %s\n",
1549 target_pid_to_str (lp
->ptid
));
1551 /* Back up the PC if necessary. */
1552 if (DECR_PC_AFTER_BREAK
)
1553 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1555 /* Throw away the SIGTRAP. */
1562 /* Select one LWP out of those that have events pending. */
1565 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1568 int random_selector
;
1569 struct lwp_info
*event_lp
;
1571 /* Record the wait status for the origional LWP. */
1572 (*orig_lp
)->status
= *status
;
1574 /* Give preference to any LWP that is being single-stepped. */
1575 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1576 if (event_lp
!= NULL
)
1578 if (debug_linux_nat
)
1579 fprintf_unfiltered (gdb_stdlog
,
1580 "SEL: Select single-step %s\n",
1581 target_pid_to_str (event_lp
->ptid
));
1585 /* No single-stepping LWP. Select one at random, out of those
1586 which have had SIGTRAP events. */
1588 /* First see how many SIGTRAP events we have. */
1589 iterate_over_lwps (count_events_callback
, &num_events
);
1591 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1592 random_selector
= (int)
1593 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1595 if (debug_linux_nat
&& num_events
> 1)
1596 fprintf_unfiltered (gdb_stdlog
,
1597 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1598 num_events
, random_selector
);
1600 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1604 if (event_lp
!= NULL
)
1606 /* Switch the event LWP. */
1607 *orig_lp
= event_lp
;
1608 *status
= event_lp
->status
;
1611 /* Flush the wait status for the event LWP. */
1612 (*orig_lp
)->status
= 0;
1615 /* Return non-zero if LP has been resumed. */
1618 resumed_callback (struct lwp_info
*lp
, void *data
)
1625 /* We need to override child_wait to support attaching to cloned
1626 processes, since a normal wait (as done by the default version)
1627 ignores those processes. */
1629 /* Wait for child PTID to do something. Return id of the child,
1630 minus_one_ptid in case of error; store status into *OURSTATUS. */
1633 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1639 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1643 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1644 attached process. */
1647 pid
= waitpid (GET_PID (ptid
), &status
, 0);
1648 if (pid
== -1 && errno
== ECHILD
)
1649 /* Try again with __WCLONE to check cloned processes. */
1650 pid
= waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1652 if (debug_linux_nat
)
1654 fprintf_unfiltered (gdb_stdlog
,
1655 "CW: waitpid %ld received %s\n",
1656 (long) pid
, status_to_str (status
));
1661 /* Make sure we don't report an event for the exit of the
1662 original program, if we've detached from it. */
1663 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1669 /* Check for stop events reported by a process we didn't already
1670 know about - in this case, anything other than inferior_ptid.
1672 If we're expecting to receive stopped processes after fork,
1673 vfork, and clone events, then we'll just add the new one to
1674 our list and go back to waiting for the event to be reported
1675 - the stopped process might be returned from waitpid before
1676 or after the event is. If we want to handle debugging of
1677 CLONE_PTRACE processes we need to do more here, i.e. switch
1678 to multi-threaded mode. */
1679 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1680 && pid
!= GET_PID (inferior_ptid
))
1682 linux_record_stopped_pid (pid
);
1687 /* Handle GNU/Linux's extended waitstatus for trace events. */
1688 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1689 && status
>> 16 != 0)
1691 linux_handle_extended_wait (pid
, status
, ourstatus
);
1693 /* If we see a clone event, detach the child, and don't
1694 report the event. It would be nice to offer some way to
1695 switch into a non-thread-db based threaded mode at this
1697 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1699 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1700 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1701 ptrace (PTRACE_CONT
, pid
, 0, 0);
1707 clear_sigio_trap ();
1708 clear_sigint_trap ();
1710 while (pid
== -1 && save_errno
== EINTR
);
1714 warning ("Child process unexpectedly missing: %s",
1715 safe_strerror (errno
));
1717 /* Claim it exited with unknown signal. */
1718 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1719 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1720 return minus_one_ptid
;
1723 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1724 store_waitstatus (ourstatus
, status
);
1726 return pid_to_ptid (pid
);
1731 /* Stop an active thread, verify it still exists, then resume it. */
1734 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1736 struct lwp_info
*ptr
;
1738 if (!lp
->stopped
&& !lp
->signalled
)
1740 stop_callback (lp
, NULL
);
1741 stop_wait_callback (lp
, NULL
);
1742 /* Resume if the lwp still exists. */
1743 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1746 resume_callback (lp
, NULL
);
1747 resume_set_callback (lp
, NULL
);
1754 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1756 struct lwp_info
*lp
= NULL
;
1759 pid_t pid
= PIDGET (ptid
);
1760 sigset_t flush_mask
;
1762 sigemptyset (&flush_mask
);
1764 /* Make sure SIGCHLD is blocked. */
1765 if (!sigismember (&blocked_mask
, SIGCHLD
))
1767 sigaddset (&blocked_mask
, SIGCHLD
);
1768 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1773 /* Make sure there is at least one LWP that has been resumed, at
1774 least if there are any LWPs at all. */
1775 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1777 /* First check if there is a LWP with a wait status pending. */
1780 /* Any LWP that's been resumed will do. */
1781 lp
= iterate_over_lwps (status_callback
, NULL
);
1784 status
= lp
->status
;
1787 if (debug_linux_nat
&& status
)
1788 fprintf_unfiltered (gdb_stdlog
,
1789 "LLW: Using pending wait status %s for %s.\n",
1790 status_to_str (status
),
1791 target_pid_to_str (lp
->ptid
));
1794 /* But if we don't fine one, we'll have to wait, and check both
1795 cloned and uncloned processes. We start with the cloned
1797 options
= __WCLONE
| WNOHANG
;
1799 else if (is_lwp (ptid
))
1801 if (debug_linux_nat
)
1802 fprintf_unfiltered (gdb_stdlog
,
1803 "LLW: Waiting for specific LWP %s.\n",
1804 target_pid_to_str (ptid
));
1806 /* We have a specific LWP to check. */
1807 lp
= find_lwp_pid (ptid
);
1809 status
= lp
->status
;
1812 if (debug_linux_nat
&& status
)
1813 fprintf_unfiltered (gdb_stdlog
,
1814 "LLW: Using pending wait status %s for %s.\n",
1815 status_to_str (status
),
1816 target_pid_to_str (lp
->ptid
));
1818 /* If we have to wait, take into account whether PID is a cloned
1819 process or not. And we have to convert it to something that
1820 the layer beneath us can understand. */
1821 options
= lp
->cloned
? __WCLONE
: 0;
1822 pid
= GET_LWP (ptid
);
1825 if (status
&& lp
->signalled
)
1827 /* A pending SIGSTOP may interfere with the normal stream of
1828 events. In a typical case where interference is a problem,
1829 we have a SIGSTOP signal pending for LWP A while
1830 single-stepping it, encounter an event in LWP B, and take the
1831 pending SIGSTOP while trying to stop LWP A. After processing
1832 the event in LWP B, LWP A is continued, and we'll never see
1833 the SIGTRAP associated with the last time we were
1834 single-stepping LWP A. */
1836 /* Resume the thread. It should halt immediately returning the
1838 registers_changed ();
1839 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1841 if (debug_linux_nat
)
1842 fprintf_unfiltered (gdb_stdlog
,
1843 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1844 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1845 target_pid_to_str (lp
->ptid
));
1847 gdb_assert (lp
->resumed
);
1849 /* This should catch the pending SIGSTOP. */
1850 stop_wait_callback (lp
, NULL
);
1853 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1854 attached process. */
1861 lwpid
= waitpid (pid
, &status
, options
);
1864 gdb_assert (pid
== -1 || lwpid
== pid
);
1866 if (debug_linux_nat
)
1868 fprintf_unfiltered (gdb_stdlog
,
1869 "LLW: waitpid %ld received %s\n",
1870 (long) lwpid
, status_to_str (status
));
1873 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1875 /* Check for stop events reported by a process we didn't
1876 already know about - anything not already in our LWP
1879 If we're expecting to receive stopped processes after
1880 fork, vfork, and clone events, then we'll just add the
1881 new one to our list and go back to waiting for the event
1882 to be reported - the stopped process might be returned
1883 from waitpid before or after the event is. */
1884 if (WIFSTOPPED (status
) && !lp
)
1886 linux_record_stopped_pid (lwpid
);
1891 /* Make sure we don't report an event for the exit of an LWP not in
1892 our list, i.e. not part of the current process. This can happen
1893 if we detach from a program we original forked and then it
1895 if (!WIFSTOPPED (status
) && !lp
)
1901 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1902 CLONE_PTRACE processes which do not use the thread library -
1903 otherwise we wouldn't find the new LWP this way. That doesn't
1904 currently work, and the following code is currently unreachable
1905 due to the two blocks above. If it's fixed some day, this code
1906 should be broken out into a function so that we can also pick up
1907 LWPs from the new interface. */
1910 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1911 if (options
& __WCLONE
)
1916 gdb_assert (WIFSTOPPED (status
)
1917 && WSTOPSIG (status
) == SIGSTOP
);
1920 if (!in_thread_list (inferior_ptid
))
1922 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1923 GET_PID (inferior_ptid
));
1924 add_thread (inferior_ptid
);
1927 add_thread (lp
->ptid
);
1928 printf_unfiltered ("[New %s]\n",
1929 target_pid_to_str (lp
->ptid
));
1933 /* Handle GNU/Linux's extended waitstatus for trace events. */
1934 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1936 if (debug_linux_nat
)
1937 fprintf_unfiltered (gdb_stdlog
,
1938 "LLW: Handling extended status 0x%06x\n",
1940 if (linux_nat_handle_extended (lp
, status
))
1947 /* Check if the thread has exited. */
1948 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1950 if (in_thread_list (lp
->ptid
))
1952 /* Core GDB cannot deal with us deleting the current
1954 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1955 delete_thread (lp
->ptid
);
1956 printf_unfiltered ("[%s exited]\n",
1957 target_pid_to_str (lp
->ptid
));
1960 /* If this is the main thread, we must stop all threads and
1961 verify if they are still alive. This is because in the nptl
1962 thread model, there is no signal issued for exiting LWPs
1963 other than the main thread. We only get the main thread
1964 exit signal once all child threads have already exited.
1965 If we stop all the threads and use the stop_wait_callback
1966 to check if they have exited we can determine whether this
1967 signal should be ignored or whether it means the end of the
1968 debugged application, regardless of which threading model
1970 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
1973 iterate_over_lwps (stop_and_resume_callback
, NULL
);
1976 if (debug_linux_nat
)
1977 fprintf_unfiltered (gdb_stdlog
,
1978 "LLW: %s exited.\n",
1979 target_pid_to_str (lp
->ptid
));
1981 delete_lwp (lp
->ptid
);
1983 /* If there is at least one more LWP, then the exit signal
1984 was not the end of the debugged application and should be
1988 /* Make sure there is at least one thread running. */
1989 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
1991 /* Discard the event. */
1997 /* Check if the current LWP has previously exited. In the nptl
1998 thread model, LWPs other than the main thread do not issue
1999 signals when they exit so we must check whenever the thread
2000 has stopped. A similar check is made in stop_wait_callback(). */
2001 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2003 if (in_thread_list (lp
->ptid
))
2005 /* Core GDB cannot deal with us deleting the current
2007 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2008 delete_thread (lp
->ptid
);
2009 printf_unfiltered ("[%s exited]\n",
2010 target_pid_to_str (lp
->ptid
));
2012 if (debug_linux_nat
)
2013 fprintf_unfiltered (gdb_stdlog
,
2014 "LLW: %s exited.\n",
2015 target_pid_to_str (lp
->ptid
));
2017 delete_lwp (lp
->ptid
);
2019 /* Make sure there is at least one thread running. */
2020 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2022 /* Discard the event. */
2027 /* Make sure we don't report a SIGSTOP that we sent
2028 ourselves in an attempt to stop an LWP. */
2030 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2032 if (debug_linux_nat
)
2033 fprintf_unfiltered (gdb_stdlog
,
2034 "LLW: Delayed SIGSTOP caught for %s.\n",
2035 target_pid_to_str (lp
->ptid
));
2037 /* This is a delayed SIGSTOP. */
2040 registers_changed ();
2041 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2043 if (debug_linux_nat
)
2044 fprintf_unfiltered (gdb_stdlog
,
2045 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2047 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2048 target_pid_to_str (lp
->ptid
));
2051 gdb_assert (lp
->resumed
);
2053 /* Discard the event. */
2063 /* Alternate between checking cloned and uncloned processes. */
2064 options
^= __WCLONE
;
2066 /* And suspend every time we have checked both. */
2067 if (options
& __WCLONE
)
2068 sigsuspend (&suspend_mask
);
2071 /* We shouldn't end up here unless we want to try again. */
2072 gdb_assert (status
== 0);
2075 clear_sigio_trap ();
2076 clear_sigint_trap ();
2080 /* Don't report signals that GDB isn't interested in, such as
2081 signals that are neither printed nor stopped upon. Stopping all
2082 threads can be a bit time-consuming so if we want decent
2083 performance with heavily multi-threaded programs, especially when
2084 they're using a high frequency timer, we'd better avoid it if we
2087 if (WIFSTOPPED (status
))
2089 int signo
= target_signal_from_host (WSTOPSIG (status
));
2091 if (signal_stop_state (signo
) == 0
2092 && signal_print_state (signo
) == 0
2093 && signal_pass_state (signo
) == 1)
2095 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2096 here? It is not clear we should. GDB may not expect
2097 other threads to run. On the other hand, not resuming
2098 newly attached threads may cause an unwanted delay in
2099 getting them running. */
2100 registers_changed ();
2101 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2102 if (debug_linux_nat
)
2103 fprintf_unfiltered (gdb_stdlog
,
2104 "LLW: %s %s, %s (preempt 'handle')\n",
2106 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2107 target_pid_to_str (lp
->ptid
),
2108 signo
? strsignal (signo
) : "0");
2114 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2116 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2117 forwarded to the entire process group, that is, all LWP's
2118 will receive it. Since we only want to report it once,
2119 we try to flush it from all LWPs except this one. */
2120 sigaddset (&flush_mask
, SIGINT
);
2124 /* This LWP is stopped now. */
2127 if (debug_linux_nat
)
2128 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2129 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2131 /* Now stop all other LWP's ... */
2132 iterate_over_lwps (stop_callback
, NULL
);
2134 /* ... and wait until all of them have reported back that they're no
2136 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2137 iterate_over_lwps (flush_callback
, &flush_mask
);
2139 /* If we're not waiting for a specific LWP, choose an event LWP from
2140 among those that have had events. Giving equal priority to all
2141 LWPs that have had events helps prevent starvation. */
2143 select_event_lwp (&lp
, &status
);
2145 /* Now that we've selected our final event LWP, cancel any
2146 breakpoints in other LWPs that have hit a GDB breakpoint. See
2147 the comment in cancel_breakpoints_callback to find out why. */
2148 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2150 /* If we're not running in "threaded" mode, we'll report the bare
2153 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2155 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2156 if (debug_linux_nat
)
2157 fprintf_unfiltered (gdb_stdlog
,
2158 "LLW: trap_ptid is %s.\n",
2159 target_pid_to_str (trap_ptid
));
2162 trap_ptid
= null_ptid
;
2164 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2166 *ourstatus
= lp
->waitstatus
;
2167 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2170 store_waitstatus (ourstatus
, status
);
2172 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2176 kill_callback (struct lwp_info
*lp
, void *data
)
2179 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2180 if (debug_linux_nat
)
2181 fprintf_unfiltered (gdb_stdlog
,
2182 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2183 target_pid_to_str (lp
->ptid
),
2184 errno
? safe_strerror (errno
) : "OK");
2190 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2194 /* We must make sure that there are no pending events (delayed
2195 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2196 program doesn't interfere with any following debugging session. */
2198 /* For cloned processes we must check both with __WCLONE and
2199 without, since the exit status of a cloned process isn't reported
2205 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2206 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2208 fprintf_unfiltered (gdb_stdlog
,
2209 "KWC: wait %s received unknown.\n",
2210 target_pid_to_str (lp
->ptid
));
2213 while (pid
== GET_LWP (lp
->ptid
));
2215 gdb_assert (pid
== -1 && errno
== ECHILD
);
2220 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2221 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2223 fprintf_unfiltered (gdb_stdlog
,
2224 "KWC: wait %s received unk.\n",
2225 target_pid_to_str (lp
->ptid
));
2228 while (pid
== GET_LWP (lp
->ptid
));
2230 gdb_assert (pid
== -1 && errno
== ECHILD
);
2235 linux_nat_kill (void)
2237 /* Kill all LWP's ... */
2238 iterate_over_lwps (kill_callback
, NULL
);
2240 /* ... and wait until we've flushed all events. */
2241 iterate_over_lwps (kill_wait_callback
, NULL
);
2243 target_mourn_inferior ();
2247 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2250 deprecated_child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2254 linux_nat_mourn_inferior (void)
2256 trap_ptid
= null_ptid
;
2258 /* Destroy LWP info; it's no longer valid. */
2261 /* Restore the original signal mask. */
2262 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2263 sigemptyset (&blocked_mask
);
2265 deprecated_child_ops
.to_mourn_inferior ();
2269 linux_nat_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2270 struct mem_attrib
*attrib
, struct target_ops
*target
)
2272 struct cleanup
*old_chain
= save_inferior_ptid ();
2275 if (is_lwp (inferior_ptid
))
2276 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2278 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2280 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2282 do_cleanups (old_chain
);
2287 linux_nat_thread_alive (ptid_t ptid
)
2289 gdb_assert (is_lwp (ptid
));
2292 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2293 if (debug_linux_nat
)
2294 fprintf_unfiltered (gdb_stdlog
,
2295 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2296 target_pid_to_str (ptid
),
2297 errno
? safe_strerror (errno
) : "OK");
2305 linux_nat_pid_to_str (ptid_t ptid
)
2307 static char buf
[64];
2311 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2315 return normal_pid_to_str (ptid
);
2319 init_linux_nat_ops (void)
2322 linux_nat_ops
.to_open
= linux_nat_open
;
2324 linux_nat_ops
.to_shortname
= "lwp-layer";
2325 linux_nat_ops
.to_longname
= "lwp-layer";
2326 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2327 linux_nat_ops
.to_attach
= linux_nat_attach
;
2328 linux_nat_ops
.to_detach
= linux_nat_detach
;
2329 linux_nat_ops
.to_resume
= linux_nat_resume
;
2330 linux_nat_ops
.to_wait
= linux_nat_wait
;
2331 /* fetch_inferior_registers and store_inferior_registers will
2332 honor the LWP id, so we can use them directly. */
2333 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2334 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2335 linux_nat_ops
.deprecated_xfer_memory
= linux_nat_xfer_memory
;
2336 linux_nat_ops
.to_kill
= linux_nat_kill
;
2337 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2338 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2339 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2340 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2341 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2342 linux_nat_ops
.to_post_attach
= child_post_attach
;
2343 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2344 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2345 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2347 linux_nat_ops
.to_stratum
= thread_stratum
;
2348 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2349 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2353 sigchld_handler (int signo
)
2355 /* Do nothing. The only reason for this handler is that it allows
2356 us to use sigsuspend in linux_nat_wait above to wait for the
2357 arrival of a SIGCHLD. */
2360 /* Accepts an integer PID; Returns a string representing a file that
2361 can be opened to get the symbols for the child process. */
2364 child_pid_to_exec_file (int pid
)
2366 char *name1
, *name2
;
2368 name1
= xmalloc (MAXPATHLEN
);
2369 name2
= xmalloc (MAXPATHLEN
);
2370 make_cleanup (xfree
, name1
);
2371 make_cleanup (xfree
, name2
);
2372 memset (name2
, 0, MAXPATHLEN
);
2374 sprintf (name1
, "/proc/%d/exe", pid
);
2375 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2381 /* Service function for corefiles and info proc. */
2384 read_mapping (FILE *mapfile
,
2389 char *device
, long long *inode
, char *filename
)
2391 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2392 addr
, endaddr
, permissions
, offset
, device
, inode
);
2394 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
2396 /* Eat everything up to EOL for the filename. This will prevent
2397 weird filenames (such as one with embedded whitespace) from
2398 confusing this code. It also makes this code more robust in
2399 respect to annotations the kernel may add after the filename.
2401 Note the filename is used for informational purposes
2403 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2407 filename
[0] = '\0'; /* no filename */
2408 fscanf (mapfile
, "\n");
2410 return (ret
!= 0 && ret
!= EOF
);
2413 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2414 regions in the inferior for a corefile. */
2417 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2419 int, int, int, void *), void *obfd
)
2421 long long pid
= PIDGET (inferior_ptid
);
2422 char mapsfilename
[MAXPATHLEN
];
2424 long long addr
, endaddr
, size
, offset
, inode
;
2425 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2426 int read
, write
, exec
;
2429 /* Compose the filename for the /proc memory map, and open it. */
2430 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2431 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2432 error ("Could not open %s\n", mapsfilename
);
2435 fprintf_filtered (gdb_stdout
,
2436 "Reading memory regions from %s\n", mapsfilename
);
2438 /* Now iterate until end-of-file. */
2439 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2440 &offset
, &device
[0], &inode
, &filename
[0]))
2442 size
= endaddr
- addr
;
2444 /* Get the segment's permissions. */
2445 read
= (strchr (permissions
, 'r') != 0);
2446 write
= (strchr (permissions
, 'w') != 0);
2447 exec
= (strchr (permissions
, 'x') != 0);
2451 fprintf_filtered (gdb_stdout
,
2452 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2453 size
, paddr_nz (addr
),
2455 write
? 'w' : ' ', exec
? 'x' : ' ');
2456 if (filename
&& filename
[0])
2457 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2458 fprintf_filtered (gdb_stdout
, "\n");
2461 /* Invoke the callback function to create the corefile
2463 func (addr
, size
, read
, write
, exec
, obfd
);
2469 /* Records the thread's register state for the corefile note
2473 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2474 char *note_data
, int *note_size
)
2476 gdb_gregset_t gregs
;
2477 gdb_fpregset_t fpregs
;
2478 #ifdef FILL_FPXREGSET
2479 gdb_fpxregset_t fpxregs
;
2481 unsigned long lwp
= ptid_get_lwp (ptid
);
2483 fill_gregset (&gregs
, -1);
2484 note_data
= (char *) elfcore_write_prstatus (obfd
,
2488 stop_signal
, &gregs
);
2490 fill_fpregset (&fpregs
, -1);
2491 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2494 &fpregs
, sizeof (fpregs
));
2495 #ifdef FILL_FPXREGSET
2496 fill_fpxregset (&fpxregs
, -1);
2497 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2500 &fpxregs
, sizeof (fpxregs
));
2505 struct linux_nat_corefile_thread_data
2513 /* Called by gdbthread.c once per thread. Records the thread's
2514 register state for the corefile note section. */
2517 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2519 struct linux_nat_corefile_thread_data
*args
= data
;
2520 ptid_t saved_ptid
= inferior_ptid
;
2522 inferior_ptid
= ti
->ptid
;
2523 registers_changed ();
2524 target_fetch_registers (-1); /* FIXME should not be necessary;
2525 fill_gregset should do it automatically. */
2526 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2531 inferior_ptid
= saved_ptid
;
2532 registers_changed ();
2533 target_fetch_registers (-1); /* FIXME should not be necessary;
2534 fill_gregset should do it automatically. */
2538 /* Records the register state for the corefile note section. */
2541 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2542 char *note_data
, int *note_size
)
2544 registers_changed ();
2545 target_fetch_registers (-1); /* FIXME should not be necessary;
2546 fill_gregset should do it automatically. */
2547 return linux_nat_do_thread_registers (obfd
,
2548 ptid_build (ptid_get_pid (inferior_ptid
),
2549 ptid_get_pid (inferior_ptid
),
2551 note_data
, note_size
);
2555 /* Fills the "to_make_corefile_note" target vector. Builds the note
2556 section for a corefile, and returns it in a malloc buffer. */
2559 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2561 struct linux_nat_corefile_thread_data thread_args
;
2562 struct cleanup
*old_chain
;
2563 char fname
[16] = { '\0' };
2564 char psargs
[80] = { '\0' };
2565 char *note_data
= NULL
;
2566 ptid_t current_ptid
= inferior_ptid
;
2570 if (get_exec_file (0))
2572 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2573 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2574 if (get_inferior_args ())
2576 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2577 strncat (psargs
, get_inferior_args (),
2578 sizeof (psargs
) - strlen (psargs
));
2580 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2582 note_size
, fname
, psargs
);
2585 /* Dump information for threads. */
2586 thread_args
.obfd
= obfd
;
2587 thread_args
.note_data
= note_data
;
2588 thread_args
.note_size
= note_size
;
2589 thread_args
.num_notes
= 0;
2590 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2591 if (thread_args
.num_notes
== 0)
2593 /* iterate_over_threads didn't come up with any threads; just
2594 use inferior_ptid. */
2595 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2596 note_data
, note_size
);
2600 note_data
= thread_args
.note_data
;
2603 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2606 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2607 "CORE", NT_AUXV
, auxv
, auxv_len
);
2611 make_cleanup (xfree
, note_data
);
2615 /* Implement the "info proc" command. */
2618 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2620 long long pid
= PIDGET (inferior_ptid
);
2623 char buffer
[MAXPATHLEN
];
2624 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2637 /* Break up 'args' into an argv array. */
2638 if ((argv
= buildargv (args
)) == NULL
)
2641 make_cleanup_freeargv (argv
);
2643 while (argv
!= NULL
&& *argv
!= NULL
)
2645 if (isdigit (argv
[0][0]))
2647 pid
= strtoul (argv
[0], NULL
, 10);
2649 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2653 else if (strcmp (argv
[0], "status") == 0)
2657 else if (strcmp (argv
[0], "stat") == 0)
2661 else if (strcmp (argv
[0], "cmd") == 0)
2665 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2669 else if (strcmp (argv
[0], "cwd") == 0)
2673 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2679 /* [...] (future options here) */
2684 error ("No current process: you must name one.");
2686 sprintf (fname1
, "/proc/%lld", pid
);
2687 if (stat (fname1
, &dummy
) != 0)
2688 error ("No /proc directory: '%s'", fname1
);
2690 printf_filtered ("process %lld\n", pid
);
2691 if (cmdline_f
|| all
)
2693 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2694 if ((procfile
= fopen (fname1
, "r")) > 0)
2696 fgets (buffer
, sizeof (buffer
), procfile
);
2697 printf_filtered ("cmdline = '%s'\n", buffer
);
2701 warning ("unable to open /proc file '%s'", fname1
);
2705 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2706 memset (fname2
, 0, sizeof (fname2
));
2707 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2708 printf_filtered ("cwd = '%s'\n", fname2
);
2710 warning ("unable to read link '%s'", fname1
);
2714 sprintf (fname1
, "/proc/%lld/exe", pid
);
2715 memset (fname2
, 0, sizeof (fname2
));
2716 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2717 printf_filtered ("exe = '%s'\n", fname2
);
2719 warning ("unable to read link '%s'", fname1
);
2721 if (mappings_f
|| all
)
2723 sprintf (fname1
, "/proc/%lld/maps", pid
);
2724 if ((procfile
= fopen (fname1
, "r")) > 0)
2726 long long addr
, endaddr
, size
, offset
, inode
;
2727 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2729 printf_filtered ("Mapped address spaces:\n\n");
2730 if (TARGET_ADDR_BIT
== 32)
2732 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2735 " Size", " Offset", "objfile");
2739 printf_filtered (" %18s %18s %10s %10s %7s\n",
2742 " Size", " Offset", "objfile");
2745 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2746 &offset
, &device
[0], &inode
, &filename
[0]))
2748 size
= endaddr
- addr
;
2750 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2751 calls here (and possibly above) should be abstracted
2752 out into their own functions? Andrew suggests using
2753 a generic local_address_string instead to print out
2754 the addresses; that makes sense to me, too. */
2756 if (TARGET_ADDR_BIT
== 32)
2758 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2759 (unsigned long) addr
, /* FIXME: pr_addr */
2760 (unsigned long) endaddr
,
2762 (unsigned int) offset
,
2763 filename
[0] ? filename
: "");
2767 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2768 (unsigned long) addr
, /* FIXME: pr_addr */
2769 (unsigned long) endaddr
,
2771 (unsigned int) offset
,
2772 filename
[0] ? filename
: "");
2779 warning ("unable to open /proc file '%s'", fname1
);
2781 if (status_f
|| all
)
2783 sprintf (fname1
, "/proc/%lld/status", pid
);
2784 if ((procfile
= fopen (fname1
, "r")) > 0)
2786 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2787 puts_filtered (buffer
);
2791 warning ("unable to open /proc file '%s'", fname1
);
2795 sprintf (fname1
, "/proc/%lld/stat", pid
);
2796 if ((procfile
= fopen (fname1
, "r")) > 0)
2801 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2802 printf_filtered ("Process: %d\n", itmp
);
2803 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2804 printf_filtered ("Exec file: %s\n", buffer
);
2805 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2806 printf_filtered ("State: %c\n", ctmp
);
2807 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2808 printf_filtered ("Parent process: %d\n", itmp
);
2809 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2810 printf_filtered ("Process group: %d\n", itmp
);
2811 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2812 printf_filtered ("Session id: %d\n", itmp
);
2813 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2814 printf_filtered ("TTY: %d\n", itmp
);
2815 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2816 printf_filtered ("TTY owner process group: %d\n", itmp
);
2817 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2818 printf_filtered ("Flags: 0x%x\n", itmp
);
2819 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2820 printf_filtered ("Minor faults (no memory page): %u\n",
2821 (unsigned int) itmp
);
2822 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2823 printf_filtered ("Minor faults, children: %u\n",
2824 (unsigned int) itmp
);
2825 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2826 printf_filtered ("Major faults (memory page faults): %u\n",
2827 (unsigned int) itmp
);
2828 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2829 printf_filtered ("Major faults, children: %u\n",
2830 (unsigned int) itmp
);
2831 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2832 printf_filtered ("utime: %d\n", itmp
);
2833 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2834 printf_filtered ("stime: %d\n", itmp
);
2835 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2836 printf_filtered ("utime, children: %d\n", itmp
);
2837 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2838 printf_filtered ("stime, children: %d\n", itmp
);
2839 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2840 printf_filtered ("jiffies remaining in current time slice: %d\n",
2842 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2843 printf_filtered ("'nice' value: %d\n", itmp
);
2844 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2845 printf_filtered ("jiffies until next timeout: %u\n",
2846 (unsigned int) itmp
);
2847 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2848 printf_filtered ("jiffies until next SIGALRM: %u\n",
2849 (unsigned int) itmp
);
2850 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2851 printf_filtered ("start time (jiffies since system boot): %d\n",
2853 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2854 printf_filtered ("Virtual memory size: %u\n",
2855 (unsigned int) itmp
);
2856 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2857 printf_filtered ("Resident set size: %u\n", (unsigned int) itmp
);
2858 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2859 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2860 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2861 printf_filtered ("Start of text: 0x%x\n", itmp
);
2862 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2863 printf_filtered ("End of text: 0x%x\n", itmp
);
2864 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2865 printf_filtered ("Start of stack: 0x%x\n", itmp
);
2866 #if 0 /* Don't know how architecture-dependent the rest is...
2867 Anyway the signal bitmap info is available from "status". */
2868 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2869 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp
);
2870 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2871 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp
);
2872 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2873 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp
);
2874 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2875 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp
);
2876 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2877 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp
);
2878 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2879 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp
);
2880 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2881 printf_filtered ("wchan (system call): 0x%x\n", itmp
);
2886 warning ("unable to open /proc file '%s'", fname1
);
2891 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
2892 struct mem_attrib
*attrib
, struct target_ops
*target
)
2900 /* Don't bother for one word. */
2901 if (len
< 3 * sizeof (long))
2904 /* We could keep this file open and cache it - possibly one per
2905 thread. That requires some juggling, but is even faster. */
2906 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2907 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2911 /* If pread64 is available, use it. It's faster if the kernel
2912 supports it (only one syscall), and it's 64-bit safe even on
2913 32-bit platforms (for instance, SPARC debugging a SPARC64
2916 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2918 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2928 /* Parse LINE as a signal set and add its set bits to SIGS. */
2931 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2933 int len
= strlen (line
) - 1;
2937 if (line
[len
] != '\n')
2938 error ("Could not parse signal set: %s", line
);
2946 if (*p
>= '0' && *p
<= '9')
2948 else if (*p
>= 'a' && *p
<= 'f')
2949 digit
= *p
- 'a' + 10;
2951 error ("Could not parse signal set: %s", line
);
2956 sigaddset (sigs
, signum
+ 1);
2958 sigaddset (sigs
, signum
+ 2);
2960 sigaddset (sigs
, signum
+ 3);
2962 sigaddset (sigs
, signum
+ 4);
2968 /* Find process PID's pending signals from /proc/pid/status and set
2972 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
2975 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
2978 sigemptyset (pending
);
2979 sigemptyset (blocked
);
2980 sigemptyset (ignored
);
2981 sprintf (fname
, "/proc/%d/status", pid
);
2982 procfile
= fopen (fname
, "r");
2983 if (procfile
== NULL
)
2984 error ("Could not open %s", fname
);
2986 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
2988 /* Normal queued signals are on the SigPnd line in the status
2989 file. However, 2.6 kernels also have a "shared" pending
2990 queue for delivering signals to a thread group, so check for
2993 Unfortunately some Red Hat kernels include the shared pending
2994 queue but not the ShdPnd status field. */
2996 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
2997 add_line_to_sigset (buffer
+ 8, pending
);
2998 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
2999 add_line_to_sigset (buffer
+ 8, pending
);
3000 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3001 add_line_to_sigset (buffer
+ 8, blocked
);
3002 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3003 add_line_to_sigset (buffer
+ 8, ignored
);
3010 _initialize_linux_nat (void)
3012 struct sigaction action
;
3013 extern void thread_db_init (struct target_ops
*);
3015 deprecated_child_ops
.to_find_memory_regions
= linux_nat_find_memory_regions
;
3016 deprecated_child_ops
.to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3018 add_info ("proc", linux_nat_info_proc_cmd
,
3019 "Show /proc process information about any running process.\n\
3020 Specify any process id, or use the program being debugged by default.\n\
3021 Specify any of the following keywords for detailed info:\n\
3022 mappings -- list of mapped memory regions.\n\
3023 stat -- list a bunch of random process info.\n\
3024 status -- list a different bunch of random process info.\n\
3025 all -- list all available /proc info.");
3027 init_linux_nat_ops ();
3028 add_target (&linux_nat_ops
);
3029 thread_db_init (&linux_nat_ops
);
3031 /* Save the original signal mask. */
3032 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3034 action
.sa_handler
= sigchld_handler
;
3035 sigemptyset (&action
.sa_mask
);
3036 action
.sa_flags
= 0;
3037 sigaction (SIGCHLD
, &action
, NULL
);
3039 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3040 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3041 sigdelset (&suspend_mask
, SIGCHLD
);
3043 sigemptyset (&blocked_mask
);
3045 deprecated_add_show_from_set
3046 (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
3047 (char *) &debug_linux_nat
,
3048 "Set debugging of GNU/Linux lwp module.\n\
3049 Enables printf debugging output.\n", &setdebuglist
), &showdebuglist
);
3053 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3054 the GNU/Linux Threads library and therefore doesn't really belong
3057 /* Read variable NAME in the target and return its value if found.
3058 Otherwise return zero. It is assumed that the type of the variable
3062 get_signo (const char *name
)
3064 struct minimal_symbol
*ms
;
3067 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3071 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
3072 sizeof (signo
)) != 0)
3078 /* Return the set of signals used by the threads library in *SET. */
3081 lin_thread_get_thread_signals (sigset_t
*set
)
3083 struct sigaction action
;
3084 int restart
, cancel
;
3088 restart
= get_signo ("__pthread_sig_restart");
3092 cancel
= get_signo ("__pthread_sig_cancel");
3096 sigaddset (set
, restart
);
3097 sigaddset (set
, cancel
);
3099 /* The GNU/Linux Threads library makes terminating threads send a
3100 special "cancel" signal instead of SIGCHLD. Make sure we catch
3101 those (to prevent them from terminating GDB itself, which is
3102 likely to be their default action) and treat them the same way as
3105 action
.sa_handler
= sigchld_handler
;
3106 sigemptyset (&action
.sa_mask
);
3107 action
.sa_flags
= 0;
3108 sigaction (cancel
, &action
, NULL
);
3110 /* We block the "cancel" signal throughout this code ... */
3111 sigaddset (&blocked_mask
, cancel
);
3112 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3114 /* ... except during a sigsuspend. */
3115 sigdelset (&suspend_mask
, cancel
);