1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "linux-fork.h"
34 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
50 #include "event-loop.h"
51 #include "event-top.h"
53 #include <sys/types.h>
54 #include "gdb_dirent.h"
55 #include "xml-support.h"
57 #ifdef HAVE_PERSONALITY
58 # include <sys/personality.h>
59 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
60 # define ADDR_NO_RANDOMIZE 0x0040000
62 #endif /* HAVE_PERSONALITY */
64 /* This comment documents high-level logic of this file.
66 Waiting for events in sync mode
67 ===============================
69 When waiting for an event in a specific thread, we just use waitpid, passing
70 the specific pid, and not passing WNOHANG.
72 When waiting for an event in all threads, waitpid is not quite good. Prior to
73 version 2.4, Linux can either wait for event in main thread, or in secondary
74 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
75 miss an event. The solution is to use non-blocking waitpid, together with
76 sigsuspend. First, we use non-blocking waitpid to get an event in the main
77 process, if any. Second, we use non-blocking waitpid with the __WCLONED
78 flag to check for events in cloned processes. If nothing is found, we use
79 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
80 happened to a child process -- and SIGCHLD will be delivered both for events
81 in main debugged process and in cloned processes. As soon as we know there's
82 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
84 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
85 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
86 blocked, the signal becomes pending and sigsuspend immediately
87 notices it and returns.
89 Waiting for events in async mode
90 ================================
92 In async mode, GDB should always be ready to handle both user input
93 and target events, so neither blocking waitpid nor sigsuspend are
94 viable options. Instead, we should asynchronously notify the GDB main
95 event loop whenever there's an unprocessed event from the target. We
96 detect asynchronous target events by handling SIGCHLD signals. To
97 notify the event loop about target events, the self-pipe trick is used
98 --- a pipe is registered as waitable event source in the event loop,
99 the event loop select/poll's on the read end of this pipe (as well on
100 other event sources, e.g., stdin), and the SIGCHLD handler writes a
101 byte to this pipe. This is more portable than relying on
102 pselect/ppoll, since on kernels that lack those syscalls, libc
103 emulates them with select/poll+sigprocmask, and that is racy
104 (a.k.a. plain broken).
106 Obviously, if we fail to notify the event loop if there's a target
107 event, it's bad. OTOH, if we notify the event loop when there's no
108 event from the target, linux_nat_wait will detect that there's no real
109 event to report, and return event of type TARGET_WAITKIND_IGNORE.
110 This is mostly harmless, but it will waste time and is better avoided.
112 The main design point is that every time GDB is outside linux-nat.c,
113 we have a SIGCHLD handler installed that is called when something
114 happens to the target and notifies the GDB event loop. Whenever GDB
115 core decides to handle the event, and calls into linux-nat.c, we
116 process things as in sync mode, except that the we never block in
119 While processing an event, we may end up momentarily blocked in
120 waitpid calls. Those waitpid calls, while blocking, are guarantied to
121 return quickly. E.g., in all-stop mode, before reporting to the core
122 that an LWP hit a breakpoint, all LWPs are stopped by sending them
123 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
124 Note that this is different from blocking indefinitely waiting for the
125 next event --- here, we're already handling an event.
130 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
131 signal is not entirely significant; we just need for a signal to be delivered,
132 so that we can intercept it. SIGSTOP's advantage is that it can not be
133 blocked. A disadvantage is that it is not a real-time signal, so it can only
134 be queued once; we do not keep track of other sources of SIGSTOP.
136 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
137 use them, because they have special behavior when the signal is generated -
138 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
139 kills the entire thread group.
141 A delivered SIGSTOP would stop the entire thread group, not just the thread we
142 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
143 cancel it (by PTRACE_CONT without passing SIGSTOP).
145 We could use a real-time signal instead. This would solve those problems; we
146 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
147 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
148 generates it, and there are races with trying to find a signal that is not
152 #define O_LARGEFILE 0
155 /* If the system headers did not provide the constants, hard-code the normal
157 #ifndef PTRACE_EVENT_FORK
159 #define PTRACE_SETOPTIONS 0x4200
160 #define PTRACE_GETEVENTMSG 0x4201
162 /* options set using PTRACE_SETOPTIONS */
163 #define PTRACE_O_TRACESYSGOOD 0x00000001
164 #define PTRACE_O_TRACEFORK 0x00000002
165 #define PTRACE_O_TRACEVFORK 0x00000004
166 #define PTRACE_O_TRACECLONE 0x00000008
167 #define PTRACE_O_TRACEEXEC 0x00000010
168 #define PTRACE_O_TRACEVFORKDONE 0x00000020
169 #define PTRACE_O_TRACEEXIT 0x00000040
171 /* Wait extended result codes for the above trace options. */
172 #define PTRACE_EVENT_FORK 1
173 #define PTRACE_EVENT_VFORK 2
174 #define PTRACE_EVENT_CLONE 3
175 #define PTRACE_EVENT_EXEC 4
176 #define PTRACE_EVENT_VFORK_DONE 5
177 #define PTRACE_EVENT_EXIT 6
179 #endif /* PTRACE_EVENT_FORK */
181 /* We can't always assume that this flag is available, but all systems
182 with the ptrace event handlers also have __WALL, so it's safe to use
185 #define __WALL 0x40000000 /* Wait for any child. */
188 #ifndef PTRACE_GETSIGINFO
189 # define PTRACE_GETSIGINFO 0x4202
190 # define PTRACE_SETSIGINFO 0x4203
193 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
194 the use of the multi-threaded target. */
195 static struct target_ops
*linux_ops
;
196 static struct target_ops linux_ops_saved
;
198 /* The method to call, if any, when a new thread is attached. */
199 static void (*linux_nat_new_thread
) (ptid_t
);
201 /* The method to call, if any, when the siginfo object needs to be
202 converted between the layout returned by ptrace, and the layout in
203 the architecture of the inferior. */
204 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
208 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
209 Called by our to_xfer_partial. */
210 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
212 const char *, gdb_byte
*,
216 static int debug_linux_nat
;
218 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
219 struct cmd_list_element
*c
, const char *value
)
221 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
225 static int debug_linux_nat_async
= 0;
227 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
228 struct cmd_list_element
*c
, const char *value
)
230 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
234 static int disable_randomization
= 1;
237 show_disable_randomization (struct ui_file
*file
, int from_tty
,
238 struct cmd_list_element
*c
, const char *value
)
240 #ifdef HAVE_PERSONALITY
241 fprintf_filtered (file
, _("\
242 Disabling randomization of debuggee's virtual address space is %s.\n"),
244 #else /* !HAVE_PERSONALITY */
246 Disabling randomization of debuggee's virtual address space is unsupported on\n\
247 this platform.\n"), file
);
248 #endif /* !HAVE_PERSONALITY */
252 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
254 #ifndef HAVE_PERSONALITY
256 Disabling randomization of debuggee's virtual address space is unsupported on\n\
258 #endif /* !HAVE_PERSONALITY */
261 static int linux_parent_pid
;
263 struct simple_pid_list
267 struct simple_pid_list
*next
;
269 struct simple_pid_list
*stopped_pids
;
271 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
272 can not be used, 1 if it can. */
274 static int linux_supports_tracefork_flag
= -1;
276 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
277 PTRACE_O_TRACEVFORKDONE. */
279 static int linux_supports_tracevforkdone_flag
= -1;
281 /* Async mode support */
283 /* Zero if the async mode, although enabled, is masked, which means
284 linux_nat_wait should behave as if async mode was off. */
285 static int linux_nat_async_mask_value
= 1;
287 /* The read/write ends of the pipe registered as waitable file in the
289 static int linux_nat_event_pipe
[2] = { -1, -1 };
291 /* Flush the event pipe. */
294 async_file_flush (void)
301 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
303 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
306 /* Put something (anything, doesn't matter what, or how much) in event
307 pipe, so that the select/poll in the event-loop realizes we have
308 something to process. */
311 async_file_mark (void)
315 /* It doesn't really matter what the pipe contains, as long we end
316 up with something in it. Might as well flush the previous
322 ret
= write (linux_nat_event_pipe
[1], "+", 1);
324 while (ret
== -1 && errno
== EINTR
);
326 /* Ignore EAGAIN. If the pipe is full, the event loop will already
327 be awakened anyway. */
330 static void linux_nat_async (void (*callback
)
331 (enum inferior_event_type event_type
, void *context
),
333 static int linux_nat_async_mask (int mask
);
334 static int kill_lwp (int lwpid
, int signo
);
336 static int stop_callback (struct lwp_info
*lp
, void *data
);
338 static void block_child_signals (sigset_t
*prev_mask
);
339 static void restore_child_signals_mask (sigset_t
*prev_mask
);
341 /* Trivial list manipulation functions to keep track of a list of
342 new stopped processes. */
344 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
346 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
348 new_pid
->status
= status
;
349 new_pid
->next
= *listp
;
354 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
356 struct simple_pid_list
**p
;
358 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
359 if ((*p
)->pid
== pid
)
361 struct simple_pid_list
*next
= (*p
)->next
;
362 *status
= (*p
)->status
;
371 linux_record_stopped_pid (int pid
, int status
)
373 add_to_pid_list (&stopped_pids
, pid
, status
);
377 /* A helper function for linux_test_for_tracefork, called after fork (). */
380 linux_tracefork_child (void)
384 ptrace (PTRACE_TRACEME
, 0, 0, 0);
385 kill (getpid (), SIGSTOP
);
390 /* Wrapper function for waitpid which handles EINTR. */
393 my_waitpid (int pid
, int *status
, int flags
)
399 ret
= waitpid (pid
, status
, flags
);
401 while (ret
== -1 && errno
== EINTR
);
406 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
408 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
409 we know that the feature is not available. This may change the tracing
410 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
412 However, if it succeeds, we don't know for sure that the feature is
413 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
414 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
415 fork tracing, and let it fork. If the process exits, we assume that we
416 can't use TRACEFORK; if we get the fork notification, and we can extract
417 the new child's PID, then we assume that we can. */
420 linux_test_for_tracefork (int original_pid
)
422 int child_pid
, ret
, status
;
426 /* We don't want those ptrace calls to be interrupted. */
427 block_child_signals (&prev_mask
);
429 linux_supports_tracefork_flag
= 0;
430 linux_supports_tracevforkdone_flag
= 0;
432 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
435 restore_child_signals_mask (&prev_mask
);
441 perror_with_name (("fork"));
444 linux_tracefork_child ();
446 ret
= my_waitpid (child_pid
, &status
, 0);
448 perror_with_name (("waitpid"));
449 else if (ret
!= child_pid
)
450 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
451 if (! WIFSTOPPED (status
))
452 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
454 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
457 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
460 warning (_("linux_test_for_tracefork: failed to kill child"));
461 restore_child_signals_mask (&prev_mask
);
465 ret
= my_waitpid (child_pid
, &status
, 0);
466 if (ret
!= child_pid
)
467 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
468 else if (!WIFSIGNALED (status
))
469 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
470 "killed child"), status
);
472 restore_child_signals_mask (&prev_mask
);
476 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
477 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
478 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
479 linux_supports_tracevforkdone_flag
= (ret
== 0);
481 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
483 warning (_("linux_test_for_tracefork: failed to resume child"));
485 ret
= my_waitpid (child_pid
, &status
, 0);
487 if (ret
== child_pid
&& WIFSTOPPED (status
)
488 && status
>> 16 == PTRACE_EVENT_FORK
)
491 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
492 if (ret
== 0 && second_pid
!= 0)
496 linux_supports_tracefork_flag
= 1;
497 my_waitpid (second_pid
, &second_status
, 0);
498 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
500 warning (_("linux_test_for_tracefork: failed to kill second child"));
501 my_waitpid (second_pid
, &status
, 0);
505 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
506 "(%d, status 0x%x)"), ret
, status
);
508 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
510 warning (_("linux_test_for_tracefork: failed to kill child"));
511 my_waitpid (child_pid
, &status
, 0);
513 restore_child_signals_mask (&prev_mask
);
516 /* Return non-zero iff we have tracefork functionality available.
517 This function also sets linux_supports_tracefork_flag. */
520 linux_supports_tracefork (int pid
)
522 if (linux_supports_tracefork_flag
== -1)
523 linux_test_for_tracefork (pid
);
524 return linux_supports_tracefork_flag
;
528 linux_supports_tracevforkdone (int pid
)
530 if (linux_supports_tracefork_flag
== -1)
531 linux_test_for_tracefork (pid
);
532 return linux_supports_tracevforkdone_flag
;
537 linux_enable_event_reporting (ptid_t ptid
)
539 int pid
= ptid_get_lwp (ptid
);
543 pid
= ptid_get_pid (ptid
);
545 if (! linux_supports_tracefork (pid
))
548 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
549 | PTRACE_O_TRACECLONE
;
550 if (linux_supports_tracevforkdone (pid
))
551 options
|= PTRACE_O_TRACEVFORKDONE
;
553 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
554 read-only process state. */
556 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
560 linux_child_post_attach (int pid
)
562 linux_enable_event_reporting (pid_to_ptid (pid
));
563 check_for_thread_db ();
567 linux_child_post_startup_inferior (ptid_t ptid
)
569 linux_enable_event_reporting (ptid
);
570 check_for_thread_db ();
574 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
578 struct target_waitstatus last_status
;
580 int parent_pid
, child_pid
;
582 block_child_signals (&prev_mask
);
584 get_last_target_status (&last_ptid
, &last_status
);
585 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
586 parent_pid
= ptid_get_lwp (last_ptid
);
588 parent_pid
= ptid_get_pid (last_ptid
);
589 child_pid
= PIDGET (last_status
.value
.related_pid
);
593 /* We're already attached to the parent, by default. */
595 /* Before detaching from the child, remove all breakpoints from
596 it. If we forked, then this has already been taken care of
597 by infrun.c. If we vforked however, any breakpoint inserted
598 in the parent is visible in the child, even those added while
599 stopped in a vfork catchpoint. This won't actually modify
600 the breakpoint list, but will physically remove the
601 breakpoints from the child. This will remove the breakpoints
602 from the parent also, but they'll be reinserted below. */
604 detach_breakpoints (child_pid
);
606 /* Detach new forked process? */
609 if (info_verbose
|| debug_linux_nat
)
611 target_terminal_ours ();
612 fprintf_filtered (gdb_stdlog
,
613 "Detaching after fork from child process %d.\n",
617 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
621 struct fork_info
*fp
;
622 struct inferior
*parent_inf
, *child_inf
;
624 /* Add process to GDB's tables. */
625 child_inf
= add_inferior (child_pid
);
627 parent_inf
= find_inferior_pid (GET_PID (last_ptid
));
628 child_inf
->attach_flag
= parent_inf
->attach_flag
;
630 /* Retain child fork in ptrace (stopped) state. */
631 fp
= find_fork_pid (child_pid
);
633 fp
= add_fork (child_pid
);
634 fork_save_infrun_state (fp
, 0);
639 gdb_assert (linux_supports_tracefork_flag
>= 0);
640 if (linux_supports_tracevforkdone (0))
644 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
645 my_waitpid (parent_pid
, &status
, __WALL
);
646 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
647 warning (_("Unexpected waitpid result %06x when waiting for "
648 "vfork-done"), status
);
652 /* We can't insert breakpoints until the child has
653 finished with the shared memory region. We need to
654 wait until that happens. Ideal would be to just
656 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
657 - waitpid (parent_pid, &status, __WALL);
658 However, most architectures can't handle a syscall
659 being traced on the way out if it wasn't traced on
662 We might also think to loop, continuing the child
663 until it exits or gets a SIGTRAP. One problem is
664 that the child might call ptrace with PTRACE_TRACEME.
666 There's no simple and reliable way to figure out when
667 the vforked child will be done with its copy of the
668 shared memory. We could step it out of the syscall,
669 two instructions, let it go, and then single-step the
670 parent once. When we have hardware single-step, this
671 would work; with software single-step it could still
672 be made to work but we'd have to be able to insert
673 single-step breakpoints in the child, and we'd have
674 to insert -just- the single-step breakpoint in the
675 parent. Very awkward.
677 In the end, the best we can do is to make sure it
678 runs for a little while. Hopefully it will be out of
679 range of any breakpoints we reinsert. Usually this
680 is only the single-step breakpoint at vfork's return
686 /* Since we vforked, breakpoints were removed in the parent
687 too. Put them back. */
688 reattach_breakpoints (parent_pid
);
693 struct thread_info
*last_tp
= find_thread_pid (last_ptid
);
694 struct thread_info
*tp
;
695 char child_pid_spelling
[40];
696 struct inferior
*parent_inf
, *child_inf
;
698 /* Copy user stepping state to the new inferior thread. */
699 struct breakpoint
*step_resume_breakpoint
= last_tp
->step_resume_breakpoint
;
700 CORE_ADDR step_range_start
= last_tp
->step_range_start
;
701 CORE_ADDR step_range_end
= last_tp
->step_range_end
;
702 struct frame_id step_frame_id
= last_tp
->step_frame_id
;
704 /* Otherwise, deleting the parent would get rid of this
706 last_tp
->step_resume_breakpoint
= NULL
;
708 /* Before detaching from the parent, remove all breakpoints from it. */
709 remove_breakpoints ();
711 if (info_verbose
|| debug_linux_nat
)
713 target_terminal_ours ();
714 fprintf_filtered (gdb_stdlog
,
715 "Attaching after fork to child process %d.\n",
719 /* Add the new inferior first, so that the target_detach below
720 doesn't unpush the target. */
722 child_inf
= add_inferior (child_pid
);
724 parent_inf
= find_inferior_pid (GET_PID (last_ptid
));
725 child_inf
->attach_flag
= parent_inf
->attach_flag
;
727 /* If we're vforking, we may want to hold on to the parent until
728 the child exits or execs. At exec time we can remove the old
729 breakpoints from the parent and detach it; at exit time we
730 could do the same (or even, sneakily, resume debugging it - the
731 child's exec has failed, or something similar).
733 This doesn't clean up "properly", because we can't call
734 target_detach, but that's OK; if the current target is "child",
735 then it doesn't need any further cleanups, and lin_lwp will
736 generally not encounter vfork (vfork is defined to fork
739 The holding part is very easy if we have VFORKDONE events;
740 but keeping track of both processes is beyond GDB at the
741 moment. So we don't expose the parent to the rest of GDB.
742 Instead we quietly hold onto it until such time as we can
747 linux_parent_pid
= parent_pid
;
748 detach_inferior (parent_pid
);
750 else if (!detach_fork
)
752 struct fork_info
*fp
;
753 /* Retain parent fork in ptrace (stopped) state. */
754 fp
= find_fork_pid (parent_pid
);
756 fp
= add_fork (parent_pid
);
757 fork_save_infrun_state (fp
, 0);
759 /* Also add an entry for the child fork. */
760 fp
= find_fork_pid (child_pid
);
762 fp
= add_fork (child_pid
);
763 fork_save_infrun_state (fp
, 0);
766 target_detach (NULL
, 0);
768 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
770 linux_nat_switch_fork (inferior_ptid
);
771 check_for_thread_db ();
773 tp
= inferior_thread ();
774 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
775 tp
->step_range_start
= step_range_start
;
776 tp
->step_range_end
= step_range_end
;
777 tp
->step_frame_id
= step_frame_id
;
779 /* Reset breakpoints in the child as appropriate. */
780 follow_inferior_reset_breakpoints ();
783 restore_child_signals_mask (&prev_mask
);
789 linux_child_insert_fork_catchpoint (int pid
)
791 if (! linux_supports_tracefork (pid
))
792 error (_("Your system does not support fork catchpoints."));
796 linux_child_insert_vfork_catchpoint (int pid
)
798 if (!linux_supports_tracefork (pid
))
799 error (_("Your system does not support vfork catchpoints."));
803 linux_child_insert_exec_catchpoint (int pid
)
805 if (!linux_supports_tracefork (pid
))
806 error (_("Your system does not support exec catchpoints."));
809 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
810 are processes sharing the same VM space. A multi-threaded process
811 is basically a group of such processes. However, such a grouping
812 is almost entirely a user-space issue; the kernel doesn't enforce
813 such a grouping at all (this might change in the future). In
814 general, we'll rely on the threads library (i.e. the GNU/Linux
815 Threads library) to provide such a grouping.
817 It is perfectly well possible to write a multi-threaded application
818 without the assistance of a threads library, by using the clone
819 system call directly. This module should be able to give some
820 rudimentary support for debugging such applications if developers
821 specify the CLONE_PTRACE flag in the clone system call, and are
822 using the Linux kernel 2.4 or above.
824 Note that there are some peculiarities in GNU/Linux that affect
827 - In general one should specify the __WCLONE flag to waitpid in
828 order to make it report events for any of the cloned processes
829 (and leave it out for the initial process). However, if a cloned
830 process has exited the exit status is only reported if the
831 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
832 we cannot use it since GDB must work on older systems too.
834 - When a traced, cloned process exits and is waited for by the
835 debugger, the kernel reassigns it to the original parent and
836 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
837 library doesn't notice this, which leads to the "zombie problem":
838 When debugged a multi-threaded process that spawns a lot of
839 threads will run out of processes, even if the threads exit,
840 because the "zombies" stay around. */
842 /* List of known LWPs. */
843 struct lwp_info
*lwp_list
;
846 /* Original signal mask. */
847 static sigset_t normal_mask
;
849 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
850 _initialize_linux_nat. */
851 static sigset_t suspend_mask
;
853 /* Signals to block to make that sigsuspend work. */
854 static sigset_t blocked_mask
;
856 /* SIGCHLD action. */
857 struct sigaction sigchld_action
;
859 /* Block child signals (SIGCHLD and linux threads signals), and store
860 the previous mask in PREV_MASK. */
863 block_child_signals (sigset_t
*prev_mask
)
865 /* Make sure SIGCHLD is blocked. */
866 if (!sigismember (&blocked_mask
, SIGCHLD
))
867 sigaddset (&blocked_mask
, SIGCHLD
);
869 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
872 /* Restore child signals mask, previously returned by
873 block_child_signals. */
876 restore_child_signals_mask (sigset_t
*prev_mask
)
878 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
882 /* Prototypes for local functions. */
883 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
884 static int linux_thread_alive (ptid_t ptid
);
885 static char *linux_child_pid_to_exec_file (int pid
);
886 static int cancel_breakpoint (struct lwp_info
*lp
);
889 /* Convert wait status STATUS to a string. Used for printing debug
893 status_to_str (int status
)
897 if (WIFSTOPPED (status
))
898 snprintf (buf
, sizeof (buf
), "%s (stopped)",
899 strsignal (WSTOPSIG (status
)));
900 else if (WIFSIGNALED (status
))
901 snprintf (buf
, sizeof (buf
), "%s (terminated)",
902 strsignal (WSTOPSIG (status
)));
904 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
909 /* Initialize the list of LWPs. Note that this module, contrary to
910 what GDB's generic threads layer does for its thread list,
911 re-initializes the LWP lists whenever we mourn or detach (which
912 doesn't involve mourning) the inferior. */
917 struct lwp_info
*lp
, *lpnext
;
919 for (lp
= lwp_list
; lp
; lp
= lpnext
)
928 /* Remove all LWPs belong to PID from the lwp list. */
931 purge_lwp_list (int pid
)
933 struct lwp_info
*lp
, *lpprev
, *lpnext
;
937 for (lp
= lwp_list
; lp
; lp
= lpnext
)
941 if (ptid_get_pid (lp
->ptid
) == pid
)
946 lpprev
->next
= lp
->next
;
955 /* Return the number of known LWPs in the tgid given by PID. */
963 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
964 if (ptid_get_pid (lp
->ptid
) == pid
)
970 /* Add the LWP specified by PID to the list. Return a pointer to the
971 structure describing the new LWP. The LWP should already be stopped
972 (with an exception for the very first LWP). */
974 static struct lwp_info
*
975 add_lwp (ptid_t ptid
)
979 gdb_assert (is_lwp (ptid
));
981 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
983 memset (lp
, 0, sizeof (struct lwp_info
));
985 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
992 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
993 linux_nat_new_thread (ptid
);
998 /* Remove the LWP specified by PID from the list. */
1001 delete_lwp (ptid_t ptid
)
1003 struct lwp_info
*lp
, *lpprev
;
1007 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1008 if (ptid_equal (lp
->ptid
, ptid
))
1015 lpprev
->next
= lp
->next
;
1017 lwp_list
= lp
->next
;
1022 /* Return a pointer to the structure describing the LWP corresponding
1023 to PID. If no corresponding LWP could be found, return NULL. */
1025 static struct lwp_info
*
1026 find_lwp_pid (ptid_t ptid
)
1028 struct lwp_info
*lp
;
1032 lwp
= GET_LWP (ptid
);
1034 lwp
= GET_PID (ptid
);
1036 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1037 if (lwp
== GET_LWP (lp
->ptid
))
1043 /* Returns true if PTID matches filter FILTER. FILTER can be the wild
1044 card MINUS_ONE_PTID (all ptid match it); can be a ptid representing
1045 a process (ptid_is_pid returns true), in which case, all lwps of
1046 that give process match, lwps of other process do not; or, it can
1047 represent a specific thread, in which case, only that thread will
1048 match true. PTID must represent an LWP, it can never be a wild
1052 ptid_match (ptid_t ptid
, ptid_t filter
)
1054 /* Since both parameters have the same type, prevent easy mistakes
1056 gdb_assert (!ptid_equal (ptid
, minus_one_ptid
)
1057 && !ptid_equal (ptid
, null_ptid
));
1059 if (ptid_equal (filter
, minus_one_ptid
))
1061 if (ptid_is_pid (filter
)
1062 && ptid_get_pid (ptid
) == ptid_get_pid (filter
))
1064 else if (ptid_equal (ptid
, filter
))
1070 /* Call CALLBACK with its second argument set to DATA for every LWP in
1071 the list. If CALLBACK returns 1 for a particular LWP, return a
1072 pointer to the structure describing that LWP immediately.
1073 Otherwise return NULL. */
1076 iterate_over_lwps (ptid_t filter
,
1077 int (*callback
) (struct lwp_info
*, void *),
1080 struct lwp_info
*lp
, *lpnext
;
1082 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1086 if (ptid_match (lp
->ptid
, filter
))
1088 if ((*callback
) (lp
, data
))
1096 /* Update our internal state when changing from one fork (checkpoint,
1097 et cetera) to another indicated by NEW_PTID. We can only switch
1098 single-threaded applications, so we only create one new LWP, and
1099 the previous list is discarded. */
1102 linux_nat_switch_fork (ptid_t new_ptid
)
1104 struct lwp_info
*lp
;
1107 lp
= add_lwp (new_ptid
);
1110 init_thread_list ();
1111 add_thread_silent (new_ptid
);
1114 /* Handle the exit of a single thread LP. */
1117 exit_lwp (struct lwp_info
*lp
)
1119 struct thread_info
*th
= find_thread_pid (lp
->ptid
);
1123 if (print_thread_events
)
1124 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1126 delete_thread (lp
->ptid
);
1129 delete_lwp (lp
->ptid
);
1132 /* Return an lwp's tgid, found in `/proc/PID/status'. */
1135 linux_proc_get_tgid (int lwpid
)
1141 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
1142 status_file
= fopen (buf
, "r");
1143 if (status_file
!= NULL
)
1145 while (fgets (buf
, sizeof (buf
), status_file
))
1147 if (strncmp (buf
, "Tgid:", 5) == 0)
1149 tgid
= strtoul (buf
+ strlen ("Tgid:"), NULL
, 10);
1154 fclose (status_file
);
1160 /* Detect `T (stopped)' in `/proc/PID/status'.
1161 Other states including `T (tracing stop)' are reported as false. */
1164 pid_is_stopped (pid_t pid
)
1170 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1171 status_file
= fopen (buf
, "r");
1172 if (status_file
!= NULL
)
1176 while (fgets (buf
, sizeof (buf
), status_file
))
1178 if (strncmp (buf
, "State:", 6) == 0)
1184 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1186 fclose (status_file
);
1191 /* Wait for the LWP specified by LP, which we have just attached to.
1192 Returns a wait status for that LWP, to cache. */
1195 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1198 pid_t new_pid
, pid
= GET_LWP (ptid
);
1201 if (pid_is_stopped (pid
))
1203 if (debug_linux_nat
)
1204 fprintf_unfiltered (gdb_stdlog
,
1205 "LNPAW: Attaching to a stopped process\n");
1207 /* The process is definitely stopped. It is in a job control
1208 stop, unless the kernel predates the TASK_STOPPED /
1209 TASK_TRACED distinction, in which case it might be in a
1210 ptrace stop. Make sure it is in a ptrace stop; from there we
1211 can kill it, signal it, et cetera.
1213 First make sure there is a pending SIGSTOP. Since we are
1214 already attached, the process can not transition from stopped
1215 to running without a PTRACE_CONT; so we know this signal will
1216 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1217 probably already in the queue (unless this kernel is old
1218 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1219 is not an RT signal, it can only be queued once. */
1220 kill_lwp (pid
, SIGSTOP
);
1222 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1223 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1224 ptrace (PTRACE_CONT
, pid
, 0, 0);
1227 /* Make sure the initial process is stopped. The user-level threads
1228 layer might want to poke around in the inferior, and that won't
1229 work if things haven't stabilized yet. */
1230 new_pid
= my_waitpid (pid
, &status
, 0);
1231 if (new_pid
== -1 && errno
== ECHILD
)
1234 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1236 /* Try again with __WCLONE to check cloned processes. */
1237 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1241 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1243 if (WSTOPSIG (status
) != SIGSTOP
)
1246 if (debug_linux_nat
)
1247 fprintf_unfiltered (gdb_stdlog
,
1248 "LNPAW: Received %s after attaching\n",
1249 status_to_str (status
));
1255 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1256 if the new LWP could not be attached. */
1259 lin_lwp_attach_lwp (ptid_t ptid
)
1261 struct lwp_info
*lp
;
1264 gdb_assert (is_lwp (ptid
));
1266 block_child_signals (&prev_mask
);
1268 lp
= find_lwp_pid (ptid
);
1270 /* We assume that we're already attached to any LWP that has an id
1271 equal to the overall process id, and to any LWP that is already
1272 in our list of LWPs. If we're not seeing exit events from threads
1273 and we've had PID wraparound since we last tried to stop all threads,
1274 this assumption might be wrong; fortunately, this is very unlikely
1276 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1278 int status
, cloned
= 0, signalled
= 0;
1280 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1282 /* If we fail to attach to the thread, issue a warning,
1283 but continue. One way this can happen is if thread
1284 creation is interrupted; as of Linux kernel 2.6.19, a
1285 bug may place threads in the thread list and then fail
1287 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1288 safe_strerror (errno
));
1289 restore_child_signals_mask (&prev_mask
);
1293 if (debug_linux_nat
)
1294 fprintf_unfiltered (gdb_stdlog
,
1295 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1296 target_pid_to_str (ptid
));
1298 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1299 lp
= add_lwp (ptid
);
1301 lp
->cloned
= cloned
;
1302 lp
->signalled
= signalled
;
1303 if (WSTOPSIG (status
) != SIGSTOP
)
1306 lp
->status
= status
;
1309 target_post_attach (GET_LWP (lp
->ptid
));
1311 if (debug_linux_nat
)
1313 fprintf_unfiltered (gdb_stdlog
,
1314 "LLAL: waitpid %s received %s\n",
1315 target_pid_to_str (ptid
),
1316 status_to_str (status
));
1321 /* We assume that the LWP representing the original process is
1322 already stopped. Mark it as stopped in the data structure
1323 that the GNU/linux ptrace layer uses to keep track of
1324 threads. Note that this won't have already been done since
1325 the main thread will have, we assume, been stopped by an
1326 attach from a different layer. */
1328 lp
= add_lwp (ptid
);
1332 restore_child_signals_mask (&prev_mask
);
1337 linux_nat_create_inferior (struct target_ops
*ops
,
1338 char *exec_file
, char *allargs
, char **env
,
1341 int saved_async
= 0;
1342 #ifdef HAVE_PERSONALITY
1343 int personality_orig
= 0, personality_set
= 0;
1344 #endif /* HAVE_PERSONALITY */
1346 /* The fork_child mechanism is synchronous and calls target_wait, so
1347 we have to mask the async mode. */
1349 if (target_can_async_p ())
1350 /* Mask async mode. Creating a child requires a loop calling
1351 wait_for_inferior currently. */
1352 saved_async
= linux_nat_async_mask (0);
1354 #ifdef HAVE_PERSONALITY
1355 if (disable_randomization
)
1358 personality_orig
= personality (0xffffffff);
1359 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1361 personality_set
= 1;
1362 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1364 if (errno
!= 0 || (personality_set
1365 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1366 warning (_("Error disabling address space randomization: %s"),
1367 safe_strerror (errno
));
1369 #endif /* HAVE_PERSONALITY */
1371 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1373 #ifdef HAVE_PERSONALITY
1374 if (personality_set
)
1377 personality (personality_orig
);
1379 warning (_("Error restoring address space randomization: %s"),
1380 safe_strerror (errno
));
1382 #endif /* HAVE_PERSONALITY */
1385 linux_nat_async_mask (saved_async
);
1389 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1391 struct lwp_info
*lp
;
1395 linux_ops
->to_attach (ops
, args
, from_tty
);
1397 /* The ptrace base target adds the main thread with (pid,0,0)
1398 format. Decorate it with lwp info. */
1399 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1400 thread_change_ptid (inferior_ptid
, ptid
);
1402 /* Add the initial process as the first LWP to the list. */
1403 lp
= add_lwp (ptid
);
1405 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1409 /* Save the wait status to report later. */
1411 if (debug_linux_nat
)
1412 fprintf_unfiltered (gdb_stdlog
,
1413 "LNA: waitpid %ld, saving status %s\n",
1414 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1416 lp
->status
= status
;
1418 if (target_can_async_p ())
1419 target_async (inferior_event_handler
, 0);
1422 /* Get pending status of LP. */
1424 get_pending_status (struct lwp_info
*lp
, int *status
)
1426 struct target_waitstatus last
;
1429 get_last_target_status (&last_ptid
, &last
);
1431 /* If this lwp is the ptid that GDB is processing an event from, the
1432 signal will be in stop_signal. Otherwise, we may cache pending
1433 events in lp->status while trying to stop all threads (see
1434 stop_wait_callback). */
1440 enum target_signal signo
= TARGET_SIGNAL_0
;
1442 if (is_executing (lp
->ptid
))
1444 /* If the core thought this lwp was executing --- e.g., the
1445 executing property hasn't been updated yet, but the
1446 thread has been stopped with a stop_callback /
1447 stop_wait_callback sequence (see linux_nat_detach for
1448 example) --- we can only have pending events in the local
1450 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1454 /* If the core knows the thread is not executing, then we
1455 have the last signal recorded in
1456 thread_info->stop_signal. */
1458 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1459 signo
= tp
->stop_signal
;
1462 if (signo
!= TARGET_SIGNAL_0
1463 && !signal_pass_state (signo
))
1465 if (debug_linux_nat
)
1466 fprintf_unfiltered (gdb_stdlog
, "\
1467 GPT: lwp %s had signal %s, but it is in no pass state\n",
1468 target_pid_to_str (lp
->ptid
),
1469 target_signal_to_string (signo
));
1473 if (signo
!= TARGET_SIGNAL_0
)
1474 *status
= W_STOPCODE (target_signal_to_host (signo
));
1476 if (debug_linux_nat
)
1477 fprintf_unfiltered (gdb_stdlog
,
1478 "GPT: lwp %s as pending signal %s\n",
1479 target_pid_to_str (lp
->ptid
),
1480 target_signal_to_string (signo
));
1485 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1487 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1488 if (tp
->stop_signal
!= TARGET_SIGNAL_0
1489 && signal_pass_state (tp
->stop_signal
))
1490 *status
= W_STOPCODE (target_signal_to_host (tp
->stop_signal
));
1493 *status
= lp
->status
;
1500 detach_callback (struct lwp_info
*lp
, void *data
)
1502 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1504 if (debug_linux_nat
&& lp
->status
)
1505 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1506 strsignal (WSTOPSIG (lp
->status
)),
1507 target_pid_to_str (lp
->ptid
));
1509 /* If there is a pending SIGSTOP, get rid of it. */
1512 if (debug_linux_nat
)
1513 fprintf_unfiltered (gdb_stdlog
,
1514 "DC: Sending SIGCONT to %s\n",
1515 target_pid_to_str (lp
->ptid
));
1517 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1521 /* We don't actually detach from the LWP that has an id equal to the
1522 overall process id just yet. */
1523 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1527 /* Pass on any pending signal for this LWP. */
1528 get_pending_status (lp
, &status
);
1531 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1532 WSTOPSIG (status
)) < 0)
1533 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1534 safe_strerror (errno
));
1536 if (debug_linux_nat
)
1537 fprintf_unfiltered (gdb_stdlog
,
1538 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1539 target_pid_to_str (lp
->ptid
),
1540 strsignal (WSTOPSIG (status
)));
1542 delete_lwp (lp
->ptid
);
1549 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1553 enum target_signal sig
;
1554 struct lwp_info
*main_lwp
;
1556 pid
= GET_PID (inferior_ptid
);
1558 if (target_can_async_p ())
1559 linux_nat_async (NULL
, 0);
1561 /* Stop all threads before detaching. ptrace requires that the
1562 thread is stopped to sucessfully detach. */
1563 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1564 /* ... and wait until all of them have reported back that
1565 they're no longer running. */
1566 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1568 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1570 /* Only the initial process should be left right now. */
1571 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1573 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1575 /* Pass on any pending signal for the last LWP. */
1576 if ((args
== NULL
|| *args
== '\0')
1577 && get_pending_status (main_lwp
, &status
) != -1
1578 && WIFSTOPPED (status
))
1580 /* Put the signal number in ARGS so that inf_ptrace_detach will
1581 pass it along with PTRACE_DETACH. */
1583 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1584 fprintf_unfiltered (gdb_stdlog
,
1585 "LND: Sending signal %s to %s\n",
1587 target_pid_to_str (main_lwp
->ptid
));
1590 delete_lwp (main_lwp
->ptid
);
1592 if (forks_exist_p ())
1594 /* Multi-fork case. The current inferior_ptid is being detached
1595 from, but there are other viable forks to debug. Detach from
1596 the current fork, and context-switch to the first
1598 linux_fork_detach (args
, from_tty
);
1600 if (non_stop
&& target_can_async_p ())
1601 target_async (inferior_event_handler
, 0);
1604 linux_ops
->to_detach (ops
, args
, from_tty
);
1610 resume_callback (struct lwp_info
*lp
, void *data
)
1612 if (lp
->stopped
&& lp
->status
== 0)
1614 if (debug_linux_nat
)
1615 fprintf_unfiltered (gdb_stdlog
,
1616 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1617 target_pid_to_str (lp
->ptid
));
1619 linux_ops
->to_resume (linux_ops
,
1620 pid_to_ptid (GET_LWP (lp
->ptid
)),
1621 0, TARGET_SIGNAL_0
);
1622 if (debug_linux_nat
)
1623 fprintf_unfiltered (gdb_stdlog
,
1624 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1625 target_pid_to_str (lp
->ptid
));
1628 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1630 else if (lp
->stopped
&& debug_linux_nat
)
1631 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1632 target_pid_to_str (lp
->ptid
));
1633 else if (debug_linux_nat
)
1634 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1635 target_pid_to_str (lp
->ptid
));
1641 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1648 resume_set_callback (struct lwp_info
*lp
, void *data
)
1655 linux_nat_resume (struct target_ops
*ops
,
1656 ptid_t ptid
, int step
, enum target_signal signo
)
1659 struct lwp_info
*lp
;
1662 if (debug_linux_nat
)
1663 fprintf_unfiltered (gdb_stdlog
,
1664 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1665 step
? "step" : "resume",
1666 target_pid_to_str (ptid
),
1667 signo
? strsignal (signo
) : "0",
1668 target_pid_to_str (inferior_ptid
));
1670 block_child_signals (&prev_mask
);
1672 /* A specific PTID means `step only this process id'. */
1673 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1674 || ptid_is_pid (ptid
));
1678 /* Mark the lwps we're resuming as resumed. */
1679 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
1680 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1683 iterate_over_lwps (minus_one_ptid
, resume_set_callback
, NULL
);
1685 /* See if it's the current inferior that should be handled
1688 lp
= find_lwp_pid (inferior_ptid
);
1690 lp
= find_lwp_pid (ptid
);
1691 gdb_assert (lp
!= NULL
);
1693 /* Remember if we're stepping. */
1696 /* If we have a pending wait status for this thread, there is no
1697 point in resuming the process. But first make sure that
1698 linux_nat_wait won't preemptively handle the event - we
1699 should never take this short-circuit if we are going to
1700 leave LP running, since we have skipped resuming all the
1701 other threads. This bit of code needs to be synchronized
1702 with linux_nat_wait. */
1704 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1707 struct inferior
*inf
;
1709 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
1711 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1713 /* Defer to common code if we're gaining control of the
1715 if (inf
->stop_soon
== NO_STOP_QUIETLY
1716 && signal_stop_state (saved_signo
) == 0
1717 && signal_print_state (saved_signo
) == 0
1718 && signal_pass_state (saved_signo
) == 1)
1720 if (debug_linux_nat
)
1721 fprintf_unfiltered (gdb_stdlog
,
1722 "LLR: Not short circuiting for ignored "
1723 "status 0x%x\n", lp
->status
);
1725 /* FIXME: What should we do if we are supposed to continue
1726 this thread with a signal? */
1727 gdb_assert (signo
== TARGET_SIGNAL_0
);
1728 signo
= saved_signo
;
1735 /* FIXME: What should we do if we are supposed to continue
1736 this thread with a signal? */
1737 gdb_assert (signo
== TARGET_SIGNAL_0
);
1739 if (debug_linux_nat
)
1740 fprintf_unfiltered (gdb_stdlog
,
1741 "LLR: Short circuiting for status 0x%x\n",
1744 restore_child_signals_mask (&prev_mask
);
1745 if (target_can_async_p ())
1747 target_async (inferior_event_handler
, 0);
1748 /* Tell the event loop we have something to process. */
1754 /* Mark LWP as not stopped to prevent it from being continued by
1759 iterate_over_lwps (ptid
, resume_callback
, NULL
);
1761 /* Convert to something the lower layer understands. */
1762 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1764 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
1765 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1767 if (debug_linux_nat
)
1768 fprintf_unfiltered (gdb_stdlog
,
1769 "LLR: %s %s, %s (resume event thread)\n",
1770 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1771 target_pid_to_str (ptid
),
1772 signo
? strsignal (signo
) : "0");
1774 restore_child_signals_mask (&prev_mask
);
1775 if (target_can_async_p ())
1776 target_async (inferior_event_handler
, 0);
1779 /* Issue kill to specified lwp. */
1781 static int tkill_failed
;
1784 kill_lwp (int lwpid
, int signo
)
1788 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1789 fails, then we are not using nptl threads and we should be using kill. */
1791 #ifdef HAVE_TKILL_SYSCALL
1794 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1795 if (errno
!= ENOSYS
)
1802 return kill (lwpid
, signo
);
1805 /* Handle a GNU/Linux extended wait response. If we see a clone
1806 event, we need to add the new LWP to our list (and not report the
1807 trap to higher layers). This function returns non-zero if the
1808 event should be ignored and we should wait again. If STOPPING is
1809 true, the new LWP remains stopped, otherwise it is continued. */
1812 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1815 int pid
= GET_LWP (lp
->ptid
);
1816 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1817 struct lwp_info
*new_lp
= NULL
;
1818 int event
= status
>> 16;
1820 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1821 || event
== PTRACE_EVENT_CLONE
)
1823 unsigned long new_pid
;
1826 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1828 /* If we haven't already seen the new PID stop, wait for it now. */
1829 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1831 /* The new child has a pending SIGSTOP. We can't affect it until it
1832 hits the SIGSTOP, but we're already attached. */
1833 ret
= my_waitpid (new_pid
, &status
,
1834 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1836 perror_with_name (_("waiting for new child"));
1837 else if (ret
!= new_pid
)
1838 internal_error (__FILE__
, __LINE__
,
1839 _("wait returned unexpected PID %d"), ret
);
1840 else if (!WIFSTOPPED (status
))
1841 internal_error (__FILE__
, __LINE__
,
1842 _("wait returned unexpected status 0x%x"), status
);
1845 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1847 if (event
== PTRACE_EVENT_FORK
)
1848 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1849 else if (event
== PTRACE_EVENT_VFORK
)
1850 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1853 struct cleanup
*old_chain
;
1855 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1856 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
1858 new_lp
->stopped
= 1;
1860 if (WSTOPSIG (status
) != SIGSTOP
)
1862 /* This can happen if someone starts sending signals to
1863 the new thread before it gets a chance to run, which
1864 have a lower number than SIGSTOP (e.g. SIGUSR1).
1865 This is an unlikely case, and harder to handle for
1866 fork / vfork than for clone, so we do not try - but
1867 we handle it for clone events here. We'll send
1868 the other signal on to the thread below. */
1870 new_lp
->signalled
= 1;
1877 /* Add the new thread to GDB's lists as soon as possible
1880 1) the frontend doesn't have to wait for a stop to
1883 2) we tag it with the correct running state. */
1885 /* If the thread_db layer is active, let it know about
1886 this new thread, and add it to GDB's list. */
1887 if (!thread_db_attach_lwp (new_lp
->ptid
))
1889 /* We're not using thread_db. Add it to GDB's
1891 target_post_attach (GET_LWP (new_lp
->ptid
));
1892 add_thread (new_lp
->ptid
);
1897 set_running (new_lp
->ptid
, 1);
1898 set_executing (new_lp
->ptid
, 1);
1904 new_lp
->stopped
= 0;
1905 new_lp
->resumed
= 1;
1906 ptrace (PTRACE_CONT
, new_pid
, 0,
1907 status
? WSTOPSIG (status
) : 0);
1910 if (debug_linux_nat
)
1911 fprintf_unfiltered (gdb_stdlog
,
1912 "LHEW: Got clone event from LWP %ld, resuming\n",
1913 GET_LWP (lp
->ptid
));
1914 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1922 if (event
== PTRACE_EVENT_EXEC
)
1924 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1925 ourstatus
->value
.execd_pathname
1926 = xstrdup (linux_child_pid_to_exec_file (pid
));
1928 if (linux_parent_pid
)
1930 detach_breakpoints (linux_parent_pid
);
1931 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1933 linux_parent_pid
= 0;
1936 /* At this point, all inserted breakpoints are gone. Doing this
1937 as soon as we detect an exec prevents the badness of deleting
1938 a breakpoint writing the current "shadow contents" to lift
1939 the bp. That shadow is NOT valid after an exec.
1941 Note that we have to do this after the detach_breakpoints
1942 call above, otherwise breakpoints wouldn't be lifted from the
1943 parent on a vfork, because detach_breakpoints would think
1944 that breakpoints are not inserted. */
1945 mark_breakpoints_out ();
1949 internal_error (__FILE__
, __LINE__
,
1950 _("unknown ptrace event %d"), event
);
1953 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1957 wait_lwp (struct lwp_info
*lp
)
1961 int thread_dead
= 0;
1963 gdb_assert (!lp
->stopped
);
1964 gdb_assert (lp
->status
== 0);
1966 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1967 if (pid
== -1 && errno
== ECHILD
)
1969 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1970 if (pid
== -1 && errno
== ECHILD
)
1972 /* The thread has previously exited. We need to delete it
1973 now because, for some vendor 2.4 kernels with NPTL
1974 support backported, there won't be an exit event unless
1975 it is the main thread. 2.6 kernels will report an exit
1976 event for each thread that exits, as expected. */
1978 if (debug_linux_nat
)
1979 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1980 target_pid_to_str (lp
->ptid
));
1986 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1988 if (debug_linux_nat
)
1990 fprintf_unfiltered (gdb_stdlog
,
1991 "WL: waitpid %s received %s\n",
1992 target_pid_to_str (lp
->ptid
),
1993 status_to_str (status
));
1997 /* Check if the thread has exited. */
1998 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2001 if (debug_linux_nat
)
2002 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2003 target_pid_to_str (lp
->ptid
));
2012 gdb_assert (WIFSTOPPED (status
));
2014 /* Handle GNU/Linux's extended waitstatus for trace events. */
2015 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2017 if (debug_linux_nat
)
2018 fprintf_unfiltered (gdb_stdlog
,
2019 "WL: Handling extended status 0x%06x\n",
2021 if (linux_handle_extended_wait (lp
, status
, 1))
2022 return wait_lwp (lp
);
2028 /* Save the most recent siginfo for LP. This is currently only called
2029 for SIGTRAP; some ports use the si_addr field for
2030 target_stopped_data_address. In the future, it may also be used to
2031 restore the siginfo of requeued signals. */
2034 save_siginfo (struct lwp_info
*lp
)
2037 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2038 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2041 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2044 /* Send a SIGSTOP to LP. */
2047 stop_callback (struct lwp_info
*lp
, void *data
)
2049 if (!lp
->stopped
&& !lp
->signalled
)
2053 if (debug_linux_nat
)
2055 fprintf_unfiltered (gdb_stdlog
,
2056 "SC: kill %s **<SIGSTOP>**\n",
2057 target_pid_to_str (lp
->ptid
));
2060 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2061 if (debug_linux_nat
)
2063 fprintf_unfiltered (gdb_stdlog
,
2064 "SC: lwp kill %d %s\n",
2066 errno
? safe_strerror (errno
) : "ERRNO-OK");
2070 gdb_assert (lp
->status
== 0);
2076 /* Return non-zero if LWP PID has a pending SIGINT. */
2079 linux_nat_has_pending_sigint (int pid
)
2081 sigset_t pending
, blocked
, ignored
;
2084 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2086 if (sigismember (&pending
, SIGINT
)
2087 && !sigismember (&ignored
, SIGINT
))
2093 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2096 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2098 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2099 flag to consume the next one. */
2100 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2101 && WSTOPSIG (lp
->status
) == SIGINT
)
2104 lp
->ignore_sigint
= 1;
2109 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2110 This function is called after we know the LWP has stopped; if the LWP
2111 stopped before the expected SIGINT was delivered, then it will never have
2112 arrived. Also, if the signal was delivered to a shared queue and consumed
2113 by a different thread, it will never be delivered to this LWP. */
2116 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2118 if (!lp
->ignore_sigint
)
2121 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2123 if (debug_linux_nat
)
2124 fprintf_unfiltered (gdb_stdlog
,
2125 "MCIS: Clearing bogus flag for %s\n",
2126 target_pid_to_str (lp
->ptid
));
2127 lp
->ignore_sigint
= 0;
2131 /* Wait until LP is stopped. */
2134 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2140 status
= wait_lwp (lp
);
2144 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2145 && WSTOPSIG (status
) == SIGINT
)
2147 lp
->ignore_sigint
= 0;
2150 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2151 if (debug_linux_nat
)
2152 fprintf_unfiltered (gdb_stdlog
,
2153 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2154 target_pid_to_str (lp
->ptid
),
2155 errno
? safe_strerror (errno
) : "OK");
2157 return stop_wait_callback (lp
, NULL
);
2160 maybe_clear_ignore_sigint (lp
);
2162 if (WSTOPSIG (status
) != SIGSTOP
)
2164 if (WSTOPSIG (status
) == SIGTRAP
)
2166 /* If a LWP other than the LWP that we're reporting an
2167 event for has hit a GDB breakpoint (as opposed to
2168 some random trap signal), then just arrange for it to
2169 hit it again later. We don't keep the SIGTRAP status
2170 and don't forward the SIGTRAP signal to the LWP. We
2171 will handle the current event, eventually we will
2172 resume all LWPs, and this one will get its breakpoint
2175 If we do not do this, then we run the risk that the
2176 user will delete or disable the breakpoint, but the
2177 thread will have already tripped on it. */
2179 /* Save the trap's siginfo in case we need it later. */
2182 /* Now resume this LWP and get the SIGSTOP event. */
2184 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2185 if (debug_linux_nat
)
2187 fprintf_unfiltered (gdb_stdlog
,
2188 "PTRACE_CONT %s, 0, 0 (%s)\n",
2189 target_pid_to_str (lp
->ptid
),
2190 errno
? safe_strerror (errno
) : "OK");
2192 fprintf_unfiltered (gdb_stdlog
,
2193 "SWC: Candidate SIGTRAP event in %s\n",
2194 target_pid_to_str (lp
->ptid
));
2196 /* Hold this event/waitstatus while we check to see if
2197 there are any more (we still want to get that SIGSTOP). */
2198 stop_wait_callback (lp
, NULL
);
2200 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2201 there's another event, throw it back into the
2205 if (debug_linux_nat
)
2206 fprintf_unfiltered (gdb_stdlog
,
2207 "SWC: kill %s, %s\n",
2208 target_pid_to_str (lp
->ptid
),
2209 status_to_str ((int) status
));
2210 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2213 /* Save the sigtrap event. */
2214 lp
->status
= status
;
2219 /* The thread was stopped with a signal other than
2220 SIGSTOP, and didn't accidentally trip a breakpoint. */
2222 if (debug_linux_nat
)
2224 fprintf_unfiltered (gdb_stdlog
,
2225 "SWC: Pending event %s in %s\n",
2226 status_to_str ((int) status
),
2227 target_pid_to_str (lp
->ptid
));
2229 /* Now resume this LWP and get the SIGSTOP event. */
2231 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2232 if (debug_linux_nat
)
2233 fprintf_unfiltered (gdb_stdlog
,
2234 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2235 target_pid_to_str (lp
->ptid
),
2236 errno
? safe_strerror (errno
) : "OK");
2238 /* Hold this event/waitstatus while we check to see if
2239 there are any more (we still want to get that SIGSTOP). */
2240 stop_wait_callback (lp
, NULL
);
2242 /* If the lp->status field is still empty, use it to
2243 hold this event. If not, then this event must be
2244 returned to the event queue of the LWP. */
2247 if (debug_linux_nat
)
2249 fprintf_unfiltered (gdb_stdlog
,
2250 "SWC: kill %s, %s\n",
2251 target_pid_to_str (lp
->ptid
),
2252 status_to_str ((int) status
));
2254 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2257 lp
->status
= status
;
2263 /* We caught the SIGSTOP that we intended to catch, so
2264 there's no SIGSTOP pending. */
2273 /* Return non-zero if LP has a wait status pending. */
2276 status_callback (struct lwp_info
*lp
, void *data
)
2278 /* Only report a pending wait status if we pretend that this has
2279 indeed been resumed. */
2280 /* We check for lp->waitstatus in addition to lp->status, because we
2281 can have pending process exits recorded in lp->waitstatus, and
2282 W_EXITCODE(0,0) == 0. */
2283 return ((lp
->status
!= 0
2284 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2288 /* Return non-zero if LP isn't stopped. */
2291 running_callback (struct lwp_info
*lp
, void *data
)
2293 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2296 /* Count the LWP's that have had events. */
2299 count_events_callback (struct lwp_info
*lp
, void *data
)
2303 gdb_assert (count
!= NULL
);
2305 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2306 if (lp
->status
!= 0 && lp
->resumed
2307 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2313 /* Select the LWP (if any) that is currently being single-stepped. */
2316 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2318 if (lp
->step
&& lp
->status
!= 0)
2324 /* Select the Nth LWP that has had a SIGTRAP event. */
2327 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2329 int *selector
= data
;
2331 gdb_assert (selector
!= NULL
);
2333 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2334 if (lp
->status
!= 0 && lp
->resumed
2335 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2336 if ((*selector
)-- == 0)
2343 cancel_breakpoint (struct lwp_info
*lp
)
2345 /* Arrange for a breakpoint to be hit again later. We don't keep
2346 the SIGTRAP status and don't forward the SIGTRAP signal to the
2347 LWP. We will handle the current event, eventually we will resume
2348 this LWP, and this breakpoint will trap again.
2350 If we do not do this, then we run the risk that the user will
2351 delete or disable the breakpoint, but the LWP will have already
2354 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2355 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2358 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2359 if (breakpoint_inserted_here_p (pc
))
2361 if (debug_linux_nat
)
2362 fprintf_unfiltered (gdb_stdlog
,
2363 "CB: Push back breakpoint for %s\n",
2364 target_pid_to_str (lp
->ptid
));
2366 /* Back up the PC if necessary. */
2367 if (gdbarch_decr_pc_after_break (gdbarch
))
2368 regcache_write_pc (regcache
, pc
);
2376 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2378 struct lwp_info
*event_lp
= data
;
2380 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2384 /* If a LWP other than the LWP that we're reporting an event for has
2385 hit a GDB breakpoint (as opposed to some random trap signal),
2386 then just arrange for it to hit it again later. We don't keep
2387 the SIGTRAP status and don't forward the SIGTRAP signal to the
2388 LWP. We will handle the current event, eventually we will resume
2389 all LWPs, and this one will get its breakpoint trap again.
2391 If we do not do this, then we run the risk that the user will
2392 delete or disable the breakpoint, but the LWP will have already
2396 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2397 && cancel_breakpoint (lp
))
2398 /* Throw away the SIGTRAP. */
2404 /* Select one LWP out of those that have events pending. */
2407 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2410 int random_selector
;
2411 struct lwp_info
*event_lp
;
2413 /* Record the wait status for the original LWP. */
2414 (*orig_lp
)->status
= *status
;
2416 /* Give preference to any LWP that is being single-stepped. */
2417 event_lp
= iterate_over_lwps (filter
,
2418 select_singlestep_lwp_callback
, NULL
);
2419 if (event_lp
!= NULL
)
2421 if (debug_linux_nat
)
2422 fprintf_unfiltered (gdb_stdlog
,
2423 "SEL: Select single-step %s\n",
2424 target_pid_to_str (event_lp
->ptid
));
2428 /* No single-stepping LWP. Select one at random, out of those
2429 which have had SIGTRAP events. */
2431 /* First see how many SIGTRAP events we have. */
2432 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2434 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2435 random_selector
= (int)
2436 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2438 if (debug_linux_nat
&& num_events
> 1)
2439 fprintf_unfiltered (gdb_stdlog
,
2440 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2441 num_events
, random_selector
);
2443 event_lp
= iterate_over_lwps (filter
,
2444 select_event_lwp_callback
,
2448 if (event_lp
!= NULL
)
2450 /* Switch the event LWP. */
2451 *orig_lp
= event_lp
;
2452 *status
= event_lp
->status
;
2455 /* Flush the wait status for the event LWP. */
2456 (*orig_lp
)->status
= 0;
2459 /* Return non-zero if LP has been resumed. */
2462 resumed_callback (struct lwp_info
*lp
, void *data
)
2467 /* Stop an active thread, verify it still exists, then resume it. */
2470 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2472 struct lwp_info
*ptr
;
2474 if (!lp
->stopped
&& !lp
->signalled
)
2476 stop_callback (lp
, NULL
);
2477 stop_wait_callback (lp
, NULL
);
2478 /* Resume if the lwp still exists. */
2479 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2482 resume_callback (lp
, NULL
);
2483 resume_set_callback (lp
, NULL
);
2489 /* Check if we should go on and pass this event to common code.
2490 Return the affected lwp if we are, or NULL otherwise. */
2491 static struct lwp_info
*
2492 linux_nat_filter_event (int lwpid
, int status
, int options
)
2494 struct lwp_info
*lp
;
2496 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2498 /* Check for stop events reported by a process we didn't already
2499 know about - anything not already in our LWP list.
2501 If we're expecting to receive stopped processes after
2502 fork, vfork, and clone events, then we'll just add the
2503 new one to our list and go back to waiting for the event
2504 to be reported - the stopped process might be returned
2505 from waitpid before or after the event is. */
2506 if (WIFSTOPPED (status
) && !lp
)
2508 linux_record_stopped_pid (lwpid
, status
);
2512 /* Make sure we don't report an event for the exit of an LWP not in
2513 our list, i.e. not part of the current process. This can happen
2514 if we detach from a program we original forked and then it
2516 if (!WIFSTOPPED (status
) && !lp
)
2519 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2520 CLONE_PTRACE processes which do not use the thread library -
2521 otherwise we wouldn't find the new LWP this way. That doesn't
2522 currently work, and the following code is currently unreachable
2523 due to the two blocks above. If it's fixed some day, this code
2524 should be broken out into a function so that we can also pick up
2525 LWPs from the new interface. */
2528 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2529 if (options
& __WCLONE
)
2532 gdb_assert (WIFSTOPPED (status
)
2533 && WSTOPSIG (status
) == SIGSTOP
);
2536 if (!in_thread_list (inferior_ptid
))
2538 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2539 GET_PID (inferior_ptid
));
2540 add_thread (inferior_ptid
);
2543 add_thread (lp
->ptid
);
2546 /* Save the trap's siginfo in case we need it later. */
2547 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2550 /* Handle GNU/Linux's extended waitstatus for trace events. */
2551 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2553 if (debug_linux_nat
)
2554 fprintf_unfiltered (gdb_stdlog
,
2555 "LLW: Handling extended status 0x%06x\n",
2557 if (linux_handle_extended_wait (lp
, status
, 0))
2561 /* Check if the thread has exited. */
2562 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
2563 && num_lwps (GET_PID (lp
->ptid
)) > 1)
2565 /* If this is the main thread, we must stop all threads and verify
2566 if they are still alive. This is because in the nptl thread model
2567 on Linux 2.4, there is no signal issued for exiting LWPs
2568 other than the main thread. We only get the main thread exit
2569 signal once all child threads have already exited. If we
2570 stop all the threads and use the stop_wait_callback to check
2571 if they have exited we can determine whether this signal
2572 should be ignored or whether it means the end of the debugged
2573 application, regardless of which threading model is being
2575 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2578 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
2579 stop_and_resume_callback
, NULL
);
2582 if (debug_linux_nat
)
2583 fprintf_unfiltered (gdb_stdlog
,
2584 "LLW: %s exited.\n",
2585 target_pid_to_str (lp
->ptid
));
2587 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
2589 /* If there is at least one more LWP, then the exit signal
2590 was not the end of the debugged application and should be
2597 /* Check if the current LWP has previously exited. In the nptl
2598 thread model, LWPs other than the main thread do not issue
2599 signals when they exit so we must check whenever the thread has
2600 stopped. A similar check is made in stop_wait_callback(). */
2601 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
2603 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
2605 if (debug_linux_nat
)
2606 fprintf_unfiltered (gdb_stdlog
,
2607 "LLW: %s exited.\n",
2608 target_pid_to_str (lp
->ptid
));
2612 /* Make sure there is at least one thread running. */
2613 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
2615 /* Discard the event. */
2619 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2620 an attempt to stop an LWP. */
2622 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2624 if (debug_linux_nat
)
2625 fprintf_unfiltered (gdb_stdlog
,
2626 "LLW: Delayed SIGSTOP caught for %s.\n",
2627 target_pid_to_str (lp
->ptid
));
2629 /* This is a delayed SIGSTOP. */
2632 registers_changed ();
2634 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2635 lp
->step
, TARGET_SIGNAL_0
);
2636 if (debug_linux_nat
)
2637 fprintf_unfiltered (gdb_stdlog
,
2638 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2640 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2641 target_pid_to_str (lp
->ptid
));
2644 gdb_assert (lp
->resumed
);
2646 /* Discard the event. */
2650 /* Make sure we don't report a SIGINT that we have already displayed
2651 for another thread. */
2652 if (lp
->ignore_sigint
2653 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2655 if (debug_linux_nat
)
2656 fprintf_unfiltered (gdb_stdlog
,
2657 "LLW: Delayed SIGINT caught for %s.\n",
2658 target_pid_to_str (lp
->ptid
));
2660 /* This is a delayed SIGINT. */
2661 lp
->ignore_sigint
= 0;
2663 registers_changed ();
2664 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2665 lp
->step
, TARGET_SIGNAL_0
);
2666 if (debug_linux_nat
)
2667 fprintf_unfiltered (gdb_stdlog
,
2668 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2670 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2671 target_pid_to_str (lp
->ptid
));
2674 gdb_assert (lp
->resumed
);
2676 /* Discard the event. */
2680 /* An interesting event. */
2686 linux_nat_wait_1 (struct target_ops
*ops
,
2687 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2689 static sigset_t prev_mask
;
2690 struct lwp_info
*lp
= NULL
;
2695 if (debug_linux_nat_async
)
2696 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2698 /* The first time we get here after starting a new inferior, we may
2699 not have added it to the LWP list yet - this is the earliest
2700 moment at which we know its PID. */
2701 if (ptid_is_pid (inferior_ptid
))
2703 /* Upgrade the main thread's ptid. */
2704 thread_change_ptid (inferior_ptid
,
2705 BUILD_LWP (GET_PID (inferior_ptid
),
2706 GET_PID (inferior_ptid
)));
2708 lp
= add_lwp (inferior_ptid
);
2712 /* Make sure SIGCHLD is blocked. */
2713 block_child_signals (&prev_mask
);
2715 if (ptid_equal (ptid
, minus_one_ptid
))
2717 else if (ptid_is_pid (ptid
))
2718 /* A request to wait for a specific tgid. This is not possible
2719 with waitpid, so instead, we wait for any child, and leave
2720 children we're not interested in right now with a pending
2721 status to report later. */
2724 pid
= GET_LWP (ptid
);
2730 /* Make sure there is at least one LWP that has been resumed. */
2731 gdb_assert (iterate_over_lwps (ptid
, resumed_callback
, NULL
));
2733 /* First check if there is a LWP with a wait status pending. */
2736 /* Any LWP that's been resumed will do. */
2737 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
2740 status
= lp
->status
;
2743 if (debug_linux_nat
&& status
)
2744 fprintf_unfiltered (gdb_stdlog
,
2745 "LLW: Using pending wait status %s for %s.\n",
2746 status_to_str (status
),
2747 target_pid_to_str (lp
->ptid
));
2750 /* But if we don't find one, we'll have to wait, and check both
2751 cloned and uncloned processes. We start with the cloned
2753 options
= __WCLONE
| WNOHANG
;
2755 else if (is_lwp (ptid
))
2757 if (debug_linux_nat
)
2758 fprintf_unfiltered (gdb_stdlog
,
2759 "LLW: Waiting for specific LWP %s.\n",
2760 target_pid_to_str (ptid
));
2762 /* We have a specific LWP to check. */
2763 lp
= find_lwp_pid (ptid
);
2765 status
= lp
->status
;
2768 if (debug_linux_nat
&& status
)
2769 fprintf_unfiltered (gdb_stdlog
,
2770 "LLW: Using pending wait status %s for %s.\n",
2771 status_to_str (status
),
2772 target_pid_to_str (lp
->ptid
));
2774 /* If we have to wait, take into account whether PID is a cloned
2775 process or not. And we have to convert it to something that
2776 the layer beneath us can understand. */
2777 options
= lp
->cloned
? __WCLONE
: 0;
2778 pid
= GET_LWP (ptid
);
2780 /* We check for lp->waitstatus in addition to lp->status,
2781 because we can have pending process exits recorded in
2782 lp->status and W_EXITCODE(0,0) == 0. We should probably have
2783 an additional lp->status_p flag. */
2784 if (status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
2788 if (lp
&& lp
->signalled
)
2790 /* A pending SIGSTOP may interfere with the normal stream of
2791 events. In a typical case where interference is a problem,
2792 we have a SIGSTOP signal pending for LWP A while
2793 single-stepping it, encounter an event in LWP B, and take the
2794 pending SIGSTOP while trying to stop LWP A. After processing
2795 the event in LWP B, LWP A is continued, and we'll never see
2796 the SIGTRAP associated with the last time we were
2797 single-stepping LWP A. */
2799 /* Resume the thread. It should halt immediately returning the
2801 registers_changed ();
2802 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2803 lp
->step
, TARGET_SIGNAL_0
);
2804 if (debug_linux_nat
)
2805 fprintf_unfiltered (gdb_stdlog
,
2806 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2807 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2808 target_pid_to_str (lp
->ptid
));
2810 gdb_assert (lp
->resumed
);
2812 /* This should catch the pending SIGSTOP. */
2813 stop_wait_callback (lp
, NULL
);
2816 if (!target_can_async_p ())
2818 /* Causes SIGINT to be passed on to the attached process. */
2822 if (target_can_async_p ())
2823 options
|= WNOHANG
; /* In async mode, don't block. */
2829 lwpid
= my_waitpid (pid
, &status
, options
);
2833 gdb_assert (pid
== -1 || lwpid
== pid
);
2835 if (debug_linux_nat
)
2837 fprintf_unfiltered (gdb_stdlog
,
2838 "LLW: waitpid %ld received %s\n",
2839 (long) lwpid
, status_to_str (status
));
2842 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2845 && ptid_is_pid (ptid
)
2846 && ptid_get_pid (lp
->ptid
) != ptid_get_pid (ptid
))
2848 if (debug_linux_nat
)
2849 fprintf (stderr
, "LWP %ld got an event %06x, leaving pending.\n",
2850 ptid_get_lwp (lp
->ptid
), status
);
2852 if (WIFSTOPPED (status
))
2854 if (WSTOPSIG (status
) != SIGSTOP
)
2856 lp
->status
= status
;
2858 stop_callback (lp
, NULL
);
2860 /* Resume in order to collect the sigstop. */
2861 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2863 stop_wait_callback (lp
, NULL
);
2871 else if (WIFEXITED (status
) || WIFSIGNALED (status
))
2873 if (debug_linux_nat
)
2874 fprintf (stderr
, "Process %ld exited while stopping LWPs\n",
2875 ptid_get_lwp (lp
->ptid
));
2877 /* This was the last lwp in the process. Since
2878 events are serialized to GDB core, and we can't
2879 report this one right now, but GDB core and the
2880 other target layers will want to be notified
2881 about the exit code/signal, leave the status
2882 pending for the next time we're able to report
2884 lp
->status
= status
;
2886 /* Prevent trying to stop this thread again. We'll
2887 never try to resume it because it has a pending
2891 /* Dead LWP's aren't expected to reported a pending
2895 /* Store the pending event in the waitstatus as
2896 well, because W_EXITCODE(0,0) == 0. */
2897 store_waitstatus (&lp
->waitstatus
, status
);
2911 /* waitpid did return something. Restart over. */
2912 options
|= __WCLONE
;
2920 /* Alternate between checking cloned and uncloned processes. */
2921 options
^= __WCLONE
;
2923 /* And every time we have checked both:
2924 In async mode, return to event loop;
2925 In sync mode, suspend waiting for a SIGCHLD signal. */
2926 if (options
& __WCLONE
)
2928 if (target_can_async_p ())
2930 /* No interesting event. */
2931 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2933 if (debug_linux_nat_async
)
2934 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2936 restore_child_signals_mask (&prev_mask
);
2937 return minus_one_ptid
;
2940 sigsuspend (&suspend_mask
);
2944 /* We shouldn't end up here unless we want to try again. */
2945 gdb_assert (lp
== NULL
);
2948 if (!target_can_async_p ())
2949 clear_sigint_trap ();
2953 /* Don't report signals that GDB isn't interested in, such as
2954 signals that are neither printed nor stopped upon. Stopping all
2955 threads can be a bit time-consuming so if we want decent
2956 performance with heavily multi-threaded programs, especially when
2957 they're using a high frequency timer, we'd better avoid it if we
2960 if (WIFSTOPPED (status
))
2962 int signo
= target_signal_from_host (WSTOPSIG (status
));
2963 struct inferior
*inf
;
2965 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2968 /* Defer to common code if we get a signal while
2969 single-stepping, since that may need special care, e.g. to
2970 skip the signal handler, or, if we're gaining control of the
2973 && inf
->stop_soon
== NO_STOP_QUIETLY
2974 && signal_stop_state (signo
) == 0
2975 && signal_print_state (signo
) == 0
2976 && signal_pass_state (signo
) == 1)
2978 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2979 here? It is not clear we should. GDB may not expect
2980 other threads to run. On the other hand, not resuming
2981 newly attached threads may cause an unwanted delay in
2982 getting them running. */
2983 registers_changed ();
2984 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2986 if (debug_linux_nat
)
2987 fprintf_unfiltered (gdb_stdlog
,
2988 "LLW: %s %s, %s (preempt 'handle')\n",
2990 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2991 target_pid_to_str (lp
->ptid
),
2992 signo
? strsignal (signo
) : "0");
2999 /* Only do the below in all-stop, as we currently use SIGINT
3000 to implement target_stop (see linux_nat_stop) in
3002 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3004 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3005 forwarded to the entire process group, that is, all LWPs
3006 will receive it - unless they're using CLONE_THREAD to
3007 share signals. Since we only want to report it once, we
3008 mark it as ignored for all LWPs except this one. */
3009 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3010 set_ignore_sigint
, NULL
);
3011 lp
->ignore_sigint
= 0;
3014 maybe_clear_ignore_sigint (lp
);
3018 /* This LWP is stopped now. */
3021 if (debug_linux_nat
)
3022 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3023 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3027 /* Now stop all other LWP's ... */
3028 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3030 /* ... and wait until all of them have reported back that
3031 they're no longer running. */
3032 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3034 /* If we're not waiting for a specific LWP, choose an event LWP
3035 from among those that have had events. Giving equal priority
3036 to all LWPs that have had events helps prevent
3039 select_event_lwp (ptid
, &lp
, &status
);
3042 /* Now that we've selected our final event LWP, cancel any
3043 breakpoints in other LWPs that have hit a GDB breakpoint. See
3044 the comment in cancel_breakpoints_callback to find out why. */
3045 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3047 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3049 if (debug_linux_nat
)
3050 fprintf_unfiltered (gdb_stdlog
,
3051 "LLW: trap ptid is %s.\n",
3052 target_pid_to_str (lp
->ptid
));
3055 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3057 *ourstatus
= lp
->waitstatus
;
3058 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3061 store_waitstatus (ourstatus
, status
);
3063 if (debug_linux_nat_async
)
3064 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3066 restore_child_signals_mask (&prev_mask
);
3071 linux_nat_wait (struct target_ops
*ops
,
3072 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
3076 if (debug_linux_nat
)
3077 fprintf_unfiltered (gdb_stdlog
, "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
3079 /* Flush the async file first. */
3080 if (target_can_async_p ())
3081 async_file_flush ();
3083 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
);
3085 /* If we requested any event, and something came out, assume there
3086 may be more. If we requested a specific lwp or process, also
3087 assume there may be more. */
3088 if (target_can_async_p ()
3089 && (ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3090 || !ptid_equal (ptid
, minus_one_ptid
)))
3093 /* Get ready for the next event. */
3094 if (target_can_async_p ())
3095 target_async (inferior_event_handler
, 0);
3101 kill_callback (struct lwp_info
*lp
, void *data
)
3104 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3105 if (debug_linux_nat
)
3106 fprintf_unfiltered (gdb_stdlog
,
3107 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3108 target_pid_to_str (lp
->ptid
),
3109 errno
? safe_strerror (errno
) : "OK");
3115 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3119 /* We must make sure that there are no pending events (delayed
3120 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3121 program doesn't interfere with any following debugging session. */
3123 /* For cloned processes we must check both with __WCLONE and
3124 without, since the exit status of a cloned process isn't reported
3130 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3131 if (pid
!= (pid_t
) -1)
3133 if (debug_linux_nat
)
3134 fprintf_unfiltered (gdb_stdlog
,
3135 "KWC: wait %s received unknown.\n",
3136 target_pid_to_str (lp
->ptid
));
3137 /* The Linux kernel sometimes fails to kill a thread
3138 completely after PTRACE_KILL; that goes from the stop
3139 point in do_fork out to the one in
3140 get_signal_to_deliever and waits again. So kill it
3142 kill_callback (lp
, NULL
);
3145 while (pid
== GET_LWP (lp
->ptid
));
3147 gdb_assert (pid
== -1 && errno
== ECHILD
);
3152 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3153 if (pid
!= (pid_t
) -1)
3155 if (debug_linux_nat
)
3156 fprintf_unfiltered (gdb_stdlog
,
3157 "KWC: wait %s received unk.\n",
3158 target_pid_to_str (lp
->ptid
));
3159 /* See the call to kill_callback above. */
3160 kill_callback (lp
, NULL
);
3163 while (pid
== GET_LWP (lp
->ptid
));
3165 gdb_assert (pid
== -1 && errno
== ECHILD
);
3170 linux_nat_kill (struct target_ops
*ops
)
3172 struct target_waitstatus last
;
3176 /* If we're stopped while forking and we haven't followed yet,
3177 kill the other task. We need to do this first because the
3178 parent will be sleeping if this is a vfork. */
3180 get_last_target_status (&last_ptid
, &last
);
3182 if (last
.kind
== TARGET_WAITKIND_FORKED
3183 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3185 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3189 if (forks_exist_p ())
3190 linux_fork_killall ();
3193 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3194 /* Stop all threads before killing them, since ptrace requires
3195 that the thread is stopped to sucessfully PTRACE_KILL. */
3196 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3197 /* ... and wait until all of them have reported back that
3198 they're no longer running. */
3199 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3201 /* Kill all LWP's ... */
3202 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3204 /* ... and wait until we've flushed all events. */
3205 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3208 target_mourn_inferior ();
3212 linux_nat_mourn_inferior (struct target_ops
*ops
)
3214 purge_lwp_list (ptid_get_pid (inferior_ptid
));
3216 if (! forks_exist_p ())
3217 /* Normal case, no other forks available. */
3218 linux_ops
->to_mourn_inferior (ops
);
3220 /* Multi-fork case. The current inferior_ptid has exited, but
3221 there are other viable forks to debug. Delete the exiting
3222 one and context-switch to the first available. */
3223 linux_fork_mourn_inferior ();
3226 /* Convert a native/host siginfo object, into/from the siginfo in the
3227 layout of the inferiors' architecture. */
3230 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3234 if (linux_nat_siginfo_fixup
!= NULL
)
3235 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3237 /* If there was no callback, or the callback didn't do anything,
3238 then just do a straight memcpy. */
3242 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
3244 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
3249 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3250 const char *annex
, gdb_byte
*readbuf
,
3251 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3254 struct siginfo siginfo
;
3255 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
3257 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3258 gdb_assert (readbuf
|| writebuf
);
3260 pid
= GET_LWP (inferior_ptid
);
3262 pid
= GET_PID (inferior_ptid
);
3264 if (offset
> sizeof (siginfo
))
3268 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3272 /* When GDB is built as a 64-bit application, ptrace writes into
3273 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3274 inferior with a 64-bit GDB should look the same as debugging it
3275 with a 32-bit GDB, we need to convert it. GDB core always sees
3276 the converted layout, so any read/write will have to be done
3278 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3280 if (offset
+ len
> sizeof (siginfo
))
3281 len
= sizeof (siginfo
) - offset
;
3283 if (readbuf
!= NULL
)
3284 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3287 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3289 /* Convert back to ptrace layout before flushing it out. */
3290 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3293 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3302 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3303 const char *annex
, gdb_byte
*readbuf
,
3304 const gdb_byte
*writebuf
,
3305 ULONGEST offset
, LONGEST len
)
3307 struct cleanup
*old_chain
;
3310 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3311 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3314 old_chain
= save_inferior_ptid ();
3316 if (is_lwp (inferior_ptid
))
3317 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3319 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3322 do_cleanups (old_chain
);
3327 linux_thread_alive (ptid_t ptid
)
3331 gdb_assert (is_lwp (ptid
));
3333 /* Send signal 0 instead of anything ptrace, because ptracing a
3334 running thread errors out claiming that the thread doesn't
3336 err
= kill_lwp (GET_LWP (ptid
), 0);
3338 if (debug_linux_nat
)
3339 fprintf_unfiltered (gdb_stdlog
,
3340 "LLTA: KILL(SIG0) %s (%s)\n",
3341 target_pid_to_str (ptid
),
3342 err
? safe_strerror (err
) : "OK");
3351 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3353 return linux_thread_alive (ptid
);
3357 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3359 static char buf
[64];
3362 && (GET_PID (ptid
) != GET_LWP (ptid
)
3363 || num_lwps (GET_PID (ptid
)) > 1))
3365 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3369 return normal_pid_to_str (ptid
);
3372 /* Accepts an integer PID; Returns a string representing a file that
3373 can be opened to get the symbols for the child process. */
3376 linux_child_pid_to_exec_file (int pid
)
3378 char *name1
, *name2
;
3380 name1
= xmalloc (MAXPATHLEN
);
3381 name2
= xmalloc (MAXPATHLEN
);
3382 make_cleanup (xfree
, name1
);
3383 make_cleanup (xfree
, name2
);
3384 memset (name2
, 0, MAXPATHLEN
);
3386 sprintf (name1
, "/proc/%d/exe", pid
);
3387 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3393 /* Service function for corefiles and info proc. */
3396 read_mapping (FILE *mapfile
,
3401 char *device
, long long *inode
, char *filename
)
3403 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3404 addr
, endaddr
, permissions
, offset
, device
, inode
);
3407 if (ret
> 0 && ret
!= EOF
)
3409 /* Eat everything up to EOL for the filename. This will prevent
3410 weird filenames (such as one with embedded whitespace) from
3411 confusing this code. It also makes this code more robust in
3412 respect to annotations the kernel may add after the filename.
3414 Note the filename is used for informational purposes
3416 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3419 return (ret
!= 0 && ret
!= EOF
);
3422 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3423 regions in the inferior for a corefile. */
3426 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3428 int, int, int, void *), void *obfd
)
3430 int pid
= PIDGET (inferior_ptid
);
3431 char mapsfilename
[MAXPATHLEN
];
3433 long long addr
, endaddr
, size
, offset
, inode
;
3434 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3435 int read
, write
, exec
;
3437 struct cleanup
*cleanup
;
3439 /* Compose the filename for the /proc memory map, and open it. */
3440 sprintf (mapsfilename
, "/proc/%d/maps", pid
);
3441 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3442 error (_("Could not open %s."), mapsfilename
);
3443 cleanup
= make_cleanup_fclose (mapsfile
);
3446 fprintf_filtered (gdb_stdout
,
3447 "Reading memory regions from %s\n", mapsfilename
);
3449 /* Now iterate until end-of-file. */
3450 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3451 &offset
, &device
[0], &inode
, &filename
[0]))
3453 size
= endaddr
- addr
;
3455 /* Get the segment's permissions. */
3456 read
= (strchr (permissions
, 'r') != 0);
3457 write
= (strchr (permissions
, 'w') != 0);
3458 exec
= (strchr (permissions
, 'x') != 0);
3462 fprintf_filtered (gdb_stdout
,
3463 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3464 size
, paddr_nz (addr
),
3466 write
? 'w' : ' ', exec
? 'x' : ' ');
3468 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3469 fprintf_filtered (gdb_stdout
, "\n");
3472 /* Invoke the callback function to create the corefile
3474 func (addr
, size
, read
, write
, exec
, obfd
);
3476 do_cleanups (cleanup
);
3481 find_signalled_thread (struct thread_info
*info
, void *data
)
3483 if (info
->stop_signal
!= TARGET_SIGNAL_0
3484 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3490 static enum target_signal
3491 find_stop_signal (void)
3493 struct thread_info
*info
=
3494 iterate_over_threads (find_signalled_thread
, NULL
);
3497 return info
->stop_signal
;
3499 return TARGET_SIGNAL_0
;
3502 /* Records the thread's register state for the corefile note
3506 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3507 char *note_data
, int *note_size
,
3508 enum target_signal stop_signal
)
3510 gdb_gregset_t gregs
;
3511 gdb_fpregset_t fpregs
;
3512 unsigned long lwp
= ptid_get_lwp (ptid
);
3513 struct regcache
*regcache
= get_thread_regcache (ptid
);
3514 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3515 const struct regset
*regset
;
3517 struct cleanup
*old_chain
;
3518 struct core_regset_section
*sect_list
;
3521 old_chain
= save_inferior_ptid ();
3522 inferior_ptid
= ptid
;
3523 target_fetch_registers (regcache
, -1);
3524 do_cleanups (old_chain
);
3526 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3527 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3530 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3531 sizeof (gregs
))) != NULL
3532 && regset
->collect_regset
!= NULL
)
3533 regset
->collect_regset (regset
, regcache
, -1,
3534 &gregs
, sizeof (gregs
));
3536 fill_gregset (regcache
, &gregs
, -1);
3538 note_data
= (char *) elfcore_write_prstatus (obfd
,
3542 stop_signal
, &gregs
);
3544 /* The loop below uses the new struct core_regset_section, which stores
3545 the supported section names and sizes for the core file. Note that
3546 note PRSTATUS needs to be treated specially. But the other notes are
3547 structurally the same, so they can benefit from the new struct. */
3548 if (core_regset_p
&& sect_list
!= NULL
)
3549 while (sect_list
->sect_name
!= NULL
)
3551 /* .reg was already handled above. */
3552 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3557 regset
= gdbarch_regset_from_core_section (gdbarch
,
3558 sect_list
->sect_name
,
3560 gdb_assert (regset
&& regset
->collect_regset
);
3561 gdb_regset
= xmalloc (sect_list
->size
);
3562 regset
->collect_regset (regset
, regcache
, -1,
3563 gdb_regset
, sect_list
->size
);
3564 note_data
= (char *) elfcore_write_register_note (obfd
,
3567 sect_list
->sect_name
,
3574 /* For architectures that does not have the struct core_regset_section
3575 implemented, we use the old method. When all the architectures have
3576 the new support, the code below should be deleted. */
3580 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3581 sizeof (fpregs
))) != NULL
3582 && regset
->collect_regset
!= NULL
)
3583 regset
->collect_regset (regset
, regcache
, -1,
3584 &fpregs
, sizeof (fpregs
));
3586 fill_fpregset (regcache
, &fpregs
, -1);
3588 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3591 &fpregs
, sizeof (fpregs
));
3597 struct linux_nat_corefile_thread_data
3603 enum target_signal stop_signal
;
3606 /* Called by gdbthread.c once per thread. Records the thread's
3607 register state for the corefile note section. */
3610 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3612 struct linux_nat_corefile_thread_data
*args
= data
;
3614 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3624 /* Fills the "to_make_corefile_note" target vector. Builds the note
3625 section for a corefile, and returns it in a malloc buffer. */
3628 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3630 struct linux_nat_corefile_thread_data thread_args
;
3631 struct cleanup
*old_chain
;
3632 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3633 char fname
[16] = { '\0' };
3634 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3635 char psargs
[80] = { '\0' };
3636 char *note_data
= NULL
;
3637 ptid_t current_ptid
= inferior_ptid
;
3638 ptid_t filter
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3642 if (get_exec_file (0))
3644 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3645 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3646 if (get_inferior_args ())
3649 char *psargs_end
= psargs
+ sizeof (psargs
);
3651 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3653 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3654 if (string_end
!= NULL
)
3656 *string_end
++ = ' ';
3657 strncpy (string_end
, get_inferior_args (),
3658 psargs_end
- string_end
);
3661 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3663 note_size
, fname
, psargs
);
3666 /* Dump information for threads. */
3667 thread_args
.obfd
= obfd
;
3668 thread_args
.note_data
= note_data
;
3669 thread_args
.note_size
= note_size
;
3670 thread_args
.num_notes
= 0;
3671 thread_args
.stop_signal
= find_stop_signal ();
3672 iterate_over_lwps (filter
, linux_nat_corefile_thread_callback
, &thread_args
);
3673 gdb_assert (thread_args
.num_notes
!= 0);
3674 note_data
= thread_args
.note_data
;
3676 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3680 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3681 "CORE", NT_AUXV
, auxv
, auxv_len
);
3685 make_cleanup (xfree
, note_data
);
3689 /* Implement the "info proc" command. */
3692 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3694 /* A long is used for pid instead of an int to avoid a loss of precision
3695 compiler warning from the output of strtoul. */
3696 long pid
= PIDGET (inferior_ptid
);
3699 char buffer
[MAXPATHLEN
];
3700 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3713 /* Break up 'args' into an argv array. */
3714 argv
= gdb_buildargv (args
);
3715 make_cleanup_freeargv (argv
);
3717 while (argv
!= NULL
&& *argv
!= NULL
)
3719 if (isdigit (argv
[0][0]))
3721 pid
= strtoul (argv
[0], NULL
, 10);
3723 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3727 else if (strcmp (argv
[0], "status") == 0)
3731 else if (strcmp (argv
[0], "stat") == 0)
3735 else if (strcmp (argv
[0], "cmd") == 0)
3739 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3743 else if (strcmp (argv
[0], "cwd") == 0)
3747 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3753 /* [...] (future options here) */
3758 error (_("No current process: you must name one."));
3760 sprintf (fname1
, "/proc/%ld", pid
);
3761 if (stat (fname1
, &dummy
) != 0)
3762 error (_("No /proc directory: '%s'"), fname1
);
3764 printf_filtered (_("process %ld\n"), pid
);
3765 if (cmdline_f
|| all
)
3767 sprintf (fname1
, "/proc/%ld/cmdline", pid
);
3768 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3770 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3771 if (fgets (buffer
, sizeof (buffer
), procfile
))
3772 printf_filtered ("cmdline = '%s'\n", buffer
);
3774 warning (_("unable to read '%s'"), fname1
);
3775 do_cleanups (cleanup
);
3778 warning (_("unable to open /proc file '%s'"), fname1
);
3782 sprintf (fname1
, "/proc/%ld/cwd", pid
);
3783 memset (fname2
, 0, sizeof (fname2
));
3784 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3785 printf_filtered ("cwd = '%s'\n", fname2
);
3787 warning (_("unable to read link '%s'"), fname1
);
3791 sprintf (fname1
, "/proc/%ld/exe", pid
);
3792 memset (fname2
, 0, sizeof (fname2
));
3793 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3794 printf_filtered ("exe = '%s'\n", fname2
);
3796 warning (_("unable to read link '%s'"), fname1
);
3798 if (mappings_f
|| all
)
3800 sprintf (fname1
, "/proc/%ld/maps", pid
);
3801 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3803 long long addr
, endaddr
, size
, offset
, inode
;
3804 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3805 struct cleanup
*cleanup
;
3807 cleanup
= make_cleanup_fclose (procfile
);
3808 printf_filtered (_("Mapped address spaces:\n\n"));
3809 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3811 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3814 " Size", " Offset", "objfile");
3818 printf_filtered (" %18s %18s %10s %10s %7s\n",
3821 " Size", " Offset", "objfile");
3824 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3825 &offset
, &device
[0], &inode
, &filename
[0]))
3827 size
= endaddr
- addr
;
3829 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3830 calls here (and possibly above) should be abstracted
3831 out into their own functions? Andrew suggests using
3832 a generic local_address_string instead to print out
3833 the addresses; that makes sense to me, too. */
3835 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3837 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3838 (unsigned long) addr
, /* FIXME: pr_addr */
3839 (unsigned long) endaddr
,
3841 (unsigned int) offset
,
3842 filename
[0] ? filename
: "");
3846 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3847 (unsigned long) addr
, /* FIXME: pr_addr */
3848 (unsigned long) endaddr
,
3850 (unsigned int) offset
,
3851 filename
[0] ? filename
: "");
3855 do_cleanups (cleanup
);
3858 warning (_("unable to open /proc file '%s'"), fname1
);
3860 if (status_f
|| all
)
3862 sprintf (fname1
, "/proc/%ld/status", pid
);
3863 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3865 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3866 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3867 puts_filtered (buffer
);
3868 do_cleanups (cleanup
);
3871 warning (_("unable to open /proc file '%s'"), fname1
);
3875 sprintf (fname1
, "/proc/%ld/stat", pid
);
3876 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3881 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3883 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3884 printf_filtered (_("Process: %d\n"), itmp
);
3885 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3886 printf_filtered (_("Exec file: %s\n"), buffer
);
3887 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3888 printf_filtered (_("State: %c\n"), ctmp
);
3889 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3890 printf_filtered (_("Parent process: %d\n"), itmp
);
3891 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3892 printf_filtered (_("Process group: %d\n"), itmp
);
3893 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3894 printf_filtered (_("Session id: %d\n"), itmp
);
3895 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3896 printf_filtered (_("TTY: %d\n"), itmp
);
3897 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3898 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3899 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3900 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3901 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3902 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3903 (unsigned long) ltmp
);
3904 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3905 printf_filtered (_("Minor faults, children: %lu\n"),
3906 (unsigned long) ltmp
);
3907 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3908 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3909 (unsigned long) ltmp
);
3910 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3911 printf_filtered (_("Major faults, children: %lu\n"),
3912 (unsigned long) ltmp
);
3913 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3914 printf_filtered (_("utime: %ld\n"), ltmp
);
3915 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3916 printf_filtered (_("stime: %ld\n"), ltmp
);
3917 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3918 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3919 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3920 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3921 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3922 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3924 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3925 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3926 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3927 printf_filtered (_("jiffies until next timeout: %lu\n"),
3928 (unsigned long) ltmp
);
3929 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3930 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3931 (unsigned long) ltmp
);
3932 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3933 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3935 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3936 printf_filtered (_("Virtual memory size: %lu\n"),
3937 (unsigned long) ltmp
);
3938 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3939 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3940 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3941 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3942 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3943 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3944 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3945 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3946 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3947 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3948 #if 0 /* Don't know how architecture-dependent the rest is...
3949 Anyway the signal bitmap info is available from "status". */
3950 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3951 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3952 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3953 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3954 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3955 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3956 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3957 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3958 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3959 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
3960 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3961 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
3962 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3963 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
3965 do_cleanups (cleanup
);
3968 warning (_("unable to open /proc file '%s'"), fname1
);
3972 /* Implement the to_xfer_partial interface for memory reads using the /proc
3973 filesystem. Because we can use a single read() call for /proc, this
3974 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3975 but it doesn't support writes. */
3978 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3979 const char *annex
, gdb_byte
*readbuf
,
3980 const gdb_byte
*writebuf
,
3981 ULONGEST offset
, LONGEST len
)
3987 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3990 /* Don't bother for one word. */
3991 if (len
< 3 * sizeof (long))
3994 /* We could keep this file open and cache it - possibly one per
3995 thread. That requires some juggling, but is even faster. */
3996 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3997 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4001 /* If pread64 is available, use it. It's faster if the kernel
4002 supports it (only one syscall), and it's 64-bit safe even on
4003 32-bit platforms (for instance, SPARC debugging a SPARC64
4006 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4008 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4018 /* Parse LINE as a signal set and add its set bits to SIGS. */
4021 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4023 int len
= strlen (line
) - 1;
4027 if (line
[len
] != '\n')
4028 error (_("Could not parse signal set: %s"), line
);
4036 if (*p
>= '0' && *p
<= '9')
4038 else if (*p
>= 'a' && *p
<= 'f')
4039 digit
= *p
- 'a' + 10;
4041 error (_("Could not parse signal set: %s"), line
);
4046 sigaddset (sigs
, signum
+ 1);
4048 sigaddset (sigs
, signum
+ 2);
4050 sigaddset (sigs
, signum
+ 3);
4052 sigaddset (sigs
, signum
+ 4);
4058 /* Find process PID's pending signals from /proc/pid/status and set
4062 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
4065 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4067 struct cleanup
*cleanup
;
4069 sigemptyset (pending
);
4070 sigemptyset (blocked
);
4071 sigemptyset (ignored
);
4072 sprintf (fname
, "/proc/%d/status", pid
);
4073 procfile
= fopen (fname
, "r");
4074 if (procfile
== NULL
)
4075 error (_("Could not open %s"), fname
);
4076 cleanup
= make_cleanup_fclose (procfile
);
4078 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4080 /* Normal queued signals are on the SigPnd line in the status
4081 file. However, 2.6 kernels also have a "shared" pending
4082 queue for delivering signals to a thread group, so check for
4085 Unfortunately some Red Hat kernels include the shared pending
4086 queue but not the ShdPnd status field. */
4088 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4089 add_line_to_sigset (buffer
+ 8, pending
);
4090 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4091 add_line_to_sigset (buffer
+ 8, pending
);
4092 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4093 add_line_to_sigset (buffer
+ 8, blocked
);
4094 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4095 add_line_to_sigset (buffer
+ 8, ignored
);
4098 do_cleanups (cleanup
);
4102 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4103 const char *annex
, gdb_byte
*readbuf
,
4104 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4106 /* We make the process list snapshot when the object starts to be
4108 static const char *buf
;
4109 static LONGEST len_avail
= -1;
4110 static struct obstack obstack
;
4114 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4116 if (strcmp (annex
, "processes") != 0)
4119 gdb_assert (readbuf
&& !writebuf
);
4123 if (len_avail
!= -1 && len_avail
!= 0)
4124 obstack_free (&obstack
, NULL
);
4127 obstack_init (&obstack
);
4128 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4130 dirp
= opendir ("/proc");
4134 while ((dp
= readdir (dirp
)) != NULL
)
4136 struct stat statbuf
;
4137 char procentry
[sizeof ("/proc/4294967295")];
4139 if (!isdigit (dp
->d_name
[0])
4140 || NAMELEN (dp
) > sizeof ("4294967295") - 1)
4143 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4144 if (stat (procentry
, &statbuf
) == 0
4145 && S_ISDIR (statbuf
.st_mode
))
4149 char cmd
[MAXPATHLEN
+ 1];
4150 struct passwd
*entry
;
4152 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4153 entry
= getpwuid (statbuf
.st_uid
);
4155 if ((f
= fopen (pathname
, "r")) != NULL
)
4157 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4161 for (i
= 0; i
< len
; i
++)
4166 obstack_xml_printf (
4169 "<column name=\"pid\">%s</column>"
4170 "<column name=\"user\">%s</column>"
4171 "<column name=\"command\">%s</column>"
4174 entry
? entry
->pw_name
: "?",
4187 obstack_grow_str0 (&obstack
, "</osdata>\n");
4188 buf
= obstack_finish (&obstack
);
4189 len_avail
= strlen (buf
);
4192 if (offset
>= len_avail
)
4194 /* Done. Get rid of the obstack. */
4195 obstack_free (&obstack
, NULL
);
4201 if (len
> len_avail
- offset
)
4202 len
= len_avail
- offset
;
4203 memcpy (readbuf
, buf
+ offset
, len
);
4209 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4210 const char *annex
, gdb_byte
*readbuf
,
4211 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4215 if (object
== TARGET_OBJECT_AUXV
)
4216 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4219 if (object
== TARGET_OBJECT_OSDATA
)
4220 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4223 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4228 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4232 /* Create a prototype generic GNU/Linux target. The client can override
4233 it with local methods. */
4236 linux_target_install_ops (struct target_ops
*t
)
4238 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4239 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4240 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4241 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4242 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4243 t
->to_post_attach
= linux_child_post_attach
;
4244 t
->to_follow_fork
= linux_child_follow_fork
;
4245 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4246 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4248 super_xfer_partial
= t
->to_xfer_partial
;
4249 t
->to_xfer_partial
= linux_xfer_partial
;
4255 struct target_ops
*t
;
4257 t
= inf_ptrace_target ();
4258 linux_target_install_ops (t
);
4264 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4266 struct target_ops
*t
;
4268 t
= inf_ptrace_trad_target (register_u_offset
);
4269 linux_target_install_ops (t
);
4274 /* target_is_async_p implementation. */
4277 linux_nat_is_async_p (void)
4279 /* NOTE: palves 2008-03-21: We're only async when the user requests
4280 it explicitly with the "set target-async" command.
4281 Someday, linux will always be async. */
4282 if (!target_async_permitted
)
4285 /* See target.h/target_async_mask. */
4286 return linux_nat_async_mask_value
;
4289 /* target_can_async_p implementation. */
4292 linux_nat_can_async_p (void)
4294 /* NOTE: palves 2008-03-21: We're only async when the user requests
4295 it explicitly with the "set target-async" command.
4296 Someday, linux will always be async. */
4297 if (!target_async_permitted
)
4300 /* See target.h/target_async_mask. */
4301 return linux_nat_async_mask_value
;
4305 linux_nat_supports_non_stop (void)
4310 /* True if we want to support multi-process. To be removed when GDB
4311 supports multi-exec. */
4313 int linux_multi_process
= 0;
4316 linux_nat_supports_multi_process (void)
4318 return linux_multi_process
;
4321 /* target_async_mask implementation. */
4324 linux_nat_async_mask (int new_mask
)
4326 int curr_mask
= linux_nat_async_mask_value
;
4328 if (curr_mask
!= new_mask
)
4332 linux_nat_async (NULL
, 0);
4333 linux_nat_async_mask_value
= new_mask
;
4337 linux_nat_async_mask_value
= new_mask
;
4339 /* If we're going out of async-mask in all-stop, then the
4340 inferior is stopped. The next resume will call
4341 target_async. In non-stop, the target event source
4342 should be always registered in the event loop. Do so
4345 linux_nat_async (inferior_event_handler
, 0);
4352 static int async_terminal_is_ours
= 1;
4354 /* target_terminal_inferior implementation. */
4357 linux_nat_terminal_inferior (void)
4359 if (!target_is_async_p ())
4361 /* Async mode is disabled. */
4362 terminal_inferior ();
4366 /* GDB should never give the terminal to the inferior, if the
4367 inferior is running in the background (run&, continue&, etc.).
4368 This check can be removed when the common code is fixed. */
4369 if (!sync_execution
)
4372 terminal_inferior ();
4374 if (!async_terminal_is_ours
)
4377 delete_file_handler (input_fd
);
4378 async_terminal_is_ours
= 0;
4382 /* target_terminal_ours implementation. */
4385 linux_nat_terminal_ours (void)
4387 if (!target_is_async_p ())
4389 /* Async mode is disabled. */
4394 /* GDB should never give the terminal to the inferior if the
4395 inferior is running in the background (run&, continue&, etc.),
4396 but claiming it sure should. */
4399 if (!sync_execution
)
4402 if (async_terminal_is_ours
)
4405 clear_sigint_trap ();
4406 add_file_handler (input_fd
, stdin_event_handler
, 0);
4407 async_terminal_is_ours
= 1;
4410 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4412 static void *async_client_context
;
4414 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4415 so we notice when any child changes state, and notify the
4416 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4417 above to wait for the arrival of a SIGCHLD. */
4420 sigchld_handler (int signo
)
4422 int old_errno
= errno
;
4424 if (debug_linux_nat_async
)
4425 fprintf_unfiltered (gdb_stdlog
, "sigchld\n");
4427 if (signo
== SIGCHLD
4428 && linux_nat_event_pipe
[0] != -1)
4429 async_file_mark (); /* Let the event loop know that there are
4430 events to handle. */
4435 /* Callback registered with the target events file descriptor. */
4438 handle_target_event (int error
, gdb_client_data client_data
)
4440 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4443 /* Create/destroy the target events pipe. Returns previous state. */
4446 linux_async_pipe (int enable
)
4448 int previous
= (linux_nat_event_pipe
[0] != -1);
4450 if (previous
!= enable
)
4454 block_child_signals (&prev_mask
);
4458 if (pipe (linux_nat_event_pipe
) == -1)
4459 internal_error (__FILE__
, __LINE__
,
4460 "creating event pipe failed.");
4462 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4463 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4467 close (linux_nat_event_pipe
[0]);
4468 close (linux_nat_event_pipe
[1]);
4469 linux_nat_event_pipe
[0] = -1;
4470 linux_nat_event_pipe
[1] = -1;
4473 restore_child_signals_mask (&prev_mask
);
4479 /* target_async implementation. */
4482 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4483 void *context
), void *context
)
4485 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
4486 internal_error (__FILE__
, __LINE__
,
4487 "Calling target_async when async is masked");
4489 if (callback
!= NULL
)
4491 async_client_callback
= callback
;
4492 async_client_context
= context
;
4493 if (!linux_async_pipe (1))
4495 add_file_handler (linux_nat_event_pipe
[0],
4496 handle_target_event
, NULL
);
4497 /* There may be pending events to handle. Tell the event loop
4504 async_client_callback
= callback
;
4505 async_client_context
= context
;
4506 delete_file_handler (linux_nat_event_pipe
[0]);
4507 linux_async_pipe (0);
4512 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4516 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4521 ptid_t ptid
= lwp
->ptid
;
4523 if (debug_linux_nat
)
4524 fprintf_unfiltered (gdb_stdlog
,
4525 "LNSL: running -> suspending %s\n",
4526 target_pid_to_str (lwp
->ptid
));
4529 stop_callback (lwp
, NULL
);
4530 stop_wait_callback (lwp
, NULL
);
4532 /* If the lwp exits while we try to stop it, there's nothing
4534 lwp
= find_lwp_pid (ptid
);
4538 /* If we didn't collect any signal other than SIGSTOP while
4539 stopping the LWP, push a SIGNAL_0 event. In either case, the
4540 event-loop will end up calling target_wait which will collect
4542 if (lwp
->status
== 0)
4543 lwp
->status
= W_STOPCODE (0);
4548 /* Already known to be stopped; do nothing. */
4550 if (debug_linux_nat
)
4552 if (find_thread_pid (lwp
->ptid
)->stop_requested
)
4553 fprintf_unfiltered (gdb_stdlog
, "\
4554 LNSL: already stopped/stop_requested %s\n",
4555 target_pid_to_str (lwp
->ptid
));
4557 fprintf_unfiltered (gdb_stdlog
, "\
4558 LNSL: already stopped/no stop_requested yet %s\n",
4559 target_pid_to_str (lwp
->ptid
));
4566 linux_nat_stop (ptid_t ptid
)
4569 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4571 linux_ops
->to_stop (ptid
);
4575 linux_nat_close (int quitting
)
4577 /* Unregister from the event loop. */
4578 if (target_is_async_p ())
4579 target_async (NULL
, 0);
4581 /* Reset the async_masking. */
4582 linux_nat_async_mask_value
= 1;
4584 if (linux_ops
->to_close
)
4585 linux_ops
->to_close (quitting
);
4589 linux_nat_add_target (struct target_ops
*t
)
4591 /* Save the provided single-threaded target. We save this in a separate
4592 variable because another target we've inherited from (e.g. inf-ptrace)
4593 may have saved a pointer to T; we want to use it for the final
4594 process stratum target. */
4595 linux_ops_saved
= *t
;
4596 linux_ops
= &linux_ops_saved
;
4598 /* Override some methods for multithreading. */
4599 t
->to_create_inferior
= linux_nat_create_inferior
;
4600 t
->to_attach
= linux_nat_attach
;
4601 t
->to_detach
= linux_nat_detach
;
4602 t
->to_resume
= linux_nat_resume
;
4603 t
->to_wait
= linux_nat_wait
;
4604 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4605 t
->to_kill
= linux_nat_kill
;
4606 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4607 t
->to_thread_alive
= linux_nat_thread_alive
;
4608 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4609 t
->to_has_thread_control
= tc_schedlock
;
4611 t
->to_can_async_p
= linux_nat_can_async_p
;
4612 t
->to_is_async_p
= linux_nat_is_async_p
;
4613 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4614 t
->to_async
= linux_nat_async
;
4615 t
->to_async_mask
= linux_nat_async_mask
;
4616 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4617 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4618 t
->to_close
= linux_nat_close
;
4620 /* Methods for non-stop support. */
4621 t
->to_stop
= linux_nat_stop
;
4623 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4625 /* We don't change the stratum; this target will sit at
4626 process_stratum and thread_db will set at thread_stratum. This
4627 is a little strange, since this is a multi-threaded-capable
4628 target, but we want to be on the stack below thread_db, and we
4629 also want to be used for single-threaded processes. */
4634 /* Register a method to call whenever a new thread is attached. */
4636 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4638 /* Save the pointer. We only support a single registered instance
4639 of the GNU/Linux native target, so we do not need to map this to
4641 linux_nat_new_thread
= new_thread
;
4644 /* Register a method that converts a siginfo object between the layout
4645 that ptrace returns, and the layout in the architecture of the
4648 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4649 int (*siginfo_fixup
) (struct siginfo
*,
4653 /* Save the pointer. */
4654 linux_nat_siginfo_fixup
= siginfo_fixup
;
4657 /* Return the saved siginfo associated with PTID. */
4659 linux_nat_get_siginfo (ptid_t ptid
)
4661 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4663 gdb_assert (lp
!= NULL
);
4665 return &lp
->siginfo
;
4668 /* Provide a prototype to silence -Wmissing-prototypes. */
4669 extern initialize_file_ftype _initialize_linux_nat
;
4672 _initialize_linux_nat (void)
4676 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4677 Show /proc process information about any running process.\n\
4678 Specify any process id, or use the program being debugged by default.\n\
4679 Specify any of the following keywords for detailed info:\n\
4680 mappings -- list of mapped memory regions.\n\
4681 stat -- list a bunch of random process info.\n\
4682 status -- list a different bunch of random process info.\n\
4683 all -- list all available /proc info."));
4685 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4686 &debug_linux_nat
, _("\
4687 Set debugging of GNU/Linux lwp module."), _("\
4688 Show debugging of GNU/Linux lwp module."), _("\
4689 Enables printf debugging output."),
4691 show_debug_linux_nat
,
4692 &setdebuglist
, &showdebuglist
);
4694 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4695 &debug_linux_nat_async
, _("\
4696 Set debugging of GNU/Linux async lwp module."), _("\
4697 Show debugging of GNU/Linux async lwp module."), _("\
4698 Enables printf debugging output."),
4700 show_debug_linux_nat_async
,
4701 &setdebuglist
, &showdebuglist
);
4703 /* Save this mask as the default. */
4704 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4706 /* Install a SIGCHLD handler. */
4707 sigchld_action
.sa_handler
= sigchld_handler
;
4708 sigemptyset (&sigchld_action
.sa_mask
);
4709 sigchld_action
.sa_flags
= SA_RESTART
;
4711 /* Make it the default. */
4712 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4714 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4715 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4716 sigdelset (&suspend_mask
, SIGCHLD
);
4718 sigemptyset (&blocked_mask
);
4720 add_setshow_boolean_cmd ("disable-randomization", class_support
,
4721 &disable_randomization
, _("\
4722 Set disabling of debuggee's virtual address space randomization."), _("\
4723 Show disabling of debuggee's virtual address space randomization."), _("\
4724 When this mode is on (which is the default), randomization of the virtual\n\
4725 address space is disabled. Standalone programs run with the randomization\n\
4726 enabled by default on some platforms."),
4727 &set_disable_randomization
,
4728 &show_disable_randomization
,
4729 &setlist
, &showlist
);
4733 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4734 the GNU/Linux Threads library and therefore doesn't really belong
4737 /* Read variable NAME in the target and return its value if found.
4738 Otherwise return zero. It is assumed that the type of the variable
4742 get_signo (const char *name
)
4744 struct minimal_symbol
*ms
;
4747 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4751 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4752 sizeof (signo
)) != 0)
4758 /* Return the set of signals used by the threads library in *SET. */
4761 lin_thread_get_thread_signals (sigset_t
*set
)
4763 struct sigaction action
;
4764 int restart
, cancel
;
4766 sigemptyset (&blocked_mask
);
4769 restart
= get_signo ("__pthread_sig_restart");
4770 cancel
= get_signo ("__pthread_sig_cancel");
4772 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4773 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4774 not provide any way for the debugger to query the signal numbers -
4775 fortunately they don't change! */
4778 restart
= __SIGRTMIN
;
4781 cancel
= __SIGRTMIN
+ 1;
4783 sigaddset (set
, restart
);
4784 sigaddset (set
, cancel
);
4786 /* The GNU/Linux Threads library makes terminating threads send a
4787 special "cancel" signal instead of SIGCHLD. Make sure we catch
4788 those (to prevent them from terminating GDB itself, which is
4789 likely to be their default action) and treat them the same way as
4792 action
.sa_handler
= sigchld_handler
;
4793 sigemptyset (&action
.sa_mask
);
4794 action
.sa_flags
= SA_RESTART
;
4795 sigaction (cancel
, &action
, NULL
);
4797 /* We block the "cancel" signal throughout this code ... */
4798 sigaddset (&blocked_mask
, cancel
);
4799 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4801 /* ... except during a sigsuspend. */
4802 sigdelset (&suspend_mask
, cancel
);