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 /* Unlike other extended result codes, WSTOPSIG (status) on
188 PTRACE_O_TRACESYSGOOD syscall events doesn't return SIGTRAP, but
189 instead SIGTRAP with bit 7 set. */
190 #define SYSCALL_SIGTRAP (SIGTRAP | 0x80)
192 /* We can't always assume that this flag is available, but all systems
193 with the ptrace event handlers also have __WALL, so it's safe to use
196 #define __WALL 0x40000000 /* Wait for any child. */
199 #ifndef PTRACE_GETSIGINFO
200 # define PTRACE_GETSIGINFO 0x4202
201 # define PTRACE_SETSIGINFO 0x4203
204 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
205 the use of the multi-threaded target. */
206 static struct target_ops
*linux_ops
;
207 static struct target_ops linux_ops_saved
;
209 /* The method to call, if any, when a new thread is attached. */
210 static void (*linux_nat_new_thread
) (ptid_t
);
212 /* The method to call, if any, when the siginfo object needs to be
213 converted between the layout returned by ptrace, and the layout in
214 the architecture of the inferior. */
215 static int (*linux_nat_siginfo_fixup
) (struct siginfo
*,
219 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
220 Called by our to_xfer_partial. */
221 static LONGEST (*super_xfer_partial
) (struct target_ops
*,
223 const char *, gdb_byte
*,
227 static int debug_linux_nat
;
229 show_debug_linux_nat (struct ui_file
*file
, int from_tty
,
230 struct cmd_list_element
*c
, const char *value
)
232 fprintf_filtered (file
, _("Debugging of GNU/Linux lwp module is %s.\n"),
236 static int debug_linux_nat_async
= 0;
238 show_debug_linux_nat_async (struct ui_file
*file
, int from_tty
,
239 struct cmd_list_element
*c
, const char *value
)
241 fprintf_filtered (file
, _("Debugging of GNU/Linux async lwp module is %s.\n"),
245 static int disable_randomization
= 1;
248 show_disable_randomization (struct ui_file
*file
, int from_tty
,
249 struct cmd_list_element
*c
, const char *value
)
251 #ifdef HAVE_PERSONALITY
252 fprintf_filtered (file
, _("\
253 Disabling randomization of debuggee's virtual address space is %s.\n"),
255 #else /* !HAVE_PERSONALITY */
257 Disabling randomization of debuggee's virtual address space is unsupported on\n\
258 this platform.\n"), file
);
259 #endif /* !HAVE_PERSONALITY */
263 set_disable_randomization (char *args
, int from_tty
, struct cmd_list_element
*c
)
265 #ifndef HAVE_PERSONALITY
267 Disabling randomization of debuggee's virtual address space is unsupported on\n\
269 #endif /* !HAVE_PERSONALITY */
272 static int linux_parent_pid
;
274 struct simple_pid_list
278 struct simple_pid_list
*next
;
280 struct simple_pid_list
*stopped_pids
;
282 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
283 can not be used, 1 if it can. */
285 static int linux_supports_tracefork_flag
= -1;
287 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACESYSGOOD
288 can not be used, 1 if it can. */
290 static int linux_supports_tracesysgood_flag
= -1;
292 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
293 PTRACE_O_TRACEVFORKDONE. */
295 static int linux_supports_tracevforkdone_flag
= -1;
297 /* Async mode support */
299 /* Zero if the async mode, although enabled, is masked, which means
300 linux_nat_wait should behave as if async mode was off. */
301 static int linux_nat_async_mask_value
= 1;
303 /* Stores the current used ptrace() options. */
304 static int current_ptrace_options
= 0;
306 /* The read/write ends of the pipe registered as waitable file in the
308 static int linux_nat_event_pipe
[2] = { -1, -1 };
310 /* Flush the event pipe. */
313 async_file_flush (void)
320 ret
= read (linux_nat_event_pipe
[0], &buf
, 1);
322 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
325 /* Put something (anything, doesn't matter what, or how much) in event
326 pipe, so that the select/poll in the event-loop realizes we have
327 something to process. */
330 async_file_mark (void)
334 /* It doesn't really matter what the pipe contains, as long we end
335 up with something in it. Might as well flush the previous
341 ret
= write (linux_nat_event_pipe
[1], "+", 1);
343 while (ret
== -1 && errno
== EINTR
);
345 /* Ignore EAGAIN. If the pipe is full, the event loop will already
346 be awakened anyway. */
349 static void linux_nat_async (void (*callback
)
350 (enum inferior_event_type event_type
, void *context
),
352 static int linux_nat_async_mask (int mask
);
353 static int kill_lwp (int lwpid
, int signo
);
355 static int stop_callback (struct lwp_info
*lp
, void *data
);
357 static void block_child_signals (sigset_t
*prev_mask
);
358 static void restore_child_signals_mask (sigset_t
*prev_mask
);
361 static struct lwp_info
*add_lwp (ptid_t ptid
);
362 static void purge_lwp_list (int pid
);
363 static struct lwp_info
*find_lwp_pid (ptid_t ptid
);
366 /* Trivial list manipulation functions to keep track of a list of
367 new stopped processes. */
369 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
371 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
373 new_pid
->status
= status
;
374 new_pid
->next
= *listp
;
379 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *status
)
381 struct simple_pid_list
**p
;
383 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
384 if ((*p
)->pid
== pid
)
386 struct simple_pid_list
*next
= (*p
)->next
;
387 *status
= (*p
)->status
;
396 linux_record_stopped_pid (int pid
, int status
)
398 add_to_pid_list (&stopped_pids
, pid
, status
);
402 /* A helper function for linux_test_for_tracefork, called after fork (). */
405 linux_tracefork_child (void)
409 ptrace (PTRACE_TRACEME
, 0, 0, 0);
410 kill (getpid (), SIGSTOP
);
415 /* Wrapper function for waitpid which handles EINTR. */
418 my_waitpid (int pid
, int *status
, int flags
)
424 ret
= waitpid (pid
, status
, flags
);
426 while (ret
== -1 && errno
== EINTR
);
431 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
433 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
434 we know that the feature is not available. This may change the tracing
435 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
437 However, if it succeeds, we don't know for sure that the feature is
438 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
439 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
440 fork tracing, and let it fork. If the process exits, we assume that we
441 can't use TRACEFORK; if we get the fork notification, and we can extract
442 the new child's PID, then we assume that we can. */
445 linux_test_for_tracefork (int original_pid
)
447 int child_pid
, ret
, status
;
451 /* We don't want those ptrace calls to be interrupted. */
452 block_child_signals (&prev_mask
);
454 linux_supports_tracefork_flag
= 0;
455 linux_supports_tracevforkdone_flag
= 0;
457 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACEFORK
);
460 restore_child_signals_mask (&prev_mask
);
466 perror_with_name (("fork"));
469 linux_tracefork_child ();
471 ret
= my_waitpid (child_pid
, &status
, 0);
473 perror_with_name (("waitpid"));
474 else if (ret
!= child_pid
)
475 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret
);
476 if (! WIFSTOPPED (status
))
477 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status
);
479 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
482 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
485 warning (_("linux_test_for_tracefork: failed to kill child"));
486 restore_child_signals_mask (&prev_mask
);
490 ret
= my_waitpid (child_pid
, &status
, 0);
491 if (ret
!= child_pid
)
492 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
493 else if (!WIFSIGNALED (status
))
494 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
495 "killed child"), status
);
497 restore_child_signals_mask (&prev_mask
);
501 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
502 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
503 PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORKDONE
);
504 linux_supports_tracevforkdone_flag
= (ret
== 0);
506 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
508 warning (_("linux_test_for_tracefork: failed to resume child"));
510 ret
= my_waitpid (child_pid
, &status
, 0);
512 if (ret
== child_pid
&& WIFSTOPPED (status
)
513 && status
>> 16 == PTRACE_EVENT_FORK
)
516 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
517 if (ret
== 0 && second_pid
!= 0)
521 linux_supports_tracefork_flag
= 1;
522 my_waitpid (second_pid
, &second_status
, 0);
523 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
525 warning (_("linux_test_for_tracefork: failed to kill second child"));
526 my_waitpid (second_pid
, &status
, 0);
530 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
531 "(%d, status 0x%x)"), ret
, status
);
533 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
535 warning (_("linux_test_for_tracefork: failed to kill child"));
536 my_waitpid (child_pid
, &status
, 0);
538 restore_child_signals_mask (&prev_mask
);
541 /* Determine if PTRACE_O_TRACESYSGOOD can be used to follow syscalls.
543 We try to enable syscall tracing on ORIGINAL_PID. If this fails,
544 we know that the feature is not available. This may change the tracing
545 options for ORIGINAL_PID, but we'll be setting them shortly anyway. */
548 linux_test_for_tracesysgood (int original_pid
)
553 /* We don't want those ptrace calls to be interrupted. */
554 block_child_signals (&prev_mask
);
556 linux_supports_tracesysgood_flag
= 0;
558 ret
= ptrace (PTRACE_SETOPTIONS
, original_pid
, 0, PTRACE_O_TRACESYSGOOD
);
562 linux_supports_tracesysgood_flag
= 1;
564 restore_child_signals_mask (&prev_mask
);
567 /* Determine wether we support PTRACE_O_TRACESYSGOOD option available.
568 This function also sets linux_supports_tracesysgood_flag. */
571 linux_supports_tracesysgood (int pid
)
573 if (linux_supports_tracesysgood_flag
== -1)
574 linux_test_for_tracesysgood (pid
);
575 return linux_supports_tracesysgood_flag
;
578 /* Return non-zero iff we have tracefork functionality available.
579 This function also sets linux_supports_tracefork_flag. */
582 linux_supports_tracefork (int pid
)
584 if (linux_supports_tracefork_flag
== -1)
585 linux_test_for_tracefork (pid
);
586 return linux_supports_tracefork_flag
;
590 linux_supports_tracevforkdone (int pid
)
592 if (linux_supports_tracefork_flag
== -1)
593 linux_test_for_tracefork (pid
);
594 return linux_supports_tracevforkdone_flag
;
598 linux_enable_tracesysgood (ptid_t ptid
)
600 int pid
= ptid_get_lwp (ptid
);
603 pid
= ptid_get_pid (ptid
);
605 if (linux_supports_tracesysgood (pid
) == 0)
608 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
610 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
615 linux_enable_event_reporting (ptid_t ptid
)
617 int pid
= ptid_get_lwp (ptid
);
620 pid
= ptid_get_pid (ptid
);
622 if (! linux_supports_tracefork (pid
))
625 current_ptrace_options
|= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
626 | PTRACE_O_TRACEEXEC
| PTRACE_O_TRACECLONE
;
628 if (linux_supports_tracevforkdone (pid
))
629 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
631 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
632 read-only process state. */
634 ptrace (PTRACE_SETOPTIONS
, pid
, 0, current_ptrace_options
);
638 linux_child_post_attach (int pid
)
640 linux_enable_event_reporting (pid_to_ptid (pid
));
641 check_for_thread_db ();
642 linux_enable_tracesysgood (pid_to_ptid (pid
));
646 linux_child_post_startup_inferior (ptid_t ptid
)
648 linux_enable_event_reporting (ptid
);
649 check_for_thread_db ();
650 linux_enable_tracesysgood (ptid
);
654 linux_child_follow_fork (struct target_ops
*ops
, int follow_child
)
658 int parent_pid
, child_pid
;
660 block_child_signals (&prev_mask
);
662 has_vforked
= (inferior_thread ()->pending_follow
.kind
663 == TARGET_WAITKIND_VFORKED
);
664 parent_pid
= ptid_get_lwp (inferior_ptid
);
666 parent_pid
= ptid_get_pid (inferior_ptid
);
667 child_pid
= PIDGET (inferior_thread ()->pending_follow
.value
.related_pid
);
670 linux_enable_event_reporting (pid_to_ptid (child_pid
));
674 /* We're already attached to the parent, by default. */
676 /* Before detaching from the child, remove all breakpoints from
677 it. If we forked, then this has already been taken care of
678 by infrun.c. If we vforked however, any breakpoint inserted
679 in the parent is visible in the child, even those added while
680 stopped in a vfork catchpoint. This won't actually modify
681 the breakpoint list, but will physically remove the
682 breakpoints from the child. This will remove the breakpoints
683 from the parent also, but they'll be reinserted below. */
685 detach_breakpoints (child_pid
);
687 /* Detach new forked process? */
690 if (info_verbose
|| debug_linux_nat
)
692 target_terminal_ours ();
693 fprintf_filtered (gdb_stdlog
,
694 "Detaching after fork from child process %d.\n",
698 ptrace (PTRACE_DETACH
, child_pid
, 0, 0);
702 struct inferior
*parent_inf
, *child_inf
;
704 struct cleanup
*old_chain
;
706 /* Add process to GDB's tables. */
707 child_inf
= add_inferior (child_pid
);
709 parent_inf
= current_inferior ();
710 child_inf
->attach_flag
= parent_inf
->attach_flag
;
711 copy_terminal_info (child_inf
, parent_inf
);
713 old_chain
= save_inferior_ptid ();
715 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
716 add_thread (inferior_ptid
);
717 lp
= add_lwp (inferior_ptid
);
720 check_for_thread_db ();
722 do_cleanups (old_chain
);
727 gdb_assert (linux_supports_tracefork_flag
>= 0);
728 if (linux_supports_tracevforkdone (0))
732 ptrace (PTRACE_CONT
, parent_pid
, 0, 0);
733 my_waitpid (parent_pid
, &status
, __WALL
);
734 if ((status
>> 16) != PTRACE_EVENT_VFORK_DONE
)
735 warning (_("Unexpected waitpid result %06x when waiting for "
736 "vfork-done"), status
);
740 /* We can't insert breakpoints until the child has
741 finished with the shared memory region. We need to
742 wait until that happens. Ideal would be to just
744 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
745 - waitpid (parent_pid, &status, __WALL);
746 However, most architectures can't handle a syscall
747 being traced on the way out if it wasn't traced on
750 We might also think to loop, continuing the child
751 until it exits or gets a SIGTRAP. One problem is
752 that the child might call ptrace with PTRACE_TRACEME.
754 There's no simple and reliable way to figure out when
755 the vforked child will be done with its copy of the
756 shared memory. We could step it out of the syscall,
757 two instructions, let it go, and then single-step the
758 parent once. When we have hardware single-step, this
759 would work; with software single-step it could still
760 be made to work but we'd have to be able to insert
761 single-step breakpoints in the child, and we'd have
762 to insert -just- the single-step breakpoint in the
763 parent. Very awkward.
765 In the end, the best we can do is to make sure it
766 runs for a little while. Hopefully it will be out of
767 range of any breakpoints we reinsert. Usually this
768 is only the single-step breakpoint at vfork's return
774 /* Since we vforked, breakpoints were removed in the parent
775 too. Put them back. */
776 reattach_breakpoints (parent_pid
);
781 struct thread_info
*tp
;
782 struct inferior
*parent_inf
, *child_inf
;
785 /* Before detaching from the parent, remove all breakpoints from it. */
786 remove_breakpoints ();
788 if (info_verbose
|| debug_linux_nat
)
790 target_terminal_ours ();
791 fprintf_filtered (gdb_stdlog
,
792 "Attaching after fork to child process %d.\n",
796 /* Add the new inferior first, so that the target_detach below
797 doesn't unpush the target. */
799 child_inf
= add_inferior (child_pid
);
801 parent_inf
= current_inferior ();
802 child_inf
->attach_flag
= parent_inf
->attach_flag
;
803 copy_terminal_info (child_inf
, parent_inf
);
805 /* If we're vforking, we may want to hold on to the parent until
806 the child exits or execs. At exec time we can remove the old
807 breakpoints from the parent and detach it; at exit time we
808 could do the same (or even, sneakily, resume debugging it - the
809 child's exec has failed, or something similar).
811 This doesn't clean up "properly", because we can't call
812 target_detach, but that's OK; if the current target is "child",
813 then it doesn't need any further cleanups, and lin_lwp will
814 generally not encounter vfork (vfork is defined to fork
817 The holding part is very easy if we have VFORKDONE events;
818 but keeping track of both processes is beyond GDB at the
819 moment. So we don't expose the parent to the rest of GDB.
820 Instead we quietly hold onto it until such time as we can
825 struct lwp_info
*parent_lwp
;
827 linux_parent_pid
= parent_pid
;
829 /* Get rid of the inferior on the core side as well. */
830 inferior_ptid
= null_ptid
;
831 detach_inferior (parent_pid
);
833 /* Also get rid of all its lwps. We will detach from this
834 inferior soon-ish, but, we will still get an exit event
835 reported through waitpid when it exits. If we didn't get
836 rid of the lwps from our list, we would end up reporting
837 the inferior exit to the core, which would then try to
838 mourn a non-existing (from the core's perspective)
840 parent_lwp
= find_lwp_pid (pid_to_ptid (parent_pid
));
841 purge_lwp_list (GET_PID (parent_lwp
->ptid
));
842 linux_parent_pid
= parent_pid
;
844 else if (detach_fork
)
845 target_detach (NULL
, 0);
847 inferior_ptid
= ptid_build (child_pid
, child_pid
, 0);
848 add_thread (inferior_ptid
);
849 lp
= add_lwp (inferior_ptid
);
852 check_for_thread_db ();
855 restore_child_signals_mask (&prev_mask
);
861 linux_child_insert_fork_catchpoint (int pid
)
863 if (! linux_supports_tracefork (pid
))
864 error (_("Your system does not support fork catchpoints."));
868 linux_child_insert_vfork_catchpoint (int pid
)
870 if (!linux_supports_tracefork (pid
))
871 error (_("Your system does not support vfork catchpoints."));
875 linux_child_insert_exec_catchpoint (int pid
)
877 if (!linux_supports_tracefork (pid
))
878 error (_("Your system does not support exec catchpoints."));
882 linux_child_set_syscall_catchpoint (int pid
, int needed
, int any_count
,
883 int table_size
, int *table
)
885 if (! linux_supports_tracesysgood (pid
))
886 error (_("Your system does not support syscall catchpoints."));
887 /* On GNU/Linux, we ignore the arguments. It means that we only
888 enable the syscall catchpoints, but do not disable them.
890 Also, we do not use the `table' information because we do not
891 filter system calls here. We let GDB do the logic for us. */
895 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
896 are processes sharing the same VM space. A multi-threaded process
897 is basically a group of such processes. However, such a grouping
898 is almost entirely a user-space issue; the kernel doesn't enforce
899 such a grouping at all (this might change in the future). In
900 general, we'll rely on the threads library (i.e. the GNU/Linux
901 Threads library) to provide such a grouping.
903 It is perfectly well possible to write a multi-threaded application
904 without the assistance of a threads library, by using the clone
905 system call directly. This module should be able to give some
906 rudimentary support for debugging such applications if developers
907 specify the CLONE_PTRACE flag in the clone system call, and are
908 using the Linux kernel 2.4 or above.
910 Note that there are some peculiarities in GNU/Linux that affect
913 - In general one should specify the __WCLONE flag to waitpid in
914 order to make it report events for any of the cloned processes
915 (and leave it out for the initial process). However, if a cloned
916 process has exited the exit status is only reported if the
917 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
918 we cannot use it since GDB must work on older systems too.
920 - When a traced, cloned process exits and is waited for by the
921 debugger, the kernel reassigns it to the original parent and
922 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
923 library doesn't notice this, which leads to the "zombie problem":
924 When debugged a multi-threaded process that spawns a lot of
925 threads will run out of processes, even if the threads exit,
926 because the "zombies" stay around. */
928 /* List of known LWPs. */
929 struct lwp_info
*lwp_list
;
932 /* Original signal mask. */
933 static sigset_t normal_mask
;
935 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
936 _initialize_linux_nat. */
937 static sigset_t suspend_mask
;
939 /* Signals to block to make that sigsuspend work. */
940 static sigset_t blocked_mask
;
942 /* SIGCHLD action. */
943 struct sigaction sigchld_action
;
945 /* Block child signals (SIGCHLD and linux threads signals), and store
946 the previous mask in PREV_MASK. */
949 block_child_signals (sigset_t
*prev_mask
)
951 /* Make sure SIGCHLD is blocked. */
952 if (!sigismember (&blocked_mask
, SIGCHLD
))
953 sigaddset (&blocked_mask
, SIGCHLD
);
955 sigprocmask (SIG_BLOCK
, &blocked_mask
, prev_mask
);
958 /* Restore child signals mask, previously returned by
959 block_child_signals. */
962 restore_child_signals_mask (sigset_t
*prev_mask
)
964 sigprocmask (SIG_SETMASK
, prev_mask
, NULL
);
968 /* Prototypes for local functions. */
969 static int stop_wait_callback (struct lwp_info
*lp
, void *data
);
970 static int linux_thread_alive (ptid_t ptid
);
971 static char *linux_child_pid_to_exec_file (int pid
);
972 static int cancel_breakpoint (struct lwp_info
*lp
);
975 /* Convert wait status STATUS to a string. Used for printing debug
979 status_to_str (int status
)
983 if (WIFSTOPPED (status
))
985 if (WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
986 snprintf (buf
, sizeof (buf
), "%s (stopped at syscall)",
987 strsignal (SIGTRAP
));
989 snprintf (buf
, sizeof (buf
), "%s (stopped)",
990 strsignal (WSTOPSIG (status
)));
992 else if (WIFSIGNALED (status
))
993 snprintf (buf
, sizeof (buf
), "%s (terminated)",
994 strsignal (WSTOPSIG (status
)));
996 snprintf (buf
, sizeof (buf
), "%d (exited)", WEXITSTATUS (status
));
1001 /* Initialize the list of LWPs. Note that this module, contrary to
1002 what GDB's generic threads layer does for its thread list,
1003 re-initializes the LWP lists whenever we mourn or detach (which
1004 doesn't involve mourning) the inferior. */
1007 init_lwp_list (void)
1009 struct lwp_info
*lp
, *lpnext
;
1011 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1020 /* Remove all LWPs belong to PID from the lwp list. */
1023 purge_lwp_list (int pid
)
1025 struct lwp_info
*lp
, *lpprev
, *lpnext
;
1029 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1033 if (ptid_get_pid (lp
->ptid
) == pid
)
1036 lwp_list
= lp
->next
;
1038 lpprev
->next
= lp
->next
;
1047 /* Return the number of known LWPs in the tgid given by PID. */
1053 struct lwp_info
*lp
;
1055 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1056 if (ptid_get_pid (lp
->ptid
) == pid
)
1062 /* Add the LWP specified by PID to the list. Return a pointer to the
1063 structure describing the new LWP. The LWP should already be stopped
1064 (with an exception for the very first LWP). */
1066 static struct lwp_info
*
1067 add_lwp (ptid_t ptid
)
1069 struct lwp_info
*lp
;
1071 gdb_assert (is_lwp (ptid
));
1073 lp
= (struct lwp_info
*) xmalloc (sizeof (struct lwp_info
));
1075 memset (lp
, 0, sizeof (struct lwp_info
));
1077 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
1081 lp
->next
= lwp_list
;
1084 if (num_lwps (GET_PID (ptid
)) > 1 && linux_nat_new_thread
!= NULL
)
1085 linux_nat_new_thread (ptid
);
1090 /* Remove the LWP specified by PID from the list. */
1093 delete_lwp (ptid_t ptid
)
1095 struct lwp_info
*lp
, *lpprev
;
1099 for (lp
= lwp_list
; lp
; lpprev
= lp
, lp
= lp
->next
)
1100 if (ptid_equal (lp
->ptid
, ptid
))
1107 lpprev
->next
= lp
->next
;
1109 lwp_list
= lp
->next
;
1114 /* Return a pointer to the structure describing the LWP corresponding
1115 to PID. If no corresponding LWP could be found, return NULL. */
1117 static struct lwp_info
*
1118 find_lwp_pid (ptid_t ptid
)
1120 struct lwp_info
*lp
;
1124 lwp
= GET_LWP (ptid
);
1126 lwp
= GET_PID (ptid
);
1128 for (lp
= lwp_list
; lp
; lp
= lp
->next
)
1129 if (lwp
== GET_LWP (lp
->ptid
))
1135 /* Returns true if PTID matches filter FILTER. FILTER can be the wild
1136 card MINUS_ONE_PTID (all ptid match it); can be a ptid representing
1137 a process (ptid_is_pid returns true), in which case, all lwps of
1138 that give process match, lwps of other process do not; or, it can
1139 represent a specific thread, in which case, only that thread will
1140 match true. PTID must represent an LWP, it can never be a wild
1144 ptid_match (ptid_t ptid
, ptid_t filter
)
1146 /* Since both parameters have the same type, prevent easy mistakes
1148 gdb_assert (!ptid_equal (ptid
, minus_one_ptid
)
1149 && !ptid_equal (ptid
, null_ptid
));
1151 if (ptid_equal (filter
, minus_one_ptid
))
1153 if (ptid_is_pid (filter
)
1154 && ptid_get_pid (ptid
) == ptid_get_pid (filter
))
1156 else if (ptid_equal (ptid
, filter
))
1162 /* Call CALLBACK with its second argument set to DATA for every LWP in
1163 the list. If CALLBACK returns 1 for a particular LWP, return a
1164 pointer to the structure describing that LWP immediately.
1165 Otherwise return NULL. */
1168 iterate_over_lwps (ptid_t filter
,
1169 int (*callback
) (struct lwp_info
*, void *),
1172 struct lwp_info
*lp
, *lpnext
;
1174 for (lp
= lwp_list
; lp
; lp
= lpnext
)
1178 if (ptid_match (lp
->ptid
, filter
))
1180 if ((*callback
) (lp
, data
))
1188 /* Update our internal state when changing from one checkpoint to
1189 another indicated by NEW_PTID. We can only switch single-threaded
1190 applications, so we only create one new LWP, and the previous list
1194 linux_nat_switch_fork (ptid_t new_ptid
)
1196 struct lwp_info
*lp
;
1198 purge_lwp_list (GET_PID (inferior_ptid
));
1200 lp
= add_lwp (new_ptid
);
1203 /* This changes the thread's ptid while preserving the gdb thread
1204 num. Also changes the inferior pid, while preserving the
1206 thread_change_ptid (inferior_ptid
, new_ptid
);
1208 /* We've just told GDB core that the thread changed target id, but,
1209 in fact, it really is a different thread, with different register
1211 registers_changed ();
1214 /* Handle the exit of a single thread LP. */
1217 exit_lwp (struct lwp_info
*lp
)
1219 struct thread_info
*th
= find_thread_ptid (lp
->ptid
);
1223 if (print_thread_events
)
1224 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp
->ptid
));
1226 delete_thread (lp
->ptid
);
1229 delete_lwp (lp
->ptid
);
1232 /* Return an lwp's tgid, found in `/proc/PID/status'. */
1235 linux_proc_get_tgid (int lwpid
)
1241 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
1242 status_file
= fopen (buf
, "r");
1243 if (status_file
!= NULL
)
1245 while (fgets (buf
, sizeof (buf
), status_file
))
1247 if (strncmp (buf
, "Tgid:", 5) == 0)
1249 tgid
= strtoul (buf
+ strlen ("Tgid:"), NULL
, 10);
1254 fclose (status_file
);
1260 /* Detect `T (stopped)' in `/proc/PID/status'.
1261 Other states including `T (tracing stop)' are reported as false. */
1264 pid_is_stopped (pid_t pid
)
1270 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) pid
);
1271 status_file
= fopen (buf
, "r");
1272 if (status_file
!= NULL
)
1276 while (fgets (buf
, sizeof (buf
), status_file
))
1278 if (strncmp (buf
, "State:", 6) == 0)
1284 if (have_state
&& strstr (buf
, "T (stopped)") != NULL
)
1286 fclose (status_file
);
1291 /* Wait for the LWP specified by LP, which we have just attached to.
1292 Returns a wait status for that LWP, to cache. */
1295 linux_nat_post_attach_wait (ptid_t ptid
, int first
, int *cloned
,
1298 pid_t new_pid
, pid
= GET_LWP (ptid
);
1301 if (pid_is_stopped (pid
))
1303 if (debug_linux_nat
)
1304 fprintf_unfiltered (gdb_stdlog
,
1305 "LNPAW: Attaching to a stopped process\n");
1307 /* The process is definitely stopped. It is in a job control
1308 stop, unless the kernel predates the TASK_STOPPED /
1309 TASK_TRACED distinction, in which case it might be in a
1310 ptrace stop. Make sure it is in a ptrace stop; from there we
1311 can kill it, signal it, et cetera.
1313 First make sure there is a pending SIGSTOP. Since we are
1314 already attached, the process can not transition from stopped
1315 to running without a PTRACE_CONT; so we know this signal will
1316 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1317 probably already in the queue (unless this kernel is old
1318 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1319 is not an RT signal, it can only be queued once. */
1320 kill_lwp (pid
, SIGSTOP
);
1322 /* Finally, resume the stopped process. This will deliver the SIGSTOP
1323 (or a higher priority signal, just like normal PTRACE_ATTACH). */
1324 ptrace (PTRACE_CONT
, pid
, 0, 0);
1327 /* Make sure the initial process is stopped. The user-level threads
1328 layer might want to poke around in the inferior, and that won't
1329 work if things haven't stabilized yet. */
1330 new_pid
= my_waitpid (pid
, &status
, 0);
1331 if (new_pid
== -1 && errno
== ECHILD
)
1334 warning (_("%s is a cloned process"), target_pid_to_str (ptid
));
1336 /* Try again with __WCLONE to check cloned processes. */
1337 new_pid
= my_waitpid (pid
, &status
, __WCLONE
);
1341 gdb_assert (pid
== new_pid
&& WIFSTOPPED (status
));
1343 if (WSTOPSIG (status
) != SIGSTOP
)
1346 if (debug_linux_nat
)
1347 fprintf_unfiltered (gdb_stdlog
,
1348 "LNPAW: Received %s after attaching\n",
1349 status_to_str (status
));
1355 /* Attach to the LWP specified by PID. Return 0 if successful or -1
1356 if the new LWP could not be attached. */
1359 lin_lwp_attach_lwp (ptid_t ptid
)
1361 struct lwp_info
*lp
;
1364 gdb_assert (is_lwp (ptid
));
1366 block_child_signals (&prev_mask
);
1368 lp
= find_lwp_pid (ptid
);
1370 /* We assume that we're already attached to any LWP that has an id
1371 equal to the overall process id, and to any LWP that is already
1372 in our list of LWPs. If we're not seeing exit events from threads
1373 and we've had PID wraparound since we last tried to stop all threads,
1374 this assumption might be wrong; fortunately, this is very unlikely
1376 if (GET_LWP (ptid
) != GET_PID (ptid
) && lp
== NULL
)
1378 int status
, cloned
= 0, signalled
= 0;
1380 if (ptrace (PTRACE_ATTACH
, GET_LWP (ptid
), 0, 0) < 0)
1382 /* If we fail to attach to the thread, issue a warning,
1383 but continue. One way this can happen is if thread
1384 creation is interrupted; as of Linux kernel 2.6.19, a
1385 bug may place threads in the thread list and then fail
1387 warning (_("Can't attach %s: %s"), target_pid_to_str (ptid
),
1388 safe_strerror (errno
));
1389 restore_child_signals_mask (&prev_mask
);
1393 if (debug_linux_nat
)
1394 fprintf_unfiltered (gdb_stdlog
,
1395 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1396 target_pid_to_str (ptid
));
1398 status
= linux_nat_post_attach_wait (ptid
, 0, &cloned
, &signalled
);
1399 lp
= add_lwp (ptid
);
1401 lp
->cloned
= cloned
;
1402 lp
->signalled
= signalled
;
1403 if (WSTOPSIG (status
) != SIGSTOP
)
1406 lp
->status
= status
;
1409 target_post_attach (GET_LWP (lp
->ptid
));
1411 if (debug_linux_nat
)
1413 fprintf_unfiltered (gdb_stdlog
,
1414 "LLAL: waitpid %s received %s\n",
1415 target_pid_to_str (ptid
),
1416 status_to_str (status
));
1421 /* We assume that the LWP representing the original process is
1422 already stopped. Mark it as stopped in the data structure
1423 that the GNU/linux ptrace layer uses to keep track of
1424 threads. Note that this won't have already been done since
1425 the main thread will have, we assume, been stopped by an
1426 attach from a different layer. */
1428 lp
= add_lwp (ptid
);
1432 restore_child_signals_mask (&prev_mask
);
1437 linux_nat_create_inferior (struct target_ops
*ops
,
1438 char *exec_file
, char *allargs
, char **env
,
1441 #ifdef HAVE_PERSONALITY
1442 int personality_orig
= 0, personality_set
= 0;
1443 #endif /* HAVE_PERSONALITY */
1445 /* The fork_child mechanism is synchronous and calls target_wait, so
1446 we have to mask the async mode. */
1448 #ifdef HAVE_PERSONALITY
1449 if (disable_randomization
)
1452 personality_orig
= personality (0xffffffff);
1453 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
1455 personality_set
= 1;
1456 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
1458 if (errno
!= 0 || (personality_set
1459 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
1460 warning (_("Error disabling address space randomization: %s"),
1461 safe_strerror (errno
));
1463 #endif /* HAVE_PERSONALITY */
1465 linux_ops
->to_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
1467 #ifdef HAVE_PERSONALITY
1468 if (personality_set
)
1471 personality (personality_orig
);
1473 warning (_("Error restoring address space randomization: %s"),
1474 safe_strerror (errno
));
1476 #endif /* HAVE_PERSONALITY */
1480 linux_nat_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1482 struct lwp_info
*lp
;
1486 linux_ops
->to_attach (ops
, args
, from_tty
);
1488 /* The ptrace base target adds the main thread with (pid,0,0)
1489 format. Decorate it with lwp info. */
1490 ptid
= BUILD_LWP (GET_PID (inferior_ptid
), GET_PID (inferior_ptid
));
1491 thread_change_ptid (inferior_ptid
, ptid
);
1493 /* Add the initial process as the first LWP to the list. */
1494 lp
= add_lwp (ptid
);
1496 status
= linux_nat_post_attach_wait (lp
->ptid
, 1, &lp
->cloned
,
1500 /* Save the wait status to report later. */
1502 if (debug_linux_nat
)
1503 fprintf_unfiltered (gdb_stdlog
,
1504 "LNA: waitpid %ld, saving status %s\n",
1505 (long) GET_PID (lp
->ptid
), status_to_str (status
));
1507 lp
->status
= status
;
1509 if (target_can_async_p ())
1510 target_async (inferior_event_handler
, 0);
1513 /* Get pending status of LP. */
1515 get_pending_status (struct lwp_info
*lp
, int *status
)
1517 enum target_signal signo
= TARGET_SIGNAL_0
;
1519 /* If we paused threads momentarily, we may have stored pending
1520 events in lp->status or lp->waitstatus (see stop_wait_callback),
1521 and GDB core hasn't seen any signal for those threads.
1522 Otherwise, the last signal reported to the core is found in the
1523 thread object's stop_signal.
1525 There's a corner case that isn't handled here at present. Only
1526 if the thread stopped with a TARGET_WAITKIND_STOPPED does
1527 stop_signal make sense as a real signal to pass to the inferior.
1528 Some catchpoint related events, like
1529 TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1530 to TARGET_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1531 those traps are debug API (ptrace in our case) related and
1532 induced; the inferior wouldn't see them if it wasn't being
1533 traced. Hence, we should never pass them to the inferior, even
1534 when set to pass state. Since this corner case isn't handled by
1535 infrun.c when proceeding with a signal, for consistency, neither
1536 do we handle it here (or elsewhere in the file we check for
1537 signal pass state). Normally SIGTRAP isn't set to pass state, so
1538 this is really a corner case. */
1540 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
1541 signo
= TARGET_SIGNAL_0
; /* a pending ptrace event, not a real signal. */
1542 else if (lp
->status
)
1543 signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1544 else if (non_stop
&& !is_executing (lp
->ptid
))
1546 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1547 signo
= tp
->stop_signal
;
1551 struct target_waitstatus last
;
1554 get_last_target_status (&last_ptid
, &last
);
1556 if (GET_LWP (lp
->ptid
) == GET_LWP (last_ptid
))
1558 struct thread_info
*tp
= find_thread_ptid (lp
->ptid
);
1559 signo
= tp
->stop_signal
;
1565 if (signo
== TARGET_SIGNAL_0
)
1567 if (debug_linux_nat
)
1568 fprintf_unfiltered (gdb_stdlog
,
1569 "GPT: lwp %s has no pending signal\n",
1570 target_pid_to_str (lp
->ptid
));
1572 else if (!signal_pass_state (signo
))
1574 if (debug_linux_nat
)
1575 fprintf_unfiltered (gdb_stdlog
, "\
1576 GPT: lwp %s had signal %s, but it is in no pass state\n",
1577 target_pid_to_str (lp
->ptid
),
1578 target_signal_to_string (signo
));
1582 *status
= W_STOPCODE (target_signal_to_host (signo
));
1584 if (debug_linux_nat
)
1585 fprintf_unfiltered (gdb_stdlog
,
1586 "GPT: lwp %s has pending signal %s\n",
1587 target_pid_to_str (lp
->ptid
),
1588 target_signal_to_string (signo
));
1595 detach_callback (struct lwp_info
*lp
, void *data
)
1597 gdb_assert (lp
->status
== 0 || WIFSTOPPED (lp
->status
));
1599 if (debug_linux_nat
&& lp
->status
)
1600 fprintf_unfiltered (gdb_stdlog
, "DC: Pending %s for %s on detach.\n",
1601 strsignal (WSTOPSIG (lp
->status
)),
1602 target_pid_to_str (lp
->ptid
));
1604 /* If there is a pending SIGSTOP, get rid of it. */
1607 if (debug_linux_nat
)
1608 fprintf_unfiltered (gdb_stdlog
,
1609 "DC: Sending SIGCONT to %s\n",
1610 target_pid_to_str (lp
->ptid
));
1612 kill_lwp (GET_LWP (lp
->ptid
), SIGCONT
);
1616 /* We don't actually detach from the LWP that has an id equal to the
1617 overall process id just yet. */
1618 if (GET_LWP (lp
->ptid
) != GET_PID (lp
->ptid
))
1622 /* Pass on any pending signal for this LWP. */
1623 get_pending_status (lp
, &status
);
1626 if (ptrace (PTRACE_DETACH
, GET_LWP (lp
->ptid
), 0,
1627 WSTOPSIG (status
)) < 0)
1628 error (_("Can't detach %s: %s"), target_pid_to_str (lp
->ptid
),
1629 safe_strerror (errno
));
1631 if (debug_linux_nat
)
1632 fprintf_unfiltered (gdb_stdlog
,
1633 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1634 target_pid_to_str (lp
->ptid
),
1635 strsignal (WSTOPSIG (status
)));
1637 delete_lwp (lp
->ptid
);
1644 linux_nat_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1648 enum target_signal sig
;
1649 struct lwp_info
*main_lwp
;
1651 pid
= GET_PID (inferior_ptid
);
1653 if (target_can_async_p ())
1654 linux_nat_async (NULL
, 0);
1656 /* Stop all threads before detaching. ptrace requires that the
1657 thread is stopped to sucessfully detach. */
1658 iterate_over_lwps (pid_to_ptid (pid
), stop_callback
, NULL
);
1659 /* ... and wait until all of them have reported back that
1660 they're no longer running. */
1661 iterate_over_lwps (pid_to_ptid (pid
), stop_wait_callback
, NULL
);
1663 iterate_over_lwps (pid_to_ptid (pid
), detach_callback
, NULL
);
1665 /* Only the initial process should be left right now. */
1666 gdb_assert (num_lwps (GET_PID (inferior_ptid
)) == 1);
1668 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1670 /* Pass on any pending signal for the last LWP. */
1671 if ((args
== NULL
|| *args
== '\0')
1672 && get_pending_status (main_lwp
, &status
) != -1
1673 && WIFSTOPPED (status
))
1675 /* Put the signal number in ARGS so that inf_ptrace_detach will
1676 pass it along with PTRACE_DETACH. */
1678 sprintf (args
, "%d", (int) WSTOPSIG (status
));
1679 fprintf_unfiltered (gdb_stdlog
,
1680 "LND: Sending signal %s to %s\n",
1682 target_pid_to_str (main_lwp
->ptid
));
1685 delete_lwp (main_lwp
->ptid
);
1687 if (forks_exist_p ())
1689 /* Multi-fork case. The current inferior_ptid is being detached
1690 from, but there are other viable forks to debug. Detach from
1691 the current fork, and context-switch to the first
1693 linux_fork_detach (args
, from_tty
);
1695 if (non_stop
&& target_can_async_p ())
1696 target_async (inferior_event_handler
, 0);
1699 linux_ops
->to_detach (ops
, args
, from_tty
);
1705 resume_callback (struct lwp_info
*lp
, void *data
)
1707 if (lp
->stopped
&& lp
->status
== 0)
1709 if (debug_linux_nat
)
1710 fprintf_unfiltered (gdb_stdlog
,
1711 "RC: PTRACE_CONT %s, 0, 0 (resuming sibling)\n",
1712 target_pid_to_str (lp
->ptid
));
1714 linux_ops
->to_resume (linux_ops
,
1715 pid_to_ptid (GET_LWP (lp
->ptid
)),
1716 0, TARGET_SIGNAL_0
);
1717 if (debug_linux_nat
)
1718 fprintf_unfiltered (gdb_stdlog
,
1719 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1720 target_pid_to_str (lp
->ptid
));
1723 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1725 else if (lp
->stopped
&& debug_linux_nat
)
1726 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (has pending)\n",
1727 target_pid_to_str (lp
->ptid
));
1728 else if (debug_linux_nat
)
1729 fprintf_unfiltered (gdb_stdlog
, "RC: Not resuming sibling %s (not stopped)\n",
1730 target_pid_to_str (lp
->ptid
));
1736 resume_clear_callback (struct lwp_info
*lp
, void *data
)
1743 resume_set_callback (struct lwp_info
*lp
, void *data
)
1750 linux_nat_resume (struct target_ops
*ops
,
1751 ptid_t ptid
, int step
, enum target_signal signo
)
1754 struct lwp_info
*lp
;
1757 if (debug_linux_nat
)
1758 fprintf_unfiltered (gdb_stdlog
,
1759 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1760 step
? "step" : "resume",
1761 target_pid_to_str (ptid
),
1762 signo
? strsignal (signo
) : "0",
1763 target_pid_to_str (inferior_ptid
));
1765 block_child_signals (&prev_mask
);
1767 /* A specific PTID means `step only this process id'. */
1768 resume_many
= (ptid_equal (minus_one_ptid
, ptid
)
1769 || ptid_is_pid (ptid
));
1773 /* Mark the lwps we're resuming as resumed. */
1774 iterate_over_lwps (minus_one_ptid
, resume_clear_callback
, NULL
);
1775 iterate_over_lwps (ptid
, resume_set_callback
, NULL
);
1778 iterate_over_lwps (minus_one_ptid
, resume_set_callback
, NULL
);
1780 /* See if it's the current inferior that should be handled
1783 lp
= find_lwp_pid (inferior_ptid
);
1785 lp
= find_lwp_pid (ptid
);
1786 gdb_assert (lp
!= NULL
);
1788 /* Remember if we're stepping. */
1791 /* If we have a pending wait status for this thread, there is no
1792 point in resuming the process. But first make sure that
1793 linux_nat_wait won't preemptively handle the event - we
1794 should never take this short-circuit if we are going to
1795 leave LP running, since we have skipped resuming all the
1796 other threads. This bit of code needs to be synchronized
1797 with linux_nat_wait. */
1799 if (lp
->status
&& WIFSTOPPED (lp
->status
))
1802 struct inferior
*inf
;
1804 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
1806 saved_signo
= target_signal_from_host (WSTOPSIG (lp
->status
));
1808 /* Defer to common code if we're gaining control of the
1810 if (inf
->stop_soon
== NO_STOP_QUIETLY
1811 && signal_stop_state (saved_signo
) == 0
1812 && signal_print_state (saved_signo
) == 0
1813 && signal_pass_state (saved_signo
) == 1)
1815 if (debug_linux_nat
)
1816 fprintf_unfiltered (gdb_stdlog
,
1817 "LLR: Not short circuiting for ignored "
1818 "status 0x%x\n", lp
->status
);
1820 /* FIXME: What should we do if we are supposed to continue
1821 this thread with a signal? */
1822 gdb_assert (signo
== TARGET_SIGNAL_0
);
1823 signo
= saved_signo
;
1830 /* FIXME: What should we do if we are supposed to continue
1831 this thread with a signal? */
1832 gdb_assert (signo
== TARGET_SIGNAL_0
);
1834 if (debug_linux_nat
)
1835 fprintf_unfiltered (gdb_stdlog
,
1836 "LLR: Short circuiting for status 0x%x\n",
1839 restore_child_signals_mask (&prev_mask
);
1840 if (target_can_async_p ())
1842 target_async (inferior_event_handler
, 0);
1843 /* Tell the event loop we have something to process. */
1849 /* Mark LWP as not stopped to prevent it from being continued by
1854 iterate_over_lwps (ptid
, resume_callback
, NULL
);
1856 /* Convert to something the lower layer understands. */
1857 ptid
= pid_to_ptid (GET_LWP (lp
->ptid
));
1859 linux_ops
->to_resume (linux_ops
, ptid
, step
, signo
);
1860 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
1862 if (debug_linux_nat
)
1863 fprintf_unfiltered (gdb_stdlog
,
1864 "LLR: %s %s, %s (resume event thread)\n",
1865 step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1866 target_pid_to_str (ptid
),
1867 signo
? strsignal (signo
) : "0");
1869 restore_child_signals_mask (&prev_mask
);
1870 if (target_can_async_p ())
1871 target_async (inferior_event_handler
, 0);
1874 /* Issue kill to specified lwp. */
1876 static int tkill_failed
;
1879 kill_lwp (int lwpid
, int signo
)
1883 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1884 fails, then we are not using nptl threads and we should be using kill. */
1886 #ifdef HAVE_TKILL_SYSCALL
1889 int ret
= syscall (__NR_tkill
, lwpid
, signo
);
1890 if (errno
!= ENOSYS
)
1897 return kill (lwpid
, signo
);
1900 /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1901 event, check if the core is interested in it: if not, ignore the
1902 event, and keep waiting; otherwise, we need to toggle the LWP's
1903 syscall entry/exit status, since the ptrace event itself doesn't
1904 indicate it, and report the trap to higher layers. */
1907 linux_handle_syscall_trap (struct lwp_info
*lp
, int stopping
)
1909 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
1910 struct gdbarch
*gdbarch
= target_thread_architecture (lp
->ptid
);
1911 int syscall_number
= (int) gdbarch_get_syscall_number (gdbarch
, lp
->ptid
);
1915 /* If we're stopping threads, there's a SIGSTOP pending, which
1916 makes it so that the LWP reports an immediate syscall return,
1917 followed by the SIGSTOP. Skip seeing that "return" using
1918 PTRACE_CONT directly, and let stop_wait_callback collect the
1919 SIGSTOP. Later when the thread is resumed, a new syscall
1920 entry event. If we didn't do this (and returned 0), we'd
1921 leave a syscall entry pending, and our caller, by using
1922 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1923 itself. Later, when the user re-resumes this LWP, we'd see
1924 another syscall entry event and we'd mistake it for a return.
1926 If stop_wait_callback didn't force the SIGSTOP out of the LWP
1927 (leaving immediately with LWP->signalled set, without issuing
1928 a PTRACE_CONT), it would still be problematic to leave this
1929 syscall enter pending, as later when the thread is resumed,
1930 it would then see the same syscall exit mentioned above,
1931 followed by the delayed SIGSTOP, while the syscall didn't
1932 actually get to execute. It seems it would be even more
1933 confusing to the user. */
1935 if (debug_linux_nat
)
1936 fprintf_unfiltered (gdb_stdlog
,
1937 "LHST: ignoring syscall %d "
1938 "for LWP %ld (stopping threads), "
1939 "resuming with PTRACE_CONT for SIGSTOP\n",
1941 GET_LWP (lp
->ptid
));
1943 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
1944 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
1948 if (catch_syscall_enabled ())
1950 /* Always update the entry/return state, even if this particular
1951 syscall isn't interesting to the core now. In async mode,
1952 the user could install a new catchpoint for this syscall
1953 between syscall enter/return, and we'll need to know to
1954 report a syscall return if that happens. */
1955 lp
->syscall_state
= (lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1956 ? TARGET_WAITKIND_SYSCALL_RETURN
1957 : TARGET_WAITKIND_SYSCALL_ENTRY
);
1959 if (catching_syscall_number (syscall_number
))
1961 /* Alright, an event to report. */
1962 ourstatus
->kind
= lp
->syscall_state
;
1963 ourstatus
->value
.syscall_number
= syscall_number
;
1965 if (debug_linux_nat
)
1966 fprintf_unfiltered (gdb_stdlog
,
1967 "LHST: stopping for %s of syscall %d"
1969 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1970 ? "entry" : "return",
1972 GET_LWP (lp
->ptid
));
1976 if (debug_linux_nat
)
1977 fprintf_unfiltered (gdb_stdlog
,
1978 "LHST: ignoring %s of syscall %d "
1980 lp
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
1981 ? "entry" : "return",
1983 GET_LWP (lp
->ptid
));
1987 /* If we had been syscall tracing, and hence used PT_SYSCALL
1988 before on this LWP, it could happen that the user removes all
1989 syscall catchpoints before we get to process this event.
1990 There are two noteworthy issues here:
1992 - When stopped at a syscall entry event, resuming with
1993 PT_STEP still resumes executing the syscall and reports a
1996 - Only PT_SYSCALL catches syscall enters. If we last
1997 single-stepped this thread, then this event can't be a
1998 syscall enter. If we last single-stepped this thread, this
1999 has to be a syscall exit.
2001 The points above mean that the next resume, be it PT_STEP or
2002 PT_CONTINUE, can not trigger a syscall trace event. */
2003 if (debug_linux_nat
)
2004 fprintf_unfiltered (gdb_stdlog
,
2005 "LHST: caught syscall event with no syscall catchpoints."
2006 " %d for LWP %ld, ignoring\n",
2008 GET_LWP (lp
->ptid
));
2009 lp
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2012 /* The core isn't interested in this event. For efficiency, avoid
2013 stopping all threads only to have the core resume them all again.
2014 Since we're not stopping threads, if we're still syscall tracing
2015 and not stepping, we can't use PTRACE_CONT here, as we'd miss any
2016 subsequent syscall. Simply resume using the inf-ptrace layer,
2017 which knows when to use PT_SYSCALL or PT_CONTINUE. */
2019 /* Note that gdbarch_get_syscall_number may access registers, hence
2021 registers_changed ();
2022 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2023 lp
->step
, TARGET_SIGNAL_0
);
2027 /* Handle a GNU/Linux extended wait response. If we see a clone
2028 event, we need to add the new LWP to our list (and not report the
2029 trap to higher layers). This function returns non-zero if the
2030 event should be ignored and we should wait again. If STOPPING is
2031 true, the new LWP remains stopped, otherwise it is continued. */
2034 linux_handle_extended_wait (struct lwp_info
*lp
, int status
,
2037 int pid
= GET_LWP (lp
->ptid
);
2038 struct target_waitstatus
*ourstatus
= &lp
->waitstatus
;
2039 struct lwp_info
*new_lp
= NULL
;
2040 int event
= status
>> 16;
2042 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
2043 || event
== PTRACE_EVENT_CLONE
)
2045 unsigned long new_pid
;
2048 ptrace (PTRACE_GETEVENTMSG
, pid
, 0, &new_pid
);
2050 /* If we haven't already seen the new PID stop, wait for it now. */
2051 if (! pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
2053 /* The new child has a pending SIGSTOP. We can't affect it until it
2054 hits the SIGSTOP, but we're already attached. */
2055 ret
= my_waitpid (new_pid
, &status
,
2056 (event
== PTRACE_EVENT_CLONE
) ? __WCLONE
: 0);
2058 perror_with_name (_("waiting for new child"));
2059 else if (ret
!= new_pid
)
2060 internal_error (__FILE__
, __LINE__
,
2061 _("wait returned unexpected PID %d"), ret
);
2062 else if (!WIFSTOPPED (status
))
2063 internal_error (__FILE__
, __LINE__
,
2064 _("wait returned unexpected status 0x%x"), status
);
2067 ourstatus
->value
.related_pid
= ptid_build (new_pid
, new_pid
, 0);
2069 if (event
== PTRACE_EVENT_FORK
2070 && linux_fork_checkpointing_p (GET_PID (lp
->ptid
)))
2072 struct fork_info
*fp
;
2074 /* Handle checkpointing by linux-fork.c here as a special
2075 case. We don't want the follow-fork-mode or 'catch fork'
2076 to interfere with this. */
2078 /* This won't actually modify the breakpoint list, but will
2079 physically remove the breakpoints from the child. */
2080 detach_breakpoints (new_pid
);
2082 /* Retain child fork in ptrace (stopped) state. */
2083 fp
= find_fork_pid (new_pid
);
2085 fp
= add_fork (new_pid
);
2087 /* Report as spurious, so that infrun doesn't want to follow
2088 this fork. We're actually doing an infcall in
2090 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
2091 linux_enable_event_reporting (pid_to_ptid (new_pid
));
2093 /* Report the stop to the core. */
2097 if (event
== PTRACE_EVENT_FORK
)
2098 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
2099 else if (event
== PTRACE_EVENT_VFORK
)
2100 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
2103 struct cleanup
*old_chain
;
2105 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2106 new_lp
= add_lwp (BUILD_LWP (new_pid
, GET_PID (lp
->ptid
)));
2108 new_lp
->stopped
= 1;
2110 if (WSTOPSIG (status
) != SIGSTOP
)
2112 /* This can happen if someone starts sending signals to
2113 the new thread before it gets a chance to run, which
2114 have a lower number than SIGSTOP (e.g. SIGUSR1).
2115 This is an unlikely case, and harder to handle for
2116 fork / vfork than for clone, so we do not try - but
2117 we handle it for clone events here. We'll send
2118 the other signal on to the thread below. */
2120 new_lp
->signalled
= 1;
2127 /* Add the new thread to GDB's lists as soon as possible
2130 1) the frontend doesn't have to wait for a stop to
2133 2) we tag it with the correct running state. */
2135 /* If the thread_db layer is active, let it know about
2136 this new thread, and add it to GDB's list. */
2137 if (!thread_db_attach_lwp (new_lp
->ptid
))
2139 /* We're not using thread_db. Add it to GDB's
2141 target_post_attach (GET_LWP (new_lp
->ptid
));
2142 add_thread (new_lp
->ptid
);
2147 set_running (new_lp
->ptid
, 1);
2148 set_executing (new_lp
->ptid
, 1);
2152 /* Note the need to use the low target ops to resume, to
2153 handle resuming with PT_SYSCALL if we have syscall
2159 new_lp
->stopped
= 0;
2160 new_lp
->resumed
= 1;
2163 ? target_signal_from_host (WSTOPSIG (status
))
2166 linux_ops
->to_resume (linux_ops
, pid_to_ptid (new_pid
),
2170 if (debug_linux_nat
)
2171 fprintf_unfiltered (gdb_stdlog
,
2172 "LHEW: Got clone event from LWP %ld, resuming\n",
2173 GET_LWP (lp
->ptid
));
2174 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2175 0, TARGET_SIGNAL_0
);
2183 if (event
== PTRACE_EVENT_EXEC
)
2185 if (debug_linux_nat
)
2186 fprintf_unfiltered (gdb_stdlog
,
2187 "LHEW: Got exec event from LWP %ld\n",
2188 GET_LWP (lp
->ptid
));
2190 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
2191 ourstatus
->value
.execd_pathname
2192 = xstrdup (linux_child_pid_to_exec_file (pid
));
2194 if (linux_parent_pid
)
2196 detach_breakpoints (linux_parent_pid
);
2197 ptrace (PTRACE_DETACH
, linux_parent_pid
, 0, 0);
2199 linux_parent_pid
= 0;
2202 /* At this point, all inserted breakpoints are gone. Doing this
2203 as soon as we detect an exec prevents the badness of deleting
2204 a breakpoint writing the current "shadow contents" to lift
2205 the bp. That shadow is NOT valid after an exec.
2207 Note that we have to do this after the detach_breakpoints
2208 call above, otherwise breakpoints wouldn't be lifted from the
2209 parent on a vfork, because detach_breakpoints would think
2210 that breakpoints are not inserted. */
2211 mark_breakpoints_out ();
2215 internal_error (__FILE__
, __LINE__
,
2216 _("unknown ptrace event %d"), event
);
2219 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2223 wait_lwp (struct lwp_info
*lp
)
2227 int thread_dead
= 0;
2229 gdb_assert (!lp
->stopped
);
2230 gdb_assert (lp
->status
== 0);
2232 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, 0);
2233 if (pid
== -1 && errno
== ECHILD
)
2235 pid
= my_waitpid (GET_LWP (lp
->ptid
), &status
, __WCLONE
);
2236 if (pid
== -1 && errno
== ECHILD
)
2238 /* The thread has previously exited. We need to delete it
2239 now because, for some vendor 2.4 kernels with NPTL
2240 support backported, there won't be an exit event unless
2241 it is the main thread. 2.6 kernels will report an exit
2242 event for each thread that exits, as expected. */
2244 if (debug_linux_nat
)
2245 fprintf_unfiltered (gdb_stdlog
, "WL: %s vanished.\n",
2246 target_pid_to_str (lp
->ptid
));
2252 gdb_assert (pid
== GET_LWP (lp
->ptid
));
2254 if (debug_linux_nat
)
2256 fprintf_unfiltered (gdb_stdlog
,
2257 "WL: waitpid %s received %s\n",
2258 target_pid_to_str (lp
->ptid
),
2259 status_to_str (status
));
2263 /* Check if the thread has exited. */
2264 if (WIFEXITED (status
) || WIFSIGNALED (status
))
2267 if (debug_linux_nat
)
2268 fprintf_unfiltered (gdb_stdlog
, "WL: %s exited.\n",
2269 target_pid_to_str (lp
->ptid
));
2278 gdb_assert (WIFSTOPPED (status
));
2280 /* Handle GNU/Linux's syscall SIGTRAPs. */
2281 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2283 /* No longer need the sysgood bit. The ptrace event ends up
2284 recorded in lp->waitstatus if we care for it. We can carry
2285 on handling the event like a regular SIGTRAP from here
2287 status
= W_STOPCODE (SIGTRAP
);
2288 if (linux_handle_syscall_trap (lp
, 1))
2289 return wait_lwp (lp
);
2292 /* Handle GNU/Linux's extended waitstatus for trace events. */
2293 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2295 if (debug_linux_nat
)
2296 fprintf_unfiltered (gdb_stdlog
,
2297 "WL: Handling extended status 0x%06x\n",
2299 if (linux_handle_extended_wait (lp
, status
, 1))
2300 return wait_lwp (lp
);
2306 /* Save the most recent siginfo for LP. This is currently only called
2307 for SIGTRAP; some ports use the si_addr field for
2308 target_stopped_data_address. In the future, it may also be used to
2309 restore the siginfo of requeued signals. */
2312 save_siginfo (struct lwp_info
*lp
)
2315 ptrace (PTRACE_GETSIGINFO
, GET_LWP (lp
->ptid
),
2316 (PTRACE_TYPE_ARG3
) 0, &lp
->siginfo
);
2319 memset (&lp
->siginfo
, 0, sizeof (lp
->siginfo
));
2322 /* Send a SIGSTOP to LP. */
2325 stop_callback (struct lwp_info
*lp
, void *data
)
2327 if (!lp
->stopped
&& !lp
->signalled
)
2331 if (debug_linux_nat
)
2333 fprintf_unfiltered (gdb_stdlog
,
2334 "SC: kill %s **<SIGSTOP>**\n",
2335 target_pid_to_str (lp
->ptid
));
2338 ret
= kill_lwp (GET_LWP (lp
->ptid
), SIGSTOP
);
2339 if (debug_linux_nat
)
2341 fprintf_unfiltered (gdb_stdlog
,
2342 "SC: lwp kill %d %s\n",
2344 errno
? safe_strerror (errno
) : "ERRNO-OK");
2348 gdb_assert (lp
->status
== 0);
2354 /* Return non-zero if LWP PID has a pending SIGINT. */
2357 linux_nat_has_pending_sigint (int pid
)
2359 sigset_t pending
, blocked
, ignored
;
2362 linux_proc_pending_signals (pid
, &pending
, &blocked
, &ignored
);
2364 if (sigismember (&pending
, SIGINT
)
2365 && !sigismember (&ignored
, SIGINT
))
2371 /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2374 set_ignore_sigint (struct lwp_info
*lp
, void *data
)
2376 /* If a thread has a pending SIGINT, consume it; otherwise, set a
2377 flag to consume the next one. */
2378 if (lp
->stopped
&& lp
->status
!= 0 && WIFSTOPPED (lp
->status
)
2379 && WSTOPSIG (lp
->status
) == SIGINT
)
2382 lp
->ignore_sigint
= 1;
2387 /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2388 This function is called after we know the LWP has stopped; if the LWP
2389 stopped before the expected SIGINT was delivered, then it will never have
2390 arrived. Also, if the signal was delivered to a shared queue and consumed
2391 by a different thread, it will never be delivered to this LWP. */
2394 maybe_clear_ignore_sigint (struct lwp_info
*lp
)
2396 if (!lp
->ignore_sigint
)
2399 if (!linux_nat_has_pending_sigint (GET_LWP (lp
->ptid
)))
2401 if (debug_linux_nat
)
2402 fprintf_unfiltered (gdb_stdlog
,
2403 "MCIS: Clearing bogus flag for %s\n",
2404 target_pid_to_str (lp
->ptid
));
2405 lp
->ignore_sigint
= 0;
2409 /* Wait until LP is stopped. */
2412 stop_wait_callback (struct lwp_info
*lp
, void *data
)
2418 status
= wait_lwp (lp
);
2422 if (lp
->ignore_sigint
&& WIFSTOPPED (status
)
2423 && WSTOPSIG (status
) == SIGINT
)
2425 lp
->ignore_sigint
= 0;
2428 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2429 if (debug_linux_nat
)
2430 fprintf_unfiltered (gdb_stdlog
,
2431 "PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)\n",
2432 target_pid_to_str (lp
->ptid
),
2433 errno
? safe_strerror (errno
) : "OK");
2435 return stop_wait_callback (lp
, NULL
);
2438 maybe_clear_ignore_sigint (lp
);
2440 if (WSTOPSIG (status
) != SIGSTOP
)
2442 if (WSTOPSIG (status
) == SIGTRAP
)
2444 /* If a LWP other than the LWP that we're reporting an
2445 event for has hit a GDB breakpoint (as opposed to
2446 some random trap signal), then just arrange for it to
2447 hit it again later. We don't keep the SIGTRAP status
2448 and don't forward the SIGTRAP signal to the LWP. We
2449 will handle the current event, eventually we will
2450 resume all LWPs, and this one will get its breakpoint
2453 If we do not do this, then we run the risk that the
2454 user will delete or disable the breakpoint, but the
2455 thread will have already tripped on it. */
2457 /* Save the trap's siginfo in case we need it later. */
2460 /* Now resume this LWP and get the SIGSTOP event. */
2462 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2463 if (debug_linux_nat
)
2465 fprintf_unfiltered (gdb_stdlog
,
2466 "PTRACE_CONT %s, 0, 0 (%s)\n",
2467 target_pid_to_str (lp
->ptid
),
2468 errno
? safe_strerror (errno
) : "OK");
2470 fprintf_unfiltered (gdb_stdlog
,
2471 "SWC: Candidate SIGTRAP event in %s\n",
2472 target_pid_to_str (lp
->ptid
));
2474 /* Hold this event/waitstatus while we check to see if
2475 there are any more (we still want to get that SIGSTOP). */
2476 stop_wait_callback (lp
, NULL
);
2478 /* Hold the SIGTRAP for handling by linux_nat_wait. If
2479 there's another event, throw it back into the
2483 if (debug_linux_nat
)
2484 fprintf_unfiltered (gdb_stdlog
,
2485 "SWC: kill %s, %s\n",
2486 target_pid_to_str (lp
->ptid
),
2487 status_to_str ((int) status
));
2488 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
2491 /* Save the sigtrap event. */
2492 lp
->status
= status
;
2497 /* The thread was stopped with a signal other than
2498 SIGSTOP, and didn't accidentally trip a breakpoint. */
2500 if (debug_linux_nat
)
2502 fprintf_unfiltered (gdb_stdlog
,
2503 "SWC: Pending event %s in %s\n",
2504 status_to_str ((int) status
),
2505 target_pid_to_str (lp
->ptid
));
2507 /* Now resume this LWP and get the SIGSTOP event. */
2509 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
2510 if (debug_linux_nat
)
2511 fprintf_unfiltered (gdb_stdlog
,
2512 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
2513 target_pid_to_str (lp
->ptid
),
2514 errno
? safe_strerror (errno
) : "OK");
2516 /* Hold this event/waitstatus while we check to see if
2517 there are any more (we still want to get that SIGSTOP). */
2518 stop_wait_callback (lp
, NULL
);
2520 /* If the lp->status field is still empty, use it to
2521 hold this event. If not, then this event must be
2522 returned to the event queue of the LWP. */
2525 if (debug_linux_nat
)
2527 fprintf_unfiltered (gdb_stdlog
,
2528 "SWC: kill %s, %s\n",
2529 target_pid_to_str (lp
->ptid
),
2530 status_to_str ((int) status
));
2532 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (status
));
2535 lp
->status
= status
;
2541 /* We caught the SIGSTOP that we intended to catch, so
2542 there's no SIGSTOP pending. */
2551 /* Return non-zero if LP has a wait status pending. */
2554 status_callback (struct lwp_info
*lp
, void *data
)
2556 /* Only report a pending wait status if we pretend that this has
2557 indeed been resumed. */
2561 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
2563 /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2564 or a a pending process exit. Note that `W_EXITCODE(0,0) ==
2565 0', so a clean process exit can not be stored pending in
2566 lp->status, it is indistinguishable from
2567 no-pending-status. */
2571 if (lp
->status
!= 0)
2577 /* Return non-zero if LP isn't stopped. */
2580 running_callback (struct lwp_info
*lp
, void *data
)
2582 return (lp
->stopped
== 0 || (lp
->status
!= 0 && lp
->resumed
));
2585 /* Count the LWP's that have had events. */
2588 count_events_callback (struct lwp_info
*lp
, void *data
)
2592 gdb_assert (count
!= NULL
);
2594 /* Count only resumed LWPs that have a SIGTRAP event pending. */
2595 if (lp
->status
!= 0 && lp
->resumed
2596 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2602 /* Select the LWP (if any) that is currently being single-stepped. */
2605 select_singlestep_lwp_callback (struct lwp_info
*lp
, void *data
)
2607 if (lp
->step
&& lp
->status
!= 0)
2613 /* Select the Nth LWP that has had a SIGTRAP event. */
2616 select_event_lwp_callback (struct lwp_info
*lp
, void *data
)
2618 int *selector
= data
;
2620 gdb_assert (selector
!= NULL
);
2622 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2623 if (lp
->status
!= 0 && lp
->resumed
2624 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
)
2625 if ((*selector
)-- == 0)
2632 cancel_breakpoint (struct lwp_info
*lp
)
2634 /* Arrange for a breakpoint to be hit again later. We don't keep
2635 the SIGTRAP status and don't forward the SIGTRAP signal to the
2636 LWP. We will handle the current event, eventually we will resume
2637 this LWP, and this breakpoint will trap again.
2639 If we do not do this, then we run the risk that the user will
2640 delete or disable the breakpoint, but the LWP will have already
2643 struct regcache
*regcache
= get_thread_regcache (lp
->ptid
);
2644 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2647 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
2648 if (breakpoint_inserted_here_p (pc
))
2650 if (debug_linux_nat
)
2651 fprintf_unfiltered (gdb_stdlog
,
2652 "CB: Push back breakpoint for %s\n",
2653 target_pid_to_str (lp
->ptid
));
2655 /* Back up the PC if necessary. */
2656 if (gdbarch_decr_pc_after_break (gdbarch
))
2657 regcache_write_pc (regcache
, pc
);
2665 cancel_breakpoints_callback (struct lwp_info
*lp
, void *data
)
2667 struct lwp_info
*event_lp
= data
;
2669 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2673 /* If a LWP other than the LWP that we're reporting an event for has
2674 hit a GDB breakpoint (as opposed to some random trap signal),
2675 then just arrange for it to hit it again later. We don't keep
2676 the SIGTRAP status and don't forward the SIGTRAP signal to the
2677 LWP. We will handle the current event, eventually we will resume
2678 all LWPs, and this one will get its breakpoint trap again.
2680 If we do not do this, then we run the risk that the user will
2681 delete or disable the breakpoint, but the LWP will have already
2684 if (lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
2686 && WIFSTOPPED (lp
->status
) && WSTOPSIG (lp
->status
) == SIGTRAP
2687 && cancel_breakpoint (lp
))
2688 /* Throw away the SIGTRAP. */
2694 /* Select one LWP out of those that have events pending. */
2697 select_event_lwp (ptid_t filter
, struct lwp_info
**orig_lp
, int *status
)
2700 int random_selector
;
2701 struct lwp_info
*event_lp
;
2703 /* Record the wait status for the original LWP. */
2704 (*orig_lp
)->status
= *status
;
2706 /* Give preference to any LWP that is being single-stepped. */
2707 event_lp
= iterate_over_lwps (filter
,
2708 select_singlestep_lwp_callback
, NULL
);
2709 if (event_lp
!= NULL
)
2711 if (debug_linux_nat
)
2712 fprintf_unfiltered (gdb_stdlog
,
2713 "SEL: Select single-step %s\n",
2714 target_pid_to_str (event_lp
->ptid
));
2718 /* No single-stepping LWP. Select one at random, out of those
2719 which have had SIGTRAP events. */
2721 /* First see how many SIGTRAP events we have. */
2722 iterate_over_lwps (filter
, count_events_callback
, &num_events
);
2724 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2725 random_selector
= (int)
2726 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2728 if (debug_linux_nat
&& num_events
> 1)
2729 fprintf_unfiltered (gdb_stdlog
,
2730 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2731 num_events
, random_selector
);
2733 event_lp
= iterate_over_lwps (filter
,
2734 select_event_lwp_callback
,
2738 if (event_lp
!= NULL
)
2740 /* Switch the event LWP. */
2741 *orig_lp
= event_lp
;
2742 *status
= event_lp
->status
;
2745 /* Flush the wait status for the event LWP. */
2746 (*orig_lp
)->status
= 0;
2749 /* Return non-zero if LP has been resumed. */
2752 resumed_callback (struct lwp_info
*lp
, void *data
)
2757 /* Stop an active thread, verify it still exists, then resume it. */
2760 stop_and_resume_callback (struct lwp_info
*lp
, void *data
)
2762 struct lwp_info
*ptr
;
2764 if (!lp
->stopped
&& !lp
->signalled
)
2766 stop_callback (lp
, NULL
);
2767 stop_wait_callback (lp
, NULL
);
2768 /* Resume if the lwp still exists. */
2769 for (ptr
= lwp_list
; ptr
; ptr
= ptr
->next
)
2772 resume_callback (lp
, NULL
);
2773 resume_set_callback (lp
, NULL
);
2779 /* Check if we should go on and pass this event to common code.
2780 Return the affected lwp if we are, or NULL otherwise. */
2781 static struct lwp_info
*
2782 linux_nat_filter_event (int lwpid
, int status
, int options
)
2784 struct lwp_info
*lp
;
2786 lp
= find_lwp_pid (pid_to_ptid (lwpid
));
2788 /* Check for stop events reported by a process we didn't already
2789 know about - anything not already in our LWP list.
2791 If we're expecting to receive stopped processes after
2792 fork, vfork, and clone events, then we'll just add the
2793 new one to our list and go back to waiting for the event
2794 to be reported - the stopped process might be returned
2795 from waitpid before or after the event is. */
2796 if (WIFSTOPPED (status
) && !lp
)
2798 linux_record_stopped_pid (lwpid
, status
);
2802 /* Make sure we don't report an event for the exit of an LWP not in
2803 our list, i.e. not part of the current process. This can happen
2804 if we detach from a program we original forked and then it
2806 if (!WIFSTOPPED (status
) && !lp
)
2809 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2810 CLONE_PTRACE processes which do not use the thread library -
2811 otherwise we wouldn't find the new LWP this way. That doesn't
2812 currently work, and the following code is currently unreachable
2813 due to the two blocks above. If it's fixed some day, this code
2814 should be broken out into a function so that we can also pick up
2815 LWPs from the new interface. */
2818 lp
= add_lwp (BUILD_LWP (lwpid
, GET_PID (inferior_ptid
)));
2819 if (options
& __WCLONE
)
2822 gdb_assert (WIFSTOPPED (status
)
2823 && WSTOPSIG (status
) == SIGSTOP
);
2826 if (!in_thread_list (inferior_ptid
))
2828 inferior_ptid
= BUILD_LWP (GET_PID (inferior_ptid
),
2829 GET_PID (inferior_ptid
));
2830 add_thread (inferior_ptid
);
2833 add_thread (lp
->ptid
);
2836 /* Handle GNU/Linux's syscall SIGTRAPs. */
2837 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SYSCALL_SIGTRAP
)
2839 /* No longer need the sysgood bit. The ptrace event ends up
2840 recorded in lp->waitstatus if we care for it. We can carry
2841 on handling the event like a regular SIGTRAP from here
2843 status
= W_STOPCODE (SIGTRAP
);
2844 if (linux_handle_syscall_trap (lp
, 0))
2848 /* Handle GNU/Linux's extended waitstatus for trace events. */
2849 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
2851 if (debug_linux_nat
)
2852 fprintf_unfiltered (gdb_stdlog
,
2853 "LLW: Handling extended status 0x%06x\n",
2855 if (linux_handle_extended_wait (lp
, status
, 0))
2859 /* Save the trap's siginfo in case we need it later. */
2860 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
2863 /* Check if the thread has exited. */
2864 if ((WIFEXITED (status
) || WIFSIGNALED (status
))
2865 && num_lwps (GET_PID (lp
->ptid
)) > 1)
2867 /* If this is the main thread, we must stop all threads and verify
2868 if they are still alive. This is because in the nptl thread model
2869 on Linux 2.4, there is no signal issued for exiting LWPs
2870 other than the main thread. We only get the main thread exit
2871 signal once all child threads have already exited. If we
2872 stop all the threads and use the stop_wait_callback to check
2873 if they have exited we can determine whether this signal
2874 should be ignored or whether it means the end of the debugged
2875 application, regardless of which threading model is being
2877 if (GET_PID (lp
->ptid
) == GET_LWP (lp
->ptid
))
2880 iterate_over_lwps (pid_to_ptid (GET_PID (lp
->ptid
)),
2881 stop_and_resume_callback
, NULL
);
2884 if (debug_linux_nat
)
2885 fprintf_unfiltered (gdb_stdlog
,
2886 "LLW: %s exited.\n",
2887 target_pid_to_str (lp
->ptid
));
2889 if (num_lwps (GET_PID (lp
->ptid
)) > 1)
2891 /* If there is at least one more LWP, then the exit signal
2892 was not the end of the debugged application and should be
2899 /* Check if the current LWP has previously exited. In the nptl
2900 thread model, LWPs other than the main thread do not issue
2901 signals when they exit so we must check whenever the thread has
2902 stopped. A similar check is made in stop_wait_callback(). */
2903 if (num_lwps (GET_PID (lp
->ptid
)) > 1 && !linux_thread_alive (lp
->ptid
))
2905 ptid_t ptid
= pid_to_ptid (GET_PID (lp
->ptid
));
2907 if (debug_linux_nat
)
2908 fprintf_unfiltered (gdb_stdlog
,
2909 "LLW: %s exited.\n",
2910 target_pid_to_str (lp
->ptid
));
2914 /* Make sure there is at least one thread running. */
2915 gdb_assert (iterate_over_lwps (ptid
, running_callback
, NULL
));
2917 /* Discard the event. */
2921 /* Make sure we don't report a SIGSTOP that we sent ourselves in
2922 an attempt to stop an LWP. */
2924 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGSTOP
)
2926 if (debug_linux_nat
)
2927 fprintf_unfiltered (gdb_stdlog
,
2928 "LLW: Delayed SIGSTOP caught for %s.\n",
2929 target_pid_to_str (lp
->ptid
));
2931 /* This is a delayed SIGSTOP. */
2934 registers_changed ();
2936 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2937 lp
->step
, TARGET_SIGNAL_0
);
2938 if (debug_linux_nat
)
2939 fprintf_unfiltered (gdb_stdlog
,
2940 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2942 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2943 target_pid_to_str (lp
->ptid
));
2946 gdb_assert (lp
->resumed
);
2948 /* Discard the event. */
2952 /* Make sure we don't report a SIGINT that we have already displayed
2953 for another thread. */
2954 if (lp
->ignore_sigint
2955 && WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGINT
)
2957 if (debug_linux_nat
)
2958 fprintf_unfiltered (gdb_stdlog
,
2959 "LLW: Delayed SIGINT caught for %s.\n",
2960 target_pid_to_str (lp
->ptid
));
2962 /* This is a delayed SIGINT. */
2963 lp
->ignore_sigint
= 0;
2965 registers_changed ();
2966 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
2967 lp
->step
, TARGET_SIGNAL_0
);
2968 if (debug_linux_nat
)
2969 fprintf_unfiltered (gdb_stdlog
,
2970 "LLW: %s %s, 0, 0 (discard SIGINT)\n",
2972 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2973 target_pid_to_str (lp
->ptid
));
2976 gdb_assert (lp
->resumed
);
2978 /* Discard the event. */
2982 /* An interesting event. */
2984 lp
->status
= status
;
2989 linux_nat_wait_1 (struct target_ops
*ops
,
2990 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
2993 static sigset_t prev_mask
;
2994 struct lwp_info
*lp
= NULL
;
2999 if (debug_linux_nat_async
)
3000 fprintf_unfiltered (gdb_stdlog
, "LLW: enter\n");
3002 /* The first time we get here after starting a new inferior, we may
3003 not have added it to the LWP list yet - this is the earliest
3004 moment at which we know its PID. */
3005 if (ptid_is_pid (inferior_ptid
))
3007 /* Upgrade the main thread's ptid. */
3008 thread_change_ptid (inferior_ptid
,
3009 BUILD_LWP (GET_PID (inferior_ptid
),
3010 GET_PID (inferior_ptid
)));
3012 lp
= add_lwp (inferior_ptid
);
3016 /* Make sure SIGCHLD is blocked. */
3017 block_child_signals (&prev_mask
);
3019 if (ptid_equal (ptid
, minus_one_ptid
))
3021 else if (ptid_is_pid (ptid
))
3022 /* A request to wait for a specific tgid. This is not possible
3023 with waitpid, so instead, we wait for any child, and leave
3024 children we're not interested in right now with a pending
3025 status to report later. */
3028 pid
= GET_LWP (ptid
);
3034 /* Make sure there is at least one LWP that has been resumed. */
3035 gdb_assert (iterate_over_lwps (ptid
, resumed_callback
, NULL
));
3037 /* First check if there is a LWP with a wait status pending. */
3040 /* Any LWP that's been resumed will do. */
3041 lp
= iterate_over_lwps (ptid
, status_callback
, NULL
);
3044 if (debug_linux_nat
&& lp
->status
)
3045 fprintf_unfiltered (gdb_stdlog
,
3046 "LLW: Using pending wait status %s for %s.\n",
3047 status_to_str (lp
->status
),
3048 target_pid_to_str (lp
->ptid
));
3051 /* But if we don't find one, we'll have to wait, and check both
3052 cloned and uncloned processes. We start with the cloned
3054 options
= __WCLONE
| WNOHANG
;
3056 else if (is_lwp (ptid
))
3058 if (debug_linux_nat
)
3059 fprintf_unfiltered (gdb_stdlog
,
3060 "LLW: Waiting for specific LWP %s.\n",
3061 target_pid_to_str (ptid
));
3063 /* We have a specific LWP to check. */
3064 lp
= find_lwp_pid (ptid
);
3067 if (debug_linux_nat
&& lp
->status
)
3068 fprintf_unfiltered (gdb_stdlog
,
3069 "LLW: Using pending wait status %s for %s.\n",
3070 status_to_str (lp
->status
),
3071 target_pid_to_str (lp
->ptid
));
3073 /* If we have to wait, take into account whether PID is a cloned
3074 process or not. And we have to convert it to something that
3075 the layer beneath us can understand. */
3076 options
= lp
->cloned
? __WCLONE
: 0;
3077 pid
= GET_LWP (ptid
);
3079 /* We check for lp->waitstatus in addition to lp->status,
3080 because we can have pending process exits recorded in
3081 lp->status and W_EXITCODE(0,0) == 0. We should probably have
3082 an additional lp->status_p flag. */
3083 if (lp
->status
== 0 && lp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
)
3087 if (lp
&& lp
->signalled
)
3089 /* A pending SIGSTOP may interfere with the normal stream of
3090 events. In a typical case where interference is a problem,
3091 we have a SIGSTOP signal pending for LWP A while
3092 single-stepping it, encounter an event in LWP B, and take the
3093 pending SIGSTOP while trying to stop LWP A. After processing
3094 the event in LWP B, LWP A is continued, and we'll never see
3095 the SIGTRAP associated with the last time we were
3096 single-stepping LWP A. */
3098 /* Resume the thread. It should halt immediately returning the
3100 registers_changed ();
3101 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3102 lp
->step
, TARGET_SIGNAL_0
);
3103 if (debug_linux_nat
)
3104 fprintf_unfiltered (gdb_stdlog
,
3105 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
3106 lp
->step
? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3107 target_pid_to_str (lp
->ptid
));
3109 gdb_assert (lp
->resumed
);
3111 /* Catch the pending SIGSTOP. */
3112 status
= lp
->status
;
3115 stop_wait_callback (lp
, NULL
);
3117 /* If the lp->status field isn't empty, we caught another signal
3118 while flushing the SIGSTOP. Return it back to the event
3119 queue of the LWP, as we already have an event to handle. */
3122 if (debug_linux_nat
)
3123 fprintf_unfiltered (gdb_stdlog
,
3124 "LLW: kill %s, %s\n",
3125 target_pid_to_str (lp
->ptid
),
3126 status_to_str (lp
->status
));
3127 kill_lwp (GET_LWP (lp
->ptid
), WSTOPSIG (lp
->status
));
3130 lp
->status
= status
;
3133 if (!target_can_async_p ())
3135 /* Causes SIGINT to be passed on to the attached process. */
3139 /* Translate generic target_wait options into waitpid options. */
3140 if (target_options
& TARGET_WNOHANG
)
3147 lwpid
= my_waitpid (pid
, &status
, options
);
3151 gdb_assert (pid
== -1 || lwpid
== pid
);
3153 if (debug_linux_nat
)
3155 fprintf_unfiltered (gdb_stdlog
,
3156 "LLW: waitpid %ld received %s\n",
3157 (long) lwpid
, status_to_str (status
));
3160 lp
= linux_nat_filter_event (lwpid
, status
, options
);
3163 && ptid_is_pid (ptid
)
3164 && ptid_get_pid (lp
->ptid
) != ptid_get_pid (ptid
))
3166 if (debug_linux_nat
)
3167 fprintf (stderr
, "LWP %ld got an event %06x, leaving pending.\n",
3168 ptid_get_lwp (lp
->ptid
), status
);
3170 if (WIFSTOPPED (lp
->status
))
3172 if (WSTOPSIG (lp
->status
) != SIGSTOP
)
3174 stop_callback (lp
, NULL
);
3176 /* Resume in order to collect the sigstop. */
3177 ptrace (PTRACE_CONT
, GET_LWP (lp
->ptid
), 0, 0);
3179 stop_wait_callback (lp
, NULL
);
3187 else if (WIFEXITED (status
) || WIFSIGNALED (status
))
3189 if (debug_linux_nat
)
3190 fprintf (stderr
, "Process %ld exited while stopping LWPs\n",
3191 ptid_get_lwp (lp
->ptid
));
3193 /* This was the last lwp in the process. Since
3194 events are serialized to GDB core, and we can't
3195 report this one right now, but GDB core and the
3196 other target layers will want to be notified
3197 about the exit code/signal, leave the status
3198 pending for the next time we're able to report
3201 /* Prevent trying to stop this thread again. We'll
3202 never try to resume it because it has a pending
3206 /* Dead LWP's aren't expected to reported a pending
3210 /* Store the pending event in the waitstatus as
3211 well, because W_EXITCODE(0,0) == 0. */
3212 store_waitstatus (&lp
->waitstatus
, lp
->status
);
3226 /* waitpid did return something. Restart over. */
3227 options
|= __WCLONE
;
3235 /* Alternate between checking cloned and uncloned processes. */
3236 options
^= __WCLONE
;
3238 /* And every time we have checked both:
3239 In async mode, return to event loop;
3240 In sync mode, suspend waiting for a SIGCHLD signal. */
3241 if (options
& __WCLONE
)
3243 if (target_options
& TARGET_WNOHANG
)
3245 /* No interesting event. */
3246 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3248 if (debug_linux_nat_async
)
3249 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3251 restore_child_signals_mask (&prev_mask
);
3252 return minus_one_ptid
;
3255 sigsuspend (&suspend_mask
);
3258 else if (target_options
& TARGET_WNOHANG
)
3260 /* No interesting event for PID yet. */
3261 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3263 if (debug_linux_nat_async
)
3264 fprintf_unfiltered (gdb_stdlog
, "LLW: exit (ignore)\n");
3266 restore_child_signals_mask (&prev_mask
);
3267 return minus_one_ptid
;
3270 /* We shouldn't end up here unless we want to try again. */
3271 gdb_assert (lp
== NULL
);
3274 if (!target_can_async_p ())
3275 clear_sigint_trap ();
3279 status
= lp
->status
;
3282 /* Don't report signals that GDB isn't interested in, such as
3283 signals that are neither printed nor stopped upon. Stopping all
3284 threads can be a bit time-consuming so if we want decent
3285 performance with heavily multi-threaded programs, especially when
3286 they're using a high frequency timer, we'd better avoid it if we
3289 if (WIFSTOPPED (status
))
3291 int signo
= target_signal_from_host (WSTOPSIG (status
));
3292 struct inferior
*inf
;
3294 inf
= find_inferior_pid (ptid_get_pid (lp
->ptid
));
3297 /* Defer to common code if we get a signal while
3298 single-stepping, since that may need special care, e.g. to
3299 skip the signal handler, or, if we're gaining control of the
3302 && inf
->stop_soon
== NO_STOP_QUIETLY
3303 && signal_stop_state (signo
) == 0
3304 && signal_print_state (signo
) == 0
3305 && signal_pass_state (signo
) == 1)
3307 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3308 here? It is not clear we should. GDB may not expect
3309 other threads to run. On the other hand, not resuming
3310 newly attached threads may cause an unwanted delay in
3311 getting them running. */
3312 registers_changed ();
3313 linux_ops
->to_resume (linux_ops
, pid_to_ptid (GET_LWP (lp
->ptid
)),
3315 if (debug_linux_nat
)
3316 fprintf_unfiltered (gdb_stdlog
,
3317 "LLW: %s %s, %s (preempt 'handle')\n",
3319 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3320 target_pid_to_str (lp
->ptid
),
3321 signo
? strsignal (signo
) : "0");
3328 /* Only do the below in all-stop, as we currently use SIGINT
3329 to implement target_stop (see linux_nat_stop) in
3331 if (signo
== TARGET_SIGNAL_INT
&& signal_pass_state (signo
) == 0)
3333 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3334 forwarded to the entire process group, that is, all LWPs
3335 will receive it - unless they're using CLONE_THREAD to
3336 share signals. Since we only want to report it once, we
3337 mark it as ignored for all LWPs except this one. */
3338 iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid
)),
3339 set_ignore_sigint
, NULL
);
3340 lp
->ignore_sigint
= 0;
3343 maybe_clear_ignore_sigint (lp
);
3347 /* This LWP is stopped now. */
3350 if (debug_linux_nat
)
3351 fprintf_unfiltered (gdb_stdlog
, "LLW: Candidate event %s in %s.\n",
3352 status_to_str (status
), target_pid_to_str (lp
->ptid
));
3356 /* Now stop all other LWP's ... */
3357 iterate_over_lwps (minus_one_ptid
, stop_callback
, NULL
);
3359 /* ... and wait until all of them have reported back that
3360 they're no longer running. */
3361 iterate_over_lwps (minus_one_ptid
, stop_wait_callback
, NULL
);
3363 /* If we're not waiting for a specific LWP, choose an event LWP
3364 from among those that have had events. Giving equal priority
3365 to all LWPs that have had events helps prevent
3368 select_event_lwp (ptid
, &lp
, &status
);
3371 /* Now that we've selected our final event LWP, cancel any
3372 breakpoints in other LWPs that have hit a GDB breakpoint. See
3373 the comment in cancel_breakpoints_callback to find out why. */
3374 iterate_over_lwps (minus_one_ptid
, cancel_breakpoints_callback
, lp
);
3376 if (WIFSTOPPED (status
) && WSTOPSIG (status
) == SIGTRAP
)
3378 if (debug_linux_nat
)
3379 fprintf_unfiltered (gdb_stdlog
,
3380 "LLW: trap ptid is %s.\n",
3381 target_pid_to_str (lp
->ptid
));
3384 if (lp
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3386 *ourstatus
= lp
->waitstatus
;
3387 lp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3390 store_waitstatus (ourstatus
, status
);
3392 if (debug_linux_nat_async
)
3393 fprintf_unfiltered (gdb_stdlog
, "LLW: exit\n");
3395 restore_child_signals_mask (&prev_mask
);
3400 linux_nat_wait (struct target_ops
*ops
,
3401 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
3406 if (debug_linux_nat
)
3407 fprintf_unfiltered (gdb_stdlog
, "linux_nat_wait: [%s]\n", target_pid_to_str (ptid
));
3409 /* Flush the async file first. */
3410 if (target_can_async_p ())
3411 async_file_flush ();
3413 event_ptid
= linux_nat_wait_1 (ops
, ptid
, ourstatus
, target_options
);
3415 /* If we requested any event, and something came out, assume there
3416 may be more. If we requested a specific lwp or process, also
3417 assume there may be more. */
3418 if (target_can_async_p ()
3419 && (ourstatus
->kind
!= TARGET_WAITKIND_IGNORE
3420 || !ptid_equal (ptid
, minus_one_ptid
)))
3423 /* Get ready for the next event. */
3424 if (target_can_async_p ())
3425 target_async (inferior_event_handler
, 0);
3431 kill_callback (struct lwp_info
*lp
, void *data
)
3434 ptrace (PTRACE_KILL
, GET_LWP (lp
->ptid
), 0, 0);
3435 if (debug_linux_nat
)
3436 fprintf_unfiltered (gdb_stdlog
,
3437 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3438 target_pid_to_str (lp
->ptid
),
3439 errno
? safe_strerror (errno
) : "OK");
3445 kill_wait_callback (struct lwp_info
*lp
, void *data
)
3449 /* We must make sure that there are no pending events (delayed
3450 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3451 program doesn't interfere with any following debugging session. */
3453 /* For cloned processes we must check both with __WCLONE and
3454 without, since the exit status of a cloned process isn't reported
3460 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, __WCLONE
);
3461 if (pid
!= (pid_t
) -1)
3463 if (debug_linux_nat
)
3464 fprintf_unfiltered (gdb_stdlog
,
3465 "KWC: wait %s received unknown.\n",
3466 target_pid_to_str (lp
->ptid
));
3467 /* The Linux kernel sometimes fails to kill a thread
3468 completely after PTRACE_KILL; that goes from the stop
3469 point in do_fork out to the one in
3470 get_signal_to_deliever and waits again. So kill it
3472 kill_callback (lp
, NULL
);
3475 while (pid
== GET_LWP (lp
->ptid
));
3477 gdb_assert (pid
== -1 && errno
== ECHILD
);
3482 pid
= my_waitpid (GET_LWP (lp
->ptid
), NULL
, 0);
3483 if (pid
!= (pid_t
) -1)
3485 if (debug_linux_nat
)
3486 fprintf_unfiltered (gdb_stdlog
,
3487 "KWC: wait %s received unk.\n",
3488 target_pid_to_str (lp
->ptid
));
3489 /* See the call to kill_callback above. */
3490 kill_callback (lp
, NULL
);
3493 while (pid
== GET_LWP (lp
->ptid
));
3495 gdb_assert (pid
== -1 && errno
== ECHILD
);
3500 linux_nat_kill (struct target_ops
*ops
)
3502 struct target_waitstatus last
;
3506 /* If we're stopped while forking and we haven't followed yet,
3507 kill the other task. We need to do this first because the
3508 parent will be sleeping if this is a vfork. */
3510 get_last_target_status (&last_ptid
, &last
);
3512 if (last
.kind
== TARGET_WAITKIND_FORKED
3513 || last
.kind
== TARGET_WAITKIND_VFORKED
)
3515 ptrace (PT_KILL
, PIDGET (last
.value
.related_pid
), 0, 0);
3519 if (forks_exist_p ())
3520 linux_fork_killall ();
3523 ptid_t ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
3524 /* Stop all threads before killing them, since ptrace requires
3525 that the thread is stopped to sucessfully PTRACE_KILL. */
3526 iterate_over_lwps (ptid
, stop_callback
, NULL
);
3527 /* ... and wait until all of them have reported back that
3528 they're no longer running. */
3529 iterate_over_lwps (ptid
, stop_wait_callback
, NULL
);
3531 /* Kill all LWP's ... */
3532 iterate_over_lwps (ptid
, kill_callback
, NULL
);
3534 /* ... and wait until we've flushed all events. */
3535 iterate_over_lwps (ptid
, kill_wait_callback
, NULL
);
3538 target_mourn_inferior ();
3542 linux_nat_mourn_inferior (struct target_ops
*ops
)
3544 purge_lwp_list (ptid_get_pid (inferior_ptid
));
3546 if (! forks_exist_p ())
3547 /* Normal case, no other forks available. */
3548 linux_ops
->to_mourn_inferior (ops
);
3550 /* Multi-fork case. The current inferior_ptid has exited, but
3551 there are other viable forks to debug. Delete the exiting
3552 one and context-switch to the first available. */
3553 linux_fork_mourn_inferior ();
3556 /* Convert a native/host siginfo object, into/from the siginfo in the
3557 layout of the inferiors' architecture. */
3560 siginfo_fixup (struct siginfo
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
3564 if (linux_nat_siginfo_fixup
!= NULL
)
3565 done
= linux_nat_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
3567 /* If there was no callback, or the callback didn't do anything,
3568 then just do a straight memcpy. */
3572 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
3574 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
3579 linux_xfer_siginfo (struct target_ops
*ops
, enum target_object object
,
3580 const char *annex
, gdb_byte
*readbuf
,
3581 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3584 struct siginfo siginfo
;
3585 gdb_byte inf_siginfo
[sizeof (struct siginfo
)];
3587 gdb_assert (object
== TARGET_OBJECT_SIGNAL_INFO
);
3588 gdb_assert (readbuf
|| writebuf
);
3590 pid
= GET_LWP (inferior_ptid
);
3592 pid
= GET_PID (inferior_ptid
);
3594 if (offset
> sizeof (siginfo
))
3598 ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3602 /* When GDB is built as a 64-bit application, ptrace writes into
3603 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3604 inferior with a 64-bit GDB should look the same as debugging it
3605 with a 32-bit GDB, we need to convert it. GDB core always sees
3606 the converted layout, so any read/write will have to be done
3608 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
3610 if (offset
+ len
> sizeof (siginfo
))
3611 len
= sizeof (siginfo
) - offset
;
3613 if (readbuf
!= NULL
)
3614 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
3617 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
3619 /* Convert back to ptrace layout before flushing it out. */
3620 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
3623 ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
);
3632 linux_nat_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3633 const char *annex
, gdb_byte
*readbuf
,
3634 const gdb_byte
*writebuf
,
3635 ULONGEST offset
, LONGEST len
)
3637 struct cleanup
*old_chain
;
3640 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
3641 return linux_xfer_siginfo (ops
, object
, annex
, readbuf
, writebuf
,
3644 /* The target is connected but no live inferior is selected. Pass
3645 this request down to a lower stratum (e.g., the executable
3647 if (object
== TARGET_OBJECT_MEMORY
&& ptid_equal (inferior_ptid
, null_ptid
))
3650 old_chain
= save_inferior_ptid ();
3652 if (is_lwp (inferior_ptid
))
3653 inferior_ptid
= pid_to_ptid (GET_LWP (inferior_ptid
));
3655 xfer
= linux_ops
->to_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
3658 do_cleanups (old_chain
);
3663 linux_thread_alive (ptid_t ptid
)
3667 gdb_assert (is_lwp (ptid
));
3669 /* Send signal 0 instead of anything ptrace, because ptracing a
3670 running thread errors out claiming that the thread doesn't
3672 err
= kill_lwp (GET_LWP (ptid
), 0);
3674 if (debug_linux_nat
)
3675 fprintf_unfiltered (gdb_stdlog
,
3676 "LLTA: KILL(SIG0) %s (%s)\n",
3677 target_pid_to_str (ptid
),
3678 err
? safe_strerror (err
) : "OK");
3687 linux_nat_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3689 return linux_thread_alive (ptid
);
3693 linux_nat_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3695 static char buf
[64];
3698 && (GET_PID (ptid
) != GET_LWP (ptid
)
3699 || num_lwps (GET_PID (ptid
)) > 1))
3701 snprintf (buf
, sizeof (buf
), "LWP %ld", GET_LWP (ptid
));
3705 return normal_pid_to_str (ptid
);
3708 /* Accepts an integer PID; Returns a string representing a file that
3709 can be opened to get the symbols for the child process. */
3712 linux_child_pid_to_exec_file (int pid
)
3714 char *name1
, *name2
;
3716 name1
= xmalloc (MAXPATHLEN
);
3717 name2
= xmalloc (MAXPATHLEN
);
3718 make_cleanup (xfree
, name1
);
3719 make_cleanup (xfree
, name2
);
3720 memset (name2
, 0, MAXPATHLEN
);
3722 sprintf (name1
, "/proc/%d/exe", pid
);
3723 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
3729 /* Service function for corefiles and info proc. */
3732 read_mapping (FILE *mapfile
,
3737 char *device
, long long *inode
, char *filename
)
3739 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
3740 addr
, endaddr
, permissions
, offset
, device
, inode
);
3743 if (ret
> 0 && ret
!= EOF
)
3745 /* Eat everything up to EOL for the filename. This will prevent
3746 weird filenames (such as one with embedded whitespace) from
3747 confusing this code. It also makes this code more robust in
3748 respect to annotations the kernel may add after the filename.
3750 Note the filename is used for informational purposes
3752 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
3755 return (ret
!= 0 && ret
!= EOF
);
3758 /* Fills the "to_find_memory_regions" target vector. Lists the memory
3759 regions in the inferior for a corefile. */
3762 linux_nat_find_memory_regions (int (*func
) (CORE_ADDR
,
3764 int, int, int, void *), void *obfd
)
3766 int pid
= PIDGET (inferior_ptid
);
3767 char mapsfilename
[MAXPATHLEN
];
3769 long long addr
, endaddr
, size
, offset
, inode
;
3770 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
3771 int read
, write
, exec
;
3773 struct cleanup
*cleanup
;
3775 /* Compose the filename for the /proc memory map, and open it. */
3776 sprintf (mapsfilename
, "/proc/%d/maps", pid
);
3777 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
3778 error (_("Could not open %s."), mapsfilename
);
3779 cleanup
= make_cleanup_fclose (mapsfile
);
3782 fprintf_filtered (gdb_stdout
,
3783 "Reading memory regions from %s\n", mapsfilename
);
3785 /* Now iterate until end-of-file. */
3786 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
3787 &offset
, &device
[0], &inode
, &filename
[0]))
3789 size
= endaddr
- addr
;
3791 /* Get the segment's permissions. */
3792 read
= (strchr (permissions
, 'r') != 0);
3793 write
= (strchr (permissions
, 'w') != 0);
3794 exec
= (strchr (permissions
, 'x') != 0);
3798 fprintf_filtered (gdb_stdout
,
3799 "Save segment, %lld bytes at %s (%c%c%c)",
3800 size
, paddress (target_gdbarch
, addr
),
3802 write
? 'w' : ' ', exec
? 'x' : ' ');
3804 fprintf_filtered (gdb_stdout
, " for %s", filename
);
3805 fprintf_filtered (gdb_stdout
, "\n");
3808 /* Invoke the callback function to create the corefile
3810 func (addr
, size
, read
, write
, exec
, obfd
);
3812 do_cleanups (cleanup
);
3817 find_signalled_thread (struct thread_info
*info
, void *data
)
3819 if (info
->stop_signal
!= TARGET_SIGNAL_0
3820 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3826 static enum target_signal
3827 find_stop_signal (void)
3829 struct thread_info
*info
=
3830 iterate_over_threads (find_signalled_thread
, NULL
);
3833 return info
->stop_signal
;
3835 return TARGET_SIGNAL_0
;
3838 /* Records the thread's register state for the corefile note
3842 linux_nat_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3843 char *note_data
, int *note_size
,
3844 enum target_signal stop_signal
)
3846 gdb_gregset_t gregs
;
3847 gdb_fpregset_t fpregs
;
3848 unsigned long lwp
= ptid_get_lwp (ptid
);
3849 struct gdbarch
*gdbarch
= target_gdbarch
;
3850 struct regcache
*regcache
= get_thread_arch_regcache (ptid
, gdbarch
);
3851 const struct regset
*regset
;
3853 struct cleanup
*old_chain
;
3854 struct core_regset_section
*sect_list
;
3857 old_chain
= save_inferior_ptid ();
3858 inferior_ptid
= ptid
;
3859 target_fetch_registers (regcache
, -1);
3860 do_cleanups (old_chain
);
3862 core_regset_p
= gdbarch_regset_from_core_section_p (gdbarch
);
3863 sect_list
= gdbarch_core_regset_sections (gdbarch
);
3866 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg",
3867 sizeof (gregs
))) != NULL
3868 && regset
->collect_regset
!= NULL
)
3869 regset
->collect_regset (regset
, regcache
, -1,
3870 &gregs
, sizeof (gregs
));
3872 fill_gregset (regcache
, &gregs
, -1);
3874 note_data
= (char *) elfcore_write_prstatus (obfd
,
3878 stop_signal
, &gregs
);
3880 /* The loop below uses the new struct core_regset_section, which stores
3881 the supported section names and sizes for the core file. Note that
3882 note PRSTATUS needs to be treated specially. But the other notes are
3883 structurally the same, so they can benefit from the new struct. */
3884 if (core_regset_p
&& sect_list
!= NULL
)
3885 while (sect_list
->sect_name
!= NULL
)
3887 /* .reg was already handled above. */
3888 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
3893 regset
= gdbarch_regset_from_core_section (gdbarch
,
3894 sect_list
->sect_name
,
3896 gdb_assert (regset
&& regset
->collect_regset
);
3897 gdb_regset
= xmalloc (sect_list
->size
);
3898 regset
->collect_regset (regset
, regcache
, -1,
3899 gdb_regset
, sect_list
->size
);
3900 note_data
= (char *) elfcore_write_register_note (obfd
,
3903 sect_list
->sect_name
,
3910 /* For architectures that does not have the struct core_regset_section
3911 implemented, we use the old method. When all the architectures have
3912 the new support, the code below should be deleted. */
3916 && (regset
= gdbarch_regset_from_core_section (gdbarch
, ".reg2",
3917 sizeof (fpregs
))) != NULL
3918 && regset
->collect_regset
!= NULL
)
3919 regset
->collect_regset (regset
, regcache
, -1,
3920 &fpregs
, sizeof (fpregs
));
3922 fill_fpregset (regcache
, &fpregs
, -1);
3924 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3927 &fpregs
, sizeof (fpregs
));
3933 struct linux_nat_corefile_thread_data
3939 enum target_signal stop_signal
;
3942 /* Called by gdbthread.c once per thread. Records the thread's
3943 register state for the corefile note section. */
3946 linux_nat_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
3948 struct linux_nat_corefile_thread_data
*args
= data
;
3950 args
->note_data
= linux_nat_do_thread_registers (args
->obfd
,
3960 /* Enumerate spufs IDs for process PID. */
3963 iterate_over_spus (int pid
, void (*callback
) (void *, int), void *data
)
3967 struct dirent
*entry
;
3969 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
3970 dir
= opendir (path
);
3975 while ((entry
= readdir (dir
)) != NULL
)
3981 fd
= atoi (entry
->d_name
);
3985 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
3986 if (stat (path
, &st
) != 0)
3988 if (!S_ISDIR (st
.st_mode
))
3991 if (statfs (path
, &stfs
) != 0)
3993 if (stfs
.f_type
!= SPUFS_MAGIC
)
3996 callback (data
, fd
);
4002 /* Generate corefile notes for SPU contexts. */
4004 struct linux_spu_corefile_data
4012 linux_spu_corefile_callback (void *data
, int fd
)
4014 struct linux_spu_corefile_data
*args
= data
;
4017 static const char *spu_files
[] =
4039 for (i
= 0; i
< sizeof (spu_files
) / sizeof (spu_files
[0]); i
++)
4041 char annex
[32], note_name
[32];
4045 xsnprintf (annex
, sizeof annex
, "%d/%s", fd
, spu_files
[i
]);
4046 spu_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_SPU
,
4050 xsnprintf (note_name
, sizeof note_name
, "SPU/%s", annex
);
4051 args
->note_data
= elfcore_write_note (args
->obfd
, args
->note_data
,
4052 args
->note_size
, note_name
,
4053 NT_SPU
, spu_data
, spu_len
);
4060 linux_spu_make_corefile_notes (bfd
*obfd
, char *note_data
, int *note_size
)
4062 struct linux_spu_corefile_data args
;
4064 args
.note_data
= note_data
;
4065 args
.note_size
= note_size
;
4067 iterate_over_spus (PIDGET (inferior_ptid
),
4068 linux_spu_corefile_callback
, &args
);
4070 return args
.note_data
;
4073 /* Fills the "to_make_corefile_note" target vector. Builds the note
4074 section for a corefile, and returns it in a malloc buffer. */
4077 linux_nat_make_corefile_notes (bfd
*obfd
, int *note_size
)
4079 struct linux_nat_corefile_thread_data thread_args
;
4080 struct cleanup
*old_chain
;
4081 /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */
4082 char fname
[16] = { '\0' };
4083 /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */
4084 char psargs
[80] = { '\0' };
4085 char *note_data
= NULL
;
4086 ptid_t current_ptid
= inferior_ptid
;
4087 ptid_t filter
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
4091 if (get_exec_file (0))
4093 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
4094 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
4095 if (get_inferior_args ())
4098 char *psargs_end
= psargs
+ sizeof (psargs
);
4100 /* linux_elfcore_write_prpsinfo () handles zero unterminated
4102 string_end
= memchr (psargs
, 0, sizeof (psargs
));
4103 if (string_end
!= NULL
)
4105 *string_end
++ = ' ';
4106 strncpy (string_end
, get_inferior_args (),
4107 psargs_end
- string_end
);
4110 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
4112 note_size
, fname
, psargs
);
4115 /* Dump information for threads. */
4116 thread_args
.obfd
= obfd
;
4117 thread_args
.note_data
= note_data
;
4118 thread_args
.note_size
= note_size
;
4119 thread_args
.num_notes
= 0;
4120 thread_args
.stop_signal
= find_stop_signal ();
4121 iterate_over_lwps (filter
, linux_nat_corefile_thread_callback
, &thread_args
);
4122 gdb_assert (thread_args
.num_notes
!= 0);
4123 note_data
= thread_args
.note_data
;
4125 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
4129 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
4130 "CORE", NT_AUXV
, auxv
, auxv_len
);
4134 note_data
= linux_spu_make_corefile_notes (obfd
, note_data
, note_size
);
4136 make_cleanup (xfree
, note_data
);
4140 /* Implement the "info proc" command. */
4143 linux_nat_info_proc_cmd (char *args
, int from_tty
)
4145 /* A long is used for pid instead of an int to avoid a loss of precision
4146 compiler warning from the output of strtoul. */
4147 long pid
= PIDGET (inferior_ptid
);
4150 char buffer
[MAXPATHLEN
];
4151 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
4164 /* Break up 'args' into an argv array. */
4165 argv
= gdb_buildargv (args
);
4166 make_cleanup_freeargv (argv
);
4168 while (argv
!= NULL
&& *argv
!= NULL
)
4170 if (isdigit (argv
[0][0]))
4172 pid
= strtoul (argv
[0], NULL
, 10);
4174 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
4178 else if (strcmp (argv
[0], "status") == 0)
4182 else if (strcmp (argv
[0], "stat") == 0)
4186 else if (strcmp (argv
[0], "cmd") == 0)
4190 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
4194 else if (strcmp (argv
[0], "cwd") == 0)
4198 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
4204 /* [...] (future options here) */
4209 error (_("No current process: you must name one."));
4211 sprintf (fname1
, "/proc/%ld", pid
);
4212 if (stat (fname1
, &dummy
) != 0)
4213 error (_("No /proc directory: '%s'"), fname1
);
4215 printf_filtered (_("process %ld\n"), pid
);
4216 if (cmdline_f
|| all
)
4218 sprintf (fname1
, "/proc/%ld/cmdline", pid
);
4219 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4221 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
4222 if (fgets (buffer
, sizeof (buffer
), procfile
))
4223 printf_filtered ("cmdline = '%s'\n", buffer
);
4225 warning (_("unable to read '%s'"), fname1
);
4226 do_cleanups (cleanup
);
4229 warning (_("unable to open /proc file '%s'"), fname1
);
4233 sprintf (fname1
, "/proc/%ld/cwd", pid
);
4234 memset (fname2
, 0, sizeof (fname2
));
4235 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
4236 printf_filtered ("cwd = '%s'\n", fname2
);
4238 warning (_("unable to read link '%s'"), fname1
);
4242 sprintf (fname1
, "/proc/%ld/exe", pid
);
4243 memset (fname2
, 0, sizeof (fname2
));
4244 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
4245 printf_filtered ("exe = '%s'\n", fname2
);
4247 warning (_("unable to read link '%s'"), fname1
);
4249 if (mappings_f
|| all
)
4251 sprintf (fname1
, "/proc/%ld/maps", pid
);
4252 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4254 long long addr
, endaddr
, size
, offset
, inode
;
4255 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
4256 struct cleanup
*cleanup
;
4258 cleanup
= make_cleanup_fclose (procfile
);
4259 printf_filtered (_("Mapped address spaces:\n\n"));
4260 if (gdbarch_addr_bit (target_gdbarch
) == 32)
4262 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
4265 " Size", " Offset", "objfile");
4269 printf_filtered (" %18s %18s %10s %10s %7s\n",
4272 " Size", " Offset", "objfile");
4275 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
4276 &offset
, &device
[0], &inode
, &filename
[0]))
4278 size
= endaddr
- addr
;
4280 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
4281 calls here (and possibly above) should be abstracted
4282 out into their own functions? Andrew suggests using
4283 a generic local_address_string instead to print out
4284 the addresses; that makes sense to me, too. */
4286 if (gdbarch_addr_bit (target_gdbarch
) == 32)
4288 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
4289 (unsigned long) addr
, /* FIXME: pr_addr */
4290 (unsigned long) endaddr
,
4292 (unsigned int) offset
,
4293 filename
[0] ? filename
: "");
4297 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
4298 (unsigned long) addr
, /* FIXME: pr_addr */
4299 (unsigned long) endaddr
,
4301 (unsigned int) offset
,
4302 filename
[0] ? filename
: "");
4306 do_cleanups (cleanup
);
4309 warning (_("unable to open /proc file '%s'"), fname1
);
4311 if (status_f
|| all
)
4313 sprintf (fname1
, "/proc/%ld/status", pid
);
4314 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4316 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
4317 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
4318 puts_filtered (buffer
);
4319 do_cleanups (cleanup
);
4322 warning (_("unable to open /proc file '%s'"), fname1
);
4326 sprintf (fname1
, "/proc/%ld/stat", pid
);
4327 if ((procfile
= fopen (fname1
, "r")) != NULL
)
4332 struct cleanup
*cleanup
= make_cleanup_fclose (procfile
);
4334 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4335 printf_filtered (_("Process: %d\n"), itmp
);
4336 if (fscanf (procfile
, "(%[^)]) ", &buffer
[0]) > 0)
4337 printf_filtered (_("Exec file: %s\n"), buffer
);
4338 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
4339 printf_filtered (_("State: %c\n"), ctmp
);
4340 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4341 printf_filtered (_("Parent process: %d\n"), itmp
);
4342 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4343 printf_filtered (_("Process group: %d\n"), itmp
);
4344 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4345 printf_filtered (_("Session id: %d\n"), itmp
);
4346 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4347 printf_filtered (_("TTY: %d\n"), itmp
);
4348 if (fscanf (procfile
, "%d ", &itmp
) > 0)
4349 printf_filtered (_("TTY owner process group: %d\n"), itmp
);
4350 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4351 printf_filtered (_("Flags: 0x%lx\n"), ltmp
);
4352 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4353 printf_filtered (_("Minor faults (no memory page): %lu\n"),
4354 (unsigned long) ltmp
);
4355 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4356 printf_filtered (_("Minor faults, children: %lu\n"),
4357 (unsigned long) ltmp
);
4358 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4359 printf_filtered (_("Major faults (memory page faults): %lu\n"),
4360 (unsigned long) ltmp
);
4361 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4362 printf_filtered (_("Major faults, children: %lu\n"),
4363 (unsigned long) ltmp
);
4364 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4365 printf_filtered (_("utime: %ld\n"), ltmp
);
4366 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4367 printf_filtered (_("stime: %ld\n"), ltmp
);
4368 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4369 printf_filtered (_("utime, children: %ld\n"), ltmp
);
4370 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4371 printf_filtered (_("stime, children: %ld\n"), ltmp
);
4372 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4373 printf_filtered (_("jiffies remaining in current time slice: %ld\n"),
4375 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4376 printf_filtered (_("'nice' value: %ld\n"), ltmp
);
4377 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4378 printf_filtered (_("jiffies until next timeout: %lu\n"),
4379 (unsigned long) ltmp
);
4380 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4381 printf_filtered (_("jiffies until next SIGALRM: %lu\n"),
4382 (unsigned long) ltmp
);
4383 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4384 printf_filtered (_("start time (jiffies since system boot): %ld\n"),
4386 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4387 printf_filtered (_("Virtual memory size: %lu\n"),
4388 (unsigned long) ltmp
);
4389 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4390 printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp
);
4391 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4392 printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp
);
4393 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4394 printf_filtered (_("Start of text: 0x%lx\n"), ltmp
);
4395 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4396 printf_filtered (_("End of text: 0x%lx\n"), ltmp
);
4397 if (fscanf (procfile
, "%lu ", <mp
) > 0)
4398 printf_filtered (_("Start of stack: 0x%lx\n"), ltmp
);
4399 #if 0 /* Don't know how architecture-dependent the rest is...
4400 Anyway the signal bitmap info is available from "status". */
4401 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4402 printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp
);
4403 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4404 printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp
);
4405 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4406 printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp
);
4407 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4408 printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp
);
4409 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4410 printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp
);
4411 if (fscanf (procfile
, "%ld ", <mp
) > 0)
4412 printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp
);
4413 if (fscanf (procfile
, "%lu ", <mp
) > 0) /* FIXME arch? */
4414 printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp
);
4416 do_cleanups (cleanup
);
4419 warning (_("unable to open /proc file '%s'"), fname1
);
4423 /* Implement the to_xfer_partial interface for memory reads using the /proc
4424 filesystem. Because we can use a single read() call for /proc, this
4425 can be much more efficient than banging away at PTRACE_PEEKTEXT,
4426 but it doesn't support writes. */
4429 linux_proc_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4430 const char *annex
, gdb_byte
*readbuf
,
4431 const gdb_byte
*writebuf
,
4432 ULONGEST offset
, LONGEST len
)
4438 if (object
!= TARGET_OBJECT_MEMORY
|| !readbuf
)
4441 /* Don't bother for one word. */
4442 if (len
< 3 * sizeof (long))
4445 /* We could keep this file open and cache it - possibly one per
4446 thread. That requires some juggling, but is even faster. */
4447 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
4448 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4452 /* If pread64 is available, use it. It's faster if the kernel
4453 supports it (only one syscall), and it's 64-bit safe even on
4454 32-bit platforms (for instance, SPARC debugging a SPARC64
4457 if (pread64 (fd
, readbuf
, len
, offset
) != len
)
4459 if (lseek (fd
, offset
, SEEK_SET
) == -1 || read (fd
, readbuf
, len
) != len
)
4470 /* Enumerate spufs IDs for process PID. */
4472 spu_enumerate_spu_ids (int pid
, gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
4474 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
4476 LONGEST written
= 0;
4479 struct dirent
*entry
;
4481 xsnprintf (path
, sizeof path
, "/proc/%d/fd", pid
);
4482 dir
= opendir (path
);
4487 while ((entry
= readdir (dir
)) != NULL
)
4493 fd
= atoi (entry
->d_name
);
4497 xsnprintf (path
, sizeof path
, "/proc/%d/fd/%d", pid
, fd
);
4498 if (stat (path
, &st
) != 0)
4500 if (!S_ISDIR (st
.st_mode
))
4503 if (statfs (path
, &stfs
) != 0)
4505 if (stfs
.f_type
!= SPUFS_MAGIC
)
4508 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4510 store_unsigned_integer (buf
+ pos
- offset
, 4, byte_order
, fd
);
4520 /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4521 object type, using the /proc file system. */
4523 linux_proc_xfer_spu (struct target_ops
*ops
, enum target_object object
,
4524 const char *annex
, gdb_byte
*readbuf
,
4525 const gdb_byte
*writebuf
,
4526 ULONGEST offset
, LONGEST len
)
4531 int pid
= PIDGET (inferior_ptid
);
4538 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4541 xsnprintf (buf
, sizeof buf
, "/proc/%d/fd/%s", pid
, annex
);
4542 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4547 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4554 ret
= write (fd
, writebuf
, (size_t) len
);
4556 ret
= read (fd
, readbuf
, (size_t) len
);
4563 /* Parse LINE as a signal set and add its set bits to SIGS. */
4566 add_line_to_sigset (const char *line
, sigset_t
*sigs
)
4568 int len
= strlen (line
) - 1;
4572 if (line
[len
] != '\n')
4573 error (_("Could not parse signal set: %s"), line
);
4581 if (*p
>= '0' && *p
<= '9')
4583 else if (*p
>= 'a' && *p
<= 'f')
4584 digit
= *p
- 'a' + 10;
4586 error (_("Could not parse signal set: %s"), line
);
4591 sigaddset (sigs
, signum
+ 1);
4593 sigaddset (sigs
, signum
+ 2);
4595 sigaddset (sigs
, signum
+ 3);
4597 sigaddset (sigs
, signum
+ 4);
4603 /* Find process PID's pending signals from /proc/pid/status and set
4607 linux_proc_pending_signals (int pid
, sigset_t
*pending
, sigset_t
*blocked
, sigset_t
*ignored
)
4610 char buffer
[MAXPATHLEN
], fname
[MAXPATHLEN
];
4612 struct cleanup
*cleanup
;
4614 sigemptyset (pending
);
4615 sigemptyset (blocked
);
4616 sigemptyset (ignored
);
4617 sprintf (fname
, "/proc/%d/status", pid
);
4618 procfile
= fopen (fname
, "r");
4619 if (procfile
== NULL
)
4620 error (_("Could not open %s"), fname
);
4621 cleanup
= make_cleanup_fclose (procfile
);
4623 while (fgets (buffer
, MAXPATHLEN
, procfile
) != NULL
)
4625 /* Normal queued signals are on the SigPnd line in the status
4626 file. However, 2.6 kernels also have a "shared" pending
4627 queue for delivering signals to a thread group, so check for
4630 Unfortunately some Red Hat kernels include the shared pending
4631 queue but not the ShdPnd status field. */
4633 if (strncmp (buffer
, "SigPnd:\t", 8) == 0)
4634 add_line_to_sigset (buffer
+ 8, pending
);
4635 else if (strncmp (buffer
, "ShdPnd:\t", 8) == 0)
4636 add_line_to_sigset (buffer
+ 8, pending
);
4637 else if (strncmp (buffer
, "SigBlk:\t", 8) == 0)
4638 add_line_to_sigset (buffer
+ 8, blocked
);
4639 else if (strncmp (buffer
, "SigIgn:\t", 8) == 0)
4640 add_line_to_sigset (buffer
+ 8, ignored
);
4643 do_cleanups (cleanup
);
4647 linux_nat_xfer_osdata (struct target_ops
*ops
, enum target_object object
,
4648 const char *annex
, gdb_byte
*readbuf
,
4649 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4651 /* We make the process list snapshot when the object starts to be
4653 static const char *buf
;
4654 static LONGEST len_avail
= -1;
4655 static struct obstack obstack
;
4659 gdb_assert (object
== TARGET_OBJECT_OSDATA
);
4661 if (strcmp (annex
, "processes") != 0)
4664 gdb_assert (readbuf
&& !writebuf
);
4668 if (len_avail
!= -1 && len_avail
!= 0)
4669 obstack_free (&obstack
, NULL
);
4672 obstack_init (&obstack
);
4673 obstack_grow_str (&obstack
, "<osdata type=\"processes\">\n");
4675 dirp
= opendir ("/proc");
4679 while ((dp
= readdir (dirp
)) != NULL
)
4681 struct stat statbuf
;
4682 char procentry
[sizeof ("/proc/4294967295")];
4684 if (!isdigit (dp
->d_name
[0])
4685 || NAMELEN (dp
) > sizeof ("4294967295") - 1)
4688 sprintf (procentry
, "/proc/%s", dp
->d_name
);
4689 if (stat (procentry
, &statbuf
) == 0
4690 && S_ISDIR (statbuf
.st_mode
))
4694 char cmd
[MAXPATHLEN
+ 1];
4695 struct passwd
*entry
;
4697 pathname
= xstrprintf ("/proc/%s/cmdline", dp
->d_name
);
4698 entry
= getpwuid (statbuf
.st_uid
);
4700 if ((f
= fopen (pathname
, "r")) != NULL
)
4702 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
4706 for (i
= 0; i
< len
; i
++)
4711 obstack_xml_printf (
4714 "<column name=\"pid\">%s</column>"
4715 "<column name=\"user\">%s</column>"
4716 "<column name=\"command\">%s</column>"
4719 entry
? entry
->pw_name
: "?",
4732 obstack_grow_str0 (&obstack
, "</osdata>\n");
4733 buf
= obstack_finish (&obstack
);
4734 len_avail
= strlen (buf
);
4737 if (offset
>= len_avail
)
4739 /* Done. Get rid of the obstack. */
4740 obstack_free (&obstack
, NULL
);
4746 if (len
> len_avail
- offset
)
4747 len
= len_avail
- offset
;
4748 memcpy (readbuf
, buf
+ offset
, len
);
4754 linux_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4755 const char *annex
, gdb_byte
*readbuf
,
4756 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4760 if (object
== TARGET_OBJECT_AUXV
)
4761 return procfs_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4764 if (object
== TARGET_OBJECT_OSDATA
)
4765 return linux_nat_xfer_osdata (ops
, object
, annex
, readbuf
, writebuf
,
4768 if (object
== TARGET_OBJECT_SPU
)
4769 return linux_proc_xfer_spu (ops
, object
, annex
, readbuf
, writebuf
,
4772 /* GDB calculates all the addresses in possibly larget width of the address.
4773 Address width needs to be masked before its final use - either by
4774 linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4776 Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4778 if (object
== TARGET_OBJECT_MEMORY
)
4780 int addr_bit
= gdbarch_addr_bit (target_gdbarch
);
4782 if (addr_bit
< (sizeof (ULONGEST
) * HOST_CHAR_BIT
))
4783 offset
&= ((ULONGEST
) 1 << addr_bit
) - 1;
4786 xfer
= linux_proc_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4791 return super_xfer_partial (ops
, object
, annex
, readbuf
, writebuf
,
4795 /* Create a prototype generic GNU/Linux target. The client can override
4796 it with local methods. */
4799 linux_target_install_ops (struct target_ops
*t
)
4801 t
->to_insert_fork_catchpoint
= linux_child_insert_fork_catchpoint
;
4802 t
->to_insert_vfork_catchpoint
= linux_child_insert_vfork_catchpoint
;
4803 t
->to_insert_exec_catchpoint
= linux_child_insert_exec_catchpoint
;
4804 t
->to_set_syscall_catchpoint
= linux_child_set_syscall_catchpoint
;
4805 t
->to_pid_to_exec_file
= linux_child_pid_to_exec_file
;
4806 t
->to_post_startup_inferior
= linux_child_post_startup_inferior
;
4807 t
->to_post_attach
= linux_child_post_attach
;
4808 t
->to_follow_fork
= linux_child_follow_fork
;
4809 t
->to_find_memory_regions
= linux_nat_find_memory_regions
;
4810 t
->to_make_corefile_notes
= linux_nat_make_corefile_notes
;
4812 super_xfer_partial
= t
->to_xfer_partial
;
4813 t
->to_xfer_partial
= linux_xfer_partial
;
4819 struct target_ops
*t
;
4821 t
= inf_ptrace_target ();
4822 linux_target_install_ops (t
);
4828 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
4830 struct target_ops
*t
;
4832 t
= inf_ptrace_trad_target (register_u_offset
);
4833 linux_target_install_ops (t
);
4838 /* target_is_async_p implementation. */
4841 linux_nat_is_async_p (void)
4843 /* NOTE: palves 2008-03-21: We're only async when the user requests
4844 it explicitly with the "set target-async" command.
4845 Someday, linux will always be async. */
4846 if (!target_async_permitted
)
4849 /* See target.h/target_async_mask. */
4850 return linux_nat_async_mask_value
;
4853 /* target_can_async_p implementation. */
4856 linux_nat_can_async_p (void)
4858 /* NOTE: palves 2008-03-21: We're only async when the user requests
4859 it explicitly with the "set target-async" command.
4860 Someday, linux will always be async. */
4861 if (!target_async_permitted
)
4864 /* See target.h/target_async_mask. */
4865 return linux_nat_async_mask_value
;
4869 linux_nat_supports_non_stop (void)
4874 /* True if we want to support multi-process. To be removed when GDB
4875 supports multi-exec. */
4877 int linux_multi_process
= 1;
4880 linux_nat_supports_multi_process (void)
4882 return linux_multi_process
;
4885 /* target_async_mask implementation. */
4888 linux_nat_async_mask (int new_mask
)
4890 int curr_mask
= linux_nat_async_mask_value
;
4892 if (curr_mask
!= new_mask
)
4896 linux_nat_async (NULL
, 0);
4897 linux_nat_async_mask_value
= new_mask
;
4901 linux_nat_async_mask_value
= new_mask
;
4903 /* If we're going out of async-mask in all-stop, then the
4904 inferior is stopped. The next resume will call
4905 target_async. In non-stop, the target event source
4906 should be always registered in the event loop. Do so
4909 linux_nat_async (inferior_event_handler
, 0);
4916 static int async_terminal_is_ours
= 1;
4918 /* target_terminal_inferior implementation. */
4921 linux_nat_terminal_inferior (void)
4923 if (!target_is_async_p ())
4925 /* Async mode is disabled. */
4926 terminal_inferior ();
4930 terminal_inferior ();
4932 /* Calls to target_terminal_*() are meant to be idempotent. */
4933 if (!async_terminal_is_ours
)
4936 delete_file_handler (input_fd
);
4937 async_terminal_is_ours
= 0;
4941 /* target_terminal_ours implementation. */
4944 linux_nat_terminal_ours (void)
4946 if (!target_is_async_p ())
4948 /* Async mode is disabled. */
4953 /* GDB should never give the terminal to the inferior if the
4954 inferior is running in the background (run&, continue&, etc.),
4955 but claiming it sure should. */
4958 if (async_terminal_is_ours
)
4961 clear_sigint_trap ();
4962 add_file_handler (input_fd
, stdin_event_handler
, 0);
4963 async_terminal_is_ours
= 1;
4966 static void (*async_client_callback
) (enum inferior_event_type event_type
,
4968 static void *async_client_context
;
4970 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4971 so we notice when any child changes state, and notify the
4972 event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4973 above to wait for the arrival of a SIGCHLD. */
4976 sigchld_handler (int signo
)
4978 int old_errno
= errno
;
4980 if (debug_linux_nat_async
)
4981 fprintf_unfiltered (gdb_stdlog
, "sigchld\n");
4983 if (signo
== SIGCHLD
4984 && linux_nat_event_pipe
[0] != -1)
4985 async_file_mark (); /* Let the event loop know that there are
4986 events to handle. */
4991 /* Callback registered with the target events file descriptor. */
4994 handle_target_event (int error
, gdb_client_data client_data
)
4996 (*async_client_callback
) (INF_REG_EVENT
, async_client_context
);
4999 /* Create/destroy the target events pipe. Returns previous state. */
5002 linux_async_pipe (int enable
)
5004 int previous
= (linux_nat_event_pipe
[0] != -1);
5006 if (previous
!= enable
)
5010 block_child_signals (&prev_mask
);
5014 if (pipe (linux_nat_event_pipe
) == -1)
5015 internal_error (__FILE__
, __LINE__
,
5016 "creating event pipe failed.");
5018 fcntl (linux_nat_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
5019 fcntl (linux_nat_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
5023 close (linux_nat_event_pipe
[0]);
5024 close (linux_nat_event_pipe
[1]);
5025 linux_nat_event_pipe
[0] = -1;
5026 linux_nat_event_pipe
[1] = -1;
5029 restore_child_signals_mask (&prev_mask
);
5035 /* target_async implementation. */
5038 linux_nat_async (void (*callback
) (enum inferior_event_type event_type
,
5039 void *context
), void *context
)
5041 if (linux_nat_async_mask_value
== 0 || !target_async_permitted
)
5042 internal_error (__FILE__
, __LINE__
,
5043 "Calling target_async when async is masked");
5045 if (callback
!= NULL
)
5047 async_client_callback
= callback
;
5048 async_client_context
= context
;
5049 if (!linux_async_pipe (1))
5051 add_file_handler (linux_nat_event_pipe
[0],
5052 handle_target_event
, NULL
);
5053 /* There may be pending events to handle. Tell the event loop
5060 async_client_callback
= callback
;
5061 async_client_context
= context
;
5062 delete_file_handler (linux_nat_event_pipe
[0]);
5063 linux_async_pipe (0);
5068 /* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
5072 linux_nat_stop_lwp (struct lwp_info
*lwp
, void *data
)
5077 ptid_t ptid
= lwp
->ptid
;
5079 if (debug_linux_nat
)
5080 fprintf_unfiltered (gdb_stdlog
,
5081 "LNSL: running -> suspending %s\n",
5082 target_pid_to_str (lwp
->ptid
));
5085 stop_callback (lwp
, NULL
);
5086 stop_wait_callback (lwp
, NULL
);
5088 /* If the lwp exits while we try to stop it, there's nothing
5090 lwp
= find_lwp_pid (ptid
);
5094 /* If we didn't collect any signal other than SIGSTOP while
5095 stopping the LWP, push a SIGNAL_0 event. In either case, the
5096 event-loop will end up calling target_wait which will collect
5098 if (lwp
->status
== 0)
5099 lwp
->status
= W_STOPCODE (0);
5104 /* Already known to be stopped; do nothing. */
5106 if (debug_linux_nat
)
5108 if (find_thread_ptid (lwp
->ptid
)->stop_requested
)
5109 fprintf_unfiltered (gdb_stdlog
, "\
5110 LNSL: already stopped/stop_requested %s\n",
5111 target_pid_to_str (lwp
->ptid
));
5113 fprintf_unfiltered (gdb_stdlog
, "\
5114 LNSL: already stopped/no stop_requested yet %s\n",
5115 target_pid_to_str (lwp
->ptid
));
5122 linux_nat_stop (ptid_t ptid
)
5125 iterate_over_lwps (ptid
, linux_nat_stop_lwp
, NULL
);
5127 linux_ops
->to_stop (ptid
);
5131 linux_nat_close (int quitting
)
5133 /* Unregister from the event loop. */
5134 if (target_is_async_p ())
5135 target_async (NULL
, 0);
5137 /* Reset the async_masking. */
5138 linux_nat_async_mask_value
= 1;
5140 if (linux_ops
->to_close
)
5141 linux_ops
->to_close (quitting
);
5145 linux_nat_add_target (struct target_ops
*t
)
5147 /* Save the provided single-threaded target. We save this in a separate
5148 variable because another target we've inherited from (e.g. inf-ptrace)
5149 may have saved a pointer to T; we want to use it for the final
5150 process stratum target. */
5151 linux_ops_saved
= *t
;
5152 linux_ops
= &linux_ops_saved
;
5154 /* Override some methods for multithreading. */
5155 t
->to_create_inferior
= linux_nat_create_inferior
;
5156 t
->to_attach
= linux_nat_attach
;
5157 t
->to_detach
= linux_nat_detach
;
5158 t
->to_resume
= linux_nat_resume
;
5159 t
->to_wait
= linux_nat_wait
;
5160 t
->to_xfer_partial
= linux_nat_xfer_partial
;
5161 t
->to_kill
= linux_nat_kill
;
5162 t
->to_mourn_inferior
= linux_nat_mourn_inferior
;
5163 t
->to_thread_alive
= linux_nat_thread_alive
;
5164 t
->to_pid_to_str
= linux_nat_pid_to_str
;
5165 t
->to_has_thread_control
= tc_schedlock
;
5167 t
->to_can_async_p
= linux_nat_can_async_p
;
5168 t
->to_is_async_p
= linux_nat_is_async_p
;
5169 t
->to_supports_non_stop
= linux_nat_supports_non_stop
;
5170 t
->to_async
= linux_nat_async
;
5171 t
->to_async_mask
= linux_nat_async_mask
;
5172 t
->to_terminal_inferior
= linux_nat_terminal_inferior
;
5173 t
->to_terminal_ours
= linux_nat_terminal_ours
;
5174 t
->to_close
= linux_nat_close
;
5176 /* Methods for non-stop support. */
5177 t
->to_stop
= linux_nat_stop
;
5179 t
->to_supports_multi_process
= linux_nat_supports_multi_process
;
5181 /* We don't change the stratum; this target will sit at
5182 process_stratum and thread_db will set at thread_stratum. This
5183 is a little strange, since this is a multi-threaded-capable
5184 target, but we want to be on the stack below thread_db, and we
5185 also want to be used for single-threaded processes. */
5190 /* Register a method to call whenever a new thread is attached. */
5192 linux_nat_set_new_thread (struct target_ops
*t
, void (*new_thread
) (ptid_t
))
5194 /* Save the pointer. We only support a single registered instance
5195 of the GNU/Linux native target, so we do not need to map this to
5197 linux_nat_new_thread
= new_thread
;
5200 /* Register a method that converts a siginfo object between the layout
5201 that ptrace returns, and the layout in the architecture of the
5204 linux_nat_set_siginfo_fixup (struct target_ops
*t
,
5205 int (*siginfo_fixup
) (struct siginfo
*,
5209 /* Save the pointer. */
5210 linux_nat_siginfo_fixup
= siginfo_fixup
;
5213 /* Return the saved siginfo associated with PTID. */
5215 linux_nat_get_siginfo (ptid_t ptid
)
5217 struct lwp_info
*lp
= find_lwp_pid (ptid
);
5219 gdb_assert (lp
!= NULL
);
5221 return &lp
->siginfo
;
5224 /* Provide a prototype to silence -Wmissing-prototypes. */
5225 extern initialize_file_ftype _initialize_linux_nat
;
5228 _initialize_linux_nat (void)
5232 add_info ("proc", linux_nat_info_proc_cmd
, _("\
5233 Show /proc process information about any running process.\n\
5234 Specify any process id, or use the program being debugged by default.\n\
5235 Specify any of the following keywords for detailed info:\n\
5236 mappings -- list of mapped memory regions.\n\
5237 stat -- list a bunch of random process info.\n\
5238 status -- list a different bunch of random process info.\n\
5239 all -- list all available /proc info."));
5241 add_setshow_zinteger_cmd ("lin-lwp", class_maintenance
,
5242 &debug_linux_nat
, _("\
5243 Set debugging of GNU/Linux lwp module."), _("\
5244 Show debugging of GNU/Linux lwp module."), _("\
5245 Enables printf debugging output."),
5247 show_debug_linux_nat
,
5248 &setdebuglist
, &showdebuglist
);
5250 add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance
,
5251 &debug_linux_nat_async
, _("\
5252 Set debugging of GNU/Linux async lwp module."), _("\
5253 Show debugging of GNU/Linux async lwp module."), _("\
5254 Enables printf debugging output."),
5256 show_debug_linux_nat_async
,
5257 &setdebuglist
, &showdebuglist
);
5259 /* Save this mask as the default. */
5260 sigprocmask (SIG_SETMASK
, NULL
, &normal_mask
);
5262 /* Install a SIGCHLD handler. */
5263 sigchld_action
.sa_handler
= sigchld_handler
;
5264 sigemptyset (&sigchld_action
.sa_mask
);
5265 sigchld_action
.sa_flags
= SA_RESTART
;
5267 /* Make it the default. */
5268 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5270 /* Make sure we don't block SIGCHLD during a sigsuspend. */
5271 sigprocmask (SIG_SETMASK
, NULL
, &suspend_mask
);
5272 sigdelset (&suspend_mask
, SIGCHLD
);
5274 sigemptyset (&blocked_mask
);
5276 add_setshow_boolean_cmd ("disable-randomization", class_support
,
5277 &disable_randomization
, _("\
5278 Set disabling of debuggee's virtual address space randomization."), _("\
5279 Show disabling of debuggee's virtual address space randomization."), _("\
5280 When this mode is on (which is the default), randomization of the virtual\n\
5281 address space is disabled. Standalone programs run with the randomization\n\
5282 enabled by default on some platforms."),
5283 &set_disable_randomization
,
5284 &show_disable_randomization
,
5285 &setlist
, &showlist
);
5289 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
5290 the GNU/Linux Threads library and therefore doesn't really belong
5293 /* Read variable NAME in the target and return its value if found.
5294 Otherwise return zero. It is assumed that the type of the variable
5298 get_signo (const char *name
)
5300 struct minimal_symbol
*ms
;
5303 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
5307 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms
), (gdb_byte
*) &signo
,
5308 sizeof (signo
)) != 0)
5314 /* Return the set of signals used by the threads library in *SET. */
5317 lin_thread_get_thread_signals (sigset_t
*set
)
5319 struct sigaction action
;
5320 int restart
, cancel
;
5322 sigemptyset (&blocked_mask
);
5325 restart
= get_signo ("__pthread_sig_restart");
5326 cancel
= get_signo ("__pthread_sig_cancel");
5328 /* LinuxThreads normally uses the first two RT signals, but in some legacy
5329 cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5330 not provide any way for the debugger to query the signal numbers -
5331 fortunately they don't change! */
5334 restart
= __SIGRTMIN
;
5337 cancel
= __SIGRTMIN
+ 1;
5339 sigaddset (set
, restart
);
5340 sigaddset (set
, cancel
);
5342 /* The GNU/Linux Threads library makes terminating threads send a
5343 special "cancel" signal instead of SIGCHLD. Make sure we catch
5344 those (to prevent them from terminating GDB itself, which is
5345 likely to be their default action) and treat them the same way as
5348 action
.sa_handler
= sigchld_handler
;
5349 sigemptyset (&action
.sa_mask
);
5350 action
.sa_flags
= SA_RESTART
;
5351 sigaction (cancel
, &action
, NULL
);
5353 /* We block the "cancel" signal throughout this code ... */
5354 sigaddset (&blocked_mask
, cancel
);
5355 sigprocmask (SIG_BLOCK
, &blocked_mask
, NULL
);
5357 /* ... except during a sigsuspend. */
5358 sigdelset (&suspend_mask
, cancel
);