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