1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "linux-fork.h"
34 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
50 #include "event-loop.h"
51 #include "event-top.h"
53 #include <sys/types.h>
54 #include "gdb_dirent.h"
55 #include "xml-support.h"
57 #ifdef HAVE_PERSONALITY
58 # include <sys/personality.h>
59 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
60 # define ADDR_NO_RANDOMIZE 0x0040000
62 #endif /* HAVE_PERSONALITY */
64 /* This comment documents high-level logic of this file.
66 Waiting for events in sync mode
67 ===============================
69 When waiting for an event in a specific thread, we just use waitpid, passing
70 the specific pid, and not passing WNOHANG.
72 When waiting for an event in all threads, waitpid is not quite good. Prior to
73 version 2.4, Linux can either wait for event in main thread, or in secondary
74 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
75 miss an event. The solution is to use non-blocking waitpid, together with
76 sigsuspend. First, we use non-blocking waitpid to get an event in the main
77 process, if any. Second, we use non-blocking waitpid with the __WCLONED
78 flag to check for events in cloned processes. If nothing is found, we use
79 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
80 happened to a child process -- and SIGCHLD will be delivered both for events
81 in main debugged process and in cloned processes. As soon as we know there's
82 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
84 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
85 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
86 blocked, the signal becomes pending and sigsuspend immediately
87 notices it and returns.
89 Waiting for events in async mode
90 ================================
92 In async mode, GDB should always be ready to handle both user input and target
93 events, so neither blocking waitpid nor sigsuspend are viable
94 options. Instead, we should notify the GDB main event loop whenever there's
95 unprocessed event from the target. The only way to notify this event loop is
96 to make it wait on input from a pipe, and write something to the pipe whenever
97 there's event. Obviously, if we fail to notify the event loop if there's
98 target event, it's bad. If we notify the event loop when there's no event
99 from target, linux-nat.c will detect that there's no event, actually, and
100 report event of type TARGET_WAITKIND_IGNORE, but it will waste time and
103 The main design point is that every time GDB is outside linux-nat.c, we have a
104 SIGCHLD handler installed that is called when something happens to the target
105 and notifies the GDB event loop. Also, the event is extracted from the target
106 using waitpid and stored for future use. Whenever GDB core decides to handle
107 the event, and calls into linux-nat.c, we disable SIGCHLD and process things
108 as in sync mode, except that before waitpid call we check if there are any
109 previously read events.
111 It could happen that during event processing, we'll try to get more events
112 than there are events in the local queue, which will result to waitpid call.
113 Those waitpid calls, while blocking, are guarantied to always have
114 something for waitpid to return. E.g., stopping a thread with SIGSTOP, and
115 waiting for the lwp to stop.
117 The event loop is notified about new events using a pipe. SIGCHLD handler does
118 waitpid and writes the results in to a pipe. GDB event loop has the other end
119 of the pipe among the sources. When event loop starts to process the event
120 and calls a function in linux-nat.c, all events from the pipe are transferred
121 into a local queue and SIGCHLD is blocked. Further processing goes as in sync
122 mode. Before we return from linux_nat_wait, we transfer all unprocessed events
123 from local queue back to the pipe, so that when we get back to event loop,
124 event loop will notice there's something more to do.
126 SIGCHLD is blocked when we're inside target_wait, so that should we actually
127 want to wait for some more events, SIGCHLD handler does not steal them from
128 us. Technically, it would be possible to add new events to the local queue but
129 it's about the same amount of work as blocking SIGCHLD.
131 This moving of events from pipe into local queue and back into pipe when we
132 enter/leave linux-nat.c is somewhat ugly. Unfortunately, GDB event loop is
133 home-grown and incapable to wait on any queue.
138 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
139 signal is not entirely significant; we just need for a signal to be delivered,
140 so that we can intercept it. SIGSTOP's advantage is that it can not be
141 blocked. A disadvantage is that it is not a real-time signal, so it can only
142 be queued once; we do not keep track of other sources of SIGSTOP.
144 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
145 use them, because they have special behavior when the signal is generated -
146 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
147 kills the entire thread group.
149 A delivered SIGSTOP would stop the entire thread group, not just the thread we
150 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
151 cancel it (by PTRACE_CONT without passing SIGSTOP).
153 We could use a real-time signal instead. This would solve those problems; we
154 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
155 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
156 generates it, and there are races with trying to find a signal that is not
160 #define O_LARGEFILE 0
163 /* If the system headers did not provide the constants, hard-code the normal
165 #ifndef PTRACE_EVENT_FORK
167 #define PTRACE_SETOPTIONS 0x4200
168 #define PTRACE_GETEVENTMSG 0x4201
170 /* options set using PTRACE_SETOPTIONS */
171 #define PTRACE_O_TRACESYSGOOD 0x00000001
172 #define PTRACE_O_TRACEFORK 0x00000002
173 #define PTRACE_O_TRACEVFORK 0x00000004
174 #define PTRACE_O_TRACECLONE 0x00000008
175 #define PTRACE_O_TRACEEXEC 0x00000010
176 #define PTRACE_O_TRACEVFORKDONE 0x00000020
177 #define PTRACE_O_TRACEEXIT 0x00000040
179 /* Wait extended result codes for the above trace options. */
180 #define PTRACE_EVENT_FORK 1
181 #define PTRACE_EVENT_VFORK 2
182 #define PTRACE_EVENT_CLONE 3
183 #define PTRACE_EVENT_EXEC 4
184 #define PTRACE_EVENT_VFORK_DONE 5
185 #define PTRACE_EVENT_EXIT 6
187 #endif /* PTRACE_EVENT_FORK */
189 /* We can't always assume that this flag is available, but all systems
190 with the ptrace event handlers also have __WALL, so it's safe to use
193 #define __WALL 0x40000000 /* Wait for any child. */
196 #ifndef PTRACE_GETSIGINFO
197 # define PTRACE_GETSIGINFO 0x4202
198 # define PTRACE_SETSIGINFO 0x4203
201 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
202 the use of the multi-threaded target. */
203 static struct target_ops
*linux_ops
;
204 static struct target_ops linux_ops_saved
;
206 /* The method to call, if any, when a new thread is attached. */
207 static void (*linux_nat_new_thread
) (ptid_t
);
209 /* The method to call, if any, when the siginfo object needs to be
210 converted between the layout returned by ptrace, and the layout in
211 the architecture of the inferior. */
212 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
216 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
217 Called by our to_xfer_partial. */
218 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
220 const char *, gdb_byte
*,
224 static int debug_linux_nat
;
226 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
227 struct cmd_list_element
*c
, const char *value
)
229 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
233 static int debug_linux_nat_async
= 0;
235 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
236 struct cmd_list_element
*c
, const char *value
)
238 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
242 static int disable_randomization
= 1;
245 show_disable_randomization (struct ui_file
*file
, int from_tty
,
246 struct cmd_list_element
*c
, const char *value
)
248 #ifdef HAVE_PERSONALITY
249 fprintf_filtered (file
, _("\
250 Disabling randomization of debuggee's virtual address space is %s.\n"),
252 #else /* !HAVE_PERSONALITY */
254 Disabling randomization of debuggee's virtual address space is unsupported on\n\
255 this platform.\n"), file
);
256 #endif /* !HAVE_PERSONALITY */
260 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
262 #ifndef HAVE_PERSONALITY
264 Disabling randomization of debuggee's virtual address space is unsupported on\n\
266 #endif /* !HAVE_PERSONALITY */
269 static int linux_parent_pid
;
271 struct simple_pid_list
275 struct simple_pid_list
*next
;
277 struct simple_pid_list
*stopped_pids
;
279 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
280 can not be used, 1 if it can. */
282 static int linux_supports_tracefork_flag
= -1;
284 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
285 PTRACE_O_TRACEVFORKDONE. */
287 static int linux_supports_tracevforkdone_flag
= -1;
289 /* Async mode support */
291 /* Zero if the async mode, although enabled, is masked, which means
292 linux_nat_wait should behave as if async mode was off. */
293 static int linux_nat_async_mask_value
= 1;
295 /* The read/write ends of the pipe registered as waitable file in the
297 static int linux_nat_event_pipe
[2] = { -1, -1 };
299 /* Number of queued events in the pipe. */
300 static volatile int linux_nat_num_queued_events
;
302 /* The possible SIGCHLD handling states. */
306 /* SIGCHLD disabled, with action set to sigchld_handler, for the
307 sigsuspend in linux_nat_wait. */
309 /* SIGCHLD enabled, with action set to async_sigchld_handler. */
311 /* Set SIGCHLD to default action. Used while creating an
316 /* The current SIGCHLD handling state. */
317 static enum sigchld_state linux_nat_async_events_state
;
319 static enum sigchld_state
linux_nat_async_events (enum sigchld_state enable
);
320 static void pipe_to_local_event_queue (void);
321 static void local_event_queue_to_pipe (void);
322 static void linux_nat_event_pipe_push (int pid
, int status
, int options
);
323 static int linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
);
324 static void linux_nat_set_async_mode (int on
);
325 static void linux_nat_async (void (*callback
)
326 (enum inferior_event_type event_type
, void *context
),
328 static int linux_nat_async_mask (int mask
);
329 static int kill_lwp (int lwpid
, int signo
);
331 static int stop_callback (struct lwp_info
*lp
, void *data
);
333 /* Captures the result of a successful waitpid call, along with the
334 options used in that call. */
335 struct waitpid_result
340 struct waitpid_result
*next
;
343 /* A singly-linked list of the results of the waitpid calls performed
344 in the async SIGCHLD handler. */
345 static struct waitpid_result
*waitpid_queue
= NULL
;
347 /* Similarly to `waitpid', but check the local event queue instead of
348 querying the kernel queue. If PEEK, don't remove the event found
352 queued_waitpid_1 (int pid
, int *status
, int flags
, int peek
)
354 struct waitpid_result
*msg
= waitpid_queue
, *prev
= NULL
;
356 if (debug_linux_nat_async
)
357 fprintf_unfiltered (gdb_stdlog
,
359 QWPID: linux_nat_async_events_state(%d), linux_nat_num_queued_events(%d)\n",
360 linux_nat_async_events_state
,
361 linux_nat_num_queued_events
);
365 for (; msg
; prev
= msg
, msg
= msg
->next
)
366 if (pid
== -1 || pid
== msg
->pid
)
369 else if (flags
& __WCLONE
)
371 for (; msg
; prev
= msg
, msg
= msg
->next
)
372 if (msg
->options
& __WCLONE
373 && (pid
== -1 || pid
== msg
->pid
))
378 for (; msg
; prev
= msg
, msg
= msg
->next
)
379 if ((msg
->options
& __WCLONE
) == 0
380 && (pid
== -1 || pid
== msg
->pid
))
389 *status
= msg
->status
;
392 if (debug_linux_nat_async
)
393 fprintf_unfiltered (gdb_stdlog
, "QWPID: pid(%d), status(%x)\n",
399 prev
->next
= msg
->next
;
401 waitpid_queue
= msg
->next
;
410 if (debug_linux_nat_async
)
411 fprintf_unfiltered (gdb_stdlog
, "QWPID: miss\n");
418 /* Similarly to `waitpid', but check the local event queue. */
421 queued_waitpid (int pid
, int *status
, int flags
)
423 return queued_waitpid_1 (pid
, status
, flags
, 0);
427 push_waitpid (int pid
, int status
, int options
)
429 struct waitpid_result
*event
, *new_event
;
431 new_event
= xmalloc (sizeof (*new_event
));
432 new_event
->pid
= pid
;
433 new_event
->status
= status
;
434 new_event
->options
= options
;
435 new_event
->next
= NULL
;
439 for (event
= waitpid_queue
;
440 event
&& event
->next
;
444 event
->next
= new_event
;
447 waitpid_queue
= new_event
;
450 /* Drain all queued events of PID. If PID is -1, the effect is of
451 draining all events. */
453 drain_queued_events (int pid
)
455 while (queued_waitpid (pid
, NULL
, __WALL
) != -1)
460 /* Trivial list manipulation functions to keep track of a list of
461 new stopped processes. */
463 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
465 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
467 new_pid
->status
= status
;
468 new_pid
->next
= *listp
;
473 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
475 struct simple_pid_list
**p
;
477 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
478 if ((*p
)->pid
== pid
)
480 struct simple_pid_list
*next
= (*p
)->next
;
481 *status
= (*p
)->status
;
490 linux_record_stopped_pid (int pid
, int status
)
492 add_to_pid_list (&stopped_pids
, pid
, status
);
496 /* A helper function for linux_test_for_tracefork, called after fork (). */
499 linux_tracefork_child (void)
503 ptrace (PTRACE_TRACEME
, 0, 0, 0);
504 kill (getpid (), SIGSTOP
);
509 /* Wrapper function for waitpid which handles EINTR, and checks for
510 locally queued events. */
513 my_waitpid (int pid
, int *status
, int flags
)
517 /* There should be no concurrent calls to waitpid. */
518 gdb_assert (linux_nat_async_events_state
== sigchld_sync
);
520 ret
= queued_waitpid (pid
, status
, flags
);
526 ret
= waitpid (pid
, status
, flags
);
528 while (ret
== -1 && errno
== EINTR
);
533 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
535 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
536 we know that the feature is not available. This may change the tracing
537 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
539 However, if it succeeds, we don't know for sure that the feature is
540 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
541 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
542 fork tracing, and let it fork. If the process exits, we assume that we
543 can't use TRACEFORK; if we get the fork notification, and we can extract
544 the new child's PID, then we assume that we can. */
547 linux_test_for_tracefork (int original_pid
)
549 int child_pid
, ret
, status
;
551 enum sigchld_state async_events_original_state
;
553 async_events_original_state
= linux_nat_async_events (sigchld_sync
);
555 linux_supports_tracefork_flag
= 0;
556 linux_supports_tracevforkdone_flag
= 0;
558 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
564 perror_with_name (("fork"));
567 linux_tracefork_child ();
569 ret
= my_waitpid (child_pid
, &status
, 0);
571 perror_with_name (("waitpid"));
572 else if (ret
!= child_pid
)
573 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
574 if (! WIFSTOPPED (status
))
575 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
577 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
580 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
583 warning (_("linux_test_for_tracefork: failed to kill child"));
584 linux_nat_async_events (async_events_original_state
);
588 ret
= my_waitpid (child_pid
, &status
, 0);
589 if (ret
!= child_pid
)
590 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
591 else if (!WIFSIGNALED (status
))
592 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
593 "killed child"), status
);
595 linux_nat_async_events (async_events_original_state
);
599 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
600 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
601 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
602 linux_supports_tracevforkdone_flag
= (ret
== 0);
604 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
606 warning (_("linux_test_for_tracefork: failed to resume child"));
608 ret
= my_waitpid (child_pid
, &status
, 0);
610 if (ret
== child_pid
&& WIFSTOPPED (status
)
611 && status
>> 16 == PTRACE_EVENT_FORK
)
614 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
615 if (ret
== 0 && second_pid
!= 0)
619 linux_supports_tracefork_flag
= 1;
620 my_waitpid (second_pid
, &second_status
, 0);
621 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
623 warning (_("linux_test_for_tracefork: failed to kill second child"));
624 my_waitpid (second_pid
, &status
, 0);
628 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
629 "(%d, status 0x%x)"), ret
, status
);
631 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
633 warning (_("linux_test_for_tracefork: failed to kill child"));
634 my_waitpid (child_pid
, &status
, 0);
636 linux_nat_async_events (async_events_original_state
);
639 /* Return non-zero iff we have tracefork functionality available.
640 This function also sets linux_supports_tracefork_flag. */
643 linux_supports_tracefork (int pid
)
645 if (linux_supports_tracefork_flag
== -1)
646 linux_test_for_tracefork (pid
);
647 return linux_supports_tracefork_flag
;
651 linux_supports_tracevforkdone (int pid
)
653 if (linux_supports_tracefork_flag
== -1)
654 linux_test_for_tracefork (pid
);
655 return linux_supports_tracevforkdone_flag
;
660 linux_enable_event_reporting (ptid_t ptid
)
662 int pid
= ptid_get_lwp (ptid
);
666 pid
= ptid_get_pid (ptid
);
668 if (! linux_supports_tracefork (pid
))
671 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
672 | PTRACE_O_TRACECLONE
;
673 if (linux_supports_tracevforkdone (pid
))
674 options
|= PTRACE_O_TRACEVFORKDONE
;
676 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
677 read-only process state. */
679 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
683 linux_child_post_attach (int pid
)
685 linux_enable_event_reporting (pid_to_ptid (pid
));
686 check_for_thread_db ();
690 linux_child_post_startup_inferior (ptid_t ptid
)
692 linux_enable_event_reporting (ptid
);
693 check_for_thread_db ();
697 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
700 struct target_waitstatus last_status
;
702 int parent_pid
, child_pid
;
704 if (target_can_async_p ())
705 target_async (NULL
, 0);
707 get_last_target_status (&last_ptid
, &last_status
);
708 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
709 parent_pid
= ptid_get_lwp (last_ptid
);
711 parent_pid
= ptid_get_pid (last_ptid
);
712 child_pid
= PIDGET (last_status
.value
.related_pid
);
716 /* We're already attached to the parent, by default. */
718 /* Before detaching from the child, remove all breakpoints from
719 it. (This won't actually modify the breakpoint list, but will
720 physically remove the breakpoints from the child.) */
721 /* If we vforked this will remove the breakpoints from the parent
722 also, but they'll be reinserted below. */
723 detach_breakpoints (child_pid
);
725 /* Detach new forked process? */
728 if (info_verbose
|| debug_linux_nat
)
730 target_terminal_ours ();
731 fprintf_filtered (gdb_stdlog
,
732 "Detaching after fork from child process %d.\n",
736 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
740 struct fork_info
*fp
;
741 struct inferior
*parent_inf
, *child_inf
;
743 /* Add process to GDB's tables. */
744 child_inf
= add_inferior (child_pid
);
746 parent_inf
= find_inferior_pid (GET_PID (last_ptid
));
747 child_inf
->attach_flag
= parent_inf
->attach_flag
;
749 /* Retain child fork in ptrace (stopped) state. */
750 fp
= find_fork_pid (child_pid
);
752 fp
= add_fork (child_pid
);
753 fork_save_infrun_state (fp
, 0);
758 gdb_assert (linux_supports_tracefork_flag
>= 0);
759 if (linux_supports_tracevforkdone (0))
763 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
764 my_waitpid (parent_pid
, &status
, __WALL
);
765 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
766 warning (_("Unexpected waitpid result %06x when waiting for "
767 "vfork-done"), status
);
771 /* We can't insert breakpoints until the child has
772 finished with the shared memory region. We need to
773 wait until that happens. Ideal would be to just
775 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
776 - waitpid (parent_pid, &status, __WALL);
777 However, most architectures can't handle a syscall
778 being traced on the way out if it wasn't traced on
781 We might also think to loop, continuing the child
782 until it exits or gets a SIGTRAP. One problem is
783 that the child might call ptrace with PTRACE_TRACEME.
785 There's no simple and reliable way to figure out when
786 the vforked child will be done with its copy of the
787 shared memory. We could step it out of the syscall,
788 two instructions, let it go, and then single-step the
789 parent once. When we have hardware single-step, this
790 would work; with software single-step it could still
791 be made to work but we'd have to be able to insert
792 single-step breakpoints in the child, and we'd have
793 to insert -just- the single-step breakpoint in the
794 parent. Very awkward.
796 In the end, the best we can do is to make sure it
797 runs for a little while. Hopefully it will be out of
798 range of any breakpoints we reinsert. Usually this
799 is only the single-step breakpoint at vfork's return
805 /* Since we vforked, breakpoints were removed in the parent
806 too. Put them back. */
807 reattach_breakpoints (parent_pid
);
812 struct thread_info
*last_tp
= find_thread_pid (last_ptid
);
813 struct thread_info
*tp
;
814 char child_pid_spelling
[40];
815 struct inferior
*parent_inf
, *child_inf
;
817 /* Copy user stepping state to the new inferior thread. */
818 struct breakpoint
*step_resume_breakpoint
= last_tp
->step_resume_breakpoint
;
819 CORE_ADDR step_range_start
= last_tp
->step_range_start
;
820 CORE_ADDR step_range_end
= last_tp
->step_range_end
;
821 struct frame_id step_frame_id
= last_tp
->step_frame_id
;
823 /* Otherwise, deleting the parent would get rid of this
825 last_tp
->step_resume_breakpoint
= NULL
;
827 /* Needed to keep the breakpoint lists in sync. */
829 detach_breakpoints (child_pid
);
831 /* Before detaching from the parent, remove all breakpoints from it. */
832 remove_breakpoints ();
834 if (info_verbose
|| debug_linux_nat
)
836 target_terminal_ours ();
837 fprintf_filtered (gdb_stdlog
,
838 "Attaching after fork to child process %d.\n",
842 /* Add the new inferior first, so that the target_detach below
843 doesn't unpush the target. */
845 child_inf
= add_inferior (child_pid
);
847 parent_inf
= find_inferior_pid (GET_PID (last_ptid
));
848 child_inf
->attach_flag
= parent_inf
->attach_flag
;
850 /* If we're vforking, we may want to hold on to the parent until
851 the child exits or execs. At exec time we can remove the old
852 breakpoints from the parent and detach it; at exit time we
853 could do the same (or even, sneakily, resume debugging it - the
854 child's exec has failed, or something similar).
856 This doesn't clean up "properly", because we can't call
857 target_detach, but that's OK; if the current target is "child",
858 then it doesn't need any further cleanups, and lin_lwp will
859 generally not encounter vfork (vfork is defined to fork
862 The holding part is very easy if we have VFORKDONE events;
863 but keeping track of both processes is beyond GDB at the
864 moment. So we don't expose the parent to the rest of GDB.
865 Instead we quietly hold onto it until such time as we can
870 linux_parent_pid
= parent_pid
;
871 detach_inferior (parent_pid
);
873 else if (!detach_fork
)
875 struct fork_info
*fp
;
876 /* Retain parent fork in ptrace (stopped) state. */
877 fp
= find_fork_pid (parent_pid
);
879 fp
= add_fork (parent_pid
);
880 fork_save_infrun_state (fp
, 0);
882 /* Also add an entry for the child fork. */
883 fp
= find_fork_pid (child_pid
);
885 fp
= add_fork (child_pid
);
886 fork_save_infrun_state (fp
, 0);
889 target_detach (NULL
, 0);
891 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
893 linux_nat_switch_fork (inferior_ptid
);
894 check_for_thread_db ();
896 tp
= inferior_thread ();
897 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
898 tp
->step_range_start
= step_range_start
;
899 tp
->step_range_end
= step_range_end
;
900 tp
->step_frame_id
= step_frame_id
;
902 /* Reset breakpoints in the child as appropriate. */
903 follow_inferior_reset_breakpoints ();
906 if (target_can_async_p ())
907 target_async (inferior_event_handler
, 0);
914 linux_child_insert_fork_catchpoint (int pid
)
916 if (! linux_supports_tracefork (pid
))
917 error (_("Your system does not support fork catchpoints."));
921 linux_child_insert_vfork_catchpoint (int pid
)
923 if (!linux_supports_tracefork (pid
))
924 error (_("Your system does not support vfork catchpoints."));
928 linux_child_insert_exec_catchpoint (int pid
)
930 if (!linux_supports_tracefork (pid
))
931 error (_("Your system does not support exec catchpoints."));
934 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
935 are processes sharing the same VM space. A multi-threaded process
936 is basically a group of such processes. However, such a grouping
937 is almost entirely a user-space issue; the kernel doesn't enforce
938 such a grouping at all (this might change in the future). In
939 general, we'll rely on the threads library (i.e. the GNU/Linux
940 Threads library) to provide such a grouping.
942 It is perfectly well possible to write a multi-threaded application
943 without the assistance of a threads library, by using the clone
944 system call directly. This module should be able to give some
945 rudimentary support for debugging such applications if developers
946 specify the CLONE_PTRACE flag in the clone system call, and are
947 using the Linux kernel 2.4 or above.
949 Note that there are some peculiarities in GNU/Linux that affect
952 - In general one should specify the __WCLONE flag to waitpid in
953 order to make it report events for any of the cloned processes
954 (and leave it out for the initial process). However, if a cloned
955 process has exited the exit status is only reported if the
956 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
957 we cannot use it since GDB must work on older systems too.
959 - When a traced, cloned process exits and is waited for by the
960 debugger, the kernel reassigns it to the original parent and
961 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
962 library doesn't notice this, which leads to the "zombie problem":
963 When debugged a multi-threaded process that spawns a lot of
964 threads will run out of processes, even if the threads exit,
965 because the "zombies" stay around. */
967 /* List of known LWPs. */
968 struct lwp_info
*lwp_list
;
970 /* Number of LWPs in the list. */
974 /* Original signal mask. */
975 static sigset_t normal_mask
;
977 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
978 _initialize_linux_nat. */
979 static sigset_t suspend_mask
;
981 /* SIGCHLD action for synchronous mode. */
982 struct sigaction sync_sigchld_action
;
984 /* SIGCHLD action for asynchronous mode. */
985 static struct sigaction async_sigchld_action
;
987 /* SIGCHLD default action, to pass to new inferiors. */
988 static struct sigaction sigchld_default_action
;
991 /* Prototypes for local functions. */
992 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
993 static int linux_nat_thread_alive (ptid_t ptid
);
994 static char *linux_child_pid_to_exec_file (int pid
);
995 static int cancel_breakpoint (struct lwp_info
*lp
);
998 /* Convert wait status STATUS to a string. Used for printing debug
1002 status_to_str (int status
)
1004 static char buf
[64];
1006 if (WIFSTOPPED (status
))
1007 snprintf (buf
, sizeof (buf
), "%s (stopped)",
1008 strsignal (WSTOPSIG (status
)));
1009 else if (WIFSIGNALED (status
))
1010 snprintf (buf
, sizeof (buf
), "%s (terminated)",
1011 strsignal (WSTOPSIG (status
)));
1013 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1018 /* Initialize the list of LWPs. Note that this module, contrary to
1019 what GDB's generic threads layer does for its thread list,
1020 re-initializes the LWP lists whenever we mourn or detach (which
1021 doesn't involve mourning) the inferior. */
1024 init_lwp_list (void)
1026 struct lwp_info
*lp
, *lpnext
;
1028 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1038 /* Add the LWP specified by PID to the list. Return a pointer to the
1039 structure describing the new LWP. The LWP should already be stopped
1040 (with an exception for the very first LWP). */
1042 static struct lwp_info
*
1043 add_lwp (ptid_t ptid
)
1045 struct lwp_info
*lp
;
1047 gdb_assert (is_lwp (ptid
));
1049 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1051 memset (lp
, 0, sizeof (struct lwp_info
));
1053 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1057 lp
->next
= lwp_list
;
1061 if (num_lwps
> 1 && linux_nat_new_thread
!= NULL
)
1062 linux_nat_new_thread (ptid
);
1067 /* Remove the LWP specified by PID from the list. */
1070 delete_lwp (ptid_t ptid
)
1072 struct lwp_info
*lp
, *lpprev
;
1076 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1077 if (ptid_equal (lp
->ptid
, ptid
))
1086 lpprev
->next
= lp
->next
;
1088 lwp_list
= lp
->next
;
1093 /* Return a pointer to the structure describing the LWP corresponding
1094 to PID. If no corresponding LWP could be found, return NULL. */
1096 static struct lwp_info
*
1097 find_lwp_pid (ptid_t ptid
)
1099 struct lwp_info
*lp
;
1103 lwp
= GET_LWP (ptid
);
1105 lwp
= GET_PID (ptid
);
1107 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1108 if (lwp
== GET_LWP (lp
->ptid
))
1114 /* Call CALLBACK with its second argument set to DATA for every LWP in
1115 the list. If CALLBACK returns 1 for a particular LWP, return a
1116 pointer to the structure describing that LWP immediately.
1117 Otherwise return NULL. */
1120 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
1122 struct lwp_info
*lp
, *lpnext
;
1124 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1127 if ((*callback
) (lp
, data
))
1134 /* Update our internal state when changing from one fork (checkpoint,
1135 et cetera) to another indicated by NEW_PTID. We can only switch
1136 single-threaded applications, so we only create one new LWP, and
1137 the previous list is discarded. */
1140 linux_nat_switch_fork (ptid_t new_ptid
)
1142 struct lwp_info
*lp
;
1145 lp
= add_lwp (new_ptid
);
1148 init_thread_list ();
1149 add_thread_silent (new_ptid
);
1152 /* Handle the exit of a single thread LP. */
1155 exit_lwp (struct lwp_info
*lp
)
1157 struct thread_info
*th
= find_thread_pid (lp
->ptid
);
1161 if (print_thread_events
)
1162 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1164 delete_thread (lp
->ptid
);
1167 delete_lwp (lp
->ptid
);
1170 /* Detect `T (stopped)' in `/proc/PID/status'.
1171 Other states including `T (tracing stop)' are reported as false. */
1174 pid_is_stopped (pid_t pid
)
1180 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1181 status_file
= fopen (buf
, "r");
1182 if (status_file
!= NULL
)
1186 while (fgets (buf
, sizeof (buf
), status_file
))
1188 if (strncmp (buf
, "State:", 6) == 0)
1194 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1196 fclose (status_file
);
1201 /* Wait for the LWP specified by LP, which we have just attached to.
1202 Returns a wait status for that LWP, to cache. */
1205 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1208 pid_t new_pid
, pid
= GET_LWP (ptid
);
1211 if (pid_is_stopped (pid
))
1213 if (debug_linux_nat
)
1214 fprintf_unfiltered (gdb_stdlog
,
1215 "LNPAW: Attaching to a stopped process\n");
1217 /* The process is definitely stopped. It is in a job control
1218 stop, unless the kernel predates the TASK_STOPPED /
1219 TASK_TRACED distinction, in which case it might be in a
1220 ptrace stop. Make sure it is in a ptrace stop; from there we
1221 can kill it, signal it, et cetera.
1223 First make sure there is a pending SIGSTOP. Since we are
1224 already attached, the process can not transition from stopped
1225 to running without a PTRACE_CONT; so we know this signal will
1226 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1227 probably already in the queue (unless this kernel is old
1228 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1229 is not an RT signal, it can only be queued once. */
1230 kill_lwp (pid
, SIGSTOP
);
1232 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1233 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1234 ptrace (PTRACE_CONT
, pid
, 0, 0);
1237 /* Make sure the initial process is stopped. The user-level threads
1238 layer might want to poke around in the inferior, and that won't
1239 work if things haven't stabilized yet. */
1240 new_pid
= my_waitpid (pid
, &status
, 0);
1241 if (new_pid
== -1 && errno
== ECHILD
)
1244 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1246 /* Try again with __WCLONE to check cloned processes. */
1247 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1251 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1253 if (WSTOPSIG (status
) != SIGSTOP
)
1256 if (debug_linux_nat
)
1257 fprintf_unfiltered (gdb_stdlog
,
1258 "LNPAW: Received %s after attaching\n",
1259 status_to_str (status
));
1265 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1266 if the new LWP could not be attached. */
1269 lin_lwp_attach_lwp (ptid_t ptid
)
1271 struct lwp_info
*lp
;
1272 enum sigchld_state async_events_original_state
;
1274 gdb_assert (is_lwp (ptid
));
1276 async_events_original_state
= linux_nat_async_events (sigchld_sync
);
1278 lp
= find_lwp_pid (ptid
);
1280 /* We assume that we're already attached to any LWP that has an id
1281 equal to the overall process id, and to any LWP that is already
1282 in our list of LWPs. If we're not seeing exit events from threads
1283 and we've had PID wraparound since we last tried to stop all threads,
1284 this assumption might be wrong; fortunately, this is very unlikely
1286 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1288 int status
, cloned
= 0, signalled
= 0;
1290 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1292 /* If we fail to attach to the thread, issue a warning,
1293 but continue. One way this can happen is if thread
1294 creation is interrupted; as of Linux kernel 2.6.19, a
1295 bug may place threads in the thread list and then fail
1297 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1298 safe_strerror (errno
));
1302 if (debug_linux_nat
)
1303 fprintf_unfiltered (gdb_stdlog
,
1304 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1305 target_pid_to_str (ptid
));
1307 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1308 lp
= add_lwp (ptid
);
1310 lp
->cloned
= cloned
;
1311 lp
->signalled
= signalled
;
1312 if (WSTOPSIG (status
) != SIGSTOP
)
1315 lp
->status
= status
;
1318 target_post_attach (GET_LWP (lp
->ptid
));
1320 if (debug_linux_nat
)
1322 fprintf_unfiltered (gdb_stdlog
,
1323 "LLAL: waitpid %s received %s\n",
1324 target_pid_to_str (ptid
),
1325 status_to_str (status
));
1330 /* We assume that the LWP representing the original process is
1331 already stopped. Mark it as stopped in the data structure
1332 that the GNU/linux ptrace layer uses to keep track of
1333 threads. Note that this won't have already been done since
1334 the main thread will have, we assume, been stopped by an
1335 attach from a different layer. */
1337 lp
= add_lwp (ptid
);
1341 linux_nat_async_events (async_events_original_state
);
1346 linux_nat_create_inferior (struct target_ops
*ops
,
1347 char *exec_file
, char *allargs
, char **env
,
1350 int saved_async
= 0;
1351 #ifdef HAVE_PERSONALITY
1352 int personality_orig
= 0, personality_set
= 0;
1353 #endif /* HAVE_PERSONALITY */
1355 /* The fork_child mechanism is synchronous and calls target_wait, so
1356 we have to mask the async mode. */
1358 if (target_can_async_p ())
1359 /* Mask async mode. Creating a child requires a loop calling
1360 wait_for_inferior currently. */
1361 saved_async
= linux_nat_async_mask (0);
1364 /* Restore the original signal mask. */
1365 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1366 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1367 suspend_mask
= normal_mask
;
1368 sigdelset (&suspend_mask
, SIGCHLD
);
1371 /* Set SIGCHLD to the default action, until after execing the child,
1372 since the inferior inherits the superior's signal mask. It will
1373 be blocked again in linux_nat_wait, which is only reached after
1374 the inferior execing. */
1375 linux_nat_async_events (sigchld_default
);
1377 #ifdef HAVE_PERSONALITY
1378 if (disable_randomization
)
1381 personality_orig
= personality (0xffffffff);
1382 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1384 personality_set
= 1;
1385 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1387 if (errno
!= 0 || (personality_set
1388 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1389 warning (_("Error disabling address space randomization: %s"),
1390 safe_strerror (errno
));
1392 #endif /* HAVE_PERSONALITY */
1394 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1396 #ifdef HAVE_PERSONALITY
1397 if (personality_set
)
1400 personality (personality_orig
);
1402 warning (_("Error restoring address space randomization: %s"),
1403 safe_strerror (errno
));
1405 #endif /* HAVE_PERSONALITY */
1408 linux_nat_async_mask (saved_async
);
1412 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1414 struct lwp_info
*lp
;
1418 /* FIXME: We should probably accept a list of process id's, and
1419 attach all of them. */
1420 linux_ops
->to_attach (ops
, args
, from_tty
);
1422 if (!target_can_async_p ())
1424 /* Restore the original signal mask. */
1425 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1426 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1427 suspend_mask
= normal_mask
;
1428 sigdelset (&suspend_mask
, SIGCHLD
);
1431 /* The ptrace base target adds the main thread with (pid,0,0)
1432 format. Decorate it with lwp info. */
1433 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1434 thread_change_ptid (inferior_ptid
, ptid
);
1436 /* Add the initial process as the first LWP to the list. */
1437 lp
= add_lwp (ptid
);
1439 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1443 /* Save the wait status to report later. */
1445 if (debug_linux_nat
)
1446 fprintf_unfiltered (gdb_stdlog
,
1447 "LNA: waitpid %ld, saving status %s\n",
1448 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1450 if (!target_can_async_p ())
1451 lp
->status
= status
;
1454 /* We already waited for this LWP, so put the wait result on the
1455 pipe. The event loop will wake up and gets us to handling
1457 linux_nat_event_pipe_push (GET_PID (lp
->ptid
), status
,
1458 lp
->cloned
? __WCLONE
: 0);
1459 /* Register in the event loop. */
1460 target_async (inferior_event_handler
, 0);
1464 /* Get pending status of LP. */
1466 get_pending_status (struct lwp_info
*lp
, int *status
)
1468 struct target_waitstatus last
;
1471 get_last_target_status (&last_ptid
, &last
);
1473 /* If this lwp is the ptid that GDB is processing an event from, the
1474 signal will be in stop_signal. Otherwise, in all-stop + sync
1475 mode, we may cache pending events in lp->status while trying to
1476 stop all threads (see stop_wait_callback). In async mode, the
1477 events are always cached in waitpid_queue. */
1483 enum target_signal signo
= TARGET_SIGNAL_0
;
1485 if (is_executing (lp
->ptid
))
1487 /* If the core thought this lwp was executing --- e.g., the
1488 executing property hasn't been updated yet, but the
1489 thread has been stopped with a stop_callback /
1490 stop_wait_callback sequence (see linux_nat_detach for
1491 example) --- we can only have pending events in the local
1493 if (queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
) != -1)
1495 if (WIFSTOPPED (*status
))
1496 signo
= target_signal_from_host (WSTOPSIG (*status
));
1498 /* If not stopped, then the lwp is gone, no use in
1499 resending a signal. */
1504 /* If the core knows the thread is not executing, then we
1505 have the last signal recorded in
1506 thread_info->stop_signal. */
1508 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1509 signo
= tp
->stop_signal
;
1512 if (signo
!= TARGET_SIGNAL_0
1513 && !signal_pass_state (signo
))
1515 if (debug_linux_nat
)
1516 fprintf_unfiltered (gdb_stdlog
, "\
1517 GPT: lwp %s had signal %s, but it is in no pass state\n",
1518 target_pid_to_str (lp
->ptid
),
1519 target_signal_to_string (signo
));
1523 if (signo
!= TARGET_SIGNAL_0
)
1524 *status
= W_STOPCODE (target_signal_to_host (signo
));
1526 if (debug_linux_nat
)
1527 fprintf_unfiltered (gdb_stdlog
,
1528 "GPT: lwp %s as pending signal %s\n",
1529 target_pid_to_str (lp
->ptid
),
1530 target_signal_to_string (signo
));
1535 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1537 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1538 if (tp
->stop_signal
!= TARGET_SIGNAL_0
1539 && signal_pass_state (tp
->stop_signal
))
1540 *status
= W_STOPCODE (target_signal_to_host (tp
->stop_signal
));
1542 else if (target_can_async_p ())
1543 queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
);
1545 *status
= lp
->status
;
1552 detach_callback (struct lwp_info
*lp
, void *data
)
1554 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1556 if (debug_linux_nat
&& lp
->status
)
1557 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1558 strsignal (WSTOPSIG (lp
->status
)),
1559 target_pid_to_str (lp
->ptid
));
1561 /* If there is a pending SIGSTOP, get rid of it. */
1564 if (debug_linux_nat
)
1565 fprintf_unfiltered (gdb_stdlog
,
1566 "DC: Sending SIGCONT to %s\n",
1567 target_pid_to_str (lp
->ptid
));
1569 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1573 /* We don't actually detach from the LWP that has an id equal to the
1574 overall process id just yet. */
1575 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1579 /* Pass on any pending signal for this LWP. */
1580 get_pending_status (lp
, &status
);
1583 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1584 WSTOPSIG (status
)) < 0)
1585 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1586 safe_strerror (errno
));
1588 if (debug_linux_nat
)
1589 fprintf_unfiltered (gdb_stdlog
,
1590 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1591 target_pid_to_str (lp
->ptid
),
1592 strsignal (WSTOPSIG (lp
->status
)));
1594 delete_lwp (lp
->ptid
);
1601 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1605 enum target_signal sig
;
1607 if (target_can_async_p ())
1608 linux_nat_async (NULL
, 0);
1610 /* Stop all threads before detaching. ptrace requires that the
1611 thread is stopped to sucessfully detach. */
1612 iterate_over_lwps (stop_callback
, NULL
);
1613 /* ... and wait until all of them have reported back that
1614 they're no longer running. */
1615 iterate_over_lwps (stop_wait_callback
, NULL
);
1617 iterate_over_lwps (detach_callback
, NULL
);
1619 /* Only the initial process should be left right now. */
1620 gdb_assert (num_lwps
== 1);
1622 /* Pass on any pending signal for the last LWP. */
1623 if ((args
== NULL
|| *args
== '\0')
1624 && get_pending_status (lwp_list
, &status
) != -1
1625 && WIFSTOPPED (status
))
1627 /* Put the signal number in ARGS so that inf_ptrace_detach will
1628 pass it along with PTRACE_DETACH. */
1630 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1631 fprintf_unfiltered (gdb_stdlog
,
1632 "LND: Sending signal %s to %s\n",
1634 target_pid_to_str (lwp_list
->ptid
));
1637 /* Destroy LWP info; it's no longer valid. */
1640 pid
= ptid_get_pid (inferior_ptid
);
1642 if (target_can_async_p ())
1643 drain_queued_events (pid
);
1645 if (forks_exist_p ())
1647 /* Multi-fork case. The current inferior_ptid is being detached
1648 from, but there are other viable forks to debug. Detach from
1649 the current fork, and context-switch to the first
1651 linux_fork_detach (args
, from_tty
);
1653 if (non_stop
&& target_can_async_p ())
1654 target_async (inferior_event_handler
, 0);
1657 linux_ops
->to_detach (ops
, args
, from_tty
);
1663 resume_callback (struct lwp_info
*lp
, void *data
)
1665 if (lp
->stopped
&& lp
->status
== 0)
1667 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1668 0, TARGET_SIGNAL_0
);
1669 if (debug_linux_nat
)
1670 fprintf_unfiltered (gdb_stdlog
,
1671 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1672 target_pid_to_str (lp
->ptid
));
1675 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1677 else if (lp
->stopped
&& debug_linux_nat
)
1678 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1679 target_pid_to_str (lp
->ptid
));
1680 else if (debug_linux_nat
)
1681 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1682 target_pid_to_str (lp
->ptid
));
1688 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1695 resume_set_callback (struct lwp_info
*lp
, void *data
)
1702 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1704 struct lwp_info
*lp
;
1707 if (debug_linux_nat
)
1708 fprintf_unfiltered (gdb_stdlog
,
1709 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1710 step
? "step" : "resume",
1711 target_pid_to_str (ptid
),
1712 signo
? strsignal (signo
) : "0",
1713 target_pid_to_str (inferior_ptid
));
1715 if (target_can_async_p ())
1716 /* Block events while we're here. */
1717 linux_nat_async_events (sigchld_sync
);
1719 /* A specific PTID means `step only this process id'. */
1720 resume_all
= (PIDGET (ptid
) == -1);
1722 if (non_stop
&& resume_all
)
1723 internal_error (__FILE__
, __LINE__
,
1724 "can't resume all in non-stop mode");
1729 iterate_over_lwps (resume_set_callback
, NULL
);
1731 iterate_over_lwps (resume_clear_callback
, NULL
);
1734 /* If PID is -1, it's the current inferior that should be
1735 handled specially. */
1736 if (PIDGET (ptid
) == -1)
1737 ptid
= inferior_ptid
;
1739 lp
= find_lwp_pid (ptid
);
1740 gdb_assert (lp
!= NULL
);
1742 /* Convert to something the lower layer understands. */
1743 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1745 /* Remember if we're stepping. */
1748 /* Mark this LWP as resumed. */
1751 /* If we have a pending wait status for this thread, there is no
1752 point in resuming the process. But first make sure that
1753 linux_nat_wait won't preemptively handle the event - we
1754 should never take this short-circuit if we are going to
1755 leave LP running, since we have skipped resuming all the
1756 other threads. This bit of code needs to be synchronized
1757 with linux_nat_wait. */
1759 /* In async mode, we never have pending wait status. */
1760 if (target_can_async_p () && lp
->status
)
1761 internal_error (__FILE__
, __LINE__
, "Pending status in async mode");
1763 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1766 struct inferior
*inf
;
1768 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1770 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1772 /* Defer to common code if we're gaining control of the
1774 if (inf
->stop_soon
== NO_STOP_QUIETLY
1775 && signal_stop_state (saved_signo
) == 0
1776 && signal_print_state (saved_signo
) == 0
1777 && signal_pass_state (saved_signo
) == 1)
1779 if (debug_linux_nat
)
1780 fprintf_unfiltered (gdb_stdlog
,
1781 "LLR: Not short circuiting for ignored "
1782 "status 0x%x\n", lp
->status
);
1784 /* FIXME: What should we do if we are supposed to continue
1785 this thread with a signal? */
1786 gdb_assert (signo
== TARGET_SIGNAL_0
);
1787 signo
= saved_signo
;
1794 /* FIXME: What should we do if we are supposed to continue
1795 this thread with a signal? */
1796 gdb_assert (signo
== TARGET_SIGNAL_0
);
1798 if (debug_linux_nat
)
1799 fprintf_unfiltered (gdb_stdlog
,
1800 "LLR: Short circuiting for status 0x%x\n",
1806 /* Mark LWP as not stopped to prevent it from being continued by
1811 iterate_over_lwps (resume_callback
, NULL
);
1813 linux_ops
->to_resume (ptid
, step
, signo
);
1814 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1816 if (debug_linux_nat
)
1817 fprintf_unfiltered (gdb_stdlog
,
1818 "LLR: %s %s, %s (resume event thread)\n",
1819 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1820 target_pid_to_str (ptid
),
1821 signo
? strsignal (signo
) : "0");
1823 if (target_can_async_p ())
1824 target_async (inferior_event_handler
, 0);
1827 /* Issue kill to specified lwp. */
1829 static int tkill_failed
;
1832 kill_lwp (int lwpid
, int signo
)
1836 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1837 fails, then we are not using nptl threads and we should be using kill. */
1839 #ifdef HAVE_TKILL_SYSCALL
1842 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1843 if (errno
!= ENOSYS
)
1850 return kill (lwpid
, signo
);
1853 /* Handle a GNU/Linux extended wait response. If we see a clone
1854 event, we need to add the new LWP to our list (and not report the
1855 trap to higher layers). This function returns non-zero if the
1856 event should be ignored and we should wait again. If STOPPING is
1857 true, the new LWP remains stopped, otherwise it is continued. */
1860 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1863 int pid
= GET_LWP (lp
->ptid
);
1864 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1865 struct lwp_info
*new_lp
= NULL
;
1866 int event
= status
>> 16;
1868 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1869 || event
== PTRACE_EVENT_CLONE
)
1871 unsigned long new_pid
;
1874 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1876 /* If we haven't already seen the new PID stop, wait for it now. */
1877 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1879 /* The new child has a pending SIGSTOP. We can't affect it until it
1880 hits the SIGSTOP, but we're already attached. */
1881 ret
= my_waitpid (new_pid
, &status
,
1882 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1884 perror_with_name (_("waiting for new child"));
1885 else if (ret
!= new_pid
)
1886 internal_error (__FILE__
, __LINE__
,
1887 _("wait returned unexpected PID %d"), ret
);
1888 else if (!WIFSTOPPED (status
))
1889 internal_error (__FILE__
, __LINE__
,
1890 _("wait returned unexpected status 0x%x"), status
);
1893 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1895 if (event
== PTRACE_EVENT_FORK
)
1896 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1897 else if (event
== PTRACE_EVENT_VFORK
)
1898 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1901 struct cleanup
*old_chain
;
1903 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1904 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (inferior_ptid
)));
1906 new_lp
->stopped
= 1;
1908 if (WSTOPSIG (status
) != SIGSTOP
)
1910 /* This can happen if someone starts sending signals to
1911 the new thread before it gets a chance to run, which
1912 have a lower number than SIGSTOP (e.g. SIGUSR1).
1913 This is an unlikely case, and harder to handle for
1914 fork / vfork than for clone, so we do not try - but
1915 we handle it for clone events here. We'll send
1916 the other signal on to the thread below. */
1918 new_lp
->signalled
= 1;
1925 /* Add the new thread to GDB's lists as soon as possible
1928 1) the frontend doesn't have to wait for a stop to
1931 2) we tag it with the correct running state. */
1933 /* If the thread_db layer is active, let it know about
1934 this new thread, and add it to GDB's list. */
1935 if (!thread_db_attach_lwp (new_lp
->ptid
))
1937 /* We're not using thread_db. Add it to GDB's
1939 target_post_attach (GET_LWP (new_lp
->ptid
));
1940 add_thread (new_lp
->ptid
);
1945 set_running (new_lp
->ptid
, 1);
1946 set_executing (new_lp
->ptid
, 1);
1952 new_lp
->stopped
= 0;
1953 new_lp
->resumed
= 1;
1954 ptrace (PTRACE_CONT
, new_pid
, 0,
1955 status
? WSTOPSIG (status
) : 0);
1958 if (debug_linux_nat
)
1959 fprintf_unfiltered (gdb_stdlog
,
1960 "LHEW: Got clone event from LWP %ld, resuming\n",
1961 GET_LWP (lp
->ptid
));
1962 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1970 if (event
== PTRACE_EVENT_EXEC
)
1972 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1973 ourstatus
->value
.execd_pathname
1974 = xstrdup (linux_child_pid_to_exec_file (pid
));
1976 if (linux_parent_pid
)
1978 detach_breakpoints (linux_parent_pid
);
1979 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1981 linux_parent_pid
= 0;
1984 /* At this point, all inserted breakpoints are gone. Doing this
1985 as soon as we detect an exec prevents the badness of deleting
1986 a breakpoint writing the current "shadow contents" to lift
1987 the bp. That shadow is NOT valid after an exec.
1989 Note that we have to do this after the detach_breakpoints
1990 call above, otherwise breakpoints wouldn't be lifted from the
1991 parent on a vfork, because detach_breakpoints would think
1992 that breakpoints are not inserted. */
1993 mark_breakpoints_out ();
1997 internal_error (__FILE__
, __LINE__
,
1998 _("unknown ptrace event %d"), event
);
2001 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2005 wait_lwp (struct lwp_info
*lp
)
2009 int thread_dead
= 0;
2011 gdb_assert (!lp
->stopped
);
2012 gdb_assert (lp
->status
== 0);
2014 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
2015 if (pid
== -1 && errno
== ECHILD
)
2017 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
2018 if (pid
== -1 && errno
== ECHILD
)
2020 /* The thread has previously exited. We need to delete it
2021 now because, for some vendor 2.4 kernels with NPTL
2022 support backported, there won't be an exit event unless
2023 it is the main thread. 2.6 kernels will report an exit
2024 event for each thread that exits, as expected. */
2026 if (debug_linux_nat
)
2027 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2028 target_pid_to_str (lp
->ptid
));
2034 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2036 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
,
2039 "WL: waitpid %s received %s\n",
2040 target_pid_to_str (lp
->ptid
),
2041 status_to_str (status
));
2045 /* Check if the thread has exited. */
2046 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2049 if (debug_linux_nat
)
2050 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2051 target_pid_to_str (lp
->ptid
));
2060 gdb_assert (WIFSTOPPED (status
));
2062 /* Handle GNU/Linux's extended waitstatus for trace events. */
2063 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2065 if (debug_linux_nat
)
2066 fprintf_unfiltered (gdb_stdlog
,
2067 "WL: Handling extended status 0x%06x\n",
2069 if (linux_handle_extended_wait (lp
, status
, 1))
2070 return wait_lwp (lp
);
2076 /* Save the most recent siginfo for LP. This is currently only called
2077 for SIGTRAP; some ports use the si_addr field for
2078 target_stopped_data_address. In the future, it may also be used to
2079 restore the siginfo of requeued signals. */
2082 save_siginfo (struct lwp_info
*lp
)
2085 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2086 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2089 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2092 /* Send a SIGSTOP to LP. */
2095 stop_callback (struct lwp_info
*lp
, void *data
)
2097 if (!lp
->stopped
&& !lp
->signalled
)
2101 if (debug_linux_nat
)
2103 fprintf_unfiltered (gdb_stdlog
,
2104 "SC: kill %s **<SIGSTOP>**\n",
2105 target_pid_to_str (lp
->ptid
));
2108 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2109 if (debug_linux_nat
)
2111 fprintf_unfiltered (gdb_stdlog
,
2112 "SC: lwp kill %d %s\n",
2114 errno
? safe_strerror (errno
) : "ERRNO-OK");
2118 gdb_assert (lp
->status
== 0);
2124 /* Return non-zero if LWP PID has a pending SIGINT. */
2127 linux_nat_has_pending_sigint (int pid
)
2129 sigset_t pending
, blocked
, ignored
;
2132 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2134 if (sigismember (&pending
, SIGINT
)
2135 && !sigismember (&ignored
, SIGINT
))
2141 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2144 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2146 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2147 flag to consume the next one. */
2148 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2149 && WSTOPSIG (lp
->status
) == SIGINT
)
2152 lp
->ignore_sigint
= 1;
2157 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2158 This function is called after we know the LWP has stopped; if the LWP
2159 stopped before the expected SIGINT was delivered, then it will never have
2160 arrived. Also, if the signal was delivered to a shared queue and consumed
2161 by a different thread, it will never be delivered to this LWP. */
2164 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2166 if (!lp
->ignore_sigint
)
2169 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2171 if (debug_linux_nat
)
2172 fprintf_unfiltered (gdb_stdlog
,
2173 "MCIS: Clearing bogus flag for %s\n",
2174 target_pid_to_str (lp
->ptid
));
2175 lp
->ignore_sigint
= 0;
2179 /* Wait until LP is stopped. */
2182 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2188 status
= wait_lwp (lp
);
2192 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2193 && WSTOPSIG (status
) == SIGINT
)
2195 lp
->ignore_sigint
= 0;
2198 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2199 if (debug_linux_nat
)
2200 fprintf_unfiltered (gdb_stdlog
,
2201 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2202 target_pid_to_str (lp
->ptid
),
2203 errno
? safe_strerror (errno
) : "OK");
2205 return stop_wait_callback (lp
, NULL
);
2208 maybe_clear_ignore_sigint (lp
);
2210 if (WSTOPSIG (status
) != SIGSTOP
)
2212 if (WSTOPSIG (status
) == SIGTRAP
)
2214 /* If a LWP other than the LWP that we're reporting an
2215 event for has hit a GDB breakpoint (as opposed to
2216 some random trap signal), then just arrange for it to
2217 hit it again later. We don't keep the SIGTRAP status
2218 and don't forward the SIGTRAP signal to the LWP. We
2219 will handle the current event, eventually we will
2220 resume all LWPs, and this one will get its breakpoint
2223 If we do not do this, then we run the risk that the
2224 user will delete or disable the breakpoint, but the
2225 thread will have already tripped on it. */
2227 /* Save the trap's siginfo in case we need it later. */
2230 /* Now resume this LWP and get the SIGSTOP event. */
2232 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2233 if (debug_linux_nat
)
2235 fprintf_unfiltered (gdb_stdlog
,
2236 "PTRACE_CONT %s, 0, 0 (%s)\n",
2237 target_pid_to_str (lp
->ptid
),
2238 errno
? safe_strerror (errno
) : "OK");
2240 fprintf_unfiltered (gdb_stdlog
,
2241 "SWC: Candidate SIGTRAP event in %s\n",
2242 target_pid_to_str (lp
->ptid
));
2244 /* Hold this event/waitstatus while we check to see if
2245 there are any more (we still want to get that SIGSTOP). */
2246 stop_wait_callback (lp
, NULL
);
2248 if (target_can_async_p ())
2250 /* Don't leave a pending wait status in async mode.
2251 Retrigger the breakpoint. */
2252 if (!cancel_breakpoint (lp
))
2254 /* There was no gdb breakpoint set at pc. Put
2255 the event back in the queue. */
2256 if (debug_linux_nat
)
2257 fprintf_unfiltered (gdb_stdlog
, "\
2258 SWC: leaving SIGTRAP in local queue of %s\n", target_pid_to_str (lp
->ptid
));
2259 push_waitpid (GET_LWP (lp
->ptid
),
2260 W_STOPCODE (SIGTRAP
),
2261 lp
->cloned
? __WCLONE
: 0);
2266 /* Hold the SIGTRAP for handling by
2268 /* If there's another event, throw it back into the
2272 if (debug_linux_nat
)
2273 fprintf_unfiltered (gdb_stdlog
,
2274 "SWC: kill %s, %s\n",
2275 target_pid_to_str (lp
->ptid
),
2276 status_to_str ((int) status
));
2277 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2279 /* Save the sigtrap event. */
2280 lp
->status
= status
;
2286 /* The thread was stopped with a signal other than
2287 SIGSTOP, and didn't accidentally trip a breakpoint. */
2289 if (debug_linux_nat
)
2291 fprintf_unfiltered (gdb_stdlog
,
2292 "SWC: Pending event %s in %s\n",
2293 status_to_str ((int) status
),
2294 target_pid_to_str (lp
->ptid
));
2296 /* Now resume this LWP and get the SIGSTOP event. */
2298 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2299 if (debug_linux_nat
)
2300 fprintf_unfiltered (gdb_stdlog
,
2301 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2302 target_pid_to_str (lp
->ptid
),
2303 errno
? safe_strerror (errno
) : "OK");
2305 /* Hold this event/waitstatus while we check to see if
2306 there are any more (we still want to get that SIGSTOP). */
2307 stop_wait_callback (lp
, NULL
);
2309 /* If the lp->status field is still empty, use it to
2310 hold this event. If not, then this event must be
2311 returned to the event queue of the LWP. */
2312 if (lp
->status
|| target_can_async_p ())
2314 if (debug_linux_nat
)
2316 fprintf_unfiltered (gdb_stdlog
,
2317 "SWC: kill %s, %s\n",
2318 target_pid_to_str (lp
->ptid
),
2319 status_to_str ((int) status
));
2321 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2324 lp
->status
= status
;
2330 /* We caught the SIGSTOP that we intended to catch, so
2331 there's no SIGSTOP pending. */
2340 /* Return non-zero if LP has a wait status pending. */
2343 status_callback (struct lwp_info
*lp
, void *data
)
2345 /* Only report a pending wait status if we pretend that this has
2346 indeed been resumed. */
2347 return (lp
->status
!= 0 && lp
->resumed
);
2350 /* Return non-zero if LP isn't stopped. */
2353 running_callback (struct lwp_info
*lp
, void *data
)
2355 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2358 /* Count the LWP's that have had events. */
2361 count_events_callback (struct lwp_info
*lp
, void *data
)
2365 gdb_assert (count
!= NULL
);
2367 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2368 if (lp
->status
!= 0 && lp
->resumed
2369 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2375 /* Select the LWP (if any) that is currently being single-stepped. */
2378 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2380 if (lp
->step
&& lp
->status
!= 0)
2386 /* Select the Nth LWP that has had a SIGTRAP event. */
2389 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2391 int *selector
= data
;
2393 gdb_assert (selector
!= NULL
);
2395 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2396 if (lp
->status
!= 0 && lp
->resumed
2397 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2398 if ((*selector
)-- == 0)
2405 cancel_breakpoint (struct lwp_info
*lp
)
2407 /* Arrange for a breakpoint to be hit again later. We don't keep
2408 the SIGTRAP status and don't forward the SIGTRAP signal to the
2409 LWP. We will handle the current event, eventually we will resume
2410 this LWP, and this breakpoint will trap again.
2412 If we do not do this, then we run the risk that the user will
2413 delete or disable the breakpoint, but the LWP will have already
2416 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2417 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2420 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2421 if (breakpoint_inserted_here_p (pc
))
2423 if (debug_linux_nat
)
2424 fprintf_unfiltered (gdb_stdlog
,
2425 "CB: Push back breakpoint for %s\n",
2426 target_pid_to_str (lp
->ptid
));
2428 /* Back up the PC if necessary. */
2429 if (gdbarch_decr_pc_after_break (gdbarch
))
2430 regcache_write_pc (regcache
, pc
);
2438 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2440 struct lwp_info
*event_lp
= data
;
2442 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2446 /* If a LWP other than the LWP that we're reporting an event for has
2447 hit a GDB breakpoint (as opposed to some random trap signal),
2448 then just arrange for it to hit it again later. We don't keep
2449 the SIGTRAP status and don't forward the SIGTRAP signal to the
2450 LWP. We will handle the current event, eventually we will resume
2451 all LWPs, and this one will get its breakpoint trap again.
2453 If we do not do this, then we run the risk that the user will
2454 delete or disable the breakpoint, but the LWP will have already
2458 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2459 && cancel_breakpoint (lp
))
2460 /* Throw away the SIGTRAP. */
2466 /* Select one LWP out of those that have events pending. */
2469 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
2472 int random_selector
;
2473 struct lwp_info
*event_lp
;
2475 /* Record the wait status for the original LWP. */
2476 (*orig_lp
)->status
= *status
;
2478 /* Give preference to any LWP that is being single-stepped. */
2479 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
2480 if (event_lp
!= NULL
)
2482 if (debug_linux_nat
)
2483 fprintf_unfiltered (gdb_stdlog
,
2484 "SEL: Select single-step %s\n",
2485 target_pid_to_str (event_lp
->ptid
));
2489 /* No single-stepping LWP. Select one at random, out of those
2490 which have had SIGTRAP events. */
2492 /* First see how many SIGTRAP events we have. */
2493 iterate_over_lwps (count_events_callback
, &num_events
);
2495 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2496 random_selector
= (int)
2497 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2499 if (debug_linux_nat
&& num_events
> 1)
2500 fprintf_unfiltered (gdb_stdlog
,
2501 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2502 num_events
, random_selector
);
2504 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
2508 if (event_lp
!= NULL
)
2510 /* Switch the event LWP. */
2511 *orig_lp
= event_lp
;
2512 *status
= event_lp
->status
;
2515 /* Flush the wait status for the event LWP. */
2516 (*orig_lp
)->status
= 0;
2519 /* Return non-zero if LP has been resumed. */
2522 resumed_callback (struct lwp_info
*lp
, void *data
)
2527 /* Stop an active thread, verify it still exists, then resume it. */
2530 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2532 struct lwp_info
*ptr
;
2534 if (!lp
->stopped
&& !lp
->signalled
)
2536 stop_callback (lp
, NULL
);
2537 stop_wait_callback (lp
, NULL
);
2538 /* Resume if the lwp still exists. */
2539 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2542 resume_callback (lp
, NULL
);
2543 resume_set_callback (lp
, NULL
);
2549 /* Check if we should go on and pass this event to common code.
2550 Return the affected lwp if we are, or NULL otherwise. */
2551 static struct lwp_info
*
2552 linux_nat_filter_event (int lwpid
, int status
, int options
)
2554 struct lwp_info
*lp
;
2556 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2558 /* Check for stop events reported by a process we didn't already
2559 know about - anything not already in our LWP list.
2561 If we're expecting to receive stopped processes after
2562 fork, vfork, and clone events, then we'll just add the
2563 new one to our list and go back to waiting for the event
2564 to be reported - the stopped process might be returned
2565 from waitpid before or after the event is. */
2566 if (WIFSTOPPED (status
) && !lp
)
2568 linux_record_stopped_pid (lwpid
, status
);
2572 /* Make sure we don't report an event for the exit of an LWP not in
2573 our list, i.e. not part of the current process. This can happen
2574 if we detach from a program we original forked and then it
2576 if (!WIFSTOPPED (status
) && !lp
)
2579 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2580 CLONE_PTRACE processes which do not use the thread library -
2581 otherwise we wouldn't find the new LWP this way. That doesn't
2582 currently work, and the following code is currently unreachable
2583 due to the two blocks above. If it's fixed some day, this code
2584 should be broken out into a function so that we can also pick up
2585 LWPs from the new interface. */
2588 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2589 if (options
& __WCLONE
)
2592 gdb_assert (WIFSTOPPED (status
)
2593 && WSTOPSIG (status
) == SIGSTOP
);
2596 if (!in_thread_list (inferior_ptid
))
2598 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2599 GET_PID (inferior_ptid
));
2600 add_thread (inferior_ptid
);
2603 add_thread (lp
->ptid
);
2606 /* Save the trap's siginfo in case we need it later. */
2607 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2610 /* Handle GNU/Linux's extended waitstatus for trace events. */
2611 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2613 if (debug_linux_nat
)
2614 fprintf_unfiltered (gdb_stdlog
,
2615 "LLW: Handling extended status 0x%06x\n",
2617 if (linux_handle_extended_wait (lp
, status
, 0))
2621 /* Check if the thread has exited. */
2622 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2624 /* If this is the main thread, we must stop all threads and
2625 verify if they are still alive. This is because in the nptl
2626 thread model, there is no signal issued for exiting LWPs
2627 other than the main thread. We only get the main thread exit
2628 signal once all child threads have already exited. If we
2629 stop all the threads and use the stop_wait_callback to check
2630 if they have exited we can determine whether this signal
2631 should be ignored or whether it means the end of the debugged
2632 application, regardless of which threading model is being
2634 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2637 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2640 if (debug_linux_nat
)
2641 fprintf_unfiltered (gdb_stdlog
,
2642 "LLW: %s exited.\n",
2643 target_pid_to_str (lp
->ptid
));
2647 /* If there is at least one more LWP, then the exit signal was
2648 not the end of the debugged application and should be
2654 /* Check if the current LWP has previously exited. In the nptl
2655 thread model, LWPs other than the main thread do not issue
2656 signals when they exit so we must check whenever the thread has
2657 stopped. A similar check is made in stop_wait_callback(). */
2658 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2660 if (debug_linux_nat
)
2661 fprintf_unfiltered (gdb_stdlog
,
2662 "LLW: %s exited.\n",
2663 target_pid_to_str (lp
->ptid
));
2667 /* Make sure there is at least one thread running. */
2668 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2670 /* Discard the event. */
2674 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2675 an attempt to stop an LWP. */
2677 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2679 if (debug_linux_nat
)
2680 fprintf_unfiltered (gdb_stdlog
,
2681 "LLW: Delayed SIGSTOP caught for %s.\n",
2682 target_pid_to_str (lp
->ptid
));
2684 /* This is a delayed SIGSTOP. */
2687 registers_changed ();
2689 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2690 lp
->step
, TARGET_SIGNAL_0
);
2691 if (debug_linux_nat
)
2692 fprintf_unfiltered (gdb_stdlog
,
2693 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2695 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2696 target_pid_to_str (lp
->ptid
));
2699 gdb_assert (lp
->resumed
);
2701 /* Discard the event. */
2705 /* Make sure we don't report a SIGINT that we have already displayed
2706 for another thread. */
2707 if (lp
->ignore_sigint
2708 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2710 if (debug_linux_nat
)
2711 fprintf_unfiltered (gdb_stdlog
,
2712 "LLW: Delayed SIGINT caught for %s.\n",
2713 target_pid_to_str (lp
->ptid
));
2715 /* This is a delayed SIGINT. */
2716 lp
->ignore_sigint
= 0;
2718 registers_changed ();
2719 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2720 lp
->step
, TARGET_SIGNAL_0
);
2721 if (debug_linux_nat
)
2722 fprintf_unfiltered (gdb_stdlog
,
2723 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2725 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2726 target_pid_to_str (lp
->ptid
));
2729 gdb_assert (lp
->resumed
);
2731 /* Discard the event. */
2735 /* An interesting event. */
2740 /* Get the events stored in the pipe into the local queue, so they are
2741 accessible to queued_waitpid. We need to do this, since it is not
2742 always the case that the event at the head of the pipe is the event
2746 pipe_to_local_event_queue (void)
2748 if (debug_linux_nat_async
)
2749 fprintf_unfiltered (gdb_stdlog
,
2750 "PTLEQ: linux_nat_num_queued_events(%d)\n",
2751 linux_nat_num_queued_events
);
2752 while (linux_nat_num_queued_events
)
2754 int lwpid
, status
, options
;
2755 lwpid
= linux_nat_event_pipe_pop (&status
, &options
);
2756 gdb_assert (lwpid
> 0);
2757 push_waitpid (lwpid
, status
, options
);
2761 /* Get the unprocessed events stored in the local queue back into the
2762 pipe, so the event loop realizes there's something else to
2766 local_event_queue_to_pipe (void)
2768 struct waitpid_result
*w
= waitpid_queue
;
2771 struct waitpid_result
*next
= w
->next
;
2772 linux_nat_event_pipe_push (w
->pid
,
2778 waitpid_queue
= NULL
;
2780 if (debug_linux_nat_async
)
2781 fprintf_unfiltered (gdb_stdlog
,
2782 "LEQTP: linux_nat_num_queued_events(%d)\n",
2783 linux_nat_num_queued_events
);
2787 linux_nat_wait (struct target_ops
*ops
,
2788 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2790 struct lwp_info
*lp
= NULL
;
2793 pid_t pid
= PIDGET (ptid
);
2795 if (debug_linux_nat_async
)
2796 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2798 /* The first time we get here after starting a new inferior, we may
2799 not have added it to the LWP list yet - this is the earliest
2800 moment at which we know its PID. */
2803 gdb_assert (!is_lwp (inferior_ptid
));
2805 /* Upgrade the main thread's ptid. */
2806 thread_change_ptid (inferior_ptid
,
2807 BUILD_LWP (GET_PID (inferior_ptid
),
2808 GET_PID (inferior_ptid
)));
2810 lp
= add_lwp (inferior_ptid
);
2814 /* Block events while we're here. */
2815 linux_nat_async_events (sigchld_sync
);
2819 /* Make sure there is at least one LWP that has been resumed. */
2820 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
2822 /* First check if there is a LWP with a wait status pending. */
2825 /* Any LWP that's been resumed will do. */
2826 lp
= iterate_over_lwps (status_callback
, NULL
);
2829 if (target_can_async_p ())
2830 internal_error (__FILE__
, __LINE__
,
2831 "Found an LWP with a pending status in async mode.");
2833 status
= lp
->status
;
2836 if (debug_linux_nat
&& status
)
2837 fprintf_unfiltered (gdb_stdlog
,
2838 "LLW: Using pending wait status %s for %s.\n",
2839 status_to_str (status
),
2840 target_pid_to_str (lp
->ptid
));
2843 /* But if we don't find one, we'll have to wait, and check both
2844 cloned and uncloned processes. We start with the cloned
2846 options
= __WCLONE
| WNOHANG
;
2848 else if (is_lwp (ptid
))
2850 if (debug_linux_nat
)
2851 fprintf_unfiltered (gdb_stdlog
,
2852 "LLW: Waiting for specific LWP %s.\n",
2853 target_pid_to_str (ptid
));
2855 /* We have a specific LWP to check. */
2856 lp
= find_lwp_pid (ptid
);
2858 status
= lp
->status
;
2861 if (debug_linux_nat
&& status
)
2862 fprintf_unfiltered (gdb_stdlog
,
2863 "LLW: Using pending wait status %s for %s.\n",
2864 status_to_str (status
),
2865 target_pid_to_str (lp
->ptid
));
2867 /* If we have to wait, take into account whether PID is a cloned
2868 process or not. And we have to convert it to something that
2869 the layer beneath us can understand. */
2870 options
= lp
->cloned
? __WCLONE
: 0;
2871 pid
= GET_LWP (ptid
);
2874 if (status
&& lp
->signalled
)
2876 /* A pending SIGSTOP may interfere with the normal stream of
2877 events. In a typical case where interference is a problem,
2878 we have a SIGSTOP signal pending for LWP A while
2879 single-stepping it, encounter an event in LWP B, and take the
2880 pending SIGSTOP while trying to stop LWP A. After processing
2881 the event in LWP B, LWP A is continued, and we'll never see
2882 the SIGTRAP associated with the last time we were
2883 single-stepping LWP A. */
2885 /* Resume the thread. It should halt immediately returning the
2887 registers_changed ();
2888 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2889 lp
->step
, TARGET_SIGNAL_0
);
2890 if (debug_linux_nat
)
2891 fprintf_unfiltered (gdb_stdlog
,
2892 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2893 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2894 target_pid_to_str (lp
->ptid
));
2896 gdb_assert (lp
->resumed
);
2898 /* This should catch the pending SIGSTOP. */
2899 stop_wait_callback (lp
, NULL
);
2902 if (!target_can_async_p ())
2904 /* Causes SIGINT to be passed on to the attached process. */
2912 if (target_can_async_p ())
2913 /* In async mode, don't ever block. Only look at the locally
2915 lwpid
= queued_waitpid (pid
, &status
, options
);
2917 lwpid
= my_waitpid (pid
, &status
, options
);
2921 gdb_assert (pid
== -1 || lwpid
== pid
);
2923 if (debug_linux_nat
)
2925 fprintf_unfiltered (gdb_stdlog
,
2926 "LLW: waitpid %ld received %s\n",
2927 (long) lwpid
, status_to_str (status
));
2930 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2933 /* A discarded event. */
2943 /* Alternate between checking cloned and uncloned processes. */
2944 options
^= __WCLONE
;
2946 /* And every time we have checked both:
2947 In async mode, return to event loop;
2948 In sync mode, suspend waiting for a SIGCHLD signal. */
2949 if (options
& __WCLONE
)
2951 if (target_can_async_p ())
2953 /* No interesting event. */
2954 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2956 /* Get ready for the next event. */
2957 target_async (inferior_event_handler
, 0);
2959 if (debug_linux_nat_async
)
2960 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2962 return minus_one_ptid
;
2965 sigsuspend (&suspend_mask
);
2969 /* We shouldn't end up here unless we want to try again. */
2970 gdb_assert (status
== 0);
2973 if (!target_can_async_p ())
2974 clear_sigint_trap ();
2978 /* Don't report signals that GDB isn't interested in, such as
2979 signals that are neither printed nor stopped upon. Stopping all
2980 threads can be a bit time-consuming so if we want decent
2981 performance with heavily multi-threaded programs, especially when
2982 they're using a high frequency timer, we'd better avoid it if we
2985 if (WIFSTOPPED (status
))
2987 int signo
= target_signal_from_host (WSTOPSIG (status
));
2988 struct inferior
*inf
;
2990 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2993 /* Defer to common code if we get a signal while
2994 single-stepping, since that may need special care, e.g. to
2995 skip the signal handler, or, if we're gaining control of the
2998 && inf
->stop_soon
== NO_STOP_QUIETLY
2999 && signal_stop_state (signo
) == 0
3000 && signal_print_state (signo
) == 0
3001 && signal_pass_state (signo
) == 1)
3003 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3004 here? It is not clear we should. GDB may not expect
3005 other threads to run. On the other hand, not resuming
3006 newly attached threads may cause an unwanted delay in
3007 getting them running. */
3008 registers_changed ();
3009 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
3011 if (debug_linux_nat
)
3012 fprintf_unfiltered (gdb_stdlog
,
3013 "LLW: %s %s, %s (preempt 'handle')\n",
3015 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3016 target_pid_to_str (lp
->ptid
),
3017 signo
? strsignal (signo
) : "0");
3025 /* Only do the below in all-stop, as we currently use SIGINT
3026 to implement target_stop (see linux_nat_stop) in
3028 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3030 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3031 forwarded to the entire process group, that is, all LWPs
3032 will receive it - unless they're using CLONE_THREAD to
3033 share signals. Since we only want to report it once, we
3034 mark it as ignored for all LWPs except this one. */
3035 iterate_over_lwps (set_ignore_sigint
, NULL
);
3036 lp
->ignore_sigint
= 0;
3039 maybe_clear_ignore_sigint (lp
);
3043 /* This LWP is stopped now. */
3046 if (debug_linux_nat
)
3047 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3048 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3052 /* Now stop all other LWP's ... */
3053 iterate_over_lwps (stop_callback
, NULL
);
3055 /* ... and wait until all of them have reported back that
3056 they're no longer running. */
3057 iterate_over_lwps (stop_wait_callback
, NULL
);
3059 /* If we're not waiting for a specific LWP, choose an event LWP
3060 from among those that have had events. Giving equal priority
3061 to all LWPs that have had events helps prevent
3064 select_event_lwp (&lp
, &status
);
3067 /* Now that we've selected our final event LWP, cancel any
3068 breakpoints in other LWPs that have hit a GDB breakpoint. See
3069 the comment in cancel_breakpoints_callback to find out why. */
3070 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
3072 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3074 if (debug_linux_nat
)
3075 fprintf_unfiltered (gdb_stdlog
,
3076 "LLW: trap ptid is %s.\n",
3077 target_pid_to_str (lp
->ptid
));
3080 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3082 *ourstatus
= lp
->waitstatus
;
3083 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3086 store_waitstatus (ourstatus
, status
);
3088 /* Get ready for the next event. */
3089 if (target_can_async_p ())
3090 target_async (inferior_event_handler
, 0);
3092 if (debug_linux_nat_async
)
3093 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3099 kill_callback (struct lwp_info
*lp
, void *data
)
3102 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3103 if (debug_linux_nat
)
3104 fprintf_unfiltered (gdb_stdlog
,
3105 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3106 target_pid_to_str (lp
->ptid
),
3107 errno
? safe_strerror (errno
) : "OK");
3113 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3117 /* We must make sure that there are no pending events (delayed
3118 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3119 program doesn't interfere with any following debugging session. */
3121 /* For cloned processes we must check both with __WCLONE and
3122 without, since the exit status of a cloned process isn't reported
3128 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3129 if (pid
!= (pid_t
) -1)
3131 if (debug_linux_nat
)
3132 fprintf_unfiltered (gdb_stdlog
,
3133 "KWC: wait %s received unknown.\n",
3134 target_pid_to_str (lp
->ptid
));
3135 /* The Linux kernel sometimes fails to kill a thread
3136 completely after PTRACE_KILL; that goes from the stop
3137 point in do_fork out to the one in
3138 get_signal_to_deliever and waits again. So kill it
3140 kill_callback (lp
, NULL
);
3143 while (pid
== GET_LWP (lp
->ptid
));
3145 gdb_assert (pid
== -1 && errno
== ECHILD
);
3150 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3151 if (pid
!= (pid_t
) -1)
3153 if (debug_linux_nat
)
3154 fprintf_unfiltered (gdb_stdlog
,
3155 "KWC: wait %s received unk.\n",
3156 target_pid_to_str (lp
->ptid
));
3157 /* See the call to kill_callback above. */
3158 kill_callback (lp
, NULL
);
3161 while (pid
== GET_LWP (lp
->ptid
));
3163 gdb_assert (pid
== -1 && errno
== ECHILD
);
3168 linux_nat_kill (void)
3170 struct target_waitstatus last
;
3174 if (target_can_async_p ())
3175 target_async (NULL
, 0);
3177 /* If we're stopped while forking and we haven't followed yet,
3178 kill the other task. We need to do this first because the
3179 parent will be sleeping if this is a vfork. */
3181 get_last_target_status (&last_ptid
, &last
);
3183 if (last
.kind
== TARGET_WAITKIND_FORKED
3184 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3186 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3190 if (forks_exist_p ())
3192 linux_fork_killall ();
3193 drain_queued_events (-1);
3197 /* Stop all threads before killing them, since ptrace requires
3198 that the thread is stopped to sucessfully PTRACE_KILL. */
3199 iterate_over_lwps (stop_callback
, NULL
);
3200 /* ... and wait until all of them have reported back that
3201 they're no longer running. */
3202 iterate_over_lwps (stop_wait_callback
, NULL
);
3204 /* Kill all LWP's ... */
3205 iterate_over_lwps (kill_callback
, NULL
);
3207 /* ... and wait until we've flushed all events. */
3208 iterate_over_lwps (kill_wait_callback
, NULL
);
3211 target_mourn_inferior ();
3215 linux_nat_mourn_inferior (struct target_ops
*ops
)
3217 /* Destroy LWP info; it's no longer valid. */
3220 if (! forks_exist_p ())
3222 /* Normal case, no other forks available. */
3223 if (target_can_async_p ())
3224 linux_nat_async (NULL
, 0);
3225 linux_ops
->to_mourn_inferior (ops
);
3228 /* Multi-fork case. The current inferior_ptid has exited, but
3229 there are other viable forks to debug. Delete the exiting
3230 one and context-switch to the first available. */
3231 linux_fork_mourn_inferior ();
3234 /* Convert a native/host siginfo object, into/from the siginfo in the
3235 layout of the inferiors' architecture. */
3238 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3242 if (linux_nat_siginfo_fixup
!= NULL
)
3243 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3245 /* If there was no callback, or the callback didn't do anything,
3246 then just do a straight memcpy. */
3250 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
3252 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
3257 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3258 const char *annex
, gdb_byte
*readbuf
,
3259 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3261 struct lwp_info
*lp
;
3264 struct siginfo siginfo
;
3265 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
3267 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3268 gdb_assert (readbuf
|| writebuf
);
3270 pid
= GET_LWP (inferior_ptid
);
3272 pid
= GET_PID (inferior_ptid
);
3274 if (offset
> sizeof (siginfo
))
3278 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3282 /* When GDB is built as a 64-bit application, ptrace writes into
3283 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3284 inferior with a 64-bit GDB should look the same as debugging it
3285 with a 32-bit GDB, we need to convert it. GDB core always sees
3286 the converted layout, so any read/write will have to be done
3288 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3290 if (offset
+ len
> sizeof (siginfo
))
3291 len
= sizeof (siginfo
) - offset
;
3293 if (readbuf
!= NULL
)
3294 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3297 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3299 /* Convert back to ptrace layout before flushing it out. */
3300 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3303 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3312 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3313 const char *annex
, gdb_byte
*readbuf
,
3314 const gdb_byte
*writebuf
,
3315 ULONGEST offset
, LONGEST len
)
3317 struct cleanup
*old_chain
;
3320 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3321 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3324 old_chain
= save_inferior_ptid ();
3326 if (is_lwp (inferior_ptid
))
3327 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3329 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3332 do_cleanups (old_chain
);
3337 linux_nat_thread_alive (ptid_t ptid
)
3341 gdb_assert (is_lwp (ptid
));
3343 /* Send signal 0 instead of anything ptrace, because ptracing a
3344 running thread errors out claiming that the thread doesn't
3346 err
= kill_lwp (GET_LWP (ptid
), 0);
3348 if (debug_linux_nat
)
3349 fprintf_unfiltered (gdb_stdlog
,
3350 "LLTA: KILL(SIG0) %s (%s)\n",
3351 target_pid_to_str (ptid
),
3352 err
? safe_strerror (err
) : "OK");
3361 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3363 static char buf
[64];
3366 && ((lwp_list
&& lwp_list
->next
)
3367 || GET_PID (ptid
) != GET_LWP (ptid
)))
3369 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3373 return normal_pid_to_str (ptid
);
3377 sigchld_handler (int signo
)
3379 if (target_async_permitted
3380 && linux_nat_async_events_state
!= sigchld_sync
3381 && signo
== SIGCHLD
)
3382 /* It is *always* a bug to hit this. */
3383 internal_error (__FILE__
, __LINE__
,
3384 "sigchld_handler called when async events are enabled");
3386 /* Do nothing. The only reason for this handler is that it allows
3387 us to use sigsuspend in linux_nat_wait above to wait for the
3388 arrival of a SIGCHLD. */
3391 /* Accepts an integer PID; Returns a string representing a file that
3392 can be opened to get the symbols for the child process. */
3395 linux_child_pid_to_exec_file (int pid
)
3397 char *name1
, *name2
;
3399 name1
= xmalloc (MAXPATHLEN
);
3400 name2
= xmalloc (MAXPATHLEN
);
3401 make_cleanup (xfree
, name1
);
3402 make_cleanup (xfree
, name2
);
3403 memset (name2
, 0, MAXPATHLEN
);
3405 sprintf (name1
, "/proc/%d/exe", pid
);
3406 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3412 /* Service function for corefiles and info proc. */
3415 read_mapping (FILE *mapfile
,
3420 char *device
, long long *inode
, char *filename
)
3422 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3423 addr
, endaddr
, permissions
, offset
, device
, inode
);
3426 if (ret
> 0 && ret
!= EOF
)
3428 /* Eat everything up to EOL for the filename. This will prevent
3429 weird filenames (such as one with embedded whitespace) from
3430 confusing this code. It also makes this code more robust in
3431 respect to annotations the kernel may add after the filename.
3433 Note the filename is used for informational purposes
3435 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3438 return (ret
!= 0 && ret
!= EOF
);
3441 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3442 regions in the inferior for a corefile. */
3445 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3447 int, int, int, void *), void *obfd
)
3449 long long pid
= PIDGET (inferior_ptid
);
3450 char mapsfilename
[MAXPATHLEN
];
3452 long long addr
, endaddr
, size
, offset
, inode
;
3453 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3454 int read
, write
, exec
;
3456 struct cleanup
*cleanup
;
3458 /* Compose the filename for the /proc memory map, and open it. */
3459 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
3460 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3461 error (_("Could not open %s."), mapsfilename
);
3462 cleanup
= make_cleanup_fclose (mapsfile
);
3465 fprintf_filtered (gdb_stdout
,
3466 "Reading memory regions from %s\n", mapsfilename
);
3468 /* Now iterate until end-of-file. */
3469 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3470 &offset
, &device
[0], &inode
, &filename
[0]))
3472 size
= endaddr
- addr
;
3474 /* Get the segment's permissions. */
3475 read
= (strchr (permissions
, 'r') != 0);
3476 write
= (strchr (permissions
, 'w') != 0);
3477 exec
= (strchr (permissions
, 'x') != 0);
3481 fprintf_filtered (gdb_stdout
,
3482 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3483 size
, paddr_nz (addr
),
3485 write
? 'w' : ' ', exec
? 'x' : ' ');
3487 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3488 fprintf_filtered (gdb_stdout
, "\n");
3491 /* Invoke the callback function to create the corefile
3493 func (addr
, size
, read
, write
, exec
, obfd
);
3495 do_cleanups (cleanup
);
3500 find_signalled_thread (struct thread_info
*info
, void *data
)
3502 if (info
->stop_signal
!= TARGET_SIGNAL_0
3503 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3509 static enum target_signal
3510 find_stop_signal (void)
3512 struct thread_info
*info
=
3513 iterate_over_threads (find_signalled_thread
, NULL
);
3516 return info
->stop_signal
;
3518 return TARGET_SIGNAL_0
;
3521 /* Records the thread's register state for the corefile note
3525 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3526 char *note_data
, int *note_size
,
3527 enum target_signal stop_signal
)
3529 gdb_gregset_t gregs
;
3530 gdb_fpregset_t fpregs
;
3531 unsigned long lwp
= ptid_get_lwp (ptid
);
3532 struct regcache
*regcache
= get_thread_regcache (ptid
);
3533 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3534 const struct regset
*regset
;
3536 struct cleanup
*old_chain
;
3537 struct core_regset_section
*sect_list
;
3540 old_chain
= save_inferior_ptid ();
3541 inferior_ptid
= ptid
;
3542 target_fetch_registers (regcache
, -1);
3543 do_cleanups (old_chain
);
3545 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3546 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3549 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3550 sizeof (gregs
))) != NULL
3551 && regset
->collect_regset
!= NULL
)
3552 regset
->collect_regset (regset
, regcache
, -1,
3553 &gregs
, sizeof (gregs
));
3555 fill_gregset (regcache
, &gregs
, -1);
3557 note_data
= (char *) elfcore_write_prstatus (obfd
,
3561 stop_signal
, &gregs
);
3563 /* The loop below uses the new struct core_regset_section, which stores
3564 the supported section names and sizes for the core file. Note that
3565 note PRSTATUS needs to be treated specially. But the other notes are
3566 structurally the same, so they can benefit from the new struct. */
3567 if (core_regset_p
&& sect_list
!= NULL
)
3568 while (sect_list
->sect_name
!= NULL
)
3570 /* .reg was already handled above. */
3571 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3576 regset
= gdbarch_regset_from_core_section (gdbarch
,
3577 sect_list
->sect_name
,
3579 gdb_assert (regset
&& regset
->collect_regset
);
3580 gdb_regset
= xmalloc (sect_list
->size
);
3581 regset
->collect_regset (regset
, regcache
, -1,
3582 gdb_regset
, sect_list
->size
);
3583 note_data
= (char *) elfcore_write_register_note (obfd
,
3586 sect_list
->sect_name
,
3593 /* For architectures that does not have the struct core_regset_section
3594 implemented, we use the old method. When all the architectures have
3595 the new support, the code below should be deleted. */
3599 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3600 sizeof (fpregs
))) != NULL
3601 && regset
->collect_regset
!= NULL
)
3602 regset
->collect_regset (regset
, regcache
, -1,
3603 &fpregs
, sizeof (fpregs
));
3605 fill_fpregset (regcache
, &fpregs
, -1);
3607 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3610 &fpregs
, sizeof (fpregs
));
3616 struct linux_nat_corefile_thread_data
3622 enum target_signal stop_signal
;
3625 /* Called by gdbthread.c once per thread. Records the thread's
3626 register state for the corefile note section. */
3629 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3631 struct linux_nat_corefile_thread_data
*args
= data
;
3633 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3643 /* Fills the "to_make_corefile_note" target vector. Builds the note
3644 section for a corefile, and returns it in a malloc buffer. */
3647 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3649 struct linux_nat_corefile_thread_data thread_args
;
3650 struct cleanup
*old_chain
;
3651 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3652 char fname
[16] = { '\0' };
3653 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3654 char psargs
[80] = { '\0' };
3655 char *note_data
= NULL
;
3656 ptid_t current_ptid
= inferior_ptid
;
3660 if (get_exec_file (0))
3662 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3663 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3664 if (get_inferior_args ())
3667 char *psargs_end
= psargs
+ sizeof (psargs
);
3669 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3671 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3672 if (string_end
!= NULL
)
3674 *string_end
++ = ' ';
3675 strncpy (string_end
, get_inferior_args (),
3676 psargs_end
- string_end
);
3679 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3681 note_size
, fname
, psargs
);
3684 /* Dump information for threads. */
3685 thread_args
.obfd
= obfd
;
3686 thread_args
.note_data
= note_data
;
3687 thread_args
.note_size
= note_size
;
3688 thread_args
.num_notes
= 0;
3689 thread_args
.stop_signal
= find_stop_signal ();
3690 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
3691 gdb_assert (thread_args
.num_notes
!= 0);
3692 note_data
= thread_args
.note_data
;
3694 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3698 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3699 "CORE", NT_AUXV
, auxv
, auxv_len
);
3703 make_cleanup (xfree
, note_data
);
3707 /* Implement the "info proc" command. */
3710 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3712 long long pid
= PIDGET (inferior_ptid
);
3715 char buffer
[MAXPATHLEN
];
3716 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3729 /* Break up 'args' into an argv array. */
3730 argv
= gdb_buildargv (args
);
3731 make_cleanup_freeargv (argv
);
3733 while (argv
!= NULL
&& *argv
!= NULL
)
3735 if (isdigit (argv
[0][0]))
3737 pid
= strtoul (argv
[0], NULL
, 10);
3739 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3743 else if (strcmp (argv
[0], "status") == 0)
3747 else if (strcmp (argv
[0], "stat") == 0)
3751 else if (strcmp (argv
[0], "cmd") == 0)
3755 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3759 else if (strcmp (argv
[0], "cwd") == 0)
3763 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3769 /* [...] (future options here) */
3774 error (_("No current process: you must name one."));
3776 sprintf (fname1
, "/proc/%lld", pid
);
3777 if (stat (fname1
, &dummy
) != 0)
3778 error (_("No /proc directory: '%s'"), fname1
);
3780 printf_filtered (_("process %lld\n"), pid
);
3781 if (cmdline_f
|| all
)
3783 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
3784 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3786 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3787 if (fgets (buffer
, sizeof (buffer
), procfile
))
3788 printf_filtered ("cmdline = '%s'\n", buffer
);
3790 warning (_("unable to read '%s'"), fname1
);
3791 do_cleanups (cleanup
);
3794 warning (_("unable to open /proc file '%s'"), fname1
);
3798 sprintf (fname1
, "/proc/%lld/cwd", pid
);
3799 memset (fname2
, 0, sizeof (fname2
));
3800 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3801 printf_filtered ("cwd = '%s'\n", fname2
);
3803 warning (_("unable to read link '%s'"), fname1
);
3807 sprintf (fname1
, "/proc/%lld/exe", pid
);
3808 memset (fname2
, 0, sizeof (fname2
));
3809 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3810 printf_filtered ("exe = '%s'\n", fname2
);
3812 warning (_("unable to read link '%s'"), fname1
);
3814 if (mappings_f
|| all
)
3816 sprintf (fname1
, "/proc/%lld/maps", pid
);
3817 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3819 long long addr
, endaddr
, size
, offset
, inode
;
3820 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3821 struct cleanup
*cleanup
;
3823 cleanup
= make_cleanup_fclose (procfile
);
3824 printf_filtered (_("Mapped address spaces:\n\n"));
3825 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3827 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3830 " Size", " Offset", "objfile");
3834 printf_filtered (" %18s %18s %10s %10s %7s\n",
3837 " Size", " Offset", "objfile");
3840 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3841 &offset
, &device
[0], &inode
, &filename
[0]))
3843 size
= endaddr
- addr
;
3845 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3846 calls here (and possibly above) should be abstracted
3847 out into their own functions? Andrew suggests using
3848 a generic local_address_string instead to print out
3849 the addresses; that makes sense to me, too. */
3851 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3853 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3854 (unsigned long) addr
, /* FIXME: pr_addr */
3855 (unsigned long) endaddr
,
3857 (unsigned int) offset
,
3858 filename
[0] ? filename
: "");
3862 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3863 (unsigned long) addr
, /* FIXME: pr_addr */
3864 (unsigned long) endaddr
,
3866 (unsigned int) offset
,
3867 filename
[0] ? filename
: "");
3871 do_cleanups (cleanup
);
3874 warning (_("unable to open /proc file '%s'"), fname1
);
3876 if (status_f
|| all
)
3878 sprintf (fname1
, "/proc/%lld/status", pid
);
3879 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3881 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3882 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3883 puts_filtered (buffer
);
3884 do_cleanups (cleanup
);
3887 warning (_("unable to open /proc file '%s'"), fname1
);
3891 sprintf (fname1
, "/proc/%lld/stat", pid
);
3892 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3897 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3899 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3900 printf_filtered (_("Process: %d\n"), itmp
);
3901 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3902 printf_filtered (_("Exec file: %s\n"), buffer
);
3903 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3904 printf_filtered (_("State: %c\n"), ctmp
);
3905 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3906 printf_filtered (_("Parent process: %d\n"), itmp
);
3907 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3908 printf_filtered (_("Process group: %d\n"), itmp
);
3909 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3910 printf_filtered (_("Session id: %d\n"), itmp
);
3911 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3912 printf_filtered (_("TTY: %d\n"), itmp
);
3913 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3914 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3915 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3916 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3917 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3918 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3919 (unsigned long) ltmp
);
3920 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3921 printf_filtered (_("Minor faults, children: %lu\n"),
3922 (unsigned long) ltmp
);
3923 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3924 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3925 (unsigned long) ltmp
);
3926 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3927 printf_filtered (_("Major faults, children: %lu\n"),
3928 (unsigned long) ltmp
);
3929 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3930 printf_filtered (_("utime: %ld\n"), ltmp
);
3931 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3932 printf_filtered (_("stime: %ld\n"), ltmp
);
3933 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3934 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3935 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3936 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3937 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3938 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3940 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3941 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3942 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3943 printf_filtered (_("jiffies until next timeout: %lu\n"),
3944 (unsigned long) ltmp
);
3945 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3946 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3947 (unsigned long) ltmp
);
3948 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3949 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3951 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3952 printf_filtered (_("Virtual memory size: %lu\n"),
3953 (unsigned long) ltmp
);
3954 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3955 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3956 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3957 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3958 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3959 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3960 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3961 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3962 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3963 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3964 #if 0 /* Don't know how architecture-dependent the rest is...
3965 Anyway the signal bitmap info is available from "status". */
3966 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3967 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3968 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3969 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3970 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3971 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3972 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3973 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3974 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3975 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
3976 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3977 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
3978 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3979 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
3981 do_cleanups (cleanup
);
3984 warning (_("unable to open /proc file '%s'"), fname1
);
3988 /* Implement the to_xfer_partial interface for memory reads using the /proc
3989 filesystem. Because we can use a single read() call for /proc, this
3990 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3991 but it doesn't support writes. */
3994 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3995 const char *annex
, gdb_byte
*readbuf
,
3996 const gdb_byte
*writebuf
,
3997 ULONGEST offset
, LONGEST len
)
4003 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4006 /* Don't bother for one word. */
4007 if (len
< 3 * sizeof (long))
4010 /* We could keep this file open and cache it - possibly one per
4011 thread. That requires some juggling, but is even faster. */
4012 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4013 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4017 /* If pread64 is available, use it. It's faster if the kernel
4018 supports it (only one syscall), and it's 64-bit safe even on
4019 32-bit platforms (for instance, SPARC debugging a SPARC64
4022 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4024 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4034 /* Parse LINE as a signal set and add its set bits to SIGS. */
4037 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4039 int len
= strlen (line
) - 1;
4043 if (line
[len
] != '\n')
4044 error (_("Could not parse signal set: %s"), line
);
4052 if (*p
>= '0' && *p
<= '9')
4054 else if (*p
>= 'a' && *p
<= 'f')
4055 digit
= *p
- 'a' + 10;
4057 error (_("Could not parse signal set: %s"), line
);
4062 sigaddset (sigs
, signum
+ 1);
4064 sigaddset (sigs
, signum
+ 2);
4066 sigaddset (sigs
, signum
+ 3);
4068 sigaddset (sigs
, signum
+ 4);
4074 /* Find process PID's pending signals from /proc/pid/status and set
4078 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
4081 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4083 struct cleanup
*cleanup
;
4085 sigemptyset (pending
);
4086 sigemptyset (blocked
);
4087 sigemptyset (ignored
);
4088 sprintf (fname
, "/proc/%d/status", pid
);
4089 procfile
= fopen (fname
, "r");
4090 if (procfile
== NULL
)
4091 error (_("Could not open %s"), fname
);
4092 cleanup
= make_cleanup_fclose (procfile
);
4094 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4096 /* Normal queued signals are on the SigPnd line in the status
4097 file. However, 2.6 kernels also have a "shared" pending
4098 queue for delivering signals to a thread group, so check for
4101 Unfortunately some Red Hat kernels include the shared pending
4102 queue but not the ShdPnd status field. */
4104 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4105 add_line_to_sigset (buffer
+ 8, pending
);
4106 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4107 add_line_to_sigset (buffer
+ 8, pending
);
4108 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4109 add_line_to_sigset (buffer
+ 8, blocked
);
4110 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4111 add_line_to_sigset (buffer
+ 8, ignored
);
4114 do_cleanups (cleanup
);
4118 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4119 const char *annex
, gdb_byte
*readbuf
,
4120 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4122 /* We make the process list snapshot when the object starts to be
4124 static const char *buf
;
4125 static LONGEST len_avail
= -1;
4126 static struct obstack obstack
;
4130 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4132 if (strcmp (annex
, "processes") != 0)
4135 gdb_assert (readbuf
&& !writebuf
);
4139 if (len_avail
!= -1 && len_avail
!= 0)
4140 obstack_free (&obstack
, NULL
);
4143 obstack_init (&obstack
);
4144 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4146 dirp
= opendir ("/proc");
4150 while ((dp
= readdir (dirp
)) != NULL
)
4152 struct stat statbuf
;
4153 char procentry
[sizeof ("/proc/4294967295")];
4155 if (!isdigit (dp
->d_name
[0])
4156 || strlen (dp
->d_name
) > sizeof ("4294967295") - 1)
4159 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4160 if (stat (procentry
, &statbuf
) == 0
4161 && S_ISDIR (statbuf
.st_mode
))
4165 char cmd
[MAXPATHLEN
+ 1];
4166 struct passwd
*entry
;
4168 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4169 entry
= getpwuid (statbuf
.st_uid
);
4171 if ((f
= fopen (pathname
, "r")) != NULL
)
4173 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4177 for (i
= 0; i
< len
; i
++)
4182 obstack_xml_printf (
4185 "<column name=\"pid\">%s</column>"
4186 "<column name=\"user\">%s</column>"
4187 "<column name=\"command\">%s</column>"
4190 entry
? entry
->pw_name
: "?",
4203 obstack_grow_str0 (&obstack
, "</osdata>\n");
4204 buf
= obstack_finish (&obstack
);
4205 len_avail
= strlen (buf
);
4208 if (offset
>= len_avail
)
4210 /* Done. Get rid of the obstack. */
4211 obstack_free (&obstack
, NULL
);
4217 if (len
> len_avail
- offset
)
4218 len
= len_avail
- offset
;
4219 memcpy (readbuf
, buf
+ offset
, len
);
4225 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4226 const char *annex
, gdb_byte
*readbuf
,
4227 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4231 if (object
== TARGET_OBJECT_AUXV
)
4232 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4235 if (object
== TARGET_OBJECT_OSDATA
)
4236 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4239 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4244 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4248 /* Create a prototype generic GNU/Linux target. The client can override
4249 it with local methods. */
4252 linux_target_install_ops (struct target_ops
*t
)
4254 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4255 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4256 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4257 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4258 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4259 t
->to_post_attach
= linux_child_post_attach
;
4260 t
->to_follow_fork
= linux_child_follow_fork
;
4261 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4262 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4264 super_xfer_partial
= t
->to_xfer_partial
;
4265 t
->to_xfer_partial
= linux_xfer_partial
;
4271 struct target_ops
*t
;
4273 t
= inf_ptrace_target ();
4274 linux_target_install_ops (t
);
4280 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4282 struct target_ops
*t
;
4284 t
= inf_ptrace_trad_target (register_u_offset
);
4285 linux_target_install_ops (t
);
4290 /* target_is_async_p implementation. */
4293 linux_nat_is_async_p (void)
4295 /* NOTE: palves 2008-03-21: We're only async when the user requests
4296 it explicitly with the "maintenance set target-async" command.
4297 Someday, linux will always be async. */
4298 if (!target_async_permitted
)
4304 /* target_can_async_p implementation. */
4307 linux_nat_can_async_p (void)
4309 /* NOTE: palves 2008-03-21: We're only async when the user requests
4310 it explicitly with the "maintenance set target-async" command.
4311 Someday, linux will always be async. */
4312 if (!target_async_permitted
)
4315 /* See target.h/target_async_mask. */
4316 return linux_nat_async_mask_value
;
4320 linux_nat_supports_non_stop (void)
4325 /* target_async_mask implementation. */
4328 linux_nat_async_mask (int mask
)
4331 current_state
= linux_nat_async_mask_value
;
4333 if (current_state
!= mask
)
4337 linux_nat_async (NULL
, 0);
4338 linux_nat_async_mask_value
= mask
;
4342 linux_nat_async_mask_value
= mask
;
4343 linux_nat_async (inferior_event_handler
, 0);
4347 return current_state
;
4350 /* Pop an event from the event pipe. */
4353 linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
)
4355 struct waitpid_result event
= {0};
4360 ret
= read (linux_nat_event_pipe
[0], &event
, sizeof (event
));
4362 while (ret
== -1 && errno
== EINTR
);
4364 gdb_assert (ret
== sizeof (event
));
4366 *ptr_status
= event
.status
;
4367 *ptr_options
= event
.options
;
4369 linux_nat_num_queued_events
--;
4374 /* Push an event into the event pipe. */
4377 linux_nat_event_pipe_push (int pid
, int status
, int options
)
4380 struct waitpid_result event
= {0};
4382 event
.status
= status
;
4383 event
.options
= options
;
4387 ret
= write (linux_nat_event_pipe
[1], &event
, sizeof (event
));
4388 gdb_assert ((ret
== -1 && errno
== EINTR
) || ret
== sizeof (event
));
4389 } while (ret
== -1 && errno
== EINTR
);
4391 linux_nat_num_queued_events
++;
4395 get_pending_events (void)
4397 int status
, options
, pid
;
4399 if (!target_async_permitted
4400 || linux_nat_async_events_state
!= sigchld_async
)
4401 internal_error (__FILE__
, __LINE__
,
4402 "get_pending_events called with async masked");
4407 options
= __WCLONE
| WNOHANG
;
4411 pid
= waitpid (-1, &status
, options
);
4413 while (pid
== -1 && errno
== EINTR
);
4420 pid
= waitpid (-1, &status
, options
);
4422 while (pid
== -1 && errno
== EINTR
);
4426 /* No more children reporting events. */
4429 if (debug_linux_nat_async
)
4430 fprintf_unfiltered (gdb_stdlog
, "\
4431 get_pending_events: pid(%d), status(%x), options (%x)\n",
4432 pid
, status
, options
);
4434 linux_nat_event_pipe_push (pid
, status
, options
);
4437 if (debug_linux_nat_async
)
4438 fprintf_unfiltered (gdb_stdlog
, "\
4439 get_pending_events: linux_nat_num_queued_events(%d)\n",
4440 linux_nat_num_queued_events
);
4443 /* SIGCHLD handler for async mode. */
4446 async_sigchld_handler (int signo
)
4448 if (debug_linux_nat_async
)
4449 fprintf_unfiltered (gdb_stdlog
, "async_sigchld_handler\n");
4451 get_pending_events ();
4454 /* Set SIGCHLD handling state to STATE. Returns previous state. */
4456 static enum sigchld_state
4457 linux_nat_async_events (enum sigchld_state state
)
4459 enum sigchld_state current_state
= linux_nat_async_events_state
;
4461 if (debug_linux_nat_async
)
4462 fprintf_unfiltered (gdb_stdlog
,
4463 "LNAE: state(%d): linux_nat_async_events_state(%d), "
4464 "linux_nat_num_queued_events(%d)\n",
4465 state
, linux_nat_async_events_state
,
4466 linux_nat_num_queued_events
);
4468 if (current_state
!= state
)
4471 sigemptyset (&mask
);
4472 sigaddset (&mask
, SIGCHLD
);
4474 /* Always block before changing state. */
4475 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4477 /* Set new state. */
4478 linux_nat_async_events_state
= state
;
4484 /* Block target events. */
4485 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4486 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4487 /* Get events out of queue, and make them available to
4488 queued_waitpid / my_waitpid. */
4489 pipe_to_local_event_queue ();
4494 /* Unblock target events for async mode. */
4496 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4498 /* Put events we already waited on, in the pipe first, so
4500 local_event_queue_to_pipe ();
4501 /* While in masked async, we may have not collected all
4502 the pending events. Get them out now. */
4503 get_pending_events ();
4506 sigaction (SIGCHLD
, &async_sigchld_action
, NULL
);
4507 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4510 case sigchld_default
:
4512 /* SIGCHLD default mode. */
4513 sigaction (SIGCHLD
, &sigchld_default_action
, NULL
);
4515 /* Get events out of queue, and make them available to
4516 queued_waitpid / my_waitpid. */
4517 pipe_to_local_event_queue ();
4519 /* Unblock SIGCHLD. */
4520 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4526 return current_state
;
4529 static int async_terminal_is_ours
= 1;
4531 /* target_terminal_inferior implementation. */
4534 linux_nat_terminal_inferior (void)
4536 if (!target_is_async_p ())
4538 /* Async mode is disabled. */
4539 terminal_inferior ();
4543 /* GDB should never give the terminal to the inferior, if the
4544 inferior is running in the background (run&, continue&, etc.).
4545 This check can be removed when the common code is fixed. */
4546 if (!sync_execution
)
4549 terminal_inferior ();
4551 if (!async_terminal_is_ours
)
4554 delete_file_handler (input_fd
);
4555 async_terminal_is_ours
= 0;
4559 /* target_terminal_ours implementation. */
4562 linux_nat_terminal_ours (void)
4564 if (!target_is_async_p ())
4566 /* Async mode is disabled. */
4571 /* GDB should never give the terminal to the inferior if the
4572 inferior is running in the background (run&, continue&, etc.),
4573 but claiming it sure should. */
4576 if (!sync_execution
)
4579 if (async_terminal_is_ours
)
4582 clear_sigint_trap ();
4583 add_file_handler (input_fd
, stdin_event_handler
, 0);
4584 async_terminal_is_ours
= 1;
4587 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4589 static void *async_client_context
;
4592 linux_nat_async_file_handler (int error
, gdb_client_data client_data
)
4594 async_client_callback (INF_REG_EVENT
, async_client_context
);
4597 /* target_async implementation. */
4600 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4601 void *context
), void *context
)
4603 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
4604 internal_error (__FILE__
, __LINE__
,
4605 "Calling target_async when async is masked");
4607 if (callback
!= NULL
)
4609 async_client_callback
= callback
;
4610 async_client_context
= context
;
4611 add_file_handler (linux_nat_event_pipe
[0],
4612 linux_nat_async_file_handler
, NULL
);
4614 linux_nat_async_events (sigchld_async
);
4618 async_client_callback
= callback
;
4619 async_client_context
= context
;
4621 linux_nat_async_events (sigchld_sync
);
4622 delete_file_handler (linux_nat_event_pipe
[0]);
4627 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4631 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4633 ptid_t ptid
= * (ptid_t
*) data
;
4635 if (ptid_equal (lwp
->ptid
, ptid
)
4636 || ptid_equal (minus_one_ptid
, ptid
)
4637 || (ptid_is_pid (ptid
)
4638 && ptid_get_pid (ptid
) == ptid_get_pid (lwp
->ptid
)))
4644 if (debug_linux_nat
)
4645 fprintf_unfiltered (gdb_stdlog
,
4646 "LNSL: running -> suspending %s\n",
4647 target_pid_to_str (lwp
->ptid
));
4649 /* Peek once, to check if we've already waited for this
4651 pid
= queued_waitpid_1 (ptid_get_lwp (lwp
->ptid
), &status
,
4652 lwp
->cloned
? __WCLONE
: 0, 1 /* peek */);
4656 ptid_t ptid
= lwp
->ptid
;
4658 stop_callback (lwp
, NULL
);
4659 stop_wait_callback (lwp
, NULL
);
4661 /* If the lwp exits while we try to stop it, there's
4662 nothing else to do. */
4663 lwp
= find_lwp_pid (ptid
);
4667 pid
= queued_waitpid_1 (ptid_get_lwp (lwp
->ptid
), &status
,
4668 lwp
->cloned
? __WCLONE
: 0,
4672 /* If we didn't collect any signal other than SIGSTOP while
4673 stopping the LWP, push a SIGNAL_0 event. In either case,
4674 the event-loop will end up calling target_wait which will
4677 push_waitpid (ptid_get_lwp (lwp
->ptid
), W_STOPCODE (0),
4678 lwp
->cloned
? __WCLONE
: 0);
4682 /* Already known to be stopped; do nothing. */
4684 if (debug_linux_nat
)
4686 if (find_thread_pid (lwp
->ptid
)->stop_requested
)
4687 fprintf_unfiltered (gdb_stdlog
, "\
4688 LNSL: already stopped/stop_requested %s\n",
4689 target_pid_to_str (lwp
->ptid
));
4691 fprintf_unfiltered (gdb_stdlog
, "\
4692 LNSL: already stopped/no stop_requested yet %s\n",
4693 target_pid_to_str (lwp
->ptid
));
4701 linux_nat_stop (ptid_t ptid
)
4705 linux_nat_async_events (sigchld_sync
);
4706 iterate_over_lwps (linux_nat_stop_lwp
, &ptid
);
4707 target_async (inferior_event_handler
, 0);
4710 linux_ops
->to_stop (ptid
);
4714 linux_nat_add_target (struct target_ops
*t
)
4716 /* Save the provided single-threaded target. We save this in a separate
4717 variable because another target we've inherited from (e.g. inf-ptrace)
4718 may have saved a pointer to T; we want to use it for the final
4719 process stratum target. */
4720 linux_ops_saved
= *t
;
4721 linux_ops
= &linux_ops_saved
;
4723 /* Override some methods for multithreading. */
4724 t
->to_create_inferior
= linux_nat_create_inferior
;
4725 t
->to_attach
= linux_nat_attach
;
4726 t
->to_detach
= linux_nat_detach
;
4727 t
->to_resume
= linux_nat_resume
;
4728 t
->to_wait
= linux_nat_wait
;
4729 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4730 t
->to_kill
= linux_nat_kill
;
4731 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4732 t
->to_thread_alive
= linux_nat_thread_alive
;
4733 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4734 t
->to_has_thread_control
= tc_schedlock
;
4736 t
->to_can_async_p
= linux_nat_can_async_p
;
4737 t
->to_is_async_p
= linux_nat_is_async_p
;
4738 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4739 t
->to_async
= linux_nat_async
;
4740 t
->to_async_mask
= linux_nat_async_mask
;
4741 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4742 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4744 /* Methods for non-stop support. */
4745 t
->to_stop
= linux_nat_stop
;
4747 /* We don't change the stratum; this target will sit at
4748 process_stratum and thread_db will set at thread_stratum. This
4749 is a little strange, since this is a multi-threaded-capable
4750 target, but we want to be on the stack below thread_db, and we
4751 also want to be used for single-threaded processes. */
4756 /* Register a method to call whenever a new thread is attached. */
4758 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4760 /* Save the pointer. We only support a single registered instance
4761 of the GNU/Linux native target, so we do not need to map this to
4763 linux_nat_new_thread
= new_thread
;
4766 /* Register a method that converts a siginfo object between the layout
4767 that ptrace returns, and the layout in the architecture of the
4770 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4771 int (*siginfo_fixup
) (struct siginfo
*,
4775 /* Save the pointer. */
4776 linux_nat_siginfo_fixup
= siginfo_fixup
;
4779 /* Return the saved siginfo associated with PTID. */
4781 linux_nat_get_siginfo (ptid_t ptid
)
4783 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4785 gdb_assert (lp
!= NULL
);
4787 return &lp
->siginfo
;
4790 /* Enable/Disable async mode. */
4793 linux_nat_setup_async (void)
4795 if (pipe (linux_nat_event_pipe
) == -1)
4796 internal_error (__FILE__
, __LINE__
,
4797 "creating event pipe failed.");
4798 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4799 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4803 _initialize_linux_nat (void)
4807 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4808 Show /proc process information about any running process.\n\
4809 Specify any process id, or use the program being debugged by default.\n\
4810 Specify any of the following keywords for detailed info:\n\
4811 mappings -- list of mapped memory regions.\n\
4812 stat -- list a bunch of random process info.\n\
4813 status -- list a different bunch of random process info.\n\
4814 all -- list all available /proc info."));
4816 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4817 &debug_linux_nat
, _("\
4818 Set debugging of GNU/Linux lwp module."), _("\
4819 Show debugging of GNU/Linux lwp module."), _("\
4820 Enables printf debugging output."),
4822 show_debug_linux_nat
,
4823 &setdebuglist
, &showdebuglist
);
4825 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4826 &debug_linux_nat_async
, _("\
4827 Set debugging of GNU/Linux async lwp module."), _("\
4828 Show debugging of GNU/Linux async lwp module."), _("\
4829 Enables printf debugging output."),
4831 show_debug_linux_nat_async
,
4832 &setdebuglist
, &showdebuglist
);
4834 /* Get the default SIGCHLD action. Used while forking an inferior
4835 (see linux_nat_create_inferior/linux_nat_async_events). */
4836 sigaction (SIGCHLD
, NULL
, &sigchld_default_action
);
4838 /* Block SIGCHLD by default. Doing this early prevents it getting
4839 unblocked if an exception is thrown due to an error while the
4840 inferior is starting (sigsetjmp/siglongjmp). */
4841 sigemptyset (&mask
);
4842 sigaddset (&mask
, SIGCHLD
);
4843 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4845 /* Save this mask as the default. */
4846 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4848 /* The synchronous SIGCHLD handler. */
4849 sync_sigchld_action
.sa_handler
= sigchld_handler
;
4850 sigemptyset (&sync_sigchld_action
.sa_mask
);
4851 sync_sigchld_action
.sa_flags
= SA_RESTART
;
4853 /* Make it the default. */
4854 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4856 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4857 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4858 sigdelset (&suspend_mask
, SIGCHLD
);
4860 /* SIGCHLD handler for async mode. */
4861 async_sigchld_action
.sa_handler
= async_sigchld_handler
;
4862 sigemptyset (&async_sigchld_action
.sa_mask
);
4863 async_sigchld_action
.sa_flags
= SA_RESTART
;
4865 linux_nat_setup_async ();
4867 add_setshow_boolean_cmd ("disable-randomization", class_support
,
4868 &disable_randomization
, _("\
4869 Set disabling of debuggee's virtual address space randomization."), _("\
4870 Show disabling of debuggee's virtual address space randomization."), _("\
4871 When this mode is on (which is the default), randomization of the virtual\n\
4872 address space is disabled. Standalone programs run with the randomization\n\
4873 enabled by default on some platforms."),
4874 &set_disable_randomization
,
4875 &show_disable_randomization
,
4876 &setlist
, &showlist
);
4880 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4881 the GNU/Linux Threads library and therefore doesn't really belong
4884 /* Read variable NAME in the target and return its value if found.
4885 Otherwise return zero. It is assumed that the type of the variable
4889 get_signo (const char *name
)
4891 struct minimal_symbol
*ms
;
4894 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4898 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4899 sizeof (signo
)) != 0)
4905 /* Return the set of signals used by the threads library in *SET. */
4908 lin_thread_get_thread_signals (sigset_t
*set
)
4910 struct sigaction action
;
4911 int restart
, cancel
;
4912 sigset_t blocked_mask
;
4914 sigemptyset (&blocked_mask
);
4917 restart
= get_signo ("__pthread_sig_restart");
4918 cancel
= get_signo ("__pthread_sig_cancel");
4920 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4921 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4922 not provide any way for the debugger to query the signal numbers -
4923 fortunately they don't change! */
4926 restart
= __SIGRTMIN
;
4929 cancel
= __SIGRTMIN
+ 1;
4931 sigaddset (set
, restart
);
4932 sigaddset (set
, cancel
);
4934 /* The GNU/Linux Threads library makes terminating threads send a
4935 special "cancel" signal instead of SIGCHLD. Make sure we catch
4936 those (to prevent them from terminating GDB itself, which is
4937 likely to be their default action) and treat them the same way as
4940 action
.sa_handler
= sigchld_handler
;
4941 sigemptyset (&action
.sa_mask
);
4942 action
.sa_flags
= SA_RESTART
;
4943 sigaction (cancel
, &action
, NULL
);
4945 /* We block the "cancel" signal throughout this code ... */
4946 sigaddset (&blocked_mask
, cancel
);
4947 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4949 /* ... except during a sigsuspend. */
4950 sigdelset (&suspend_mask
, cancel
);