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