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