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