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