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