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