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