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