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