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