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