Commit | Line | Data |
---|---|---|
8605d56e | 1 | /* Multi-threaded debugging support for GNU/Linux (LWP layer). |
b3ba1b44 | 2 | Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
fb0e1ba7 MK |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | ||
23 | #include "gdb_assert.h" | |
309367d4 | 24 | #include "gdb_string.h" |
fb0e1ba7 MK |
25 | #include <errno.h> |
26 | #include <signal.h> | |
b757528f JJ |
27 | #ifdef HAVE_TKILL_SYSCALL |
28 | #include <unistd.h> | |
29 | #include <sys/syscall.h> | |
30 | #endif | |
fb0e1ba7 MK |
31 | #include <sys/ptrace.h> |
32 | #include "gdb_wait.h" | |
33 | ||
34 | #include "gdbthread.h" | |
35 | #include "inferior.h" | |
36 | #include "target.h" | |
4e052eda | 37 | #include "regcache.h" |
7ca673cd | 38 | #include "gdbcmd.h" |
fb0e1ba7 | 39 | |
7ca673cd | 40 | static int debug_lin_lwp; |
1b84163e | 41 | extern char *strsignal (int sig); |
fb0e1ba7 | 42 | |
8605d56e AC |
43 | /* On GNU/Linux there are no real LWP's. The closest thing to LWP's |
44 | are processes sharing the same VM space. A multi-threaded process | |
45 | is basically a group of such processes. However, such a grouping | |
46 | is almost entirely a user-space issue; the kernel doesn't enforce | |
47 | such a grouping at all (this might change in the future). In | |
48 | general, we'll rely on the threads library (i.e. the GNU/Linux | |
49 | Threads library) to provide such a grouping. | |
fb0e1ba7 MK |
50 | |
51 | It is perfectly well possible to write a multi-threaded application | |
52 | without the assistance of a threads library, by using the clone | |
53 | system call directly. This module should be able to give some | |
54 | rudimentary support for debugging such applications if developers | |
55 | specify the CLONE_PTRACE flag in the clone system call, and are | |
8605d56e | 56 | using the Linux kernel 2.4 or above. |
fb0e1ba7 | 57 | |
8605d56e AC |
58 | Note that there are some peculiarities in GNU/Linux that affect |
59 | this code: | |
fb0e1ba7 MK |
60 | |
61 | - In general one should specify the __WCLONE flag to waitpid in | |
3f07c44b MK |
62 | order to make it report events for any of the cloned processes |
63 | (and leave it out for the initial process). However, if a cloned | |
64 | process has exited the exit status is only reported if the | |
8605d56e AC |
65 | __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but |
66 | we cannot use it since GDB must work on older systems too. | |
fb0e1ba7 MK |
67 | |
68 | - When a traced, cloned process exits and is waited for by the | |
4c8de859 | 69 | debugger, the kernel reassigns it to the original parent and |
8605d56e AC |
70 | keeps it around as a "zombie". Somehow, the GNU/Linux Threads |
71 | library doesn't notice this, which leads to the "zombie problem": | |
72 | When debugged a multi-threaded process that spawns a lot of | |
73 | threads will run out of processes, even if the threads exit, | |
74 | because the "zombies" stay around. */ | |
fb0e1ba7 MK |
75 | |
76 | /* Structure describing a LWP. */ | |
77 | struct lwp_info | |
78 | { | |
79 | /* The process id of the LWP. This is a combination of the LWP id | |
80 | and overall process id. */ | |
39f77062 | 81 | ptid_t ptid; |
fb0e1ba7 | 82 | |
cacab7c4 MK |
83 | /* Non-zero if this LWP is cloned. In this context "cloned" means |
84 | that the LWP is reporting to its parent using a signal other than | |
85 | SIGCHLD. */ | |
86 | int cloned; | |
87 | ||
fb0e1ba7 MK |
88 | /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report |
89 | it back yet). */ | |
90 | int signalled; | |
91 | ||
92 | /* Non-zero if this LWP is stopped. */ | |
93 | int stopped; | |
94 | ||
fce0e6e1 MK |
95 | /* Non-zero if this LWP will be/has been resumed. Note that an LWP |
96 | can be marked both as stopped and resumed at the same time. This | |
97 | happens if we try to resume an LWP that has a wait status | |
98 | pending. We shouldn't let the LWP run until that wait status has | |
99 | been processed, but we should not report that wait status if GDB | |
100 | didn't try to let the LWP run. */ | |
101 | int resumed; | |
102 | ||
fb0e1ba7 MK |
103 | /* If non-zero, a pending wait status. */ |
104 | int status; | |
105 | ||
106 | /* Non-zero if we were stepping this LWP. */ | |
107 | int step; | |
108 | ||
109 | /* Next LWP in list. */ | |
110 | struct lwp_info *next; | |
111 | }; | |
112 | ||
113 | /* List of known LWPs. */ | |
114 | static struct lwp_info *lwp_list; | |
115 | ||
116 | /* Number of LWPs in the list. */ | |
117 | static int num_lwps; | |
118 | ||
119 | /* Non-zero if we're running in "threaded" mode. */ | |
120 | static int threaded; | |
121 | \f | |
122 | ||
ca6724c1 KB |
123 | #define GET_LWP(ptid) ptid_get_lwp (ptid) |
124 | #define GET_PID(ptid) ptid_get_pid (ptid) | |
125 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) | |
126 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) | |
fb0e1ba7 | 127 | |
fb0e1ba7 MK |
128 | /* If the last reported event was a SIGTRAP, this variable is set to |
129 | the process id of the LWP/thread that got it. */ | |
39f77062 | 130 | ptid_t trap_ptid; |
fb0e1ba7 MK |
131 | \f |
132 | ||
133 | /* This module's target-specific operations. */ | |
134 | static struct target_ops lin_lwp_ops; | |
135 | ||
136 | /* The standard child operations. */ | |
137 | extern struct target_ops child_ops; | |
138 | ||
3f07c44b MK |
139 | /* Since we cannot wait (in lin_lwp_wait) for the initial process and |
140 | any cloned processes with a single call to waitpid, we have to use | |
4c8de859 | 141 | the WNOHANG flag and call waitpid in a loop. To optimize |
3f07c44b MK |
142 | things a bit we use `sigsuspend' to wake us up when a process has |
143 | something to report (it will send us a SIGCHLD if it has). To make | |
144 | this work we have to juggle with the signal mask. We save the | |
4c8de859 | 145 | original signal mask such that we can restore it before creating a |
3f07c44b MK |
146 | new process in order to avoid blocking certain signals in the |
147 | inferior. We then block SIGCHLD during the waitpid/sigsuspend | |
148 | loop. */ | |
149 | ||
4c8de859 | 150 | /* Original signal mask. */ |
3f07c44b MK |
151 | static sigset_t normal_mask; |
152 | ||
fb0e1ba7 MK |
153 | /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in |
154 | _initialize_lin_lwp. */ | |
155 | static sigset_t suspend_mask; | |
3f07c44b MK |
156 | |
157 | /* Signals to block to make that sigsuspend work. */ | |
158 | static sigset_t blocked_mask; | |
fb0e1ba7 MK |
159 | \f |
160 | ||
161 | /* Prototypes for local functions. */ | |
c194fbe1 | 162 | static int stop_wait_callback (struct lwp_info *lp, void *data); |
b757528f | 163 | static int lin_lwp_thread_alive (ptid_t ptid); |
fb0e1ba7 | 164 | \f |
58eeadba MK |
165 | /* Convert wait status STATUS to a string. Used for printing debug |
166 | messages only. */ | |
fb0e1ba7 | 167 | |
58eeadba MK |
168 | static char * |
169 | status_to_str (int status) | |
170 | { | |
171 | static char buf[64]; | |
172 | ||
173 | if (WIFSTOPPED (status)) | |
174 | snprintf (buf, sizeof (buf), "%s (stopped)", | |
175 | strsignal (WSTOPSIG (status))); | |
176 | else if (WIFSIGNALED (status)) | |
177 | snprintf (buf, sizeof (buf), "%s (terminated)", | |
178 | strsignal (WSTOPSIG (status))); | |
179 | else | |
6949171e | 180 | snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status)); |
58eeadba MK |
181 | |
182 | return buf; | |
183 | } | |
184 | \f | |
c194fbe1 MK |
185 | /* Initialize the list of LWPs. Note that this module, contrary to |
186 | what GDB's generic threads layer does for its thread list, | |
187 | re-initializes the LWP lists whenever we mourn or detach (which | |
188 | doesn't involve mourning) the inferior. */ | |
fb0e1ba7 MK |
189 | |
190 | static void | |
191 | init_lwp_list (void) | |
192 | { | |
193 | struct lwp_info *lp, *lpnext; | |
194 | ||
195 | for (lp = lwp_list; lp; lp = lpnext) | |
196 | { | |
197 | lpnext = lp->next; | |
b8c9b27d | 198 | xfree (lp); |
fb0e1ba7 MK |
199 | } |
200 | ||
201 | lwp_list = NULL; | |
202 | num_lwps = 0; | |
203 | threaded = 0; | |
204 | } | |
205 | ||
206 | /* Add the LWP specified by PID to the list. If this causes the | |
207 | number of LWPs to become larger than one, go into "threaded" mode. | |
208 | Return a pointer to the structure describing the new LWP. */ | |
209 | ||
210 | static struct lwp_info * | |
39f77062 | 211 | add_lwp (ptid_t ptid) |
fb0e1ba7 MK |
212 | { |
213 | struct lwp_info *lp; | |
214 | ||
39f77062 | 215 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 MK |
216 | |
217 | lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); | |
218 | ||
219 | memset (lp, 0, sizeof (struct lwp_info)); | |
220 | ||
39f77062 | 221 | lp->ptid = ptid; |
fb0e1ba7 MK |
222 | |
223 | lp->next = lwp_list; | |
224 | lwp_list = lp; | |
225 | if (++num_lwps > 1) | |
226 | threaded = 1; | |
227 | ||
228 | return lp; | |
229 | } | |
230 | ||
231 | /* Remove the LWP specified by PID from the list. */ | |
232 | ||
233 | static void | |
39f77062 | 234 | delete_lwp (ptid_t ptid) |
fb0e1ba7 MK |
235 | { |
236 | struct lwp_info *lp, *lpprev; | |
237 | ||
238 | lpprev = NULL; | |
239 | ||
240 | for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) | |
39f77062 | 241 | if (ptid_equal (lp->ptid, ptid)) |
fb0e1ba7 MK |
242 | break; |
243 | ||
244 | if (!lp) | |
245 | return; | |
246 | ||
247 | /* We don't go back to "non-threaded" mode if the number of threads | |
248 | becomes less than two. */ | |
249 | num_lwps--; | |
250 | ||
251 | if (lpprev) | |
252 | lpprev->next = lp->next; | |
253 | else | |
254 | lwp_list = lp->next; | |
255 | ||
b8c9b27d | 256 | xfree (lp); |
fb0e1ba7 MK |
257 | } |
258 | ||
259 | /* Return a pointer to the structure describing the LWP corresponding | |
260 | to PID. If no corresponding LWP could be found, return NULL. */ | |
261 | ||
262 | static struct lwp_info * | |
39f77062 | 263 | find_lwp_pid (ptid_t ptid) |
fb0e1ba7 MK |
264 | { |
265 | struct lwp_info *lp; | |
39f77062 | 266 | int lwp; |
fb0e1ba7 | 267 | |
39f77062 KB |
268 | if (is_lwp (ptid)) |
269 | lwp = GET_LWP (ptid); | |
270 | else | |
271 | lwp = GET_PID (ptid); | |
fb0e1ba7 MK |
272 | |
273 | for (lp = lwp_list; lp; lp = lp->next) | |
39f77062 | 274 | if (lwp == GET_LWP (lp->ptid)) |
fb0e1ba7 MK |
275 | return lp; |
276 | ||
277 | return NULL; | |
278 | } | |
279 | ||
280 | /* Call CALLBACK with its second argument set to DATA for every LWP in | |
281 | the list. If CALLBACK returns 1 for a particular LWP, return a | |
282 | pointer to the structure describing that LWP immediately. | |
283 | Otherwise return NULL. */ | |
284 | ||
285 | struct lwp_info * | |
286 | iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) | |
287 | { | |
fce0e6e1 | 288 | struct lwp_info *lp, *lpnext; |
fb0e1ba7 | 289 | |
fce0e6e1 MK |
290 | for (lp = lwp_list; lp; lp = lpnext) |
291 | { | |
292 | lpnext = lp->next; | |
293 | if ((*callback) (lp, data)) | |
294 | return lp; | |
295 | } | |
fb0e1ba7 MK |
296 | |
297 | return NULL; | |
298 | } | |
299 | \f | |
300 | ||
8605d56e | 301 | /* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP |
e02bc4cc DS |
302 | layer. |
303 | ||
304 | Note that this implementation is potentially redundant now that | |
8849f47d JL |
305 | default_prepare_to_proceed() has been added. |
306 | ||
307 | FIXME This may not support switching threads after Ctrl-C | |
308 | correctly. The default implementation does support this. */ | |
fb0e1ba7 MK |
309 | |
310 | int | |
311 | lin_lwp_prepare_to_proceed (void) | |
312 | { | |
6949171e JJ |
313 | if (!ptid_equal (trap_ptid, null_ptid) |
314 | && !ptid_equal (inferior_ptid, trap_ptid)) | |
fb0e1ba7 MK |
315 | { |
316 | /* Switched over from TRAP_PID. */ | |
317 | CORE_ADDR stop_pc = read_pc (); | |
318 | CORE_ADDR trap_pc; | |
319 | ||
320 | /* Avoid switching where it wouldn't do any good, i.e. if both | |
321 | threads are at the same breakpoint. */ | |
39f77062 | 322 | trap_pc = read_pc_pid (trap_ptid); |
fb0e1ba7 MK |
323 | if (trap_pc != stop_pc && breakpoint_here_p (trap_pc)) |
324 | { | |
325 | /* User hasn't deleted the breakpoint. Return non-zero, and | |
6949171e | 326 | switch back to TRAP_PID. */ |
39f77062 | 327 | inferior_ptid = trap_ptid; |
fb0e1ba7 MK |
328 | |
329 | /* FIXME: Is this stuff really necessary? */ | |
330 | flush_cached_frames (); | |
331 | registers_changed (); | |
332 | ||
333 | return 1; | |
334 | } | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | \f | |
340 | ||
341 | #if 0 | |
342 | static void | |
343 | lin_lwp_open (char *args, int from_tty) | |
344 | { | |
345 | push_target (&lin_lwp_ops); | |
346 | } | |
347 | #endif | |
348 | ||
349 | /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print | |
350 | a message telling the user that a new LWP has been added to the | |
351 | process. */ | |
352 | ||
353 | void | |
39f77062 | 354 | lin_lwp_attach_lwp (ptid_t ptid, int verbose) |
fb0e1ba7 MK |
355 | { |
356 | struct lwp_info *lp; | |
357 | ||
39f77062 | 358 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 | 359 | |
da9c7185 KB |
360 | /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events |
361 | to interrupt either the ptrace() or waitpid() calls below. */ | |
6949171e | 362 | if (!sigismember (&blocked_mask, SIGCHLD)) |
da9c7185 KB |
363 | { |
364 | sigaddset (&blocked_mask, SIGCHLD); | |
365 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
366 | } | |
367 | ||
fb0e1ba7 | 368 | if (verbose) |
39f77062 | 369 | printf_filtered ("[New %s]\n", target_pid_to_str (ptid)); |
fb0e1ba7 | 370 | |
c194fbe1 MK |
371 | lp = find_lwp_pid (ptid); |
372 | if (lp == NULL) | |
373 | lp = add_lwp (ptid); | |
374 | ||
cacab7c4 MK |
375 | /* We assume that we're already attached to any LWP that has an |
376 | id equal to the overall process id. */ | |
377 | if (GET_LWP (ptid) != GET_PID (ptid)) | |
c4365b19 | 378 | { |
cacab7c4 MK |
379 | pid_t pid; |
380 | int status; | |
381 | ||
382 | if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) | |
383 | error ("Can't attach %s: %s", target_pid_to_str (ptid), | |
dc672865 | 384 | safe_strerror (errno)); |
cacab7c4 | 385 | |
9fe7d6bf | 386 | if (debug_lin_lwp) |
6949171e JJ |
387 | fprintf_unfiltered (gdb_stdlog, |
388 | "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n", | |
9fe7d6bf MS |
389 | target_pid_to_str (ptid)); |
390 | ||
cacab7c4 MK |
391 | pid = waitpid (GET_LWP (ptid), &status, 0); |
392 | if (pid == -1 && errno == ECHILD) | |
393 | { | |
394 | /* Try again with __WCLONE to check cloned processes. */ | |
395 | pid = waitpid (GET_LWP (ptid), &status, __WCLONE); | |
396 | lp->cloned = 1; | |
397 | } | |
398 | ||
399 | gdb_assert (pid == GET_LWP (ptid) | |
400 | && WIFSTOPPED (status) && WSTOPSIG (status)); | |
401 | ||
402 | lp->stopped = 1; | |
9fe7d6bf MS |
403 | |
404 | if (debug_lin_lwp) | |
405 | { | |
406 | fprintf_unfiltered (gdb_stdlog, | |
407 | "LLAL: waitpid %s received %s\n", | |
6949171e | 408 | target_pid_to_str (ptid), |
9fe7d6bf MS |
409 | status_to_str (status)); |
410 | } | |
c4365b19 | 411 | } |
da9c7185 KB |
412 | else |
413 | { | |
414 | /* We assume that the LWP representing the original process | |
6949171e JJ |
415 | is already stopped. Mark it as stopped in the data structure |
416 | that the lin-lwp layer uses to keep track of threads. Note | |
417 | that this won't have already been done since the main thread | |
418 | will have, we assume, been stopped by an attach from a | |
419 | different layer. */ | |
da9c7185 KB |
420 | lp->stopped = 1; |
421 | } | |
fb0e1ba7 MK |
422 | } |
423 | ||
424 | static void | |
425 | lin_lwp_attach (char *args, int from_tty) | |
426 | { | |
c194fbe1 | 427 | struct lwp_info *lp; |
cacab7c4 MK |
428 | pid_t pid; |
429 | int status; | |
c194fbe1 | 430 | |
fb0e1ba7 MK |
431 | /* FIXME: We should probably accept a list of process id's, and |
432 | attach all of them. */ | |
c194fbe1 MK |
433 | child_ops.to_attach (args, from_tty); |
434 | ||
435 | /* Add the initial process as the first LWP to the list. */ | |
cacab7c4 | 436 | lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid))); |
c194fbe1 MK |
437 | |
438 | /* Make sure the initial process is stopped. The user-level threads | |
439 | layer might want to poke around in the inferior, and that won't | |
440 | work if things haven't stabilized yet. */ | |
cacab7c4 MK |
441 | pid = waitpid (GET_PID (inferior_ptid), &status, 0); |
442 | if (pid == -1 && errno == ECHILD) | |
443 | { | |
444 | warning ("%s is a cloned process", target_pid_to_str (inferior_ptid)); | |
445 | ||
446 | /* Try again with __WCLONE to check cloned processes. */ | |
447 | pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE); | |
448 | lp->cloned = 1; | |
449 | } | |
450 | ||
451 | gdb_assert (pid == GET_PID (inferior_ptid) | |
452 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP); | |
453 | ||
454 | lp->stopped = 1; | |
c194fbe1 MK |
455 | |
456 | /* Fake the SIGSTOP that core GDB expects. */ | |
457 | lp->status = W_STOPCODE (SIGSTOP); | |
fce0e6e1 | 458 | lp->resumed = 1; |
9fe7d6bf MS |
459 | if (debug_lin_lwp) |
460 | { | |
461 | fprintf_unfiltered (gdb_stdlog, | |
6949171e | 462 | "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid); |
9fe7d6bf | 463 | } |
c194fbe1 MK |
464 | } |
465 | ||
466 | static int | |
467 | detach_callback (struct lwp_info *lp, void *data) | |
468 | { | |
469 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
470 | ||
471 | if (debug_lin_lwp && lp->status) | |
9fe7d6bf | 472 | fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n", |
6949171e | 473 | strsignal (WSTOPSIG (lp->status)), |
9fe7d6bf | 474 | target_pid_to_str (lp->ptid)); |
c194fbe1 MK |
475 | |
476 | while (lp->signalled && lp->stopped) | |
477 | { | |
9fe7d6bf | 478 | errno = 0; |
c194fbe1 MK |
479 | if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, |
480 | WSTOPSIG (lp->status)) < 0) | |
481 | error ("Can't continue %s: %s", target_pid_to_str (lp->ptid), | |
dc672865 | 482 | safe_strerror (errno)); |
c194fbe1 | 483 | |
9fe7d6bf | 484 | if (debug_lin_lwp) |
6949171e | 485 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
486 | "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n", |
487 | target_pid_to_str (lp->ptid), | |
6949171e | 488 | status_to_str (lp->status)); |
9fe7d6bf | 489 | |
c194fbe1 | 490 | lp->stopped = 0; |
c4365b19 | 491 | lp->signalled = 0; |
c194fbe1 MK |
492 | lp->status = 0; |
493 | stop_wait_callback (lp, NULL); | |
494 | ||
495 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
496 | } | |
497 | ||
cacab7c4 MK |
498 | /* We don't actually detach from the LWP that has an id equal to the |
499 | overall process id just yet. */ | |
500 | if (GET_LWP (lp->ptid) != GET_PID (lp->ptid)) | |
c194fbe1 | 501 | { |
9fe7d6bf | 502 | errno = 0; |
c194fbe1 MK |
503 | if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, |
504 | WSTOPSIG (lp->status)) < 0) | |
505 | error ("Can't detach %s: %s", target_pid_to_str (lp->ptid), | |
dc672865 | 506 | safe_strerror (errno)); |
c194fbe1 | 507 | |
9fe7d6bf MS |
508 | if (debug_lin_lwp) |
509 | fprintf_unfiltered (gdb_stdlog, | |
510 | "PTRACE_DETACH (%s, %s, 0) (OK)\n", | |
6949171e | 511 | target_pid_to_str (lp->ptid), |
9fe7d6bf MS |
512 | strsignal (WSTOPSIG (lp->status))); |
513 | ||
c194fbe1 MK |
514 | delete_lwp (lp->ptid); |
515 | } | |
516 | ||
517 | return 0; | |
fb0e1ba7 MK |
518 | } |
519 | ||
520 | static void | |
521 | lin_lwp_detach (char *args, int from_tty) | |
522 | { | |
c194fbe1 MK |
523 | iterate_over_lwps (detach_callback, NULL); |
524 | ||
cacab7c4 | 525 | /* Only the initial process should be left right now. */ |
c194fbe1 MK |
526 | gdb_assert (num_lwps == 1); |
527 | ||
528 | trap_ptid = null_ptid; | |
529 | ||
530 | /* Destroy LWP info; it's no longer valid. */ | |
531 | init_lwp_list (); | |
532 | ||
533 | /* Restore the original signal mask. */ | |
534 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); | |
535 | sigemptyset (&blocked_mask); | |
536 | ||
01263b57 | 537 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
c194fbe1 | 538 | child_ops.to_detach (args, from_tty); |
fb0e1ba7 MK |
539 | } |
540 | \f | |
541 | ||
fb0e1ba7 MK |
542 | /* Resume LP. */ |
543 | ||
544 | static int | |
545 | resume_callback (struct lwp_info *lp, void *data) | |
546 | { | |
547 | if (lp->stopped && lp->status == 0) | |
548 | { | |
549 | struct thread_info *tp; | |
550 | ||
39f77062 | 551 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0); |
9fe7d6bf | 552 | if (debug_lin_lwp) |
6949171e | 553 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
554 | "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n", |
555 | target_pid_to_str (lp->ptid)); | |
fb0e1ba7 MK |
556 | lp->stopped = 0; |
557 | lp->step = 0; | |
558 | } | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
fce0e6e1 MK |
563 | static int |
564 | resume_clear_callback (struct lwp_info *lp, void *data) | |
565 | { | |
566 | lp->resumed = 0; | |
567 | return 0; | |
568 | } | |
569 | ||
570 | static int | |
571 | resume_set_callback (struct lwp_info *lp, void *data) | |
572 | { | |
573 | lp->resumed = 1; | |
574 | return 0; | |
575 | } | |
576 | ||
fb0e1ba7 | 577 | static void |
39f77062 | 578 | lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo) |
fb0e1ba7 MK |
579 | { |
580 | struct lwp_info *lp; | |
581 | int resume_all; | |
582 | ||
2a4b7c45 DJ |
583 | /* A specific PTID means `step only this process id'. */ |
584 | resume_all = (PIDGET (ptid) == -1); | |
fb0e1ba7 | 585 | |
fce0e6e1 MK |
586 | if (resume_all) |
587 | iterate_over_lwps (resume_set_callback, NULL); | |
588 | else | |
589 | iterate_over_lwps (resume_clear_callback, NULL); | |
590 | ||
fb0e1ba7 | 591 | /* If PID is -1, it's the current inferior that should be |
c4365b19 | 592 | handled specially. */ |
39f77062 KB |
593 | if (PIDGET (ptid) == -1) |
594 | ptid = inferior_ptid; | |
fb0e1ba7 | 595 | |
39f77062 | 596 | lp = find_lwp_pid (ptid); |
fb0e1ba7 MK |
597 | if (lp) |
598 | { | |
39f77062 | 599 | ptid = pid_to_ptid (GET_LWP (lp->ptid)); |
fb0e1ba7 | 600 | |
fb0e1ba7 MK |
601 | /* Remember if we're stepping. */ |
602 | lp->step = step; | |
603 | ||
fce0e6e1 MK |
604 | /* Mark this LWP as resumed. */ |
605 | lp->resumed = 1; | |
606 | ||
fb0e1ba7 MK |
607 | /* If we have a pending wait status for this thread, there is no |
608 | point in resuming the process. */ | |
609 | if (lp->status) | |
610 | { | |
611 | /* FIXME: What should we do if we are supposed to continue | |
6949171e | 612 | this thread with a signal? */ |
fb0e1ba7 MK |
613 | gdb_assert (signo == TARGET_SIGNAL_0); |
614 | return; | |
615 | } | |
40564aca MK |
616 | |
617 | /* Mark LWP as not stopped to prevent it from being continued by | |
6949171e | 618 | resume_callback. */ |
40564aca | 619 | lp->stopped = 0; |
fb0e1ba7 MK |
620 | } |
621 | ||
622 | if (resume_all) | |
623 | iterate_over_lwps (resume_callback, NULL); | |
624 | ||
39f77062 | 625 | child_resume (ptid, step, signo); |
9fe7d6bf MS |
626 | if (debug_lin_lwp) |
627 | fprintf_unfiltered (gdb_stdlog, | |
628 | "LLR: %s %s, %s (resume event thread)\n", | |
629 | step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
630 | target_pid_to_str (ptid), | |
631 | signo ? strsignal (signo) : "0"); | |
fb0e1ba7 MK |
632 | } |
633 | \f | |
634 | ||
b757528f JJ |
635 | /* Issue kill to specified lwp. */ |
636 | ||
637 | static int tkill_failed; | |
638 | ||
639 | static int | |
640 | kill_lwp (int lwpid, int signo) | |
641 | { | |
642 | errno = 0; | |
643 | ||
644 | /* Use tkill, if possible, in case we are using nptl threads. If tkill | |
645 | fails, then we are not using nptl threads and we should be using kill. */ | |
646 | ||
647 | #ifdef HAVE_TKILL_SYSCALL | |
648 | if (!tkill_failed) | |
649 | { | |
650 | int ret = syscall (__NR_tkill, lwpid, signo); | |
651 | if (errno != ENOSYS) | |
652 | return ret; | |
653 | errno = 0; | |
654 | tkill_failed = 1; | |
655 | } | |
656 | #endif | |
657 | ||
658 | return kill (lwpid, signo); | |
659 | } | |
660 | ||
fb0e1ba7 MK |
661 | /* Send a SIGSTOP to LP. */ |
662 | ||
663 | static int | |
664 | stop_callback (struct lwp_info *lp, void *data) | |
665 | { | |
6949171e | 666 | if (!lp->stopped && !lp->signalled) |
fb0e1ba7 MK |
667 | { |
668 | int ret; | |
669 | ||
9fe7d6bf MS |
670 | if (debug_lin_lwp) |
671 | { | |
672 | fprintf_unfiltered (gdb_stdlog, | |
673 | "SC: kill %s **<SIGSTOP>**\n", | |
674 | target_pid_to_str (lp->ptid)); | |
675 | } | |
b757528f JJ |
676 | errno = 0; |
677 | ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP); | |
678 | if (debug_lin_lwp) | |
679 | { | |
680 | fprintf_unfiltered (gdb_stdlog, | |
681 | "SC: lwp kill %d %s\n", | |
682 | ret, | |
683 | errno ? safe_strerror (errno) : "ERRNO-OK"); | |
684 | } | |
fb0e1ba7 MK |
685 | |
686 | lp->signalled = 1; | |
687 | gdb_assert (lp->status == 0); | |
688 | } | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
de4ca854 MK |
693 | /* Wait until LP is stopped. If DATA is non-null it is interpreted as |
694 | a pointer to a set of signals to be flushed immediately. */ | |
fb0e1ba7 MK |
695 | |
696 | static int | |
697 | stop_wait_callback (struct lwp_info *lp, void *data) | |
698 | { | |
de4ca854 MK |
699 | sigset_t *flush_mask = data; |
700 | ||
6949171e | 701 | if (!lp->stopped && lp->signalled) |
fb0e1ba7 MK |
702 | { |
703 | pid_t pid; | |
704 | int status; | |
705 | ||
706 | gdb_assert (lp->status == 0); | |
707 | ||
b757528f | 708 | pid = waitpid (GET_LWP (lp->ptid), &status, 0); |
fb0e1ba7 | 709 | if (pid == -1 && errno == ECHILD) |
b757528f JJ |
710 | { |
711 | pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE); | |
712 | if (pid == -1 && errno == ECHILD) | |
713 | { | |
714 | /* The thread has previously exited. We need to delete it now | |
715 | because in the case of nptl threads, there won't be an | |
716 | exit event unless it is the main thread. */ | |
717 | if (debug_lin_lwp) | |
718 | fprintf_unfiltered (gdb_stdlog, | |
719 | "SWC: %s exited.\n", | |
720 | target_pid_to_str (lp->ptid)); | |
721 | delete_lwp (lp->ptid); | |
722 | return 0; | |
723 | } | |
724 | } | |
fb0e1ba7 | 725 | |
39f77062 | 726 | gdb_assert (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 | 727 | |
9fe7d6bf MS |
728 | if (debug_lin_lwp) |
729 | { | |
730 | fprintf_unfiltered (gdb_stdlog, | |
731 | "SWC: waitpid %s received %s\n", | |
6949171e | 732 | target_pid_to_str (lp->ptid), |
9fe7d6bf MS |
733 | status_to_str (status)); |
734 | } | |
735 | ||
b757528f | 736 | /* Check if the thread has exited. */ |
fb0e1ba7 MK |
737 | if (WIFEXITED (status) || WIFSIGNALED (status)) |
738 | { | |
739 | gdb_assert (num_lwps > 1); | |
fb0e1ba7 | 740 | |
39f77062 | 741 | if (in_thread_list (lp->ptid)) |
e6328671 MK |
742 | { |
743 | /* Core GDB cannot deal with us deleting the current | |
6949171e | 744 | thread. */ |
39f77062 KB |
745 | if (!ptid_equal (lp->ptid, inferior_ptid)) |
746 | delete_thread (lp->ptid); | |
e6328671 | 747 | printf_unfiltered ("[%s exited]\n", |
39f77062 | 748 | target_pid_to_str (lp->ptid)); |
e6328671 | 749 | } |
7ca673cd | 750 | if (debug_lin_lwp) |
b757528f JJ |
751 | fprintf_unfiltered (gdb_stdlog, |
752 | "SWC: %s exited.\n", | |
753 | target_pid_to_str (lp->ptid)); | |
754 | ||
755 | delete_lwp (lp->ptid); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | /* Check if the current LWP has previously exited. For nptl threads, | |
760 | there is no exit signal issued for LWPs that are not the | |
761 | main thread so we should check whenever the thread is stopped. */ | |
762 | if (!lin_lwp_thread_alive (lp->ptid)) | |
763 | { | |
764 | if (in_thread_list (lp->ptid)) | |
765 | { | |
766 | /* Core GDB cannot deal with us deleting the current | |
767 | thread. */ | |
768 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
769 | delete_thread (lp->ptid); | |
770 | printf_unfiltered ("[%s exited]\n", | |
771 | target_pid_to_str (lp->ptid)); | |
772 | } | |
773 | if (debug_lin_lwp) | |
774 | fprintf_unfiltered (gdb_stdlog, | |
775 | "SWC: %s already exited.\n", | |
9fe7d6bf | 776 | target_pid_to_str (lp->ptid)); |
7ca673cd | 777 | |
39f77062 | 778 | delete_lwp (lp->ptid); |
fb0e1ba7 MK |
779 | return 0; |
780 | } | |
781 | ||
782 | gdb_assert (WIFSTOPPED (status)); | |
fb0e1ba7 | 783 | |
de4ca854 MK |
784 | /* Ignore any signals in FLUSH_MASK. */ |
785 | if (flush_mask && sigismember (flush_mask, WSTOPSIG (status))) | |
786 | { | |
9fe7d6bf | 787 | errno = 0; |
de4ca854 | 788 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
9fe7d6bf MS |
789 | if (debug_lin_lwp) |
790 | fprintf_unfiltered (gdb_stdlog, | |
791 | "PTRACE_CONT %s, 0, 0 (%s)\n", | |
792 | target_pid_to_str (lp->ptid), | |
793 | errno ? safe_strerror (errno) : "OK"); | |
794 | ||
de4ca854 MK |
795 | return stop_wait_callback (lp, flush_mask); |
796 | } | |
797 | ||
fb0e1ba7 MK |
798 | if (WSTOPSIG (status) != SIGSTOP) |
799 | { | |
b1aeb4c5 | 800 | if (WSTOPSIG (status) == SIGTRAP) |
fb0e1ba7 MK |
801 | { |
802 | /* If a LWP other than the LWP that we're reporting an | |
6949171e JJ |
803 | event for has hit a GDB breakpoint (as opposed to |
804 | some random trap signal), then just arrange for it to | |
805 | hit it again later. We don't keep the SIGTRAP status | |
806 | and don't forward the SIGTRAP signal to the LWP. We | |
807 | will handle the current event, eventually we will | |
808 | resume all LWPs, and this one will get its breakpoint | |
809 | trap again. | |
810 | ||
811 | If we do not do this, then we run the risk that the | |
812 | user will delete or disable the breakpoint, but the | |
813 | thread will have already tripped on it. */ | |
7ca673cd | 814 | |
c4365b19 | 815 | /* Now resume this LWP and get the SIGSTOP event. */ |
9fe7d6bf | 816 | errno = 0; |
c4365b19 | 817 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
b1aeb4c5 MS |
818 | if (debug_lin_lwp) |
819 | { | |
6949171e | 820 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
821 | "PTRACE_CONT %s, 0, 0 (%s)\n", |
822 | target_pid_to_str (lp->ptid), | |
823 | errno ? safe_strerror (errno) : "OK"); | |
824 | ||
6949171e | 825 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
826 | "SWC: Candidate SIGTRAP event in %s\n", |
827 | target_pid_to_str (lp->ptid)); | |
b1aeb4c5 MS |
828 | } |
829 | /* Hold the SIGTRAP for handling by lin_lwp_wait. */ | |
830 | stop_wait_callback (lp, data); | |
831 | /* If there's another event, throw it back into the queue. */ | |
832 | if (lp->status) | |
9fe7d6bf | 833 | { |
b757528f JJ |
834 | if (debug_lin_lwp) |
835 | { | |
836 | fprintf_unfiltered (gdb_stdlog, | |
837 | "SWC: kill %s, %s\n", | |
838 | target_pid_to_str (lp->ptid), | |
839 | status_to_str ((int) status)); | |
840 | } | |
841 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); | |
9fe7d6bf | 842 | } |
b1aeb4c5 MS |
843 | /* Save the sigtrap event. */ |
844 | lp->status = status; | |
845 | return 0; | |
fb0e1ba7 MK |
846 | } |
847 | else | |
848 | { | |
b1aeb4c5 | 849 | /* The thread was stopped with a signal other than |
6949171e | 850 | SIGSTOP, and didn't accidentally trip a breakpoint. */ |
b1aeb4c5 | 851 | |
7ca673cd | 852 | if (debug_lin_lwp) |
b1aeb4c5 | 853 | { |
6949171e | 854 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 855 | "SWC: Pending event %s in %s\n", |
6949171e | 856 | status_to_str ((int) status), |
9fe7d6bf | 857 | target_pid_to_str (lp->ptid)); |
b1aeb4c5 MS |
858 | } |
859 | /* Now resume this LWP and get the SIGSTOP event. */ | |
9fe7d6bf | 860 | errno = 0; |
b1aeb4c5 | 861 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
9fe7d6bf | 862 | if (debug_lin_lwp) |
6949171e | 863 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
864 | "SWC: PTRACE_CONT %s, 0, 0 (%s)\n", |
865 | target_pid_to_str (lp->ptid), | |
866 | errno ? safe_strerror (errno) : "OK"); | |
7ca673cd | 867 | |
b1aeb4c5 | 868 | /* Hold this event/waitstatus while we check to see if |
6949171e | 869 | there are any more (we still want to get that SIGSTOP). */ |
b1aeb4c5 MS |
870 | stop_wait_callback (lp, data); |
871 | /* If the lp->status field is still empty, use it to hold | |
6949171e JJ |
872 | this event. If not, then this event must be returned |
873 | to the event queue of the LWP. */ | |
b1aeb4c5 MS |
874 | if (lp->status == 0) |
875 | lp->status = status; | |
876 | else | |
9fe7d6bf MS |
877 | { |
878 | if (debug_lin_lwp) | |
879 | { | |
6949171e | 880 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 881 | "SWC: kill %s, %s\n", |
6949171e | 882 | target_pid_to_str (lp->ptid), |
9fe7d6bf MS |
883 | status_to_str ((int) status)); |
884 | } | |
b757528f | 885 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status)); |
9fe7d6bf | 886 | } |
b1aeb4c5 | 887 | return 0; |
fb0e1ba7 MK |
888 | } |
889 | } | |
890 | else | |
891 | { | |
892 | /* We caught the SIGSTOP that we intended to catch, so | |
6949171e | 893 | there's no SIGSTOP pending. */ |
b1aeb4c5 | 894 | lp->stopped = 1; |
fb0e1ba7 MK |
895 | lp->signalled = 0; |
896 | } | |
897 | } | |
898 | ||
899 | return 0; | |
900 | } | |
901 | ||
902 | /* Return non-zero if LP has a wait status pending. */ | |
903 | ||
904 | static int | |
905 | status_callback (struct lwp_info *lp, void *data) | |
906 | { | |
fce0e6e1 MK |
907 | /* Only report a pending wait status if we pretend that this has |
908 | indeed been resumed. */ | |
909 | return (lp->status != 0 && lp->resumed); | |
fb0e1ba7 MK |
910 | } |
911 | ||
912 | /* Return non-zero if LP isn't stopped. */ | |
913 | ||
914 | static int | |
915 | running_callback (struct lwp_info *lp, void *data) | |
916 | { | |
917 | return (lp->stopped == 0); | |
918 | } | |
919 | ||
00d4fce6 | 920 | /* Count the LWP's that have had events. */ |
b1aeb4c5 MS |
921 | |
922 | static int | |
923 | count_events_callback (struct lwp_info *lp, void *data) | |
924 | { | |
925 | int *count = data; | |
926 | ||
00d4fce6 MK |
927 | gdb_assert (count != NULL); |
928 | ||
929 | /* Count only LWPs that have a SIGTRAP event pending. */ | |
930 | if (lp->status != 0 | |
931 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) | |
b1aeb4c5 MS |
932 | (*count)++; |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
00d4fce6 | 937 | /* Select the LWP (if any) that is currently being single-stepped. */ |
b1aeb4c5 MS |
938 | |
939 | static int | |
940 | select_singlestep_lwp_callback (struct lwp_info *lp, void *data) | |
941 | { | |
942 | if (lp->step && lp->status != 0) | |
943 | return 1; | |
944 | else | |
945 | return 0; | |
946 | } | |
947 | ||
00d4fce6 | 948 | /* Select the Nth LWP that has had a SIGTRAP event. */ |
b1aeb4c5 MS |
949 | |
950 | static int | |
951 | select_event_lwp_callback (struct lwp_info *lp, void *data) | |
952 | { | |
953 | int *selector = data; | |
954 | ||
00d4fce6 MK |
955 | gdb_assert (selector != NULL); |
956 | ||
957 | /* Select only LWPs that have a SIGTRAP event pending. */ | |
958 | if (lp->status != 0 | |
959 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) | |
b1aeb4c5 MS |
960 | if ((*selector)-- == 0) |
961 | return 1; | |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
966 | static int | |
967 | cancel_breakpoints_callback (struct lwp_info *lp, void *data) | |
968 | { | |
969 | struct lwp_info *event_lp = data; | |
970 | ||
00d4fce6 MK |
971 | /* Leave the LWP that has been elected to receive a SIGTRAP alone. */ |
972 | if (lp == event_lp) | |
973 | return 0; | |
974 | ||
975 | /* If a LWP other than the LWP that we're reporting an event for has | |
976 | hit a GDB breakpoint (as opposed to some random trap signal), | |
977 | then just arrange for it to hit it again later. We don't keep | |
978 | the SIGTRAP status and don't forward the SIGTRAP signal to the | |
979 | LWP. We will handle the current event, eventually we will resume | |
980 | all LWPs, and this one will get its breakpoint trap again. | |
981 | ||
982 | If we do not do this, then we run the risk that the user will | |
983 | delete or disable the breakpoint, but the LWP will have already | |
984 | tripped on it. */ | |
985 | ||
986 | if (lp->status != 0 | |
6949171e JJ |
987 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP |
988 | && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - | |
00d4fce6 | 989 | DECR_PC_AFTER_BREAK)) |
b1aeb4c5 MS |
990 | { |
991 | if (debug_lin_lwp) | |
00d4fce6 | 992 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
993 | "CBC: Push back breakpoint for %s\n", |
994 | target_pid_to_str (lp->ptid)); | |
00d4fce6 MK |
995 | |
996 | /* Back up the PC if necessary. */ | |
b1aeb4c5 | 997 | if (DECR_PC_AFTER_BREAK) |
00d4fce6 MK |
998 | write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid); |
999 | ||
1000 | /* Throw away the SIGTRAP. */ | |
b1aeb4c5 MS |
1001 | lp->status = 0; |
1002 | } | |
00d4fce6 | 1003 | |
b1aeb4c5 MS |
1004 | return 0; |
1005 | } | |
1006 | ||
00d4fce6 | 1007 | /* Select one LWP out of those that have events pending. */ |
b1aeb4c5 MS |
1008 | |
1009 | static void | |
1010 | select_event_lwp (struct lwp_info **orig_lp, int *status) | |
1011 | { | |
1012 | int num_events = 0; | |
1013 | int random_selector; | |
1014 | struct lwp_info *event_lp; | |
1015 | ||
00d4fce6 MK |
1016 | /* Record the wait status for the origional LWP. */ |
1017 | (*orig_lp)->status = *status; | |
1018 | ||
1019 | /* Give preference to any LWP that is being single-stepped. */ | |
b1aeb4c5 MS |
1020 | event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); |
1021 | if (event_lp != NULL) | |
1022 | { | |
1023 | if (debug_lin_lwp) | |
00d4fce6 | 1024 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
1025 | "SEL: Select single-step %s\n", |
1026 | target_pid_to_str (event_lp->ptid)); | |
b1aeb4c5 MS |
1027 | } |
1028 | else | |
1029 | { | |
1030 | /* No single-stepping LWP. Select one at random, out of those | |
6949171e | 1031 | which have had SIGTRAP events. */ |
b1aeb4c5 | 1032 | |
00d4fce6 MK |
1033 | /* First see how many SIGTRAP events we have. */ |
1034 | iterate_over_lwps (count_events_callback, &num_events); | |
b1aeb4c5 | 1035 | |
00d4fce6 MK |
1036 | /* Now randomly pick a LWP out of those that have had a SIGTRAP. */ |
1037 | random_selector = (int) | |
b1aeb4c5 MS |
1038 | ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); |
1039 | ||
00d4fce6 | 1040 | if (debug_lin_lwp && num_events > 1) |
6949171e JJ |
1041 | fprintf_unfiltered (gdb_stdlog, |
1042 | "SEL: Found %d SIGTRAP events, selecting #%d\n", | |
00d4fce6 | 1043 | num_events, random_selector); |
b1aeb4c5 | 1044 | |
00d4fce6 MK |
1045 | event_lp = iterate_over_lwps (select_event_lwp_callback, |
1046 | &random_selector); | |
b1aeb4c5 MS |
1047 | } |
1048 | ||
1049 | if (event_lp != NULL) | |
1050 | { | |
00d4fce6 | 1051 | /* Switch the event LWP. */ |
b1aeb4c5 | 1052 | *orig_lp = event_lp; |
6949171e | 1053 | *status = event_lp->status; |
b1aeb4c5 | 1054 | } |
00d4fce6 MK |
1055 | |
1056 | /* Flush the wait status for the event LWP. */ | |
1057 | (*orig_lp)->status = 0; | |
b1aeb4c5 MS |
1058 | } |
1059 | ||
fce0e6e1 MK |
1060 | /* Return non-zero if LP has been resumed. */ |
1061 | ||
1062 | static int | |
1063 | resumed_callback (struct lwp_info *lp, void *data) | |
1064 | { | |
1065 | return lp->resumed; | |
1066 | } | |
1067 | ||
cacab7c4 MK |
1068 | #ifdef CHILD_WAIT |
1069 | ||
1070 | /* We need to override child_wait to support attaching to cloned | |
1071 | processes, since a normal wait (as done by the default version) | |
1072 | ignores those processes. */ | |
1073 | ||
1074 | /* Wait for child PTID to do something. Return id of the child, | |
1075 | minus_one_ptid in case of error; store status into *OURSTATUS. */ | |
1076 | ||
1077 | ptid_t | |
1078 | child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
1079 | { | |
1080 | int save_errno; | |
1081 | int status; | |
1082 | pid_t pid; | |
1083 | ||
1084 | do | |
1085 | { | |
1086 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
1087 | attached process. */ | |
1088 | set_sigio_trap (); | |
1089 | ||
1090 | pid = waitpid (GET_PID (ptid), &status, 0); | |
1091 | if (pid == -1 && errno == ECHILD) | |
1092 | /* Try again with __WCLONE to check cloned processes. */ | |
1093 | pid = waitpid (GET_PID (ptid), &status, __WCLONE); | |
9fe7d6bf MS |
1094 | |
1095 | if (debug_lin_lwp) | |
1096 | { | |
6949171e | 1097 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 1098 | "CW: waitpid %ld received %s\n", |
6949171e | 1099 | (long) pid, status_to_str (status)); |
9fe7d6bf MS |
1100 | } |
1101 | ||
cacab7c4 MK |
1102 | save_errno = errno; |
1103 | ||
b3ba1b44 | 1104 | /* Make sure we don't report an event for the exit of the |
6949171e JJ |
1105 | original program, if we've detached from it. */ |
1106 | if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid)) | |
b3ba1b44 DJ |
1107 | { |
1108 | pid = -1; | |
1109 | save_errno = EINTR; | |
1110 | } | |
1111 | ||
cacab7c4 MK |
1112 | clear_sigio_trap (); |
1113 | clear_sigint_trap (); | |
1114 | } | |
2d1bfe2e | 1115 | while (pid == -1 && save_errno == EINTR); |
cacab7c4 MK |
1116 | |
1117 | if (pid == -1) | |
1118 | { | |
6949171e | 1119 | warning ("Child process unexpectedly missing: %s", |
9fe7d6bf | 1120 | safe_strerror (errno)); |
cacab7c4 MK |
1121 | |
1122 | /* Claim it exited with unknown signal. */ | |
1123 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1124 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
1125 | return minus_one_ptid; | |
1126 | } | |
1127 | ||
1128 | store_waitstatus (ourstatus, status); | |
1129 | return pid_to_ptid (pid); | |
1130 | } | |
1131 | ||
1132 | #endif | |
1133 | ||
b757528f JJ |
1134 | /* Stop an active thread, verify it still exists, then resume it. */ |
1135 | ||
1136 | static int | |
1137 | stop_and_resume_callback (struct lwp_info *lp, void *data) | |
1138 | { | |
1139 | struct lwp_info *ptr; | |
1140 | ||
1141 | if (!lp->stopped && !lp->signalled) | |
1142 | { | |
1143 | stop_callback (lp, NULL); | |
1144 | stop_wait_callback (lp, NULL); | |
1145 | /* Resume if the lwp still exists. */ | |
1146 | for (ptr = lwp_list; ptr; ptr = ptr->next) | |
1147 | if (lp == ptr) | |
1148 | resume_callback (lp, NULL); | |
1149 | } | |
1150 | return 0; | |
1151 | } | |
1152 | ||
39f77062 KB |
1153 | static ptid_t |
1154 | lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
fb0e1ba7 MK |
1155 | { |
1156 | struct lwp_info *lp = NULL; | |
1157 | int options = 0; | |
1158 | int status = 0; | |
39f77062 | 1159 | pid_t pid = PIDGET (ptid); |
de4ca854 MK |
1160 | sigset_t flush_mask; |
1161 | ||
1162 | sigemptyset (&flush_mask); | |
fb0e1ba7 | 1163 | |
3f07c44b | 1164 | /* Make sure SIGCHLD is blocked. */ |
6949171e | 1165 | if (!sigismember (&blocked_mask, SIGCHLD)) |
3f07c44b MK |
1166 | { |
1167 | sigaddset (&blocked_mask, SIGCHLD); | |
1168 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
1169 | } | |
1170 | ||
6949171e | 1171 | retry: |
fb0e1ba7 | 1172 | |
9a973a8f MK |
1173 | /* Make sure there is at least one LWP that has been resumed, at |
1174 | least if there are any LWPs at all. */ | |
1175 | gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL)); | |
fce0e6e1 | 1176 | |
fb0e1ba7 MK |
1177 | /* First check if there is a LWP with a wait status pending. */ |
1178 | if (pid == -1) | |
1179 | { | |
b1aeb4c5 | 1180 | /* Any LWP that's been resumed will do. */ |
fb0e1ba7 MK |
1181 | lp = iterate_over_lwps (status_callback, NULL); |
1182 | if (lp) | |
1183 | { | |
fb0e1ba7 MK |
1184 | status = lp->status; |
1185 | lp->status = 0; | |
b1aeb4c5 MS |
1186 | |
1187 | if (debug_lin_lwp && status) | |
58eeadba | 1188 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 1189 | "LLW: Using pending wait status %s for %s.\n", |
6949171e | 1190 | status_to_str (status), |
9fe7d6bf | 1191 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 MK |
1192 | } |
1193 | ||
1194 | /* But if we don't fine one, we'll have to wait, and check both | |
1195 | cloned and uncloned processes. We start with the cloned | |
1196 | processes. */ | |
1197 | options = __WCLONE | WNOHANG; | |
1198 | } | |
39f77062 | 1199 | else if (is_lwp (ptid)) |
fb0e1ba7 | 1200 | { |
7ca673cd | 1201 | if (debug_lin_lwp) |
6949171e | 1202 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
1203 | "LLW: Waiting for specific LWP %s.\n", |
1204 | target_pid_to_str (ptid)); | |
7ca673cd | 1205 | |
fb0e1ba7 | 1206 | /* We have a specific LWP to check. */ |
39f77062 | 1207 | lp = find_lwp_pid (ptid); |
fb0e1ba7 MK |
1208 | gdb_assert (lp); |
1209 | status = lp->status; | |
1210 | lp->status = 0; | |
7ca673cd | 1211 | |
b1aeb4c5 | 1212 | if (debug_lin_lwp && status) |
58eeadba | 1213 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 1214 | "LLW: Using pending wait status %s for %s.\n", |
6949171e | 1215 | status_to_str (status), |
9fe7d6bf | 1216 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 MK |
1217 | |
1218 | /* If we have to wait, take into account whether PID is a cloned | |
1219 | process or not. And we have to convert it to something that | |
1220 | the layer beneath us can understand. */ | |
cacab7c4 | 1221 | options = lp->cloned ? __WCLONE : 0; |
39f77062 | 1222 | pid = GET_LWP (ptid); |
fb0e1ba7 MK |
1223 | } |
1224 | ||
1225 | if (status && lp->signalled) | |
1226 | { | |
1227 | /* A pending SIGSTOP may interfere with the normal stream of | |
6949171e JJ |
1228 | events. In a typical case where interference is a problem, |
1229 | we have a SIGSTOP signal pending for LWP A while | |
1230 | single-stepping it, encounter an event in LWP B, and take the | |
1231 | pending SIGSTOP while trying to stop LWP A. After processing | |
1232 | the event in LWP B, LWP A is continued, and we'll never see | |
1233 | the SIGTRAP associated with the last time we were | |
1234 | single-stepping LWP A. */ | |
fb0e1ba7 MK |
1235 | |
1236 | /* Resume the thread. It should halt immediately returning the | |
6949171e | 1237 | pending SIGSTOP. */ |
9fe7d6bf | 1238 | registers_changed (); |
39f77062 | 1239 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
6949171e | 1240 | TARGET_SIGNAL_0); |
9fe7d6bf MS |
1241 | if (debug_lin_lwp) |
1242 | fprintf_unfiltered (gdb_stdlog, | |
1243 | "LLW: %s %s, 0, 0 (expect SIGSTOP)\n", | |
1244 | lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
1245 | target_pid_to_str (lp->ptid)); | |
fb0e1ba7 | 1246 | lp->stopped = 0; |
fce0e6e1 | 1247 | gdb_assert (lp->resumed); |
fb0e1ba7 MK |
1248 | |
1249 | /* This should catch the pending SIGSTOP. */ | |
1250 | stop_wait_callback (lp, NULL); | |
1251 | } | |
1252 | ||
6949171e JJ |
1253 | set_sigint_trap (); /* Causes SIGINT to be passed on to the |
1254 | attached process. */ | |
fb0e1ba7 MK |
1255 | set_sigio_trap (); |
1256 | ||
1257 | while (status == 0) | |
1258 | { | |
1259 | pid_t lwpid; | |
1260 | ||
1261 | lwpid = waitpid (pid, &status, options); | |
1262 | if (lwpid > 0) | |
1263 | { | |
1264 | gdb_assert (pid == -1 || lwpid == pid); | |
1265 | ||
9fe7d6bf MS |
1266 | if (debug_lin_lwp) |
1267 | { | |
1268 | fprintf_unfiltered (gdb_stdlog, | |
1269 | "LLW: waitpid %ld received %s\n", | |
6949171e | 1270 | (long) lwpid, status_to_str (status)); |
9fe7d6bf MS |
1271 | } |
1272 | ||
39f77062 | 1273 | lp = find_lwp_pid (pid_to_ptid (lwpid)); |
b3ba1b44 DJ |
1274 | |
1275 | /* Make sure we don't report an event for the exit of an LWP not in | |
1276 | our list, i.e. not part of the current process. This can happen | |
1277 | if we detach from a program we original forked and then it | |
1278 | exits. */ | |
6949171e | 1279 | if (!WIFSTOPPED (status) && !lp) |
b3ba1b44 DJ |
1280 | { |
1281 | status = 0; | |
1282 | continue; | |
1283 | } | |
1284 | ||
6949171e | 1285 | if (!lp) |
fb0e1ba7 | 1286 | { |
39f77062 | 1287 | lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); |
cacab7c4 MK |
1288 | if (options & __WCLONE) |
1289 | lp->cloned = 1; | |
1290 | ||
fb0e1ba7 MK |
1291 | if (threaded) |
1292 | { | |
3f07c44b MK |
1293 | gdb_assert (WIFSTOPPED (status) |
1294 | && WSTOPSIG (status) == SIGSTOP); | |
fb0e1ba7 MK |
1295 | lp->signalled = 1; |
1296 | ||
6949171e | 1297 | if (!in_thread_list (inferior_ptid)) |
fb0e1ba7 | 1298 | { |
39f77062 | 1299 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), |
6949171e | 1300 | GET_PID (inferior_ptid)); |
39f77062 | 1301 | add_thread (inferior_ptid); |
fb0e1ba7 MK |
1302 | } |
1303 | ||
39f77062 | 1304 | add_thread (lp->ptid); |
fb0e1ba7 | 1305 | printf_unfiltered ("[New %s]\n", |
39f77062 | 1306 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 MK |
1307 | } |
1308 | } | |
1309 | ||
b757528f | 1310 | /* Check if the thread has exited. */ |
fb0e1ba7 | 1311 | if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) |
b757528f JJ |
1312 | { |
1313 | if (in_thread_list (lp->ptid)) | |
1314 | { | |
1315 | /* Core GDB cannot deal with us deleting the current | |
1316 | thread. */ | |
1317 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
1318 | delete_thread (lp->ptid); | |
1319 | printf_unfiltered ("[%s exited]\n", | |
1320 | target_pid_to_str (lp->ptid)); | |
1321 | } | |
1322 | ||
1323 | /* If this is the main thread, we must stop all threads and | |
1324 | verify if they are still alive. This is because in the nptl | |
1325 | thread model, there is no signal issued for exiting LWPs | |
1326 | other than the main thread. We only get the main thread | |
1327 | exit signal once all child threads have already exited. | |
1328 | If we stop all the threads and use the stop_wait_callback | |
1329 | to check if they have exited we can determine whether this | |
1330 | signal should be ignored or whether it means the end of the | |
1331 | debugged application, regardless of which threading model | |
1332 | is being used. */ | |
1333 | if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)) | |
1334 | { | |
1335 | lp->stopped = 1; | |
1336 | iterate_over_lwps (stop_and_resume_callback, NULL); | |
1337 | } | |
1338 | ||
1339 | if (debug_lin_lwp) | |
1340 | fprintf_unfiltered (gdb_stdlog, | |
1341 | "LLW: %s exited.\n", | |
1342 | target_pid_to_str (lp->ptid)); | |
1343 | ||
1344 | delete_lwp (lp->ptid); | |
1345 | ||
1346 | /* If there is at least one more LWP, then the exit signal | |
1347 | was not the end of the debugged application and should be | |
1348 | ignored. */ | |
1349 | if (num_lwps > 0) | |
1350 | { | |
1351 | /* Make sure there is at least one thread running. */ | |
1352 | gdb_assert (iterate_over_lwps (running_callback, NULL)); | |
1353 | ||
1354 | /* Discard the event. */ | |
1355 | status = 0; | |
1356 | continue; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /* Check if the current LWP has previously exited. In the nptl | |
1361 | thread model, LWPs other than the main thread do not issue | |
1362 | signals when they exit so we must check whenever the thread | |
1363 | has stopped. A similar check is made in stop_wait_callback(). */ | |
1364 | if (num_lwps > 1 && !lin_lwp_thread_alive (lp->ptid)) | |
fb0e1ba7 | 1365 | { |
39f77062 | 1366 | if (in_thread_list (lp->ptid)) |
fb0e1ba7 | 1367 | { |
e6328671 | 1368 | /* Core GDB cannot deal with us deleting the current |
6949171e JJ |
1369 | thread. */ |
1370 | if (!ptid_equal (lp->ptid, inferior_ptid)) | |
39f77062 | 1371 | delete_thread (lp->ptid); |
fb0e1ba7 | 1372 | printf_unfiltered ("[%s exited]\n", |
39f77062 | 1373 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 | 1374 | } |
7ca673cd | 1375 | if (debug_lin_lwp) |
6949171e JJ |
1376 | fprintf_unfiltered (gdb_stdlog, |
1377 | "LLW: %s exited.\n", | |
39f77062 | 1378 | target_pid_to_str (lp->ptid)); |
7ca673cd | 1379 | |
39f77062 | 1380 | delete_lwp (lp->ptid); |
fb0e1ba7 MK |
1381 | |
1382 | /* Make sure there is at least one thread running. */ | |
1383 | gdb_assert (iterate_over_lwps (running_callback, NULL)); | |
1384 | ||
1385 | /* Discard the event. */ | |
1386 | status = 0; | |
1387 | continue; | |
1388 | } | |
1389 | ||
1390 | /* Make sure we don't report a SIGSTOP that we sent | |
6949171e | 1391 | ourselves in an attempt to stop an LWP. */ |
9fe7d6bf | 1392 | if (lp->signalled |
6949171e | 1393 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) |
fb0e1ba7 | 1394 | { |
7ca673cd | 1395 | if (debug_lin_lwp) |
6949171e | 1396 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf | 1397 | "LLW: Delayed SIGSTOP caught for %s.\n", |
39f77062 | 1398 | target_pid_to_str (lp->ptid)); |
7ca673cd | 1399 | |
fb0e1ba7 MK |
1400 | /* This is a delayed SIGSTOP. */ |
1401 | lp->signalled = 0; | |
1402 | ||
9fe7d6bf | 1403 | registers_changed (); |
39f77062 | 1404 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
6949171e | 1405 | TARGET_SIGNAL_0); |
9fe7d6bf MS |
1406 | if (debug_lin_lwp) |
1407 | fprintf_unfiltered (gdb_stdlog, | |
1408 | "LLW: %s %s, 0, 0 (discard SIGSTOP)\n", | |
6949171e | 1409 | lp->step ? |
9fe7d6bf MS |
1410 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", |
1411 | target_pid_to_str (lp->ptid)); | |
1412 | ||
fb0e1ba7 | 1413 | lp->stopped = 0; |
fce0e6e1 | 1414 | gdb_assert (lp->resumed); |
fb0e1ba7 MK |
1415 | |
1416 | /* Discard the event. */ | |
1417 | status = 0; | |
1418 | continue; | |
1419 | } | |
1420 | ||
1421 | break; | |
1422 | } | |
1423 | ||
1424 | if (pid == -1) | |
1425 | { | |
1426 | /* Alternate between checking cloned and uncloned processes. */ | |
1427 | options ^= __WCLONE; | |
1428 | ||
1429 | /* And suspend every time we have checked both. */ | |
1430 | if (options & __WCLONE) | |
1431 | sigsuspend (&suspend_mask); | |
1432 | } | |
1433 | ||
1434 | /* We shouldn't end up here unless we want to try again. */ | |
1435 | gdb_assert (status == 0); | |
1436 | } | |
1437 | ||
1438 | clear_sigio_trap (); | |
1439 | clear_sigint_trap (); | |
1440 | ||
1441 | gdb_assert (lp); | |
1442 | ||
1443 | /* Don't report signals that GDB isn't interested in, such as | |
1444 | signals that are neither printed nor stopped upon. Stopping all | |
1445 | threads can be a bit time-consuming so if we want decent | |
1446 | performance with heavily multi-threaded programs, especially when | |
1447 | they're using a high frequency timer, we'd better avoid it if we | |
1448 | can. */ | |
1449 | ||
1450 | if (WIFSTOPPED (status)) | |
1451 | { | |
1452 | int signo = target_signal_from_host (WSTOPSIG (status)); | |
1453 | ||
1454 | if (signal_stop_state (signo) == 0 | |
1455 | && signal_print_state (signo) == 0 | |
1456 | && signal_pass_state (signo) == 1) | |
1457 | { | |
fce0e6e1 | 1458 | /* FIMXE: kettenis/2001-06-06: Should we resume all threads |
6949171e JJ |
1459 | here? It is not clear we should. GDB may not expect |
1460 | other threads to run. On the other hand, not resuming | |
1461 | newly attached threads may cause an unwanted delay in | |
1462 | getting them running. */ | |
9fe7d6bf | 1463 | registers_changed (); |
c4365b19 | 1464 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo); |
9fe7d6bf MS |
1465 | if (debug_lin_lwp) |
1466 | fprintf_unfiltered (gdb_stdlog, | |
1467 | "LLW: %s %s, %s (preempt 'handle')\n", | |
6949171e JJ |
1468 | lp->step ? |
1469 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", | |
9fe7d6bf MS |
1470 | target_pid_to_str (lp->ptid), |
1471 | signo ? strsignal (signo) : "0"); | |
fce0e6e1 | 1472 | lp->stopped = 0; |
fb0e1ba7 MK |
1473 | status = 0; |
1474 | goto retry; | |
1475 | } | |
de4ca854 | 1476 | |
6949171e | 1477 | if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0) |
de4ca854 MK |
1478 | { |
1479 | /* If ^C/BREAK is typed at the tty/console, SIGINT gets | |
6949171e JJ |
1480 | forwarded to the entire process group, that is, all LWP's |
1481 | will receive it. Since we only want to report it once, | |
1482 | we try to flush it from all LWPs except this one. */ | |
de4ca854 MK |
1483 | sigaddset (&flush_mask, SIGINT); |
1484 | } | |
fb0e1ba7 MK |
1485 | } |
1486 | ||
1487 | /* This LWP is stopped now. */ | |
1488 | lp->stopped = 1; | |
1489 | ||
b1aeb4c5 | 1490 | if (debug_lin_lwp) |
9fe7d6bf | 1491 | fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n", |
6949171e | 1492 | status_to_str (status), target_pid_to_str (lp->ptid)); |
b1aeb4c5 | 1493 | |
fb0e1ba7 MK |
1494 | /* Now stop all other LWP's ... */ |
1495 | iterate_over_lwps (stop_callback, NULL); | |
1496 | ||
1497 | /* ... and wait until all of them have reported back that they're no | |
1498 | longer running. */ | |
de4ca854 | 1499 | iterate_over_lwps (stop_wait_callback, &flush_mask); |
fb0e1ba7 | 1500 | |
00d4fce6 MK |
1501 | /* If we're not waiting for a specific LWP, choose an event LWP from |
1502 | among those that have had events. Giving equal priority to all | |
1503 | LWPs that have had events helps prevent starvation. */ | |
1504 | if (pid == -1) | |
1505 | select_event_lwp (&lp, &status); | |
b1aeb4c5 | 1506 | |
00d4fce6 MK |
1507 | /* Now that we've selected our final event LWP, cancel any |
1508 | breakpoints in other LWPs that have hit a GDB breakpoint. See | |
1509 | the comment in cancel_breakpoints_callback to find out why. */ | |
1510 | iterate_over_lwps (cancel_breakpoints_callback, lp); | |
b1aeb4c5 | 1511 | |
fb0e1ba7 MK |
1512 | /* If we're not running in "threaded" mode, we'll report the bare |
1513 | process id. */ | |
1514 | ||
1515 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) | |
b1aeb4c5 MS |
1516 | { |
1517 | trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); | |
1518 | if (debug_lin_lwp) | |
6949171e | 1519 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
1520 | "LLW: trap_ptid is %s.\n", |
1521 | target_pid_to_str (trap_ptid)); | |
b1aeb4c5 | 1522 | } |
fb0e1ba7 | 1523 | else |
39f77062 | 1524 | trap_ptid = null_ptid; |
fb0e1ba7 MK |
1525 | |
1526 | store_waitstatus (ourstatus, status); | |
39f77062 | 1527 | return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); |
fb0e1ba7 MK |
1528 | } |
1529 | ||
1530 | static int | |
1531 | kill_callback (struct lwp_info *lp, void *data) | |
1532 | { | |
9fe7d6bf | 1533 | errno = 0; |
39f77062 | 1534 | ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); |
9fe7d6bf | 1535 | if (debug_lin_lwp) |
6949171e | 1536 | fprintf_unfiltered (gdb_stdlog, |
9fe7d6bf MS |
1537 | "KC: PTRACE_KILL %s, 0, 0 (%s)\n", |
1538 | target_pid_to_str (lp->ptid), | |
1539 | errno ? safe_strerror (errno) : "OK"); | |
1540 | ||
fb0e1ba7 MK |
1541 | return 0; |
1542 | } | |
1543 | ||
1544 | static int | |
1545 | kill_wait_callback (struct lwp_info *lp, void *data) | |
1546 | { | |
1547 | pid_t pid; | |
1548 | ||
1549 | /* We must make sure that there are no pending events (delayed | |
1550 | SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current | |
1551 | program doesn't interfere with any following debugging session. */ | |
1552 | ||
1553 | /* For cloned processes we must check both with __WCLONE and | |
1554 | without, since the exit status of a cloned process isn't reported | |
1555 | with __WCLONE. */ | |
cacab7c4 | 1556 | if (lp->cloned) |
fb0e1ba7 MK |
1557 | { |
1558 | do | |
1559 | { | |
39f77062 | 1560 | pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); |
9fe7d6bf MS |
1561 | if (pid != (pid_t) -1 && debug_lin_lwp) |
1562 | { | |
1563 | fprintf_unfiltered (gdb_stdlog, | |
1564 | "KWC: wait %s received unknown.\n", | |
1565 | target_pid_to_str (lp->ptid)); | |
1566 | } | |
fb0e1ba7 | 1567 | } |
39f77062 | 1568 | while (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 MK |
1569 | |
1570 | gdb_assert (pid == -1 && errno == ECHILD); | |
1571 | } | |
1572 | ||
1573 | do | |
1574 | { | |
39f77062 | 1575 | pid = waitpid (GET_LWP (lp->ptid), NULL, 0); |
9fe7d6bf MS |
1576 | if (pid != (pid_t) -1 && debug_lin_lwp) |
1577 | { | |
1578 | fprintf_unfiltered (gdb_stdlog, | |
6949171e | 1579 | "KWC: wait %s received unk.\n", |
9fe7d6bf MS |
1580 | target_pid_to_str (lp->ptid)); |
1581 | } | |
fb0e1ba7 | 1582 | } |
39f77062 | 1583 | while (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 MK |
1584 | |
1585 | gdb_assert (pid == -1 && errno == ECHILD); | |
1586 | return 0; | |
1587 | } | |
1588 | ||
1589 | static void | |
1590 | lin_lwp_kill (void) | |
1591 | { | |
1592 | /* Kill all LWP's ... */ | |
1593 | iterate_over_lwps (kill_callback, NULL); | |
1594 | ||
1595 | /* ... and wait until we've flushed all events. */ | |
1596 | iterate_over_lwps (kill_wait_callback, NULL); | |
1597 | ||
1598 | target_mourn_inferior (); | |
1599 | } | |
1600 | ||
1601 | static void | |
1602 | lin_lwp_create_inferior (char *exec_file, char *allargs, char **env) | |
1603 | { | |
c194fbe1 | 1604 | child_ops.to_create_inferior (exec_file, allargs, env); |
fb0e1ba7 MK |
1605 | } |
1606 | ||
6949171e | 1607 | static void |
fb0e1ba7 MK |
1608 | lin_lwp_mourn_inferior (void) |
1609 | { | |
c194fbe1 | 1610 | trap_ptid = null_ptid; |
fb0e1ba7 | 1611 | |
c194fbe1 | 1612 | /* Destroy LWP info; it's no longer valid. */ |
fb0e1ba7 MK |
1613 | init_lwp_list (); |
1614 | ||
4c8de859 | 1615 | /* Restore the original signal mask. */ |
3f07c44b MK |
1616 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
1617 | sigemptyset (&blocked_mask); | |
1618 | ||
c194fbe1 | 1619 | child_ops.to_mourn_inferior (); |
fb0e1ba7 MK |
1620 | } |
1621 | ||
fb0e1ba7 MK |
1622 | static int |
1623 | lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, | |
6949171e | 1624 | struct mem_attrib *attrib, struct target_ops *target) |
fb0e1ba7 | 1625 | { |
39f77062 | 1626 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 MK |
1627 | int xfer; |
1628 | ||
39f77062 KB |
1629 | if (is_lwp (inferior_ptid)) |
1630 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); | |
fb0e1ba7 | 1631 | |
eb784848 DJ |
1632 | xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target); |
1633 | if (xfer == 0) | |
1634 | xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target); | |
fb0e1ba7 MK |
1635 | |
1636 | do_cleanups (old_chain); | |
1637 | return xfer; | |
1638 | } | |
1639 | ||
1640 | static int | |
39f77062 | 1641 | lin_lwp_thread_alive (ptid_t ptid) |
fb0e1ba7 | 1642 | { |
39f77062 | 1643 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 MK |
1644 | |
1645 | errno = 0; | |
39f77062 | 1646 | ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); |
9fe7d6bf MS |
1647 | if (debug_lin_lwp) |
1648 | fprintf_unfiltered (gdb_stdlog, | |
1649 | "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n", | |
6949171e | 1650 | target_pid_to_str (ptid), |
9fe7d6bf | 1651 | errno ? safe_strerror (errno) : "OK"); |
fb0e1ba7 MK |
1652 | if (errno) |
1653 | return 0; | |
1654 | ||
1655 | return 1; | |
1656 | } | |
1657 | ||
1658 | static char * | |
39f77062 | 1659 | lin_lwp_pid_to_str (ptid_t ptid) |
fb0e1ba7 MK |
1660 | { |
1661 | static char buf[64]; | |
1662 | ||
39f77062 | 1663 | if (is_lwp (ptid)) |
fb0e1ba7 | 1664 | { |
b08cfdb6 | 1665 | snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); |
fb0e1ba7 MK |
1666 | return buf; |
1667 | } | |
1668 | ||
39f77062 | 1669 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1670 | } |
1671 | ||
1672 | static void | |
1673 | init_lin_lwp_ops (void) | |
1674 | { | |
1675 | #if 0 | |
1676 | lin_lwp_ops.to_open = lin_lwp_open; | |
1677 | #endif | |
1678 | lin_lwp_ops.to_shortname = "lwp-layer"; | |
1679 | lin_lwp_ops.to_longname = "lwp-layer"; | |
1680 | lin_lwp_ops.to_doc = "Low level threads support (LWP layer)"; | |
1681 | lin_lwp_ops.to_attach = lin_lwp_attach; | |
1682 | lin_lwp_ops.to_detach = lin_lwp_detach; | |
1683 | lin_lwp_ops.to_resume = lin_lwp_resume; | |
1684 | lin_lwp_ops.to_wait = lin_lwp_wait; | |
02ae7771 AC |
1685 | /* fetch_inferior_registers and store_inferior_registers will |
1686 | honor the LWP id, so we can use them directly. */ | |
1687 | lin_lwp_ops.to_fetch_registers = fetch_inferior_registers; | |
1688 | lin_lwp_ops.to_store_registers = store_inferior_registers; | |
fb0e1ba7 MK |
1689 | lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory; |
1690 | lin_lwp_ops.to_kill = lin_lwp_kill; | |
1691 | lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior; | |
1692 | lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior; | |
1693 | lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive; | |
1694 | lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str; | |
1695 | lin_lwp_ops.to_stratum = thread_stratum; | |
1696 | lin_lwp_ops.to_has_thread_control = tc_schedlock; | |
1697 | lin_lwp_ops.to_magic = OPS_MAGIC; | |
1698 | } | |
1699 | ||
1700 | static void | |
1701 | sigchld_handler (int signo) | |
1702 | { | |
1703 | /* Do nothing. The only reason for this handler is that it allows | |
1704 | us to use sigsuspend in lin_lwp_wait above to wait for the | |
1705 | arrival of a SIGCHLD. */ | |
1706 | } | |
1707 | ||
1708 | void | |
1709 | _initialize_lin_lwp (void) | |
1710 | { | |
1711 | struct sigaction action; | |
fb0e1ba7 MK |
1712 | |
1713 | extern void thread_db_init (struct target_ops *); | |
1714 | ||
1715 | init_lin_lwp_ops (); | |
1716 | add_target (&lin_lwp_ops); | |
1717 | thread_db_init (&lin_lwp_ops); | |
1718 | ||
4c8de859 | 1719 | /* Save the original signal mask. */ |
3f07c44b MK |
1720 | sigprocmask (SIG_SETMASK, NULL, &normal_mask); |
1721 | ||
fb0e1ba7 MK |
1722 | action.sa_handler = sigchld_handler; |
1723 | sigemptyset (&action.sa_mask); | |
1724 | action.sa_flags = 0; | |
1725 | sigaction (SIGCHLD, &action, NULL); | |
1726 | ||
3f07c44b MK |
1727 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
1728 | sigprocmask (SIG_SETMASK, NULL, &suspend_mask); | |
fb0e1ba7 | 1729 | sigdelset (&suspend_mask, SIGCHLD); |
3f07c44b MK |
1730 | |
1731 | sigemptyset (&blocked_mask); | |
7ca673cd MS |
1732 | |
1733 | add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger, | |
6949171e | 1734 | (char *) &debug_lin_lwp, |
8605d56e | 1735 | "Set debugging of GNU/Linux lwp module.\n\ |
6949171e | 1736 | Enables printf debugging output.\n", &setdebuglist), &showdebuglist); |
fb0e1ba7 MK |
1737 | } |
1738 | \f | |
1739 | ||
1740 | /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to | |
8605d56e AC |
1741 | the GNU/Linux Threads library and therefore doesn't really belong |
1742 | here. */ | |
fb0e1ba7 MK |
1743 | |
1744 | /* Read variable NAME in the target and return its value if found. | |
1745 | Otherwise return zero. It is assumed that the type of the variable | |
1746 | is `int'. */ | |
1747 | ||
1748 | static int | |
1749 | get_signo (const char *name) | |
1750 | { | |
1751 | struct minimal_symbol *ms; | |
1752 | int signo; | |
1753 | ||
1754 | ms = lookup_minimal_symbol (name, NULL, NULL); | |
1755 | if (ms == NULL) | |
1756 | return 0; | |
1757 | ||
1758 | if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo, | |
1759 | sizeof (signo)) != 0) | |
1760 | return 0; | |
1761 | ||
1762 | return signo; | |
1763 | } | |
1764 | ||
1765 | /* Return the set of signals used by the threads library in *SET. */ | |
1766 | ||
1767 | void | |
1768 | lin_thread_get_thread_signals (sigset_t *set) | |
1769 | { | |
3f07c44b MK |
1770 | struct sigaction action; |
1771 | int restart, cancel; | |
fb0e1ba7 MK |
1772 | |
1773 | sigemptyset (set); | |
1774 | ||
1775 | restart = get_signo ("__pthread_sig_restart"); | |
1776 | if (restart == 0) | |
1777 | return; | |
1778 | ||
1779 | cancel = get_signo ("__pthread_sig_cancel"); | |
1780 | if (cancel == 0) | |
1781 | return; | |
1782 | ||
1783 | sigaddset (set, restart); | |
1784 | sigaddset (set, cancel); | |
3f07c44b | 1785 | |
8605d56e AC |
1786 | /* The GNU/Linux Threads library makes terminating threads send a |
1787 | special "cancel" signal instead of SIGCHLD. Make sure we catch | |
1788 | those (to prevent them from terminating GDB itself, which is | |
1789 | likely to be their default action) and treat them the same way as | |
1790 | SIGCHLD. */ | |
3f07c44b MK |
1791 | |
1792 | action.sa_handler = sigchld_handler; | |
1793 | sigemptyset (&action.sa_mask); | |
1794 | action.sa_flags = 0; | |
1795 | sigaction (cancel, &action, NULL); | |
1796 | ||
1797 | /* We block the "cancel" signal throughout this code ... */ | |
1798 | sigaddset (&blocked_mask, cancel); | |
1799 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
1800 | ||
1801 | /* ... except during a sigsuspend. */ | |
1802 | sigdelset (&suspend_mask, cancel); | |
fb0e1ba7 | 1803 | } |