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