linux-nat.c revision 1.1 1 1.1 christos /* GNU/Linux native-dependent code common to multiple platforms.
2 1.1 christos
3 1.1 christos Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "inferior.h"
22 1.1 christos #include "target.h"
23 1.1 christos #include "nat/linux-nat.h"
24 1.1 christos #include "nat/linux-waitpid.h"
25 1.1 christos #include <string.h>
26 1.1 christos #include "gdb_wait.h"
27 1.1 christos #include "gdb_assert.h"
28 1.1 christos #ifdef HAVE_TKILL_SYSCALL
29 1.1 christos #include <unistd.h>
30 1.1 christos #include <sys/syscall.h>
31 1.1 christos #endif
32 1.1 christos #include <sys/ptrace.h>
33 1.1 christos #include "linux-nat.h"
34 1.1 christos #include "linux-ptrace.h"
35 1.1 christos #include "linux-procfs.h"
36 1.1 christos #include "linux-fork.h"
37 1.1 christos #include "gdbthread.h"
38 1.1 christos #include "gdbcmd.h"
39 1.1 christos #include "regcache.h"
40 1.1 christos #include "regset.h"
41 1.1 christos #include "inf-child.h"
42 1.1 christos #include "inf-ptrace.h"
43 1.1 christos #include "auxv.h"
44 1.1 christos #include <sys/procfs.h> /* for elf_gregset etc. */
45 1.1 christos #include "elf-bfd.h" /* for elfcore_write_* */
46 1.1 christos #include "gregset.h" /* for gregset */
47 1.1 christos #include "gdbcore.h" /* for get_exec_file */
48 1.1 christos #include <ctype.h> /* for isdigit */
49 1.1 christos #include <sys/stat.h> /* for struct stat */
50 1.1 christos #include <fcntl.h> /* for O_RDONLY */
51 1.1 christos #include "inf-loop.h"
52 1.1 christos #include "event-loop.h"
53 1.1 christos #include "event-top.h"
54 1.1 christos #include <pwd.h>
55 1.1 christos #include <sys/types.h>
56 1.1 christos #include <dirent.h>
57 1.1 christos #include "xml-support.h"
58 1.1 christos #include "terminal.h"
59 1.1 christos #include <sys/vfs.h>
60 1.1 christos #include "solib.h"
61 1.1 christos #include "linux-osdata.h"
62 1.1 christos #include "linux-tdep.h"
63 1.1 christos #include "symfile.h"
64 1.1 christos #include "agent.h"
65 1.1 christos #include "tracepoint.h"
66 1.1 christos #include "exceptions.h"
67 1.1 christos #include "buffer.h"
68 1.1 christos #include "target-descriptions.h"
69 1.1 christos #include "filestuff.h"
70 1.1 christos
71 1.1 christos #ifndef SPUFS_MAGIC
72 1.1 christos #define SPUFS_MAGIC 0x23c9b64e
73 1.1 christos #endif
74 1.1 christos
75 1.1 christos #ifdef HAVE_PERSONALITY
76 1.1 christos # include <sys/personality.h>
77 1.1 christos # if !HAVE_DECL_ADDR_NO_RANDOMIZE
78 1.1 christos # define ADDR_NO_RANDOMIZE 0x0040000
79 1.1 christos # endif
80 1.1 christos #endif /* HAVE_PERSONALITY */
81 1.1 christos
82 1.1 christos /* This comment documents high-level logic of this file.
83 1.1 christos
84 1.1 christos Waiting for events in sync mode
85 1.1 christos ===============================
86 1.1 christos
87 1.1 christos When waiting for an event in a specific thread, we just use waitpid, passing
88 1.1 christos the specific pid, and not passing WNOHANG.
89 1.1 christos
90 1.1 christos When waiting for an event in all threads, waitpid is not quite good. Prior to
91 1.1 christos version 2.4, Linux can either wait for event in main thread, or in secondary
92 1.1 christos threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
93 1.1 christos miss an event. The solution is to use non-blocking waitpid, together with
94 1.1 christos sigsuspend. First, we use non-blocking waitpid to get an event in the main
95 1.1 christos process, if any. Second, we use non-blocking waitpid with the __WCLONED
96 1.1 christos flag to check for events in cloned processes. If nothing is found, we use
97 1.1 christos sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
98 1.1 christos happened to a child process -- and SIGCHLD will be delivered both for events
99 1.1 christos in main debugged process and in cloned processes. As soon as we know there's
100 1.1 christos an event, we get back to calling nonblocking waitpid with and without
101 1.1 christos __WCLONED.
102 1.1 christos
103 1.1 christos Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
104 1.1 christos so that we don't miss a signal. If SIGCHLD arrives in between, when it's
105 1.1 christos blocked, the signal becomes pending and sigsuspend immediately
106 1.1 christos notices it and returns.
107 1.1 christos
108 1.1 christos Waiting for events in async mode
109 1.1 christos ================================
110 1.1 christos
111 1.1 christos In async mode, GDB should always be ready to handle both user input
112 1.1 christos and target events, so neither blocking waitpid nor sigsuspend are
113 1.1 christos viable options. Instead, we should asynchronously notify the GDB main
114 1.1 christos event loop whenever there's an unprocessed event from the target. We
115 1.1 christos detect asynchronous target events by handling SIGCHLD signals. To
116 1.1 christos notify the event loop about target events, the self-pipe trick is used
117 1.1 christos --- a pipe is registered as waitable event source in the event loop,
118 1.1 christos the event loop select/poll's on the read end of this pipe (as well on
119 1.1 christos other event sources, e.g., stdin), and the SIGCHLD handler writes a
120 1.1 christos byte to this pipe. This is more portable than relying on
121 1.1 christos pselect/ppoll, since on kernels that lack those syscalls, libc
122 1.1 christos emulates them with select/poll+sigprocmask, and that is racy
123 1.1 christos (a.k.a. plain broken).
124 1.1 christos
125 1.1 christos Obviously, if we fail to notify the event loop if there's a target
126 1.1 christos event, it's bad. OTOH, if we notify the event loop when there's no
127 1.1 christos event from the target, linux_nat_wait will detect that there's no real
128 1.1 christos event to report, and return event of type TARGET_WAITKIND_IGNORE.
129 1.1 christos This is mostly harmless, but it will waste time and is better avoided.
130 1.1 christos
131 1.1 christos The main design point is that every time GDB is outside linux-nat.c,
132 1.1 christos we have a SIGCHLD handler installed that is called when something
133 1.1 christos happens to the target and notifies the GDB event loop. Whenever GDB
134 1.1 christos core decides to handle the event, and calls into linux-nat.c, we
135 1.1 christos process things as in sync mode, except that the we never block in
136 1.1 christos sigsuspend.
137 1.1 christos
138 1.1 christos While processing an event, we may end up momentarily blocked in
139 1.1 christos waitpid calls. Those waitpid calls, while blocking, are guarantied to
140 1.1 christos return quickly. E.g., in all-stop mode, before reporting to the core
141 1.1 christos that an LWP hit a breakpoint, all LWPs are stopped by sending them
142 1.1 christos SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
143 1.1 christos Note that this is different from blocking indefinitely waiting for the
144 1.1 christos next event --- here, we're already handling an event.
145 1.1 christos
146 1.1 christos Use of signals
147 1.1 christos ==============
148 1.1 christos
149 1.1 christos We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
150 1.1 christos signal is not entirely significant; we just need for a signal to be delivered,
151 1.1 christos so that we can intercept it. SIGSTOP's advantage is that it can not be
152 1.1 christos blocked. A disadvantage is that it is not a real-time signal, so it can only
153 1.1 christos be queued once; we do not keep track of other sources of SIGSTOP.
154 1.1 christos
155 1.1 christos Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
156 1.1 christos use them, because they have special behavior when the signal is generated -
157 1.1 christos not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
158 1.1 christos kills the entire thread group.
159 1.1 christos
160 1.1 christos A delivered SIGSTOP would stop the entire thread group, not just the thread we
161 1.1 christos tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
162 1.1 christos cancel it (by PTRACE_CONT without passing SIGSTOP).
163 1.1 christos
164 1.1 christos We could use a real-time signal instead. This would solve those problems; we
165 1.1 christos could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
166 1.1 christos But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
167 1.1 christos generates it, and there are races with trying to find a signal that is not
168 1.1 christos blocked. */
169 1.1 christos
170 1.1 christos #ifndef O_LARGEFILE
171 1.1 christos #define O_LARGEFILE 0
172 1.1 christos #endif
173 1.1 christos
174 1.1 christos /* The single-threaded native GNU/Linux target_ops. We save a pointer for
175 1.1 christos the use of the multi-threaded target. */
176 1.1 christos static struct target_ops *linux_ops;
177 1.1 christos static struct target_ops linux_ops_saved;
178 1.1 christos
179 1.1 christos /* The method to call, if any, when a new thread is attached. */
180 1.1 christos static void (*linux_nat_new_thread) (struct lwp_info *);
181 1.1 christos
182 1.1 christos /* The method to call, if any, when a new fork is attached. */
183 1.1 christos static linux_nat_new_fork_ftype *linux_nat_new_fork;
184 1.1 christos
185 1.1 christos /* The method to call, if any, when a process is no longer
186 1.1 christos attached. */
187 1.1 christos static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
188 1.1 christos
189 1.1 christos /* Hook to call prior to resuming a thread. */
190 1.1 christos static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
191 1.1 christos
192 1.1 christos /* The method to call, if any, when the siginfo object needs to be
193 1.1 christos converted between the layout returned by ptrace, and the layout in
194 1.1 christos the architecture of the inferior. */
195 1.1 christos static int (*linux_nat_siginfo_fixup) (siginfo_t *,
196 1.1 christos gdb_byte *,
197 1.1 christos int);
198 1.1 christos
199 1.1 christos /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
200 1.1 christos Called by our to_xfer_partial. */
201 1.1 christos static target_xfer_partial_ftype *super_xfer_partial;
202 1.1 christos
203 1.1 christos static unsigned int debug_linux_nat;
204 1.1 christos static void
205 1.1 christos show_debug_linux_nat (struct ui_file *file, int from_tty,
206 1.1 christos struct cmd_list_element *c, const char *value)
207 1.1 christos {
208 1.1 christos fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
209 1.1 christos value);
210 1.1 christos }
211 1.1 christos
212 1.1 christos struct simple_pid_list
213 1.1 christos {
214 1.1 christos int pid;
215 1.1 christos int status;
216 1.1 christos struct simple_pid_list *next;
217 1.1 christos };
218 1.1 christos struct simple_pid_list *stopped_pids;
219 1.1 christos
220 1.1 christos /* Async mode support. */
221 1.1 christos
222 1.1 christos /* The read/write ends of the pipe registered as waitable file in the
223 1.1 christos event loop. */
224 1.1 christos static int linux_nat_event_pipe[2] = { -1, -1 };
225 1.1 christos
226 1.1 christos /* Flush the event pipe. */
227 1.1 christos
228 1.1 christos static void
229 1.1 christos async_file_flush (void)
230 1.1 christos {
231 1.1 christos int ret;
232 1.1 christos char buf;
233 1.1 christos
234 1.1 christos do
235 1.1 christos {
236 1.1 christos ret = read (linux_nat_event_pipe[0], &buf, 1);
237 1.1 christos }
238 1.1 christos while (ret >= 0 || (ret == -1 && errno == EINTR));
239 1.1 christos }
240 1.1 christos
241 1.1 christos /* Put something (anything, doesn't matter what, or how much) in event
242 1.1 christos pipe, so that the select/poll in the event-loop realizes we have
243 1.1 christos something to process. */
244 1.1 christos
245 1.1 christos static void
246 1.1 christos async_file_mark (void)
247 1.1 christos {
248 1.1 christos int ret;
249 1.1 christos
250 1.1 christos /* It doesn't really matter what the pipe contains, as long we end
251 1.1 christos up with something in it. Might as well flush the previous
252 1.1 christos left-overs. */
253 1.1 christos async_file_flush ();
254 1.1 christos
255 1.1 christos do
256 1.1 christos {
257 1.1 christos ret = write (linux_nat_event_pipe[1], "+", 1);
258 1.1 christos }
259 1.1 christos while (ret == -1 && errno == EINTR);
260 1.1 christos
261 1.1 christos /* Ignore EAGAIN. If the pipe is full, the event loop will already
262 1.1 christos be awakened anyway. */
263 1.1 christos }
264 1.1 christos
265 1.1 christos static void linux_nat_async (void (*callback)
266 1.1 christos (enum inferior_event_type event_type,
267 1.1 christos void *context),
268 1.1 christos void *context);
269 1.1 christos static int kill_lwp (int lwpid, int signo);
270 1.1 christos
271 1.1 christos static int stop_callback (struct lwp_info *lp, void *data);
272 1.1 christos
273 1.1 christos static void block_child_signals (sigset_t *prev_mask);
274 1.1 christos static void restore_child_signals_mask (sigset_t *prev_mask);
275 1.1 christos
276 1.1 christos struct lwp_info;
277 1.1 christos static struct lwp_info *add_lwp (ptid_t ptid);
278 1.1 christos static void purge_lwp_list (int pid);
279 1.1 christos static void delete_lwp (ptid_t ptid);
280 1.1 christos static struct lwp_info *find_lwp_pid (ptid_t ptid);
281 1.1 christos
282 1.1 christos
283 1.1 christos /* Trivial list manipulation functions to keep track of a list of
285 1.1 christos new stopped processes. */
286 1.1 christos static void
287 1.1 christos add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
288 1.1 christos {
289 1.1 christos struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
290 1.1 christos
291 1.1 christos new_pid->pid = pid;
292 1.1 christos new_pid->status = status;
293 1.1 christos new_pid->next = *listp;
294 1.1 christos *listp = new_pid;
295 1.1 christos }
296 1.1 christos
297 1.1 christos static int
298 1.1 christos in_pid_list_p (struct simple_pid_list *list, int pid)
299 1.1 christos {
300 1.1 christos struct simple_pid_list *p;
301 1.1 christos
302 1.1 christos for (p = list; p != NULL; p = p->next)
303 1.1 christos if (p->pid == pid)
304 1.1 christos return 1;
305 1.1 christos return 0;
306 1.1 christos }
307 1.1 christos
308 1.1 christos static int
309 1.1 christos pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
310 1.1 christos {
311 1.1 christos struct simple_pid_list **p;
312 1.1 christos
313 1.1 christos for (p = listp; *p != NULL; p = &(*p)->next)
314 1.1 christos if ((*p)->pid == pid)
315 1.1 christos {
316 1.1 christos struct simple_pid_list *next = (*p)->next;
317 1.1 christos
318 1.1 christos *statusp = (*p)->status;
319 1.1 christos xfree (*p);
320 1.1 christos *p = next;
321 1.1 christos return 1;
322 1.1 christos }
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos
326 1.1 christos /* Initialize ptrace warnings and check for supported ptrace
327 1.1 christos features given PID. */
328 1.1 christos
329 1.1 christos static void
330 1.1 christos linux_init_ptrace (pid_t pid)
331 1.1 christos {
332 1.1 christos linux_enable_event_reporting (pid);
333 1.1 christos linux_ptrace_init_warnings ();
334 1.1 christos }
335 1.1 christos
336 1.1 christos static void
337 1.1 christos linux_child_post_attach (int pid)
338 1.1 christos {
339 1.1 christos linux_init_ptrace (pid);
340 1.1 christos }
341 1.1 christos
342 1.1 christos static void
343 1.1 christos linux_child_post_startup_inferior (ptid_t ptid)
344 1.1 christos {
345 1.1 christos linux_init_ptrace (ptid_get_pid (ptid));
346 1.1 christos }
347 1.1 christos
348 1.1 christos /* Return the number of known LWPs in the tgid given by PID. */
349 1.1 christos
350 1.1 christos static int
351 1.1 christos num_lwps (int pid)
352 1.1 christos {
353 1.1 christos int count = 0;
354 1.1 christos struct lwp_info *lp;
355 1.1 christos
356 1.1 christos for (lp = lwp_list; lp; lp = lp->next)
357 1.1 christos if (ptid_get_pid (lp->ptid) == pid)
358 1.1 christos count++;
359 1.1 christos
360 1.1 christos return count;
361 1.1 christos }
362 1.1 christos
363 1.1 christos /* Call delete_lwp with prototype compatible for make_cleanup. */
364 1.1 christos
365 1.1 christos static void
366 1.1 christos delete_lwp_cleanup (void *lp_voidp)
367 1.1 christos {
368 1.1 christos struct lwp_info *lp = lp_voidp;
369 1.1 christos
370 1.1 christos delete_lwp (lp->ptid);
371 1.1 christos }
372 1.1 christos
373 1.1 christos static int
374 1.1 christos linux_child_follow_fork (struct target_ops *ops, int follow_child,
375 1.1 christos int detach_fork)
376 1.1 christos {
377 1.1 christos int has_vforked;
378 1.1 christos int parent_pid, child_pid;
379 1.1 christos
380 1.1 christos has_vforked = (inferior_thread ()->pending_follow.kind
381 1.1 christos == TARGET_WAITKIND_VFORKED);
382 1.1 christos parent_pid = ptid_get_lwp (inferior_ptid);
383 1.1 christos if (parent_pid == 0)
384 1.1 christos parent_pid = ptid_get_pid (inferior_ptid);
385 1.1 christos child_pid
386 1.1 christos = ptid_get_pid (inferior_thread ()->pending_follow.value.related_pid);
387 1.1 christos
388 1.1 christos if (has_vforked
389 1.1 christos && !non_stop /* Non-stop always resumes both branches. */
390 1.1 christos && (!target_is_async_p () || sync_execution)
391 1.1 christos && !(follow_child || detach_fork || sched_multi))
392 1.1 christos {
393 1.1 christos /* The parent stays blocked inside the vfork syscall until the
394 1.1 christos child execs or exits. If we don't let the child run, then
395 1.1 christos the parent stays blocked. If we're telling the parent to run
396 1.1 christos in the foreground, the user will not be able to ctrl-c to get
397 1.1 christos back the terminal, effectively hanging the debug session. */
398 1.1 christos fprintf_filtered (gdb_stderr, _("\
399 1.1 christos Can not resume the parent process over vfork in the foreground while\n\
400 1.1 christos holding the child stopped. Try \"set detach-on-fork\" or \
401 1.1 christos \"set schedule-multiple\".\n"));
402 1.1 christos /* FIXME output string > 80 columns. */
403 1.1 christos return 1;
404 1.1 christos }
405 1.1 christos
406 1.1 christos if (! follow_child)
407 1.1 christos {
408 1.1 christos struct lwp_info *child_lp = NULL;
409 1.1 christos
410 1.1 christos /* We're already attached to the parent, by default. */
411 1.1 christos
412 1.1 christos /* Detach new forked process? */
413 1.1 christos if (detach_fork)
414 1.1 christos {
415 1.1 christos struct cleanup *old_chain;
416 1.1 christos
417 1.1 christos /* Before detaching from the child, remove all breakpoints
418 1.1 christos from it. If we forked, then this has already been taken
419 1.1 christos care of by infrun.c. If we vforked however, any
420 1.1 christos breakpoint inserted in the parent is visible in the
421 1.1 christos child, even those added while stopped in a vfork
422 1.1 christos catchpoint. This will remove the breakpoints from the
423 1.1 christos parent also, but they'll be reinserted below. */
424 1.1 christos if (has_vforked)
425 1.1 christos {
426 1.1 christos /* keep breakpoints list in sync. */
427 1.1 christos remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
428 1.1 christos }
429 1.1 christos
430 1.1 christos if (info_verbose || debug_linux_nat)
431 1.1 christos {
432 1.1 christos target_terminal_ours ();
433 1.1 christos fprintf_filtered (gdb_stdlog,
434 1.1 christos "Detaching after fork from "
435 1.1 christos "child process %d.\n",
436 1.1 christos child_pid);
437 1.1 christos }
438 1.1 christos
439 1.1 christos old_chain = save_inferior_ptid ();
440 1.1 christos inferior_ptid = ptid_build (child_pid, child_pid, 0);
441 1.1 christos
442 1.1 christos child_lp = add_lwp (inferior_ptid);
443 1.1 christos child_lp->stopped = 1;
444 1.1 christos child_lp->last_resume_kind = resume_stop;
445 1.1 christos make_cleanup (delete_lwp_cleanup, child_lp);
446 1.1 christos
447 1.1 christos if (linux_nat_prepare_to_resume != NULL)
448 1.1 christos linux_nat_prepare_to_resume (child_lp);
449 1.1 christos ptrace (PTRACE_DETACH, child_pid, 0, 0);
450 1.1 christos
451 1.1 christos do_cleanups (old_chain);
452 1.1 christos }
453 1.1 christos else
454 1.1 christos {
455 1.1 christos struct inferior *parent_inf, *child_inf;
456 1.1 christos struct cleanup *old_chain;
457 1.1 christos
458 1.1 christos /* Add process to GDB's tables. */
459 1.1 christos child_inf = add_inferior (child_pid);
460 1.1 christos
461 1.1 christos parent_inf = current_inferior ();
462 1.1 christos child_inf->attach_flag = parent_inf->attach_flag;
463 1.1 christos copy_terminal_info (child_inf, parent_inf);
464 1.1 christos child_inf->gdbarch = parent_inf->gdbarch;
465 1.1 christos copy_inferior_target_desc_info (child_inf, parent_inf);
466 1.1 christos
467 1.1 christos old_chain = save_inferior_ptid ();
468 1.1 christos save_current_program_space ();
469 1.1 christos
470 1.1 christos inferior_ptid = ptid_build (child_pid, child_pid, 0);
471 1.1 christos add_thread (inferior_ptid);
472 1.1 christos child_lp = add_lwp (inferior_ptid);
473 1.1 christos child_lp->stopped = 1;
474 1.1 christos child_lp->last_resume_kind = resume_stop;
475 1.1 christos child_inf->symfile_flags = SYMFILE_NO_READ;
476 1.1 christos
477 1.1 christos /* If this is a vfork child, then the address-space is
478 1.1 christos shared with the parent. */
479 1.1 christos if (has_vforked)
480 1.1 christos {
481 1.1 christos child_inf->pspace = parent_inf->pspace;
482 1.1 christos child_inf->aspace = parent_inf->aspace;
483 1.1 christos
484 1.1 christos /* The parent will be frozen until the child is done
485 1.1 christos with the shared region. Keep track of the
486 1.1 christos parent. */
487 1.1 christos child_inf->vfork_parent = parent_inf;
488 1.1 christos child_inf->pending_detach = 0;
489 1.1 christos parent_inf->vfork_child = child_inf;
490 1.1 christos parent_inf->pending_detach = 0;
491 1.1 christos }
492 1.1 christos else
493 1.1 christos {
494 1.1 christos child_inf->aspace = new_address_space ();
495 1.1 christos child_inf->pspace = add_program_space (child_inf->aspace);
496 1.1 christos child_inf->removable = 1;
497 1.1 christos set_current_program_space (child_inf->pspace);
498 1.1 christos clone_program_space (child_inf->pspace, parent_inf->pspace);
499 1.1 christos
500 1.1 christos /* Let the shared library layer (solib-svr4) learn about
501 1.1 christos this new process, relocate the cloned exec, pull in
502 1.1 christos shared libraries, and install the solib event
503 1.1 christos breakpoint. If a "cloned-VM" event was propagated
504 1.1 christos better throughout the core, this wouldn't be
505 1.1 christos required. */
506 1.1 christos solib_create_inferior_hook (0);
507 1.1 christos }
508 1.1 christos
509 1.1 christos /* Let the thread_db layer learn about this new process. */
510 1.1 christos check_for_thread_db ();
511 1.1 christos
512 1.1 christos do_cleanups (old_chain);
513 1.1 christos }
514 1.1 christos
515 1.1 christos if (has_vforked)
516 1.1 christos {
517 1.1 christos struct lwp_info *parent_lp;
518 1.1 christos struct inferior *parent_inf;
519 1.1 christos
520 1.1 christos parent_inf = current_inferior ();
521 1.1 christos
522 1.1 christos /* If we detached from the child, then we have to be careful
523 1.1 christos to not insert breakpoints in the parent until the child
524 1.1 christos is done with the shared memory region. However, if we're
525 1.1 christos staying attached to the child, then we can and should
526 1.1 christos insert breakpoints, so that we can debug it. A
527 1.1 christos subsequent child exec or exit is enough to know when does
528 1.1 christos the child stops using the parent's address space. */
529 1.1 christos parent_inf->waiting_for_vfork_done = detach_fork;
530 1.1 christos parent_inf->pspace->breakpoints_not_allowed = detach_fork;
531 1.1 christos
532 1.1 christos parent_lp = find_lwp_pid (pid_to_ptid (parent_pid));
533 1.1 christos gdb_assert (linux_supports_tracefork () >= 0);
534 1.1 christos
535 1.1 christos if (linux_supports_tracevforkdone ())
536 1.1 christos {
537 1.1 christos if (debug_linux_nat)
538 1.1 christos fprintf_unfiltered (gdb_stdlog,
539 1.1 christos "LCFF: waiting for VFORK_DONE on %d\n",
540 1.1 christos parent_pid);
541 1.1 christos parent_lp->stopped = 1;
542 1.1 christos
543 1.1 christos /* We'll handle the VFORK_DONE event like any other
544 1.1 christos event, in target_wait. */
545 1.1 christos }
546 1.1 christos else
547 1.1 christos {
548 1.1 christos /* We can't insert breakpoints until the child has
549 1.1 christos finished with the shared memory region. We need to
550 1.1 christos wait until that happens. Ideal would be to just
551 1.1 christos call:
552 1.1 christos - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
553 1.1 christos - waitpid (parent_pid, &status, __WALL);
554 1.1 christos However, most architectures can't handle a syscall
555 1.1 christos being traced on the way out if it wasn't traced on
556 1.1 christos the way in.
557 1.1 christos
558 1.1 christos We might also think to loop, continuing the child
559 1.1 christos until it exits or gets a SIGTRAP. One problem is
560 1.1 christos that the child might call ptrace with PTRACE_TRACEME.
561 1.1 christos
562 1.1 christos There's no simple and reliable way to figure out when
563 1.1 christos the vforked child will be done with its copy of the
564 1.1 christos shared memory. We could step it out of the syscall,
565 1.1 christos two instructions, let it go, and then single-step the
566 1.1 christos parent once. When we have hardware single-step, this
567 1.1 christos would work; with software single-step it could still
568 1.1 christos be made to work but we'd have to be able to insert
569 1.1 christos single-step breakpoints in the child, and we'd have
570 1.1 christos to insert -just- the single-step breakpoint in the
571 1.1 christos parent. Very awkward.
572 1.1 christos
573 1.1 christos In the end, the best we can do is to make sure it
574 1.1 christos runs for a little while. Hopefully it will be out of
575 1.1 christos range of any breakpoints we reinsert. Usually this
576 1.1 christos is only the single-step breakpoint at vfork's return
577 1.1 christos point. */
578 1.1 christos
579 1.1 christos if (debug_linux_nat)
580 1.1 christos fprintf_unfiltered (gdb_stdlog,
581 1.1 christos "LCFF: no VFORK_DONE "
582 1.1 christos "support, sleeping a bit\n");
583 1.1 christos
584 1.1 christos usleep (10000);
585 1.1 christos
586 1.1 christos /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
587 1.1 christos and leave it pending. The next linux_nat_resume call
588 1.1 christos will notice a pending event, and bypasses actually
589 1.1 christos resuming the inferior. */
590 1.1 christos parent_lp->status = 0;
591 1.1 christos parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
592 1.1 christos parent_lp->stopped = 1;
593 1.1 christos
594 1.1 christos /* If we're in async mode, need to tell the event loop
595 1.1 christos there's something here to process. */
596 1.1 christos if (target_can_async_p ())
597 1.1 christos async_file_mark ();
598 1.1 christos }
599 1.1 christos }
600 1.1 christos }
601 1.1 christos else
602 1.1 christos {
603 1.1 christos struct inferior *parent_inf, *child_inf;
604 1.1 christos struct lwp_info *child_lp;
605 1.1 christos struct program_space *parent_pspace;
606 1.1 christos
607 1.1 christos if (info_verbose || debug_linux_nat)
608 1.1 christos {
609 1.1 christos target_terminal_ours ();
610 1.1 christos if (has_vforked)
611 1.1 christos fprintf_filtered (gdb_stdlog,
612 1.1 christos _("Attaching after process %d "
613 1.1 christos "vfork to child process %d.\n"),
614 1.1 christos parent_pid, child_pid);
615 1.1 christos else
616 1.1 christos fprintf_filtered (gdb_stdlog,
617 1.1 christos _("Attaching after process %d "
618 1.1 christos "fork to child process %d.\n"),
619 1.1 christos parent_pid, child_pid);
620 1.1 christos }
621 1.1 christos
622 1.1 christos /* Add the new inferior first, so that the target_detach below
623 1.1 christos doesn't unpush the target. */
624 1.1 christos
625 1.1 christos child_inf = add_inferior (child_pid);
626 1.1 christos
627 1.1 christos parent_inf = current_inferior ();
628 1.1 christos child_inf->attach_flag = parent_inf->attach_flag;
629 1.1 christos copy_terminal_info (child_inf, parent_inf);
630 1.1 christos child_inf->gdbarch = parent_inf->gdbarch;
631 1.1 christos copy_inferior_target_desc_info (child_inf, parent_inf);
632 1.1 christos
633 1.1 christos parent_pspace = parent_inf->pspace;
634 1.1 christos
635 1.1 christos /* If we're vforking, we want to hold on to the parent until the
636 1.1 christos child exits or execs. At child exec or exit time we can
637 1.1 christos remove the old breakpoints from the parent and detach or
638 1.1 christos resume debugging it. Otherwise, detach the parent now; we'll
639 1.1 christos want to reuse it's program/address spaces, but we can't set
640 1.1 christos them to the child before removing breakpoints from the
641 1.1 christos parent, otherwise, the breakpoints module could decide to
642 1.1 christos remove breakpoints from the wrong process (since they'd be
643 1.1 christos assigned to the same address space). */
644 1.1 christos
645 1.1 christos if (has_vforked)
646 1.1 christos {
647 1.1 christos gdb_assert (child_inf->vfork_parent == NULL);
648 1.1 christos gdb_assert (parent_inf->vfork_child == NULL);
649 1.1 christos child_inf->vfork_parent = parent_inf;
650 1.1 christos child_inf->pending_detach = 0;
651 1.1 christos parent_inf->vfork_child = child_inf;
652 1.1 christos parent_inf->pending_detach = detach_fork;
653 1.1 christos parent_inf->waiting_for_vfork_done = 0;
654 1.1 christos }
655 1.1 christos else if (detach_fork)
656 1.1 christos target_detach (NULL, 0);
657 1.1 christos
658 1.1 christos /* Note that the detach above makes PARENT_INF dangling. */
659 1.1 christos
660 1.1 christos /* Add the child thread to the appropriate lists, and switch to
661 1.1 christos this new thread, before cloning the program space, and
662 1.1 christos informing the solib layer about this new process. */
663 1.1 christos
664 1.1 christos inferior_ptid = ptid_build (child_pid, child_pid, 0);
665 1.1 christos add_thread (inferior_ptid);
666 1.1 christos child_lp = add_lwp (inferior_ptid);
667 1.1 christos child_lp->stopped = 1;
668 1.1 christos child_lp->last_resume_kind = resume_stop;
669 1.1 christos
670 1.1 christos /* If this is a vfork child, then the address-space is shared
671 1.1 christos with the parent. If we detached from the parent, then we can
672 1.1 christos reuse the parent's program/address spaces. */
673 1.1 christos if (has_vforked || detach_fork)
674 1.1 christos {
675 1.1 christos child_inf->pspace = parent_pspace;
676 1.1 christos child_inf->aspace = child_inf->pspace->aspace;
677 1.1 christos }
678 1.1 christos else
679 1.1 christos {
680 1.1 christos child_inf->aspace = new_address_space ();
681 1.1 christos child_inf->pspace = add_program_space (child_inf->aspace);
682 1.1 christos child_inf->removable = 1;
683 1.1 christos child_inf->symfile_flags = SYMFILE_NO_READ;
684 1.1 christos set_current_program_space (child_inf->pspace);
685 1.1 christos clone_program_space (child_inf->pspace, parent_pspace);
686 1.1 christos
687 1.1 christos /* Let the shared library layer (solib-svr4) learn about
688 1.1 christos this new process, relocate the cloned exec, pull in
689 1.1 christos shared libraries, and install the solib event breakpoint.
690 1.1 christos If a "cloned-VM" event was propagated better throughout
691 1.1 christos the core, this wouldn't be required. */
692 1.1 christos solib_create_inferior_hook (0);
693 1.1 christos }
694 1.1 christos
695 1.1 christos /* Let the thread_db layer learn about this new process. */
696 1.1 christos check_for_thread_db ();
697 1.1 christos }
698 1.1 christos
699 1.1 christos return 0;
700 1.1 christos }
701 1.1 christos
702 1.1 christos
703 1.1 christos static int
705 1.1 christos linux_child_insert_fork_catchpoint (int pid)
706 1.1 christos {
707 1.1 christos return !linux_supports_tracefork ();
708 1.1 christos }
709 1.1 christos
710 1.1 christos static int
711 1.1 christos linux_child_remove_fork_catchpoint (int pid)
712 1.1 christos {
713 1.1 christos return 0;
714 1.1 christos }
715 1.1 christos
716 1.1 christos static int
717 1.1 christos linux_child_insert_vfork_catchpoint (int pid)
718 1.1 christos {
719 1.1 christos return !linux_supports_tracefork ();
720 1.1 christos }
721 1.1 christos
722 1.1 christos static int
723 1.1 christos linux_child_remove_vfork_catchpoint (int pid)
724 1.1 christos {
725 1.1 christos return 0;
726 1.1 christos }
727 1.1 christos
728 1.1 christos static int
729 1.1 christos linux_child_insert_exec_catchpoint (int pid)
730 1.1 christos {
731 1.1 christos return !linux_supports_tracefork ();
732 1.1 christos }
733 1.1 christos
734 1.1 christos static int
735 1.1 christos linux_child_remove_exec_catchpoint (int pid)
736 1.1 christos {
737 1.1 christos return 0;
738 1.1 christos }
739 1.1 christos
740 1.1 christos static int
741 1.1 christos linux_child_set_syscall_catchpoint (int pid, int needed, int any_count,
742 1.1 christos int table_size, int *table)
743 1.1 christos {
744 1.1 christos if (!linux_supports_tracesysgood ())
745 1.1 christos return 1;
746 1.1 christos
747 1.1 christos /* On GNU/Linux, we ignore the arguments. It means that we only
748 1.1 christos enable the syscall catchpoints, but do not disable them.
749 1.1 christos
750 1.1 christos Also, we do not use the `table' information because we do not
751 1.1 christos filter system calls here. We let GDB do the logic for us. */
752 1.1 christos return 0;
753 1.1 christos }
754 1.1 christos
755 1.1 christos /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
756 1.1 christos are processes sharing the same VM space. A multi-threaded process
757 1.1 christos is basically a group of such processes. However, such a grouping
758 1.1 christos is almost entirely a user-space issue; the kernel doesn't enforce
759 1.1 christos such a grouping at all (this might change in the future). In
760 1.1 christos general, we'll rely on the threads library (i.e. the GNU/Linux
761 1.1 christos Threads library) to provide such a grouping.
762 1.1 christos
763 1.1 christos It is perfectly well possible to write a multi-threaded application
764 1.1 christos without the assistance of a threads library, by using the clone
765 1.1 christos system call directly. This module should be able to give some
766 1.1 christos rudimentary support for debugging such applications if developers
767 1.1 christos specify the CLONE_PTRACE flag in the clone system call, and are
768 1.1 christos using the Linux kernel 2.4 or above.
769 1.1 christos
770 1.1 christos Note that there are some peculiarities in GNU/Linux that affect
771 1.1 christos this code:
772 1.1 christos
773 1.1 christos - In general one should specify the __WCLONE flag to waitpid in
774 1.1 christos order to make it report events for any of the cloned processes
775 1.1 christos (and leave it out for the initial process). However, if a cloned
776 1.1 christos process has exited the exit status is only reported if the
777 1.1 christos __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
778 1.1 christos we cannot use it since GDB must work on older systems too.
779 1.1 christos
780 1.1 christos - When a traced, cloned process exits and is waited for by the
781 1.1 christos debugger, the kernel reassigns it to the original parent and
782 1.1 christos keeps it around as a "zombie". Somehow, the GNU/Linux Threads
783 1.1 christos library doesn't notice this, which leads to the "zombie problem":
784 1.1 christos When debugged a multi-threaded process that spawns a lot of
785 1.1 christos threads will run out of processes, even if the threads exit,
786 1.1 christos because the "zombies" stay around. */
787 1.1 christos
788 1.1 christos /* List of known LWPs. */
789 1.1 christos struct lwp_info *lwp_list;
790 1.1 christos
791 1.1 christos
793 1.1 christos /* Original signal mask. */
794 1.1 christos static sigset_t normal_mask;
795 1.1 christos
796 1.1 christos /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
797 1.1 christos _initialize_linux_nat. */
798 1.1 christos static sigset_t suspend_mask;
799 1.1 christos
800 1.1 christos /* Signals to block to make that sigsuspend work. */
801 1.1 christos static sigset_t blocked_mask;
802 1.1 christos
803 1.1 christos /* SIGCHLD action. */
804 1.1 christos struct sigaction sigchld_action;
805 1.1 christos
806 1.1 christos /* Block child signals (SIGCHLD and linux threads signals), and store
807 1.1 christos the previous mask in PREV_MASK. */
808 1.1 christos
809 1.1 christos static void
810 1.1 christos block_child_signals (sigset_t *prev_mask)
811 1.1 christos {
812 1.1 christos /* Make sure SIGCHLD is blocked. */
813 1.1 christos if (!sigismember (&blocked_mask, SIGCHLD))
814 1.1 christos sigaddset (&blocked_mask, SIGCHLD);
815 1.1 christos
816 1.1 christos sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
817 1.1 christos }
818 1.1 christos
819 1.1 christos /* Restore child signals mask, previously returned by
820 1.1 christos block_child_signals. */
821 1.1 christos
822 1.1 christos static void
823 1.1 christos restore_child_signals_mask (sigset_t *prev_mask)
824 1.1 christos {
825 1.1 christos sigprocmask (SIG_SETMASK, prev_mask, NULL);
826 1.1 christos }
827 1.1 christos
828 1.1 christos /* Mask of signals to pass directly to the inferior. */
829 1.1 christos static sigset_t pass_mask;
830 1.1 christos
831 1.1 christos /* Update signals to pass to the inferior. */
832 1.1 christos static void
833 1.1 christos linux_nat_pass_signals (int numsigs, unsigned char *pass_signals)
834 1.1 christos {
835 1.1 christos int signo;
836 1.1 christos
837 1.1 christos sigemptyset (&pass_mask);
838 1.1 christos
839 1.1 christos for (signo = 1; signo < NSIG; signo++)
840 1.1 christos {
841 1.1 christos int target_signo = gdb_signal_from_host (signo);
842 1.1 christos if (target_signo < numsigs && pass_signals[target_signo])
843 1.1 christos sigaddset (&pass_mask, signo);
844 1.1 christos }
845 1.1 christos }
846 1.1 christos
847 1.1 christos
848 1.1 christos
850 1.1 christos /* Prototypes for local functions. */
851 1.1 christos static int stop_wait_callback (struct lwp_info *lp, void *data);
852 1.1 christos static int linux_thread_alive (ptid_t ptid);
853 1.1 christos static char *linux_child_pid_to_exec_file (int pid);
854 1.1 christos
855 1.1 christos
856 1.1 christos /* Convert wait status STATUS to a string. Used for printing debug
858 1.1 christos messages only. */
859 1.1 christos
860 1.1 christos static char *
861 1.1 christos status_to_str (int status)
862 1.1 christos {
863 1.1 christos static char buf[64];
864 1.1 christos
865 1.1 christos if (WIFSTOPPED (status))
866 1.1 christos {
867 1.1 christos if (WSTOPSIG (status) == SYSCALL_SIGTRAP)
868 1.1 christos snprintf (buf, sizeof (buf), "%s (stopped at syscall)",
869 1.1 christos strsignal (SIGTRAP));
870 1.1 christos else
871 1.1 christos snprintf (buf, sizeof (buf), "%s (stopped)",
872 1.1 christos strsignal (WSTOPSIG (status)));
873 1.1 christos }
874 1.1 christos else if (WIFSIGNALED (status))
875 1.1 christos snprintf (buf, sizeof (buf), "%s (terminated)",
876 1.1 christos strsignal (WTERMSIG (status)));
877 1.1 christos else
878 1.1 christos snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
879 1.1 christos
880 1.1 christos return buf;
881 1.1 christos }
882 1.1 christos
883 1.1 christos /* Destroy and free LP. */
884 1.1 christos
885 1.1 christos static void
886 1.1 christos lwp_free (struct lwp_info *lp)
887 1.1 christos {
888 1.1 christos xfree (lp->arch_private);
889 1.1 christos xfree (lp);
890 1.1 christos }
891 1.1 christos
892 1.1 christos /* Remove all LWPs belong to PID from the lwp list. */
893 1.1 christos
894 1.1 christos static void
895 1.1 christos purge_lwp_list (int pid)
896 1.1 christos {
897 1.1 christos struct lwp_info *lp, *lpprev, *lpnext;
898 1.1 christos
899 1.1 christos lpprev = NULL;
900 1.1 christos
901 1.1 christos for (lp = lwp_list; lp; lp = lpnext)
902 1.1 christos {
903 1.1 christos lpnext = lp->next;
904 1.1 christos
905 1.1 christos if (ptid_get_pid (lp->ptid) == pid)
906 1.1 christos {
907 1.1 christos if (lp == lwp_list)
908 1.1 christos lwp_list = lp->next;
909 1.1 christos else
910 1.1 christos lpprev->next = lp->next;
911 1.1 christos
912 1.1 christos lwp_free (lp);
913 1.1 christos }
914 1.1 christos else
915 1.1 christos lpprev = lp;
916 1.1 christos }
917 1.1 christos }
918 1.1 christos
919 1.1 christos /* Add the LWP specified by PTID to the list. PTID is the first LWP
920 1.1 christos in the process. Return a pointer to the structure describing the
921 1.1 christos new LWP.
922 1.1 christos
923 1.1 christos This differs from add_lwp in that we don't let the arch specific
924 1.1 christos bits know about this new thread. Current clients of this callback
925 1.1 christos take the opportunity to install watchpoints in the new thread, and
926 1.1 christos we shouldn't do that for the first thread. If we're spawning a
927 1.1 christos child ("run"), the thread executes the shell wrapper first, and we
928 1.1 christos shouldn't touch it until it execs the program we want to debug.
929 1.1 christos For "attach", it'd be okay to call the callback, but it's not
930 1.1 christos necessary, because watchpoints can't yet have been inserted into
931 1.1 christos the inferior. */
932 1.1 christos
933 1.1 christos static struct lwp_info *
934 1.1 christos add_initial_lwp (ptid_t ptid)
935 1.1 christos {
936 1.1 christos struct lwp_info *lp;
937 1.1 christos
938 1.1 christos gdb_assert (ptid_lwp_p (ptid));
939 1.1 christos
940 1.1 christos lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
941 1.1 christos
942 1.1 christos memset (lp, 0, sizeof (struct lwp_info));
943 1.1 christos
944 1.1 christos lp->last_resume_kind = resume_continue;
945 1.1 christos lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
946 1.1 christos
947 1.1 christos lp->ptid = ptid;
948 1.1 christos lp->core = -1;
949 1.1 christos
950 1.1 christos lp->next = lwp_list;
951 1.1 christos lwp_list = lp;
952 1.1 christos
953 1.1 christos return lp;
954 1.1 christos }
955 1.1 christos
956 1.1 christos /* Add the LWP specified by PID to the list. Return a pointer to the
957 1.1 christos structure describing the new LWP. The LWP should already be
958 1.1 christos stopped. */
959 1.1 christos
960 1.1 christos static struct lwp_info *
961 1.1 christos add_lwp (ptid_t ptid)
962 1.1 christos {
963 1.1 christos struct lwp_info *lp;
964 1.1 christos
965 1.1 christos lp = add_initial_lwp (ptid);
966 1.1 christos
967 1.1 christos /* Let the arch specific bits know about this new thread. Current
968 1.1 christos clients of this callback take the opportunity to install
969 1.1 christos watchpoints in the new thread. We don't do this for the first
970 1.1 christos thread though. See add_initial_lwp. */
971 1.1 christos if (linux_nat_new_thread != NULL)
972 1.1 christos linux_nat_new_thread (lp);
973 1.1 christos
974 1.1 christos return lp;
975 1.1 christos }
976 1.1 christos
977 1.1 christos /* Remove the LWP specified by PID from the list. */
978 1.1 christos
979 1.1 christos static void
980 1.1 christos delete_lwp (ptid_t ptid)
981 1.1 christos {
982 1.1 christos struct lwp_info *lp, *lpprev;
983 1.1 christos
984 1.1 christos lpprev = NULL;
985 1.1 christos
986 1.1 christos for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
987 1.1 christos if (ptid_equal (lp->ptid, ptid))
988 1.1 christos break;
989 1.1 christos
990 1.1 christos if (!lp)
991 1.1 christos return;
992 1.1 christos
993 1.1 christos if (lpprev)
994 1.1 christos lpprev->next = lp->next;
995 1.1 christos else
996 1.1 christos lwp_list = lp->next;
997 1.1 christos
998 1.1 christos lwp_free (lp);
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos /* Return a pointer to the structure describing the LWP corresponding
1002 1.1 christos to PID. If no corresponding LWP could be found, return NULL. */
1003 1.1 christos
1004 1.1 christos static struct lwp_info *
1005 1.1 christos find_lwp_pid (ptid_t ptid)
1006 1.1 christos {
1007 1.1 christos struct lwp_info *lp;
1008 1.1 christos int lwp;
1009 1.1 christos
1010 1.1 christos if (ptid_lwp_p (ptid))
1011 1.1 christos lwp = ptid_get_lwp (ptid);
1012 1.1 christos else
1013 1.1 christos lwp = ptid_get_pid (ptid);
1014 1.1 christos
1015 1.1 christos for (lp = lwp_list; lp; lp = lp->next)
1016 1.1 christos if (lwp == ptid_get_lwp (lp->ptid))
1017 1.1 christos return lp;
1018 1.1 christos
1019 1.1 christos return NULL;
1020 1.1 christos }
1021 1.1 christos
1022 1.1 christos /* Call CALLBACK with its second argument set to DATA for every LWP in
1023 1.1 christos the list. If CALLBACK returns 1 for a particular LWP, return a
1024 1.1 christos pointer to the structure describing that LWP immediately.
1025 1.1 christos Otherwise return NULL. */
1026 1.1 christos
1027 1.1 christos struct lwp_info *
1028 1.1 christos iterate_over_lwps (ptid_t filter,
1029 1.1 christos int (*callback) (struct lwp_info *, void *),
1030 1.1 christos void *data)
1031 1.1 christos {
1032 1.1 christos struct lwp_info *lp, *lpnext;
1033 1.1 christos
1034 1.1 christos for (lp = lwp_list; lp; lp = lpnext)
1035 1.1 christos {
1036 1.1 christos lpnext = lp->next;
1037 1.1 christos
1038 1.1 christos if (ptid_match (lp->ptid, filter))
1039 1.1 christos {
1040 1.1 christos if ((*callback) (lp, data))
1041 1.1 christos return lp;
1042 1.1 christos }
1043 1.1 christos }
1044 1.1 christos
1045 1.1 christos return NULL;
1046 1.1 christos }
1047 1.1 christos
1048 1.1 christos /* Update our internal state when changing from one checkpoint to
1049 1.1 christos another indicated by NEW_PTID. We can only switch single-threaded
1050 1.1 christos applications, so we only create one new LWP, and the previous list
1051 1.1 christos is discarded. */
1052 1.1 christos
1053 1.1 christos void
1054 1.1 christos linux_nat_switch_fork (ptid_t new_ptid)
1055 1.1 christos {
1056 1.1 christos struct lwp_info *lp;
1057 1.1 christos
1058 1.1 christos purge_lwp_list (ptid_get_pid (inferior_ptid));
1059 1.1 christos
1060 1.1 christos lp = add_lwp (new_ptid);
1061 1.1 christos lp->stopped = 1;
1062 1.1 christos
1063 1.1 christos /* This changes the thread's ptid while preserving the gdb thread
1064 1.1 christos num. Also changes the inferior pid, while preserving the
1065 1.1 christos inferior num. */
1066 1.1 christos thread_change_ptid (inferior_ptid, new_ptid);
1067 1.1 christos
1068 1.1 christos /* We've just told GDB core that the thread changed target id, but,
1069 1.1 christos in fact, it really is a different thread, with different register
1070 1.1 christos contents. */
1071 1.1 christos registers_changed ();
1072 1.1 christos }
1073 1.1 christos
1074 1.1 christos /* Handle the exit of a single thread LP. */
1075 1.1 christos
1076 1.1 christos static void
1077 1.1 christos exit_lwp (struct lwp_info *lp)
1078 1.1 christos {
1079 1.1 christos struct thread_info *th = find_thread_ptid (lp->ptid);
1080 1.1 christos
1081 1.1 christos if (th)
1082 1.1 christos {
1083 1.1 christos if (print_thread_events)
1084 1.1 christos printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
1085 1.1 christos
1086 1.1 christos delete_thread (lp->ptid);
1087 1.1 christos }
1088 1.1 christos
1089 1.1 christos delete_lwp (lp->ptid);
1090 1.1 christos }
1091 1.1 christos
1092 1.1 christos /* Wait for the LWP specified by LP, which we have just attached to.
1093 1.1 christos Returns a wait status for that LWP, to cache. */
1094 1.1 christos
1095 1.1 christos static int
1096 1.1 christos linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
1097 1.1 christos int *signalled)
1098 1.1 christos {
1099 1.1 christos pid_t new_pid, pid = ptid_get_lwp (ptid);
1100 1.1 christos int status;
1101 1.1 christos
1102 1.1 christos if (linux_proc_pid_is_stopped (pid))
1103 1.1 christos {
1104 1.1 christos if (debug_linux_nat)
1105 1.1 christos fprintf_unfiltered (gdb_stdlog,
1106 1.1 christos "LNPAW: Attaching to a stopped process\n");
1107 1.1 christos
1108 1.1 christos /* The process is definitely stopped. It is in a job control
1109 1.1 christos stop, unless the kernel predates the TASK_STOPPED /
1110 1.1 christos TASK_TRACED distinction, in which case it might be in a
1111 1.1 christos ptrace stop. Make sure it is in a ptrace stop; from there we
1112 1.1 christos can kill it, signal it, et cetera.
1113 1.1 christos
1114 1.1 christos First make sure there is a pending SIGSTOP. Since we are
1115 1.1 christos already attached, the process can not transition from stopped
1116 1.1 christos to running without a PTRACE_CONT; so we know this signal will
1117 1.1 christos go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1118 1.1 christos probably already in the queue (unless this kernel is old
1119 1.1 christos enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
1120 1.1 christos is not an RT signal, it can only be queued once. */
1121 1.1 christos kill_lwp (pid, SIGSTOP);
1122 1.1 christos
1123 1.1 christos /* Finally, resume the stopped process. This will deliver the SIGSTOP
1124 1.1 christos (or a higher priority signal, just like normal PTRACE_ATTACH). */
1125 1.1 christos ptrace (PTRACE_CONT, pid, 0, 0);
1126 1.1 christos }
1127 1.1 christos
1128 1.1 christos /* Make sure the initial process is stopped. The user-level threads
1129 1.1 christos layer might want to poke around in the inferior, and that won't
1130 1.1 christos work if things haven't stabilized yet. */
1131 1.1 christos new_pid = my_waitpid (pid, &status, 0);
1132 1.1 christos if (new_pid == -1 && errno == ECHILD)
1133 1.1 christos {
1134 1.1 christos if (first)
1135 1.1 christos warning (_("%s is a cloned process"), target_pid_to_str (ptid));
1136 1.1 christos
1137 1.1 christos /* Try again with __WCLONE to check cloned processes. */
1138 1.1 christos new_pid = my_waitpid (pid, &status, __WCLONE);
1139 1.1 christos *cloned = 1;
1140 1.1 christos }
1141 1.1 christos
1142 1.1 christos gdb_assert (pid == new_pid);
1143 1.1 christos
1144 1.1 christos if (!WIFSTOPPED (status))
1145 1.1 christos {
1146 1.1 christos /* The pid we tried to attach has apparently just exited. */
1147 1.1 christos if (debug_linux_nat)
1148 1.1 christos fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
1149 1.1 christos pid, status_to_str (status));
1150 1.1 christos return status;
1151 1.1 christos }
1152 1.1 christos
1153 1.1 christos if (WSTOPSIG (status) != SIGSTOP)
1154 1.1 christos {
1155 1.1 christos *signalled = 1;
1156 1.1 christos if (debug_linux_nat)
1157 1.1 christos fprintf_unfiltered (gdb_stdlog,
1158 1.1 christos "LNPAW: Received %s after attaching\n",
1159 1.1 christos status_to_str (status));
1160 1.1 christos }
1161 1.1 christos
1162 1.1 christos return status;
1163 1.1 christos }
1164 1.1 christos
1165 1.1 christos /* Attach to the LWP specified by PID. Return 0 if successful, -1 if
1166 1.1 christos the new LWP could not be attached, or 1 if we're already auto
1167 1.1 christos attached to this thread, but haven't processed the
1168 1.1 christos PTRACE_EVENT_CLONE event of its parent thread, so we just ignore
1169 1.1 christos its existance, without considering it an error. */
1170 1.1 christos
1171 1.1 christos int
1172 1.1 christos lin_lwp_attach_lwp (ptid_t ptid)
1173 1.1 christos {
1174 1.1 christos struct lwp_info *lp;
1175 1.1 christos int lwpid;
1176 1.1 christos
1177 1.1 christos gdb_assert (ptid_lwp_p (ptid));
1178 1.1 christos
1179 1.1 christos lp = find_lwp_pid (ptid);
1180 1.1 christos lwpid = ptid_get_lwp (ptid);
1181 1.1 christos
1182 1.1 christos /* We assume that we're already attached to any LWP that has an id
1183 1.1 christos equal to the overall process id, and to any LWP that is already
1184 1.1 christos in our list of LWPs. If we're not seeing exit events from threads
1185 1.1 christos and we've had PID wraparound since we last tried to stop all threads,
1186 1.1 christos this assumption might be wrong; fortunately, this is very unlikely
1187 1.1 christos to happen. */
1188 1.1 christos if (lwpid != ptid_get_pid (ptid) && lp == NULL)
1189 1.1 christos {
1190 1.1 christos int status, cloned = 0, signalled = 0;
1191 1.1 christos
1192 1.1 christos if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
1193 1.1 christos {
1194 1.1 christos if (linux_supports_tracefork ())
1195 1.1 christos {
1196 1.1 christos /* If we haven't stopped all threads when we get here,
1197 1.1 christos we may have seen a thread listed in thread_db's list,
1198 1.1 christos but not processed the PTRACE_EVENT_CLONE yet. If
1199 1.1 christos that's the case, ignore this new thread, and let
1200 1.1 christos normal event handling discover it later. */
1201 1.1 christos if (in_pid_list_p (stopped_pids, lwpid))
1202 1.1 christos {
1203 1.1 christos /* We've already seen this thread stop, but we
1204 1.1 christos haven't seen the PTRACE_EVENT_CLONE extended
1205 1.1 christos event yet. */
1206 1.1 christos return 0;
1207 1.1 christos }
1208 1.1 christos else
1209 1.1 christos {
1210 1.1 christos int new_pid;
1211 1.1 christos int status;
1212 1.1 christos
1213 1.1 christos /* See if we've got a stop for this new child
1214 1.1 christos pending. If so, we're already attached. */
1215 1.1 christos new_pid = my_waitpid (lwpid, &status, WNOHANG);
1216 1.1 christos if (new_pid == -1 && errno == ECHILD)
1217 1.1 christos new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
1218 1.1 christos if (new_pid != -1)
1219 1.1 christos {
1220 1.1 christos if (WIFSTOPPED (status))
1221 1.1 christos add_to_pid_list (&stopped_pids, lwpid, status);
1222 1.1 christos return 1;
1223 1.1 christos }
1224 1.1 christos }
1225 1.1 christos }
1226 1.1 christos
1227 1.1 christos /* If we fail to attach to the thread, issue a warning,
1228 1.1 christos but continue. One way this can happen is if thread
1229 1.1 christos creation is interrupted; as of Linux kernel 2.6.19, a
1230 1.1 christos bug may place threads in the thread list and then fail
1231 1.1 christos to create them. */
1232 1.1 christos warning (_("Can't attach %s: %s"), target_pid_to_str (ptid),
1233 1.1 christos safe_strerror (errno));
1234 1.1 christos return -1;
1235 1.1 christos }
1236 1.1 christos
1237 1.1 christos if (debug_linux_nat)
1238 1.1 christos fprintf_unfiltered (gdb_stdlog,
1239 1.1 christos "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1240 1.1 christos target_pid_to_str (ptid));
1241 1.1 christos
1242 1.1 christos status = linux_nat_post_attach_wait (ptid, 0, &cloned, &signalled);
1243 1.1 christos if (!WIFSTOPPED (status))
1244 1.1 christos return 1;
1245 1.1 christos
1246 1.1 christos lp = add_lwp (ptid);
1247 1.1 christos lp->stopped = 1;
1248 1.1 christos lp->cloned = cloned;
1249 1.1 christos lp->signalled = signalled;
1250 1.1 christos if (WSTOPSIG (status) != SIGSTOP)
1251 1.1 christos {
1252 1.1 christos lp->resumed = 1;
1253 1.1 christos lp->status = status;
1254 1.1 christos }
1255 1.1 christos
1256 1.1 christos target_post_attach (ptid_get_lwp (lp->ptid));
1257 1.1 christos
1258 1.1 christos if (debug_linux_nat)
1259 1.1 christos {
1260 1.1 christos fprintf_unfiltered (gdb_stdlog,
1261 1.1 christos "LLAL: waitpid %s received %s\n",
1262 1.1 christos target_pid_to_str (ptid),
1263 1.1 christos status_to_str (status));
1264 1.1 christos }
1265 1.1 christos }
1266 1.1 christos else
1267 1.1 christos {
1268 1.1 christos /* We assume that the LWP representing the original process is
1269 1.1 christos already stopped. Mark it as stopped in the data structure
1270 1.1 christos that the GNU/linux ptrace layer uses to keep track of
1271 1.1 christos threads. Note that this won't have already been done since
1272 1.1 christos the main thread will have, we assume, been stopped by an
1273 1.1 christos attach from a different layer. */
1274 1.1 christos if (lp == NULL)
1275 1.1 christos lp = add_lwp (ptid);
1276 1.1 christos lp->stopped = 1;
1277 1.1 christos }
1278 1.1 christos
1279 1.1 christos lp->last_resume_kind = resume_stop;
1280 1.1 christos return 0;
1281 1.1 christos }
1282 1.1 christos
1283 1.1 christos static void
1284 1.1 christos linux_nat_create_inferior (struct target_ops *ops,
1285 1.1 christos char *exec_file, char *allargs, char **env,
1286 1.1 christos int from_tty)
1287 1.1 christos {
1288 1.1 christos #ifdef HAVE_PERSONALITY
1289 1.1 christos int personality_orig = 0, personality_set = 0;
1290 1.1 christos #endif /* HAVE_PERSONALITY */
1291 1.1 christos
1292 1.1 christos /* The fork_child mechanism is synchronous and calls target_wait, so
1293 1.1 christos we have to mask the async mode. */
1294 1.1 christos
1295 1.1 christos #ifdef HAVE_PERSONALITY
1296 1.1 christos if (disable_randomization)
1297 1.1 christos {
1298 1.1 christos errno = 0;
1299 1.1 christos personality_orig = personality (0xffffffff);
1300 1.1 christos if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
1301 1.1 christos {
1302 1.1 christos personality_set = 1;
1303 1.1 christos personality (personality_orig | ADDR_NO_RANDOMIZE);
1304 1.1 christos }
1305 1.1 christos if (errno != 0 || (personality_set
1306 1.1 christos && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
1307 1.1 christos warning (_("Error disabling address space randomization: %s"),
1308 1.1 christos safe_strerror (errno));
1309 1.1 christos }
1310 1.1 christos #endif /* HAVE_PERSONALITY */
1311 1.1 christos
1312 1.1 christos /* Make sure we report all signals during startup. */
1313 1.1 christos linux_nat_pass_signals (0, NULL);
1314 1.1 christos
1315 1.1 christos linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
1316 1.1 christos
1317 1.1 christos #ifdef HAVE_PERSONALITY
1318 1.1 christos if (personality_set)
1319 1.1 christos {
1320 1.1 christos errno = 0;
1321 1.1 christos personality (personality_orig);
1322 1.1 christos if (errno != 0)
1323 1.1 christos warning (_("Error restoring address space randomization: %s"),
1324 1.1 christos safe_strerror (errno));
1325 1.1 christos }
1326 1.1 christos #endif /* HAVE_PERSONALITY */
1327 1.1 christos }
1328 1.1 christos
1329 1.1 christos static void
1330 1.1 christos linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
1331 1.1 christos {
1332 1.1 christos struct lwp_info *lp;
1333 1.1 christos int status;
1334 1.1 christos ptid_t ptid;
1335 1.1 christos volatile struct gdb_exception ex;
1336 1.1 christos
1337 1.1 christos /* Make sure we report all signals during attach. */
1338 1.1 christos linux_nat_pass_signals (0, NULL);
1339 1.1 christos
1340 1.1 christos TRY_CATCH (ex, RETURN_MASK_ERROR)
1341 1.1 christos {
1342 1.1 christos linux_ops->to_attach (ops, args, from_tty);
1343 1.1 christos }
1344 1.1 christos if (ex.reason < 0)
1345 1.1 christos {
1346 1.1 christos pid_t pid = parse_pid_to_attach (args);
1347 1.1 christos struct buffer buffer;
1348 1.1 christos char *message, *buffer_s;
1349 1.1 christos
1350 1.1 christos message = xstrdup (ex.message);
1351 1.1 christos make_cleanup (xfree, message);
1352 1.1 christos
1353 1.1 christos buffer_init (&buffer);
1354 1.1 christos linux_ptrace_attach_warnings (pid, &buffer);
1355 1.1 christos
1356 1.1 christos buffer_grow_str0 (&buffer, "");
1357 1.1 christos buffer_s = buffer_finish (&buffer);
1358 1.1 christos make_cleanup (xfree, buffer_s);
1359 1.1 christos
1360 1.1 christos throw_error (ex.error, "%s%s", buffer_s, message);
1361 1.1 christos }
1362 1.1 christos
1363 1.1 christos /* The ptrace base target adds the main thread with (pid,0,0)
1364 1.1 christos format. Decorate it with lwp info. */
1365 1.1 christos ptid = ptid_build (ptid_get_pid (inferior_ptid),
1366 1.1 christos ptid_get_pid (inferior_ptid),
1367 1.1 christos 0);
1368 1.1 christos thread_change_ptid (inferior_ptid, ptid);
1369 1.1 christos
1370 1.1 christos /* Add the initial process as the first LWP to the list. */
1371 1.1 christos lp = add_initial_lwp (ptid);
1372 1.1 christos
1373 1.1 christos status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
1374 1.1 christos &lp->signalled);
1375 1.1 christos if (!WIFSTOPPED (status))
1376 1.1 christos {
1377 1.1 christos if (WIFEXITED (status))
1378 1.1 christos {
1379 1.1 christos int exit_code = WEXITSTATUS (status);
1380 1.1 christos
1381 1.1 christos target_terminal_ours ();
1382 1.1 christos target_mourn_inferior ();
1383 1.1 christos if (exit_code == 0)
1384 1.1 christos error (_("Unable to attach: program exited normally."));
1385 1.1 christos else
1386 1.1 christos error (_("Unable to attach: program exited with code %d."),
1387 1.1 christos exit_code);
1388 1.1 christos }
1389 1.1 christos else if (WIFSIGNALED (status))
1390 1.1 christos {
1391 1.1 christos enum gdb_signal signo;
1392 1.1 christos
1393 1.1 christos target_terminal_ours ();
1394 1.1 christos target_mourn_inferior ();
1395 1.1 christos
1396 1.1 christos signo = gdb_signal_from_host (WTERMSIG (status));
1397 1.1 christos error (_("Unable to attach: program terminated with signal "
1398 1.1 christos "%s, %s."),
1399 1.1 christos gdb_signal_to_name (signo),
1400 1.1 christos gdb_signal_to_string (signo));
1401 1.1 christos }
1402 1.1 christos
1403 1.1 christos internal_error (__FILE__, __LINE__,
1404 1.1 christos _("unexpected status %d for PID %ld"),
1405 1.1 christos status, (long) ptid_get_lwp (ptid));
1406 1.1 christos }
1407 1.1 christos
1408 1.1 christos lp->stopped = 1;
1409 1.1 christos
1410 1.1 christos /* Save the wait status to report later. */
1411 1.1 christos lp->resumed = 1;
1412 1.1 christos if (debug_linux_nat)
1413 1.1 christos fprintf_unfiltered (gdb_stdlog,
1414 1.1 christos "LNA: waitpid %ld, saving status %s\n",
1415 1.1 christos (long) ptid_get_pid (lp->ptid), status_to_str (status));
1416 1.1 christos
1417 1.1 christos lp->status = status;
1418 1.1 christos
1419 1.1 christos if (target_can_async_p ())
1420 1.1 christos target_async (inferior_event_handler, 0);
1421 1.1 christos }
1422 1.1 christos
1423 1.1 christos /* Get pending status of LP. */
1424 1.1 christos static int
1425 1.1 christos get_pending_status (struct lwp_info *lp, int *status)
1426 1.1 christos {
1427 1.1 christos enum gdb_signal signo = GDB_SIGNAL_0;
1428 1.1 christos
1429 1.1 christos /* If we paused threads momentarily, we may have stored pending
1430 1.1 christos events in lp->status or lp->waitstatus (see stop_wait_callback),
1431 1.1 christos and GDB core hasn't seen any signal for those threads.
1432 1.1 christos Otherwise, the last signal reported to the core is found in the
1433 1.1 christos thread object's stop_signal.
1434 1.1 christos
1435 1.1 christos There's a corner case that isn't handled here at present. Only
1436 1.1 christos if the thread stopped with a TARGET_WAITKIND_STOPPED does
1437 1.1 christos stop_signal make sense as a real signal to pass to the inferior.
1438 1.1 christos Some catchpoint related events, like
1439 1.1 christos TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
1440 1.1 christos to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
1441 1.1 christos those traps are debug API (ptrace in our case) related and
1442 1.1 christos induced; the inferior wouldn't see them if it wasn't being
1443 1.1 christos traced. Hence, we should never pass them to the inferior, even
1444 1.1 christos when set to pass state. Since this corner case isn't handled by
1445 1.1 christos infrun.c when proceeding with a signal, for consistency, neither
1446 1.1 christos do we handle it here (or elsewhere in the file we check for
1447 1.1 christos signal pass state). Normally SIGTRAP isn't set to pass state, so
1448 1.1 christos this is really a corner case. */
1449 1.1 christos
1450 1.1 christos if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1451 1.1 christos signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
1452 1.1 christos else if (lp->status)
1453 1.1 christos signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1454 1.1 christos else if (non_stop && !is_executing (lp->ptid))
1455 1.1 christos {
1456 1.1 christos struct thread_info *tp = find_thread_ptid (lp->ptid);
1457 1.1 christos
1458 1.1 christos signo = tp->suspend.stop_signal;
1459 1.1 christos }
1460 1.1 christos else if (!non_stop)
1461 1.1 christos {
1462 1.1 christos struct target_waitstatus last;
1463 1.1 christos ptid_t last_ptid;
1464 1.1 christos
1465 1.1 christos get_last_target_status (&last_ptid, &last);
1466 1.1 christos
1467 1.1 christos if (ptid_get_lwp (lp->ptid) == ptid_get_lwp (last_ptid))
1468 1.1 christos {
1469 1.1 christos struct thread_info *tp = find_thread_ptid (lp->ptid);
1470 1.1 christos
1471 1.1 christos signo = tp->suspend.stop_signal;
1472 1.1 christos }
1473 1.1 christos }
1474 1.1 christos
1475 1.1 christos *status = 0;
1476 1.1 christos
1477 1.1 christos if (signo == GDB_SIGNAL_0)
1478 1.1 christos {
1479 1.1 christos if (debug_linux_nat)
1480 1.1 christos fprintf_unfiltered (gdb_stdlog,
1481 1.1 christos "GPT: lwp %s has no pending signal\n",
1482 1.1 christos target_pid_to_str (lp->ptid));
1483 1.1 christos }
1484 1.1 christos else if (!signal_pass_state (signo))
1485 1.1 christos {
1486 1.1 christos if (debug_linux_nat)
1487 1.1 christos fprintf_unfiltered (gdb_stdlog,
1488 1.1 christos "GPT: lwp %s had signal %s, "
1489 1.1 christos "but it is in no pass state\n",
1490 1.1 christos target_pid_to_str (lp->ptid),
1491 1.1 christos gdb_signal_to_string (signo));
1492 1.1 christos }
1493 1.1 christos else
1494 1.1 christos {
1495 1.1 christos *status = W_STOPCODE (gdb_signal_to_host (signo));
1496 1.1 christos
1497 1.1 christos if (debug_linux_nat)
1498 1.1 christos fprintf_unfiltered (gdb_stdlog,
1499 1.1 christos "GPT: lwp %s has pending signal %s\n",
1500 1.1 christos target_pid_to_str (lp->ptid),
1501 1.1 christos gdb_signal_to_string (signo));
1502 1.1 christos }
1503 1.1 christos
1504 1.1 christos return 0;
1505 1.1 christos }
1506 1.1 christos
1507 1.1 christos static int
1508 1.1 christos detach_callback (struct lwp_info *lp, void *data)
1509 1.1 christos {
1510 1.1 christos gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1511 1.1 christos
1512 1.1 christos if (debug_linux_nat && lp->status)
1513 1.1 christos fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1514 1.1 christos strsignal (WSTOPSIG (lp->status)),
1515 1.1 christos target_pid_to_str (lp->ptid));
1516 1.1 christos
1517 1.1 christos /* If there is a pending SIGSTOP, get rid of it. */
1518 1.1 christos if (lp->signalled)
1519 1.1 christos {
1520 1.1 christos if (debug_linux_nat)
1521 1.1 christos fprintf_unfiltered (gdb_stdlog,
1522 1.1 christos "DC: Sending SIGCONT to %s\n",
1523 1.1 christos target_pid_to_str (lp->ptid));
1524 1.1 christos
1525 1.1 christos kill_lwp (ptid_get_lwp (lp->ptid), SIGCONT);
1526 1.1 christos lp->signalled = 0;
1527 1.1 christos }
1528 1.1 christos
1529 1.1 christos /* We don't actually detach from the LWP that has an id equal to the
1530 1.1 christos overall process id just yet. */
1531 1.1 christos if (ptid_get_lwp (lp->ptid) != ptid_get_pid (lp->ptid))
1532 1.1 christos {
1533 1.1 christos int status = 0;
1534 1.1 christos
1535 1.1 christos /* Pass on any pending signal for this LWP. */
1536 1.1 christos get_pending_status (lp, &status);
1537 1.1 christos
1538 1.1 christos if (linux_nat_prepare_to_resume != NULL)
1539 1.1 christos linux_nat_prepare_to_resume (lp);
1540 1.1 christos errno = 0;
1541 1.1 christos if (ptrace (PTRACE_DETACH, ptid_get_lwp (lp->ptid), 0,
1542 1.1 christos WSTOPSIG (status)) < 0)
1543 1.1 christos error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1544 1.1 christos safe_strerror (errno));
1545 1.1 christos
1546 1.1 christos if (debug_linux_nat)
1547 1.1 christos fprintf_unfiltered (gdb_stdlog,
1548 1.1 christos "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1549 1.1 christos target_pid_to_str (lp->ptid),
1550 1.1 christos strsignal (WSTOPSIG (status)));
1551 1.1 christos
1552 1.1 christos delete_lwp (lp->ptid);
1553 1.1 christos }
1554 1.1 christos
1555 1.1 christos return 0;
1556 1.1 christos }
1557 1.1 christos
1558 1.1 christos static void
1559 1.1 christos linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
1560 1.1 christos {
1561 1.1 christos int pid;
1562 1.1 christos int status;
1563 1.1 christos struct lwp_info *main_lwp;
1564 1.1 christos
1565 1.1 christos pid = ptid_get_pid (inferior_ptid);
1566 1.1 christos
1567 1.1 christos /* Don't unregister from the event loop, as there may be other
1568 1.1 christos inferiors running. */
1569 1.1 christos
1570 1.1 christos /* Stop all threads before detaching. ptrace requires that the
1571 1.1 christos thread is stopped to sucessfully detach. */
1572 1.1 christos iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
1573 1.1 christos /* ... and wait until all of them have reported back that
1574 1.1 christos they're no longer running. */
1575 1.1 christos iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
1576 1.1 christos
1577 1.1 christos iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
1578 1.1 christos
1579 1.1 christos /* Only the initial process should be left right now. */
1580 1.1 christos gdb_assert (num_lwps (ptid_get_pid (inferior_ptid)) == 1);
1581 1.1 christos
1582 1.1 christos main_lwp = find_lwp_pid (pid_to_ptid (pid));
1583 1.1 christos
1584 1.1 christos /* Pass on any pending signal for the last LWP. */
1585 1.1 christos if ((args == NULL || *args == '\0')
1586 1.1 christos && get_pending_status (main_lwp, &status) != -1
1587 1.1 christos && WIFSTOPPED (status))
1588 1.1 christos {
1589 1.1 christos char *tem;
1590 1.1 christos
1591 1.1 christos /* Put the signal number in ARGS so that inf_ptrace_detach will
1592 1.1 christos pass it along with PTRACE_DETACH. */
1593 1.1 christos tem = alloca (8);
1594 1.1 christos xsnprintf (tem, 8, "%d", (int) WSTOPSIG (status));
1595 1.1 christos args = tem;
1596 1.1 christos if (debug_linux_nat)
1597 1.1 christos fprintf_unfiltered (gdb_stdlog,
1598 1.1 christos "LND: Sending signal %s to %s\n",
1599 1.1 christos args,
1600 1.1 christos target_pid_to_str (main_lwp->ptid));
1601 1.1 christos }
1602 1.1 christos
1603 1.1 christos if (linux_nat_prepare_to_resume != NULL)
1604 1.1 christos linux_nat_prepare_to_resume (main_lwp);
1605 1.1 christos delete_lwp (main_lwp->ptid);
1606 1.1 christos
1607 1.1 christos if (forks_exist_p ())
1608 1.1 christos {
1609 1.1 christos /* Multi-fork case. The current inferior_ptid is being detached
1610 1.1 christos from, but there are other viable forks to debug. Detach from
1611 1.1 christos the current fork, and context-switch to the first
1612 1.1 christos available. */
1613 1.1 christos linux_fork_detach (args, from_tty);
1614 1.1 christos }
1615 1.1 christos else
1616 1.1 christos linux_ops->to_detach (ops, args, from_tty);
1617 1.1 christos }
1618 1.1 christos
1619 1.1 christos /* Resume LP. */
1620 1.1 christos
1621 1.1 christos static void
1622 1.1 christos resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
1623 1.1 christos {
1624 1.1 christos if (lp->stopped)
1625 1.1 christos {
1626 1.1 christos struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
1627 1.1 christos
1628 1.1 christos if (inf->vfork_child != NULL)
1629 1.1 christos {
1630 1.1 christos if (debug_linux_nat)
1631 1.1 christos fprintf_unfiltered (gdb_stdlog,
1632 1.1 christos "RC: Not resuming %s (vfork parent)\n",
1633 1.1 christos target_pid_to_str (lp->ptid));
1634 1.1 christos }
1635 1.1 christos else if (lp->status == 0
1636 1.1 christos && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
1637 1.1 christos {
1638 1.1 christos if (debug_linux_nat)
1639 1.1 christos fprintf_unfiltered (gdb_stdlog,
1640 1.1 christos "RC: Resuming sibling %s, %s, %s\n",
1641 1.1 christos target_pid_to_str (lp->ptid),
1642 1.1 christos (signo != GDB_SIGNAL_0
1643 1.1 christos ? strsignal (gdb_signal_to_host (signo))
1644 1.1 christos : "0"),
1645 1.1 christos step ? "step" : "resume");
1646 1.1 christos
1647 1.1 christos if (linux_nat_prepare_to_resume != NULL)
1648 1.1 christos linux_nat_prepare_to_resume (lp);
1649 1.1 christos linux_ops->to_resume (linux_ops,
1650 1.1 christos pid_to_ptid (ptid_get_lwp (lp->ptid)),
1651 1.1 christos step, signo);
1652 1.1 christos lp->stopped = 0;
1653 1.1 christos lp->step = step;
1654 1.1 christos lp->stopped_by_watchpoint = 0;
1655 1.1 christos }
1656 1.1 christos else
1657 1.1 christos {
1658 1.1 christos if (debug_linux_nat)
1659 1.1 christos fprintf_unfiltered (gdb_stdlog,
1660 1.1 christos "RC: Not resuming sibling %s (has pending)\n",
1661 1.1 christos target_pid_to_str (lp->ptid));
1662 1.1 christos }
1663 1.1 christos }
1664 1.1 christos else
1665 1.1 christos {
1666 1.1 christos if (debug_linux_nat)
1667 1.1 christos fprintf_unfiltered (gdb_stdlog,
1668 1.1 christos "RC: Not resuming sibling %s (not stopped)\n",
1669 1.1 christos target_pid_to_str (lp->ptid));
1670 1.1 christos }
1671 1.1 christos }
1672 1.1 christos
1673 1.1 christos /* Resume LWP, with the last stop signal, if it is in pass state. */
1674 1.1 christos
1675 1.1 christos static int
1676 1.1 christos linux_nat_resume_callback (struct lwp_info *lp, void *data)
1677 1.1 christos {
1678 1.1 christos enum gdb_signal signo = GDB_SIGNAL_0;
1679 1.1 christos
1680 1.1 christos if (lp->stopped)
1681 1.1 christos {
1682 1.1 christos struct thread_info *thread;
1683 1.1 christos
1684 1.1 christos thread = find_thread_ptid (lp->ptid);
1685 1.1 christos if (thread != NULL)
1686 1.1 christos {
1687 1.1 christos if (signal_pass_state (thread->suspend.stop_signal))
1688 1.1 christos signo = thread->suspend.stop_signal;
1689 1.1 christos thread->suspend.stop_signal = GDB_SIGNAL_0;
1690 1.1 christos }
1691 1.1 christos }
1692 1.1 christos
1693 1.1 christos resume_lwp (lp, 0, signo);
1694 1.1 christos return 0;
1695 1.1 christos }
1696 1.1 christos
1697 1.1 christos static int
1698 1.1 christos resume_clear_callback (struct lwp_info *lp, void *data)
1699 1.1 christos {
1700 1.1 christos lp->resumed = 0;
1701 1.1 christos lp->last_resume_kind = resume_stop;
1702 1.1 christos return 0;
1703 1.1 christos }
1704 1.1 christos
1705 1.1 christos static int
1706 1.1 christos resume_set_callback (struct lwp_info *lp, void *data)
1707 1.1 christos {
1708 1.1 christos lp->resumed = 1;
1709 1.1 christos lp->last_resume_kind = resume_continue;
1710 1.1 christos return 0;
1711 1.1 christos }
1712 1.1 christos
1713 1.1 christos static void
1714 1.1 christos linux_nat_resume (struct target_ops *ops,
1715 1.1 christos ptid_t ptid, int step, enum gdb_signal signo)
1716 1.1 christos {
1717 1.1 christos struct lwp_info *lp;
1718 1.1 christos int resume_many;
1719 1.1 christos
1720 1.1 christos if (debug_linux_nat)
1721 1.1 christos fprintf_unfiltered (gdb_stdlog,
1722 1.1 christos "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1723 1.1 christos step ? "step" : "resume",
1724 1.1 christos target_pid_to_str (ptid),
1725 1.1 christos (signo != GDB_SIGNAL_0
1726 1.1 christos ? strsignal (gdb_signal_to_host (signo)) : "0"),
1727 1.1 christos target_pid_to_str (inferior_ptid));
1728 1.1 christos
1729 1.1 christos /* A specific PTID means `step only this process id'. */
1730 1.1 christos resume_many = (ptid_equal (minus_one_ptid, ptid)
1731 1.1 christos || ptid_is_pid (ptid));
1732 1.1 christos
1733 1.1 christos /* Mark the lwps we're resuming as resumed. */
1734 1.1 christos iterate_over_lwps (ptid, resume_set_callback, NULL);
1735 1.1 christos
1736 1.1 christos /* See if it's the current inferior that should be handled
1737 1.1 christos specially. */
1738 1.1 christos if (resume_many)
1739 1.1 christos lp = find_lwp_pid (inferior_ptid);
1740 1.1 christos else
1741 1.1 christos lp = find_lwp_pid (ptid);
1742 1.1 christos gdb_assert (lp != NULL);
1743 1.1 christos
1744 1.1 christos /* Remember if we're stepping. */
1745 1.1 christos lp->step = step;
1746 1.1 christos lp->last_resume_kind = step ? resume_step : resume_continue;
1747 1.1 christos
1748 1.1 christos /* If we have a pending wait status for this thread, there is no
1749 1.1 christos point in resuming the process. But first make sure that
1750 1.1 christos linux_nat_wait won't preemptively handle the event - we
1751 1.1 christos should never take this short-circuit if we are going to
1752 1.1 christos leave LP running, since we have skipped resuming all the
1753 1.1 christos other threads. This bit of code needs to be synchronized
1754 1.1 christos with linux_nat_wait. */
1755 1.1 christos
1756 1.1 christos if (lp->status && WIFSTOPPED (lp->status))
1757 1.1 christos {
1758 1.1 christos if (!lp->step
1759 1.1 christos && WSTOPSIG (lp->status)
1760 1.1 christos && sigismember (&pass_mask, WSTOPSIG (lp->status)))
1761 1.1 christos {
1762 1.1 christos if (debug_linux_nat)
1763 1.1 christos fprintf_unfiltered (gdb_stdlog,
1764 1.1 christos "LLR: Not short circuiting for ignored "
1765 1.1 christos "status 0x%x\n", lp->status);
1766 1.1 christos
1767 1.1 christos /* FIXME: What should we do if we are supposed to continue
1768 1.1 christos this thread with a signal? */
1769 1.1 christos gdb_assert (signo == GDB_SIGNAL_0);
1770 1.1 christos signo = gdb_signal_from_host (WSTOPSIG (lp->status));
1771 1.1 christos lp->status = 0;
1772 1.1 christos }
1773 1.1 christos }
1774 1.1 christos
1775 1.1 christos if (lp->status || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
1776 1.1 christos {
1777 1.1 christos /* FIXME: What should we do if we are supposed to continue
1778 1.1 christos this thread with a signal? */
1779 1.1 christos gdb_assert (signo == GDB_SIGNAL_0);
1780 1.1 christos
1781 1.1 christos if (debug_linux_nat)
1782 1.1 christos fprintf_unfiltered (gdb_stdlog,
1783 1.1 christos "LLR: Short circuiting for status 0x%x\n",
1784 1.1 christos lp->status);
1785 1.1 christos
1786 1.1 christos if (target_can_async_p ())
1787 1.1 christos {
1788 1.1 christos target_async (inferior_event_handler, 0);
1789 1.1 christos /* Tell the event loop we have something to process. */
1790 1.1 christos async_file_mark ();
1791 1.1 christos }
1792 1.1 christos return;
1793 1.1 christos }
1794 1.1 christos
1795 1.1 christos /* Mark LWP as not stopped to prevent it from being continued by
1796 1.1 christos linux_nat_resume_callback. */
1797 1.1 christos lp->stopped = 0;
1798 1.1 christos
1799 1.1 christos if (resume_many)
1800 1.1 christos iterate_over_lwps (ptid, linux_nat_resume_callback, NULL);
1801 1.1 christos
1802 1.1 christos /* Convert to something the lower layer understands. */
1803 1.1 christos ptid = pid_to_ptid (ptid_get_lwp (lp->ptid));
1804 1.1 christos
1805 1.1 christos if (linux_nat_prepare_to_resume != NULL)
1806 1.1 christos linux_nat_prepare_to_resume (lp);
1807 1.1 christos linux_ops->to_resume (linux_ops, ptid, step, signo);
1808 1.1 christos lp->stopped_by_watchpoint = 0;
1809 1.1 christos
1810 1.1 christos if (debug_linux_nat)
1811 1.1 christos fprintf_unfiltered (gdb_stdlog,
1812 1.1 christos "LLR: %s %s, %s (resume event thread)\n",
1813 1.1 christos step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1814 1.1 christos target_pid_to_str (ptid),
1815 1.1 christos (signo != GDB_SIGNAL_0
1816 1.1 christos ? strsignal (gdb_signal_to_host (signo)) : "0"));
1817 1.1 christos
1818 1.1 christos if (target_can_async_p ())
1819 1.1 christos target_async (inferior_event_handler, 0);
1820 1.1 christos }
1821 1.1 christos
1822 1.1 christos /* Send a signal to an LWP. */
1823 1.1 christos
1824 1.1 christos static int
1825 1.1 christos kill_lwp (int lwpid, int signo)
1826 1.1 christos {
1827 1.1 christos /* Use tkill, if possible, in case we are using nptl threads. If tkill
1828 1.1 christos fails, then we are not using nptl threads and we should be using kill. */
1829 1.1 christos
1830 1.1 christos #ifdef HAVE_TKILL_SYSCALL
1831 1.1 christos {
1832 1.1 christos static int tkill_failed;
1833 1.1 christos
1834 1.1 christos if (!tkill_failed)
1835 1.1 christos {
1836 1.1 christos int ret;
1837 1.1 christos
1838 1.1 christos errno = 0;
1839 1.1 christos ret = syscall (__NR_tkill, lwpid, signo);
1840 1.1 christos if (errno != ENOSYS)
1841 1.1 christos return ret;
1842 1.1 christos tkill_failed = 1;
1843 1.1 christos }
1844 1.1 christos }
1845 1.1 christos #endif
1846 1.1 christos
1847 1.1 christos return kill (lwpid, signo);
1848 1.1 christos }
1849 1.1 christos
1850 1.1 christos /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
1851 1.1 christos event, check if the core is interested in it: if not, ignore the
1852 1.1 christos event, and keep waiting; otherwise, we need to toggle the LWP's
1853 1.1 christos syscall entry/exit status, since the ptrace event itself doesn't
1854 1.1 christos indicate it, and report the trap to higher layers. */
1855 1.1 christos
1856 1.1 christos static int
1857 1.1 christos linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
1858 1.1 christos {
1859 1.1 christos struct target_waitstatus *ourstatus = &lp->waitstatus;
1860 1.1 christos struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
1861 1.1 christos int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
1862 1.1 christos
1863 1.1 christos if (stopping)
1864 1.1 christos {
1865 1.1 christos /* If we're stopping threads, there's a SIGSTOP pending, which
1866 1.1 christos makes it so that the LWP reports an immediate syscall return,
1867 1.1 christos followed by the SIGSTOP. Skip seeing that "return" using
1868 1.1 christos PTRACE_CONT directly, and let stop_wait_callback collect the
1869 1.1 christos SIGSTOP. Later when the thread is resumed, a new syscall
1870 1.1 christos entry event. If we didn't do this (and returned 0), we'd
1871 1.1 christos leave a syscall entry pending, and our caller, by using
1872 1.1 christos PTRACE_CONT to collect the SIGSTOP, skips the syscall return
1873 1.1 christos itself. Later, when the user re-resumes this LWP, we'd see
1874 1.1 christos another syscall entry event and we'd mistake it for a return.
1875 1.1 christos
1876 1.1 christos If stop_wait_callback didn't force the SIGSTOP out of the LWP
1877 1.1 christos (leaving immediately with LWP->signalled set, without issuing
1878 1.1 christos a PTRACE_CONT), it would still be problematic to leave this
1879 1.1 christos syscall enter pending, as later when the thread is resumed,
1880 1.1 christos it would then see the same syscall exit mentioned above,
1881 1.1 christos followed by the delayed SIGSTOP, while the syscall didn't
1882 1.1 christos actually get to execute. It seems it would be even more
1883 1.1 christos confusing to the user. */
1884 1.1 christos
1885 1.1 christos if (debug_linux_nat)
1886 1.1 christos fprintf_unfiltered (gdb_stdlog,
1887 1.1 christos "LHST: ignoring syscall %d "
1888 1.1 christos "for LWP %ld (stopping threads), "
1889 1.1 christos "resuming with PTRACE_CONT for SIGSTOP\n",
1890 1.1 christos syscall_number,
1891 1.1 christos ptid_get_lwp (lp->ptid));
1892 1.1 christos
1893 1.1 christos lp->syscall_state = TARGET_WAITKIND_IGNORE;
1894 1.1 christos ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
1895 1.1 christos return 1;
1896 1.1 christos }
1897 1.1 christos
1898 1.1 christos if (catch_syscall_enabled ())
1899 1.1 christos {
1900 1.1 christos /* Always update the entry/return state, even if this particular
1901 1.1 christos syscall isn't interesting to the core now. In async mode,
1902 1.1 christos the user could install a new catchpoint for this syscall
1903 1.1 christos between syscall enter/return, and we'll need to know to
1904 1.1 christos report a syscall return if that happens. */
1905 1.1 christos lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1906 1.1 christos ? TARGET_WAITKIND_SYSCALL_RETURN
1907 1.1 christos : TARGET_WAITKIND_SYSCALL_ENTRY);
1908 1.1 christos
1909 1.1 christos if (catching_syscall_number (syscall_number))
1910 1.1 christos {
1911 1.1 christos /* Alright, an event to report. */
1912 1.1 christos ourstatus->kind = lp->syscall_state;
1913 1.1 christos ourstatus->value.syscall_number = syscall_number;
1914 1.1 christos
1915 1.1 christos if (debug_linux_nat)
1916 1.1 christos fprintf_unfiltered (gdb_stdlog,
1917 1.1 christos "LHST: stopping for %s of syscall %d"
1918 1.1 christos " for LWP %ld\n",
1919 1.1 christos lp->syscall_state
1920 1.1 christos == TARGET_WAITKIND_SYSCALL_ENTRY
1921 1.1 christos ? "entry" : "return",
1922 1.1 christos syscall_number,
1923 1.1 christos ptid_get_lwp (lp->ptid));
1924 1.1 christos return 0;
1925 1.1 christos }
1926 1.1 christos
1927 1.1 christos if (debug_linux_nat)
1928 1.1 christos fprintf_unfiltered (gdb_stdlog,
1929 1.1 christos "LHST: ignoring %s of syscall %d "
1930 1.1 christos "for LWP %ld\n",
1931 1.1 christos lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
1932 1.1 christos ? "entry" : "return",
1933 1.1 christos syscall_number,
1934 1.1 christos ptid_get_lwp (lp->ptid));
1935 1.1 christos }
1936 1.1 christos else
1937 1.1 christos {
1938 1.1 christos /* If we had been syscall tracing, and hence used PT_SYSCALL
1939 1.1 christos before on this LWP, it could happen that the user removes all
1940 1.1 christos syscall catchpoints before we get to process this event.
1941 1.1 christos There are two noteworthy issues here:
1942 1.1 christos
1943 1.1 christos - When stopped at a syscall entry event, resuming with
1944 1.1 christos PT_STEP still resumes executing the syscall and reports a
1945 1.1 christos syscall return.
1946 1.1 christos
1947 1.1 christos - Only PT_SYSCALL catches syscall enters. If we last
1948 1.1 christos single-stepped this thread, then this event can't be a
1949 1.1 christos syscall enter. If we last single-stepped this thread, this
1950 1.1 christos has to be a syscall exit.
1951 1.1 christos
1952 1.1 christos The points above mean that the next resume, be it PT_STEP or
1953 1.1 christos PT_CONTINUE, can not trigger a syscall trace event. */
1954 1.1 christos if (debug_linux_nat)
1955 1.1 christos fprintf_unfiltered (gdb_stdlog,
1956 1.1 christos "LHST: caught syscall event "
1957 1.1 christos "with no syscall catchpoints."
1958 1.1 christos " %d for LWP %ld, ignoring\n",
1959 1.1 christos syscall_number,
1960 1.1 christos ptid_get_lwp (lp->ptid));
1961 1.1 christos lp->syscall_state = TARGET_WAITKIND_IGNORE;
1962 1.1 christos }
1963 1.1 christos
1964 1.1 christos /* The core isn't interested in this event. For efficiency, avoid
1965 1.1 christos stopping all threads only to have the core resume them all again.
1966 1.1 christos Since we're not stopping threads, if we're still syscall tracing
1967 1.1 christos and not stepping, we can't use PTRACE_CONT here, as we'd miss any
1968 1.1 christos subsequent syscall. Simply resume using the inf-ptrace layer,
1969 1.1 christos which knows when to use PT_SYSCALL or PT_CONTINUE. */
1970 1.1 christos
1971 1.1 christos /* Note that gdbarch_get_syscall_number may access registers, hence
1972 1.1 christos fill a regcache. */
1973 1.1 christos registers_changed ();
1974 1.1 christos if (linux_nat_prepare_to_resume != NULL)
1975 1.1 christos linux_nat_prepare_to_resume (lp);
1976 1.1 christos linux_ops->to_resume (linux_ops, pid_to_ptid (ptid_get_lwp (lp->ptid)),
1977 1.1 christos lp->step, GDB_SIGNAL_0);
1978 1.1 christos return 1;
1979 1.1 christos }
1980 1.1 christos
1981 1.1 christos /* Handle a GNU/Linux extended wait response. If we see a clone
1982 1.1 christos event, we need to add the new LWP to our list (and not report the
1983 1.1 christos trap to higher layers). This function returns non-zero if the
1984 1.1 christos event should be ignored and we should wait again. If STOPPING is
1985 1.1 christos true, the new LWP remains stopped, otherwise it is continued. */
1986 1.1 christos
1987 1.1 christos static int
1988 1.1 christos linux_handle_extended_wait (struct lwp_info *lp, int status,
1989 1.1 christos int stopping)
1990 1.1 christos {
1991 1.1 christos int pid = ptid_get_lwp (lp->ptid);
1992 1.1 christos struct target_waitstatus *ourstatus = &lp->waitstatus;
1993 1.1 christos int event = status >> 16;
1994 1.1 christos
1995 1.1 christos if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
1996 1.1 christos || event == PTRACE_EVENT_CLONE)
1997 1.1 christos {
1998 1.1 christos unsigned long new_pid;
1999 1.1 christos int ret;
2000 1.1 christos
2001 1.1 christos ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
2002 1.1 christos
2003 1.1 christos /* If we haven't already seen the new PID stop, wait for it now. */
2004 1.1 christos if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
2005 1.1 christos {
2006 1.1 christos /* The new child has a pending SIGSTOP. We can't affect it until it
2007 1.1 christos hits the SIGSTOP, but we're already attached. */
2008 1.1 christos ret = my_waitpid (new_pid, &status,
2009 1.1 christos (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
2010 1.1 christos if (ret == -1)
2011 1.1 christos perror_with_name (_("waiting for new child"));
2012 1.1 christos else if (ret != new_pid)
2013 1.1 christos internal_error (__FILE__, __LINE__,
2014 1.1 christos _("wait returned unexpected PID %d"), ret);
2015 1.1 christos else if (!WIFSTOPPED (status))
2016 1.1 christos internal_error (__FILE__, __LINE__,
2017 1.1 christos _("wait returned unexpected status 0x%x"), status);
2018 1.1 christos }
2019 1.1 christos
2020 1.1 christos ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
2021 1.1 christos
2022 1.1 christos if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
2023 1.1 christos {
2024 1.1 christos /* The arch-specific native code may need to know about new
2025 1.1 christos forks even if those end up never mapped to an
2026 1.1 christos inferior. */
2027 1.1 christos if (linux_nat_new_fork != NULL)
2028 1.1 christos linux_nat_new_fork (lp, new_pid);
2029 1.1 christos }
2030 1.1 christos
2031 1.1 christos if (event == PTRACE_EVENT_FORK
2032 1.1 christos && linux_fork_checkpointing_p (ptid_get_pid (lp->ptid)))
2033 1.1 christos {
2034 1.1 christos /* Handle checkpointing by linux-fork.c here as a special
2035 1.1 christos case. We don't want the follow-fork-mode or 'catch fork'
2036 1.1 christos to interfere with this. */
2037 1.1 christos
2038 1.1 christos /* This won't actually modify the breakpoint list, but will
2039 1.1 christos physically remove the breakpoints from the child. */
2040 1.1 christos detach_breakpoints (ptid_build (new_pid, new_pid, 0));
2041 1.1 christos
2042 1.1 christos /* Retain child fork in ptrace (stopped) state. */
2043 1.1 christos if (!find_fork_pid (new_pid))
2044 1.1 christos add_fork (new_pid);
2045 1.1 christos
2046 1.1 christos /* Report as spurious, so that infrun doesn't want to follow
2047 1.1 christos this fork. We're actually doing an infcall in
2048 1.1 christos linux-fork.c. */
2049 1.1 christos ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
2050 1.1 christos
2051 1.1 christos /* Report the stop to the core. */
2052 1.1 christos return 0;
2053 1.1 christos }
2054 1.1 christos
2055 1.1 christos if (event == PTRACE_EVENT_FORK)
2056 1.1 christos ourstatus->kind = TARGET_WAITKIND_FORKED;
2057 1.1 christos else if (event == PTRACE_EVENT_VFORK)
2058 1.1 christos ourstatus->kind = TARGET_WAITKIND_VFORKED;
2059 1.1 christos else
2060 1.1 christos {
2061 1.1 christos struct lwp_info *new_lp;
2062 1.1 christos
2063 1.1 christos ourstatus->kind = TARGET_WAITKIND_IGNORE;
2064 1.1 christos
2065 1.1 christos if (debug_linux_nat)
2066 1.1 christos fprintf_unfiltered (gdb_stdlog,
2067 1.1 christos "LHEW: Got clone event "
2068 1.1 christos "from LWP %d, new child is LWP %ld\n",
2069 1.1 christos pid, new_pid);
2070 1.1 christos
2071 1.1 christos new_lp = add_lwp (ptid_build (ptid_get_pid (lp->ptid), new_pid, 0));
2072 1.1 christos new_lp->cloned = 1;
2073 1.1 christos new_lp->stopped = 1;
2074 1.1 christos
2075 1.1 christos if (WSTOPSIG (status) != SIGSTOP)
2076 1.1 christos {
2077 1.1 christos /* This can happen if someone starts sending signals to
2078 1.1 christos the new thread before it gets a chance to run, which
2079 1.1 christos have a lower number than SIGSTOP (e.g. SIGUSR1).
2080 1.1 christos This is an unlikely case, and harder to handle for
2081 1.1 christos fork / vfork than for clone, so we do not try - but
2082 1.1 christos we handle it for clone events here. We'll send
2083 1.1 christos the other signal on to the thread below. */
2084 1.1 christos
2085 1.1 christos new_lp->signalled = 1;
2086 1.1 christos }
2087 1.1 christos else
2088 1.1 christos {
2089 1.1 christos struct thread_info *tp;
2090 1.1 christos
2091 1.1 christos /* When we stop for an event in some other thread, and
2092 1.1 christos pull the thread list just as this thread has cloned,
2093 1.1 christos we'll have seen the new thread in the thread_db list
2094 1.1 christos before handling the CLONE event (glibc's
2095 1.1 christos pthread_create adds the new thread to the thread list
2096 1.1 christos before clone'ing, and has the kernel fill in the
2097 1.1 christos thread's tid on the clone call with
2098 1.1 christos CLONE_PARENT_SETTID). If that happened, and the core
2099 1.1 christos had requested the new thread to stop, we'll have
2100 1.1 christos killed it with SIGSTOP. But since SIGSTOP is not an
2101 1.1 christos RT signal, it can only be queued once. We need to be
2102 1.1 christos careful to not resume the LWP if we wanted it to
2103 1.1 christos stop. In that case, we'll leave the SIGSTOP pending.
2104 1.1 christos It will later be reported as GDB_SIGNAL_0. */
2105 1.1 christos tp = find_thread_ptid (new_lp->ptid);
2106 1.1 christos if (tp != NULL && tp->stop_requested)
2107 1.1 christos new_lp->last_resume_kind = resume_stop;
2108 1.1 christos else
2109 1.1 christos status = 0;
2110 1.1 christos }
2111 1.1 christos
2112 1.1 christos if (non_stop)
2113 1.1 christos {
2114 1.1 christos /* Add the new thread to GDB's lists as soon as possible
2115 1.1 christos so that:
2116 1.1 christos
2117 1.1 christos 1) the frontend doesn't have to wait for a stop to
2118 1.1 christos display them, and,
2119 1.1 christos
2120 1.1 christos 2) we tag it with the correct running state. */
2121 1.1 christos
2122 1.1 christos /* If the thread_db layer is active, let it know about
2123 1.1 christos this new thread, and add it to GDB's list. */
2124 1.1 christos if (!thread_db_attach_lwp (new_lp->ptid))
2125 1.1 christos {
2126 1.1 christos /* We're not using thread_db. Add it to GDB's
2127 1.1 christos list. */
2128 1.1 christos target_post_attach (ptid_get_lwp (new_lp->ptid));
2129 1.1 christos add_thread (new_lp->ptid);
2130 1.1 christos }
2131 1.1 christos
2132 1.1 christos if (!stopping)
2133 1.1 christos {
2134 1.1 christos set_running (new_lp->ptid, 1);
2135 1.1 christos set_executing (new_lp->ptid, 1);
2136 1.1 christos /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
2137 1.1 christos resume_stop. */
2138 1.1 christos new_lp->last_resume_kind = resume_continue;
2139 1.1 christos }
2140 1.1 christos }
2141 1.1 christos
2142 1.1 christos if (status != 0)
2143 1.1 christos {
2144 1.1 christos /* We created NEW_LP so it cannot yet contain STATUS. */
2145 1.1 christos gdb_assert (new_lp->status == 0);
2146 1.1 christos
2147 1.1 christos /* Save the wait status to report later. */
2148 1.1 christos if (debug_linux_nat)
2149 1.1 christos fprintf_unfiltered (gdb_stdlog,
2150 1.1 christos "LHEW: waitpid of new LWP %ld, "
2151 1.1 christos "saving status %s\n",
2152 1.1 christos (long) ptid_get_lwp (new_lp->ptid),
2153 1.1 christos status_to_str (status));
2154 1.1 christos new_lp->status = status;
2155 1.1 christos }
2156 1.1 christos
2157 1.1 christos /* Note the need to use the low target ops to resume, to
2158 1.1 christos handle resuming with PT_SYSCALL if we have syscall
2159 1.1 christos catchpoints. */
2160 1.1 christos if (!stopping)
2161 1.1 christos {
2162 1.1 christos new_lp->resumed = 1;
2163 1.1 christos
2164 1.1 christos if (status == 0)
2165 1.1 christos {
2166 1.1 christos gdb_assert (new_lp->last_resume_kind == resume_continue);
2167 1.1 christos if (debug_linux_nat)
2168 1.1 christos fprintf_unfiltered (gdb_stdlog,
2169 1.1 christos "LHEW: resuming new LWP %ld\n",
2170 1.1 christos ptid_get_lwp (new_lp->ptid));
2171 1.1 christos if (linux_nat_prepare_to_resume != NULL)
2172 1.1 christos linux_nat_prepare_to_resume (new_lp);
2173 1.1 christos linux_ops->to_resume (linux_ops, pid_to_ptid (new_pid),
2174 1.1 christos 0, GDB_SIGNAL_0);
2175 1.1 christos new_lp->stopped = 0;
2176 1.1 christos }
2177 1.1 christos }
2178 1.1 christos
2179 1.1 christos if (debug_linux_nat)
2180 1.1 christos fprintf_unfiltered (gdb_stdlog,
2181 1.1 christos "LHEW: resuming parent LWP %d\n", pid);
2182 1.1 christos if (linux_nat_prepare_to_resume != NULL)
2183 1.1 christos linux_nat_prepare_to_resume (lp);
2184 1.1 christos linux_ops->to_resume (linux_ops,
2185 1.1 christos pid_to_ptid (ptid_get_lwp (lp->ptid)),
2186 1.1 christos 0, GDB_SIGNAL_0);
2187 1.1 christos
2188 1.1 christos return 1;
2189 1.1 christos }
2190 1.1 christos
2191 1.1 christos return 0;
2192 1.1 christos }
2193 1.1 christos
2194 1.1 christos if (event == PTRACE_EVENT_EXEC)
2195 1.1 christos {
2196 1.1 christos if (debug_linux_nat)
2197 1.1 christos fprintf_unfiltered (gdb_stdlog,
2198 1.1 christos "LHEW: Got exec event from LWP %ld\n",
2199 1.1 christos ptid_get_lwp (lp->ptid));
2200 1.1 christos
2201 1.1 christos ourstatus->kind = TARGET_WAITKIND_EXECD;
2202 1.1 christos ourstatus->value.execd_pathname
2203 1.1 christos = xstrdup (linux_child_pid_to_exec_file (pid));
2204 1.1 christos
2205 1.1 christos return 0;
2206 1.1 christos }
2207 1.1 christos
2208 1.1 christos if (event == PTRACE_EVENT_VFORK_DONE)
2209 1.1 christos {
2210 1.1 christos if (current_inferior ()->waiting_for_vfork_done)
2211 1.1 christos {
2212 1.1 christos if (debug_linux_nat)
2213 1.1 christos fprintf_unfiltered (gdb_stdlog,
2214 1.1 christos "LHEW: Got expected PTRACE_EVENT_"
2215 1.1 christos "VFORK_DONE from LWP %ld: stopping\n",
2216 1.1 christos ptid_get_lwp (lp->ptid));
2217 1.1 christos
2218 1.1 christos ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
2219 1.1 christos return 0;
2220 1.1 christos }
2221 1.1 christos
2222 1.1 christos if (debug_linux_nat)
2223 1.1 christos fprintf_unfiltered (gdb_stdlog,
2224 1.1 christos "LHEW: Got PTRACE_EVENT_VFORK_DONE "
2225 1.1 christos "from LWP %ld: resuming\n",
2226 1.1 christos ptid_get_lwp (lp->ptid));
2227 1.1 christos ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
2228 1.1 christos return 1;
2229 1.1 christos }
2230 1.1 christos
2231 1.1 christos internal_error (__FILE__, __LINE__,
2232 1.1 christos _("unknown ptrace event %d"), event);
2233 1.1 christos }
2234 1.1 christos
2235 1.1 christos /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
2236 1.1 christos exited. */
2237 1.1 christos
2238 1.1 christos static int
2239 1.1 christos wait_lwp (struct lwp_info *lp)
2240 1.1 christos {
2241 1.1 christos pid_t pid;
2242 1.1 christos int status = 0;
2243 1.1 christos int thread_dead = 0;
2244 1.1 christos sigset_t prev_mask;
2245 1.1 christos
2246 1.1 christos gdb_assert (!lp->stopped);
2247 1.1 christos gdb_assert (lp->status == 0);
2248 1.1 christos
2249 1.1 christos /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
2250 1.1 christos block_child_signals (&prev_mask);
2251 1.1 christos
2252 1.1 christos for (;;)
2253 1.1 christos {
2254 1.1 christos /* If my_waitpid returns 0 it means the __WCLONE vs. non-__WCLONE kind
2255 1.1 christos was right and we should just call sigsuspend. */
2256 1.1 christos
2257 1.1 christos pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, WNOHANG);
2258 1.1 christos if (pid == -1 && errno == ECHILD)
2259 1.1 christos pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, __WCLONE | WNOHANG);
2260 1.1 christos if (pid == -1 && errno == ECHILD)
2261 1.1 christos {
2262 1.1 christos /* The thread has previously exited. We need to delete it
2263 1.1 christos now because, for some vendor 2.4 kernels with NPTL
2264 1.1 christos support backported, there won't be an exit event unless
2265 1.1 christos it is the main thread. 2.6 kernels will report an exit
2266 1.1 christos event for each thread that exits, as expected. */
2267 1.1 christos thread_dead = 1;
2268 1.1 christos if (debug_linux_nat)
2269 1.1 christos fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
2270 1.1 christos target_pid_to_str (lp->ptid));
2271 1.1 christos }
2272 1.1 christos if (pid != 0)
2273 1.1 christos break;
2274 1.1 christos
2275 1.1 christos /* Bugs 10970, 12702.
2276 1.1 christos Thread group leader may have exited in which case we'll lock up in
2277 1.1 christos waitpid if there are other threads, even if they are all zombies too.
2278 1.1 christos Basically, we're not supposed to use waitpid this way.
2279 1.1 christos __WCLONE is not applicable for the leader so we can't use that.
2280 1.1 christos LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
2281 1.1 christos process; it gets ESRCH both for the zombie and for running processes.
2282 1.1 christos
2283 1.1 christos As a workaround, check if we're waiting for the thread group leader and
2284 1.1 christos if it's a zombie, and avoid calling waitpid if it is.
2285 1.1 christos
2286 1.1 christos This is racy, what if the tgl becomes a zombie right after we check?
2287 1.1 christos Therefore always use WNOHANG with sigsuspend - it is equivalent to
2288 1.1 christos waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
2289 1.1 christos
2290 1.1 christos if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid)
2291 1.1 christos && linux_proc_pid_is_zombie (ptid_get_lwp (lp->ptid)))
2292 1.1 christos {
2293 1.1 christos thread_dead = 1;
2294 1.1 christos if (debug_linux_nat)
2295 1.1 christos fprintf_unfiltered (gdb_stdlog,
2296 1.1 christos "WL: Thread group leader %s vanished.\n",
2297 1.1 christos target_pid_to_str (lp->ptid));
2298 1.1 christos break;
2299 1.1 christos }
2300 1.1 christos
2301 1.1 christos /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
2302 1.1 christos get invoked despite our caller had them intentionally blocked by
2303 1.1 christos block_child_signals. This is sensitive only to the loop of
2304 1.1 christos linux_nat_wait_1 and there if we get called my_waitpid gets called
2305 1.1 christos again before it gets to sigsuspend so we can safely let the handlers
2306 1.1 christos get executed here. */
2307 1.1 christos
2308 1.1 christos sigsuspend (&suspend_mask);
2309 1.1 christos }
2310 1.1 christos
2311 1.1 christos restore_child_signals_mask (&prev_mask);
2312 1.1 christos
2313 1.1 christos if (!thread_dead)
2314 1.1 christos {
2315 1.1 christos gdb_assert (pid == ptid_get_lwp (lp->ptid));
2316 1.1 christos
2317 1.1 christos if (debug_linux_nat)
2318 1.1 christos {
2319 1.1 christos fprintf_unfiltered (gdb_stdlog,
2320 1.1 christos "WL: waitpid %s received %s\n",
2321 1.1 christos target_pid_to_str (lp->ptid),
2322 1.1 christos status_to_str (status));
2323 1.1 christos }
2324 1.1 christos
2325 1.1 christos /* Check if the thread has exited. */
2326 1.1 christos if (WIFEXITED (status) || WIFSIGNALED (status))
2327 1.1 christos {
2328 1.1 christos thread_dead = 1;
2329 1.1 christos if (debug_linux_nat)
2330 1.1 christos fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
2331 1.1 christos target_pid_to_str (lp->ptid));
2332 1.1 christos }
2333 1.1 christos }
2334 1.1 christos
2335 1.1 christos if (thread_dead)
2336 1.1 christos {
2337 1.1 christos exit_lwp (lp);
2338 1.1 christos return 0;
2339 1.1 christos }
2340 1.1 christos
2341 1.1 christos gdb_assert (WIFSTOPPED (status));
2342 1.1 christos
2343 1.1 christos /* Handle GNU/Linux's syscall SIGTRAPs. */
2344 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2345 1.1 christos {
2346 1.1 christos /* No longer need the sysgood bit. The ptrace event ends up
2347 1.1 christos recorded in lp->waitstatus if we care for it. We can carry
2348 1.1 christos on handling the event like a regular SIGTRAP from here
2349 1.1 christos on. */
2350 1.1 christos status = W_STOPCODE (SIGTRAP);
2351 1.1 christos if (linux_handle_syscall_trap (lp, 1))
2352 1.1 christos return wait_lwp (lp);
2353 1.1 christos }
2354 1.1 christos
2355 1.1 christos /* Handle GNU/Linux's extended waitstatus for trace events. */
2356 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2357 1.1 christos {
2358 1.1 christos if (debug_linux_nat)
2359 1.1 christos fprintf_unfiltered (gdb_stdlog,
2360 1.1 christos "WL: Handling extended status 0x%06x\n",
2361 1.1 christos status);
2362 1.1 christos if (linux_handle_extended_wait (lp, status, 1))
2363 1.1 christos return wait_lwp (lp);
2364 1.1 christos }
2365 1.1 christos
2366 1.1 christos return status;
2367 1.1 christos }
2368 1.1 christos
2369 1.1 christos /* Send a SIGSTOP to LP. */
2370 1.1 christos
2371 1.1 christos static int
2372 1.1 christos stop_callback (struct lwp_info *lp, void *data)
2373 1.1 christos {
2374 1.1 christos if (!lp->stopped && !lp->signalled)
2375 1.1 christos {
2376 1.1 christos int ret;
2377 1.1 christos
2378 1.1 christos if (debug_linux_nat)
2379 1.1 christos {
2380 1.1 christos fprintf_unfiltered (gdb_stdlog,
2381 1.1 christos "SC: kill %s **<SIGSTOP>**\n",
2382 1.1 christos target_pid_to_str (lp->ptid));
2383 1.1 christos }
2384 1.1 christos errno = 0;
2385 1.1 christos ret = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
2386 1.1 christos if (debug_linux_nat)
2387 1.1 christos {
2388 1.1 christos fprintf_unfiltered (gdb_stdlog,
2389 1.1 christos "SC: lwp kill %d %s\n",
2390 1.1 christos ret,
2391 1.1 christos errno ? safe_strerror (errno) : "ERRNO-OK");
2392 1.1 christos }
2393 1.1 christos
2394 1.1 christos lp->signalled = 1;
2395 1.1 christos gdb_assert (lp->status == 0);
2396 1.1 christos }
2397 1.1 christos
2398 1.1 christos return 0;
2399 1.1 christos }
2400 1.1 christos
2401 1.1 christos /* Request a stop on LWP. */
2402 1.1 christos
2403 1.1 christos void
2404 1.1 christos linux_stop_lwp (struct lwp_info *lwp)
2405 1.1 christos {
2406 1.1 christos stop_callback (lwp, NULL);
2407 1.1 christos }
2408 1.1 christos
2409 1.1 christos /* Return non-zero if LWP PID has a pending SIGINT. */
2410 1.1 christos
2411 1.1 christos static int
2412 1.1 christos linux_nat_has_pending_sigint (int pid)
2413 1.1 christos {
2414 1.1 christos sigset_t pending, blocked, ignored;
2415 1.1 christos
2416 1.1 christos linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
2417 1.1 christos
2418 1.1 christos if (sigismember (&pending, SIGINT)
2419 1.1 christos && !sigismember (&ignored, SIGINT))
2420 1.1 christos return 1;
2421 1.1 christos
2422 1.1 christos return 0;
2423 1.1 christos }
2424 1.1 christos
2425 1.1 christos /* Set a flag in LP indicating that we should ignore its next SIGINT. */
2426 1.1 christos
2427 1.1 christos static int
2428 1.1 christos set_ignore_sigint (struct lwp_info *lp, void *data)
2429 1.1 christos {
2430 1.1 christos /* If a thread has a pending SIGINT, consume it; otherwise, set a
2431 1.1 christos flag to consume the next one. */
2432 1.1 christos if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
2433 1.1 christos && WSTOPSIG (lp->status) == SIGINT)
2434 1.1 christos lp->status = 0;
2435 1.1 christos else
2436 1.1 christos lp->ignore_sigint = 1;
2437 1.1 christos
2438 1.1 christos return 0;
2439 1.1 christos }
2440 1.1 christos
2441 1.1 christos /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
2442 1.1 christos This function is called after we know the LWP has stopped; if the LWP
2443 1.1 christos stopped before the expected SIGINT was delivered, then it will never have
2444 1.1 christos arrived. Also, if the signal was delivered to a shared queue and consumed
2445 1.1 christos by a different thread, it will never be delivered to this LWP. */
2446 1.1 christos
2447 1.1 christos static void
2448 1.1 christos maybe_clear_ignore_sigint (struct lwp_info *lp)
2449 1.1 christos {
2450 1.1 christos if (!lp->ignore_sigint)
2451 1.1 christos return;
2452 1.1 christos
2453 1.1 christos if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp->ptid)))
2454 1.1 christos {
2455 1.1 christos if (debug_linux_nat)
2456 1.1 christos fprintf_unfiltered (gdb_stdlog,
2457 1.1 christos "MCIS: Clearing bogus flag for %s\n",
2458 1.1 christos target_pid_to_str (lp->ptid));
2459 1.1 christos lp->ignore_sigint = 0;
2460 1.1 christos }
2461 1.1 christos }
2462 1.1 christos
2463 1.1 christos /* Fetch the possible triggered data watchpoint info and store it in
2464 1.1 christos LP.
2465 1.1 christos
2466 1.1 christos On some archs, like x86, that use debug registers to set
2467 1.1 christos watchpoints, it's possible that the way to know which watched
2468 1.1 christos address trapped, is to check the register that is used to select
2469 1.1 christos which address to watch. Problem is, between setting the watchpoint
2470 1.1 christos and reading back which data address trapped, the user may change
2471 1.1 christos the set of watchpoints, and, as a consequence, GDB changes the
2472 1.1 christos debug registers in the inferior. To avoid reading back a stale
2473 1.1 christos stopped-data-address when that happens, we cache in LP the fact
2474 1.1 christos that a watchpoint trapped, and the corresponding data address, as
2475 1.1 christos soon as we see LP stop with a SIGTRAP. If GDB changes the debug
2476 1.1 christos registers meanwhile, we have the cached data we can rely on. */
2477 1.1 christos
2478 1.1 christos static void
2479 1.1 christos save_sigtrap (struct lwp_info *lp)
2480 1.1 christos {
2481 1.1 christos struct cleanup *old_chain;
2482 1.1 christos
2483 1.1 christos if (linux_ops->to_stopped_by_watchpoint == NULL)
2484 1.1 christos {
2485 1.1 christos lp->stopped_by_watchpoint = 0;
2486 1.1 christos return;
2487 1.1 christos }
2488 1.1 christos
2489 1.1 christos old_chain = save_inferior_ptid ();
2490 1.1 christos inferior_ptid = lp->ptid;
2491 1.1 christos
2492 1.1 christos lp->stopped_by_watchpoint = linux_ops->to_stopped_by_watchpoint ();
2493 1.1 christos
2494 1.1 christos if (lp->stopped_by_watchpoint)
2495 1.1 christos {
2496 1.1 christos if (linux_ops->to_stopped_data_address != NULL)
2497 1.1 christos lp->stopped_data_address_p =
2498 1.1 christos linux_ops->to_stopped_data_address (¤t_target,
2499 1.1 christos &lp->stopped_data_address);
2500 1.1 christos else
2501 1.1 christos lp->stopped_data_address_p = 0;
2502 1.1 christos }
2503 1.1 christos
2504 1.1 christos do_cleanups (old_chain);
2505 1.1 christos }
2506 1.1 christos
2507 1.1 christos /* See save_sigtrap. */
2508 1.1 christos
2509 1.1 christos static int
2510 1.1 christos linux_nat_stopped_by_watchpoint (void)
2511 1.1 christos {
2512 1.1 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2513 1.1 christos
2514 1.1 christos gdb_assert (lp != NULL);
2515 1.1 christos
2516 1.1 christos return lp->stopped_by_watchpoint;
2517 1.1 christos }
2518 1.1 christos
2519 1.1 christos static int
2520 1.1 christos linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
2521 1.1 christos {
2522 1.1 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid);
2523 1.1 christos
2524 1.1 christos gdb_assert (lp != NULL);
2525 1.1 christos
2526 1.1 christos *addr_p = lp->stopped_data_address;
2527 1.1 christos
2528 1.1 christos return lp->stopped_data_address_p;
2529 1.1 christos }
2530 1.1 christos
2531 1.1 christos /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
2532 1.1 christos
2533 1.1 christos static int
2534 1.1 christos sigtrap_is_event (int status)
2535 1.1 christos {
2536 1.1 christos return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
2537 1.1 christos }
2538 1.1 christos
2539 1.1 christos /* SIGTRAP-like events recognizer. */
2540 1.1 christos
2541 1.1 christos static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
2542 1.1 christos
2543 1.1 christos /* Check for SIGTRAP-like events in LP. */
2544 1.1 christos
2545 1.1 christos static int
2546 1.1 christos linux_nat_lp_status_is_event (struct lwp_info *lp)
2547 1.1 christos {
2548 1.1 christos /* We check for lp->waitstatus in addition to lp->status, because we can
2549 1.1 christos have pending process exits recorded in lp->status
2550 1.1 christos and W_EXITCODE(0,0) == 0. We should probably have an additional
2551 1.1 christos lp->status_p flag. */
2552 1.1 christos
2553 1.1 christos return (lp->waitstatus.kind == TARGET_WAITKIND_IGNORE
2554 1.1 christos && linux_nat_status_is_event (lp->status));
2555 1.1 christos }
2556 1.1 christos
2557 1.1 christos /* Set alternative SIGTRAP-like events recognizer. If
2558 1.1 christos breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
2559 1.1 christos applied. */
2560 1.1 christos
2561 1.1 christos void
2562 1.1 christos linux_nat_set_status_is_event (struct target_ops *t,
2563 1.1 christos int (*status_is_event) (int status))
2564 1.1 christos {
2565 1.1 christos linux_nat_status_is_event = status_is_event;
2566 1.1 christos }
2567 1.1 christos
2568 1.1 christos /* Wait until LP is stopped. */
2569 1.1 christos
2570 1.1 christos static int
2571 1.1 christos stop_wait_callback (struct lwp_info *lp, void *data)
2572 1.1 christos {
2573 1.1 christos struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
2574 1.1 christos
2575 1.1 christos /* If this is a vfork parent, bail out, it is not going to report
2576 1.1 christos any SIGSTOP until the vfork is done with. */
2577 1.1 christos if (inf->vfork_child != NULL)
2578 1.1 christos return 0;
2579 1.1 christos
2580 1.1 christos if (!lp->stopped)
2581 1.1 christos {
2582 1.1 christos int status;
2583 1.1 christos
2584 1.1 christos status = wait_lwp (lp);
2585 1.1 christos if (status == 0)
2586 1.1 christos return 0;
2587 1.1 christos
2588 1.1 christos if (lp->ignore_sigint && WIFSTOPPED (status)
2589 1.1 christos && WSTOPSIG (status) == SIGINT)
2590 1.1 christos {
2591 1.1 christos lp->ignore_sigint = 0;
2592 1.1 christos
2593 1.1 christos errno = 0;
2594 1.1 christos ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
2595 1.1 christos if (debug_linux_nat)
2596 1.1 christos fprintf_unfiltered (gdb_stdlog,
2597 1.1 christos "PTRACE_CONT %s, 0, 0 (%s) "
2598 1.1 christos "(discarding SIGINT)\n",
2599 1.1 christos target_pid_to_str (lp->ptid),
2600 1.1 christos errno ? safe_strerror (errno) : "OK");
2601 1.1 christos
2602 1.1 christos return stop_wait_callback (lp, NULL);
2603 1.1 christos }
2604 1.1 christos
2605 1.1 christos maybe_clear_ignore_sigint (lp);
2606 1.1 christos
2607 1.1 christos if (WSTOPSIG (status) != SIGSTOP)
2608 1.1 christos {
2609 1.1 christos /* The thread was stopped with a signal other than SIGSTOP. */
2610 1.1 christos
2611 1.1 christos save_sigtrap (lp);
2612 1.1 christos
2613 1.1 christos if (debug_linux_nat)
2614 1.1 christos fprintf_unfiltered (gdb_stdlog,
2615 1.1 christos "SWC: Pending event %s in %s\n",
2616 1.1 christos status_to_str ((int) status),
2617 1.1 christos target_pid_to_str (lp->ptid));
2618 1.1 christos
2619 1.1 christos /* Save the sigtrap event. */
2620 1.1 christos lp->status = status;
2621 1.1 christos gdb_assert (!lp->stopped);
2622 1.1 christos gdb_assert (lp->signalled);
2623 1.1 christos lp->stopped = 1;
2624 1.1 christos }
2625 1.1 christos else
2626 1.1 christos {
2627 1.1 christos /* We caught the SIGSTOP that we intended to catch, so
2628 1.1 christos there's no SIGSTOP pending. */
2629 1.1 christos
2630 1.1 christos if (debug_linux_nat)
2631 1.1 christos fprintf_unfiltered (gdb_stdlog,
2632 1.1 christos "SWC: Delayed SIGSTOP caught for %s.\n",
2633 1.1 christos target_pid_to_str (lp->ptid));
2634 1.1 christos
2635 1.1 christos lp->stopped = 1;
2636 1.1 christos
2637 1.1 christos /* Reset SIGNALLED only after the stop_wait_callback call
2638 1.1 christos above as it does gdb_assert on SIGNALLED. */
2639 1.1 christos lp->signalled = 0;
2640 1.1 christos }
2641 1.1 christos }
2642 1.1 christos
2643 1.1 christos return 0;
2644 1.1 christos }
2645 1.1 christos
2646 1.1 christos /* Return non-zero if LP has a wait status pending. */
2647 1.1 christos
2648 1.1 christos static int
2649 1.1 christos status_callback (struct lwp_info *lp, void *data)
2650 1.1 christos {
2651 1.1 christos /* Only report a pending wait status if we pretend that this has
2652 1.1 christos indeed been resumed. */
2653 1.1 christos if (!lp->resumed)
2654 1.1 christos return 0;
2655 1.1 christos
2656 1.1 christos if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2657 1.1 christos {
2658 1.1 christos /* A ptrace event, like PTRACE_FORK|VFORK|EXEC, syscall event,
2659 1.1 christos or a pending process exit. Note that `W_EXITCODE(0,0) ==
2660 1.1 christos 0', so a clean process exit can not be stored pending in
2661 1.1 christos lp->status, it is indistinguishable from
2662 1.1 christos no-pending-status. */
2663 1.1 christos return 1;
2664 1.1 christos }
2665 1.1 christos
2666 1.1 christos if (lp->status != 0)
2667 1.1 christos return 1;
2668 1.1 christos
2669 1.1 christos return 0;
2670 1.1 christos }
2671 1.1 christos
2672 1.1 christos /* Return non-zero if LP isn't stopped. */
2673 1.1 christos
2674 1.1 christos static int
2675 1.1 christos running_callback (struct lwp_info *lp, void *data)
2676 1.1 christos {
2677 1.1 christos return (!lp->stopped
2678 1.1 christos || ((lp->status != 0
2679 1.1 christos || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2680 1.1 christos && lp->resumed));
2681 1.1 christos }
2682 1.1 christos
2683 1.1 christos /* Count the LWP's that have had events. */
2684 1.1 christos
2685 1.1 christos static int
2686 1.1 christos count_events_callback (struct lwp_info *lp, void *data)
2687 1.1 christos {
2688 1.1 christos int *count = data;
2689 1.1 christos
2690 1.1 christos gdb_assert (count != NULL);
2691 1.1 christos
2692 1.1 christos /* Count only resumed LWPs that have a SIGTRAP event pending. */
2693 1.1 christos if (lp->resumed && linux_nat_lp_status_is_event (lp))
2694 1.1 christos (*count)++;
2695 1.1 christos
2696 1.1 christos return 0;
2697 1.1 christos }
2698 1.1 christos
2699 1.1 christos /* Select the LWP (if any) that is currently being single-stepped. */
2700 1.1 christos
2701 1.1 christos static int
2702 1.1 christos select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
2703 1.1 christos {
2704 1.1 christos if (lp->last_resume_kind == resume_step
2705 1.1 christos && lp->status != 0)
2706 1.1 christos return 1;
2707 1.1 christos else
2708 1.1 christos return 0;
2709 1.1 christos }
2710 1.1 christos
2711 1.1 christos /* Select the Nth LWP that has had a SIGTRAP event. */
2712 1.1 christos
2713 1.1 christos static int
2714 1.1 christos select_event_lwp_callback (struct lwp_info *lp, void *data)
2715 1.1 christos {
2716 1.1 christos int *selector = data;
2717 1.1 christos
2718 1.1 christos gdb_assert (selector != NULL);
2719 1.1 christos
2720 1.1 christos /* Select only resumed LWPs that have a SIGTRAP event pending. */
2721 1.1 christos if (lp->resumed && linux_nat_lp_status_is_event (lp))
2722 1.1 christos if ((*selector)-- == 0)
2723 1.1 christos return 1;
2724 1.1 christos
2725 1.1 christos return 0;
2726 1.1 christos }
2727 1.1 christos
2728 1.1 christos static int
2729 1.1 christos cancel_breakpoint (struct lwp_info *lp)
2730 1.1 christos {
2731 1.1 christos /* Arrange for a breakpoint to be hit again later. We don't keep
2732 1.1 christos the SIGTRAP status and don't forward the SIGTRAP signal to the
2733 1.1 christos LWP. We will handle the current event, eventually we will resume
2734 1.1 christos this LWP, and this breakpoint will trap again.
2735 1.1 christos
2736 1.1 christos If we do not do this, then we run the risk that the user will
2737 1.1 christos delete or disable the breakpoint, but the LWP will have already
2738 1.1 christos tripped on it. */
2739 1.1 christos
2740 1.1 christos struct regcache *regcache = get_thread_regcache (lp->ptid);
2741 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
2742 1.1 christos CORE_ADDR pc;
2743 1.1 christos
2744 1.1 christos pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
2745 1.1 christos if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
2746 1.1 christos {
2747 1.1 christos if (debug_linux_nat)
2748 1.1 christos fprintf_unfiltered (gdb_stdlog,
2749 1.1 christos "CB: Push back breakpoint for %s\n",
2750 1.1 christos target_pid_to_str (lp->ptid));
2751 1.1 christos
2752 1.1 christos /* Back up the PC if necessary. */
2753 1.1 christos if (gdbarch_decr_pc_after_break (gdbarch))
2754 1.1 christos regcache_write_pc (regcache, pc);
2755 1.1 christos
2756 1.1 christos return 1;
2757 1.1 christos }
2758 1.1 christos return 0;
2759 1.1 christos }
2760 1.1 christos
2761 1.1 christos static int
2762 1.1 christos cancel_breakpoints_callback (struct lwp_info *lp, void *data)
2763 1.1 christos {
2764 1.1 christos struct lwp_info *event_lp = data;
2765 1.1 christos
2766 1.1 christos /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2767 1.1 christos if (lp == event_lp)
2768 1.1 christos return 0;
2769 1.1 christos
2770 1.1 christos /* If a LWP other than the LWP that we're reporting an event for has
2771 1.1 christos hit a GDB breakpoint (as opposed to some random trap signal),
2772 1.1 christos then just arrange for it to hit it again later. We don't keep
2773 1.1 christos the SIGTRAP status and don't forward the SIGTRAP signal to the
2774 1.1 christos LWP. We will handle the current event, eventually we will resume
2775 1.1 christos all LWPs, and this one will get its breakpoint trap again.
2776 1.1 christos
2777 1.1 christos If we do not do this, then we run the risk that the user will
2778 1.1 christos delete or disable the breakpoint, but the LWP will have already
2779 1.1 christos tripped on it. */
2780 1.1 christos
2781 1.1 christos if (linux_nat_lp_status_is_event (lp)
2782 1.1 christos && cancel_breakpoint (lp))
2783 1.1 christos /* Throw away the SIGTRAP. */
2784 1.1 christos lp->status = 0;
2785 1.1 christos
2786 1.1 christos return 0;
2787 1.1 christos }
2788 1.1 christos
2789 1.1 christos /* Select one LWP out of those that have events pending. */
2790 1.1 christos
2791 1.1 christos static void
2792 1.1 christos select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
2793 1.1 christos {
2794 1.1 christos int num_events = 0;
2795 1.1 christos int random_selector;
2796 1.1 christos struct lwp_info *event_lp;
2797 1.1 christos
2798 1.1 christos /* Record the wait status for the original LWP. */
2799 1.1 christos (*orig_lp)->status = *status;
2800 1.1 christos
2801 1.1 christos /* Give preference to any LWP that is being single-stepped. */
2802 1.1 christos event_lp = iterate_over_lwps (filter,
2803 1.1 christos select_singlestep_lwp_callback, NULL);
2804 1.1 christos if (event_lp != NULL)
2805 1.1 christos {
2806 1.1 christos if (debug_linux_nat)
2807 1.1 christos fprintf_unfiltered (gdb_stdlog,
2808 1.1 christos "SEL: Select single-step %s\n",
2809 1.1 christos target_pid_to_str (event_lp->ptid));
2810 1.1 christos }
2811 1.1 christos else
2812 1.1 christos {
2813 1.1 christos /* No single-stepping LWP. Select one at random, out of those
2814 1.1 christos which have had SIGTRAP events. */
2815 1.1 christos
2816 1.1 christos /* First see how many SIGTRAP events we have. */
2817 1.1 christos iterate_over_lwps (filter, count_events_callback, &num_events);
2818 1.1 christos
2819 1.1 christos /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2820 1.1 christos random_selector = (int)
2821 1.1 christos ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2822 1.1 christos
2823 1.1 christos if (debug_linux_nat && num_events > 1)
2824 1.1 christos fprintf_unfiltered (gdb_stdlog,
2825 1.1 christos "SEL: Found %d SIGTRAP events, selecting #%d\n",
2826 1.1 christos num_events, random_selector);
2827 1.1 christos
2828 1.1 christos event_lp = iterate_over_lwps (filter,
2829 1.1 christos select_event_lwp_callback,
2830 1.1 christos &random_selector);
2831 1.1 christos }
2832 1.1 christos
2833 1.1 christos if (event_lp != NULL)
2834 1.1 christos {
2835 1.1 christos /* Switch the event LWP. */
2836 1.1 christos *orig_lp = event_lp;
2837 1.1 christos *status = event_lp->status;
2838 1.1 christos }
2839 1.1 christos
2840 1.1 christos /* Flush the wait status for the event LWP. */
2841 1.1 christos (*orig_lp)->status = 0;
2842 1.1 christos }
2843 1.1 christos
2844 1.1 christos /* Return non-zero if LP has been resumed. */
2845 1.1 christos
2846 1.1 christos static int
2847 1.1 christos resumed_callback (struct lwp_info *lp, void *data)
2848 1.1 christos {
2849 1.1 christos return lp->resumed;
2850 1.1 christos }
2851 1.1 christos
2852 1.1 christos /* Stop an active thread, verify it still exists, then resume it. If
2853 1.1 christos the thread ends up with a pending status, then it is not resumed,
2854 1.1 christos and *DATA (really a pointer to int), is set. */
2855 1.1 christos
2856 1.1 christos static int
2857 1.1 christos stop_and_resume_callback (struct lwp_info *lp, void *data)
2858 1.1 christos {
2859 1.1 christos int *new_pending_p = data;
2860 1.1 christos
2861 1.1 christos if (!lp->stopped)
2862 1.1 christos {
2863 1.1 christos ptid_t ptid = lp->ptid;
2864 1.1 christos
2865 1.1 christos stop_callback (lp, NULL);
2866 1.1 christos stop_wait_callback (lp, NULL);
2867 1.1 christos
2868 1.1 christos /* Resume if the lwp still exists, and the core wanted it
2869 1.1 christos running. */
2870 1.1 christos lp = find_lwp_pid (ptid);
2871 1.1 christos if (lp != NULL)
2872 1.1 christos {
2873 1.1 christos if (lp->last_resume_kind == resume_stop
2874 1.1 christos && lp->status == 0)
2875 1.1 christos {
2876 1.1 christos /* The core wanted the LWP to stop. Even if it stopped
2877 1.1 christos cleanly (with SIGSTOP), leave the event pending. */
2878 1.1 christos if (debug_linux_nat)
2879 1.1 christos fprintf_unfiltered (gdb_stdlog,
2880 1.1 christos "SARC: core wanted LWP %ld stopped "
2881 1.1 christos "(leaving SIGSTOP pending)\n",
2882 1.1 christos ptid_get_lwp (lp->ptid));
2883 1.1 christos lp->status = W_STOPCODE (SIGSTOP);
2884 1.1 christos }
2885 1.1 christos
2886 1.1 christos if (lp->status == 0)
2887 1.1 christos {
2888 1.1 christos if (debug_linux_nat)
2889 1.1 christos fprintf_unfiltered (gdb_stdlog,
2890 1.1 christos "SARC: re-resuming LWP %ld\n",
2891 1.1 christos ptid_get_lwp (lp->ptid));
2892 1.1 christos resume_lwp (lp, lp->step, GDB_SIGNAL_0);
2893 1.1 christos }
2894 1.1 christos else
2895 1.1 christos {
2896 1.1 christos if (debug_linux_nat)
2897 1.1 christos fprintf_unfiltered (gdb_stdlog,
2898 1.1 christos "SARC: not re-resuming LWP %ld "
2899 1.1 christos "(has pending)\n",
2900 1.1 christos ptid_get_lwp (lp->ptid));
2901 1.1 christos if (new_pending_p)
2902 1.1 christos *new_pending_p = 1;
2903 1.1 christos }
2904 1.1 christos }
2905 1.1 christos }
2906 1.1 christos return 0;
2907 1.1 christos }
2908 1.1 christos
2909 1.1 christos /* Check if we should go on and pass this event to common code.
2910 1.1 christos Return the affected lwp if we are, or NULL otherwise. If we stop
2911 1.1 christos all lwps temporarily, we may end up with new pending events in some
2912 1.1 christos other lwp. In that case set *NEW_PENDING_P to true. */
2913 1.1 christos
2914 1.1 christos static struct lwp_info *
2915 1.1 christos linux_nat_filter_event (int lwpid, int status, int *new_pending_p)
2916 1.1 christos {
2917 1.1 christos struct lwp_info *lp;
2918 1.1 christos
2919 1.1 christos *new_pending_p = 0;
2920 1.1 christos
2921 1.1 christos lp = find_lwp_pid (pid_to_ptid (lwpid));
2922 1.1 christos
2923 1.1 christos /* Check for stop events reported by a process we didn't already
2924 1.1 christos know about - anything not already in our LWP list.
2925 1.1 christos
2926 1.1 christos If we're expecting to receive stopped processes after
2927 1.1 christos fork, vfork, and clone events, then we'll just add the
2928 1.1 christos new one to our list and go back to waiting for the event
2929 1.1 christos to be reported - the stopped process might be returned
2930 1.1 christos from waitpid before or after the event is.
2931 1.1 christos
2932 1.1 christos But note the case of a non-leader thread exec'ing after the
2933 1.1 christos leader having exited, and gone from our lists. The non-leader
2934 1.1 christos thread changes its tid to the tgid. */
2935 1.1 christos
2936 1.1 christos if (WIFSTOPPED (status) && lp == NULL
2937 1.1 christos && (WSTOPSIG (status) == SIGTRAP && status >> 16 == PTRACE_EVENT_EXEC))
2938 1.1 christos {
2939 1.1 christos /* A multi-thread exec after we had seen the leader exiting. */
2940 1.1 christos if (debug_linux_nat)
2941 1.1 christos fprintf_unfiltered (gdb_stdlog,
2942 1.1 christos "LLW: Re-adding thread group leader LWP %d.\n",
2943 1.1 christos lwpid);
2944 1.1 christos
2945 1.1 christos lp = add_lwp (ptid_build (lwpid, lwpid, 0));
2946 1.1 christos lp->stopped = 1;
2947 1.1 christos lp->resumed = 1;
2948 1.1 christos add_thread (lp->ptid);
2949 1.1 christos }
2950 1.1 christos
2951 1.1 christos if (WIFSTOPPED (status) && !lp)
2952 1.1 christos {
2953 1.1 christos add_to_pid_list (&stopped_pids, lwpid, status);
2954 1.1 christos return NULL;
2955 1.1 christos }
2956 1.1 christos
2957 1.1 christos /* Make sure we don't report an event for the exit of an LWP not in
2958 1.1 christos our list, i.e. not part of the current process. This can happen
2959 1.1 christos if we detach from a program we originally forked and then it
2960 1.1 christos exits. */
2961 1.1 christos if (!WIFSTOPPED (status) && !lp)
2962 1.1 christos return NULL;
2963 1.1 christos
2964 1.1 christos /* Handle GNU/Linux's syscall SIGTRAPs. */
2965 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
2966 1.1 christos {
2967 1.1 christos /* No longer need the sysgood bit. The ptrace event ends up
2968 1.1 christos recorded in lp->waitstatus if we care for it. We can carry
2969 1.1 christos on handling the event like a regular SIGTRAP from here
2970 1.1 christos on. */
2971 1.1 christos status = W_STOPCODE (SIGTRAP);
2972 1.1 christos if (linux_handle_syscall_trap (lp, 0))
2973 1.1 christos return NULL;
2974 1.1 christos }
2975 1.1 christos
2976 1.1 christos /* Handle GNU/Linux's extended waitstatus for trace events. */
2977 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2978 1.1 christos {
2979 1.1 christos if (debug_linux_nat)
2980 1.1 christos fprintf_unfiltered (gdb_stdlog,
2981 1.1 christos "LLW: Handling extended status 0x%06x\n",
2982 1.1 christos status);
2983 1.1 christos if (linux_handle_extended_wait (lp, status, 0))
2984 1.1 christos return NULL;
2985 1.1 christos }
2986 1.1 christos
2987 1.1 christos if (linux_nat_status_is_event (status))
2988 1.1 christos save_sigtrap (lp);
2989 1.1 christos
2990 1.1 christos /* Check if the thread has exited. */
2991 1.1 christos if ((WIFEXITED (status) || WIFSIGNALED (status))
2992 1.1 christos && num_lwps (ptid_get_pid (lp->ptid)) > 1)
2993 1.1 christos {
2994 1.1 christos /* If this is the main thread, we must stop all threads and verify
2995 1.1 christos if they are still alive. This is because in the nptl thread model
2996 1.1 christos on Linux 2.4, there is no signal issued for exiting LWPs
2997 1.1 christos other than the main thread. We only get the main thread exit
2998 1.1 christos signal once all child threads have already exited. If we
2999 1.1 christos stop all the threads and use the stop_wait_callback to check
3000 1.1 christos if they have exited we can determine whether this signal
3001 1.1 christos should be ignored or whether it means the end of the debugged
3002 1.1 christos application, regardless of which threading model is being
3003 1.1 christos used. */
3004 1.1 christos if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
3005 1.1 christos {
3006 1.1 christos lp->stopped = 1;
3007 1.1 christos iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
3008 1.1 christos stop_and_resume_callback, new_pending_p);
3009 1.1 christos }
3010 1.1 christos
3011 1.1 christos if (debug_linux_nat)
3012 1.1 christos fprintf_unfiltered (gdb_stdlog,
3013 1.1 christos "LLW: %s exited.\n",
3014 1.1 christos target_pid_to_str (lp->ptid));
3015 1.1 christos
3016 1.1 christos if (num_lwps (ptid_get_pid (lp->ptid)) > 1)
3017 1.1 christos {
3018 1.1 christos /* If there is at least one more LWP, then the exit signal
3019 1.1 christos was not the end of the debugged application and should be
3020 1.1 christos ignored. */
3021 1.1 christos exit_lwp (lp);
3022 1.1 christos return NULL;
3023 1.1 christos }
3024 1.1 christos }
3025 1.1 christos
3026 1.1 christos /* Check if the current LWP has previously exited. In the nptl
3027 1.1 christos thread model, LWPs other than the main thread do not issue
3028 1.1 christos signals when they exit so we must check whenever the thread has
3029 1.1 christos stopped. A similar check is made in stop_wait_callback(). */
3030 1.1 christos if (num_lwps (ptid_get_pid (lp->ptid)) > 1 && !linux_thread_alive (lp->ptid))
3031 1.1 christos {
3032 1.1 christos ptid_t ptid = pid_to_ptid (ptid_get_pid (lp->ptid));
3033 1.1 christos
3034 1.1 christos if (debug_linux_nat)
3035 1.1 christos fprintf_unfiltered (gdb_stdlog,
3036 1.1 christos "LLW: %s exited.\n",
3037 1.1 christos target_pid_to_str (lp->ptid));
3038 1.1 christos
3039 1.1 christos exit_lwp (lp);
3040 1.1 christos
3041 1.1 christos /* Make sure there is at least one thread running. */
3042 1.1 christos gdb_assert (iterate_over_lwps (ptid, running_callback, NULL));
3043 1.1 christos
3044 1.1 christos /* Discard the event. */
3045 1.1 christos return NULL;
3046 1.1 christos }
3047 1.1 christos
3048 1.1 christos /* Make sure we don't report a SIGSTOP that we sent ourselves in
3049 1.1 christos an attempt to stop an LWP. */
3050 1.1 christos if (lp->signalled
3051 1.1 christos && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
3052 1.1 christos {
3053 1.1 christos if (debug_linux_nat)
3054 1.1 christos fprintf_unfiltered (gdb_stdlog,
3055 1.1 christos "LLW: Delayed SIGSTOP caught for %s.\n",
3056 1.1 christos target_pid_to_str (lp->ptid));
3057 1.1 christos
3058 1.1 christos lp->signalled = 0;
3059 1.1 christos
3060 1.1 christos if (lp->last_resume_kind != resume_stop)
3061 1.1 christos {
3062 1.1 christos /* This is a delayed SIGSTOP. */
3063 1.1 christos
3064 1.1 christos registers_changed ();
3065 1.1 christos
3066 1.1 christos if (linux_nat_prepare_to_resume != NULL)
3067 1.1 christos linux_nat_prepare_to_resume (lp);
3068 1.1 christos linux_ops->to_resume (linux_ops,
3069 1.1 christos pid_to_ptid (ptid_get_lwp (lp->ptid)),
3070 1.1 christos lp->step, GDB_SIGNAL_0);
3071 1.1 christos if (debug_linux_nat)
3072 1.1 christos fprintf_unfiltered (gdb_stdlog,
3073 1.1 christos "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
3074 1.1 christos lp->step ?
3075 1.1 christos "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3076 1.1 christos target_pid_to_str (lp->ptid));
3077 1.1 christos
3078 1.1 christos lp->stopped = 0;
3079 1.1 christos gdb_assert (lp->resumed);
3080 1.1 christos
3081 1.1 christos /* Discard the event. */
3082 1.1 christos return NULL;
3083 1.1 christos }
3084 1.1 christos }
3085 1.1 christos
3086 1.1 christos /* Make sure we don't report a SIGINT that we have already displayed
3087 1.1 christos for another thread. */
3088 1.1 christos if (lp->ignore_sigint
3089 1.1 christos && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
3090 1.1 christos {
3091 1.1 christos if (debug_linux_nat)
3092 1.1 christos fprintf_unfiltered (gdb_stdlog,
3093 1.1 christos "LLW: Delayed SIGINT caught for %s.\n",
3094 1.1 christos target_pid_to_str (lp->ptid));
3095 1.1 christos
3096 1.1 christos /* This is a delayed SIGINT. */
3097 1.1 christos lp->ignore_sigint = 0;
3098 1.1 christos
3099 1.1 christos registers_changed ();
3100 1.1 christos if (linux_nat_prepare_to_resume != NULL)
3101 1.1 christos linux_nat_prepare_to_resume (lp);
3102 1.1 christos linux_ops->to_resume (linux_ops, pid_to_ptid (ptid_get_lwp (lp->ptid)),
3103 1.1 christos lp->step, GDB_SIGNAL_0);
3104 1.1 christos if (debug_linux_nat)
3105 1.1 christos fprintf_unfiltered (gdb_stdlog,
3106 1.1 christos "LLW: %s %s, 0, 0 (discard SIGINT)\n",
3107 1.1 christos lp->step ?
3108 1.1 christos "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3109 1.1 christos target_pid_to_str (lp->ptid));
3110 1.1 christos
3111 1.1 christos lp->stopped = 0;
3112 1.1 christos gdb_assert (lp->resumed);
3113 1.1 christos
3114 1.1 christos /* Discard the event. */
3115 1.1 christos return NULL;
3116 1.1 christos }
3117 1.1 christos
3118 1.1 christos /* An interesting event. */
3119 1.1 christos gdb_assert (lp);
3120 1.1 christos lp->status = status;
3121 1.1 christos return lp;
3122 1.1 christos }
3123 1.1 christos
3124 1.1 christos /* Detect zombie thread group leaders, and "exit" them. We can't reap
3125 1.1 christos their exits until all other threads in the group have exited. */
3126 1.1 christos
3127 1.1 christos static void
3128 1.1 christos check_zombie_leaders (void)
3129 1.1 christos {
3130 1.1 christos struct inferior *inf;
3131 1.1 christos
3132 1.1 christos ALL_INFERIORS (inf)
3133 1.1 christos {
3134 1.1 christos struct lwp_info *leader_lp;
3135 1.1 christos
3136 1.1 christos if (inf->pid == 0)
3137 1.1 christos continue;
3138 1.1 christos
3139 1.1 christos leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
3140 1.1 christos if (leader_lp != NULL
3141 1.1 christos /* Check if there are other threads in the group, as we may
3142 1.1 christos have raced with the inferior simply exiting. */
3143 1.1 christos && num_lwps (inf->pid) > 1
3144 1.1 christos && linux_proc_pid_is_zombie (inf->pid))
3145 1.1 christos {
3146 1.1 christos if (debug_linux_nat)
3147 1.1 christos fprintf_unfiltered (gdb_stdlog,
3148 1.1 christos "CZL: Thread group leader %d zombie "
3149 1.1 christos "(it exited, or another thread execd).\n",
3150 1.1 christos inf->pid);
3151 1.1 christos
3152 1.1 christos /* A leader zombie can mean one of two things:
3153 1.1 christos
3154 1.1 christos - It exited, and there's an exit status pending
3155 1.1 christos available, or only the leader exited (not the whole
3156 1.1 christos program). In the latter case, we can't waitpid the
3157 1.1 christos leader's exit status until all other threads are gone.
3158 1.1 christos
3159 1.1 christos - There are 3 or more threads in the group, and a thread
3160 1.1 christos other than the leader exec'd. On an exec, the Linux
3161 1.1 christos kernel destroys all other threads (except the execing
3162 1.1 christos one) in the thread group, and resets the execing thread's
3163 1.1 christos tid to the tgid. No exit notification is sent for the
3164 1.1 christos execing thread -- from the ptracer's perspective, it
3165 1.1 christos appears as though the execing thread just vanishes.
3166 1.1 christos Until we reap all other threads except the leader and the
3167 1.1 christos execing thread, the leader will be zombie, and the
3168 1.1 christos execing thread will be in `D (disc sleep)'. As soon as
3169 1.1 christos all other threads are reaped, the execing thread changes
3170 1.1 christos it's tid to the tgid, and the previous (zombie) leader
3171 1.1 christos vanishes, giving place to the "new" leader. We could try
3172 1.1 christos distinguishing the exit and exec cases, by waiting once
3173 1.1 christos more, and seeing if something comes out, but it doesn't
3174 1.1 christos sound useful. The previous leader _does_ go away, and
3175 1.1 christos we'll re-add the new one once we see the exec event
3176 1.1 christos (which is just the same as what would happen if the
3177 1.1 christos previous leader did exit voluntarily before some other
3178 1.1 christos thread execs). */
3179 1.1 christos
3180 1.1 christos if (debug_linux_nat)
3181 1.1 christos fprintf_unfiltered (gdb_stdlog,
3182 1.1 christos "CZL: Thread group leader %d vanished.\n",
3183 1.1 christos inf->pid);
3184 1.1 christos exit_lwp (leader_lp);
3185 1.1 christos }
3186 1.1 christos }
3187 1.1 christos }
3188 1.1 christos
3189 1.1 christos static ptid_t
3190 1.1 christos linux_nat_wait_1 (struct target_ops *ops,
3191 1.1 christos ptid_t ptid, struct target_waitstatus *ourstatus,
3192 1.1 christos int target_options)
3193 1.1 christos {
3194 1.1 christos static sigset_t prev_mask;
3195 1.1 christos enum resume_kind last_resume_kind;
3196 1.1 christos struct lwp_info *lp;
3197 1.1 christos int status;
3198 1.1 christos
3199 1.1 christos if (debug_linux_nat)
3200 1.1 christos fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
3201 1.1 christos
3202 1.1 christos /* The first time we get here after starting a new inferior, we may
3203 1.1 christos not have added it to the LWP list yet - this is the earliest
3204 1.1 christos moment at which we know its PID. */
3205 1.1 christos if (ptid_is_pid (inferior_ptid))
3206 1.1 christos {
3207 1.1 christos /* Upgrade the main thread's ptid. */
3208 1.1 christos thread_change_ptid (inferior_ptid,
3209 1.1 christos ptid_build (ptid_get_pid (inferior_ptid),
3210 1.1 christos ptid_get_pid (inferior_ptid), 0));
3211 1.1 christos
3212 1.1 christos lp = add_initial_lwp (inferior_ptid);
3213 1.1 christos lp->resumed = 1;
3214 1.1 christos }
3215 1.1 christos
3216 1.1 christos /* Make sure SIGCHLD is blocked until the sigsuspend below. */
3217 1.1 christos block_child_signals (&prev_mask);
3218 1.1 christos
3219 1.1 christos retry:
3220 1.1 christos lp = NULL;
3221 1.1 christos status = 0;
3222 1.1 christos
3223 1.1 christos /* First check if there is a LWP with a wait status pending. */
3224 1.1 christos if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3225 1.1 christos {
3226 1.1 christos /* Any LWP in the PTID group that's been resumed will do. */
3227 1.1 christos lp = iterate_over_lwps (ptid, status_callback, NULL);
3228 1.1 christos if (lp)
3229 1.1 christos {
3230 1.1 christos if (debug_linux_nat && lp->status)
3231 1.1 christos fprintf_unfiltered (gdb_stdlog,
3232 1.1 christos "LLW: Using pending wait status %s for %s.\n",
3233 1.1 christos status_to_str (lp->status),
3234 1.1 christos target_pid_to_str (lp->ptid));
3235 1.1 christos }
3236 1.1 christos }
3237 1.1 christos else if (ptid_lwp_p (ptid))
3238 1.1 christos {
3239 1.1 christos if (debug_linux_nat)
3240 1.1 christos fprintf_unfiltered (gdb_stdlog,
3241 1.1 christos "LLW: Waiting for specific LWP %s.\n",
3242 1.1 christos target_pid_to_str (ptid));
3243 1.1 christos
3244 1.1 christos /* We have a specific LWP to check. */
3245 1.1 christos lp = find_lwp_pid (ptid);
3246 1.1 christos gdb_assert (lp);
3247 1.1 christos
3248 1.1 christos if (debug_linux_nat && lp->status)
3249 1.1 christos fprintf_unfiltered (gdb_stdlog,
3250 1.1 christos "LLW: Using pending wait status %s for %s.\n",
3251 1.1 christos status_to_str (lp->status),
3252 1.1 christos target_pid_to_str (lp->ptid));
3253 1.1 christos
3254 1.1 christos /* We check for lp->waitstatus in addition to lp->status,
3255 1.1 christos because we can have pending process exits recorded in
3256 1.1 christos lp->status and W_EXITCODE(0,0) == 0. We should probably have
3257 1.1 christos an additional lp->status_p flag. */
3258 1.1 christos if (lp->status == 0 && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
3259 1.1 christos lp = NULL;
3260 1.1 christos }
3261 1.1 christos
3262 1.1 christos if (!target_can_async_p ())
3263 1.1 christos {
3264 1.1 christos /* Causes SIGINT to be passed on to the attached process. */
3265 1.1 christos set_sigint_trap ();
3266 1.1 christos }
3267 1.1 christos
3268 1.1 christos /* But if we don't find a pending event, we'll have to wait. */
3269 1.1 christos
3270 1.1 christos while (lp == NULL)
3271 1.1 christos {
3272 1.1 christos pid_t lwpid;
3273 1.1 christos
3274 1.1 christos /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
3275 1.1 christos quirks:
3276 1.1 christos
3277 1.1 christos - If the thread group leader exits while other threads in the
3278 1.1 christos thread group still exist, waitpid(TGID, ...) hangs. That
3279 1.1 christos waitpid won't return an exit status until the other threads
3280 1.1 christos in the group are reapped.
3281 1.1 christos
3282 1.1 christos - When a non-leader thread execs, that thread just vanishes
3283 1.1 christos without reporting an exit (so we'd hang if we waited for it
3284 1.1 christos explicitly in that case). The exec event is reported to
3285 1.1 christos the TGID pid. */
3286 1.1 christos
3287 1.1 christos errno = 0;
3288 1.1 christos lwpid = my_waitpid (-1, &status, __WCLONE | WNOHANG);
3289 1.1 christos if (lwpid == 0 || (lwpid == -1 && errno == ECHILD))
3290 1.1 christos lwpid = my_waitpid (-1, &status, WNOHANG);
3291 1.1 christos
3292 1.1 christos if (debug_linux_nat)
3293 1.1 christos fprintf_unfiltered (gdb_stdlog,
3294 1.1 christos "LNW: waitpid(-1, ...) returned %d, %s\n",
3295 1.1 christos lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
3296 1.1 christos
3297 1.1 christos if (lwpid > 0)
3298 1.1 christos {
3299 1.1 christos /* If this is true, then we paused LWPs momentarily, and may
3300 1.1 christos now have pending events to handle. */
3301 1.1 christos int new_pending;
3302 1.1 christos
3303 1.1 christos if (debug_linux_nat)
3304 1.1 christos {
3305 1.1 christos fprintf_unfiltered (gdb_stdlog,
3306 1.1 christos "LLW: waitpid %ld received %s\n",
3307 1.1 christos (long) lwpid, status_to_str (status));
3308 1.1 christos }
3309 1.1 christos
3310 1.1 christos lp = linux_nat_filter_event (lwpid, status, &new_pending);
3311 1.1 christos
3312 1.1 christos /* STATUS is now no longer valid, use LP->STATUS instead. */
3313 1.1 christos status = 0;
3314 1.1 christos
3315 1.1 christos if (lp && !ptid_match (lp->ptid, ptid))
3316 1.1 christos {
3317 1.1 christos gdb_assert (lp->resumed);
3318 1.1 christos
3319 1.1 christos if (debug_linux_nat)
3320 1.1 christos fprintf (stderr,
3321 1.1 christos "LWP %ld got an event %06x, leaving pending.\n",
3322 1.1 christos ptid_get_lwp (lp->ptid), lp->status);
3323 1.1 christos
3324 1.1 christos if (WIFSTOPPED (lp->status))
3325 1.1 christos {
3326 1.1 christos if (WSTOPSIG (lp->status) != SIGSTOP)
3327 1.1 christos {
3328 1.1 christos /* Cancel breakpoint hits. The breakpoint may
3329 1.1 christos be removed before we fetch events from this
3330 1.1 christos process to report to the core. It is best
3331 1.1 christos not to assume the moribund breakpoints
3332 1.1 christos heuristic always handles these cases --- it
3333 1.1 christos could be too many events go through to the
3334 1.1 christos core before this one is handled. All-stop
3335 1.1 christos always cancels breakpoint hits in all
3336 1.1 christos threads. */
3337 1.1 christos if (non_stop
3338 1.1 christos && linux_nat_lp_status_is_event (lp)
3339 1.1 christos && cancel_breakpoint (lp))
3340 1.1 christos {
3341 1.1 christos /* Throw away the SIGTRAP. */
3342 1.1 christos lp->status = 0;
3343 1.1 christos
3344 1.1 christos if (debug_linux_nat)
3345 1.1 christos fprintf (stderr,
3346 1.1 christos "LLW: LWP %ld hit a breakpoint while"
3347 1.1 christos " waiting for another process;"
3348 1.1 christos " cancelled it\n",
3349 1.1 christos ptid_get_lwp (lp->ptid));
3350 1.1 christos }
3351 1.1 christos lp->stopped = 1;
3352 1.1 christos }
3353 1.1 christos else
3354 1.1 christos {
3355 1.1 christos lp->stopped = 1;
3356 1.1 christos lp->signalled = 0;
3357 1.1 christos }
3358 1.1 christos }
3359 1.1 christos else if (WIFEXITED (lp->status) || WIFSIGNALED (lp->status))
3360 1.1 christos {
3361 1.1 christos if (debug_linux_nat)
3362 1.1 christos fprintf (stderr,
3363 1.1 christos "Process %ld exited while stopping LWPs\n",
3364 1.1 christos ptid_get_lwp (lp->ptid));
3365 1.1 christos
3366 1.1 christos /* This was the last lwp in the process. Since
3367 1.1 christos events are serialized to GDB core, and we can't
3368 1.1 christos report this one right now, but GDB core and the
3369 1.1 christos other target layers will want to be notified
3370 1.1 christos about the exit code/signal, leave the status
3371 1.1 christos pending for the next time we're able to report
3372 1.1 christos it. */
3373 1.1 christos
3374 1.1 christos /* Prevent trying to stop this thread again. We'll
3375 1.1 christos never try to resume it because it has a pending
3376 1.1 christos status. */
3377 1.1 christos lp->stopped = 1;
3378 1.1 christos
3379 1.1 christos /* Dead LWP's aren't expected to reported a pending
3380 1.1 christos sigstop. */
3381 1.1 christos lp->signalled = 0;
3382 1.1 christos
3383 1.1 christos /* Store the pending event in the waitstatus as
3384 1.1 christos well, because W_EXITCODE(0,0) == 0. */
3385 1.1 christos store_waitstatus (&lp->waitstatus, lp->status);
3386 1.1 christos }
3387 1.1 christos
3388 1.1 christos /* Keep looking. */
3389 1.1 christos lp = NULL;
3390 1.1 christos }
3391 1.1 christos
3392 1.1 christos if (new_pending)
3393 1.1 christos {
3394 1.1 christos /* Some LWP now has a pending event. Go all the way
3395 1.1 christos back to check it. */
3396 1.1 christos goto retry;
3397 1.1 christos }
3398 1.1 christos
3399 1.1 christos if (lp)
3400 1.1 christos {
3401 1.1 christos /* We got an event to report to the core. */
3402 1.1 christos break;
3403 1.1 christos }
3404 1.1 christos
3405 1.1 christos /* Retry until nothing comes out of waitpid. A single
3406 1.1 christos SIGCHLD can indicate more than one child stopped. */
3407 1.1 christos continue;
3408 1.1 christos }
3409 1.1 christos
3410 1.1 christos /* Check for zombie thread group leaders. Those can't be reaped
3411 1.1 christos until all other threads in the thread group are. */
3412 1.1 christos check_zombie_leaders ();
3413 1.1 christos
3414 1.1 christos /* If there are no resumed children left, bail. We'd be stuck
3415 1.1 christos forever in the sigsuspend call below otherwise. */
3416 1.1 christos if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
3417 1.1 christos {
3418 1.1 christos if (debug_linux_nat)
3419 1.1 christos fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
3420 1.1 christos
3421 1.1 christos ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3422 1.1 christos
3423 1.1 christos if (!target_can_async_p ())
3424 1.1 christos clear_sigint_trap ();
3425 1.1 christos
3426 1.1 christos restore_child_signals_mask (&prev_mask);
3427 1.1 christos return minus_one_ptid;
3428 1.1 christos }
3429 1.1 christos
3430 1.1 christos /* No interesting event to report to the core. */
3431 1.1 christos
3432 1.1 christos if (target_options & TARGET_WNOHANG)
3433 1.1 christos {
3434 1.1 christos if (debug_linux_nat)
3435 1.1 christos fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
3436 1.1 christos
3437 1.1 christos ourstatus->kind = TARGET_WAITKIND_IGNORE;
3438 1.1 christos restore_child_signals_mask (&prev_mask);
3439 1.1 christos return minus_one_ptid;
3440 1.1 christos }
3441 1.1 christos
3442 1.1 christos /* We shouldn't end up here unless we want to try again. */
3443 1.1 christos gdb_assert (lp == NULL);
3444 1.1 christos
3445 1.1 christos /* Block until we get an event reported with SIGCHLD. */
3446 1.1 christos sigsuspend (&suspend_mask);
3447 1.1 christos }
3448 1.1 christos
3449 1.1 christos if (!target_can_async_p ())
3450 1.1 christos clear_sigint_trap ();
3451 1.1 christos
3452 1.1 christos gdb_assert (lp);
3453 1.1 christos
3454 1.1 christos status = lp->status;
3455 1.1 christos lp->status = 0;
3456 1.1 christos
3457 1.1 christos /* Don't report signals that GDB isn't interested in, such as
3458 1.1 christos signals that are neither printed nor stopped upon. Stopping all
3459 1.1 christos threads can be a bit time-consuming so if we want decent
3460 1.1 christos performance with heavily multi-threaded programs, especially when
3461 1.1 christos they're using a high frequency timer, we'd better avoid it if we
3462 1.1 christos can. */
3463 1.1 christos
3464 1.1 christos if (WIFSTOPPED (status))
3465 1.1 christos {
3466 1.1 christos enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
3467 1.1 christos
3468 1.1 christos /* When using hardware single-step, we need to report every signal.
3469 1.1 christos Otherwise, signals in pass_mask may be short-circuited. */
3470 1.1 christos if (!lp->step
3471 1.1 christos && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)))
3472 1.1 christos {
3473 1.1 christos /* FIMXE: kettenis/2001-06-06: Should we resume all threads
3474 1.1 christos here? It is not clear we should. GDB may not expect
3475 1.1 christos other threads to run. On the other hand, not resuming
3476 1.1 christos newly attached threads may cause an unwanted delay in
3477 1.1 christos getting them running. */
3478 1.1 christos registers_changed ();
3479 1.1 christos if (linux_nat_prepare_to_resume != NULL)
3480 1.1 christos linux_nat_prepare_to_resume (lp);
3481 1.1 christos linux_ops->to_resume (linux_ops,
3482 1.1 christos pid_to_ptid (ptid_get_lwp (lp->ptid)),
3483 1.1 christos lp->step, signo);
3484 1.1 christos if (debug_linux_nat)
3485 1.1 christos fprintf_unfiltered (gdb_stdlog,
3486 1.1 christos "LLW: %s %s, %s (preempt 'handle')\n",
3487 1.1 christos lp->step ?
3488 1.1 christos "PTRACE_SINGLESTEP" : "PTRACE_CONT",
3489 1.1 christos target_pid_to_str (lp->ptid),
3490 1.1 christos (signo != GDB_SIGNAL_0
3491 1.1 christos ? strsignal (gdb_signal_to_host (signo))
3492 1.1 christos : "0"));
3493 1.1 christos lp->stopped = 0;
3494 1.1 christos goto retry;
3495 1.1 christos }
3496 1.1 christos
3497 1.1 christos if (!non_stop)
3498 1.1 christos {
3499 1.1 christos /* Only do the below in all-stop, as we currently use SIGINT
3500 1.1 christos to implement target_stop (see linux_nat_stop) in
3501 1.1 christos non-stop. */
3502 1.1 christos if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
3503 1.1 christos {
3504 1.1 christos /* If ^C/BREAK is typed at the tty/console, SIGINT gets
3505 1.1 christos forwarded to the entire process group, that is, all LWPs
3506 1.1 christos will receive it - unless they're using CLONE_THREAD to
3507 1.1 christos share signals. Since we only want to report it once, we
3508 1.1 christos mark it as ignored for all LWPs except this one. */
3509 1.1 christos iterate_over_lwps (pid_to_ptid (ptid_get_pid (ptid)),
3510 1.1 christos set_ignore_sigint, NULL);
3511 1.1 christos lp->ignore_sigint = 0;
3512 1.1 christos }
3513 1.1 christos else
3514 1.1 christos maybe_clear_ignore_sigint (lp);
3515 1.1 christos }
3516 1.1 christos }
3517 1.1 christos
3518 1.1 christos /* This LWP is stopped now. */
3519 1.1 christos lp->stopped = 1;
3520 1.1 christos
3521 1.1 christos if (debug_linux_nat)
3522 1.1 christos fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
3523 1.1 christos status_to_str (status), target_pid_to_str (lp->ptid));
3524 1.1 christos
3525 1.1 christos if (!non_stop)
3526 1.1 christos {
3527 1.1 christos /* Now stop all other LWP's ... */
3528 1.1 christos iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
3529 1.1 christos
3530 1.1 christos /* ... and wait until all of them have reported back that
3531 1.1 christos they're no longer running. */
3532 1.1 christos iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
3533 1.1 christos
3534 1.1 christos /* If we're not waiting for a specific LWP, choose an event LWP
3535 1.1 christos from among those that have had events. Giving equal priority
3536 1.1 christos to all LWPs that have had events helps prevent
3537 1.1 christos starvation. */
3538 1.1 christos if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
3539 1.1 christos select_event_lwp (ptid, &lp, &status);
3540 1.1 christos
3541 1.1 christos /* Now that we've selected our final event LWP, cancel any
3542 1.1 christos breakpoints in other LWPs that have hit a GDB breakpoint.
3543 1.1 christos See the comment in cancel_breakpoints_callback to find out
3544 1.1 christos why. */
3545 1.1 christos iterate_over_lwps (minus_one_ptid, cancel_breakpoints_callback, lp);
3546 1.1 christos
3547 1.1 christos /* We'll need this to determine whether to report a SIGSTOP as
3548 1.1 christos TARGET_WAITKIND_0. Need to take a copy because
3549 1.1 christos resume_clear_callback clears it. */
3550 1.1 christos last_resume_kind = lp->last_resume_kind;
3551 1.1 christos
3552 1.1 christos /* In all-stop, from the core's perspective, all LWPs are now
3553 1.1 christos stopped until a new resume action is sent over. */
3554 1.1 christos iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
3555 1.1 christos }
3556 1.1 christos else
3557 1.1 christos {
3558 1.1 christos /* See above. */
3559 1.1 christos last_resume_kind = lp->last_resume_kind;
3560 1.1 christos resume_clear_callback (lp, NULL);
3561 1.1 christos }
3562 1.1 christos
3563 1.1 christos if (linux_nat_status_is_event (status))
3564 1.1 christos {
3565 1.1 christos if (debug_linux_nat)
3566 1.1 christos fprintf_unfiltered (gdb_stdlog,
3567 1.1 christos "LLW: trap ptid is %s.\n",
3568 1.1 christos target_pid_to_str (lp->ptid));
3569 1.1 christos }
3570 1.1 christos
3571 1.1 christos if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3572 1.1 christos {
3573 1.1 christos *ourstatus = lp->waitstatus;
3574 1.1 christos lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3575 1.1 christos }
3576 1.1 christos else
3577 1.1 christos store_waitstatus (ourstatus, status);
3578 1.1 christos
3579 1.1 christos if (debug_linux_nat)
3580 1.1 christos fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
3581 1.1 christos
3582 1.1 christos restore_child_signals_mask (&prev_mask);
3583 1.1 christos
3584 1.1 christos if (last_resume_kind == resume_stop
3585 1.1 christos && ourstatus->kind == TARGET_WAITKIND_STOPPED
3586 1.1 christos && WSTOPSIG (status) == SIGSTOP)
3587 1.1 christos {
3588 1.1 christos /* A thread that has been requested to stop by GDB with
3589 1.1 christos target_stop, and it stopped cleanly, so report as SIG0. The
3590 1.1 christos use of SIGSTOP is an implementation detail. */
3591 1.1 christos ourstatus->value.sig = GDB_SIGNAL_0;
3592 1.1 christos }
3593 1.1 christos
3594 1.1 christos if (ourstatus->kind == TARGET_WAITKIND_EXITED
3595 1.1 christos || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
3596 1.1 christos lp->core = -1;
3597 1.1 christos else
3598 1.1 christos lp->core = linux_common_core_of_thread (lp->ptid);
3599 1.1 christos
3600 1.1 christos return lp->ptid;
3601 1.1 christos }
3602 1.1 christos
3603 1.1 christos /* Resume LWPs that are currently stopped without any pending status
3604 1.1 christos to report, but are resumed from the core's perspective. */
3605 1.1 christos
3606 1.1 christos static int
3607 1.1 christos resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
3608 1.1 christos {
3609 1.1 christos ptid_t *wait_ptid_p = data;
3610 1.1 christos
3611 1.1 christos if (lp->stopped
3612 1.1 christos && lp->resumed
3613 1.1 christos && lp->status == 0
3614 1.1 christos && lp->waitstatus.kind == TARGET_WAITKIND_IGNORE)
3615 1.1 christos {
3616 1.1 christos struct regcache *regcache = get_thread_regcache (lp->ptid);
3617 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
3618 1.1 christos CORE_ADDR pc = regcache_read_pc (regcache);
3619 1.1 christos
3620 1.1 christos gdb_assert (is_executing (lp->ptid));
3621 1.1 christos
3622 1.1 christos /* Don't bother if there's a breakpoint at PC that we'd hit
3623 1.1 christos immediately, and we're not waiting for this LWP. */
3624 1.1 christos if (!ptid_match (lp->ptid, *wait_ptid_p))
3625 1.1 christos {
3626 1.1 christos if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
3627 1.1 christos return 0;
3628 1.1 christos }
3629 1.1 christos
3630 1.1 christos if (debug_linux_nat)
3631 1.1 christos fprintf_unfiltered (gdb_stdlog,
3632 1.1 christos "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
3633 1.1 christos target_pid_to_str (lp->ptid),
3634 1.1 christos paddress (gdbarch, pc),
3635 1.1 christos lp->step);
3636 1.1 christos
3637 1.1 christos registers_changed ();
3638 1.1 christos if (linux_nat_prepare_to_resume != NULL)
3639 1.1 christos linux_nat_prepare_to_resume (lp);
3640 1.1 christos linux_ops->to_resume (linux_ops, pid_to_ptid (ptid_get_lwp (lp->ptid)),
3641 1.1 christos lp->step, GDB_SIGNAL_0);
3642 1.1 christos lp->stopped = 0;
3643 1.1 christos lp->stopped_by_watchpoint = 0;
3644 1.1 christos }
3645 1.1 christos
3646 1.1 christos return 0;
3647 1.1 christos }
3648 1.1 christos
3649 1.1 christos static ptid_t
3650 1.1 christos linux_nat_wait (struct target_ops *ops,
3651 1.1 christos ptid_t ptid, struct target_waitstatus *ourstatus,
3652 1.1 christos int target_options)
3653 1.1 christos {
3654 1.1 christos ptid_t event_ptid;
3655 1.1 christos
3656 1.1 christos if (debug_linux_nat)
3657 1.1 christos {
3658 1.1 christos char *options_string;
3659 1.1 christos
3660 1.1 christos options_string = target_options_to_string (target_options);
3661 1.1 christos fprintf_unfiltered (gdb_stdlog,
3662 1.1 christos "linux_nat_wait: [%s], [%s]\n",
3663 1.1 christos target_pid_to_str (ptid),
3664 1.1 christos options_string);
3665 1.1 christos xfree (options_string);
3666 1.1 christos }
3667 1.1 christos
3668 1.1 christos /* Flush the async file first. */
3669 1.1 christos if (target_can_async_p ())
3670 1.1 christos async_file_flush ();
3671 1.1 christos
3672 1.1 christos /* Resume LWPs that are currently stopped without any pending status
3673 1.1 christos to report, but are resumed from the core's perspective. LWPs get
3674 1.1 christos in this state if we find them stopping at a time we're not
3675 1.1 christos interested in reporting the event (target_wait on a
3676 1.1 christos specific_process, for example, see linux_nat_wait_1), and
3677 1.1 christos meanwhile the event became uninteresting. Don't bother resuming
3678 1.1 christos LWPs we're not going to wait for if they'd stop immediately. */
3679 1.1 christos if (non_stop)
3680 1.1 christos iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
3681 1.1 christos
3682 1.1 christos event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
3683 1.1 christos
3684 1.1 christos /* If we requested any event, and something came out, assume there
3685 1.1 christos may be more. If we requested a specific lwp or process, also
3686 1.1 christos assume there may be more. */
3687 1.1 christos if (target_can_async_p ()
3688 1.1 christos && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
3689 1.1 christos && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
3690 1.1 christos || !ptid_equal (ptid, minus_one_ptid)))
3691 1.1 christos async_file_mark ();
3692 1.1 christos
3693 1.1 christos /* Get ready for the next event. */
3694 1.1 christos if (target_can_async_p ())
3695 1.1 christos target_async (inferior_event_handler, 0);
3696 1.1 christos
3697 1.1 christos return event_ptid;
3698 1.1 christos }
3699 1.1 christos
3700 1.1 christos static int
3701 1.1 christos kill_callback (struct lwp_info *lp, void *data)
3702 1.1 christos {
3703 1.1 christos /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
3704 1.1 christos
3705 1.1 christos errno = 0;
3706 1.1 christos kill (ptid_get_lwp (lp->ptid), SIGKILL);
3707 1.1 christos if (debug_linux_nat)
3708 1.1 christos fprintf_unfiltered (gdb_stdlog,
3709 1.1 christos "KC: kill (SIGKILL) %s, 0, 0 (%s)\n",
3710 1.1 christos target_pid_to_str (lp->ptid),
3711 1.1 christos errno ? safe_strerror (errno) : "OK");
3712 1.1 christos
3713 1.1 christos /* Some kernels ignore even SIGKILL for processes under ptrace. */
3714 1.1 christos
3715 1.1 christos errno = 0;
3716 1.1 christos ptrace (PTRACE_KILL, ptid_get_lwp (lp->ptid), 0, 0);
3717 1.1 christos if (debug_linux_nat)
3718 1.1 christos fprintf_unfiltered (gdb_stdlog,
3719 1.1 christos "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
3720 1.1 christos target_pid_to_str (lp->ptid),
3721 1.1 christos errno ? safe_strerror (errno) : "OK");
3722 1.1 christos
3723 1.1 christos return 0;
3724 1.1 christos }
3725 1.1 christos
3726 1.1 christos static int
3727 1.1 christos kill_wait_callback (struct lwp_info *lp, void *data)
3728 1.1 christos {
3729 1.1 christos pid_t pid;
3730 1.1 christos
3731 1.1 christos /* We must make sure that there are no pending events (delayed
3732 1.1 christos SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
3733 1.1 christos program doesn't interfere with any following debugging session. */
3734 1.1 christos
3735 1.1 christos /* For cloned processes we must check both with __WCLONE and
3736 1.1 christos without, since the exit status of a cloned process isn't reported
3737 1.1 christos with __WCLONE. */
3738 1.1 christos if (lp->cloned)
3739 1.1 christos {
3740 1.1 christos do
3741 1.1 christos {
3742 1.1 christos pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, __WCLONE);
3743 1.1 christos if (pid != (pid_t) -1)
3744 1.1 christos {
3745 1.1 christos if (debug_linux_nat)
3746 1.1 christos fprintf_unfiltered (gdb_stdlog,
3747 1.1 christos "KWC: wait %s received unknown.\n",
3748 1.1 christos target_pid_to_str (lp->ptid));
3749 1.1 christos /* The Linux kernel sometimes fails to kill a thread
3750 1.1 christos completely after PTRACE_KILL; that goes from the stop
3751 1.1 christos point in do_fork out to the one in
3752 1.1 christos get_signal_to_deliever and waits again. So kill it
3753 1.1 christos again. */
3754 1.1 christos kill_callback (lp, NULL);
3755 1.1 christos }
3756 1.1 christos }
3757 1.1 christos while (pid == ptid_get_lwp (lp->ptid));
3758 1.1 christos
3759 1.1 christos gdb_assert (pid == -1 && errno == ECHILD);
3760 1.1 christos }
3761 1.1 christos
3762 1.1 christos do
3763 1.1 christos {
3764 1.1 christos pid = my_waitpid (ptid_get_lwp (lp->ptid), NULL, 0);
3765 1.1 christos if (pid != (pid_t) -1)
3766 1.1 christos {
3767 1.1 christos if (debug_linux_nat)
3768 1.1 christos fprintf_unfiltered (gdb_stdlog,
3769 1.1 christos "KWC: wait %s received unk.\n",
3770 1.1 christos target_pid_to_str (lp->ptid));
3771 1.1 christos /* See the call to kill_callback above. */
3772 1.1 christos kill_callback (lp, NULL);
3773 1.1 christos }
3774 1.1 christos }
3775 1.1 christos while (pid == ptid_get_lwp (lp->ptid));
3776 1.1 christos
3777 1.1 christos gdb_assert (pid == -1 && errno == ECHILD);
3778 1.1 christos return 0;
3779 1.1 christos }
3780 1.1 christos
3781 1.1 christos static void
3782 1.1 christos linux_nat_kill (struct target_ops *ops)
3783 1.1 christos {
3784 1.1 christos struct target_waitstatus last;
3785 1.1 christos ptid_t last_ptid;
3786 1.1 christos int status;
3787 1.1 christos
3788 1.1 christos /* If we're stopped while forking and we haven't followed yet,
3789 1.1 christos kill the other task. We need to do this first because the
3790 1.1 christos parent will be sleeping if this is a vfork. */
3791 1.1 christos
3792 1.1 christos get_last_target_status (&last_ptid, &last);
3793 1.1 christos
3794 1.1 christos if (last.kind == TARGET_WAITKIND_FORKED
3795 1.1 christos || last.kind == TARGET_WAITKIND_VFORKED)
3796 1.1 christos {
3797 1.1 christos ptrace (PT_KILL, ptid_get_pid (last.value.related_pid), 0, 0);
3798 1.1 christos wait (&status);
3799 1.1 christos
3800 1.1 christos /* Let the arch-specific native code know this process is
3801 1.1 christos gone. */
3802 1.1 christos linux_nat_forget_process (ptid_get_pid (last.value.related_pid));
3803 1.1 christos }
3804 1.1 christos
3805 1.1 christos if (forks_exist_p ())
3806 1.1 christos linux_fork_killall ();
3807 1.1 christos else
3808 1.1 christos {
3809 1.1 christos ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
3810 1.1 christos
3811 1.1 christos /* Stop all threads before killing them, since ptrace requires
3812 1.1 christos that the thread is stopped to sucessfully PTRACE_KILL. */
3813 1.1 christos iterate_over_lwps (ptid, stop_callback, NULL);
3814 1.1 christos /* ... and wait until all of them have reported back that
3815 1.1 christos they're no longer running. */
3816 1.1 christos iterate_over_lwps (ptid, stop_wait_callback, NULL);
3817 1.1 christos
3818 1.1 christos /* Kill all LWP's ... */
3819 1.1 christos iterate_over_lwps (ptid, kill_callback, NULL);
3820 1.1 christos
3821 1.1 christos /* ... and wait until we've flushed all events. */
3822 1.1 christos iterate_over_lwps (ptid, kill_wait_callback, NULL);
3823 1.1 christos }
3824 1.1 christos
3825 1.1 christos target_mourn_inferior ();
3826 1.1 christos }
3827 1.1 christos
3828 1.1 christos static void
3829 1.1 christos linux_nat_mourn_inferior (struct target_ops *ops)
3830 1.1 christos {
3831 1.1 christos int pid = ptid_get_pid (inferior_ptid);
3832 1.1 christos
3833 1.1 christos purge_lwp_list (pid);
3834 1.1 christos
3835 1.1 christos if (! forks_exist_p ())
3836 1.1 christos /* Normal case, no other forks available. */
3837 1.1 christos linux_ops->to_mourn_inferior (ops);
3838 1.1 christos else
3839 1.1 christos /* Multi-fork case. The current inferior_ptid has exited, but
3840 1.1 christos there are other viable forks to debug. Delete the exiting
3841 1.1 christos one and context-switch to the first available. */
3842 1.1 christos linux_fork_mourn_inferior ();
3843 1.1 christos
3844 1.1 christos /* Let the arch-specific native code know this process is gone. */
3845 1.1 christos linux_nat_forget_process (pid);
3846 1.1 christos }
3847 1.1 christos
3848 1.1 christos /* Convert a native/host siginfo object, into/from the siginfo in the
3849 1.1 christos layout of the inferiors' architecture. */
3850 1.1 christos
3851 1.1 christos static void
3852 1.1 christos siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
3853 1.1 christos {
3854 1.1 christos int done = 0;
3855 1.1 christos
3856 1.1 christos if (linux_nat_siginfo_fixup != NULL)
3857 1.1 christos done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
3858 1.1 christos
3859 1.1 christos /* If there was no callback, or the callback didn't do anything,
3860 1.1 christos then just do a straight memcpy. */
3861 1.1 christos if (!done)
3862 1.1 christos {
3863 1.1 christos if (direction == 1)
3864 1.1 christos memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
3865 1.1 christos else
3866 1.1 christos memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
3867 1.1 christos }
3868 1.1 christos }
3869 1.1 christos
3870 1.1 christos static LONGEST
3871 1.1 christos linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
3872 1.1 christos const char *annex, gdb_byte *readbuf,
3873 1.1 christos const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
3874 1.1 christos {
3875 1.1 christos int pid;
3876 1.1 christos siginfo_t siginfo;
3877 1.1 christos gdb_byte inf_siginfo[sizeof (siginfo_t)];
3878 1.1 christos
3879 1.1 christos gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
3880 1.1 christos gdb_assert (readbuf || writebuf);
3881 1.1 christos
3882 1.1 christos pid = ptid_get_lwp (inferior_ptid);
3883 1.1 christos if (pid == 0)
3884 1.1 christos pid = ptid_get_pid (inferior_ptid);
3885 1.1 christos
3886 1.1 christos if (offset > sizeof (siginfo))
3887 1.1 christos return -1;
3888 1.1 christos
3889 1.1 christos errno = 0;
3890 1.1 christos ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3891 1.1 christos if (errno != 0)
3892 1.1 christos return -1;
3893 1.1 christos
3894 1.1 christos /* When GDB is built as a 64-bit application, ptrace writes into
3895 1.1 christos SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3896 1.1 christos inferior with a 64-bit GDB should look the same as debugging it
3897 1.1 christos with a 32-bit GDB, we need to convert it. GDB core always sees
3898 1.1 christos the converted layout, so any read/write will have to be done
3899 1.1 christos post-conversion. */
3900 1.1 christos siginfo_fixup (&siginfo, inf_siginfo, 0);
3901 1.1 christos
3902 1.1 christos if (offset + len > sizeof (siginfo))
3903 1.1 christos len = sizeof (siginfo) - offset;
3904 1.1 christos
3905 1.1 christos if (readbuf != NULL)
3906 1.1 christos memcpy (readbuf, inf_siginfo + offset, len);
3907 1.1 christos else
3908 1.1 christos {
3909 1.1 christos memcpy (inf_siginfo + offset, writebuf, len);
3910 1.1 christos
3911 1.1 christos /* Convert back to ptrace layout before flushing it out. */
3912 1.1 christos siginfo_fixup (&siginfo, inf_siginfo, 1);
3913 1.1 christos
3914 1.1 christos errno = 0;
3915 1.1 christos ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
3916 1.1 christos if (errno != 0)
3917 1.1 christos return -1;
3918 1.1 christos }
3919 1.1 christos
3920 1.1 christos return len;
3921 1.1 christos }
3922 1.1 christos
3923 1.1 christos static LONGEST
3924 1.1 christos linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
3925 1.1 christos const char *annex, gdb_byte *readbuf,
3926 1.1 christos const gdb_byte *writebuf,
3927 1.1 christos ULONGEST offset, LONGEST len)
3928 1.1 christos {
3929 1.1 christos struct cleanup *old_chain;
3930 1.1 christos LONGEST xfer;
3931 1.1 christos
3932 1.1 christos if (object == TARGET_OBJECT_SIGNAL_INFO)
3933 1.1 christos return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
3934 1.1 christos offset, len);
3935 1.1 christos
3936 1.1 christos /* The target is connected but no live inferior is selected. Pass
3937 1.1 christos this request down to a lower stratum (e.g., the executable
3938 1.1 christos file). */
3939 1.1 christos if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
3940 1.1 christos return 0;
3941 1.1 christos
3942 1.1 christos old_chain = save_inferior_ptid ();
3943 1.1 christos
3944 1.1 christos if (ptid_lwp_p (inferior_ptid))
3945 1.1 christos inferior_ptid = pid_to_ptid (ptid_get_lwp (inferior_ptid));
3946 1.1 christos
3947 1.1 christos xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
3948 1.1 christos offset, len);
3949 1.1 christos
3950 1.1 christos do_cleanups (old_chain);
3951 1.1 christos return xfer;
3952 1.1 christos }
3953 1.1 christos
3954 1.1 christos static int
3955 1.1 christos linux_thread_alive (ptid_t ptid)
3956 1.1 christos {
3957 1.1 christos int err, tmp_errno;
3958 1.1 christos
3959 1.1 christos gdb_assert (ptid_lwp_p (ptid));
3960 1.1 christos
3961 1.1 christos /* Send signal 0 instead of anything ptrace, because ptracing a
3962 1.1 christos running thread errors out claiming that the thread doesn't
3963 1.1 christos exist. */
3964 1.1 christos err = kill_lwp (ptid_get_lwp (ptid), 0);
3965 1.1 christos tmp_errno = errno;
3966 1.1 christos if (debug_linux_nat)
3967 1.1 christos fprintf_unfiltered (gdb_stdlog,
3968 1.1 christos "LLTA: KILL(SIG0) %s (%s)\n",
3969 1.1 christos target_pid_to_str (ptid),
3970 1.1 christos err ? safe_strerror (tmp_errno) : "OK");
3971 1.1 christos
3972 1.1 christos if (err != 0)
3973 1.1 christos return 0;
3974 1.1 christos
3975 1.1 christos return 1;
3976 1.1 christos }
3977 1.1 christos
3978 1.1 christos static int
3979 1.1 christos linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
3980 1.1 christos {
3981 1.1 christos return linux_thread_alive (ptid);
3982 1.1 christos }
3983 1.1 christos
3984 1.1 christos static char *
3985 1.1 christos linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
3986 1.1 christos {
3987 1.1 christos static char buf[64];
3988 1.1 christos
3989 1.1 christos if (ptid_lwp_p (ptid)
3990 1.1 christos && (ptid_get_pid (ptid) != ptid_get_lwp (ptid)
3991 1.1 christos || num_lwps (ptid_get_pid (ptid)) > 1))
3992 1.1 christos {
3993 1.1 christos snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
3994 1.1 christos return buf;
3995 1.1 christos }
3996 1.1 christos
3997 1.1 christos return normal_pid_to_str (ptid);
3998 1.1 christos }
3999 1.1 christos
4000 1.1 christos static char *
4001 1.1 christos linux_nat_thread_name (struct thread_info *thr)
4002 1.1 christos {
4003 1.1 christos int pid = ptid_get_pid (thr->ptid);
4004 1.1 christos long lwp = ptid_get_lwp (thr->ptid);
4005 1.1 christos #define FORMAT "/proc/%d/task/%ld/comm"
4006 1.1 christos char buf[sizeof (FORMAT) + 30];
4007 1.1 christos FILE *comm_file;
4008 1.1 christos char *result = NULL;
4009 1.1 christos
4010 1.1 christos snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
4011 1.1 christos comm_file = gdb_fopen_cloexec (buf, "r");
4012 1.1 christos if (comm_file)
4013 1.1 christos {
4014 1.1 christos /* Not exported by the kernel, so we define it here. */
4015 1.1 christos #define COMM_LEN 16
4016 1.1 christos static char line[COMM_LEN + 1];
4017 1.1 christos
4018 1.1 christos if (fgets (line, sizeof (line), comm_file))
4019 1.1 christos {
4020 1.1 christos char *nl = strchr (line, '\n');
4021 1.1 christos
4022 1.1 christos if (nl)
4023 1.1 christos *nl = '\0';
4024 1.1 christos if (*line != '\0')
4025 1.1 christos result = line;
4026 1.1 christos }
4027 1.1 christos
4028 1.1 christos fclose (comm_file);
4029 1.1 christos }
4030 1.1 christos
4031 1.1 christos #undef COMM_LEN
4032 1.1 christos #undef FORMAT
4033 1.1 christos
4034 1.1 christos return result;
4035 1.1 christos }
4036 1.1 christos
4037 1.1 christos /* Accepts an integer PID; Returns a string representing a file that
4038 1.1 christos can be opened to get the symbols for the child process. */
4039 1.1 christos
4040 1.1 christos static char *
4041 1.1 christos linux_child_pid_to_exec_file (int pid)
4042 1.1 christos {
4043 1.1 christos char *name1, *name2;
4044 1.1 christos
4045 1.1 christos name1 = xmalloc (PATH_MAX);
4046 1.1 christos name2 = xmalloc (PATH_MAX);
4047 1.1 christos make_cleanup (xfree, name1);
4048 1.1 christos make_cleanup (xfree, name2);
4049 1.1 christos memset (name2, 0, PATH_MAX);
4050 1.1 christos
4051 1.1 christos xsnprintf (name1, PATH_MAX, "/proc/%d/exe", pid);
4052 1.1 christos if (readlink (name1, name2, PATH_MAX - 1) > 0)
4053 1.1 christos return name2;
4054 1.1 christos else
4055 1.1 christos return name1;
4056 1.1 christos }
4057 1.1 christos
4058 1.1 christos /* Records the thread's register state for the corefile note
4059 1.1 christos section. */
4060 1.1 christos
4061 1.1 christos static char *
4062 1.1 christos linux_nat_collect_thread_registers (const struct regcache *regcache,
4063 1.1 christos ptid_t ptid, bfd *obfd,
4064 1.1 christos char *note_data, int *note_size,
4065 1.1 christos enum gdb_signal stop_signal)
4066 1.1 christos {
4067 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
4068 1.1 christos const struct regset *regset;
4069 1.1 christos int core_regset_p;
4070 1.1 christos gdb_gregset_t gregs;
4071 1.1 christos gdb_fpregset_t fpregs;
4072 1.1 christos
4073 1.1 christos core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
4074 1.1 christos
4075 1.1 christos if (core_regset_p
4076 1.1 christos && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
4077 1.1 christos sizeof (gregs)))
4078 1.1 christos != NULL && regset->collect_regset != NULL)
4079 1.1 christos regset->collect_regset (regset, regcache, -1, &gregs, sizeof (gregs));
4080 1.1 christos else
4081 1.1 christos fill_gregset (regcache, &gregs, -1);
4082 1.1 christos
4083 1.1 christos note_data = (char *) elfcore_write_prstatus
4084 1.1 christos (obfd, note_data, note_size, ptid_get_lwp (ptid),
4085 1.1 christos gdb_signal_to_host (stop_signal), &gregs);
4086 1.1 christos
4087 1.1 christos if (core_regset_p
4088 1.1 christos && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
4089 1.1 christos sizeof (fpregs)))
4090 1.1 christos != NULL && regset->collect_regset != NULL)
4091 1.1 christos regset->collect_regset (regset, regcache, -1, &fpregs, sizeof (fpregs));
4092 1.1 christos else
4093 1.1 christos fill_fpregset (regcache, &fpregs, -1);
4094 1.1 christos
4095 1.1 christos note_data = (char *) elfcore_write_prfpreg (obfd, note_data, note_size,
4096 1.1 christos &fpregs, sizeof (fpregs));
4097 1.1 christos
4098 1.1 christos return note_data;
4099 1.1 christos }
4100 1.1 christos
4101 1.1 christos /* Fills the "to_make_corefile_note" target vector. Builds the note
4102 1.1 christos section for a corefile, and returns it in a malloc buffer. */
4103 1.1 christos
4104 1.1 christos static char *
4105 1.1 christos linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
4106 1.1 christos {
4107 1.1 christos /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
4108 1.1 christos converted to gdbarch_core_regset_sections, this function can go away. */
4109 1.1 christos return linux_make_corefile_notes (target_gdbarch (), obfd, note_size,
4110 1.1 christos linux_nat_collect_thread_registers);
4111 1.1 christos }
4112 1.1 christos
4113 1.1 christos /* Implement the to_xfer_partial interface for memory reads using the /proc
4114 1.1 christos filesystem. Because we can use a single read() call for /proc, this
4115 1.1 christos can be much more efficient than banging away at PTRACE_PEEKTEXT,
4116 1.1 christos but it doesn't support writes. */
4117 1.1 christos
4118 1.1 christos static LONGEST
4119 1.1 christos linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
4120 1.1 christos const char *annex, gdb_byte *readbuf,
4121 1.1 christos const gdb_byte *writebuf,
4122 1.1 christos ULONGEST offset, LONGEST len)
4123 1.1 christos {
4124 1.1 christos LONGEST ret;
4125 1.1 christos int fd;
4126 1.1 christos char filename[64];
4127 1.1 christos
4128 1.1 christos if (object != TARGET_OBJECT_MEMORY || !readbuf)
4129 1.1 christos return 0;
4130 1.1 christos
4131 1.1 christos /* Don't bother for one word. */
4132 1.1 christos if (len < 3 * sizeof (long))
4133 1.1 christos return 0;
4134 1.1 christos
4135 1.1 christos /* We could keep this file open and cache it - possibly one per
4136 1.1 christos thread. That requires some juggling, but is even faster. */
4137 1.1 christos xsnprintf (filename, sizeof filename, "/proc/%d/mem",
4138 1.1 christos ptid_get_pid (inferior_ptid));
4139 1.1 christos fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
4140 1.1 christos if (fd == -1)
4141 1.1 christos return 0;
4142 1.1 christos
4143 1.1 christos /* If pread64 is available, use it. It's faster if the kernel
4144 1.1 christos supports it (only one syscall), and it's 64-bit safe even on
4145 1.1 christos 32-bit platforms (for instance, SPARC debugging a SPARC64
4146 1.1 christos application). */
4147 1.1 christos #ifdef HAVE_PREAD64
4148 1.1 christos if (pread64 (fd, readbuf, len, offset) != len)
4149 1.1 christos #else
4150 1.1 christos if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
4151 1.1 christos #endif
4152 1.1 christos ret = 0;
4153 1.1 christos else
4154 1.1 christos ret = len;
4155 1.1 christos
4156 1.1 christos close (fd);
4157 1.1 christos return ret;
4158 1.1 christos }
4159 1.1 christos
4160 1.1 christos
4161 1.1 christos /* Enumerate spufs IDs for process PID. */
4162 1.1 christos static LONGEST
4163 1.1 christos spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, LONGEST len)
4164 1.1 christos {
4165 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
4166 1.1 christos LONGEST pos = 0;
4167 1.1 christos LONGEST written = 0;
4168 1.1 christos char path[128];
4169 1.1 christos DIR *dir;
4170 1.1 christos struct dirent *entry;
4171 1.1 christos
4172 1.1 christos xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
4173 1.1 christos dir = opendir (path);
4174 1.1 christos if (!dir)
4175 1.1 christos return -1;
4176 1.1 christos
4177 1.1 christos rewinddir (dir);
4178 1.1 christos while ((entry = readdir (dir)) != NULL)
4179 1.1 christos {
4180 1.1 christos struct stat st;
4181 1.1 christos struct statfs stfs;
4182 1.1 christos int fd;
4183 1.1 christos
4184 1.1 christos fd = atoi (entry->d_name);
4185 1.1 christos if (!fd)
4186 1.1 christos continue;
4187 1.1 christos
4188 1.1 christos xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
4189 1.1 christos if (stat (path, &st) != 0)
4190 1.1 christos continue;
4191 1.1 christos if (!S_ISDIR (st.st_mode))
4192 1.1 christos continue;
4193 1.1 christos
4194 1.1 christos if (statfs (path, &stfs) != 0)
4195 1.1 christos continue;
4196 1.1 christos if (stfs.f_type != SPUFS_MAGIC)
4197 1.1 christos continue;
4198 1.1 christos
4199 1.1 christos if (pos >= offset && pos + 4 <= offset + len)
4200 1.1 christos {
4201 1.1 christos store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
4202 1.1 christos written += 4;
4203 1.1 christos }
4204 1.1 christos pos += 4;
4205 1.1 christos }
4206 1.1 christos
4207 1.1 christos closedir (dir);
4208 1.1 christos return written;
4209 1.1 christos }
4210 1.1 christos
4211 1.1 christos /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
4212 1.1 christos object type, using the /proc file system. */
4213 1.1 christos static LONGEST
4214 1.1 christos linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
4215 1.1 christos const char *annex, gdb_byte *readbuf,
4216 1.1 christos const gdb_byte *writebuf,
4217 1.1 christos ULONGEST offset, LONGEST len)
4218 1.1 christos {
4219 1.1 christos char buf[128];
4220 1.1 christos int fd = 0;
4221 1.1 christos int ret = -1;
4222 1.1 christos int pid = ptid_get_pid (inferior_ptid);
4223 1.1 christos
4224 1.1 christos if (!annex)
4225 1.1 christos {
4226 1.1 christos if (!readbuf)
4227 1.1 christos return -1;
4228 1.1 christos else
4229 1.1 christos return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4230 1.1 christos }
4231 1.1 christos
4232 1.1 christos xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
4233 1.1 christos fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
4234 1.1 christos if (fd <= 0)
4235 1.1 christos return -1;
4236 1.1 christos
4237 1.1 christos if (offset != 0
4238 1.1 christos && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4239 1.1 christos {
4240 1.1 christos close (fd);
4241 1.1 christos return 0;
4242 1.1 christos }
4243 1.1 christos
4244 1.1 christos if (writebuf)
4245 1.1 christos ret = write (fd, writebuf, (size_t) len);
4246 1.1 christos else if (readbuf)
4247 1.1 christos ret = read (fd, readbuf, (size_t) len);
4248 1.1 christos
4249 1.1 christos close (fd);
4250 1.1 christos return ret;
4251 1.1 christos }
4252 1.1 christos
4253 1.1 christos
4254 1.1 christos /* Parse LINE as a signal set and add its set bits to SIGS. */
4255 1.1 christos
4256 1.1 christos static void
4257 1.1 christos add_line_to_sigset (const char *line, sigset_t *sigs)
4258 1.1 christos {
4259 1.1 christos int len = strlen (line) - 1;
4260 1.1 christos const char *p;
4261 1.1 christos int signum;
4262 1.1 christos
4263 1.1 christos if (line[len] != '\n')
4264 1.1 christos error (_("Could not parse signal set: %s"), line);
4265 1.1 christos
4266 1.1 christos p = line;
4267 1.1 christos signum = len * 4;
4268 1.1 christos while (len-- > 0)
4269 1.1 christos {
4270 1.1 christos int digit;
4271 1.1 christos
4272 1.1 christos if (*p >= '0' && *p <= '9')
4273 1.1 christos digit = *p - '0';
4274 1.1 christos else if (*p >= 'a' && *p <= 'f')
4275 1.1 christos digit = *p - 'a' + 10;
4276 1.1 christos else
4277 1.1 christos error (_("Could not parse signal set: %s"), line);
4278 1.1 christos
4279 1.1 christos signum -= 4;
4280 1.1 christos
4281 1.1 christos if (digit & 1)
4282 1.1 christos sigaddset (sigs, signum + 1);
4283 1.1 christos if (digit & 2)
4284 1.1 christos sigaddset (sigs, signum + 2);
4285 1.1 christos if (digit & 4)
4286 1.1 christos sigaddset (sigs, signum + 3);
4287 1.1 christos if (digit & 8)
4288 1.1 christos sigaddset (sigs, signum + 4);
4289 1.1 christos
4290 1.1 christos p++;
4291 1.1 christos }
4292 1.1 christos }
4293 1.1 christos
4294 1.1 christos /* Find process PID's pending signals from /proc/pid/status and set
4295 1.1 christos SIGS to match. */
4296 1.1 christos
4297 1.1 christos void
4298 1.1 christos linux_proc_pending_signals (int pid, sigset_t *pending,
4299 1.1 christos sigset_t *blocked, sigset_t *ignored)
4300 1.1 christos {
4301 1.1 christos FILE *procfile;
4302 1.1 christos char buffer[PATH_MAX], fname[PATH_MAX];
4303 1.1 christos struct cleanup *cleanup;
4304 1.1 christos
4305 1.1 christos sigemptyset (pending);
4306 1.1 christos sigemptyset (blocked);
4307 1.1 christos sigemptyset (ignored);
4308 1.1 christos xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
4309 1.1 christos procfile = gdb_fopen_cloexec (fname, "r");
4310 1.1 christos if (procfile == NULL)
4311 1.1 christos error (_("Could not open %s"), fname);
4312 1.1 christos cleanup = make_cleanup_fclose (procfile);
4313 1.1 christos
4314 1.1 christos while (fgets (buffer, PATH_MAX, procfile) != NULL)
4315 1.1 christos {
4316 1.1 christos /* Normal queued signals are on the SigPnd line in the status
4317 1.1 christos file. However, 2.6 kernels also have a "shared" pending
4318 1.1 christos queue for delivering signals to a thread group, so check for
4319 1.1 christos a ShdPnd line also.
4320 1.1 christos
4321 1.1 christos Unfortunately some Red Hat kernels include the shared pending
4322 1.1 christos queue but not the ShdPnd status field. */
4323 1.1 christos
4324 1.1 christos if (strncmp (buffer, "SigPnd:\t", 8) == 0)
4325 1.1 christos add_line_to_sigset (buffer + 8, pending);
4326 1.1 christos else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
4327 1.1 christos add_line_to_sigset (buffer + 8, pending);
4328 1.1 christos else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
4329 1.1 christos add_line_to_sigset (buffer + 8, blocked);
4330 1.1 christos else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
4331 1.1 christos add_line_to_sigset (buffer + 8, ignored);
4332 1.1 christos }
4333 1.1 christos
4334 1.1 christos do_cleanups (cleanup);
4335 1.1 christos }
4336 1.1 christos
4337 1.1 christos static LONGEST
4338 1.1 christos linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
4339 1.1 christos const char *annex, gdb_byte *readbuf,
4340 1.1 christos const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4341 1.1 christos {
4342 1.1 christos gdb_assert (object == TARGET_OBJECT_OSDATA);
4343 1.1 christos
4344 1.1 christos return linux_common_xfer_osdata (annex, readbuf, offset, len);
4345 1.1 christos }
4346 1.1 christos
4347 1.1 christos static LONGEST
4348 1.1 christos linux_xfer_partial (struct target_ops *ops, enum target_object object,
4349 1.1 christos const char *annex, gdb_byte *readbuf,
4350 1.1 christos const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4351 1.1 christos {
4352 1.1 christos LONGEST xfer;
4353 1.1 christos
4354 1.1 christos if (object == TARGET_OBJECT_AUXV)
4355 1.1 christos return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4356 1.1 christos offset, len);
4357 1.1 christos
4358 1.1 christos if (object == TARGET_OBJECT_OSDATA)
4359 1.1 christos return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
4360 1.1 christos offset, len);
4361 1.1 christos
4362 1.1 christos if (object == TARGET_OBJECT_SPU)
4363 1.1 christos return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
4364 1.1 christos offset, len);
4365 1.1 christos
4366 1.1 christos /* GDB calculates all the addresses in possibly larget width of the address.
4367 1.1 christos Address width needs to be masked before its final use - either by
4368 1.1 christos linux_proc_xfer_partial or inf_ptrace_xfer_partial.
4369 1.1 christos
4370 1.1 christos Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
4371 1.1 christos
4372 1.1 christos if (object == TARGET_OBJECT_MEMORY)
4373 1.1 christos {
4374 1.1 christos int addr_bit = gdbarch_addr_bit (target_gdbarch ());
4375 1.1 christos
4376 1.1 christos if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
4377 1.1 christos offset &= ((ULONGEST) 1 << addr_bit) - 1;
4378 1.1 christos }
4379 1.1 christos
4380 1.1 christos xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
4381 1.1 christos offset, len);
4382 1.1 christos if (xfer != 0)
4383 1.1 christos return xfer;
4384 1.1 christos
4385 1.1 christos return super_xfer_partial (ops, object, annex, readbuf, writebuf,
4386 1.1 christos offset, len);
4387 1.1 christos }
4388 1.1 christos
4389 1.1 christos static void
4390 1.1 christos cleanup_target_stop (void *arg)
4391 1.1 christos {
4392 1.1 christos ptid_t *ptid = (ptid_t *) arg;
4393 1.1 christos
4394 1.1 christos gdb_assert (arg != NULL);
4395 1.1 christos
4396 1.1 christos /* Unpause all */
4397 1.1 christos target_resume (*ptid, 0, GDB_SIGNAL_0);
4398 1.1 christos }
4399 1.1 christos
4400 1.1 christos static VEC(static_tracepoint_marker_p) *
4401 1.1 christos linux_child_static_tracepoint_markers_by_strid (const char *strid)
4402 1.1 christos {
4403 1.1 christos char s[IPA_CMD_BUF_SIZE];
4404 1.1 christos struct cleanup *old_chain;
4405 1.1 christos int pid = ptid_get_pid (inferior_ptid);
4406 1.1 christos VEC(static_tracepoint_marker_p) *markers = NULL;
4407 1.1 christos struct static_tracepoint_marker *marker = NULL;
4408 1.1 christos char *p = s;
4409 1.1 christos ptid_t ptid = ptid_build (pid, 0, 0);
4410 1.1 christos
4411 1.1 christos /* Pause all */
4412 1.1 christos target_stop (ptid);
4413 1.1 christos
4414 1.1 christos memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
4415 1.1 christos s[sizeof ("qTfSTM")] = 0;
4416 1.1 christos
4417 1.1 christos agent_run_command (pid, s, strlen (s) + 1);
4418 1.1 christos
4419 1.1 christos old_chain = make_cleanup (free_current_marker, &marker);
4420 1.1 christos make_cleanup (cleanup_target_stop, &ptid);
4421 1.1 christos
4422 1.1 christos while (*p++ == 'm')
4423 1.1 christos {
4424 1.1 christos if (marker == NULL)
4425 1.1 christos marker = XCNEW (struct static_tracepoint_marker);
4426 1.1 christos
4427 1.1 christos do
4428 1.1 christos {
4429 1.1 christos parse_static_tracepoint_marker_definition (p, &p, marker);
4430 1.1 christos
4431 1.1 christos if (strid == NULL || strcmp (strid, marker->str_id) == 0)
4432 1.1 christos {
4433 1.1 christos VEC_safe_push (static_tracepoint_marker_p,
4434 1.1 christos markers, marker);
4435 1.1 christos marker = NULL;
4436 1.1 christos }
4437 1.1 christos else
4438 1.1 christos {
4439 1.1 christos release_static_tracepoint_marker (marker);
4440 1.1 christos memset (marker, 0, sizeof (*marker));
4441 1.1 christos }
4442 1.1 christos }
4443 1.1 christos while (*p++ == ','); /* comma-separated list */
4444 1.1 christos
4445 1.1 christos memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
4446 1.1 christos s[sizeof ("qTsSTM")] = 0;
4447 1.1 christos agent_run_command (pid, s, strlen (s) + 1);
4448 1.1 christos p = s;
4449 1.1 christos }
4450 1.1 christos
4451 1.1 christos do_cleanups (old_chain);
4452 1.1 christos
4453 1.1 christos return markers;
4454 1.1 christos }
4455 1.1 christos
4456 1.1 christos /* Create a prototype generic GNU/Linux target. The client can override
4457 1.1 christos it with local methods. */
4458 1.1 christos
4459 1.1 christos static void
4460 1.1 christos linux_target_install_ops (struct target_ops *t)
4461 1.1 christos {
4462 1.1 christos t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
4463 1.1 christos t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
4464 1.1 christos t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
4465 1.1 christos t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
4466 1.1 christos t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
4467 1.1 christos t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
4468 1.1 christos t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
4469 1.1 christos t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
4470 1.1 christos t->to_post_startup_inferior = linux_child_post_startup_inferior;
4471 1.1 christos t->to_post_attach = linux_child_post_attach;
4472 1.1 christos t->to_follow_fork = linux_child_follow_fork;
4473 1.1 christos t->to_make_corefile_notes = linux_nat_make_corefile_notes;
4474 1.1 christos
4475 1.1 christos super_xfer_partial = t->to_xfer_partial;
4476 1.1 christos t->to_xfer_partial = linux_xfer_partial;
4477 1.1 christos
4478 1.1 christos t->to_static_tracepoint_markers_by_strid
4479 1.1 christos = linux_child_static_tracepoint_markers_by_strid;
4480 1.1 christos }
4481 1.1 christos
4482 1.1 christos struct target_ops *
4483 1.1 christos linux_target (void)
4484 1.1 christos {
4485 1.1 christos struct target_ops *t;
4486 1.1 christos
4487 1.1 christos t = inf_ptrace_target ();
4488 1.1 christos linux_target_install_ops (t);
4489 1.1 christos
4490 1.1 christos return t;
4491 1.1 christos }
4492 1.1 christos
4493 1.1 christos struct target_ops *
4494 1.1 christos linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
4495 1.1 christos {
4496 1.1 christos struct target_ops *t;
4497 1.1 christos
4498 1.1 christos t = inf_ptrace_trad_target (register_u_offset);
4499 1.1 christos linux_target_install_ops (t);
4500 1.1 christos
4501 1.1 christos return t;
4502 1.1 christos }
4503 1.1 christos
4504 1.1 christos /* target_is_async_p implementation. */
4505 1.1 christos
4506 1.1 christos static int
4507 1.1 christos linux_nat_is_async_p (void)
4508 1.1 christos {
4509 1.1 christos /* NOTE: palves 2008-03-21: We're only async when the user requests
4510 1.1 christos it explicitly with the "set target-async" command.
4511 1.1 christos Someday, linux will always be async. */
4512 1.1 christos return target_async_permitted;
4513 1.1 christos }
4514 1.1 christos
4515 1.1 christos /* target_can_async_p implementation. */
4516 1.1 christos
4517 1.1 christos static int
4518 1.1 christos linux_nat_can_async_p (void)
4519 1.1 christos {
4520 1.1 christos /* NOTE: palves 2008-03-21: We're only async when the user requests
4521 1.1 christos it explicitly with the "set target-async" command.
4522 1.1 christos Someday, linux will always be async. */
4523 1.1 christos return target_async_permitted;
4524 1.1 christos }
4525 1.1 christos
4526 1.1 christos static int
4527 1.1 christos linux_nat_supports_non_stop (void)
4528 1.1 christos {
4529 1.1 christos return 1;
4530 1.1 christos }
4531 1.1 christos
4532 1.1 christos /* True if we want to support multi-process. To be removed when GDB
4533 1.1 christos supports multi-exec. */
4534 1.1 christos
4535 1.1 christos int linux_multi_process = 1;
4536 1.1 christos
4537 1.1 christos static int
4538 1.1 christos linux_nat_supports_multi_process (void)
4539 1.1 christos {
4540 1.1 christos return linux_multi_process;
4541 1.1 christos }
4542 1.1 christos
4543 1.1 christos static int
4544 1.1 christos linux_nat_supports_disable_randomization (void)
4545 1.1 christos {
4546 1.1 christos #ifdef HAVE_PERSONALITY
4547 1.1 christos return 1;
4548 1.1 christos #else
4549 1.1 christos return 0;
4550 1.1 christos #endif
4551 1.1 christos }
4552 1.1 christos
4553 1.1 christos static int async_terminal_is_ours = 1;
4554 1.1 christos
4555 1.1 christos /* target_terminal_inferior implementation. */
4556 1.1 christos
4557 1.1 christos static void
4558 1.1 christos linux_nat_terminal_inferior (void)
4559 1.1 christos {
4560 1.1 christos if (!target_is_async_p ())
4561 1.1 christos {
4562 1.1 christos /* Async mode is disabled. */
4563 1.1 christos terminal_inferior ();
4564 1.1 christos return;
4565 1.1 christos }
4566 1.1 christos
4567 1.1 christos terminal_inferior ();
4568 1.1 christos
4569 1.1 christos /* Calls to target_terminal_*() are meant to be idempotent. */
4570 1.1 christos if (!async_terminal_is_ours)
4571 1.1 christos return;
4572 1.1 christos
4573 1.1 christos delete_file_handler (input_fd);
4574 1.1 christos async_terminal_is_ours = 0;
4575 1.1 christos set_sigint_trap ();
4576 1.1 christos }
4577 1.1 christos
4578 1.1 christos /* target_terminal_ours implementation. */
4579 1.1 christos
4580 1.1 christos static void
4581 1.1 christos linux_nat_terminal_ours (void)
4582 1.1 christos {
4583 1.1 christos if (!target_is_async_p ())
4584 1.1 christos {
4585 1.1 christos /* Async mode is disabled. */
4586 1.1 christos terminal_ours ();
4587 1.1 christos return;
4588 1.1 christos }
4589 1.1 christos
4590 1.1 christos /* GDB should never give the terminal to the inferior if the
4591 1.1 christos inferior is running in the background (run&, continue&, etc.),
4592 1.1 christos but claiming it sure should. */
4593 1.1 christos terminal_ours ();
4594 1.1 christos
4595 1.1 christos if (async_terminal_is_ours)
4596 1.1 christos return;
4597 1.1 christos
4598 1.1 christos clear_sigint_trap ();
4599 1.1 christos add_file_handler (input_fd, stdin_event_handler, 0);
4600 1.1 christos async_terminal_is_ours = 1;
4601 1.1 christos }
4602 1.1 christos
4603 1.1 christos static void (*async_client_callback) (enum inferior_event_type event_type,
4604 1.1 christos void *context);
4605 1.1 christos static void *async_client_context;
4606 1.1 christos
4607 1.1 christos /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4608 1.1 christos so we notice when any child changes state, and notify the
4609 1.1 christos event-loop; it allows us to use sigsuspend in linux_nat_wait_1
4610 1.1 christos above to wait for the arrival of a SIGCHLD. */
4611 1.1 christos
4612 1.1 christos static void
4613 1.1 christos sigchld_handler (int signo)
4614 1.1 christos {
4615 1.1 christos int old_errno = errno;
4616 1.1 christos
4617 1.1 christos if (debug_linux_nat)
4618 1.1 christos ui_file_write_async_safe (gdb_stdlog,
4619 1.1 christos "sigchld\n", sizeof ("sigchld\n") - 1);
4620 1.1 christos
4621 1.1 christos if (signo == SIGCHLD
4622 1.1 christos && linux_nat_event_pipe[0] != -1)
4623 1.1 christos async_file_mark (); /* Let the event loop know that there are
4624 1.1 christos events to handle. */
4625 1.1 christos
4626 1.1 christos errno = old_errno;
4627 1.1 christos }
4628 1.1 christos
4629 1.1 christos /* Callback registered with the target events file descriptor. */
4630 1.1 christos
4631 1.1 christos static void
4632 1.1 christos handle_target_event (int error, gdb_client_data client_data)
4633 1.1 christos {
4634 1.1 christos (*async_client_callback) (INF_REG_EVENT, async_client_context);
4635 1.1 christos }
4636 1.1 christos
4637 1.1 christos /* Create/destroy the target events pipe. Returns previous state. */
4638 1.1 christos
4639 1.1 christos static int
4640 1.1 christos linux_async_pipe (int enable)
4641 1.1 christos {
4642 1.1 christos int previous = (linux_nat_event_pipe[0] != -1);
4643 1.1 christos
4644 1.1 christos if (previous != enable)
4645 1.1 christos {
4646 1.1 christos sigset_t prev_mask;
4647 1.1 christos
4648 1.1 christos /* Block child signals while we create/destroy the pipe, as
4649 1.1 christos their handler writes to it. */
4650 1.1 christos block_child_signals (&prev_mask);
4651 1.1 christos
4652 1.1 christos if (enable)
4653 1.1 christos {
4654 1.1 christos if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
4655 1.1 christos internal_error (__FILE__, __LINE__,
4656 1.1 christos "creating event pipe failed.");
4657 1.1 christos
4658 1.1 christos fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
4659 1.1 christos fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
4660 1.1 christos }
4661 1.1 christos else
4662 1.1 christos {
4663 1.1 christos close (linux_nat_event_pipe[0]);
4664 1.1 christos close (linux_nat_event_pipe[1]);
4665 1.1 christos linux_nat_event_pipe[0] = -1;
4666 1.1 christos linux_nat_event_pipe[1] = -1;
4667 1.1 christos }
4668 1.1 christos
4669 1.1 christos restore_child_signals_mask (&prev_mask);
4670 1.1 christos }
4671 1.1 christos
4672 1.1 christos return previous;
4673 1.1 christos }
4674 1.1 christos
4675 1.1 christos /* target_async implementation. */
4676 1.1 christos
4677 1.1 christos static void
4678 1.1 christos linux_nat_async (void (*callback) (enum inferior_event_type event_type,
4679 1.1 christos void *context), void *context)
4680 1.1 christos {
4681 1.1 christos if (callback != NULL)
4682 1.1 christos {
4683 1.1 christos async_client_callback = callback;
4684 1.1 christos async_client_context = context;
4685 1.1 christos if (!linux_async_pipe (1))
4686 1.1 christos {
4687 1.1 christos add_file_handler (linux_nat_event_pipe[0],
4688 1.1 christos handle_target_event, NULL);
4689 1.1 christos /* There may be pending events to handle. Tell the event loop
4690 1.1 christos to poll them. */
4691 1.1 christos async_file_mark ();
4692 1.1 christos }
4693 1.1 christos }
4694 1.1 christos else
4695 1.1 christos {
4696 1.1 christos async_client_callback = callback;
4697 1.1 christos async_client_context = context;
4698 1.1 christos delete_file_handler (linux_nat_event_pipe[0]);
4699 1.1 christos linux_async_pipe (0);
4700 1.1 christos }
4701 1.1 christos return;
4702 1.1 christos }
4703 1.1 christos
4704 1.1 christos /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
4705 1.1 christos event came out. */
4706 1.1 christos
4707 1.1 christos static int
4708 1.1 christos linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
4709 1.1 christos {
4710 1.1 christos if (!lwp->stopped)
4711 1.1 christos {
4712 1.1 christos if (debug_linux_nat)
4713 1.1 christos fprintf_unfiltered (gdb_stdlog,
4714 1.1 christos "LNSL: running -> suspending %s\n",
4715 1.1 christos target_pid_to_str (lwp->ptid));
4716 1.1 christos
4717 1.1 christos
4718 1.1 christos if (lwp->last_resume_kind == resume_stop)
4719 1.1 christos {
4720 1.1 christos if (debug_linux_nat)
4721 1.1 christos fprintf_unfiltered (gdb_stdlog,
4722 1.1 christos "linux-nat: already stopping LWP %ld at "
4723 1.1 christos "GDB's request\n",
4724 1.1 christos ptid_get_lwp (lwp->ptid));
4725 1.1 christos return 0;
4726 1.1 christos }
4727 1.1 christos
4728 1.1 christos stop_callback (lwp, NULL);
4729 1.1 christos lwp->last_resume_kind = resume_stop;
4730 1.1 christos }
4731 1.1 christos else
4732 1.1 christos {
4733 1.1 christos /* Already known to be stopped; do nothing. */
4734 1.1 christos
4735 1.1 christos if (debug_linux_nat)
4736 1.1 christos {
4737 1.1 christos if (find_thread_ptid (lwp->ptid)->stop_requested)
4738 1.1 christos fprintf_unfiltered (gdb_stdlog,
4739 1.1 christos "LNSL: already stopped/stop_requested %s\n",
4740 1.1 christos target_pid_to_str (lwp->ptid));
4741 1.1 christos else
4742 1.1 christos fprintf_unfiltered (gdb_stdlog,
4743 1.1 christos "LNSL: already stopped/no "
4744 1.1 christos "stop_requested yet %s\n",
4745 1.1 christos target_pid_to_str (lwp->ptid));
4746 1.1 christos }
4747 1.1 christos }
4748 1.1 christos return 0;
4749 1.1 christos }
4750 1.1 christos
4751 1.1 christos static void
4752 1.1 christos linux_nat_stop (ptid_t ptid)
4753 1.1 christos {
4754 1.1 christos if (non_stop)
4755 1.1 christos iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
4756 1.1 christos else
4757 1.1 christos linux_ops->to_stop (ptid);
4758 1.1 christos }
4759 1.1 christos
4760 1.1 christos static void
4761 1.1 christos linux_nat_close (void)
4762 1.1 christos {
4763 1.1 christos /* Unregister from the event loop. */
4764 1.1 christos if (linux_nat_is_async_p ())
4765 1.1 christos linux_nat_async (NULL, 0);
4766 1.1 christos
4767 1.1 christos if (linux_ops->to_close)
4768 1.1 christos linux_ops->to_close ();
4769 1.1 christos }
4770 1.1 christos
4771 1.1 christos /* When requests are passed down from the linux-nat layer to the
4772 1.1 christos single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
4773 1.1 christos used. The address space pointer is stored in the inferior object,
4774 1.1 christos but the common code that is passed such ptid can't tell whether
4775 1.1 christos lwpid is a "main" process id or not (it assumes so). We reverse
4776 1.1 christos look up the "main" process id from the lwp here. */
4777 1.1 christos
4778 1.1 christos static struct address_space *
4779 1.1 christos linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
4780 1.1 christos {
4781 1.1 christos struct lwp_info *lwp;
4782 1.1 christos struct inferior *inf;
4783 1.1 christos int pid;
4784 1.1 christos
4785 1.1 christos pid = ptid_get_lwp (ptid);
4786 1.1 christos if (ptid_get_lwp (ptid) == 0)
4787 1.1 christos {
4788 1.1 christos /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
4789 1.1 christos tgid. */
4790 1.1 christos lwp = find_lwp_pid (ptid);
4791 1.1 christos pid = ptid_get_pid (lwp->ptid);
4792 1.1 christos }
4793 1.1 christos else
4794 1.1 christos {
4795 1.1 christos /* A (pid,lwpid,0) ptid. */
4796 1.1 christos pid = ptid_get_pid (ptid);
4797 1.1 christos }
4798 1.1 christos
4799 1.1 christos inf = find_inferior_pid (pid);
4800 1.1 christos gdb_assert (inf != NULL);
4801 1.1 christos return inf->aspace;
4802 1.1 christos }
4803 1.1 christos
4804 1.1 christos /* Return the cached value of the processor core for thread PTID. */
4805 1.1 christos
4806 1.1 christos static int
4807 1.1 christos linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
4808 1.1 christos {
4809 1.1 christos struct lwp_info *info = find_lwp_pid (ptid);
4810 1.1 christos
4811 1.1 christos if (info)
4812 1.1 christos return info->core;
4813 1.1 christos return -1;
4814 1.1 christos }
4815 1.1 christos
4816 1.1 christos void
4817 1.1 christos linux_nat_add_target (struct target_ops *t)
4818 1.1 christos {
4819 1.1 christos /* Save the provided single-threaded target. We save this in a separate
4820 1.1 christos variable because another target we've inherited from (e.g. inf-ptrace)
4821 1.1 christos may have saved a pointer to T; we want to use it for the final
4822 1.1 christos process stratum target. */
4823 1.1 christos linux_ops_saved = *t;
4824 1.1 christos linux_ops = &linux_ops_saved;
4825 1.1 christos
4826 1.1 christos /* Override some methods for multithreading. */
4827 1.1 christos t->to_create_inferior = linux_nat_create_inferior;
4828 1.1 christos t->to_attach = linux_nat_attach;
4829 1.1 christos t->to_detach = linux_nat_detach;
4830 1.1 christos t->to_resume = linux_nat_resume;
4831 1.1 christos t->to_wait = linux_nat_wait;
4832 1.1 christos t->to_pass_signals = linux_nat_pass_signals;
4833 1.1 christos t->to_xfer_partial = linux_nat_xfer_partial;
4834 1.1 christos t->to_kill = linux_nat_kill;
4835 1.1 christos t->to_mourn_inferior = linux_nat_mourn_inferior;
4836 1.1 christos t->to_thread_alive = linux_nat_thread_alive;
4837 1.1 christos t->to_pid_to_str = linux_nat_pid_to_str;
4838 1.1 christos t->to_thread_name = linux_nat_thread_name;
4839 1.1 christos t->to_has_thread_control = tc_schedlock;
4840 1.1 christos t->to_thread_address_space = linux_nat_thread_address_space;
4841 1.1 christos t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
4842 1.1 christos t->to_stopped_data_address = linux_nat_stopped_data_address;
4843 1.1 christos
4844 1.1 christos t->to_can_async_p = linux_nat_can_async_p;
4845 1.1 christos t->to_is_async_p = linux_nat_is_async_p;
4846 1.1 christos t->to_supports_non_stop = linux_nat_supports_non_stop;
4847 1.1 christos t->to_async = linux_nat_async;
4848 1.1 christos t->to_terminal_inferior = linux_nat_terminal_inferior;
4849 1.1 christos t->to_terminal_ours = linux_nat_terminal_ours;
4850 1.1 christos t->to_close = linux_nat_close;
4851 1.1 christos
4852 1.1 christos /* Methods for non-stop support. */
4853 1.1 christos t->to_stop = linux_nat_stop;
4854 1.1 christos
4855 1.1 christos t->to_supports_multi_process = linux_nat_supports_multi_process;
4856 1.1 christos
4857 1.1 christos t->to_supports_disable_randomization
4858 1.1 christos = linux_nat_supports_disable_randomization;
4859 1.1 christos
4860 1.1 christos t->to_core_of_thread = linux_nat_core_of_thread;
4861 1.1 christos
4862 1.1 christos /* We don't change the stratum; this target will sit at
4863 1.1 christos process_stratum and thread_db will set at thread_stratum. This
4864 1.1 christos is a little strange, since this is a multi-threaded-capable
4865 1.1 christos target, but we want to be on the stack below thread_db, and we
4866 1.1 christos also want to be used for single-threaded processes. */
4867 1.1 christos
4868 1.1 christos add_target (t);
4869 1.1 christos }
4870 1.1 christos
4871 1.1 christos /* Register a method to call whenever a new thread is attached. */
4872 1.1 christos void
4873 1.1 christos linux_nat_set_new_thread (struct target_ops *t,
4874 1.1 christos void (*new_thread) (struct lwp_info *))
4875 1.1 christos {
4876 1.1 christos /* Save the pointer. We only support a single registered instance
4877 1.1 christos of the GNU/Linux native target, so we do not need to map this to
4878 1.1 christos T. */
4879 1.1 christos linux_nat_new_thread = new_thread;
4880 1.1 christos }
4881 1.1 christos
4882 1.1 christos /* See declaration in linux-nat.h. */
4883 1.1 christos
4884 1.1 christos void
4885 1.1 christos linux_nat_set_new_fork (struct target_ops *t,
4886 1.1 christos linux_nat_new_fork_ftype *new_fork)
4887 1.1 christos {
4888 1.1 christos /* Save the pointer. */
4889 1.1 christos linux_nat_new_fork = new_fork;
4890 1.1 christos }
4891 1.1 christos
4892 1.1 christos /* See declaration in linux-nat.h. */
4893 1.1 christos
4894 1.1 christos void
4895 1.1 christos linux_nat_set_forget_process (struct target_ops *t,
4896 1.1 christos linux_nat_forget_process_ftype *fn)
4897 1.1 christos {
4898 1.1 christos /* Save the pointer. */
4899 1.1 christos linux_nat_forget_process_hook = fn;
4900 1.1 christos }
4901 1.1 christos
4902 1.1 christos /* See declaration in linux-nat.h. */
4903 1.1 christos
4904 1.1 christos void
4905 1.1 christos linux_nat_forget_process (pid_t pid)
4906 1.1 christos {
4907 1.1 christos if (linux_nat_forget_process_hook != NULL)
4908 1.1 christos linux_nat_forget_process_hook (pid);
4909 1.1 christos }
4910 1.1 christos
4911 1.1 christos /* Register a method that converts a siginfo object between the layout
4912 1.1 christos that ptrace returns, and the layout in the architecture of the
4913 1.1 christos inferior. */
4914 1.1 christos void
4915 1.1 christos linux_nat_set_siginfo_fixup (struct target_ops *t,
4916 1.1 christos int (*siginfo_fixup) (siginfo_t *,
4917 1.1 christos gdb_byte *,
4918 1.1 christos int))
4919 1.1 christos {
4920 1.1 christos /* Save the pointer. */
4921 1.1 christos linux_nat_siginfo_fixup = siginfo_fixup;
4922 1.1 christos }
4923 1.1 christos
4924 1.1 christos /* Register a method to call prior to resuming a thread. */
4925 1.1 christos
4926 1.1 christos void
4927 1.1 christos linux_nat_set_prepare_to_resume (struct target_ops *t,
4928 1.1 christos void (*prepare_to_resume) (struct lwp_info *))
4929 1.1 christos {
4930 1.1 christos /* Save the pointer. */
4931 1.1 christos linux_nat_prepare_to_resume = prepare_to_resume;
4932 1.1 christos }
4933 1.1 christos
4934 1.1 christos /* See linux-nat.h. */
4935 1.1 christos
4936 1.1 christos int
4937 1.1 christos linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
4938 1.1 christos {
4939 1.1 christos int pid;
4940 1.1 christos
4941 1.1 christos pid = ptid_get_lwp (ptid);
4942 1.1 christos if (pid == 0)
4943 1.1 christos pid = ptid_get_pid (ptid);
4944 1.1 christos
4945 1.1 christos errno = 0;
4946 1.1 christos ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
4947 1.1 christos if (errno != 0)
4948 1.1 christos {
4949 1.1 christos memset (siginfo, 0, sizeof (*siginfo));
4950 1.1 christos return 0;
4951 1.1 christos }
4952 1.1 christos return 1;
4953 1.1 christos }
4954 1.1 christos
4955 1.1 christos /* Provide a prototype to silence -Wmissing-prototypes. */
4956 1.1 christos extern initialize_file_ftype _initialize_linux_nat;
4957 1.1 christos
4958 1.1 christos void
4959 1.1 christos _initialize_linux_nat (void)
4960 1.1 christos {
4961 1.1 christos add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
4962 1.1 christos &debug_linux_nat, _("\
4963 1.1 christos Set debugging of GNU/Linux lwp module."), _("\
4964 1.1 christos Show debugging of GNU/Linux lwp module."), _("\
4965 1.1 christos Enables printf debugging output."),
4966 1.1 christos NULL,
4967 1.1 christos show_debug_linux_nat,
4968 1.1 christos &setdebuglist, &showdebuglist);
4969 1.1 christos
4970 1.1 christos /* Save this mask as the default. */
4971 1.1 christos sigprocmask (SIG_SETMASK, NULL, &normal_mask);
4972 1.1 christos
4973 1.1 christos /* Install a SIGCHLD handler. */
4974 1.1 christos sigchld_action.sa_handler = sigchld_handler;
4975 1.1 christos sigemptyset (&sigchld_action.sa_mask);
4976 1.1 christos sigchld_action.sa_flags = SA_RESTART;
4977 1.1 christos
4978 1.1 christos /* Make it the default. */
4979 1.1 christos sigaction (SIGCHLD, &sigchld_action, NULL);
4980 1.1 christos
4981 1.1 christos /* Make sure we don't block SIGCHLD during a sigsuspend. */
4982 1.1 christos sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
4983 1.1 christos sigdelset (&suspend_mask, SIGCHLD);
4984 1.1 christos
4985 1.1 christos sigemptyset (&blocked_mask);
4986 1.1 christos }
4987 1.1 christos
4988 1.1 christos
4990 1.1 christos /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
4991 1.1 christos the GNU/Linux Threads library and therefore doesn't really belong
4992 1.1 christos here. */
4993 1.1 christos
4994 1.1 christos /* Read variable NAME in the target and return its value if found.
4995 1.1 christos Otherwise return zero. It is assumed that the type of the variable
4996 1.1 christos is `int'. */
4997 1.1 christos
4998 1.1 christos static int
4999 1.1 christos get_signo (const char *name)
5000 1.1 christos {
5001 1.1 christos struct minimal_symbol *ms;
5002 1.1 christos int signo;
5003 1.1 christos
5004 1.1 christos ms = lookup_minimal_symbol (name, NULL, NULL);
5005 1.1 christos if (ms == NULL)
5006 1.1 christos return 0;
5007 1.1 christos
5008 1.1 christos if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
5009 1.1 christos sizeof (signo)) != 0)
5010 1.1 christos return 0;
5011 1.1 christos
5012 1.1 christos return signo;
5013 1.1 christos }
5014 1.1 christos
5015 1.1 christos /* Return the set of signals used by the threads library in *SET. */
5016 1.1 christos
5017 1.1 christos void
5018 1.1 christos lin_thread_get_thread_signals (sigset_t *set)
5019 1.1 christos {
5020 1.1 christos struct sigaction action;
5021 1.1 christos int restart, cancel;
5022 1.1 christos
5023 1.1 christos sigemptyset (&blocked_mask);
5024 1.1 christos sigemptyset (set);
5025 1.1 christos
5026 1.1 christos restart = get_signo ("__pthread_sig_restart");
5027 1.1 christos cancel = get_signo ("__pthread_sig_cancel");
5028 1.1 christos
5029 1.1 christos /* LinuxThreads normally uses the first two RT signals, but in some legacy
5030 1.1 christos cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does
5031 1.1 christos not provide any way for the debugger to query the signal numbers -
5032 1.1 christos fortunately they don't change! */
5033 1.1 christos
5034 1.1 christos if (restart == 0)
5035 1.1 christos restart = __SIGRTMIN;
5036 1.1 christos
5037 1.1 christos if (cancel == 0)
5038 1.1 christos cancel = __SIGRTMIN + 1;
5039 1.1 christos
5040 1.1 christos sigaddset (set, restart);
5041 1.1 christos sigaddset (set, cancel);
5042 1.1 christos
5043 1.1 christos /* The GNU/Linux Threads library makes terminating threads send a
5044 1.1 christos special "cancel" signal instead of SIGCHLD. Make sure we catch
5045 1.1 christos those (to prevent them from terminating GDB itself, which is
5046 1.1 christos likely to be their default action) and treat them the same way as
5047 1.1 christos SIGCHLD. */
5048 1.1 christos
5049 1.1 christos action.sa_handler = sigchld_handler;
5050 1.1 christos sigemptyset (&action.sa_mask);
5051 1.1 christos action.sa_flags = SA_RESTART;
5052 1.1 christos sigaction (cancel, &action, NULL);
5053 1.1 christos
5054 1.1 christos /* We block the "cancel" signal throughout this code ... */
5055 sigaddset (&blocked_mask, cancel);
5056 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
5057
5058 /* ... except during a sigsuspend. */
5059 sigdelset (&suspend_mask, cancel);
5060 }
5061