Commit | Line | Data |
---|---|---|
3993f6b1 | 1 | /* GNU/Linux native-dependent code common to multiple platforms. |
dba24537 AC |
2 | |
3 | Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. | |
3993f6b1 DJ |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
24 | #include "target.h" | |
d6b0e80f | 25 | #include "gdb_string.h" |
3993f6b1 | 26 | #include "gdb_wait.h" |
d6b0e80f AC |
27 | #include "gdb_assert.h" |
28 | #ifdef HAVE_TKILL_SYSCALL | |
29 | #include <unistd.h> | |
30 | #include <sys/syscall.h> | |
31 | #endif | |
3993f6b1 | 32 | #include <sys/ptrace.h> |
0274a8ce | 33 | #include "linux-nat.h" |
d6b0e80f AC |
34 | #include "gdbthread.h" |
35 | #include "gdbcmd.h" | |
36 | #include "regcache.h" | |
dba24537 AC |
37 | #include <sys/param.h> /* for MAXPATHLEN */ |
38 | #include <sys/procfs.h> /* for elf_gregset etc. */ | |
39 | #include "elf-bfd.h" /* for elfcore_write_* */ | |
40 | #include "gregset.h" /* for gregset */ | |
41 | #include "gdbcore.h" /* for get_exec_file */ | |
42 | #include <ctype.h> /* for isdigit */ | |
43 | #include "gdbthread.h" /* for struct thread_info etc. */ | |
44 | #include "gdb_stat.h" /* for struct stat */ | |
45 | #include <fcntl.h> /* for O_RDONLY */ | |
46 | ||
47 | #ifndef O_LARGEFILE | |
48 | #define O_LARGEFILE 0 | |
49 | #endif | |
0274a8ce | 50 | |
3993f6b1 DJ |
51 | /* If the system headers did not provide the constants, hard-code the normal |
52 | values. */ | |
53 | #ifndef PTRACE_EVENT_FORK | |
54 | ||
55 | #define PTRACE_SETOPTIONS 0x4200 | |
56 | #define PTRACE_GETEVENTMSG 0x4201 | |
57 | ||
58 | /* options set using PTRACE_SETOPTIONS */ | |
59 | #define PTRACE_O_TRACESYSGOOD 0x00000001 | |
60 | #define PTRACE_O_TRACEFORK 0x00000002 | |
61 | #define PTRACE_O_TRACEVFORK 0x00000004 | |
62 | #define PTRACE_O_TRACECLONE 0x00000008 | |
63 | #define PTRACE_O_TRACEEXEC 0x00000010 | |
9016a515 DJ |
64 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
65 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
3993f6b1 DJ |
66 | |
67 | /* Wait extended result codes for the above trace options. */ | |
68 | #define PTRACE_EVENT_FORK 1 | |
69 | #define PTRACE_EVENT_VFORK 2 | |
70 | #define PTRACE_EVENT_CLONE 3 | |
71 | #define PTRACE_EVENT_EXEC 4 | |
c874c7fc | 72 | #define PTRACE_EVENT_VFORK_DONE 5 |
9016a515 | 73 | #define PTRACE_EVENT_EXIT 6 |
3993f6b1 DJ |
74 | |
75 | #endif /* PTRACE_EVENT_FORK */ | |
76 | ||
77 | /* We can't always assume that this flag is available, but all systems | |
78 | with the ptrace event handlers also have __WALL, so it's safe to use | |
79 | here. */ | |
80 | #ifndef __WALL | |
81 | #define __WALL 0x40000000 /* Wait for any child. */ | |
82 | #endif | |
83 | ||
d6b0e80f AC |
84 | static int debug_linux_nat; |
85 | ||
9016a515 DJ |
86 | static int linux_parent_pid; |
87 | ||
ae087d01 DJ |
88 | struct simple_pid_list |
89 | { | |
90 | int pid; | |
91 | struct simple_pid_list *next; | |
92 | }; | |
93 | struct simple_pid_list *stopped_pids; | |
94 | ||
3993f6b1 DJ |
95 | /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK |
96 | can not be used, 1 if it can. */ | |
97 | ||
98 | static int linux_supports_tracefork_flag = -1; | |
99 | ||
9016a515 DJ |
100 | /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have |
101 | PTRACE_O_TRACEVFORKDONE. */ | |
102 | ||
103 | static int linux_supports_tracevforkdone_flag = -1; | |
104 | ||
ae087d01 DJ |
105 | \f |
106 | /* Trivial list manipulation functions to keep track of a list of | |
107 | new stopped processes. */ | |
108 | static void | |
109 | add_to_pid_list (struct simple_pid_list **listp, int pid) | |
110 | { | |
111 | struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list)); | |
112 | new_pid->pid = pid; | |
113 | new_pid->next = *listp; | |
114 | *listp = new_pid; | |
115 | } | |
116 | ||
117 | static int | |
118 | pull_pid_from_list (struct simple_pid_list **listp, int pid) | |
119 | { | |
120 | struct simple_pid_list **p; | |
121 | ||
122 | for (p = listp; *p != NULL; p = &(*p)->next) | |
123 | if ((*p)->pid == pid) | |
124 | { | |
125 | struct simple_pid_list *next = (*p)->next; | |
126 | xfree (*p); | |
127 | *p = next; | |
128 | return 1; | |
129 | } | |
130 | return 0; | |
131 | } | |
132 | ||
133 | void | |
134 | linux_record_stopped_pid (int pid) | |
135 | { | |
136 | add_to_pid_list (&stopped_pids, pid); | |
137 | } | |
138 | ||
3993f6b1 DJ |
139 | \f |
140 | /* A helper function for linux_test_for_tracefork, called after fork (). */ | |
141 | ||
142 | static void | |
143 | linux_tracefork_child (void) | |
144 | { | |
145 | int ret; | |
146 | ||
147 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
148 | kill (getpid (), SIGSTOP); | |
149 | fork (); | |
150 | exit (0); | |
151 | } | |
152 | ||
b957e937 DJ |
153 | /* Wrapper function for waitpid which handles EINTR. */ |
154 | ||
155 | static int | |
156 | my_waitpid (int pid, int *status, int flags) | |
157 | { | |
158 | int ret; | |
159 | do | |
160 | { | |
161 | ret = waitpid (pid, status, flags); | |
162 | } | |
163 | while (ret == -1 && errno == EINTR); | |
164 | ||
165 | return ret; | |
166 | } | |
167 | ||
168 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. | |
169 | ||
170 | First, we try to enable fork tracing on ORIGINAL_PID. If this fails, | |
171 | we know that the feature is not available. This may change the tracing | |
172 | options for ORIGINAL_PID, but we'll be setting them shortly anyway. | |
173 | ||
174 | However, if it succeeds, we don't know for sure that the feature is | |
175 | available; old versions of PTRACE_SETOPTIONS ignored unknown options. We | |
3993f6b1 | 176 | create a child process, attach to it, use PTRACE_SETOPTIONS to enable |
b957e937 DJ |
177 | fork tracing, and let it fork. If the process exits, we assume that we |
178 | can't use TRACEFORK; if we get the fork notification, and we can extract | |
179 | the new child's PID, then we assume that we can. */ | |
3993f6b1 DJ |
180 | |
181 | static void | |
b957e937 | 182 | linux_test_for_tracefork (int original_pid) |
3993f6b1 DJ |
183 | { |
184 | int child_pid, ret, status; | |
185 | long second_pid; | |
186 | ||
b957e937 DJ |
187 | linux_supports_tracefork_flag = 0; |
188 | linux_supports_tracevforkdone_flag = 0; | |
189 | ||
190 | ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK); | |
191 | if (ret != 0) | |
192 | return; | |
193 | ||
3993f6b1 DJ |
194 | child_pid = fork (); |
195 | if (child_pid == -1) | |
196 | perror_with_name ("linux_test_for_tracefork: fork"); | |
197 | ||
198 | if (child_pid == 0) | |
199 | linux_tracefork_child (); | |
200 | ||
b957e937 | 201 | ret = my_waitpid (child_pid, &status, 0); |
3993f6b1 DJ |
202 | if (ret == -1) |
203 | perror_with_name ("linux_test_for_tracefork: waitpid"); | |
204 | else if (ret != child_pid) | |
205 | error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret); | |
206 | if (! WIFSTOPPED (status)) | |
207 | error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status); | |
208 | ||
3993f6b1 DJ |
209 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); |
210 | if (ret != 0) | |
211 | { | |
b957e937 DJ |
212 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); |
213 | if (ret != 0) | |
214 | { | |
215 | warning ("linux_test_for_tracefork: failed to kill child"); | |
216 | return; | |
217 | } | |
218 | ||
219 | ret = my_waitpid (child_pid, &status, 0); | |
220 | if (ret != child_pid) | |
221 | warning ("linux_test_for_tracefork: failed to wait for killed child"); | |
222 | else if (!WIFSIGNALED (status)) | |
223 | warning ("linux_test_for_tracefork: unexpected wait status 0x%x from " | |
224 | "killed child", status); | |
225 | ||
3993f6b1 DJ |
226 | return; |
227 | } | |
228 | ||
9016a515 DJ |
229 | /* Check whether PTRACE_O_TRACEVFORKDONE is available. */ |
230 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, | |
231 | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE); | |
232 | linux_supports_tracevforkdone_flag = (ret == 0); | |
233 | ||
b957e937 DJ |
234 | ret = ptrace (PTRACE_CONT, child_pid, 0, 0); |
235 | if (ret != 0) | |
236 | warning ("linux_test_for_tracefork: failed to resume child"); | |
237 | ||
238 | ret = my_waitpid (child_pid, &status, 0); | |
239 | ||
3993f6b1 DJ |
240 | if (ret == child_pid && WIFSTOPPED (status) |
241 | && status >> 16 == PTRACE_EVENT_FORK) | |
242 | { | |
243 | second_pid = 0; | |
244 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); | |
245 | if (ret == 0 && second_pid != 0) | |
246 | { | |
247 | int second_status; | |
248 | ||
249 | linux_supports_tracefork_flag = 1; | |
b957e937 DJ |
250 | my_waitpid (second_pid, &second_status, 0); |
251 | ret = ptrace (PTRACE_KILL, second_pid, 0, 0); | |
252 | if (ret != 0) | |
253 | warning ("linux_test_for_tracefork: failed to kill second child"); | |
3993f6b1 DJ |
254 | } |
255 | } | |
b957e937 DJ |
256 | else |
257 | warning ("linux_test_for_tracefork: unexpected result from waitpid " | |
258 | "(%d, status 0x%x)", ret, status); | |
3993f6b1 | 259 | |
b957e937 DJ |
260 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); |
261 | if (ret != 0) | |
262 | warning ("linux_test_for_tracefork: failed to kill child"); | |
263 | my_waitpid (child_pid, &status, 0); | |
3993f6b1 DJ |
264 | } |
265 | ||
266 | /* Return non-zero iff we have tracefork functionality available. | |
267 | This function also sets linux_supports_tracefork_flag. */ | |
268 | ||
269 | static int | |
b957e937 | 270 | linux_supports_tracefork (int pid) |
3993f6b1 DJ |
271 | { |
272 | if (linux_supports_tracefork_flag == -1) | |
b957e937 | 273 | linux_test_for_tracefork (pid); |
3993f6b1 DJ |
274 | return linux_supports_tracefork_flag; |
275 | } | |
276 | ||
9016a515 | 277 | static int |
b957e937 | 278 | linux_supports_tracevforkdone (int pid) |
9016a515 DJ |
279 | { |
280 | if (linux_supports_tracefork_flag == -1) | |
b957e937 | 281 | linux_test_for_tracefork (pid); |
9016a515 DJ |
282 | return linux_supports_tracevforkdone_flag; |
283 | } | |
284 | ||
3993f6b1 | 285 | \f |
4de4c07c DJ |
286 | void |
287 | linux_enable_event_reporting (ptid_t ptid) | |
288 | { | |
289 | int pid = ptid_get_pid (ptid); | |
290 | int options; | |
291 | ||
b957e937 | 292 | if (! linux_supports_tracefork (pid)) |
4de4c07c DJ |
293 | return; |
294 | ||
a2f23071 DJ |
295 | options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC |
296 | | PTRACE_O_TRACECLONE; | |
b957e937 | 297 | if (linux_supports_tracevforkdone (pid)) |
9016a515 DJ |
298 | options |= PTRACE_O_TRACEVFORKDONE; |
299 | ||
300 | /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support | |
301 | read-only process state. */ | |
4de4c07c DJ |
302 | |
303 | ptrace (PTRACE_SETOPTIONS, pid, 0, options); | |
304 | } | |
305 | ||
306 | void | |
307 | child_post_attach (int pid) | |
308 | { | |
309 | linux_enable_event_reporting (pid_to_ptid (pid)); | |
310 | } | |
311 | ||
312 | void | |
313 | linux_child_post_startup_inferior (ptid_t ptid) | |
314 | { | |
315 | linux_enable_event_reporting (ptid); | |
316 | } | |
317 | ||
318 | #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR | |
319 | void | |
320 | child_post_startup_inferior (ptid_t ptid) | |
321 | { | |
322 | linux_child_post_startup_inferior (ptid); | |
323 | } | |
324 | #endif | |
325 | ||
3993f6b1 | 326 | int |
4de4c07c | 327 | child_follow_fork (int follow_child) |
3993f6b1 | 328 | { |
4de4c07c DJ |
329 | ptid_t last_ptid; |
330 | struct target_waitstatus last_status; | |
9016a515 | 331 | int has_vforked; |
4de4c07c DJ |
332 | int parent_pid, child_pid; |
333 | ||
334 | get_last_target_status (&last_ptid, &last_status); | |
9016a515 | 335 | has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED); |
4de4c07c DJ |
336 | parent_pid = ptid_get_pid (last_ptid); |
337 | child_pid = last_status.value.related_pid; | |
338 | ||
339 | if (! follow_child) | |
340 | { | |
341 | /* We're already attached to the parent, by default. */ | |
342 | ||
343 | /* Before detaching from the child, remove all breakpoints from | |
344 | it. (This won't actually modify the breakpoint list, but will | |
345 | physically remove the breakpoints from the child.) */ | |
9016a515 DJ |
346 | /* If we vforked this will remove the breakpoints from the parent |
347 | also, but they'll be reinserted below. */ | |
4de4c07c DJ |
348 | detach_breakpoints (child_pid); |
349 | ||
350 | fprintf_filtered (gdb_stdout, | |
351 | "Detaching after fork from child process %d.\n", | |
352 | child_pid); | |
353 | ||
354 | ptrace (PTRACE_DETACH, child_pid, 0, 0); | |
9016a515 DJ |
355 | |
356 | if (has_vforked) | |
357 | { | |
b957e937 DJ |
358 | gdb_assert (linux_supports_tracefork_flag >= 0); |
359 | if (linux_supports_tracevforkdone (0)) | |
9016a515 DJ |
360 | { |
361 | int status; | |
362 | ||
363 | ptrace (PTRACE_CONT, parent_pid, 0, 0); | |
364 | waitpid (parent_pid, &status, __WALL); | |
c874c7fc | 365 | if ((status >> 16) != PTRACE_EVENT_VFORK_DONE) |
9016a515 DJ |
366 | warning ("Unexpected waitpid result %06x when waiting for " |
367 | "vfork-done", status); | |
368 | } | |
369 | else | |
370 | { | |
371 | /* We can't insert breakpoints until the child has | |
372 | finished with the shared memory region. We need to | |
373 | wait until that happens. Ideal would be to just | |
374 | call: | |
375 | - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0); | |
376 | - waitpid (parent_pid, &status, __WALL); | |
377 | However, most architectures can't handle a syscall | |
378 | being traced on the way out if it wasn't traced on | |
379 | the way in. | |
380 | ||
381 | We might also think to loop, continuing the child | |
382 | until it exits or gets a SIGTRAP. One problem is | |
383 | that the child might call ptrace with PTRACE_TRACEME. | |
384 | ||
385 | There's no simple and reliable way to figure out when | |
386 | the vforked child will be done with its copy of the | |
387 | shared memory. We could step it out of the syscall, | |
388 | two instructions, let it go, and then single-step the | |
389 | parent once. When we have hardware single-step, this | |
390 | would work; with software single-step it could still | |
391 | be made to work but we'd have to be able to insert | |
392 | single-step breakpoints in the child, and we'd have | |
393 | to insert -just- the single-step breakpoint in the | |
394 | parent. Very awkward. | |
395 | ||
396 | In the end, the best we can do is to make sure it | |
397 | runs for a little while. Hopefully it will be out of | |
398 | range of any breakpoints we reinsert. Usually this | |
399 | is only the single-step breakpoint at vfork's return | |
400 | point. */ | |
401 | ||
402 | usleep (10000); | |
403 | } | |
404 | ||
405 | /* Since we vforked, breakpoints were removed in the parent | |
406 | too. Put them back. */ | |
407 | reattach_breakpoints (parent_pid); | |
408 | } | |
4de4c07c | 409 | } |
3993f6b1 | 410 | else |
4de4c07c DJ |
411 | { |
412 | char child_pid_spelling[40]; | |
413 | ||
414 | /* Needed to keep the breakpoint lists in sync. */ | |
9016a515 DJ |
415 | if (! has_vforked) |
416 | detach_breakpoints (child_pid); | |
4de4c07c DJ |
417 | |
418 | /* Before detaching from the parent, remove all breakpoints from it. */ | |
419 | remove_breakpoints (); | |
420 | ||
421 | fprintf_filtered (gdb_stdout, | |
422 | "Attaching after fork to child process %d.\n", | |
423 | child_pid); | |
424 | ||
9016a515 DJ |
425 | /* If we're vforking, we may want to hold on to the parent until |
426 | the child exits or execs. At exec time we can remove the old | |
427 | breakpoints from the parent and detach it; at exit time we | |
428 | could do the same (or even, sneakily, resume debugging it - the | |
429 | child's exec has failed, or something similar). | |
430 | ||
431 | This doesn't clean up "properly", because we can't call | |
432 | target_detach, but that's OK; if the current target is "child", | |
433 | then it doesn't need any further cleanups, and lin_lwp will | |
434 | generally not encounter vfork (vfork is defined to fork | |
435 | in libpthread.so). | |
436 | ||
437 | The holding part is very easy if we have VFORKDONE events; | |
438 | but keeping track of both processes is beyond GDB at the | |
439 | moment. So we don't expose the parent to the rest of GDB. | |
440 | Instead we quietly hold onto it until such time as we can | |
441 | safely resume it. */ | |
442 | ||
443 | if (has_vforked) | |
444 | linux_parent_pid = parent_pid; | |
445 | else | |
446 | target_detach (NULL, 0); | |
4de4c07c DJ |
447 | |
448 | inferior_ptid = pid_to_ptid (child_pid); | |
1df84f13 | 449 | push_target (&deprecated_child_ops); |
4de4c07c DJ |
450 | |
451 | /* Reset breakpoints in the child as appropriate. */ | |
452 | follow_inferior_reset_breakpoints (); | |
453 | } | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | ptid_t | |
459 | linux_handle_extended_wait (int pid, int status, | |
460 | struct target_waitstatus *ourstatus) | |
461 | { | |
462 | int event = status >> 16; | |
463 | ||
a2f23071 DJ |
464 | if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK |
465 | || event == PTRACE_EVENT_CLONE) | |
4de4c07c DJ |
466 | { |
467 | unsigned long new_pid; | |
468 | int ret; | |
469 | ||
470 | ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid); | |
471 | ||
472 | /* If we haven't already seen the new PID stop, wait for it now. */ | |
473 | if (! pull_pid_from_list (&stopped_pids, new_pid)) | |
474 | { | |
475 | /* The new child has a pending SIGSTOP. We can't affect it until it | |
a2f23071 | 476 | hits the SIGSTOP, but we're already attached. */ |
4de4c07c | 477 | do { |
a2f23071 DJ |
478 | ret = waitpid (new_pid, &status, |
479 | (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0); | |
4de4c07c DJ |
480 | } while (ret == -1 && errno == EINTR); |
481 | if (ret == -1) | |
482 | perror_with_name ("waiting for new child"); | |
483 | else if (ret != new_pid) | |
484 | internal_error (__FILE__, __LINE__, | |
485 | "wait returned unexpected PID %d", ret); | |
486 | else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP) | |
487 | internal_error (__FILE__, __LINE__, | |
488 | "wait returned unexpected status 0x%x", status); | |
489 | } | |
490 | ||
a2f23071 DJ |
491 | if (event == PTRACE_EVENT_FORK) |
492 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
493 | else if (event == PTRACE_EVENT_VFORK) | |
494 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
495 | else | |
496 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
497 | ||
4de4c07c DJ |
498 | ourstatus->value.related_pid = new_pid; |
499 | return inferior_ptid; | |
500 | } | |
501 | ||
9016a515 DJ |
502 | if (event == PTRACE_EVENT_EXEC) |
503 | { | |
504 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
505 | ourstatus->value.execd_pathname | |
506 | = xstrdup (child_pid_to_exec_file (pid)); | |
507 | ||
508 | if (linux_parent_pid) | |
509 | { | |
510 | detach_breakpoints (linux_parent_pid); | |
511 | ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0); | |
512 | ||
513 | linux_parent_pid = 0; | |
514 | } | |
515 | ||
516 | return inferior_ptid; | |
517 | } | |
518 | ||
4de4c07c DJ |
519 | internal_error (__FILE__, __LINE__, |
520 | "unknown ptrace event %d", event); | |
521 | } | |
522 | ||
523 | \f | |
524 | int | |
525 | child_insert_fork_catchpoint (int pid) | |
526 | { | |
b957e937 | 527 | if (! linux_supports_tracefork (pid)) |
3993f6b1 | 528 | error ("Your system does not support fork catchpoints."); |
4de4c07c DJ |
529 | |
530 | return 0; | |
3993f6b1 DJ |
531 | } |
532 | ||
533 | int | |
534 | child_insert_vfork_catchpoint (int pid) | |
535 | { | |
b957e937 | 536 | if (!linux_supports_tracefork (pid)) |
3993f6b1 | 537 | error ("Your system does not support vfork catchpoints."); |
9016a515 DJ |
538 | |
539 | return 0; | |
3993f6b1 DJ |
540 | } |
541 | ||
542 | int | |
543 | child_insert_exec_catchpoint (int pid) | |
544 | { | |
b957e937 | 545 | if (!linux_supports_tracefork (pid)) |
3993f6b1 | 546 | error ("Your system does not support exec catchpoints."); |
9016a515 DJ |
547 | |
548 | return 0; | |
3993f6b1 DJ |
549 | } |
550 | ||
4de4c07c DJ |
551 | void |
552 | kill_inferior (void) | |
553 | { | |
554 | int status; | |
555 | int pid = PIDGET (inferior_ptid); | |
556 | struct target_waitstatus last; | |
557 | ptid_t last_ptid; | |
558 | int ret; | |
559 | ||
560 | if (pid == 0) | |
561 | return; | |
562 | ||
563 | /* If we're stopped while forking and we haven't followed yet, kill the | |
564 | other task. We need to do this first because the parent will be | |
565 | sleeping if this is a vfork. */ | |
566 | ||
567 | get_last_target_status (&last_ptid, &last); | |
3993f6b1 | 568 | |
4de4c07c DJ |
569 | if (last.kind == TARGET_WAITKIND_FORKED |
570 | || last.kind == TARGET_WAITKIND_VFORKED) | |
571 | { | |
de9a9e51 | 572 | ptrace (PT_KILL, last.value.related_pid, 0, 0); |
ee21b650 | 573 | wait (&status); |
4de4c07c DJ |
574 | } |
575 | ||
576 | /* Kill the current process. */ | |
de9a9e51 | 577 | ptrace (PT_KILL, pid, 0, 0); |
ee21b650 | 578 | ret = wait (&status); |
4de4c07c DJ |
579 | |
580 | /* We might get a SIGCHLD instead of an exit status. This is | |
581 | aggravated by the first kill above - a child has just died. */ | |
582 | ||
583 | while (ret == pid && WIFSTOPPED (status)) | |
584 | { | |
de9a9e51 | 585 | ptrace (PT_KILL, pid, 0, 0); |
ee21b650 | 586 | ret = wait (&status); |
4de4c07c DJ |
587 | } |
588 | ||
589 | target_mourn_inferior (); | |
590 | } | |
d6b0e80f AC |
591 | |
592 | /* On GNU/Linux there are no real LWP's. The closest thing to LWP's | |
593 | are processes sharing the same VM space. A multi-threaded process | |
594 | is basically a group of such processes. However, such a grouping | |
595 | is almost entirely a user-space issue; the kernel doesn't enforce | |
596 | such a grouping at all (this might change in the future). In | |
597 | general, we'll rely on the threads library (i.e. the GNU/Linux | |
598 | Threads library) to provide such a grouping. | |
599 | ||
600 | It is perfectly well possible to write a multi-threaded application | |
601 | without the assistance of a threads library, by using the clone | |
602 | system call directly. This module should be able to give some | |
603 | rudimentary support for debugging such applications if developers | |
604 | specify the CLONE_PTRACE flag in the clone system call, and are | |
605 | using the Linux kernel 2.4 or above. | |
606 | ||
607 | Note that there are some peculiarities in GNU/Linux that affect | |
608 | this code: | |
609 | ||
610 | - In general one should specify the __WCLONE flag to waitpid in | |
611 | order to make it report events for any of the cloned processes | |
612 | (and leave it out for the initial process). However, if a cloned | |
613 | process has exited the exit status is only reported if the | |
614 | __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but | |
615 | we cannot use it since GDB must work on older systems too. | |
616 | ||
617 | - When a traced, cloned process exits and is waited for by the | |
618 | debugger, the kernel reassigns it to the original parent and | |
619 | keeps it around as a "zombie". Somehow, the GNU/Linux Threads | |
620 | library doesn't notice this, which leads to the "zombie problem": | |
621 | When debugged a multi-threaded process that spawns a lot of | |
622 | threads will run out of processes, even if the threads exit, | |
623 | because the "zombies" stay around. */ | |
624 | ||
625 | /* List of known LWPs. */ | |
626 | static struct lwp_info *lwp_list; | |
627 | ||
628 | /* Number of LWPs in the list. */ | |
629 | static int num_lwps; | |
630 | ||
631 | /* Non-zero if we're running in "threaded" mode. */ | |
632 | static int threaded; | |
633 | \f | |
634 | ||
635 | #define GET_LWP(ptid) ptid_get_lwp (ptid) | |
636 | #define GET_PID(ptid) ptid_get_pid (ptid) | |
637 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) | |
638 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) | |
639 | ||
640 | /* If the last reported event was a SIGTRAP, this variable is set to | |
641 | the process id of the LWP/thread that got it. */ | |
642 | ptid_t trap_ptid; | |
643 | \f | |
644 | ||
645 | /* This module's target-specific operations. */ | |
646 | static struct target_ops linux_nat_ops; | |
647 | ||
d6b0e80f AC |
648 | /* Since we cannot wait (in linux_nat_wait) for the initial process and |
649 | any cloned processes with a single call to waitpid, we have to use | |
650 | the WNOHANG flag and call waitpid in a loop. To optimize | |
651 | things a bit we use `sigsuspend' to wake us up when a process has | |
652 | something to report (it will send us a SIGCHLD if it has). To make | |
653 | this work we have to juggle with the signal mask. We save the | |
654 | original signal mask such that we can restore it before creating a | |
655 | new process in order to avoid blocking certain signals in the | |
656 | inferior. We then block SIGCHLD during the waitpid/sigsuspend | |
657 | loop. */ | |
658 | ||
659 | /* Original signal mask. */ | |
660 | static sigset_t normal_mask; | |
661 | ||
662 | /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in | |
663 | _initialize_linux_nat. */ | |
664 | static sigset_t suspend_mask; | |
665 | ||
666 | /* Signals to block to make that sigsuspend work. */ | |
667 | static sigset_t blocked_mask; | |
668 | \f | |
669 | ||
670 | /* Prototypes for local functions. */ | |
671 | static int stop_wait_callback (struct lwp_info *lp, void *data); | |
672 | static int linux_nat_thread_alive (ptid_t ptid); | |
673 | \f | |
674 | /* Convert wait status STATUS to a string. Used for printing debug | |
675 | messages only. */ | |
676 | ||
677 | static char * | |
678 | status_to_str (int status) | |
679 | { | |
680 | static char buf[64]; | |
681 | ||
682 | if (WIFSTOPPED (status)) | |
683 | snprintf (buf, sizeof (buf), "%s (stopped)", | |
684 | strsignal (WSTOPSIG (status))); | |
685 | else if (WIFSIGNALED (status)) | |
686 | snprintf (buf, sizeof (buf), "%s (terminated)", | |
687 | strsignal (WSTOPSIG (status))); | |
688 | else | |
689 | snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status)); | |
690 | ||
691 | return buf; | |
692 | } | |
693 | ||
694 | /* Initialize the list of LWPs. Note that this module, contrary to | |
695 | what GDB's generic threads layer does for its thread list, | |
696 | re-initializes the LWP lists whenever we mourn or detach (which | |
697 | doesn't involve mourning) the inferior. */ | |
698 | ||
699 | static void | |
700 | init_lwp_list (void) | |
701 | { | |
702 | struct lwp_info *lp, *lpnext; | |
703 | ||
704 | for (lp = lwp_list; lp; lp = lpnext) | |
705 | { | |
706 | lpnext = lp->next; | |
707 | xfree (lp); | |
708 | } | |
709 | ||
710 | lwp_list = NULL; | |
711 | num_lwps = 0; | |
712 | threaded = 0; | |
713 | } | |
714 | ||
715 | /* Add the LWP specified by PID to the list. If this causes the | |
716 | number of LWPs to become larger than one, go into "threaded" mode. | |
717 | Return a pointer to the structure describing the new LWP. */ | |
718 | ||
719 | static struct lwp_info * | |
720 | add_lwp (ptid_t ptid) | |
721 | { | |
722 | struct lwp_info *lp; | |
723 | ||
724 | gdb_assert (is_lwp (ptid)); | |
725 | ||
726 | lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); | |
727 | ||
728 | memset (lp, 0, sizeof (struct lwp_info)); | |
729 | ||
730 | lp->waitstatus.kind = TARGET_WAITKIND_IGNORE; | |
731 | ||
732 | lp->ptid = ptid; | |
733 | ||
734 | lp->next = lwp_list; | |
735 | lwp_list = lp; | |
736 | if (++num_lwps > 1) | |
737 | threaded = 1; | |
738 | ||
739 | return lp; | |
740 | } | |
741 | ||
742 | /* Remove the LWP specified by PID from the list. */ | |
743 | ||
744 | static void | |
745 | delete_lwp (ptid_t ptid) | |
746 | { | |
747 | struct lwp_info *lp, *lpprev; | |
748 | ||
749 | lpprev = NULL; | |
750 | ||
751 | for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) | |
752 | if (ptid_equal (lp->ptid, ptid)) | |
753 | break; | |
754 | ||
755 | if (!lp) | |
756 | return; | |
757 | ||
758 | /* We don't go back to "non-threaded" mode if the number of threads | |
759 | becomes less than two. */ | |
760 | num_lwps--; | |
761 | ||
762 | if (lpprev) | |
763 | lpprev->next = lp->next; | |
764 | else | |
765 | lwp_list = lp->next; | |
766 | ||
767 | xfree (lp); | |
768 | } | |
769 | ||
770 | /* Return a pointer to the structure describing the LWP corresponding | |
771 | to PID. If no corresponding LWP could be found, return NULL. */ | |
772 | ||
773 | static struct lwp_info * | |
774 | find_lwp_pid (ptid_t ptid) | |
775 | { | |
776 | struct lwp_info *lp; | |
777 | int lwp; | |
778 | ||
779 | if (is_lwp (ptid)) | |
780 | lwp = GET_LWP (ptid); | |
781 | else | |
782 | lwp = GET_PID (ptid); | |
783 | ||
784 | for (lp = lwp_list; lp; lp = lp->next) | |
785 | if (lwp == GET_LWP (lp->ptid)) | |
786 | return lp; | |
787 | ||
788 | return NULL; | |
789 | } | |
790 | ||
791 | /* Call CALLBACK with its second argument set to DATA for every LWP in | |
792 | the list. If CALLBACK returns 1 for a particular LWP, return a | |
793 | pointer to the structure describing that LWP immediately. | |
794 | Otherwise return NULL. */ | |
795 | ||
796 | struct lwp_info * | |
797 | iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) | |
798 | { | |
799 | struct lwp_info *lp, *lpnext; | |
800 | ||
801 | for (lp = lwp_list; lp; lp = lpnext) | |
802 | { | |
803 | lpnext = lp->next; | |
804 | if ((*callback) (lp, data)) | |
805 | return lp; | |
806 | } | |
807 | ||
808 | return NULL; | |
809 | } | |
810 | ||
811 | /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print | |
812 | a message telling the user that a new LWP has been added to the | |
813 | process. */ | |
814 | ||
815 | void | |
816 | lin_lwp_attach_lwp (ptid_t ptid, int verbose) | |
817 | { | |
818 | struct lwp_info *lp, *found_lp; | |
819 | ||
820 | gdb_assert (is_lwp (ptid)); | |
821 | ||
822 | /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events | |
823 | to interrupt either the ptrace() or waitpid() calls below. */ | |
824 | if (!sigismember (&blocked_mask, SIGCHLD)) | |
825 | { | |
826 | sigaddset (&blocked_mask, SIGCHLD); | |
827 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
828 | } | |
829 | ||
830 | if (verbose) | |
831 | printf_filtered ("[New %s]\n", target_pid_to_str (ptid)); | |
832 | ||
833 | found_lp = lp = find_lwp_pid (ptid); | |
834 | if (lp == NULL) | |
835 | lp = add_lwp (ptid); | |
836 | ||
837 | /* We assume that we're already attached to any LWP that has an id | |
838 | equal to the overall process id, and to any LWP that is already | |
839 | in our list of LWPs. If we're not seeing exit events from threads | |
840 | and we've had PID wraparound since we last tried to stop all threads, | |
841 | this assumption might be wrong; fortunately, this is very unlikely | |
842 | to happen. */ | |
843 | if (GET_LWP (ptid) != GET_PID (ptid) && found_lp == NULL) | |
844 | { | |
845 | pid_t pid; | |
846 | int status; | |
847 | ||
848 | if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) | |
849 | error ("Can't attach %s: %s", target_pid_to_str (ptid), | |
850 | safe_strerror (errno)); | |
851 | ||
852 | if (debug_linux_nat) | |
853 | fprintf_unfiltered (gdb_stdlog, | |
854 | "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n", | |
855 | target_pid_to_str (ptid)); | |
856 | ||
857 | pid = waitpid (GET_LWP (ptid), &status, 0); | |
858 | if (pid == -1 && errno == ECHILD) | |
859 | { | |
860 | /* Try again with __WCLONE to check cloned processes. */ | |
861 | pid = waitpid (GET_LWP (ptid), &status, __WCLONE); | |
862 | lp->cloned = 1; | |
863 | } | |
864 | ||
865 | gdb_assert (pid == GET_LWP (ptid) | |
866 | && WIFSTOPPED (status) && WSTOPSIG (status)); | |
867 | ||
868 | child_post_attach (pid); | |
869 | ||
870 | lp->stopped = 1; | |
871 | ||
872 | if (debug_linux_nat) | |
873 | { | |
874 | fprintf_unfiltered (gdb_stdlog, | |
875 | "LLAL: waitpid %s received %s\n", | |
876 | target_pid_to_str (ptid), | |
877 | status_to_str (status)); | |
878 | } | |
879 | } | |
880 | else | |
881 | { | |
882 | /* We assume that the LWP representing the original process is | |
883 | already stopped. Mark it as stopped in the data structure | |
884 | that the linux ptrace layer uses to keep track of threads. | |
885 | Note that this won't have already been done since the main | |
886 | thread will have, we assume, been stopped by an attach from a | |
887 | different layer. */ | |
888 | lp->stopped = 1; | |
889 | } | |
890 | } | |
891 | ||
892 | static void | |
893 | linux_nat_attach (char *args, int from_tty) | |
894 | { | |
895 | struct lwp_info *lp; | |
896 | pid_t pid; | |
897 | int status; | |
898 | ||
899 | /* FIXME: We should probably accept a list of process id's, and | |
900 | attach all of them. */ | |
1df84f13 | 901 | deprecated_child_ops.to_attach (args, from_tty); |
d6b0e80f AC |
902 | |
903 | /* Add the initial process as the first LWP to the list. */ | |
904 | lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid))); | |
905 | ||
906 | /* Make sure the initial process is stopped. The user-level threads | |
907 | layer might want to poke around in the inferior, and that won't | |
908 | work if things haven't stabilized yet. */ | |
909 | pid = waitpid (GET_PID (inferior_ptid), &status, 0); | |
910 | if (pid == -1 && errno == ECHILD) | |
911 | { | |
912 | warning ("%s is a cloned process", target_pid_to_str (inferior_ptid)); | |
913 | ||
914 | /* Try again with __WCLONE to check cloned processes. */ | |
915 | pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE); | |
916 | lp->cloned = 1; | |
917 | } | |
918 | ||
919 | gdb_assert (pid == GET_PID (inferior_ptid) | |
920 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP); | |
921 | ||
922 | lp->stopped = 1; | |
923 | ||
924 | /* Fake the SIGSTOP that core GDB expects. */ | |
925 | lp->status = W_STOPCODE (SIGSTOP); | |
926 | lp->resumed = 1; | |
927 | if (debug_linux_nat) | |
928 | { | |
929 | fprintf_unfiltered (gdb_stdlog, | |
930 | "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid); | |
931 | } | |
932 | } | |
933 | ||
934 | static int | |
935 | detach_callback (struct lwp_info *lp, void *data) | |
936 | { | |
937 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
938 | ||
939 | if (debug_linux_nat && lp->status) | |
940 | fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n", | |
941 | strsignal (WSTOPSIG (lp->status)), | |
942 | target_pid_to_str (lp->ptid)); | |
943 | ||
944 | while (lp->signalled && lp->stopped) | |
945 | { | |
946 | errno = 0; | |
947 | if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, | |
948 | WSTOPSIG (lp->status)) < 0) | |
949 | error ("Can't continue %s: %s", target_pid_to_str (lp->ptid), | |
950 | safe_strerror (errno)); | |
951 | ||
952 | if (debug_linux_nat) | |
953 | fprintf_unfiltered (gdb_stdlog, | |
954 | "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n", | |
955 | target_pid_to_str (lp->ptid), | |
956 | status_to_str (lp->status)); | |
957 | ||
958 | lp->stopped = 0; | |
959 | lp->signalled = 0; | |
960 | lp->status = 0; | |
961 | /* FIXME drow/2003-08-26: There was a call to stop_wait_callback | |
962 | here. But since lp->signalled was cleared above, | |
963 | stop_wait_callback didn't do anything; the process was left | |
964 | running. Shouldn't we be waiting for it to stop? | |
965 | I've removed the call, since stop_wait_callback now does do | |
966 | something when called with lp->signalled == 0. */ | |
967 | ||
968 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
969 | } | |
970 | ||
971 | /* We don't actually detach from the LWP that has an id equal to the | |
972 | overall process id just yet. */ | |
973 | if (GET_LWP (lp->ptid) != GET_PID (lp->ptid)) | |
974 | { | |
975 | errno = 0; | |
976 | if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, | |
977 | WSTOPSIG (lp->status)) < 0) | |
978 | error ("Can't detach %s: %s", target_pid_to_str (lp->ptid), | |
979 | safe_strerror (errno)); | |
980 | ||
981 | if (debug_linux_nat) | |
982 | fprintf_unfiltered (gdb_stdlog, | |
983 | "PTRACE_DETACH (%s, %s, 0) (OK)\n", | |
984 | target_pid_to_str (lp->ptid), | |
985 | strsignal (WSTOPSIG (lp->status))); | |
986 | ||
987 | delete_lwp (lp->ptid); | |
988 | } | |
989 | ||
990 | return 0; | |
991 | } | |
992 | ||
993 | static void | |
994 | linux_nat_detach (char *args, int from_tty) | |
995 | { | |
996 | iterate_over_lwps (detach_callback, NULL); | |
997 | ||
998 | /* Only the initial process should be left right now. */ | |
999 | gdb_assert (num_lwps == 1); | |
1000 | ||
1001 | trap_ptid = null_ptid; | |
1002 | ||
1003 | /* Destroy LWP info; it's no longer valid. */ | |
1004 | init_lwp_list (); | |
1005 | ||
1006 | /* Restore the original signal mask. */ | |
1007 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); | |
1008 | sigemptyset (&blocked_mask); | |
1009 | ||
1010 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); | |
1df84f13 | 1011 | deprecated_child_ops.to_detach (args, from_tty); |
d6b0e80f AC |
1012 | } |
1013 | ||
1014 | /* Resume LP. */ | |
1015 | ||
1016 | static int | |
1017 | resume_callback (struct lwp_info *lp, void *data) | |
1018 | { | |
1019 | if (lp->stopped && lp->status == 0) | |
1020 | { | |
1021 | struct thread_info *tp; | |
1022 | ||
1023 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0); | |
1024 | if (debug_linux_nat) | |
1025 | fprintf_unfiltered (gdb_stdlog, | |
1026 | "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n", | |
1027 | target_pid_to_str (lp->ptid)); | |
1028 | lp->stopped = 0; | |
1029 | lp->step = 0; | |
1030 | } | |
1031 | ||
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | static int | |
1036 | resume_clear_callback (struct lwp_info *lp, void *data) | |
1037 | { | |
1038 | lp->resumed = 0; | |
1039 | return 0; | |
1040 | } | |
1041 | ||
1042 | static int | |
1043 | resume_set_callback (struct lwp_info *lp, void *data) | |
1044 | { | |
1045 | lp->resumed = 1; | |
1046 | return 0; | |
1047 | } | |
1048 | ||
1049 | static void | |
1050 | linux_nat_resume (ptid_t ptid, int step, enum target_signal signo) | |
1051 | { | |
1052 | struct lwp_info *lp; | |
1053 | int resume_all; | |
1054 | ||
1055 | /* A specific PTID means `step only this process id'. */ | |
1056 | resume_all = (PIDGET (ptid) == -1); | |
1057 | ||
1058 | if (resume_all) | |
1059 | iterate_over_lwps (resume_set_callback, NULL); | |
1060 | else | |
1061 | iterate_over_lwps (resume_clear_callback, NULL); | |
1062 | ||
1063 | /* If PID is -1, it's the current inferior that should be | |
1064 | handled specially. */ | |
1065 | if (PIDGET (ptid) == -1) | |
1066 | ptid = inferior_ptid; | |
1067 | ||
1068 | lp = find_lwp_pid (ptid); | |
1069 | if (lp) | |
1070 | { | |
1071 | ptid = pid_to_ptid (GET_LWP (lp->ptid)); | |
1072 | ||
1073 | /* Remember if we're stepping. */ | |
1074 | lp->step = step; | |
1075 | ||
1076 | /* Mark this LWP as resumed. */ | |
1077 | lp->resumed = 1; | |
1078 | ||
1079 | /* If we have a pending wait status for this thread, there is no | |
1080 | point in resuming the process. */ | |
1081 | if (lp->status) | |
1082 | { | |
1083 | /* FIXME: What should we do if we are supposed to continue | |
1084 | this thread with a signal? */ | |
1085 | gdb_assert (signo == TARGET_SIGNAL_0); | |
1086 | return; | |
1087 | } | |
1088 | ||
1089 | /* Mark LWP as not stopped to prevent it from being continued by | |
1090 | resume_callback. */ | |
1091 | lp->stopped = 0; | |
1092 | } | |
1093 | ||
1094 | if (resume_all) | |
1095 | iterate_over_lwps (resume_callback, NULL); | |
1096 | ||
1097 | child_resume (ptid, step, signo); | |
1098 | if (debug_linux_nat) | |
1099 | fprintf_unfiltered (gdb_stdlog, | |
1100 | "LLR: %s %s, %s (resume event thread)\n", | |
1101 | step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
1102 | target_pid_to_str (ptid), | |
1103 | signo ? strsignal (signo) : "0"); | |
1104 | } | |
1105 | ||
1106 | /* Issue kill to specified lwp. */ | |
1107 | ||
1108 | static int tkill_failed; | |
1109 | ||
1110 | static int | |
1111 | kill_lwp (int lwpid, int signo) | |
1112 | { | |
1113 | errno = 0; | |
1114 | ||
1115 | /* Use tkill, if possible, in case we are using nptl threads. If tkill | |
1116 | fails, then we are not using nptl threads and we should be using kill. */ | |
1117 | ||
1118 | #ifdef HAVE_TKILL_SYSCALL | |
1119 | if (!tkill_failed) | |
1120 | { | |
1121 | int ret = syscall (__NR_tkill, lwpid, signo); | |
1122 | if (errno != ENOSYS) | |
1123 | return ret; | |
1124 | errno = 0; | |
1125 | tkill_failed = 1; | |
1126 | } | |
1127 | #endif | |
1128 | ||
1129 | return kill (lwpid, signo); | |
1130 | } | |
1131 | ||
1132 | /* Handle a GNU/Linux extended wait response. Most of the work we | |
1133 | just pass off to linux_handle_extended_wait, but if it reports a | |
1134 | clone event we need to add the new LWP to our list (and not report | |
1135 | the trap to higher layers). This function returns non-zero if | |
1136 | the event should be ignored and we should wait again. */ | |
1137 | ||
1138 | static int | |
1139 | linux_nat_handle_extended (struct lwp_info *lp, int status) | |
1140 | { | |
1141 | linux_handle_extended_wait (GET_LWP (lp->ptid), status, | |
1142 | &lp->waitstatus); | |
1143 | ||
1144 | /* TARGET_WAITKIND_SPURIOUS is used to indicate clone events. */ | |
1145 | if (lp->waitstatus.kind == TARGET_WAITKIND_SPURIOUS) | |
1146 | { | |
1147 | struct lwp_info *new_lp; | |
1148 | new_lp = add_lwp (BUILD_LWP (lp->waitstatus.value.related_pid, | |
1149 | GET_PID (inferior_ptid))); | |
1150 | new_lp->cloned = 1; | |
1151 | new_lp->stopped = 1; | |
1152 | ||
1153 | lp->waitstatus.kind = TARGET_WAITKIND_IGNORE; | |
1154 | ||
1155 | if (debug_linux_nat) | |
1156 | fprintf_unfiltered (gdb_stdlog, | |
1157 | "LLHE: Got clone event from LWP %ld, resuming\n", | |
1158 | GET_LWP (lp->ptid)); | |
1159 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
1160 | ||
1161 | return 1; | |
1162 | } | |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has | |
1168 | exited. */ | |
1169 | ||
1170 | static int | |
1171 | wait_lwp (struct lwp_info *lp) | |
1172 | { | |
1173 | pid_t pid; | |
1174 | int status; | |
1175 | int thread_dead = 0; | |
1176 | ||
1177 | gdb_assert (!lp->stopped); | |
1178 | gdb_assert (lp->status == 0); | |
1179 | ||
1180 | pid = waitpid (GET_LWP (lp->ptid), &status, 0); | |
1181 | if (pid == -1 && errno == ECHILD) | |
1182 | { | |
1183 | pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE); | |
1184 | if (pid == -1 && errno == ECHILD) | |
1185 | { | |
1186 | /* The thread has previously exited. We need to delete it | |
1187 | now because, for some vendor 2.4 kernels with NPTL | |
1188 | support backported, there won't be an exit event unless | |
1189 | it is the main thread. 2.6 kernels will report an exit | |
1190 | event for each thread that exits, as expected. */ | |
1191 | thread_dead = 1; | |
1192 | if (debug_linux_nat) | |
1193 | fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n", | |
1194 | target_pid_to_str (lp->ptid)); | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | if (!thread_dead) | |
1199 | { | |
1200 | gdb_assert (pid == GET_LWP (lp->ptid)); | |
1201 | ||
1202 | if (debug_linux_nat) | |
1203 | { | |
1204 | fprintf_unfiltered (gdb_stdlog, | |
1205 | "WL: waitpid %s received %s\n", | |
1206 | target_pid_to_str (lp->ptid), | |
1207 | status_to_str (status)); | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | /* Check if the thread has exited. */ | |
1212 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
1213 | { | |
1214 | thread_dead = 1; | |
1215 | if (debug_linux_nat) | |
1216 | fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n", | |
1217 | target_pid_to_str (lp->ptid)); | |
1218 | } | |
1219 | ||
1220 | if (thread_dead) | |
1221 | { | |
1222 | if (in_thread_list (lp->ptid)) | |
1223 | { | |
1224 | /* Core GDB cannot deal with us deleting the current thread. */ | |
1225 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
1226 | delete_thread (lp->ptid); | |
1227 | printf_unfiltered ("[%s exited]\n", | |
1228 | target_pid_to_str (lp->ptid)); | |
1229 | } | |
1230 | ||
1231 | delete_lwp (lp->ptid); | |
1232 | return 0; | |
1233 | } | |
1234 | ||
1235 | gdb_assert (WIFSTOPPED (status)); | |
1236 | ||
1237 | /* Handle GNU/Linux's extended waitstatus for trace events. */ | |
1238 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) | |
1239 | { | |
1240 | if (debug_linux_nat) | |
1241 | fprintf_unfiltered (gdb_stdlog, | |
1242 | "WL: Handling extended status 0x%06x\n", | |
1243 | status); | |
1244 | if (linux_nat_handle_extended (lp, status)) | |
1245 | return wait_lwp (lp); | |
1246 | } | |
1247 | ||
1248 | return status; | |
1249 | } | |
1250 | ||
1251 | /* Send a SIGSTOP to LP. */ | |
1252 | ||
1253 | static int | |
1254 | stop_callback (struct lwp_info *lp, void *data) | |
1255 | { | |
1256 | if (!lp->stopped && !lp->signalled) | |
1257 | { | |
1258 | int ret; | |
1259 | ||
1260 | if (debug_linux_nat) | |
1261 | { | |
1262 | fprintf_unfiltered (gdb_stdlog, | |
1263 | "SC: kill %s **<SIGSTOP>**\n", | |
1264 | target_pid_to_str (lp->ptid)); | |
1265 | } | |
1266 | errno = 0; | |
1267 | ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP); | |
1268 | if (debug_linux_nat) | |
1269 | { | |
1270 | fprintf_unfiltered (gdb_stdlog, | |
1271 | "SC: lwp kill %d %s\n", | |
1272 | ret, | |
1273 | errno ? safe_strerror (errno) : "ERRNO-OK"); | |
1274 | } | |
1275 | ||
1276 | lp->signalled = 1; | |
1277 | gdb_assert (lp->status == 0); | |
1278 | } | |
1279 | ||
1280 | return 0; | |
1281 | } | |
1282 | ||
1283 | /* Wait until LP is stopped. If DATA is non-null it is interpreted as | |
1284 | a pointer to a set of signals to be flushed immediately. */ | |
1285 | ||
1286 | static int | |
1287 | stop_wait_callback (struct lwp_info *lp, void *data) | |
1288 | { | |
1289 | sigset_t *flush_mask = data; | |
1290 | ||
1291 | if (!lp->stopped) | |
1292 | { | |
1293 | int status; | |
1294 | ||
1295 | status = wait_lwp (lp); | |
1296 | if (status == 0) | |
1297 | return 0; | |
1298 | ||
1299 | /* Ignore any signals in FLUSH_MASK. */ | |
1300 | if (flush_mask && sigismember (flush_mask, WSTOPSIG (status))) | |
1301 | { | |
1302 | if (!lp->signalled) | |
1303 | { | |
1304 | lp->stopped = 1; | |
1305 | return 0; | |
1306 | } | |
1307 | ||
1308 | errno = 0; | |
1309 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
1310 | if (debug_linux_nat) | |
1311 | fprintf_unfiltered (gdb_stdlog, | |
1312 | "PTRACE_CONT %s, 0, 0 (%s)\n", | |
1313 | target_pid_to_str (lp->ptid), | |
1314 | errno ? safe_strerror (errno) : "OK"); | |
1315 | ||
1316 | return stop_wait_callback (lp, flush_mask); | |
1317 | } | |
1318 | ||
1319 | if (WSTOPSIG (status) != SIGSTOP) | |
1320 | { | |
1321 | if (WSTOPSIG (status) == SIGTRAP) | |
1322 | { | |
1323 | /* If a LWP other than the LWP that we're reporting an | |
1324 | event for has hit a GDB breakpoint (as opposed to | |
1325 | some random trap signal), then just arrange for it to | |
1326 | hit it again later. We don't keep the SIGTRAP status | |
1327 | and don't forward the SIGTRAP signal to the LWP. We | |
1328 | will handle the current event, eventually we will | |
1329 | resume all LWPs, and this one will get its breakpoint | |
1330 | trap again. | |
1331 | ||
1332 | If we do not do this, then we run the risk that the | |
1333 | user will delete or disable the breakpoint, but the | |
1334 | thread will have already tripped on it. */ | |
1335 | ||
1336 | /* Now resume this LWP and get the SIGSTOP event. */ | |
1337 | errno = 0; | |
1338 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
1339 | if (debug_linux_nat) | |
1340 | { | |
1341 | fprintf_unfiltered (gdb_stdlog, | |
1342 | "PTRACE_CONT %s, 0, 0 (%s)\n", | |
1343 | target_pid_to_str (lp->ptid), | |
1344 | errno ? safe_strerror (errno) : "OK"); | |
1345 | ||
1346 | fprintf_unfiltered (gdb_stdlog, | |
1347 | "SWC: Candidate SIGTRAP event in %s\n", | |
1348 | target_pid_to_str (lp->ptid)); | |
1349 | } | |
1350 | /* Hold the SIGTRAP for handling by linux_nat_wait. */ | |
1351 | stop_wait_callback (lp, data); | |
1352 | /* If there's another event, throw it back into the queue. */ | |
1353 | if (lp->status) | |
1354 | { | |
1355 | if (debug_linux_nat) | |
1356 | { | |
1357 | fprintf_unfiltered (gdb_stdlog, | |
1358 | "SWC: kill %s, %s\n", | |
1359 | target_pid_to_str (lp->ptid), | |
1360 | status_to_str ((int) status)); | |
1361 | } | |
1362 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); | |
1363 | } | |
1364 | /* Save the sigtrap event. */ | |
1365 | lp->status = status; | |
1366 | return 0; | |
1367 | } | |
1368 | else | |
1369 | { | |
1370 | /* The thread was stopped with a signal other than | |
1371 | SIGSTOP, and didn't accidentally trip a breakpoint. */ | |
1372 | ||
1373 | if (debug_linux_nat) | |
1374 | { | |
1375 | fprintf_unfiltered (gdb_stdlog, | |
1376 | "SWC: Pending event %s in %s\n", | |
1377 | status_to_str ((int) status), | |
1378 | target_pid_to_str (lp->ptid)); | |
1379 | } | |
1380 | /* Now resume this LWP and get the SIGSTOP event. */ | |
1381 | errno = 0; | |
1382 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
1383 | if (debug_linux_nat) | |
1384 | fprintf_unfiltered (gdb_stdlog, | |
1385 | "SWC: PTRACE_CONT %s, 0, 0 (%s)\n", | |
1386 | target_pid_to_str (lp->ptid), | |
1387 | errno ? safe_strerror (errno) : "OK"); | |
1388 | ||
1389 | /* Hold this event/waitstatus while we check to see if | |
1390 | there are any more (we still want to get that SIGSTOP). */ | |
1391 | stop_wait_callback (lp, data); | |
1392 | /* If the lp->status field is still empty, use it to hold | |
1393 | this event. If not, then this event must be returned | |
1394 | to the event queue of the LWP. */ | |
1395 | if (lp->status == 0) | |
1396 | lp->status = status; | |
1397 | else | |
1398 | { | |
1399 | if (debug_linux_nat) | |
1400 | { | |
1401 | fprintf_unfiltered (gdb_stdlog, | |
1402 | "SWC: kill %s, %s\n", | |
1403 | target_pid_to_str (lp->ptid), | |
1404 | status_to_str ((int) status)); | |
1405 | } | |
1406 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status)); | |
1407 | } | |
1408 | return 0; | |
1409 | } | |
1410 | } | |
1411 | else | |
1412 | { | |
1413 | /* We caught the SIGSTOP that we intended to catch, so | |
1414 | there's no SIGSTOP pending. */ | |
1415 | lp->stopped = 1; | |
1416 | lp->signalled = 0; | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | return 0; | |
1421 | } | |
1422 | ||
1423 | /* Check whether PID has any pending signals in FLUSH_MASK. If so set | |
1424 | the appropriate bits in PENDING, and return 1 - otherwise return 0. */ | |
1425 | ||
1426 | static int | |
1427 | linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask) | |
1428 | { | |
1429 | sigset_t blocked, ignored; | |
1430 | int i; | |
1431 | ||
1432 | linux_proc_pending_signals (pid, pending, &blocked, &ignored); | |
1433 | ||
1434 | if (!flush_mask) | |
1435 | return 0; | |
1436 | ||
1437 | for (i = 1; i < NSIG; i++) | |
1438 | if (sigismember (pending, i)) | |
1439 | if (!sigismember (flush_mask, i) | |
1440 | || sigismember (&blocked, i) | |
1441 | || sigismember (&ignored, i)) | |
1442 | sigdelset (pending, i); | |
1443 | ||
1444 | if (sigisemptyset (pending)) | |
1445 | return 0; | |
1446 | ||
1447 | return 1; | |
1448 | } | |
1449 | ||
1450 | /* DATA is interpreted as a mask of signals to flush. If LP has | |
1451 | signals pending, and they are all in the flush mask, then arrange | |
1452 | to flush them. LP should be stopped, as should all other threads | |
1453 | it might share a signal queue with. */ | |
1454 | ||
1455 | static int | |
1456 | flush_callback (struct lwp_info *lp, void *data) | |
1457 | { | |
1458 | sigset_t *flush_mask = data; | |
1459 | sigset_t pending, intersection, blocked, ignored; | |
1460 | int pid, status; | |
1461 | ||
1462 | /* Normally, when an LWP exits, it is removed from the LWP list. The | |
1463 | last LWP isn't removed till later, however. So if there is only | |
1464 | one LWP on the list, make sure it's alive. */ | |
1465 | if (lwp_list == lp && lp->next == NULL) | |
1466 | if (!linux_nat_thread_alive (lp->ptid)) | |
1467 | return 0; | |
1468 | ||
1469 | /* Just because the LWP is stopped doesn't mean that new signals | |
1470 | can't arrive from outside, so this function must be careful of | |
1471 | race conditions. However, because all threads are stopped, we | |
1472 | can assume that the pending mask will not shrink unless we resume | |
1473 | the LWP, and that it will then get another signal. We can't | |
1474 | control which one, however. */ | |
1475 | ||
1476 | if (lp->status) | |
1477 | { | |
1478 | if (debug_linux_nat) | |
1479 | printf_unfiltered ("FC: LP has pending status %06x\n", lp->status); | |
1480 | if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status))) | |
1481 | lp->status = 0; | |
1482 | } | |
1483 | ||
1484 | while (linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask)) | |
1485 | { | |
1486 | int ret; | |
1487 | ||
1488 | errno = 0; | |
1489 | ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
1490 | if (debug_linux_nat) | |
1491 | fprintf_unfiltered (gdb_stderr, | |
1492 | "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno); | |
1493 | ||
1494 | lp->stopped = 0; | |
1495 | stop_wait_callback (lp, flush_mask); | |
1496 | if (debug_linux_nat) | |
1497 | fprintf_unfiltered (gdb_stderr, | |
1498 | "FC: Wait finished; saved status is %d\n", | |
1499 | lp->status); | |
1500 | } | |
1501 | ||
1502 | return 0; | |
1503 | } | |
1504 | ||
1505 | /* Return non-zero if LP has a wait status pending. */ | |
1506 | ||
1507 | static int | |
1508 | status_callback (struct lwp_info *lp, void *data) | |
1509 | { | |
1510 | /* Only report a pending wait status if we pretend that this has | |
1511 | indeed been resumed. */ | |
1512 | return (lp->status != 0 && lp->resumed); | |
1513 | } | |
1514 | ||
1515 | /* Return non-zero if LP isn't stopped. */ | |
1516 | ||
1517 | static int | |
1518 | running_callback (struct lwp_info *lp, void *data) | |
1519 | { | |
1520 | return (lp->stopped == 0 || (lp->status != 0 && lp->resumed)); | |
1521 | } | |
1522 | ||
1523 | /* Count the LWP's that have had events. */ | |
1524 | ||
1525 | static int | |
1526 | count_events_callback (struct lwp_info *lp, void *data) | |
1527 | { | |
1528 | int *count = data; | |
1529 | ||
1530 | gdb_assert (count != NULL); | |
1531 | ||
1532 | /* Count only LWPs that have a SIGTRAP event pending. */ | |
1533 | if (lp->status != 0 | |
1534 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) | |
1535 | (*count)++; | |
1536 | ||
1537 | return 0; | |
1538 | } | |
1539 | ||
1540 | /* Select the LWP (if any) that is currently being single-stepped. */ | |
1541 | ||
1542 | static int | |
1543 | select_singlestep_lwp_callback (struct lwp_info *lp, void *data) | |
1544 | { | |
1545 | if (lp->step && lp->status != 0) | |
1546 | return 1; | |
1547 | else | |
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | /* Select the Nth LWP that has had a SIGTRAP event. */ | |
1552 | ||
1553 | static int | |
1554 | select_event_lwp_callback (struct lwp_info *lp, void *data) | |
1555 | { | |
1556 | int *selector = data; | |
1557 | ||
1558 | gdb_assert (selector != NULL); | |
1559 | ||
1560 | /* Select only LWPs that have a SIGTRAP event pending. */ | |
1561 | if (lp->status != 0 | |
1562 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) | |
1563 | if ((*selector)-- == 0) | |
1564 | return 1; | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static int | |
1570 | cancel_breakpoints_callback (struct lwp_info *lp, void *data) | |
1571 | { | |
1572 | struct lwp_info *event_lp = data; | |
1573 | ||
1574 | /* Leave the LWP that has been elected to receive a SIGTRAP alone. */ | |
1575 | if (lp == event_lp) | |
1576 | return 0; | |
1577 | ||
1578 | /* If a LWP other than the LWP that we're reporting an event for has | |
1579 | hit a GDB breakpoint (as opposed to some random trap signal), | |
1580 | then just arrange for it to hit it again later. We don't keep | |
1581 | the SIGTRAP status and don't forward the SIGTRAP signal to the | |
1582 | LWP. We will handle the current event, eventually we will resume | |
1583 | all LWPs, and this one will get its breakpoint trap again. | |
1584 | ||
1585 | If we do not do this, then we run the risk that the user will | |
1586 | delete or disable the breakpoint, but the LWP will have already | |
1587 | tripped on it. */ | |
1588 | ||
1589 | if (lp->status != 0 | |
1590 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP | |
1591 | && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - | |
1592 | DECR_PC_AFTER_BREAK)) | |
1593 | { | |
1594 | if (debug_linux_nat) | |
1595 | fprintf_unfiltered (gdb_stdlog, | |
1596 | "CBC: Push back breakpoint for %s\n", | |
1597 | target_pid_to_str (lp->ptid)); | |
1598 | ||
1599 | /* Back up the PC if necessary. */ | |
1600 | if (DECR_PC_AFTER_BREAK) | |
1601 | write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid); | |
1602 | ||
1603 | /* Throw away the SIGTRAP. */ | |
1604 | lp->status = 0; | |
1605 | } | |
1606 | ||
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | /* Select one LWP out of those that have events pending. */ | |
1611 | ||
1612 | static void | |
1613 | select_event_lwp (struct lwp_info **orig_lp, int *status) | |
1614 | { | |
1615 | int num_events = 0; | |
1616 | int random_selector; | |
1617 | struct lwp_info *event_lp; | |
1618 | ||
1619 | /* Record the wait status for the origional LWP. */ | |
1620 | (*orig_lp)->status = *status; | |
1621 | ||
1622 | /* Give preference to any LWP that is being single-stepped. */ | |
1623 | event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); | |
1624 | if (event_lp != NULL) | |
1625 | { | |
1626 | if (debug_linux_nat) | |
1627 | fprintf_unfiltered (gdb_stdlog, | |
1628 | "SEL: Select single-step %s\n", | |
1629 | target_pid_to_str (event_lp->ptid)); | |
1630 | } | |
1631 | else | |
1632 | { | |
1633 | /* No single-stepping LWP. Select one at random, out of those | |
1634 | which have had SIGTRAP events. */ | |
1635 | ||
1636 | /* First see how many SIGTRAP events we have. */ | |
1637 | iterate_over_lwps (count_events_callback, &num_events); | |
1638 | ||
1639 | /* Now randomly pick a LWP out of those that have had a SIGTRAP. */ | |
1640 | random_selector = (int) | |
1641 | ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); | |
1642 | ||
1643 | if (debug_linux_nat && num_events > 1) | |
1644 | fprintf_unfiltered (gdb_stdlog, | |
1645 | "SEL: Found %d SIGTRAP events, selecting #%d\n", | |
1646 | num_events, random_selector); | |
1647 | ||
1648 | event_lp = iterate_over_lwps (select_event_lwp_callback, | |
1649 | &random_selector); | |
1650 | } | |
1651 | ||
1652 | if (event_lp != NULL) | |
1653 | { | |
1654 | /* Switch the event LWP. */ | |
1655 | *orig_lp = event_lp; | |
1656 | *status = event_lp->status; | |
1657 | } | |
1658 | ||
1659 | /* Flush the wait status for the event LWP. */ | |
1660 | (*orig_lp)->status = 0; | |
1661 | } | |
1662 | ||
1663 | /* Return non-zero if LP has been resumed. */ | |
1664 | ||
1665 | static int | |
1666 | resumed_callback (struct lwp_info *lp, void *data) | |
1667 | { | |
1668 | return lp->resumed; | |
1669 | } | |
1670 | ||
1671 | #ifdef CHILD_WAIT | |
1672 | ||
1673 | /* We need to override child_wait to support attaching to cloned | |
1674 | processes, since a normal wait (as done by the default version) | |
1675 | ignores those processes. */ | |
1676 | ||
1677 | /* Wait for child PTID to do something. Return id of the child, | |
1678 | minus_one_ptid in case of error; store status into *OURSTATUS. */ | |
1679 | ||
1680 | ptid_t | |
1681 | child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
1682 | { | |
1683 | int save_errno; | |
1684 | int status; | |
1685 | pid_t pid; | |
1686 | ||
1687 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
1688 | ||
1689 | do | |
1690 | { | |
1691 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
1692 | attached process. */ | |
1693 | set_sigio_trap (); | |
1694 | ||
1695 | pid = waitpid (GET_PID (ptid), &status, 0); | |
1696 | if (pid == -1 && errno == ECHILD) | |
1697 | /* Try again with __WCLONE to check cloned processes. */ | |
1698 | pid = waitpid (GET_PID (ptid), &status, __WCLONE); | |
1699 | ||
1700 | if (debug_linux_nat) | |
1701 | { | |
1702 | fprintf_unfiltered (gdb_stdlog, | |
1703 | "CW: waitpid %ld received %s\n", | |
1704 | (long) pid, status_to_str (status)); | |
1705 | } | |
1706 | ||
1707 | save_errno = errno; | |
1708 | ||
1709 | /* Make sure we don't report an event for the exit of the | |
1710 | original program, if we've detached from it. */ | |
1711 | if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid)) | |
1712 | { | |
1713 | pid = -1; | |
1714 | save_errno = EINTR; | |
1715 | } | |
1716 | ||
1717 | /* Check for stop events reported by a process we didn't already | |
1718 | know about - in this case, anything other than inferior_ptid. | |
1719 | ||
1720 | If we're expecting to receive stopped processes after fork, | |
1721 | vfork, and clone events, then we'll just add the new one to | |
1722 | our list and go back to waiting for the event to be reported | |
1723 | - the stopped process might be returned from waitpid before | |
1724 | or after the event is. If we want to handle debugging of | |
1725 | CLONE_PTRACE processes we need to do more here, i.e. switch | |
1726 | to multi-threaded mode. */ | |
1727 | if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP | |
1728 | && pid != GET_PID (inferior_ptid)) | |
1729 | { | |
1730 | linux_record_stopped_pid (pid); | |
1731 | pid = -1; | |
1732 | save_errno = EINTR; | |
1733 | } | |
1734 | ||
1735 | /* Handle GNU/Linux's extended waitstatus for trace events. */ | |
1736 | if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP | |
1737 | && status >> 16 != 0) | |
1738 | { | |
1739 | linux_handle_extended_wait (pid, status, ourstatus); | |
1740 | ||
1741 | /* If we see a clone event, detach the child, and don't | |
1742 | report the event. It would be nice to offer some way to | |
1743 | switch into a non-thread-db based threaded mode at this | |
1744 | point. */ | |
1745 | if (ourstatus->kind == TARGET_WAITKIND_SPURIOUS) | |
1746 | { | |
1747 | ptrace (PTRACE_DETACH, ourstatus->value.related_pid, 0, 0); | |
1748 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
1749 | ptrace (PTRACE_CONT, pid, 0, 0); | |
1750 | pid = -1; | |
1751 | save_errno = EINTR; | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | clear_sigio_trap (); | |
1756 | clear_sigint_trap (); | |
1757 | } | |
1758 | while (pid == -1 && save_errno == EINTR); | |
1759 | ||
1760 | if (pid == -1) | |
1761 | { | |
1762 | warning ("Child process unexpectedly missing: %s", | |
1763 | safe_strerror (errno)); | |
1764 | ||
1765 | /* Claim it exited with unknown signal. */ | |
1766 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1767 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
1768 | return minus_one_ptid; | |
1769 | } | |
1770 | ||
1771 | if (ourstatus->kind == TARGET_WAITKIND_IGNORE) | |
1772 | store_waitstatus (ourstatus, status); | |
1773 | ||
1774 | return pid_to_ptid (pid); | |
1775 | } | |
1776 | ||
1777 | #endif | |
1778 | ||
1779 | /* Stop an active thread, verify it still exists, then resume it. */ | |
1780 | ||
1781 | static int | |
1782 | stop_and_resume_callback (struct lwp_info *lp, void *data) | |
1783 | { | |
1784 | struct lwp_info *ptr; | |
1785 | ||
1786 | if (!lp->stopped && !lp->signalled) | |
1787 | { | |
1788 | stop_callback (lp, NULL); | |
1789 | stop_wait_callback (lp, NULL); | |
1790 | /* Resume if the lwp still exists. */ | |
1791 | for (ptr = lwp_list; ptr; ptr = ptr->next) | |
1792 | if (lp == ptr) | |
1793 | { | |
1794 | resume_callback (lp, NULL); | |
1795 | resume_set_callback (lp, NULL); | |
1796 | } | |
1797 | } | |
1798 | return 0; | |
1799 | } | |
1800 | ||
1801 | static ptid_t | |
1802 | linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
1803 | { | |
1804 | struct lwp_info *lp = NULL; | |
1805 | int options = 0; | |
1806 | int status = 0; | |
1807 | pid_t pid = PIDGET (ptid); | |
1808 | sigset_t flush_mask; | |
1809 | ||
1810 | sigemptyset (&flush_mask); | |
1811 | ||
1812 | /* Make sure SIGCHLD is blocked. */ | |
1813 | if (!sigismember (&blocked_mask, SIGCHLD)) | |
1814 | { | |
1815 | sigaddset (&blocked_mask, SIGCHLD); | |
1816 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
1817 | } | |
1818 | ||
1819 | retry: | |
1820 | ||
1821 | /* Make sure there is at least one LWP that has been resumed, at | |
1822 | least if there are any LWPs at all. */ | |
1823 | gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL)); | |
1824 | ||
1825 | /* First check if there is a LWP with a wait status pending. */ | |
1826 | if (pid == -1) | |
1827 | { | |
1828 | /* Any LWP that's been resumed will do. */ | |
1829 | lp = iterate_over_lwps (status_callback, NULL); | |
1830 | if (lp) | |
1831 | { | |
1832 | status = lp->status; | |
1833 | lp->status = 0; | |
1834 | ||
1835 | if (debug_linux_nat && status) | |
1836 | fprintf_unfiltered (gdb_stdlog, | |
1837 | "LLW: Using pending wait status %s for %s.\n", | |
1838 | status_to_str (status), | |
1839 | target_pid_to_str (lp->ptid)); | |
1840 | } | |
1841 | ||
1842 | /* But if we don't fine one, we'll have to wait, and check both | |
1843 | cloned and uncloned processes. We start with the cloned | |
1844 | processes. */ | |
1845 | options = __WCLONE | WNOHANG; | |
1846 | } | |
1847 | else if (is_lwp (ptid)) | |
1848 | { | |
1849 | if (debug_linux_nat) | |
1850 | fprintf_unfiltered (gdb_stdlog, | |
1851 | "LLW: Waiting for specific LWP %s.\n", | |
1852 | target_pid_to_str (ptid)); | |
1853 | ||
1854 | /* We have a specific LWP to check. */ | |
1855 | lp = find_lwp_pid (ptid); | |
1856 | gdb_assert (lp); | |
1857 | status = lp->status; | |
1858 | lp->status = 0; | |
1859 | ||
1860 | if (debug_linux_nat && status) | |
1861 | fprintf_unfiltered (gdb_stdlog, | |
1862 | "LLW: Using pending wait status %s for %s.\n", | |
1863 | status_to_str (status), | |
1864 | target_pid_to_str (lp->ptid)); | |
1865 | ||
1866 | /* If we have to wait, take into account whether PID is a cloned | |
1867 | process or not. And we have to convert it to something that | |
1868 | the layer beneath us can understand. */ | |
1869 | options = lp->cloned ? __WCLONE : 0; | |
1870 | pid = GET_LWP (ptid); | |
1871 | } | |
1872 | ||
1873 | if (status && lp->signalled) | |
1874 | { | |
1875 | /* A pending SIGSTOP may interfere with the normal stream of | |
1876 | events. In a typical case where interference is a problem, | |
1877 | we have a SIGSTOP signal pending for LWP A while | |
1878 | single-stepping it, encounter an event in LWP B, and take the | |
1879 | pending SIGSTOP while trying to stop LWP A. After processing | |
1880 | the event in LWP B, LWP A is continued, and we'll never see | |
1881 | the SIGTRAP associated with the last time we were | |
1882 | single-stepping LWP A. */ | |
1883 | ||
1884 | /* Resume the thread. It should halt immediately returning the | |
1885 | pending SIGSTOP. */ | |
1886 | registers_changed (); | |
1887 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, | |
1888 | TARGET_SIGNAL_0); | |
1889 | if (debug_linux_nat) | |
1890 | fprintf_unfiltered (gdb_stdlog, | |
1891 | "LLW: %s %s, 0, 0 (expect SIGSTOP)\n", | |
1892 | lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
1893 | target_pid_to_str (lp->ptid)); | |
1894 | lp->stopped = 0; | |
1895 | gdb_assert (lp->resumed); | |
1896 | ||
1897 | /* This should catch the pending SIGSTOP. */ | |
1898 | stop_wait_callback (lp, NULL); | |
1899 | } | |
1900 | ||
1901 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
1902 | attached process. */ | |
1903 | set_sigio_trap (); | |
1904 | ||
1905 | while (status == 0) | |
1906 | { | |
1907 | pid_t lwpid; | |
1908 | ||
1909 | lwpid = waitpid (pid, &status, options); | |
1910 | if (lwpid > 0) | |
1911 | { | |
1912 | gdb_assert (pid == -1 || lwpid == pid); | |
1913 | ||
1914 | if (debug_linux_nat) | |
1915 | { | |
1916 | fprintf_unfiltered (gdb_stdlog, | |
1917 | "LLW: waitpid %ld received %s\n", | |
1918 | (long) lwpid, status_to_str (status)); | |
1919 | } | |
1920 | ||
1921 | lp = find_lwp_pid (pid_to_ptid (lwpid)); | |
1922 | ||
1923 | /* Check for stop events reported by a process we didn't | |
1924 | already know about - anything not already in our LWP | |
1925 | list. | |
1926 | ||
1927 | If we're expecting to receive stopped processes after | |
1928 | fork, vfork, and clone events, then we'll just add the | |
1929 | new one to our list and go back to waiting for the event | |
1930 | to be reported - the stopped process might be returned | |
1931 | from waitpid before or after the event is. */ | |
1932 | if (WIFSTOPPED (status) && !lp) | |
1933 | { | |
1934 | linux_record_stopped_pid (lwpid); | |
1935 | status = 0; | |
1936 | continue; | |
1937 | } | |
1938 | ||
1939 | /* Make sure we don't report an event for the exit of an LWP not in | |
1940 | our list, i.e. not part of the current process. This can happen | |
1941 | if we detach from a program we original forked and then it | |
1942 | exits. */ | |
1943 | if (!WIFSTOPPED (status) && !lp) | |
1944 | { | |
1945 | status = 0; | |
1946 | continue; | |
1947 | } | |
1948 | ||
1949 | /* NOTE drow/2003-06-17: This code seems to be meant for debugging | |
1950 | CLONE_PTRACE processes which do not use the thread library - | |
1951 | otherwise we wouldn't find the new LWP this way. That doesn't | |
1952 | currently work, and the following code is currently unreachable | |
1953 | due to the two blocks above. If it's fixed some day, this code | |
1954 | should be broken out into a function so that we can also pick up | |
1955 | LWPs from the new interface. */ | |
1956 | if (!lp) | |
1957 | { | |
1958 | lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); | |
1959 | if (options & __WCLONE) | |
1960 | lp->cloned = 1; | |
1961 | ||
1962 | if (threaded) | |
1963 | { | |
1964 | gdb_assert (WIFSTOPPED (status) | |
1965 | && WSTOPSIG (status) == SIGSTOP); | |
1966 | lp->signalled = 1; | |
1967 | ||
1968 | if (!in_thread_list (inferior_ptid)) | |
1969 | { | |
1970 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), | |
1971 | GET_PID (inferior_ptid)); | |
1972 | add_thread (inferior_ptid); | |
1973 | } | |
1974 | ||
1975 | add_thread (lp->ptid); | |
1976 | printf_unfiltered ("[New %s]\n", | |
1977 | target_pid_to_str (lp->ptid)); | |
1978 | } | |
1979 | } | |
1980 | ||
1981 | /* Handle GNU/Linux's extended waitstatus for trace events. */ | |
1982 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) | |
1983 | { | |
1984 | if (debug_linux_nat) | |
1985 | fprintf_unfiltered (gdb_stdlog, | |
1986 | "LLW: Handling extended status 0x%06x\n", | |
1987 | status); | |
1988 | if (linux_nat_handle_extended (lp, status)) | |
1989 | { | |
1990 | status = 0; | |
1991 | continue; | |
1992 | } | |
1993 | } | |
1994 | ||
1995 | /* Check if the thread has exited. */ | |
1996 | if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) | |
1997 | { | |
1998 | if (in_thread_list (lp->ptid)) | |
1999 | { | |
2000 | /* Core GDB cannot deal with us deleting the current | |
2001 | thread. */ | |
2002 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
2003 | delete_thread (lp->ptid); | |
2004 | printf_unfiltered ("[%s exited]\n", | |
2005 | target_pid_to_str (lp->ptid)); | |
2006 | } | |
2007 | ||
2008 | /* If this is the main thread, we must stop all threads and | |
2009 | verify if they are still alive. This is because in the nptl | |
2010 | thread model, there is no signal issued for exiting LWPs | |
2011 | other than the main thread. We only get the main thread | |
2012 | exit signal once all child threads have already exited. | |
2013 | If we stop all the threads and use the stop_wait_callback | |
2014 | to check if they have exited we can determine whether this | |
2015 | signal should be ignored or whether it means the end of the | |
2016 | debugged application, regardless of which threading model | |
2017 | is being used. */ | |
2018 | if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)) | |
2019 | { | |
2020 | lp->stopped = 1; | |
2021 | iterate_over_lwps (stop_and_resume_callback, NULL); | |
2022 | } | |
2023 | ||
2024 | if (debug_linux_nat) | |
2025 | fprintf_unfiltered (gdb_stdlog, | |
2026 | "LLW: %s exited.\n", | |
2027 | target_pid_to_str (lp->ptid)); | |
2028 | ||
2029 | delete_lwp (lp->ptid); | |
2030 | ||
2031 | /* If there is at least one more LWP, then the exit signal | |
2032 | was not the end of the debugged application and should be | |
2033 | ignored. */ | |
2034 | if (num_lwps > 0) | |
2035 | { | |
2036 | /* Make sure there is at least one thread running. */ | |
2037 | gdb_assert (iterate_over_lwps (running_callback, NULL)); | |
2038 | ||
2039 | /* Discard the event. */ | |
2040 | status = 0; | |
2041 | continue; | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | /* Check if the current LWP has previously exited. In the nptl | |
2046 | thread model, LWPs other than the main thread do not issue | |
2047 | signals when they exit so we must check whenever the thread | |
2048 | has stopped. A similar check is made in stop_wait_callback(). */ | |
2049 | if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid)) | |
2050 | { | |
2051 | if (in_thread_list (lp->ptid)) | |
2052 | { | |
2053 | /* Core GDB cannot deal with us deleting the current | |
2054 | thread. */ | |
2055 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
2056 | delete_thread (lp->ptid); | |
2057 | printf_unfiltered ("[%s exited]\n", | |
2058 | target_pid_to_str (lp->ptid)); | |
2059 | } | |
2060 | if (debug_linux_nat) | |
2061 | fprintf_unfiltered (gdb_stdlog, | |
2062 | "LLW: %s exited.\n", | |
2063 | target_pid_to_str (lp->ptid)); | |
2064 | ||
2065 | delete_lwp (lp->ptid); | |
2066 | ||
2067 | /* Make sure there is at least one thread running. */ | |
2068 | gdb_assert (iterate_over_lwps (running_callback, NULL)); | |
2069 | ||
2070 | /* Discard the event. */ | |
2071 | status = 0; | |
2072 | continue; | |
2073 | } | |
2074 | ||
2075 | /* Make sure we don't report a SIGSTOP that we sent | |
2076 | ourselves in an attempt to stop an LWP. */ | |
2077 | if (lp->signalled | |
2078 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) | |
2079 | { | |
2080 | if (debug_linux_nat) | |
2081 | fprintf_unfiltered (gdb_stdlog, | |
2082 | "LLW: Delayed SIGSTOP caught for %s.\n", | |
2083 | target_pid_to_str (lp->ptid)); | |
2084 | ||
2085 | /* This is a delayed SIGSTOP. */ | |
2086 | lp->signalled = 0; | |
2087 | ||
2088 | registers_changed (); | |
2089 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, | |
2090 | TARGET_SIGNAL_0); | |
2091 | if (debug_linux_nat) | |
2092 | fprintf_unfiltered (gdb_stdlog, | |
2093 | "LLW: %s %s, 0, 0 (discard SIGSTOP)\n", | |
2094 | lp->step ? | |
2095 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
2096 | target_pid_to_str (lp->ptid)); | |
2097 | ||
2098 | lp->stopped = 0; | |
2099 | gdb_assert (lp->resumed); | |
2100 | ||
2101 | /* Discard the event. */ | |
2102 | status = 0; | |
2103 | continue; | |
2104 | } | |
2105 | ||
2106 | break; | |
2107 | } | |
2108 | ||
2109 | if (pid == -1) | |
2110 | { | |
2111 | /* Alternate between checking cloned and uncloned processes. */ | |
2112 | options ^= __WCLONE; | |
2113 | ||
2114 | /* And suspend every time we have checked both. */ | |
2115 | if (options & __WCLONE) | |
2116 | sigsuspend (&suspend_mask); | |
2117 | } | |
2118 | ||
2119 | /* We shouldn't end up here unless we want to try again. */ | |
2120 | gdb_assert (status == 0); | |
2121 | } | |
2122 | ||
2123 | clear_sigio_trap (); | |
2124 | clear_sigint_trap (); | |
2125 | ||
2126 | gdb_assert (lp); | |
2127 | ||
2128 | /* Don't report signals that GDB isn't interested in, such as | |
2129 | signals that are neither printed nor stopped upon. Stopping all | |
2130 | threads can be a bit time-consuming so if we want decent | |
2131 | performance with heavily multi-threaded programs, especially when | |
2132 | they're using a high frequency timer, we'd better avoid it if we | |
2133 | can. */ | |
2134 | ||
2135 | if (WIFSTOPPED (status)) | |
2136 | { | |
2137 | int signo = target_signal_from_host (WSTOPSIG (status)); | |
2138 | ||
2139 | if (signal_stop_state (signo) == 0 | |
2140 | && signal_print_state (signo) == 0 | |
2141 | && signal_pass_state (signo) == 1) | |
2142 | { | |
2143 | /* FIMXE: kettenis/2001-06-06: Should we resume all threads | |
2144 | here? It is not clear we should. GDB may not expect | |
2145 | other threads to run. On the other hand, not resuming | |
2146 | newly attached threads may cause an unwanted delay in | |
2147 | getting them running. */ | |
2148 | registers_changed (); | |
2149 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo); | |
2150 | if (debug_linux_nat) | |
2151 | fprintf_unfiltered (gdb_stdlog, | |
2152 | "LLW: %s %s, %s (preempt 'handle')\n", | |
2153 | lp->step ? | |
2154 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
2155 | target_pid_to_str (lp->ptid), | |
2156 | signo ? strsignal (signo) : "0"); | |
2157 | lp->stopped = 0; | |
2158 | status = 0; | |
2159 | goto retry; | |
2160 | } | |
2161 | ||
2162 | if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0) | |
2163 | { | |
2164 | /* If ^C/BREAK is typed at the tty/console, SIGINT gets | |
2165 | forwarded to the entire process group, that is, all LWP's | |
2166 | will receive it. Since we only want to report it once, | |
2167 | we try to flush it from all LWPs except this one. */ | |
2168 | sigaddset (&flush_mask, SIGINT); | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | /* This LWP is stopped now. */ | |
2173 | lp->stopped = 1; | |
2174 | ||
2175 | if (debug_linux_nat) | |
2176 | fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n", | |
2177 | status_to_str (status), target_pid_to_str (lp->ptid)); | |
2178 | ||
2179 | /* Now stop all other LWP's ... */ | |
2180 | iterate_over_lwps (stop_callback, NULL); | |
2181 | ||
2182 | /* ... and wait until all of them have reported back that they're no | |
2183 | longer running. */ | |
2184 | iterate_over_lwps (stop_wait_callback, &flush_mask); | |
2185 | iterate_over_lwps (flush_callback, &flush_mask); | |
2186 | ||
2187 | /* If we're not waiting for a specific LWP, choose an event LWP from | |
2188 | among those that have had events. Giving equal priority to all | |
2189 | LWPs that have had events helps prevent starvation. */ | |
2190 | if (pid == -1) | |
2191 | select_event_lwp (&lp, &status); | |
2192 | ||
2193 | /* Now that we've selected our final event LWP, cancel any | |
2194 | breakpoints in other LWPs that have hit a GDB breakpoint. See | |
2195 | the comment in cancel_breakpoints_callback to find out why. */ | |
2196 | iterate_over_lwps (cancel_breakpoints_callback, lp); | |
2197 | ||
2198 | /* If we're not running in "threaded" mode, we'll report the bare | |
2199 | process id. */ | |
2200 | ||
2201 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) | |
2202 | { | |
2203 | trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); | |
2204 | if (debug_linux_nat) | |
2205 | fprintf_unfiltered (gdb_stdlog, | |
2206 | "LLW: trap_ptid is %s.\n", | |
2207 | target_pid_to_str (trap_ptid)); | |
2208 | } | |
2209 | else | |
2210 | trap_ptid = null_ptid; | |
2211 | ||
2212 | if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE) | |
2213 | { | |
2214 | *ourstatus = lp->waitstatus; | |
2215 | lp->waitstatus.kind = TARGET_WAITKIND_IGNORE; | |
2216 | } | |
2217 | else | |
2218 | store_waitstatus (ourstatus, status); | |
2219 | ||
2220 | return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); | |
2221 | } | |
2222 | ||
2223 | static int | |
2224 | kill_callback (struct lwp_info *lp, void *data) | |
2225 | { | |
2226 | errno = 0; | |
2227 | ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); | |
2228 | if (debug_linux_nat) | |
2229 | fprintf_unfiltered (gdb_stdlog, | |
2230 | "KC: PTRACE_KILL %s, 0, 0 (%s)\n", | |
2231 | target_pid_to_str (lp->ptid), | |
2232 | errno ? safe_strerror (errno) : "OK"); | |
2233 | ||
2234 | return 0; | |
2235 | } | |
2236 | ||
2237 | static int | |
2238 | kill_wait_callback (struct lwp_info *lp, void *data) | |
2239 | { | |
2240 | pid_t pid; | |
2241 | ||
2242 | /* We must make sure that there are no pending events (delayed | |
2243 | SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current | |
2244 | program doesn't interfere with any following debugging session. */ | |
2245 | ||
2246 | /* For cloned processes we must check both with __WCLONE and | |
2247 | without, since the exit status of a cloned process isn't reported | |
2248 | with __WCLONE. */ | |
2249 | if (lp->cloned) | |
2250 | { | |
2251 | do | |
2252 | { | |
2253 | pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); | |
2254 | if (pid != (pid_t) -1 && debug_linux_nat) | |
2255 | { | |
2256 | fprintf_unfiltered (gdb_stdlog, | |
2257 | "KWC: wait %s received unknown.\n", | |
2258 | target_pid_to_str (lp->ptid)); | |
2259 | } | |
2260 | } | |
2261 | while (pid == GET_LWP (lp->ptid)); | |
2262 | ||
2263 | gdb_assert (pid == -1 && errno == ECHILD); | |
2264 | } | |
2265 | ||
2266 | do | |
2267 | { | |
2268 | pid = waitpid (GET_LWP (lp->ptid), NULL, 0); | |
2269 | if (pid != (pid_t) -1 && debug_linux_nat) | |
2270 | { | |
2271 | fprintf_unfiltered (gdb_stdlog, | |
2272 | "KWC: wait %s received unk.\n", | |
2273 | target_pid_to_str (lp->ptid)); | |
2274 | } | |
2275 | } | |
2276 | while (pid == GET_LWP (lp->ptid)); | |
2277 | ||
2278 | gdb_assert (pid == -1 && errno == ECHILD); | |
2279 | return 0; | |
2280 | } | |
2281 | ||
2282 | static void | |
2283 | linux_nat_kill (void) | |
2284 | { | |
2285 | /* Kill all LWP's ... */ | |
2286 | iterate_over_lwps (kill_callback, NULL); | |
2287 | ||
2288 | /* ... and wait until we've flushed all events. */ | |
2289 | iterate_over_lwps (kill_wait_callback, NULL); | |
2290 | ||
2291 | target_mourn_inferior (); | |
2292 | } | |
2293 | ||
2294 | static void | |
2295 | linux_nat_create_inferior (char *exec_file, char *allargs, char **env, | |
2296 | int from_tty) | |
2297 | { | |
1df84f13 | 2298 | deprecated_child_ops.to_create_inferior (exec_file, allargs, env, from_tty); |
d6b0e80f AC |
2299 | } |
2300 | ||
2301 | static void | |
2302 | linux_nat_mourn_inferior (void) | |
2303 | { | |
2304 | trap_ptid = null_ptid; | |
2305 | ||
2306 | /* Destroy LWP info; it's no longer valid. */ | |
2307 | init_lwp_list (); | |
2308 | ||
2309 | /* Restore the original signal mask. */ | |
2310 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); | |
2311 | sigemptyset (&blocked_mask); | |
2312 | ||
1df84f13 | 2313 | deprecated_child_ops.to_mourn_inferior (); |
d6b0e80f AC |
2314 | } |
2315 | ||
2316 | static int | |
2317 | linux_nat_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, | |
2318 | struct mem_attrib *attrib, struct target_ops *target) | |
2319 | { | |
2320 | struct cleanup *old_chain = save_inferior_ptid (); | |
2321 | int xfer; | |
2322 | ||
2323 | if (is_lwp (inferior_ptid)) | |
2324 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); | |
2325 | ||
2326 | xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target); | |
2327 | if (xfer == 0) | |
2328 | xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target); | |
2329 | ||
2330 | do_cleanups (old_chain); | |
2331 | return xfer; | |
2332 | } | |
2333 | ||
2334 | static int | |
2335 | linux_nat_thread_alive (ptid_t ptid) | |
2336 | { | |
2337 | gdb_assert (is_lwp (ptid)); | |
2338 | ||
2339 | errno = 0; | |
2340 | ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); | |
2341 | if (debug_linux_nat) | |
2342 | fprintf_unfiltered (gdb_stdlog, | |
2343 | "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n", | |
2344 | target_pid_to_str (ptid), | |
2345 | errno ? safe_strerror (errno) : "OK"); | |
2346 | if (errno) | |
2347 | return 0; | |
2348 | ||
2349 | return 1; | |
2350 | } | |
2351 | ||
2352 | static char * | |
2353 | linux_nat_pid_to_str (ptid_t ptid) | |
2354 | { | |
2355 | static char buf[64]; | |
2356 | ||
2357 | if (is_lwp (ptid)) | |
2358 | { | |
2359 | snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); | |
2360 | return buf; | |
2361 | } | |
2362 | ||
2363 | return normal_pid_to_str (ptid); | |
2364 | } | |
2365 | ||
2366 | static void | |
2367 | init_linux_nat_ops (void) | |
2368 | { | |
2369 | #if 0 | |
2370 | linux_nat_ops.to_open = linux_nat_open; | |
2371 | #endif | |
2372 | linux_nat_ops.to_shortname = "lwp-layer"; | |
2373 | linux_nat_ops.to_longname = "lwp-layer"; | |
2374 | linux_nat_ops.to_doc = "Low level threads support (LWP layer)"; | |
2375 | linux_nat_ops.to_attach = linux_nat_attach; | |
2376 | linux_nat_ops.to_detach = linux_nat_detach; | |
2377 | linux_nat_ops.to_resume = linux_nat_resume; | |
2378 | linux_nat_ops.to_wait = linux_nat_wait; | |
2379 | /* fetch_inferior_registers and store_inferior_registers will | |
2380 | honor the LWP id, so we can use them directly. */ | |
2381 | linux_nat_ops.to_fetch_registers = fetch_inferior_registers; | |
2382 | linux_nat_ops.to_store_registers = store_inferior_registers; | |
c8e73a31 | 2383 | linux_nat_ops.deprecated_xfer_memory = linux_nat_xfer_memory; |
d6b0e80f AC |
2384 | linux_nat_ops.to_kill = linux_nat_kill; |
2385 | linux_nat_ops.to_create_inferior = linux_nat_create_inferior; | |
2386 | linux_nat_ops.to_mourn_inferior = linux_nat_mourn_inferior; | |
2387 | linux_nat_ops.to_thread_alive = linux_nat_thread_alive; | |
2388 | linux_nat_ops.to_pid_to_str = linux_nat_pid_to_str; | |
2389 | linux_nat_ops.to_post_startup_inferior = child_post_startup_inferior; | |
2390 | linux_nat_ops.to_post_attach = child_post_attach; | |
2391 | linux_nat_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint; | |
2392 | linux_nat_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint; | |
2393 | linux_nat_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint; | |
2394 | ||
2395 | linux_nat_ops.to_stratum = thread_stratum; | |
2396 | linux_nat_ops.to_has_thread_control = tc_schedlock; | |
2397 | linux_nat_ops.to_magic = OPS_MAGIC; | |
2398 | } | |
2399 | ||
2400 | static void | |
2401 | sigchld_handler (int signo) | |
2402 | { | |
2403 | /* Do nothing. The only reason for this handler is that it allows | |
2404 | us to use sigsuspend in linux_nat_wait above to wait for the | |
2405 | arrival of a SIGCHLD. */ | |
2406 | } | |
2407 | ||
dba24537 AC |
2408 | /* Accepts an integer PID; Returns a string representing a file that |
2409 | can be opened to get the symbols for the child process. */ | |
2410 | ||
2411 | char * | |
2412 | child_pid_to_exec_file (int pid) | |
2413 | { | |
2414 | char *name1, *name2; | |
2415 | ||
2416 | name1 = xmalloc (MAXPATHLEN); | |
2417 | name2 = xmalloc (MAXPATHLEN); | |
2418 | make_cleanup (xfree, name1); | |
2419 | make_cleanup (xfree, name2); | |
2420 | memset (name2, 0, MAXPATHLEN); | |
2421 | ||
2422 | sprintf (name1, "/proc/%d/exe", pid); | |
2423 | if (readlink (name1, name2, MAXPATHLEN) > 0) | |
2424 | return name2; | |
2425 | else | |
2426 | return name1; | |
2427 | } | |
2428 | ||
2429 | /* Service function for corefiles and info proc. */ | |
2430 | ||
2431 | static int | |
2432 | read_mapping (FILE *mapfile, | |
2433 | long long *addr, | |
2434 | long long *endaddr, | |
2435 | char *permissions, | |
2436 | long long *offset, | |
2437 | char *device, long long *inode, char *filename) | |
2438 | { | |
2439 | int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx", | |
2440 | addr, endaddr, permissions, offset, device, inode); | |
2441 | ||
2442 | if (ret > 0 && ret != EOF && *inode != 0) | |
2443 | { | |
2444 | /* Eat everything up to EOL for the filename. This will prevent | |
2445 | weird filenames (such as one with embedded whitespace) from | |
2446 | confusing this code. It also makes this code more robust in | |
2447 | respect to annotations the kernel may add after the filename. | |
2448 | ||
2449 | Note the filename is used for informational purposes | |
2450 | only. */ | |
2451 | ret += fscanf (mapfile, "%[^\n]\n", filename); | |
2452 | } | |
2453 | else | |
2454 | { | |
2455 | filename[0] = '\0'; /* no filename */ | |
2456 | fscanf (mapfile, "\n"); | |
2457 | } | |
2458 | return (ret != 0 && ret != EOF); | |
2459 | } | |
2460 | ||
2461 | /* Fills the "to_find_memory_regions" target vector. Lists the memory | |
2462 | regions in the inferior for a corefile. */ | |
2463 | ||
2464 | static int | |
2465 | linux_nat_find_memory_regions (int (*func) (CORE_ADDR, | |
2466 | unsigned long, | |
2467 | int, int, int, void *), void *obfd) | |
2468 | { | |
2469 | long long pid = PIDGET (inferior_ptid); | |
2470 | char mapsfilename[MAXPATHLEN]; | |
2471 | FILE *mapsfile; | |
2472 | long long addr, endaddr, size, offset, inode; | |
2473 | char permissions[8], device[8], filename[MAXPATHLEN]; | |
2474 | int read, write, exec; | |
2475 | int ret; | |
2476 | ||
2477 | /* Compose the filename for the /proc memory map, and open it. */ | |
2478 | sprintf (mapsfilename, "/proc/%lld/maps", pid); | |
2479 | if ((mapsfile = fopen (mapsfilename, "r")) == NULL) | |
2480 | error ("Could not open %s\n", mapsfilename); | |
2481 | ||
2482 | if (info_verbose) | |
2483 | fprintf_filtered (gdb_stdout, | |
2484 | "Reading memory regions from %s\n", mapsfilename); | |
2485 | ||
2486 | /* Now iterate until end-of-file. */ | |
2487 | while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0], | |
2488 | &offset, &device[0], &inode, &filename[0])) | |
2489 | { | |
2490 | size = endaddr - addr; | |
2491 | ||
2492 | /* Get the segment's permissions. */ | |
2493 | read = (strchr (permissions, 'r') != 0); | |
2494 | write = (strchr (permissions, 'w') != 0); | |
2495 | exec = (strchr (permissions, 'x') != 0); | |
2496 | ||
2497 | if (info_verbose) | |
2498 | { | |
2499 | fprintf_filtered (gdb_stdout, | |
2500 | "Save segment, %lld bytes at 0x%s (%c%c%c)", | |
2501 | size, paddr_nz (addr), | |
2502 | read ? 'r' : ' ', | |
2503 | write ? 'w' : ' ', exec ? 'x' : ' '); | |
2504 | if (filename && filename[0]) | |
2505 | fprintf_filtered (gdb_stdout, " for %s", filename); | |
2506 | fprintf_filtered (gdb_stdout, "\n"); | |
2507 | } | |
2508 | ||
2509 | /* Invoke the callback function to create the corefile | |
2510 | segment. */ | |
2511 | func (addr, size, read, write, exec, obfd); | |
2512 | } | |
2513 | fclose (mapsfile); | |
2514 | return 0; | |
2515 | } | |
2516 | ||
2517 | /* Records the thread's register state for the corefile note | |
2518 | section. */ | |
2519 | ||
2520 | static char * | |
2521 | linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid, | |
2522 | char *note_data, int *note_size) | |
2523 | { | |
2524 | gdb_gregset_t gregs; | |
2525 | gdb_fpregset_t fpregs; | |
2526 | #ifdef FILL_FPXREGSET | |
2527 | gdb_fpxregset_t fpxregs; | |
2528 | #endif | |
2529 | unsigned long lwp = ptid_get_lwp (ptid); | |
2530 | ||
2531 | fill_gregset (&gregs, -1); | |
2532 | note_data = (char *) elfcore_write_prstatus (obfd, | |
2533 | note_data, | |
2534 | note_size, | |
2535 | lwp, | |
2536 | stop_signal, &gregs); | |
2537 | ||
2538 | fill_fpregset (&fpregs, -1); | |
2539 | note_data = (char *) elfcore_write_prfpreg (obfd, | |
2540 | note_data, | |
2541 | note_size, | |
2542 | &fpregs, sizeof (fpregs)); | |
2543 | #ifdef FILL_FPXREGSET | |
2544 | fill_fpxregset (&fpxregs, -1); | |
2545 | note_data = (char *) elfcore_write_prxfpreg (obfd, | |
2546 | note_data, | |
2547 | note_size, | |
2548 | &fpxregs, sizeof (fpxregs)); | |
2549 | #endif | |
2550 | return note_data; | |
2551 | } | |
2552 | ||
2553 | struct linux_nat_corefile_thread_data | |
2554 | { | |
2555 | bfd *obfd; | |
2556 | char *note_data; | |
2557 | int *note_size; | |
2558 | int num_notes; | |
2559 | }; | |
2560 | ||
2561 | /* Called by gdbthread.c once per thread. Records the thread's | |
2562 | register state for the corefile note section. */ | |
2563 | ||
2564 | static int | |
2565 | linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data) | |
2566 | { | |
2567 | struct linux_nat_corefile_thread_data *args = data; | |
2568 | ptid_t saved_ptid = inferior_ptid; | |
2569 | ||
2570 | inferior_ptid = ti->ptid; | |
2571 | registers_changed (); | |
2572 | target_fetch_registers (-1); /* FIXME should not be necessary; | |
2573 | fill_gregset should do it automatically. */ | |
2574 | args->note_data = linux_nat_do_thread_registers (args->obfd, | |
2575 | ti->ptid, | |
2576 | args->note_data, | |
2577 | args->note_size); | |
2578 | args->num_notes++; | |
2579 | inferior_ptid = saved_ptid; | |
2580 | registers_changed (); | |
2581 | target_fetch_registers (-1); /* FIXME should not be necessary; | |
2582 | fill_gregset should do it automatically. */ | |
2583 | return 0; | |
2584 | } | |
2585 | ||
2586 | /* Records the register state for the corefile note section. */ | |
2587 | ||
2588 | static char * | |
2589 | linux_nat_do_registers (bfd *obfd, ptid_t ptid, | |
2590 | char *note_data, int *note_size) | |
2591 | { | |
2592 | registers_changed (); | |
2593 | target_fetch_registers (-1); /* FIXME should not be necessary; | |
2594 | fill_gregset should do it automatically. */ | |
2595 | return linux_nat_do_thread_registers (obfd, | |
2596 | ptid_build (ptid_get_pid (inferior_ptid), | |
2597 | ptid_get_pid (inferior_ptid), | |
2598 | 0), | |
2599 | note_data, note_size); | |
2600 | return note_data; | |
2601 | } | |
2602 | ||
2603 | /* Fills the "to_make_corefile_note" target vector. Builds the note | |
2604 | section for a corefile, and returns it in a malloc buffer. */ | |
2605 | ||
2606 | static char * | |
2607 | linux_nat_make_corefile_notes (bfd *obfd, int *note_size) | |
2608 | { | |
2609 | struct linux_nat_corefile_thread_data thread_args; | |
2610 | struct cleanup *old_chain; | |
2611 | char fname[16] = { '\0' }; | |
2612 | char psargs[80] = { '\0' }; | |
2613 | char *note_data = NULL; | |
2614 | ptid_t current_ptid = inferior_ptid; | |
2615 | char *auxv; | |
2616 | int auxv_len; | |
2617 | ||
2618 | if (get_exec_file (0)) | |
2619 | { | |
2620 | strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); | |
2621 | strncpy (psargs, get_exec_file (0), sizeof (psargs)); | |
2622 | if (get_inferior_args ()) | |
2623 | { | |
2624 | strncat (psargs, " ", sizeof (psargs) - strlen (psargs)); | |
2625 | strncat (psargs, get_inferior_args (), | |
2626 | sizeof (psargs) - strlen (psargs)); | |
2627 | } | |
2628 | note_data = (char *) elfcore_write_prpsinfo (obfd, | |
2629 | note_data, | |
2630 | note_size, fname, psargs); | |
2631 | } | |
2632 | ||
2633 | /* Dump information for threads. */ | |
2634 | thread_args.obfd = obfd; | |
2635 | thread_args.note_data = note_data; | |
2636 | thread_args.note_size = note_size; | |
2637 | thread_args.num_notes = 0; | |
2638 | iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args); | |
2639 | if (thread_args.num_notes == 0) | |
2640 | { | |
2641 | /* iterate_over_threads didn't come up with any threads; just | |
2642 | use inferior_ptid. */ | |
2643 | note_data = linux_nat_do_registers (obfd, inferior_ptid, | |
2644 | note_data, note_size); | |
2645 | } | |
2646 | else | |
2647 | { | |
2648 | note_data = thread_args.note_data; | |
2649 | } | |
2650 | ||
2651 | auxv_len = target_auxv_read (¤t_target, &auxv); | |
2652 | if (auxv_len > 0) | |
2653 | { | |
2654 | note_data = elfcore_write_note (obfd, note_data, note_size, | |
2655 | "CORE", NT_AUXV, auxv, auxv_len); | |
2656 | xfree (auxv); | |
2657 | } | |
2658 | ||
2659 | make_cleanup (xfree, note_data); | |
2660 | return note_data; | |
2661 | } | |
2662 | ||
2663 | /* Implement the "info proc" command. */ | |
2664 | ||
2665 | static void | |
2666 | linux_nat_info_proc_cmd (char *args, int from_tty) | |
2667 | { | |
2668 | long long pid = PIDGET (inferior_ptid); | |
2669 | FILE *procfile; | |
2670 | char **argv = NULL; | |
2671 | char buffer[MAXPATHLEN]; | |
2672 | char fname1[MAXPATHLEN], fname2[MAXPATHLEN]; | |
2673 | int cmdline_f = 1; | |
2674 | int cwd_f = 1; | |
2675 | int exe_f = 1; | |
2676 | int mappings_f = 0; | |
2677 | int environ_f = 0; | |
2678 | int status_f = 0; | |
2679 | int stat_f = 0; | |
2680 | int all = 0; | |
2681 | struct stat dummy; | |
2682 | ||
2683 | if (args) | |
2684 | { | |
2685 | /* Break up 'args' into an argv array. */ | |
2686 | if ((argv = buildargv (args)) == NULL) | |
2687 | nomem (0); | |
2688 | else | |
2689 | make_cleanup_freeargv (argv); | |
2690 | } | |
2691 | while (argv != NULL && *argv != NULL) | |
2692 | { | |
2693 | if (isdigit (argv[0][0])) | |
2694 | { | |
2695 | pid = strtoul (argv[0], NULL, 10); | |
2696 | } | |
2697 | else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0) | |
2698 | { | |
2699 | mappings_f = 1; | |
2700 | } | |
2701 | else if (strcmp (argv[0], "status") == 0) | |
2702 | { | |
2703 | status_f = 1; | |
2704 | } | |
2705 | else if (strcmp (argv[0], "stat") == 0) | |
2706 | { | |
2707 | stat_f = 1; | |
2708 | } | |
2709 | else if (strcmp (argv[0], "cmd") == 0) | |
2710 | { | |
2711 | cmdline_f = 1; | |
2712 | } | |
2713 | else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0) | |
2714 | { | |
2715 | exe_f = 1; | |
2716 | } | |
2717 | else if (strcmp (argv[0], "cwd") == 0) | |
2718 | { | |
2719 | cwd_f = 1; | |
2720 | } | |
2721 | else if (strncmp (argv[0], "all", strlen (argv[0])) == 0) | |
2722 | { | |
2723 | all = 1; | |
2724 | } | |
2725 | else | |
2726 | { | |
2727 | /* [...] (future options here) */ | |
2728 | } | |
2729 | argv++; | |
2730 | } | |
2731 | if (pid == 0) | |
2732 | error ("No current process: you must name one."); | |
2733 | ||
2734 | sprintf (fname1, "/proc/%lld", pid); | |
2735 | if (stat (fname1, &dummy) != 0) | |
2736 | error ("No /proc directory: '%s'", fname1); | |
2737 | ||
2738 | printf_filtered ("process %lld\n", pid); | |
2739 | if (cmdline_f || all) | |
2740 | { | |
2741 | sprintf (fname1, "/proc/%lld/cmdline", pid); | |
2742 | if ((procfile = fopen (fname1, "r")) > 0) | |
2743 | { | |
2744 | fgets (buffer, sizeof (buffer), procfile); | |
2745 | printf_filtered ("cmdline = '%s'\n", buffer); | |
2746 | fclose (procfile); | |
2747 | } | |
2748 | else | |
2749 | warning ("unable to open /proc file '%s'", fname1); | |
2750 | } | |
2751 | if (cwd_f || all) | |
2752 | { | |
2753 | sprintf (fname1, "/proc/%lld/cwd", pid); | |
2754 | memset (fname2, 0, sizeof (fname2)); | |
2755 | if (readlink (fname1, fname2, sizeof (fname2)) > 0) | |
2756 | printf_filtered ("cwd = '%s'\n", fname2); | |
2757 | else | |
2758 | warning ("unable to read link '%s'", fname1); | |
2759 | } | |
2760 | if (exe_f || all) | |
2761 | { | |
2762 | sprintf (fname1, "/proc/%lld/exe", pid); | |
2763 | memset (fname2, 0, sizeof (fname2)); | |
2764 | if (readlink (fname1, fname2, sizeof (fname2)) > 0) | |
2765 | printf_filtered ("exe = '%s'\n", fname2); | |
2766 | else | |
2767 | warning ("unable to read link '%s'", fname1); | |
2768 | } | |
2769 | if (mappings_f || all) | |
2770 | { | |
2771 | sprintf (fname1, "/proc/%lld/maps", pid); | |
2772 | if ((procfile = fopen (fname1, "r")) > 0) | |
2773 | { | |
2774 | long long addr, endaddr, size, offset, inode; | |
2775 | char permissions[8], device[8], filename[MAXPATHLEN]; | |
2776 | ||
2777 | printf_filtered ("Mapped address spaces:\n\n"); | |
2778 | if (TARGET_ADDR_BIT == 32) | |
2779 | { | |
2780 | printf_filtered ("\t%10s %10s %10s %10s %7s\n", | |
2781 | "Start Addr", | |
2782 | " End Addr", | |
2783 | " Size", " Offset", "objfile"); | |
2784 | } | |
2785 | else | |
2786 | { | |
2787 | printf_filtered (" %18s %18s %10s %10s %7s\n", | |
2788 | "Start Addr", | |
2789 | " End Addr", | |
2790 | " Size", " Offset", "objfile"); | |
2791 | } | |
2792 | ||
2793 | while (read_mapping (procfile, &addr, &endaddr, &permissions[0], | |
2794 | &offset, &device[0], &inode, &filename[0])) | |
2795 | { | |
2796 | size = endaddr - addr; | |
2797 | ||
2798 | /* FIXME: carlton/2003-08-27: Maybe the printf_filtered | |
2799 | calls here (and possibly above) should be abstracted | |
2800 | out into their own functions? Andrew suggests using | |
2801 | a generic local_address_string instead to print out | |
2802 | the addresses; that makes sense to me, too. */ | |
2803 | ||
2804 | if (TARGET_ADDR_BIT == 32) | |
2805 | { | |
2806 | printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n", | |
2807 | (unsigned long) addr, /* FIXME: pr_addr */ | |
2808 | (unsigned long) endaddr, | |
2809 | (int) size, | |
2810 | (unsigned int) offset, | |
2811 | filename[0] ? filename : ""); | |
2812 | } | |
2813 | else | |
2814 | { | |
2815 | printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n", | |
2816 | (unsigned long) addr, /* FIXME: pr_addr */ | |
2817 | (unsigned long) endaddr, | |
2818 | (int) size, | |
2819 | (unsigned int) offset, | |
2820 | filename[0] ? filename : ""); | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | fclose (procfile); | |
2825 | } | |
2826 | else | |
2827 | warning ("unable to open /proc file '%s'", fname1); | |
2828 | } | |
2829 | if (status_f || all) | |
2830 | { | |
2831 | sprintf (fname1, "/proc/%lld/status", pid); | |
2832 | if ((procfile = fopen (fname1, "r")) > 0) | |
2833 | { | |
2834 | while (fgets (buffer, sizeof (buffer), procfile) != NULL) | |
2835 | puts_filtered (buffer); | |
2836 | fclose (procfile); | |
2837 | } | |
2838 | else | |
2839 | warning ("unable to open /proc file '%s'", fname1); | |
2840 | } | |
2841 | if (stat_f || all) | |
2842 | { | |
2843 | sprintf (fname1, "/proc/%lld/stat", pid); | |
2844 | if ((procfile = fopen (fname1, "r")) > 0) | |
2845 | { | |
2846 | int itmp; | |
2847 | char ctmp; | |
2848 | ||
2849 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2850 | printf_filtered ("Process: %d\n", itmp); | |
2851 | if (fscanf (procfile, "%s ", &buffer[0]) > 0) | |
2852 | printf_filtered ("Exec file: %s\n", buffer); | |
2853 | if (fscanf (procfile, "%c ", &ctmp) > 0) | |
2854 | printf_filtered ("State: %c\n", ctmp); | |
2855 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2856 | printf_filtered ("Parent process: %d\n", itmp); | |
2857 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2858 | printf_filtered ("Process group: %d\n", itmp); | |
2859 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2860 | printf_filtered ("Session id: %d\n", itmp); | |
2861 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2862 | printf_filtered ("TTY: %d\n", itmp); | |
2863 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2864 | printf_filtered ("TTY owner process group: %d\n", itmp); | |
2865 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2866 | printf_filtered ("Flags: 0x%x\n", itmp); | |
2867 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2868 | printf_filtered ("Minor faults (no memory page): %u\n", | |
2869 | (unsigned int) itmp); | |
2870 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2871 | printf_filtered ("Minor faults, children: %u\n", | |
2872 | (unsigned int) itmp); | |
2873 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2874 | printf_filtered ("Major faults (memory page faults): %u\n", | |
2875 | (unsigned int) itmp); | |
2876 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2877 | printf_filtered ("Major faults, children: %u\n", | |
2878 | (unsigned int) itmp); | |
2879 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2880 | printf_filtered ("utime: %d\n", itmp); | |
2881 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2882 | printf_filtered ("stime: %d\n", itmp); | |
2883 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2884 | printf_filtered ("utime, children: %d\n", itmp); | |
2885 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2886 | printf_filtered ("stime, children: %d\n", itmp); | |
2887 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2888 | printf_filtered ("jiffies remaining in current time slice: %d\n", | |
2889 | itmp); | |
2890 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2891 | printf_filtered ("'nice' value: %d\n", itmp); | |
2892 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2893 | printf_filtered ("jiffies until next timeout: %u\n", | |
2894 | (unsigned int) itmp); | |
2895 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2896 | printf_filtered ("jiffies until next SIGALRM: %u\n", | |
2897 | (unsigned int) itmp); | |
2898 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2899 | printf_filtered ("start time (jiffies since system boot): %d\n", | |
2900 | itmp); | |
2901 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2902 | printf_filtered ("Virtual memory size: %u\n", | |
2903 | (unsigned int) itmp); | |
2904 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2905 | printf_filtered ("Resident set size: %u\n", (unsigned int) itmp); | |
2906 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2907 | printf_filtered ("rlim: %u\n", (unsigned int) itmp); | |
2908 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2909 | printf_filtered ("Start of text: 0x%x\n", itmp); | |
2910 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2911 | printf_filtered ("End of text: 0x%x\n", itmp); | |
2912 | if (fscanf (procfile, "%u ", &itmp) > 0) | |
2913 | printf_filtered ("Start of stack: 0x%x\n", itmp); | |
2914 | #if 0 /* Don't know how architecture-dependent the rest is... | |
2915 | Anyway the signal bitmap info is available from "status". */ | |
2916 | if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ | |
2917 | printf_filtered ("Kernel stack pointer: 0x%x\n", itmp); | |
2918 | if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ | |
2919 | printf_filtered ("Kernel instr pointer: 0x%x\n", itmp); | |
2920 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2921 | printf_filtered ("Pending signals bitmap: 0x%x\n", itmp); | |
2922 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2923 | printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp); | |
2924 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2925 | printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp); | |
2926 | if (fscanf (procfile, "%d ", &itmp) > 0) | |
2927 | printf_filtered ("Catched signals bitmap: 0x%x\n", itmp); | |
2928 | if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ | |
2929 | printf_filtered ("wchan (system call): 0x%x\n", itmp); | |
2930 | #endif | |
2931 | fclose (procfile); | |
2932 | } | |
2933 | else | |
2934 | warning ("unable to open /proc file '%s'", fname1); | |
2935 | } | |
2936 | } | |
2937 | ||
2938 | int | |
2939 | linux_proc_xfer_memory (CORE_ADDR addr, char *myaddr, int len, int write, | |
2940 | struct mem_attrib *attrib, struct target_ops *target) | |
2941 | { | |
2942 | int fd, ret; | |
2943 | char filename[64]; | |
2944 | ||
2945 | if (write) | |
2946 | return 0; | |
2947 | ||
2948 | /* Don't bother for one word. */ | |
2949 | if (len < 3 * sizeof (long)) | |
2950 | return 0; | |
2951 | ||
2952 | /* We could keep this file open and cache it - possibly one per | |
2953 | thread. That requires some juggling, but is even faster. */ | |
2954 | sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid)); | |
2955 | fd = open (filename, O_RDONLY | O_LARGEFILE); | |
2956 | if (fd == -1) | |
2957 | return 0; | |
2958 | ||
2959 | /* If pread64 is available, use it. It's faster if the kernel | |
2960 | supports it (only one syscall), and it's 64-bit safe even on | |
2961 | 32-bit platforms (for instance, SPARC debugging a SPARC64 | |
2962 | application). */ | |
2963 | #ifdef HAVE_PREAD64 | |
2964 | if (pread64 (fd, myaddr, len, addr) != len) | |
2965 | #else | |
2966 | if (lseek (fd, addr, SEEK_SET) == -1 || read (fd, myaddr, len) != len) | |
2967 | #endif | |
2968 | ret = 0; | |
2969 | else | |
2970 | ret = len; | |
2971 | ||
2972 | close (fd); | |
2973 | return ret; | |
2974 | } | |
2975 | ||
2976 | /* Parse LINE as a signal set and add its set bits to SIGS. */ | |
2977 | ||
2978 | static void | |
2979 | add_line_to_sigset (const char *line, sigset_t *sigs) | |
2980 | { | |
2981 | int len = strlen (line) - 1; | |
2982 | const char *p; | |
2983 | int signum; | |
2984 | ||
2985 | if (line[len] != '\n') | |
2986 | error ("Could not parse signal set: %s", line); | |
2987 | ||
2988 | p = line; | |
2989 | signum = len * 4; | |
2990 | while (len-- > 0) | |
2991 | { | |
2992 | int digit; | |
2993 | ||
2994 | if (*p >= '0' && *p <= '9') | |
2995 | digit = *p - '0'; | |
2996 | else if (*p >= 'a' && *p <= 'f') | |
2997 | digit = *p - 'a' + 10; | |
2998 | else | |
2999 | error ("Could not parse signal set: %s", line); | |
3000 | ||
3001 | signum -= 4; | |
3002 | ||
3003 | if (digit & 1) | |
3004 | sigaddset (sigs, signum + 1); | |
3005 | if (digit & 2) | |
3006 | sigaddset (sigs, signum + 2); | |
3007 | if (digit & 4) | |
3008 | sigaddset (sigs, signum + 3); | |
3009 | if (digit & 8) | |
3010 | sigaddset (sigs, signum + 4); | |
3011 | ||
3012 | p++; | |
3013 | } | |
3014 | } | |
3015 | ||
3016 | /* Find process PID's pending signals from /proc/pid/status and set | |
3017 | SIGS to match. */ | |
3018 | ||
3019 | void | |
3020 | linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored) | |
3021 | { | |
3022 | FILE *procfile; | |
3023 | char buffer[MAXPATHLEN], fname[MAXPATHLEN]; | |
3024 | int signum; | |
3025 | ||
3026 | sigemptyset (pending); | |
3027 | sigemptyset (blocked); | |
3028 | sigemptyset (ignored); | |
3029 | sprintf (fname, "/proc/%d/status", pid); | |
3030 | procfile = fopen (fname, "r"); | |
3031 | if (procfile == NULL) | |
3032 | error ("Could not open %s", fname); | |
3033 | ||
3034 | while (fgets (buffer, MAXPATHLEN, procfile) != NULL) | |
3035 | { | |
3036 | /* Normal queued signals are on the SigPnd line in the status | |
3037 | file. However, 2.6 kernels also have a "shared" pending | |
3038 | queue for delivering signals to a thread group, so check for | |
3039 | a ShdPnd line also. | |
3040 | ||
3041 | Unfortunately some Red Hat kernels include the shared pending | |
3042 | queue but not the ShdPnd status field. */ | |
3043 | ||
3044 | if (strncmp (buffer, "SigPnd:\t", 8) == 0) | |
3045 | add_line_to_sigset (buffer + 8, pending); | |
3046 | else if (strncmp (buffer, "ShdPnd:\t", 8) == 0) | |
3047 | add_line_to_sigset (buffer + 8, pending); | |
3048 | else if (strncmp (buffer, "SigBlk:\t", 8) == 0) | |
3049 | add_line_to_sigset (buffer + 8, blocked); | |
3050 | else if (strncmp (buffer, "SigIgn:\t", 8) == 0) | |
3051 | add_line_to_sigset (buffer + 8, ignored); | |
3052 | } | |
3053 | ||
3054 | fclose (procfile); | |
3055 | } | |
3056 | ||
d6b0e80f AC |
3057 | void |
3058 | _initialize_linux_nat (void) | |
3059 | { | |
3060 | struct sigaction action; | |
d6b0e80f | 3061 | extern void thread_db_init (struct target_ops *); |
dba24537 | 3062 | |
146c42e3 JB |
3063 | deprecated_child_ops.to_find_memory_regions = linux_nat_find_memory_regions; |
3064 | deprecated_child_ops.to_make_corefile_notes = linux_nat_make_corefile_notes; | |
dba24537 AC |
3065 | |
3066 | add_info ("proc", linux_nat_info_proc_cmd, | |
3067 | "Show /proc process information about any running process.\n\ | |
3068 | Specify any process id, or use the program being debugged by default.\n\ | |
3069 | Specify any of the following keywords for detailed info:\n\ | |
3070 | mappings -- list of mapped memory regions.\n\ | |
3071 | stat -- list a bunch of random process info.\n\ | |
3072 | status -- list a different bunch of random process info.\n\ | |
3073 | all -- list all available /proc info."); | |
d6b0e80f AC |
3074 | |
3075 | init_linux_nat_ops (); | |
3076 | add_target (&linux_nat_ops); | |
3077 | thread_db_init (&linux_nat_ops); | |
3078 | ||
3079 | /* Save the original signal mask. */ | |
3080 | sigprocmask (SIG_SETMASK, NULL, &normal_mask); | |
3081 | ||
3082 | action.sa_handler = sigchld_handler; | |
3083 | sigemptyset (&action.sa_mask); | |
3084 | action.sa_flags = 0; | |
3085 | sigaction (SIGCHLD, &action, NULL); | |
3086 | ||
3087 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ | |
3088 | sigprocmask (SIG_SETMASK, NULL, &suspend_mask); | |
3089 | sigdelset (&suspend_mask, SIGCHLD); | |
3090 | ||
3091 | sigemptyset (&blocked_mask); | |
3092 | ||
3093 | deprecated_add_show_from_set | |
3094 | (add_set_cmd ("lin-lwp", no_class, var_zinteger, | |
3095 | (char *) &debug_linux_nat, | |
3096 | "Set debugging of GNU/Linux lwp module.\n\ | |
3097 | Enables printf debugging output.\n", &setdebuglist), &showdebuglist); | |
3098 | } | |
3099 | \f | |
3100 | ||
3101 | /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to | |
3102 | the GNU/Linux Threads library and therefore doesn't really belong | |
3103 | here. */ | |
3104 | ||
3105 | /* Read variable NAME in the target and return its value if found. | |
3106 | Otherwise return zero. It is assumed that the type of the variable | |
3107 | is `int'. */ | |
3108 | ||
3109 | static int | |
3110 | get_signo (const char *name) | |
3111 | { | |
3112 | struct minimal_symbol *ms; | |
3113 | int signo; | |
3114 | ||
3115 | ms = lookup_minimal_symbol (name, NULL, NULL); | |
3116 | if (ms == NULL) | |
3117 | return 0; | |
3118 | ||
3119 | if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo, | |
3120 | sizeof (signo)) != 0) | |
3121 | return 0; | |
3122 | ||
3123 | return signo; | |
3124 | } | |
3125 | ||
3126 | /* Return the set of signals used by the threads library in *SET. */ | |
3127 | ||
3128 | void | |
3129 | lin_thread_get_thread_signals (sigset_t *set) | |
3130 | { | |
3131 | struct sigaction action; | |
3132 | int restart, cancel; | |
3133 | ||
3134 | sigemptyset (set); | |
3135 | ||
3136 | restart = get_signo ("__pthread_sig_restart"); | |
3137 | if (restart == 0) | |
3138 | return; | |
3139 | ||
3140 | cancel = get_signo ("__pthread_sig_cancel"); | |
3141 | if (cancel == 0) | |
3142 | return; | |
3143 | ||
3144 | sigaddset (set, restart); | |
3145 | sigaddset (set, cancel); | |
3146 | ||
3147 | /* The GNU/Linux Threads library makes terminating threads send a | |
3148 | special "cancel" signal instead of SIGCHLD. Make sure we catch | |
3149 | those (to prevent them from terminating GDB itself, which is | |
3150 | likely to be their default action) and treat them the same way as | |
3151 | SIGCHLD. */ | |
3152 | ||
3153 | action.sa_handler = sigchld_handler; | |
3154 | sigemptyset (&action.sa_mask); | |
3155 | action.sa_flags = 0; | |
3156 | sigaction (cancel, &action, NULL); | |
3157 | ||
3158 | /* We block the "cancel" signal throughout this code ... */ | |
3159 | sigaddset (&blocked_mask, cancel); | |
3160 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
3161 | ||
3162 | /* ... except during a sigsuspend. */ | |
3163 | sigdelset (&suspend_mask, cancel); | |
3164 | } |