1 /* GNU/Linux native-dependent code common to multiple platforms.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #ifdef HAVE_TKILL_SYSCALL
29 #include <sys/syscall.h>
31 #include <sys/ptrace.h>
32 #include "linux-nat.h"
33 #include "linux-fork.h"
34 #include "gdbthread.h"
38 #include "inf-ptrace.h"
40 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <sys/procfs.h> /* for elf_gregset etc. */
42 #include "elf-bfd.h" /* for elfcore_write_* */
43 #include "gregset.h" /* for gregset */
44 #include "gdbcore.h" /* for get_exec_file */
45 #include <ctype.h> /* for isdigit */
46 #include "gdbthread.h" /* for struct thread_info etc. */
47 #include "gdb_stat.h" /* for struct stat */
48 #include <fcntl.h> /* for O_RDONLY */
50 #include "event-loop.h"
51 #include "event-top.h"
53 #include <sys/types.h>
54 #include "gdb_dirent.h"
55 #include "xml-support.h"
60 #define SPUFS_MAGIC 0x23c9b64e
63 #ifdef HAVE_PERSONALITY
64 # include <sys/personality.h>
65 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66 # define ADDR_NO_RANDOMIZE 0x0040000
68 #endif /* HAVE_PERSONALITY */
70 /* This comment documents high-level logic of this file.
72 Waiting for events in sync mode
73 ===============================
75 When waiting for an event in a specific thread, we just use waitpid, passing
76 the specific pid, and not passing WNOHANG.
78 When waiting for an event in all threads, waitpid is not quite good. Prior to
79 version 2.4, Linux can either wait for event in main thread, or in secondary
80 threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
81 miss an event. The solution is to use non-blocking waitpid, together with
82 sigsuspend. First, we use non-blocking waitpid to get an event in the main
83 process, if any. Second, we use non-blocking waitpid with the __WCLONED
84 flag to check for events in cloned processes. If nothing is found, we use
85 sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
86 happened to a child process -- and SIGCHLD will be delivered both for events
87 in main debugged process and in cloned processes. As soon as we know there's
88 an event, we get back to calling nonblocking waitpid with and without __WCLONED.
90 Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
91 so that we don't miss a signal. If SIGCHLD arrives in between, when it's
92 blocked, the signal becomes pending and sigsuspend immediately
93 notices it and returns.
95 Waiting for events in async mode
96 ================================
98 In async mode, GDB should always be ready to handle both user input
99 and target events, so neither blocking waitpid nor sigsuspend are
100 viable options. Instead, we should asynchronously notify the GDB main
101 event loop whenever there's an unprocessed event from the target. We
102 detect asynchronous target events by handling SIGCHLD signals. To
103 notify the event loop about target events, the self-pipe trick is used
104 --- a pipe is registered as waitable event source in the event loop,
105 the event loop select/poll's on the read end of this pipe (as well on
106 other event sources, e.g., stdin), and the SIGCHLD handler writes a
107 byte to this pipe. This is more portable than relying on
108 pselect/ppoll, since on kernels that lack those syscalls, libc
109 emulates them with select/poll+sigprocmask, and that is racy
110 (a.k.a. plain broken).
112 Obviously, if we fail to notify the event loop if there's a target
113 event, it's bad. OTOH, if we notify the event loop when there's no
114 event from the target, linux_nat_wait will detect that there's no real
115 event to report, and return event of type TARGET_WAITKIND_IGNORE.
116 This is mostly harmless, but it will waste time and is better avoided.
118 The main design point is that every time GDB is outside linux-nat.c,
119 we have a SIGCHLD handler installed that is called when something
120 happens to the target and notifies the GDB event loop. Whenever GDB
121 core decides to handle the event, and calls into linux-nat.c, we
122 process things as in sync mode, except that the we never block in
125 While processing an event, we may end up momentarily blocked in
126 waitpid calls. Those waitpid calls, while blocking, are guarantied to
127 return quickly. E.g., in all-stop mode, before reporting to the core
128 that an LWP hit a breakpoint, all LWPs are stopped by sending them
129 SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
130 Note that this is different from blocking indefinitely waiting for the
131 next event --- here, we're already handling an event.
136 We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
137 signal is not entirely significant; we just need for a signal to be delivered,
138 so that we can intercept it. SIGSTOP's advantage is that it can not be
139 blocked. A disadvantage is that it is not a real-time signal, so it can only
140 be queued once; we do not keep track of other sources of SIGSTOP.
142 Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
143 use them, because they have special behavior when the signal is generated -
144 not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
145 kills the entire thread group.
147 A delivered SIGSTOP would stop the entire thread group, not just the thread we
148 tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
149 cancel it (by PTRACE_CONT without passing SIGSTOP).
151 We could use a real-time signal instead. This would solve those problems; we
152 could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
153 But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
154 generates it, and there are races with trying to find a signal that is not
158 #define O_LARGEFILE 0
161 /* If the system headers did not provide the constants, hard-code the normal
163 #ifndef PTRACE_EVENT_FORK
165 #define PTRACE_SETOPTIONS 0x4200
166 #define PTRACE_GETEVENTMSG 0x4201
168 /* options set using PTRACE_SETOPTIONS */
169 #define PTRACE_O_TRACESYSGOOD 0x00000001
170 #define PTRACE_O_TRACEFORK 0x00000002
171 #define PTRACE_O_TRACEVFORK 0x00000004
172 #define PTRACE_O_TRACECLONE 0x00000008
173 #define PTRACE_O_TRACEEXEC 0x00000010
174 #define PTRACE_O_TRACEVFORKDONE 0x00000020
175 #define PTRACE_O_TRACEEXIT 0x00000040
177 /* Wait extended result codes for the above trace options. */
178 #define PTRACE_EVENT_FORK 1
179 #define PTRACE_EVENT_VFORK 2
180 #define PTRACE_EVENT_CLONE 3
181 #define PTRACE_EVENT_EXEC 4
182 #define PTRACE_EVENT_VFORK_DONE 5
183 #define PTRACE_EVENT_EXIT 6
185 #endif /* PTRACE_EVENT_FORK */
187 /* We can't always assume that this flag is available, but all systems
188 with the ptrace event handlers also have __WALL, so it's safe to use
191 #define __WALL 0x40000000 /* Wait for any child. */
194 #ifndef PTRACE_GETSIGINFO
195 # define PTRACE_GETSIGINFO 0x4202
196 # define PTRACE_SETSIGINFO 0x4203
199 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
200 the use of the multi-threaded target. */
201 static struct target_ops
*linux_ops
;
202 static struct target_ops linux_ops_saved
;
204 /* The method to call, if any, when a new thread is attached. */
205 static void (*linux_nat_new_thread
) (ptid_t
);
207 /* The method to call, if any, when the siginfo object needs to be
208 converted between the layout returned by ptrace, and the layout in
209 the architecture of the inferior. */
210 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
214 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
215 Called by our to_xfer_partial. */
216 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
218 const char *, gdb_byte
*,
222 static int debug_linux_nat
;
224 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
225 struct cmd_list_element
*c
, const char *value
)
227 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
231 static int debug_linux_nat_async
= 0;
233 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
234 struct cmd_list_element
*c
, const char *value
)
236 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
240 static int disable_randomization
= 1;
243 show_disable_randomization (struct ui_file
*file
, int from_tty
,
244 struct cmd_list_element
*c
, const char *value
)
246 #ifdef HAVE_PERSONALITY
247 fprintf_filtered (file
, _("\
248 Disabling randomization of debuggee's virtual address space is %s.\n"),
250 #else /* !HAVE_PERSONALITY */
252 Disabling randomization of debuggee's virtual address space is unsupported on\n\
253 this platform.\n"), file
);
254 #endif /* !HAVE_PERSONALITY */
258 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
260 #ifndef HAVE_PERSONALITY
262 Disabling randomization of debuggee's virtual address space is unsupported on\n\
264 #endif /* !HAVE_PERSONALITY */
267 static int linux_parent_pid
;
269 struct simple_pid_list
273 struct simple_pid_list
*next
;
275 struct simple_pid_list
*stopped_pids
;
277 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
278 can not be used, 1 if it can. */
280 static int linux_supports_tracefork_flag
= -1;
282 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
283 PTRACE_O_TRACEVFORKDONE. */
285 static int linux_supports_tracevforkdone_flag
= -1;
287 /* Async mode support */
289 /* Zero if the async mode, although enabled, is masked, which means
290 linux_nat_wait should behave as if async mode was off. */
291 static int linux_nat_async_mask_value
= 1;
293 /* The read/write ends of the pipe registered as waitable file in the
295 static int linux_nat_event_pipe
[2] = { -1, -1 };
297 /* Flush the event pipe. */
300 async_file_flush (void)
307 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
309 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
312 /* Put something (anything, doesn't matter what, or how much) in event
313 pipe, so that the select/poll in the event-loop realizes we have
314 something to process. */
317 async_file_mark (void)
321 /* It doesn't really matter what the pipe contains, as long we end
322 up with something in it. Might as well flush the previous
328 ret
= write (linux_nat_event_pipe
[1], "+", 1);
330 while (ret
== -1 && errno
== EINTR
);
332 /* Ignore EAGAIN. If the pipe is full, the event loop will already
333 be awakened anyway. */
336 static void linux_nat_async (void (*callback
)
337 (enum inferior_event_type event_type
, void *context
),
339 static int linux_nat_async_mask (int mask
);
340 static int kill_lwp (int lwpid
, int signo
);
342 static int stop_callback (struct lwp_info
*lp
, void *data
);
344 static void block_child_signals (sigset_t
*prev_mask
);
345 static void restore_child_signals_mask (sigset_t
*prev_mask
);
348 static struct lwp_info
*add_lwp (ptid_t ptid
);
349 static void purge_lwp_list (int pid
);
350 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
353 /* Trivial list manipulation functions to keep track of a list of
354 new stopped processes. */
356 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
358 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
360 new_pid
->status
= status
;
361 new_pid
->next
= *listp
;
366 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
368 struct simple_pid_list
**p
;
370 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
371 if ((*p
)->pid
== pid
)
373 struct simple_pid_list
*next
= (*p
)->next
;
374 *status
= (*p
)->status
;
383 linux_record_stopped_pid (int pid
, int status
)
385 add_to_pid_list (&stopped_pids
, pid
, status
);
389 /* A helper function for linux_test_for_tracefork, called after fork (). */
392 linux_tracefork_child (void)
396 ptrace (PTRACE_TRACEME
, 0, 0, 0);
397 kill (getpid (), SIGSTOP
);
402 /* Wrapper function for waitpid which handles EINTR. */
405 my_waitpid (int pid
, int *status
, int flags
)
411 ret
= waitpid (pid
, status
, flags
);
413 while (ret
== -1 && errno
== EINTR
);
418 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
420 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
421 we know that the feature is not available. This may change the tracing
422 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
424 However, if it succeeds, we don't know for sure that the feature is
425 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
426 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
427 fork tracing, and let it fork. If the process exits, we assume that we
428 can't use TRACEFORK; if we get the fork notification, and we can extract
429 the new child's PID, then we assume that we can. */
432 linux_test_for_tracefork (int original_pid
)
434 int child_pid
, ret
, status
;
438 /* We don't want those ptrace calls to be interrupted. */
439 block_child_signals (&prev_mask
);
441 linux_supports_tracefork_flag
= 0;
442 linux_supports_tracevforkdone_flag
= 0;
444 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
447 restore_child_signals_mask (&prev_mask
);
453 perror_with_name (("fork"));
456 linux_tracefork_child ();
458 ret
= my_waitpid (child_pid
, &status
, 0);
460 perror_with_name (("waitpid"));
461 else if (ret
!= child_pid
)
462 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
463 if (! WIFSTOPPED (status
))
464 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
466 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
469 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
472 warning (_("linux_test_for_tracefork: failed to kill child"));
473 restore_child_signals_mask (&prev_mask
);
477 ret
= my_waitpid (child_pid
, &status
, 0);
478 if (ret
!= child_pid
)
479 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
480 else if (!WIFSIGNALED (status
))
481 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
482 "killed child"), status
);
484 restore_child_signals_mask (&prev_mask
);
488 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
489 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
490 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
491 linux_supports_tracevforkdone_flag
= (ret
== 0);
493 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
495 warning (_("linux_test_for_tracefork: failed to resume child"));
497 ret
= my_waitpid (child_pid
, &status
, 0);
499 if (ret
== child_pid
&& WIFSTOPPED (status
)
500 && status
>> 16 == PTRACE_EVENT_FORK
)
503 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
504 if (ret
== 0 && second_pid
!= 0)
508 linux_supports_tracefork_flag
= 1;
509 my_waitpid (second_pid
, &second_status
, 0);
510 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
512 warning (_("linux_test_for_tracefork: failed to kill second child"));
513 my_waitpid (second_pid
, &status
, 0);
517 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
518 "(%d, status 0x%x)"), ret
, status
);
520 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
522 warning (_("linux_test_for_tracefork: failed to kill child"));
523 my_waitpid (child_pid
, &status
, 0);
525 restore_child_signals_mask (&prev_mask
);
528 /* Return non-zero iff we have tracefork functionality available.
529 This function also sets linux_supports_tracefork_flag. */
532 linux_supports_tracefork (int pid
)
534 if (linux_supports_tracefork_flag
== -1)
535 linux_test_for_tracefork (pid
);
536 return linux_supports_tracefork_flag
;
540 linux_supports_tracevforkdone (int pid
)
542 if (linux_supports_tracefork_flag
== -1)
543 linux_test_for_tracefork (pid
);
544 return linux_supports_tracevforkdone_flag
;
549 linux_enable_event_reporting (ptid_t ptid
)
551 int pid
= ptid_get_lwp (ptid
);
555 pid
= ptid_get_pid (ptid
);
557 if (! linux_supports_tracefork (pid
))
560 options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEEXEC
561 | PTRACE_O_TRACECLONE
;
562 if (linux_supports_tracevforkdone (pid
))
563 options
|= PTRACE_O_TRACEVFORKDONE
;
565 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
566 read-only process state. */
568 ptrace (PTRACE_SETOPTIONS
, pid
, 0, options
);
572 linux_child_post_attach (int pid
)
574 linux_enable_event_reporting (pid_to_ptid (pid
));
575 check_for_thread_db ();
579 linux_child_post_startup_inferior (ptid_t ptid
)
581 linux_enable_event_reporting (ptid
);
582 check_for_thread_db ();
586 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
590 int parent_pid
, child_pid
;
592 block_child_signals (&prev_mask
);
594 has_vforked
= (inferior_thread ()->pending_follow
.kind
595 == TARGET_WAITKIND_VFORKED
);
596 parent_pid
= ptid_get_lwp (inferior_ptid
);
598 parent_pid
= ptid_get_pid (inferior_ptid
);
599 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
602 linux_enable_event_reporting (pid_to_ptid (child_pid
));
606 /* We're already attached to the parent, by default. */
608 /* Before detaching from the child, remove all breakpoints from
609 it. If we forked, then this has already been taken care of
610 by infrun.c. If we vforked however, any breakpoint inserted
611 in the parent is visible in the child, even those added while
612 stopped in a vfork catchpoint. This won't actually modify
613 the breakpoint list, but will physically remove the
614 breakpoints from the child. This will remove the breakpoints
615 from the parent also, but they'll be reinserted below. */
617 detach_breakpoints (child_pid
);
619 /* Detach new forked process? */
622 if (info_verbose
|| debug_linux_nat
)
624 target_terminal_ours ();
625 fprintf_filtered (gdb_stdlog
,
626 "Detaching after fork from child process %d.\n",
630 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
634 struct inferior
*parent_inf
, *child_inf
;
636 struct cleanup
*old_chain
;
638 /* Add process to GDB's tables. */
639 child_inf
= add_inferior (child_pid
);
641 parent_inf
= current_inferior ();
642 child_inf
->attach_flag
= parent_inf
->attach_flag
;
643 copy_terminal_info (child_inf
, parent_inf
);
645 old_chain
= save_inferior_ptid ();
647 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
648 add_thread (inferior_ptid
);
649 lp
= add_lwp (inferior_ptid
);
652 check_for_thread_db ();
654 do_cleanups (old_chain
);
659 gdb_assert (linux_supports_tracefork_flag
>= 0);
660 if (linux_supports_tracevforkdone (0))
664 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
665 my_waitpid (parent_pid
, &status
, __WALL
);
666 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
667 warning (_("Unexpected waitpid result %06x when waiting for "
668 "vfork-done"), status
);
672 /* We can't insert breakpoints until the child has
673 finished with the shared memory region. We need to
674 wait until that happens. Ideal would be to just
676 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
677 - waitpid (parent_pid, &status, __WALL);
678 However, most architectures can't handle a syscall
679 being traced on the way out if it wasn't traced on
682 We might also think to loop, continuing the child
683 until it exits or gets a SIGTRAP. One problem is
684 that the child might call ptrace with PTRACE_TRACEME.
686 There's no simple and reliable way to figure out when
687 the vforked child will be done with its copy of the
688 shared memory. We could step it out of the syscall,
689 two instructions, let it go, and then single-step the
690 parent once. When we have hardware single-step, this
691 would work; with software single-step it could still
692 be made to work but we'd have to be able to insert
693 single-step breakpoints in the child, and we'd have
694 to insert -just- the single-step breakpoint in the
695 parent. Very awkward.
697 In the end, the best we can do is to make sure it
698 runs for a little while. Hopefully it will be out of
699 range of any breakpoints we reinsert. Usually this
700 is only the single-step breakpoint at vfork's return
706 /* Since we vforked, breakpoints were removed in the parent
707 too. Put them back. */
708 reattach_breakpoints (parent_pid
);
713 struct thread_info
*tp
;
714 struct inferior
*parent_inf
, *child_inf
;
717 /* Before detaching from the parent, remove all breakpoints from it. */
718 remove_breakpoints ();
720 if (info_verbose
|| debug_linux_nat
)
722 target_terminal_ours ();
723 fprintf_filtered (gdb_stdlog
,
724 "Attaching after fork to child process %d.\n",
728 /* Add the new inferior first, so that the target_detach below
729 doesn't unpush the target. */
731 child_inf
= add_inferior (child_pid
);
733 parent_inf
= current_inferior ();
734 child_inf
->attach_flag
= parent_inf
->attach_flag
;
735 copy_terminal_info (child_inf
, parent_inf
);
737 /* If we're vforking, we may want to hold on to the parent until
738 the child exits or execs. At exec time we can remove the old
739 breakpoints from the parent and detach it; at exit time we
740 could do the same (or even, sneakily, resume debugging it - the
741 child's exec has failed, or something similar).
743 This doesn't clean up "properly", because we can't call
744 target_detach, but that's OK; if the current target is "child",
745 then it doesn't need any further cleanups, and lin_lwp will
746 generally not encounter vfork (vfork is defined to fork
749 The holding part is very easy if we have VFORKDONE events;
750 but keeping track of both processes is beyond GDB at the
751 moment. So we don't expose the parent to the rest of GDB.
752 Instead we quietly hold onto it until such time as we can
757 struct lwp_info
*parent_lwp
;
759 linux_parent_pid
= parent_pid
;
761 /* Get rid of the inferior on the core side as well. */
762 inferior_ptid
= null_ptid
;
763 detach_inferior (parent_pid
);
765 /* Also get rid of all its lwps. We will detach from this
766 inferior soon-ish, but, we will still get an exit event
767 reported through waitpid when it exits. If we didn't get
768 rid of the lwps from our list, we would end up reporting
769 the inferior exit to the core, which would then try to
770 mourn a non-existing (from the core's perspective)
772 parent_lwp
= find_lwp_pid (pid_to_ptid (parent_pid
));
773 purge_lwp_list (GET_PID (parent_lwp
->ptid
));
774 linux_parent_pid
= parent_pid
;
776 else if (detach_fork
)
777 target_detach (NULL
, 0);
779 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
780 add_thread (inferior_ptid
);
781 lp
= add_lwp (inferior_ptid
);
784 check_for_thread_db ();
787 restore_child_signals_mask (&prev_mask
);
793 linux_child_insert_fork_catchpoint (int pid
)
795 if (! linux_supports_tracefork (pid
))
796 error (_("Your system does not support fork catchpoints."));
800 linux_child_insert_vfork_catchpoint (int pid
)
802 if (!linux_supports_tracefork (pid
))
803 error (_("Your system does not support vfork catchpoints."));
807 linux_child_insert_exec_catchpoint (int pid
)
809 if (!linux_supports_tracefork (pid
))
810 error (_("Your system does not support exec catchpoints."));
813 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
814 are processes sharing the same VM space. A multi-threaded process
815 is basically a group of such processes. However, such a grouping
816 is almost entirely a user-space issue; the kernel doesn't enforce
817 such a grouping at all (this might change in the future). In
818 general, we'll rely on the threads library (i.e. the GNU/Linux
819 Threads library) to provide such a grouping.
821 It is perfectly well possible to write a multi-threaded application
822 without the assistance of a threads library, by using the clone
823 system call directly. This module should be able to give some
824 rudimentary support for debugging such applications if developers
825 specify the CLONE_PTRACE flag in the clone system call, and are
826 using the Linux kernel 2.4 or above.
828 Note that there are some peculiarities in GNU/Linux that affect
831 - In general one should specify the __WCLONE flag to waitpid in
832 order to make it report events for any of the cloned processes
833 (and leave it out for the initial process). However, if a cloned
834 process has exited the exit status is only reported if the
835 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
836 we cannot use it since GDB must work on older systems too.
838 - When a traced, cloned process exits and is waited for by the
839 debugger, the kernel reassigns it to the original parent and
840 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
841 library doesn't notice this, which leads to the "zombie problem":
842 When debugged a multi-threaded process that spawns a lot of
843 threads will run out of processes, even if the threads exit,
844 because the "zombies" stay around. */
846 /* List of known LWPs. */
847 struct lwp_info
*lwp_list
;
850 /* Original signal mask. */
851 static sigset_t normal_mask
;
853 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
854 _initialize_linux_nat. */
855 static sigset_t suspend_mask
;
857 /* Signals to block to make that sigsuspend work. */
858 static sigset_t blocked_mask
;
860 /* SIGCHLD action. */
861 struct sigaction sigchld_action
;
863 /* Block child signals (SIGCHLD and linux threads signals), and store
864 the previous mask in PREV_MASK. */
867 block_child_signals (sigset_t
*prev_mask
)
869 /* Make sure SIGCHLD is blocked. */
870 if (!sigismember (&blocked_mask
, SIGCHLD
))
871 sigaddset (&blocked_mask
, SIGCHLD
);
873 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
876 /* Restore child signals mask, previously returned by
877 block_child_signals. */
880 restore_child_signals_mask (sigset_t
*prev_mask
)
882 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
886 /* Prototypes for local functions. */
887 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
888 static int linux_thread_alive (ptid_t ptid
);
889 static char *linux_child_pid_to_exec_file (int pid
);
890 static int cancel_breakpoint (struct lwp_info
*lp
);
893 /* Convert wait status STATUS to a string. Used for printing debug
897 status_to_str (int status
)
901 if (WIFSTOPPED (status
))
902 snprintf (buf
, sizeof (buf
), "%s (stopped)",
903 strsignal (WSTOPSIG (status
)));
904 else if (WIFSIGNALED (status
))
905 snprintf (buf
, sizeof (buf
), "%s (terminated)",
906 strsignal (WSTOPSIG (status
)));
908 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
913 /* Initialize the list of LWPs. Note that this module, contrary to
914 what GDB's generic threads layer does for its thread list,
915 re-initializes the LWP lists whenever we mourn or detach (which
916 doesn't involve mourning) the inferior. */
921 struct lwp_info
*lp
, *lpnext
;
923 for (lp
= lwp_list
; lp
; lp
= lpnext
)
932 /* Remove all LWPs belong to PID from the lwp list. */
935 purge_lwp_list (int pid
)
937 struct lwp_info
*lp
, *lpprev
, *lpnext
;
941 for (lp
= lwp_list
; lp
; lp
= lpnext
)
945 if (ptid_get_pid (lp
->ptid
) == pid
)
950 lpprev
->next
= lp
->next
;
959 /* Return the number of known LWPs in the tgid given by PID. */
967 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
968 if (ptid_get_pid (lp
->ptid
) == pid
)
974 /* Add the LWP specified by PID to the list. Return a pointer to the
975 structure describing the new LWP. The LWP should already be stopped
976 (with an exception for the very first LWP). */
978 static struct lwp_info
*
979 add_lwp (ptid_t ptid
)
983 gdb_assert (is_lwp (ptid
));
985 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
987 memset (lp
, 0, sizeof (struct lwp_info
));
989 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
996 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
997 linux_nat_new_thread (ptid
);
1002 /* Remove the LWP specified by PID from the list. */
1005 delete_lwp (ptid_t ptid
)
1007 struct lwp_info
*lp
, *lpprev
;
1011 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1012 if (ptid_equal (lp
->ptid
, ptid
))
1019 lpprev
->next
= lp
->next
;
1021 lwp_list
= lp
->next
;
1026 /* Return a pointer to the structure describing the LWP corresponding
1027 to PID. If no corresponding LWP could be found, return NULL. */
1029 static struct lwp_info
*
1030 find_lwp_pid (ptid_t ptid
)
1032 struct lwp_info
*lp
;
1036 lwp
= GET_LWP (ptid
);
1038 lwp
= GET_PID (ptid
);
1040 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1041 if (lwp
== GET_LWP (lp
->ptid
))
1047 /* Returns true if PTID matches filter FILTER. FILTER can be the wild
1048 card MINUS_ONE_PTID (all ptid match it); can be a ptid representing
1049 a process (ptid_is_pid returns true), in which case, all lwps of
1050 that give process match, lwps of other process do not; or, it can
1051 represent a specific thread, in which case, only that thread will
1052 match true. PTID must represent an LWP, it can never be a wild
1056 ptid_match (ptid_t ptid
, ptid_t filter
)
1058 /* Since both parameters have the same type, prevent easy mistakes
1060 gdb_assert (!ptid_equal (ptid
, minus_one_ptid
)
1061 && !ptid_equal (ptid
, null_ptid
));
1063 if (ptid_equal (filter
, minus_one_ptid
))
1065 if (ptid_is_pid (filter
)
1066 && ptid_get_pid (ptid
) == ptid_get_pid (filter
))
1068 else if (ptid_equal (ptid
, filter
))
1074 /* Call CALLBACK with its second argument set to DATA for every LWP in
1075 the list. If CALLBACK returns 1 for a particular LWP, return a
1076 pointer to the structure describing that LWP immediately.
1077 Otherwise return NULL. */
1080 iterate_over_lwps (ptid_t filter
,
1081 int (*callback
) (struct lwp_info
*, void *),
1084 struct lwp_info
*lp
, *lpnext
;
1086 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1090 if (ptid_match (lp
->ptid
, filter
))
1092 if ((*callback
) (lp
, data
))
1100 /* Update our internal state when changing from one checkpoint to
1101 another indicated by NEW_PTID. We can only switch single-threaded
1102 applications, so we only create one new LWP, and the previous list
1106 linux_nat_switch_fork (ptid_t new_ptid
)
1108 struct lwp_info
*lp
;
1110 purge_lwp_list (GET_PID (inferior_ptid
));
1112 lp
= add_lwp (new_ptid
);
1115 /* This changes the thread's ptid while preserving the gdb thread
1116 num. Also changes the inferior pid, while preserving the
1118 thread_change_ptid (inferior_ptid
, new_ptid
);
1120 /* We've just told GDB core that the thread changed target id, but,
1121 in fact, it really is a different thread, with different register
1123 registers_changed ();
1126 /* Handle the exit of a single thread LP. */
1129 exit_lwp (struct lwp_info
*lp
)
1131 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1135 if (print_thread_events
)
1136 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1138 delete_thread (lp
->ptid
);
1141 delete_lwp (lp
->ptid
);
1144 /* Return an lwp's tgid, found in `/proc/PID/status'. */
1147 linux_proc_get_tgid (int lwpid
)
1153 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
1154 status_file
= fopen (buf
, "r");
1155 if (status_file
!= NULL
)
1157 while (fgets (buf
, sizeof (buf
), status_file
))
1159 if (strncmp (buf
, "Tgid:", 5) == 0)
1161 tgid
= strtoul (buf
+ strlen ("Tgid:"), NULL
, 10);
1166 fclose (status_file
);
1172 /* Detect `T (stopped)' in `/proc/PID/status'.
1173 Other states including `T (tracing stop)' are reported as false. */
1176 pid_is_stopped (pid_t pid
)
1182 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1183 status_file
= fopen (buf
, "r");
1184 if (status_file
!= NULL
)
1188 while (fgets (buf
, sizeof (buf
), status_file
))
1190 if (strncmp (buf
, "State:", 6) == 0)
1196 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1198 fclose (status_file
);
1203 /* Wait for the LWP specified by LP, which we have just attached to.
1204 Returns a wait status for that LWP, to cache. */
1207 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1210 pid_t new_pid
, pid
= GET_LWP (ptid
);
1213 if (pid_is_stopped (pid
))
1215 if (debug_linux_nat
)
1216 fprintf_unfiltered (gdb_stdlog
,
1217 "LNPAW: Attaching to a stopped process\n");
1219 /* The process is definitely stopped. It is in a job control
1220 stop, unless the kernel predates the TASK_STOPPED /
1221 TASK_TRACED distinction, in which case it might be in a
1222 ptrace stop. Make sure it is in a ptrace stop; from there we
1223 can kill it, signal it, et cetera.
1225 First make sure there is a pending SIGSTOP. Since we are
1226 already attached, the process can not transition from stopped
1227 to running without a PTRACE_CONT; so we know this signal will
1228 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1229 probably already in the queue (unless this kernel is old
1230 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1231 is not an RT signal, it can only be queued once. */
1232 kill_lwp (pid
, SIGSTOP
);
1234 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1235 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1236 ptrace (PTRACE_CONT
, pid
, 0, 0);
1239 /* Make sure the initial process is stopped. The user-level threads
1240 layer might want to poke around in the inferior, and that won't
1241 work if things haven't stabilized yet. */
1242 new_pid
= my_waitpid (pid
, &status
, 0);
1243 if (new_pid
== -1 && errno
== ECHILD
)
1246 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1248 /* Try again with __WCLONE to check cloned processes. */
1249 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1253 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1255 if (WSTOPSIG (status
) != SIGSTOP
)
1258 if (debug_linux_nat
)
1259 fprintf_unfiltered (gdb_stdlog
,
1260 "LNPAW: Received %s after attaching\n",
1261 status_to_str (status
));
1267 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1268 if the new LWP could not be attached. */
1271 lin_lwp_attach_lwp (ptid_t ptid
)
1273 struct lwp_info
*lp
;
1276 gdb_assert (is_lwp (ptid
));
1278 block_child_signals (&prev_mask
);
1280 lp
= find_lwp_pid (ptid
);
1282 /* We assume that we're already attached to any LWP that has an id
1283 equal to the overall process id, and to any LWP that is already
1284 in our list of LWPs. If we're not seeing exit events from threads
1285 and we've had PID wraparound since we last tried to stop all threads,
1286 this assumption might be wrong; fortunately, this is very unlikely
1288 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1290 int status
, cloned
= 0, signalled
= 0;
1292 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1294 /* If we fail to attach to the thread, issue a warning,
1295 but continue. One way this can happen is if thread
1296 creation is interrupted; as of Linux kernel 2.6.19, a
1297 bug may place threads in the thread list and then fail
1299 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1300 safe_strerror (errno
));
1301 restore_child_signals_mask (&prev_mask
);
1305 if (debug_linux_nat
)
1306 fprintf_unfiltered (gdb_stdlog
,
1307 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1308 target_pid_to_str (ptid
));
1310 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1311 lp
= add_lwp (ptid
);
1313 lp
->cloned
= cloned
;
1314 lp
->signalled
= signalled
;
1315 if (WSTOPSIG (status
) != SIGSTOP
)
1318 lp
->status
= status
;
1321 target_post_attach (GET_LWP (lp
->ptid
));
1323 if (debug_linux_nat
)
1325 fprintf_unfiltered (gdb_stdlog
,
1326 "LLAL: waitpid %s received %s\n",
1327 target_pid_to_str (ptid
),
1328 status_to_str (status
));
1333 /* We assume that the LWP representing the original process is
1334 already stopped. Mark it as stopped in the data structure
1335 that the GNU/linux ptrace layer uses to keep track of
1336 threads. Note that this won't have already been done since
1337 the main thread will have, we assume, been stopped by an
1338 attach from a different layer. */
1340 lp
= add_lwp (ptid
);
1344 restore_child_signals_mask (&prev_mask
);
1349 linux_nat_create_inferior (struct target_ops
*ops
,
1350 char *exec_file
, char *allargs
, char **env
,
1353 #ifdef HAVE_PERSONALITY
1354 int personality_orig
= 0, personality_set
= 0;
1355 #endif /* HAVE_PERSONALITY */
1357 /* The fork_child mechanism is synchronous and calls target_wait, so
1358 we have to mask the async mode. */
1360 #ifdef HAVE_PERSONALITY
1361 if (disable_randomization
)
1364 personality_orig
= personality (0xffffffff);
1365 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1367 personality_set
= 1;
1368 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1370 if (errno
!= 0 || (personality_set
1371 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1372 warning (_("Error disabling address space randomization: %s"),
1373 safe_strerror (errno
));
1375 #endif /* HAVE_PERSONALITY */
1377 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1379 #ifdef HAVE_PERSONALITY
1380 if (personality_set
)
1383 personality (personality_orig
);
1385 warning (_("Error restoring address space randomization: %s"),
1386 safe_strerror (errno
));
1388 #endif /* HAVE_PERSONALITY */
1392 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1394 struct lwp_info
*lp
;
1398 linux_ops
->to_attach (ops
, args
, from_tty
);
1400 /* The ptrace base target adds the main thread with (pid,0,0)
1401 format. Decorate it with lwp info. */
1402 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1403 thread_change_ptid (inferior_ptid
, ptid
);
1405 /* Add the initial process as the first LWP to the list. */
1406 lp
= add_lwp (ptid
);
1408 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1412 /* Save the wait status to report later. */
1414 if (debug_linux_nat
)
1415 fprintf_unfiltered (gdb_stdlog
,
1416 "LNA: waitpid %ld, saving status %s\n",
1417 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1419 lp
->status
= status
;
1421 if (target_can_async_p ())
1422 target_async (inferior_event_handler
, 0);
1425 /* Get pending status of LP. */
1427 get_pending_status (struct lwp_info
*lp
, int *status
)
1429 struct target_waitstatus last
;
1432 get_last_target_status (&last_ptid
, &last
);
1434 /* If this lwp is the ptid that GDB is processing an event from, the
1435 signal will be in stop_signal. Otherwise, we may cache pending
1436 events in lp->status while trying to stop all threads (see
1437 stop_wait_callback). */
1443 enum target_signal signo
= TARGET_SIGNAL_0
;
1445 if (is_executing (lp
->ptid
))
1447 /* If the core thought this lwp was executing --- e.g., the
1448 executing property hasn't been updated yet, but the
1449 thread has been stopped with a stop_callback /
1450 stop_wait_callback sequence (see linux_nat_detach for
1451 example) --- we can only have pending events in the local
1453 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1457 /* If the core knows the thread is not executing, then we
1458 have the last signal recorded in
1459 thread_info->stop_signal. */
1461 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1462 signo
= tp
->stop_signal
;
1465 if (signo
!= TARGET_SIGNAL_0
1466 && !signal_pass_state (signo
))
1468 if (debug_linux_nat
)
1469 fprintf_unfiltered (gdb_stdlog
, "\
1470 GPT: lwp %s had signal %s, but it is in no pass state\n",
1471 target_pid_to_str (lp
->ptid
),
1472 target_signal_to_string (signo
));
1476 if (signo
!= TARGET_SIGNAL_0
)
1477 *status
= W_STOPCODE (target_signal_to_host (signo
));
1479 if (debug_linux_nat
)
1480 fprintf_unfiltered (gdb_stdlog
,
1481 "GPT: lwp %s as pending signal %s\n",
1482 target_pid_to_str (lp
->ptid
),
1483 target_signal_to_string (signo
));
1488 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1490 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1491 if (tp
->stop_signal
!= TARGET_SIGNAL_0
1492 && signal_pass_state (tp
->stop_signal
))
1493 *status
= W_STOPCODE (target_signal_to_host (tp
->stop_signal
));
1496 *status
= lp
->status
;
1503 detach_callback (struct lwp_info
*lp
, void *data
)
1505 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1507 if (debug_linux_nat
&& lp
->status
)
1508 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1509 strsignal (WSTOPSIG (lp
->status
)),
1510 target_pid_to_str (lp
->ptid
));
1512 /* If there is a pending SIGSTOP, get rid of it. */
1515 if (debug_linux_nat
)
1516 fprintf_unfiltered (gdb_stdlog
,
1517 "DC: Sending SIGCONT to %s\n",
1518 target_pid_to_str (lp
->ptid
));
1520 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1524 /* We don't actually detach from the LWP that has an id equal to the
1525 overall process id just yet. */
1526 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1530 /* Pass on any pending signal for this LWP. */
1531 get_pending_status (lp
, &status
);
1534 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1535 WSTOPSIG (status
)) < 0)
1536 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1537 safe_strerror (errno
));
1539 if (debug_linux_nat
)
1540 fprintf_unfiltered (gdb_stdlog
,
1541 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1542 target_pid_to_str (lp
->ptid
),
1543 strsignal (WSTOPSIG (status
)));
1545 delete_lwp (lp
->ptid
);
1552 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1556 enum target_signal sig
;
1557 struct lwp_info
*main_lwp
;
1559 pid
= GET_PID (inferior_ptid
);
1561 if (target_can_async_p ())
1562 linux_nat_async (NULL
, 0);
1564 /* Stop all threads before detaching. ptrace requires that the
1565 thread is stopped to sucessfully detach. */
1566 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1567 /* ... and wait until all of them have reported back that
1568 they're no longer running. */
1569 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1571 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1573 /* Only the initial process should be left right now. */
1574 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1576 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1578 /* Pass on any pending signal for the last LWP. */
1579 if ((args
== NULL
|| *args
== '\0')
1580 && get_pending_status (main_lwp
, &status
) != -1
1581 && WIFSTOPPED (status
))
1583 /* Put the signal number in ARGS so that inf_ptrace_detach will
1584 pass it along with PTRACE_DETACH. */
1586 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1587 fprintf_unfiltered (gdb_stdlog
,
1588 "LND: Sending signal %s to %s\n",
1590 target_pid_to_str (main_lwp
->ptid
));
1593 delete_lwp (main_lwp
->ptid
);
1595 if (forks_exist_p ())
1597 /* Multi-fork case. The current inferior_ptid is being detached
1598 from, but there are other viable forks to debug. Detach from
1599 the current fork, and context-switch to the first
1601 linux_fork_detach (args
, from_tty
);
1603 if (non_stop
&& target_can_async_p ())
1604 target_async (inferior_event_handler
, 0);
1607 linux_ops
->to_detach (ops
, args
, from_tty
);
1613 resume_callback (struct lwp_info
*lp
, void *data
)
1615 if (lp
->stopped
&& lp
->status
== 0)
1617 if (debug_linux_nat
)
1618 fprintf_unfiltered (gdb_stdlog
,
1619 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1620 target_pid_to_str (lp
->ptid
));
1622 linux_ops
->to_resume (linux_ops
,
1623 pid_to_ptid (GET_LWP (lp
->ptid
)),
1624 0, TARGET_SIGNAL_0
);
1625 if (debug_linux_nat
)
1626 fprintf_unfiltered (gdb_stdlog
,
1627 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1628 target_pid_to_str (lp
->ptid
));
1631 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1633 else if (lp
->stopped
&& debug_linux_nat
)
1634 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1635 target_pid_to_str (lp
->ptid
));
1636 else if (debug_linux_nat
)
1637 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1638 target_pid_to_str (lp
->ptid
));
1644 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1651 resume_set_callback (struct lwp_info
*lp
, void *data
)
1658 linux_nat_resume (struct target_ops
*ops
,
1659 ptid_t ptid
, int step
, enum target_signal signo
)
1662 struct lwp_info
*lp
;
1665 if (debug_linux_nat
)
1666 fprintf_unfiltered (gdb_stdlog
,
1667 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1668 step
? "step" : "resume",
1669 target_pid_to_str (ptid
),
1670 signo
? strsignal (signo
) : "0",
1671 target_pid_to_str (inferior_ptid
));
1673 block_child_signals (&prev_mask
);
1675 /* A specific PTID means `step only this process id'. */
1676 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1677 || ptid_is_pid (ptid
));
1681 /* Mark the lwps we're resuming as resumed. */
1682 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
1683 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1686 iterate_over_lwps (minus_one_ptid
, resume_set_callback
, NULL
);
1688 /* See if it's the current inferior that should be handled
1691 lp
= find_lwp_pid (inferior_ptid
);
1693 lp
= find_lwp_pid (ptid
);
1694 gdb_assert (lp
!= NULL
);
1696 /* Remember if we're stepping. */
1699 /* If we have a pending wait status for this thread, there is no
1700 point in resuming the process. But first make sure that
1701 linux_nat_wait won't preemptively handle the event - we
1702 should never take this short-circuit if we are going to
1703 leave LP running, since we have skipped resuming all the
1704 other threads. This bit of code needs to be synchronized
1705 with linux_nat_wait. */
1707 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1710 struct inferior
*inf
;
1712 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
1714 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1716 /* Defer to common code if we're gaining control of the
1718 if (inf
->stop_soon
== NO_STOP_QUIETLY
1719 && signal_stop_state (saved_signo
) == 0
1720 && signal_print_state (saved_signo
) == 0
1721 && signal_pass_state (saved_signo
) == 1)
1723 if (debug_linux_nat
)
1724 fprintf_unfiltered (gdb_stdlog
,
1725 "LLR: Not short circuiting for ignored "
1726 "status 0x%x\n", lp
->status
);
1728 /* FIXME: What should we do if we are supposed to continue
1729 this thread with a signal? */
1730 gdb_assert (signo
== TARGET_SIGNAL_0
);
1731 signo
= saved_signo
;
1738 /* FIXME: What should we do if we are supposed to continue
1739 this thread with a signal? */
1740 gdb_assert (signo
== TARGET_SIGNAL_0
);
1742 if (debug_linux_nat
)
1743 fprintf_unfiltered (gdb_stdlog
,
1744 "LLR: Short circuiting for status 0x%x\n",
1747 restore_child_signals_mask (&prev_mask
);
1748 if (target_can_async_p ())
1750 target_async (inferior_event_handler
, 0);
1751 /* Tell the event loop we have something to process. */
1757 /* Mark LWP as not stopped to prevent it from being continued by
1762 iterate_over_lwps (ptid
, resume_callback
, NULL
);
1764 /* Convert to something the lower layer understands. */
1765 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1767 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
1768 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1770 if (debug_linux_nat
)
1771 fprintf_unfiltered (gdb_stdlog
,
1772 "LLR: %s %s, %s (resume event thread)\n",
1773 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1774 target_pid_to_str (ptid
),
1775 signo
? strsignal (signo
) : "0");
1777 restore_child_signals_mask (&prev_mask
);
1778 if (target_can_async_p ())
1779 target_async (inferior_event_handler
, 0);
1782 /* Issue kill to specified lwp. */
1784 static int tkill_failed
;
1787 kill_lwp (int lwpid
, int signo
)
1791 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1792 fails, then we are not using nptl threads and we should be using kill. */
1794 #ifdef HAVE_TKILL_SYSCALL
1797 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1798 if (errno
!= ENOSYS
)
1805 return kill (lwpid
, signo
);
1808 /* Handle a GNU/Linux extended wait response. If we see a clone
1809 event, we need to add the new LWP to our list (and not report the
1810 trap to higher layers). This function returns non-zero if the
1811 event should be ignored and we should wait again. If STOPPING is
1812 true, the new LWP remains stopped, otherwise it is continued. */
1815 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
1818 int pid
= GET_LWP (lp
->ptid
);
1819 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1820 struct lwp_info
*new_lp
= NULL
;
1821 int event
= status
>> 16;
1823 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
1824 || event
== PTRACE_EVENT_CLONE
)
1826 unsigned long new_pid
;
1829 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
1831 /* If we haven't already seen the new PID stop, wait for it now. */
1832 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
1834 /* The new child has a pending SIGSTOP. We can't affect it until it
1835 hits the SIGSTOP, but we're already attached. */
1836 ret
= my_waitpid (new_pid
, &status
,
1837 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
1839 perror_with_name (_("waiting for new child"));
1840 else if (ret
!= new_pid
)
1841 internal_error (__FILE__
, __LINE__
,
1842 _("wait returned unexpected PID %d"), ret
);
1843 else if (!WIFSTOPPED (status
))
1844 internal_error (__FILE__
, __LINE__
,
1845 _("wait returned unexpected status 0x%x"), status
);
1848 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
1850 if (event
== PTRACE_EVENT_FORK
1851 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
1853 struct fork_info
*fp
;
1855 /* Handle checkpointing by linux-fork.c here as a special
1856 case. We don't want the follow-fork-mode or 'catch fork'
1857 to interfere with this. */
1859 /* This won't actually modify the breakpoint list, but will
1860 physically remove the breakpoints from the child. */
1861 detach_breakpoints (new_pid
);
1863 /* Retain child fork in ptrace (stopped) state. */
1864 fp
= find_fork_pid (new_pid
);
1866 fp
= add_fork (new_pid
);
1868 /* Report as spurious, so that infrun doesn't want to follow
1869 this fork. We're actually doing an infcall in
1871 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1872 linux_enable_event_reporting (pid_to_ptid (new_pid
));
1874 /* Report the stop to the core. */
1878 if (event
== PTRACE_EVENT_FORK
)
1879 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1880 else if (event
== PTRACE_EVENT_VFORK
)
1881 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1884 struct cleanup
*old_chain
;
1886 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1887 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
1889 new_lp
->stopped
= 1;
1891 if (WSTOPSIG (status
) != SIGSTOP
)
1893 /* This can happen if someone starts sending signals to
1894 the new thread before it gets a chance to run, which
1895 have a lower number than SIGSTOP (e.g. SIGUSR1).
1896 This is an unlikely case, and harder to handle for
1897 fork / vfork than for clone, so we do not try - but
1898 we handle it for clone events here. We'll send
1899 the other signal on to the thread below. */
1901 new_lp
->signalled
= 1;
1908 /* Add the new thread to GDB's lists as soon as possible
1911 1) the frontend doesn't have to wait for a stop to
1914 2) we tag it with the correct running state. */
1916 /* If the thread_db layer is active, let it know about
1917 this new thread, and add it to GDB's list. */
1918 if (!thread_db_attach_lwp (new_lp
->ptid
))
1920 /* We're not using thread_db. Add it to GDB's
1922 target_post_attach (GET_LWP (new_lp
->ptid
));
1923 add_thread (new_lp
->ptid
);
1928 set_running (new_lp
->ptid
, 1);
1929 set_executing (new_lp
->ptid
, 1);
1935 new_lp
->stopped
= 0;
1936 new_lp
->resumed
= 1;
1937 ptrace (PTRACE_CONT
, new_pid
, 0,
1938 status
? WSTOPSIG (status
) : 0);
1941 if (debug_linux_nat
)
1942 fprintf_unfiltered (gdb_stdlog
,
1943 "LHEW: Got clone event from LWP %ld, resuming\n",
1944 GET_LWP (lp
->ptid
));
1945 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1953 if (event
== PTRACE_EVENT_EXEC
)
1955 if (debug_linux_nat
)
1956 fprintf_unfiltered (gdb_stdlog
,
1957 "LHEW: Got exec event from LWP %ld\n",
1958 GET_LWP (lp
->ptid
));
1960 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1961 ourstatus
->value
.execd_pathname
1962 = xstrdup (linux_child_pid_to_exec_file (pid
));
1964 if (linux_parent_pid
)
1966 detach_breakpoints (linux_parent_pid
);
1967 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
1969 linux_parent_pid
= 0;
1972 /* At this point, all inserted breakpoints are gone. Doing this
1973 as soon as we detect an exec prevents the badness of deleting
1974 a breakpoint writing the current "shadow contents" to lift
1975 the bp. That shadow is NOT valid after an exec.
1977 Note that we have to do this after the detach_breakpoints
1978 call above, otherwise breakpoints wouldn't be lifted from the
1979 parent on a vfork, because detach_breakpoints would think
1980 that breakpoints are not inserted. */
1981 mark_breakpoints_out ();
1985 internal_error (__FILE__
, __LINE__
,
1986 _("unknown ptrace event %d"), event
);
1989 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1993 wait_lwp (struct lwp_info
*lp
)
1997 int thread_dead
= 0;
1999 gdb_assert (!lp
->stopped
);
2000 gdb_assert (lp
->status
== 0);
2002 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
2003 if (pid
== -1 && errno
== ECHILD
)
2005 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
2006 if (pid
== -1 && errno
== ECHILD
)
2008 /* The thread has previously exited. We need to delete it
2009 now because, for some vendor 2.4 kernels with NPTL
2010 support backported, there won't be an exit event unless
2011 it is the main thread. 2.6 kernels will report an exit
2012 event for each thread that exits, as expected. */
2014 if (debug_linux_nat
)
2015 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2016 target_pid_to_str (lp
->ptid
));
2022 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2024 if (debug_linux_nat
)
2026 fprintf_unfiltered (gdb_stdlog
,
2027 "WL: waitpid %s received %s\n",
2028 target_pid_to_str (lp
->ptid
),
2029 status_to_str (status
));
2033 /* Check if the thread has exited. */
2034 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2037 if (debug_linux_nat
)
2038 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2039 target_pid_to_str (lp
->ptid
));
2048 gdb_assert (WIFSTOPPED (status
));
2050 /* Handle GNU/Linux's extended waitstatus for trace events. */
2051 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2053 if (debug_linux_nat
)
2054 fprintf_unfiltered (gdb_stdlog
,
2055 "WL: Handling extended status 0x%06x\n",
2057 if (linux_handle_extended_wait (lp
, status
, 1))
2058 return wait_lwp (lp
);
2064 /* Save the most recent siginfo for LP. This is currently only called
2065 for SIGTRAP; some ports use the si_addr field for
2066 target_stopped_data_address. In the future, it may also be used to
2067 restore the siginfo of requeued signals. */
2070 save_siginfo (struct lwp_info
*lp
)
2073 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2074 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2077 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2080 /* Send a SIGSTOP to LP. */
2083 stop_callback (struct lwp_info
*lp
, void *data
)
2085 if (!lp
->stopped
&& !lp
->signalled
)
2089 if (debug_linux_nat
)
2091 fprintf_unfiltered (gdb_stdlog
,
2092 "SC: kill %s **<SIGSTOP>**\n",
2093 target_pid_to_str (lp
->ptid
));
2096 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2097 if (debug_linux_nat
)
2099 fprintf_unfiltered (gdb_stdlog
,
2100 "SC: lwp kill %d %s\n",
2102 errno
? safe_strerror (errno
) : "ERRNO-OK");
2106 gdb_assert (lp
->status
== 0);
2112 /* Return non-zero if LWP PID has a pending SIGINT. */
2115 linux_nat_has_pending_sigint (int pid
)
2117 sigset_t pending
, blocked
, ignored
;
2120 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2122 if (sigismember (&pending
, SIGINT
)
2123 && !sigismember (&ignored
, SIGINT
))
2129 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2132 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2134 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2135 flag to consume the next one. */
2136 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2137 && WSTOPSIG (lp
->status
) == SIGINT
)
2140 lp
->ignore_sigint
= 1;
2145 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2146 This function is called after we know the LWP has stopped; if the LWP
2147 stopped before the expected SIGINT was delivered, then it will never have
2148 arrived. Also, if the signal was delivered to a shared queue and consumed
2149 by a different thread, it will never be delivered to this LWP. */
2152 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2154 if (!lp
->ignore_sigint
)
2157 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2159 if (debug_linux_nat
)
2160 fprintf_unfiltered (gdb_stdlog
,
2161 "MCIS: Clearing bogus flag for %s\n",
2162 target_pid_to_str (lp
->ptid
));
2163 lp
->ignore_sigint
= 0;
2167 /* Wait until LP is stopped. */
2170 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2176 status
= wait_lwp (lp
);
2180 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2181 && WSTOPSIG (status
) == SIGINT
)
2183 lp
->ignore_sigint
= 0;
2186 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2187 if (debug_linux_nat
)
2188 fprintf_unfiltered (gdb_stdlog
,
2189 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2190 target_pid_to_str (lp
->ptid
),
2191 errno
? safe_strerror (errno
) : "OK");
2193 return stop_wait_callback (lp
, NULL
);
2196 maybe_clear_ignore_sigint (lp
);
2198 if (WSTOPSIG (status
) != SIGSTOP
)
2200 if (WSTOPSIG (status
) == SIGTRAP
)
2202 /* If a LWP other than the LWP that we're reporting an
2203 event for has hit a GDB breakpoint (as opposed to
2204 some random trap signal), then just arrange for it to
2205 hit it again later. We don't keep the SIGTRAP status
2206 and don't forward the SIGTRAP signal to the LWP. We
2207 will handle the current event, eventually we will
2208 resume all LWPs, and this one will get its breakpoint
2211 If we do not do this, then we run the risk that the
2212 user will delete or disable the breakpoint, but the
2213 thread will have already tripped on it. */
2215 /* Save the trap's siginfo in case we need it later. */
2218 /* Now resume this LWP and get the SIGSTOP event. */
2220 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2221 if (debug_linux_nat
)
2223 fprintf_unfiltered (gdb_stdlog
,
2224 "PTRACE_CONT %s, 0, 0 (%s)\n",
2225 target_pid_to_str (lp
->ptid
),
2226 errno
? safe_strerror (errno
) : "OK");
2228 fprintf_unfiltered (gdb_stdlog
,
2229 "SWC: Candidate SIGTRAP event in %s\n",
2230 target_pid_to_str (lp
->ptid
));
2232 /* Hold this event/waitstatus while we check to see if
2233 there are any more (we still want to get that SIGSTOP). */
2234 stop_wait_callback (lp
, NULL
);
2236 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2237 there's another event, throw it back into the
2241 if (debug_linux_nat
)
2242 fprintf_unfiltered (gdb_stdlog
,
2243 "SWC: kill %s, %s\n",
2244 target_pid_to_str (lp
->ptid
),
2245 status_to_str ((int) status
));
2246 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2249 /* Save the sigtrap event. */
2250 lp
->status
= status
;
2255 /* The thread was stopped with a signal other than
2256 SIGSTOP, and didn't accidentally trip a breakpoint. */
2258 if (debug_linux_nat
)
2260 fprintf_unfiltered (gdb_stdlog
,
2261 "SWC: Pending event %s in %s\n",
2262 status_to_str ((int) status
),
2263 target_pid_to_str (lp
->ptid
));
2265 /* Now resume this LWP and get the SIGSTOP event. */
2267 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2268 if (debug_linux_nat
)
2269 fprintf_unfiltered (gdb_stdlog
,
2270 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2271 target_pid_to_str (lp
->ptid
),
2272 errno
? safe_strerror (errno
) : "OK");
2274 /* Hold this event/waitstatus while we check to see if
2275 there are any more (we still want to get that SIGSTOP). */
2276 stop_wait_callback (lp
, NULL
);
2278 /* If the lp->status field is still empty, use it to
2279 hold this event. If not, then this event must be
2280 returned to the event queue of the LWP. */
2283 if (debug_linux_nat
)
2285 fprintf_unfiltered (gdb_stdlog
,
2286 "SWC: kill %s, %s\n",
2287 target_pid_to_str (lp
->ptid
),
2288 status_to_str ((int) status
));
2290 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2293 lp
->status
= status
;
2299 /* We caught the SIGSTOP that we intended to catch, so
2300 there's no SIGSTOP pending. */
2309 /* Return non-zero if LP has a wait status pending. */
2312 status_callback (struct lwp_info
*lp
, void *data
)
2314 /* Only report a pending wait status if we pretend that this has
2315 indeed been resumed. */
2316 /* We check for lp->waitstatus in addition to lp->status, because we
2317 can have pending process exits recorded in lp->waitstatus, and
2318 W_EXITCODE(0,0) == 0. */
2319 return ((lp
->status
!= 0
2320 || lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2324 /* Return non-zero if LP isn't stopped. */
2327 running_callback (struct lwp_info
*lp
, void *data
)
2329 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2332 /* Count the LWP's that have had events. */
2335 count_events_callback (struct lwp_info
*lp
, void *data
)
2339 gdb_assert (count
!= NULL
);
2341 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2342 if (lp
->status
!= 0 && lp
->resumed
2343 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2349 /* Select the LWP (if any) that is currently being single-stepped. */
2352 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2354 if (lp
->step
&& lp
->status
!= 0)
2360 /* Select the Nth LWP that has had a SIGTRAP event. */
2363 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2365 int *selector
= data
;
2367 gdb_assert (selector
!= NULL
);
2369 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2370 if (lp
->status
!= 0 && lp
->resumed
2371 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2372 if ((*selector
)-- == 0)
2379 cancel_breakpoint (struct lwp_info
*lp
)
2381 /* Arrange for a breakpoint to be hit again later. We don't keep
2382 the SIGTRAP status and don't forward the SIGTRAP signal to the
2383 LWP. We will handle the current event, eventually we will resume
2384 this LWP, and this breakpoint will trap again.
2386 If we do not do this, then we run the risk that the user will
2387 delete or disable the breakpoint, but the LWP will have already
2390 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2391 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2394 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2395 if (breakpoint_inserted_here_p (pc
))
2397 if (debug_linux_nat
)
2398 fprintf_unfiltered (gdb_stdlog
,
2399 "CB: Push back breakpoint for %s\n",
2400 target_pid_to_str (lp
->ptid
));
2402 /* Back up the PC if necessary. */
2403 if (gdbarch_decr_pc_after_break (gdbarch
))
2404 regcache_write_pc (regcache
, pc
);
2412 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2414 struct lwp_info
*event_lp
= data
;
2416 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2420 /* If a LWP other than the LWP that we're reporting an event for has
2421 hit a GDB breakpoint (as opposed to some random trap signal),
2422 then just arrange for it to hit it again later. We don't keep
2423 the SIGTRAP status and don't forward the SIGTRAP signal to the
2424 LWP. We will handle the current event, eventually we will resume
2425 all LWPs, and this one will get its breakpoint trap again.
2427 If we do not do this, then we run the risk that the user will
2428 delete or disable the breakpoint, but the LWP will have already
2432 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2433 && cancel_breakpoint (lp
))
2434 /* Throw away the SIGTRAP. */
2440 /* Select one LWP out of those that have events pending. */
2443 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2446 int random_selector
;
2447 struct lwp_info
*event_lp
;
2449 /* Record the wait status for the original LWP. */
2450 (*orig_lp
)->status
= *status
;
2452 /* Give preference to any LWP that is being single-stepped. */
2453 event_lp
= iterate_over_lwps (filter
,
2454 select_singlestep_lwp_callback
, NULL
);
2455 if (event_lp
!= NULL
)
2457 if (debug_linux_nat
)
2458 fprintf_unfiltered (gdb_stdlog
,
2459 "SEL: Select single-step %s\n",
2460 target_pid_to_str (event_lp
->ptid
));
2464 /* No single-stepping LWP. Select one at random, out of those
2465 which have had SIGTRAP events. */
2467 /* First see how many SIGTRAP events we have. */
2468 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2470 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2471 random_selector
= (int)
2472 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2474 if (debug_linux_nat
&& num_events
> 1)
2475 fprintf_unfiltered (gdb_stdlog
,
2476 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2477 num_events
, random_selector
);
2479 event_lp
= iterate_over_lwps (filter
,
2480 select_event_lwp_callback
,
2484 if (event_lp
!= NULL
)
2486 /* Switch the event LWP. */
2487 *orig_lp
= event_lp
;
2488 *status
= event_lp
->status
;
2491 /* Flush the wait status for the event LWP. */
2492 (*orig_lp
)->status
= 0;
2495 /* Return non-zero if LP has been resumed. */
2498 resumed_callback (struct lwp_info
*lp
, void *data
)
2503 /* Stop an active thread, verify it still exists, then resume it. */
2506 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2508 struct lwp_info
*ptr
;
2510 if (!lp
->stopped
&& !lp
->signalled
)
2512 stop_callback (lp
, NULL
);
2513 stop_wait_callback (lp
, NULL
);
2514 /* Resume if the lwp still exists. */
2515 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2518 resume_callback (lp
, NULL
);
2519 resume_set_callback (lp
, NULL
);
2525 /* Check if we should go on and pass this event to common code.
2526 Return the affected lwp if we are, or NULL otherwise. */
2527 static struct lwp_info
*
2528 linux_nat_filter_event (int lwpid
, int status
, int options
)
2530 struct lwp_info
*lp
;
2532 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2534 /* Check for stop events reported by a process we didn't already
2535 know about - anything not already in our LWP list.
2537 If we're expecting to receive stopped processes after
2538 fork, vfork, and clone events, then we'll just add the
2539 new one to our list and go back to waiting for the event
2540 to be reported - the stopped process might be returned
2541 from waitpid before or after the event is. */
2542 if (WIFSTOPPED (status
) && !lp
)
2544 linux_record_stopped_pid (lwpid
, status
);
2548 /* Make sure we don't report an event for the exit of an LWP not in
2549 our list, i.e. not part of the current process. This can happen
2550 if we detach from a program we original forked and then it
2552 if (!WIFSTOPPED (status
) && !lp
)
2555 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2556 CLONE_PTRACE processes which do not use the thread library -
2557 otherwise we wouldn't find the new LWP this way. That doesn't
2558 currently work, and the following code is currently unreachable
2559 due to the two blocks above. If it's fixed some day, this code
2560 should be broken out into a function so that we can also pick up
2561 LWPs from the new interface. */
2564 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2565 if (options
& __WCLONE
)
2568 gdb_assert (WIFSTOPPED (status
)
2569 && WSTOPSIG (status
) == SIGSTOP
);
2572 if (!in_thread_list (inferior_ptid
))
2574 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2575 GET_PID (inferior_ptid
));
2576 add_thread (inferior_ptid
);
2579 add_thread (lp
->ptid
);
2582 /* Save the trap's siginfo in case we need it later. */
2583 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2586 /* Handle GNU/Linux's extended waitstatus for trace events. */
2587 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2589 if (debug_linux_nat
)
2590 fprintf_unfiltered (gdb_stdlog
,
2591 "LLW: Handling extended status 0x%06x\n",
2593 if (linux_handle_extended_wait (lp
, status
, 0))
2597 /* Check if the thread has exited. */
2598 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
2599 && num_lwps (GET_PID (lp
->ptid
)) > 1)
2601 /* If this is the main thread, we must stop all threads and verify
2602 if they are still alive. This is because in the nptl thread model
2603 on Linux 2.4, there is no signal issued for exiting LWPs
2604 other than the main thread. We only get the main thread exit
2605 signal once all child threads have already exited. If we
2606 stop all the threads and use the stop_wait_callback to check
2607 if they have exited we can determine whether this signal
2608 should be ignored or whether it means the end of the debugged
2609 application, regardless of which threading model is being
2611 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2614 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
2615 stop_and_resume_callback
, NULL
);
2618 if (debug_linux_nat
)
2619 fprintf_unfiltered (gdb_stdlog
,
2620 "LLW: %s exited.\n",
2621 target_pid_to_str (lp
->ptid
));
2623 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
2625 /* If there is at least one more LWP, then the exit signal
2626 was not the end of the debugged application and should be
2633 /* Check if the current LWP has previously exited. In the nptl
2634 thread model, LWPs other than the main thread do not issue
2635 signals when they exit so we must check whenever the thread has
2636 stopped. A similar check is made in stop_wait_callback(). */
2637 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
2639 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
2641 if (debug_linux_nat
)
2642 fprintf_unfiltered (gdb_stdlog
,
2643 "LLW: %s exited.\n",
2644 target_pid_to_str (lp
->ptid
));
2648 /* Make sure there is at least one thread running. */
2649 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
2651 /* Discard the event. */
2655 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2656 an attempt to stop an LWP. */
2658 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2660 if (debug_linux_nat
)
2661 fprintf_unfiltered (gdb_stdlog
,
2662 "LLW: Delayed SIGSTOP caught for %s.\n",
2663 target_pid_to_str (lp
->ptid
));
2665 /* This is a delayed SIGSTOP. */
2668 registers_changed ();
2670 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2671 lp
->step
, TARGET_SIGNAL_0
);
2672 if (debug_linux_nat
)
2673 fprintf_unfiltered (gdb_stdlog
,
2674 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2676 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2677 target_pid_to_str (lp
->ptid
));
2680 gdb_assert (lp
->resumed
);
2682 /* Discard the event. */
2686 /* Make sure we don't report a SIGINT that we have already displayed
2687 for another thread. */
2688 if (lp
->ignore_sigint
2689 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2691 if (debug_linux_nat
)
2692 fprintf_unfiltered (gdb_stdlog
,
2693 "LLW: Delayed SIGINT caught for %s.\n",
2694 target_pid_to_str (lp
->ptid
));
2696 /* This is a delayed SIGINT. */
2697 lp
->ignore_sigint
= 0;
2699 registers_changed ();
2700 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2701 lp
->step
, TARGET_SIGNAL_0
);
2702 if (debug_linux_nat
)
2703 fprintf_unfiltered (gdb_stdlog
,
2704 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2706 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2707 target_pid_to_str (lp
->ptid
));
2710 gdb_assert (lp
->resumed
);
2712 /* Discard the event. */
2716 /* An interesting event. */
2722 linux_nat_wait_1 (struct target_ops
*ops
,
2723 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
2726 static sigset_t prev_mask
;
2727 struct lwp_info
*lp
= NULL
;
2732 if (debug_linux_nat_async
)
2733 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
2735 /* The first time we get here after starting a new inferior, we may
2736 not have added it to the LWP list yet - this is the earliest
2737 moment at which we know its PID. */
2738 if (ptid_is_pid (inferior_ptid
))
2740 /* Upgrade the main thread's ptid. */
2741 thread_change_ptid (inferior_ptid
,
2742 BUILD_LWP (GET_PID (inferior_ptid
),
2743 GET_PID (inferior_ptid
)));
2745 lp
= add_lwp (inferior_ptid
);
2749 /* Make sure SIGCHLD is blocked. */
2750 block_child_signals (&prev_mask
);
2752 if (ptid_equal (ptid
, minus_one_ptid
))
2754 else if (ptid_is_pid (ptid
))
2755 /* A request to wait for a specific tgid. This is not possible
2756 with waitpid, so instead, we wait for any child, and leave
2757 children we're not interested in right now with a pending
2758 status to report later. */
2761 pid
= GET_LWP (ptid
);
2767 /* Make sure there is at least one LWP that has been resumed. */
2768 gdb_assert (iterate_over_lwps (ptid
, resumed_callback
, NULL
));
2770 /* First check if there is a LWP with a wait status pending. */
2773 /* Any LWP that's been resumed will do. */
2774 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
2777 status
= lp
->status
;
2780 if (debug_linux_nat
&& status
)
2781 fprintf_unfiltered (gdb_stdlog
,
2782 "LLW: Using pending wait status %s for %s.\n",
2783 status_to_str (status
),
2784 target_pid_to_str (lp
->ptid
));
2787 /* But if we don't find one, we'll have to wait, and check both
2788 cloned and uncloned processes. We start with the cloned
2790 options
= __WCLONE
| WNOHANG
;
2792 else if (is_lwp (ptid
))
2794 if (debug_linux_nat
)
2795 fprintf_unfiltered (gdb_stdlog
,
2796 "LLW: Waiting for specific LWP %s.\n",
2797 target_pid_to_str (ptid
));
2799 /* We have a specific LWP to check. */
2800 lp
= find_lwp_pid (ptid
);
2802 status
= lp
->status
;
2805 if (debug_linux_nat
&& status
)
2806 fprintf_unfiltered (gdb_stdlog
,
2807 "LLW: Using pending wait status %s for %s.\n",
2808 status_to_str (status
),
2809 target_pid_to_str (lp
->ptid
));
2811 /* If we have to wait, take into account whether PID is a cloned
2812 process or not. And we have to convert it to something that
2813 the layer beneath us can understand. */
2814 options
= lp
->cloned
? __WCLONE
: 0;
2815 pid
= GET_LWP (ptid
);
2817 /* We check for lp->waitstatus in addition to lp->status,
2818 because we can have pending process exits recorded in
2819 lp->status and W_EXITCODE(0,0) == 0. We should probably have
2820 an additional lp->status_p flag. */
2821 if (status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
2825 if (lp
&& lp
->signalled
)
2827 /* A pending SIGSTOP may interfere with the normal stream of
2828 events. In a typical case where interference is a problem,
2829 we have a SIGSTOP signal pending for LWP A while
2830 single-stepping it, encounter an event in LWP B, and take the
2831 pending SIGSTOP while trying to stop LWP A. After processing
2832 the event in LWP B, LWP A is continued, and we'll never see
2833 the SIGTRAP associated with the last time we were
2834 single-stepping LWP A. */
2836 /* Resume the thread. It should halt immediately returning the
2838 registers_changed ();
2839 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2840 lp
->step
, TARGET_SIGNAL_0
);
2841 if (debug_linux_nat
)
2842 fprintf_unfiltered (gdb_stdlog
,
2843 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2844 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2845 target_pid_to_str (lp
->ptid
));
2847 gdb_assert (lp
->resumed
);
2849 /* This should catch the pending SIGSTOP. */
2850 stop_wait_callback (lp
, NULL
);
2853 if (!target_can_async_p ())
2855 /* Causes SIGINT to be passed on to the attached process. */
2859 /* Translate generic target_wait options into waitpid options. */
2860 if (target_options
& TARGET_WNOHANG
)
2867 lwpid
= my_waitpid (pid
, &status
, options
);
2871 gdb_assert (pid
== -1 || lwpid
== pid
);
2873 if (debug_linux_nat
)
2875 fprintf_unfiltered (gdb_stdlog
,
2876 "LLW: waitpid %ld received %s\n",
2877 (long) lwpid
, status_to_str (status
));
2880 lp
= linux_nat_filter_event (lwpid
, status
, options
);
2883 && ptid_is_pid (ptid
)
2884 && ptid_get_pid (lp
->ptid
) != ptid_get_pid (ptid
))
2886 if (debug_linux_nat
)
2887 fprintf (stderr
, "LWP %ld got an event %06x, leaving pending.\n",
2888 ptid_get_lwp (lp
->ptid
), status
);
2890 if (WIFSTOPPED (status
))
2892 if (WSTOPSIG (status
) != SIGSTOP
)
2894 lp
->status
= status
;
2896 stop_callback (lp
, NULL
);
2898 /* Resume in order to collect the sigstop. */
2899 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2901 stop_wait_callback (lp
, NULL
);
2909 else if (WIFEXITED (status
) || WIFSIGNALED (status
))
2911 if (debug_linux_nat
)
2912 fprintf (stderr
, "Process %ld exited while stopping LWPs\n",
2913 ptid_get_lwp (lp
->ptid
));
2915 /* This was the last lwp in the process. Since
2916 events are serialized to GDB core, and we can't
2917 report this one right now, but GDB core and the
2918 other target layers will want to be notified
2919 about the exit code/signal, leave the status
2920 pending for the next time we're able to report
2922 lp
->status
= status
;
2924 /* Prevent trying to stop this thread again. We'll
2925 never try to resume it because it has a pending
2929 /* Dead LWP's aren't expected to reported a pending
2933 /* Store the pending event in the waitstatus as
2934 well, because W_EXITCODE(0,0) == 0. */
2935 store_waitstatus (&lp
->waitstatus
, status
);
2949 /* waitpid did return something. Restart over. */
2950 options
|= __WCLONE
;
2958 /* Alternate between checking cloned and uncloned processes. */
2959 options
^= __WCLONE
;
2961 /* And every time we have checked both:
2962 In async mode, return to event loop;
2963 In sync mode, suspend waiting for a SIGCHLD signal. */
2964 if (options
& __WCLONE
)
2966 if (target_options
& TARGET_WNOHANG
)
2968 /* No interesting event. */
2969 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2971 if (debug_linux_nat_async
)
2972 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
2974 restore_child_signals_mask (&prev_mask
);
2975 return minus_one_ptid
;
2978 sigsuspend (&suspend_mask
);
2982 /* We shouldn't end up here unless we want to try again. */
2983 gdb_assert (lp
== NULL
);
2986 if (!target_can_async_p ())
2987 clear_sigint_trap ();
2991 /* Don't report signals that GDB isn't interested in, such as
2992 signals that are neither printed nor stopped upon. Stopping all
2993 threads can be a bit time-consuming so if we want decent
2994 performance with heavily multi-threaded programs, especially when
2995 they're using a high frequency timer, we'd better avoid it if we
2998 if (WIFSTOPPED (status
))
3000 int signo
= target_signal_from_host (WSTOPSIG (status
));
3001 struct inferior
*inf
;
3003 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
3006 /* Defer to common code if we get a signal while
3007 single-stepping, since that may need special care, e.g. to
3008 skip the signal handler, or, if we're gaining control of the
3011 && inf
->stop_soon
== NO_STOP_QUIETLY
3012 && signal_stop_state (signo
) == 0
3013 && signal_print_state (signo
) == 0
3014 && signal_pass_state (signo
) == 1)
3016 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3017 here? It is not clear we should. GDB may not expect
3018 other threads to run. On the other hand, not resuming
3019 newly attached threads may cause an unwanted delay in
3020 getting them running. */
3021 registers_changed ();
3022 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3024 if (debug_linux_nat
)
3025 fprintf_unfiltered (gdb_stdlog
,
3026 "LLW: %s %s, %s (preempt 'handle')\n",
3028 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3029 target_pid_to_str (lp
->ptid
),
3030 signo
? strsignal (signo
) : "0");
3037 /* Only do the below in all-stop, as we currently use SIGINT
3038 to implement target_stop (see linux_nat_stop) in
3040 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3042 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3043 forwarded to the entire process group, that is, all LWPs
3044 will receive it - unless they're using CLONE_THREAD to
3045 share signals. Since we only want to report it once, we
3046 mark it as ignored for all LWPs except this one. */
3047 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3048 set_ignore_sigint
, NULL
);
3049 lp
->ignore_sigint
= 0;
3052 maybe_clear_ignore_sigint (lp
);
3056 /* This LWP is stopped now. */
3059 if (debug_linux_nat
)
3060 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3061 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3065 /* Now stop all other LWP's ... */
3066 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3068 /* ... and wait until all of them have reported back that
3069 they're no longer running. */
3070 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3072 /* If we're not waiting for a specific LWP, choose an event LWP
3073 from among those that have had events. Giving equal priority
3074 to all LWPs that have had events helps prevent
3077 select_event_lwp (ptid
, &lp
, &status
);
3080 /* Now that we've selected our final event LWP, cancel any
3081 breakpoints in other LWPs that have hit a GDB breakpoint. See
3082 the comment in cancel_breakpoints_callback to find out why. */
3083 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3085 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3087 if (debug_linux_nat
)
3088 fprintf_unfiltered (gdb_stdlog
,
3089 "LLW: trap ptid is %s.\n",
3090 target_pid_to_str (lp
->ptid
));
3093 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3095 *ourstatus
= lp
->waitstatus
;
3096 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3099 store_waitstatus (ourstatus
, status
);
3101 if (debug_linux_nat_async
)
3102 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3104 restore_child_signals_mask (&prev_mask
);
3109 linux_nat_wait (struct target_ops
*ops
,
3110 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3115 if (debug_linux_nat
)
3116 fprintf_unfiltered (gdb_stdlog
, "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
3118 /* Flush the async file first. */
3119 if (target_can_async_p ())
3120 async_file_flush ();
3122 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3124 /* If we requested any event, and something came out, assume there
3125 may be more. If we requested a specific lwp or process, also
3126 assume there may be more. */
3127 if (target_can_async_p ()
3128 && (ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3129 || !ptid_equal (ptid
, minus_one_ptid
)))
3132 /* Get ready for the next event. */
3133 if (target_can_async_p ())
3134 target_async (inferior_event_handler
, 0);
3140 kill_callback (struct lwp_info
*lp
, void *data
)
3143 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3144 if (debug_linux_nat
)
3145 fprintf_unfiltered (gdb_stdlog
,
3146 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3147 target_pid_to_str (lp
->ptid
),
3148 errno
? safe_strerror (errno
) : "OK");
3154 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3158 /* We must make sure that there are no pending events (delayed
3159 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3160 program doesn't interfere with any following debugging session. */
3162 /* For cloned processes we must check both with __WCLONE and
3163 without, since the exit status of a cloned process isn't reported
3169 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3170 if (pid
!= (pid_t
) -1)
3172 if (debug_linux_nat
)
3173 fprintf_unfiltered (gdb_stdlog
,
3174 "KWC: wait %s received unknown.\n",
3175 target_pid_to_str (lp
->ptid
));
3176 /* The Linux kernel sometimes fails to kill a thread
3177 completely after PTRACE_KILL; that goes from the stop
3178 point in do_fork out to the one in
3179 get_signal_to_deliever and waits again. So kill it
3181 kill_callback (lp
, NULL
);
3184 while (pid
== GET_LWP (lp
->ptid
));
3186 gdb_assert (pid
== -1 && errno
== ECHILD
);
3191 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3192 if (pid
!= (pid_t
) -1)
3194 if (debug_linux_nat
)
3195 fprintf_unfiltered (gdb_stdlog
,
3196 "KWC: wait %s received unk.\n",
3197 target_pid_to_str (lp
->ptid
));
3198 /* See the call to kill_callback above. */
3199 kill_callback (lp
, NULL
);
3202 while (pid
== GET_LWP (lp
->ptid
));
3204 gdb_assert (pid
== -1 && errno
== ECHILD
);
3209 linux_nat_kill (struct target_ops
*ops
)
3211 struct target_waitstatus last
;
3215 /* If we're stopped while forking and we haven't followed yet,
3216 kill the other task. We need to do this first because the
3217 parent will be sleeping if this is a vfork. */
3219 get_last_target_status (&last_ptid
, &last
);
3221 if (last
.kind
== TARGET_WAITKIND_FORKED
3222 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3224 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3228 if (forks_exist_p ())
3229 linux_fork_killall ();
3232 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3233 /* Stop all threads before killing them, since ptrace requires
3234 that the thread is stopped to sucessfully PTRACE_KILL. */
3235 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3236 /* ... and wait until all of them have reported back that
3237 they're no longer running. */
3238 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3240 /* Kill all LWP's ... */
3241 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3243 /* ... and wait until we've flushed all events. */
3244 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3247 target_mourn_inferior ();
3251 linux_nat_mourn_inferior (struct target_ops
*ops
)
3253 purge_lwp_list (ptid_get_pid (inferior_ptid
));
3255 if (! forks_exist_p ())
3256 /* Normal case, no other forks available. */
3257 linux_ops
->to_mourn_inferior (ops
);
3259 /* Multi-fork case. The current inferior_ptid has exited, but
3260 there are other viable forks to debug. Delete the exiting
3261 one and context-switch to the first available. */
3262 linux_fork_mourn_inferior ();
3265 /* Convert a native/host siginfo object, into/from the siginfo in the
3266 layout of the inferiors' architecture. */
3269 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3273 if (linux_nat_siginfo_fixup
!= NULL
)
3274 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3276 /* If there was no callback, or the callback didn't do anything,
3277 then just do a straight memcpy. */
3281 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
3283 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
3288 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3289 const char *annex
, gdb_byte
*readbuf
,
3290 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3293 struct siginfo siginfo
;
3294 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
3296 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3297 gdb_assert (readbuf
|| writebuf
);
3299 pid
= GET_LWP (inferior_ptid
);
3301 pid
= GET_PID (inferior_ptid
);
3303 if (offset
> sizeof (siginfo
))
3307 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3311 /* When GDB is built as a 64-bit application, ptrace writes into
3312 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3313 inferior with a 64-bit GDB should look the same as debugging it
3314 with a 32-bit GDB, we need to convert it. GDB core always sees
3315 the converted layout, so any read/write will have to be done
3317 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3319 if (offset
+ len
> sizeof (siginfo
))
3320 len
= sizeof (siginfo
) - offset
;
3322 if (readbuf
!= NULL
)
3323 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3326 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3328 /* Convert back to ptrace layout before flushing it out. */
3329 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3332 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3341 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3342 const char *annex
, gdb_byte
*readbuf
,
3343 const gdb_byte
*writebuf
,
3344 ULONGEST offset
, LONGEST len
)
3346 struct cleanup
*old_chain
;
3349 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3350 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3353 /* The target is connected but no live inferior is selected. Pass
3354 this request down to a lower stratum (e.g., the executable
3356 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3359 old_chain
= save_inferior_ptid ();
3361 if (is_lwp (inferior_ptid
))
3362 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3364 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3367 do_cleanups (old_chain
);
3372 linux_thread_alive (ptid_t ptid
)
3376 gdb_assert (is_lwp (ptid
));
3378 /* Send signal 0 instead of anything ptrace, because ptracing a
3379 running thread errors out claiming that the thread doesn't
3381 err
= kill_lwp (GET_LWP (ptid
), 0);
3383 if (debug_linux_nat
)
3384 fprintf_unfiltered (gdb_stdlog
,
3385 "LLTA: KILL(SIG0) %s (%s)\n",
3386 target_pid_to_str (ptid
),
3387 err
? safe_strerror (err
) : "OK");
3396 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3398 return linux_thread_alive (ptid
);
3402 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3404 static char buf
[64];
3407 && (GET_PID (ptid
) != GET_LWP (ptid
)
3408 || num_lwps (GET_PID (ptid
)) > 1))
3410 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3414 return normal_pid_to_str (ptid
);
3417 /* Accepts an integer PID; Returns a string representing a file that
3418 can be opened to get the symbols for the child process. */
3421 linux_child_pid_to_exec_file (int pid
)
3423 char *name1
, *name2
;
3425 name1
= xmalloc (MAXPATHLEN
);
3426 name2
= xmalloc (MAXPATHLEN
);
3427 make_cleanup (xfree
, name1
);
3428 make_cleanup (xfree
, name2
);
3429 memset (name2
, 0, MAXPATHLEN
);
3431 sprintf (name1
, "/proc/%d/exe", pid
);
3432 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3438 /* Service function for corefiles and info proc. */
3441 read_mapping (FILE *mapfile
,
3446 char *device
, long long *inode
, char *filename
)
3448 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3449 addr
, endaddr
, permissions
, offset
, device
, inode
);
3452 if (ret
> 0 && ret
!= EOF
)
3454 /* Eat everything up to EOL for the filename. This will prevent
3455 weird filenames (such as one with embedded whitespace) from
3456 confusing this code. It also makes this code more robust in
3457 respect to annotations the kernel may add after the filename.
3459 Note the filename is used for informational purposes
3461 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3464 return (ret
!= 0 && ret
!= EOF
);
3467 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3468 regions in the inferior for a corefile. */
3471 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3473 int, int, int, void *), void *obfd
)
3475 int pid
= PIDGET (inferior_ptid
);
3476 char mapsfilename
[MAXPATHLEN
];
3478 long long addr
, endaddr
, size
, offset
, inode
;
3479 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3480 int read
, write
, exec
;
3482 struct cleanup
*cleanup
;
3484 /* Compose the filename for the /proc memory map, and open it. */
3485 sprintf (mapsfilename
, "/proc/%d/maps", pid
);
3486 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3487 error (_("Could not open %s."), mapsfilename
);
3488 cleanup
= make_cleanup_fclose (mapsfile
);
3491 fprintf_filtered (gdb_stdout
,
3492 "Reading memory regions from %s\n", mapsfilename
);
3494 /* Now iterate until end-of-file. */
3495 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3496 &offset
, &device
[0], &inode
, &filename
[0]))
3498 size
= endaddr
- addr
;
3500 /* Get the segment's permissions. */
3501 read
= (strchr (permissions
, 'r') != 0);
3502 write
= (strchr (permissions
, 'w') != 0);
3503 exec
= (strchr (permissions
, 'x') != 0);
3507 fprintf_filtered (gdb_stdout
,
3508 "Save segment, %lld bytes at %s (%c%c%c)",
3509 size
, paddress (target_gdbarch
, addr
),
3511 write
? 'w' : ' ', exec
? 'x' : ' ');
3513 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3514 fprintf_filtered (gdb_stdout
, "\n");
3517 /* Invoke the callback function to create the corefile
3519 func (addr
, size
, read
, write
, exec
, obfd
);
3521 do_cleanups (cleanup
);
3526 find_signalled_thread (struct thread_info
*info
, void *data
)
3528 if (info
->stop_signal
!= TARGET_SIGNAL_0
3529 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3535 static enum target_signal
3536 find_stop_signal (void)
3538 struct thread_info
*info
=
3539 iterate_over_threads (find_signalled_thread
, NULL
);
3542 return info
->stop_signal
;
3544 return TARGET_SIGNAL_0
;
3547 /* Records the thread's register state for the corefile note
3551 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3552 char *note_data
, int *note_size
,
3553 enum target_signal stop_signal
)
3555 gdb_gregset_t gregs
;
3556 gdb_fpregset_t fpregs
;
3557 unsigned long lwp
= ptid_get_lwp (ptid
);
3558 struct gdbarch
*gdbarch
= target_gdbarch
;
3559 struct regcache
*regcache
= get_thread_arch_regcache (ptid
, gdbarch
);
3560 const struct regset
*regset
;
3562 struct cleanup
*old_chain
;
3563 struct core_regset_section
*sect_list
;
3566 old_chain
= save_inferior_ptid ();
3567 inferior_ptid
= ptid
;
3568 target_fetch_registers (regcache
, -1);
3569 do_cleanups (old_chain
);
3571 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3572 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3575 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3576 sizeof (gregs
))) != NULL
3577 && regset
->collect_regset
!= NULL
)
3578 regset
->collect_regset (regset
, regcache
, -1,
3579 &gregs
, sizeof (gregs
));
3581 fill_gregset (regcache
, &gregs
, -1);
3583 note_data
= (char *) elfcore_write_prstatus (obfd
,
3587 stop_signal
, &gregs
);
3589 /* The loop below uses the new struct core_regset_section, which stores
3590 the supported section names and sizes for the core file. Note that
3591 note PRSTATUS needs to be treated specially. But the other notes are
3592 structurally the same, so they can benefit from the new struct. */
3593 if (core_regset_p
&& sect_list
!= NULL
)
3594 while (sect_list
->sect_name
!= NULL
)
3596 /* .reg was already handled above. */
3597 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3602 regset
= gdbarch_regset_from_core_section (gdbarch
,
3603 sect_list
->sect_name
,
3605 gdb_assert (regset
&& regset
->collect_regset
);
3606 gdb_regset
= xmalloc (sect_list
->size
);
3607 regset
->collect_regset (regset
, regcache
, -1,
3608 gdb_regset
, sect_list
->size
);
3609 note_data
= (char *) elfcore_write_register_note (obfd
,
3612 sect_list
->sect_name
,
3619 /* For architectures that does not have the struct core_regset_section
3620 implemented, we use the old method. When all the architectures have
3621 the new support, the code below should be deleted. */
3625 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3626 sizeof (fpregs
))) != NULL
3627 && regset
->collect_regset
!= NULL
)
3628 regset
->collect_regset (regset
, regcache
, -1,
3629 &fpregs
, sizeof (fpregs
));
3631 fill_fpregset (regcache
, &fpregs
, -1);
3633 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3636 &fpregs
, sizeof (fpregs
));
3642 struct linux_nat_corefile_thread_data
3648 enum target_signal stop_signal
;
3651 /* Called by gdbthread.c once per thread. Records the thread's
3652 register state for the corefile note section. */
3655 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3657 struct linux_nat_corefile_thread_data
*args
= data
;
3659 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3669 /* Enumerate spufs IDs for process PID. */
3672 iterate_over_spus (int pid
, void (*callback
) (void *, int), void *data
)
3676 struct dirent
*entry
;
3678 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
3679 dir
= opendir (path
);
3684 while ((entry
= readdir (dir
)) != NULL
)
3690 fd
= atoi (entry
->d_name
);
3694 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
3695 if (stat (path
, &st
) != 0)
3697 if (!S_ISDIR (st
.st_mode
))
3700 if (statfs (path
, &stfs
) != 0)
3702 if (stfs
.f_type
!= SPUFS_MAGIC
)
3705 callback (data
, fd
);
3711 /* Generate corefile notes for SPU contexts. */
3713 struct linux_spu_corefile_data
3721 linux_spu_corefile_callback (void *data
, int fd
)
3723 struct linux_spu_corefile_data
*args
= data
;
3726 static const char *spu_files
[] =
3748 for (i
= 0; i
< sizeof (spu_files
) / sizeof (spu_files
[0]); i
++)
3750 char annex
[32], note_name
[32];
3754 xsnprintf (annex
, sizeof annex
, "%d/%s", fd
, spu_files
[i
]);
3755 spu_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_SPU
,
3759 xsnprintf (note_name
, sizeof note_name
, "SPU/%s", annex
);
3760 args
->note_data
= elfcore_write_note (args
->obfd
, args
->note_data
,
3761 args
->note_size
, note_name
,
3762 NT_SPU
, spu_data
, spu_len
);
3769 linux_spu_make_corefile_notes (bfd
*obfd
, char *note_data
, int *note_size
)
3771 struct linux_spu_corefile_data args
;
3773 args
.note_data
= note_data
;
3774 args
.note_size
= note_size
;
3776 iterate_over_spus (PIDGET (inferior_ptid
),
3777 linux_spu_corefile_callback
, &args
);
3779 return args
.note_data
;
3782 /* Fills the "to_make_corefile_note" target vector. Builds the note
3783 section for a corefile, and returns it in a malloc buffer. */
3786 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
3788 struct linux_nat_corefile_thread_data thread_args
;
3789 struct cleanup
*old_chain
;
3790 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
3791 char fname
[16] = { '\0' };
3792 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
3793 char psargs
[80] = { '\0' };
3794 char *note_data
= NULL
;
3795 ptid_t current_ptid
= inferior_ptid
;
3796 ptid_t filter
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3800 if (get_exec_file (0))
3802 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
3803 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3804 if (get_inferior_args ())
3807 char *psargs_end
= psargs
+ sizeof (psargs
);
3809 /* linux_elfcore_write_prpsinfo () handles zero unterminated
3811 string_end
= memchr (psargs
, 0, sizeof (psargs
));
3812 if (string_end
!= NULL
)
3814 *string_end
++ = ' ';
3815 strncpy (string_end
, get_inferior_args (),
3816 psargs_end
- string_end
);
3819 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3821 note_size
, fname
, psargs
);
3824 /* Dump information for threads. */
3825 thread_args
.obfd
= obfd
;
3826 thread_args
.note_data
= note_data
;
3827 thread_args
.note_size
= note_size
;
3828 thread_args
.num_notes
= 0;
3829 thread_args
.stop_signal
= find_stop_signal ();
3830 iterate_over_lwps (filter
, linux_nat_corefile_thread_callback
, &thread_args
);
3831 gdb_assert (thread_args
.num_notes
!= 0);
3832 note_data
= thread_args
.note_data
;
3834 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3838 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3839 "CORE", NT_AUXV
, auxv
, auxv_len
);
3843 note_data
= linux_spu_make_corefile_notes (obfd
, note_data
, note_size
);
3845 make_cleanup (xfree
, note_data
);
3849 /* Implement the "info proc" command. */
3852 linux_nat_info_proc_cmd (char *args
, int from_tty
)
3854 /* A long is used for pid instead of an int to avoid a loss of precision
3855 compiler warning from the output of strtoul. */
3856 long pid
= PIDGET (inferior_ptid
);
3859 char buffer
[MAXPATHLEN
];
3860 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
3873 /* Break up 'args' into an argv array. */
3874 argv
= gdb_buildargv (args
);
3875 make_cleanup_freeargv (argv
);
3877 while (argv
!= NULL
&& *argv
!= NULL
)
3879 if (isdigit (argv
[0][0]))
3881 pid
= strtoul (argv
[0], NULL
, 10);
3883 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
3887 else if (strcmp (argv
[0], "status") == 0)
3891 else if (strcmp (argv
[0], "stat") == 0)
3895 else if (strcmp (argv
[0], "cmd") == 0)
3899 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
3903 else if (strcmp (argv
[0], "cwd") == 0)
3907 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
3913 /* [...] (future options here) */
3918 error (_("No current process: you must name one."));
3920 sprintf (fname1
, "/proc/%ld", pid
);
3921 if (stat (fname1
, &dummy
) != 0)
3922 error (_("No /proc directory: '%s'"), fname1
);
3924 printf_filtered (_("process %ld\n"), pid
);
3925 if (cmdline_f
|| all
)
3927 sprintf (fname1
, "/proc/%ld/cmdline", pid
);
3928 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3930 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
3931 if (fgets (buffer
, sizeof (buffer
), procfile
))
3932 printf_filtered ("cmdline = '%s'\n", buffer
);
3934 warning (_("unable to read '%s'"), fname1
);
3935 do_cleanups (cleanup
);
3938 warning (_("unable to open /proc file '%s'"), fname1
);
3942 sprintf (fname1
, "/proc/%ld/cwd", pid
);
3943 memset (fname2
, 0, sizeof (fname2
));
3944 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3945 printf_filtered ("cwd = '%s'\n", fname2
);
3947 warning (_("unable to read link '%s'"), fname1
);
3951 sprintf (fname1
, "/proc/%ld/exe", pid
);
3952 memset (fname2
, 0, sizeof (fname2
));
3953 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
3954 printf_filtered ("exe = '%s'\n", fname2
);
3956 warning (_("unable to read link '%s'"), fname1
);
3958 if (mappings_f
|| all
)
3960 sprintf (fname1
, "/proc/%ld/maps", pid
);
3961 if ((procfile
= fopen (fname1
, "r")) != NULL
)
3963 long long addr
, endaddr
, size
, offset
, inode
;
3964 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3965 struct cleanup
*cleanup
;
3967 cleanup
= make_cleanup_fclose (procfile
);
3968 printf_filtered (_("Mapped address spaces:\n\n"));
3969 if (gdbarch_addr_bit (target_gdbarch
) == 32)
3971 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3974 " Size", " Offset", "objfile");
3978 printf_filtered (" %18s %18s %10s %10s %7s\n",
3981 " Size", " Offset", "objfile");
3984 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
3985 &offset
, &device
[0], &inode
, &filename
[0]))
3987 size
= endaddr
- addr
;
3989 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3990 calls here (and possibly above) should be abstracted
3991 out into their own functions? Andrew suggests using
3992 a generic local_address_string instead to print out
3993 the addresses; that makes sense to me, too. */
3995 if (gdbarch_addr_bit (target_gdbarch
) == 32)
3997 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3998 (unsigned long) addr
, /* FIXME: pr_addr */
3999 (unsigned long) endaddr
,
4001 (unsigned int) offset
,
4002 filename
[0] ? filename
: "");
4006 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
4007 (unsigned long) addr
, /* FIXME: pr_addr */
4008 (unsigned long) endaddr
,
4010 (unsigned int) offset
,
4011 filename
[0] ? filename
: "");
4015 do_cleanups (cleanup
);
4018 warning (_("unable to open /proc file '%s'"), fname1
);
4020 if (status_f
|| all
)
4022 sprintf (fname1
, "/proc/%ld/status", pid
);
4023 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4025 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
4026 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
4027 puts_filtered (buffer
);
4028 do_cleanups (cleanup
);
4031 warning (_("unable to open /proc file '%s'"), fname1
);
4035 sprintf (fname1
, "/proc/%ld/stat", pid
);
4036 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4041 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
4043 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4044 printf_filtered (_("Process: %d\n"), itmp
);
4045 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
4046 printf_filtered (_("Exec file: %s\n"), buffer
);
4047 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
4048 printf_filtered (_("State: %c\n"), ctmp
);
4049 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4050 printf_filtered (_("Parent process: %d\n"), itmp
);
4051 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4052 printf_filtered (_("Process group: %d\n"), itmp
);
4053 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4054 printf_filtered (_("Session id: %d\n"), itmp
);
4055 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4056 printf_filtered (_("TTY: %d\n"), itmp
);
4057 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4058 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
4059 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4060 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
4061 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4062 printf_filtered (_("Minor faults (no memory page): %lu\n"),
4063 (unsigned long) ltmp
);
4064 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4065 printf_filtered (_("Minor faults, children: %lu\n"),
4066 (unsigned long) ltmp
);
4067 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4068 printf_filtered (_("Major faults (memory page faults): %lu\n"),
4069 (unsigned long) ltmp
);
4070 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4071 printf_filtered (_("Major faults, children: %lu\n"),
4072 (unsigned long) ltmp
);
4073 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4074 printf_filtered (_("utime: %ld\n"), ltmp
);
4075 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4076 printf_filtered (_("stime: %ld\n"), ltmp
);
4077 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4078 printf_filtered (_("utime, children: %ld\n"), ltmp
);
4079 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4080 printf_filtered (_("stime, children: %ld\n"), ltmp
);
4081 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4082 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
4084 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4085 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
4086 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4087 printf_filtered (_("jiffies until next timeout: %lu\n"),
4088 (unsigned long) ltmp
);
4089 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4090 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
4091 (unsigned long) ltmp
);
4092 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4093 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
4095 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4096 printf_filtered (_("Virtual memory size: %lu\n"),
4097 (unsigned long) ltmp
);
4098 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4099 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
4100 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4101 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
4102 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4103 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
4104 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4105 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
4106 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4107 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
4108 #if 0 /* Don't know how architecture-dependent the rest is...
4109 Anyway the signal bitmap info is available from "status". */
4110 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4111 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
4112 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4113 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
4114 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4115 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
4116 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4117 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
4118 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4119 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
4120 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4121 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
4122 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4123 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
4125 do_cleanups (cleanup
);
4128 warning (_("unable to open /proc file '%s'"), fname1
);
4132 /* Implement the to_xfer_partial interface for memory reads using the /proc
4133 filesystem. Because we can use a single read() call for /proc, this
4134 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4135 but it doesn't support writes. */
4138 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4139 const char *annex
, gdb_byte
*readbuf
,
4140 const gdb_byte
*writebuf
,
4141 ULONGEST offset
, LONGEST len
)
4147 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4150 /* Don't bother for one word. */
4151 if (len
< 3 * sizeof (long))
4154 /* We could keep this file open and cache it - possibly one per
4155 thread. That requires some juggling, but is even faster. */
4156 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4157 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4161 /* If pread64 is available, use it. It's faster if the kernel
4162 supports it (only one syscall), and it's 64-bit safe even on
4163 32-bit platforms (for instance, SPARC debugging a SPARC64
4166 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4168 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4179 /* Enumerate spufs IDs for process PID. */
4181 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
4183 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
4185 LONGEST written
= 0;
4188 struct dirent
*entry
;
4190 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4191 dir
= opendir (path
);
4196 while ((entry
= readdir (dir
)) != NULL
)
4202 fd
= atoi (entry
->d_name
);
4206 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4207 if (stat (path
, &st
) != 0)
4209 if (!S_ISDIR (st
.st_mode
))
4212 if (statfs (path
, &stfs
) != 0)
4214 if (stfs
.f_type
!= SPUFS_MAGIC
)
4217 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4219 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4229 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4230 object type, using the /proc file system. */
4232 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4233 const char *annex
, gdb_byte
*readbuf
,
4234 const gdb_byte
*writebuf
,
4235 ULONGEST offset
, LONGEST len
)
4240 int pid
= PIDGET (inferior_ptid
);
4247 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4250 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4251 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4256 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4263 ret
= write (fd
, writebuf
, (size_t) len
);
4265 ret
= read (fd
, readbuf
, (size_t) len
);
4272 /* Parse LINE as a signal set and add its set bits to SIGS. */
4275 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4277 int len
= strlen (line
) - 1;
4281 if (line
[len
] != '\n')
4282 error (_("Could not parse signal set: %s"), line
);
4290 if (*p
>= '0' && *p
<= '9')
4292 else if (*p
>= 'a' && *p
<= 'f')
4293 digit
= *p
- 'a' + 10;
4295 error (_("Could not parse signal set: %s"), line
);
4300 sigaddset (sigs
, signum
+ 1);
4302 sigaddset (sigs
, signum
+ 2);
4304 sigaddset (sigs
, signum
+ 3);
4306 sigaddset (sigs
, signum
+ 4);
4312 /* Find process PID's pending signals from /proc/pid/status and set
4316 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
4319 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4321 struct cleanup
*cleanup
;
4323 sigemptyset (pending
);
4324 sigemptyset (blocked
);
4325 sigemptyset (ignored
);
4326 sprintf (fname
, "/proc/%d/status", pid
);
4327 procfile
= fopen (fname
, "r");
4328 if (procfile
== NULL
)
4329 error (_("Could not open %s"), fname
);
4330 cleanup
= make_cleanup_fclose (procfile
);
4332 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4334 /* Normal queued signals are on the SigPnd line in the status
4335 file. However, 2.6 kernels also have a "shared" pending
4336 queue for delivering signals to a thread group, so check for
4339 Unfortunately some Red Hat kernels include the shared pending
4340 queue but not the ShdPnd status field. */
4342 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4343 add_line_to_sigset (buffer
+ 8, pending
);
4344 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4345 add_line_to_sigset (buffer
+ 8, pending
);
4346 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4347 add_line_to_sigset (buffer
+ 8, blocked
);
4348 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4349 add_line_to_sigset (buffer
+ 8, ignored
);
4352 do_cleanups (cleanup
);
4356 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4357 const char *annex
, gdb_byte
*readbuf
,
4358 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4360 /* We make the process list snapshot when the object starts to be
4362 static const char *buf
;
4363 static LONGEST len_avail
= -1;
4364 static struct obstack obstack
;
4368 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4370 if (strcmp (annex
, "processes") != 0)
4373 gdb_assert (readbuf
&& !writebuf
);
4377 if (len_avail
!= -1 && len_avail
!= 0)
4378 obstack_free (&obstack
, NULL
);
4381 obstack_init (&obstack
);
4382 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4384 dirp
= opendir ("/proc");
4388 while ((dp
= readdir (dirp
)) != NULL
)
4390 struct stat statbuf
;
4391 char procentry
[sizeof ("/proc/4294967295")];
4393 if (!isdigit (dp
->d_name
[0])
4394 || NAMELEN (dp
) > sizeof ("4294967295") - 1)
4397 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4398 if (stat (procentry
, &statbuf
) == 0
4399 && S_ISDIR (statbuf
.st_mode
))
4403 char cmd
[MAXPATHLEN
+ 1];
4404 struct passwd
*entry
;
4406 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4407 entry
= getpwuid (statbuf
.st_uid
);
4409 if ((f
= fopen (pathname
, "r")) != NULL
)
4411 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4415 for (i
= 0; i
< len
; i
++)
4420 obstack_xml_printf (
4423 "<column name=\"pid\">%s</column>"
4424 "<column name=\"user\">%s</column>"
4425 "<column name=\"command\">%s</column>"
4428 entry
? entry
->pw_name
: "?",
4441 obstack_grow_str0 (&obstack
, "</osdata>\n");
4442 buf
= obstack_finish (&obstack
);
4443 len_avail
= strlen (buf
);
4446 if (offset
>= len_avail
)
4448 /* Done. Get rid of the obstack. */
4449 obstack_free (&obstack
, NULL
);
4455 if (len
> len_avail
- offset
)
4456 len
= len_avail
- offset
;
4457 memcpy (readbuf
, buf
+ offset
, len
);
4463 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4464 const char *annex
, gdb_byte
*readbuf
,
4465 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4469 if (object
== TARGET_OBJECT_AUXV
)
4470 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4473 if (object
== TARGET_OBJECT_OSDATA
)
4474 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4477 if (object
== TARGET_OBJECT_SPU
)
4478 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4481 /* GDB calculates all the addresses in possibly larget width of the address.
4482 Address width needs to be masked before its final use - either by
4483 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4485 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4487 if (object
== TARGET_OBJECT_MEMORY
)
4489 int addr_bit
= gdbarch_addr_bit (target_gdbarch
);
4491 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4492 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4495 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4500 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4504 /* Create a prototype generic GNU/Linux target. The client can override
4505 it with local methods. */
4508 linux_target_install_ops (struct target_ops
*t
)
4510 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4511 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4512 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4513 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4514 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4515 t
->to_post_attach
= linux_child_post_attach
;
4516 t
->to_follow_fork
= linux_child_follow_fork
;
4517 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4518 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4520 super_xfer_partial
= t
->to_xfer_partial
;
4521 t
->to_xfer_partial
= linux_xfer_partial
;
4527 struct target_ops
*t
;
4529 t
= inf_ptrace_target ();
4530 linux_target_install_ops (t
);
4536 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4538 struct target_ops
*t
;
4540 t
= inf_ptrace_trad_target (register_u_offset
);
4541 linux_target_install_ops (t
);
4546 /* target_is_async_p implementation. */
4549 linux_nat_is_async_p (void)
4551 /* NOTE: palves 2008-03-21: We're only async when the user requests
4552 it explicitly with the "set target-async" command.
4553 Someday, linux will always be async. */
4554 if (!target_async_permitted
)
4557 /* See target.h/target_async_mask. */
4558 return linux_nat_async_mask_value
;
4561 /* target_can_async_p implementation. */
4564 linux_nat_can_async_p (void)
4566 /* NOTE: palves 2008-03-21: We're only async when the user requests
4567 it explicitly with the "set target-async" command.
4568 Someday, linux will always be async. */
4569 if (!target_async_permitted
)
4572 /* See target.h/target_async_mask. */
4573 return linux_nat_async_mask_value
;
4577 linux_nat_supports_non_stop (void)
4582 /* True if we want to support multi-process. To be removed when GDB
4583 supports multi-exec. */
4585 int linux_multi_process
= 1;
4588 linux_nat_supports_multi_process (void)
4590 return linux_multi_process
;
4593 /* target_async_mask implementation. */
4596 linux_nat_async_mask (int new_mask
)
4598 int curr_mask
= linux_nat_async_mask_value
;
4600 if (curr_mask
!= new_mask
)
4604 linux_nat_async (NULL
, 0);
4605 linux_nat_async_mask_value
= new_mask
;
4609 linux_nat_async_mask_value
= new_mask
;
4611 /* If we're going out of async-mask in all-stop, then the
4612 inferior is stopped. The next resume will call
4613 target_async. In non-stop, the target event source
4614 should be always registered in the event loop. Do so
4617 linux_nat_async (inferior_event_handler
, 0);
4624 static int async_terminal_is_ours
= 1;
4626 /* target_terminal_inferior implementation. */
4629 linux_nat_terminal_inferior (void)
4631 if (!target_is_async_p ())
4633 /* Async mode is disabled. */
4634 terminal_inferior ();
4638 terminal_inferior ();
4640 /* Calls to target_terminal_*() are meant to be idempotent. */
4641 if (!async_terminal_is_ours
)
4644 delete_file_handler (input_fd
);
4645 async_terminal_is_ours
= 0;
4649 /* target_terminal_ours implementation. */
4652 linux_nat_terminal_ours (void)
4654 if (!target_is_async_p ())
4656 /* Async mode is disabled. */
4661 /* GDB should never give the terminal to the inferior if the
4662 inferior is running in the background (run&, continue&, etc.),
4663 but claiming it sure should. */
4666 if (async_terminal_is_ours
)
4669 clear_sigint_trap ();
4670 add_file_handler (input_fd
, stdin_event_handler
, 0);
4671 async_terminal_is_ours
= 1;
4674 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4676 static void *async_client_context
;
4678 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4679 so we notice when any child changes state, and notify the
4680 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4681 above to wait for the arrival of a SIGCHLD. */
4684 sigchld_handler (int signo
)
4686 int old_errno
= errno
;
4688 if (debug_linux_nat_async
)
4689 fprintf_unfiltered (gdb_stdlog
, "sigchld\n");
4691 if (signo
== SIGCHLD
4692 && linux_nat_event_pipe
[0] != -1)
4693 async_file_mark (); /* Let the event loop know that there are
4694 events to handle. */
4699 /* Callback registered with the target events file descriptor. */
4702 handle_target_event (int error
, gdb_client_data client_data
)
4704 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4707 /* Create/destroy the target events pipe. Returns previous state. */
4710 linux_async_pipe (int enable
)
4712 int previous
= (linux_nat_event_pipe
[0] != -1);
4714 if (previous
!= enable
)
4718 block_child_signals (&prev_mask
);
4722 if (pipe (linux_nat_event_pipe
) == -1)
4723 internal_error (__FILE__
, __LINE__
,
4724 "creating event pipe failed.");
4726 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4727 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4731 close (linux_nat_event_pipe
[0]);
4732 close (linux_nat_event_pipe
[1]);
4733 linux_nat_event_pipe
[0] = -1;
4734 linux_nat_event_pipe
[1] = -1;
4737 restore_child_signals_mask (&prev_mask
);
4743 /* target_async implementation. */
4746 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
4747 void *context
), void *context
)
4749 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
4750 internal_error (__FILE__
, __LINE__
,
4751 "Calling target_async when async is masked");
4753 if (callback
!= NULL
)
4755 async_client_callback
= callback
;
4756 async_client_context
= context
;
4757 if (!linux_async_pipe (1))
4759 add_file_handler (linux_nat_event_pipe
[0],
4760 handle_target_event
, NULL
);
4761 /* There may be pending events to handle. Tell the event loop
4768 async_client_callback
= callback
;
4769 async_client_context
= context
;
4770 delete_file_handler (linux_nat_event_pipe
[0]);
4771 linux_async_pipe (0);
4776 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
4780 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
4785 ptid_t ptid
= lwp
->ptid
;
4787 if (debug_linux_nat
)
4788 fprintf_unfiltered (gdb_stdlog
,
4789 "LNSL: running -> suspending %s\n",
4790 target_pid_to_str (lwp
->ptid
));
4793 stop_callback (lwp
, NULL
);
4794 stop_wait_callback (lwp
, NULL
);
4796 /* If the lwp exits while we try to stop it, there's nothing
4798 lwp
= find_lwp_pid (ptid
);
4802 /* If we didn't collect any signal other than SIGSTOP while
4803 stopping the LWP, push a SIGNAL_0 event. In either case, the
4804 event-loop will end up calling target_wait which will collect
4806 if (lwp
->status
== 0)
4807 lwp
->status
= W_STOPCODE (0);
4812 /* Already known to be stopped; do nothing. */
4814 if (debug_linux_nat
)
4816 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
4817 fprintf_unfiltered (gdb_stdlog
, "\
4818 LNSL: already stopped/stop_requested %s\n",
4819 target_pid_to_str (lwp
->ptid
));
4821 fprintf_unfiltered (gdb_stdlog
, "\
4822 LNSL: already stopped/no stop_requested yet %s\n",
4823 target_pid_to_str (lwp
->ptid
));
4830 linux_nat_stop (ptid_t ptid
)
4833 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
4835 linux_ops
->to_stop (ptid
);
4839 linux_nat_close (int quitting
)
4841 /* Unregister from the event loop. */
4842 if (target_is_async_p ())
4843 target_async (NULL
, 0);
4845 /* Reset the async_masking. */
4846 linux_nat_async_mask_value
= 1;
4848 if (linux_ops
->to_close
)
4849 linux_ops
->to_close (quitting
);
4853 linux_nat_add_target (struct target_ops
*t
)
4855 /* Save the provided single-threaded target. We save this in a separate
4856 variable because another target we've inherited from (e.g. inf-ptrace)
4857 may have saved a pointer to T; we want to use it for the final
4858 process stratum target. */
4859 linux_ops_saved
= *t
;
4860 linux_ops
= &linux_ops_saved
;
4862 /* Override some methods for multithreading. */
4863 t
->to_create_inferior
= linux_nat_create_inferior
;
4864 t
->to_attach
= linux_nat_attach
;
4865 t
->to_detach
= linux_nat_detach
;
4866 t
->to_resume
= linux_nat_resume
;
4867 t
->to_wait
= linux_nat_wait
;
4868 t
->to_xfer_partial
= linux_nat_xfer_partial
;
4869 t
->to_kill
= linux_nat_kill
;
4870 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
4871 t
->to_thread_alive
= linux_nat_thread_alive
;
4872 t
->to_pid_to_str
= linux_nat_pid_to_str
;
4873 t
->to_has_thread_control
= tc_schedlock
;
4875 t
->to_can_async_p
= linux_nat_can_async_p
;
4876 t
->to_is_async_p
= linux_nat_is_async_p
;
4877 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
4878 t
->to_async
= linux_nat_async
;
4879 t
->to_async_mask
= linux_nat_async_mask
;
4880 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
4881 t
->to_terminal_ours
= linux_nat_terminal_ours
;
4882 t
->to_close
= linux_nat_close
;
4884 /* Methods for non-stop support. */
4885 t
->to_stop
= linux_nat_stop
;
4887 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
4889 /* We don't change the stratum; this target will sit at
4890 process_stratum and thread_db will set at thread_stratum. This
4891 is a little strange, since this is a multi-threaded-capable
4892 target, but we want to be on the stack below thread_db, and we
4893 also want to be used for single-threaded processes. */
4898 /* Register a method to call whenever a new thread is attached. */
4900 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
4902 /* Save the pointer. We only support a single registered instance
4903 of the GNU/Linux native target, so we do not need to map this to
4905 linux_nat_new_thread
= new_thread
;
4908 /* Register a method that converts a siginfo object between the layout
4909 that ptrace returns, and the layout in the architecture of the
4912 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
4913 int (*siginfo_fixup
) (struct siginfo
*,
4917 /* Save the pointer. */
4918 linux_nat_siginfo_fixup
= siginfo_fixup
;
4921 /* Return the saved siginfo associated with PTID. */
4923 linux_nat_get_siginfo (ptid_t ptid
)
4925 struct lwp_info
*lp
= find_lwp_pid (ptid
);
4927 gdb_assert (lp
!= NULL
);
4929 return &lp
->siginfo
;
4932 /* Provide a prototype to silence -Wmissing-prototypes. */
4933 extern initialize_file_ftype _initialize_linux_nat
;
4936 _initialize_linux_nat (void)
4940 add_info ("proc", linux_nat_info_proc_cmd
, _("\
4941 Show /proc process information about any running process.\n\
4942 Specify any process id, or use the program being debugged by default.\n\
4943 Specify any of the following keywords for detailed info:\n\
4944 mappings -- list of mapped memory regions.\n\
4945 stat -- list a bunch of random process info.\n\
4946 status -- list a different bunch of random process info.\n\
4947 all -- list all available /proc info."));
4949 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
4950 &debug_linux_nat
, _("\
4951 Set debugging of GNU/Linux lwp module."), _("\
4952 Show debugging of GNU/Linux lwp module."), _("\
4953 Enables printf debugging output."),
4955 show_debug_linux_nat
,
4956 &setdebuglist
, &showdebuglist
);
4958 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
4959 &debug_linux_nat_async
, _("\
4960 Set debugging of GNU/Linux async lwp module."), _("\
4961 Show debugging of GNU/Linux async lwp module."), _("\
4962 Enables printf debugging output."),
4964 show_debug_linux_nat_async
,
4965 &setdebuglist
, &showdebuglist
);
4967 /* Save this mask as the default. */
4968 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
4970 /* Install a SIGCHLD handler. */
4971 sigchld_action
.sa_handler
= sigchld_handler
;
4972 sigemptyset (&sigchld_action
.sa_mask
);
4973 sigchld_action
.sa_flags
= SA_RESTART
;
4975 /* Make it the default. */
4976 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
4978 /* Make sure we don't block SIGCHLD during a sigsuspend. */
4979 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
4980 sigdelset (&suspend_mask
, SIGCHLD
);
4982 sigemptyset (&blocked_mask
);
4984 add_setshow_boolean_cmd ("disable-randomization", class_support
,
4985 &disable_randomization
, _("\
4986 Set disabling of debuggee's virtual address space randomization."), _("\
4987 Show disabling of debuggee's virtual address space randomization."), _("\
4988 When this mode is on (which is the default), randomization of the virtual\n\
4989 address space is disabled. Standalone programs run with the randomization\n\
4990 enabled by default on some platforms."),
4991 &set_disable_randomization
,
4992 &show_disable_randomization
,
4993 &setlist
, &showlist
);
4997 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4998 the GNU/Linux Threads library and therefore doesn't really belong
5001 /* Read variable NAME in the target and return its value if found.
5002 Otherwise return zero. It is assumed that the type of the variable
5006 get_signo (const char *name
)
5008 struct minimal_symbol
*ms
;
5011 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5015 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5016 sizeof (signo
)) != 0)
5022 /* Return the set of signals used by the threads library in *SET. */
5025 lin_thread_get_thread_signals (sigset_t
*set
)
5027 struct sigaction action
;
5028 int restart
, cancel
;
5030 sigemptyset (&blocked_mask
);
5033 restart
= get_signo ("__pthread_sig_restart");
5034 cancel
= get_signo ("__pthread_sig_cancel");
5036 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5037 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5038 not provide any way for the debugger to query the signal numbers -
5039 fortunately they don't change! */
5042 restart
= __SIGRTMIN
;
5045 cancel
= __SIGRTMIN
+ 1;
5047 sigaddset (set
, restart
);
5048 sigaddset (set
, cancel
);
5050 /* The GNU/Linux Threads library makes terminating threads send a
5051 special "cancel" signal instead of SIGCHLD. Make sure we catch
5052 those (to prevent them from terminating GDB itself, which is
5053 likely to be their default action) and treat them the same way as
5056 action
.sa_handler
= sigchld_handler
;
5057 sigemptyset (&action
.sa_mask
);
5058 action
.sa_flags
= SA_RESTART
;
5059 sigaction (cancel
, &action
, NULL
);
5061 /* We block the "cancel" signal throughout this code ... */
5062 sigaddset (&blocked_mask
, cancel
);
5063 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5065 /* ... except during a sigsuspend. */
5066 sigdelset (&suspend_mask
, cancel
);