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