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