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_VFORKDONE 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 extern struct target_ops child_ops
;
88 static int linux_parent_pid
;
90 struct simple_pid_list
93 struct simple_pid_list
*next
;
95 struct simple_pid_list
*stopped_pids
;
97 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
98 can not be used, 1 if it can. */
100 static int linux_supports_tracefork_flag
= -1;
102 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
103 PTRACE_O_TRACEVFORKDONE. */
105 static int linux_supports_tracevforkdone_flag
= -1;
108 /* Trivial list manipulation functions to keep track of a list of
109 new stopped processes. */
111 add_to_pid_list (struct simple_pid_list
**listp
, int pid
)
113 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
115 new_pid
->next
= *listp
;
120 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
)
122 struct simple_pid_list
**p
;
124 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
125 if ((*p
)->pid
== pid
)
127 struct simple_pid_list
*next
= (*p
)->next
;
136 linux_record_stopped_pid (int pid
)
138 add_to_pid_list (&stopped_pids
, pid
);
142 /* A helper function for linux_test_for_tracefork, called after fork (). */
145 linux_tracefork_child (void)
149 ptrace (PTRACE_TRACEME
, 0, 0, 0);
150 kill (getpid (), SIGSTOP
);
155 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
156 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
157 fork tracing, and let it fork. If the process exits, we assume that
158 we can't use TRACEFORK; if we get the fork notification, and we can
159 extract the new child's PID, then we assume that we can. */
162 linux_test_for_tracefork (void)
164 int child_pid
, ret
, status
;
169 perror_with_name ("linux_test_for_tracefork: fork");
172 linux_tracefork_child ();
174 ret
= waitpid (child_pid
, &status
, 0);
176 perror_with_name ("linux_test_for_tracefork: waitpid");
177 else if (ret
!= child_pid
)
178 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
179 if (! WIFSTOPPED (status
))
180 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
182 linux_supports_tracefork_flag
= 0;
184 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
187 ptrace (PTRACE_KILL
, child_pid
, 0, 0);
188 waitpid (child_pid
, &status
, 0);
192 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
193 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
194 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
195 linux_supports_tracevforkdone_flag
= (ret
== 0);
197 ptrace (PTRACE_CONT
, child_pid
, 0, 0);
198 ret
= waitpid (child_pid
, &status
, 0);
199 if (ret
== child_pid
&& WIFSTOPPED (status
)
200 && status
>> 16 == PTRACE_EVENT_FORK
)
203 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
204 if (ret
== 0 && second_pid
!= 0)
208 linux_supports_tracefork_flag
= 1;
209 waitpid (second_pid
, &second_status
, 0);
210 ptrace (PTRACE_DETACH
, second_pid
, 0, 0);
214 if (WIFSTOPPED (status
))
216 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
217 waitpid (child_pid
, &status
, 0);
221 /* Return non-zero iff we have tracefork functionality available.
222 This function also sets linux_supports_tracefork_flag. */
225 linux_supports_tracefork (void)
227 if (linux_supports_tracefork_flag
== -1)
228 linux_test_for_tracefork ();
229 return linux_supports_tracefork_flag
;
233 linux_supports_tracevforkdone (void)
235 if (linux_supports_tracefork_flag
== -1)
236 linux_test_for_tracefork ();
237 return linux_supports_tracevforkdone_flag
;
242 linux_enable_event_reporting (ptid_t ptid
)
244 int pid
= ptid_get_pid (ptid
);
247 if (! linux_supports_tracefork ())
250 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
251 | PTRACE_O_TRACECLONE
;
252 if (linux_supports_tracevforkdone ())
253 options
|= PTRACE_O_TRACEVFORKDONE
;
255 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
256 read-only process state. */
258 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
262 child_post_attach (int pid
)
264 linux_enable_event_reporting (pid_to_ptid (pid
));
268 linux_child_post_startup_inferior (ptid_t ptid
)
270 linux_enable_event_reporting (ptid
);
273 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
275 child_post_startup_inferior (ptid_t ptid
)
277 linux_child_post_startup_inferior (ptid
);
282 child_follow_fork (int follow_child
)
285 struct target_waitstatus last_status
;
287 int parent_pid
, child_pid
;
289 get_last_target_status (&last_ptid
, &last_status
);
290 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
291 parent_pid
= ptid_get_pid (last_ptid
);
292 child_pid
= last_status
.value
.related_pid
;
296 /* We're already attached to the parent, by default. */
298 /* Before detaching from the child, remove all breakpoints from
299 it. (This won't actually modify the breakpoint list, but will
300 physically remove the breakpoints from the child.) */
301 /* If we vforked this will remove the breakpoints from the parent
302 also, but they'll be reinserted below. */
303 detach_breakpoints (child_pid
);
305 fprintf_filtered (gdb_stdout
,
306 "Detaching after fork from child process %d.\n",
309 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
313 if (linux_supports_tracevforkdone ())
317 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
318 waitpid (parent_pid
, &status
, __WALL
);
319 if ((status
>> 16) != PTRACE_EVENT_VFORKDONE
)
320 warning ("Unexpected waitpid result %06x when waiting for "
321 "vfork-done", status
);
325 /* We can't insert breakpoints until the child has
326 finished with the shared memory region. We need to
327 wait until that happens. Ideal would be to just
329 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
330 - waitpid (parent_pid, &status, __WALL);
331 However, most architectures can't handle a syscall
332 being traced on the way out if it wasn't traced on
335 We might also think to loop, continuing the child
336 until it exits or gets a SIGTRAP. One problem is
337 that the child might call ptrace with PTRACE_TRACEME.
339 There's no simple and reliable way to figure out when
340 the vforked child will be done with its copy of the
341 shared memory. We could step it out of the syscall,
342 two instructions, let it go, and then single-step the
343 parent once. When we have hardware single-step, this
344 would work; with software single-step it could still
345 be made to work but we'd have to be able to insert
346 single-step breakpoints in the child, and we'd have
347 to insert -just- the single-step breakpoint in the
348 parent. Very awkward.
350 In the end, the best we can do is to make sure it
351 runs for a little while. Hopefully it will be out of
352 range of any breakpoints we reinsert. Usually this
353 is only the single-step breakpoint at vfork's return
359 /* Since we vforked, breakpoints were removed in the parent
360 too. Put them back. */
361 reattach_breakpoints (parent_pid
);
366 char child_pid_spelling
[40];
368 /* Needed to keep the breakpoint lists in sync. */
370 detach_breakpoints (child_pid
);
372 /* Before detaching from the parent, remove all breakpoints from it. */
373 remove_breakpoints ();
375 fprintf_filtered (gdb_stdout
,
376 "Attaching after fork to child process %d.\n",
379 /* If we're vforking, we may want to hold on to the parent until
380 the child exits or execs. At exec time we can remove the old
381 breakpoints from the parent and detach it; at exit time we
382 could do the same (or even, sneakily, resume debugging it - the
383 child's exec has failed, or something similar).
385 This doesn't clean up "properly", because we can't call
386 target_detach, but that's OK; if the current target is "child",
387 then it doesn't need any further cleanups, and lin_lwp will
388 generally not encounter vfork (vfork is defined to fork
391 The holding part is very easy if we have VFORKDONE events;
392 but keeping track of both processes is beyond GDB at the
393 moment. So we don't expose the parent to the rest of GDB.
394 Instead we quietly hold onto it until such time as we can
398 linux_parent_pid
= parent_pid
;
400 target_detach (NULL
, 0);
402 inferior_ptid
= pid_to_ptid (child_pid
);
403 push_target (&child_ops
);
405 /* Reset breakpoints in the child as appropriate. */
406 follow_inferior_reset_breakpoints ();
413 linux_handle_extended_wait (int pid
, int status
,
414 struct target_waitstatus
*ourstatus
)
416 int event
= status
>> 16;
418 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
419 || event
== PTRACE_EVENT_CLONE
)
421 unsigned long new_pid
;
424 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
426 /* If we haven't already seen the new PID stop, wait for it now. */
427 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
429 /* The new child has a pending SIGSTOP. We can't affect it until it
430 hits the SIGSTOP, but we're already attached. */
432 ret
= waitpid (new_pid
, &status
,
433 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
434 } while (ret
== -1 && errno
== EINTR
);
436 perror_with_name ("waiting for new child");
437 else if (ret
!= new_pid
)
438 internal_error (__FILE__
, __LINE__
,
439 "wait returned unexpected PID %d", ret
);
440 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
441 internal_error (__FILE__
, __LINE__
,
442 "wait returned unexpected status 0x%x", status
);
445 if (event
== PTRACE_EVENT_FORK
)
446 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
447 else if (event
== PTRACE_EVENT_VFORK
)
448 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
450 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
452 ourstatus
->value
.related_pid
= new_pid
;
453 return inferior_ptid
;
456 if (event
== PTRACE_EVENT_EXEC
)
458 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
459 ourstatus
->value
.execd_pathname
460 = xstrdup (child_pid_to_exec_file (pid
));
462 if (linux_parent_pid
)
464 detach_breakpoints (linux_parent_pid
);
465 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
467 linux_parent_pid
= 0;
470 return inferior_ptid
;
473 internal_error (__FILE__
, __LINE__
,
474 "unknown ptrace event %d", event
);
479 child_insert_fork_catchpoint (int pid
)
481 if (! linux_supports_tracefork ())
482 error ("Your system does not support fork catchpoints.");
488 child_insert_vfork_catchpoint (int pid
)
490 if (!linux_supports_tracefork ())
491 error ("Your system does not support vfork catchpoints.");
497 child_insert_exec_catchpoint (int pid
)
499 if (!linux_supports_tracefork ())
500 error ("Your system does not support exec catchpoints.");
509 int pid
= PIDGET (inferior_ptid
);
510 struct target_waitstatus last
;
517 /* If we're stopped while forking and we haven't followed yet, kill the
518 other task. We need to do this first because the parent will be
519 sleeping if this is a vfork. */
521 get_last_target_status (&last_ptid
, &last
);
523 if (last
.kind
== TARGET_WAITKIND_FORKED
524 || last
.kind
== TARGET_WAITKIND_VFORKED
)
526 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
527 ptrace_wait (null_ptid
, &status
);
530 /* Kill the current process. */
531 ptrace (PT_KILL
, pid
, 0, 0);
532 ret
= ptrace_wait (null_ptid
, &status
);
534 /* We might get a SIGCHLD instead of an exit status. This is
535 aggravated by the first kill above - a child has just died. */
537 while (ret
== pid
&& WIFSTOPPED (status
))
539 ptrace (PT_KILL
, pid
, 0, 0);
540 ret
= ptrace_wait (null_ptid
, &status
);
543 target_mourn_inferior ();
546 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
547 are processes sharing the same VM space. A multi-threaded process
548 is basically a group of such processes. However, such a grouping
549 is almost entirely a user-space issue; the kernel doesn't enforce
550 such a grouping at all (this might change in the future). In
551 general, we'll rely on the threads library (i.e. the GNU/Linux
552 Threads library) to provide such a grouping.
554 It is perfectly well possible to write a multi-threaded application
555 without the assistance of a threads library, by using the clone
556 system call directly. This module should be able to give some
557 rudimentary support for debugging such applications if developers
558 specify the CLONE_PTRACE flag in the clone system call, and are
559 using the Linux kernel 2.4 or above.
561 Note that there are some peculiarities in GNU/Linux that affect
564 - In general one should specify the __WCLONE flag to waitpid in
565 order to make it report events for any of the cloned processes
566 (and leave it out for the initial process). However, if a cloned
567 process has exited the exit status is only reported if the
568 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
569 we cannot use it since GDB must work on older systems too.
571 - When a traced, cloned process exits and is waited for by the
572 debugger, the kernel reassigns it to the original parent and
573 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
574 library doesn't notice this, which leads to the "zombie problem":
575 When debugged a multi-threaded process that spawns a lot of
576 threads will run out of processes, even if the threads exit,
577 because the "zombies" stay around. */
579 /* List of known LWPs. */
580 static struct lwp_info
*lwp_list
;
582 /* Number of LWPs in the list. */
585 /* Non-zero if we're running in "threaded" mode. */
589 #define GET_LWP(ptid) ptid_get_lwp (ptid)
590 #define GET_PID(ptid) ptid_get_pid (ptid)
591 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
592 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
594 /* If the last reported event was a SIGTRAP, this variable is set to
595 the process id of the LWP/thread that got it. */
599 /* This module's target-specific operations. */
600 static struct target_ops linux_nat_ops
;
602 /* The standard child operations. */
603 extern struct target_ops child_ops
;
605 /* Since we cannot wait (in linux_nat_wait) for the initial process and
606 any cloned processes with a single call to waitpid, we have to use
607 the WNOHANG flag and call waitpid in a loop. To optimize
608 things a bit we use `sigsuspend' to wake us up when a process has
609 something to report (it will send us a SIGCHLD if it has). To make
610 this work we have to juggle with the signal mask. We save the
611 original signal mask such that we can restore it before creating a
612 new process in order to avoid blocking certain signals in the
613 inferior. We then block SIGCHLD during the waitpid/sigsuspend
616 /* Original signal mask. */
617 static sigset_t normal_mask
;
619 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
620 _initialize_linux_nat. */
621 static sigset_t suspend_mask
;
623 /* Signals to block to make that sigsuspend work. */
624 static sigset_t blocked_mask
;
627 /* Prototypes for local functions. */
628 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
629 static int linux_nat_thread_alive (ptid_t ptid
);
631 /* Convert wait status STATUS to a string. Used for printing debug
635 status_to_str (int status
)
639 if (WIFSTOPPED (status
))
640 snprintf (buf
, sizeof (buf
), "%s (stopped)",
641 strsignal (WSTOPSIG (status
)));
642 else if (WIFSIGNALED (status
))
643 snprintf (buf
, sizeof (buf
), "%s (terminated)",
644 strsignal (WSTOPSIG (status
)));
646 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
651 /* Initialize the list of LWPs. Note that this module, contrary to
652 what GDB's generic threads layer does for its thread list,
653 re-initializes the LWP lists whenever we mourn or detach (which
654 doesn't involve mourning) the inferior. */
659 struct lwp_info
*lp
, *lpnext
;
661 for (lp
= lwp_list
; lp
; lp
= lpnext
)
672 /* Add the LWP specified by PID to the list. If this causes the
673 number of LWPs to become larger than one, go into "threaded" mode.
674 Return a pointer to the structure describing the new LWP. */
676 static struct lwp_info
*
677 add_lwp (ptid_t ptid
)
681 gdb_assert (is_lwp (ptid
));
683 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
685 memset (lp
, 0, sizeof (struct lwp_info
));
687 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
699 /* Remove the LWP specified by PID from the list. */
702 delete_lwp (ptid_t ptid
)
704 struct lwp_info
*lp
, *lpprev
;
708 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
709 if (ptid_equal (lp
->ptid
, ptid
))
715 /* We don't go back to "non-threaded" mode if the number of threads
716 becomes less than two. */
720 lpprev
->next
= lp
->next
;
727 /* Return a pointer to the structure describing the LWP corresponding
728 to PID. If no corresponding LWP could be found, return NULL. */
730 static struct lwp_info
*
731 find_lwp_pid (ptid_t ptid
)
737 lwp
= GET_LWP (ptid
);
739 lwp
= GET_PID (ptid
);
741 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
742 if (lwp
== GET_LWP (lp
->ptid
))
748 /* Call CALLBACK with its second argument set to DATA for every LWP in
749 the list. If CALLBACK returns 1 for a particular LWP, return a
750 pointer to the structure describing that LWP immediately.
751 Otherwise return NULL. */
754 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
756 struct lwp_info
*lp
, *lpnext
;
758 for (lp
= lwp_list
; lp
; lp
= lpnext
)
761 if ((*callback
) (lp
, data
))
768 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
769 a message telling the user that a new LWP has been added to the
773 lin_lwp_attach_lwp (ptid_t ptid
, int verbose
)
775 struct lwp_info
*lp
, *found_lp
;
777 gdb_assert (is_lwp (ptid
));
779 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
780 to interrupt either the ptrace() or waitpid() calls below. */
781 if (!sigismember (&blocked_mask
, SIGCHLD
))
783 sigaddset (&blocked_mask
, SIGCHLD
);
784 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
788 printf_filtered ("[New %s]\n", target_pid_to_str (ptid
));
790 found_lp
= lp
= find_lwp_pid (ptid
);
794 /* We assume that we're already attached to any LWP that has an id
795 equal to the overall process id, and to any LWP that is already
796 in our list of LWPs. If we're not seeing exit events from threads
797 and we've had PID wraparound since we last tried to stop all threads,
798 this assumption might be wrong; fortunately, this is very unlikely
800 if (GET_LWP (ptid
) != GET_PID (ptid
) && found_lp
== NULL
)
805 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
806 error ("Can't attach %s: %s", target_pid_to_str (ptid
),
807 safe_strerror (errno
));
810 fprintf_unfiltered (gdb_stdlog
,
811 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
812 target_pid_to_str (ptid
));
814 pid
= waitpid (GET_LWP (ptid
), &status
, 0);
815 if (pid
== -1 && errno
== ECHILD
)
817 /* Try again with __WCLONE to check cloned processes. */
818 pid
= waitpid (GET_LWP (ptid
), &status
, __WCLONE
);
822 gdb_assert (pid
== GET_LWP (ptid
)
823 && WIFSTOPPED (status
) && WSTOPSIG (status
));
825 child_post_attach (pid
);
831 fprintf_unfiltered (gdb_stdlog
,
832 "LLAL: waitpid %s received %s\n",
833 target_pid_to_str (ptid
),
834 status_to_str (status
));
839 /* We assume that the LWP representing the original process is
840 already stopped. Mark it as stopped in the data structure
841 that the linux ptrace layer uses to keep track of threads.
842 Note that this won't have already been done since the main
843 thread will have, we assume, been stopped by an attach from a
850 linux_nat_attach (char *args
, int from_tty
)
856 /* FIXME: We should probably accept a list of process id's, and
857 attach all of them. */
858 child_ops
.to_attach (args
, from_tty
);
860 /* Add the initial process as the first LWP to the list. */
861 lp
= add_lwp (BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
)));
863 /* Make sure the initial process is stopped. The user-level threads
864 layer might want to poke around in the inferior, and that won't
865 work if things haven't stabilized yet. */
866 pid
= waitpid (GET_PID (inferior_ptid
), &status
, 0);
867 if (pid
== -1 && errno
== ECHILD
)
869 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid
));
871 /* Try again with __WCLONE to check cloned processes. */
872 pid
= waitpid (GET_PID (inferior_ptid
), &status
, __WCLONE
);
876 gdb_assert (pid
== GET_PID (inferior_ptid
)
877 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
);
881 /* Fake the SIGSTOP that core GDB expects. */
882 lp
->status
= W_STOPCODE (SIGSTOP
);
886 fprintf_unfiltered (gdb_stdlog
,
887 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid
);
892 detach_callback (struct lwp_info
*lp
, void *data
)
894 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
896 if (debug_linux_nat
&& lp
->status
)
897 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
898 strsignal (WSTOPSIG (lp
->status
)),
899 target_pid_to_str (lp
->ptid
));
901 while (lp
->signalled
&& lp
->stopped
)
904 if (ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0,
905 WSTOPSIG (lp
->status
)) < 0)
906 error ("Can't continue %s: %s", target_pid_to_str (lp
->ptid
),
907 safe_strerror (errno
));
910 fprintf_unfiltered (gdb_stdlog
,
911 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
912 target_pid_to_str (lp
->ptid
),
913 status_to_str (lp
->status
));
918 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
919 here. But since lp->signalled was cleared above,
920 stop_wait_callback didn't do anything; the process was left
921 running. Shouldn't we be waiting for it to stop?
922 I've removed the call, since stop_wait_callback now does do
923 something when called with lp->signalled == 0. */
925 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
928 /* We don't actually detach from the LWP that has an id equal to the
929 overall process id just yet. */
930 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
933 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
934 WSTOPSIG (lp
->status
)) < 0)
935 error ("Can't detach %s: %s", target_pid_to_str (lp
->ptid
),
936 safe_strerror (errno
));
939 fprintf_unfiltered (gdb_stdlog
,
940 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
941 target_pid_to_str (lp
->ptid
),
942 strsignal (WSTOPSIG (lp
->status
)));
944 delete_lwp (lp
->ptid
);
951 linux_nat_detach (char *args
, int from_tty
)
953 iterate_over_lwps (detach_callback
, NULL
);
955 /* Only the initial process should be left right now. */
956 gdb_assert (num_lwps
== 1);
958 trap_ptid
= null_ptid
;
960 /* Destroy LWP info; it's no longer valid. */
963 /* Restore the original signal mask. */
964 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
965 sigemptyset (&blocked_mask
);
967 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
968 child_ops
.to_detach (args
, from_tty
);
974 resume_callback (struct lwp_info
*lp
, void *data
)
976 if (lp
->stopped
&& lp
->status
== 0)
978 struct thread_info
*tp
;
980 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), 0, TARGET_SIGNAL_0
);
982 fprintf_unfiltered (gdb_stdlog
,
983 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
984 target_pid_to_str (lp
->ptid
));
993 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1000 resume_set_callback (struct lwp_info
*lp
, void *data
)
1007 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1009 struct lwp_info
*lp
;
1012 /* A specific PTID means `step only this process id'. */
1013 resume_all
= (PIDGET (ptid
) == -1);
1016 iterate_over_lwps (resume_set_callback
, NULL
);
1018 iterate_over_lwps (resume_clear_callback
, NULL
);
1020 /* If PID is -1, it's the current inferior that should be
1021 handled specially. */
1022 if (PIDGET (ptid
) == -1)
1023 ptid
= inferior_ptid
;
1025 lp
= find_lwp_pid (ptid
);
1028 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1030 /* Remember if we're stepping. */
1033 /* Mark this LWP as resumed. */
1036 /* If we have a pending wait status for this thread, there is no
1037 point in resuming the process. */
1040 /* FIXME: What should we do if we are supposed to continue
1041 this thread with a signal? */
1042 gdb_assert (signo
== TARGET_SIGNAL_0
);
1046 /* Mark LWP as not stopped to prevent it from being continued by
1052 iterate_over_lwps (resume_callback
, NULL
);
1054 child_resume (ptid
, step
, signo
);
1055 if (debug_linux_nat
)
1056 fprintf_unfiltered (gdb_stdlog
,
1057 "LLR: %s %s, %s (resume event thread)\n",
1058 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1059 target_pid_to_str (ptid
),
1060 signo
? strsignal (signo
) : "0");
1063 /* Issue kill to specified lwp. */
1065 static int tkill_failed
;
1068 kill_lwp (int lwpid
, int signo
)
1072 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1073 fails, then we are not using nptl threads and we should be using kill. */
1075 #ifdef HAVE_TKILL_SYSCALL
1078 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1079 if (errno
!= ENOSYS
)
1086 return kill (lwpid
, signo
);
1089 /* Handle a GNU/Linux extended wait response. Most of the work we
1090 just pass off to linux_handle_extended_wait, but if it reports a
1091 clone event we need to add the new LWP to our list (and not report
1092 the trap to higher layers). This function returns non-zero if
1093 the event should be ignored and we should wait again. */
1096 linux_nat_handle_extended (struct lwp_info
*lp
, int status
)
1098 linux_handle_extended_wait (GET_LWP (lp
->ptid
), status
,
1101 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1102 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_SPURIOUS
)
1104 struct lwp_info
*new_lp
;
1105 new_lp
= add_lwp (BUILD_LWP (lp
->waitstatus
.value
.related_pid
,
1106 GET_PID (inferior_ptid
)));
1108 new_lp
->stopped
= 1;
1110 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1112 if (debug_linux_nat
)
1113 fprintf_unfiltered (gdb_stdlog
,
1114 "LLHE: Got clone event from LWP %ld, resuming\n",
1115 GET_LWP (lp
->ptid
));
1116 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1124 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1128 wait_lwp (struct lwp_info
*lp
)
1132 int thread_dead
= 0;
1134 gdb_assert (!lp
->stopped
);
1135 gdb_assert (lp
->status
== 0);
1137 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1138 if (pid
== -1 && errno
== ECHILD
)
1140 pid
= waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1141 if (pid
== -1 && errno
== ECHILD
)
1143 /* The thread has previously exited. We need to delete it
1144 now because, for some vendor 2.4 kernels with NPTL
1145 support backported, there won't be an exit event unless
1146 it is the main thread. 2.6 kernels will report an exit
1147 event for each thread that exits, as expected. */
1149 if (debug_linux_nat
)
1150 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1151 target_pid_to_str (lp
->ptid
));
1157 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1159 if (debug_linux_nat
)
1161 fprintf_unfiltered (gdb_stdlog
,
1162 "WL: waitpid %s received %s\n",
1163 target_pid_to_str (lp
->ptid
),
1164 status_to_str (status
));
1168 /* Check if the thread has exited. */
1169 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1172 if (debug_linux_nat
)
1173 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1174 target_pid_to_str (lp
->ptid
));
1179 if (in_thread_list (lp
->ptid
))
1181 /* Core GDB cannot deal with us deleting the current thread. */
1182 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1183 delete_thread (lp
->ptid
);
1184 printf_unfiltered ("[%s exited]\n",
1185 target_pid_to_str (lp
->ptid
));
1188 delete_lwp (lp
->ptid
);
1192 gdb_assert (WIFSTOPPED (status
));
1194 /* Handle GNU/Linux's extended waitstatus for trace events. */
1195 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1197 if (debug_linux_nat
)
1198 fprintf_unfiltered (gdb_stdlog
,
1199 "WL: Handling extended status 0x%06x\n",
1201 if (linux_nat_handle_extended (lp
, status
))
1202 return wait_lwp (lp
);
1208 /* Send a SIGSTOP to LP. */
1211 stop_callback (struct lwp_info
*lp
, void *data
)
1213 if (!lp
->stopped
&& !lp
->signalled
)
1217 if (debug_linux_nat
)
1219 fprintf_unfiltered (gdb_stdlog
,
1220 "SC: kill %s **<SIGSTOP>**\n",
1221 target_pid_to_str (lp
->ptid
));
1224 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1225 if (debug_linux_nat
)
1227 fprintf_unfiltered (gdb_stdlog
,
1228 "SC: lwp kill %d %s\n",
1230 errno
? safe_strerror (errno
) : "ERRNO-OK");
1234 gdb_assert (lp
->status
== 0);
1240 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1241 a pointer to a set of signals to be flushed immediately. */
1244 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1246 sigset_t
*flush_mask
= data
;
1252 status
= wait_lwp (lp
);
1256 /* Ignore any signals in FLUSH_MASK. */
1257 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1266 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1267 if (debug_linux_nat
)
1268 fprintf_unfiltered (gdb_stdlog
,
1269 "PTRACE_CONT %s, 0, 0 (%s)\n",
1270 target_pid_to_str (lp
->ptid
),
1271 errno
? safe_strerror (errno
) : "OK");
1273 return stop_wait_callback (lp
, flush_mask
);
1276 if (WSTOPSIG (status
) != SIGSTOP
)
1278 if (WSTOPSIG (status
) == SIGTRAP
)
1280 /* If a LWP other than the LWP that we're reporting an
1281 event for has hit a GDB breakpoint (as opposed to
1282 some random trap signal), then just arrange for it to
1283 hit it again later. We don't keep the SIGTRAP status
1284 and don't forward the SIGTRAP signal to the LWP. We
1285 will handle the current event, eventually we will
1286 resume all LWPs, and this one will get its breakpoint
1289 If we do not do this, then we run the risk that the
1290 user will delete or disable the breakpoint, but the
1291 thread will have already tripped on it. */
1293 /* Now resume this LWP and get the SIGSTOP event. */
1295 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1296 if (debug_linux_nat
)
1298 fprintf_unfiltered (gdb_stdlog
,
1299 "PTRACE_CONT %s, 0, 0 (%s)\n",
1300 target_pid_to_str (lp
->ptid
),
1301 errno
? safe_strerror (errno
) : "OK");
1303 fprintf_unfiltered (gdb_stdlog
,
1304 "SWC: Candidate SIGTRAP event in %s\n",
1305 target_pid_to_str (lp
->ptid
));
1307 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1308 stop_wait_callback (lp
, data
);
1309 /* If there's another event, throw it back into the queue. */
1312 if (debug_linux_nat
)
1314 fprintf_unfiltered (gdb_stdlog
,
1315 "SWC: kill %s, %s\n",
1316 target_pid_to_str (lp
->ptid
),
1317 status_to_str ((int) status
));
1319 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
1321 /* Save the sigtrap event. */
1322 lp
->status
= status
;
1327 /* The thread was stopped with a signal other than
1328 SIGSTOP, and didn't accidentally trip a breakpoint. */
1330 if (debug_linux_nat
)
1332 fprintf_unfiltered (gdb_stdlog
,
1333 "SWC: Pending event %s in %s\n",
1334 status_to_str ((int) status
),
1335 target_pid_to_str (lp
->ptid
));
1337 /* Now resume this LWP and get the SIGSTOP event. */
1339 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1340 if (debug_linux_nat
)
1341 fprintf_unfiltered (gdb_stdlog
,
1342 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1343 target_pid_to_str (lp
->ptid
),
1344 errno
? safe_strerror (errno
) : "OK");
1346 /* Hold this event/waitstatus while we check to see if
1347 there are any more (we still want to get that SIGSTOP). */
1348 stop_wait_callback (lp
, data
);
1349 /* If the lp->status field is still empty, use it to hold
1350 this event. If not, then this event must be returned
1351 to the event queue of the LWP. */
1352 if (lp
->status
== 0)
1353 lp
->status
= status
;
1356 if (debug_linux_nat
)
1358 fprintf_unfiltered (gdb_stdlog
,
1359 "SWC: kill %s, %s\n",
1360 target_pid_to_str (lp
->ptid
),
1361 status_to_str ((int) status
));
1363 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1370 /* We caught the SIGSTOP that we intended to catch, so
1371 there's no SIGSTOP pending. */
1380 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1381 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1384 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
1386 sigset_t blocked
, ignored
;
1389 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
1394 for (i
= 1; i
< NSIG
; i
++)
1395 if (sigismember (pending
, i
))
1396 if (!sigismember (flush_mask
, i
)
1397 || sigismember (&blocked
, i
)
1398 || sigismember (&ignored
, i
))
1399 sigdelset (pending
, i
);
1401 if (sigisemptyset (pending
))
1407 /* DATA is interpreted as a mask of signals to flush. If LP has
1408 signals pending, and they are all in the flush mask, then arrange
1409 to flush them. LP should be stopped, as should all other threads
1410 it might share a signal queue with. */
1413 flush_callback (struct lwp_info
*lp
, void *data
)
1415 sigset_t
*flush_mask
= data
;
1416 sigset_t pending
, intersection
, blocked
, ignored
;
1419 /* Normally, when an LWP exits, it is removed from the LWP list. The
1420 last LWP isn't removed till later, however. So if there is only
1421 one LWP on the list, make sure it's alive. */
1422 if (lwp_list
== lp
&& lp
->next
== NULL
)
1423 if (!linux_nat_thread_alive (lp
->ptid
))
1426 /* Just because the LWP is stopped doesn't mean that new signals
1427 can't arrive from outside, so this function must be careful of
1428 race conditions. However, because all threads are stopped, we
1429 can assume that the pending mask will not shrink unless we resume
1430 the LWP, and that it will then get another signal. We can't
1431 control which one, however. */
1435 if (debug_linux_nat
)
1436 printf_unfiltered ("FC: LP has pending status %06x\n", lp
->status
);
1437 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
1441 while (linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
1446 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1447 if (debug_linux_nat
)
1448 fprintf_unfiltered (gdb_stderr
,
1449 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
1452 stop_wait_callback (lp
, flush_mask
);
1453 if (debug_linux_nat
)
1454 fprintf_unfiltered (gdb_stderr
,
1455 "FC: Wait finished; saved status is %d\n",
1462 /* Return non-zero if LP has a wait status pending. */
1465 status_callback (struct lwp_info
*lp
, void *data
)
1467 /* Only report a pending wait status if we pretend that this has
1468 indeed been resumed. */
1469 return (lp
->status
!= 0 && lp
->resumed
);
1472 /* Return non-zero if LP isn't stopped. */
1475 running_callback (struct lwp_info
*lp
, void *data
)
1477 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
1480 /* Count the LWP's that have had events. */
1483 count_events_callback (struct lwp_info
*lp
, void *data
)
1487 gdb_assert (count
!= NULL
);
1489 /* Count only LWPs that have a SIGTRAP event pending. */
1491 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1497 /* Select the LWP (if any) that is currently being single-stepped. */
1500 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
1502 if (lp
->step
&& lp
->status
!= 0)
1508 /* Select the Nth LWP that has had a SIGTRAP event. */
1511 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
1513 int *selector
= data
;
1515 gdb_assert (selector
!= NULL
);
1517 /* Select only LWPs that have a SIGTRAP event pending. */
1519 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
1520 if ((*selector
)-- == 0)
1527 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
1529 struct lwp_info
*event_lp
= data
;
1531 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1535 /* If a LWP other than the LWP that we're reporting an event for has
1536 hit a GDB breakpoint (as opposed to some random trap signal),
1537 then just arrange for it to hit it again later. We don't keep
1538 the SIGTRAP status and don't forward the SIGTRAP signal to the
1539 LWP. We will handle the current event, eventually we will resume
1540 all LWPs, and this one will get its breakpoint trap again.
1542 If we do not do this, then we run the risk that the user will
1543 delete or disable the breakpoint, but the LWP will have already
1547 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
1548 && breakpoint_inserted_here_p (read_pc_pid (lp
->ptid
) -
1549 DECR_PC_AFTER_BREAK
))
1551 if (debug_linux_nat
)
1552 fprintf_unfiltered (gdb_stdlog
,
1553 "CBC: Push back breakpoint for %s\n",
1554 target_pid_to_str (lp
->ptid
));
1556 /* Back up the PC if necessary. */
1557 if (DECR_PC_AFTER_BREAK
)
1558 write_pc_pid (read_pc_pid (lp
->ptid
) - DECR_PC_AFTER_BREAK
, lp
->ptid
);
1560 /* Throw away the SIGTRAP. */
1567 /* Select one LWP out of those that have events pending. */
1570 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
1573 int random_selector
;
1574 struct lwp_info
*event_lp
;
1576 /* Record the wait status for the origional LWP. */
1577 (*orig_lp
)->status
= *status
;
1579 /* Give preference to any LWP that is being single-stepped. */
1580 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
1581 if (event_lp
!= NULL
)
1583 if (debug_linux_nat
)
1584 fprintf_unfiltered (gdb_stdlog
,
1585 "SEL: Select single-step %s\n",
1586 target_pid_to_str (event_lp
->ptid
));
1590 /* No single-stepping LWP. Select one at random, out of those
1591 which have had SIGTRAP events. */
1593 /* First see how many SIGTRAP events we have. */
1594 iterate_over_lwps (count_events_callback
, &num_events
);
1596 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1597 random_selector
= (int)
1598 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1600 if (debug_linux_nat
&& num_events
> 1)
1601 fprintf_unfiltered (gdb_stdlog
,
1602 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1603 num_events
, random_selector
);
1605 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
1609 if (event_lp
!= NULL
)
1611 /* Switch the event LWP. */
1612 *orig_lp
= event_lp
;
1613 *status
= event_lp
->status
;
1616 /* Flush the wait status for the event LWP. */
1617 (*orig_lp
)->status
= 0;
1620 /* Return non-zero if LP has been resumed. */
1623 resumed_callback (struct lwp_info
*lp
, void *data
)
1630 /* We need to override child_wait to support attaching to cloned
1631 processes, since a normal wait (as done by the default version)
1632 ignores those processes. */
1634 /* Wait for child PTID to do something. Return id of the child,
1635 minus_one_ptid in case of error; store status into *OURSTATUS. */
1638 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1644 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1648 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1649 attached process. */
1652 pid
= waitpid (GET_PID (ptid
), &status
, 0);
1653 if (pid
== -1 && errno
== ECHILD
)
1654 /* Try again with __WCLONE to check cloned processes. */
1655 pid
= waitpid (GET_PID (ptid
), &status
, __WCLONE
);
1657 if (debug_linux_nat
)
1659 fprintf_unfiltered (gdb_stdlog
,
1660 "CW: waitpid %ld received %s\n",
1661 (long) pid
, status_to_str (status
));
1666 /* Make sure we don't report an event for the exit of the
1667 original program, if we've detached from it. */
1668 if (pid
!= -1 && !WIFSTOPPED (status
) && pid
!= GET_PID (inferior_ptid
))
1674 /* Check for stop events reported by a process we didn't already
1675 know about - in this case, anything other than inferior_ptid.
1677 If we're expecting to receive stopped processes after fork,
1678 vfork, and clone events, then we'll just add the new one to
1679 our list and go back to waiting for the event to be reported
1680 - the stopped process might be returned from waitpid before
1681 or after the event is. If we want to handle debugging of
1682 CLONE_PTRACE processes we need to do more here, i.e. switch
1683 to multi-threaded mode. */
1684 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
1685 && pid
!= GET_PID (inferior_ptid
))
1687 linux_record_stopped_pid (pid
);
1692 /* Handle GNU/Linux's extended waitstatus for trace events. */
1693 if (pid
!= -1 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
1694 && status
>> 16 != 0)
1696 linux_handle_extended_wait (pid
, status
, ourstatus
);
1698 /* If we see a clone event, detach the child, and don't
1699 report the event. It would be nice to offer some way to
1700 switch into a non-thread-db based threaded mode at this
1702 if (ourstatus
->kind
== TARGET_WAITKIND_SPURIOUS
)
1704 ptrace (PTRACE_DETACH
, ourstatus
->value
.related_pid
, 0, 0);
1705 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1706 ptrace (PTRACE_CONT
, pid
, 0, 0);
1712 clear_sigio_trap ();
1713 clear_sigint_trap ();
1715 while (pid
== -1 && save_errno
== EINTR
);
1719 warning ("Child process unexpectedly missing: %s",
1720 safe_strerror (errno
));
1722 /* Claim it exited with unknown signal. */
1723 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1724 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1725 return minus_one_ptid
;
1728 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1729 store_waitstatus (ourstatus
, status
);
1731 return pid_to_ptid (pid
);
1736 /* Stop an active thread, verify it still exists, then resume it. */
1739 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
1741 struct lwp_info
*ptr
;
1743 if (!lp
->stopped
&& !lp
->signalled
)
1745 stop_callback (lp
, NULL
);
1746 stop_wait_callback (lp
, NULL
);
1747 /* Resume if the lwp still exists. */
1748 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
1751 resume_callback (lp
, NULL
);
1752 resume_set_callback (lp
, NULL
);
1759 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1761 struct lwp_info
*lp
= NULL
;
1764 pid_t pid
= PIDGET (ptid
);
1765 sigset_t flush_mask
;
1767 sigemptyset (&flush_mask
);
1769 /* Make sure SIGCHLD is blocked. */
1770 if (!sigismember (&blocked_mask
, SIGCHLD
))
1772 sigaddset (&blocked_mask
, SIGCHLD
);
1773 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
1778 /* Make sure there is at least one LWP that has been resumed, at
1779 least if there are any LWPs at all. */
1780 gdb_assert (num_lwps
== 0 || iterate_over_lwps (resumed_callback
, NULL
));
1782 /* First check if there is a LWP with a wait status pending. */
1785 /* Any LWP that's been resumed will do. */
1786 lp
= iterate_over_lwps (status_callback
, NULL
);
1789 status
= lp
->status
;
1792 if (debug_linux_nat
&& status
)
1793 fprintf_unfiltered (gdb_stdlog
,
1794 "LLW: Using pending wait status %s for %s.\n",
1795 status_to_str (status
),
1796 target_pid_to_str (lp
->ptid
));
1799 /* But if we don't fine one, we'll have to wait, and check both
1800 cloned and uncloned processes. We start with the cloned
1802 options
= __WCLONE
| WNOHANG
;
1804 else if (is_lwp (ptid
))
1806 if (debug_linux_nat
)
1807 fprintf_unfiltered (gdb_stdlog
,
1808 "LLW: Waiting for specific LWP %s.\n",
1809 target_pid_to_str (ptid
));
1811 /* We have a specific LWP to check. */
1812 lp
= find_lwp_pid (ptid
);
1814 status
= lp
->status
;
1817 if (debug_linux_nat
&& status
)
1818 fprintf_unfiltered (gdb_stdlog
,
1819 "LLW: Using pending wait status %s for %s.\n",
1820 status_to_str (status
),
1821 target_pid_to_str (lp
->ptid
));
1823 /* If we have to wait, take into account whether PID is a cloned
1824 process or not. And we have to convert it to something that
1825 the layer beneath us can understand. */
1826 options
= lp
->cloned
? __WCLONE
: 0;
1827 pid
= GET_LWP (ptid
);
1830 if (status
&& lp
->signalled
)
1832 /* A pending SIGSTOP may interfere with the normal stream of
1833 events. In a typical case where interference is a problem,
1834 we have a SIGSTOP signal pending for LWP A while
1835 single-stepping it, encounter an event in LWP B, and take the
1836 pending SIGSTOP while trying to stop LWP A. After processing
1837 the event in LWP B, LWP A is continued, and we'll never see
1838 the SIGTRAP associated with the last time we were
1839 single-stepping LWP A. */
1841 /* Resume the thread. It should halt immediately returning the
1843 registers_changed ();
1844 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
1846 if (debug_linux_nat
)
1847 fprintf_unfiltered (gdb_stdlog
,
1848 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1849 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1850 target_pid_to_str (lp
->ptid
));
1852 gdb_assert (lp
->resumed
);
1854 /* This should catch the pending SIGSTOP. */
1855 stop_wait_callback (lp
, NULL
);
1858 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1859 attached process. */
1866 lwpid
= waitpid (pid
, &status
, options
);
1869 gdb_assert (pid
== -1 || lwpid
== pid
);
1871 if (debug_linux_nat
)
1873 fprintf_unfiltered (gdb_stdlog
,
1874 "LLW: waitpid %ld received %s\n",
1875 (long) lwpid
, status_to_str (status
));
1878 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
1880 /* Check for stop events reported by a process we didn't
1881 already know about - anything not already in our LWP
1884 If we're expecting to receive stopped processes after
1885 fork, vfork, and clone events, then we'll just add the
1886 new one to our list and go back to waiting for the event
1887 to be reported - the stopped process might be returned
1888 from waitpid before or after the event is. */
1889 if (WIFSTOPPED (status
) && !lp
)
1891 linux_record_stopped_pid (lwpid
);
1896 /* Make sure we don't report an event for the exit of an LWP not in
1897 our list, i.e. not part of the current process. This can happen
1898 if we detach from a program we original forked and then it
1900 if (!WIFSTOPPED (status
) && !lp
)
1906 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1907 CLONE_PTRACE processes which do not use the thread library -
1908 otherwise we wouldn't find the new LWP this way. That doesn't
1909 currently work, and the following code is currently unreachable
1910 due to the two blocks above. If it's fixed some day, this code
1911 should be broken out into a function so that we can also pick up
1912 LWPs from the new interface. */
1915 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
1916 if (options
& __WCLONE
)
1921 gdb_assert (WIFSTOPPED (status
)
1922 && WSTOPSIG (status
) == SIGSTOP
);
1925 if (!in_thread_list (inferior_ptid
))
1927 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
1928 GET_PID (inferior_ptid
));
1929 add_thread (inferior_ptid
);
1932 add_thread (lp
->ptid
);
1933 printf_unfiltered ("[New %s]\n",
1934 target_pid_to_str (lp
->ptid
));
1938 /* Handle GNU/Linux's extended waitstatus for trace events. */
1939 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1941 if (debug_linux_nat
)
1942 fprintf_unfiltered (gdb_stdlog
,
1943 "LLW: Handling extended status 0x%06x\n",
1945 if (linux_nat_handle_extended (lp
, status
))
1952 /* Check if the thread has exited. */
1953 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
1955 if (in_thread_list (lp
->ptid
))
1957 /* Core GDB cannot deal with us deleting the current
1959 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1960 delete_thread (lp
->ptid
);
1961 printf_unfiltered ("[%s exited]\n",
1962 target_pid_to_str (lp
->ptid
));
1965 /* If this is the main thread, we must stop all threads and
1966 verify if they are still alive. This is because in the nptl
1967 thread model, there is no signal issued for exiting LWPs
1968 other than the main thread. We only get the main thread
1969 exit signal once all child threads have already exited.
1970 If we stop all the threads and use the stop_wait_callback
1971 to check if they have exited we can determine whether this
1972 signal should be ignored or whether it means the end of the
1973 debugged application, regardless of which threading model
1975 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
1978 iterate_over_lwps (stop_and_resume_callback
, NULL
);
1981 if (debug_linux_nat
)
1982 fprintf_unfiltered (gdb_stdlog
,
1983 "LLW: %s exited.\n",
1984 target_pid_to_str (lp
->ptid
));
1986 delete_lwp (lp
->ptid
);
1988 /* If there is at least one more LWP, then the exit signal
1989 was not the end of the debugged application and should be
1993 /* Make sure there is at least one thread running. */
1994 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
1996 /* Discard the event. */
2002 /* Check if the current LWP has previously exited. In the nptl
2003 thread model, LWPs other than the main thread do not issue
2004 signals when they exit so we must check whenever the thread
2005 has stopped. A similar check is made in stop_wait_callback(). */
2006 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2008 if (in_thread_list (lp
->ptid
))
2010 /* Core GDB cannot deal with us deleting the current
2012 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
2013 delete_thread (lp
->ptid
);
2014 printf_unfiltered ("[%s exited]\n",
2015 target_pid_to_str (lp
->ptid
));
2017 if (debug_linux_nat
)
2018 fprintf_unfiltered (gdb_stdlog
,
2019 "LLW: %s exited.\n",
2020 target_pid_to_str (lp
->ptid
));
2022 delete_lwp (lp
->ptid
);
2024 /* Make sure there is at least one thread running. */
2025 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2027 /* Discard the event. */
2032 /* Make sure we don't report a SIGSTOP that we sent
2033 ourselves in an attempt to stop an LWP. */
2035 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2037 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
,
2039 "LLW: Delayed SIGSTOP caught for %s.\n",
2040 target_pid_to_str (lp
->ptid
));
2042 /* This is a delayed SIGSTOP. */
2045 registers_changed ();
2046 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
,
2048 if (debug_linux_nat
)
2049 fprintf_unfiltered (gdb_stdlog
,
2050 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2052 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2053 target_pid_to_str (lp
->ptid
));
2056 gdb_assert (lp
->resumed
);
2058 /* Discard the event. */
2068 /* Alternate between checking cloned and uncloned processes. */
2069 options
^= __WCLONE
;
2071 /* And suspend every time we have checked both. */
2072 if (options
& __WCLONE
)
2073 sigsuspend (&suspend_mask
);
2076 /* We shouldn't end up here unless we want to try again. */
2077 gdb_assert (status
== 0);
2080 clear_sigio_trap ();
2081 clear_sigint_trap ();
2085 /* Don't report signals that GDB isn't interested in, such as
2086 signals that are neither printed nor stopped upon. Stopping all
2087 threads can be a bit time-consuming so if we want decent
2088 performance with heavily multi-threaded programs, especially when
2089 they're using a high frequency timer, we'd better avoid it if we
2092 if (WIFSTOPPED (status
))
2094 int signo
= target_signal_from_host (WSTOPSIG (status
));
2096 if (signal_stop_state (signo
) == 0
2097 && signal_print_state (signo
) == 0
2098 && signal_pass_state (signo
) == 1)
2100 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2101 here? It is not clear we should. GDB may not expect
2102 other threads to run. On the other hand, not resuming
2103 newly attached threads may cause an unwanted delay in
2104 getting them running. */
2105 registers_changed ();
2106 child_resume (pid_to_ptid (GET_LWP (lp
->ptid
)), lp
->step
, signo
);
2107 if (debug_linux_nat
)
2108 fprintf_unfiltered (gdb_stdlog
,
2109 "LLW: %s %s, %s (preempt 'handle')\n",
2111 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2112 target_pid_to_str (lp
->ptid
),
2113 signo
? strsignal (signo
) : "0");
2119 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2121 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2122 forwarded to the entire process group, that is, all LWP's
2123 will receive it. Since we only want to report it once,
2124 we try to flush it from all LWPs except this one. */
2125 sigaddset (&flush_mask
, SIGINT
);
2129 /* This LWP is stopped now. */
2132 if (debug_linux_nat
)
2133 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2134 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2136 /* Now stop all other LWP's ... */
2137 iterate_over_lwps (stop_callback
, NULL
);
2139 /* ... and wait until all of them have reported back that they're no
2141 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2142 iterate_over_lwps (flush_callback
, &flush_mask
);
2144 /* If we're not waiting for a specific LWP, choose an event LWP from
2145 among those that have had events. Giving equal priority to all
2146 LWPs that have had events helps prevent starvation. */
2148 select_event_lwp (&lp
, &status
);
2150 /* Now that we've selected our final event LWP, cancel any
2151 breakpoints in other LWPs that have hit a GDB breakpoint. See
2152 the comment in cancel_breakpoints_callback to find out why. */
2153 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2155 /* If we're not running in "threaded" mode, we'll report the bare
2158 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2160 trap_ptid
= (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2161 if (debug_linux_nat
)
2162 fprintf_unfiltered (gdb_stdlog
,
2163 "LLW: trap_ptid is %s.\n",
2164 target_pid_to_str (trap_ptid
));
2167 trap_ptid
= null_ptid
;
2169 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2171 *ourstatus
= lp
->waitstatus
;
2172 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2175 store_waitstatus (ourstatus
, status
);
2177 return (threaded
? lp
->ptid
: pid_to_ptid (GET_LWP (lp
->ptid
)));
2181 kill_callback (struct lwp_info
*lp
, void *data
)
2184 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2185 if (debug_linux_nat
)
2186 fprintf_unfiltered (gdb_stdlog
,
2187 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2188 target_pid_to_str (lp
->ptid
),
2189 errno
? safe_strerror (errno
) : "OK");
2195 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2199 /* We must make sure that there are no pending events (delayed
2200 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2201 program doesn't interfere with any following debugging session. */
2203 /* For cloned processes we must check both with __WCLONE and
2204 without, since the exit status of a cloned process isn't reported
2210 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2211 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2213 fprintf_unfiltered (gdb_stdlog
,
2214 "KWC: wait %s received unknown.\n",
2215 target_pid_to_str (lp
->ptid
));
2218 while (pid
== GET_LWP (lp
->ptid
));
2220 gdb_assert (pid
== -1 && errno
== ECHILD
);
2225 pid
= waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2226 if (pid
!= (pid_t
) -1 && debug_linux_nat
)
2228 fprintf_unfiltered (gdb_stdlog
,
2229 "KWC: wait %s received unk.\n",
2230 target_pid_to_str (lp
->ptid
));
2233 while (pid
== GET_LWP (lp
->ptid
));
2235 gdb_assert (pid
== -1 && errno
== ECHILD
);
2240 linux_nat_kill (void)
2242 /* Kill all LWP's ... */
2243 iterate_over_lwps (kill_callback
, NULL
);
2245 /* ... and wait until we've flushed all events. */
2246 iterate_over_lwps (kill_wait_callback
, NULL
);
2248 target_mourn_inferior ();
2252 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
2255 child_ops
.to_create_inferior (exec_file
, allargs
, env
, from_tty
);
2259 linux_nat_mourn_inferior (void)
2261 trap_ptid
= null_ptid
;
2263 /* Destroy LWP info; it's no longer valid. */
2266 /* Restore the original signal mask. */
2267 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
2268 sigemptyset (&blocked_mask
);
2270 child_ops
.to_mourn_inferior ();
2274 linux_nat_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2275 struct mem_attrib
*attrib
, struct target_ops
*target
)
2277 struct cleanup
*old_chain
= save_inferior_ptid ();
2280 if (is_lwp (inferior_ptid
))
2281 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
2283 xfer
= linux_proc_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2285 xfer
= child_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
2287 do_cleanups (old_chain
);
2292 linux_nat_thread_alive (ptid_t ptid
)
2294 gdb_assert (is_lwp (ptid
));
2297 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
2298 if (debug_linux_nat
)
2299 fprintf_unfiltered (gdb_stdlog
,
2300 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2301 target_pid_to_str (ptid
),
2302 errno
? safe_strerror (errno
) : "OK");
2310 linux_nat_pid_to_str (ptid_t ptid
)
2312 static char buf
[64];
2316 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
2320 return normal_pid_to_str (ptid
);
2324 init_linux_nat_ops (void)
2327 linux_nat_ops
.to_open
= linux_nat_open
;
2329 linux_nat_ops
.to_shortname
= "lwp-layer";
2330 linux_nat_ops
.to_longname
= "lwp-layer";
2331 linux_nat_ops
.to_doc
= "Low level threads support (LWP layer)";
2332 linux_nat_ops
.to_attach
= linux_nat_attach
;
2333 linux_nat_ops
.to_detach
= linux_nat_detach
;
2334 linux_nat_ops
.to_resume
= linux_nat_resume
;
2335 linux_nat_ops
.to_wait
= linux_nat_wait
;
2336 /* fetch_inferior_registers and store_inferior_registers will
2337 honor the LWP id, so we can use them directly. */
2338 linux_nat_ops
.to_fetch_registers
= fetch_inferior_registers
;
2339 linux_nat_ops
.to_store_registers
= store_inferior_registers
;
2340 linux_nat_ops
.to_xfer_memory
= linux_nat_xfer_memory
;
2341 linux_nat_ops
.to_kill
= linux_nat_kill
;
2342 linux_nat_ops
.to_create_inferior
= linux_nat_create_inferior
;
2343 linux_nat_ops
.to_mourn_inferior
= linux_nat_mourn_inferior
;
2344 linux_nat_ops
.to_thread_alive
= linux_nat_thread_alive
;
2345 linux_nat_ops
.to_pid_to_str
= linux_nat_pid_to_str
;
2346 linux_nat_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
2347 linux_nat_ops
.to_post_attach
= child_post_attach
;
2348 linux_nat_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
2349 linux_nat_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
2350 linux_nat_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
2352 linux_nat_ops
.to_stratum
= thread_stratum
;
2353 linux_nat_ops
.to_has_thread_control
= tc_schedlock
;
2354 linux_nat_ops
.to_magic
= OPS_MAGIC
;
2358 sigchld_handler (int signo
)
2360 /* Do nothing. The only reason for this handler is that it allows
2361 us to use sigsuspend in linux_nat_wait above to wait for the
2362 arrival of a SIGCHLD. */
2365 /* Accepts an integer PID; Returns a string representing a file that
2366 can be opened to get the symbols for the child process. */
2369 child_pid_to_exec_file (int pid
)
2371 char *name1
, *name2
;
2373 name1
= xmalloc (MAXPATHLEN
);
2374 name2
= xmalloc (MAXPATHLEN
);
2375 make_cleanup (xfree
, name1
);
2376 make_cleanup (xfree
, name2
);
2377 memset (name2
, 0, MAXPATHLEN
);
2379 sprintf (name1
, "/proc/%d/exe", pid
);
2380 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
2386 /* Service function for corefiles and info proc. */
2389 read_mapping (FILE *mapfile
,
2394 char *device
, long long *inode
, char *filename
)
2396 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
2397 addr
, endaddr
, permissions
, offset
, device
, inode
);
2399 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
2401 /* Eat everything up to EOL for the filename. This will prevent
2402 weird filenames (such as one with embedded whitespace) from
2403 confusing this code. It also makes this code more robust in
2404 respect to annotations the kernel may add after the filename.
2406 Note the filename is used for informational purposes
2408 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
2412 filename
[0] = '\0'; /* no filename */
2413 fscanf (mapfile
, "\n");
2415 return (ret
!= 0 && ret
!= EOF
);
2418 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2419 regions in the inferior for a corefile. */
2422 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
2424 int, int, int, void *), void *obfd
)
2426 long long pid
= PIDGET (inferior_ptid
);
2427 char mapsfilename
[MAXPATHLEN
];
2429 long long addr
, endaddr
, size
, offset
, inode
;
2430 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2431 int read
, write
, exec
;
2434 /* Compose the filename for the /proc memory map, and open it. */
2435 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
2436 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
2437 error ("Could not open %s\n", mapsfilename
);
2440 fprintf_filtered (gdb_stdout
,
2441 "Reading memory regions from %s\n", mapsfilename
);
2443 /* Now iterate until end-of-file. */
2444 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
2445 &offset
, &device
[0], &inode
, &filename
[0]))
2447 size
= endaddr
- addr
;
2449 /* Get the segment's permissions. */
2450 read
= (strchr (permissions
, 'r') != 0);
2451 write
= (strchr (permissions
, 'w') != 0);
2452 exec
= (strchr (permissions
, 'x') != 0);
2456 fprintf_filtered (gdb_stdout
,
2457 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2458 size
, paddr_nz (addr
),
2460 write
? 'w' : ' ', exec
? 'x' : ' ');
2461 if (filename
&& filename
[0])
2462 fprintf_filtered (gdb_stdout
, " for %s", filename
);
2463 fprintf_filtered (gdb_stdout
, "\n");
2466 /* Invoke the callback function to create the corefile
2468 func (addr
, size
, read
, write
, exec
, obfd
);
2474 /* Records the thread's register state for the corefile note
2478 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
2479 char *note_data
, int *note_size
)
2481 gdb_gregset_t gregs
;
2482 gdb_fpregset_t fpregs
;
2483 #ifdef FILL_FPXREGSET
2484 gdb_fpxregset_t fpxregs
;
2486 unsigned long lwp
= ptid_get_lwp (ptid
);
2488 fill_gregset (&gregs
, -1);
2489 note_data
= (char *) elfcore_write_prstatus (obfd
,
2493 stop_signal
, &gregs
);
2495 fill_fpregset (&fpregs
, -1);
2496 note_data
= (char *) elfcore_write_prfpreg (obfd
,
2499 &fpregs
, sizeof (fpregs
));
2500 #ifdef FILL_FPXREGSET
2501 fill_fpxregset (&fpxregs
, -1);
2502 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
2505 &fpxregs
, sizeof (fpxregs
));
2510 struct linux_nat_corefile_thread_data
2518 /* Called by gdbthread.c once per thread. Records the thread's
2519 register state for the corefile note section. */
2522 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
2524 struct linux_nat_corefile_thread_data
*args
= data
;
2525 ptid_t saved_ptid
= inferior_ptid
;
2527 inferior_ptid
= ti
->ptid
;
2528 registers_changed ();
2529 target_fetch_registers (-1); /* FIXME should not be necessary;
2530 fill_gregset should do it automatically. */
2531 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
2536 inferior_ptid
= saved_ptid
;
2537 registers_changed ();
2538 target_fetch_registers (-1); /* FIXME should not be necessary;
2539 fill_gregset should do it automatically. */
2543 /* Records the register state for the corefile note section. */
2546 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
2547 char *note_data
, int *note_size
)
2549 registers_changed ();
2550 target_fetch_registers (-1); /* FIXME should not be necessary;
2551 fill_gregset should do it automatically. */
2552 return linux_nat_do_thread_registers (obfd
,
2553 ptid_build (ptid_get_pid (inferior_ptid
),
2554 ptid_get_pid (inferior_ptid
),
2556 note_data
, note_size
);
2560 /* Fills the "to_make_corefile_note" target vector. Builds the note
2561 section for a corefile, and returns it in a malloc buffer. */
2564 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
2566 struct linux_nat_corefile_thread_data thread_args
;
2567 struct cleanup
*old_chain
;
2568 char fname
[16] = { '\0' };
2569 char psargs
[80] = { '\0' };
2570 char *note_data
= NULL
;
2571 ptid_t current_ptid
= inferior_ptid
;
2575 if (get_exec_file (0))
2577 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
2578 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
2579 if (get_inferior_args ())
2581 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
2582 strncat (psargs
, get_inferior_args (),
2583 sizeof (psargs
) - strlen (psargs
));
2585 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
2587 note_size
, fname
, psargs
);
2590 /* Dump information for threads. */
2591 thread_args
.obfd
= obfd
;
2592 thread_args
.note_data
= note_data
;
2593 thread_args
.note_size
= note_size
;
2594 thread_args
.num_notes
= 0;
2595 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
2596 if (thread_args
.num_notes
== 0)
2598 /* iterate_over_threads didn't come up with any threads; just
2599 use inferior_ptid. */
2600 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
2601 note_data
, note_size
);
2605 note_data
= thread_args
.note_data
;
2608 auxv_len
= target_auxv_read (¤t_target
, &auxv
);
2611 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
2612 "CORE", NT_AUXV
, auxv
, auxv_len
);
2616 make_cleanup (xfree
, note_data
);
2620 /* Implement the "info proc" command. */
2623 linux_nat_info_proc_cmd (char *args
, int from_tty
)
2625 long long pid
= PIDGET (inferior_ptid
);
2628 char buffer
[MAXPATHLEN
];
2629 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
2642 /* Break up 'args' into an argv array. */
2643 if ((argv
= buildargv (args
)) == NULL
)
2646 make_cleanup_freeargv (argv
);
2648 while (argv
!= NULL
&& *argv
!= NULL
)
2650 if (isdigit (argv
[0][0]))
2652 pid
= strtoul (argv
[0], NULL
, 10);
2654 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
2658 else if (strcmp (argv
[0], "status") == 0)
2662 else if (strcmp (argv
[0], "stat") == 0)
2666 else if (strcmp (argv
[0], "cmd") == 0)
2670 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
2674 else if (strcmp (argv
[0], "cwd") == 0)
2678 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
2684 /* [...] (future options here) */
2689 error ("No current process: you must name one.");
2691 sprintf (fname1
, "/proc/%lld", pid
);
2692 if (stat (fname1
, &dummy
) != 0)
2693 error ("No /proc directory: '%s'", fname1
);
2695 printf_filtered ("process %lld\n", pid
);
2696 if (cmdline_f
|| all
)
2698 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
2699 if ((procfile
= fopen (fname1
, "r")) > 0)
2701 fgets (buffer
, sizeof (buffer
), procfile
);
2702 printf_filtered ("cmdline = '%s'\n", buffer
);
2706 warning ("unable to open /proc file '%s'", fname1
);
2710 sprintf (fname1
, "/proc/%lld/cwd", pid
);
2711 memset (fname2
, 0, sizeof (fname2
));
2712 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2713 printf_filtered ("cwd = '%s'\n", fname2
);
2715 warning ("unable to read link '%s'", fname1
);
2719 sprintf (fname1
, "/proc/%lld/exe", pid
);
2720 memset (fname2
, 0, sizeof (fname2
));
2721 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
2722 printf_filtered ("exe = '%s'\n", fname2
);
2724 warning ("unable to read link '%s'", fname1
);
2726 if (mappings_f
|| all
)
2728 sprintf (fname1
, "/proc/%lld/maps", pid
);
2729 if ((procfile
= fopen (fname1
, "r")) > 0)
2731 long long addr
, endaddr
, size
, offset
, inode
;
2732 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
2734 printf_filtered ("Mapped address spaces:\n\n");
2735 if (TARGET_ADDR_BIT
== 32)
2737 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2740 " Size", " Offset", "objfile");
2744 printf_filtered (" %18s %18s %10s %10s %7s\n",
2747 " Size", " Offset", "objfile");
2750 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
2751 &offset
, &device
[0], &inode
, &filename
[0]))
2753 size
= endaddr
- addr
;
2755 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
2756 calls here (and possibly above) should be abstracted
2757 out into their own functions? Andrew suggests using
2758 a generic local_address_string instead to print out
2759 the addresses; that makes sense to me, too. */
2761 if (TARGET_ADDR_BIT
== 32)
2763 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
2764 (unsigned long) addr
, /* FIXME: pr_addr */
2765 (unsigned long) endaddr
,
2767 (unsigned int) offset
,
2768 filename
[0] ? filename
: "");
2772 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
2773 (unsigned long) addr
, /* FIXME: pr_addr */
2774 (unsigned long) endaddr
,
2776 (unsigned int) offset
,
2777 filename
[0] ? filename
: "");
2784 warning ("unable to open /proc file '%s'", fname1
);
2786 if (status_f
|| all
)
2788 sprintf (fname1
, "/proc/%lld/status", pid
);
2789 if ((procfile
= fopen (fname1
, "r")) > 0)
2791 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
2792 puts_filtered (buffer
);
2796 warning ("unable to open /proc file '%s'", fname1
);
2800 sprintf (fname1
, "/proc/%lld/stat", pid
);
2801 if ((procfile
= fopen (fname1
, "r")) > 0)
2806 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2807 printf_filtered ("Process: %d\n", itmp
);
2808 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
2809 printf_filtered ("Exec file: %s\n", buffer
);
2810 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
2811 printf_filtered ("State: %c\n", ctmp
);
2812 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2813 printf_filtered ("Parent process: %d\n", itmp
);
2814 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2815 printf_filtered ("Process group: %d\n", itmp
);
2816 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2817 printf_filtered ("Session id: %d\n", itmp
);
2818 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2819 printf_filtered ("TTY: %d\n", itmp
);
2820 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2821 printf_filtered ("TTY owner process group: %d\n", itmp
);
2822 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2823 printf_filtered ("Flags: 0x%x\n", itmp
);
2824 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2825 printf_filtered ("Minor faults (no memory page): %u\n",
2826 (unsigned int) itmp
);
2827 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2828 printf_filtered ("Minor faults, children: %u\n",
2829 (unsigned int) itmp
);
2830 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2831 printf_filtered ("Major faults (memory page faults): %u\n",
2832 (unsigned int) itmp
);
2833 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2834 printf_filtered ("Major faults, children: %u\n",
2835 (unsigned int) itmp
);
2836 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2837 printf_filtered ("utime: %d\n", itmp
);
2838 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2839 printf_filtered ("stime: %d\n", itmp
);
2840 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2841 printf_filtered ("utime, children: %d\n", itmp
);
2842 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2843 printf_filtered ("stime, children: %d\n", itmp
);
2844 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2845 printf_filtered ("jiffies remaining in current time slice: %d\n",
2847 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2848 printf_filtered ("'nice' value: %d\n", itmp
);
2849 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2850 printf_filtered ("jiffies until next timeout: %u\n",
2851 (unsigned int) itmp
);
2852 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2853 printf_filtered ("jiffies until next SIGALRM: %u\n",
2854 (unsigned int) itmp
);
2855 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2856 printf_filtered ("start time (jiffies since system boot): %d\n",
2858 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2859 printf_filtered ("Virtual memory size: %u\n",
2860 (unsigned int) itmp
);
2861 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2862 printf_filtered ("Resident set size: %u\n", (unsigned int) itmp
);
2863 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2864 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
2865 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2866 printf_filtered ("Start of text: 0x%x\n", itmp
);
2867 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2868 printf_filtered ("End of text: 0x%x\n", itmp
);
2869 if (fscanf (procfile
, "%u ", &itmp
) > 0)
2870 printf_filtered ("Start of stack: 0x%x\n", itmp
);
2871 #if 0 /* Don't know how architecture-dependent the rest is...
2872 Anyway the signal bitmap info is available from "status". */
2873 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2874 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp
);
2875 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2876 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp
);
2877 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2878 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp
);
2879 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2880 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp
);
2881 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2882 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp
);
2883 if (fscanf (procfile
, "%d ", &itmp
) > 0)
2884 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp
);
2885 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
2886 printf_filtered ("wchan (system call): 0x%x\n", itmp
);
2891 warning ("unable to open /proc file '%s'", fname1
);
2896 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
2897 struct mem_attrib
*attrib
, struct target_ops
*target
)
2905 /* Don't bother for one word. */
2906 if (len
< 3 * sizeof (long))
2909 /* We could keep this file open and cache it - possibly one per
2910 thread. That requires some juggling, but is even faster. */
2911 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
2912 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2916 /* If pread64 is available, use it. It's faster if the kernel
2917 supports it (only one syscall), and it's 64-bit safe even on
2918 32-bit platforms (for instance, SPARC debugging a SPARC64
2921 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
2923 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
2933 /* Parse LINE as a signal set and add its set bits to SIGS. */
2936 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
2938 int len
= strlen (line
) - 1;
2942 if (line
[len
] != '\n')
2943 error ("Could not parse signal set: %s", line
);
2951 if (*p
>= '0' && *p
<= '9')
2953 else if (*p
>= 'a' && *p
<= 'f')
2954 digit
= *p
- 'a' + 10;
2956 error ("Could not parse signal set: %s", line
);
2961 sigaddset (sigs
, signum
+ 1);
2963 sigaddset (sigs
, signum
+ 2);
2965 sigaddset (sigs
, signum
+ 3);
2967 sigaddset (sigs
, signum
+ 4);
2973 /* Find process PID's pending signals from /proc/pid/status and set
2977 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
2980 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
2983 sigemptyset (pending
);
2984 sigemptyset (blocked
);
2985 sigemptyset (ignored
);
2986 sprintf (fname
, "/proc/%d/status", pid
);
2987 procfile
= fopen (fname
, "r");
2988 if (procfile
== NULL
)
2989 error ("Could not open %s", fname
);
2991 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
2993 /* Normal queued signals are on the SigPnd line in the status
2994 file. However, 2.6 kernels also have a "shared" pending
2995 queue for delivering signals to a thread group, so check for
2998 Unfortunately some Red Hat kernels include the shared pending
2999 queue but not the ShdPnd status field. */
3001 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3002 add_line_to_sigset (buffer
+ 8, pending
);
3003 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3004 add_line_to_sigset (buffer
+ 8, pending
);
3005 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3006 add_line_to_sigset (buffer
+ 8, blocked
);
3007 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3008 add_line_to_sigset (buffer
+ 8, ignored
);
3015 _initialize_linux_nat (void)
3017 struct sigaction action
;
3018 extern void thread_db_init (struct target_ops
*);
3019 extern void inftarg_set_find_memory_regions ();
3020 extern void inftarg_set_make_corefile_notes ();
3022 inftarg_set_find_memory_regions (linux_nat_find_memory_regions
);
3023 inftarg_set_make_corefile_notes (linux_nat_make_corefile_notes
);
3025 add_info ("proc", linux_nat_info_proc_cmd
,
3026 "Show /proc process information about any running process.\n\
3027 Specify any process id, or use the program being debugged by default.\n\
3028 Specify any of the following keywords for detailed info:\n\
3029 mappings -- list of mapped memory regions.\n\
3030 stat -- list a bunch of random process info.\n\
3031 status -- list a different bunch of random process info.\n\
3032 all -- list all available /proc info.");
3034 init_linux_nat_ops ();
3035 add_target (&linux_nat_ops
);
3036 thread_db_init (&linux_nat_ops
);
3038 /* Save the original signal mask. */
3039 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
3041 action
.sa_handler
= sigchld_handler
;
3042 sigemptyset (&action
.sa_mask
);
3043 action
.sa_flags
= 0;
3044 sigaction (SIGCHLD
, &action
, NULL
);
3046 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3047 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
3048 sigdelset (&suspend_mask
, SIGCHLD
);
3050 sigemptyset (&blocked_mask
);
3052 deprecated_add_show_from_set
3053 (add_set_cmd ("lin-lwp", no_class
, var_zinteger
,
3054 (char *) &debug_linux_nat
,
3055 "Set debugging of GNU/Linux lwp module.\n\
3056 Enables printf debugging output.\n", &setdebuglist
), &showdebuglist
);
3060 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3061 the GNU/Linux Threads library and therefore doesn't really belong
3064 /* Read variable NAME in the target and return its value if found.
3065 Otherwise return zero. It is assumed that the type of the variable
3069 get_signo (const char *name
)
3071 struct minimal_symbol
*ms
;
3074 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
3078 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (char *) &signo
,
3079 sizeof (signo
)) != 0)
3085 /* Return the set of signals used by the threads library in *SET. */
3088 lin_thread_get_thread_signals (sigset_t
*set
)
3090 struct sigaction action
;
3091 int restart
, cancel
;
3095 restart
= get_signo ("__pthread_sig_restart");
3099 cancel
= get_signo ("__pthread_sig_cancel");
3103 sigaddset (set
, restart
);
3104 sigaddset (set
, cancel
);
3106 /* The GNU/Linux Threads library makes terminating threads send a
3107 special "cancel" signal instead of SIGCHLD. Make sure we catch
3108 those (to prevent them from terminating GDB itself, which is
3109 likely to be their default action) and treat them the same way as
3112 action
.sa_handler
= sigchld_handler
;
3113 sigemptyset (&action
.sa_mask
);
3114 action
.sa_flags
= 0;
3115 sigaction (cancel
, &action
, NULL
);
3117 /* We block the "cancel" signal throughout this code ... */
3118 sigaddset (&blocked_mask
, cancel
);
3119 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
3121 /* ... except during a sigsuspend. */
3122 sigdelset (&suspend_mask
, cancel
);