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