Commit | Line | Data |
---|---|---|
fb0e1ba7 | 1 | /* Multi-threaded debugging support for Linux (LWP layer). |
4e052eda | 2 | Copyright 2000, 2001 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" | |
24 | #include <errno.h> | |
25 | #include <signal.h> | |
26 | #include <sys/ptrace.h> | |
27 | #include "gdb_wait.h" | |
28 | ||
29 | #include "gdbthread.h" | |
30 | #include "inferior.h" | |
31 | #include "target.h" | |
4e052eda | 32 | #include "regcache.h" |
7ca673cd | 33 | #include "gdbcmd.h" |
fb0e1ba7 | 34 | |
7ca673cd | 35 | static int debug_lin_lwp; |
fb0e1ba7 | 36 | extern const char *strsignal (int sig); |
fb0e1ba7 MK |
37 | |
38 | /* On Linux there are no real LWP's. The closest thing to LWP's are | |
39 | processes sharing the same VM space. A multi-threaded process is | |
40 | basically a group of such processes. However, such a grouping is | |
41 | almost entirely a user-space issue; the kernel doesn't enforce such | |
42 | a grouping at all (this might change in the future). In general, | |
43 | we'll rely on the threads library (i.e. the LinuxThreads library) | |
44 | to provide such a grouping. | |
45 | ||
46 | It is perfectly well possible to write a multi-threaded application | |
47 | without the assistance of a threads library, by using the clone | |
48 | system call directly. This module should be able to give some | |
49 | rudimentary support for debugging such applications if developers | |
50 | specify the CLONE_PTRACE flag in the clone system call, and are | |
51 | using Linux 2.4 or above. | |
52 | ||
53 | Note that there are some peculiarities in Linux that affect this | |
54 | code: | |
55 | ||
56 | - In general one should specify the __WCLONE flag to waitpid in | |
3f07c44b MK |
57 | order to make it report events for any of the cloned processes |
58 | (and leave it out for the initial process). However, if a cloned | |
59 | process has exited the exit status is only reported if the | |
60 | __WCLONE flag is absent. Linux 2.4 has a __WALL flag, but we | |
61 | cannot use it since GDB must work on older systems too. | |
fb0e1ba7 MK |
62 | |
63 | - When a traced, cloned process exits and is waited for by the | |
4c8de859 | 64 | debugger, the kernel reassigns it to the original parent and |
fb0e1ba7 MK |
65 | keeps it around as a "zombie". Somehow, the LinuxThreads library |
66 | doesn't notice this, which leads to the "zombie problem": When | |
67 | debugged a multi-threaded process that spawns a lot of threads | |
68 | will run out of processes, even if the threads exit, because the | |
69 | "zombies" stay around. */ | |
70 | ||
71 | /* Structure describing a LWP. */ | |
72 | struct lwp_info | |
73 | { | |
74 | /* The process id of the LWP. This is a combination of the LWP id | |
75 | and overall process id. */ | |
39f77062 | 76 | ptid_t ptid; |
fb0e1ba7 MK |
77 | |
78 | /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report | |
79 | it back yet). */ | |
80 | int signalled; | |
81 | ||
82 | /* Non-zero if this LWP is stopped. */ | |
83 | int stopped; | |
84 | ||
fce0e6e1 MK |
85 | /* Non-zero if this LWP will be/has been resumed. Note that an LWP |
86 | can be marked both as stopped and resumed at the same time. This | |
87 | happens if we try to resume an LWP that has a wait status | |
88 | pending. We shouldn't let the LWP run until that wait status has | |
89 | been processed, but we should not report that wait status if GDB | |
90 | didn't try to let the LWP run. */ | |
91 | int resumed; | |
92 | ||
fb0e1ba7 MK |
93 | /* If non-zero, a pending wait status. */ |
94 | int status; | |
95 | ||
96 | /* Non-zero if we were stepping this LWP. */ | |
97 | int step; | |
98 | ||
99 | /* Next LWP in list. */ | |
100 | struct lwp_info *next; | |
101 | }; | |
102 | ||
103 | /* List of known LWPs. */ | |
104 | static struct lwp_info *lwp_list; | |
105 | ||
106 | /* Number of LWPs in the list. */ | |
107 | static int num_lwps; | |
108 | ||
109 | /* Non-zero if we're running in "threaded" mode. */ | |
110 | static int threaded; | |
111 | \f | |
112 | ||
ca6724c1 KB |
113 | #define GET_LWP(ptid) ptid_get_lwp (ptid) |
114 | #define GET_PID(ptid) ptid_get_pid (ptid) | |
115 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) | |
116 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) | |
fb0e1ba7 MK |
117 | |
118 | #define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid)) | |
119 | ||
120 | /* If the last reported event was a SIGTRAP, this variable is set to | |
121 | the process id of the LWP/thread that got it. */ | |
39f77062 | 122 | ptid_t trap_ptid; |
fb0e1ba7 MK |
123 | \f |
124 | ||
125 | /* This module's target-specific operations. */ | |
126 | static struct target_ops lin_lwp_ops; | |
127 | ||
128 | /* The standard child operations. */ | |
129 | extern struct target_ops child_ops; | |
130 | ||
3f07c44b MK |
131 | /* Since we cannot wait (in lin_lwp_wait) for the initial process and |
132 | any cloned processes with a single call to waitpid, we have to use | |
4c8de859 | 133 | the WNOHANG flag and call waitpid in a loop. To optimize |
3f07c44b MK |
134 | things a bit we use `sigsuspend' to wake us up when a process has |
135 | something to report (it will send us a SIGCHLD if it has). To make | |
136 | this work we have to juggle with the signal mask. We save the | |
4c8de859 | 137 | original signal mask such that we can restore it before creating a |
3f07c44b MK |
138 | new process in order to avoid blocking certain signals in the |
139 | inferior. We then block SIGCHLD during the waitpid/sigsuspend | |
140 | loop. */ | |
141 | ||
4c8de859 | 142 | /* Original signal mask. */ |
3f07c44b MK |
143 | static sigset_t normal_mask; |
144 | ||
fb0e1ba7 MK |
145 | /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in |
146 | _initialize_lin_lwp. */ | |
147 | static sigset_t suspend_mask; | |
3f07c44b MK |
148 | |
149 | /* Signals to block to make that sigsuspend work. */ | |
150 | static sigset_t blocked_mask; | |
fb0e1ba7 MK |
151 | \f |
152 | ||
153 | /* Prototypes for local functions. */ | |
c194fbe1 | 154 | static int stop_wait_callback (struct lwp_info *lp, void *data); |
fb0e1ba7 MK |
155 | \f |
156 | ||
c194fbe1 MK |
157 | /* Initialize the list of LWPs. Note that this module, contrary to |
158 | what GDB's generic threads layer does for its thread list, | |
159 | re-initializes the LWP lists whenever we mourn or detach (which | |
160 | doesn't involve mourning) the inferior. */ | |
fb0e1ba7 MK |
161 | |
162 | static void | |
163 | init_lwp_list (void) | |
164 | { | |
165 | struct lwp_info *lp, *lpnext; | |
166 | ||
167 | for (lp = lwp_list; lp; lp = lpnext) | |
168 | { | |
169 | lpnext = lp->next; | |
b8c9b27d | 170 | xfree (lp); |
fb0e1ba7 MK |
171 | } |
172 | ||
173 | lwp_list = NULL; | |
174 | num_lwps = 0; | |
175 | threaded = 0; | |
176 | } | |
177 | ||
178 | /* Add the LWP specified by PID to the list. If this causes the | |
179 | number of LWPs to become larger than one, go into "threaded" mode. | |
180 | Return a pointer to the structure describing the new LWP. */ | |
181 | ||
182 | static struct lwp_info * | |
39f77062 | 183 | add_lwp (ptid_t ptid) |
fb0e1ba7 MK |
184 | { |
185 | struct lwp_info *lp; | |
186 | ||
39f77062 | 187 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 MK |
188 | |
189 | lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); | |
190 | ||
191 | memset (lp, 0, sizeof (struct lwp_info)); | |
192 | ||
39f77062 | 193 | lp->ptid = ptid; |
fb0e1ba7 MK |
194 | |
195 | lp->next = lwp_list; | |
196 | lwp_list = lp; | |
197 | if (++num_lwps > 1) | |
198 | threaded = 1; | |
199 | ||
200 | return lp; | |
201 | } | |
202 | ||
203 | /* Remove the LWP specified by PID from the list. */ | |
204 | ||
205 | static void | |
39f77062 | 206 | delete_lwp (ptid_t ptid) |
fb0e1ba7 MK |
207 | { |
208 | struct lwp_info *lp, *lpprev; | |
209 | ||
210 | lpprev = NULL; | |
211 | ||
212 | for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) | |
39f77062 | 213 | if (ptid_equal (lp->ptid, ptid)) |
fb0e1ba7 MK |
214 | break; |
215 | ||
216 | if (!lp) | |
217 | return; | |
218 | ||
219 | /* We don't go back to "non-threaded" mode if the number of threads | |
220 | becomes less than two. */ | |
221 | num_lwps--; | |
222 | ||
223 | if (lpprev) | |
224 | lpprev->next = lp->next; | |
225 | else | |
226 | lwp_list = lp->next; | |
227 | ||
b8c9b27d | 228 | xfree (lp); |
fb0e1ba7 MK |
229 | } |
230 | ||
231 | /* Return a pointer to the structure describing the LWP corresponding | |
232 | to PID. If no corresponding LWP could be found, return NULL. */ | |
233 | ||
234 | static struct lwp_info * | |
39f77062 | 235 | find_lwp_pid (ptid_t ptid) |
fb0e1ba7 MK |
236 | { |
237 | struct lwp_info *lp; | |
39f77062 | 238 | int lwp; |
fb0e1ba7 | 239 | |
39f77062 KB |
240 | if (is_lwp (ptid)) |
241 | lwp = GET_LWP (ptid); | |
242 | else | |
243 | lwp = GET_PID (ptid); | |
fb0e1ba7 MK |
244 | |
245 | for (lp = lwp_list; lp; lp = lp->next) | |
39f77062 | 246 | if (lwp == GET_LWP (lp->ptid)) |
fb0e1ba7 MK |
247 | return lp; |
248 | ||
249 | return NULL; | |
250 | } | |
251 | ||
252 | /* Call CALLBACK with its second argument set to DATA for every LWP in | |
253 | the list. If CALLBACK returns 1 for a particular LWP, return a | |
254 | pointer to the structure describing that LWP immediately. | |
255 | Otherwise return NULL. */ | |
256 | ||
257 | struct lwp_info * | |
258 | iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) | |
259 | { | |
fce0e6e1 | 260 | struct lwp_info *lp, *lpnext; |
fb0e1ba7 | 261 | |
fce0e6e1 MK |
262 | for (lp = lwp_list; lp; lp = lpnext) |
263 | { | |
264 | lpnext = lp->next; | |
265 | if ((*callback) (lp, data)) | |
266 | return lp; | |
267 | } | |
fb0e1ba7 MK |
268 | |
269 | return NULL; | |
270 | } | |
271 | \f | |
272 | ||
e02bc4cc DS |
273 | /* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP |
274 | layer. | |
275 | ||
276 | Note that this implementation is potentially redundant now that | |
8849f47d JL |
277 | default_prepare_to_proceed() has been added. |
278 | ||
279 | FIXME This may not support switching threads after Ctrl-C | |
280 | correctly. The default implementation does support this. */ | |
fb0e1ba7 MK |
281 | |
282 | int | |
283 | lin_lwp_prepare_to_proceed (void) | |
284 | { | |
39f77062 KB |
285 | if (! ptid_equal (trap_ptid, null_ptid) |
286 | && ! ptid_equal (inferior_ptid, trap_ptid)) | |
fb0e1ba7 MK |
287 | { |
288 | /* Switched over from TRAP_PID. */ | |
289 | CORE_ADDR stop_pc = read_pc (); | |
290 | CORE_ADDR trap_pc; | |
291 | ||
292 | /* Avoid switching where it wouldn't do any good, i.e. if both | |
293 | threads are at the same breakpoint. */ | |
39f77062 | 294 | trap_pc = read_pc_pid (trap_ptid); |
fb0e1ba7 MK |
295 | if (trap_pc != stop_pc && breakpoint_here_p (trap_pc)) |
296 | { | |
297 | /* User hasn't deleted the breakpoint. Return non-zero, and | |
298 | switch back to TRAP_PID. */ | |
39f77062 | 299 | inferior_ptid = trap_ptid; |
fb0e1ba7 MK |
300 | |
301 | /* FIXME: Is this stuff really necessary? */ | |
302 | flush_cached_frames (); | |
303 | registers_changed (); | |
304 | ||
305 | return 1; | |
306 | } | |
307 | } | |
308 | ||
309 | return 0; | |
310 | } | |
311 | \f | |
312 | ||
313 | #if 0 | |
314 | static void | |
315 | lin_lwp_open (char *args, int from_tty) | |
316 | { | |
317 | push_target (&lin_lwp_ops); | |
318 | } | |
319 | #endif | |
320 | ||
321 | /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print | |
322 | a message telling the user that a new LWP has been added to the | |
323 | process. */ | |
324 | ||
325 | void | |
39f77062 | 326 | lin_lwp_attach_lwp (ptid_t ptid, int verbose) |
fb0e1ba7 MK |
327 | { |
328 | struct lwp_info *lp; | |
329 | ||
39f77062 | 330 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 MK |
331 | |
332 | if (verbose) | |
39f77062 | 333 | printf_filtered ("[New %s]\n", target_pid_to_str (ptid)); |
fb0e1ba7 | 334 | |
c194fbe1 MK |
335 | /* We assume that we're already tracing the initial process. */ |
336 | if (is_cloned (ptid) && ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) | |
39f77062 | 337 | error ("Can't attach %s: %s", target_pid_to_str (ptid), strerror (errno)); |
fb0e1ba7 | 338 | |
c194fbe1 MK |
339 | lp = find_lwp_pid (ptid); |
340 | if (lp == NULL) | |
341 | lp = add_lwp (ptid); | |
342 | ||
343 | if (is_cloned (ptid)) | |
c4365b19 MS |
344 | { |
345 | lp->signalled = 1; | |
346 | stop_wait_callback (lp, NULL); | |
347 | } | |
fb0e1ba7 MK |
348 | } |
349 | ||
350 | static void | |
351 | lin_lwp_attach (char *args, int from_tty) | |
352 | { | |
c194fbe1 MK |
353 | struct lwp_info *lp; |
354 | ||
fb0e1ba7 MK |
355 | /* FIXME: We should probably accept a list of process id's, and |
356 | attach all of them. */ | |
c194fbe1 MK |
357 | child_ops.to_attach (args, from_tty); |
358 | ||
359 | /* Add the initial process as the first LWP to the list. */ | |
01263b57 | 360 | lp = add_lwp (BUILD_LWP (PIDGET (inferior_ptid), PIDGET (inferior_ptid))); |
c194fbe1 MK |
361 | |
362 | /* Make sure the initial process is stopped. The user-level threads | |
363 | layer might want to poke around in the inferior, and that won't | |
364 | work if things haven't stabilized yet. */ | |
365 | lp->signalled = 1; | |
366 | stop_wait_callback (lp, NULL); | |
367 | gdb_assert (lp->status == 0); | |
368 | ||
369 | /* Fake the SIGSTOP that core GDB expects. */ | |
370 | lp->status = W_STOPCODE (SIGSTOP); | |
fce0e6e1 | 371 | lp->resumed = 1; |
c194fbe1 MK |
372 | } |
373 | ||
374 | static int | |
375 | detach_callback (struct lwp_info *lp, void *data) | |
376 | { | |
377 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
378 | ||
379 | if (debug_lin_lwp && lp->status) | |
b08cfdb6 | 380 | fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n", |
c194fbe1 MK |
381 | strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid)); |
382 | ||
383 | while (lp->signalled && lp->stopped) | |
384 | { | |
385 | if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, | |
386 | WSTOPSIG (lp->status)) < 0) | |
387 | error ("Can't continue %s: %s", target_pid_to_str (lp->ptid), | |
388 | strerror (errno)); | |
389 | ||
390 | lp->stopped = 0; | |
c4365b19 | 391 | lp->signalled = 0; |
c194fbe1 MK |
392 | lp->status = 0; |
393 | stop_wait_callback (lp, NULL); | |
394 | ||
395 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); | |
396 | } | |
397 | ||
398 | if (is_cloned (lp->ptid)) | |
399 | { | |
400 | if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, | |
401 | WSTOPSIG (lp->status)) < 0) | |
402 | error ("Can't detach %s: %s", target_pid_to_str (lp->ptid), | |
403 | strerror (errno)); | |
404 | ||
405 | delete_lwp (lp->ptid); | |
406 | } | |
407 | ||
408 | return 0; | |
fb0e1ba7 MK |
409 | } |
410 | ||
411 | static void | |
412 | lin_lwp_detach (char *args, int from_tty) | |
413 | { | |
c194fbe1 MK |
414 | iterate_over_lwps (detach_callback, NULL); |
415 | ||
416 | /* Only the initial (uncloned) process should be left right now. */ | |
417 | gdb_assert (num_lwps == 1); | |
418 | ||
419 | trap_ptid = null_ptid; | |
420 | ||
421 | /* Destroy LWP info; it's no longer valid. */ | |
422 | init_lwp_list (); | |
423 | ||
424 | /* Restore the original signal mask. */ | |
425 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); | |
426 | sigemptyset (&blocked_mask); | |
427 | ||
01263b57 | 428 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
c194fbe1 | 429 | child_ops.to_detach (args, from_tty); |
fb0e1ba7 MK |
430 | } |
431 | \f | |
432 | ||
433 | struct private_thread_info | |
434 | { | |
435 | int lwpid; | |
436 | }; | |
437 | ||
438 | /* Return non-zero if TP corresponds to the LWP specified by DATA | |
439 | (which is assumed to be a pointer to a `struct lwp_info'. */ | |
440 | ||
441 | static int | |
442 | find_lwp_callback (struct thread_info *tp, void *data) | |
443 | { | |
444 | struct lwp_info *lp = data; | |
445 | ||
39f77062 | 446 | if (tp->private->lwpid == GET_LWP (lp->ptid)) |
fb0e1ba7 MK |
447 | return 1; |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | /* Resume LP. */ | |
453 | ||
454 | static int | |
455 | resume_callback (struct lwp_info *lp, void *data) | |
456 | { | |
457 | if (lp->stopped && lp->status == 0) | |
458 | { | |
459 | struct thread_info *tp; | |
460 | ||
b1aeb4c5 | 461 | #if 0 |
fb0e1ba7 MK |
462 | /* FIXME: kettenis/2000-08-26: This should really be handled |
463 | properly by core GDB. */ | |
464 | ||
39f77062 | 465 | tp = find_thread_pid (lp->ptid); |
fb0e1ba7 MK |
466 | if (tp == NULL) |
467 | tp = iterate_over_threads (find_lwp_callback, lp); | |
468 | gdb_assert (tp); | |
469 | ||
470 | /* If we were previously stepping the thread, and now continue | |
471 | the thread we must invalidate the stepping range. However, | |
472 | if there is a step_resume breakpoint for this thread, we must | |
473 | preserve the stepping range to make it possible to continue | |
474 | stepping once we hit it. */ | |
475 | if (tp->step_range_end && tp->step_resume_breakpoint == NULL) | |
476 | { | |
477 | gdb_assert (lp->step); | |
478 | tp->step_range_start = tp->step_range_end = 0; | |
479 | } | |
480 | #endif | |
481 | ||
39f77062 | 482 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0); |
fb0e1ba7 MK |
483 | lp->stopped = 0; |
484 | lp->step = 0; | |
485 | } | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
fce0e6e1 MK |
490 | static int |
491 | resume_clear_callback (struct lwp_info *lp, void *data) | |
492 | { | |
493 | lp->resumed = 0; | |
494 | return 0; | |
495 | } | |
496 | ||
497 | static int | |
498 | resume_set_callback (struct lwp_info *lp, void *data) | |
499 | { | |
500 | lp->resumed = 1; | |
501 | return 0; | |
502 | } | |
503 | ||
fb0e1ba7 | 504 | static void |
39f77062 | 505 | lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo) |
fb0e1ba7 MK |
506 | { |
507 | struct lwp_info *lp; | |
508 | int resume_all; | |
509 | ||
510 | /* Apparently the interpretation of PID is dependent on STEP: If | |
511 | STEP is non-zero, a specific PID means `step only this process | |
512 | id'. But if STEP is zero, then PID means `continue *all* | |
513 | processes, but give the signal only to this one'. */ | |
39f77062 | 514 | resume_all = (PIDGET (ptid) == -1) || !step; |
fb0e1ba7 | 515 | |
fce0e6e1 MK |
516 | if (resume_all) |
517 | iterate_over_lwps (resume_set_callback, NULL); | |
518 | else | |
519 | iterate_over_lwps (resume_clear_callback, NULL); | |
520 | ||
fb0e1ba7 | 521 | /* If PID is -1, it's the current inferior that should be |
c4365b19 | 522 | handled specially. */ |
39f77062 KB |
523 | if (PIDGET (ptid) == -1) |
524 | ptid = inferior_ptid; | |
fb0e1ba7 | 525 | |
39f77062 | 526 | lp = find_lwp_pid (ptid); |
fb0e1ba7 MK |
527 | if (lp) |
528 | { | |
39f77062 | 529 | ptid = pid_to_ptid (GET_LWP (lp->ptid)); |
fb0e1ba7 | 530 | |
fb0e1ba7 MK |
531 | /* Remember if we're stepping. */ |
532 | lp->step = step; | |
533 | ||
fce0e6e1 MK |
534 | /* Mark this LWP as resumed. */ |
535 | lp->resumed = 1; | |
536 | ||
fb0e1ba7 MK |
537 | /* If we have a pending wait status for this thread, there is no |
538 | point in resuming the process. */ | |
539 | if (lp->status) | |
540 | { | |
541 | /* FIXME: What should we do if we are supposed to continue | |
542 | this thread with a signal? */ | |
543 | gdb_assert (signo == TARGET_SIGNAL_0); | |
544 | return; | |
545 | } | |
40564aca MK |
546 | |
547 | /* Mark LWP as not stopped to prevent it from being continued by | |
548 | resume_callback. */ | |
549 | lp->stopped = 0; | |
fb0e1ba7 MK |
550 | } |
551 | ||
552 | if (resume_all) | |
553 | iterate_over_lwps (resume_callback, NULL); | |
554 | ||
39f77062 | 555 | child_resume (ptid, step, signo); |
fb0e1ba7 MK |
556 | } |
557 | \f | |
558 | ||
559 | /* Send a SIGSTOP to LP. */ | |
560 | ||
561 | static int | |
562 | stop_callback (struct lwp_info *lp, void *data) | |
563 | { | |
564 | if (! lp->stopped && ! lp->signalled) | |
565 | { | |
566 | int ret; | |
567 | ||
39f77062 | 568 | ret = kill (GET_LWP (lp->ptid), SIGSTOP); |
fb0e1ba7 MK |
569 | gdb_assert (ret == 0); |
570 | ||
571 | lp->signalled = 1; | |
572 | gdb_assert (lp->status == 0); | |
573 | } | |
574 | ||
575 | return 0; | |
576 | } | |
577 | ||
578 | /* Wait until LP is stopped. */ | |
579 | ||
580 | static int | |
581 | stop_wait_callback (struct lwp_info *lp, void *data) | |
582 | { | |
583 | if (! lp->stopped && lp->signalled) | |
584 | { | |
585 | pid_t pid; | |
586 | int status; | |
587 | ||
588 | gdb_assert (lp->status == 0); | |
589 | ||
39f77062 KB |
590 | pid = waitpid (GET_LWP (lp->ptid), &status, |
591 | is_cloned (lp->ptid) ? __WCLONE : 0); | |
fb0e1ba7 MK |
592 | if (pid == -1 && errno == ECHILD) |
593 | /* OK, the proccess has disappeared. We'll catch the actual | |
3f07c44b | 594 | exit event in lin_lwp_wait. */ |
fb0e1ba7 MK |
595 | return 0; |
596 | ||
39f77062 | 597 | gdb_assert (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 MK |
598 | |
599 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
600 | { | |
601 | gdb_assert (num_lwps > 1); | |
fb0e1ba7 | 602 | |
39f77062 | 603 | if (in_thread_list (lp->ptid)) |
e6328671 MK |
604 | { |
605 | /* Core GDB cannot deal with us deleting the current | |
606 | thread. */ | |
39f77062 KB |
607 | if (!ptid_equal (lp->ptid, inferior_ptid)) |
608 | delete_thread (lp->ptid); | |
e6328671 | 609 | printf_unfiltered ("[%s exited]\n", |
39f77062 | 610 | target_pid_to_str (lp->ptid)); |
e6328671 | 611 | } |
7ca673cd | 612 | if (debug_lin_lwp) |
9085700c | 613 | fprintf_unfiltered (gdb_stdlog, |
39f77062 | 614 | "%s exited.\n", target_pid_to_str (lp->ptid)); |
7ca673cd | 615 | |
39f77062 | 616 | delete_lwp (lp->ptid); |
fb0e1ba7 MK |
617 | return 0; |
618 | } | |
619 | ||
620 | gdb_assert (WIFSTOPPED (status)); | |
fb0e1ba7 MK |
621 | |
622 | if (WSTOPSIG (status) != SIGSTOP) | |
623 | { | |
b1aeb4c5 | 624 | if (WSTOPSIG (status) == SIGTRAP) |
fb0e1ba7 MK |
625 | { |
626 | /* If a LWP other than the LWP that we're reporting an | |
627 | event for has hit a GDB breakpoint (as opposed to | |
628 | some random trap signal), then just arrange for it to | |
629 | hit it again later. We don't keep the SIGTRAP status | |
630 | and don't forward the SIGTRAP signal to the LWP. We | |
631 | will handle the current event, eventually we will | |
632 | resume all LWPs, and this one will get its breakpoint | |
633 | trap again. | |
634 | ||
635 | If we do not do this, then we run the risk that the | |
636 | user will delete or disable the breakpoint, but the | |
637 | thread will have already tripped on it. */ | |
7ca673cd | 638 | |
c4365b19 MS |
639 | /* Now resume this LWP and get the SIGSTOP event. */ |
640 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
b1aeb4c5 MS |
641 | if (debug_lin_lwp) |
642 | { | |
643 | fprintf_unfiltered (gdb_stderr, | |
644 | "SWC: Candidate SIGTRAP event in %ld\n", | |
645 | GET_LWP (lp->ptid)); | |
646 | } | |
647 | /* Hold the SIGTRAP for handling by lin_lwp_wait. */ | |
648 | stop_wait_callback (lp, data); | |
649 | /* If there's another event, throw it back into the queue. */ | |
650 | if (lp->status) | |
651 | kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); | |
652 | /* Save the sigtrap event. */ | |
653 | lp->status = status; | |
654 | return 0; | |
fb0e1ba7 | 655 | } |
5f885618 MS |
656 | else if (WSTOPSIG (status) == SIGINT && |
657 | signal_pass_state (SIGINT) == 0) | |
658 | { | |
659 | /* Since SIGINT gets forwarded to the entire process group | |
660 | (in the case where ^C/BREAK is typed at the tty/console), | |
661 | just ignore all SIGINT events from all lwp's except for | |
662 | the one that was caught by lin_lwp_wait. */ | |
c4365b19 | 663 | |
b1aeb4c5 | 664 | /* Now resume this LWP and get the SIGSTOP event. */ |
c4365b19 | 665 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
b1aeb4c5 | 666 | return stop_wait_callback (lp, data); |
5f885618 | 667 | } |
fb0e1ba7 MK |
668 | else |
669 | { | |
b1aeb4c5 MS |
670 | /* The thread was stopped with a signal other than |
671 | SIGSTOP, and didn't accidentally trip a breakpoint. */ | |
672 | ||
7ca673cd | 673 | if (debug_lin_lwp) |
b1aeb4c5 MS |
674 | { |
675 | fprintf_unfiltered (gdb_stderr, | |
676 | "SWC: Pending event %d in %ld\n", | |
677 | WSTOPSIG (status), GET_LWP (lp->ptid)); | |
678 | } | |
679 | /* Now resume this LWP and get the SIGSTOP event. */ | |
680 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); | |
7ca673cd | 681 | |
b1aeb4c5 MS |
682 | /* Hold this event/waitstatus while we check to see if |
683 | there are any more (we still want to get that SIGSTOP). */ | |
684 | stop_wait_callback (lp, data); | |
685 | /* If the lp->status field is still empty, use it to hold | |
686 | this event. If not, then this event must be returned | |
687 | to the event queue of the LWP. */ | |
688 | if (lp->status == 0) | |
689 | lp->status = status; | |
690 | else | |
691 | kill (GET_LWP (lp->ptid), WSTOPSIG (status)); | |
692 | return 0; | |
fb0e1ba7 MK |
693 | } |
694 | } | |
695 | else | |
696 | { | |
697 | /* We caught the SIGSTOP that we intended to catch, so | |
698 | there's no SIGSTOP pending. */ | |
b1aeb4c5 | 699 | lp->stopped = 1; |
fb0e1ba7 MK |
700 | lp->signalled = 0; |
701 | } | |
702 | } | |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
707 | /* Return non-zero if LP has a wait status pending. */ | |
708 | ||
709 | static int | |
710 | status_callback (struct lwp_info *lp, void *data) | |
711 | { | |
fce0e6e1 MK |
712 | /* Only report a pending wait status if we pretend that this has |
713 | indeed been resumed. */ | |
714 | return (lp->status != 0 && lp->resumed); | |
fb0e1ba7 MK |
715 | } |
716 | ||
717 | /* Return non-zero if LP isn't stopped. */ | |
718 | ||
719 | static int | |
720 | running_callback (struct lwp_info *lp, void *data) | |
721 | { | |
722 | return (lp->stopped == 0); | |
723 | } | |
724 | ||
b1aeb4c5 MS |
725 | /* Count the LWP's that have had events. */ |
726 | ||
727 | static int | |
728 | count_events_callback (struct lwp_info *lp, void *data) | |
729 | { | |
730 | int *count = data; | |
731 | ||
732 | /* Count only threads that have a SIGTRAP pending. */ | |
733 | if (lp->status != 0 && | |
734 | WIFSTOPPED (lp->status) && | |
735 | WSTOPSIG (lp->status) == SIGTRAP && | |
736 | count != NULL) /* paranoia */ | |
737 | (*count)++; | |
738 | ||
739 | return 0; | |
740 | } | |
741 | ||
742 | /* Select the LWP (if any) that is currently being single-stepped. */ | |
743 | ||
744 | static int | |
745 | select_singlestep_lwp_callback (struct lwp_info *lp, void *data) | |
746 | { | |
747 | if (lp->step && lp->status != 0) | |
748 | return 1; | |
749 | else | |
750 | return 0; | |
751 | } | |
752 | ||
753 | /* Select the Nth LWP that has had a SIGTRAP event. */ | |
754 | ||
755 | static int | |
756 | select_event_lwp_callback (struct lwp_info *lp, void *data) | |
757 | { | |
758 | int *selector = data; | |
759 | ||
760 | /* Select only threads that have a SIGTRAP event pending. */ | |
761 | if (lp->status != 0 && | |
762 | WIFSTOPPED (lp->status) && | |
763 | WSTOPSIG (lp->status) == SIGTRAP && | |
764 | selector != NULL) /* paranoia */ | |
765 | if ((*selector)-- == 0) | |
766 | return 1; | |
767 | ||
768 | return 0; | |
769 | } | |
770 | ||
771 | static int | |
772 | cancel_breakpoints_callback (struct lwp_info *lp, void *data) | |
773 | { | |
774 | struct lwp_info *event_lp = data; | |
775 | ||
776 | if (lp != event_lp && | |
777 | lp->status != 0 && | |
778 | WIFSTOPPED (lp->status) && | |
779 | WSTOPSIG (lp->status) == SIGTRAP && | |
780 | breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - | |
781 | DECR_PC_AFTER_BREAK)) | |
782 | { | |
783 | if (debug_lin_lwp) | |
784 | { | |
785 | fprintf_unfiltered (gdb_stdlog, | |
786 | "Push back BP for %ld\n", | |
787 | GET_LWP (lp->ptid)); | |
788 | } | |
789 | /* For each lp except the event lp, if there was a trap, | |
790 | set the PC to before the trap. */ | |
791 | if (DECR_PC_AFTER_BREAK) | |
792 | { | |
793 | write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, | |
794 | lp->ptid); | |
795 | } | |
796 | lp->status = 0; | |
797 | } | |
798 | return 0; | |
799 | } | |
800 | ||
801 | /* Select one LWP out of those that have events to be the event thread. */ | |
802 | ||
803 | static void | |
804 | select_event_lwp (struct lwp_info **orig_lp, int *status) | |
805 | { | |
806 | int num_events = 0; | |
807 | int random_selector; | |
808 | struct lwp_info *event_lp; | |
809 | ||
810 | /* Give preference to any LWP that is being single-stepped. */ | |
811 | event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); | |
812 | if (event_lp != NULL) | |
813 | { | |
814 | if (debug_lin_lwp) | |
815 | fprintf_unfiltered (gdb_stdlog, | |
816 | "Select singlestep lwp %ld\n", | |
817 | GET_LWP (event_lp->ptid)); | |
818 | } | |
819 | else | |
820 | { | |
821 | /* No single-stepping LWP. Select one at random, out of those | |
822 | which have had SIGTRAP events. */ | |
823 | ||
824 | /* First see how many SIGTRAP events we have. */ | |
825 | iterate_over_lwps (count_events_callback, (void *) &num_events); | |
826 | ||
827 | /* OK, now randomly pick the Nth LWP of those that have had SIGTRAP. */ | |
828 | random_selector = (int) | |
829 | ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); | |
830 | ||
831 | if (debug_lin_lwp) | |
832 | { | |
833 | if (num_events > 1) | |
834 | fprintf_unfiltered (gdb_stdlog, | |
835 | "Found %d SIGTRAP events, selecting #%d\n", | |
836 | num_events, random_selector); | |
837 | else if (num_events <= 0) | |
838 | fprintf_unfiltered (gdb_stdlog, | |
839 | "ERROR select_event_lwp %d events!\n", | |
840 | num_events); | |
841 | } | |
842 | ||
843 | event_lp = iterate_over_lwps (select_event_lwp_callback, | |
844 | (void *) &random_selector); | |
845 | } | |
846 | ||
847 | if (event_lp != NULL) | |
848 | { | |
849 | /* "event_lp" is now the selected event thread. | |
850 | If any other threads have had SIGTRAP events, these events | |
851 | must now be cancelled, so that the respective thread will | |
852 | trip the breakpoint again once it is resumed. */ | |
853 | iterate_over_lwps (cancel_breakpoints_callback, (void *) event_lp); | |
854 | *orig_lp = event_lp; | |
855 | *status = event_lp->status; | |
856 | event_lp->status = 0; | |
857 | } | |
858 | } | |
859 | ||
fce0e6e1 MK |
860 | /* Return non-zero if LP has been resumed. */ |
861 | ||
862 | static int | |
863 | resumed_callback (struct lwp_info *lp, void *data) | |
864 | { | |
865 | return lp->resumed; | |
866 | } | |
867 | ||
39f77062 KB |
868 | static ptid_t |
869 | lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
fb0e1ba7 MK |
870 | { |
871 | struct lwp_info *lp = NULL; | |
872 | int options = 0; | |
873 | int status = 0; | |
39f77062 | 874 | pid_t pid = PIDGET (ptid); |
fb0e1ba7 | 875 | |
3f07c44b MK |
876 | /* Make sure SIGCHLD is blocked. */ |
877 | if (! sigismember (&blocked_mask, SIGCHLD)) | |
878 | { | |
879 | sigaddset (&blocked_mask, SIGCHLD); | |
880 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
881 | } | |
882 | ||
fb0e1ba7 MK |
883 | retry: |
884 | ||
fce0e6e1 MK |
885 | /* Make sure there is at least one thread that has been resumed. */ |
886 | gdb_assert (iterate_over_lwps (resumed_callback, NULL)); | |
887 | ||
fb0e1ba7 MK |
888 | /* First check if there is a LWP with a wait status pending. */ |
889 | if (pid == -1) | |
890 | { | |
b1aeb4c5 | 891 | /* Any LWP that's been resumed will do. */ |
fb0e1ba7 MK |
892 | lp = iterate_over_lwps (status_callback, NULL); |
893 | if (lp) | |
894 | { | |
fb0e1ba7 MK |
895 | status = lp->status; |
896 | lp->status = 0; | |
b1aeb4c5 MS |
897 | |
898 | if (debug_lin_lwp && status) | |
899 | fprintf_unfiltered (gdb_stdlog, | |
900 | "Using pending wait status %d for LWP %ld.\n", | |
901 | WIFSTOPPED (status) ? WSTOPSIG (status) : | |
902 | WIFSIGNALED (status) ? WTERMSIG (status) : | |
903 | WEXITSTATUS (status), GET_LWP (lp->ptid)); | |
fb0e1ba7 MK |
904 | } |
905 | ||
906 | /* But if we don't fine one, we'll have to wait, and check both | |
907 | cloned and uncloned processes. We start with the cloned | |
908 | processes. */ | |
909 | options = __WCLONE | WNOHANG; | |
910 | } | |
39f77062 | 911 | else if (is_lwp (ptid)) |
fb0e1ba7 | 912 | { |
7ca673cd | 913 | if (debug_lin_lwp) |
9085700c | 914 | fprintf_unfiltered (gdb_stdlog, |
b08cfdb6 | 915 | "Waiting for specific LWP %ld.\n", |
ce696e05 | 916 | GET_LWP (ptid)); |
7ca673cd | 917 | |
fb0e1ba7 | 918 | /* We have a specific LWP to check. */ |
39f77062 | 919 | lp = find_lwp_pid (ptid); |
fb0e1ba7 MK |
920 | gdb_assert (lp); |
921 | status = lp->status; | |
922 | lp->status = 0; | |
7ca673cd | 923 | |
b1aeb4c5 MS |
924 | if (debug_lin_lwp && status) |
925 | fprintf_unfiltered (gdb_stdlog, | |
926 | "Using pending wait status %d for LWP %ld.\n", | |
927 | WIFSTOPPED (status) ? WSTOPSIG (status) : | |
928 | WIFSIGNALED (status) ? WTERMSIG (status) : | |
929 | WEXITSTATUS (status), GET_LWP (lp->ptid)); | |
fb0e1ba7 MK |
930 | |
931 | /* If we have to wait, take into account whether PID is a cloned | |
932 | process or not. And we have to convert it to something that | |
933 | the layer beneath us can understand. */ | |
39f77062 KB |
934 | options = is_cloned (lp->ptid) ? __WCLONE : 0; |
935 | pid = GET_LWP (ptid); | |
fb0e1ba7 MK |
936 | } |
937 | ||
938 | if (status && lp->signalled) | |
939 | { | |
940 | /* A pending SIGSTOP may interfere with the normal stream of | |
941 | events. In a typical case where interference is a problem, | |
942 | we have a SIGSTOP signal pending for LWP A while | |
943 | single-stepping it, encounter an event in LWP B, and take the | |
944 | pending SIGSTOP while trying to stop LWP A. After processing | |
945 | the event in LWP B, LWP A is continued, and we'll never see | |
946 | the SIGTRAP associated with the last time we were | |
947 | single-stepping LWP A. */ | |
948 | ||
949 | /* Resume the thread. It should halt immediately returning the | |
950 | pending SIGSTOP. */ | |
39f77062 KB |
951 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
952 | TARGET_SIGNAL_0); | |
fb0e1ba7 | 953 | lp->stopped = 0; |
fce0e6e1 | 954 | gdb_assert (lp->resumed); |
fb0e1ba7 MK |
955 | |
956 | /* This should catch the pending SIGSTOP. */ | |
957 | stop_wait_callback (lp, NULL); | |
958 | } | |
959 | ||
960 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
961 | attached process. */ | |
962 | set_sigio_trap (); | |
963 | ||
964 | while (status == 0) | |
965 | { | |
966 | pid_t lwpid; | |
967 | ||
968 | lwpid = waitpid (pid, &status, options); | |
969 | if (lwpid > 0) | |
970 | { | |
971 | gdb_assert (pid == -1 || lwpid == pid); | |
972 | ||
39f77062 | 973 | lp = find_lwp_pid (pid_to_ptid (lwpid)); |
fb0e1ba7 MK |
974 | if (! lp) |
975 | { | |
39f77062 | 976 | lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); |
fb0e1ba7 MK |
977 | if (threaded) |
978 | { | |
3f07c44b MK |
979 | gdb_assert (WIFSTOPPED (status) |
980 | && WSTOPSIG (status) == SIGSTOP); | |
fb0e1ba7 MK |
981 | lp->signalled = 1; |
982 | ||
39f77062 | 983 | if (! in_thread_list (inferior_ptid)) |
fb0e1ba7 | 984 | { |
39f77062 KB |
985 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), |
986 | GET_PID (inferior_ptid)); | |
987 | add_thread (inferior_ptid); | |
fb0e1ba7 MK |
988 | } |
989 | ||
39f77062 | 990 | add_thread (lp->ptid); |
fb0e1ba7 | 991 | printf_unfiltered ("[New %s]\n", |
39f77062 | 992 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 MK |
993 | } |
994 | } | |
995 | ||
996 | /* Make sure we don't report a TARGET_WAITKIND_EXITED or | |
997 | TARGET_WAITKIND_SIGNALLED event if there are still LWP's | |
998 | left in the process. */ | |
999 | if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) | |
1000 | { | |
39f77062 | 1001 | if (in_thread_list (lp->ptid)) |
fb0e1ba7 | 1002 | { |
e6328671 | 1003 | /* Core GDB cannot deal with us deleting the current |
fb0e1ba7 | 1004 | thread. */ |
39f77062 KB |
1005 | if (! ptid_equal (lp->ptid, inferior_ptid)) |
1006 | delete_thread (lp->ptid); | |
fb0e1ba7 | 1007 | printf_unfiltered ("[%s exited]\n", |
39f77062 | 1008 | target_pid_to_str (lp->ptid)); |
fb0e1ba7 | 1009 | } |
7ca673cd | 1010 | if (debug_lin_lwp) |
9085700c MS |
1011 | fprintf_unfiltered (gdb_stdlog, |
1012 | "%s exited.\n", | |
39f77062 | 1013 | target_pid_to_str (lp->ptid)); |
7ca673cd | 1014 | |
39f77062 | 1015 | delete_lwp (lp->ptid); |
fb0e1ba7 MK |
1016 | |
1017 | /* Make sure there is at least one thread running. */ | |
1018 | gdb_assert (iterate_over_lwps (running_callback, NULL)); | |
1019 | ||
1020 | /* Discard the event. */ | |
1021 | status = 0; | |
1022 | continue; | |
1023 | } | |
1024 | ||
1025 | /* Make sure we don't report a SIGSTOP that we sent | |
1026 | ourselves in an attempt to stop an LWP. */ | |
1027 | if (lp->signalled && WIFSTOPPED (status) | |
1028 | && WSTOPSIG (status) == SIGSTOP) | |
1029 | { | |
7ca673cd | 1030 | if (debug_lin_lwp) |
9085700c MS |
1031 | fprintf_unfiltered (gdb_stdlog, |
1032 | "Delayed SIGSTOP caught for %s.\n", | |
39f77062 | 1033 | target_pid_to_str (lp->ptid)); |
7ca673cd | 1034 | |
fb0e1ba7 MK |
1035 | /* This is a delayed SIGSTOP. */ |
1036 | lp->signalled = 0; | |
1037 | ||
39f77062 KB |
1038 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
1039 | TARGET_SIGNAL_0); | |
fb0e1ba7 | 1040 | lp->stopped = 0; |
fce0e6e1 | 1041 | gdb_assert (lp->resumed); |
fb0e1ba7 MK |
1042 | |
1043 | /* Discard the event. */ | |
1044 | status = 0; | |
1045 | continue; | |
1046 | } | |
1047 | ||
1048 | break; | |
1049 | } | |
1050 | ||
1051 | if (pid == -1) | |
1052 | { | |
1053 | /* Alternate between checking cloned and uncloned processes. */ | |
1054 | options ^= __WCLONE; | |
1055 | ||
1056 | /* And suspend every time we have checked both. */ | |
1057 | if (options & __WCLONE) | |
1058 | sigsuspend (&suspend_mask); | |
1059 | } | |
1060 | ||
1061 | /* We shouldn't end up here unless we want to try again. */ | |
1062 | gdb_assert (status == 0); | |
1063 | } | |
1064 | ||
1065 | clear_sigio_trap (); | |
1066 | clear_sigint_trap (); | |
1067 | ||
1068 | gdb_assert (lp); | |
1069 | ||
1070 | /* Don't report signals that GDB isn't interested in, such as | |
1071 | signals that are neither printed nor stopped upon. Stopping all | |
1072 | threads can be a bit time-consuming so if we want decent | |
1073 | performance with heavily multi-threaded programs, especially when | |
1074 | they're using a high frequency timer, we'd better avoid it if we | |
1075 | can. */ | |
1076 | ||
1077 | if (WIFSTOPPED (status)) | |
1078 | { | |
1079 | int signo = target_signal_from_host (WSTOPSIG (status)); | |
1080 | ||
1081 | if (signal_stop_state (signo) == 0 | |
1082 | && signal_print_state (signo) == 0 | |
1083 | && signal_pass_state (signo) == 1) | |
1084 | { | |
fce0e6e1 MK |
1085 | /* FIMXE: kettenis/2001-06-06: Should we resume all threads |
1086 | here? It is not clear we should. GDB may not expect | |
1087 | other threads to run. On the other hand, not resuming | |
1088 | newly attached threads may cause an unwanted delay in | |
1089 | getting them running. */ | |
c4365b19 | 1090 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo); |
fce0e6e1 | 1091 | lp->stopped = 0; |
fb0e1ba7 MK |
1092 | status = 0; |
1093 | goto retry; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | /* This LWP is stopped now. */ | |
1098 | lp->stopped = 1; | |
1099 | ||
b1aeb4c5 MS |
1100 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) |
1101 | { | |
1102 | /* Save SIGTRAP event for select_event_lwp. */ | |
1103 | lp->status = status; | |
1104 | } | |
1105 | ||
1106 | if (debug_lin_lwp) | |
1107 | fprintf_unfiltered (gdb_stdlog, | |
1108 | "LLW: Candidate event %d in %ld\n", | |
1109 | WSTOPSIG (status), GET_LWP (lp->ptid)); | |
1110 | ||
fb0e1ba7 MK |
1111 | /* Now stop all other LWP's ... */ |
1112 | iterate_over_lwps (stop_callback, NULL); | |
1113 | ||
1114 | /* ... and wait until all of them have reported back that they're no | |
1115 | longer running. */ | |
1116 | iterate_over_lwps (stop_wait_callback, NULL); | |
1117 | ||
b1aeb4c5 MS |
1118 | /* MVS Now choose an event thread from among those that |
1119 | have had events. Giving equal priority to all threads | |
1120 | that have had events helps prevent starvation. */ | |
1121 | ||
1122 | select_event_lwp (&lp, &status); | |
1123 | ||
fb0e1ba7 MK |
1124 | /* If we're not running in "threaded" mode, we'll report the bare |
1125 | process id. */ | |
1126 | ||
1127 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) | |
b1aeb4c5 MS |
1128 | { |
1129 | trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); | |
1130 | if (debug_lin_lwp) | |
1131 | fprintf_unfiltered (gdb_stdlog, | |
1132 | "LLW: trap_ptid is %ld\n", | |
1133 | GET_LWP (trap_ptid)); | |
1134 | } | |
fb0e1ba7 | 1135 | else |
39f77062 | 1136 | trap_ptid = null_ptid; |
fb0e1ba7 MK |
1137 | |
1138 | store_waitstatus (ourstatus, status); | |
39f77062 | 1139 | return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); |
fb0e1ba7 MK |
1140 | } |
1141 | ||
1142 | static int | |
1143 | kill_callback (struct lwp_info *lp, void *data) | |
1144 | { | |
39f77062 | 1145 | ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); |
fb0e1ba7 MK |
1146 | return 0; |
1147 | } | |
1148 | ||
1149 | static int | |
1150 | kill_wait_callback (struct lwp_info *lp, void *data) | |
1151 | { | |
1152 | pid_t pid; | |
1153 | ||
1154 | /* We must make sure that there are no pending events (delayed | |
1155 | SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current | |
1156 | program doesn't interfere with any following debugging session. */ | |
1157 | ||
1158 | /* For cloned processes we must check both with __WCLONE and | |
1159 | without, since the exit status of a cloned process isn't reported | |
1160 | with __WCLONE. */ | |
39f77062 | 1161 | if (is_cloned (lp->ptid)) |
fb0e1ba7 MK |
1162 | { |
1163 | do | |
1164 | { | |
39f77062 | 1165 | pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); |
fb0e1ba7 | 1166 | } |
39f77062 | 1167 | while (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 MK |
1168 | |
1169 | gdb_assert (pid == -1 && errno == ECHILD); | |
1170 | } | |
1171 | ||
1172 | do | |
1173 | { | |
39f77062 | 1174 | pid = waitpid (GET_LWP (lp->ptid), NULL, 0); |
fb0e1ba7 | 1175 | } |
39f77062 | 1176 | while (pid == GET_LWP (lp->ptid)); |
fb0e1ba7 MK |
1177 | |
1178 | gdb_assert (pid == -1 && errno == ECHILD); | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | static void | |
1183 | lin_lwp_kill (void) | |
1184 | { | |
1185 | /* Kill all LWP's ... */ | |
1186 | iterate_over_lwps (kill_callback, NULL); | |
1187 | ||
1188 | /* ... and wait until we've flushed all events. */ | |
1189 | iterate_over_lwps (kill_wait_callback, NULL); | |
1190 | ||
1191 | target_mourn_inferior (); | |
1192 | } | |
1193 | ||
1194 | static void | |
1195 | lin_lwp_create_inferior (char *exec_file, char *allargs, char **env) | |
1196 | { | |
c194fbe1 | 1197 | child_ops.to_create_inferior (exec_file, allargs, env); |
fb0e1ba7 MK |
1198 | } |
1199 | ||
1200 | static void | |
1201 | lin_lwp_mourn_inferior (void) | |
1202 | { | |
c194fbe1 | 1203 | trap_ptid = null_ptid; |
fb0e1ba7 | 1204 | |
c194fbe1 | 1205 | /* Destroy LWP info; it's no longer valid. */ |
fb0e1ba7 MK |
1206 | init_lwp_list (); |
1207 | ||
4c8de859 | 1208 | /* Restore the original signal mask. */ |
3f07c44b MK |
1209 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
1210 | sigemptyset (&blocked_mask); | |
1211 | ||
c194fbe1 | 1212 | child_ops.to_mourn_inferior (); |
fb0e1ba7 MK |
1213 | } |
1214 | ||
1215 | static void | |
1216 | lin_lwp_fetch_registers (int regno) | |
1217 | { | |
39f77062 | 1218 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 | 1219 | |
39f77062 KB |
1220 | if (is_lwp (inferior_ptid)) |
1221 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); | |
fb0e1ba7 MK |
1222 | |
1223 | fetch_inferior_registers (regno); | |
1224 | ||
1225 | do_cleanups (old_chain); | |
1226 | } | |
1227 | ||
1228 | static void | |
1229 | lin_lwp_store_registers (int regno) | |
1230 | { | |
39f77062 | 1231 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 | 1232 | |
39f77062 KB |
1233 | if (is_lwp (inferior_ptid)) |
1234 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); | |
fb0e1ba7 MK |
1235 | |
1236 | store_inferior_registers (regno); | |
1237 | ||
1238 | do_cleanups (old_chain); | |
1239 | } | |
1240 | ||
1241 | static int | |
1242 | lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, | |
e5da8f38 | 1243 | struct mem_attrib *attrib, |
fb0e1ba7 MK |
1244 | struct target_ops *target) |
1245 | { | |
39f77062 | 1246 | struct cleanup *old_chain = save_inferior_ptid (); |
fb0e1ba7 MK |
1247 | int xfer; |
1248 | ||
39f77062 KB |
1249 | if (is_lwp (inferior_ptid)) |
1250 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); | |
fb0e1ba7 | 1251 | |
e5da8f38 | 1252 | xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target); |
fb0e1ba7 MK |
1253 | |
1254 | do_cleanups (old_chain); | |
1255 | return xfer; | |
1256 | } | |
1257 | ||
1258 | static int | |
39f77062 | 1259 | lin_lwp_thread_alive (ptid_t ptid) |
fb0e1ba7 | 1260 | { |
39f77062 | 1261 | gdb_assert (is_lwp (ptid)); |
fb0e1ba7 MK |
1262 | |
1263 | errno = 0; | |
39f77062 | 1264 | ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); |
fb0e1ba7 MK |
1265 | if (errno) |
1266 | return 0; | |
1267 | ||
1268 | return 1; | |
1269 | } | |
1270 | ||
1271 | static char * | |
39f77062 | 1272 | lin_lwp_pid_to_str (ptid_t ptid) |
fb0e1ba7 MK |
1273 | { |
1274 | static char buf[64]; | |
1275 | ||
39f77062 | 1276 | if (is_lwp (ptid)) |
fb0e1ba7 | 1277 | { |
b08cfdb6 | 1278 | snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); |
fb0e1ba7 MK |
1279 | return buf; |
1280 | } | |
1281 | ||
39f77062 | 1282 | return normal_pid_to_str (ptid); |
fb0e1ba7 MK |
1283 | } |
1284 | ||
1285 | static void | |
1286 | init_lin_lwp_ops (void) | |
1287 | { | |
1288 | #if 0 | |
1289 | lin_lwp_ops.to_open = lin_lwp_open; | |
1290 | #endif | |
1291 | lin_lwp_ops.to_shortname = "lwp-layer"; | |
1292 | lin_lwp_ops.to_longname = "lwp-layer"; | |
1293 | lin_lwp_ops.to_doc = "Low level threads support (LWP layer)"; | |
1294 | lin_lwp_ops.to_attach = lin_lwp_attach; | |
1295 | lin_lwp_ops.to_detach = lin_lwp_detach; | |
1296 | lin_lwp_ops.to_resume = lin_lwp_resume; | |
1297 | lin_lwp_ops.to_wait = lin_lwp_wait; | |
1298 | lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers; | |
1299 | lin_lwp_ops.to_store_registers = lin_lwp_store_registers; | |
1300 | lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory; | |
1301 | lin_lwp_ops.to_kill = lin_lwp_kill; | |
1302 | lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior; | |
1303 | lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior; | |
1304 | lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive; | |
1305 | lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str; | |
1306 | lin_lwp_ops.to_stratum = thread_stratum; | |
1307 | lin_lwp_ops.to_has_thread_control = tc_schedlock; | |
1308 | lin_lwp_ops.to_magic = OPS_MAGIC; | |
1309 | } | |
1310 | ||
1311 | static void | |
1312 | sigchld_handler (int signo) | |
1313 | { | |
1314 | /* Do nothing. The only reason for this handler is that it allows | |
1315 | us to use sigsuspend in lin_lwp_wait above to wait for the | |
1316 | arrival of a SIGCHLD. */ | |
1317 | } | |
1318 | ||
1319 | void | |
1320 | _initialize_lin_lwp (void) | |
1321 | { | |
1322 | struct sigaction action; | |
fb0e1ba7 MK |
1323 | |
1324 | extern void thread_db_init (struct target_ops *); | |
1325 | ||
1326 | init_lin_lwp_ops (); | |
1327 | add_target (&lin_lwp_ops); | |
1328 | thread_db_init (&lin_lwp_ops); | |
1329 | ||
4c8de859 | 1330 | /* Save the original signal mask. */ |
3f07c44b MK |
1331 | sigprocmask (SIG_SETMASK, NULL, &normal_mask); |
1332 | ||
fb0e1ba7 MK |
1333 | action.sa_handler = sigchld_handler; |
1334 | sigemptyset (&action.sa_mask); | |
1335 | action.sa_flags = 0; | |
1336 | sigaction (SIGCHLD, &action, NULL); | |
1337 | ||
3f07c44b MK |
1338 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
1339 | sigprocmask (SIG_SETMASK, NULL, &suspend_mask); | |
fb0e1ba7 | 1340 | sigdelset (&suspend_mask, SIGCHLD); |
3f07c44b MK |
1341 | |
1342 | sigemptyset (&blocked_mask); | |
7ca673cd MS |
1343 | |
1344 | add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger, | |
1345 | (char *) &debug_lin_lwp, | |
1346 | "Set debugging of linux lwp module.\n\ | |
1347 | Enables printf debugging output.\n", | |
1348 | &setdebuglist), | |
1349 | &showdebuglist); | |
fb0e1ba7 MK |
1350 | } |
1351 | \f | |
1352 | ||
1353 | /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to | |
1354 | the LinuxThreads library and therefore doesn't really belong here. */ | |
1355 | ||
1356 | /* Read variable NAME in the target and return its value if found. | |
1357 | Otherwise return zero. It is assumed that the type of the variable | |
1358 | is `int'. */ | |
1359 | ||
1360 | static int | |
1361 | get_signo (const char *name) | |
1362 | { | |
1363 | struct minimal_symbol *ms; | |
1364 | int signo; | |
1365 | ||
1366 | ms = lookup_minimal_symbol (name, NULL, NULL); | |
1367 | if (ms == NULL) | |
1368 | return 0; | |
1369 | ||
1370 | if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo, | |
1371 | sizeof (signo)) != 0) | |
1372 | return 0; | |
1373 | ||
1374 | return signo; | |
1375 | } | |
1376 | ||
1377 | /* Return the set of signals used by the threads library in *SET. */ | |
1378 | ||
1379 | void | |
1380 | lin_thread_get_thread_signals (sigset_t *set) | |
1381 | { | |
3f07c44b MK |
1382 | struct sigaction action; |
1383 | int restart, cancel; | |
fb0e1ba7 MK |
1384 | |
1385 | sigemptyset (set); | |
1386 | ||
1387 | restart = get_signo ("__pthread_sig_restart"); | |
1388 | if (restart == 0) | |
1389 | return; | |
1390 | ||
1391 | cancel = get_signo ("__pthread_sig_cancel"); | |
1392 | if (cancel == 0) | |
1393 | return; | |
1394 | ||
1395 | sigaddset (set, restart); | |
1396 | sigaddset (set, cancel); | |
3f07c44b MK |
1397 | |
1398 | /* The LinuxThreads library makes terminating threads send a special | |
1399 | "cancel" signal instead of SIGCHLD. Make sure we catch those (to | |
1400 | prevent them from terminating GDB itself, which is likely to be | |
1401 | their default action) and treat them the same way as SIGCHLD. */ | |
1402 | ||
1403 | action.sa_handler = sigchld_handler; | |
1404 | sigemptyset (&action.sa_mask); | |
1405 | action.sa_flags = 0; | |
1406 | sigaction (cancel, &action, NULL); | |
1407 | ||
1408 | /* We block the "cancel" signal throughout this code ... */ | |
1409 | sigaddset (&blocked_mask, cancel); | |
1410 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); | |
1411 | ||
1412 | /* ... except during a sigsuspend. */ | |
1413 | sigdelset (&suspend_mask, cancel); | |
fb0e1ba7 | 1414 | } |