* linux-nat.c (struct saved_ptids, threads_to_delete)
[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
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdb_string.h"
27 #include "gdb_wait.h"
28 #include "gdb_assert.h"
29 #ifdef HAVE_TKILL_SYSCALL
30 #include <unistd.h>
31 #include <sys/syscall.h>
32 #endif
33 #include <sys/ptrace.h>
34 #include "linux-nat.h"
35 #include "linux-fork.h"
36 #include "gdbthread.h"
37 #include "gdbcmd.h"
38 #include "regcache.h"
39 #include "inf-ptrace.h"
40 #include "auxv.h"
41 #include <sys/param.h> /* for MAXPATHLEN */
42 #include <sys/procfs.h> /* for elf_gregset etc. */
43 #include "elf-bfd.h" /* for elfcore_write_* */
44 #include "gregset.h" /* for gregset */
45 #include "gdbcore.h" /* for get_exec_file */
46 #include <ctype.h> /* for isdigit */
47 #include "gdbthread.h" /* for struct thread_info etc. */
48 #include "gdb_stat.h" /* for struct stat */
49 #include <fcntl.h> /* for O_RDONLY */
50
51 #ifndef O_LARGEFILE
52 #define O_LARGEFILE 0
53 #endif
54
55 /* If the system headers did not provide the constants, hard-code the normal
56 values. */
57 #ifndef PTRACE_EVENT_FORK
58
59 #define PTRACE_SETOPTIONS 0x4200
60 #define PTRACE_GETEVENTMSG 0x4201
61
62 /* options set using PTRACE_SETOPTIONS */
63 #define PTRACE_O_TRACESYSGOOD 0x00000001
64 #define PTRACE_O_TRACEFORK 0x00000002
65 #define PTRACE_O_TRACEVFORK 0x00000004
66 #define PTRACE_O_TRACECLONE 0x00000008
67 #define PTRACE_O_TRACEEXEC 0x00000010
68 #define PTRACE_O_TRACEVFORKDONE 0x00000020
69 #define PTRACE_O_TRACEEXIT 0x00000040
70
71 /* Wait extended result codes for the above trace options. */
72 #define PTRACE_EVENT_FORK 1
73 #define PTRACE_EVENT_VFORK 2
74 #define PTRACE_EVENT_CLONE 3
75 #define PTRACE_EVENT_EXEC 4
76 #define PTRACE_EVENT_VFORK_DONE 5
77 #define PTRACE_EVENT_EXIT 6
78
79 #endif /* PTRACE_EVENT_FORK */
80
81 /* We can't always assume that this flag is available, but all systems
82 with the ptrace event handlers also have __WALL, so it's safe to use
83 here. */
84 #ifndef __WALL
85 #define __WALL 0x40000000 /* Wait for any child. */
86 #endif
87
88 /* The single-threaded native GNU/Linux target_ops. We save a pointer for
89 the use of the multi-threaded target. */
90 static struct target_ops *linux_ops;
91
92 /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
93 Called by our to_xfer_partial. */
94 static LONGEST (*super_xfer_partial) (struct target_ops *,
95 enum target_object,
96 const char *, gdb_byte *,
97 const gdb_byte *,
98 ULONGEST, LONGEST);
99
100 /* The saved to_mourn_inferior method, inherited from inf-ptrace.c.
101 Called by our to_mourn_inferior. */
102 static void (*super_mourn_inferior) (void);
103
104 static int debug_linux_nat;
105 static void
106 show_debug_linux_nat (struct ui_file *file, int from_tty,
107 struct cmd_list_element *c, const char *value)
108 {
109 fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
110 value);
111 }
112
113 static int linux_parent_pid;
114
115 struct simple_pid_list
116 {
117 int pid;
118 struct simple_pid_list *next;
119 };
120 struct simple_pid_list *stopped_pids;
121
122 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
123 can not be used, 1 if it can. */
124
125 static int linux_supports_tracefork_flag = -1;
126
127 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
128 PTRACE_O_TRACEVFORKDONE. */
129
130 static int linux_supports_tracevforkdone_flag = -1;
131
132 \f
133 /* Trivial list manipulation functions to keep track of a list of
134 new stopped processes. */
135 static void
136 add_to_pid_list (struct simple_pid_list **listp, int pid)
137 {
138 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
139 new_pid->pid = pid;
140 new_pid->next = *listp;
141 *listp = new_pid;
142 }
143
144 static int
145 pull_pid_from_list (struct simple_pid_list **listp, int pid)
146 {
147 struct simple_pid_list **p;
148
149 for (p = listp; *p != NULL; p = &(*p)->next)
150 if ((*p)->pid == pid)
151 {
152 struct simple_pid_list *next = (*p)->next;
153 xfree (*p);
154 *p = next;
155 return 1;
156 }
157 return 0;
158 }
159
160 void
161 linux_record_stopped_pid (int pid)
162 {
163 add_to_pid_list (&stopped_pids, pid);
164 }
165
166 \f
167 /* A helper function for linux_test_for_tracefork, called after fork (). */
168
169 static void
170 linux_tracefork_child (void)
171 {
172 int ret;
173
174 ptrace (PTRACE_TRACEME, 0, 0, 0);
175 kill (getpid (), SIGSTOP);
176 fork ();
177 _exit (0);
178 }
179
180 /* Wrapper function for waitpid which handles EINTR. */
181
182 static int
183 my_waitpid (int pid, int *status, int flags)
184 {
185 int ret;
186 do
187 {
188 ret = waitpid (pid, status, flags);
189 }
190 while (ret == -1 && errno == EINTR);
191
192 return ret;
193 }
194
195 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
196
197 First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
198 we know that the feature is not available. This may change the tracing
199 options for ORIGINAL_PID, but we'll be setting them shortly anyway.
200
201 However, if it succeeds, we don't know for sure that the feature is
202 available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
203 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
204 fork tracing, and let it fork. If the process exits, we assume that we
205 can't use TRACEFORK; if we get the fork notification, and we can extract
206 the new child's PID, then we assume that we can. */
207
208 static void
209 linux_test_for_tracefork (int original_pid)
210 {
211 int child_pid, ret, status;
212 long second_pid;
213
214 linux_supports_tracefork_flag = 0;
215 linux_supports_tracevforkdone_flag = 0;
216
217 ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
218 if (ret != 0)
219 return;
220
221 child_pid = fork ();
222 if (child_pid == -1)
223 perror_with_name (("fork"));
224
225 if (child_pid == 0)
226 linux_tracefork_child ();
227
228 ret = my_waitpid (child_pid, &status, 0);
229 if (ret == -1)
230 perror_with_name (("waitpid"));
231 else if (ret != child_pid)
232 error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret);
233 if (! WIFSTOPPED (status))
234 error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status);
235
236 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
237 if (ret != 0)
238 {
239 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
240 if (ret != 0)
241 {
242 warning (_("linux_test_for_tracefork: failed to kill child"));
243 return;
244 }
245
246 ret = my_waitpid (child_pid, &status, 0);
247 if (ret != child_pid)
248 warning (_("linux_test_for_tracefork: failed to wait for killed child"));
249 else if (!WIFSIGNALED (status))
250 warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from "
251 "killed child"), status);
252
253 return;
254 }
255
256 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
257 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
258 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
259 linux_supports_tracevforkdone_flag = (ret == 0);
260
261 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
262 if (ret != 0)
263 warning (_("linux_test_for_tracefork: failed to resume child"));
264
265 ret = my_waitpid (child_pid, &status, 0);
266
267 if (ret == child_pid && WIFSTOPPED (status)
268 && status >> 16 == PTRACE_EVENT_FORK)
269 {
270 second_pid = 0;
271 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
272 if (ret == 0 && second_pid != 0)
273 {
274 int second_status;
275
276 linux_supports_tracefork_flag = 1;
277 my_waitpid (second_pid, &second_status, 0);
278 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
279 if (ret != 0)
280 warning (_("linux_test_for_tracefork: failed to kill second child"));
281 }
282 }
283 else
284 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
285 "(%d, status 0x%x)"), ret, status);
286
287 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
288 if (ret != 0)
289 warning (_("linux_test_for_tracefork: failed to kill child"));
290 my_waitpid (child_pid, &status, 0);
291 }
292
293 /* Return non-zero iff we have tracefork functionality available.
294 This function also sets linux_supports_tracefork_flag. */
295
296 static int
297 linux_supports_tracefork (int pid)
298 {
299 if (linux_supports_tracefork_flag == -1)
300 linux_test_for_tracefork (pid);
301 return linux_supports_tracefork_flag;
302 }
303
304 static int
305 linux_supports_tracevforkdone (int pid)
306 {
307 if (linux_supports_tracefork_flag == -1)
308 linux_test_for_tracefork (pid);
309 return linux_supports_tracevforkdone_flag;
310 }
311
312 \f
313 void
314 linux_enable_event_reporting (ptid_t ptid)
315 {
316 int pid = ptid_get_lwp (ptid);
317 int options;
318
319 if (pid == 0)
320 pid = ptid_get_pid (ptid);
321
322 if (! linux_supports_tracefork (pid))
323 return;
324
325 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
326 | PTRACE_O_TRACECLONE;
327 if (linux_supports_tracevforkdone (pid))
328 options |= PTRACE_O_TRACEVFORKDONE;
329
330 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
331 read-only process state. */
332
333 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
334 }
335
336 void
337 child_post_attach (int pid)
338 {
339 linux_enable_event_reporting (pid_to_ptid (pid));
340 }
341
342 static void
343 linux_child_post_startup_inferior (ptid_t ptid)
344 {
345 linux_enable_event_reporting (ptid);
346 }
347
348 int
349 child_follow_fork (struct target_ops *ops, int follow_child)
350 {
351 ptid_t last_ptid;
352 struct target_waitstatus last_status;
353 int has_vforked;
354 int parent_pid, child_pid;
355
356 get_last_target_status (&last_ptid, &last_status);
357 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
358 parent_pid = ptid_get_lwp (last_ptid);
359 if (parent_pid == 0)
360 parent_pid = ptid_get_pid (last_ptid);
361 child_pid = last_status.value.related_pid;
362
363 if (! follow_child)
364 {
365 /* We're already attached to the parent, by default. */
366
367 /* Before detaching from the child, remove all breakpoints from
368 it. (This won't actually modify the breakpoint list, but will
369 physically remove the breakpoints from the child.) */
370 /* If we vforked this will remove the breakpoints from the parent
371 also, but they'll be reinserted below. */
372 detach_breakpoints (child_pid);
373
374 /* Detach new forked process? */
375 if (detach_fork)
376 {
377 if (debug_linux_nat)
378 {
379 target_terminal_ours ();
380 fprintf_filtered (gdb_stdlog,
381 "Detaching after fork from child process %d.\n",
382 child_pid);
383 }
384
385 ptrace (PTRACE_DETACH, child_pid, 0, 0);
386 }
387 else
388 {
389 struct fork_info *fp;
390 /* Retain child fork in ptrace (stopped) state. */
391 fp = find_fork_pid (child_pid);
392 if (!fp)
393 fp = add_fork (child_pid);
394 fork_save_infrun_state (fp, 0);
395 }
396
397 if (has_vforked)
398 {
399 gdb_assert (linux_supports_tracefork_flag >= 0);
400 if (linux_supports_tracevforkdone (0))
401 {
402 int status;
403
404 ptrace (PTRACE_CONT, parent_pid, 0, 0);
405 my_waitpid (parent_pid, &status, __WALL);
406 if ((status >> 16) != PTRACE_EVENT_VFORK_DONE)
407 warning (_("Unexpected waitpid result %06x when waiting for "
408 "vfork-done"), status);
409 }
410 else
411 {
412 /* We can't insert breakpoints until the child has
413 finished with the shared memory region. We need to
414 wait until that happens. Ideal would be to just
415 call:
416 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
417 - waitpid (parent_pid, &status, __WALL);
418 However, most architectures can't handle a syscall
419 being traced on the way out if it wasn't traced on
420 the way in.
421
422 We might also think to loop, continuing the child
423 until it exits or gets a SIGTRAP. One problem is
424 that the child might call ptrace with PTRACE_TRACEME.
425
426 There's no simple and reliable way to figure out when
427 the vforked child will be done with its copy of the
428 shared memory. We could step it out of the syscall,
429 two instructions, let it go, and then single-step the
430 parent once. When we have hardware single-step, this
431 would work; with software single-step it could still
432 be made to work but we'd have to be able to insert
433 single-step breakpoints in the child, and we'd have
434 to insert -just- the single-step breakpoint in the
435 parent. Very awkward.
436
437 In the end, the best we can do is to make sure it
438 runs for a little while. Hopefully it will be out of
439 range of any breakpoints we reinsert. Usually this
440 is only the single-step breakpoint at vfork's return
441 point. */
442
443 usleep (10000);
444 }
445
446 /* Since we vforked, breakpoints were removed in the parent
447 too. Put them back. */
448 reattach_breakpoints (parent_pid);
449 }
450 }
451 else
452 {
453 char child_pid_spelling[40];
454
455 /* Needed to keep the breakpoint lists in sync. */
456 if (! has_vforked)
457 detach_breakpoints (child_pid);
458
459 /* Before detaching from the parent, remove all breakpoints from it. */
460 remove_breakpoints ();
461
462 if (debug_linux_nat)
463 {
464 target_terminal_ours ();
465 fprintf_filtered (gdb_stdlog,
466 "Attaching after fork to child process %d.\n",
467 child_pid);
468 }
469
470 /* If we're vforking, we may want to hold on to the parent until
471 the child exits or execs. At exec time we can remove the old
472 breakpoints from the parent and detach it; at exit time we
473 could do the same (or even, sneakily, resume debugging it - the
474 child's exec has failed, or something similar).
475
476 This doesn't clean up "properly", because we can't call
477 target_detach, but that's OK; if the current target is "child",
478 then it doesn't need any further cleanups, and lin_lwp will
479 generally not encounter vfork (vfork is defined to fork
480 in libpthread.so).
481
482 The holding part is very easy if we have VFORKDONE events;
483 but keeping track of both processes is beyond GDB at the
484 moment. So we don't expose the parent to the rest of GDB.
485 Instead we quietly hold onto it until such time as we can
486 safely resume it. */
487
488 if (has_vforked)
489 linux_parent_pid = parent_pid;
490 else if (!detach_fork)
491 {
492 struct fork_info *fp;
493 /* Retain parent fork in ptrace (stopped) state. */
494 fp = find_fork_pid (parent_pid);
495 if (!fp)
496 fp = add_fork (parent_pid);
497 fork_save_infrun_state (fp, 0);
498 }
499 else
500 {
501 target_detach (NULL, 0);
502 }
503
504 inferior_ptid = pid_to_ptid (child_pid);
505
506 /* Reinstall ourselves, since we might have been removed in
507 target_detach (which does other necessary cleanup). */
508
509 push_target (ops);
510
511 /* Reset breakpoints in the child as appropriate. */
512 follow_inferior_reset_breakpoints ();
513 }
514
515 return 0;
516 }
517
518 ptid_t
519 linux_handle_extended_wait (int pid, int status,
520 struct target_waitstatus *ourstatus)
521 {
522 int event = status >> 16;
523
524 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
525 || event == PTRACE_EVENT_CLONE)
526 {
527 unsigned long new_pid;
528 int ret;
529
530 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
531
532 /* If we haven't already seen the new PID stop, wait for it now. */
533 if (! pull_pid_from_list (&stopped_pids, new_pid))
534 {
535 /* The new child has a pending SIGSTOP. We can't affect it until it
536 hits the SIGSTOP, but we're already attached. */
537 ret = my_waitpid (new_pid, &status,
538 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
539 if (ret == -1)
540 perror_with_name (_("waiting for new child"));
541 else if (ret != new_pid)
542 internal_error (__FILE__, __LINE__,
543 _("wait returned unexpected PID %d"), ret);
544 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
545 internal_error (__FILE__, __LINE__,
546 _("wait returned unexpected status 0x%x"), status);
547 }
548
549 if (event == PTRACE_EVENT_FORK)
550 ourstatus->kind = TARGET_WAITKIND_FORKED;
551 else if (event == PTRACE_EVENT_VFORK)
552 ourstatus->kind = TARGET_WAITKIND_VFORKED;
553 else
554 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
555
556 ourstatus->value.related_pid = new_pid;
557 return inferior_ptid;
558 }
559
560 if (event == PTRACE_EVENT_EXEC)
561 {
562 ourstatus->kind = TARGET_WAITKIND_EXECD;
563 ourstatus->value.execd_pathname
564 = xstrdup (child_pid_to_exec_file (pid));
565
566 if (linux_parent_pid)
567 {
568 detach_breakpoints (linux_parent_pid);
569 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
570
571 linux_parent_pid = 0;
572 }
573
574 return inferior_ptid;
575 }
576
577 internal_error (__FILE__, __LINE__,
578 _("unknown ptrace event %d"), event);
579 }
580
581 \f
582 void
583 child_insert_fork_catchpoint (int pid)
584 {
585 if (! linux_supports_tracefork (pid))
586 error (_("Your system does not support fork catchpoints."));
587 }
588
589 void
590 child_insert_vfork_catchpoint (int pid)
591 {
592 if (!linux_supports_tracefork (pid))
593 error (_("Your system does not support vfork catchpoints."));
594 }
595
596 void
597 child_insert_exec_catchpoint (int pid)
598 {
599 if (!linux_supports_tracefork (pid))
600 error (_("Your system does not support exec catchpoints."));
601 }
602
603 void
604 kill_inferior (void)
605 {
606 int status;
607 int pid = PIDGET (inferior_ptid);
608 struct target_waitstatus last;
609 ptid_t last_ptid;
610 int ret;
611
612 if (pid == 0)
613 return;
614
615 /* First cut -- let's crudely do everything inline. */
616 if (forks_exist_p ())
617 {
618 linux_fork_killall ();
619 pop_target ();
620 generic_mourn_inferior ();
621 }
622 else
623 {
624 /* If we're stopped while forking and we haven't followed yet,
625 kill the other task. We need to do this first because the
626 parent will be sleeping if this is a vfork. */
627
628 get_last_target_status (&last_ptid, &last);
629
630 if (last.kind == TARGET_WAITKIND_FORKED
631 || last.kind == TARGET_WAITKIND_VFORKED)
632 {
633 ptrace (PT_KILL, last.value.related_pid, 0, 0);
634 wait (&status);
635 }
636
637 /* Kill the current process. */
638 ptrace (PT_KILL, pid, 0, 0);
639 ret = wait (&status);
640
641 /* We might get a SIGCHLD instead of an exit status. This is
642 aggravated by the first kill above - a child has just died. */
643
644 while (ret == pid && WIFSTOPPED (status))
645 {
646 ptrace (PT_KILL, pid, 0, 0);
647 ret = wait (&status);
648 }
649 target_mourn_inferior ();
650 }
651 }
652
653 /* On GNU/Linux there are no real LWP's. The closest thing to LWP's
654 are processes sharing the same VM space. A multi-threaded process
655 is basically a group of such processes. However, such a grouping
656 is almost entirely a user-space issue; the kernel doesn't enforce
657 such a grouping at all (this might change in the future). In
658 general, we'll rely on the threads library (i.e. the GNU/Linux
659 Threads library) to provide such a grouping.
660
661 It is perfectly well possible to write a multi-threaded application
662 without the assistance of a threads library, by using the clone
663 system call directly. This module should be able to give some
664 rudimentary support for debugging such applications if developers
665 specify the CLONE_PTRACE flag in the clone system call, and are
666 using the Linux kernel 2.4 or above.
667
668 Note that there are some peculiarities in GNU/Linux that affect
669 this code:
670
671 - In general one should specify the __WCLONE flag to waitpid in
672 order to make it report events for any of the cloned processes
673 (and leave it out for the initial process). However, if a cloned
674 process has exited the exit status is only reported if the
675 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
676 we cannot use it since GDB must work on older systems too.
677
678 - When a traced, cloned process exits and is waited for by the
679 debugger, the kernel reassigns it to the original parent and
680 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
681 library doesn't notice this, which leads to the "zombie problem":
682 When debugged a multi-threaded process that spawns a lot of
683 threads will run out of processes, even if the threads exit,
684 because the "zombies" stay around. */
685
686 /* List of known LWPs. */
687 static struct lwp_info *lwp_list;
688
689 /* Number of LWPs in the list. */
690 static int num_lwps;
691
692 /* Non-zero if we're running in "threaded" mode. */
693 static int threaded;
694 \f
695
696 #define GET_LWP(ptid) ptid_get_lwp (ptid)
697 #define GET_PID(ptid) ptid_get_pid (ptid)
698 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
699 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
700
701 /* If the last reported event was a SIGTRAP, this variable is set to
702 the process id of the LWP/thread that got it. */
703 ptid_t trap_ptid;
704 \f
705
706 /* This module's target-specific operations. */
707 static struct target_ops linux_nat_ops;
708
709 /* Since we cannot wait (in linux_nat_wait) for the initial process and
710 any cloned processes with a single call to waitpid, we have to use
711 the WNOHANG flag and call waitpid in a loop. To optimize
712 things a bit we use `sigsuspend' to wake us up when a process has
713 something to report (it will send us a SIGCHLD if it has). To make
714 this work we have to juggle with the signal mask. We save the
715 original signal mask such that we can restore it before creating a
716 new process in order to avoid blocking certain signals in the
717 inferior. We then block SIGCHLD during the waitpid/sigsuspend
718 loop. */
719
720 /* Original signal mask. */
721 static sigset_t normal_mask;
722
723 /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
724 _initialize_linux_nat. */
725 static sigset_t suspend_mask;
726
727 /* Signals to block to make that sigsuspend work. */
728 static sigset_t blocked_mask;
729 \f
730
731 /* Prototypes for local functions. */
732 static int stop_wait_callback (struct lwp_info *lp, void *data);
733 static int linux_nat_thread_alive (ptid_t ptid);
734 \f
735 /* Convert wait status STATUS to a string. Used for printing debug
736 messages only. */
737
738 static char *
739 status_to_str (int status)
740 {
741 static char buf[64];
742
743 if (WIFSTOPPED (status))
744 snprintf (buf, sizeof (buf), "%s (stopped)",
745 strsignal (WSTOPSIG (status)));
746 else if (WIFSIGNALED (status))
747 snprintf (buf, sizeof (buf), "%s (terminated)",
748 strsignal (WSTOPSIG (status)));
749 else
750 snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
751
752 return buf;
753 }
754
755 /* Initialize the list of LWPs. Note that this module, contrary to
756 what GDB's generic threads layer does for its thread list,
757 re-initializes the LWP lists whenever we mourn or detach (which
758 doesn't involve mourning) the inferior. */
759
760 static void
761 init_lwp_list (void)
762 {
763 struct lwp_info *lp, *lpnext;
764
765 for (lp = lwp_list; lp; lp = lpnext)
766 {
767 lpnext = lp->next;
768 xfree (lp);
769 }
770
771 lwp_list = NULL;
772 num_lwps = 0;
773 threaded = 0;
774 }
775
776 /* Add the LWP specified by PID to the list. If this causes the
777 number of LWPs to become larger than one, go into "threaded" mode.
778 Return a pointer to the structure describing the new LWP. */
779
780 static struct lwp_info *
781 add_lwp (ptid_t ptid)
782 {
783 struct lwp_info *lp;
784
785 gdb_assert (is_lwp (ptid));
786
787 lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
788
789 memset (lp, 0, sizeof (struct lwp_info));
790
791 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
792
793 lp->ptid = ptid;
794
795 lp->next = lwp_list;
796 lwp_list = lp;
797 if (++num_lwps > 1)
798 threaded = 1;
799
800 return lp;
801 }
802
803 /* Remove the LWP specified by PID from the list. */
804
805 static void
806 delete_lwp (ptid_t ptid)
807 {
808 struct lwp_info *lp, *lpprev;
809
810 lpprev = NULL;
811
812 for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
813 if (ptid_equal (lp->ptid, ptid))
814 break;
815
816 if (!lp)
817 return;
818
819 /* We don't go back to "non-threaded" mode if the number of threads
820 becomes less than two. */
821 num_lwps--;
822
823 if (lpprev)
824 lpprev->next = lp->next;
825 else
826 lwp_list = lp->next;
827
828 xfree (lp);
829 }
830
831 /* Return a pointer to the structure describing the LWP corresponding
832 to PID. If no corresponding LWP could be found, return NULL. */
833
834 static struct lwp_info *
835 find_lwp_pid (ptid_t ptid)
836 {
837 struct lwp_info *lp;
838 int lwp;
839
840 if (is_lwp (ptid))
841 lwp = GET_LWP (ptid);
842 else
843 lwp = GET_PID (ptid);
844
845 for (lp = lwp_list; lp; lp = lp->next)
846 if (lwp == GET_LWP (lp->ptid))
847 return lp;
848
849 return NULL;
850 }
851
852 /* Call CALLBACK with its second argument set to DATA for every LWP in
853 the list. If CALLBACK returns 1 for a particular LWP, return a
854 pointer to the structure describing that LWP immediately.
855 Otherwise return NULL. */
856
857 struct lwp_info *
858 iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
859 {
860 struct lwp_info *lp, *lpnext;
861
862 for (lp = lwp_list; lp; lp = lpnext)
863 {
864 lpnext = lp->next;
865 if ((*callback) (lp, data))
866 return lp;
867 }
868
869 return NULL;
870 }
871
872 /* Record a PTID for later deletion. */
873
874 struct saved_ptids
875 {
876 ptid_t ptid;
877 struct saved_ptids *next;
878 };
879 static struct saved_ptids *threads_to_delete;
880
881 static void
882 record_dead_thread (ptid_t ptid)
883 {
884 struct saved_ptids *p = xmalloc (sizeof (struct saved_ptids));
885 p->ptid = ptid;
886 p->next = threads_to_delete;
887 threads_to_delete = p;
888 }
889
890 /* Delete any dead threads which are not the current thread. */
891
892 static void
893 prune_lwps (void)
894 {
895 struct saved_ptids **p = &threads_to_delete;
896
897 while (*p)
898 if (! ptid_equal ((*p)->ptid, inferior_ptid))
899 {
900 struct saved_ptids *tmp = *p;
901 delete_thread (tmp->ptid);
902 *p = tmp->next;
903 xfree (tmp);
904 }
905 else
906 p = &(*p)->next;
907 }
908
909 /* Callback for iterate_over_threads that finds a thread corresponding
910 to the given LWP. */
911
912 static int
913 find_thread_from_lwp (struct thread_info *thr, void *dummy)
914 {
915 ptid_t *ptid_p = dummy;
916
917 if (GET_LWP (thr->ptid) && GET_LWP (thr->ptid) == GET_LWP (*ptid_p))
918 return 1;
919 else
920 return 0;
921 }
922
923 /* Handle the exit of a single thread LP. */
924
925 static void
926 exit_lwp (struct lwp_info *lp)
927 {
928 if (in_thread_list (lp->ptid))
929 {
930 /* Core GDB cannot deal with us deleting the current thread. */
931 if (!ptid_equal (lp->ptid, inferior_ptid))
932 delete_thread (lp->ptid);
933 else
934 record_dead_thread (lp->ptid);
935 printf_unfiltered (_("[%s exited]\n"),
936 target_pid_to_str (lp->ptid));
937 }
938 else
939 {
940 /* Even if LP->PTID is not in the global GDB thread list, the
941 LWP may be - with an additional thread ID. We don't need
942 to print anything in this case; thread_db is in use and
943 already took care of that. But it didn't delete the thread
944 in order to handle zombies correctly. */
945
946 struct thread_info *thr;
947
948 thr = iterate_over_threads (find_thread_from_lwp, &lp->ptid);
949 if (thr && !ptid_equal (thr->ptid, inferior_ptid))
950 delete_thread (thr->ptid);
951 else
952 record_dead_thread (thr->ptid);
953 }
954
955 delete_lwp (lp->ptid);
956 }
957
958 /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print
959 a message telling the user that a new LWP has been added to the
960 process. */
961
962 void
963 lin_lwp_attach_lwp (ptid_t ptid, int verbose)
964 {
965 struct lwp_info *lp, *found_lp;
966
967 gdb_assert (is_lwp (ptid));
968
969 /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events
970 to interrupt either the ptrace() or waitpid() calls below. */
971 if (!sigismember (&blocked_mask, SIGCHLD))
972 {
973 sigaddset (&blocked_mask, SIGCHLD);
974 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
975 }
976
977 if (verbose)
978 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid));
979
980 found_lp = lp = find_lwp_pid (ptid);
981 if (lp == NULL)
982 lp = add_lwp (ptid);
983
984 /* We assume that we're already attached to any LWP that has an id
985 equal to the overall process id, and to any LWP that is already
986 in our list of LWPs. If we're not seeing exit events from threads
987 and we've had PID wraparound since we last tried to stop all threads,
988 this assumption might be wrong; fortunately, this is very unlikely
989 to happen. */
990 if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL)
991 {
992 pid_t pid;
993 int status;
994
995 if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
996 error (_("Can't attach %s: %s"), target_pid_to_str (ptid),
997 safe_strerror (errno));
998
999 if (debug_linux_nat)
1000 fprintf_unfiltered (gdb_stdlog,
1001 "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
1002 target_pid_to_str (ptid));
1003
1004 pid = my_waitpid (GET_LWP (ptid), &status, 0);
1005 if (pid == -1 && errno == ECHILD)
1006 {
1007 /* Try again with __WCLONE to check cloned processes. */
1008 pid = my_waitpid (GET_LWP (ptid), &status, __WCLONE);
1009 lp->cloned = 1;
1010 }
1011
1012 gdb_assert (pid == GET_LWP (ptid)
1013 && WIFSTOPPED (status) && WSTOPSIG (status));
1014
1015 child_post_attach (pid);
1016
1017 lp->stopped = 1;
1018
1019 if (debug_linux_nat)
1020 {
1021 fprintf_unfiltered (gdb_stdlog,
1022 "LLAL: waitpid %s received %s\n",
1023 target_pid_to_str (ptid),
1024 status_to_str (status));
1025 }
1026 }
1027 else
1028 {
1029 /* We assume that the LWP representing the original process is
1030 already stopped. Mark it as stopped in the data structure
1031 that the linux ptrace layer uses to keep track of threads.
1032 Note that this won't have already been done since the main
1033 thread will have, we assume, been stopped by an attach from a
1034 different layer. */
1035 lp->stopped = 1;
1036 }
1037 }
1038
1039 static void
1040 linux_nat_attach (char *args, int from_tty)
1041 {
1042 struct lwp_info *lp;
1043 pid_t pid;
1044 int status;
1045
1046 /* FIXME: We should probably accept a list of process id's, and
1047 attach all of them. */
1048 linux_ops->to_attach (args, from_tty);
1049
1050 /* Add the initial process as the first LWP to the list. */
1051 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
1052
1053 /* Make sure the initial process is stopped. The user-level threads
1054 layer might want to poke around in the inferior, and that won't
1055 work if things haven't stabilized yet. */
1056 pid = my_waitpid (GET_PID (inferior_ptid), &status, 0);
1057 if (pid == -1 && errno == ECHILD)
1058 {
1059 warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid));
1060
1061 /* Try again with __WCLONE to check cloned processes. */
1062 pid = my_waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
1063 lp->cloned = 1;
1064 }
1065
1066 gdb_assert (pid == GET_PID (inferior_ptid)
1067 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
1068
1069 lp->stopped = 1;
1070
1071 /* Fake the SIGSTOP that core GDB expects. */
1072 lp->status = W_STOPCODE (SIGSTOP);
1073 lp->resumed = 1;
1074 if (debug_linux_nat)
1075 {
1076 fprintf_unfiltered (gdb_stdlog,
1077 "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
1078 }
1079 }
1080
1081 static int
1082 detach_callback (struct lwp_info *lp, void *data)
1083 {
1084 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1085
1086 if (debug_linux_nat && lp->status)
1087 fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n",
1088 strsignal (WSTOPSIG (lp->status)),
1089 target_pid_to_str (lp->ptid));
1090
1091 while (lp->signalled && lp->stopped)
1092 {
1093 errno = 0;
1094 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
1095 WSTOPSIG (lp->status)) < 0)
1096 error (_("Can't continue %s: %s"), target_pid_to_str (lp->ptid),
1097 safe_strerror (errno));
1098
1099 if (debug_linux_nat)
1100 fprintf_unfiltered (gdb_stdlog,
1101 "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
1102 target_pid_to_str (lp->ptid),
1103 status_to_str (lp->status));
1104
1105 lp->stopped = 0;
1106 lp->signalled = 0;
1107 lp->status = 0;
1108 /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
1109 here. But since lp->signalled was cleared above,
1110 stop_wait_callback didn't do anything; the process was left
1111 running. Shouldn't we be waiting for it to stop?
1112 I've removed the call, since stop_wait_callback now does do
1113 something when called with lp->signalled == 0. */
1114
1115 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
1116 }
1117
1118 /* We don't actually detach from the LWP that has an id equal to the
1119 overall process id just yet. */
1120 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
1121 {
1122 errno = 0;
1123 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
1124 WSTOPSIG (lp->status)) < 0)
1125 error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
1126 safe_strerror (errno));
1127
1128 if (debug_linux_nat)
1129 fprintf_unfiltered (gdb_stdlog,
1130 "PTRACE_DETACH (%s, %s, 0) (OK)\n",
1131 target_pid_to_str (lp->ptid),
1132 strsignal (WSTOPSIG (lp->status)));
1133
1134 delete_lwp (lp->ptid);
1135 }
1136
1137 return 0;
1138 }
1139
1140 static void
1141 linux_nat_detach (char *args, int from_tty)
1142 {
1143 iterate_over_lwps (detach_callback, NULL);
1144
1145 /* Only the initial process should be left right now. */
1146 gdb_assert (num_lwps == 1);
1147
1148 trap_ptid = null_ptid;
1149
1150 /* Destroy LWP info; it's no longer valid. */
1151 init_lwp_list ();
1152
1153 /* Restore the original signal mask. */
1154 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1155 sigemptyset (&blocked_mask);
1156
1157 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
1158 linux_ops->to_detach (args, from_tty);
1159 }
1160
1161 /* Resume LP. */
1162
1163 static int
1164 resume_callback (struct lwp_info *lp, void *data)
1165 {
1166 if (lp->stopped && lp->status == 0)
1167 {
1168 struct thread_info *tp;
1169
1170 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
1171 0, TARGET_SIGNAL_0);
1172 if (debug_linux_nat)
1173 fprintf_unfiltered (gdb_stdlog,
1174 "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n",
1175 target_pid_to_str (lp->ptid));
1176 lp->stopped = 0;
1177 lp->step = 0;
1178 }
1179
1180 return 0;
1181 }
1182
1183 static int
1184 resume_clear_callback (struct lwp_info *lp, void *data)
1185 {
1186 lp->resumed = 0;
1187 return 0;
1188 }
1189
1190 static int
1191 resume_set_callback (struct lwp_info *lp, void *data)
1192 {
1193 lp->resumed = 1;
1194 return 0;
1195 }
1196
1197 static void
1198 linux_nat_resume (ptid_t ptid, int step, enum target_signal signo)
1199 {
1200 struct lwp_info *lp;
1201 int resume_all;
1202
1203 if (debug_linux_nat)
1204 fprintf_unfiltered (gdb_stdlog,
1205 "LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
1206 step ? "step" : "resume",
1207 target_pid_to_str (ptid),
1208 signo ? strsignal (signo) : "0",
1209 target_pid_to_str (inferior_ptid));
1210
1211 prune_lwps ();
1212
1213 /* A specific PTID means `step only this process id'. */
1214 resume_all = (PIDGET (ptid) == -1);
1215
1216 if (resume_all)
1217 iterate_over_lwps (resume_set_callback, NULL);
1218 else
1219 iterate_over_lwps (resume_clear_callback, NULL);
1220
1221 /* If PID is -1, it's the current inferior that should be
1222 handled specially. */
1223 if (PIDGET (ptid) == -1)
1224 ptid = inferior_ptid;
1225
1226 lp = find_lwp_pid (ptid);
1227 if (lp)
1228 {
1229 ptid = pid_to_ptid (GET_LWP (lp->ptid));
1230
1231 /* Remember if we're stepping. */
1232 lp->step = step;
1233
1234 /* Mark this LWP as resumed. */
1235 lp->resumed = 1;
1236
1237 /* If we have a pending wait status for this thread, there is no
1238 point in resuming the process. But first make sure that
1239 linux_nat_wait won't preemptively handle the event - we
1240 should never take this short-circuit if we are going to
1241 leave LP running, since we have skipped resuming all the
1242 other threads. This bit of code needs to be synchronized
1243 with linux_nat_wait. */
1244
1245 if (lp->status && WIFSTOPPED (lp->status))
1246 {
1247 int saved_signo = target_signal_from_host (WSTOPSIG (lp->status));
1248
1249 if (signal_stop_state (saved_signo) == 0
1250 && signal_print_state (saved_signo) == 0
1251 && signal_pass_state (saved_signo) == 1)
1252 {
1253 if (debug_linux_nat)
1254 fprintf_unfiltered (gdb_stdlog,
1255 "LLR: Not short circuiting for ignored "
1256 "status 0x%x\n", lp->status);
1257
1258 /* FIXME: What should we do if we are supposed to continue
1259 this thread with a signal? */
1260 gdb_assert (signo == TARGET_SIGNAL_0);
1261 signo = saved_signo;
1262 lp->status = 0;
1263 }
1264 }
1265
1266 if (lp->status)
1267 {
1268 /* FIXME: What should we do if we are supposed to continue
1269 this thread with a signal? */
1270 gdb_assert (signo == TARGET_SIGNAL_0);
1271
1272 if (debug_linux_nat)
1273 fprintf_unfiltered (gdb_stdlog,
1274 "LLR: Short circuiting for status 0x%x\n",
1275 lp->status);
1276
1277 return;
1278 }
1279
1280 /* Mark LWP as not stopped to prevent it from being continued by
1281 resume_callback. */
1282 lp->stopped = 0;
1283 }
1284
1285 if (resume_all)
1286 iterate_over_lwps (resume_callback, NULL);
1287
1288 linux_ops->to_resume (ptid, step, signo);
1289 if (debug_linux_nat)
1290 fprintf_unfiltered (gdb_stdlog,
1291 "LLR: %s %s, %s (resume event thread)\n",
1292 step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1293 target_pid_to_str (ptid),
1294 signo ? strsignal (signo) : "0");
1295 }
1296
1297 /* Issue kill to specified lwp. */
1298
1299 static int tkill_failed;
1300
1301 static int
1302 kill_lwp (int lwpid, int signo)
1303 {
1304 errno = 0;
1305
1306 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1307 fails, then we are not using nptl threads and we should be using kill. */
1308
1309 #ifdef HAVE_TKILL_SYSCALL
1310 if (!tkill_failed)
1311 {
1312 int ret = syscall (__NR_tkill, lwpid, signo);
1313 if (errno != ENOSYS)
1314 return ret;
1315 errno = 0;
1316 tkill_failed = 1;
1317 }
1318 #endif
1319
1320 return kill (lwpid, signo);
1321 }
1322
1323 /* Handle a GNU/Linux extended wait response. Most of the work we
1324 just pass off to linux_handle_extended_wait, but if it reports a
1325 clone event we need to add the new LWP to our list (and not report
1326 the trap to higher layers). This function returns non-zero if
1327 the event should be ignored and we should wait again. */
1328
1329 static int
1330 linux_nat_handle_extended (struct lwp_info *lp, int status)
1331 {
1332 linux_handle_extended_wait (GET_LWP (lp->ptid), status,
1333 &lp->waitstatus);
1334
1335 /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */
1336 if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS)
1337 {
1338 struct lwp_info *new_lp;
1339 new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid,
1340 GET_PID (inferior_ptid)));
1341 new_lp->cloned = 1;
1342 new_lp->stopped = 1;
1343
1344 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
1345
1346 if (debug_linux_nat)
1347 fprintf_unfiltered (gdb_stdlog,
1348 "LLHE: Got clone event from LWP %ld, resuming\n",
1349 GET_LWP (lp->ptid));
1350 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1351
1352 return 1;
1353 }
1354
1355 return 0;
1356 }
1357
1358 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
1359 exited. */
1360
1361 static int
1362 wait_lwp (struct lwp_info *lp)
1363 {
1364 pid_t pid;
1365 int status;
1366 int thread_dead = 0;
1367
1368 gdb_assert (!lp->stopped);
1369 gdb_assert (lp->status == 0);
1370
1371 pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
1372 if (pid == -1 && errno == ECHILD)
1373 {
1374 pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
1375 if (pid == -1 && errno == ECHILD)
1376 {
1377 /* The thread has previously exited. We need to delete it
1378 now because, for some vendor 2.4 kernels with NPTL
1379 support backported, there won't be an exit event unless
1380 it is the main thread. 2.6 kernels will report an exit
1381 event for each thread that exits, as expected. */
1382 thread_dead = 1;
1383 if (debug_linux_nat)
1384 fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
1385 target_pid_to_str (lp->ptid));
1386 }
1387 }
1388
1389 if (!thread_dead)
1390 {
1391 gdb_assert (pid == GET_LWP (lp->ptid));
1392
1393 if (debug_linux_nat)
1394 {
1395 fprintf_unfiltered (gdb_stdlog,
1396 "WL: waitpid %s received %s\n",
1397 target_pid_to_str (lp->ptid),
1398 status_to_str (status));
1399 }
1400 }
1401
1402 /* Check if the thread has exited. */
1403 if (WIFEXITED (status) || WIFSIGNALED (status))
1404 {
1405 thread_dead = 1;
1406 if (debug_linux_nat)
1407 fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
1408 target_pid_to_str (lp->ptid));
1409 }
1410
1411 if (thread_dead)
1412 {
1413 exit_lwp (lp);
1414 return 0;
1415 }
1416
1417 gdb_assert (WIFSTOPPED (status));
1418
1419 /* Handle GNU/Linux's extended waitstatus for trace events. */
1420 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1421 {
1422 if (debug_linux_nat)
1423 fprintf_unfiltered (gdb_stdlog,
1424 "WL: Handling extended status 0x%06x\n",
1425 status);
1426 if (linux_nat_handle_extended (lp, status))
1427 return wait_lwp (lp);
1428 }
1429
1430 return status;
1431 }
1432
1433 /* Send a SIGSTOP to LP. */
1434
1435 static int
1436 stop_callback (struct lwp_info *lp, void *data)
1437 {
1438 if (!lp->stopped && !lp->signalled)
1439 {
1440 int ret;
1441
1442 if (debug_linux_nat)
1443 {
1444 fprintf_unfiltered (gdb_stdlog,
1445 "SC: kill %s **<SIGSTOP>**\n",
1446 target_pid_to_str (lp->ptid));
1447 }
1448 errno = 0;
1449 ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
1450 if (debug_linux_nat)
1451 {
1452 fprintf_unfiltered (gdb_stdlog,
1453 "SC: lwp kill %d %s\n",
1454 ret,
1455 errno ? safe_strerror (errno) : "ERRNO-OK");
1456 }
1457
1458 lp->signalled = 1;
1459 gdb_assert (lp->status == 0);
1460 }
1461
1462 return 0;
1463 }
1464
1465 /* Wait until LP is stopped. If DATA is non-null it is interpreted as
1466 a pointer to a set of signals to be flushed immediately. */
1467
1468 static int
1469 stop_wait_callback (struct lwp_info *lp, void *data)
1470 {
1471 sigset_t *flush_mask = data;
1472
1473 if (!lp->stopped)
1474 {
1475 int status;
1476
1477 status = wait_lwp (lp);
1478 if (status == 0)
1479 return 0;
1480
1481 /* Ignore any signals in FLUSH_MASK. */
1482 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
1483 {
1484 if (!lp->signalled)
1485 {
1486 lp->stopped = 1;
1487 return 0;
1488 }
1489
1490 errno = 0;
1491 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1492 if (debug_linux_nat)
1493 fprintf_unfiltered (gdb_stdlog,
1494 "PTRACE_CONT %s, 0, 0 (%s)\n",
1495 target_pid_to_str (lp->ptid),
1496 errno ? safe_strerror (errno) : "OK");
1497
1498 return stop_wait_callback (lp, flush_mask);
1499 }
1500
1501 if (WSTOPSIG (status) != SIGSTOP)
1502 {
1503 if (WSTOPSIG (status) == SIGTRAP)
1504 {
1505 /* If a LWP other than the LWP that we're reporting an
1506 event for has hit a GDB breakpoint (as opposed to
1507 some random trap signal), then just arrange for it to
1508 hit it again later. We don't keep the SIGTRAP status
1509 and don't forward the SIGTRAP signal to the LWP. We
1510 will handle the current event, eventually we will
1511 resume all LWPs, and this one will get its breakpoint
1512 trap again.
1513
1514 If we do not do this, then we run the risk that the
1515 user will delete or disable the breakpoint, but the
1516 thread will have already tripped on it. */
1517
1518 /* Now resume this LWP and get the SIGSTOP event. */
1519 errno = 0;
1520 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1521 if (debug_linux_nat)
1522 {
1523 fprintf_unfiltered (gdb_stdlog,
1524 "PTRACE_CONT %s, 0, 0 (%s)\n",
1525 target_pid_to_str (lp->ptid),
1526 errno ? safe_strerror (errno) : "OK");
1527
1528 fprintf_unfiltered (gdb_stdlog,
1529 "SWC: Candidate SIGTRAP event in %s\n",
1530 target_pid_to_str (lp->ptid));
1531 }
1532 /* Hold the SIGTRAP for handling by linux_nat_wait. */
1533 stop_wait_callback (lp, data);
1534 /* If there's another event, throw it back into the queue. */
1535 if (lp->status)
1536 {
1537 if (debug_linux_nat)
1538 {
1539 fprintf_unfiltered (gdb_stdlog,
1540 "SWC: kill %s, %s\n",
1541 target_pid_to_str (lp->ptid),
1542 status_to_str ((int) status));
1543 }
1544 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
1545 }
1546 /* Save the sigtrap event. */
1547 lp->status = status;
1548 return 0;
1549 }
1550 else
1551 {
1552 /* The thread was stopped with a signal other than
1553 SIGSTOP, and didn't accidentally trip a breakpoint. */
1554
1555 if (debug_linux_nat)
1556 {
1557 fprintf_unfiltered (gdb_stdlog,
1558 "SWC: Pending event %s in %s\n",
1559 status_to_str ((int) status),
1560 target_pid_to_str (lp->ptid));
1561 }
1562 /* Now resume this LWP and get the SIGSTOP event. */
1563 errno = 0;
1564 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1565 if (debug_linux_nat)
1566 fprintf_unfiltered (gdb_stdlog,
1567 "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
1568 target_pid_to_str (lp->ptid),
1569 errno ? safe_strerror (errno) : "OK");
1570
1571 /* Hold this event/waitstatus while we check to see if
1572 there are any more (we still want to get that SIGSTOP). */
1573 stop_wait_callback (lp, data);
1574 /* If the lp->status field is still empty, use it to hold
1575 this event. If not, then this event must be returned
1576 to the event queue of the LWP. */
1577 if (lp->status == 0)
1578 lp->status = status;
1579 else
1580 {
1581 if (debug_linux_nat)
1582 {
1583 fprintf_unfiltered (gdb_stdlog,
1584 "SWC: kill %s, %s\n",
1585 target_pid_to_str (lp->ptid),
1586 status_to_str ((int) status));
1587 }
1588 kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
1589 }
1590 return 0;
1591 }
1592 }
1593 else
1594 {
1595 /* We caught the SIGSTOP that we intended to catch, so
1596 there's no SIGSTOP pending. */
1597 lp->stopped = 1;
1598 lp->signalled = 0;
1599 }
1600 }
1601
1602 return 0;
1603 }
1604
1605 /* Check whether PID has any pending signals in FLUSH_MASK. If so set
1606 the appropriate bits in PENDING, and return 1 - otherwise return 0. */
1607
1608 static int
1609 linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
1610 {
1611 sigset_t blocked, ignored;
1612 int i;
1613
1614 linux_proc_pending_signals (pid, pending, &blocked, &ignored);
1615
1616 if (!flush_mask)
1617 return 0;
1618
1619 for (i = 1; i < NSIG; i++)
1620 if (sigismember (pending, i))
1621 if (!sigismember (flush_mask, i)
1622 || sigismember (&blocked, i)
1623 || sigismember (&ignored, i))
1624 sigdelset (pending, i);
1625
1626 if (sigisemptyset (pending))
1627 return 0;
1628
1629 return 1;
1630 }
1631
1632 /* DATA is interpreted as a mask of signals to flush. If LP has
1633 signals pending, and they are all in the flush mask, then arrange
1634 to flush them. LP should be stopped, as should all other threads
1635 it might share a signal queue with. */
1636
1637 static int
1638 flush_callback (struct lwp_info *lp, void *data)
1639 {
1640 sigset_t *flush_mask = data;
1641 sigset_t pending, intersection, blocked, ignored;
1642 int pid, status;
1643
1644 /* Normally, when an LWP exits, it is removed from the LWP list. The
1645 last LWP isn't removed till later, however. So if there is only
1646 one LWP on the list, make sure it's alive. */
1647 if (lwp_list == lp && lp->next == NULL)
1648 if (!linux_nat_thread_alive (lp->ptid))
1649 return 0;
1650
1651 /* Just because the LWP is stopped doesn't mean that new signals
1652 can't arrive from outside, so this function must be careful of
1653 race conditions. However, because all threads are stopped, we
1654 can assume that the pending mask will not shrink unless we resume
1655 the LWP, and that it will then get another signal. We can't
1656 control which one, however. */
1657
1658 if (lp->status)
1659 {
1660 if (debug_linux_nat)
1661 printf_unfiltered (_("FC: LP has pending status %06x\n"), lp->status);
1662 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
1663 lp->status = 0;
1664 }
1665
1666 while (linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
1667 {
1668 int ret;
1669
1670 errno = 0;
1671 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
1672 if (debug_linux_nat)
1673 fprintf_unfiltered (gdb_stderr,
1674 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
1675
1676 lp->stopped = 0;
1677 stop_wait_callback (lp, flush_mask);
1678 if (debug_linux_nat)
1679 fprintf_unfiltered (gdb_stderr,
1680 "FC: Wait finished; saved status is %d\n",
1681 lp->status);
1682 }
1683
1684 return 0;
1685 }
1686
1687 /* Return non-zero if LP has a wait status pending. */
1688
1689 static int
1690 status_callback (struct lwp_info *lp, void *data)
1691 {
1692 /* Only report a pending wait status if we pretend that this has
1693 indeed been resumed. */
1694 return (lp->status != 0 && lp->resumed);
1695 }
1696
1697 /* Return non-zero if LP isn't stopped. */
1698
1699 static int
1700 running_callback (struct lwp_info *lp, void *data)
1701 {
1702 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
1703 }
1704
1705 /* Count the LWP's that have had events. */
1706
1707 static int
1708 count_events_callback (struct lwp_info *lp, void *data)
1709 {
1710 int *count = data;
1711
1712 gdb_assert (count != NULL);
1713
1714 /* Count only LWPs that have a SIGTRAP event pending. */
1715 if (lp->status != 0
1716 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1717 (*count)++;
1718
1719 return 0;
1720 }
1721
1722 /* Select the LWP (if any) that is currently being single-stepped. */
1723
1724 static int
1725 select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
1726 {
1727 if (lp->step && lp->status != 0)
1728 return 1;
1729 else
1730 return 0;
1731 }
1732
1733 /* Select the Nth LWP that has had a SIGTRAP event. */
1734
1735 static int
1736 select_event_lwp_callback (struct lwp_info *lp, void *data)
1737 {
1738 int *selector = data;
1739
1740 gdb_assert (selector != NULL);
1741
1742 /* Select only LWPs that have a SIGTRAP event pending. */
1743 if (lp->status != 0
1744 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
1745 if ((*selector)-- == 0)
1746 return 1;
1747
1748 return 0;
1749 }
1750
1751 static int
1752 cancel_breakpoints_callback (struct lwp_info *lp, void *data)
1753 {
1754 struct lwp_info *event_lp = data;
1755
1756 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1757 if (lp == event_lp)
1758 return 0;
1759
1760 /* If a LWP other than the LWP that we're reporting an event for has
1761 hit a GDB breakpoint (as opposed to some random trap signal),
1762 then just arrange for it to hit it again later. We don't keep
1763 the SIGTRAP status and don't forward the SIGTRAP signal to the
1764 LWP. We will handle the current event, eventually we will resume
1765 all LWPs, and this one will get its breakpoint trap again.
1766
1767 If we do not do this, then we run the risk that the user will
1768 delete or disable the breakpoint, but the LWP will have already
1769 tripped on it. */
1770
1771 if (lp->status != 0
1772 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1773 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
1774 DECR_PC_AFTER_BREAK))
1775 {
1776 if (debug_linux_nat)
1777 fprintf_unfiltered (gdb_stdlog,
1778 "CBC: Push back breakpoint for %s\n",
1779 target_pid_to_str (lp->ptid));
1780
1781 /* Back up the PC if necessary. */
1782 if (DECR_PC_AFTER_BREAK)
1783 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
1784
1785 /* Throw away the SIGTRAP. */
1786 lp->status = 0;
1787 }
1788
1789 return 0;
1790 }
1791
1792 /* Select one LWP out of those that have events pending. */
1793
1794 static void
1795 select_event_lwp (struct lwp_info **orig_lp, int *status)
1796 {
1797 int num_events = 0;
1798 int random_selector;
1799 struct lwp_info *event_lp;
1800
1801 /* Record the wait status for the original LWP. */
1802 (*orig_lp)->status = *status;
1803
1804 /* Give preference to any LWP that is being single-stepped. */
1805 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
1806 if (event_lp != NULL)
1807 {
1808 if (debug_linux_nat)
1809 fprintf_unfiltered (gdb_stdlog,
1810 "SEL: Select single-step %s\n",
1811 target_pid_to_str (event_lp->ptid));
1812 }
1813 else
1814 {
1815 /* No single-stepping LWP. Select one at random, out of those
1816 which have had SIGTRAP events. */
1817
1818 /* First see how many SIGTRAP events we have. */
1819 iterate_over_lwps (count_events_callback, &num_events);
1820
1821 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1822 random_selector = (int)
1823 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1824
1825 if (debug_linux_nat && num_events > 1)
1826 fprintf_unfiltered (gdb_stdlog,
1827 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1828 num_events, random_selector);
1829
1830 event_lp = iterate_over_lwps (select_event_lwp_callback,
1831 &random_selector);
1832 }
1833
1834 if (event_lp != NULL)
1835 {
1836 /* Switch the event LWP. */
1837 *orig_lp = event_lp;
1838 *status = event_lp->status;
1839 }
1840
1841 /* Flush the wait status for the event LWP. */
1842 (*orig_lp)->status = 0;
1843 }
1844
1845 /* Return non-zero if LP has been resumed. */
1846
1847 static int
1848 resumed_callback (struct lwp_info *lp, void *data)
1849 {
1850 return lp->resumed;
1851 }
1852
1853 /* Local mourn_inferior -- we need to override mourn_inferior
1854 so that we can do something clever if one of several forks
1855 has exited. */
1856
1857 static void
1858 child_mourn_inferior (void)
1859 {
1860 int status;
1861
1862 if (! forks_exist_p ())
1863 {
1864 /* Normal case, no other forks available. */
1865 super_mourn_inferior ();
1866 return;
1867 }
1868 else
1869 {
1870 /* Multi-fork case. The current inferior_ptid has exited, but
1871 there are other viable forks to debug. Delete the exiting
1872 one and context-switch to the first available. */
1873 linux_fork_mourn_inferior ();
1874 }
1875 }
1876
1877 /* We need to override child_wait to support attaching to cloned
1878 processes, since a normal wait (as done by the default version)
1879 ignores those processes. */
1880
1881 /* Wait for child PTID to do something. Return id of the child,
1882 minus_one_ptid in case of error; store status into *OURSTATUS. */
1883
1884 ptid_t
1885 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1886 {
1887 int save_errno;
1888 int status;
1889 pid_t pid;
1890
1891 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1892
1893 do
1894 {
1895 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1896 attached process. */
1897 set_sigio_trap ();
1898
1899 pid = my_waitpid (GET_PID (ptid), &status, 0);
1900 if (pid == -1 && errno == ECHILD)
1901 /* Try again with __WCLONE to check cloned processes. */
1902 pid = my_waitpid (GET_PID (ptid), &status, __WCLONE);
1903
1904 if (debug_linux_nat)
1905 {
1906 fprintf_unfiltered (gdb_stdlog,
1907 "CW: waitpid %ld received %s\n",
1908 (long) pid, status_to_str (status));
1909 }
1910
1911 save_errno = errno;
1912
1913 /* Make sure we don't report an event for the exit of the
1914 original program, if we've detached from it. */
1915 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1916 {
1917 pid = -1;
1918 save_errno = EINTR;
1919 }
1920
1921 /* Check for stop events reported by a process we didn't already
1922 know about - in this case, anything other than inferior_ptid.
1923
1924 If we're expecting to receive stopped processes after fork,
1925 vfork, and clone events, then we'll just add the new one to
1926 our list and go back to waiting for the event to be reported
1927 - the stopped process might be returned from waitpid before
1928 or after the event is. If we want to handle debugging of
1929 CLONE_PTRACE processes we need to do more here, i.e. switch
1930 to multi-threaded mode. */
1931 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1932 && pid != GET_PID (inferior_ptid))
1933 {
1934 linux_record_stopped_pid (pid);
1935 pid = -1;
1936 save_errno = EINTR;
1937 }
1938
1939 /* Handle GNU/Linux's extended waitstatus for trace events. */
1940 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
1941 && status >> 16 != 0)
1942 {
1943 linux_handle_extended_wait (pid, status, ourstatus);
1944
1945 /* If we see a clone event, detach the child, and don't
1946 report the event. It would be nice to offer some way to
1947 switch into a non-thread-db based threaded mode at this
1948 point. */
1949 if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS)
1950 {
1951 ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0);
1952 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1953 ptrace (PTRACE_CONT, pid, 0, 0);
1954 pid = -1;
1955 save_errno = EINTR;
1956 }
1957 }
1958
1959 clear_sigio_trap ();
1960 clear_sigint_trap ();
1961 }
1962 while (pid == -1 && save_errno == EINTR);
1963
1964 if (pid == -1)
1965 {
1966 warning (_("Child process unexpectedly missing: %s"),
1967 safe_strerror (errno));
1968
1969 /* Claim it exited with unknown signal. */
1970 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1971 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
1972 return minus_one_ptid;
1973 }
1974
1975 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1976 store_waitstatus (ourstatus, status);
1977
1978 return pid_to_ptid (pid);
1979 }
1980
1981 /* Stop an active thread, verify it still exists, then resume it. */
1982
1983 static int
1984 stop_and_resume_callback (struct lwp_info *lp, void *data)
1985 {
1986 struct lwp_info *ptr;
1987
1988 if (!lp->stopped && !lp->signalled)
1989 {
1990 stop_callback (lp, NULL);
1991 stop_wait_callback (lp, NULL);
1992 /* Resume if the lwp still exists. */
1993 for (ptr = lwp_list; ptr; ptr = ptr->next)
1994 if (lp == ptr)
1995 {
1996 resume_callback (lp, NULL);
1997 resume_set_callback (lp, NULL);
1998 }
1999 }
2000 return 0;
2001 }
2002
2003 static ptid_t
2004 linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
2005 {
2006 struct lwp_info *lp = NULL;
2007 int options = 0;
2008 int status = 0;
2009 pid_t pid = PIDGET (ptid);
2010 sigset_t flush_mask;
2011
2012 sigemptyset (&flush_mask);
2013
2014 /* Make sure SIGCHLD is blocked. */
2015 if (!sigismember (&blocked_mask, SIGCHLD))
2016 {
2017 sigaddset (&blocked_mask, SIGCHLD);
2018 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
2019 }
2020
2021 retry:
2022
2023 /* Make sure there is at least one LWP that has been resumed, at
2024 least if there are any LWPs at all. */
2025 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
2026
2027 /* First check if there is a LWP with a wait status pending. */
2028 if (pid == -1)
2029 {
2030 /* Any LWP that's been resumed will do. */
2031 lp = iterate_over_lwps (status_callback, NULL);
2032 if (lp)
2033 {
2034 status = lp->status;
2035 lp->status = 0;
2036
2037 if (debug_linux_nat && status)
2038 fprintf_unfiltered (gdb_stdlog,
2039 "LLW: Using pending wait status %s for %s.\n",
2040 status_to_str (status),
2041 target_pid_to_str (lp->ptid));
2042 }
2043
2044 /* But if we don't fine one, we'll have to wait, and check both
2045 cloned and uncloned processes. We start with the cloned
2046 processes. */
2047 options = __WCLONE | WNOHANG;
2048 }
2049 else if (is_lwp (ptid))
2050 {
2051 if (debug_linux_nat)
2052 fprintf_unfiltered (gdb_stdlog,
2053 "LLW: Waiting for specific LWP %s.\n",
2054 target_pid_to_str (ptid));
2055
2056 /* We have a specific LWP to check. */
2057 lp = find_lwp_pid (ptid);
2058 gdb_assert (lp);
2059 status = lp->status;
2060 lp->status = 0;
2061
2062 if (debug_linux_nat && status)
2063 fprintf_unfiltered (gdb_stdlog,
2064 "LLW: Using pending wait status %s for %s.\n",
2065 status_to_str (status),
2066 target_pid_to_str (lp->ptid));
2067
2068 /* If we have to wait, take into account whether PID is a cloned
2069 process or not. And we have to convert it to something that
2070 the layer beneath us can understand. */
2071 options = lp->cloned ? __WCLONE : 0;
2072 pid = GET_LWP (ptid);
2073 }
2074
2075 if (status && lp->signalled)
2076 {
2077 /* A pending SIGSTOP may interfere with the normal stream of
2078 events. In a typical case where interference is a problem,
2079 we have a SIGSTOP signal pending for LWP A while
2080 single-stepping it, encounter an event in LWP B, and take the
2081 pending SIGSTOP while trying to stop LWP A. After processing
2082 the event in LWP B, LWP A is continued, and we'll never see
2083 the SIGTRAP associated with the last time we were
2084 single-stepping LWP A. */
2085
2086 /* Resume the thread. It should halt immediately returning the
2087 pending SIGSTOP. */
2088 registers_changed ();
2089 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2090 lp->step, TARGET_SIGNAL_0);
2091 if (debug_linux_nat)
2092 fprintf_unfiltered (gdb_stdlog,
2093 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
2094 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2095 target_pid_to_str (lp->ptid));
2096 lp->stopped = 0;
2097 gdb_assert (lp->resumed);
2098
2099 /* This should catch the pending SIGSTOP. */
2100 stop_wait_callback (lp, NULL);
2101 }
2102
2103 set_sigint_trap (); /* Causes SIGINT to be passed on to the
2104 attached process. */
2105 set_sigio_trap ();
2106
2107 while (status == 0)
2108 {
2109 pid_t lwpid;
2110
2111 lwpid = my_waitpid (pid, &status, options);
2112 if (lwpid > 0)
2113 {
2114 gdb_assert (pid == -1 || lwpid == pid);
2115
2116 if (debug_linux_nat)
2117 {
2118 fprintf_unfiltered (gdb_stdlog,
2119 "LLW: waitpid %ld received %s\n",
2120 (long) lwpid, status_to_str (status));
2121 }
2122
2123 lp = find_lwp_pid (pid_to_ptid (lwpid));
2124
2125 /* Check for stop events reported by a process we didn't
2126 already know about - anything not already in our LWP
2127 list.
2128
2129 If we're expecting to receive stopped processes after
2130 fork, vfork, and clone events, then we'll just add the
2131 new one to our list and go back to waiting for the event
2132 to be reported - the stopped process might be returned
2133 from waitpid before or after the event is. */
2134 if (WIFSTOPPED (status) && !lp)
2135 {
2136 linux_record_stopped_pid (lwpid);
2137 status = 0;
2138 continue;
2139 }
2140
2141 /* Make sure we don't report an event for the exit of an LWP not in
2142 our list, i.e. not part of the current process. This can happen
2143 if we detach from a program we original forked and then it
2144 exits. */
2145 if (!WIFSTOPPED (status) && !lp)
2146 {
2147 status = 0;
2148 continue;
2149 }
2150
2151 /* NOTE drow/2003-06-17: This code seems to be meant for debugging
2152 CLONE_PTRACE processes which do not use the thread library -
2153 otherwise we wouldn't find the new LWP this way. That doesn't
2154 currently work, and the following code is currently unreachable
2155 due to the two blocks above. If it's fixed some day, this code
2156 should be broken out into a function so that we can also pick up
2157 LWPs from the new interface. */
2158 if (!lp)
2159 {
2160 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
2161 if (options & __WCLONE)
2162 lp->cloned = 1;
2163
2164 if (threaded)
2165 {
2166 gdb_assert (WIFSTOPPED (status)
2167 && WSTOPSIG (status) == SIGSTOP);
2168 lp->signalled = 1;
2169
2170 if (!in_thread_list (inferior_ptid))
2171 {
2172 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
2173 GET_PID (inferior_ptid));
2174 add_thread (inferior_ptid);
2175 }
2176
2177 add_thread (lp->ptid);
2178 printf_unfiltered (_("[New %s]\n"),
2179 target_pid_to_str (lp->ptid));
2180 }
2181 }
2182
2183 /* Handle GNU/Linux's extended waitstatus for trace events. */
2184 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
2185 {
2186 if (debug_linux_nat)
2187 fprintf_unfiltered (gdb_stdlog,
2188 "LLW: Handling extended status 0x%06x\n",
2189 status);
2190 if (linux_nat_handle_extended (lp, status))
2191 {
2192 status = 0;
2193 continue;
2194 }
2195 }
2196
2197 /* Check if the thread has exited. */
2198 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
2199 {
2200 /* If this is the main thread, we must stop all threads and
2201 verify if they are still alive. This is because in the nptl
2202 thread model, there is no signal issued for exiting LWPs
2203 other than the main thread. We only get the main thread
2204 exit signal once all child threads have already exited.
2205 If we stop all the threads and use the stop_wait_callback
2206 to check if they have exited we can determine whether this
2207 signal should be ignored or whether it means the end of the
2208 debugged application, regardless of which threading model
2209 is being used. */
2210 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
2211 {
2212 lp->stopped = 1;
2213 iterate_over_lwps (stop_and_resume_callback, NULL);
2214 }
2215
2216 if (debug_linux_nat)
2217 fprintf_unfiltered (gdb_stdlog,
2218 "LLW: %s exited.\n",
2219 target_pid_to_str (lp->ptid));
2220
2221 exit_lwp (lp);
2222
2223 /* If there is at least one more LWP, then the exit signal
2224 was not the end of the debugged application and should be
2225 ignored. */
2226 if (num_lwps > 0)
2227 {
2228 /* Make sure there is at least one thread running. */
2229 gdb_assert (iterate_over_lwps (running_callback, NULL));
2230
2231 /* Discard the event. */
2232 status = 0;
2233 continue;
2234 }
2235 }
2236
2237 /* Check if the current LWP has previously exited. In the nptl
2238 thread model, LWPs other than the main thread do not issue
2239 signals when they exit so we must check whenever the thread
2240 has stopped. A similar check is made in stop_wait_callback(). */
2241 if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid))
2242 {
2243 if (debug_linux_nat)
2244 fprintf_unfiltered (gdb_stdlog,
2245 "LLW: %s exited.\n",
2246 target_pid_to_str (lp->ptid));
2247
2248 exit_lwp (lp);
2249
2250 /* Make sure there is at least one thread running. */
2251 gdb_assert (iterate_over_lwps (running_callback, NULL));
2252
2253 /* Discard the event. */
2254 status = 0;
2255 continue;
2256 }
2257
2258 /* Make sure we don't report a SIGSTOP that we sent
2259 ourselves in an attempt to stop an LWP. */
2260 if (lp->signalled
2261 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2262 {
2263 if (debug_linux_nat)
2264 fprintf_unfiltered (gdb_stdlog,
2265 "LLW: Delayed SIGSTOP caught for %s.\n",
2266 target_pid_to_str (lp->ptid));
2267
2268 /* This is a delayed SIGSTOP. */
2269 lp->signalled = 0;
2270
2271 registers_changed ();
2272 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2273 lp->step, TARGET_SIGNAL_0);
2274 if (debug_linux_nat)
2275 fprintf_unfiltered (gdb_stdlog,
2276 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
2277 lp->step ?
2278 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2279 target_pid_to_str (lp->ptid));
2280
2281 lp->stopped = 0;
2282 gdb_assert (lp->resumed);
2283
2284 /* Discard the event. */
2285 status = 0;
2286 continue;
2287 }
2288
2289 break;
2290 }
2291
2292 if (pid == -1)
2293 {
2294 /* Alternate between checking cloned and uncloned processes. */
2295 options ^= __WCLONE;
2296
2297 /* And suspend every time we have checked both. */
2298 if (options & __WCLONE)
2299 sigsuspend (&suspend_mask);
2300 }
2301
2302 /* We shouldn't end up here unless we want to try again. */
2303 gdb_assert (status == 0);
2304 }
2305
2306 clear_sigio_trap ();
2307 clear_sigint_trap ();
2308
2309 gdb_assert (lp);
2310
2311 /* Don't report signals that GDB isn't interested in, such as
2312 signals that are neither printed nor stopped upon. Stopping all
2313 threads can be a bit time-consuming so if we want decent
2314 performance with heavily multi-threaded programs, especially when
2315 they're using a high frequency timer, we'd better avoid it if we
2316 can. */
2317
2318 if (WIFSTOPPED (status))
2319 {
2320 int signo = target_signal_from_host (WSTOPSIG (status));
2321
2322 if (signal_stop_state (signo) == 0
2323 && signal_print_state (signo) == 0
2324 && signal_pass_state (signo) == 1)
2325 {
2326 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
2327 here? It is not clear we should. GDB may not expect
2328 other threads to run. On the other hand, not resuming
2329 newly attached threads may cause an unwanted delay in
2330 getting them running. */
2331 registers_changed ();
2332 linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)),
2333 lp->step, signo);
2334 if (debug_linux_nat)
2335 fprintf_unfiltered (gdb_stdlog,
2336 "LLW: %s %s, %s (preempt 'handle')\n",
2337 lp->step ?
2338 "PTRACE_SINGLESTEP" : "PTRACE_CONT",
2339 target_pid_to_str (lp->ptid),
2340 signo ? strsignal (signo) : "0");
2341 lp->stopped = 0;
2342 status = 0;
2343 goto retry;
2344 }
2345
2346 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
2347 {
2348 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
2349 forwarded to the entire process group, that is, all LWP's
2350 will receive it. Since we only want to report it once,
2351 we try to flush it from all LWPs except this one. */
2352 sigaddset (&flush_mask, SIGINT);
2353 }
2354 }
2355
2356 /* This LWP is stopped now. */
2357 lp->stopped = 1;
2358
2359 if (debug_linux_nat)
2360 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
2361 status_to_str (status), target_pid_to_str (lp->ptid));
2362
2363 /* Now stop all other LWP's ... */
2364 iterate_over_lwps (stop_callback, NULL);
2365
2366 /* ... and wait until all of them have reported back that they're no
2367 longer running. */
2368 iterate_over_lwps (stop_wait_callback, &flush_mask);
2369 iterate_over_lwps (flush_callback, &flush_mask);
2370
2371 /* If we're not waiting for a specific LWP, choose an event LWP from
2372 among those that have had events. Giving equal priority to all
2373 LWPs that have had events helps prevent starvation. */
2374 if (pid == -1)
2375 select_event_lwp (&lp, &status);
2376
2377 /* Now that we've selected our final event LWP, cancel any
2378 breakpoints in other LWPs that have hit a GDB breakpoint. See
2379 the comment in cancel_breakpoints_callback to find out why. */
2380 iterate_over_lwps (cancel_breakpoints_callback, lp);
2381
2382 /* If we're not running in "threaded" mode, we'll report the bare
2383 process id. */
2384
2385 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
2386 {
2387 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2388 if (debug_linux_nat)
2389 fprintf_unfiltered (gdb_stdlog,
2390 "LLW: trap_ptid is %s.\n",
2391 target_pid_to_str (trap_ptid));
2392 }
2393 else
2394 trap_ptid = null_ptid;
2395
2396 if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
2397 {
2398 *ourstatus = lp->waitstatus;
2399 lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
2400 }
2401 else
2402 store_waitstatus (ourstatus, status);
2403
2404 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
2405 }
2406
2407 static int
2408 kill_callback (struct lwp_info *lp, void *data)
2409 {
2410 errno = 0;
2411 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
2412 if (debug_linux_nat)
2413 fprintf_unfiltered (gdb_stdlog,
2414 "KC: PTRACE_KILL %s, 0, 0 (%s)\n",
2415 target_pid_to_str (lp->ptid),
2416 errno ? safe_strerror (errno) : "OK");
2417
2418 return 0;
2419 }
2420
2421 static int
2422 kill_wait_callback (struct lwp_info *lp, void *data)
2423 {
2424 pid_t pid;
2425
2426 /* We must make sure that there are no pending events (delayed
2427 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
2428 program doesn't interfere with any following debugging session. */
2429
2430 /* For cloned processes we must check both with __WCLONE and
2431 without, since the exit status of a cloned process isn't reported
2432 with __WCLONE. */
2433 if (lp->cloned)
2434 {
2435 do
2436 {
2437 pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
2438 if (pid != (pid_t) -1 && debug_linux_nat)
2439 {
2440 fprintf_unfiltered (gdb_stdlog,
2441 "KWC: wait %s received unknown.\n",
2442 target_pid_to_str (lp->ptid));
2443 }
2444 }
2445 while (pid == GET_LWP (lp->ptid));
2446
2447 gdb_assert (pid == -1 && errno == ECHILD);
2448 }
2449
2450 do
2451 {
2452 pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0);
2453 if (pid != (pid_t) -1 && debug_linux_nat)
2454 {
2455 fprintf_unfiltered (gdb_stdlog,
2456 "KWC: wait %s received unk.\n",
2457 target_pid_to_str (lp->ptid));
2458 }
2459 }
2460 while (pid == GET_LWP (lp->ptid));
2461
2462 gdb_assert (pid == -1 && errno == ECHILD);
2463 return 0;
2464 }
2465
2466 static void
2467 linux_nat_kill (void)
2468 {
2469 /* Kill all LWP's ... */
2470 iterate_over_lwps (kill_callback, NULL);
2471
2472 /* ... and wait until we've flushed all events. */
2473 iterate_over_lwps (kill_wait_callback, NULL);
2474
2475 target_mourn_inferior ();
2476 }
2477
2478 static void
2479 linux_nat_create_inferior (char *exec_file, char *allargs, char **env,
2480 int from_tty)
2481 {
2482 linux_ops->to_create_inferior (exec_file, allargs, env, from_tty);
2483 }
2484
2485 static void
2486 linux_nat_mourn_inferior (void)
2487 {
2488 trap_ptid = null_ptid;
2489
2490 /* Destroy LWP info; it's no longer valid. */
2491 init_lwp_list ();
2492
2493 /* Restore the original signal mask. */
2494 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
2495 sigemptyset (&blocked_mask);
2496
2497 linux_ops->to_mourn_inferior ();
2498 }
2499
2500 static LONGEST
2501 linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
2502 const char *annex, gdb_byte *readbuf,
2503 const gdb_byte *writebuf,
2504 ULONGEST offset, LONGEST len)
2505 {
2506 struct cleanup *old_chain = save_inferior_ptid ();
2507 LONGEST xfer;
2508
2509 if (is_lwp (inferior_ptid))
2510 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
2511
2512 xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
2513 offset, len);
2514
2515 do_cleanups (old_chain);
2516 return xfer;
2517 }
2518
2519 static int
2520 linux_nat_thread_alive (ptid_t ptid)
2521 {
2522 gdb_assert (is_lwp (ptid));
2523
2524 errno = 0;
2525 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
2526 if (debug_linux_nat)
2527 fprintf_unfiltered (gdb_stdlog,
2528 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
2529 target_pid_to_str (ptid),
2530 errno ? safe_strerror (errno) : "OK");
2531 if (errno)
2532 return 0;
2533
2534 return 1;
2535 }
2536
2537 static char *
2538 linux_nat_pid_to_str (ptid_t ptid)
2539 {
2540 static char buf[64];
2541
2542 if (is_lwp (ptid))
2543 {
2544 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
2545 return buf;
2546 }
2547
2548 return normal_pid_to_str (ptid);
2549 }
2550
2551 static void
2552 linux_nat_fetch_registers (int regnum)
2553 {
2554 /* to_fetch_registers will honor the LWP ID, so we can use it directly. */
2555 linux_ops->to_fetch_registers (regnum);
2556 }
2557
2558 static void
2559 linux_nat_store_registers (int regnum)
2560 {
2561 /* to_store_registers will honor the LWP ID, so we can use it directly. */
2562 linux_ops->to_store_registers (regnum);
2563 }
2564
2565 static void
2566 linux_nat_child_post_startup_inferior (ptid_t ptid)
2567 {
2568 linux_ops->to_post_startup_inferior (ptid);
2569 }
2570
2571 static void
2572 init_linux_nat_ops (void)
2573 {
2574 #if 0
2575 linux_nat_ops.to_open = linux_nat_open;
2576 #endif
2577 linux_nat_ops.to_shortname = "lwp-layer";
2578 linux_nat_ops.to_longname = "lwp-layer";
2579 linux_nat_ops.to_doc = "Low level threads support (LWP layer)";
2580 linux_nat_ops.to_attach = linux_nat_attach;
2581 linux_nat_ops.to_detach = linux_nat_detach;
2582 linux_nat_ops.to_resume = linux_nat_resume;
2583 linux_nat_ops.to_wait = linux_nat_wait;
2584 linux_nat_ops.to_fetch_registers = linux_nat_fetch_registers;
2585 linux_nat_ops.to_store_registers = linux_nat_store_registers;
2586 linux_nat_ops.to_xfer_partial = linux_nat_xfer_partial;
2587 linux_nat_ops.to_kill = linux_nat_kill;
2588 linux_nat_ops.to_create_inferior = linux_nat_create_inferior;
2589 linux_nat_ops.to_mourn_inferior = linux_nat_mourn_inferior;
2590 linux_nat_ops.to_thread_alive = linux_nat_thread_alive;
2591 linux_nat_ops.to_pid_to_str = linux_nat_pid_to_str;
2592 linux_nat_ops.to_post_startup_inferior
2593 = linux_nat_child_post_startup_inferior;
2594 linux_nat_ops.to_post_attach = child_post_attach;
2595 linux_nat_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
2596 linux_nat_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
2597 linux_nat_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
2598
2599 linux_nat_ops.to_stratum = thread_stratum;
2600 linux_nat_ops.to_has_thread_control = tc_schedlock;
2601 linux_nat_ops.to_magic = OPS_MAGIC;
2602 }
2603
2604 static void
2605 sigchld_handler (int signo)
2606 {
2607 /* Do nothing. The only reason for this handler is that it allows
2608 us to use sigsuspend in linux_nat_wait above to wait for the
2609 arrival of a SIGCHLD. */
2610 }
2611
2612 /* Accepts an integer PID; Returns a string representing a file that
2613 can be opened to get the symbols for the child process. */
2614
2615 char *
2616 child_pid_to_exec_file (int pid)
2617 {
2618 char *name1, *name2;
2619
2620 name1 = xmalloc (MAXPATHLEN);
2621 name2 = xmalloc (MAXPATHLEN);
2622 make_cleanup (xfree, name1);
2623 make_cleanup (xfree, name2);
2624 memset (name2, 0, MAXPATHLEN);
2625
2626 sprintf (name1, "/proc/%d/exe", pid);
2627 if (readlink (name1, name2, MAXPATHLEN) > 0)
2628 return name2;
2629 else
2630 return name1;
2631 }
2632
2633 /* Service function for corefiles and info proc. */
2634
2635 static int
2636 read_mapping (FILE *mapfile,
2637 long long *addr,
2638 long long *endaddr,
2639 char *permissions,
2640 long long *offset,
2641 char *device, long long *inode, char *filename)
2642 {
2643 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
2644 addr, endaddr, permissions, offset, device, inode);
2645
2646 filename[0] = '\0';
2647 if (ret > 0 && ret != EOF)
2648 {
2649 /* Eat everything up to EOL for the filename. This will prevent
2650 weird filenames (such as one with embedded whitespace) from
2651 confusing this code. It also makes this code more robust in
2652 respect to annotations the kernel may add after the filename.
2653
2654 Note the filename is used for informational purposes
2655 only. */
2656 ret += fscanf (mapfile, "%[^\n]\n", filename);
2657 }
2658
2659 return (ret != 0 && ret != EOF);
2660 }
2661
2662 /* Fills the "to_find_memory_regions" target vector. Lists the memory
2663 regions in the inferior for a corefile. */
2664
2665 static int
2666 linux_nat_find_memory_regions (int (*func) (CORE_ADDR,
2667 unsigned long,
2668 int, int, int, void *), void *obfd)
2669 {
2670 long long pid = PIDGET (inferior_ptid);
2671 char mapsfilename[MAXPATHLEN];
2672 FILE *mapsfile;
2673 long long addr, endaddr, size, offset, inode;
2674 char permissions[8], device[8], filename[MAXPATHLEN];
2675 int read, write, exec;
2676 int ret;
2677
2678 /* Compose the filename for the /proc memory map, and open it. */
2679 sprintf (mapsfilename, "/proc/%lld/maps", pid);
2680 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
2681 error (_("Could not open %s."), mapsfilename);
2682
2683 if (info_verbose)
2684 fprintf_filtered (gdb_stdout,
2685 "Reading memory regions from %s\n", mapsfilename);
2686
2687 /* Now iterate until end-of-file. */
2688 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
2689 &offset, &device[0], &inode, &filename[0]))
2690 {
2691 size = endaddr - addr;
2692
2693 /* Get the segment's permissions. */
2694 read = (strchr (permissions, 'r') != 0);
2695 write = (strchr (permissions, 'w') != 0);
2696 exec = (strchr (permissions, 'x') != 0);
2697
2698 if (info_verbose)
2699 {
2700 fprintf_filtered (gdb_stdout,
2701 "Save segment, %lld bytes at 0x%s (%c%c%c)",
2702 size, paddr_nz (addr),
2703 read ? 'r' : ' ',
2704 write ? 'w' : ' ', exec ? 'x' : ' ');
2705 if (filename && filename[0])
2706 fprintf_filtered (gdb_stdout, " for %s", filename);
2707 fprintf_filtered (gdb_stdout, "\n");
2708 }
2709
2710 /* Invoke the callback function to create the corefile
2711 segment. */
2712 func (addr, size, read, write, exec, obfd);
2713 }
2714 fclose (mapsfile);
2715 return 0;
2716 }
2717
2718 /* Records the thread's register state for the corefile note
2719 section. */
2720
2721 static char *
2722 linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
2723 char *note_data, int *note_size)
2724 {
2725 gdb_gregset_t gregs;
2726 gdb_fpregset_t fpregs;
2727 #ifdef FILL_FPXREGSET
2728 gdb_fpxregset_t fpxregs;
2729 #endif
2730 unsigned long lwp = ptid_get_lwp (ptid);
2731
2732 fill_gregset (&gregs, -1);
2733 note_data = (char *) elfcore_write_prstatus (obfd,
2734 note_data,
2735 note_size,
2736 lwp,
2737 stop_signal, &gregs);
2738
2739 fill_fpregset (&fpregs, -1);
2740 note_data = (char *) elfcore_write_prfpreg (obfd,
2741 note_data,
2742 note_size,
2743 &fpregs, sizeof (fpregs));
2744 #ifdef FILL_FPXREGSET
2745 fill_fpxregset (&fpxregs, -1);
2746 note_data = (char *) elfcore_write_prxfpreg (obfd,
2747 note_data,
2748 note_size,
2749 &fpxregs, sizeof (fpxregs));
2750 #endif
2751 return note_data;
2752 }
2753
2754 struct linux_nat_corefile_thread_data
2755 {
2756 bfd *obfd;
2757 char *note_data;
2758 int *note_size;
2759 int num_notes;
2760 };
2761
2762 /* Called by gdbthread.c once per thread. Records the thread's
2763 register state for the corefile note section. */
2764
2765 static int
2766 linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
2767 {
2768 struct linux_nat_corefile_thread_data *args = data;
2769 ptid_t saved_ptid = inferior_ptid;
2770
2771 inferior_ptid = ti->ptid;
2772 registers_changed ();
2773 target_fetch_registers (-1); /* FIXME should not be necessary;
2774 fill_gregset should do it automatically. */
2775 args->note_data = linux_nat_do_thread_registers (args->obfd,
2776 ti->ptid,
2777 args->note_data,
2778 args->note_size);
2779 args->num_notes++;
2780 inferior_ptid = saved_ptid;
2781 registers_changed ();
2782 target_fetch_registers (-1); /* FIXME should not be necessary;
2783 fill_gregset should do it automatically. */
2784 return 0;
2785 }
2786
2787 /* Records the register state for the corefile note section. */
2788
2789 static char *
2790 linux_nat_do_registers (bfd *obfd, ptid_t ptid,
2791 char *note_data, int *note_size)
2792 {
2793 registers_changed ();
2794 target_fetch_registers (-1); /* FIXME should not be necessary;
2795 fill_gregset should do it automatically. */
2796 return linux_nat_do_thread_registers (obfd,
2797 ptid_build (ptid_get_pid (inferior_ptid),
2798 ptid_get_pid (inferior_ptid),
2799 0),
2800 note_data, note_size);
2801 return note_data;
2802 }
2803
2804 /* Fills the "to_make_corefile_note" target vector. Builds the note
2805 section for a corefile, and returns it in a malloc buffer. */
2806
2807 static char *
2808 linux_nat_make_corefile_notes (bfd *obfd, int *note_size)
2809 {
2810 struct linux_nat_corefile_thread_data thread_args;
2811 struct cleanup *old_chain;
2812 char fname[16] = { '\0' };
2813 char psargs[80] = { '\0' };
2814 char *note_data = NULL;
2815 ptid_t current_ptid = inferior_ptid;
2816 gdb_byte *auxv;
2817 int auxv_len;
2818
2819 if (get_exec_file (0))
2820 {
2821 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
2822 strncpy (psargs, get_exec_file (0), sizeof (psargs));
2823 if (get_inferior_args ())
2824 {
2825 strncat (psargs, " ", sizeof (psargs) - strlen (psargs));
2826 strncat (psargs, get_inferior_args (),
2827 sizeof (psargs) - strlen (psargs));
2828 }
2829 note_data = (char *) elfcore_write_prpsinfo (obfd,
2830 note_data,
2831 note_size, fname, psargs);
2832 }
2833
2834 /* Dump information for threads. */
2835 thread_args.obfd = obfd;
2836 thread_args.note_data = note_data;
2837 thread_args.note_size = note_size;
2838 thread_args.num_notes = 0;
2839 iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args);
2840 if (thread_args.num_notes == 0)
2841 {
2842 /* iterate_over_threads didn't come up with any threads; just
2843 use inferior_ptid. */
2844 note_data = linux_nat_do_registers (obfd, inferior_ptid,
2845 note_data, note_size);
2846 }
2847 else
2848 {
2849 note_data = thread_args.note_data;
2850 }
2851
2852 auxv_len = target_auxv_read (&current_target, &auxv);
2853 if (auxv_len > 0)
2854 {
2855 note_data = elfcore_write_note (obfd, note_data, note_size,
2856 "CORE", NT_AUXV, auxv, auxv_len);
2857 xfree (auxv);
2858 }
2859
2860 make_cleanup (xfree, note_data);
2861 return note_data;
2862 }
2863
2864 /* Implement the "info proc" command. */
2865
2866 static void
2867 linux_nat_info_proc_cmd (char *args, int from_tty)
2868 {
2869 long long pid = PIDGET (inferior_ptid);
2870 FILE *procfile;
2871 char **argv = NULL;
2872 char buffer[MAXPATHLEN];
2873 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
2874 int cmdline_f = 1;
2875 int cwd_f = 1;
2876 int exe_f = 1;
2877 int mappings_f = 0;
2878 int environ_f = 0;
2879 int status_f = 0;
2880 int stat_f = 0;
2881 int all = 0;
2882 struct stat dummy;
2883
2884 if (args)
2885 {
2886 /* Break up 'args' into an argv array. */
2887 if ((argv = buildargv (args)) == NULL)
2888 nomem (0);
2889 else
2890 make_cleanup_freeargv (argv);
2891 }
2892 while (argv != NULL && *argv != NULL)
2893 {
2894 if (isdigit (argv[0][0]))
2895 {
2896 pid = strtoul (argv[0], NULL, 10);
2897 }
2898 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
2899 {
2900 mappings_f = 1;
2901 }
2902 else if (strcmp (argv[0], "status") == 0)
2903 {
2904 status_f = 1;
2905 }
2906 else if (strcmp (argv[0], "stat") == 0)
2907 {
2908 stat_f = 1;
2909 }
2910 else if (strcmp (argv[0], "cmd") == 0)
2911 {
2912 cmdline_f = 1;
2913 }
2914 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
2915 {
2916 exe_f = 1;
2917 }
2918 else if (strcmp (argv[0], "cwd") == 0)
2919 {
2920 cwd_f = 1;
2921 }
2922 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
2923 {
2924 all = 1;
2925 }
2926 else
2927 {
2928 /* [...] (future options here) */
2929 }
2930 argv++;
2931 }
2932 if (pid == 0)
2933 error (_("No current process: you must name one."));
2934
2935 sprintf (fname1, "/proc/%lld", pid);
2936 if (stat (fname1, &dummy) != 0)
2937 error (_("No /proc directory: '%s'"), fname1);
2938
2939 printf_filtered (_("process %lld\n"), pid);
2940 if (cmdline_f || all)
2941 {
2942 sprintf (fname1, "/proc/%lld/cmdline", pid);
2943 if ((procfile = fopen (fname1, "r")) > 0)
2944 {
2945 fgets (buffer, sizeof (buffer), procfile);
2946 printf_filtered ("cmdline = '%s'\n", buffer);
2947 fclose (procfile);
2948 }
2949 else
2950 warning (_("unable to open /proc file '%s'"), fname1);
2951 }
2952 if (cwd_f || all)
2953 {
2954 sprintf (fname1, "/proc/%lld/cwd", pid);
2955 memset (fname2, 0, sizeof (fname2));
2956 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2957 printf_filtered ("cwd = '%s'\n", fname2);
2958 else
2959 warning (_("unable to read link '%s'"), fname1);
2960 }
2961 if (exe_f || all)
2962 {
2963 sprintf (fname1, "/proc/%lld/exe", pid);
2964 memset (fname2, 0, sizeof (fname2));
2965 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
2966 printf_filtered ("exe = '%s'\n", fname2);
2967 else
2968 warning (_("unable to read link '%s'"), fname1);
2969 }
2970 if (mappings_f || all)
2971 {
2972 sprintf (fname1, "/proc/%lld/maps", pid);
2973 if ((procfile = fopen (fname1, "r")) > 0)
2974 {
2975 long long addr, endaddr, size, offset, inode;
2976 char permissions[8], device[8], filename[MAXPATHLEN];
2977
2978 printf_filtered (_("Mapped address spaces:\n\n"));
2979 if (TARGET_ADDR_BIT == 32)
2980 {
2981 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2982 "Start Addr",
2983 " End Addr",
2984 " Size", " Offset", "objfile");
2985 }
2986 else
2987 {
2988 printf_filtered (" %18s %18s %10s %10s %7s\n",
2989 "Start Addr",
2990 " End Addr",
2991 " Size", " Offset", "objfile");
2992 }
2993
2994 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
2995 &offset, &device[0], &inode, &filename[0]))
2996 {
2997 size = endaddr - addr;
2998
2999 /* FIXME: carlton/2003-08-27: Maybe the printf_filtered
3000 calls here (and possibly above) should be abstracted
3001 out into their own functions? Andrew suggests using
3002 a generic local_address_string instead to print out
3003 the addresses; that makes sense to me, too. */
3004
3005 if (TARGET_ADDR_BIT == 32)
3006 {
3007 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3008 (unsigned long) addr, /* FIXME: pr_addr */
3009 (unsigned long) endaddr,
3010 (int) size,
3011 (unsigned int) offset,
3012 filename[0] ? filename : "");
3013 }
3014 else
3015 {
3016 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3017 (unsigned long) addr, /* FIXME: pr_addr */
3018 (unsigned long) endaddr,
3019 (int) size,
3020 (unsigned int) offset,
3021 filename[0] ? filename : "");
3022 }
3023 }
3024
3025 fclose (procfile);
3026 }
3027 else
3028 warning (_("unable to open /proc file '%s'"), fname1);
3029 }
3030 if (status_f || all)
3031 {
3032 sprintf (fname1, "/proc/%lld/status", pid);
3033 if ((procfile = fopen (fname1, "r")) > 0)
3034 {
3035 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
3036 puts_filtered (buffer);
3037 fclose (procfile);
3038 }
3039 else
3040 warning (_("unable to open /proc file '%s'"), fname1);
3041 }
3042 if (stat_f || all)
3043 {
3044 sprintf (fname1, "/proc/%lld/stat", pid);
3045 if ((procfile = fopen (fname1, "r")) > 0)
3046 {
3047 int itmp;
3048 char ctmp;
3049
3050 if (fscanf (procfile, "%d ", &itmp) > 0)
3051 printf_filtered (_("Process: %d\n"), itmp);
3052 if (fscanf (procfile, "%s ", &buffer[0]) > 0)
3053 printf_filtered (_("Exec file: %s\n"), buffer);
3054 if (fscanf (procfile, "%c ", &ctmp) > 0)
3055 printf_filtered (_("State: %c\n"), ctmp);
3056 if (fscanf (procfile, "%d ", &itmp) > 0)
3057 printf_filtered (_("Parent process: %d\n"), itmp);
3058 if (fscanf (procfile, "%d ", &itmp) > 0)
3059 printf_filtered (_("Process group: %d\n"), itmp);
3060 if (fscanf (procfile, "%d ", &itmp) > 0)
3061 printf_filtered (_("Session id: %d\n"), itmp);
3062 if (fscanf (procfile, "%d ", &itmp) > 0)
3063 printf_filtered (_("TTY: %d\n"), itmp);
3064 if (fscanf (procfile, "%d ", &itmp) > 0)
3065 printf_filtered (_("TTY owner process group: %d\n"), itmp);
3066 if (fscanf (procfile, "%u ", &itmp) > 0)
3067 printf_filtered (_("Flags: 0x%x\n"), itmp);
3068 if (fscanf (procfile, "%u ", &itmp) > 0)
3069 printf_filtered (_("Minor faults (no memory page): %u\n"),
3070 (unsigned int) itmp);
3071 if (fscanf (procfile, "%u ", &itmp) > 0)
3072 printf_filtered (_("Minor faults, children: %u\n"),
3073 (unsigned int) itmp);
3074 if (fscanf (procfile, "%u ", &itmp) > 0)
3075 printf_filtered (_("Major faults (memory page faults): %u\n"),
3076 (unsigned int) itmp);
3077 if (fscanf (procfile, "%u ", &itmp) > 0)
3078 printf_filtered (_("Major faults, children: %u\n"),
3079 (unsigned int) itmp);
3080 if (fscanf (procfile, "%d ", &itmp) > 0)
3081 printf_filtered ("utime: %d\n", itmp);
3082 if (fscanf (procfile, "%d ", &itmp) > 0)
3083 printf_filtered ("stime: %d\n", itmp);
3084 if (fscanf (procfile, "%d ", &itmp) > 0)
3085 printf_filtered ("utime, children: %d\n", itmp);
3086 if (fscanf (procfile, "%d ", &itmp) > 0)
3087 printf_filtered ("stime, children: %d\n", itmp);
3088 if (fscanf (procfile, "%d ", &itmp) > 0)
3089 printf_filtered (_("jiffies remaining in current time slice: %d\n"),
3090 itmp);
3091 if (fscanf (procfile, "%d ", &itmp) > 0)
3092 printf_filtered ("'nice' value: %d\n", itmp);
3093 if (fscanf (procfile, "%u ", &itmp) > 0)
3094 printf_filtered (_("jiffies until next timeout: %u\n"),
3095 (unsigned int) itmp);
3096 if (fscanf (procfile, "%u ", &itmp) > 0)
3097 printf_filtered ("jiffies until next SIGALRM: %u\n",
3098 (unsigned int) itmp);
3099 if (fscanf (procfile, "%d ", &itmp) > 0)
3100 printf_filtered (_("start time (jiffies since system boot): %d\n"),
3101 itmp);
3102 if (fscanf (procfile, "%u ", &itmp) > 0)
3103 printf_filtered (_("Virtual memory size: %u\n"),
3104 (unsigned int) itmp);
3105 if (fscanf (procfile, "%u ", &itmp) > 0)
3106 printf_filtered (_("Resident set size: %u\n"), (unsigned int) itmp);
3107 if (fscanf (procfile, "%u ", &itmp) > 0)
3108 printf_filtered ("rlim: %u\n", (unsigned int) itmp);
3109 if (fscanf (procfile, "%u ", &itmp) > 0)
3110 printf_filtered (_("Start of text: 0x%x\n"), itmp);
3111 if (fscanf (procfile, "%u ", &itmp) > 0)
3112 printf_filtered (_("End of text: 0x%x\n"), itmp);
3113 if (fscanf (procfile, "%u ", &itmp) > 0)
3114 printf_filtered (_("Start of stack: 0x%x\n"), itmp);
3115 #if 0 /* Don't know how architecture-dependent the rest is...
3116 Anyway the signal bitmap info is available from "status". */
3117 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
3118 printf_filtered (_("Kernel stack pointer: 0x%x\n"), itmp);
3119 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
3120 printf_filtered (_("Kernel instr pointer: 0x%x\n"), itmp);
3121 if (fscanf (procfile, "%d ", &itmp) > 0)
3122 printf_filtered (_("Pending signals bitmap: 0x%x\n"), itmp);
3123 if (fscanf (procfile, "%d ", &itmp) > 0)
3124 printf_filtered (_("Blocked signals bitmap: 0x%x\n"), itmp);
3125 if (fscanf (procfile, "%d ", &itmp) > 0)
3126 printf_filtered (_("Ignored signals bitmap: 0x%x\n"), itmp);
3127 if (fscanf (procfile, "%d ", &itmp) > 0)
3128 printf_filtered (_("Catched signals bitmap: 0x%x\n"), itmp);
3129 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
3130 printf_filtered (_("wchan (system call): 0x%x\n"), itmp);
3131 #endif
3132 fclose (procfile);
3133 }
3134 else
3135 warning (_("unable to open /proc file '%s'"), fname1);
3136 }
3137 }
3138
3139 /* Implement the to_xfer_partial interface for memory reads using the /proc
3140 filesystem. Because we can use a single read() call for /proc, this
3141 can be much more efficient than banging away at PTRACE_PEEKTEXT,
3142 but it doesn't support writes. */
3143
3144 static LONGEST
3145 linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
3146 const char *annex, gdb_byte *readbuf,
3147 const gdb_byte *writebuf,
3148 ULONGEST offset, LONGEST len)
3149 {
3150 LONGEST ret;
3151 int fd;
3152 char filename[64];
3153
3154 if (object != TARGET_OBJECT_MEMORY || !readbuf)
3155 return 0;
3156
3157 /* Don't bother for one word. */
3158 if (len < 3 * sizeof (long))
3159 return 0;
3160
3161 /* We could keep this file open and cache it - possibly one per
3162 thread. That requires some juggling, but is even faster. */
3163 sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
3164 fd = open (filename, O_RDONLY | O_LARGEFILE);
3165 if (fd == -1)
3166 return 0;
3167
3168 /* If pread64 is available, use it. It's faster if the kernel
3169 supports it (only one syscall), and it's 64-bit safe even on
3170 32-bit platforms (for instance, SPARC debugging a SPARC64
3171 application). */
3172 #ifdef HAVE_PREAD64
3173 if (pread64 (fd, readbuf, len, offset) != len)
3174 #else
3175 if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
3176 #endif
3177 ret = 0;
3178 else
3179 ret = len;
3180
3181 close (fd);
3182 return ret;
3183 }
3184
3185 /* Parse LINE as a signal set and add its set bits to SIGS. */
3186
3187 static void
3188 add_line_to_sigset (const char *line, sigset_t *sigs)
3189 {
3190 int len = strlen (line) - 1;
3191 const char *p;
3192 int signum;
3193
3194 if (line[len] != '\n')
3195 error (_("Could not parse signal set: %s"), line);
3196
3197 p = line;
3198 signum = len * 4;
3199 while (len-- > 0)
3200 {
3201 int digit;
3202
3203 if (*p >= '0' && *p <= '9')
3204 digit = *p - '0';
3205 else if (*p >= 'a' && *p <= 'f')
3206 digit = *p - 'a' + 10;
3207 else
3208 error (_("Could not parse signal set: %s"), line);
3209
3210 signum -= 4;
3211
3212 if (digit & 1)
3213 sigaddset (sigs, signum + 1);
3214 if (digit & 2)
3215 sigaddset (sigs, signum + 2);
3216 if (digit & 4)
3217 sigaddset (sigs, signum + 3);
3218 if (digit & 8)
3219 sigaddset (sigs, signum + 4);
3220
3221 p++;
3222 }
3223 }
3224
3225 /* Find process PID's pending signals from /proc/pid/status and set
3226 SIGS to match. */
3227
3228 void
3229 linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored)
3230 {
3231 FILE *procfile;
3232 char buffer[MAXPATHLEN], fname[MAXPATHLEN];
3233 int signum;
3234
3235 sigemptyset (pending);
3236 sigemptyset (blocked);
3237 sigemptyset (ignored);
3238 sprintf (fname, "/proc/%d/status", pid);
3239 procfile = fopen (fname, "r");
3240 if (procfile == NULL)
3241 error (_("Could not open %s"), fname);
3242
3243 while (fgets (buffer, MAXPATHLEN, procfile) != NULL)
3244 {
3245 /* Normal queued signals are on the SigPnd line in the status
3246 file. However, 2.6 kernels also have a "shared" pending
3247 queue for delivering signals to a thread group, so check for
3248 a ShdPnd line also.
3249
3250 Unfortunately some Red Hat kernels include the shared pending
3251 queue but not the ShdPnd status field. */
3252
3253 if (strncmp (buffer, "SigPnd:\t", 8) == 0)
3254 add_line_to_sigset (buffer + 8, pending);
3255 else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
3256 add_line_to_sigset (buffer + 8, pending);
3257 else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
3258 add_line_to_sigset (buffer + 8, blocked);
3259 else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
3260 add_line_to_sigset (buffer + 8, ignored);
3261 }
3262
3263 fclose (procfile);
3264 }
3265
3266 static LONGEST
3267 linux_xfer_partial (struct target_ops *ops, enum target_object object,
3268 const char *annex, gdb_byte *readbuf,
3269 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
3270 {
3271 LONGEST xfer;
3272
3273 if (object == TARGET_OBJECT_AUXV)
3274 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
3275 offset, len);
3276
3277 xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
3278 offset, len);
3279 if (xfer != 0)
3280 return xfer;
3281
3282 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
3283 offset, len);
3284 }
3285
3286 #ifndef FETCH_INFERIOR_REGISTERS
3287
3288 /* Return the address in the core dump or inferior of register
3289 REGNO. */
3290
3291 static CORE_ADDR
3292 linux_register_u_offset (int regno)
3293 {
3294 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
3295 away. This requires disentangling the various definitions of it
3296 (particularly alpha-nat.c's). */
3297 return register_addr (regno, 0);
3298 }
3299
3300 #endif
3301
3302 /* Create a prototype generic Linux target. The client can override
3303 it with local methods. */
3304
3305 struct target_ops *
3306 linux_target (void)
3307 {
3308 struct target_ops *t;
3309
3310 #ifdef FETCH_INFERIOR_REGISTERS
3311 t = inf_ptrace_target ();
3312 #else
3313 t = inf_ptrace_trad_target (linux_register_u_offset);
3314 #endif
3315 t->to_wait = child_wait;
3316 t->to_kill = kill_inferior;
3317 t->to_insert_fork_catchpoint = child_insert_fork_catchpoint;
3318 t->to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
3319 t->to_insert_exec_catchpoint = child_insert_exec_catchpoint;
3320 t->to_pid_to_exec_file = child_pid_to_exec_file;
3321 t->to_post_startup_inferior = linux_child_post_startup_inferior;
3322 t->to_post_attach = child_post_attach;
3323 t->to_follow_fork = child_follow_fork;
3324 t->to_find_memory_regions = linux_nat_find_memory_regions;
3325 t->to_make_corefile_notes = linux_nat_make_corefile_notes;
3326
3327 super_xfer_partial = t->to_xfer_partial;
3328 t->to_xfer_partial = linux_xfer_partial;
3329
3330 super_mourn_inferior = t->to_mourn_inferior;
3331 t->to_mourn_inferior = child_mourn_inferior;
3332
3333 linux_ops = t;
3334 return t;
3335 }
3336
3337 void
3338 _initialize_linux_nat (void)
3339 {
3340 struct sigaction action;
3341 extern void thread_db_init (struct target_ops *);
3342
3343 add_info ("proc", linux_nat_info_proc_cmd, _("\
3344 Show /proc process information about any running process.\n\
3345 Specify any process id, or use the program being debugged by default.\n\
3346 Specify any of the following keywords for detailed info:\n\
3347 mappings -- list of mapped memory regions.\n\
3348 stat -- list a bunch of random process info.\n\
3349 status -- list a different bunch of random process info.\n\
3350 all -- list all available /proc info."));
3351
3352 init_linux_nat_ops ();
3353 add_target (&linux_nat_ops);
3354 thread_db_init (&linux_nat_ops);
3355
3356 /* Save the original signal mask. */
3357 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
3358
3359 action.sa_handler = sigchld_handler;
3360 sigemptyset (&action.sa_mask);
3361 action.sa_flags = SA_RESTART;
3362 sigaction (SIGCHLD, &action, NULL);
3363
3364 /* Make sure we don't block SIGCHLD during a sigsuspend. */
3365 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
3366 sigdelset (&suspend_mask, SIGCHLD);
3367
3368 sigemptyset (&blocked_mask);
3369
3370 add_setshow_zinteger_cmd ("lin-lwp", no_class, &debug_linux_nat, _("\
3371 Set debugging of GNU/Linux lwp module."), _("\
3372 Show debugging of GNU/Linux lwp module."), _("\
3373 Enables printf debugging output."),
3374 NULL,
3375 show_debug_linux_nat,
3376 &setdebuglist, &showdebuglist);
3377 }
3378 \f
3379
3380 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
3381 the GNU/Linux Threads library and therefore doesn't really belong
3382 here. */
3383
3384 /* Read variable NAME in the target and return its value if found.
3385 Otherwise return zero. It is assumed that the type of the variable
3386 is `int'. */
3387
3388 static int
3389 get_signo (const char *name)
3390 {
3391 struct minimal_symbol *ms;
3392 int signo;
3393
3394 ms = lookup_minimal_symbol (name, NULL, NULL);
3395 if (ms == NULL)
3396 return 0;
3397
3398 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo,
3399 sizeof (signo)) != 0)
3400 return 0;
3401
3402 return signo;
3403 }
3404
3405 /* Return the set of signals used by the threads library in *SET. */
3406
3407 void
3408 lin_thread_get_thread_signals (sigset_t *set)
3409 {
3410 struct sigaction action;
3411 int restart, cancel;
3412
3413 sigemptyset (set);
3414
3415 restart = get_signo ("__pthread_sig_restart");
3416 if (restart == 0)
3417 return;
3418
3419 cancel = get_signo ("__pthread_sig_cancel");
3420 if (cancel == 0)
3421 return;
3422
3423 sigaddset (set, restart);
3424 sigaddset (set, cancel);
3425
3426 /* The GNU/Linux Threads library makes terminating threads send a
3427 special "cancel" signal instead of SIGCHLD. Make sure we catch
3428 those (to prevent them from terminating GDB itself, which is
3429 likely to be their default action) and treat them the same way as
3430 SIGCHLD. */
3431
3432 action.sa_handler = sigchld_handler;
3433 sigemptyset (&action.sa_mask);
3434 action.sa_flags = SA_RESTART;
3435 sigaction (cancel, &action, NULL);
3436
3437 /* We block the "cancel" signal throughout this code ... */
3438 sigaddset (&blocked_mask, cancel);
3439 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
3440
3441 /* ... except during a sigsuspend. */
3442 sigdelset (&suspend_mask, cancel);
3443 }
3444
This page took 0.100817 seconds and 4 git commands to generate.