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