1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 /* This comment documents high-level logic of this file.
55 Waiting for events in sync mode
56 ===============================
58 When waiting for an event in a specific thread, we just use waitpid, passing
59 the specific pid, and not passing WNOHANG.
61 When waiting for an event in all threads, waitpid is not quite good. Prior to
62 version 2.4, Linux can either wait for event in main thread, or in secondary
63 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
64 miss an event. The solution is to use non-blocking waitpid, together with
65 sigsuspend. First, we use non-blocking waitpid to get an event in the main
66 process, if any. Second, we use non-blocking waitpid with the __WCLONED
67 flag to check for events in cloned processes. If nothing is found, we use
68 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
69 happened to a child process -- and SIGCHLD will be delivered both for events
70 in main debugged process and in cloned processes. As soon as we know there's
71 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
73 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
74 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
75 blocked, the signal becomes pending and sigsuspend immediately
76 notices it and returns.
78 Waiting for events in async mode
79 ================================
81 In async mode, GDB should always be ready to handle both user input and target
82 events, so neither blocking waitpid nor sigsuspend are viable
83 options. Instead, we should notify the GDB main event loop whenever there's
84 unprocessed event from the target. The only way to notify this event loop is
85 to make it wait on input from a pipe, and write something to the pipe whenever
86 there's event. Obviously, if we fail to notify the event loop if there's
87 target event, it's bad. If we notify the event loop when there's no event
88 from target, linux-nat.c will detect that there's no event, actually, and
89 report event of type TARGET_WAITKIND_IGNORE, but it will waste time and
92 The main design point is that every time GDB is outside linux-nat.c, we have a
93 SIGCHLD handler installed that is called when something happens to the target
94 and notifies the GDB event loop. Also, the event is extracted from the target
95 using waitpid and stored for future use. Whenever GDB core decides to handle
96 the event, and calls into linux-nat.c, we disable SIGCHLD and process things
97 as in sync mode, except that before waitpid call we check if there are any
98 previously read events.
100 It could happen that during event processing, we'll try to get more events
101 than there are events in the local queue, which will result to waitpid call.
102 Those waitpid calls, while blocking, are guarantied to always have
103 something for waitpid to return. E.g., stopping a thread with SIGSTOP, and
104 waiting for the lwp to stop.
106 The event loop is notified about new events using a pipe. SIGCHLD handler does
107 waitpid and writes the results in to a pipe. GDB event loop has the other end
108 of the pipe among the sources. When event loop starts to process the event
109 and calls a function in linux-nat.c, all events from the pipe are transferred
110 into a local queue and SIGCHLD is blocked. Further processing goes as in sync
111 mode. Before we return from linux_nat_wait, we transfer all unprocessed events
112 from local queue back to the pipe, so that when we get back to event loop,
113 event loop will notice there's something more to do.
115 SIGCHLD is blocked when we're inside target_wait, so that should we actually
116 want to wait for some more events, SIGCHLD handler does not steal them from
117 us. Technically, it would be possible to add new events to the local queue but
118 it's about the same amount of work as blocking SIGCHLD.
120 This moving of events from pipe into local queue and back into pipe when we
121 enter/leave linux-nat.c is somewhat ugly. Unfortunately, GDB event loop is
122 home-grown and incapable to wait on any queue.
127 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
128 signal is not entirely significant; we just need for a signal to be delivered,
129 so that we can intercept it. SIGSTOP's advantage is that it can not be
130 blocked. A disadvantage is that it is not a real-time signal, so it can only
131 be queued once; we do not keep track of other sources of SIGSTOP.
133 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
134 use them, because they have special behavior when the signal is generated -
135 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
136 kills the entire thread group.
138 A delivered SIGSTOP would stop the entire thread group, not just the thread we
139 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
140 cancel it (by PTRACE_CONT without passing SIGSTOP).
142 We could use a real-time signal instead. This would solve those problems; we
143 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
144 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
145 generates it, and there are races with trying to find a signal that is not
149 #define O_LARGEFILE 0
152 /* If the system headers did not provide the constants, hard-code the normal
154 #ifndef PTRACE_EVENT_FORK
156 #define PTRACE_SETOPTIONS 0x4200
157 #define PTRACE_GETEVENTMSG 0x4201
159 /* options set using PTRACE_SETOPTIONS */
160 #define PTRACE_O_TRACESYSGOOD 0x00000001
161 #define PTRACE_O_TRACEFORK 0x00000002
162 #define PTRACE_O_TRACEVFORK 0x00000004
163 #define PTRACE_O_TRACECLONE 0x00000008
164 #define PTRACE_O_TRACEEXEC 0x00000010
165 #define PTRACE_O_TRACEVFORKDONE 0x00000020
166 #define PTRACE_O_TRACEEXIT 0x00000040
168 /* Wait extended result codes for the above trace options. */
169 #define PTRACE_EVENT_FORK 1
170 #define PTRACE_EVENT_VFORK 2
171 #define PTRACE_EVENT_CLONE 3
172 #define PTRACE_EVENT_EXEC 4
173 #define PTRACE_EVENT_VFORK_DONE 5
174 #define PTRACE_EVENT_EXIT 6
176 #endif /* PTRACE_EVENT_FORK */
178 /* We can't always assume that this flag is available, but all systems
179 with the ptrace event handlers also have __WALL, so it's safe to use
182 #define __WALL 0x40000000 /* Wait for any child. */
185 #ifndef PTRACE_GETSIGINFO
186 #define PTRACE_GETSIGINFO 0x4202
189 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
190 the use of the multi-threaded target. */
191 static struct target_ops
*linux_ops
;
192 static struct target_ops linux_ops_saved
;
194 /* The method to call, if any, when a new thread is attached. */
195 static void (*linux_nat_new_thread
) (ptid_t
);
197 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
198 Called by our to_xfer_partial. */
199 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
201 const char *, gdb_byte
*,
205 static int debug_linux_nat
;
207 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
208 struct cmd_list_element
*c
, const char *value
)
210 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
214 static int debug_linux_nat_async
= 0;
216 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
217 struct cmd_list_element
*c
, const char *value
)
219 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
223 static int linux_parent_pid
;
225 struct simple_pid_list
229 struct simple_pid_list
*next
;
231 struct simple_pid_list
*stopped_pids
;
233 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
234 can not be used, 1 if it can. */
236 static int linux_supports_tracefork_flag
= -1;
238 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
239 PTRACE_O_TRACEVFORKDONE. */
241 static int linux_supports_tracevforkdone_flag
= -1;
243 /* Async mode support */
245 /* True if async mode is currently on. */
246 static int linux_nat_async_enabled
;
248 /* Zero if the async mode, although enabled, is masked, which means
249 linux_nat_wait should behave as if async mode was off. */
250 static int linux_nat_async_mask_value
= 1;
252 /* The read/write ends of the pipe registered as waitable file in the
254 static int linux_nat_event_pipe
[2] = { -1, -1 };
256 /* Number of queued events in the pipe. */
257 static volatile int linux_nat_num_queued_events
;
259 /* The possible SIGCHLD handling states. */
263 /* SIGCHLD disabled, with action set to sigchld_handler, for the
264 sigsuspend in linux_nat_wait. */
266 /* SIGCHLD enabled, with action set to async_sigchld_handler. */
268 /* Set SIGCHLD to default action. Used while creating an
273 /* The current SIGCHLD handling state. */
274 static enum sigchld_state linux_nat_async_events_state
;
276 static enum sigchld_state
linux_nat_async_events (enum sigchld_state enable
);
277 static void pipe_to_local_event_queue (void);
278 static void local_event_queue_to_pipe (void);
279 static void linux_nat_event_pipe_push (int pid
, int status
, int options
);
280 static int linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
);
281 static void linux_nat_set_async_mode (int on
);
282 static void linux_nat_async (void (*callback
)
283 (enum inferior_event_type event_type
, void *context
),
285 static int linux_nat_async_mask (int mask
);
286 static int kill_lwp (int lwpid
, int signo
);
288 /* Captures the result of a successful waitpid call, along with the
289 options used in that call. */
290 struct waitpid_result
295 struct waitpid_result
*next
;
298 /* A singly-linked list of the results of the waitpid calls performed
299 in the async SIGCHLD handler. */
300 static struct waitpid_result
*waitpid_queue
= NULL
;
303 queued_waitpid (int pid
, int *status
, int flags
)
305 struct waitpid_result
*msg
= waitpid_queue
, *prev
= NULL
;
307 if (debug_linux_nat_async
)
308 fprintf_unfiltered (gdb_stdlog
,
310 QWPID: linux_nat_async_events_state(%d), linux_nat_num_queued_events(%d)\n",
311 linux_nat_async_events_state
,
312 linux_nat_num_queued_events
);
316 for (; msg
; prev
= msg
, msg
= msg
->next
)
317 if (pid
== -1 || pid
== msg
->pid
)
320 else if (flags
& __WCLONE
)
322 for (; msg
; prev
= msg
, msg
= msg
->next
)
323 if (msg
->options
& __WCLONE
324 && (pid
== -1 || pid
== msg
->pid
))
329 for (; msg
; prev
= msg
, msg
= msg
->next
)
330 if ((msg
->options
& __WCLONE
) == 0
331 && (pid
== -1 || pid
== msg
->pid
))
340 prev
->next
= msg
->next
;
342 waitpid_queue
= msg
->next
;
346 *status
= msg
->status
;
349 if (debug_linux_nat_async
)
350 fprintf_unfiltered (gdb_stdlog
, "QWPID: pid(%d), status(%x)\n",
357 if (debug_linux_nat_async
)
358 fprintf_unfiltered (gdb_stdlog
, "QWPID: miss\n");
366 push_waitpid (int pid
, int status
, int options
)
368 struct waitpid_result
*event
, *new_event
;
370 new_event
= xmalloc (sizeof (*new_event
));
371 new_event
->pid
= pid
;
372 new_event
->status
= status
;
373 new_event
->options
= options
;
374 new_event
->next
= NULL
;
378 for (event
= waitpid_queue
;
379 event
&& event
->next
;
383 event
->next
= new_event
;
386 waitpid_queue
= new_event
;
389 /* Drain all queued events of PID. If PID is -1, the effect is of
390 draining all events. */
392 drain_queued_events (int pid
)
394 while (queued_waitpid (pid
, NULL
, __WALL
) != -1)
399 /* Trivial list manipulation functions to keep track of a list of
400 new stopped processes. */
402 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
404 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
406 new_pid
->status
= status
;
407 new_pid
->next
= *listp
;
412 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
414 struct simple_pid_list
**p
;
416 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
417 if ((*p
)->pid
== pid
)
419 struct simple_pid_list
*next
= (*p
)->next
;
420 *status
= (*p
)->status
;
429 linux_record_stopped_pid (int pid
, int status
)
431 add_to_pid_list (&stopped_pids
, pid
, status
);
435 /* A helper function for linux_test_for_tracefork, called after fork (). */
438 linux_tracefork_child (void)
442 ptrace (PTRACE_TRACEME
, 0, 0, 0);
443 kill (getpid (), SIGSTOP
);
448 /* Wrapper function for waitpid which handles EINTR, and checks for
449 locally queued events. */
452 my_waitpid (int pid
, int *status
, int flags
)
456 /* There should be no concurrent calls to waitpid. */
457 gdb_assert (linux_nat_async_events_state
== sigchld_sync
);
459 ret
= queued_waitpid (pid
, status
, flags
);
465 ret
= waitpid (pid
, status
, flags
);
467 while (ret
== -1 && errno
== EINTR
);
472 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
474 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
475 we know that the feature is not available. This may change the tracing
476 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
478 However, if it succeeds, we don't know for sure that the feature is
479 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
480 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
481 fork tracing, and let it fork. If the process exits, we assume that we
482 can't use TRACEFORK; if we get the fork notification, and we can extract
483 the new child's PID, then we assume that we can. */
486 linux_test_for_tracefork (int original_pid
)
488 int child_pid
, ret
, status
;
491 linux_supports_tracefork_flag
= 0;
492 linux_supports_tracevforkdone_flag
= 0;
494 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
500 perror_with_name (("fork"));
503 linux_tracefork_child ();
505 ret
= my_waitpid (child_pid
, &status
, 0);
507 perror_with_name (("waitpid"));
508 else if (ret
!= child_pid
)
509 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
510 if (! WIFSTOPPED (status
))
511 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
513 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
516 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
519 warning (_("linux_test_for_tracefork: failed to kill child"));
523 ret
= my_waitpid (child_pid
, &status
, 0);
524 if (ret
!= child_pid
)
525 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
526 else if (!WIFSIGNALED (status
))
527 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
528 "killed child"), status
);
533 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
534 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
535 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
536 linux_supports_tracevforkdone_flag
= (ret
== 0);
538 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
540 warning (_("linux_test_for_tracefork: failed to resume child"));
542 ret
= my_waitpid (child_pid
, &status
, 0);
544 if (ret
== child_pid
&& WIFSTOPPED (status
)
545 && status
>> 16 == PTRACE_EVENT_FORK
)
548 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
549 if (ret
== 0 && second_pid
!= 0)
553 linux_supports_tracefork_flag
= 1;
554 my_waitpid (second_pid
, &second_status
, 0);
555 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
557 warning (_("linux_test_for_tracefork: failed to kill second child"));
558 my_waitpid (second_pid
, &status
, 0);
562 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
563 "(%d, status 0x%x)"), ret
, status
);
565 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
567 warning (_("linux_test_for_tracefork: failed to kill child"));
568 my_waitpid (child_pid
, &status
, 0);
571 /* Return non-zero iff we have tracefork functionality available.
572 This function also sets linux_supports_tracefork_flag. */
575 linux_supports_tracefork (int pid
)
577 if (linux_supports_tracefork_flag
== -1)
578 linux_test_for_tracefork (pid
);
579 return linux_supports_tracefork_flag
;
583 linux_supports_tracevforkdone (int pid
)
585 if (linux_supports_tracefork_flag
== -1)
586 linux_test_for_tracefork (pid
);
587 return linux_supports_tracevforkdone_flag
;
592 linux_enable_event_reporting (ptid_t ptid
)
594 int pid
= ptid_get_lwp (ptid
);
598 pid
= ptid_get_pid (ptid
);
600 if (! linux_supports_tracefork (pid
))
603 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
604 | PTRACE_O_TRACECLONE
;
605 if (linux_supports_tracevforkdone (pid
))
606 options
|= PTRACE_O_TRACEVFORKDONE
;
608 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
609 read-only process state. */
611 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
615 linux_child_post_attach (int pid
)
617 linux_enable_event_reporting (pid_to_ptid (pid
));
618 check_for_thread_db ();
622 linux_child_post_startup_inferior (ptid_t ptid
)
624 linux_enable_event_reporting (ptid
);
625 check_for_thread_db ();
629 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
632 struct target_waitstatus last_status
;
634 int parent_pid
, child_pid
;
636 if (target_can_async_p ())
637 target_async (NULL
, 0);
639 get_last_target_status (&last_ptid
, &last_status
);
640 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
641 parent_pid
= ptid_get_lwp (last_ptid
);
643 parent_pid
= ptid_get_pid (last_ptid
);
644 child_pid
= last_status
.value
.related_pid
;
648 /* We're already attached to the parent, by default. */
650 /* Before detaching from the child, remove all breakpoints from
651 it. (This won't actually modify the breakpoint list, but will
652 physically remove the breakpoints from the child.) */
653 /* If we vforked this will remove the breakpoints from the parent
654 also, but they'll be reinserted below. */
655 detach_breakpoints (child_pid
);
657 /* Detach new forked process? */
660 if (info_verbose
|| debug_linux_nat
)
662 target_terminal_ours ();
663 fprintf_filtered (gdb_stdlog
,
664 "Detaching after fork from child process %d.\n",
668 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
672 struct fork_info
*fp
;
673 /* Retain child fork in ptrace (stopped) state. */
674 fp
= find_fork_pid (child_pid
);
676 fp
= add_fork (child_pid
);
677 fork_save_infrun_state (fp
, 0);
682 gdb_assert (linux_supports_tracefork_flag
>= 0);
683 if (linux_supports_tracevforkdone (0))
687 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
688 my_waitpid (parent_pid
, &status
, __WALL
);
689 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
690 warning (_("Unexpected waitpid result %06x when waiting for "
691 "vfork-done"), status
);
695 /* We can't insert breakpoints until the child has
696 finished with the shared memory region. We need to
697 wait until that happens. Ideal would be to just
699 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
700 - waitpid (parent_pid, &status, __WALL);
701 However, most architectures can't handle a syscall
702 being traced on the way out if it wasn't traced on
705 We might also think to loop, continuing the child
706 until it exits or gets a SIGTRAP. One problem is
707 that the child might call ptrace with PTRACE_TRACEME.
709 There's no simple and reliable way to figure out when
710 the vforked child will be done with its copy of the
711 shared memory. We could step it out of the syscall,
712 two instructions, let it go, and then single-step the
713 parent once. When we have hardware single-step, this
714 would work; with software single-step it could still
715 be made to work but we'd have to be able to insert
716 single-step breakpoints in the child, and we'd have
717 to insert -just- the single-step breakpoint in the
718 parent. Very awkward.
720 In the end, the best we can do is to make sure it
721 runs for a little while. Hopefully it will be out of
722 range of any breakpoints we reinsert. Usually this
723 is only the single-step breakpoint at vfork's return
729 /* Since we vforked, breakpoints were removed in the parent
730 too. Put them back. */
731 reattach_breakpoints (parent_pid
);
736 char child_pid_spelling
[40];
738 /* Needed to keep the breakpoint lists in sync. */
740 detach_breakpoints (child_pid
);
742 /* Before detaching from the parent, remove all breakpoints from it. */
743 remove_breakpoints ();
745 if (info_verbose
|| debug_linux_nat
)
747 target_terminal_ours ();
748 fprintf_filtered (gdb_stdlog
,
749 "Attaching after fork to child process %d.\n",
753 /* If we're vforking, we may want to hold on to the parent until
754 the child exits or execs. At exec time we can remove the old
755 breakpoints from the parent and detach it; at exit time we
756 could do the same (or even, sneakily, resume debugging it - the
757 child's exec has failed, or something similar).
759 This doesn't clean up "properly", because we can't call
760 target_detach, but that's OK; if the current target is "child",
761 then it doesn't need any further cleanups, and lin_lwp will
762 generally not encounter vfork (vfork is defined to fork
765 The holding part is very easy if we have VFORKDONE events;
766 but keeping track of both processes is beyond GDB at the
767 moment. So we don't expose the parent to the rest of GDB.
768 Instead we quietly hold onto it until such time as we can
772 linux_parent_pid
= parent_pid
;
773 else if (!detach_fork
)
775 struct fork_info
*fp
;
776 /* Retain parent fork in ptrace (stopped) state. */
777 fp
= find_fork_pid (parent_pid
);
779 fp
= add_fork (parent_pid
);
780 fork_save_infrun_state (fp
, 0);
783 target_detach (NULL
, 0);
785 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
787 /* Reinstall ourselves, since we might have been removed in
788 target_detach (which does other necessary cleanup). */
791 linux_nat_switch_fork (inferior_ptid
);
792 check_for_thread_db ();
794 /* Reset breakpoints in the child as appropriate. */
795 follow_inferior_reset_breakpoints ();
798 if (target_can_async_p ())
799 target_async (inferior_event_handler
, 0);
806 linux_child_insert_fork_catchpoint (int pid
)
808 if (! linux_supports_tracefork (pid
))
809 error (_("Your system does not support fork catchpoints."));
813 linux_child_insert_vfork_catchpoint (int pid
)
815 if (!linux_supports_tracefork (pid
))
816 error (_("Your system does not support vfork catchpoints."));
820 linux_child_insert_exec_catchpoint (int pid
)
822 if (!linux_supports_tracefork (pid
))
823 error (_("Your system does not support exec catchpoints."));
826 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
827 are processes sharing the same VM space. A multi-threaded process
828 is basically a group of such processes. However, such a grouping
829 is almost entirely a user-space issue; the kernel doesn't enforce
830 such a grouping at all (this might change in the future). In
831 general, we'll rely on the threads library (i.e. the GNU/Linux
832 Threads library) to provide such a grouping.
834 It is perfectly well possible to write a multi-threaded application
835 without the assistance of a threads library, by using the clone
836 system call directly. This module should be able to give some
837 rudimentary support for debugging such applications if developers
838 specify the CLONE_PTRACE flag in the clone system call, and are
839 using the Linux kernel 2.4 or above.
841 Note that there are some peculiarities in GNU/Linux that affect
844 - In general one should specify the __WCLONE flag to waitpid in
845 order to make it report events for any of the cloned processes
846 (and leave it out for the initial process). However, if a cloned
847 process has exited the exit status is only reported if the
848 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
849 we cannot use it since GDB must work on older systems too.
851 - When a traced, cloned process exits and is waited for by the
852 debugger, the kernel reassigns it to the original parent and
853 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
854 library doesn't notice this, which leads to the "zombie problem":
855 When debugged a multi-threaded process that spawns a lot of
856 threads will run out of processes, even if the threads exit,
857 because the "zombies" stay around. */
859 /* List of known LWPs. */
860 struct lwp_info
*lwp_list
;
862 /* Number of LWPs in the list. */
866 /* Original signal mask. */
867 static sigset_t normal_mask
;
869 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
870 _initialize_linux_nat. */
871 static sigset_t suspend_mask
;
873 /* SIGCHLD action for synchronous mode. */
874 struct sigaction sync_sigchld_action
;
876 /* SIGCHLD action for asynchronous mode. */
877 static struct sigaction async_sigchld_action
;
879 /* SIGCHLD default action, to pass to new inferiors. */
880 static struct sigaction sigchld_default_action
;
883 /* Prototypes for local functions. */
884 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
885 static int linux_nat_thread_alive (ptid_t ptid
);
886 static char *linux_child_pid_to_exec_file (int pid
);
887 static int cancel_breakpoint (struct lwp_info
*lp
);
890 /* Convert wait status STATUS to a string. Used for printing debug
894 status_to_str (int status
)
898 if (WIFSTOPPED (status
))
899 snprintf (buf
, sizeof (buf
), "%s (stopped)",
900 strsignal (WSTOPSIG (status
)));
901 else if (WIFSIGNALED (status
))
902 snprintf (buf
, sizeof (buf
), "%s (terminated)",
903 strsignal (WSTOPSIG (status
)));
905 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
910 /* Initialize the list of LWPs. Note that this module, contrary to
911 what GDB's generic threads layer does for its thread list,
912 re-initializes the LWP lists whenever we mourn or detach (which
913 doesn't involve mourning) the inferior. */
918 struct lwp_info
*lp
, *lpnext
;
920 for (lp
= lwp_list
; lp
; lp
= lpnext
)
930 /* Add the LWP specified by PID to the list. Return a pointer to the
931 structure describing the new LWP. The LWP should already be stopped
932 (with an exception for the very first LWP). */
934 static struct lwp_info
*
935 add_lwp (ptid_t ptid
)
939 gdb_assert (is_lwp (ptid
));
941 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
943 memset (lp
, 0, sizeof (struct lwp_info
));
945 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
953 if (num_lwps
> 1 && linux_nat_new_thread
!= NULL
)
954 linux_nat_new_thread (ptid
);
959 /* Remove the LWP specified by PID from the list. */
962 delete_lwp (ptid_t ptid
)
964 struct lwp_info
*lp
, *lpprev
;
968 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
969 if (ptid_equal (lp
->ptid
, ptid
))
978 lpprev
->next
= lp
->next
;
985 /* Return a pointer to the structure describing the LWP corresponding
986 to PID. If no corresponding LWP could be found, return NULL. */
988 static struct lwp_info
*
989 find_lwp_pid (ptid_t ptid
)
995 lwp
= GET_LWP (ptid
);
997 lwp
= GET_PID (ptid
);
999 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1000 if (lwp
== GET_LWP (lp
->ptid
))
1006 /* Call CALLBACK with its second argument set to DATA for every LWP in
1007 the list. If CALLBACK returns 1 for a particular LWP, return a
1008 pointer to the structure describing that LWP immediately.
1009 Otherwise return NULL. */
1012 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
1014 struct lwp_info
*lp
, *lpnext
;
1016 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1019 if ((*callback
) (lp
, data
))
1026 /* Update our internal state when changing from one fork (checkpoint,
1027 et cetera) to another indicated by NEW_PTID. We can only switch
1028 single-threaded applications, so we only create one new LWP, and
1029 the previous list is discarded. */
1032 linux_nat_switch_fork (ptid_t new_ptid
)
1034 struct lwp_info
*lp
;
1036 init_thread_list ();
1038 lp
= add_lwp (new_ptid
);
1039 add_thread_silent (new_ptid
);
1043 /* Record a PTID for later deletion. */
1048 struct saved_ptids
*next
;
1050 static struct saved_ptids
*threads_to_delete
;
1053 record_dead_thread (ptid_t ptid
)
1055 struct saved_ptids
*p
= xmalloc (sizeof (struct saved_ptids
));
1057 p
->next
= threads_to_delete
;
1058 threads_to_delete
= p
;
1061 /* Delete any dead threads which are not the current thread. */
1066 struct saved_ptids
**p
= &threads_to_delete
;
1069 if (! ptid_equal ((*p
)->ptid
, inferior_ptid
))
1071 struct saved_ptids
*tmp
= *p
;
1072 delete_thread (tmp
->ptid
);
1080 /* Handle the exit of a single thread LP. */
1083 exit_lwp (struct lwp_info
*lp
)
1085 struct thread_info
*th
= find_thread_pid (lp
->ptid
);
1089 if (print_thread_events
)
1090 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1092 /* Core GDB cannot deal with us deleting the current thread. */
1093 if (!ptid_equal (lp
->ptid
, inferior_ptid
))
1094 delete_thread (lp
->ptid
);
1096 record_dead_thread (lp
->ptid
);
1099 delete_lwp (lp
->ptid
);
1102 /* Detect `T (stopped)' in `/proc/PID/status'.
1103 Other states including `T (tracing stop)' are reported as false. */
1106 pid_is_stopped (pid_t pid
)
1112 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1113 status_file
= fopen (buf
, "r");
1114 if (status_file
!= NULL
)
1118 while (fgets (buf
, sizeof (buf
), status_file
))
1120 if (strncmp (buf
, "State:", 6) == 0)
1126 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1128 fclose (status_file
);
1133 /* Wait for the LWP specified by LP, which we have just attached to.
1134 Returns a wait status for that LWP, to cache. */
1137 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1140 pid_t new_pid
, pid
= GET_LWP (ptid
);
1143 if (pid_is_stopped (pid
))
1145 if (debug_linux_nat
)
1146 fprintf_unfiltered (gdb_stdlog
,
1147 "LNPAW: Attaching to a stopped process\n");
1149 /* The process is definitely stopped. It is in a job control
1150 stop, unless the kernel predates the TASK_STOPPED /
1151 TASK_TRACED distinction, in which case it might be in a
1152 ptrace stop. Make sure it is in a ptrace stop; from there we
1153 can kill it, signal it, et cetera.
1155 First make sure there is a pending SIGSTOP. Since we are
1156 already attached, the process can not transition from stopped
1157 to running without a PTRACE_CONT; so we know this signal will
1158 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1159 probably already in the queue (unless this kernel is old
1160 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1161 is not an RT signal, it can only be queued once. */
1162 kill_lwp (pid
, SIGSTOP
);
1164 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1165 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1166 ptrace (PTRACE_CONT
, pid
, 0, 0);
1169 /* Make sure the initial process is stopped. The user-level threads
1170 layer might want to poke around in the inferior, and that won't
1171 work if things haven't stabilized yet. */
1172 new_pid
= my_waitpid (pid
, &status
, 0);
1173 if (new_pid
== -1 && errno
== ECHILD
)
1176 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1178 /* Try again with __WCLONE to check cloned processes. */
1179 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1183 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1185 if (WSTOPSIG (status
) != SIGSTOP
)
1188 if (debug_linux_nat
)
1189 fprintf_unfiltered (gdb_stdlog
,
1190 "LNPAW: Received %s after attaching\n",
1191 status_to_str (status
));
1197 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1198 if the new LWP could not be attached. */
1201 lin_lwp_attach_lwp (ptid_t ptid
)
1203 struct lwp_info
*lp
;
1204 enum sigchld_state async_events_original_state
;
1206 gdb_assert (is_lwp (ptid
));
1208 async_events_original_state
= linux_nat_async_events (sigchld_sync
);
1210 lp
= find_lwp_pid (ptid
);
1212 /* We assume that we're already attached to any LWP that has an id
1213 equal to the overall process id, and to any LWP that is already
1214 in our list of LWPs. If we're not seeing exit events from threads
1215 and we've had PID wraparound since we last tried to stop all threads,
1216 this assumption might be wrong; fortunately, this is very unlikely
1218 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1220 int status
, cloned
= 0, signalled
= 0;
1222 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1224 /* If we fail to attach to the thread, issue a warning,
1225 but continue. One way this can happen is if thread
1226 creation is interrupted; as of Linux kernel 2.6.19, a
1227 bug may place threads in the thread list and then fail
1229 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1230 safe_strerror (errno
));
1234 if (debug_linux_nat
)
1235 fprintf_unfiltered (gdb_stdlog
,
1236 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1237 target_pid_to_str (ptid
));
1239 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1240 lp
= add_lwp (ptid
);
1242 lp
->cloned
= cloned
;
1243 lp
->signalled
= signalled
;
1244 if (WSTOPSIG (status
) != SIGSTOP
)
1247 lp
->status
= status
;
1250 target_post_attach (GET_LWP (lp
->ptid
));
1252 if (debug_linux_nat
)
1254 fprintf_unfiltered (gdb_stdlog
,
1255 "LLAL: waitpid %s received %s\n",
1256 target_pid_to_str (ptid
),
1257 status_to_str (status
));
1262 /* We assume that the LWP representing the original process is
1263 already stopped. Mark it as stopped in the data structure
1264 that the GNU/linux ptrace layer uses to keep track of
1265 threads. Note that this won't have already been done since
1266 the main thread will have, we assume, been stopped by an
1267 attach from a different layer. */
1269 lp
= add_lwp (ptid
);
1273 linux_nat_async_events (async_events_original_state
);
1278 linux_nat_create_inferior (char *exec_file
, char *allargs
, char **env
,
1281 int saved_async
= 0;
1283 /* The fork_child mechanism is synchronous and calls target_wait, so
1284 we have to mask the async mode. */
1286 if (target_can_async_p ())
1287 /* Mask async mode. Creating a child requires a loop calling
1288 wait_for_inferior currently. */
1289 saved_async
= linux_nat_async_mask (0);
1292 /* Restore the original signal mask. */
1293 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1294 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1295 suspend_mask
= normal_mask
;
1296 sigdelset (&suspend_mask
, SIGCHLD
);
1299 /* Set SIGCHLD to the default action, until after execing the child,
1300 since the inferior inherits the superior's signal mask. It will
1301 be blocked again in linux_nat_wait, which is only reached after
1302 the inferior execing. */
1303 linux_nat_async_events (sigchld_default
);
1305 linux_ops
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
1308 linux_nat_async_mask (saved_async
);
1312 linux_nat_attach (char *args
, int from_tty
)
1314 struct lwp_info
*lp
;
1317 /* FIXME: We should probably accept a list of process id's, and
1318 attach all of them. */
1319 linux_ops
->to_attach (args
, from_tty
);
1321 if (!target_can_async_p ())
1323 /* Restore the original signal mask. */
1324 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1325 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1326 suspend_mask
= normal_mask
;
1327 sigdelset (&suspend_mask
, SIGCHLD
);
1330 /* Add the initial process as the first LWP to the list. */
1331 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1332 lp
= add_lwp (inferior_ptid
);
1334 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1338 /* If this process is not using thread_db, then we still don't
1339 detect any other threads, but add at least this one. */
1340 add_thread_silent (lp
->ptid
);
1342 /* Save the wait status to report later. */
1344 if (debug_linux_nat
)
1345 fprintf_unfiltered (gdb_stdlog
,
1346 "LNA: waitpid %ld, saving status %s\n",
1347 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1349 if (!target_can_async_p ())
1350 lp
->status
= status
;
1353 /* We already waited for this LWP, so put the wait result on the
1354 pipe. The event loop will wake up and gets us to handling
1356 linux_nat_event_pipe_push (GET_PID (lp
->ptid
), status
,
1357 lp
->cloned
? __WCLONE
: 0);
1358 /* Register in the event loop. */
1359 target_async (inferior_event_handler
, 0);
1363 /* Get pending status of LP. */
1365 get_pending_status (struct lwp_info
*lp
, int *status
)
1367 struct target_waitstatus last
;
1370 get_last_target_status (&last_ptid
, &last
);
1372 /* If this lwp is the ptid that GDB is processing an event from, the
1373 signal will be in stop_signal. Otherwise, in all-stop + sync
1374 mode, we may cache pending events in lp->status while trying to
1375 stop all threads (see stop_wait_callback). In async mode, the
1376 events are always cached in waitpid_queue. */
1379 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1381 if (stop_signal
!= TARGET_SIGNAL_0
1382 && signal_pass_state (stop_signal
))
1383 *status
= W_STOPCODE (target_signal_to_host (stop_signal
));
1385 else if (target_can_async_p ())
1386 queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
);
1388 *status
= lp
->status
;
1394 detach_callback (struct lwp_info
*lp
, void *data
)
1396 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1398 if (debug_linux_nat
&& lp
->status
)
1399 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1400 strsignal (WSTOPSIG (lp
->status
)),
1401 target_pid_to_str (lp
->ptid
));
1403 /* If there is a pending SIGSTOP, get rid of it. */
1406 if (debug_linux_nat
)
1407 fprintf_unfiltered (gdb_stdlog
,
1408 "DC: Sending SIGCONT to %s\n",
1409 target_pid_to_str (lp
->ptid
));
1411 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1415 /* We don't actually detach from the LWP that has an id equal to the
1416 overall process id just yet. */
1417 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1421 /* Pass on any pending signal for this LWP. */
1422 get_pending_status (lp
, &status
);
1425 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1426 WSTOPSIG (status
)) < 0)
1427 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1428 safe_strerror (errno
));
1430 if (debug_linux_nat
)
1431 fprintf_unfiltered (gdb_stdlog
,
1432 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1433 target_pid_to_str (lp
->ptid
),
1434 strsignal (WSTOPSIG (lp
->status
)));
1436 delete_lwp (lp
->ptid
);
1443 linux_nat_detach (char *args
, int from_tty
)
1447 enum target_signal sig
;
1449 if (target_can_async_p ())
1450 linux_nat_async (NULL
, 0);
1452 iterate_over_lwps (detach_callback
, NULL
);
1454 /* Only the initial process should be left right now. */
1455 gdb_assert (num_lwps
== 1);
1457 /* Pass on any pending signal for the last LWP. */
1458 if ((args
== NULL
|| *args
== '\0')
1459 && get_pending_status (lwp_list
, &status
) != -1
1460 && WIFSTOPPED (status
))
1462 /* Put the signal number in ARGS so that inf_ptrace_detach will
1463 pass it along with PTRACE_DETACH. */
1465 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1466 fprintf_unfiltered (gdb_stdlog
,
1467 "LND: Sending signal %s to %s\n",
1469 target_pid_to_str (lwp_list
->ptid
));
1472 /* Destroy LWP info; it's no longer valid. */
1475 pid
= GET_PID (inferior_ptid
);
1476 inferior_ptid
= pid_to_ptid (pid
);
1477 linux_ops
->to_detach (args
, from_tty
);
1479 if (target_can_async_p ())
1480 drain_queued_events (pid
);
1486 resume_callback (struct lwp_info
*lp
, void *data
)
1488 if (lp
->stopped
&& lp
->status
== 0)
1490 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1491 0, TARGET_SIGNAL_0
);
1492 if (debug_linux_nat
)
1493 fprintf_unfiltered (gdb_stdlog
,
1494 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1495 target_pid_to_str (lp
->ptid
));
1498 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1505 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1512 resume_set_callback (struct lwp_info
*lp
, void *data
)
1519 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1521 struct lwp_info
*lp
;
1524 if (debug_linux_nat
)
1525 fprintf_unfiltered (gdb_stdlog
,
1526 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1527 step
? "step" : "resume",
1528 target_pid_to_str (ptid
),
1529 signo
? strsignal (signo
) : "0",
1530 target_pid_to_str (inferior_ptid
));
1534 if (target_can_async_p ())
1535 /* Block events while we're here. */
1536 linux_nat_async_events (sigchld_sync
);
1538 /* A specific PTID means `step only this process id'. */
1539 resume_all
= (PIDGET (ptid
) == -1);
1542 iterate_over_lwps (resume_set_callback
, NULL
);
1544 iterate_over_lwps (resume_clear_callback
, NULL
);
1546 /* If PID is -1, it's the current inferior that should be
1547 handled specially. */
1548 if (PIDGET (ptid
) == -1)
1549 ptid
= inferior_ptid
;
1551 lp
= find_lwp_pid (ptid
);
1552 gdb_assert (lp
!= NULL
);
1554 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1556 /* Remember if we're stepping. */
1559 /* Mark this LWP as resumed. */
1562 /* If we have a pending wait status for this thread, there is no
1563 point in resuming the process. But first make sure that
1564 linux_nat_wait won't preemptively handle the event - we
1565 should never take this short-circuit if we are going to
1566 leave LP running, since we have skipped resuming all the
1567 other threads. This bit of code needs to be synchronized
1568 with linux_nat_wait. */
1570 /* In async mode, we never have pending wait status. */
1571 if (target_can_async_p () && lp
->status
)
1572 internal_error (__FILE__
, __LINE__
, "Pending status in async mode");
1574 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1576 int saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1578 if (signal_stop_state (saved_signo
) == 0
1579 && signal_print_state (saved_signo
) == 0
1580 && signal_pass_state (saved_signo
) == 1)
1582 if (debug_linux_nat
)
1583 fprintf_unfiltered (gdb_stdlog
,
1584 "LLR: Not short circuiting for ignored "
1585 "status 0x%x\n", lp
->status
);
1587 /* FIXME: What should we do if we are supposed to continue
1588 this thread with a signal? */
1589 gdb_assert (signo
== TARGET_SIGNAL_0
);
1590 signo
= saved_signo
;
1597 /* FIXME: What should we do if we are supposed to continue
1598 this thread with a signal? */
1599 gdb_assert (signo
== TARGET_SIGNAL_0
);
1601 if (debug_linux_nat
)
1602 fprintf_unfiltered (gdb_stdlog
,
1603 "LLR: Short circuiting for status 0x%x\n",
1609 /* Mark LWP as not stopped to prevent it from being continued by
1614 iterate_over_lwps (resume_callback
, NULL
);
1616 linux_ops
->to_resume (ptid
, step
, signo
);
1617 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1619 if (debug_linux_nat
)
1620 fprintf_unfiltered (gdb_stdlog
,
1621 "LLR: %s %s, %s (resume event thread)\n",
1622 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1623 target_pid_to_str (ptid
),
1624 signo
? strsignal (signo
) : "0");
1626 if (target_can_async_p ())
1628 target_executing
= 1;
1629 target_async (inferior_event_handler
, 0);
1633 /* Issue kill to specified lwp. */
1635 static int tkill_failed
;
1638 kill_lwp (int lwpid
, int signo
)
1642 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1643 fails, then we are not using nptl threads and we should be using kill. */
1645 #ifdef HAVE_TKILL_SYSCALL
1648 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1649 if (errno
!= ENOSYS
)
1656 return kill (lwpid
, signo
);
1659 /* Handle a GNU/Linux extended wait response. If we see a clone
1660 event, we need to add the new LWP to our list (and not report the
1661 trap to higher layers). This function returns non-zero if the
1662 event should be ignored and we should wait again. If STOPPING is
1663 true, the new LWP remains stopped, otherwise it is continued. */
1666 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1669 int pid
= GET_LWP (lp
->ptid
);
1670 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1671 struct lwp_info
*new_lp
= NULL
;
1672 int event
= status
>> 16;
1674 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1675 || event
== PTRACE_EVENT_CLONE
)
1677 unsigned long new_pid
;
1680 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1682 /* If we haven't already seen the new PID stop, wait for it now. */
1683 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1685 /* The new child has a pending SIGSTOP. We can't affect it until it
1686 hits the SIGSTOP, but we're already attached. */
1687 ret
= my_waitpid (new_pid
, &status
,
1688 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1690 perror_with_name (_("waiting for new child"));
1691 else if (ret
!= new_pid
)
1692 internal_error (__FILE__
, __LINE__
,
1693 _("wait returned unexpected PID %d"), ret
);
1694 else if (!WIFSTOPPED (status
))
1695 internal_error (__FILE__
, __LINE__
,
1696 _("wait returned unexpected status 0x%x"), status
);
1699 ourstatus
->value
.related_pid
= new_pid
;
1701 if (event
== PTRACE_EVENT_FORK
)
1702 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1703 else if (event
== PTRACE_EVENT_VFORK
)
1704 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1707 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1708 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (inferior_ptid
)));
1711 if (WSTOPSIG (status
) != SIGSTOP
)
1713 /* This can happen if someone starts sending signals to
1714 the new thread before it gets a chance to run, which
1715 have a lower number than SIGSTOP (e.g. SIGUSR1).
1716 This is an unlikely case, and harder to handle for
1717 fork / vfork than for clone, so we do not try - but
1718 we handle it for clone events here. We'll send
1719 the other signal on to the thread below. */
1721 new_lp
->signalled
= 1;
1727 new_lp
->stopped
= 1;
1730 new_lp
->resumed
= 1;
1731 ptrace (PTRACE_CONT
, lp
->waitstatus
.value
.related_pid
, 0,
1732 status
? WSTOPSIG (status
) : 0);
1735 if (debug_linux_nat
)
1736 fprintf_unfiltered (gdb_stdlog
,
1737 "LHEW: Got clone event from LWP %ld, resuming\n",
1738 GET_LWP (lp
->ptid
));
1739 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1747 if (event
== PTRACE_EVENT_EXEC
)
1749 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1750 ourstatus
->value
.execd_pathname
1751 = xstrdup (linux_child_pid_to_exec_file (pid
));
1753 if (linux_parent_pid
)
1755 detach_breakpoints (linux_parent_pid
);
1756 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1758 linux_parent_pid
= 0;
1761 /* At this point, all inserted breakpoints are gone. Doing this
1762 as soon as we detect an exec prevents the badness of deleting
1763 a breakpoint writing the current "shadow contents" to lift
1764 the bp. That shadow is NOT valid after an exec.
1766 Note that we have to do this after the detach_breakpoints
1767 call above, otherwise breakpoints wouldn't be lifted from the
1768 parent on a vfork, because detach_breakpoints would think
1769 that breakpoints are not inserted. */
1770 mark_breakpoints_out ();
1774 internal_error (__FILE__
, __LINE__
,
1775 _("unknown ptrace event %d"), event
);
1778 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1782 wait_lwp (struct lwp_info
*lp
)
1786 int thread_dead
= 0;
1788 gdb_assert (!lp
->stopped
);
1789 gdb_assert (lp
->status
== 0);
1791 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1792 if (pid
== -1 && errno
== ECHILD
)
1794 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1795 if (pid
== -1 && errno
== ECHILD
)
1797 /* The thread has previously exited. We need to delete it
1798 now because, for some vendor 2.4 kernels with NPTL
1799 support backported, there won't be an exit event unless
1800 it is the main thread. 2.6 kernels will report an exit
1801 event for each thread that exits, as expected. */
1803 if (debug_linux_nat
)
1804 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
1805 target_pid_to_str (lp
->ptid
));
1811 gdb_assert (pid
== GET_LWP (lp
->ptid
));
1813 if (debug_linux_nat
)
1815 fprintf_unfiltered (gdb_stdlog
,
1816 "WL: waitpid %s received %s\n",
1817 target_pid_to_str (lp
->ptid
),
1818 status_to_str (status
));
1822 /* Check if the thread has exited. */
1823 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1826 if (debug_linux_nat
)
1827 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
1828 target_pid_to_str (lp
->ptid
));
1837 gdb_assert (WIFSTOPPED (status
));
1839 /* Handle GNU/Linux's extended waitstatus for trace events. */
1840 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1842 if (debug_linux_nat
)
1843 fprintf_unfiltered (gdb_stdlog
,
1844 "WL: Handling extended status 0x%06x\n",
1846 if (linux_handle_extended_wait (lp
, status
, 1))
1847 return wait_lwp (lp
);
1853 /* Save the most recent siginfo for LP. This is currently only called
1854 for SIGTRAP; some ports use the si_addr field for
1855 target_stopped_data_address. In the future, it may also be used to
1856 restore the siginfo of requeued signals. */
1859 save_siginfo (struct lwp_info
*lp
)
1862 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
1863 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
1866 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1869 /* Send a SIGSTOP to LP. */
1872 stop_callback (struct lwp_info
*lp
, void *data
)
1874 if (!lp
->stopped
&& !lp
->signalled
)
1878 if (debug_linux_nat
)
1880 fprintf_unfiltered (gdb_stdlog
,
1881 "SC: kill %s **<SIGSTOP>**\n",
1882 target_pid_to_str (lp
->ptid
));
1885 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
1886 if (debug_linux_nat
)
1888 fprintf_unfiltered (gdb_stdlog
,
1889 "SC: lwp kill %d %s\n",
1891 errno
? safe_strerror (errno
) : "ERRNO-OK");
1895 gdb_assert (lp
->status
== 0);
1901 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1902 a pointer to a set of signals to be flushed immediately. */
1905 stop_wait_callback (struct lwp_info
*lp
, void *data
)
1907 sigset_t
*flush_mask
= data
;
1913 status
= wait_lwp (lp
);
1917 /* Ignore any signals in FLUSH_MASK. */
1918 if (flush_mask
&& sigismember (flush_mask
, WSTOPSIG (status
)))
1927 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1928 if (debug_linux_nat
)
1929 fprintf_unfiltered (gdb_stdlog
,
1930 "PTRACE_CONT %s, 0, 0 (%s)\n",
1931 target_pid_to_str (lp
->ptid
),
1932 errno
? safe_strerror (errno
) : "OK");
1934 return stop_wait_callback (lp
, flush_mask
);
1937 if (WSTOPSIG (status
) != SIGSTOP
)
1939 if (WSTOPSIG (status
) == SIGTRAP
)
1941 /* If a LWP other than the LWP that we're reporting an
1942 event for has hit a GDB breakpoint (as opposed to
1943 some random trap signal), then just arrange for it to
1944 hit it again later. We don't keep the SIGTRAP status
1945 and don't forward the SIGTRAP signal to the LWP. We
1946 will handle the current event, eventually we will
1947 resume all LWPs, and this one will get its breakpoint
1950 If we do not do this, then we run the risk that the
1951 user will delete or disable the breakpoint, but the
1952 thread will have already tripped on it. */
1954 /* Save the trap's siginfo in case we need it later. */
1957 /* Now resume this LWP and get the SIGSTOP event. */
1959 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1960 if (debug_linux_nat
)
1962 fprintf_unfiltered (gdb_stdlog
,
1963 "PTRACE_CONT %s, 0, 0 (%s)\n",
1964 target_pid_to_str (lp
->ptid
),
1965 errno
? safe_strerror (errno
) : "OK");
1967 fprintf_unfiltered (gdb_stdlog
,
1968 "SWC: Candidate SIGTRAP event in %s\n",
1969 target_pid_to_str (lp
->ptid
));
1971 /* Hold this event/waitstatus while we check to see if
1972 there are any more (we still want to get that SIGSTOP). */
1973 stop_wait_callback (lp
, data
);
1975 if (target_can_async_p ())
1977 /* Don't leave a pending wait status in async mode.
1978 Retrigger the breakpoint. */
1979 if (!cancel_breakpoint (lp
))
1981 /* There was no gdb breakpoint set at pc. Put
1982 the event back in the queue. */
1983 if (debug_linux_nat
)
1984 fprintf_unfiltered (gdb_stdlog
,
1985 "SWC: kill %s, %s\n",
1986 target_pid_to_str (lp
->ptid
),
1987 status_to_str ((int) status
));
1988 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
1993 /* Hold the SIGTRAP for handling by
1995 /* If there's another event, throw it back into the
1999 if (debug_linux_nat
)
2000 fprintf_unfiltered (gdb_stdlog
,
2001 "SWC: kill %s, %s\n",
2002 target_pid_to_str (lp
->ptid
),
2003 status_to_str ((int) status
));
2004 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2006 /* Save the sigtrap event. */
2007 lp
->status
= status
;
2013 /* The thread was stopped with a signal other than
2014 SIGSTOP, and didn't accidentally trip a breakpoint. */
2016 if (debug_linux_nat
)
2018 fprintf_unfiltered (gdb_stdlog
,
2019 "SWC: Pending event %s in %s\n",
2020 status_to_str ((int) status
),
2021 target_pid_to_str (lp
->ptid
));
2023 /* Now resume this LWP and get the SIGSTOP event. */
2025 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2026 if (debug_linux_nat
)
2027 fprintf_unfiltered (gdb_stdlog
,
2028 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2029 target_pid_to_str (lp
->ptid
),
2030 errno
? safe_strerror (errno
) : "OK");
2032 /* Hold this event/waitstatus while we check to see if
2033 there are any more (we still want to get that SIGSTOP). */
2034 stop_wait_callback (lp
, data
);
2036 /* If the lp->status field is still empty, use it to
2037 hold this event. If not, then this event must be
2038 returned to the event queue of the LWP. */
2039 if (lp
->status
|| target_can_async_p ())
2041 if (debug_linux_nat
)
2043 fprintf_unfiltered (gdb_stdlog
,
2044 "SWC: kill %s, %s\n",
2045 target_pid_to_str (lp
->ptid
),
2046 status_to_str ((int) status
));
2048 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2051 lp
->status
= status
;
2057 /* We caught the SIGSTOP that we intended to catch, so
2058 there's no SIGSTOP pending. */
2067 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
2068 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
2071 linux_nat_has_pending (int pid
, sigset_t
*pending
, sigset_t
*flush_mask
)
2073 sigset_t blocked
, ignored
;
2076 linux_proc_pending_signals (pid
, pending
, &blocked
, &ignored
);
2081 for (i
= 1; i
< NSIG
; i
++)
2082 if (sigismember (pending
, i
))
2083 if (!sigismember (flush_mask
, i
)
2084 || sigismember (&blocked
, i
)
2085 || sigismember (&ignored
, i
))
2086 sigdelset (pending
, i
);
2088 if (sigisemptyset (pending
))
2094 /* DATA is interpreted as a mask of signals to flush. If LP has
2095 signals pending, and they are all in the flush mask, then arrange
2096 to flush them. LP should be stopped, as should all other threads
2097 it might share a signal queue with. */
2100 flush_callback (struct lwp_info
*lp
, void *data
)
2102 sigset_t
*flush_mask
= data
;
2103 sigset_t pending
, intersection
, blocked
, ignored
;
2106 /* Normally, when an LWP exits, it is removed from the LWP list. The
2107 last LWP isn't removed till later, however. So if there is only
2108 one LWP on the list, make sure it's alive. */
2109 if (lwp_list
== lp
&& lp
->next
== NULL
)
2110 if (!linux_nat_thread_alive (lp
->ptid
))
2113 /* Just because the LWP is stopped doesn't mean that new signals
2114 can't arrive from outside, so this function must be careful of
2115 race conditions. However, because all threads are stopped, we
2116 can assume that the pending mask will not shrink unless we resume
2117 the LWP, and that it will then get another signal. We can't
2118 control which one, however. */
2122 if (debug_linux_nat
)
2123 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp
->status
);
2124 if (WIFSTOPPED (lp
->status
) && sigismember (flush_mask
, WSTOPSIG (lp
->status
)))
2128 /* While there is a pending signal we would like to flush, continue
2129 the inferior and collect another signal. But if there's already
2130 a saved status that we don't want to flush, we can't resume the
2131 inferior - if it stopped for some other reason we wouldn't have
2132 anywhere to save the new status. In that case, we must leave the
2133 signal unflushed (and possibly generate an extra SIGINT stop).
2134 That's much less bad than losing a signal. */
2135 while (lp
->status
== 0
2136 && linux_nat_has_pending (GET_LWP (lp
->ptid
), &pending
, flush_mask
))
2141 ret
= ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2142 if (debug_linux_nat
)
2143 fprintf_unfiltered (gdb_stderr
,
2144 "FC: Sent PTRACE_CONT, ret %d %d\n", ret
, errno
);
2147 stop_wait_callback (lp
, flush_mask
);
2148 if (debug_linux_nat
)
2149 fprintf_unfiltered (gdb_stderr
,
2150 "FC: Wait finished; saved status is %d\n",
2157 /* Return non-zero if LP has a wait status pending. */
2160 status_callback (struct lwp_info
*lp
, void *data
)
2162 /* Only report a pending wait status if we pretend that this has
2163 indeed been resumed. */
2164 return (lp
->status
!= 0 && lp
->resumed
);
2167 /* Return non-zero if LP isn't stopped. */
2170 running_callback (struct lwp_info
*lp
, void *data
)
2172 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2175 /* Count the LWP's that have had events. */
2178 count_events_callback (struct lwp_info
*lp
, void *data
)
2182 gdb_assert (count
!= NULL
);
2184 /* Count only LWPs that have a SIGTRAP event pending. */
2186 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2192 /* Select the LWP (if any) that is currently being single-stepped. */
2195 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2197 if (lp
->step
&& lp
->status
!= 0)
2203 /* Select the Nth LWP that has had a SIGTRAP event. */
2206 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2208 int *selector
= data
;
2210 gdb_assert (selector
!= NULL
);
2212 /* Select only LWPs that have a SIGTRAP event pending. */
2214 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2215 if ((*selector
)-- == 0)
2222 cancel_breakpoint (struct lwp_info
*lp
)
2224 /* Arrange for a breakpoint to be hit again later. We don't keep
2225 the SIGTRAP status and don't forward the SIGTRAP signal to the
2226 LWP. We will handle the current event, eventually we will resume
2227 this LWP, and this breakpoint will trap again.
2229 If we do not do this, then we run the risk that the user will
2230 delete or disable the breakpoint, but the LWP will have already
2233 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2234 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2237 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2238 if (breakpoint_inserted_here_p (pc
))
2240 if (debug_linux_nat
)
2241 fprintf_unfiltered (gdb_stdlog
,
2242 "CB: Push back breakpoint for %s\n",
2243 target_pid_to_str (lp
->ptid
));
2245 /* Back up the PC if necessary. */
2246 if (gdbarch_decr_pc_after_break (gdbarch
))
2247 regcache_write_pc (regcache
, pc
);
2255 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2257 struct lwp_info
*event_lp
= data
;
2259 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2263 /* If a LWP other than the LWP that we're reporting an event for has
2264 hit a GDB breakpoint (as opposed to some random trap signal),
2265 then just arrange for it to hit it again later. We don't keep
2266 the SIGTRAP status and don't forward the SIGTRAP signal to the
2267 LWP. We will handle the current event, eventually we will resume
2268 all LWPs, and this one will get its breakpoint trap again.
2270 If we do not do this, then we run the risk that the user will
2271 delete or disable the breakpoint, but the LWP will have already
2275 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2276 && cancel_breakpoint (lp
))
2277 /* Throw away the SIGTRAP. */
2283 /* Select one LWP out of those that have events pending. */
2286 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
2289 int random_selector
;
2290 struct lwp_info
*event_lp
;
2292 /* Record the wait status for the original LWP. */
2293 (*orig_lp
)->status
= *status
;
2295 /* Give preference to any LWP that is being single-stepped. */
2296 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
2297 if (event_lp
!= NULL
)
2299 if (debug_linux_nat
)
2300 fprintf_unfiltered (gdb_stdlog
,
2301 "SEL: Select single-step %s\n",
2302 target_pid_to_str (event_lp
->ptid
));
2306 /* No single-stepping LWP. Select one at random, out of those
2307 which have had SIGTRAP events. */
2309 /* First see how many SIGTRAP events we have. */
2310 iterate_over_lwps (count_events_callback
, &num_events
);
2312 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2313 random_selector
= (int)
2314 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2316 if (debug_linux_nat
&& num_events
> 1)
2317 fprintf_unfiltered (gdb_stdlog
,
2318 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2319 num_events
, random_selector
);
2321 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
2325 if (event_lp
!= NULL
)
2327 /* Switch the event LWP. */
2328 *orig_lp
= event_lp
;
2329 *status
= event_lp
->status
;
2332 /* Flush the wait status for the event LWP. */
2333 (*orig_lp
)->status
= 0;
2336 /* Return non-zero if LP has been resumed. */
2339 resumed_callback (struct lwp_info
*lp
, void *data
)
2344 /* Stop an active thread, verify it still exists, then resume it. */
2347 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2349 struct lwp_info
*ptr
;
2351 if (!lp
->stopped
&& !lp
->signalled
)
2353 stop_callback (lp
, NULL
);
2354 stop_wait_callback (lp
, NULL
);
2355 /* Resume if the lwp still exists. */
2356 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2359 resume_callback (lp
, NULL
);
2360 resume_set_callback (lp
, NULL
);
2366 /* Check if we should go on and pass this event to common code.
2367 Return the affected lwp if we are, or NULL otherwise. */
2368 static struct lwp_info
*
2369 linux_nat_filter_event (int lwpid
, int status
, int options
)
2371 struct lwp_info
*lp
;
2373 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2375 /* Check for stop events reported by a process we didn't already
2376 know about - anything not already in our LWP list.
2378 If we're expecting to receive stopped processes after
2379 fork, vfork, and clone events, then we'll just add the
2380 new one to our list and go back to waiting for the event
2381 to be reported - the stopped process might be returned
2382 from waitpid before or after the event is. */
2383 if (WIFSTOPPED (status
) && !lp
)
2385 linux_record_stopped_pid (lwpid
, status
);
2389 /* Make sure we don't report an event for the exit of an LWP not in
2390 our list, i.e. not part of the current process. This can happen
2391 if we detach from a program we original forked and then it
2393 if (!WIFSTOPPED (status
) && !lp
)
2396 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2397 CLONE_PTRACE processes which do not use the thread library -
2398 otherwise we wouldn't find the new LWP this way. That doesn't
2399 currently work, and the following code is currently unreachable
2400 due to the two blocks above. If it's fixed some day, this code
2401 should be broken out into a function so that we can also pick up
2402 LWPs from the new interface. */
2405 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2406 if (options
& __WCLONE
)
2409 gdb_assert (WIFSTOPPED (status
)
2410 && WSTOPSIG (status
) == SIGSTOP
);
2413 if (!in_thread_list (inferior_ptid
))
2415 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2416 GET_PID (inferior_ptid
));
2417 add_thread (inferior_ptid
);
2420 add_thread (lp
->ptid
);
2423 /* Save the trap's siginfo in case we need it later. */
2424 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2427 /* Handle GNU/Linux's extended waitstatus for trace events. */
2428 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2430 if (debug_linux_nat
)
2431 fprintf_unfiltered (gdb_stdlog
,
2432 "LLW: Handling extended status 0x%06x\n",
2434 if (linux_handle_extended_wait (lp
, status
, 0))
2438 /* Check if the thread has exited. */
2439 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2441 /* If this is the main thread, we must stop all threads and
2442 verify if they are still alive. This is because in the nptl
2443 thread model, there is no signal issued for exiting LWPs
2444 other than the main thread. We only get the main thread exit
2445 signal once all child threads have already exited. If we
2446 stop all the threads and use the stop_wait_callback to check
2447 if they have exited we can determine whether this signal
2448 should be ignored or whether it means the end of the debugged
2449 application, regardless of which threading model is being
2451 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2454 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2457 if (debug_linux_nat
)
2458 fprintf_unfiltered (gdb_stdlog
,
2459 "LLW: %s exited.\n",
2460 target_pid_to_str (lp
->ptid
));
2464 /* If there is at least one more LWP, then the exit signal was
2465 not the end of the debugged application and should be
2469 /* Make sure there is at least one thread running. */
2470 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2472 /* Discard the event. */
2477 /* Check if the current LWP has previously exited. In the nptl
2478 thread model, LWPs other than the main thread do not issue
2479 signals when they exit so we must check whenever the thread has
2480 stopped. A similar check is made in stop_wait_callback(). */
2481 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2483 if (debug_linux_nat
)
2484 fprintf_unfiltered (gdb_stdlog
,
2485 "LLW: %s exited.\n",
2486 target_pid_to_str (lp
->ptid
));
2490 /* Make sure there is at least one thread running. */
2491 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2493 /* Discard the event. */
2497 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2498 an attempt to stop an LWP. */
2500 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2502 if (debug_linux_nat
)
2503 fprintf_unfiltered (gdb_stdlog
,
2504 "LLW: Delayed SIGSTOP caught for %s.\n",
2505 target_pid_to_str (lp
->ptid
));
2507 /* This is a delayed SIGSTOP. */
2510 registers_changed ();
2512 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2513 lp
->step
, TARGET_SIGNAL_0
);
2514 if (debug_linux_nat
)
2515 fprintf_unfiltered (gdb_stdlog
,
2516 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2518 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2519 target_pid_to_str (lp
->ptid
));
2522 gdb_assert (lp
->resumed
);
2524 /* Discard the event. */
2528 /* An interesting event. */
2533 /* Get the events stored in the pipe into the local queue, so they are
2534 accessible to queued_waitpid. We need to do this, since it is not
2535 always the case that the event at the head of the pipe is the event
2539 pipe_to_local_event_queue (void)
2541 if (debug_linux_nat_async
)
2542 fprintf_unfiltered (gdb_stdlog
,
2543 "PTLEQ: linux_nat_num_queued_events(%d)\n",
2544 linux_nat_num_queued_events
);
2545 while (linux_nat_num_queued_events
)
2547 int lwpid
, status
, options
;
2548 lwpid
= linux_nat_event_pipe_pop (&status
, &options
);
2549 gdb_assert (lwpid
> 0);
2550 push_waitpid (lwpid
, status
, options
);
2554 /* Get the unprocessed events stored in the local queue back into the
2555 pipe, so the event loop realizes there's something else to
2559 local_event_queue_to_pipe (void)
2561 struct waitpid_result
*w
= waitpid_queue
;
2564 struct waitpid_result
*next
= w
->next
;
2565 linux_nat_event_pipe_push (w
->pid
,
2571 waitpid_queue
= NULL
;
2573 if (debug_linux_nat_async
)
2574 fprintf_unfiltered (gdb_stdlog
,
2575 "LEQTP: linux_nat_num_queued_events(%d)\n",
2576 linux_nat_num_queued_events
);
2580 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2582 struct lwp_info
*lp
= NULL
;
2585 pid_t pid
= PIDGET (ptid
);
2586 sigset_t flush_mask
;
2588 if (debug_linux_nat_async
)
2589 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2591 /* The first time we get here after starting a new inferior, we may
2592 not have added it to the LWP list yet - this is the earliest
2593 moment at which we know its PID. */
2596 gdb_assert (!is_lwp (inferior_ptid
));
2598 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2599 GET_PID (inferior_ptid
));
2600 lp
= add_lwp (inferior_ptid
);
2602 /* Add the main thread to GDB's thread list. */
2603 add_thread_silent (lp
->ptid
);
2606 sigemptyset (&flush_mask
);
2608 /* Block events while we're here. */
2609 linux_nat_async_events (sigchld_sync
);
2613 /* Make sure there is at least one LWP that has been resumed. */
2614 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
2616 /* First check if there is a LWP with a wait status pending. */
2619 /* Any LWP that's been resumed will do. */
2620 lp
= iterate_over_lwps (status_callback
, NULL
);
2623 if (target_can_async_p ())
2624 internal_error (__FILE__
, __LINE__
,
2625 "Found an LWP with a pending status in async mode.");
2627 status
= lp
->status
;
2630 if (debug_linux_nat
&& status
)
2631 fprintf_unfiltered (gdb_stdlog
,
2632 "LLW: Using pending wait status %s for %s.\n",
2633 status_to_str (status
),
2634 target_pid_to_str (lp
->ptid
));
2637 /* But if we don't find one, we'll have to wait, and check both
2638 cloned and uncloned processes. We start with the cloned
2640 options
= __WCLONE
| WNOHANG
;
2642 else if (is_lwp (ptid
))
2644 if (debug_linux_nat
)
2645 fprintf_unfiltered (gdb_stdlog
,
2646 "LLW: Waiting for specific LWP %s.\n",
2647 target_pid_to_str (ptid
));
2649 /* We have a specific LWP to check. */
2650 lp
= find_lwp_pid (ptid
);
2652 status
= lp
->status
;
2655 if (debug_linux_nat
&& status
)
2656 fprintf_unfiltered (gdb_stdlog
,
2657 "LLW: Using pending wait status %s for %s.\n",
2658 status_to_str (status
),
2659 target_pid_to_str (lp
->ptid
));
2661 /* If we have to wait, take into account whether PID is a cloned
2662 process or not. And we have to convert it to something that
2663 the layer beneath us can understand. */
2664 options
= lp
->cloned
? __WCLONE
: 0;
2665 pid
= GET_LWP (ptid
);
2668 if (status
&& lp
->signalled
)
2670 /* A pending SIGSTOP may interfere with the normal stream of
2671 events. In a typical case where interference is a problem,
2672 we have a SIGSTOP signal pending for LWP A while
2673 single-stepping it, encounter an event in LWP B, and take the
2674 pending SIGSTOP while trying to stop LWP A. After processing
2675 the event in LWP B, LWP A is continued, and we'll never see
2676 the SIGTRAP associated with the last time we were
2677 single-stepping LWP A. */
2679 /* Resume the thread. It should halt immediately returning the
2681 registers_changed ();
2682 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2683 lp
->step
, TARGET_SIGNAL_0
);
2684 if (debug_linux_nat
)
2685 fprintf_unfiltered (gdb_stdlog
,
2686 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2687 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2688 target_pid_to_str (lp
->ptid
));
2690 gdb_assert (lp
->resumed
);
2692 /* This should catch the pending SIGSTOP. */
2693 stop_wait_callback (lp
, NULL
);
2696 if (!target_can_async_p ())
2698 /* Causes SIGINT to be passed on to the attached process. */
2707 if (target_can_async_p ())
2708 /* In async mode, don't ever block. Only look at the locally
2710 lwpid
= queued_waitpid (pid
, &status
, options
);
2712 lwpid
= my_waitpid (pid
, &status
, options
);
2716 gdb_assert (pid
== -1 || lwpid
== pid
);
2718 if (debug_linux_nat
)
2720 fprintf_unfiltered (gdb_stdlog
,
2721 "LLW: waitpid %ld received %s\n",
2722 (long) lwpid
, status_to_str (status
));
2725 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2728 /* A discarded event. */
2738 /* Alternate between checking cloned and uncloned processes. */
2739 options
^= __WCLONE
;
2741 /* And every time we have checked both:
2742 In async mode, return to event loop;
2743 In sync mode, suspend waiting for a SIGCHLD signal. */
2744 if (options
& __WCLONE
)
2746 if (target_can_async_p ())
2748 /* No interesting event. */
2749 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2751 /* Get ready for the next event. */
2752 target_async (inferior_event_handler
, 0);
2754 if (debug_linux_nat_async
)
2755 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2757 return minus_one_ptid
;
2760 sigsuspend (&suspend_mask
);
2764 /* We shouldn't end up here unless we want to try again. */
2765 gdb_assert (status
== 0);
2768 if (!target_can_async_p ())
2770 clear_sigio_trap ();
2771 clear_sigint_trap ();
2776 /* Don't report signals that GDB isn't interested in, such as
2777 signals that are neither printed nor stopped upon. Stopping all
2778 threads can be a bit time-consuming so if we want decent
2779 performance with heavily multi-threaded programs, especially when
2780 they're using a high frequency timer, we'd better avoid it if we
2783 if (WIFSTOPPED (status
))
2785 int signo
= target_signal_from_host (WSTOPSIG (status
));
2787 /* If we get a signal while single-stepping, we may need special
2788 care, e.g. to skip the signal handler. Defer to common code. */
2790 && signal_stop_state (signo
) == 0
2791 && signal_print_state (signo
) == 0
2792 && signal_pass_state (signo
) == 1)
2794 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2795 here? It is not clear we should. GDB may not expect
2796 other threads to run. On the other hand, not resuming
2797 newly attached threads may cause an unwanted delay in
2798 getting them running. */
2799 registers_changed ();
2800 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2802 if (debug_linux_nat
)
2803 fprintf_unfiltered (gdb_stdlog
,
2804 "LLW: %s %s, %s (preempt 'handle')\n",
2806 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2807 target_pid_to_str (lp
->ptid
),
2808 signo
? strsignal (signo
) : "0");
2814 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
2816 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2817 forwarded to the entire process group, that is, all LWP's
2818 will receive it. Since we only want to report it once,
2819 we try to flush it from all LWPs except this one. */
2820 sigaddset (&flush_mask
, SIGINT
);
2824 /* This LWP is stopped now. */
2827 if (debug_linux_nat
)
2828 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
2829 status_to_str (status
), target_pid_to_str (lp
->ptid
));
2831 /* Now stop all other LWP's ... */
2832 iterate_over_lwps (stop_callback
, NULL
);
2834 /* ... and wait until all of them have reported back that they're no
2836 iterate_over_lwps (stop_wait_callback
, &flush_mask
);
2837 iterate_over_lwps (flush_callback
, &flush_mask
);
2839 /* If we're not waiting for a specific LWP, choose an event LWP from
2840 among those that have had events. Giving equal priority to all
2841 LWPs that have had events helps prevent starvation. */
2843 select_event_lwp (&lp
, &status
);
2845 /* Now that we've selected our final event LWP, cancel any
2846 breakpoints in other LWPs that have hit a GDB breakpoint. See
2847 the comment in cancel_breakpoints_callback to find out why. */
2848 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
2850 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2852 if (debug_linux_nat
)
2853 fprintf_unfiltered (gdb_stdlog
,
2854 "LLW: trap ptid is %s.\n",
2855 target_pid_to_str (lp
->ptid
));
2858 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2860 *ourstatus
= lp
->waitstatus
;
2861 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
2864 store_waitstatus (ourstatus
, status
);
2866 /* Get ready for the next event. */
2867 if (target_can_async_p ())
2868 target_async (inferior_event_handler
, 0);
2870 if (debug_linux_nat_async
)
2871 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
2877 kill_callback (struct lwp_info
*lp
, void *data
)
2880 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
2881 if (debug_linux_nat
)
2882 fprintf_unfiltered (gdb_stdlog
,
2883 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2884 target_pid_to_str (lp
->ptid
),
2885 errno
? safe_strerror (errno
) : "OK");
2891 kill_wait_callback (struct lwp_info
*lp
, void *data
)
2895 /* We must make sure that there are no pending events (delayed
2896 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2897 program doesn't interfere with any following debugging session. */
2899 /* For cloned processes we must check both with __WCLONE and
2900 without, since the exit status of a cloned process isn't reported
2906 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
2907 if (pid
!= (pid_t
) -1)
2909 if (debug_linux_nat
)
2910 fprintf_unfiltered (gdb_stdlog
,
2911 "KWC: wait %s received unknown.\n",
2912 target_pid_to_str (lp
->ptid
));
2913 /* The Linux kernel sometimes fails to kill a thread
2914 completely after PTRACE_KILL; that goes from the stop
2915 point in do_fork out to the one in
2916 get_signal_to_deliever and waits again. So kill it
2918 kill_callback (lp
, NULL
);
2921 while (pid
== GET_LWP (lp
->ptid
));
2923 gdb_assert (pid
== -1 && errno
== ECHILD
);
2928 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
2929 if (pid
!= (pid_t
) -1)
2931 if (debug_linux_nat
)
2932 fprintf_unfiltered (gdb_stdlog
,
2933 "KWC: wait %s received unk.\n",
2934 target_pid_to_str (lp
->ptid
));
2935 /* See the call to kill_callback above. */
2936 kill_callback (lp
, NULL
);
2939 while (pid
== GET_LWP (lp
->ptid
));
2941 gdb_assert (pid
== -1 && errno
== ECHILD
);
2946 linux_nat_kill (void)
2948 struct target_waitstatus last
;
2952 if (target_can_async_p ())
2953 target_async (NULL
, 0);
2955 /* If we're stopped while forking and we haven't followed yet,
2956 kill the other task. We need to do this first because the
2957 parent will be sleeping if this is a vfork. */
2959 get_last_target_status (&last_ptid
, &last
);
2961 if (last
.kind
== TARGET_WAITKIND_FORKED
2962 || last
.kind
== TARGET_WAITKIND_VFORKED
)
2964 ptrace (PT_KILL
, last
.value
.related_pid
, 0, 0);
2968 if (forks_exist_p ())
2970 linux_fork_killall ();
2971 drain_queued_events (-1);
2975 /* Kill all LWP's ... */
2976 iterate_over_lwps (kill_callback
, NULL
);
2978 /* ... and wait until we've flushed all events. */
2979 iterate_over_lwps (kill_wait_callback
, NULL
);
2982 target_mourn_inferior ();
2986 linux_nat_mourn_inferior (void)
2988 /* Destroy LWP info; it's no longer valid. */
2991 if (! forks_exist_p ())
2993 /* Normal case, no other forks available. */
2994 if (target_can_async_p ())
2995 linux_nat_async (NULL
, 0);
2996 linux_ops
->to_mourn_inferior ();
2999 /* Multi-fork case. The current inferior_ptid has exited, but
3000 there are other viable forks to debug. Delete the exiting
3001 one and context-switch to the first available. */
3002 linux_fork_mourn_inferior ();
3006 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3007 const char *annex
, gdb_byte
*readbuf
,
3008 const gdb_byte
*writebuf
,
3009 ULONGEST offset
, LONGEST len
)
3011 struct cleanup
*old_chain
= save_inferior_ptid ();
3014 if (is_lwp (inferior_ptid
))
3015 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3017 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3020 do_cleanups (old_chain
);
3025 linux_nat_thread_alive (ptid_t ptid
)
3027 gdb_assert (is_lwp (ptid
));
3030 ptrace (PTRACE_PEEKUSER
, GET_LWP (ptid
), 0, 0);
3031 if (debug_linux_nat
)
3032 fprintf_unfiltered (gdb_stdlog
,
3033 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
3034 target_pid_to_str (ptid
),
3035 errno
? safe_strerror (errno
) : "OK");
3037 /* Not every Linux kernel implements PTRACE_PEEKUSER. But we can
3038 handle that case gracefully since ptrace will first do a lookup
3039 for the process based upon the passed-in pid. If that fails we
3040 will get either -ESRCH or -EPERM, otherwise the child exists and
3042 if (errno
== ESRCH
|| errno
== EPERM
)
3049 linux_nat_pid_to_str (ptid_t ptid
)
3051 static char buf
[64];
3054 && ((lwp_list
&& lwp_list
->next
)
3055 || GET_PID (ptid
) != GET_LWP (ptid
)))
3057 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3061 return normal_pid_to_str (ptid
);
3065 sigchld_handler (int signo
)
3067 if (linux_nat_async_enabled
3068 && linux_nat_async_events_state
!= sigchld_sync
3069 && signo
== SIGCHLD
)
3070 /* It is *always* a bug to hit this. */
3071 internal_error (__FILE__
, __LINE__
,
3072 "sigchld_handler called when async events are enabled");
3074 /* Do nothing. The only reason for this handler is that it allows
3075 us to use sigsuspend in linux_nat_wait above to wait for the
3076 arrival of a SIGCHLD. */
3079 /* Accepts an integer PID; Returns a string representing a file that
3080 can be opened to get the symbols for the child process. */
3083 linux_child_pid_to_exec_file (int pid
)
3085 char *name1
, *name2
;
3087 name1
= xmalloc (MAXPATHLEN
);
3088 name2
= xmalloc (MAXPATHLEN
);
3089 make_cleanup (xfree
, name1
);
3090 make_cleanup (xfree
, name2
);
3091 memset (name2
, 0, MAXPATHLEN
);
3093 sprintf (name1
, "/proc/%d/exe", pid
);
3094 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3100 /* Service function for corefiles and info proc. */
3103 read_mapping (FILE *mapfile
,
3108 char *device
, long long *inode
, char *filename
)
3110 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3111 addr
, endaddr
, permissions
, offset
, device
, inode
);
3114 if (ret
> 0 && ret
!= EOF
)
3116 /* Eat everything up to EOL for the filename. This will prevent
3117 weird filenames (such as one with embedded whitespace) from
3118 confusing this code. It also makes this code more robust in
3119 respect to annotations the kernel may add after the filename.
3121 Note the filename is used for informational purposes
3123 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3126 return (ret
!= 0 && ret
!= EOF
);
3129 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3130 regions in the inferior for a corefile. */
3133 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3135 int, int, int, void *), void *obfd
)
3137 long long pid
= PIDGET (inferior_ptid
);
3138 char mapsfilename
[MAXPATHLEN
];
3140 long long addr
, endaddr
, size
, offset
, inode
;
3141 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3142 int read
, write
, exec
;
3145 /* Compose the filename for the /proc memory map, and open it. */
3146 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
3147 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3148 error (_("Could not open %s."), mapsfilename
);
3151 fprintf_filtered (gdb_stdout
,
3152 "Reading memory regions from %s\n", mapsfilename
);
3154 /* Now iterate until end-of-file. */
3155 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3156 &offset
, &device
[0], &inode
, &filename
[0]))
3158 size
= endaddr
- addr
;
3160 /* Get the segment's permissions. */
3161 read
= (strchr (permissions
, 'r') != 0);
3162 write
= (strchr (permissions
, 'w') != 0);
3163 exec
= (strchr (permissions
, 'x') != 0);
3167 fprintf_filtered (gdb_stdout
,
3168 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3169 size
, paddr_nz (addr
),
3171 write
? 'w' : ' ', exec
? 'x' : ' ');
3173 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3174 fprintf_filtered (gdb_stdout
, "\n");
3177 /* Invoke the callback function to create the corefile
3179 func (addr
, size
, read
, write
, exec
, obfd
);
3185 /* Records the thread's register state for the corefile note
3189 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3190 char *note_data
, int *note_size
)
3192 gdb_gregset_t gregs
;
3193 gdb_fpregset_t fpregs
;
3194 unsigned long lwp
= ptid_get_lwp (ptid
);
3195 struct regcache
*regcache
= get_thread_regcache (ptid
);
3196 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3197 const struct regset
*regset
;
3199 struct cleanup
*old_chain
;
3200 struct core_regset_section
*sect_list
;
3203 old_chain
= save_inferior_ptid ();
3204 inferior_ptid
= ptid
;
3205 target_fetch_registers (regcache
, -1);
3206 do_cleanups (old_chain
);
3208 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3209 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3212 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3213 sizeof (gregs
))) != NULL
3214 && regset
->collect_regset
!= NULL
)
3215 regset
->collect_regset (regset
, regcache
, -1,
3216 &gregs
, sizeof (gregs
));
3218 fill_gregset (regcache
, &gregs
, -1);
3220 note_data
= (char *) elfcore_write_prstatus (obfd
,
3224 stop_signal
, &gregs
);
3226 /* The loop below uses the new struct core_regset_section, which stores
3227 the supported section names and sizes for the core file. Note that
3228 note PRSTATUS needs to be treated specially. But the other notes are
3229 structurally the same, so they can benefit from the new struct. */
3230 if (core_regset_p
&& sect_list
!= NULL
)
3231 while (sect_list
->sect_name
!= NULL
)
3233 /* .reg was already handled above. */
3234 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3239 regset
= gdbarch_regset_from_core_section (gdbarch
,
3240 sect_list
->sect_name
,
3242 gdb_assert (regset
&& regset
->collect_regset
);
3243 gdb_regset
= xmalloc (sect_list
->size
);
3244 regset
->collect_regset (regset
, regcache
, -1,
3245 gdb_regset
, sect_list
->size
);
3246 note_data
= (char *) elfcore_write_register_note (obfd
,
3249 sect_list
->sect_name
,
3256 /* For architectures that does not have the struct core_regset_section
3257 implemented, we use the old method. When all the architectures have
3258 the new support, the code below should be deleted. */
3262 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3263 sizeof (fpregs
))) != NULL
3264 && regset
->collect_regset
!= NULL
)
3265 regset
->collect_regset (regset
, regcache
, -1,
3266 &fpregs
, sizeof (fpregs
));
3268 fill_fpregset (regcache
, &fpregs
, -1);
3270 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3273 &fpregs
, sizeof (fpregs
));
3279 struct linux_nat_corefile_thread_data
3287 /* Called by gdbthread.c once per thread. Records the thread's
3288 register state for the corefile note section. */
3291 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3293 struct linux_nat_corefile_thread_data
*args
= data
;
3295 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3304 /* Records the register state for the corefile note section. */
3307 linux_nat_do_registers (bfd
*obfd
, ptid_t ptid
,
3308 char *note_data
, int *note_size
)
3310 return linux_nat_do_thread_registers (obfd
,
3311 ptid_build (ptid_get_pid (inferior_ptid
),
3312 ptid_get_pid (inferior_ptid
),
3314 note_data
, note_size
);
3317 /* Fills the "to_make_corefile_note" target vector. Builds the note
3318 section for a corefile, and returns it in a malloc buffer. */
3321 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3323 struct linux_nat_corefile_thread_data thread_args
;
3324 struct cleanup
*old_chain
;
3325 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3326 char fname
[16] = { '\0' };
3327 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3328 char psargs
[80] = { '\0' };
3329 char *note_data
= NULL
;
3330 ptid_t current_ptid
= inferior_ptid
;
3334 if (get_exec_file (0))
3336 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3337 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3338 if (get_inferior_args ())
3341 char *psargs_end
= psargs
+ sizeof (psargs
);
3343 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3345 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3346 if (string_end
!= NULL
)
3348 *string_end
++ = ' ';
3349 strncpy (string_end
, get_inferior_args (),
3350 psargs_end
- string_end
);
3353 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3355 note_size
, fname
, psargs
);
3358 /* Dump information for threads. */
3359 thread_args
.obfd
= obfd
;
3360 thread_args
.note_data
= note_data
;
3361 thread_args
.note_size
= note_size
;
3362 thread_args
.num_notes
= 0;
3363 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
3364 if (thread_args
.num_notes
== 0)
3366 /* iterate_over_threads didn't come up with any threads; just
3367 use inferior_ptid. */
3368 note_data
= linux_nat_do_registers (obfd
, inferior_ptid
,
3369 note_data
, note_size
);
3373 note_data
= thread_args
.note_data
;
3376 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3380 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3381 "CORE", NT_AUXV
, auxv
, auxv_len
);
3385 make_cleanup (xfree
, note_data
);
3389 /* Implement the "info proc" command. */
3392 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3394 long long pid
= PIDGET (inferior_ptid
);
3397 char buffer
[MAXPATHLEN
];
3398 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3411 /* Break up 'args' into an argv array. */
3412 if ((argv
= buildargv (args
)) == NULL
)
3415 make_cleanup_freeargv (argv
);
3417 while (argv
!= NULL
&& *argv
!= NULL
)
3419 if (isdigit (argv
[0][0]))
3421 pid
= strtoul (argv
[0], NULL
, 10);
3423 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3427 else if (strcmp (argv
[0], "status") == 0)
3431 else if (strcmp (argv
[0], "stat") == 0)
3435 else if (strcmp (argv
[0], "cmd") == 0)
3439 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3443 else if (strcmp (argv
[0], "cwd") == 0)
3447 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3453 /* [...] (future options here) */
3458 error (_("No current process: you must name one."));
3460 sprintf (fname1
, "/proc/%lld", pid
);
3461 if (stat (fname1
, &dummy
) != 0)
3462 error (_("No /proc directory: '%s'"), fname1
);
3464 printf_filtered (_("process %lld\n"), pid
);
3465 if (cmdline_f
|| all
)
3467 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
3468 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3470 fgets (buffer
, sizeof (buffer
), procfile
);
3471 printf_filtered ("cmdline = '%s'\n", buffer
);
3475 warning (_("unable to open /proc file '%s'"), fname1
);
3479 sprintf (fname1
, "/proc/%lld/cwd", pid
);
3480 memset (fname2
, 0, sizeof (fname2
));
3481 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3482 printf_filtered ("cwd = '%s'\n", fname2
);
3484 warning (_("unable to read link '%s'"), fname1
);
3488 sprintf (fname1
, "/proc/%lld/exe", pid
);
3489 memset (fname2
, 0, sizeof (fname2
));
3490 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3491 printf_filtered ("exe = '%s'\n", fname2
);
3493 warning (_("unable to read link '%s'"), fname1
);
3495 if (mappings_f
|| all
)
3497 sprintf (fname1
, "/proc/%lld/maps", pid
);
3498 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3500 long long addr
, endaddr
, size
, offset
, inode
;
3501 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3503 printf_filtered (_("Mapped address spaces:\n\n"));
3504 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3506 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3509 " Size", " Offset", "objfile");
3513 printf_filtered (" %18s %18s %10s %10s %7s\n",
3516 " Size", " Offset", "objfile");
3519 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3520 &offset
, &device
[0], &inode
, &filename
[0]))
3522 size
= endaddr
- addr
;
3524 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3525 calls here (and possibly above) should be abstracted
3526 out into their own functions? Andrew suggests using
3527 a generic local_address_string instead to print out
3528 the addresses; that makes sense to me, too. */
3530 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3532 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3533 (unsigned long) addr
, /* FIXME: pr_addr */
3534 (unsigned long) endaddr
,
3536 (unsigned int) offset
,
3537 filename
[0] ? filename
: "");
3541 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3542 (unsigned long) addr
, /* FIXME: pr_addr */
3543 (unsigned long) endaddr
,
3545 (unsigned int) offset
,
3546 filename
[0] ? filename
: "");
3553 warning (_("unable to open /proc file '%s'"), fname1
);
3555 if (status_f
|| all
)
3557 sprintf (fname1
, "/proc/%lld/status", pid
);
3558 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3560 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3561 puts_filtered (buffer
);
3565 warning (_("unable to open /proc file '%s'"), fname1
);
3569 sprintf (fname1
, "/proc/%lld/stat", pid
);
3570 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3576 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3577 printf_filtered (_("Process: %d\n"), itmp
);
3578 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3579 printf_filtered (_("Exec file: %s\n"), buffer
);
3580 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3581 printf_filtered (_("State: %c\n"), ctmp
);
3582 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3583 printf_filtered (_("Parent process: %d\n"), itmp
);
3584 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3585 printf_filtered (_("Process group: %d\n"), itmp
);
3586 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3587 printf_filtered (_("Session id: %d\n"), itmp
);
3588 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3589 printf_filtered (_("TTY: %d\n"), itmp
);
3590 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3591 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3592 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3593 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3594 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3595 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3596 (unsigned long) ltmp
);
3597 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3598 printf_filtered (_("Minor faults, children: %lu\n"),
3599 (unsigned long) ltmp
);
3600 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3601 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3602 (unsigned long) ltmp
);
3603 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3604 printf_filtered (_("Major faults, children: %lu\n"),
3605 (unsigned long) ltmp
);
3606 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3607 printf_filtered (_("utime: %ld\n"), ltmp
);
3608 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3609 printf_filtered (_("stime: %ld\n"), ltmp
);
3610 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3611 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3612 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3613 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3614 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3615 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3617 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3618 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3619 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3620 printf_filtered (_("jiffies until next timeout: %lu\n"),
3621 (unsigned long) ltmp
);
3622 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3623 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3624 (unsigned long) ltmp
);
3625 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3626 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3628 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3629 printf_filtered (_("Virtual memory size: %lu\n"),
3630 (unsigned long) ltmp
);
3631 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3632 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3633 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3634 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3635 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3636 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3637 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3638 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3639 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3640 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3641 #if 0 /* Don't know how architecture-dependent the rest is...
3642 Anyway the signal bitmap info is available from "status". */
3643 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3644 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3645 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3646 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3647 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3648 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3649 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3650 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3651 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3652 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
3653 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3654 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
3655 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3656 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
3661 warning (_("unable to open /proc file '%s'"), fname1
);
3665 /* Implement the to_xfer_partial interface for memory reads using the /proc
3666 filesystem. Because we can use a single read() call for /proc, this
3667 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3668 but it doesn't support writes. */
3671 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3672 const char *annex
, gdb_byte
*readbuf
,
3673 const gdb_byte
*writebuf
,
3674 ULONGEST offset
, LONGEST len
)
3680 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3683 /* Don't bother for one word. */
3684 if (len
< 3 * sizeof (long))
3687 /* We could keep this file open and cache it - possibly one per
3688 thread. That requires some juggling, but is even faster. */
3689 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3690 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3694 /* If pread64 is available, use it. It's faster if the kernel
3695 supports it (only one syscall), and it's 64-bit safe even on
3696 32-bit platforms (for instance, SPARC debugging a SPARC64
3699 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3701 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3711 /* Parse LINE as a signal set and add its set bits to SIGS. */
3714 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3716 int len
= strlen (line
) - 1;
3720 if (line
[len
] != '\n')
3721 error (_("Could not parse signal set: %s"), line
);
3729 if (*p
>= '0' && *p
<= '9')
3731 else if (*p
>= 'a' && *p
<= 'f')
3732 digit
= *p
- 'a' + 10;
3734 error (_("Could not parse signal set: %s"), line
);
3739 sigaddset (sigs
, signum
+ 1);
3741 sigaddset (sigs
, signum
+ 2);
3743 sigaddset (sigs
, signum
+ 3);
3745 sigaddset (sigs
, signum
+ 4);
3751 /* Find process PID's pending signals from /proc/pid/status and set
3755 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3758 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3761 sigemptyset (pending
);
3762 sigemptyset (blocked
);
3763 sigemptyset (ignored
);
3764 sprintf (fname
, "/proc/%d/status", pid
);
3765 procfile
= fopen (fname
, "r");
3766 if (procfile
== NULL
)
3767 error (_("Could not open %s"), fname
);
3769 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3771 /* Normal queued signals are on the SigPnd line in the status
3772 file. However, 2.6 kernels also have a "shared" pending
3773 queue for delivering signals to a thread group, so check for
3776 Unfortunately some Red Hat kernels include the shared pending
3777 queue but not the ShdPnd status field. */
3779 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3780 add_line_to_sigset (buffer
+ 8, pending
);
3781 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3782 add_line_to_sigset (buffer
+ 8, pending
);
3783 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3784 add_line_to_sigset (buffer
+ 8, blocked
);
3785 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3786 add_line_to_sigset (buffer
+ 8, ignored
);
3793 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3794 const char *annex
, gdb_byte
*readbuf
,
3795 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3799 if (object
== TARGET_OBJECT_AUXV
)
3800 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3803 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3808 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3812 /* Create a prototype generic GNU/Linux target. The client can override
3813 it with local methods. */
3816 linux_target_install_ops (struct target_ops
*t
)
3818 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
3819 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
3820 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
3821 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
3822 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
3823 t
->to_post_attach
= linux_child_post_attach
;
3824 t
->to_follow_fork
= linux_child_follow_fork
;
3825 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
3826 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
3828 super_xfer_partial
= t
->to_xfer_partial
;
3829 t
->to_xfer_partial
= linux_xfer_partial
;
3835 struct target_ops
*t
;
3837 t
= inf_ptrace_target ();
3838 linux_target_install_ops (t
);
3844 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
3846 struct target_ops
*t
;
3848 t
= inf_ptrace_trad_target (register_u_offset
);
3849 linux_target_install_ops (t
);
3854 /* Controls if async mode is permitted. */
3855 static int linux_async_permitted
= 0;
3857 /* The set command writes to this variable. If the inferior is
3858 executing, linux_nat_async_permitted is *not* updated. */
3859 static int linux_async_permitted_1
= 0;
3862 set_maintenance_linux_async_permitted (char *args
, int from_tty
,
3863 struct cmd_list_element
*c
)
3865 if (target_has_execution
)
3867 linux_async_permitted_1
= linux_async_permitted
;
3868 error (_("Cannot change this setting while the inferior is running."));
3871 linux_async_permitted
= linux_async_permitted_1
;
3872 linux_nat_set_async_mode (linux_async_permitted
);
3876 show_maintenance_linux_async_permitted (struct ui_file
*file
, int from_tty
,
3877 struct cmd_list_element
*c
, const char *value
)
3879 fprintf_filtered (file
, _("\
3880 Controlling the GNU/Linux inferior in asynchronous mode is %s.\n"),
3884 /* target_is_async_p implementation. */
3887 linux_nat_is_async_p (void)
3889 /* NOTE: palves 2008-03-21: We're only async when the user requests
3890 it explicitly with the "maintenance set linux-async" command.
3891 Someday, linux will always be async. */
3892 if (!linux_async_permitted
)
3898 /* target_can_async_p implementation. */
3901 linux_nat_can_async_p (void)
3903 /* NOTE: palves 2008-03-21: We're only async when the user requests
3904 it explicitly with the "maintenance set linux-async" command.
3905 Someday, linux will always be async. */
3906 if (!linux_async_permitted
)
3909 /* See target.h/target_async_mask. */
3910 return linux_nat_async_mask_value
;
3913 /* target_async_mask implementation. */
3916 linux_nat_async_mask (int mask
)
3919 current_state
= linux_nat_async_mask_value
;
3921 if (current_state
!= mask
)
3925 linux_nat_async (NULL
, 0);
3926 linux_nat_async_mask_value
= mask
;
3930 linux_nat_async_mask_value
= mask
;
3931 linux_nat_async (inferior_event_handler
, 0);
3935 return current_state
;
3938 /* Pop an event from the event pipe. */
3941 linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
)
3943 struct waitpid_result event
= {0};
3948 ret
= read (linux_nat_event_pipe
[0], &event
, sizeof (event
));
3950 while (ret
== -1 && errno
== EINTR
);
3952 gdb_assert (ret
== sizeof (event
));
3954 *ptr_status
= event
.status
;
3955 *ptr_options
= event
.options
;
3957 linux_nat_num_queued_events
--;
3962 /* Push an event into the event pipe. */
3965 linux_nat_event_pipe_push (int pid
, int status
, int options
)
3968 struct waitpid_result event
= {0};
3970 event
.status
= status
;
3971 event
.options
= options
;
3975 ret
= write (linux_nat_event_pipe
[1], &event
, sizeof (event
));
3976 gdb_assert ((ret
== -1 && errno
== EINTR
) || ret
== sizeof (event
));
3977 } while (ret
== -1 && errno
== EINTR
);
3979 linux_nat_num_queued_events
++;
3983 get_pending_events (void)
3985 int status
, options
, pid
;
3987 if (!linux_nat_async_enabled
3988 || linux_nat_async_events_state
!= sigchld_async
)
3989 internal_error (__FILE__
, __LINE__
,
3990 "get_pending_events called with async masked");
3995 options
= __WCLONE
| WNOHANG
;
3999 pid
= waitpid (-1, &status
, options
);
4001 while (pid
== -1 && errno
== EINTR
);
4008 pid
= waitpid (-1, &status
, options
);
4010 while (pid
== -1 && errno
== EINTR
);
4014 /* No more children reporting events. */
4017 if (debug_linux_nat_async
)
4018 fprintf_unfiltered (gdb_stdlog
, "\
4019 get_pending_events: pid(%d), status(%x), options (%x)\n",
4020 pid
, status
, options
);
4022 linux_nat_event_pipe_push (pid
, status
, options
);
4025 if (debug_linux_nat_async
)
4026 fprintf_unfiltered (gdb_stdlog
, "\
4027 get_pending_events: linux_nat_num_queued_events(%d)\n",
4028 linux_nat_num_queued_events
);
4031 /* SIGCHLD handler for async mode. */
4034 async_sigchld_handler (int signo
)
4036 if (debug_linux_nat_async
)
4037 fprintf_unfiltered (gdb_stdlog
, "async_sigchld_handler\n");
4039 get_pending_events ();
4042 /* Set SIGCHLD handling state to STATE. Returns previous state. */
4044 static enum sigchld_state
4045 linux_nat_async_events (enum sigchld_state state
)
4047 enum sigchld_state current_state
= linux_nat_async_events_state
;
4049 if (debug_linux_nat_async
)
4050 fprintf_unfiltered (gdb_stdlog
,
4051 "LNAE: state(%d): linux_nat_async_events_state(%d), "
4052 "linux_nat_num_queued_events(%d)\n",
4053 state
, linux_nat_async_events_state
,
4054 linux_nat_num_queued_events
);
4056 if (current_state
!= state
)
4059 sigemptyset (&mask
);
4060 sigaddset (&mask
, SIGCHLD
);
4062 /* Always block before changing state. */
4063 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4065 /* Set new state. */
4066 linux_nat_async_events_state
= state
;
4072 /* Block target events. */
4073 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4074 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4075 /* Get events out of queue, and make them available to
4076 queued_waitpid / my_waitpid. */
4077 pipe_to_local_event_queue ();
4082 /* Unblock target events for async mode. */
4084 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4086 /* Put events we already waited on, in the pipe first, so
4088 local_event_queue_to_pipe ();
4089 /* While in masked async, we may have not collected all
4090 the pending events. Get them out now. */
4091 get_pending_events ();
4094 sigaction (SIGCHLD
, &async_sigchld_action
, NULL
);
4095 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4098 case sigchld_default
:
4100 /* SIGCHLD default mode. */
4101 sigaction (SIGCHLD
, &sigchld_default_action
, NULL
);
4103 /* Get events out of queue, and make them available to
4104 queued_waitpid / my_waitpid. */
4105 pipe_to_local_event_queue ();
4107 /* Unblock SIGCHLD. */
4108 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4114 return current_state
;
4117 static int async_terminal_is_ours
= 1;
4119 /* target_terminal_inferior implementation. */
4122 linux_nat_terminal_inferior (void)
4124 if (!target_is_async_p ())
4126 /* Async mode is disabled. */
4127 terminal_inferior ();
4131 /* GDB should never give the terminal to the inferior, if the
4132 inferior is running in the background (run&, continue&, etc.).
4133 This check can be removed when the common code is fixed. */
4134 if (!sync_execution
)
4137 terminal_inferior ();
4139 if (!async_terminal_is_ours
)
4142 delete_file_handler (input_fd
);
4143 async_terminal_is_ours
= 0;
4147 /* target_terminal_ours implementation. */
4150 linux_nat_terminal_ours (void)
4152 if (!target_is_async_p ())
4154 /* Async mode is disabled. */
4159 /* GDB should never give the terminal to the inferior if the
4160 inferior is running in the background (run&, continue&, etc.),
4161 but claiming it sure should. */
4164 if (!sync_execution
)
4167 if (async_terminal_is_ours
)
4170 clear_sigint_trap ();
4171 add_file_handler (input_fd
, stdin_event_handler
, 0);
4172 async_terminal_is_ours
= 1;
4175 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4177 static void *async_client_context
;
4180 linux_nat_async_file_handler (int error
, gdb_client_data client_data
)
4182 async_client_callback (INF_REG_EVENT
, async_client_context
);
4185 /* target_async implementation. */
4188 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4189 void *context
), void *context
)
4191 if (linux_nat_async_mask_value
== 0 || !linux_nat_async_enabled
)
4192 internal_error (__FILE__
, __LINE__
,
4193 "Calling target_async when async is masked");
4195 if (callback
!= NULL
)
4197 async_client_callback
= callback
;
4198 async_client_context
= context
;
4199 add_file_handler (linux_nat_event_pipe
[0],
4200 linux_nat_async_file_handler
, NULL
);
4202 linux_nat_async_events (sigchld_async
);
4206 async_client_callback
= callback
;
4207 async_client_context
= context
;
4209 linux_nat_async_events (sigchld_sync
);
4210 delete_file_handler (linux_nat_event_pipe
[0]);
4215 /* Enable/Disable async mode. */
4218 linux_nat_set_async_mode (int on
)
4220 if (linux_nat_async_enabled
!= on
)
4224 gdb_assert (waitpid_queue
== NULL
);
4225 if (pipe (linux_nat_event_pipe
) == -1)
4226 internal_error (__FILE__
, __LINE__
,
4227 "creating event pipe failed.");
4228 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4229 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4233 drain_queued_events (-1);
4234 linux_nat_num_queued_events
= 0;
4235 close (linux_nat_event_pipe
[0]);
4236 close (linux_nat_event_pipe
[1]);
4237 linux_nat_event_pipe
[0] = linux_nat_event_pipe
[1] = -1;
4241 linux_nat_async_enabled
= on
;
4245 linux_nat_add_target (struct target_ops
*t
)
4247 /* Save the provided single-threaded target. We save this in a separate
4248 variable because another target we've inherited from (e.g. inf-ptrace)
4249 may have saved a pointer to T; we want to use it for the final
4250 process stratum target. */
4251 linux_ops_saved
= *t
;
4252 linux_ops
= &linux_ops_saved
;
4254 /* Override some methods for multithreading. */
4255 t
->to_create_inferior
= linux_nat_create_inferior
;
4256 t
->to_attach
= linux_nat_attach
;
4257 t
->to_detach
= linux_nat_detach
;
4258 t
->to_resume
= linux_nat_resume
;
4259 t
->to_wait
= linux_nat_wait
;
4260 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4261 t
->to_kill
= linux_nat_kill
;
4262 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4263 t
->to_thread_alive
= linux_nat_thread_alive
;
4264 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4265 t
->to_has_thread_control
= tc_schedlock
;
4267 t
->to_can_async_p
= linux_nat_can_async_p
;
4268 t
->to_is_async_p
= linux_nat_is_async_p
;
4269 t
->to_async
= linux_nat_async
;
4270 t
->to_async_mask
= linux_nat_async_mask
;
4271 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4272 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4274 /* We don't change the stratum; this target will sit at
4275 process_stratum and thread_db will set at thread_stratum. This
4276 is a little strange, since this is a multi-threaded-capable
4277 target, but we want to be on the stack below thread_db, and we
4278 also want to be used for single-threaded processes. */
4282 /* TODO: Eliminate this and have libthread_db use
4283 find_target_beneath. */
4287 /* Register a method to call whenever a new thread is attached. */
4289 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4291 /* Save the pointer. We only support a single registered instance
4292 of the GNU/Linux native target, so we do not need to map this to
4294 linux_nat_new_thread
= new_thread
;
4297 /* Return the saved siginfo associated with PTID. */
4299 linux_nat_get_siginfo (ptid_t ptid
)
4301 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4303 gdb_assert (lp
!= NULL
);
4305 return &lp
->siginfo
;
4309 _initialize_linux_nat (void)
4313 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4314 Show /proc process information about any running process.\n\
4315 Specify any process id, or use the program being debugged by default.\n\
4316 Specify any of the following keywords for detailed info:\n\
4317 mappings -- list of mapped memory regions.\n\
4318 stat -- list a bunch of random process info.\n\
4319 status -- list a different bunch of random process info.\n\
4320 all -- list all available /proc info."));
4322 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4323 &debug_linux_nat
, _("\
4324 Set debugging of GNU/Linux lwp module."), _("\
4325 Show debugging of GNU/Linux lwp module."), _("\
4326 Enables printf debugging output."),
4328 show_debug_linux_nat
,
4329 &setdebuglist
, &showdebuglist
);
4331 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4332 &debug_linux_nat_async
, _("\
4333 Set debugging of GNU/Linux async lwp module."), _("\
4334 Show debugging of GNU/Linux async lwp module."), _("\
4335 Enables printf debugging output."),
4337 show_debug_linux_nat_async
,
4338 &setdebuglist
, &showdebuglist
);
4340 add_setshow_boolean_cmd ("linux-async", class_maintenance
,
4341 &linux_async_permitted_1
, _("\
4342 Set whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\
4343 Show whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\
4344 Tells gdb whether to control the GNU/Linux inferior in asynchronous mode."),
4345 set_maintenance_linux_async_permitted
,
4346 show_maintenance_linux_async_permitted
,
4347 &maintenance_set_cmdlist
,
4348 &maintenance_show_cmdlist
);
4350 /* Get the default SIGCHLD action. Used while forking an inferior
4351 (see linux_nat_create_inferior/linux_nat_async_events). */
4352 sigaction (SIGCHLD
, NULL
, &sigchld_default_action
);
4354 /* Block SIGCHLD by default. Doing this early prevents it getting
4355 unblocked if an exception is thrown due to an error while the
4356 inferior is starting (sigsetjmp/siglongjmp). */
4357 sigemptyset (&mask
);
4358 sigaddset (&mask
, SIGCHLD
);
4359 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4361 /* Save this mask as the default. */
4362 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4364 /* The synchronous SIGCHLD handler. */
4365 sync_sigchld_action
.sa_handler
= sigchld_handler
;
4366 sigemptyset (&sync_sigchld_action
.sa_mask
);
4367 sync_sigchld_action
.sa_flags
= SA_RESTART
;
4369 /* Make it the default. */
4370 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4372 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4373 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4374 sigdelset (&suspend_mask
, SIGCHLD
);
4376 /* SIGCHLD handler for async mode. */
4377 async_sigchld_action
.sa_handler
= async_sigchld_handler
;
4378 sigemptyset (&async_sigchld_action
.sa_mask
);
4379 async_sigchld_action
.sa_flags
= SA_RESTART
;
4381 /* Install the default mode. */
4382 linux_nat_set_async_mode (linux_async_permitted
);
4386 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4387 the GNU/Linux Threads library and therefore doesn't really belong
4390 /* Read variable NAME in the target and return its value if found.
4391 Otherwise return zero. It is assumed that the type of the variable
4395 get_signo (const char *name
)
4397 struct minimal_symbol
*ms
;
4400 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4404 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4405 sizeof (signo
)) != 0)
4411 /* Return the set of signals used by the threads library in *SET. */
4414 lin_thread_get_thread_signals (sigset_t
*set
)
4416 struct sigaction action
;
4417 int restart
, cancel
;
4418 sigset_t blocked_mask
;
4420 sigemptyset (&blocked_mask
);
4423 restart
= get_signo ("__pthread_sig_restart");
4424 cancel
= get_signo ("__pthread_sig_cancel");
4426 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4427 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4428 not provide any way for the debugger to query the signal numbers -
4429 fortunately they don't change! */
4432 restart
= __SIGRTMIN
;
4435 cancel
= __SIGRTMIN
+ 1;
4437 sigaddset (set
, restart
);
4438 sigaddset (set
, cancel
);
4440 /* The GNU/Linux Threads library makes terminating threads send a
4441 special "cancel" signal instead of SIGCHLD. Make sure we catch
4442 those (to prevent them from terminating GDB itself, which is
4443 likely to be their default action) and treat them the same way as
4446 action
.sa_handler
= sigchld_handler
;
4447 sigemptyset (&action
.sa_mask
);
4448 action
.sa_flags
= SA_RESTART
;
4449 sigaction (cancel
, &action
, NULL
);
4451 /* We block the "cancel" signal throughout this code ... */
4452 sigaddset (&blocked_mask
, cancel
);
4453 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4455 /* ... except during a sigsuspend. */
4456 sigdelset (&suspend_mask
, cancel
);