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