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