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