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