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"
58 #ifdef HAVE_PERSONALITY
59 # include <sys/personality.h>
60 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
61 # define ADDR_NO_RANDOMIZE 0x0040000
63 #endif /* HAVE_PERSONALITY */
65 /* This comment documents high-level logic of this file.
67 Waiting for events in sync mode
68 ===============================
70 When waiting for an event in a specific thread, we just use waitpid, passing
71 the specific pid, and not passing WNOHANG.
73 When waiting for an event in all threads, waitpid is not quite good. Prior to
74 version 2.4, Linux can either wait for event in main thread, or in secondary
75 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
76 miss an event. The solution is to use non-blocking waitpid, together with
77 sigsuspend. First, we use non-blocking waitpid to get an event in the main
78 process, if any. Second, we use non-blocking waitpid with the __WCLONED
79 flag to check for events in cloned processes. If nothing is found, we use
80 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
81 happened to a child process -- and SIGCHLD will be delivered both for events
82 in main debugged process and in cloned processes. As soon as we know there's
83 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
85 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
86 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
87 blocked, the signal becomes pending and sigsuspend immediately
88 notices it and returns.
90 Waiting for events in async mode
91 ================================
93 In async mode, GDB should always be ready to handle both user input
94 and target events, so neither blocking waitpid nor sigsuspend are
95 viable options. Instead, we should asynchronously notify the GDB main
96 event loop whenever there's an unprocessed event from the target. We
97 detect asynchronous target events by handling SIGCHLD signals. To
98 notify the event loop about target events, the self-pipe trick is used
99 --- a pipe is registered as waitable event source in the event loop,
100 the event loop select/poll's on the read end of this pipe (as well on
101 other event sources, e.g., stdin), and the SIGCHLD handler writes a
102 byte to this pipe. This is more portable than relying on
103 pselect/ppoll, since on kernels that lack those syscalls, libc
104 emulates them with select/poll+sigprocmask, and that is racy
105 (a.k.a. plain broken).
107 Obviously, if we fail to notify the event loop if there's a target
108 event, it's bad. OTOH, if we notify the event loop when there's no
109 event from the target, linux_nat_wait will detect that there's no real
110 event to report, and return event of type TARGET_WAITKIND_IGNORE.
111 This is mostly harmless, but it will waste time and is better avoided.
113 The main design point is that every time GDB is outside linux-nat.c,
114 we have a SIGCHLD handler installed that is called when something
115 happens to the target and notifies the GDB event loop. Whenever GDB
116 core decides to handle the event, and calls into linux-nat.c, we
117 process things as in sync mode, except that the we never block in
120 While processing an event, we may end up momentarily blocked in
121 waitpid calls. Those waitpid calls, while blocking, are guarantied to
122 return quickly. E.g., in all-stop mode, before reporting to the core
123 that an LWP hit a breakpoint, all LWPs are stopped by sending them
124 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
125 Note that this is different from blocking indefinitely waiting for the
126 next event --- here, we're already handling an event.
131 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
132 signal is not entirely significant; we just need for a signal to be delivered,
133 so that we can intercept it. SIGSTOP's advantage is that it can not be
134 blocked. A disadvantage is that it is not a real-time signal, so it can only
135 be queued once; we do not keep track of other sources of SIGSTOP.
137 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
138 use them, because they have special behavior when the signal is generated -
139 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
140 kills the entire thread group.
142 A delivered SIGSTOP would stop the entire thread group, not just the thread we
143 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
144 cancel it (by PTRACE_CONT without passing SIGSTOP).
146 We could use a real-time signal instead. This would solve those problems; we
147 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
148 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
149 generates it, and there are races with trying to find a signal that is not
153 #define O_LARGEFILE 0
156 /* If the system headers did not provide the constants, hard-code the normal
158 #ifndef PTRACE_EVENT_FORK
160 #define PTRACE_SETOPTIONS 0x4200
161 #define PTRACE_GETEVENTMSG 0x4201
163 /* options set using PTRACE_SETOPTIONS */
164 #define PTRACE_O_TRACESYSGOOD 0x00000001
165 #define PTRACE_O_TRACEFORK 0x00000002
166 #define PTRACE_O_TRACEVFORK 0x00000004
167 #define PTRACE_O_TRACECLONE 0x00000008
168 #define PTRACE_O_TRACEEXEC 0x00000010
169 #define PTRACE_O_TRACEVFORKDONE 0x00000020
170 #define PTRACE_O_TRACEEXIT 0x00000040
172 /* Wait extended result codes for the above trace options. */
173 #define PTRACE_EVENT_FORK 1
174 #define PTRACE_EVENT_VFORK 2
175 #define PTRACE_EVENT_CLONE 3
176 #define PTRACE_EVENT_EXEC 4
177 #define PTRACE_EVENT_VFORK_DONE 5
178 #define PTRACE_EVENT_EXIT 6
180 #endif /* PTRACE_EVENT_FORK */
182 /* We can't always assume that this flag is available, but all systems
183 with the ptrace event handlers also have __WALL, so it's safe to use
186 #define __WALL 0x40000000 /* Wait for any child. */
189 #ifndef PTRACE_GETSIGINFO
190 # define PTRACE_GETSIGINFO 0x4202
191 # define PTRACE_SETSIGINFO 0x4203
194 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
195 the use of the multi-threaded target. */
196 static struct target_ops
*linux_ops
;
197 static struct target_ops linux_ops_saved
;
199 /* The method to call, if any, when a new thread is attached. */
200 static void (*linux_nat_new_thread
) (ptid_t
);
202 /* The method to call, if any, when the siginfo object needs to be
203 converted between the layout returned by ptrace, and the layout in
204 the architecture of the inferior. */
205 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
209 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
210 Called by our to_xfer_partial. */
211 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
213 const char *, gdb_byte
*,
217 static int debug_linux_nat
;
219 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
220 struct cmd_list_element
*c
, const char *value
)
222 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
226 static int debug_linux_nat_async
= 0;
228 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
229 struct cmd_list_element
*c
, const char *value
)
231 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
235 static int disable_randomization
= 1;
238 show_disable_randomization (struct ui_file
*file
, int from_tty
,
239 struct cmd_list_element
*c
, const char *value
)
241 #ifdef HAVE_PERSONALITY
242 fprintf_filtered (file
, _("\
243 Disabling randomization of debuggee's virtual address space is %s.\n"),
245 #else /* !HAVE_PERSONALITY */
247 Disabling randomization of debuggee's virtual address space is unsupported on\n\
248 this platform.\n"), file
);
249 #endif /* !HAVE_PERSONALITY */
253 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
255 #ifndef HAVE_PERSONALITY
257 Disabling randomization of debuggee's virtual address space is unsupported on\n\
259 #endif /* !HAVE_PERSONALITY */
262 static int linux_parent_pid
;
264 struct simple_pid_list
268 struct simple_pid_list
*next
;
270 struct simple_pid_list
*stopped_pids
;
272 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
273 can not be used, 1 if it can. */
275 static int linux_supports_tracefork_flag
= -1;
277 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
278 PTRACE_O_TRACEVFORKDONE. */
280 static int linux_supports_tracevforkdone_flag
= -1;
282 /* Async mode support */
284 /* Zero if the async mode, although enabled, is masked, which means
285 linux_nat_wait should behave as if async mode was off. */
286 static int linux_nat_async_mask_value
= 1;
288 /* The read/write ends of the pipe registered as waitable file in the
290 static int linux_nat_event_pipe
[2] = { -1, -1 };
292 /* Flush the event pipe. */
295 async_file_flush (void)
302 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
304 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
307 /* Put something (anything, doesn't matter what, or how much) in event
308 pipe, so that the select/poll in the event-loop realizes we have
309 something to process. */
312 async_file_mark (void)
316 /* It doesn't really matter what the pipe contains, as long we end
317 up with something in it. Might as well flush the previous
323 ret
= write (linux_nat_event_pipe
[1], "+", 1);
325 while (ret
== -1 && errno
== EINTR
);
327 /* Ignore EAGAIN. If the pipe is full, the event loop will already
328 be awakened anyway. */
331 static void linux_nat_async (void (*callback
)
332 (enum inferior_event_type event_type
, void *context
),
334 static int linux_nat_async_mask (int mask
);
335 static int kill_lwp (int lwpid
, int signo
);
337 static int stop_callback (struct lwp_info
*lp
, void *data
);
339 static void block_child_signals (sigset_t
*prev_mask
);
340 static void restore_child_signals_mask (sigset_t
*prev_mask
);
343 static struct lwp_info
*add_lwp (ptid_t ptid
);
344 static void purge_lwp_list (int pid
);
345 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
348 /* Trivial list manipulation functions to keep track of a list of
349 new stopped processes. */
351 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
353 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
355 new_pid
->status
= status
;
356 new_pid
->next
= *listp
;
361 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
363 struct simple_pid_list
**p
;
365 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
366 if ((*p
)->pid
== pid
)
368 struct simple_pid_list
*next
= (*p
)->next
;
369 *status
= (*p
)->status
;
378 linux_record_stopped_pid (int pid
, int status
)
380 add_to_pid_list (&stopped_pids
, pid
, status
);
384 /* A helper function for linux_test_for_tracefork, called after fork (). */
387 linux_tracefork_child (void)
391 ptrace (PTRACE_TRACEME
, 0, 0, 0);
392 kill (getpid (), SIGSTOP
);
397 /* Wrapper function for waitpid which handles EINTR. */
400 my_waitpid (int pid
, int *status
, int flags
)
406 ret
= waitpid (pid
, status
, flags
);
408 while (ret
== -1 && errno
== EINTR
);
413 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
415 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
416 we know that the feature is not available. This may change the tracing
417 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
419 However, if it succeeds, we don't know for sure that the feature is
420 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
421 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
422 fork tracing, and let it fork. If the process exits, we assume that we
423 can't use TRACEFORK; if we get the fork notification, and we can extract
424 the new child's PID, then we assume that we can. */
427 linux_test_for_tracefork (int original_pid
)
429 int child_pid
, ret
, status
;
433 /* We don't want those ptrace calls to be interrupted. */
434 block_child_signals (&prev_mask
);
436 linux_supports_tracefork_flag
= 0;
437 linux_supports_tracevforkdone_flag
= 0;
439 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
442 restore_child_signals_mask (&prev_mask
);
448 perror_with_name (("fork"));
451 linux_tracefork_child ();
453 ret
= my_waitpid (child_pid
, &status
, 0);
455 perror_with_name (("waitpid"));
456 else if (ret
!= child_pid
)
457 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
458 if (! WIFSTOPPED (status
))
459 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
461 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
464 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
467 warning (_("linux_test_for_tracefork: failed to kill child"));
468 restore_child_signals_mask (&prev_mask
);
472 ret
= my_waitpid (child_pid
, &status
, 0);
473 if (ret
!= child_pid
)
474 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
475 else if (!WIFSIGNALED (status
))
476 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
477 "killed child"), status
);
479 restore_child_signals_mask (&prev_mask
);
483 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
484 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
485 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
486 linux_supports_tracevforkdone_flag
= (ret
== 0);
488 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
490 warning (_("linux_test_for_tracefork: failed to resume child"));
492 ret
= my_waitpid (child_pid
, &status
, 0);
494 if (ret
== child_pid
&& WIFSTOPPED (status
)
495 && status
>> 16 == PTRACE_EVENT_FORK
)
498 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
499 if (ret
== 0 && second_pid
!= 0)
503 linux_supports_tracefork_flag
= 1;
504 my_waitpid (second_pid
, &second_status
, 0);
505 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
507 warning (_("linux_test_for_tracefork: failed to kill second child"));
508 my_waitpid (second_pid
, &status
, 0);
512 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
513 "(%d, status 0x%x)"), ret
, status
);
515 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
517 warning (_("linux_test_for_tracefork: failed to kill child"));
518 my_waitpid (child_pid
, &status
, 0);
520 restore_child_signals_mask (&prev_mask
);
523 /* Return non-zero iff we have tracefork functionality available.
524 This function also sets linux_supports_tracefork_flag. */
527 linux_supports_tracefork (int pid
)
529 if (linux_supports_tracefork_flag
== -1)
530 linux_test_for_tracefork (pid
);
531 return linux_supports_tracefork_flag
;
535 linux_supports_tracevforkdone (int pid
)
537 if (linux_supports_tracefork_flag
== -1)
538 linux_test_for_tracefork (pid
);
539 return linux_supports_tracevforkdone_flag
;
544 linux_enable_event_reporting (ptid_t ptid
)
546 int pid
= ptid_get_lwp (ptid
);
550 pid
= ptid_get_pid (ptid
);
552 if (! linux_supports_tracefork (pid
))
555 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
556 | PTRACE_O_TRACECLONE
;
557 if (linux_supports_tracevforkdone (pid
))
558 options
|= PTRACE_O_TRACEVFORKDONE
;
560 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
561 read-only process state. */
563 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
567 linux_child_post_attach (int pid
)
569 linux_enable_event_reporting (pid_to_ptid (pid
));
570 check_for_thread_db ();
574 linux_child_post_startup_inferior (ptid_t ptid
)
576 linux_enable_event_reporting (ptid
);
577 check_for_thread_db ();
581 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
585 int parent_pid
, child_pid
;
587 block_child_signals (&prev_mask
);
589 has_vforked
= (inferior_thread ()->pending_follow
.kind
590 == TARGET_WAITKIND_VFORKED
);
591 parent_pid
= ptid_get_lwp (inferior_ptid
);
593 parent_pid
= ptid_get_pid (inferior_ptid
);
594 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
597 linux_enable_event_reporting (pid_to_ptid (child_pid
));
601 /* We're already attached to the parent, by default. */
603 /* Before detaching from the child, remove all breakpoints from
604 it. If we forked, then this has already been taken care of
605 by infrun.c. If we vforked however, any breakpoint inserted
606 in the parent is visible in the child, even those added while
607 stopped in a vfork catchpoint. This won't actually modify
608 the breakpoint list, but will physically remove the
609 breakpoints from the child. This will remove the breakpoints
610 from the parent also, but they'll be reinserted below. */
612 detach_breakpoints (child_pid
);
614 /* Detach new forked process? */
617 if (info_verbose
|| debug_linux_nat
)
619 target_terminal_ours ();
620 fprintf_filtered (gdb_stdlog
,
621 "Detaching after fork from child process %d.\n",
625 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
629 struct inferior
*parent_inf
, *child_inf
;
631 struct cleanup
*old_chain
;
633 /* Add process to GDB's tables. */
634 child_inf
= add_inferior (child_pid
);
636 parent_inf
= current_inferior ();
637 child_inf
->attach_flag
= parent_inf
->attach_flag
;
638 copy_terminal_info (child_inf
, parent_inf
);
640 old_chain
= save_inferior_ptid ();
642 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
643 add_thread (inferior_ptid
);
644 lp
= add_lwp (inferior_ptid
);
647 check_for_thread_db ();
649 do_cleanups (old_chain
);
654 gdb_assert (linux_supports_tracefork_flag
>= 0);
655 if (linux_supports_tracevforkdone (0))
659 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
660 my_waitpid (parent_pid
, &status
, __WALL
);
661 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
662 warning (_("Unexpected waitpid result %06x when waiting for "
663 "vfork-done"), status
);
667 /* We can't insert breakpoints until the child has
668 finished with the shared memory region. We need to
669 wait until that happens. Ideal would be to just
671 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
672 - waitpid (parent_pid, &status, __WALL);
673 However, most architectures can't handle a syscall
674 being traced on the way out if it wasn't traced on
677 We might also think to loop, continuing the child
678 until it exits or gets a SIGTRAP. One problem is
679 that the child might call ptrace with PTRACE_TRACEME.
681 There's no simple and reliable way to figure out when
682 the vforked child will be done with its copy of the
683 shared memory. We could step it out of the syscall,
684 two instructions, let it go, and then single-step the
685 parent once. When we have hardware single-step, this
686 would work; with software single-step it could still
687 be made to work but we'd have to be able to insert
688 single-step breakpoints in the child, and we'd have
689 to insert -just- the single-step breakpoint in the
690 parent. Very awkward.
692 In the end, the best we can do is to make sure it
693 runs for a little while. Hopefully it will be out of
694 range of any breakpoints we reinsert. Usually this
695 is only the single-step breakpoint at vfork's return
701 /* Since we vforked, breakpoints were removed in the parent
702 too. Put them back. */
703 reattach_breakpoints (parent_pid
);
708 struct thread_info
*tp
;
709 struct inferior
*parent_inf
, *child_inf
;
712 /* Before detaching from the parent, remove all breakpoints from it. */
713 remove_breakpoints ();
715 if (info_verbose
|| debug_linux_nat
)
717 target_terminal_ours ();
718 fprintf_filtered (gdb_stdlog
,
719 "Attaching after fork to child process %d.\n",
723 /* Add the new inferior first, so that the target_detach below
724 doesn't unpush the target. */
726 child_inf
= add_inferior (child_pid
);
728 parent_inf
= current_inferior ();
729 child_inf
->attach_flag
= parent_inf
->attach_flag
;
730 copy_terminal_info (child_inf
, parent_inf
);
732 /* If we're vforking, we may want to hold on to the parent until
733 the child exits or execs. At exec time we can remove the old
734 breakpoints from the parent and detach it; at exit time we
735 could do the same (or even, sneakily, resume debugging it - the
736 child's exec has failed, or something similar).
738 This doesn't clean up "properly", because we can't call
739 target_detach, but that's OK; if the current target is "child",
740 then it doesn't need any further cleanups, and lin_lwp will
741 generally not encounter vfork (vfork is defined to fork
744 The holding part is very easy if we have VFORKDONE events;
745 but keeping track of both processes is beyond GDB at the
746 moment. So we don't expose the parent to the rest of GDB.
747 Instead we quietly hold onto it until such time as we can
752 struct lwp_info
*parent_lwp
;
754 linux_parent_pid
= parent_pid
;
756 /* Get rid of the inferior on the core side as well. */
757 inferior_ptid
= null_ptid
;
758 detach_inferior (parent_pid
);
760 /* Also get rid of all its lwps. We will detach from this
761 inferior soon-ish, but, we will still get an exit event
762 reported through waitpid when it exits. If we didn't get
763 rid of the lwps from our list, we would end up reporting
764 the inferior exit to the core, which would then try to
765 mourn a non-existing (from the core's perspective)
767 parent_lwp
= find_lwp_pid (pid_to_ptid (parent_pid
));
768 purge_lwp_list (GET_PID (parent_lwp
->ptid
));
769 linux_parent_pid
= parent_pid
;
771 else if (detach_fork
)
772 target_detach (NULL
, 0);
774 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
775 add_thread (inferior_ptid
);
776 lp
= add_lwp (inferior_ptid
);
779 check_for_thread_db ();
782 restore_child_signals_mask (&prev_mask
);
788 linux_child_insert_fork_catchpoint (int pid
)
790 if (! linux_supports_tracefork (pid
))
791 error (_("Your system does not support fork catchpoints."));
795 linux_child_insert_vfork_catchpoint (int pid
)
797 if (!linux_supports_tracefork (pid
))
798 error (_("Your system does not support vfork catchpoints."));
802 linux_child_insert_exec_catchpoint (int pid
)
804 if (!linux_supports_tracefork (pid
))
805 error (_("Your system does not support exec catchpoints."));
808 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
809 are processes sharing the same VM space. A multi-threaded process
810 is basically a group of such processes. However, such a grouping
811 is almost entirely a user-space issue; the kernel doesn't enforce
812 such a grouping at all (this might change in the future). In
813 general, we'll rely on the threads library (i.e. the GNU/Linux
814 Threads library) to provide such a grouping.
816 It is perfectly well possible to write a multi-threaded application
817 without the assistance of a threads library, by using the clone
818 system call directly. This module should be able to give some
819 rudimentary support for debugging such applications if developers
820 specify the CLONE_PTRACE flag in the clone system call, and are
821 using the Linux kernel 2.4 or above.
823 Note that there are some peculiarities in GNU/Linux that affect
826 - In general one should specify the __WCLONE flag to waitpid in
827 order to make it report events for any of the cloned processes
828 (and leave it out for the initial process). However, if a cloned
829 process has exited the exit status is only reported if the
830 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
831 we cannot use it since GDB must work on older systems too.
833 - When a traced, cloned process exits and is waited for by the
834 debugger, the kernel reassigns it to the original parent and
835 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
836 library doesn't notice this, which leads to the "zombie problem":
837 When debugged a multi-threaded process that spawns a lot of
838 threads will run out of processes, even if the threads exit,
839 because the "zombies" stay around. */
841 /* List of known LWPs. */
842 struct lwp_info
*lwp_list
;
845 /* Original signal mask. */
846 static sigset_t normal_mask
;
848 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
849 _initialize_linux_nat. */
850 static sigset_t suspend_mask
;
852 /* Signals to block to make that sigsuspend work. */
853 static sigset_t blocked_mask
;
855 /* SIGCHLD action. */
856 struct sigaction sigchld_action
;
858 /* Block child signals (SIGCHLD and linux threads signals), and store
859 the previous mask in PREV_MASK. */
862 block_child_signals (sigset_t
*prev_mask
)
864 /* Make sure SIGCHLD is blocked. */
865 if (!sigismember (&blocked_mask
, SIGCHLD
))
866 sigaddset (&blocked_mask
, SIGCHLD
);
868 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
871 /* Restore child signals mask, previously returned by
872 block_child_signals. */
875 restore_child_signals_mask (sigset_t
*prev_mask
)
877 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
881 /* Prototypes for local functions. */
882 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
883 static int linux_thread_alive (ptid_t ptid
);
884 static char *linux_child_pid_to_exec_file (int pid
);
885 static int cancel_breakpoint (struct lwp_info
*lp
);
888 /* Convert wait status STATUS to a string. Used for printing debug
892 status_to_str (int status
)
896 if (WIFSTOPPED (status
))
897 snprintf (buf
, sizeof (buf
), "%s (stopped)",
898 strsignal (WSTOPSIG (status
)));
899 else if (WIFSIGNALED (status
))
900 snprintf (buf
, sizeof (buf
), "%s (terminated)",
901 strsignal (WSTOPSIG (status
)));
903 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
908 /* Initialize the list of LWPs. Note that this module, contrary to
909 what GDB's generic threads layer does for its thread list,
910 re-initializes the LWP lists whenever we mourn or detach (which
911 doesn't involve mourning) the inferior. */
916 struct lwp_info
*lp
, *lpnext
;
918 for (lp
= lwp_list
; lp
; lp
= lpnext
)
927 /* Remove all LWPs belong to PID from the lwp list. */
930 purge_lwp_list (int pid
)
932 struct lwp_info
*lp
, *lpprev
, *lpnext
;
936 for (lp
= lwp_list
; lp
; lp
= lpnext
)
940 if (ptid_get_pid (lp
->ptid
) == pid
)
945 lpprev
->next
= lp
->next
;
954 /* Return the number of known LWPs in the tgid given by PID. */
962 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
963 if (ptid_get_pid (lp
->ptid
) == pid
)
969 /* Add the LWP specified by PID to the list. Return a pointer to the
970 structure describing the new LWP. The LWP should already be stopped
971 (with an exception for the very first LWP). */
973 static struct lwp_info
*
974 add_lwp (ptid_t ptid
)
978 gdb_assert (is_lwp (ptid
));
980 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
982 memset (lp
, 0, sizeof (struct lwp_info
));
984 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
991 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
992 linux_nat_new_thread (ptid
);
997 /* Remove the LWP specified by PID from the list. */
1000 delete_lwp (ptid_t ptid
)
1002 struct lwp_info
*lp
, *lpprev
;
1006 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1007 if (ptid_equal (lp
->ptid
, ptid
))
1014 lpprev
->next
= lp
->next
;
1016 lwp_list
= lp
->next
;
1021 /* Return a pointer to the structure describing the LWP corresponding
1022 to PID. If no corresponding LWP could be found, return NULL. */
1024 static struct lwp_info
*
1025 find_lwp_pid (ptid_t ptid
)
1027 struct lwp_info
*lp
;
1031 lwp
= GET_LWP (ptid
);
1033 lwp
= GET_PID (ptid
);
1035 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1036 if (lwp
== GET_LWP (lp
->ptid
))
1042 /* Returns true if PTID matches filter FILTER. FILTER can be the wild
1043 card MINUS_ONE_PTID (all ptid match it); can be a ptid representing
1044 a process (ptid_is_pid returns true), in which case, all lwps of
1045 that give process match, lwps of other process do not; or, it can
1046 represent a specific thread, in which case, only that thread will
1047 match true. PTID must represent an LWP, it can never be a wild
1051 ptid_match (ptid_t ptid
, ptid_t filter
)
1053 /* Since both parameters have the same type, prevent easy mistakes
1055 gdb_assert (!ptid_equal (ptid
, minus_one_ptid
)
1056 && !ptid_equal (ptid
, null_ptid
));
1058 if (ptid_equal (filter
, minus_one_ptid
))
1060 if (ptid_is_pid (filter
)
1061 && ptid_get_pid (ptid
) == ptid_get_pid (filter
))
1063 else if (ptid_equal (ptid
, filter
))
1069 /* Call CALLBACK with its second argument set to DATA for every LWP in
1070 the list. If CALLBACK returns 1 for a particular LWP, return a
1071 pointer to the structure describing that LWP immediately.
1072 Otherwise return NULL. */
1075 iterate_over_lwps (ptid_t filter
,
1076 int (*callback
) (struct lwp_info
*, void *),
1079 struct lwp_info
*lp
, *lpnext
;
1081 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1085 if (ptid_match (lp
->ptid
, filter
))
1087 if ((*callback
) (lp
, data
))
1095 /* Update our internal state when changing from one checkpoint to
1096 another indicated by NEW_PTID. We can only switch single-threaded
1097 applications, so we only create one new LWP, and the previous list
1101 linux_nat_switch_fork (ptid_t new_ptid
)
1103 struct lwp_info
*lp
;
1105 purge_lwp_list (GET_PID (inferior_ptid
));
1107 lp
= add_lwp (new_ptid
);
1110 /* This changes the thread's ptid while preserving the gdb thread
1111 num. Also changes the inferior pid, while preserving the
1113 thread_change_ptid (inferior_ptid
, new_ptid
);
1115 /* We've just told GDB core that the thread changed target id, but,
1116 in fact, it really is a different thread, with different register
1118 registers_changed ();
1121 /* Handle the exit of a single thread LP. */
1124 exit_lwp (struct lwp_info
*lp
)
1126 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1130 if (print_thread_events
)
1131 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1133 delete_thread (lp
->ptid
);
1136 delete_lwp (lp
->ptid
);
1139 /* Return an lwp's tgid, found in `/proc/PID/status'. */
1142 linux_proc_get_tgid (int lwpid
)
1148 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
1149 status_file
= fopen (buf
, "r");
1150 if (status_file
!= NULL
)
1152 while (fgets (buf
, sizeof (buf
), status_file
))
1154 if (strncmp (buf
, "Tgid:", 5) == 0)
1156 tgid
= strtoul (buf
+ strlen ("Tgid:"), NULL
, 10);
1161 fclose (status_file
);
1167 /* Detect `T (stopped)' in `/proc/PID/status'.
1168 Other states including `T (tracing stop)' are reported as false. */
1171 pid_is_stopped (pid_t pid
)
1177 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1178 status_file
= fopen (buf
, "r");
1179 if (status_file
!= NULL
)
1183 while (fgets (buf
, sizeof (buf
), status_file
))
1185 if (strncmp (buf
, "State:", 6) == 0)
1191 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1193 fclose (status_file
);
1198 /* Wait for the LWP specified by LP, which we have just attached to.
1199 Returns a wait status for that LWP, to cache. */
1202 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1205 pid_t new_pid
, pid
= GET_LWP (ptid
);
1208 if (pid_is_stopped (pid
))
1210 if (debug_linux_nat
)
1211 fprintf_unfiltered (gdb_stdlog
,
1212 "LNPAW: Attaching to a stopped process\n");
1214 /* The process is definitely stopped. It is in a job control
1215 stop, unless the kernel predates the TASK_STOPPED /
1216 TASK_TRACED distinction, in which case it might be in a
1217 ptrace stop. Make sure it is in a ptrace stop; from there we
1218 can kill it, signal it, et cetera.
1220 First make sure there is a pending SIGSTOP. Since we are
1221 already attached, the process can not transition from stopped
1222 to running without a PTRACE_CONT; so we know this signal will
1223 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1224 probably already in the queue (unless this kernel is old
1225 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1226 is not an RT signal, it can only be queued once. */
1227 kill_lwp (pid
, SIGSTOP
);
1229 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1230 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1231 ptrace (PTRACE_CONT
, pid
, 0, 0);
1234 /* Make sure the initial process is stopped. The user-level threads
1235 layer might want to poke around in the inferior, and that won't
1236 work if things haven't stabilized yet. */
1237 new_pid
= my_waitpid (pid
, &status
, 0);
1238 if (new_pid
== -1 && errno
== ECHILD
)
1241 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1243 /* Try again with __WCLONE to check cloned processes. */
1244 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1248 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1250 if (WSTOPSIG (status
) != SIGSTOP
)
1253 if (debug_linux_nat
)
1254 fprintf_unfiltered (gdb_stdlog
,
1255 "LNPAW: Received %s after attaching\n",
1256 status_to_str (status
));
1262 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1263 if the new LWP could not be attached. */
1266 lin_lwp_attach_lwp (ptid_t ptid
)
1268 struct lwp_info
*lp
;
1271 gdb_assert (is_lwp (ptid
));
1273 block_child_signals (&prev_mask
);
1275 lp
= find_lwp_pid (ptid
);
1277 /* We assume that we're already attached to any LWP that has an id
1278 equal to the overall process id, and to any LWP that is already
1279 in our list of LWPs. If we're not seeing exit events from threads
1280 and we've had PID wraparound since we last tried to stop all threads,
1281 this assumption might be wrong; fortunately, this is very unlikely
1283 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1285 int status
, cloned
= 0, signalled
= 0;
1287 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1289 /* If we fail to attach to the thread, issue a warning,
1290 but continue. One way this can happen is if thread
1291 creation is interrupted; as of Linux kernel 2.6.19, a
1292 bug may place threads in the thread list and then fail
1294 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1295 safe_strerror (errno
));
1296 restore_child_signals_mask (&prev_mask
);
1300 if (debug_linux_nat
)
1301 fprintf_unfiltered (gdb_stdlog
,
1302 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1303 target_pid_to_str (ptid
));
1305 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1306 lp
= add_lwp (ptid
);
1308 lp
->cloned
= cloned
;
1309 lp
->signalled
= signalled
;
1310 if (WSTOPSIG (status
) != SIGSTOP
)
1313 lp
->status
= status
;
1316 target_post_attach (GET_LWP (lp
->ptid
));
1318 if (debug_linux_nat
)
1320 fprintf_unfiltered (gdb_stdlog
,
1321 "LLAL: waitpid %s received %s\n",
1322 target_pid_to_str (ptid
),
1323 status_to_str (status
));
1328 /* We assume that the LWP representing the original process is
1329 already stopped. Mark it as stopped in the data structure
1330 that the GNU/linux ptrace layer uses to keep track of
1331 threads. Note that this won't have already been done since
1332 the main thread will have, we assume, been stopped by an
1333 attach from a different layer. */
1335 lp
= add_lwp (ptid
);
1339 restore_child_signals_mask (&prev_mask
);
1344 linux_nat_create_inferior (struct target_ops
*ops
,
1345 char *exec_file
, char *allargs
, char **env
,
1348 #ifdef HAVE_PERSONALITY
1349 int personality_orig
= 0, personality_set
= 0;
1350 #endif /* HAVE_PERSONALITY */
1352 /* The fork_child mechanism is synchronous and calls target_wait, so
1353 we have to mask the async mode. */
1355 #ifdef HAVE_PERSONALITY
1356 if (disable_randomization
)
1359 personality_orig
= personality (0xffffffff);
1360 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1362 personality_set
= 1;
1363 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1365 if (errno
!= 0 || (personality_set
1366 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1367 warning (_("Error disabling address space randomization: %s"),
1368 safe_strerror (errno
));
1370 #endif /* HAVE_PERSONALITY */
1372 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1374 #ifdef HAVE_PERSONALITY
1375 if (personality_set
)
1378 personality (personality_orig
);
1380 warning (_("Error restoring address space randomization: %s"),
1381 safe_strerror (errno
));
1383 #endif /* HAVE_PERSONALITY */
1387 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1389 struct lwp_info
*lp
;
1393 linux_ops
->to_attach (ops
, args
, from_tty
);
1395 /* The ptrace base target adds the main thread with (pid,0,0)
1396 format. Decorate it with lwp info. */
1397 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1398 thread_change_ptid (inferior_ptid
, ptid
);
1400 /* Add the initial process as the first LWP to the list. */
1401 lp
= add_lwp (ptid
);
1403 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1407 /* Save the wait status to report later. */
1409 if (debug_linux_nat
)
1410 fprintf_unfiltered (gdb_stdlog
,
1411 "LNA: waitpid %ld, saving status %s\n",
1412 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1414 lp
->status
= status
;
1416 if (target_can_async_p ())
1417 target_async (inferior_event_handler
, 0);
1420 /* Get pending status of LP. */
1422 get_pending_status (struct lwp_info
*lp
, int *status
)
1424 struct target_waitstatus last
;
1427 get_last_target_status (&last_ptid
, &last
);
1429 /* If this lwp is the ptid that GDB is processing an event from, the
1430 signal will be in stop_signal. Otherwise, we may cache pending
1431 events in lp->status while trying to stop all threads (see
1432 stop_wait_callback). */
1438 enum target_signal signo
= TARGET_SIGNAL_0
;
1440 if (is_executing (lp
->ptid
))
1442 /* If the core thought this lwp was executing --- e.g., the
1443 executing property hasn't been updated yet, but the
1444 thread has been stopped with a stop_callback /
1445 stop_wait_callback sequence (see linux_nat_detach for
1446 example) --- we can only have pending events in the local
1448 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1452 /* If the core knows the thread is not executing, then we
1453 have the last signal recorded in
1454 thread_info->stop_signal. */
1456 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1457 signo
= tp
->stop_signal
;
1460 if (signo
!= TARGET_SIGNAL_0
1461 && !signal_pass_state (signo
))
1463 if (debug_linux_nat
)
1464 fprintf_unfiltered (gdb_stdlog
, "\
1465 GPT: lwp %s had signal %s, but it is in no pass state\n",
1466 target_pid_to_str (lp
->ptid
),
1467 target_signal_to_string (signo
));
1471 if (signo
!= TARGET_SIGNAL_0
)
1472 *status
= W_STOPCODE (target_signal_to_host (signo
));
1474 if (debug_linux_nat
)
1475 fprintf_unfiltered (gdb_stdlog
,
1476 "GPT: lwp %s as pending signal %s\n",
1477 target_pid_to_str (lp
->ptid
),
1478 target_signal_to_string (signo
));
1483 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1485 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1486 if (tp
->stop_signal
!= TARGET_SIGNAL_0
1487 && signal_pass_state (tp
->stop_signal
))
1488 *status
= W_STOPCODE (target_signal_to_host (tp
->stop_signal
));
1491 *status
= lp
->status
;
1498 detach_callback (struct lwp_info
*lp
, void *data
)
1500 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1502 if (debug_linux_nat
&& lp
->status
)
1503 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1504 strsignal (WSTOPSIG (lp
->status
)),
1505 target_pid_to_str (lp
->ptid
));
1507 /* If there is a pending SIGSTOP, get rid of it. */
1510 if (debug_linux_nat
)
1511 fprintf_unfiltered (gdb_stdlog
,
1512 "DC: Sending SIGCONT to %s\n",
1513 target_pid_to_str (lp
->ptid
));
1515 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1519 /* We don't actually detach from the LWP that has an id equal to the
1520 overall process id just yet. */
1521 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1525 /* Pass on any pending signal for this LWP. */
1526 get_pending_status (lp
, &status
);
1529 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1530 WSTOPSIG (status
)) < 0)
1531 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1532 safe_strerror (errno
));
1534 if (debug_linux_nat
)
1535 fprintf_unfiltered (gdb_stdlog
,
1536 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1537 target_pid_to_str (lp
->ptid
),
1538 strsignal (WSTOPSIG (status
)));
1540 delete_lwp (lp
->ptid
);
1547 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1551 enum target_signal sig
;
1552 struct lwp_info
*main_lwp
;
1554 pid
= GET_PID (inferior_ptid
);
1556 if (target_can_async_p ())
1557 linux_nat_async (NULL
, 0);
1559 /* Stop all threads before detaching. ptrace requires that the
1560 thread is stopped to sucessfully detach. */
1561 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1562 /* ... and wait until all of them have reported back that
1563 they're no longer running. */
1564 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1566 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1568 /* Only the initial process should be left right now. */
1569 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1571 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1573 /* Pass on any pending signal for the last LWP. */
1574 if ((args
== NULL
|| *args
== '\0')
1575 && get_pending_status (main_lwp
, &status
) != -1
1576 && WIFSTOPPED (status
))
1578 /* Put the signal number in ARGS so that inf_ptrace_detach will
1579 pass it along with PTRACE_DETACH. */
1581 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1582 fprintf_unfiltered (gdb_stdlog
,
1583 "LND: Sending signal %s to %s\n",
1585 target_pid_to_str (main_lwp
->ptid
));
1588 delete_lwp (main_lwp
->ptid
);
1590 if (forks_exist_p ())
1592 /* Multi-fork case. The current inferior_ptid is being detached
1593 from, but there are other viable forks to debug. Detach from
1594 the current fork, and context-switch to the first
1596 linux_fork_detach (args
, from_tty
);
1598 if (non_stop
&& target_can_async_p ())
1599 target_async (inferior_event_handler
, 0);
1602 linux_ops
->to_detach (ops
, args
, from_tty
);
1608 resume_callback (struct lwp_info
*lp
, void *data
)
1610 if (lp
->stopped
&& lp
->status
== 0)
1612 if (debug_linux_nat
)
1613 fprintf_unfiltered (gdb_stdlog
,
1614 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1615 target_pid_to_str (lp
->ptid
));
1617 linux_ops
->to_resume (linux_ops
,
1618 pid_to_ptid (GET_LWP (lp
->ptid
)),
1619 0, TARGET_SIGNAL_0
);
1620 if (debug_linux_nat
)
1621 fprintf_unfiltered (gdb_stdlog
,
1622 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1623 target_pid_to_str (lp
->ptid
));
1626 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1628 else if (lp
->stopped
&& debug_linux_nat
)
1629 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1630 target_pid_to_str (lp
->ptid
));
1631 else if (debug_linux_nat
)
1632 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1633 target_pid_to_str (lp
->ptid
));
1639 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1646 resume_set_callback (struct lwp_info
*lp
, void *data
)
1653 linux_nat_resume (struct target_ops
*ops
,
1654 ptid_t ptid
, int step
, enum target_signal signo
)
1657 struct lwp_info
*lp
;
1660 if (debug_linux_nat
)
1661 fprintf_unfiltered (gdb_stdlog
,
1662 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1663 step
? "step" : "resume",
1664 target_pid_to_str (ptid
),
1665 signo
? strsignal (signo
) : "0",
1666 target_pid_to_str (inferior_ptid
));
1668 block_child_signals (&prev_mask
);
1670 /* A specific PTID means `step only this process id'. */
1671 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1672 || ptid_is_pid (ptid
));
1676 /* Mark the lwps we're resuming as resumed. */
1677 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
1678 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1681 iterate_over_lwps (minus_one_ptid
, resume_set_callback
, NULL
);
1683 /* See if it's the current inferior that should be handled
1686 lp
= find_lwp_pid (inferior_ptid
);
1688 lp
= find_lwp_pid (ptid
);
1689 gdb_assert (lp
!= NULL
);
1691 /* Remember if we're stepping. */
1694 /* If we have a pending wait status for this thread, there is no
1695 point in resuming the process. But first make sure that
1696 linux_nat_wait won't preemptively handle the event - we
1697 should never take this short-circuit if we are going to
1698 leave LP running, since we have skipped resuming all the
1699 other threads. This bit of code needs to be synchronized
1700 with linux_nat_wait. */
1702 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1705 struct inferior
*inf
;
1707 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
1709 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1711 /* Defer to common code if we're gaining control of the
1713 if (inf
->stop_soon
== NO_STOP_QUIETLY
1714 && signal_stop_state (saved_signo
) == 0
1715 && signal_print_state (saved_signo
) == 0
1716 && signal_pass_state (saved_signo
) == 1)
1718 if (debug_linux_nat
)
1719 fprintf_unfiltered (gdb_stdlog
,
1720 "LLR: Not short circuiting for ignored "
1721 "status 0x%x\n", lp
->status
);
1723 /* FIXME: What should we do if we are supposed to continue
1724 this thread with a signal? */
1725 gdb_assert (signo
== TARGET_SIGNAL_0
);
1726 signo
= saved_signo
;
1733 /* FIXME: What should we do if we are supposed to continue
1734 this thread with a signal? */
1735 gdb_assert (signo
== TARGET_SIGNAL_0
);
1737 if (debug_linux_nat
)
1738 fprintf_unfiltered (gdb_stdlog
,
1739 "LLR: Short circuiting for status 0x%x\n",
1742 restore_child_signals_mask (&prev_mask
);
1743 if (target_can_async_p ())
1745 target_async (inferior_event_handler
, 0);
1746 /* Tell the event loop we have something to process. */
1752 /* Mark LWP as not stopped to prevent it from being continued by
1757 iterate_over_lwps (ptid
, resume_callback
, NULL
);
1759 /* Convert to something the lower layer understands. */
1760 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1762 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
1763 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1765 if (debug_linux_nat
)
1766 fprintf_unfiltered (gdb_stdlog
,
1767 "LLR: %s %s, %s (resume event thread)\n",
1768 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1769 target_pid_to_str (ptid
),
1770 signo
? strsignal (signo
) : "0");
1772 restore_child_signals_mask (&prev_mask
);
1773 if (target_can_async_p ())
1774 target_async (inferior_event_handler
, 0);
1777 /* Issue kill to specified lwp. */
1779 static int tkill_failed
;
1782 kill_lwp (int lwpid
, int signo
)
1786 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1787 fails, then we are not using nptl threads and we should be using kill. */
1789 #ifdef HAVE_TKILL_SYSCALL
1792 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1793 if (errno
!= ENOSYS
)
1800 return kill (lwpid
, signo
);
1803 /* Handle a GNU/Linux extended wait response. If we see a clone
1804 event, we need to add the new LWP to our list (and not report the
1805 trap to higher layers). This function returns non-zero if the
1806 event should be ignored and we should wait again. If STOPPING is
1807 true, the new LWP remains stopped, otherwise it is continued. */
1810 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1813 int pid
= GET_LWP (lp
->ptid
);
1814 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1815 struct lwp_info
*new_lp
= NULL
;
1816 int event
= status
>> 16;
1818 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1819 || event
== PTRACE_EVENT_CLONE
)
1821 unsigned long new_pid
;
1824 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1826 /* If we haven't already seen the new PID stop, wait for it now. */
1827 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1829 /* The new child has a pending SIGSTOP. We can't affect it until it
1830 hits the SIGSTOP, but we're already attached. */
1831 ret
= my_waitpid (new_pid
, &status
,
1832 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1834 perror_with_name (_("waiting for new child"));
1835 else if (ret
!= new_pid
)
1836 internal_error (__FILE__
, __LINE__
,
1837 _("wait returned unexpected PID %d"), ret
);
1838 else if (!WIFSTOPPED (status
))
1839 internal_error (__FILE__
, __LINE__
,
1840 _("wait returned unexpected status 0x%x"), status
);
1843 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1845 if (event
== PTRACE_EVENT_FORK
1846 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
1848 struct fork_info
*fp
;
1850 /* Handle checkpointing by linux-fork.c here as a special
1851 case. We don't want the follow-fork-mode or 'catch fork'
1852 to interfere with this. */
1854 /* This won't actually modify the breakpoint list, but will
1855 physically remove the breakpoints from the child. */
1856 detach_breakpoints (new_pid
);
1858 /* Retain child fork in ptrace (stopped) state. */
1859 fp
= find_fork_pid (new_pid
);
1861 fp
= add_fork (new_pid
);
1863 /* Report as spurious, so that infrun doesn't want to follow
1864 this fork. We're actually doing an infcall in
1866 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1867 linux_enable_event_reporting (pid_to_ptid (new_pid
));
1869 /* Report the stop to the core. */
1873 if (event
== PTRACE_EVENT_FORK
)
1874 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1875 else if (event
== PTRACE_EVENT_VFORK
)
1876 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1879 struct cleanup
*old_chain
;
1881 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1882 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
1884 new_lp
->stopped
= 1;
1886 if (WSTOPSIG (status
) != SIGSTOP
)
1888 /* This can happen if someone starts sending signals to
1889 the new thread before it gets a chance to run, which
1890 have a lower number than SIGSTOP (e.g. SIGUSR1).
1891 This is an unlikely case, and harder to handle for
1892 fork / vfork than for clone, so we do not try - but
1893 we handle it for clone events here. We'll send
1894 the other signal on to the thread below. */
1896 new_lp
->signalled
= 1;
1903 /* Add the new thread to GDB's lists as soon as possible
1906 1) the frontend doesn't have to wait for a stop to
1909 2) we tag it with the correct running state. */
1911 /* If the thread_db layer is active, let it know about
1912 this new thread, and add it to GDB's list. */
1913 if (!thread_db_attach_lwp (new_lp
->ptid
))
1915 /* We're not using thread_db. Add it to GDB's
1917 target_post_attach (GET_LWP (new_lp
->ptid
));
1918 add_thread (new_lp
->ptid
);
1923 set_running (new_lp
->ptid
, 1);
1924 set_executing (new_lp
->ptid
, 1);
1930 new_lp
->stopped
= 0;
1931 new_lp
->resumed
= 1;
1932 ptrace (PTRACE_CONT
, new_pid
, 0,
1933 status
? WSTOPSIG (status
) : 0);
1936 if (debug_linux_nat
)
1937 fprintf_unfiltered (gdb_stdlog
,
1938 "LHEW: Got clone event from LWP %ld, resuming\n",
1939 GET_LWP (lp
->ptid
));
1940 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1948 if (event
== PTRACE_EVENT_EXEC
)
1950 if (debug_linux_nat
)
1951 fprintf_unfiltered (gdb_stdlog
,
1952 "LHEW: Got exec event from LWP %ld\n",
1953 GET_LWP (lp
->ptid
));
1955 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1956 ourstatus
->value
.execd_pathname
1957 = xstrdup (linux_child_pid_to_exec_file (pid
));
1959 if (linux_parent_pid
)
1961 detach_breakpoints (linux_parent_pid
);
1962 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1964 linux_parent_pid
= 0;
1967 /* At this point, all inserted breakpoints are gone. Doing this
1968 as soon as we detect an exec prevents the badness of deleting
1969 a breakpoint writing the current "shadow contents" to lift
1970 the bp. That shadow is NOT valid after an exec.
1972 Note that we have to do this after the detach_breakpoints
1973 call above, otherwise breakpoints wouldn't be lifted from the
1974 parent on a vfork, because detach_breakpoints would think
1975 that breakpoints are not inserted. */
1976 mark_breakpoints_out ();
1980 internal_error (__FILE__
, __LINE__
,
1981 _("unknown ptrace event %d"), event
);
1984 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1988 wait_lwp (struct lwp_info
*lp
)
1992 int thread_dead
= 0;
1994 gdb_assert (!lp
->stopped
);
1995 gdb_assert (lp
->status
== 0);
1997 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1998 if (pid
== -1 && errno
== ECHILD
)
2000 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
2001 if (pid
== -1 && errno
== ECHILD
)
2003 /* The thread has previously exited. We need to delete it
2004 now because, for some vendor 2.4 kernels with NPTL
2005 support backported, there won't be an exit event unless
2006 it is the main thread. 2.6 kernels will report an exit
2007 event for each thread that exits, as expected. */
2009 if (debug_linux_nat
)
2010 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2011 target_pid_to_str (lp
->ptid
));
2017 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2019 if (debug_linux_nat
)
2021 fprintf_unfiltered (gdb_stdlog
,
2022 "WL: waitpid %s received %s\n",
2023 target_pid_to_str (lp
->ptid
),
2024 status_to_str (status
));
2028 /* Check if the thread has exited. */
2029 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2032 if (debug_linux_nat
)
2033 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2034 target_pid_to_str (lp
->ptid
));
2043 gdb_assert (WIFSTOPPED (status
));
2045 /* Handle GNU/Linux's extended waitstatus for trace events. */
2046 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2048 if (debug_linux_nat
)
2049 fprintf_unfiltered (gdb_stdlog
,
2050 "WL: Handling extended status 0x%06x\n",
2052 if (linux_handle_extended_wait (lp
, status
, 1))
2053 return wait_lwp (lp
);
2059 /* Save the most recent siginfo for LP. This is currently only called
2060 for SIGTRAP; some ports use the si_addr field for
2061 target_stopped_data_address. In the future, it may also be used to
2062 restore the siginfo of requeued signals. */
2065 save_siginfo (struct lwp_info
*lp
)
2068 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2069 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2072 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2075 /* Send a SIGSTOP to LP. */
2078 stop_callback (struct lwp_info
*lp
, void *data
)
2080 if (!lp
->stopped
&& !lp
->signalled
)
2084 if (debug_linux_nat
)
2086 fprintf_unfiltered (gdb_stdlog
,
2087 "SC: kill %s **<SIGSTOP>**\n",
2088 target_pid_to_str (lp
->ptid
));
2091 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2092 if (debug_linux_nat
)
2094 fprintf_unfiltered (gdb_stdlog
,
2095 "SC: lwp kill %d %s\n",
2097 errno
? safe_strerror (errno
) : "ERRNO-OK");
2101 gdb_assert (lp
->status
== 0);
2107 /* Return non-zero if LWP PID has a pending SIGINT. */
2110 linux_nat_has_pending_sigint (int pid
)
2112 sigset_t pending
, blocked
, ignored
;
2115 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2117 if (sigismember (&pending
, SIGINT
)
2118 && !sigismember (&ignored
, SIGINT
))
2124 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2127 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2129 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2130 flag to consume the next one. */
2131 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2132 && WSTOPSIG (lp
->status
) == SIGINT
)
2135 lp
->ignore_sigint
= 1;
2140 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2141 This function is called after we know the LWP has stopped; if the LWP
2142 stopped before the expected SIGINT was delivered, then it will never have
2143 arrived. Also, if the signal was delivered to a shared queue and consumed
2144 by a different thread, it will never be delivered to this LWP. */
2147 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2149 if (!lp
->ignore_sigint
)
2152 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2154 if (debug_linux_nat
)
2155 fprintf_unfiltered (gdb_stdlog
,
2156 "MCIS: Clearing bogus flag for %s\n",
2157 target_pid_to_str (lp
->ptid
));
2158 lp
->ignore_sigint
= 0;
2162 /* Wait until LP is stopped. */
2165 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2171 status
= wait_lwp (lp
);
2175 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2176 && WSTOPSIG (status
) == SIGINT
)
2178 lp
->ignore_sigint
= 0;
2181 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2182 if (debug_linux_nat
)
2183 fprintf_unfiltered (gdb_stdlog
,
2184 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2185 target_pid_to_str (lp
->ptid
),
2186 errno
? safe_strerror (errno
) : "OK");
2188 return stop_wait_callback (lp
, NULL
);
2191 maybe_clear_ignore_sigint (lp
);
2193 if (WSTOPSIG (status
) != SIGSTOP
)
2195 if (WSTOPSIG (status
) == SIGTRAP
)
2197 /* If a LWP other than the LWP that we're reporting an
2198 event for has hit a GDB breakpoint (as opposed to
2199 some random trap signal), then just arrange for it to
2200 hit it again later. We don't keep the SIGTRAP status
2201 and don't forward the SIGTRAP signal to the LWP. We
2202 will handle the current event, eventually we will
2203 resume all LWPs, and this one will get its breakpoint
2206 If we do not do this, then we run the risk that the
2207 user will delete or disable the breakpoint, but the
2208 thread will have already tripped on it. */
2210 /* Save the trap's siginfo in case we need it later. */
2213 /* Now resume this LWP and get the SIGSTOP event. */
2215 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2216 if (debug_linux_nat
)
2218 fprintf_unfiltered (gdb_stdlog
,
2219 "PTRACE_CONT %s, 0, 0 (%s)\n",
2220 target_pid_to_str (lp
->ptid
),
2221 errno
? safe_strerror (errno
) : "OK");
2223 fprintf_unfiltered (gdb_stdlog
,
2224 "SWC: Candidate SIGTRAP event in %s\n",
2225 target_pid_to_str (lp
->ptid
));
2227 /* Hold this event/waitstatus while we check to see if
2228 there are any more (we still want to get that SIGSTOP). */
2229 stop_wait_callback (lp
, NULL
);
2231 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2232 there's another event, throw it back into the
2236 if (debug_linux_nat
)
2237 fprintf_unfiltered (gdb_stdlog
,
2238 "SWC: kill %s, %s\n",
2239 target_pid_to_str (lp
->ptid
),
2240 status_to_str ((int) status
));
2241 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2244 /* Save the sigtrap event. */
2245 lp
->status
= status
;
2250 /* The thread was stopped with a signal other than
2251 SIGSTOP, and didn't accidentally trip a breakpoint. */
2253 if (debug_linux_nat
)
2255 fprintf_unfiltered (gdb_stdlog
,
2256 "SWC: Pending event %s in %s\n",
2257 status_to_str ((int) status
),
2258 target_pid_to_str (lp
->ptid
));
2260 /* Now resume this LWP and get the SIGSTOP event. */
2262 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2263 if (debug_linux_nat
)
2264 fprintf_unfiltered (gdb_stdlog
,
2265 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2266 target_pid_to_str (lp
->ptid
),
2267 errno
? safe_strerror (errno
) : "OK");
2269 /* Hold this event/waitstatus while we check to see if
2270 there are any more (we still want to get that SIGSTOP). */
2271 stop_wait_callback (lp
, NULL
);
2273 /* If the lp->status field is still empty, use it to
2274 hold this event. If not, then this event must be
2275 returned to the event queue of the LWP. */
2278 if (debug_linux_nat
)
2280 fprintf_unfiltered (gdb_stdlog
,
2281 "SWC: kill %s, %s\n",
2282 target_pid_to_str (lp
->ptid
),
2283 status_to_str ((int) status
));
2285 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2288 lp
->status
= status
;
2294 /* We caught the SIGSTOP that we intended to catch, so
2295 there's no SIGSTOP pending. */
2304 /* Return non-zero if LP has a wait status pending. */
2307 status_callback (struct lwp_info
*lp
, void *data
)
2309 /* Only report a pending wait status if we pretend that this has
2310 indeed been resumed. */
2311 /* We check for lp->waitstatus in addition to lp->status, because we
2312 can have pending process exits recorded in lp->waitstatus, and
2313 W_EXITCODE(0,0) == 0. */
2314 return ((lp
->status
!= 0
2315 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2319 /* Return non-zero if LP isn't stopped. */
2322 running_callback (struct lwp_info
*lp
, void *data
)
2324 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2327 /* Count the LWP's that have had events. */
2330 count_events_callback (struct lwp_info
*lp
, void *data
)
2334 gdb_assert (count
!= NULL
);
2336 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2337 if (lp
->status
!= 0 && lp
->resumed
2338 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2344 /* Select the LWP (if any) that is currently being single-stepped. */
2347 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2349 if (lp
->step
&& lp
->status
!= 0)
2355 /* Select the Nth LWP that has had a SIGTRAP event. */
2358 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2360 int *selector
= data
;
2362 gdb_assert (selector
!= NULL
);
2364 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2365 if (lp
->status
!= 0 && lp
->resumed
2366 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2367 if ((*selector
)-- == 0)
2374 cancel_breakpoint (struct lwp_info
*lp
)
2376 /* Arrange for a breakpoint to be hit again later. We don't keep
2377 the SIGTRAP status and don't forward the SIGTRAP signal to the
2378 LWP. We will handle the current event, eventually we will resume
2379 this LWP, and this breakpoint will trap again.
2381 If we do not do this, then we run the risk that the user will
2382 delete or disable the breakpoint, but the LWP will have already
2385 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2386 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2389 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2390 if (breakpoint_inserted_here_p (pc
))
2392 if (debug_linux_nat
)
2393 fprintf_unfiltered (gdb_stdlog
,
2394 "CB: Push back breakpoint for %s\n",
2395 target_pid_to_str (lp
->ptid
));
2397 /* Back up the PC if necessary. */
2398 if (gdbarch_decr_pc_after_break (gdbarch
))
2399 regcache_write_pc (regcache
, pc
);
2407 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2409 struct lwp_info
*event_lp
= data
;
2411 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2415 /* If a LWP other than the LWP that we're reporting an event for has
2416 hit a GDB breakpoint (as opposed to some random trap signal),
2417 then just arrange for it to hit it again later. We don't keep
2418 the SIGTRAP status and don't forward the SIGTRAP signal to the
2419 LWP. We will handle the current event, eventually we will resume
2420 all LWPs, and this one will get its breakpoint trap again.
2422 If we do not do this, then we run the risk that the user will
2423 delete or disable the breakpoint, but the LWP will have already
2427 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2428 && cancel_breakpoint (lp
))
2429 /* Throw away the SIGTRAP. */
2435 /* Select one LWP out of those that have events pending. */
2438 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2441 int random_selector
;
2442 struct lwp_info
*event_lp
;
2444 /* Record the wait status for the original LWP. */
2445 (*orig_lp
)->status
= *status
;
2447 /* Give preference to any LWP that is being single-stepped. */
2448 event_lp
= iterate_over_lwps (filter
,
2449 select_singlestep_lwp_callback
, NULL
);
2450 if (event_lp
!= NULL
)
2452 if (debug_linux_nat
)
2453 fprintf_unfiltered (gdb_stdlog
,
2454 "SEL: Select single-step %s\n",
2455 target_pid_to_str (event_lp
->ptid
));
2459 /* No single-stepping LWP. Select one at random, out of those
2460 which have had SIGTRAP events. */
2462 /* First see how many SIGTRAP events we have. */
2463 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2465 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2466 random_selector
= (int)
2467 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2469 if (debug_linux_nat
&& num_events
> 1)
2470 fprintf_unfiltered (gdb_stdlog
,
2471 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2472 num_events
, random_selector
);
2474 event_lp
= iterate_over_lwps (filter
,
2475 select_event_lwp_callback
,
2479 if (event_lp
!= NULL
)
2481 /* Switch the event LWP. */
2482 *orig_lp
= event_lp
;
2483 *status
= event_lp
->status
;
2486 /* Flush the wait status for the event LWP. */
2487 (*orig_lp
)->status
= 0;
2490 /* Return non-zero if LP has been resumed. */
2493 resumed_callback (struct lwp_info
*lp
, void *data
)
2498 /* Stop an active thread, verify it still exists, then resume it. */
2501 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2503 struct lwp_info
*ptr
;
2505 if (!lp
->stopped
&& !lp
->signalled
)
2507 stop_callback (lp
, NULL
);
2508 stop_wait_callback (lp
, NULL
);
2509 /* Resume if the lwp still exists. */
2510 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2513 resume_callback (lp
, NULL
);
2514 resume_set_callback (lp
, NULL
);
2520 /* Check if we should go on and pass this event to common code.
2521 Return the affected lwp if we are, or NULL otherwise. */
2522 static struct lwp_info
*
2523 linux_nat_filter_event (int lwpid
, int status
, int options
)
2525 struct lwp_info
*lp
;
2527 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2529 /* Check for stop events reported by a process we didn't already
2530 know about - anything not already in our LWP list.
2532 If we're expecting to receive stopped processes after
2533 fork, vfork, and clone events, then we'll just add the
2534 new one to our list and go back to waiting for the event
2535 to be reported - the stopped process might be returned
2536 from waitpid before or after the event is. */
2537 if (WIFSTOPPED (status
) && !lp
)
2539 linux_record_stopped_pid (lwpid
, status
);
2543 /* Make sure we don't report an event for the exit of an LWP not in
2544 our list, i.e. not part of the current process. This can happen
2545 if we detach from a program we original forked and then it
2547 if (!WIFSTOPPED (status
) && !lp
)
2550 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2551 CLONE_PTRACE processes which do not use the thread library -
2552 otherwise we wouldn't find the new LWP this way. That doesn't
2553 currently work, and the following code is currently unreachable
2554 due to the two blocks above. If it's fixed some day, this code
2555 should be broken out into a function so that we can also pick up
2556 LWPs from the new interface. */
2559 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2560 if (options
& __WCLONE
)
2563 gdb_assert (WIFSTOPPED (status
)
2564 && WSTOPSIG (status
) == SIGSTOP
);
2567 if (!in_thread_list (inferior_ptid
))
2569 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2570 GET_PID (inferior_ptid
));
2571 add_thread (inferior_ptid
);
2574 add_thread (lp
->ptid
);
2577 /* Save the trap's siginfo in case we need it later. */
2578 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2581 /* Handle GNU/Linux's extended waitstatus for trace events. */
2582 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2584 if (debug_linux_nat
)
2585 fprintf_unfiltered (gdb_stdlog
,
2586 "LLW: Handling extended status 0x%06x\n",
2588 if (linux_handle_extended_wait (lp
, status
, 0))
2592 /* Check if the thread has exited. */
2593 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
2594 && num_lwps (GET_PID (lp
->ptid
)) > 1)
2596 /* If this is the main thread, we must stop all threads and verify
2597 if they are still alive. This is because in the nptl thread model
2598 on Linux 2.4, there is no signal issued for exiting LWPs
2599 other than the main thread. We only get the main thread exit
2600 signal once all child threads have already exited. If we
2601 stop all the threads and use the stop_wait_callback to check
2602 if they have exited we can determine whether this signal
2603 should be ignored or whether it means the end of the debugged
2604 application, regardless of which threading model is being
2606 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2609 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
2610 stop_and_resume_callback
, NULL
);
2613 if (debug_linux_nat
)
2614 fprintf_unfiltered (gdb_stdlog
,
2615 "LLW: %s exited.\n",
2616 target_pid_to_str (lp
->ptid
));
2618 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
2620 /* If there is at least one more LWP, then the exit signal
2621 was not the end of the debugged application and should be
2628 /* Check if the current LWP has previously exited. In the nptl
2629 thread model, LWPs other than the main thread do not issue
2630 signals when they exit so we must check whenever the thread has
2631 stopped. A similar check is made in stop_wait_callback(). */
2632 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
2634 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
2636 if (debug_linux_nat
)
2637 fprintf_unfiltered (gdb_stdlog
,
2638 "LLW: %s exited.\n",
2639 target_pid_to_str (lp
->ptid
));
2643 /* Make sure there is at least one thread running. */
2644 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
2646 /* Discard the event. */
2650 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2651 an attempt to stop an LWP. */
2653 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2655 if (debug_linux_nat
)
2656 fprintf_unfiltered (gdb_stdlog
,
2657 "LLW: Delayed SIGSTOP caught for %s.\n",
2658 target_pid_to_str (lp
->ptid
));
2660 /* This is a delayed SIGSTOP. */
2663 registers_changed ();
2665 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2666 lp
->step
, TARGET_SIGNAL_0
);
2667 if (debug_linux_nat
)
2668 fprintf_unfiltered (gdb_stdlog
,
2669 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2671 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2672 target_pid_to_str (lp
->ptid
));
2675 gdb_assert (lp
->resumed
);
2677 /* Discard the event. */
2681 /* Make sure we don't report a SIGINT that we have already displayed
2682 for another thread. */
2683 if (lp
->ignore_sigint
2684 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2686 if (debug_linux_nat
)
2687 fprintf_unfiltered (gdb_stdlog
,
2688 "LLW: Delayed SIGINT caught for %s.\n",
2689 target_pid_to_str (lp
->ptid
));
2691 /* This is a delayed SIGINT. */
2692 lp
->ignore_sigint
= 0;
2694 registers_changed ();
2695 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2696 lp
->step
, TARGET_SIGNAL_0
);
2697 if (debug_linux_nat
)
2698 fprintf_unfiltered (gdb_stdlog
,
2699 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2701 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2702 target_pid_to_str (lp
->ptid
));
2705 gdb_assert (lp
->resumed
);
2707 /* Discard the event. */
2711 /* An interesting event. */
2717 linux_nat_wait_1 (struct target_ops
*ops
,
2718 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
2721 static sigset_t prev_mask
;
2722 struct lwp_info
*lp
= NULL
;
2727 if (debug_linux_nat_async
)
2728 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2730 /* The first time we get here after starting a new inferior, we may
2731 not have added it to the LWP list yet - this is the earliest
2732 moment at which we know its PID. */
2733 if (ptid_is_pid (inferior_ptid
))
2735 /* Upgrade the main thread's ptid. */
2736 thread_change_ptid (inferior_ptid
,
2737 BUILD_LWP (GET_PID (inferior_ptid
),
2738 GET_PID (inferior_ptid
)));
2740 lp
= add_lwp (inferior_ptid
);
2744 /* Make sure SIGCHLD is blocked. */
2745 block_child_signals (&prev_mask
);
2747 if (ptid_equal (ptid
, minus_one_ptid
))
2749 else if (ptid_is_pid (ptid
))
2750 /* A request to wait for a specific tgid. This is not possible
2751 with waitpid, so instead, we wait for any child, and leave
2752 children we're not interested in right now with a pending
2753 status to report later. */
2756 pid
= GET_LWP (ptid
);
2762 /* Make sure there is at least one LWP that has been resumed. */
2763 gdb_assert (iterate_over_lwps (ptid
, resumed_callback
, NULL
));
2765 /* First check if there is a LWP with a wait status pending. */
2768 /* Any LWP that's been resumed will do. */
2769 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
2772 status
= lp
->status
;
2775 if (debug_linux_nat
&& status
)
2776 fprintf_unfiltered (gdb_stdlog
,
2777 "LLW: Using pending wait status %s for %s.\n",
2778 status_to_str (status
),
2779 target_pid_to_str (lp
->ptid
));
2782 /* But if we don't find one, we'll have to wait, and check both
2783 cloned and uncloned processes. We start with the cloned
2785 options
= __WCLONE
| WNOHANG
;
2787 else if (is_lwp (ptid
))
2789 if (debug_linux_nat
)
2790 fprintf_unfiltered (gdb_stdlog
,
2791 "LLW: Waiting for specific LWP %s.\n",
2792 target_pid_to_str (ptid
));
2794 /* We have a specific LWP to check. */
2795 lp
= find_lwp_pid (ptid
);
2797 status
= lp
->status
;
2800 if (debug_linux_nat
&& status
)
2801 fprintf_unfiltered (gdb_stdlog
,
2802 "LLW: Using pending wait status %s for %s.\n",
2803 status_to_str (status
),
2804 target_pid_to_str (lp
->ptid
));
2806 /* If we have to wait, take into account whether PID is a cloned
2807 process or not. And we have to convert it to something that
2808 the layer beneath us can understand. */
2809 options
= lp
->cloned
? __WCLONE
: 0;
2810 pid
= GET_LWP (ptid
);
2812 /* We check for lp->waitstatus in addition to lp->status,
2813 because we can have pending process exits recorded in
2814 lp->status and W_EXITCODE(0,0) == 0. We should probably have
2815 an additional lp->status_p flag. */
2816 if (status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
2820 if (lp
&& lp
->signalled
)
2822 /* A pending SIGSTOP may interfere with the normal stream of
2823 events. In a typical case where interference is a problem,
2824 we have a SIGSTOP signal pending for LWP A while
2825 single-stepping it, encounter an event in LWP B, and take the
2826 pending SIGSTOP while trying to stop LWP A. After processing
2827 the event in LWP B, LWP A is continued, and we'll never see
2828 the SIGTRAP associated with the last time we were
2829 single-stepping LWP A. */
2831 /* Resume the thread. It should halt immediately returning the
2833 registers_changed ();
2834 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2835 lp
->step
, TARGET_SIGNAL_0
);
2836 if (debug_linux_nat
)
2837 fprintf_unfiltered (gdb_stdlog
,
2838 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2839 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2840 target_pid_to_str (lp
->ptid
));
2842 gdb_assert (lp
->resumed
);
2844 /* This should catch the pending SIGSTOP. */
2845 stop_wait_callback (lp
, NULL
);
2848 if (!target_can_async_p ())
2850 /* Causes SIGINT to be passed on to the attached process. */
2854 /* Translate generic target_wait options into waitpid options. */
2855 if (target_options
& TARGET_WNOHANG
)
2862 lwpid
= my_waitpid (pid
, &status
, options
);
2866 gdb_assert (pid
== -1 || lwpid
== pid
);
2868 if (debug_linux_nat
)
2870 fprintf_unfiltered (gdb_stdlog
,
2871 "LLW: waitpid %ld received %s\n",
2872 (long) lwpid
, status_to_str (status
));
2875 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2878 && ptid_is_pid (ptid
)
2879 && ptid_get_pid (lp
->ptid
) != ptid_get_pid (ptid
))
2881 if (debug_linux_nat
)
2882 fprintf (stderr
, "LWP %ld got an event %06x, leaving pending.\n",
2883 ptid_get_lwp (lp
->ptid
), status
);
2885 if (WIFSTOPPED (status
))
2887 if (WSTOPSIG (status
) != SIGSTOP
)
2889 lp
->status
= status
;
2891 stop_callback (lp
, NULL
);
2893 /* Resume in order to collect the sigstop. */
2894 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2896 stop_wait_callback (lp
, NULL
);
2904 else if (WIFEXITED (status
) || WIFSIGNALED (status
))
2906 if (debug_linux_nat
)
2907 fprintf (stderr
, "Process %ld exited while stopping LWPs\n",
2908 ptid_get_lwp (lp
->ptid
));
2910 /* This was the last lwp in the process. Since
2911 events are serialized to GDB core, and we can't
2912 report this one right now, but GDB core and the
2913 other target layers will want to be notified
2914 about the exit code/signal, leave the status
2915 pending for the next time we're able to report
2917 lp
->status
= status
;
2919 /* Prevent trying to stop this thread again. We'll
2920 never try to resume it because it has a pending
2924 /* Dead LWP's aren't expected to reported a pending
2928 /* Store the pending event in the waitstatus as
2929 well, because W_EXITCODE(0,0) == 0. */
2930 store_waitstatus (&lp
->waitstatus
, status
);
2944 /* waitpid did return something. Restart over. */
2945 options
|= __WCLONE
;
2953 /* Alternate between checking cloned and uncloned processes. */
2954 options
^= __WCLONE
;
2956 /* And every time we have checked both:
2957 In async mode, return to event loop;
2958 In sync mode, suspend waiting for a SIGCHLD signal. */
2959 if (options
& __WCLONE
)
2961 if (target_options
& TARGET_WNOHANG
)
2963 /* No interesting event. */
2964 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2966 if (debug_linux_nat_async
)
2967 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2969 restore_child_signals_mask (&prev_mask
);
2970 return minus_one_ptid
;
2973 sigsuspend (&suspend_mask
);
2977 /* We shouldn't end up here unless we want to try again. */
2978 gdb_assert (lp
== NULL
);
2981 if (!target_can_async_p ())
2982 clear_sigint_trap ();
2986 /* Don't report signals that GDB isn't interested in, such as
2987 signals that are neither printed nor stopped upon. Stopping all
2988 threads can be a bit time-consuming so if we want decent
2989 performance with heavily multi-threaded programs, especially when
2990 they're using a high frequency timer, we'd better avoid it if we
2993 if (WIFSTOPPED (status
))
2995 int signo
= target_signal_from_host (WSTOPSIG (status
));
2996 struct inferior
*inf
;
2998 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
3001 /* Defer to common code if we get a signal while
3002 single-stepping, since that may need special care, e.g. to
3003 skip the signal handler, or, if we're gaining control of the
3006 && inf
->stop_soon
== NO_STOP_QUIETLY
3007 && signal_stop_state (signo
) == 0
3008 && signal_print_state (signo
) == 0
3009 && signal_pass_state (signo
) == 1)
3011 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3012 here? It is not clear we should. GDB may not expect
3013 other threads to run. On the other hand, not resuming
3014 newly attached threads may cause an unwanted delay in
3015 getting them running. */
3016 registers_changed ();
3017 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3019 if (debug_linux_nat
)
3020 fprintf_unfiltered (gdb_stdlog
,
3021 "LLW: %s %s, %s (preempt 'handle')\n",
3023 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3024 target_pid_to_str (lp
->ptid
),
3025 signo
? strsignal (signo
) : "0");
3032 /* Only do the below in all-stop, as we currently use SIGINT
3033 to implement target_stop (see linux_nat_stop) in
3035 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3037 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3038 forwarded to the entire process group, that is, all LWPs
3039 will receive it - unless they're using CLONE_THREAD to
3040 share signals. Since we only want to report it once, we
3041 mark it as ignored for all LWPs except this one. */
3042 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3043 set_ignore_sigint
, NULL
);
3044 lp
->ignore_sigint
= 0;
3047 maybe_clear_ignore_sigint (lp
);
3051 /* This LWP is stopped now. */
3054 if (debug_linux_nat
)
3055 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3056 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3060 /* Now stop all other LWP's ... */
3061 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3063 /* ... and wait until all of them have reported back that
3064 they're no longer running. */
3065 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3067 /* If we're not waiting for a specific LWP, choose an event LWP
3068 from among those that have had events. Giving equal priority
3069 to all LWPs that have had events helps prevent
3072 select_event_lwp (ptid
, &lp
, &status
);
3075 /* Now that we've selected our final event LWP, cancel any
3076 breakpoints in other LWPs that have hit a GDB breakpoint. See
3077 the comment in cancel_breakpoints_callback to find out why. */
3078 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3080 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3082 if (debug_linux_nat
)
3083 fprintf_unfiltered (gdb_stdlog
,
3084 "LLW: trap ptid is %s.\n",
3085 target_pid_to_str (lp
->ptid
));
3088 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3090 *ourstatus
= lp
->waitstatus
;
3091 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3094 store_waitstatus (ourstatus
, status
);
3096 if (debug_linux_nat_async
)
3097 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3099 restore_child_signals_mask (&prev_mask
);
3104 linux_nat_wait (struct target_ops
*ops
,
3105 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3110 if (debug_linux_nat
)
3111 fprintf_unfiltered (gdb_stdlog
, "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
3113 /* Flush the async file first. */
3114 if (target_can_async_p ())
3115 async_file_flush ();
3117 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3119 /* If we requested any event, and something came out, assume there
3120 may be more. If we requested a specific lwp or process, also
3121 assume there may be more. */
3122 if (target_can_async_p ()
3123 && (ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3124 || !ptid_equal (ptid
, minus_one_ptid
)))
3127 /* Get ready for the next event. */
3128 if (target_can_async_p ())
3129 target_async (inferior_event_handler
, 0);
3135 kill_callback (struct lwp_info
*lp
, void *data
)
3138 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3139 if (debug_linux_nat
)
3140 fprintf_unfiltered (gdb_stdlog
,
3141 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3142 target_pid_to_str (lp
->ptid
),
3143 errno
? safe_strerror (errno
) : "OK");
3149 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3153 /* We must make sure that there are no pending events (delayed
3154 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3155 program doesn't interfere with any following debugging session. */
3157 /* For cloned processes we must check both with __WCLONE and
3158 without, since the exit status of a cloned process isn't reported
3164 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3165 if (pid
!= (pid_t
) -1)
3167 if (debug_linux_nat
)
3168 fprintf_unfiltered (gdb_stdlog
,
3169 "KWC: wait %s received unknown.\n",
3170 target_pid_to_str (lp
->ptid
));
3171 /* The Linux kernel sometimes fails to kill a thread
3172 completely after PTRACE_KILL; that goes from the stop
3173 point in do_fork out to the one in
3174 get_signal_to_deliever and waits again. So kill it
3176 kill_callback (lp
, NULL
);
3179 while (pid
== GET_LWP (lp
->ptid
));
3181 gdb_assert (pid
== -1 && errno
== ECHILD
);
3186 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3187 if (pid
!= (pid_t
) -1)
3189 if (debug_linux_nat
)
3190 fprintf_unfiltered (gdb_stdlog
,
3191 "KWC: wait %s received unk.\n",
3192 target_pid_to_str (lp
->ptid
));
3193 /* See the call to kill_callback above. */
3194 kill_callback (lp
, NULL
);
3197 while (pid
== GET_LWP (lp
->ptid
));
3199 gdb_assert (pid
== -1 && errno
== ECHILD
);
3204 linux_nat_kill (struct target_ops
*ops
)
3206 struct target_waitstatus last
;
3210 /* If we're stopped while forking and we haven't followed yet,
3211 kill the other task. We need to do this first because the
3212 parent will be sleeping if this is a vfork. */
3214 get_last_target_status (&last_ptid
, &last
);
3216 if (last
.kind
== TARGET_WAITKIND_FORKED
3217 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3219 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3223 if (forks_exist_p ())
3224 linux_fork_killall ();
3227 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3228 /* Stop all threads before killing them, since ptrace requires
3229 that the thread is stopped to sucessfully PTRACE_KILL. */
3230 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3231 /* ... and wait until all of them have reported back that
3232 they're no longer running. */
3233 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3235 /* Kill all LWP's ... */
3236 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3238 /* ... and wait until we've flushed all events. */
3239 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3242 target_mourn_inferior ();
3246 linux_nat_mourn_inferior (struct target_ops
*ops
)
3248 purge_lwp_list (ptid_get_pid (inferior_ptid
));
3250 if (! forks_exist_p ())
3251 /* Normal case, no other forks available. */
3252 linux_ops
->to_mourn_inferior (ops
);
3254 /* Multi-fork case. The current inferior_ptid has exited, but
3255 there are other viable forks to debug. Delete the exiting
3256 one and context-switch to the first available. */
3257 linux_fork_mourn_inferior ();
3260 /* Convert a native/host siginfo object, into/from the siginfo in the
3261 layout of the inferiors' architecture. */
3264 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3268 if (linux_nat_siginfo_fixup
!= NULL
)
3269 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3271 /* If there was no callback, or the callback didn't do anything,
3272 then just do a straight memcpy. */
3276 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
3278 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
3283 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3284 const char *annex
, gdb_byte
*readbuf
,
3285 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3288 struct siginfo siginfo
;
3289 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
3291 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3292 gdb_assert (readbuf
|| writebuf
);
3294 pid
= GET_LWP (inferior_ptid
);
3296 pid
= GET_PID (inferior_ptid
);
3298 if (offset
> sizeof (siginfo
))
3302 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3306 /* When GDB is built as a 64-bit application, ptrace writes into
3307 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3308 inferior with a 64-bit GDB should look the same as debugging it
3309 with a 32-bit GDB, we need to convert it. GDB core always sees
3310 the converted layout, so any read/write will have to be done
3312 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3314 if (offset
+ len
> sizeof (siginfo
))
3315 len
= sizeof (siginfo
) - offset
;
3317 if (readbuf
!= NULL
)
3318 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3321 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3323 /* Convert back to ptrace layout before flushing it out. */
3324 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3327 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3336 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3337 const char *annex
, gdb_byte
*readbuf
,
3338 const gdb_byte
*writebuf
,
3339 ULONGEST offset
, LONGEST len
)
3341 struct cleanup
*old_chain
;
3344 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3345 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3348 /* The target is connected but no live inferior is selected. Pass
3349 this request down to a lower stratum (e.g., the executable
3351 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3354 old_chain
= save_inferior_ptid ();
3356 if (is_lwp (inferior_ptid
))
3357 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3359 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3362 do_cleanups (old_chain
);
3367 linux_thread_alive (ptid_t ptid
)
3371 gdb_assert (is_lwp (ptid
));
3373 /* Send signal 0 instead of anything ptrace, because ptracing a
3374 running thread errors out claiming that the thread doesn't
3376 err
= kill_lwp (GET_LWP (ptid
), 0);
3378 if (debug_linux_nat
)
3379 fprintf_unfiltered (gdb_stdlog
,
3380 "LLTA: KILL(SIG0) %s (%s)\n",
3381 target_pid_to_str (ptid
),
3382 err
? safe_strerror (err
) : "OK");
3391 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3393 return linux_thread_alive (ptid
);
3397 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3399 static char buf
[64];
3402 && (GET_PID (ptid
) != GET_LWP (ptid
)
3403 || num_lwps (GET_PID (ptid
)) > 1))
3405 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3409 return normal_pid_to_str (ptid
);
3412 /* Accepts an integer PID; Returns a string representing a file that
3413 can be opened to get the symbols for the child process. */
3416 linux_child_pid_to_exec_file (int pid
)
3418 char *name1
, *name2
;
3420 name1
= xmalloc (MAXPATHLEN
);
3421 name2
= xmalloc (MAXPATHLEN
);
3422 make_cleanup (xfree
, name1
);
3423 make_cleanup (xfree
, name2
);
3424 memset (name2
, 0, MAXPATHLEN
);
3426 sprintf (name1
, "/proc/%d/exe", pid
);
3427 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3433 /* Service function for corefiles and info proc. */
3436 read_mapping (FILE *mapfile
,
3441 char *device
, long long *inode
, char *filename
)
3443 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3444 addr
, endaddr
, permissions
, offset
, device
, inode
);
3447 if (ret
> 0 && ret
!= EOF
)
3449 /* Eat everything up to EOL for the filename. This will prevent
3450 weird filenames (such as one with embedded whitespace) from
3451 confusing this code. It also makes this code more robust in
3452 respect to annotations the kernel may add after the filename.
3454 Note the filename is used for informational purposes
3456 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3459 return (ret
!= 0 && ret
!= EOF
);
3462 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3463 regions in the inferior for a corefile. */
3466 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3468 int, int, int, void *), void *obfd
)
3470 int pid
= PIDGET (inferior_ptid
);
3471 char mapsfilename
[MAXPATHLEN
];
3473 long long addr
, endaddr
, size
, offset
, inode
;
3474 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3475 int read
, write
, exec
;
3477 struct cleanup
*cleanup
;
3479 /* Compose the filename for the /proc memory map, and open it. */
3480 sprintf (mapsfilename
, "/proc/%d/maps", pid
);
3481 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3482 error (_("Could not open %s."), mapsfilename
);
3483 cleanup
= make_cleanup_fclose (mapsfile
);
3486 fprintf_filtered (gdb_stdout
,
3487 "Reading memory regions from %s\n", mapsfilename
);
3489 /* Now iterate until end-of-file. */
3490 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3491 &offset
, &device
[0], &inode
, &filename
[0]))
3493 size
= endaddr
- addr
;
3495 /* Get the segment's permissions. */
3496 read
= (strchr (permissions
, 'r') != 0);
3497 write
= (strchr (permissions
, 'w') != 0);
3498 exec
= (strchr (permissions
, 'x') != 0);
3502 fprintf_filtered (gdb_stdout
,
3503 "Save segment, %lld bytes at %s (%c%c%c)",
3504 size
, paddress (target_gdbarch
, addr
),
3506 write
? 'w' : ' ', exec
? 'x' : ' ');
3508 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3509 fprintf_filtered (gdb_stdout
, "\n");
3512 /* Invoke the callback function to create the corefile
3514 func (addr
, size
, read
, write
, exec
, obfd
);
3516 do_cleanups (cleanup
);
3521 find_signalled_thread (struct thread_info
*info
, void *data
)
3523 if (info
->stop_signal
!= TARGET_SIGNAL_0
3524 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3530 static enum target_signal
3531 find_stop_signal (void)
3533 struct thread_info
*info
=
3534 iterate_over_threads (find_signalled_thread
, NULL
);
3537 return info
->stop_signal
;
3539 return TARGET_SIGNAL_0
;
3542 /* Records the thread's register state for the corefile note
3546 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3547 char *note_data
, int *note_size
,
3548 enum target_signal stop_signal
)
3550 gdb_gregset_t gregs
;
3551 gdb_fpregset_t fpregs
;
3552 unsigned long lwp
= ptid_get_lwp (ptid
);
3553 struct gdbarch
*gdbarch
= target_gdbarch
;
3554 struct regcache
*regcache
= get_thread_arch_regcache (ptid
, gdbarch
);
3555 const struct regset
*regset
;
3557 struct cleanup
*old_chain
;
3558 struct core_regset_section
*sect_list
;
3561 old_chain
= save_inferior_ptid ();
3562 inferior_ptid
= ptid
;
3563 target_fetch_registers (regcache
, -1);
3564 do_cleanups (old_chain
);
3566 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3567 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3570 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3571 sizeof (gregs
))) != NULL
3572 && regset
->collect_regset
!= NULL
)
3573 regset
->collect_regset (regset
, regcache
, -1,
3574 &gregs
, sizeof (gregs
));
3576 fill_gregset (regcache
, &gregs
, -1);
3578 note_data
= (char *) elfcore_write_prstatus (obfd
,
3582 stop_signal
, &gregs
);
3584 /* The loop below uses the new struct core_regset_section, which stores
3585 the supported section names and sizes for the core file. Note that
3586 note PRSTATUS needs to be treated specially. But the other notes are
3587 structurally the same, so they can benefit from the new struct. */
3588 if (core_regset_p
&& sect_list
!= NULL
)
3589 while (sect_list
->sect_name
!= NULL
)
3591 /* .reg was already handled above. */
3592 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3597 regset
= gdbarch_regset_from_core_section (gdbarch
,
3598 sect_list
->sect_name
,
3600 gdb_assert (regset
&& regset
->collect_regset
);
3601 gdb_regset
= xmalloc (sect_list
->size
);
3602 regset
->collect_regset (regset
, regcache
, -1,
3603 gdb_regset
, sect_list
->size
);
3604 note_data
= (char *) elfcore_write_register_note (obfd
,
3607 sect_list
->sect_name
,
3614 /* For architectures that does not have the struct core_regset_section
3615 implemented, we use the old method. When all the architectures have
3616 the new support, the code below should be deleted. */
3620 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3621 sizeof (fpregs
))) != NULL
3622 && regset
->collect_regset
!= NULL
)
3623 regset
->collect_regset (regset
, regcache
, -1,
3624 &fpregs
, sizeof (fpregs
));
3626 fill_fpregset (regcache
, &fpregs
, -1);
3628 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3631 &fpregs
, sizeof (fpregs
));
3637 struct linux_nat_corefile_thread_data
3643 enum target_signal stop_signal
;
3646 /* Called by gdbthread.c once per thread. Records the thread's
3647 register state for the corefile note section. */
3650 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3652 struct linux_nat_corefile_thread_data
*args
= data
;
3654 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3664 /* Fills the "to_make_corefile_note" target vector. Builds the note
3665 section for a corefile, and returns it in a malloc buffer. */
3668 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3670 struct linux_nat_corefile_thread_data thread_args
;
3671 struct cleanup
*old_chain
;
3672 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3673 char fname
[16] = { '\0' };
3674 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3675 char psargs
[80] = { '\0' };
3676 char *note_data
= NULL
;
3677 ptid_t current_ptid
= inferior_ptid
;
3678 ptid_t filter
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3682 if (get_exec_file (0))
3684 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3685 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3686 if (get_inferior_args ())
3689 char *psargs_end
= psargs
+ sizeof (psargs
);
3691 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3693 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3694 if (string_end
!= NULL
)
3696 *string_end
++ = ' ';
3697 strncpy (string_end
, get_inferior_args (),
3698 psargs_end
- string_end
);
3701 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3703 note_size
, fname
, psargs
);
3706 /* Dump information for threads. */
3707 thread_args
.obfd
= obfd
;
3708 thread_args
.note_data
= note_data
;
3709 thread_args
.note_size
= note_size
;
3710 thread_args
.num_notes
= 0;
3711 thread_args
.stop_signal
= find_stop_signal ();
3712 iterate_over_lwps (filter
, linux_nat_corefile_thread_callback
, &thread_args
);
3713 gdb_assert (thread_args
.num_notes
!= 0);
3714 note_data
= thread_args
.note_data
;
3716 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3720 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3721 "CORE", NT_AUXV
, auxv
, auxv_len
);
3725 make_cleanup (xfree
, note_data
);
3729 /* Implement the "info proc" command. */
3732 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3734 /* A long is used for pid instead of an int to avoid a loss of precision
3735 compiler warning from the output of strtoul. */
3736 long pid
= PIDGET (inferior_ptid
);
3739 char buffer
[MAXPATHLEN
];
3740 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3753 /* Break up 'args' into an argv array. */
3754 argv
= gdb_buildargv (args
);
3755 make_cleanup_freeargv (argv
);
3757 while (argv
!= NULL
&& *argv
!= NULL
)
3759 if (isdigit (argv
[0][0]))
3761 pid
= strtoul (argv
[0], NULL
, 10);
3763 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3767 else if (strcmp (argv
[0], "status") == 0)
3771 else if (strcmp (argv
[0], "stat") == 0)
3775 else if (strcmp (argv
[0], "cmd") == 0)
3779 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3783 else if (strcmp (argv
[0], "cwd") == 0)
3787 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3793 /* [...] (future options here) */
3798 error (_("No current process: you must name one."));
3800 sprintf (fname1
, "/proc/%ld", pid
);
3801 if (stat (fname1
, &dummy
) != 0)
3802 error (_("No /proc directory: '%s'"), fname1
);
3804 printf_filtered (_("process %ld\n"), pid
);
3805 if (cmdline_f
|| all
)
3807 sprintf (fname1
, "/proc/%ld/cmdline", pid
);
3808 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3810 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3811 if (fgets (buffer
, sizeof (buffer
), procfile
))
3812 printf_filtered ("cmdline = '%s'\n", buffer
);
3814 warning (_("unable to read '%s'"), fname1
);
3815 do_cleanups (cleanup
);
3818 warning (_("unable to open /proc file '%s'"), fname1
);
3822 sprintf (fname1
, "/proc/%ld/cwd", pid
);
3823 memset (fname2
, 0, sizeof (fname2
));
3824 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3825 printf_filtered ("cwd = '%s'\n", fname2
);
3827 warning (_("unable to read link '%s'"), fname1
);
3831 sprintf (fname1
, "/proc/%ld/exe", pid
);
3832 memset (fname2
, 0, sizeof (fname2
));
3833 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3834 printf_filtered ("exe = '%s'\n", fname2
);
3836 warning (_("unable to read link '%s'"), fname1
);
3838 if (mappings_f
|| all
)
3840 sprintf (fname1
, "/proc/%ld/maps", pid
);
3841 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3843 long long addr
, endaddr
, size
, offset
, inode
;
3844 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3845 struct cleanup
*cleanup
;
3847 cleanup
= make_cleanup_fclose (procfile
);
3848 printf_filtered (_("Mapped address spaces:\n\n"));
3849 if (gdbarch_addr_bit (target_gdbarch
) == 32)
3851 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3854 " Size", " Offset", "objfile");
3858 printf_filtered (" %18s %18s %10s %10s %7s\n",
3861 " Size", " Offset", "objfile");
3864 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3865 &offset
, &device
[0], &inode
, &filename
[0]))
3867 size
= endaddr
- addr
;
3869 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3870 calls here (and possibly above) should be abstracted
3871 out into their own functions? Andrew suggests using
3872 a generic local_address_string instead to print out
3873 the addresses; that makes sense to me, too. */
3875 if (gdbarch_addr_bit (target_gdbarch
) == 32)
3877 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3878 (unsigned long) addr
, /* FIXME: pr_addr */
3879 (unsigned long) endaddr
,
3881 (unsigned int) offset
,
3882 filename
[0] ? filename
: "");
3886 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3887 (unsigned long) addr
, /* FIXME: pr_addr */
3888 (unsigned long) endaddr
,
3890 (unsigned int) offset
,
3891 filename
[0] ? filename
: "");
3895 do_cleanups (cleanup
);
3898 warning (_("unable to open /proc file '%s'"), fname1
);
3900 if (status_f
|| all
)
3902 sprintf (fname1
, "/proc/%ld/status", pid
);
3903 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3905 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3906 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3907 puts_filtered (buffer
);
3908 do_cleanups (cleanup
);
3911 warning (_("unable to open /proc file '%s'"), fname1
);
3915 sprintf (fname1
, "/proc/%ld/stat", pid
);
3916 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3921 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3923 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3924 printf_filtered (_("Process: %d\n"), itmp
);
3925 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3926 printf_filtered (_("Exec file: %s\n"), buffer
);
3927 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3928 printf_filtered (_("State: %c\n"), ctmp
);
3929 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3930 printf_filtered (_("Parent process: %d\n"), itmp
);
3931 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3932 printf_filtered (_("Process group: %d\n"), itmp
);
3933 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3934 printf_filtered (_("Session id: %d\n"), itmp
);
3935 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3936 printf_filtered (_("TTY: %d\n"), itmp
);
3937 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3938 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3939 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3940 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3941 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3942 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3943 (unsigned long) ltmp
);
3944 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3945 printf_filtered (_("Minor faults, children: %lu\n"),
3946 (unsigned long) ltmp
);
3947 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3948 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3949 (unsigned long) ltmp
);
3950 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3951 printf_filtered (_("Major faults, children: %lu\n"),
3952 (unsigned long) ltmp
);
3953 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3954 printf_filtered (_("utime: %ld\n"), ltmp
);
3955 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3956 printf_filtered (_("stime: %ld\n"), ltmp
);
3957 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3958 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3959 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3960 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3961 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3962 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3964 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3965 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3966 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3967 printf_filtered (_("jiffies until next timeout: %lu\n"),
3968 (unsigned long) ltmp
);
3969 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3970 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3971 (unsigned long) ltmp
);
3972 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3973 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3975 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3976 printf_filtered (_("Virtual memory size: %lu\n"),
3977 (unsigned long) ltmp
);
3978 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3979 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3980 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3981 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3982 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3983 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3984 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3985 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3986 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3987 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3988 #if 0 /* Don't know how architecture-dependent the rest is...
3989 Anyway the signal bitmap info is available from "status". */
3990 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3991 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3992 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3993 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3994 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3995 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3996 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3997 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3998 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3999 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
4000 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4001 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
4002 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4003 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
4005 do_cleanups (cleanup
);
4008 warning (_("unable to open /proc file '%s'"), fname1
);
4012 /* Implement the to_xfer_partial interface for memory reads using the /proc
4013 filesystem. Because we can use a single read() call for /proc, this
4014 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4015 but it doesn't support writes. */
4018 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4019 const char *annex
, gdb_byte
*readbuf
,
4020 const gdb_byte
*writebuf
,
4021 ULONGEST offset
, LONGEST len
)
4027 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4030 /* Don't bother for one word. */
4031 if (len
< 3 * sizeof (long))
4034 /* We could keep this file open and cache it - possibly one per
4035 thread. That requires some juggling, but is even faster. */
4036 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4037 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4041 /* If pread64 is available, use it. It's faster if the kernel
4042 supports it (only one syscall), and it's 64-bit safe even on
4043 32-bit platforms (for instance, SPARC debugging a SPARC64
4046 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4048 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4058 /* Parse LINE as a signal set and add its set bits to SIGS. */
4061 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4063 int len
= strlen (line
) - 1;
4067 if (line
[len
] != '\n')
4068 error (_("Could not parse signal set: %s"), line
);
4076 if (*p
>= '0' && *p
<= '9')
4078 else if (*p
>= 'a' && *p
<= 'f')
4079 digit
= *p
- 'a' + 10;
4081 error (_("Could not parse signal set: %s"), line
);
4086 sigaddset (sigs
, signum
+ 1);
4088 sigaddset (sigs
, signum
+ 2);
4090 sigaddset (sigs
, signum
+ 3);
4092 sigaddset (sigs
, signum
+ 4);
4098 /* Find process PID's pending signals from /proc/pid/status and set
4102 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
4105 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4107 struct cleanup
*cleanup
;
4109 sigemptyset (pending
);
4110 sigemptyset (blocked
);
4111 sigemptyset (ignored
);
4112 sprintf (fname
, "/proc/%d/status", pid
);
4113 procfile
= fopen (fname
, "r");
4114 if (procfile
== NULL
)
4115 error (_("Could not open %s"), fname
);
4116 cleanup
= make_cleanup_fclose (procfile
);
4118 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4120 /* Normal queued signals are on the SigPnd line in the status
4121 file. However, 2.6 kernels also have a "shared" pending
4122 queue for delivering signals to a thread group, so check for
4125 Unfortunately some Red Hat kernels include the shared pending
4126 queue but not the ShdPnd status field. */
4128 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4129 add_line_to_sigset (buffer
+ 8, pending
);
4130 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4131 add_line_to_sigset (buffer
+ 8, pending
);
4132 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4133 add_line_to_sigset (buffer
+ 8, blocked
);
4134 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4135 add_line_to_sigset (buffer
+ 8, ignored
);
4138 do_cleanups (cleanup
);
4142 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4143 const char *annex
, gdb_byte
*readbuf
,
4144 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4146 /* We make the process list snapshot when the object starts to be
4148 static const char *buf
;
4149 static LONGEST len_avail
= -1;
4150 static struct obstack obstack
;
4154 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4156 if (strcmp (annex
, "processes") != 0)
4159 gdb_assert (readbuf
&& !writebuf
);
4163 if (len_avail
!= -1 && len_avail
!= 0)
4164 obstack_free (&obstack
, NULL
);
4167 obstack_init (&obstack
);
4168 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4170 dirp
= opendir ("/proc");
4174 while ((dp
= readdir (dirp
)) != NULL
)
4176 struct stat statbuf
;
4177 char procentry
[sizeof ("/proc/4294967295")];
4179 if (!isdigit (dp
->d_name
[0])
4180 || NAMELEN (dp
) > sizeof ("4294967295") - 1)
4183 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4184 if (stat (procentry
, &statbuf
) == 0
4185 && S_ISDIR (statbuf
.st_mode
))
4189 char cmd
[MAXPATHLEN
+ 1];
4190 struct passwd
*entry
;
4192 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4193 entry
= getpwuid (statbuf
.st_uid
);
4195 if ((f
= fopen (pathname
, "r")) != NULL
)
4197 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4201 for (i
= 0; i
< len
; i
++)
4206 obstack_xml_printf (
4209 "<column name=\"pid\">%s</column>"
4210 "<column name=\"user\">%s</column>"
4211 "<column name=\"command\">%s</column>"
4214 entry
? entry
->pw_name
: "?",
4227 obstack_grow_str0 (&obstack
, "</osdata>\n");
4228 buf
= obstack_finish (&obstack
);
4229 len_avail
= strlen (buf
);
4232 if (offset
>= len_avail
)
4234 /* Done. Get rid of the obstack. */
4235 obstack_free (&obstack
, NULL
);
4241 if (len
> len_avail
- offset
)
4242 len
= len_avail
- offset
;
4243 memcpy (readbuf
, buf
+ offset
, len
);
4249 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4250 const char *annex
, gdb_byte
*readbuf
,
4251 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4255 if (object
== TARGET_OBJECT_AUXV
)
4256 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4259 if (object
== TARGET_OBJECT_OSDATA
)
4260 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4263 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4268 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4272 /* Create a prototype generic GNU/Linux target. The client can override
4273 it with local methods. */
4276 linux_target_install_ops (struct target_ops
*t
)
4278 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4279 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4280 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4281 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4282 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4283 t
->to_post_attach
= linux_child_post_attach
;
4284 t
->to_follow_fork
= linux_child_follow_fork
;
4285 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4286 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4288 super_xfer_partial
= t
->to_xfer_partial
;
4289 t
->to_xfer_partial
= linux_xfer_partial
;
4295 struct target_ops
*t
;
4297 t
= inf_ptrace_target ();
4298 linux_target_install_ops (t
);
4304 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4306 struct target_ops
*t
;
4308 t
= inf_ptrace_trad_target (register_u_offset
);
4309 linux_target_install_ops (t
);
4314 /* target_is_async_p implementation. */
4317 linux_nat_is_async_p (void)
4319 /* NOTE: palves 2008-03-21: We're only async when the user requests
4320 it explicitly with the "set target-async" command.
4321 Someday, linux will always be async. */
4322 if (!target_async_permitted
)
4325 /* See target.h/target_async_mask. */
4326 return linux_nat_async_mask_value
;
4329 /* target_can_async_p implementation. */
4332 linux_nat_can_async_p (void)
4334 /* NOTE: palves 2008-03-21: We're only async when the user requests
4335 it explicitly with the "set target-async" command.
4336 Someday, linux will always be async. */
4337 if (!target_async_permitted
)
4340 /* See target.h/target_async_mask. */
4341 return linux_nat_async_mask_value
;
4345 linux_nat_supports_non_stop (void)
4350 /* True if we want to support multi-process. To be removed when GDB
4351 supports multi-exec. */
4353 int linux_multi_process
= 1;
4356 linux_nat_supports_multi_process (void)
4358 return linux_multi_process
;
4361 /* target_async_mask implementation. */
4364 linux_nat_async_mask (int new_mask
)
4366 int curr_mask
= linux_nat_async_mask_value
;
4368 if (curr_mask
!= new_mask
)
4372 linux_nat_async (NULL
, 0);
4373 linux_nat_async_mask_value
= new_mask
;
4377 linux_nat_async_mask_value
= new_mask
;
4379 /* If we're going out of async-mask in all-stop, then the
4380 inferior is stopped. The next resume will call
4381 target_async. In non-stop, the target event source
4382 should be always registered in the event loop. Do so
4385 linux_nat_async (inferior_event_handler
, 0);
4392 static int async_terminal_is_ours
= 1;
4394 /* target_terminal_inferior implementation. */
4397 linux_nat_terminal_inferior (void)
4399 if (!target_is_async_p ())
4401 /* Async mode is disabled. */
4402 terminal_inferior ();
4406 terminal_inferior ();
4408 /* Calls to target_terminal_*() are meant to be idempotent. */
4409 if (!async_terminal_is_ours
)
4412 delete_file_handler (input_fd
);
4413 async_terminal_is_ours
= 0;
4417 /* target_terminal_ours implementation. */
4420 linux_nat_terminal_ours (void)
4422 if (!target_is_async_p ())
4424 /* Async mode is disabled. */
4429 /* GDB should never give the terminal to the inferior if the
4430 inferior is running in the background (run&, continue&, etc.),
4431 but claiming it sure should. */
4434 if (async_terminal_is_ours
)
4437 clear_sigint_trap ();
4438 add_file_handler (input_fd
, stdin_event_handler
, 0);
4439 async_terminal_is_ours
= 1;
4442 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4444 static void *async_client_context
;
4446 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4447 so we notice when any child changes state, and notify the
4448 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4449 above to wait for the arrival of a SIGCHLD. */
4452 sigchld_handler (int signo
)
4454 int old_errno
= errno
;
4456 if (debug_linux_nat_async
)
4457 fprintf_unfiltered (gdb_stdlog
, "sigchld\n");
4459 if (signo
== SIGCHLD
4460 && linux_nat_event_pipe
[0] != -1)
4461 async_file_mark (); /* Let the event loop know that there are
4462 events to handle. */
4467 /* Callback registered with the target events file descriptor. */
4470 handle_target_event (int error
, gdb_client_data client_data
)
4472 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4475 /* Create/destroy the target events pipe. Returns previous state. */
4478 linux_async_pipe (int enable
)
4480 int previous
= (linux_nat_event_pipe
[0] != -1);
4482 if (previous
!= enable
)
4486 block_child_signals (&prev_mask
);
4490 if (pipe (linux_nat_event_pipe
) == -1)
4491 internal_error (__FILE__
, __LINE__
,
4492 "creating event pipe failed.");
4494 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4495 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4499 close (linux_nat_event_pipe
[0]);
4500 close (linux_nat_event_pipe
[1]);
4501 linux_nat_event_pipe
[0] = -1;
4502 linux_nat_event_pipe
[1] = -1;
4505 restore_child_signals_mask (&prev_mask
);
4511 /* target_async implementation. */
4514 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4515 void *context
), void *context
)
4517 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
4518 internal_error (__FILE__
, __LINE__
,
4519 "Calling target_async when async is masked");
4521 if (callback
!= NULL
)
4523 async_client_callback
= callback
;
4524 async_client_context
= context
;
4525 if (!linux_async_pipe (1))
4527 add_file_handler (linux_nat_event_pipe
[0],
4528 handle_target_event
, NULL
);
4529 /* There may be pending events to handle. Tell the event loop
4536 async_client_callback
= callback
;
4537 async_client_context
= context
;
4538 delete_file_handler (linux_nat_event_pipe
[0]);
4539 linux_async_pipe (0);
4544 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4548 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4553 ptid_t ptid
= lwp
->ptid
;
4555 if (debug_linux_nat
)
4556 fprintf_unfiltered (gdb_stdlog
,
4557 "LNSL: running -> suspending %s\n",
4558 target_pid_to_str (lwp
->ptid
));
4561 stop_callback (lwp
, NULL
);
4562 stop_wait_callback (lwp
, NULL
);
4564 /* If the lwp exits while we try to stop it, there's nothing
4566 lwp
= find_lwp_pid (ptid
);
4570 /* If we didn't collect any signal other than SIGSTOP while
4571 stopping the LWP, push a SIGNAL_0 event. In either case, the
4572 event-loop will end up calling target_wait which will collect
4574 if (lwp
->status
== 0)
4575 lwp
->status
= W_STOPCODE (0);
4580 /* Already known to be stopped; do nothing. */
4582 if (debug_linux_nat
)
4584 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4585 fprintf_unfiltered (gdb_stdlog
, "\
4586 LNSL: already stopped/stop_requested %s\n",
4587 target_pid_to_str (lwp
->ptid
));
4589 fprintf_unfiltered (gdb_stdlog
, "\
4590 LNSL: already stopped/no stop_requested yet %s\n",
4591 target_pid_to_str (lwp
->ptid
));
4598 linux_nat_stop (ptid_t ptid
)
4601 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4603 linux_ops
->to_stop (ptid
);
4607 linux_nat_close (int quitting
)
4609 /* Unregister from the event loop. */
4610 if (target_is_async_p ())
4611 target_async (NULL
, 0);
4613 /* Reset the async_masking. */
4614 linux_nat_async_mask_value
= 1;
4616 if (linux_ops
->to_close
)
4617 linux_ops
->to_close (quitting
);
4621 linux_nat_add_target (struct target_ops
*t
)
4623 /* Save the provided single-threaded target. We save this in a separate
4624 variable because another target we've inherited from (e.g. inf-ptrace)
4625 may have saved a pointer to T; we want to use it for the final
4626 process stratum target. */
4627 linux_ops_saved
= *t
;
4628 linux_ops
= &linux_ops_saved
;
4630 /* Override some methods for multithreading. */
4631 t
->to_create_inferior
= linux_nat_create_inferior
;
4632 t
->to_attach
= linux_nat_attach
;
4633 t
->to_detach
= linux_nat_detach
;
4634 t
->to_resume
= linux_nat_resume
;
4635 t
->to_wait
= linux_nat_wait
;
4636 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4637 t
->to_kill
= linux_nat_kill
;
4638 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4639 t
->to_thread_alive
= linux_nat_thread_alive
;
4640 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4641 t
->to_has_thread_control
= tc_schedlock
;
4643 t
->to_can_async_p
= linux_nat_can_async_p
;
4644 t
->to_is_async_p
= linux_nat_is_async_p
;
4645 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4646 t
->to_async
= linux_nat_async
;
4647 t
->to_async_mask
= linux_nat_async_mask
;
4648 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4649 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4650 t
->to_close
= linux_nat_close
;
4652 /* Methods for non-stop support. */
4653 t
->to_stop
= linux_nat_stop
;
4655 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4657 /* We don't change the stratum; this target will sit at
4658 process_stratum and thread_db will set at thread_stratum. This
4659 is a little strange, since this is a multi-threaded-capable
4660 target, but we want to be on the stack below thread_db, and we
4661 also want to be used for single-threaded processes. */
4666 /* Register a method to call whenever a new thread is attached. */
4668 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4670 /* Save the pointer. We only support a single registered instance
4671 of the GNU/Linux native target, so we do not need to map this to
4673 linux_nat_new_thread
= new_thread
;
4676 /* Register a method that converts a siginfo object between the layout
4677 that ptrace returns, and the layout in the architecture of the
4680 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4681 int (*siginfo_fixup
) (struct siginfo
*,
4685 /* Save the pointer. */
4686 linux_nat_siginfo_fixup
= siginfo_fixup
;
4689 /* Return the saved siginfo associated with PTID. */
4691 linux_nat_get_siginfo (ptid_t ptid
)
4693 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4695 gdb_assert (lp
!= NULL
);
4697 return &lp
->siginfo
;
4700 /* Provide a prototype to silence -Wmissing-prototypes. */
4701 extern initialize_file_ftype _initialize_linux_nat
;
4704 _initialize_linux_nat (void)
4708 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4709 Show /proc process information about any running process.\n\
4710 Specify any process id, or use the program being debugged by default.\n\
4711 Specify any of the following keywords for detailed info:\n\
4712 mappings -- list of mapped memory regions.\n\
4713 stat -- list a bunch of random process info.\n\
4714 status -- list a different bunch of random process info.\n\
4715 all -- list all available /proc info."));
4717 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4718 &debug_linux_nat
, _("\
4719 Set debugging of GNU/Linux lwp module."), _("\
4720 Show debugging of GNU/Linux lwp module."), _("\
4721 Enables printf debugging output."),
4723 show_debug_linux_nat
,
4724 &setdebuglist
, &showdebuglist
);
4726 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4727 &debug_linux_nat_async
, _("\
4728 Set debugging of GNU/Linux async lwp module."), _("\
4729 Show debugging of GNU/Linux async lwp module."), _("\
4730 Enables printf debugging output."),
4732 show_debug_linux_nat_async
,
4733 &setdebuglist
, &showdebuglist
);
4735 /* Save this mask as the default. */
4736 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4738 /* Install a SIGCHLD handler. */
4739 sigchld_action
.sa_handler
= sigchld_handler
;
4740 sigemptyset (&sigchld_action
.sa_mask
);
4741 sigchld_action
.sa_flags
= SA_RESTART
;
4743 /* Make it the default. */
4744 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4746 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4747 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4748 sigdelset (&suspend_mask
, SIGCHLD
);
4750 sigemptyset (&blocked_mask
);
4752 add_setshow_boolean_cmd ("disable-randomization", class_support
,
4753 &disable_randomization
, _("\
4754 Set disabling of debuggee's virtual address space randomization."), _("\
4755 Show disabling of debuggee's virtual address space randomization."), _("\
4756 When this mode is on (which is the default), randomization of the virtual\n\
4757 address space is disabled. Standalone programs run with the randomization\n\
4758 enabled by default on some platforms."),
4759 &set_disable_randomization
,
4760 &show_disable_randomization
,
4761 &setlist
, &showlist
);
4765 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4766 the GNU/Linux Threads library and therefore doesn't really belong
4769 /* Read variable NAME in the target and return its value if found.
4770 Otherwise return zero. It is assumed that the type of the variable
4774 get_signo (const char *name
)
4776 struct minimal_symbol
*ms
;
4779 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4783 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4784 sizeof (signo
)) != 0)
4790 /* Return the set of signals used by the threads library in *SET. */
4793 lin_thread_get_thread_signals (sigset_t
*set
)
4795 struct sigaction action
;
4796 int restart
, cancel
;
4798 sigemptyset (&blocked_mask
);
4801 restart
= get_signo ("__pthread_sig_restart");
4802 cancel
= get_signo ("__pthread_sig_cancel");
4804 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4805 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4806 not provide any way for the debugger to query the signal numbers -
4807 fortunately they don't change! */
4810 restart
= __SIGRTMIN
;
4813 cancel
= __SIGRTMIN
+ 1;
4815 sigaddset (set
, restart
);
4816 sigaddset (set
, cancel
);
4818 /* The GNU/Linux Threads library makes terminating threads send a
4819 special "cancel" signal instead of SIGCHLD. Make sure we catch
4820 those (to prevent them from terminating GDB itself, which is
4821 likely to be their default action) and treat them the same way as
4824 action
.sa_handler
= sigchld_handler
;
4825 sigemptyset (&action
.sa_mask
);
4826 action
.sa_flags
= SA_RESTART
;
4827 sigaction (cancel
, &action
, NULL
);
4829 /* We block the "cancel" signal throughout this code ... */
4830 sigaddset (&blocked_mask
, cancel
);
4831 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4833 /* ... except during a sigsuspend. */
4834 sigdelset (&suspend_mask
, cancel
);