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 #include <sys/types.h>
54 #include "gdb_dirent.h"
55 #include "xml-support.h"
57 #ifdef HAVE_PERSONALITY
58 # include <sys/personality.h>
59 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
60 # define ADDR_NO_RANDOMIZE 0x0040000
62 #endif /* HAVE_PERSONALITY */
64 /* This comment documents high-level logic of this file.
66 Waiting for events in sync mode
67 ===============================
69 When waiting for an event in a specific thread, we just use waitpid, passing
70 the specific pid, and not passing WNOHANG.
72 When waiting for an event in all threads, waitpid is not quite good. Prior to
73 version 2.4, Linux can either wait for event in main thread, or in secondary
74 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
75 miss an event. The solution is to use non-blocking waitpid, together with
76 sigsuspend. First, we use non-blocking waitpid to get an event in the main
77 process, if any. Second, we use non-blocking waitpid with the __WCLONED
78 flag to check for events in cloned processes. If nothing is found, we use
79 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
80 happened to a child process -- and SIGCHLD will be delivered both for events
81 in main debugged process and in cloned processes. As soon as we know there's
82 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
84 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
85 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
86 blocked, the signal becomes pending and sigsuspend immediately
87 notices it and returns.
89 Waiting for events in async mode
90 ================================
92 In async mode, GDB should always be ready to handle both user input and target
93 events, so neither blocking waitpid nor sigsuspend are viable
94 options. Instead, we should notify the GDB main event loop whenever there's
95 unprocessed event from the target. The only way to notify this event loop is
96 to make it wait on input from a pipe, and write something to the pipe whenever
97 there's event. Obviously, if we fail to notify the event loop if there's
98 target event, it's bad. If we notify the event loop when there's no event
99 from target, linux-nat.c will detect that there's no event, actually, and
100 report event of type TARGET_WAITKIND_IGNORE, but it will waste time and
103 The main design point is that every time GDB is outside linux-nat.c, we have a
104 SIGCHLD handler installed that is called when something happens to the target
105 and notifies the GDB event loop. Also, the event is extracted from the target
106 using waitpid and stored for future use. Whenever GDB core decides to handle
107 the event, and calls into linux-nat.c, we disable SIGCHLD and process things
108 as in sync mode, except that before waitpid call we check if there are any
109 previously read events.
111 It could happen that during event processing, we'll try to get more events
112 than there are events in the local queue, which will result to waitpid call.
113 Those waitpid calls, while blocking, are guarantied to always have
114 something for waitpid to return. E.g., stopping a thread with SIGSTOP, and
115 waiting for the lwp to stop.
117 The event loop is notified about new events using a pipe. SIGCHLD handler does
118 waitpid and writes the results in to a pipe. GDB event loop has the other end
119 of the pipe among the sources. When event loop starts to process the event
120 and calls a function in linux-nat.c, all events from the pipe are transferred
121 into a local queue and SIGCHLD is blocked. Further processing goes as in sync
122 mode. Before we return from linux_nat_wait, we transfer all unprocessed events
123 from local queue back to the pipe, so that when we get back to event loop,
124 event loop will notice there's something more to do.
126 SIGCHLD is blocked when we're inside target_wait, so that should we actually
127 want to wait for some more events, SIGCHLD handler does not steal them from
128 us. Technically, it would be possible to add new events to the local queue but
129 it's about the same amount of work as blocking SIGCHLD.
131 This moving of events from pipe into local queue and back into pipe when we
132 enter/leave linux-nat.c is somewhat ugly. Unfortunately, GDB event loop is
133 home-grown and incapable to wait on any queue.
138 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
139 signal is not entirely significant; we just need for a signal to be delivered,
140 so that we can intercept it. SIGSTOP's advantage is that it can not be
141 blocked. A disadvantage is that it is not a real-time signal, so it can only
142 be queued once; we do not keep track of other sources of SIGSTOP.
144 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
145 use them, because they have special behavior when the signal is generated -
146 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
147 kills the entire thread group.
149 A delivered SIGSTOP would stop the entire thread group, not just the thread we
150 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
151 cancel it (by PTRACE_CONT without passing SIGSTOP).
153 We could use a real-time signal instead. This would solve those problems; we
154 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
155 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
156 generates it, and there are races with trying to find a signal that is not
160 #define O_LARGEFILE 0
163 /* If the system headers did not provide the constants, hard-code the normal
165 #ifndef PTRACE_EVENT_FORK
167 #define PTRACE_SETOPTIONS 0x4200
168 #define PTRACE_GETEVENTMSG 0x4201
170 /* options set using PTRACE_SETOPTIONS */
171 #define PTRACE_O_TRACESYSGOOD 0x00000001
172 #define PTRACE_O_TRACEFORK 0x00000002
173 #define PTRACE_O_TRACEVFORK 0x00000004
174 #define PTRACE_O_TRACECLONE 0x00000008
175 #define PTRACE_O_TRACEEXEC 0x00000010
176 #define PTRACE_O_TRACEVFORKDONE 0x00000020
177 #define PTRACE_O_TRACEEXIT 0x00000040
179 /* Wait extended result codes for the above trace options. */
180 #define PTRACE_EVENT_FORK 1
181 #define PTRACE_EVENT_VFORK 2
182 #define PTRACE_EVENT_CLONE 3
183 #define PTRACE_EVENT_EXEC 4
184 #define PTRACE_EVENT_VFORK_DONE 5
185 #define PTRACE_EVENT_EXIT 6
187 #endif /* PTRACE_EVENT_FORK */
189 /* We can't always assume that this flag is available, but all systems
190 with the ptrace event handlers also have __WALL, so it's safe to use
193 #define __WALL 0x40000000 /* Wait for any child. */
196 #ifndef PTRACE_GETSIGINFO
197 #define PTRACE_GETSIGINFO 0x4202
200 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
201 the use of the multi-threaded target. */
202 static struct target_ops
*linux_ops
;
203 static struct target_ops linux_ops_saved
;
205 /* The method to call, if any, when a new thread is attached. */
206 static void (*linux_nat_new_thread
) (ptid_t
);
208 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
209 Called by our to_xfer_partial. */
210 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
212 const char *, gdb_byte
*,
216 static int debug_linux_nat
;
218 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
219 struct cmd_list_element
*c
, const char *value
)
221 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
225 static int debug_linux_nat_async
= 0;
227 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
228 struct cmd_list_element
*c
, const char *value
)
230 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
234 static int disable_randomization
= 1;
237 show_disable_randomization (struct ui_file
*file
, int from_tty
,
238 struct cmd_list_element
*c
, const char *value
)
240 #ifdef HAVE_PERSONALITY
241 fprintf_filtered (file
, _("\
242 Disabling randomization of debuggee's virtual address space is %s.\n"),
244 #else /* !HAVE_PERSONALITY */
246 Disabling randomization of debuggee's virtual address space is unsupported on\n\
247 this platform.\n"), file
);
248 #endif /* !HAVE_PERSONALITY */
252 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
254 #ifndef HAVE_PERSONALITY
256 Disabling randomization of debuggee's virtual address space is unsupported on\n\
258 #endif /* !HAVE_PERSONALITY */
261 static int linux_parent_pid
;
263 struct simple_pid_list
267 struct simple_pid_list
*next
;
269 struct simple_pid_list
*stopped_pids
;
271 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
272 can not be used, 1 if it can. */
274 static int linux_supports_tracefork_flag
= -1;
276 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
277 PTRACE_O_TRACEVFORKDONE. */
279 static int linux_supports_tracevforkdone_flag
= -1;
281 /* Async mode support */
283 /* Zero if the async mode, although enabled, is masked, which means
284 linux_nat_wait should behave as if async mode was off. */
285 static int linux_nat_async_mask_value
= 1;
287 /* The read/write ends of the pipe registered as waitable file in the
289 static int linux_nat_event_pipe
[2] = { -1, -1 };
291 /* Number of queued events in the pipe. */
292 static volatile int linux_nat_num_queued_events
;
294 /* The possible SIGCHLD handling states. */
298 /* SIGCHLD disabled, with action set to sigchld_handler, for the
299 sigsuspend in linux_nat_wait. */
301 /* SIGCHLD enabled, with action set to async_sigchld_handler. */
303 /* Set SIGCHLD to default action. Used while creating an
308 /* The current SIGCHLD handling state. */
309 static enum sigchld_state linux_nat_async_events_state
;
311 static enum sigchld_state
linux_nat_async_events (enum sigchld_state enable
);
312 static void pipe_to_local_event_queue (void);
313 static void local_event_queue_to_pipe (void);
314 static void linux_nat_event_pipe_push (int pid
, int status
, int options
);
315 static int linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
);
316 static void linux_nat_set_async_mode (int on
);
317 static void linux_nat_async (void (*callback
)
318 (enum inferior_event_type event_type
, void *context
),
320 static int linux_nat_async_mask (int mask
);
321 static int kill_lwp (int lwpid
, int signo
);
323 static int stop_callback (struct lwp_info
*lp
, void *data
);
325 /* Captures the result of a successful waitpid call, along with the
326 options used in that call. */
327 struct waitpid_result
332 struct waitpid_result
*next
;
335 /* A singly-linked list of the results of the waitpid calls performed
336 in the async SIGCHLD handler. */
337 static struct waitpid_result
*waitpid_queue
= NULL
;
339 /* Similarly to `waitpid', but check the local event queue instead of
340 querying the kernel queue. If PEEK, don't remove the event found
344 queued_waitpid_1 (int pid
, int *status
, int flags
, int peek
)
346 struct waitpid_result
*msg
= waitpid_queue
, *prev
= NULL
;
348 if (debug_linux_nat_async
)
349 fprintf_unfiltered (gdb_stdlog
,
351 QWPID: linux_nat_async_events_state(%d), linux_nat_num_queued_events(%d)\n",
352 linux_nat_async_events_state
,
353 linux_nat_num_queued_events
);
357 for (; msg
; prev
= msg
, msg
= msg
->next
)
358 if (pid
== -1 || pid
== msg
->pid
)
361 else if (flags
& __WCLONE
)
363 for (; msg
; prev
= msg
, msg
= msg
->next
)
364 if (msg
->options
& __WCLONE
365 && (pid
== -1 || pid
== msg
->pid
))
370 for (; msg
; prev
= msg
, msg
= msg
->next
)
371 if ((msg
->options
& __WCLONE
) == 0
372 && (pid
== -1 || pid
== msg
->pid
))
381 *status
= msg
->status
;
384 if (debug_linux_nat_async
)
385 fprintf_unfiltered (gdb_stdlog
, "QWPID: pid(%d), status(%x)\n",
391 prev
->next
= msg
->next
;
393 waitpid_queue
= msg
->next
;
402 if (debug_linux_nat_async
)
403 fprintf_unfiltered (gdb_stdlog
, "QWPID: miss\n");
410 /* Similarly to `waitpid', but check the local event queue. */
413 queued_waitpid (int pid
, int *status
, int flags
)
415 return queued_waitpid_1 (pid
, status
, flags
, 0);
419 push_waitpid (int pid
, int status
, int options
)
421 struct waitpid_result
*event
, *new_event
;
423 new_event
= xmalloc (sizeof (*new_event
));
424 new_event
->pid
= pid
;
425 new_event
->status
= status
;
426 new_event
->options
= options
;
427 new_event
->next
= NULL
;
431 for (event
= waitpid_queue
;
432 event
&& event
->next
;
436 event
->next
= new_event
;
439 waitpid_queue
= new_event
;
442 /* Drain all queued events of PID. If PID is -1, the effect is of
443 draining all events. */
445 drain_queued_events (int pid
)
447 while (queued_waitpid (pid
, NULL
, __WALL
) != -1)
452 /* Trivial list manipulation functions to keep track of a list of
453 new stopped processes. */
455 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
457 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
459 new_pid
->status
= status
;
460 new_pid
->next
= *listp
;
465 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
467 struct simple_pid_list
**p
;
469 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
470 if ((*p
)->pid
== pid
)
472 struct simple_pid_list
*next
= (*p
)->next
;
473 *status
= (*p
)->status
;
482 linux_record_stopped_pid (int pid
, int status
)
484 add_to_pid_list (&stopped_pids
, pid
, status
);
488 /* A helper function for linux_test_for_tracefork, called after fork (). */
491 linux_tracefork_child (void)
495 ptrace (PTRACE_TRACEME
, 0, 0, 0);
496 kill (getpid (), SIGSTOP
);
501 /* Wrapper function for waitpid which handles EINTR, and checks for
502 locally queued events. */
505 my_waitpid (int pid
, int *status
, int flags
)
509 /* There should be no concurrent calls to waitpid. */
510 gdb_assert (linux_nat_async_events_state
== sigchld_sync
);
512 ret
= queued_waitpid (pid
, status
, flags
);
518 ret
= waitpid (pid
, status
, flags
);
520 while (ret
== -1 && errno
== EINTR
);
525 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
527 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
528 we know that the feature is not available. This may change the tracing
529 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
531 However, if it succeeds, we don't know for sure that the feature is
532 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
533 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
534 fork tracing, and let it fork. If the process exits, we assume that we
535 can't use TRACEFORK; if we get the fork notification, and we can extract
536 the new child's PID, then we assume that we can. */
539 linux_test_for_tracefork (int original_pid
)
541 int child_pid
, ret
, status
;
543 enum sigchld_state async_events_original_state
;
545 async_events_original_state
= linux_nat_async_events (sigchld_sync
);
547 linux_supports_tracefork_flag
= 0;
548 linux_supports_tracevforkdone_flag
= 0;
550 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
556 perror_with_name (("fork"));
559 linux_tracefork_child ();
561 ret
= my_waitpid (child_pid
, &status
, 0);
563 perror_with_name (("waitpid"));
564 else if (ret
!= child_pid
)
565 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
566 if (! WIFSTOPPED (status
))
567 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
569 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
572 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
575 warning (_("linux_test_for_tracefork: failed to kill child"));
576 linux_nat_async_events (async_events_original_state
);
580 ret
= my_waitpid (child_pid
, &status
, 0);
581 if (ret
!= child_pid
)
582 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
583 else if (!WIFSIGNALED (status
))
584 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
585 "killed child"), status
);
587 linux_nat_async_events (async_events_original_state
);
591 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
592 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
593 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
594 linux_supports_tracevforkdone_flag
= (ret
== 0);
596 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
598 warning (_("linux_test_for_tracefork: failed to resume child"));
600 ret
= my_waitpid (child_pid
, &status
, 0);
602 if (ret
== child_pid
&& WIFSTOPPED (status
)
603 && status
>> 16 == PTRACE_EVENT_FORK
)
606 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
607 if (ret
== 0 && second_pid
!= 0)
611 linux_supports_tracefork_flag
= 1;
612 my_waitpid (second_pid
, &second_status
, 0);
613 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
615 warning (_("linux_test_for_tracefork: failed to kill second child"));
616 my_waitpid (second_pid
, &status
, 0);
620 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
621 "(%d, status 0x%x)"), ret
, status
);
623 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
625 warning (_("linux_test_for_tracefork: failed to kill child"));
626 my_waitpid (child_pid
, &status
, 0);
628 linux_nat_async_events (async_events_original_state
);
631 /* Return non-zero iff we have tracefork functionality available.
632 This function also sets linux_supports_tracefork_flag. */
635 linux_supports_tracefork (int pid
)
637 if (linux_supports_tracefork_flag
== -1)
638 linux_test_for_tracefork (pid
);
639 return linux_supports_tracefork_flag
;
643 linux_supports_tracevforkdone (int pid
)
645 if (linux_supports_tracefork_flag
== -1)
646 linux_test_for_tracefork (pid
);
647 return linux_supports_tracevforkdone_flag
;
652 linux_enable_event_reporting (ptid_t ptid
)
654 int pid
= ptid_get_lwp (ptid
);
658 pid
= ptid_get_pid (ptid
);
660 if (! linux_supports_tracefork (pid
))
663 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
664 | PTRACE_O_TRACECLONE
;
665 if (linux_supports_tracevforkdone (pid
))
666 options
|= PTRACE_O_TRACEVFORKDONE
;
668 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
669 read-only process state. */
671 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
675 linux_child_post_attach (int pid
)
677 linux_enable_event_reporting (pid_to_ptid (pid
));
678 check_for_thread_db ();
682 linux_child_post_startup_inferior (ptid_t ptid
)
684 linux_enable_event_reporting (ptid
);
685 check_for_thread_db ();
689 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
692 struct target_waitstatus last_status
;
694 int parent_pid
, child_pid
;
696 if (target_can_async_p ())
697 target_async (NULL
, 0);
699 get_last_target_status (&last_ptid
, &last_status
);
700 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
701 parent_pid
= ptid_get_lwp (last_ptid
);
703 parent_pid
= ptid_get_pid (last_ptid
);
704 child_pid
= PIDGET (last_status
.value
.related_pid
);
708 /* We're already attached to the parent, by default. */
710 /* Before detaching from the child, remove all breakpoints from
711 it. (This won't actually modify the breakpoint list, but will
712 physically remove the breakpoints from the child.) */
713 /* If we vforked this will remove the breakpoints from the parent
714 also, but they'll be reinserted below. */
715 detach_breakpoints (child_pid
);
717 /* Detach new forked process? */
720 if (info_verbose
|| debug_linux_nat
)
722 target_terminal_ours ();
723 fprintf_filtered (gdb_stdlog
,
724 "Detaching after fork from child process %d.\n",
728 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
732 struct fork_info
*fp
;
734 /* Add process to GDB's tables. */
735 add_inferior (child_pid
);
737 /* Retain child fork in ptrace (stopped) state. */
738 fp
= find_fork_pid (child_pid
);
740 fp
= add_fork (child_pid
);
741 fork_save_infrun_state (fp
, 0);
746 gdb_assert (linux_supports_tracefork_flag
>= 0);
747 if (linux_supports_tracevforkdone (0))
751 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
752 my_waitpid (parent_pid
, &status
, __WALL
);
753 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
754 warning (_("Unexpected waitpid result %06x when waiting for "
755 "vfork-done"), status
);
759 /* We can't insert breakpoints until the child has
760 finished with the shared memory region. We need to
761 wait until that happens. Ideal would be to just
763 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
764 - waitpid (parent_pid, &status, __WALL);
765 However, most architectures can't handle a syscall
766 being traced on the way out if it wasn't traced on
769 We might also think to loop, continuing the child
770 until it exits or gets a SIGTRAP. One problem is
771 that the child might call ptrace with PTRACE_TRACEME.
773 There's no simple and reliable way to figure out when
774 the vforked child will be done with its copy of the
775 shared memory. We could step it out of the syscall,
776 two instructions, let it go, and then single-step the
777 parent once. When we have hardware single-step, this
778 would work; with software single-step it could still
779 be made to work but we'd have to be able to insert
780 single-step breakpoints in the child, and we'd have
781 to insert -just- the single-step breakpoint in the
782 parent. Very awkward.
784 In the end, the best we can do is to make sure it
785 runs for a little while. Hopefully it will be out of
786 range of any breakpoints we reinsert. Usually this
787 is only the single-step breakpoint at vfork's return
793 /* Since we vforked, breakpoints were removed in the parent
794 too. Put them back. */
795 reattach_breakpoints (parent_pid
);
800 struct thread_info
*last_tp
= find_thread_pid (last_ptid
);
801 struct thread_info
*tp
;
802 char child_pid_spelling
[40];
804 /* Copy user stepping state to the new inferior thread. */
805 struct breakpoint
*step_resume_breakpoint
= last_tp
->step_resume_breakpoint
;
806 CORE_ADDR step_range_start
= last_tp
->step_range_start
;
807 CORE_ADDR step_range_end
= last_tp
->step_range_end
;
808 struct frame_id step_frame_id
= last_tp
->step_frame_id
;
810 /* Otherwise, deleting the parent would get rid of this
812 last_tp
->step_resume_breakpoint
= NULL
;
814 /* Needed to keep the breakpoint lists in sync. */
816 detach_breakpoints (child_pid
);
818 /* Before detaching from the parent, remove all breakpoints from it. */
819 remove_breakpoints ();
821 if (info_verbose
|| debug_linux_nat
)
823 target_terminal_ours ();
824 fprintf_filtered (gdb_stdlog
,
825 "Attaching after fork to child process %d.\n",
829 /* If we're vforking, we may want to hold on to the parent until
830 the child exits or execs. At exec time we can remove the old
831 breakpoints from the parent and detach it; at exit time we
832 could do the same (or even, sneakily, resume debugging it - the
833 child's exec has failed, or something similar).
835 This doesn't clean up "properly", because we can't call
836 target_detach, but that's OK; if the current target is "child",
837 then it doesn't need any further cleanups, and lin_lwp will
838 generally not encounter vfork (vfork is defined to fork
841 The holding part is very easy if we have VFORKDONE events;
842 but keeping track of both processes is beyond GDB at the
843 moment. So we don't expose the parent to the rest of GDB.
844 Instead we quietly hold onto it until such time as we can
849 linux_parent_pid
= parent_pid
;
850 detach_inferior (parent_pid
);
852 else if (!detach_fork
)
854 struct fork_info
*fp
;
855 /* Retain parent fork in ptrace (stopped) state. */
856 fp
= find_fork_pid (parent_pid
);
858 fp
= add_fork (parent_pid
);
859 fork_save_infrun_state (fp
, 0);
861 /* Also add an entry for the child fork. */
862 fp
= find_fork_pid (child_pid
);
864 fp
= add_fork (child_pid
);
865 fork_save_infrun_state (fp
, 0);
868 target_detach (NULL
, 0);
870 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
871 add_inferior (child_pid
);
873 /* Reinstall ourselves, since we might have been removed in
874 target_detach (which does other necessary cleanup). */
877 linux_nat_switch_fork (inferior_ptid
);
878 check_for_thread_db ();
880 tp
= inferior_thread ();
881 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
882 tp
->step_range_start
= step_range_start
;
883 tp
->step_range_end
= step_range_end
;
884 tp
->step_frame_id
= step_frame_id
;
886 /* Reset breakpoints in the child as appropriate. */
887 follow_inferior_reset_breakpoints ();
890 if (target_can_async_p ())
891 target_async (inferior_event_handler
, 0);
898 linux_child_insert_fork_catchpoint (int pid
)
900 if (! linux_supports_tracefork (pid
))
901 error (_("Your system does not support fork catchpoints."));
905 linux_child_insert_vfork_catchpoint (int pid
)
907 if (!linux_supports_tracefork (pid
))
908 error (_("Your system does not support vfork catchpoints."));
912 linux_child_insert_exec_catchpoint (int pid
)
914 if (!linux_supports_tracefork (pid
))
915 error (_("Your system does not support exec catchpoints."));
918 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
919 are processes sharing the same VM space. A multi-threaded process
920 is basically a group of such processes. However, such a grouping
921 is almost entirely a user-space issue; the kernel doesn't enforce
922 such a grouping at all (this might change in the future). In
923 general, we'll rely on the threads library (i.e. the GNU/Linux
924 Threads library) to provide such a grouping.
926 It is perfectly well possible to write a multi-threaded application
927 without the assistance of a threads library, by using the clone
928 system call directly. This module should be able to give some
929 rudimentary support for debugging such applications if developers
930 specify the CLONE_PTRACE flag in the clone system call, and are
931 using the Linux kernel 2.4 or above.
933 Note that there are some peculiarities in GNU/Linux that affect
936 - In general one should specify the __WCLONE flag to waitpid in
937 order to make it report events for any of the cloned processes
938 (and leave it out for the initial process). However, if a cloned
939 process has exited the exit status is only reported if the
940 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
941 we cannot use it since GDB must work on older systems too.
943 - When a traced, cloned process exits and is waited for by the
944 debugger, the kernel reassigns it to the original parent and
945 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
946 library doesn't notice this, which leads to the "zombie problem":
947 When debugged a multi-threaded process that spawns a lot of
948 threads will run out of processes, even if the threads exit,
949 because the "zombies" stay around. */
951 /* List of known LWPs. */
952 struct lwp_info
*lwp_list
;
954 /* Number of LWPs in the list. */
958 /* Original signal mask. */
959 static sigset_t normal_mask
;
961 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
962 _initialize_linux_nat. */
963 static sigset_t suspend_mask
;
965 /* SIGCHLD action for synchronous mode. */
966 struct sigaction sync_sigchld_action
;
968 /* SIGCHLD action for asynchronous mode. */
969 static struct sigaction async_sigchld_action
;
971 /* SIGCHLD default action, to pass to new inferiors. */
972 static struct sigaction sigchld_default_action
;
975 /* Prototypes for local functions. */
976 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
977 static int linux_nat_thread_alive (ptid_t ptid
);
978 static char *linux_child_pid_to_exec_file (int pid
);
979 static int cancel_breakpoint (struct lwp_info
*lp
);
982 /* Convert wait status STATUS to a string. Used for printing debug
986 status_to_str (int status
)
990 if (WIFSTOPPED (status
))
991 snprintf (buf
, sizeof (buf
), "%s (stopped)",
992 strsignal (WSTOPSIG (status
)));
993 else if (WIFSIGNALED (status
))
994 snprintf (buf
, sizeof (buf
), "%s (terminated)",
995 strsignal (WSTOPSIG (status
)));
997 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1002 /* Initialize the list of LWPs. Note that this module, contrary to
1003 what GDB's generic threads layer does for its thread list,
1004 re-initializes the LWP lists whenever we mourn or detach (which
1005 doesn't involve mourning) the inferior. */
1008 init_lwp_list (void)
1010 struct lwp_info
*lp
, *lpnext
;
1012 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1022 /* Add the LWP specified by PID to the list. Return a pointer to the
1023 structure describing the new LWP. The LWP should already be stopped
1024 (with an exception for the very first LWP). */
1026 static struct lwp_info
*
1027 add_lwp (ptid_t ptid
)
1029 struct lwp_info
*lp
;
1031 gdb_assert (is_lwp (ptid
));
1033 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1035 memset (lp
, 0, sizeof (struct lwp_info
));
1037 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1041 lp
->next
= lwp_list
;
1045 if (num_lwps
> 1 && linux_nat_new_thread
!= NULL
)
1046 linux_nat_new_thread (ptid
);
1051 /* Remove the LWP specified by PID from the list. */
1054 delete_lwp (ptid_t ptid
)
1056 struct lwp_info
*lp
, *lpprev
;
1060 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1061 if (ptid_equal (lp
->ptid
, ptid
))
1070 lpprev
->next
= lp
->next
;
1072 lwp_list
= lp
->next
;
1077 /* Return a pointer to the structure describing the LWP corresponding
1078 to PID. If no corresponding LWP could be found, return NULL. */
1080 static struct lwp_info
*
1081 find_lwp_pid (ptid_t ptid
)
1083 struct lwp_info
*lp
;
1087 lwp
= GET_LWP (ptid
);
1089 lwp
= GET_PID (ptid
);
1091 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1092 if (lwp
== GET_LWP (lp
->ptid
))
1098 /* Call CALLBACK with its second argument set to DATA for every LWP in
1099 the list. If CALLBACK returns 1 for a particular LWP, return a
1100 pointer to the structure describing that LWP immediately.
1101 Otherwise return NULL. */
1104 iterate_over_lwps (int (*callback
) (struct lwp_info
*, void *), void *data
)
1106 struct lwp_info
*lp
, *lpnext
;
1108 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1111 if ((*callback
) (lp
, data
))
1118 /* Update our internal state when changing from one fork (checkpoint,
1119 et cetera) to another indicated by NEW_PTID. We can only switch
1120 single-threaded applications, so we only create one new LWP, and
1121 the previous list is discarded. */
1124 linux_nat_switch_fork (ptid_t new_ptid
)
1126 struct lwp_info
*lp
;
1129 lp
= add_lwp (new_ptid
);
1132 init_thread_list ();
1133 add_thread_silent (new_ptid
);
1136 /* Handle the exit of a single thread LP. */
1139 exit_lwp (struct lwp_info
*lp
)
1141 struct thread_info
*th
= find_thread_pid (lp
->ptid
);
1145 if (print_thread_events
)
1146 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1148 delete_thread (lp
->ptid
);
1151 delete_lwp (lp
->ptid
);
1154 /* Detect `T (stopped)' in `/proc/PID/status'.
1155 Other states including `T (tracing stop)' are reported as false. */
1158 pid_is_stopped (pid_t pid
)
1164 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1165 status_file
= fopen (buf
, "r");
1166 if (status_file
!= NULL
)
1170 while (fgets (buf
, sizeof (buf
), status_file
))
1172 if (strncmp (buf
, "State:", 6) == 0)
1178 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1180 fclose (status_file
);
1185 /* Wait for the LWP specified by LP, which we have just attached to.
1186 Returns a wait status for that LWP, to cache. */
1189 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1192 pid_t new_pid
, pid
= GET_LWP (ptid
);
1195 if (pid_is_stopped (pid
))
1197 if (debug_linux_nat
)
1198 fprintf_unfiltered (gdb_stdlog
,
1199 "LNPAW: Attaching to a stopped process\n");
1201 /* The process is definitely stopped. It is in a job control
1202 stop, unless the kernel predates the TASK_STOPPED /
1203 TASK_TRACED distinction, in which case it might be in a
1204 ptrace stop. Make sure it is in a ptrace stop; from there we
1205 can kill it, signal it, et cetera.
1207 First make sure there is a pending SIGSTOP. Since we are
1208 already attached, the process can not transition from stopped
1209 to running without a PTRACE_CONT; so we know this signal will
1210 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1211 probably already in the queue (unless this kernel is old
1212 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1213 is not an RT signal, it can only be queued once. */
1214 kill_lwp (pid
, SIGSTOP
);
1216 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1217 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1218 ptrace (PTRACE_CONT
, pid
, 0, 0);
1221 /* Make sure the initial process is stopped. The user-level threads
1222 layer might want to poke around in the inferior, and that won't
1223 work if things haven't stabilized yet. */
1224 new_pid
= my_waitpid (pid
, &status
, 0);
1225 if (new_pid
== -1 && errno
== ECHILD
)
1228 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1230 /* Try again with __WCLONE to check cloned processes. */
1231 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1235 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1237 if (WSTOPSIG (status
) != SIGSTOP
)
1240 if (debug_linux_nat
)
1241 fprintf_unfiltered (gdb_stdlog
,
1242 "LNPAW: Received %s after attaching\n",
1243 status_to_str (status
));
1249 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1250 if the new LWP could not be attached. */
1253 lin_lwp_attach_lwp (ptid_t ptid
)
1255 struct lwp_info
*lp
;
1256 enum sigchld_state async_events_original_state
;
1258 gdb_assert (is_lwp (ptid
));
1260 async_events_original_state
= linux_nat_async_events (sigchld_sync
);
1262 lp
= find_lwp_pid (ptid
);
1264 /* We assume that we're already attached to any LWP that has an id
1265 equal to the overall process id, and to any LWP that is already
1266 in our list of LWPs. If we're not seeing exit events from threads
1267 and we've had PID wraparound since we last tried to stop all threads,
1268 this assumption might be wrong; fortunately, this is very unlikely
1270 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1272 int status
, cloned
= 0, signalled
= 0;
1274 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1276 /* If we fail to attach to the thread, issue a warning,
1277 but continue. One way this can happen is if thread
1278 creation is interrupted; as of Linux kernel 2.6.19, a
1279 bug may place threads in the thread list and then fail
1281 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1282 safe_strerror (errno
));
1286 if (debug_linux_nat
)
1287 fprintf_unfiltered (gdb_stdlog
,
1288 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1289 target_pid_to_str (ptid
));
1291 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1292 lp
= add_lwp (ptid
);
1294 lp
->cloned
= cloned
;
1295 lp
->signalled
= signalled
;
1296 if (WSTOPSIG (status
) != SIGSTOP
)
1299 lp
->status
= status
;
1302 target_post_attach (GET_LWP (lp
->ptid
));
1304 if (debug_linux_nat
)
1306 fprintf_unfiltered (gdb_stdlog
,
1307 "LLAL: waitpid %s received %s\n",
1308 target_pid_to_str (ptid
),
1309 status_to_str (status
));
1314 /* We assume that the LWP representing the original process is
1315 already stopped. Mark it as stopped in the data structure
1316 that the GNU/linux ptrace layer uses to keep track of
1317 threads. Note that this won't have already been done since
1318 the main thread will have, we assume, been stopped by an
1319 attach from a different layer. */
1321 lp
= add_lwp (ptid
);
1325 linux_nat_async_events (async_events_original_state
);
1330 linux_nat_create_inferior (struct target_ops
*ops
,
1331 char *exec_file
, char *allargs
, char **env
,
1334 int saved_async
= 0;
1335 #ifdef HAVE_PERSONALITY
1336 int personality_orig
= 0, personality_set
= 0;
1337 #endif /* HAVE_PERSONALITY */
1339 /* The fork_child mechanism is synchronous and calls target_wait, so
1340 we have to mask the async mode. */
1342 if (target_can_async_p ())
1343 /* Mask async mode. Creating a child requires a loop calling
1344 wait_for_inferior currently. */
1345 saved_async
= linux_nat_async_mask (0);
1348 /* Restore the original signal mask. */
1349 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1350 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1351 suspend_mask
= normal_mask
;
1352 sigdelset (&suspend_mask
, SIGCHLD
);
1355 /* Set SIGCHLD to the default action, until after execing the child,
1356 since the inferior inherits the superior's signal mask. It will
1357 be blocked again in linux_nat_wait, which is only reached after
1358 the inferior execing. */
1359 linux_nat_async_events (sigchld_default
);
1361 #ifdef HAVE_PERSONALITY
1362 if (disable_randomization
)
1365 personality_orig
= personality (0xffffffff);
1366 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1368 personality_set
= 1;
1369 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1371 if (errno
!= 0 || (personality_set
1372 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1373 warning (_("Error disabling address space randomization: %s"),
1374 safe_strerror (errno
));
1376 #endif /* HAVE_PERSONALITY */
1378 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1380 #ifdef HAVE_PERSONALITY
1381 if (personality_set
)
1384 personality (personality_orig
);
1386 warning (_("Error restoring address space randomization: %s"),
1387 safe_strerror (errno
));
1389 #endif /* HAVE_PERSONALITY */
1392 linux_nat_async_mask (saved_async
);
1396 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1398 struct lwp_info
*lp
;
1402 /* FIXME: We should probably accept a list of process id's, and
1403 attach all of them. */
1404 linux_ops
->to_attach (ops
, args
, from_tty
);
1406 if (!target_can_async_p ())
1408 /* Restore the original signal mask. */
1409 sigprocmask (SIG_SETMASK
, &normal_mask
, NULL
);
1410 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1411 suspend_mask
= normal_mask
;
1412 sigdelset (&suspend_mask
, SIGCHLD
);
1415 /* The ptrace base target adds the main thread with (pid,0,0)
1416 format. Decorate it with lwp info. */
1417 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1418 thread_change_ptid (inferior_ptid
, ptid
);
1420 /* Add the initial process as the first LWP to the list. */
1421 lp
= add_lwp (ptid
);
1423 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1427 /* Save the wait status to report later. */
1429 if (debug_linux_nat
)
1430 fprintf_unfiltered (gdb_stdlog
,
1431 "LNA: waitpid %ld, saving status %s\n",
1432 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1434 if (!target_can_async_p ())
1435 lp
->status
= status
;
1438 /* We already waited for this LWP, so put the wait result on the
1439 pipe. The event loop will wake up and gets us to handling
1441 linux_nat_event_pipe_push (GET_PID (lp
->ptid
), status
,
1442 lp
->cloned
? __WCLONE
: 0);
1443 /* Register in the event loop. */
1444 target_async (inferior_event_handler
, 0);
1448 /* Get pending status of LP. */
1450 get_pending_status (struct lwp_info
*lp
, int *status
)
1452 struct target_waitstatus last
;
1455 get_last_target_status (&last_ptid
, &last
);
1457 /* If this lwp is the ptid that GDB is processing an event from, the
1458 signal will be in stop_signal. Otherwise, in all-stop + sync
1459 mode, we may cache pending events in lp->status while trying to
1460 stop all threads (see stop_wait_callback). In async mode, the
1461 events are always cached in waitpid_queue. */
1467 enum target_signal signo
= TARGET_SIGNAL_0
;
1469 if (is_executing (lp
->ptid
))
1471 /* If the core thought this lwp was executing --- e.g., the
1472 executing property hasn't been updated yet, but the
1473 thread has been stopped with a stop_callback /
1474 stop_wait_callback sequence (see linux_nat_detach for
1475 example) --- we can only have pending events in the local
1477 if (queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
) != -1)
1479 if (WIFSTOPPED (*status
))
1480 signo
= target_signal_from_host (WSTOPSIG (*status
));
1482 /* If not stopped, then the lwp is gone, no use in
1483 resending a signal. */
1488 /* If the core knows the thread is not executing, then we
1489 have the last signal recorded in
1490 thread_info->stop_signal. */
1492 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1493 signo
= tp
->stop_signal
;
1496 if (signo
!= TARGET_SIGNAL_0
1497 && !signal_pass_state (signo
))
1499 if (debug_linux_nat
)
1500 fprintf_unfiltered (gdb_stdlog
, "\
1501 GPT: lwp %s had signal %s, but it is in no pass state\n",
1502 target_pid_to_str (lp
->ptid
),
1503 target_signal_to_string (signo
));
1507 if (signo
!= TARGET_SIGNAL_0
)
1508 *status
= W_STOPCODE (target_signal_to_host (signo
));
1510 if (debug_linux_nat
)
1511 fprintf_unfiltered (gdb_stdlog
,
1512 "GPT: lwp %s as pending signal %s\n",
1513 target_pid_to_str (lp
->ptid
),
1514 target_signal_to_string (signo
));
1519 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1521 struct thread_info
*tp
= find_thread_pid (lp
->ptid
);
1522 if (tp
->stop_signal
!= TARGET_SIGNAL_0
1523 && signal_pass_state (tp
->stop_signal
))
1524 *status
= W_STOPCODE (target_signal_to_host (tp
->stop_signal
));
1526 else if (target_can_async_p ())
1527 queued_waitpid (GET_LWP (lp
->ptid
), status
, __WALL
);
1529 *status
= lp
->status
;
1536 detach_callback (struct lwp_info
*lp
, void *data
)
1538 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1540 if (debug_linux_nat
&& lp
->status
)
1541 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1542 strsignal (WSTOPSIG (lp
->status
)),
1543 target_pid_to_str (lp
->ptid
));
1545 /* If there is a pending SIGSTOP, get rid of it. */
1548 if (debug_linux_nat
)
1549 fprintf_unfiltered (gdb_stdlog
,
1550 "DC: Sending SIGCONT to %s\n",
1551 target_pid_to_str (lp
->ptid
));
1553 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1557 /* We don't actually detach from the LWP that has an id equal to the
1558 overall process id just yet. */
1559 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1563 /* Pass on any pending signal for this LWP. */
1564 get_pending_status (lp
, &status
);
1567 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1568 WSTOPSIG (status
)) < 0)
1569 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1570 safe_strerror (errno
));
1572 if (debug_linux_nat
)
1573 fprintf_unfiltered (gdb_stdlog
,
1574 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1575 target_pid_to_str (lp
->ptid
),
1576 strsignal (WSTOPSIG (lp
->status
)));
1578 delete_lwp (lp
->ptid
);
1585 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1589 enum target_signal sig
;
1591 if (target_can_async_p ())
1592 linux_nat_async (NULL
, 0);
1594 /* Stop all threads before detaching. ptrace requires that the
1595 thread is stopped to sucessfully detach. */
1596 iterate_over_lwps (stop_callback
, NULL
);
1597 /* ... and wait until all of them have reported back that
1598 they're no longer running. */
1599 iterate_over_lwps (stop_wait_callback
, NULL
);
1601 iterate_over_lwps (detach_callback
, NULL
);
1603 /* Only the initial process should be left right now. */
1604 gdb_assert (num_lwps
== 1);
1606 /* Pass on any pending signal for the last LWP. */
1607 if ((args
== NULL
|| *args
== '\0')
1608 && get_pending_status (lwp_list
, &status
) != -1
1609 && WIFSTOPPED (status
))
1611 /* Put the signal number in ARGS so that inf_ptrace_detach will
1612 pass it along with PTRACE_DETACH. */
1614 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1615 fprintf_unfiltered (gdb_stdlog
,
1616 "LND: Sending signal %s to %s\n",
1618 target_pid_to_str (lwp_list
->ptid
));
1621 /* Destroy LWP info; it's no longer valid. */
1624 pid
= GET_PID (inferior_ptid
);
1625 inferior_ptid
= pid_to_ptid (pid
);
1626 linux_ops
->to_detach (ops
, args
, from_tty
);
1628 if (target_can_async_p ())
1629 drain_queued_events (pid
);
1635 resume_callback (struct lwp_info
*lp
, void *data
)
1637 if (lp
->stopped
&& lp
->status
== 0)
1639 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
1640 0, TARGET_SIGNAL_0
);
1641 if (debug_linux_nat
)
1642 fprintf_unfiltered (gdb_stdlog
,
1643 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1644 target_pid_to_str (lp
->ptid
));
1647 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1649 else if (lp
->stopped
&& debug_linux_nat
)
1650 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1651 target_pid_to_str (lp
->ptid
));
1652 else if (debug_linux_nat
)
1653 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1654 target_pid_to_str (lp
->ptid
));
1660 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1667 resume_set_callback (struct lwp_info
*lp
, void *data
)
1674 linux_nat_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1676 struct lwp_info
*lp
;
1679 if (debug_linux_nat
)
1680 fprintf_unfiltered (gdb_stdlog
,
1681 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1682 step
? "step" : "resume",
1683 target_pid_to_str (ptid
),
1684 signo
? strsignal (signo
) : "0",
1685 target_pid_to_str (inferior_ptid
));
1687 if (target_can_async_p ())
1688 /* Block events while we're here. */
1689 linux_nat_async_events (sigchld_sync
);
1691 /* A specific PTID means `step only this process id'. */
1692 resume_all
= (PIDGET (ptid
) == -1);
1694 if (non_stop
&& resume_all
)
1695 internal_error (__FILE__
, __LINE__
,
1696 "can't resume all in non-stop mode");
1701 iterate_over_lwps (resume_set_callback
, NULL
);
1703 iterate_over_lwps (resume_clear_callback
, NULL
);
1706 /* If PID is -1, it's the current inferior that should be
1707 handled specially. */
1708 if (PIDGET (ptid
) == -1)
1709 ptid
= inferior_ptid
;
1711 lp
= find_lwp_pid (ptid
);
1712 gdb_assert (lp
!= NULL
);
1714 /* Convert to something the lower layer understands. */
1715 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1717 /* Remember if we're stepping. */
1720 /* Mark this LWP as resumed. */
1723 /* If we have a pending wait status for this thread, there is no
1724 point in resuming the process. But first make sure that
1725 linux_nat_wait won't preemptively handle the event - we
1726 should never take this short-circuit if we are going to
1727 leave LP running, since we have skipped resuming all the
1728 other threads. This bit of code needs to be synchronized
1729 with linux_nat_wait. */
1731 /* In async mode, we never have pending wait status. */
1732 if (target_can_async_p () && lp
->status
)
1733 internal_error (__FILE__
, __LINE__
, "Pending status in async mode");
1735 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1738 struct inferior
*inf
;
1740 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1742 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1744 /* Defer to common code if we're gaining control of the
1746 if (inf
->stop_soon
== NO_STOP_QUIETLY
1747 && signal_stop_state (saved_signo
) == 0
1748 && signal_print_state (saved_signo
) == 0
1749 && signal_pass_state (saved_signo
) == 1)
1751 if (debug_linux_nat
)
1752 fprintf_unfiltered (gdb_stdlog
,
1753 "LLR: Not short circuiting for ignored "
1754 "status 0x%x\n", lp
->status
);
1756 /* FIXME: What should we do if we are supposed to continue
1757 this thread with a signal? */
1758 gdb_assert (signo
== TARGET_SIGNAL_0
);
1759 signo
= saved_signo
;
1766 /* FIXME: What should we do if we are supposed to continue
1767 this thread with a signal? */
1768 gdb_assert (signo
== TARGET_SIGNAL_0
);
1770 if (debug_linux_nat
)
1771 fprintf_unfiltered (gdb_stdlog
,
1772 "LLR: Short circuiting for status 0x%x\n",
1778 /* Mark LWP as not stopped to prevent it from being continued by
1783 iterate_over_lwps (resume_callback
, NULL
);
1785 linux_ops
->to_resume (ptid
, step
, signo
);
1786 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1788 if (debug_linux_nat
)
1789 fprintf_unfiltered (gdb_stdlog
,
1790 "LLR: %s %s, %s (resume event thread)\n",
1791 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1792 target_pid_to_str (ptid
),
1793 signo
? strsignal (signo
) : "0");
1795 if (target_can_async_p ())
1796 target_async (inferior_event_handler
, 0);
1799 /* Issue kill to specified lwp. */
1801 static int tkill_failed
;
1804 kill_lwp (int lwpid
, int signo
)
1808 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1809 fails, then we are not using nptl threads and we should be using kill. */
1811 #ifdef HAVE_TKILL_SYSCALL
1814 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1815 if (errno
!= ENOSYS
)
1822 return kill (lwpid
, signo
);
1825 /* Handle a GNU/Linux extended wait response. If we see a clone
1826 event, we need to add the new LWP to our list (and not report the
1827 trap to higher layers). This function returns non-zero if the
1828 event should be ignored and we should wait again. If STOPPING is
1829 true, the new LWP remains stopped, otherwise it is continued. */
1832 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1835 int pid
= GET_LWP (lp
->ptid
);
1836 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1837 struct lwp_info
*new_lp
= NULL
;
1838 int event
= status
>> 16;
1840 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1841 || event
== PTRACE_EVENT_CLONE
)
1843 unsigned long new_pid
;
1846 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1848 /* If we haven't already seen the new PID stop, wait for it now. */
1849 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1851 /* The new child has a pending SIGSTOP. We can't affect it until it
1852 hits the SIGSTOP, but we're already attached. */
1853 ret
= my_waitpid (new_pid
, &status
,
1854 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1856 perror_with_name (_("waiting for new child"));
1857 else if (ret
!= new_pid
)
1858 internal_error (__FILE__
, __LINE__
,
1859 _("wait returned unexpected PID %d"), ret
);
1860 else if (!WIFSTOPPED (status
))
1861 internal_error (__FILE__
, __LINE__
,
1862 _("wait returned unexpected status 0x%x"), status
);
1865 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1867 if (event
== PTRACE_EVENT_FORK
)
1868 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1869 else if (event
== PTRACE_EVENT_VFORK
)
1870 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1873 struct cleanup
*old_chain
;
1875 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1876 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (inferior_ptid
)));
1878 new_lp
->stopped
= 1;
1880 if (WSTOPSIG (status
) != SIGSTOP
)
1882 /* This can happen if someone starts sending signals to
1883 the new thread before it gets a chance to run, which
1884 have a lower number than SIGSTOP (e.g. SIGUSR1).
1885 This is an unlikely case, and harder to handle for
1886 fork / vfork than for clone, so we do not try - but
1887 we handle it for clone events here. We'll send
1888 the other signal on to the thread below. */
1890 new_lp
->signalled
= 1;
1897 /* Add the new thread to GDB's lists as soon as possible
1900 1) the frontend doesn't have to wait for a stop to
1903 2) we tag it with the correct running state. */
1905 /* If the thread_db layer is active, let it know about
1906 this new thread, and add it to GDB's list. */
1907 if (!thread_db_attach_lwp (new_lp
->ptid
))
1909 /* We're not using thread_db. Add it to GDB's
1911 target_post_attach (GET_LWP (new_lp
->ptid
));
1912 add_thread (new_lp
->ptid
);
1917 set_running (new_lp
->ptid
, 1);
1918 set_executing (new_lp
->ptid
, 1);
1924 new_lp
->stopped
= 0;
1925 new_lp
->resumed
= 1;
1926 ptrace (PTRACE_CONT
, new_pid
, 0,
1927 status
? WSTOPSIG (status
) : 0);
1930 if (debug_linux_nat
)
1931 fprintf_unfiltered (gdb_stdlog
,
1932 "LHEW: Got clone event from LWP %ld, resuming\n",
1933 GET_LWP (lp
->ptid
));
1934 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1942 if (event
== PTRACE_EVENT_EXEC
)
1944 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1945 ourstatus
->value
.execd_pathname
1946 = xstrdup (linux_child_pid_to_exec_file (pid
));
1948 if (linux_parent_pid
)
1950 detach_breakpoints (linux_parent_pid
);
1951 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1953 linux_parent_pid
= 0;
1956 /* At this point, all inserted breakpoints are gone. Doing this
1957 as soon as we detect an exec prevents the badness of deleting
1958 a breakpoint writing the current "shadow contents" to lift
1959 the bp. That shadow is NOT valid after an exec.
1961 Note that we have to do this after the detach_breakpoints
1962 call above, otherwise breakpoints wouldn't be lifted from the
1963 parent on a vfork, because detach_breakpoints would think
1964 that breakpoints are not inserted. */
1965 mark_breakpoints_out ();
1969 internal_error (__FILE__
, __LINE__
,
1970 _("unknown ptrace event %d"), event
);
1973 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1977 wait_lwp (struct lwp_info
*lp
)
1981 int thread_dead
= 0;
1983 gdb_assert (!lp
->stopped
);
1984 gdb_assert (lp
->status
== 0);
1986 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
1987 if (pid
== -1 && errno
== ECHILD
)
1989 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
1990 if (pid
== -1 && errno
== ECHILD
)
1992 /* The thread has previously exited. We need to delete it
1993 now because, for some vendor 2.4 kernels with NPTL
1994 support backported, there won't be an exit event unless
1995 it is the main thread. 2.6 kernels will report an exit
1996 event for each thread that exits, as expected. */
1998 if (debug_linux_nat
)
1999 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2000 target_pid_to_str (lp
->ptid
));
2006 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2008 if (debug_linux_nat
)
2010 fprintf_unfiltered (gdb_stdlog
,
2011 "WL: waitpid %s received %s\n",
2012 target_pid_to_str (lp
->ptid
),
2013 status_to_str (status
));
2017 /* Check if the thread has exited. */
2018 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2021 if (debug_linux_nat
)
2022 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2023 target_pid_to_str (lp
->ptid
));
2032 gdb_assert (WIFSTOPPED (status
));
2034 /* Handle GNU/Linux's extended waitstatus for trace events. */
2035 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2037 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
,
2039 "WL: Handling extended status 0x%06x\n",
2041 if (linux_handle_extended_wait (lp
, status
, 1))
2042 return wait_lwp (lp
);
2048 /* Save the most recent siginfo for LP. This is currently only called
2049 for SIGTRAP; some ports use the si_addr field for
2050 target_stopped_data_address. In the future, it may also be used to
2051 restore the siginfo of requeued signals. */
2054 save_siginfo (struct lwp_info
*lp
)
2057 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2058 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2061 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2064 /* Send a SIGSTOP to LP. */
2067 stop_callback (struct lwp_info
*lp
, void *data
)
2069 if (!lp
->stopped
&& !lp
->signalled
)
2073 if (debug_linux_nat
)
2075 fprintf_unfiltered (gdb_stdlog
,
2076 "SC: kill %s **<SIGSTOP>**\n",
2077 target_pid_to_str (lp
->ptid
));
2080 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2081 if (debug_linux_nat
)
2083 fprintf_unfiltered (gdb_stdlog
,
2084 "SC: lwp kill %d %s\n",
2086 errno
? safe_strerror (errno
) : "ERRNO-OK");
2090 gdb_assert (lp
->status
== 0);
2096 /* Return non-zero if LWP PID has a pending SIGINT. */
2099 linux_nat_has_pending_sigint (int pid
)
2101 sigset_t pending
, blocked
, ignored
;
2104 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2106 if (sigismember (&pending
, SIGINT
)
2107 && !sigismember (&ignored
, SIGINT
))
2113 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2116 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2118 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2119 flag to consume the next one. */
2120 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2121 && WSTOPSIG (lp
->status
) == SIGINT
)
2124 lp
->ignore_sigint
= 1;
2129 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2130 This function is called after we know the LWP has stopped; if the LWP
2131 stopped before the expected SIGINT was delivered, then it will never have
2132 arrived. Also, if the signal was delivered to a shared queue and consumed
2133 by a different thread, it will never be delivered to this LWP. */
2136 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2138 if (!lp
->ignore_sigint
)
2141 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2143 if (debug_linux_nat
)
2144 fprintf_unfiltered (gdb_stdlog
,
2145 "MCIS: Clearing bogus flag for %s\n",
2146 target_pid_to_str (lp
->ptid
));
2147 lp
->ignore_sigint
= 0;
2151 /* Wait until LP is stopped. */
2154 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2160 status
= wait_lwp (lp
);
2164 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2165 && WSTOPSIG (status
) == SIGINT
)
2167 lp
->ignore_sigint
= 0;
2170 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2171 if (debug_linux_nat
)
2172 fprintf_unfiltered (gdb_stdlog
,
2173 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2174 target_pid_to_str (lp
->ptid
),
2175 errno
? safe_strerror (errno
) : "OK");
2177 return stop_wait_callback (lp
, NULL
);
2180 maybe_clear_ignore_sigint (lp
);
2182 if (WSTOPSIG (status
) != SIGSTOP
)
2184 if (WSTOPSIG (status
) == SIGTRAP
)
2186 /* If a LWP other than the LWP that we're reporting an
2187 event for has hit a GDB breakpoint (as opposed to
2188 some random trap signal), then just arrange for it to
2189 hit it again later. We don't keep the SIGTRAP status
2190 and don't forward the SIGTRAP signal to the LWP. We
2191 will handle the current event, eventually we will
2192 resume all LWPs, and this one will get its breakpoint
2195 If we do not do this, then we run the risk that the
2196 user will delete or disable the breakpoint, but the
2197 thread will have already tripped on it. */
2199 /* Save the trap's siginfo in case we need it later. */
2202 /* Now resume this LWP and get the SIGSTOP event. */
2204 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2205 if (debug_linux_nat
)
2207 fprintf_unfiltered (gdb_stdlog
,
2208 "PTRACE_CONT %s, 0, 0 (%s)\n",
2209 target_pid_to_str (lp
->ptid
),
2210 errno
? safe_strerror (errno
) : "OK");
2212 fprintf_unfiltered (gdb_stdlog
,
2213 "SWC: Candidate SIGTRAP event in %s\n",
2214 target_pid_to_str (lp
->ptid
));
2216 /* Hold this event/waitstatus while we check to see if
2217 there are any more (we still want to get that SIGSTOP). */
2218 stop_wait_callback (lp
, NULL
);
2220 if (target_can_async_p ())
2222 /* Don't leave a pending wait status in async mode.
2223 Retrigger the breakpoint. */
2224 if (!cancel_breakpoint (lp
))
2226 /* There was no gdb breakpoint set at pc. Put
2227 the event back in the queue. */
2228 if (debug_linux_nat
)
2229 fprintf_unfiltered (gdb_stdlog
, "\
2230 SWC: leaving SIGTRAP in local queue of %s\n", target_pid_to_str (lp
->ptid
));
2231 push_waitpid (GET_LWP (lp
->ptid
),
2232 W_STOPCODE (SIGTRAP
),
2233 lp
->cloned
? __WCLONE
: 0);
2238 /* Hold the SIGTRAP for handling by
2240 /* If there's another event, throw it back into the
2244 if (debug_linux_nat
)
2245 fprintf_unfiltered (gdb_stdlog
,
2246 "SWC: kill %s, %s\n",
2247 target_pid_to_str (lp
->ptid
),
2248 status_to_str ((int) status
));
2249 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2251 /* Save the sigtrap event. */
2252 lp
->status
= status
;
2258 /* The thread was stopped with a signal other than
2259 SIGSTOP, and didn't accidentally trip a breakpoint. */
2261 if (debug_linux_nat
)
2263 fprintf_unfiltered (gdb_stdlog
,
2264 "SWC: Pending event %s in %s\n",
2265 status_to_str ((int) status
),
2266 target_pid_to_str (lp
->ptid
));
2268 /* Now resume this LWP and get the SIGSTOP event. */
2270 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2271 if (debug_linux_nat
)
2272 fprintf_unfiltered (gdb_stdlog
,
2273 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2274 target_pid_to_str (lp
->ptid
),
2275 errno
? safe_strerror (errno
) : "OK");
2277 /* Hold this event/waitstatus while we check to see if
2278 there are any more (we still want to get that SIGSTOP). */
2279 stop_wait_callback (lp
, NULL
);
2281 /* If the lp->status field is still empty, use it to
2282 hold this event. If not, then this event must be
2283 returned to the event queue of the LWP. */
2284 if (lp
->status
|| target_can_async_p ())
2286 if (debug_linux_nat
)
2288 fprintf_unfiltered (gdb_stdlog
,
2289 "SWC: kill %s, %s\n",
2290 target_pid_to_str (lp
->ptid
),
2291 status_to_str ((int) status
));
2293 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2296 lp
->status
= status
;
2302 /* We caught the SIGSTOP that we intended to catch, so
2303 there's no SIGSTOP pending. */
2312 /* Return non-zero if LP has a wait status pending. */
2315 status_callback (struct lwp_info
*lp
, void *data
)
2317 /* Only report a pending wait status if we pretend that this has
2318 indeed been resumed. */
2319 return (lp
->status
!= 0 && lp
->resumed
);
2322 /* Return non-zero if LP isn't stopped. */
2325 running_callback (struct lwp_info
*lp
, void *data
)
2327 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2330 /* Count the LWP's that have had events. */
2333 count_events_callback (struct lwp_info
*lp
, void *data
)
2337 gdb_assert (count
!= NULL
);
2339 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2340 if (lp
->status
!= 0 && lp
->resumed
2341 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2347 /* Select the LWP (if any) that is currently being single-stepped. */
2350 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2352 if (lp
->step
&& lp
->status
!= 0)
2358 /* Select the Nth LWP that has had a SIGTRAP event. */
2361 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2363 int *selector
= data
;
2365 gdb_assert (selector
!= NULL
);
2367 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2368 if (lp
->status
!= 0 && lp
->resumed
2369 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2370 if ((*selector
)-- == 0)
2377 cancel_breakpoint (struct lwp_info
*lp
)
2379 /* Arrange for a breakpoint to be hit again later. We don't keep
2380 the SIGTRAP status and don't forward the SIGTRAP signal to the
2381 LWP. We will handle the current event, eventually we will resume
2382 this LWP, and this breakpoint will trap again.
2384 If we do not do this, then we run the risk that the user will
2385 delete or disable the breakpoint, but the LWP will have already
2388 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2389 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2392 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2393 if (breakpoint_inserted_here_p (pc
))
2395 if (debug_linux_nat
)
2396 fprintf_unfiltered (gdb_stdlog
,
2397 "CB: Push back breakpoint for %s\n",
2398 target_pid_to_str (lp
->ptid
));
2400 /* Back up the PC if necessary. */
2401 if (gdbarch_decr_pc_after_break (gdbarch
))
2402 regcache_write_pc (regcache
, pc
);
2410 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2412 struct lwp_info
*event_lp
= data
;
2414 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2418 /* If a LWP other than the LWP that we're reporting an event for has
2419 hit a GDB breakpoint (as opposed to some random trap signal),
2420 then just arrange for it to hit it again later. We don't keep
2421 the SIGTRAP status and don't forward the SIGTRAP signal to the
2422 LWP. We will handle the current event, eventually we will resume
2423 all LWPs, and this one will get its breakpoint trap again.
2425 If we do not do this, then we run the risk that the user will
2426 delete or disable the breakpoint, but the LWP will have already
2430 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2431 && cancel_breakpoint (lp
))
2432 /* Throw away the SIGTRAP. */
2438 /* Select one LWP out of those that have events pending. */
2441 select_event_lwp (struct lwp_info
**orig_lp
, int *status
)
2444 int random_selector
;
2445 struct lwp_info
*event_lp
;
2447 /* Record the wait status for the original LWP. */
2448 (*orig_lp
)->status
= *status
;
2450 /* Give preference to any LWP that is being single-stepped. */
2451 event_lp
= iterate_over_lwps (select_singlestep_lwp_callback
, NULL
);
2452 if (event_lp
!= NULL
)
2454 if (debug_linux_nat
)
2455 fprintf_unfiltered (gdb_stdlog
,
2456 "SEL: Select single-step %s\n",
2457 target_pid_to_str (event_lp
->ptid
));
2461 /* No single-stepping LWP. Select one at random, out of those
2462 which have had SIGTRAP events. */
2464 /* First see how many SIGTRAP events we have. */
2465 iterate_over_lwps (count_events_callback
, &num_events
);
2467 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2468 random_selector
= (int)
2469 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2471 if (debug_linux_nat
&& num_events
> 1)
2472 fprintf_unfiltered (gdb_stdlog
,
2473 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2474 num_events
, random_selector
);
2476 event_lp
= iterate_over_lwps (select_event_lwp_callback
,
2480 if (event_lp
!= NULL
)
2482 /* Switch the event LWP. */
2483 *orig_lp
= event_lp
;
2484 *status
= event_lp
->status
;
2487 /* Flush the wait status for the event LWP. */
2488 (*orig_lp
)->status
= 0;
2491 /* Return non-zero if LP has been resumed. */
2494 resumed_callback (struct lwp_info
*lp
, void *data
)
2499 /* Stop an active thread, verify it still exists, then resume it. */
2502 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2504 struct lwp_info
*ptr
;
2506 if (!lp
->stopped
&& !lp
->signalled
)
2508 stop_callback (lp
, NULL
);
2509 stop_wait_callback (lp
, NULL
);
2510 /* Resume if the lwp still exists. */
2511 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2514 resume_callback (lp
, NULL
);
2515 resume_set_callback (lp
, NULL
);
2521 /* Check if we should go on and pass this event to common code.
2522 Return the affected lwp if we are, or NULL otherwise. */
2523 static struct lwp_info
*
2524 linux_nat_filter_event (int lwpid
, int status
, int options
)
2526 struct lwp_info
*lp
;
2528 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2530 /* Check for stop events reported by a process we didn't already
2531 know about - anything not already in our LWP list.
2533 If we're expecting to receive stopped processes after
2534 fork, vfork, and clone events, then we'll just add the
2535 new one to our list and go back to waiting for the event
2536 to be reported - the stopped process might be returned
2537 from waitpid before or after the event is. */
2538 if (WIFSTOPPED (status
) && !lp
)
2540 linux_record_stopped_pid (lwpid
, status
);
2544 /* Make sure we don't report an event for the exit of an LWP not in
2545 our list, i.e. not part of the current process. This can happen
2546 if we detach from a program we original forked and then it
2548 if (!WIFSTOPPED (status
) && !lp
)
2551 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2552 CLONE_PTRACE processes which do not use the thread library -
2553 otherwise we wouldn't find the new LWP this way. That doesn't
2554 currently work, and the following code is currently unreachable
2555 due to the two blocks above. If it's fixed some day, this code
2556 should be broken out into a function so that we can also pick up
2557 LWPs from the new interface. */
2560 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2561 if (options
& __WCLONE
)
2564 gdb_assert (WIFSTOPPED (status
)
2565 && WSTOPSIG (status
) == SIGSTOP
);
2568 if (!in_thread_list (inferior_ptid
))
2570 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2571 GET_PID (inferior_ptid
));
2572 add_thread (inferior_ptid
);
2575 add_thread (lp
->ptid
);
2578 /* Save the trap's siginfo in case we need it later. */
2579 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2582 /* Handle GNU/Linux's extended waitstatus for trace events. */
2583 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2585 if (debug_linux_nat
)
2586 fprintf_unfiltered (gdb_stdlog
,
2587 "LLW: Handling extended status 0x%06x\n",
2589 if (linux_handle_extended_wait (lp
, status
, 0))
2593 /* Check if the thread has exited. */
2594 if ((WIFEXITED (status
) || WIFSIGNALED (status
)) && num_lwps
> 1)
2596 /* If this is the main thread, we must stop all threads and
2597 verify if they are still alive. This is because in the nptl
2598 thread model, there is no signal issued for exiting LWPs
2599 other than the main thread. We only get the main thread exit
2600 signal once all child threads have already exited. If we
2601 stop all the threads and use the stop_wait_callback to check
2602 if they have exited we can determine whether this signal
2603 should be ignored or whether it means the end of the debugged
2604 application, regardless of which threading model is being
2606 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2609 iterate_over_lwps (stop_and_resume_callback
, NULL
);
2612 if (debug_linux_nat
)
2613 fprintf_unfiltered (gdb_stdlog
,
2614 "LLW: %s exited.\n",
2615 target_pid_to_str (lp
->ptid
));
2619 /* If there is at least one more LWP, then the exit signal was
2620 not the end of the debugged application and should be
2626 /* Check if the current LWP has previously exited. In the nptl
2627 thread model, LWPs other than the main thread do not issue
2628 signals when they exit so we must check whenever the thread has
2629 stopped. A similar check is made in stop_wait_callback(). */
2630 if (num_lwps
> 1 && !linux_nat_thread_alive (lp
->ptid
))
2632 if (debug_linux_nat
)
2633 fprintf_unfiltered (gdb_stdlog
,
2634 "LLW: %s exited.\n",
2635 target_pid_to_str (lp
->ptid
));
2639 /* Make sure there is at least one thread running. */
2640 gdb_assert (iterate_over_lwps (running_callback
, NULL
));
2642 /* Discard the event. */
2646 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2647 an attempt to stop an LWP. */
2649 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2651 if (debug_linux_nat
)
2652 fprintf_unfiltered (gdb_stdlog
,
2653 "LLW: Delayed SIGSTOP caught for %s.\n",
2654 target_pid_to_str (lp
->ptid
));
2656 /* This is a delayed SIGSTOP. */
2659 registers_changed ();
2661 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2662 lp
->step
, TARGET_SIGNAL_0
);
2663 if (debug_linux_nat
)
2664 fprintf_unfiltered (gdb_stdlog
,
2665 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2667 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2668 target_pid_to_str (lp
->ptid
));
2671 gdb_assert (lp
->resumed
);
2673 /* Discard the event. */
2677 /* Make sure we don't report a SIGINT that we have already displayed
2678 for another thread. */
2679 if (lp
->ignore_sigint
2680 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2682 if (debug_linux_nat
)
2683 fprintf_unfiltered (gdb_stdlog
,
2684 "LLW: Delayed SIGINT caught for %s.\n",
2685 target_pid_to_str (lp
->ptid
));
2687 /* This is a delayed SIGINT. */
2688 lp
->ignore_sigint
= 0;
2690 registers_changed ();
2691 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2692 lp
->step
, TARGET_SIGNAL_0
);
2693 if (debug_linux_nat
)
2694 fprintf_unfiltered (gdb_stdlog
,
2695 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2697 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2698 target_pid_to_str (lp
->ptid
));
2701 gdb_assert (lp
->resumed
);
2703 /* Discard the event. */
2707 /* An interesting event. */
2712 /* Get the events stored in the pipe into the local queue, so they are
2713 accessible to queued_waitpid. We need to do this, since it is not
2714 always the case that the event at the head of the pipe is the event
2718 pipe_to_local_event_queue (void)
2720 if (debug_linux_nat_async
)
2721 fprintf_unfiltered (gdb_stdlog
,
2722 "PTLEQ: linux_nat_num_queued_events(%d)\n",
2723 linux_nat_num_queued_events
);
2724 while (linux_nat_num_queued_events
)
2726 int lwpid
, status
, options
;
2727 lwpid
= linux_nat_event_pipe_pop (&status
, &options
);
2728 gdb_assert (lwpid
> 0);
2729 push_waitpid (lwpid
, status
, options
);
2733 /* Get the unprocessed events stored in the local queue back into the
2734 pipe, so the event loop realizes there's something else to
2738 local_event_queue_to_pipe (void)
2740 struct waitpid_result
*w
= waitpid_queue
;
2743 struct waitpid_result
*next
= w
->next
;
2744 linux_nat_event_pipe_push (w
->pid
,
2750 waitpid_queue
= NULL
;
2752 if (debug_linux_nat_async
)
2753 fprintf_unfiltered (gdb_stdlog
,
2754 "LEQTP: linux_nat_num_queued_events(%d)\n",
2755 linux_nat_num_queued_events
);
2759 linux_nat_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
2761 struct lwp_info
*lp
= NULL
;
2764 pid_t pid
= PIDGET (ptid
);
2766 if (debug_linux_nat_async
)
2767 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2769 /* The first time we get here after starting a new inferior, we may
2770 not have added it to the LWP list yet - this is the earliest
2771 moment at which we know its PID. */
2774 gdb_assert (!is_lwp (inferior_ptid
));
2776 /* Upgrade the main thread's ptid. */
2777 thread_change_ptid (inferior_ptid
,
2778 BUILD_LWP (GET_PID (inferior_ptid
),
2779 GET_PID (inferior_ptid
)));
2781 lp
= add_lwp (inferior_ptid
);
2785 /* Block events while we're here. */
2786 linux_nat_async_events (sigchld_sync
);
2790 /* Make sure there is at least one LWP that has been resumed. */
2791 gdb_assert (iterate_over_lwps (resumed_callback
, NULL
));
2793 /* First check if there is a LWP with a wait status pending. */
2796 /* Any LWP that's been resumed will do. */
2797 lp
= iterate_over_lwps (status_callback
, NULL
);
2800 if (target_can_async_p ())
2801 internal_error (__FILE__
, __LINE__
,
2802 "Found an LWP with a pending status in async mode.");
2804 status
= lp
->status
;
2807 if (debug_linux_nat
&& status
)
2808 fprintf_unfiltered (gdb_stdlog
,
2809 "LLW: Using pending wait status %s for %s.\n",
2810 status_to_str (status
),
2811 target_pid_to_str (lp
->ptid
));
2814 /* But if we don't find one, we'll have to wait, and check both
2815 cloned and uncloned processes. We start with the cloned
2817 options
= __WCLONE
| WNOHANG
;
2819 else if (is_lwp (ptid
))
2821 if (debug_linux_nat
)
2822 fprintf_unfiltered (gdb_stdlog
,
2823 "LLW: Waiting for specific LWP %s.\n",
2824 target_pid_to_str (ptid
));
2826 /* We have a specific LWP to check. */
2827 lp
= find_lwp_pid (ptid
);
2829 status
= lp
->status
;
2832 if (debug_linux_nat
&& status
)
2833 fprintf_unfiltered (gdb_stdlog
,
2834 "LLW: Using pending wait status %s for %s.\n",
2835 status_to_str (status
),
2836 target_pid_to_str (lp
->ptid
));
2838 /* If we have to wait, take into account whether PID is a cloned
2839 process or not. And we have to convert it to something that
2840 the layer beneath us can understand. */
2841 options
= lp
->cloned
? __WCLONE
: 0;
2842 pid
= GET_LWP (ptid
);
2845 if (status
&& lp
->signalled
)
2847 /* A pending SIGSTOP may interfere with the normal stream of
2848 events. In a typical case where interference is a problem,
2849 we have a SIGSTOP signal pending for LWP A while
2850 single-stepping it, encounter an event in LWP B, and take the
2851 pending SIGSTOP while trying to stop LWP A. After processing
2852 the event in LWP B, LWP A is continued, and we'll never see
2853 the SIGTRAP associated with the last time we were
2854 single-stepping LWP A. */
2856 /* Resume the thread. It should halt immediately returning the
2858 registers_changed ();
2859 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2860 lp
->step
, TARGET_SIGNAL_0
);
2861 if (debug_linux_nat
)
2862 fprintf_unfiltered (gdb_stdlog
,
2863 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2864 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2865 target_pid_to_str (lp
->ptid
));
2867 gdb_assert (lp
->resumed
);
2869 /* This should catch the pending SIGSTOP. */
2870 stop_wait_callback (lp
, NULL
);
2873 if (!target_can_async_p ())
2875 /* Causes SIGINT to be passed on to the attached process. */
2883 if (target_can_async_p ())
2884 /* In async mode, don't ever block. Only look at the locally
2886 lwpid
= queued_waitpid (pid
, &status
, options
);
2888 lwpid
= my_waitpid (pid
, &status
, options
);
2892 gdb_assert (pid
== -1 || lwpid
== pid
);
2894 if (debug_linux_nat
)
2896 fprintf_unfiltered (gdb_stdlog
,
2897 "LLW: waitpid %ld received %s\n",
2898 (long) lwpid
, status_to_str (status
));
2901 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2904 /* A discarded event. */
2914 /* Alternate between checking cloned and uncloned processes. */
2915 options
^= __WCLONE
;
2917 /* And every time we have checked both:
2918 In async mode, return to event loop;
2919 In sync mode, suspend waiting for a SIGCHLD signal. */
2920 if (options
& __WCLONE
)
2922 if (target_can_async_p ())
2924 /* No interesting event. */
2925 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2927 /* Get ready for the next event. */
2928 target_async (inferior_event_handler
, 0);
2930 if (debug_linux_nat_async
)
2931 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2933 return minus_one_ptid
;
2936 sigsuspend (&suspend_mask
);
2940 /* We shouldn't end up here unless we want to try again. */
2941 gdb_assert (status
== 0);
2944 if (!target_can_async_p ())
2945 clear_sigint_trap ();
2949 /* Don't report signals that GDB isn't interested in, such as
2950 signals that are neither printed nor stopped upon. Stopping all
2951 threads can be a bit time-consuming so if we want decent
2952 performance with heavily multi-threaded programs, especially when
2953 they're using a high frequency timer, we'd better avoid it if we
2956 if (WIFSTOPPED (status
))
2958 int signo
= target_signal_from_host (WSTOPSIG (status
));
2959 struct inferior
*inf
;
2961 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
2964 /* Defer to common code if we get a signal while
2965 single-stepping, since that may need special care, e.g. to
2966 skip the signal handler, or, if we're gaining control of the
2969 && inf
->stop_soon
== NO_STOP_QUIETLY
2970 && signal_stop_state (signo
) == 0
2971 && signal_print_state (signo
) == 0
2972 && signal_pass_state (signo
) == 1)
2974 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2975 here? It is not clear we should. GDB may not expect
2976 other threads to run. On the other hand, not resuming
2977 newly attached threads may cause an unwanted delay in
2978 getting them running. */
2979 registers_changed ();
2980 linux_ops
->to_resume (pid_to_ptid (GET_LWP (lp
->ptid
)),
2982 if (debug_linux_nat
)
2983 fprintf_unfiltered (gdb_stdlog
,
2984 "LLW: %s %s, %s (preempt 'handle')\n",
2986 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2987 target_pid_to_str (lp
->ptid
),
2988 signo
? strsignal (signo
) : "0");
2996 /* Only do the below in all-stop, as we currently use SIGINT
2997 to implement target_stop (see linux_nat_stop) in
2999 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3001 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3002 forwarded to the entire process group, that is, all LWPs
3003 will receive it - unless they're using CLONE_THREAD to
3004 share signals. Since we only want to report it once, we
3005 mark it as ignored for all LWPs except this one. */
3006 iterate_over_lwps (set_ignore_sigint
, NULL
);
3007 lp
->ignore_sigint
= 0;
3010 maybe_clear_ignore_sigint (lp
);
3014 /* This LWP is stopped now. */
3017 if (debug_linux_nat
)
3018 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3019 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3023 /* Now stop all other LWP's ... */
3024 iterate_over_lwps (stop_callback
, NULL
);
3026 /* ... and wait until all of them have reported back that
3027 they're no longer running. */
3028 iterate_over_lwps (stop_wait_callback
, NULL
);
3030 /* If we're not waiting for a specific LWP, choose an event LWP
3031 from among those that have had events. Giving equal priority
3032 to all LWPs that have had events helps prevent
3035 select_event_lwp (&lp
, &status
);
3038 /* Now that we've selected our final event LWP, cancel any
3039 breakpoints in other LWPs that have hit a GDB breakpoint. See
3040 the comment in cancel_breakpoints_callback to find out why. */
3041 iterate_over_lwps (cancel_breakpoints_callback
, lp
);
3043 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3045 if (debug_linux_nat
)
3046 fprintf_unfiltered (gdb_stdlog
,
3047 "LLW: trap ptid is %s.\n",
3048 target_pid_to_str (lp
->ptid
));
3051 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3053 *ourstatus
= lp
->waitstatus
;
3054 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3057 store_waitstatus (ourstatus
, status
);
3059 /* Get ready for the next event. */
3060 if (target_can_async_p ())
3061 target_async (inferior_event_handler
, 0);
3063 if (debug_linux_nat_async
)
3064 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3070 kill_callback (struct lwp_info
*lp
, void *data
)
3073 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3074 if (debug_linux_nat
)
3075 fprintf_unfiltered (gdb_stdlog
,
3076 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3077 target_pid_to_str (lp
->ptid
),
3078 errno
? safe_strerror (errno
) : "OK");
3084 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3088 /* We must make sure that there are no pending events (delayed
3089 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3090 program doesn't interfere with any following debugging session. */
3092 /* For cloned processes we must check both with __WCLONE and
3093 without, since the exit status of a cloned process isn't reported
3099 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3100 if (pid
!= (pid_t
) -1)
3102 if (debug_linux_nat
)
3103 fprintf_unfiltered (gdb_stdlog
,
3104 "KWC: wait %s received unknown.\n",
3105 target_pid_to_str (lp
->ptid
));
3106 /* The Linux kernel sometimes fails to kill a thread
3107 completely after PTRACE_KILL; that goes from the stop
3108 point in do_fork out to the one in
3109 get_signal_to_deliever and waits again. So kill it
3111 kill_callback (lp
, NULL
);
3114 while (pid
== GET_LWP (lp
->ptid
));
3116 gdb_assert (pid
== -1 && errno
== ECHILD
);
3121 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3122 if (pid
!= (pid_t
) -1)
3124 if (debug_linux_nat
)
3125 fprintf_unfiltered (gdb_stdlog
,
3126 "KWC: wait %s received unk.\n",
3127 target_pid_to_str (lp
->ptid
));
3128 /* See the call to kill_callback above. */
3129 kill_callback (lp
, NULL
);
3132 while (pid
== GET_LWP (lp
->ptid
));
3134 gdb_assert (pid
== -1 && errno
== ECHILD
);
3139 linux_nat_kill (void)
3141 struct target_waitstatus last
;
3145 if (target_can_async_p ())
3146 target_async (NULL
, 0);
3148 /* If we're stopped while forking and we haven't followed yet,
3149 kill the other task. We need to do this first because the
3150 parent will be sleeping if this is a vfork. */
3152 get_last_target_status (&last_ptid
, &last
);
3154 if (last
.kind
== TARGET_WAITKIND_FORKED
3155 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3157 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3161 if (forks_exist_p ())
3163 linux_fork_killall ();
3164 drain_queued_events (-1);
3168 /* Stop all threads before killing them, since ptrace requires
3169 that the thread is stopped to sucessfully PTRACE_KILL. */
3170 iterate_over_lwps (stop_callback
, NULL
);
3171 /* ... and wait until all of them have reported back that
3172 they're no longer running. */
3173 iterate_over_lwps (stop_wait_callback
, NULL
);
3175 /* Kill all LWP's ... */
3176 iterate_over_lwps (kill_callback
, NULL
);
3178 /* ... and wait until we've flushed all events. */
3179 iterate_over_lwps (kill_wait_callback
, NULL
);
3182 target_mourn_inferior ();
3186 linux_nat_mourn_inferior (struct target_ops
*ops
)
3188 /* Destroy LWP info; it's no longer valid. */
3191 if (! forks_exist_p ())
3193 /* Normal case, no other forks available. */
3194 if (target_can_async_p ())
3195 linux_nat_async (NULL
, 0);
3196 linux_ops
->to_mourn_inferior (ops
);
3199 /* Multi-fork case. The current inferior_ptid has exited, but
3200 there are other viable forks to debug. Delete the exiting
3201 one and context-switch to the first available. */
3202 linux_fork_mourn_inferior ();
3206 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3207 const char *annex
, gdb_byte
*readbuf
,
3208 const gdb_byte
*writebuf
,
3209 ULONGEST offset
, LONGEST len
)
3211 struct cleanup
*old_chain
= save_inferior_ptid ();
3214 if (is_lwp (inferior_ptid
))
3215 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3217 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3220 do_cleanups (old_chain
);
3225 linux_nat_thread_alive (ptid_t ptid
)
3229 gdb_assert (is_lwp (ptid
));
3231 /* Send signal 0 instead of anything ptrace, because ptracing a
3232 running thread errors out claiming that the thread doesn't
3234 err
= kill_lwp (GET_LWP (ptid
), 0);
3236 if (debug_linux_nat
)
3237 fprintf_unfiltered (gdb_stdlog
,
3238 "LLTA: KILL(SIG0) %s (%s)\n",
3239 target_pid_to_str (ptid
),
3240 err
? safe_strerror (err
) : "OK");
3249 linux_nat_pid_to_str (ptid_t ptid
)
3251 static char buf
[64];
3254 && ((lwp_list
&& lwp_list
->next
)
3255 || GET_PID (ptid
) != GET_LWP (ptid
)))
3257 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3261 return normal_pid_to_str (ptid
);
3265 sigchld_handler (int signo
)
3267 if (target_async_permitted
3268 && linux_nat_async_events_state
!= sigchld_sync
3269 && signo
== SIGCHLD
)
3270 /* It is *always* a bug to hit this. */
3271 internal_error (__FILE__
, __LINE__
,
3272 "sigchld_handler called when async events are enabled");
3274 /* Do nothing. The only reason for this handler is that it allows
3275 us to use sigsuspend in linux_nat_wait above to wait for the
3276 arrival of a SIGCHLD. */
3279 /* Accepts an integer PID; Returns a string representing a file that
3280 can be opened to get the symbols for the child process. */
3283 linux_child_pid_to_exec_file (int pid
)
3285 char *name1
, *name2
;
3287 name1
= xmalloc (MAXPATHLEN
);
3288 name2
= xmalloc (MAXPATHLEN
);
3289 make_cleanup (xfree
, name1
);
3290 make_cleanup (xfree
, name2
);
3291 memset (name2
, 0, MAXPATHLEN
);
3293 sprintf (name1
, "/proc/%d/exe", pid
);
3294 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3300 /* Service function for corefiles and info proc. */
3303 read_mapping (FILE *mapfile
,
3308 char *device
, long long *inode
, char *filename
)
3310 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3311 addr
, endaddr
, permissions
, offset
, device
, inode
);
3314 if (ret
> 0 && ret
!= EOF
)
3316 /* Eat everything up to EOL for the filename. This will prevent
3317 weird filenames (such as one with embedded whitespace) from
3318 confusing this code. It also makes this code more robust in
3319 respect to annotations the kernel may add after the filename.
3321 Note the filename is used for informational purposes
3323 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3326 return (ret
!= 0 && ret
!= EOF
);
3329 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3330 regions in the inferior for a corefile. */
3333 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3335 int, int, int, void *), void *obfd
)
3337 long long pid
= PIDGET (inferior_ptid
);
3338 char mapsfilename
[MAXPATHLEN
];
3340 long long addr
, endaddr
, size
, offset
, inode
;
3341 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3342 int read
, write
, exec
;
3344 struct cleanup
*cleanup
;
3346 /* Compose the filename for the /proc memory map, and open it. */
3347 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
3348 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3349 error (_("Could not open %s."), mapsfilename
);
3350 cleanup
= make_cleanup_fclose (mapsfile
);
3353 fprintf_filtered (gdb_stdout
,
3354 "Reading memory regions from %s\n", mapsfilename
);
3356 /* Now iterate until end-of-file. */
3357 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3358 &offset
, &device
[0], &inode
, &filename
[0]))
3360 size
= endaddr
- addr
;
3362 /* Get the segment's permissions. */
3363 read
= (strchr (permissions
, 'r') != 0);
3364 write
= (strchr (permissions
, 'w') != 0);
3365 exec
= (strchr (permissions
, 'x') != 0);
3369 fprintf_filtered (gdb_stdout
,
3370 "Save segment, %lld bytes at 0x%s (%c%c%c)",
3371 size
, paddr_nz (addr
),
3373 write
? 'w' : ' ', exec
? 'x' : ' ');
3375 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3376 fprintf_filtered (gdb_stdout
, "\n");
3379 /* Invoke the callback function to create the corefile
3381 func (addr
, size
, read
, write
, exec
, obfd
);
3383 do_cleanups (cleanup
);
3388 find_signalled_thread (struct thread_info
*info
, void *data
)
3390 if (info
->stop_signal
!= TARGET_SIGNAL_0
3391 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3397 static enum target_signal
3398 find_stop_signal (void)
3400 struct thread_info
*info
=
3401 iterate_over_threads (find_signalled_thread
, NULL
);
3404 return info
->stop_signal
;
3406 return TARGET_SIGNAL_0
;
3409 /* Records the thread's register state for the corefile note
3413 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3414 char *note_data
, int *note_size
,
3415 enum target_signal stop_signal
)
3417 gdb_gregset_t gregs
;
3418 gdb_fpregset_t fpregs
;
3419 unsigned long lwp
= ptid_get_lwp (ptid
);
3420 struct regcache
*regcache
= get_thread_regcache (ptid
);
3421 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3422 const struct regset
*regset
;
3424 struct cleanup
*old_chain
;
3425 struct core_regset_section
*sect_list
;
3428 old_chain
= save_inferior_ptid ();
3429 inferior_ptid
= ptid
;
3430 target_fetch_registers (regcache
, -1);
3431 do_cleanups (old_chain
);
3433 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3434 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3437 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3438 sizeof (gregs
))) != NULL
3439 && regset
->collect_regset
!= NULL
)
3440 regset
->collect_regset (regset
, regcache
, -1,
3441 &gregs
, sizeof (gregs
));
3443 fill_gregset (regcache
, &gregs
, -1);
3445 note_data
= (char *) elfcore_write_prstatus (obfd
,
3449 stop_signal
, &gregs
);
3451 /* The loop below uses the new struct core_regset_section, which stores
3452 the supported section names and sizes for the core file. Note that
3453 note PRSTATUS needs to be treated specially. But the other notes are
3454 structurally the same, so they can benefit from the new struct. */
3455 if (core_regset_p
&& sect_list
!= NULL
)
3456 while (sect_list
->sect_name
!= NULL
)
3458 /* .reg was already handled above. */
3459 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3464 regset
= gdbarch_regset_from_core_section (gdbarch
,
3465 sect_list
->sect_name
,
3467 gdb_assert (regset
&& regset
->collect_regset
);
3468 gdb_regset
= xmalloc (sect_list
->size
);
3469 regset
->collect_regset (regset
, regcache
, -1,
3470 gdb_regset
, sect_list
->size
);
3471 note_data
= (char *) elfcore_write_register_note (obfd
,
3474 sect_list
->sect_name
,
3481 /* For architectures that does not have the struct core_regset_section
3482 implemented, we use the old method. When all the architectures have
3483 the new support, the code below should be deleted. */
3487 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3488 sizeof (fpregs
))) != NULL
3489 && regset
->collect_regset
!= NULL
)
3490 regset
->collect_regset (regset
, regcache
, -1,
3491 &fpregs
, sizeof (fpregs
));
3493 fill_fpregset (regcache
, &fpregs
, -1);
3495 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3498 &fpregs
, sizeof (fpregs
));
3504 struct linux_nat_corefile_thread_data
3510 enum target_signal stop_signal
;
3513 /* Called by gdbthread.c once per thread. Records the thread's
3514 register state for the corefile note section. */
3517 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3519 struct linux_nat_corefile_thread_data
*args
= data
;
3521 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3531 /* Fills the "to_make_corefile_note" target vector. Builds the note
3532 section for a corefile, and returns it in a malloc buffer. */
3535 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3537 struct linux_nat_corefile_thread_data thread_args
;
3538 struct cleanup
*old_chain
;
3539 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3540 char fname
[16] = { '\0' };
3541 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3542 char psargs
[80] = { '\0' };
3543 char *note_data
= NULL
;
3544 ptid_t current_ptid
= inferior_ptid
;
3548 if (get_exec_file (0))
3550 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3551 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3552 if (get_inferior_args ())
3555 char *psargs_end
= psargs
+ sizeof (psargs
);
3557 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3559 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3560 if (string_end
!= NULL
)
3562 *string_end
++ = ' ';
3563 strncpy (string_end
, get_inferior_args (),
3564 psargs_end
- string_end
);
3567 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3569 note_size
, fname
, psargs
);
3572 /* Dump information for threads. */
3573 thread_args
.obfd
= obfd
;
3574 thread_args
.note_data
= note_data
;
3575 thread_args
.note_size
= note_size
;
3576 thread_args
.num_notes
= 0;
3577 thread_args
.stop_signal
= find_stop_signal ();
3578 iterate_over_lwps (linux_nat_corefile_thread_callback
, &thread_args
);
3579 gdb_assert (thread_args
.num_notes
!= 0);
3580 note_data
= thread_args
.note_data
;
3582 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3586 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3587 "CORE", NT_AUXV
, auxv
, auxv_len
);
3591 make_cleanup (xfree
, note_data
);
3595 /* Implement the "info proc" command. */
3598 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3600 long long pid
= PIDGET (inferior_ptid
);
3603 char buffer
[MAXPATHLEN
];
3604 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3617 /* Break up 'args' into an argv array. */
3618 argv
= gdb_buildargv (args
);
3619 make_cleanup_freeargv (argv
);
3621 while (argv
!= NULL
&& *argv
!= NULL
)
3623 if (isdigit (argv
[0][0]))
3625 pid
= strtoul (argv
[0], NULL
, 10);
3627 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3631 else if (strcmp (argv
[0], "status") == 0)
3635 else if (strcmp (argv
[0], "stat") == 0)
3639 else if (strcmp (argv
[0], "cmd") == 0)
3643 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3647 else if (strcmp (argv
[0], "cwd") == 0)
3651 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3657 /* [...] (future options here) */
3662 error (_("No current process: you must name one."));
3664 sprintf (fname1
, "/proc/%lld", pid
);
3665 if (stat (fname1
, &dummy
) != 0)
3666 error (_("No /proc directory: '%s'"), fname1
);
3668 printf_filtered (_("process %lld\n"), pid
);
3669 if (cmdline_f
|| all
)
3671 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
3672 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3674 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3675 fgets (buffer
, sizeof (buffer
), procfile
);
3676 printf_filtered ("cmdline = '%s'\n", buffer
);
3677 do_cleanups (cleanup
);
3680 warning (_("unable to open /proc file '%s'"), fname1
);
3684 sprintf (fname1
, "/proc/%lld/cwd", pid
);
3685 memset (fname2
, 0, sizeof (fname2
));
3686 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3687 printf_filtered ("cwd = '%s'\n", fname2
);
3689 warning (_("unable to read link '%s'"), fname1
);
3693 sprintf (fname1
, "/proc/%lld/exe", pid
);
3694 memset (fname2
, 0, sizeof (fname2
));
3695 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3696 printf_filtered ("exe = '%s'\n", fname2
);
3698 warning (_("unable to read link '%s'"), fname1
);
3700 if (mappings_f
|| all
)
3702 sprintf (fname1
, "/proc/%lld/maps", pid
);
3703 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3705 long long addr
, endaddr
, size
, offset
, inode
;
3706 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3707 struct cleanup
*cleanup
;
3709 cleanup
= make_cleanup_fclose (procfile
);
3710 printf_filtered (_("Mapped address spaces:\n\n"));
3711 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3713 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3716 " Size", " Offset", "objfile");
3720 printf_filtered (" %18s %18s %10s %10s %7s\n",
3723 " Size", " Offset", "objfile");
3726 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3727 &offset
, &device
[0], &inode
, &filename
[0]))
3729 size
= endaddr
- addr
;
3731 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3732 calls here (and possibly above) should be abstracted
3733 out into their own functions? Andrew suggests using
3734 a generic local_address_string instead to print out
3735 the addresses; that makes sense to me, too. */
3737 if (gdbarch_addr_bit (current_gdbarch
) == 32)
3739 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3740 (unsigned long) addr
, /* FIXME: pr_addr */
3741 (unsigned long) endaddr
,
3743 (unsigned int) offset
,
3744 filename
[0] ? filename
: "");
3748 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3749 (unsigned long) addr
, /* FIXME: pr_addr */
3750 (unsigned long) endaddr
,
3752 (unsigned int) offset
,
3753 filename
[0] ? filename
: "");
3757 do_cleanups (cleanup
);
3760 warning (_("unable to open /proc file '%s'"), fname1
);
3762 if (status_f
|| all
)
3764 sprintf (fname1
, "/proc/%lld/status", pid
);
3765 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3767 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3768 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
3769 puts_filtered (buffer
);
3770 do_cleanups (cleanup
);
3773 warning (_("unable to open /proc file '%s'"), fname1
);
3777 sprintf (fname1
, "/proc/%lld/stat", pid
);
3778 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3783 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3785 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3786 printf_filtered (_("Process: %d\n"), itmp
);
3787 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
3788 printf_filtered (_("Exec file: %s\n"), buffer
);
3789 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
3790 printf_filtered (_("State: %c\n"), ctmp
);
3791 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3792 printf_filtered (_("Parent process: %d\n"), itmp
);
3793 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3794 printf_filtered (_("Process group: %d\n"), itmp
);
3795 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3796 printf_filtered (_("Session id: %d\n"), itmp
);
3797 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3798 printf_filtered (_("TTY: %d\n"), itmp
);
3799 if (fscanf (procfile
, "%d ", &itmp
) > 0)
3800 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
3801 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3802 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
3803 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3804 printf_filtered (_("Minor faults (no memory page): %lu\n"),
3805 (unsigned long) ltmp
);
3806 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3807 printf_filtered (_("Minor faults, children: %lu\n"),
3808 (unsigned long) ltmp
);
3809 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3810 printf_filtered (_("Major faults (memory page faults): %lu\n"),
3811 (unsigned long) ltmp
);
3812 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3813 printf_filtered (_("Major faults, children: %lu\n"),
3814 (unsigned long) ltmp
);
3815 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3816 printf_filtered (_("utime: %ld\n"), ltmp
);
3817 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3818 printf_filtered (_("stime: %ld\n"), ltmp
);
3819 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3820 printf_filtered (_("utime, children: %ld\n"), ltmp
);
3821 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3822 printf_filtered (_("stime, children: %ld\n"), ltmp
);
3823 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3824 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
3826 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3827 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
3828 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3829 printf_filtered (_("jiffies until next timeout: %lu\n"),
3830 (unsigned long) ltmp
);
3831 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3832 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
3833 (unsigned long) ltmp
);
3834 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3835 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
3837 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3838 printf_filtered (_("Virtual memory size: %lu\n"),
3839 (unsigned long) ltmp
);
3840 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3841 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
3842 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3843 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
3844 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3845 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
3846 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3847 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
3848 if (fscanf (procfile
, "%lu ", <mp
) > 0)
3849 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
3850 #if 0 /* Don't know how architecture-dependent the rest is...
3851 Anyway the signal bitmap info is available from "status". */
3852 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3853 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
3854 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3855 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
3856 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3857 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
3858 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3859 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
3860 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3861 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
3862 if (fscanf (procfile
, "%ld ", <mp
) > 0)
3863 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
3864 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
3865 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
3867 do_cleanups (cleanup
);
3870 warning (_("unable to open /proc file '%s'"), fname1
);
3874 /* Implement the to_xfer_partial interface for memory reads using the /proc
3875 filesystem. Because we can use a single read() call for /proc, this
3876 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3877 but it doesn't support writes. */
3880 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3881 const char *annex
, gdb_byte
*readbuf
,
3882 const gdb_byte
*writebuf
,
3883 ULONGEST offset
, LONGEST len
)
3889 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
3892 /* Don't bother for one word. */
3893 if (len
< 3 * sizeof (long))
3896 /* We could keep this file open and cache it - possibly one per
3897 thread. That requires some juggling, but is even faster. */
3898 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
3899 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
3903 /* If pread64 is available, use it. It's faster if the kernel
3904 supports it (only one syscall), and it's 64-bit safe even on
3905 32-bit platforms (for instance, SPARC debugging a SPARC64
3908 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
3910 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
3920 /* Parse LINE as a signal set and add its set bits to SIGS. */
3923 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
3925 int len
= strlen (line
) - 1;
3929 if (line
[len
] != '\n')
3930 error (_("Could not parse signal set: %s"), line
);
3938 if (*p
>= '0' && *p
<= '9')
3940 else if (*p
>= 'a' && *p
<= 'f')
3941 digit
= *p
- 'a' + 10;
3943 error (_("Could not parse signal set: %s"), line
);
3948 sigaddset (sigs
, signum
+ 1);
3950 sigaddset (sigs
, signum
+ 2);
3952 sigaddset (sigs
, signum
+ 3);
3954 sigaddset (sigs
, signum
+ 4);
3960 /* Find process PID's pending signals from /proc/pid/status and set
3964 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
3967 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
3969 struct cleanup
*cleanup
;
3971 sigemptyset (pending
);
3972 sigemptyset (blocked
);
3973 sigemptyset (ignored
);
3974 sprintf (fname
, "/proc/%d/status", pid
);
3975 procfile
= fopen (fname
, "r");
3976 if (procfile
== NULL
)
3977 error (_("Could not open %s"), fname
);
3978 cleanup
= make_cleanup_fclose (procfile
);
3980 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
3982 /* Normal queued signals are on the SigPnd line in the status
3983 file. However, 2.6 kernels also have a "shared" pending
3984 queue for delivering signals to a thread group, so check for
3987 Unfortunately some Red Hat kernels include the shared pending
3988 queue but not the ShdPnd status field. */
3990 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
3991 add_line_to_sigset (buffer
+ 8, pending
);
3992 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
3993 add_line_to_sigset (buffer
+ 8, pending
);
3994 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
3995 add_line_to_sigset (buffer
+ 8, blocked
);
3996 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
3997 add_line_to_sigset (buffer
+ 8, ignored
);
4000 do_cleanups (cleanup
);
4004 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4005 const char *annex
, gdb_byte
*readbuf
,
4006 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4008 /* We make the process list snapshot when the object starts to be
4010 static const char *buf
;
4011 static LONGEST len_avail
= -1;
4012 static struct obstack obstack
;
4016 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4018 if (strcmp (annex
, "processes") != 0)
4021 gdb_assert (readbuf
&& !writebuf
);
4025 if (len_avail
!= -1 && len_avail
!= 0)
4026 obstack_free (&obstack
, NULL
);
4029 obstack_init (&obstack
);
4030 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4032 dirp
= opendir ("/proc");
4036 while ((dp
= readdir (dirp
)) != NULL
)
4038 struct stat statbuf
;
4039 char procentry
[sizeof ("/proc/4294967295")];
4041 if (!isdigit (dp
->d_name
[0])
4042 || strlen (dp
->d_name
) > sizeof ("4294967295") - 1)
4045 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4046 if (stat (procentry
, &statbuf
) == 0
4047 && S_ISDIR (statbuf
.st_mode
))
4051 char cmd
[MAXPATHLEN
+ 1];
4052 struct passwd
*entry
;
4054 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4055 entry
= getpwuid (statbuf
.st_uid
);
4057 if ((f
= fopen (pathname
, "r")) != NULL
)
4059 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4063 for (i
= 0; i
< len
; i
++)
4068 obstack_xml_printf (
4071 "<column name=\"pid\">%s</column>"
4072 "<column name=\"user\">%s</column>"
4073 "<column name=\"command\">%s</column>"
4076 entry
? entry
->pw_name
: "?",
4089 obstack_grow_str0 (&obstack
, "</osdata>\n");
4090 buf
= obstack_finish (&obstack
);
4091 len_avail
= strlen (buf
);
4094 if (offset
>= len_avail
)
4096 /* Done. Get rid of the obstack. */
4097 obstack_free (&obstack
, NULL
);
4103 if (len
> len_avail
- offset
)
4104 len
= len_avail
- offset
;
4105 memcpy (readbuf
, buf
+ offset
, len
);
4111 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4112 const char *annex
, gdb_byte
*readbuf
,
4113 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4117 if (object
== TARGET_OBJECT_AUXV
)
4118 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4121 if (object
== TARGET_OBJECT_OSDATA
)
4122 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4125 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4130 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4134 /* Create a prototype generic GNU/Linux target. The client can override
4135 it with local methods. */
4138 linux_target_install_ops (struct target_ops
*t
)
4140 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4141 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4142 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4143 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4144 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4145 t
->to_post_attach
= linux_child_post_attach
;
4146 t
->to_follow_fork
= linux_child_follow_fork
;
4147 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4148 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4150 super_xfer_partial
= t
->to_xfer_partial
;
4151 t
->to_xfer_partial
= linux_xfer_partial
;
4157 struct target_ops
*t
;
4159 t
= inf_ptrace_target ();
4160 linux_target_install_ops (t
);
4166 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4168 struct target_ops
*t
;
4170 t
= inf_ptrace_trad_target (register_u_offset
);
4171 linux_target_install_ops (t
);
4176 /* target_is_async_p implementation. */
4179 linux_nat_is_async_p (void)
4181 /* NOTE: palves 2008-03-21: We're only async when the user requests
4182 it explicitly with the "maintenance set target-async" command.
4183 Someday, linux will always be async. */
4184 if (!target_async_permitted
)
4190 /* target_can_async_p implementation. */
4193 linux_nat_can_async_p (void)
4195 /* NOTE: palves 2008-03-21: We're only async when the user requests
4196 it explicitly with the "maintenance set target-async" command.
4197 Someday, linux will always be async. */
4198 if (!target_async_permitted
)
4201 /* See target.h/target_async_mask. */
4202 return linux_nat_async_mask_value
;
4206 linux_nat_supports_non_stop (void)
4211 /* target_async_mask implementation. */
4214 linux_nat_async_mask (int mask
)
4217 current_state
= linux_nat_async_mask_value
;
4219 if (current_state
!= mask
)
4223 linux_nat_async (NULL
, 0);
4224 linux_nat_async_mask_value
= mask
;
4228 linux_nat_async_mask_value
= mask
;
4229 linux_nat_async (inferior_event_handler
, 0);
4233 return current_state
;
4236 /* Pop an event from the event pipe. */
4239 linux_nat_event_pipe_pop (int* ptr_status
, int* ptr_options
)
4241 struct waitpid_result event
= {0};
4246 ret
= read (linux_nat_event_pipe
[0], &event
, sizeof (event
));
4248 while (ret
== -1 && errno
== EINTR
);
4250 gdb_assert (ret
== sizeof (event
));
4252 *ptr_status
= event
.status
;
4253 *ptr_options
= event
.options
;
4255 linux_nat_num_queued_events
--;
4260 /* Push an event into the event pipe. */
4263 linux_nat_event_pipe_push (int pid
, int status
, int options
)
4266 struct waitpid_result event
= {0};
4268 event
.status
= status
;
4269 event
.options
= options
;
4273 ret
= write (linux_nat_event_pipe
[1], &event
, sizeof (event
));
4274 gdb_assert ((ret
== -1 && errno
== EINTR
) || ret
== sizeof (event
));
4275 } while (ret
== -1 && errno
== EINTR
);
4277 linux_nat_num_queued_events
++;
4281 get_pending_events (void)
4283 int status
, options
, pid
;
4285 if (!target_async_permitted
4286 || linux_nat_async_events_state
!= sigchld_async
)
4287 internal_error (__FILE__
, __LINE__
,
4288 "get_pending_events called with async masked");
4293 options
= __WCLONE
| WNOHANG
;
4297 pid
= waitpid (-1, &status
, options
);
4299 while (pid
== -1 && errno
== EINTR
);
4306 pid
= waitpid (-1, &status
, options
);
4308 while (pid
== -1 && errno
== EINTR
);
4312 /* No more children reporting events. */
4315 if (debug_linux_nat_async
)
4316 fprintf_unfiltered (gdb_stdlog
, "\
4317 get_pending_events: pid(%d), status(%x), options (%x)\n",
4318 pid
, status
, options
);
4320 linux_nat_event_pipe_push (pid
, status
, options
);
4323 if (debug_linux_nat_async
)
4324 fprintf_unfiltered (gdb_stdlog
, "\
4325 get_pending_events: linux_nat_num_queued_events(%d)\n",
4326 linux_nat_num_queued_events
);
4329 /* SIGCHLD handler for async mode. */
4332 async_sigchld_handler (int signo
)
4334 if (debug_linux_nat_async
)
4335 fprintf_unfiltered (gdb_stdlog
, "async_sigchld_handler\n");
4337 get_pending_events ();
4340 /* Set SIGCHLD handling state to STATE. Returns previous state. */
4342 static enum sigchld_state
4343 linux_nat_async_events (enum sigchld_state state
)
4345 enum sigchld_state current_state
= linux_nat_async_events_state
;
4347 if (debug_linux_nat_async
)
4348 fprintf_unfiltered (gdb_stdlog
,
4349 "LNAE: state(%d): linux_nat_async_events_state(%d), "
4350 "linux_nat_num_queued_events(%d)\n",
4351 state
, linux_nat_async_events_state
,
4352 linux_nat_num_queued_events
);
4354 if (current_state
!= state
)
4357 sigemptyset (&mask
);
4358 sigaddset (&mask
, SIGCHLD
);
4360 /* Always block before changing state. */
4361 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4363 /* Set new state. */
4364 linux_nat_async_events_state
= state
;
4370 /* Block target events. */
4371 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4372 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4373 /* Get events out of queue, and make them available to
4374 queued_waitpid / my_waitpid. */
4375 pipe_to_local_event_queue ();
4380 /* Unblock target events for async mode. */
4382 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4384 /* Put events we already waited on, in the pipe first, so
4386 local_event_queue_to_pipe ();
4387 /* While in masked async, we may have not collected all
4388 the pending events. Get them out now. */
4389 get_pending_events ();
4392 sigaction (SIGCHLD
, &async_sigchld_action
, NULL
);
4393 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4396 case sigchld_default
:
4398 /* SIGCHLD default mode. */
4399 sigaction (SIGCHLD
, &sigchld_default_action
, NULL
);
4401 /* Get events out of queue, and make them available to
4402 queued_waitpid / my_waitpid. */
4403 pipe_to_local_event_queue ();
4405 /* Unblock SIGCHLD. */
4406 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4412 return current_state
;
4415 static int async_terminal_is_ours
= 1;
4417 /* target_terminal_inferior implementation. */
4420 linux_nat_terminal_inferior (void)
4422 if (!target_is_async_p ())
4424 /* Async mode is disabled. */
4425 terminal_inferior ();
4429 /* GDB should never give the terminal to the inferior, if the
4430 inferior is running in the background (run&, continue&, etc.).
4431 This check can be removed when the common code is fixed. */
4432 if (!sync_execution
)
4435 terminal_inferior ();
4437 if (!async_terminal_is_ours
)
4440 delete_file_handler (input_fd
);
4441 async_terminal_is_ours
= 0;
4445 /* target_terminal_ours implementation. */
4448 linux_nat_terminal_ours (void)
4450 if (!target_is_async_p ())
4452 /* Async mode is disabled. */
4457 /* GDB should never give the terminal to the inferior if the
4458 inferior is running in the background (run&, continue&, etc.),
4459 but claiming it sure should. */
4462 if (!sync_execution
)
4465 if (async_terminal_is_ours
)
4468 clear_sigint_trap ();
4469 add_file_handler (input_fd
, stdin_event_handler
, 0);
4470 async_terminal_is_ours
= 1;
4473 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4475 static void *async_client_context
;
4478 linux_nat_async_file_handler (int error
, gdb_client_data client_data
)
4480 async_client_callback (INF_REG_EVENT
, async_client_context
);
4483 /* target_async implementation. */
4486 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4487 void *context
), void *context
)
4489 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
4490 internal_error (__FILE__
, __LINE__
,
4491 "Calling target_async when async is masked");
4493 if (callback
!= NULL
)
4495 async_client_callback
= callback
;
4496 async_client_context
= context
;
4497 add_file_handler (linux_nat_event_pipe
[0],
4498 linux_nat_async_file_handler
, NULL
);
4500 linux_nat_async_events (sigchld_async
);
4504 async_client_callback
= callback
;
4505 async_client_context
= context
;
4507 linux_nat_async_events (sigchld_sync
);
4508 delete_file_handler (linux_nat_event_pipe
[0]);
4513 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4517 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4519 ptid_t ptid
= * (ptid_t
*) data
;
4521 if (ptid_equal (lwp
->ptid
, ptid
)
4522 || ptid_equal (minus_one_ptid
, ptid
)
4523 || (ptid_is_pid (ptid
)
4524 && ptid_get_pid (ptid
) == ptid_get_pid (lwp
->ptid
)))
4530 if (debug_linux_nat
)
4531 fprintf_unfiltered (gdb_stdlog
,
4532 "LNSL: running -> suspending %s\n",
4533 target_pid_to_str (lwp
->ptid
));
4535 /* Peek once, to check if we've already waited for this
4537 pid
= queued_waitpid_1 (ptid_get_lwp (lwp
->ptid
), &status
,
4538 lwp
->cloned
? __WCLONE
: 0, 1 /* peek */);
4542 ptid_t ptid
= lwp
->ptid
;
4544 stop_callback (lwp
, NULL
);
4545 stop_wait_callback (lwp
, NULL
);
4547 /* If the lwp exits while we try to stop it, there's
4548 nothing else to do. */
4549 lwp
= find_lwp_pid (ptid
);
4553 pid
= queued_waitpid_1 (ptid_get_lwp (lwp
->ptid
), &status
,
4554 lwp
->cloned
? __WCLONE
: 0,
4558 /* If we didn't collect any signal other than SIGSTOP while
4559 stopping the LWP, push a SIGNAL_0 event. In either case,
4560 the event-loop will end up calling target_wait which will
4563 push_waitpid (ptid_get_lwp (lwp
->ptid
), W_STOPCODE (0),
4564 lwp
->cloned
? __WCLONE
: 0);
4568 /* Already known to be stopped; do nothing. */
4570 if (debug_linux_nat
)
4572 if (find_thread_pid (lwp
->ptid
)->stop_requested
)
4573 fprintf_unfiltered (gdb_stdlog
, "\
4574 LNSL: already stopped/stop_requested %s\n",
4575 target_pid_to_str (lwp
->ptid
));
4577 fprintf_unfiltered (gdb_stdlog
, "\
4578 LNSL: already stopped/no stop_requested yet %s\n",
4579 target_pid_to_str (lwp
->ptid
));
4587 linux_nat_stop (ptid_t ptid
)
4591 linux_nat_async_events (sigchld_sync
);
4592 iterate_over_lwps (linux_nat_stop_lwp
, &ptid
);
4593 target_async (inferior_event_handler
, 0);
4596 linux_ops
->to_stop (ptid
);
4600 linux_nat_add_target (struct target_ops
*t
)
4602 /* Save the provided single-threaded target. We save this in a separate
4603 variable because another target we've inherited from (e.g. inf-ptrace)
4604 may have saved a pointer to T; we want to use it for the final
4605 process stratum target. */
4606 linux_ops_saved
= *t
;
4607 linux_ops
= &linux_ops_saved
;
4609 /* Override some methods for multithreading. */
4610 t
->to_create_inferior
= linux_nat_create_inferior
;
4611 t
->to_attach
= linux_nat_attach
;
4612 t
->to_detach
= linux_nat_detach
;
4613 t
->to_resume
= linux_nat_resume
;
4614 t
->to_wait
= linux_nat_wait
;
4615 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4616 t
->to_kill
= linux_nat_kill
;
4617 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4618 t
->to_thread_alive
= linux_nat_thread_alive
;
4619 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4620 t
->to_has_thread_control
= tc_schedlock
;
4622 t
->to_can_async_p
= linux_nat_can_async_p
;
4623 t
->to_is_async_p
= linux_nat_is_async_p
;
4624 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4625 t
->to_async
= linux_nat_async
;
4626 t
->to_async_mask
= linux_nat_async_mask
;
4627 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4628 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4630 /* Methods for non-stop support. */
4631 t
->to_stop
= linux_nat_stop
;
4633 /* We don't change the stratum; this target will sit at
4634 process_stratum and thread_db will set at thread_stratum. This
4635 is a little strange, since this is a multi-threaded-capable
4636 target, but we want to be on the stack below thread_db, and we
4637 also want to be used for single-threaded processes. */
4641 /* TODO: Eliminate this and have libthread_db use
4642 find_target_beneath. */
4646 /* Register a method to call whenever a new thread is attached. */
4648 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4650 /* Save the pointer. We only support a single registered instance
4651 of the GNU/Linux native target, so we do not need to map this to
4653 linux_nat_new_thread
= new_thread
;
4656 /* Return the saved siginfo associated with PTID. */
4658 linux_nat_get_siginfo (ptid_t ptid
)
4660 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4662 gdb_assert (lp
!= NULL
);
4664 return &lp
->siginfo
;
4667 /* Enable/Disable async mode. */
4670 linux_nat_setup_async (void)
4672 if (pipe (linux_nat_event_pipe
) == -1)
4673 internal_error (__FILE__
, __LINE__
,
4674 "creating event pipe failed.");
4675 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4676 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4680 _initialize_linux_nat (void)
4684 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4685 Show /proc process information about any running process.\n\
4686 Specify any process id, or use the program being debugged by default.\n\
4687 Specify any of the following keywords for detailed info:\n\
4688 mappings -- list of mapped memory regions.\n\
4689 stat -- list a bunch of random process info.\n\
4690 status -- list a different bunch of random process info.\n\
4691 all -- list all available /proc info."));
4693 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4694 &debug_linux_nat
, _("\
4695 Set debugging of GNU/Linux lwp module."), _("\
4696 Show debugging of GNU/Linux lwp module."), _("\
4697 Enables printf debugging output."),
4699 show_debug_linux_nat
,
4700 &setdebuglist
, &showdebuglist
);
4702 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4703 &debug_linux_nat_async
, _("\
4704 Set debugging of GNU/Linux async lwp module."), _("\
4705 Show debugging of GNU/Linux async lwp module."), _("\
4706 Enables printf debugging output."),
4708 show_debug_linux_nat_async
,
4709 &setdebuglist
, &showdebuglist
);
4711 /* Get the default SIGCHLD action. Used while forking an inferior
4712 (see linux_nat_create_inferior/linux_nat_async_events). */
4713 sigaction (SIGCHLD
, NULL
, &sigchld_default_action
);
4715 /* Block SIGCHLD by default. Doing this early prevents it getting
4716 unblocked if an exception is thrown due to an error while the
4717 inferior is starting (sigsetjmp/siglongjmp). */
4718 sigemptyset (&mask
);
4719 sigaddset (&mask
, SIGCHLD
);
4720 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4722 /* Save this mask as the default. */
4723 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4725 /* The synchronous SIGCHLD handler. */
4726 sync_sigchld_action
.sa_handler
= sigchld_handler
;
4727 sigemptyset (&sync_sigchld_action
.sa_mask
);
4728 sync_sigchld_action
.sa_flags
= SA_RESTART
;
4730 /* Make it the default. */
4731 sigaction (SIGCHLD
, &sync_sigchld_action
, NULL
);
4733 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4734 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4735 sigdelset (&suspend_mask
, SIGCHLD
);
4737 /* SIGCHLD handler for async mode. */
4738 async_sigchld_action
.sa_handler
= async_sigchld_handler
;
4739 sigemptyset (&async_sigchld_action
.sa_mask
);
4740 async_sigchld_action
.sa_flags
= SA_RESTART
;
4742 linux_nat_setup_async ();
4744 add_setshow_boolean_cmd ("disable-randomization", class_support
,
4745 &disable_randomization
, _("\
4746 Set disabling of debuggee's virtual address space randomization."), _("\
4747 Show disabling of debuggee's virtual address space randomization."), _("\
4748 When this mode is on (which is the default), randomization of the virtual\n\
4749 address space is disabled. Standalone programs run with the randomization\n\
4750 enabled by default on some platforms."),
4751 &set_disable_randomization
,
4752 &show_disable_randomization
,
4753 &setlist
, &showlist
);
4757 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4758 the GNU/Linux Threads library and therefore doesn't really belong
4761 /* Read variable NAME in the target and return its value if found.
4762 Otherwise return zero. It is assumed that the type of the variable
4766 get_signo (const char *name
)
4768 struct minimal_symbol
*ms
;
4771 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
4775 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
4776 sizeof (signo
)) != 0)
4782 /* Return the set of signals used by the threads library in *SET. */
4785 lin_thread_get_thread_signals (sigset_t
*set
)
4787 struct sigaction action
;
4788 int restart
, cancel
;
4789 sigset_t blocked_mask
;
4791 sigemptyset (&blocked_mask
);
4794 restart
= get_signo ("__pthread_sig_restart");
4795 cancel
= get_signo ("__pthread_sig_cancel");
4797 /* LinuxThreads normally uses the first two RT signals, but in some legacy
4798 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
4799 not provide any way for the debugger to query the signal numbers -
4800 fortunately they don't change! */
4803 restart
= __SIGRTMIN
;
4806 cancel
= __SIGRTMIN
+ 1;
4808 sigaddset (set
, restart
);
4809 sigaddset (set
, cancel
);
4811 /* The GNU/Linux Threads library makes terminating threads send a
4812 special "cancel" signal instead of SIGCHLD. Make sure we catch
4813 those (to prevent them from terminating GDB itself, which is
4814 likely to be their default action) and treat them the same way as
4817 action
.sa_handler
= sigchld_handler
;
4818 sigemptyset (&action
.sa_mask
);
4819 action
.sa_flags
= SA_RESTART
;
4820 sigaction (cancel
, &action
, NULL
);
4822 /* We block the "cancel" signal throughout this code ... */
4823 sigaddset (&blocked_mask
, cancel
);
4824 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
4826 /* ... except during a sigsuspend. */
4827 sigdelset (&suspend_mask
, cancel
);