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