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