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