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