daily update
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
CommitLineData
8605d56e 1/* Multi-threaded debugging support for GNU/Linux (LWP layer).
4e052eda 2 Copyright 2000, 2001 Free Software Foundation, Inc.
fb0e1ba7
MK
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
22
23#include "gdb_assert.h"
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 35static int debug_lin_lwp;
fb0e1ba7 36extern const char *strsignal (int sig);
fb0e1ba7 37
8605d56e
AC
38/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
39 are processes sharing the same VM space. A multi-threaded process
40 is basically a group of such processes. However, such a grouping
41 is almost entirely a user-space issue; the kernel doesn't enforce
42 such a grouping at all (this might change in the future). In
43 general, we'll rely on the threads library (i.e. the GNU/Linux
44 Threads library) to provide such a grouping.
fb0e1ba7
MK
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
8605d56e 51 using the Linux kernel 2.4 or above.
fb0e1ba7 52
8605d56e
AC
53 Note that there are some peculiarities in GNU/Linux that affect
54 this code:
fb0e1ba7
MK
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
8605d56e
AC
60 __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
61 we 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
8605d56e
AC
65 keeps it around as a "zombie". Somehow, the GNU/Linux Threads
66 library doesn't notice this, which leads to the "zombie problem":
67 When debugged a multi-threaded process that spawns a lot of
68 threads will run out of processes, even if the threads exit,
69 because the "zombies" stay around. */
fb0e1ba7
MK
70
71/* Structure describing a LWP. */
72struct 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 77
cacab7c4
MK
78 /* Non-zero if this LWP is cloned. In this context "cloned" means
79 that the LWP is reporting to its parent using a signal other than
80 SIGCHLD. */
81 int cloned;
82
fb0e1ba7
MK
83 /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
84 it back yet). */
85 int signalled;
86
87 /* Non-zero if this LWP is stopped. */
88 int stopped;
89
fce0e6e1
MK
90 /* Non-zero if this LWP will be/has been resumed. Note that an LWP
91 can be marked both as stopped and resumed at the same time. This
92 happens if we try to resume an LWP that has a wait status
93 pending. We shouldn't let the LWP run until that wait status has
94 been processed, but we should not report that wait status if GDB
95 didn't try to let the LWP run. */
96 int resumed;
97
fb0e1ba7
MK
98 /* If non-zero, a pending wait status. */
99 int status;
100
101 /* Non-zero if we were stepping this LWP. */
102 int step;
103
104 /* Next LWP in list. */
105 struct lwp_info *next;
106};
107
108/* List of known LWPs. */
109static struct lwp_info *lwp_list;
110
111/* Number of LWPs in the list. */
112static int num_lwps;
113
114/* Non-zero if we're running in "threaded" mode. */
115static int threaded;
116\f
117
ca6724c1
KB
118#define GET_LWP(ptid) ptid_get_lwp (ptid)
119#define GET_PID(ptid) ptid_get_pid (ptid)
120#define is_lwp(ptid) (GET_LWP (ptid) != 0)
121#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
fb0e1ba7 122
fb0e1ba7
MK
123/* If the last reported event was a SIGTRAP, this variable is set to
124 the process id of the LWP/thread that got it. */
39f77062 125ptid_t trap_ptid;
fb0e1ba7
MK
126\f
127
128/* This module's target-specific operations. */
129static struct target_ops lin_lwp_ops;
130
131/* The standard child operations. */
132extern struct target_ops child_ops;
133
3f07c44b
MK
134/* Since we cannot wait (in lin_lwp_wait) for the initial process and
135 any cloned processes with a single call to waitpid, we have to use
4c8de859 136 the WNOHANG flag and call waitpid in a loop. To optimize
3f07c44b
MK
137 things a bit we use `sigsuspend' to wake us up when a process has
138 something to report (it will send us a SIGCHLD if it has). To make
139 this work we have to juggle with the signal mask. We save the
4c8de859 140 original signal mask such that we can restore it before creating a
3f07c44b
MK
141 new process in order to avoid blocking certain signals in the
142 inferior. We then block SIGCHLD during the waitpid/sigsuspend
143 loop. */
144
4c8de859 145/* Original signal mask. */
3f07c44b
MK
146static sigset_t normal_mask;
147
fb0e1ba7
MK
148/* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
149 _initialize_lin_lwp. */
150static sigset_t suspend_mask;
3f07c44b
MK
151
152/* Signals to block to make that sigsuspend work. */
153static sigset_t blocked_mask;
fb0e1ba7
MK
154\f
155
156/* Prototypes for local functions. */
c194fbe1 157static int stop_wait_callback (struct lwp_info *lp, void *data);
fb0e1ba7 158\f
58eeadba
MK
159/* Convert wait status STATUS to a string. Used for printing debug
160 messages only. */
fb0e1ba7 161
58eeadba
MK
162static char *
163status_to_str (int status)
164{
165 static char buf[64];
166
167 if (WIFSTOPPED (status))
168 snprintf (buf, sizeof (buf), "%s (stopped)",
169 strsignal (WSTOPSIG (status)));
170 else if (WIFSIGNALED (status))
171 snprintf (buf, sizeof (buf), "%s (terminated)",
172 strsignal (WSTOPSIG (status)));
173 else
174 snprintf (buf, sizeof (buf), "%d (exited)",
175 WEXITSTATUS (status));
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
185static void
186init_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
205static struct lwp_info *
39f77062 206add_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
228static void
39f77062 229delete_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
257static struct lwp_info *
39f77062 258find_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
280struct lwp_info *
281iterate_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
305int
306lin_lwp_prepare_to_proceed (void)
307{
39f77062
KB
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
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
337static void
338lin_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
348void
39f77062 349lin_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. */
357 if (! sigismember (&blocked_mask, SIGCHLD))
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
MK
380
381 pid = waitpid (GET_LWP (ptid), &status, 0);
382 if (pid == -1 && errno == ECHILD)
383 {
384 /* Try again with __WCLONE to check cloned processes. */
385 pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
386 lp->cloned = 1;
387 }
388
389 gdb_assert (pid == GET_LWP (ptid)
390 && WIFSTOPPED (status) && WSTOPSIG (status));
391
392 lp->stopped = 1;
c4365b19 393 }
da9c7185
KB
394 else
395 {
396 /* We assume that the LWP representing the original process
397 is already stopped. Mark it as stopped in the data structure
398 that the lin-lwp layer uses to keep track of threads. Note
399 that this won't have already been done since the main thread
400 will have, we assume, been stopped by an attach from a
401 different layer. */
402 lp->stopped = 1;
403 }
fb0e1ba7
MK
404}
405
406static void
407lin_lwp_attach (char *args, int from_tty)
408{
c194fbe1 409 struct lwp_info *lp;
cacab7c4
MK
410 pid_t pid;
411 int status;
c194fbe1 412
fb0e1ba7
MK
413 /* FIXME: We should probably accept a list of process id's, and
414 attach all of them. */
c194fbe1
MK
415 child_ops.to_attach (args, from_tty);
416
417 /* Add the initial process as the first LWP to the list. */
cacab7c4 418 lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
c194fbe1
MK
419
420 /* Make sure the initial process is stopped. The user-level threads
421 layer might want to poke around in the inferior, and that won't
422 work if things haven't stabilized yet. */
cacab7c4
MK
423 pid = waitpid (GET_PID (inferior_ptid), &status, 0);
424 if (pid == -1 && errno == ECHILD)
425 {
426 warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
427
428 /* Try again with __WCLONE to check cloned processes. */
429 pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
430 lp->cloned = 1;
431 }
432
433 gdb_assert (pid == GET_PID (inferior_ptid)
434 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
435
436 lp->stopped = 1;
c194fbe1
MK
437
438 /* Fake the SIGSTOP that core GDB expects. */
439 lp->status = W_STOPCODE (SIGSTOP);
fce0e6e1 440 lp->resumed = 1;
c194fbe1
MK
441}
442
443static int
444detach_callback (struct lwp_info *lp, void *data)
445{
446 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
447
448 if (debug_lin_lwp && lp->status)
b08cfdb6 449 fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n",
c194fbe1
MK
450 strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid));
451
452 while (lp->signalled && lp->stopped)
453 {
454 if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
455 WSTOPSIG (lp->status)) < 0)
456 error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
dc672865 457 safe_strerror (errno));
c194fbe1
MK
458
459 lp->stopped = 0;
c4365b19 460 lp->signalled = 0;
c194fbe1
MK
461 lp->status = 0;
462 stop_wait_callback (lp, NULL);
463
464 gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
465 }
466
cacab7c4
MK
467 /* We don't actually detach from the LWP that has an id equal to the
468 overall process id just yet. */
469 if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
c194fbe1
MK
470 {
471 if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
472 WSTOPSIG (lp->status)) < 0)
473 error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
dc672865 474 safe_strerror (errno));
c194fbe1
MK
475
476 delete_lwp (lp->ptid);
477 }
478
479 return 0;
fb0e1ba7
MK
480}
481
482static void
483lin_lwp_detach (char *args, int from_tty)
484{
c194fbe1
MK
485 iterate_over_lwps (detach_callback, NULL);
486
cacab7c4 487 /* Only the initial process should be left right now. */
c194fbe1
MK
488 gdb_assert (num_lwps == 1);
489
490 trap_ptid = null_ptid;
491
492 /* Destroy LWP info; it's no longer valid. */
493 init_lwp_list ();
494
495 /* Restore the original signal mask. */
496 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
497 sigemptyset (&blocked_mask);
498
01263b57 499 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
c194fbe1 500 child_ops.to_detach (args, from_tty);
fb0e1ba7
MK
501}
502\f
503
504struct private_thread_info
505{
506 int lwpid;
507};
508
509/* Return non-zero if TP corresponds to the LWP specified by DATA
510 (which is assumed to be a pointer to a `struct lwp_info'. */
511
512static int
513find_lwp_callback (struct thread_info *tp, void *data)
514{
515 struct lwp_info *lp = data;
516
39f77062 517 if (tp->private->lwpid == GET_LWP (lp->ptid))
fb0e1ba7
MK
518 return 1;
519
520 return 0;
521}
522
523/* Resume LP. */
524
525static int
526resume_callback (struct lwp_info *lp, void *data)
527{
528 if (lp->stopped && lp->status == 0)
529 {
530 struct thread_info *tp;
531
b1aeb4c5 532#if 0
fb0e1ba7
MK
533 /* FIXME: kettenis/2000-08-26: This should really be handled
534 properly by core GDB. */
535
39f77062 536 tp = find_thread_pid (lp->ptid);
fb0e1ba7
MK
537 if (tp == NULL)
538 tp = iterate_over_threads (find_lwp_callback, lp);
539 gdb_assert (tp);
540
541 /* If we were previously stepping the thread, and now continue
542 the thread we must invalidate the stepping range. However,
543 if there is a step_resume breakpoint for this thread, we must
544 preserve the stepping range to make it possible to continue
545 stepping once we hit it. */
546 if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
547 {
548 gdb_assert (lp->step);
549 tp->step_range_start = tp->step_range_end = 0;
550 }
551#endif
552
39f77062 553 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
fb0e1ba7
MK
554 lp->stopped = 0;
555 lp->step = 0;
556 }
557
558 return 0;
559}
560
fce0e6e1
MK
561static int
562resume_clear_callback (struct lwp_info *lp, void *data)
563{
564 lp->resumed = 0;
565 return 0;
566}
567
568static int
569resume_set_callback (struct lwp_info *lp, void *data)
570{
571 lp->resumed = 1;
572 return 0;
573}
574
fb0e1ba7 575static void
39f77062 576lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
fb0e1ba7
MK
577{
578 struct lwp_info *lp;
579 int resume_all;
580
581 /* Apparently the interpretation of PID is dependent on STEP: If
582 STEP is non-zero, a specific PID means `step only this process
583 id'. But if STEP is zero, then PID means `continue *all*
584 processes, but give the signal only to this one'. */
39f77062 585 resume_all = (PIDGET (ptid) == -1) || !step;
fb0e1ba7 586
fce0e6e1
MK
587 if (resume_all)
588 iterate_over_lwps (resume_set_callback, NULL);
589 else
590 iterate_over_lwps (resume_clear_callback, NULL);
591
fb0e1ba7 592 /* If PID is -1, it's the current inferior that should be
c4365b19 593 handled specially. */
39f77062
KB
594 if (PIDGET (ptid) == -1)
595 ptid = inferior_ptid;
fb0e1ba7 596
39f77062 597 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
598 if (lp)
599 {
39f77062 600 ptid = pid_to_ptid (GET_LWP (lp->ptid));
fb0e1ba7 601
fb0e1ba7
MK
602 /* Remember if we're stepping. */
603 lp->step = step;
604
fce0e6e1
MK
605 /* Mark this LWP as resumed. */
606 lp->resumed = 1;
607
fb0e1ba7
MK
608 /* If we have a pending wait status for this thread, there is no
609 point in resuming the process. */
610 if (lp->status)
611 {
612 /* FIXME: What should we do if we are supposed to continue
613 this thread with a signal? */
614 gdb_assert (signo == TARGET_SIGNAL_0);
615 return;
616 }
40564aca
MK
617
618 /* Mark LWP as not stopped to prevent it from being continued by
619 resume_callback. */
620 lp->stopped = 0;
fb0e1ba7
MK
621 }
622
623 if (resume_all)
624 iterate_over_lwps (resume_callback, NULL);
625
39f77062 626 child_resume (ptid, step, signo);
fb0e1ba7
MK
627}
628\f
629
630/* Send a SIGSTOP to LP. */
631
632static int
633stop_callback (struct lwp_info *lp, void *data)
634{
635 if (! lp->stopped && ! lp->signalled)
636 {
637 int ret;
638
39f77062 639 ret = kill (GET_LWP (lp->ptid), SIGSTOP);
fb0e1ba7
MK
640 gdb_assert (ret == 0);
641
642 lp->signalled = 1;
643 gdb_assert (lp->status == 0);
644 }
645
646 return 0;
647}
648
de4ca854
MK
649/* Wait until LP is stopped. If DATA is non-null it is interpreted as
650 a pointer to a set of signals to be flushed immediately. */
fb0e1ba7
MK
651
652static int
653stop_wait_callback (struct lwp_info *lp, void *data)
654{
de4ca854
MK
655 sigset_t *flush_mask = data;
656
fb0e1ba7
MK
657 if (! lp->stopped && lp->signalled)
658 {
659 pid_t pid;
660 int status;
661
662 gdb_assert (lp->status == 0);
663
cacab7c4 664 pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
fb0e1ba7
MK
665 if (pid == -1 && errno == ECHILD)
666 /* OK, the proccess has disappeared. We'll catch the actual
3f07c44b 667 exit event in lin_lwp_wait. */
fb0e1ba7
MK
668 return 0;
669
39f77062 670 gdb_assert (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
671
672 if (WIFEXITED (status) || WIFSIGNALED (status))
673 {
674 gdb_assert (num_lwps > 1);
fb0e1ba7 675
39f77062 676 if (in_thread_list (lp->ptid))
e6328671
MK
677 {
678 /* Core GDB cannot deal with us deleting the current
679 thread. */
39f77062
KB
680 if (!ptid_equal (lp->ptid, inferior_ptid))
681 delete_thread (lp->ptid);
e6328671 682 printf_unfiltered ("[%s exited]\n",
39f77062 683 target_pid_to_str (lp->ptid));
e6328671 684 }
7ca673cd 685 if (debug_lin_lwp)
9085700c 686 fprintf_unfiltered (gdb_stdlog,
39f77062 687 "%s exited.\n", target_pid_to_str (lp->ptid));
7ca673cd 688
39f77062 689 delete_lwp (lp->ptid);
fb0e1ba7
MK
690 return 0;
691 }
692
693 gdb_assert (WIFSTOPPED (status));
fb0e1ba7 694
de4ca854
MK
695 /* Ignore any signals in FLUSH_MASK. */
696 if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
697 {
698 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
699 return stop_wait_callback (lp, flush_mask);
700 }
701
fb0e1ba7
MK
702 if (WSTOPSIG (status) != SIGSTOP)
703 {
b1aeb4c5 704 if (WSTOPSIG (status) == SIGTRAP)
fb0e1ba7
MK
705 {
706 /* If a LWP other than the LWP that we're reporting an
707 event for has hit a GDB breakpoint (as opposed to
708 some random trap signal), then just arrange for it to
709 hit it again later. We don't keep the SIGTRAP status
710 and don't forward the SIGTRAP signal to the LWP. We
711 will handle the current event, eventually we will
712 resume all LWPs, and this one will get its breakpoint
713 trap again.
714
715 If we do not do this, then we run the risk that the
716 user will delete or disable the breakpoint, but the
717 thread will have already tripped on it. */
7ca673cd 718
c4365b19
MS
719 /* Now resume this LWP and get the SIGSTOP event. */
720 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
b1aeb4c5
MS
721 if (debug_lin_lwp)
722 {
723 fprintf_unfiltered (gdb_stderr,
724 "SWC: Candidate SIGTRAP event in %ld\n",
725 GET_LWP (lp->ptid));
726 }
727 /* Hold the SIGTRAP for handling by lin_lwp_wait. */
728 stop_wait_callback (lp, data);
729 /* If there's another event, throw it back into the queue. */
730 if (lp->status)
731 kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
732 /* Save the sigtrap event. */
733 lp->status = status;
734 return 0;
fb0e1ba7
MK
735 }
736 else
737 {
b1aeb4c5
MS
738 /* The thread was stopped with a signal other than
739 SIGSTOP, and didn't accidentally trip a breakpoint. */
740
7ca673cd 741 if (debug_lin_lwp)
b1aeb4c5
MS
742 {
743 fprintf_unfiltered (gdb_stderr,
744 "SWC: Pending event %d in %ld\n",
745 WSTOPSIG (status), GET_LWP (lp->ptid));
746 }
747 /* Now resume this LWP and get the SIGSTOP event. */
748 ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
7ca673cd 749
b1aeb4c5
MS
750 /* Hold this event/waitstatus while we check to see if
751 there are any more (we still want to get that SIGSTOP). */
752 stop_wait_callback (lp, data);
753 /* If the lp->status field is still empty, use it to hold
754 this event. If not, then this event must be returned
755 to the event queue of the LWP. */
756 if (lp->status == 0)
757 lp->status = status;
758 else
759 kill (GET_LWP (lp->ptid), WSTOPSIG (status));
760 return 0;
fb0e1ba7
MK
761 }
762 }
763 else
764 {
765 /* We caught the SIGSTOP that we intended to catch, so
766 there's no SIGSTOP pending. */
b1aeb4c5 767 lp->stopped = 1;
fb0e1ba7
MK
768 lp->signalled = 0;
769 }
770 }
771
772 return 0;
773}
774
775/* Return non-zero if LP has a wait status pending. */
776
777static int
778status_callback (struct lwp_info *lp, void *data)
779{
fce0e6e1
MK
780 /* Only report a pending wait status if we pretend that this has
781 indeed been resumed. */
782 return (lp->status != 0 && lp->resumed);
fb0e1ba7
MK
783}
784
785/* Return non-zero if LP isn't stopped. */
786
787static int
788running_callback (struct lwp_info *lp, void *data)
789{
790 return (lp->stopped == 0);
791}
792
00d4fce6 793/* Count the LWP's that have had events. */
b1aeb4c5
MS
794
795static int
796count_events_callback (struct lwp_info *lp, void *data)
797{
798 int *count = data;
799
00d4fce6
MK
800 gdb_assert (count != NULL);
801
802 /* Count only LWPs that have a SIGTRAP event pending. */
803 if (lp->status != 0
804 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
805 (*count)++;
806
807 return 0;
808}
809
00d4fce6 810/* Select the LWP (if any) that is currently being single-stepped. */
b1aeb4c5
MS
811
812static int
813select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
814{
815 if (lp->step && lp->status != 0)
816 return 1;
817 else
818 return 0;
819}
820
00d4fce6 821/* Select the Nth LWP that has had a SIGTRAP event. */
b1aeb4c5
MS
822
823static int
824select_event_lwp_callback (struct lwp_info *lp, void *data)
825{
826 int *selector = data;
827
00d4fce6
MK
828 gdb_assert (selector != NULL);
829
830 /* Select only LWPs that have a SIGTRAP event pending. */
831 if (lp->status != 0
832 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
b1aeb4c5
MS
833 if ((*selector)-- == 0)
834 return 1;
835
836 return 0;
837}
838
839static int
840cancel_breakpoints_callback (struct lwp_info *lp, void *data)
841{
842 struct lwp_info *event_lp = data;
843
00d4fce6
MK
844 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
845 if (lp == event_lp)
846 return 0;
847
848 /* If a LWP other than the LWP that we're reporting an event for has
849 hit a GDB breakpoint (as opposed to some random trap signal),
850 then just arrange for it to hit it again later. We don't keep
851 the SIGTRAP status and don't forward the SIGTRAP signal to the
852 LWP. We will handle the current event, eventually we will resume
853 all LWPs, and this one will get its breakpoint trap again.
854
855 If we do not do this, then we run the risk that the user will
856 delete or disable the breakpoint, but the LWP will have already
857 tripped on it. */
858
859 if (lp->status != 0
860 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
861 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
862 DECR_PC_AFTER_BREAK))
b1aeb4c5
MS
863 {
864 if (debug_lin_lwp)
00d4fce6
MK
865 fprintf_unfiltered (gdb_stdlog,
866 "Push back breakpoint for LWP %ld\n",
867 GET_LWP (lp->ptid));
868
869 /* Back up the PC if necessary. */
b1aeb4c5 870 if (DECR_PC_AFTER_BREAK)
00d4fce6
MK
871 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
872
873 /* Throw away the SIGTRAP. */
b1aeb4c5
MS
874 lp->status = 0;
875 }
00d4fce6 876
b1aeb4c5
MS
877 return 0;
878}
879
00d4fce6 880/* Select one LWP out of those that have events pending. */
b1aeb4c5
MS
881
882static void
883select_event_lwp (struct lwp_info **orig_lp, int *status)
884{
885 int num_events = 0;
886 int random_selector;
887 struct lwp_info *event_lp;
888
00d4fce6
MK
889 /* Record the wait status for the origional LWP. */
890 (*orig_lp)->status = *status;
891
892 /* Give preference to any LWP that is being single-stepped. */
b1aeb4c5
MS
893 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
894 if (event_lp != NULL)
895 {
896 if (debug_lin_lwp)
00d4fce6
MK
897 fprintf_unfiltered (gdb_stdlog,
898 "Select single-step LWP %ld\n",
b1aeb4c5
MS
899 GET_LWP (event_lp->ptid));
900 }
901 else
902 {
903 /* No single-stepping LWP. Select one at random, out of those
00d4fce6 904 which have had SIGTRAP events. */
b1aeb4c5 905
00d4fce6
MK
906 /* First see how many SIGTRAP events we have. */
907 iterate_over_lwps (count_events_callback, &num_events);
b1aeb4c5 908
00d4fce6
MK
909 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
910 random_selector = (int)
b1aeb4c5
MS
911 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
912
00d4fce6
MK
913 if (debug_lin_lwp && num_events > 1)
914 fprintf_unfiltered (gdb_stdlog,
915 "Found %d SIGTRAP events, selecting #%d\n",
916 num_events, random_selector);
b1aeb4c5 917
00d4fce6
MK
918 event_lp = iterate_over_lwps (select_event_lwp_callback,
919 &random_selector);
b1aeb4c5
MS
920 }
921
922 if (event_lp != NULL)
923 {
00d4fce6 924 /* Switch the event LWP. */
b1aeb4c5
MS
925 *orig_lp = event_lp;
926 *status = event_lp->status;
b1aeb4c5 927 }
00d4fce6
MK
928
929 /* Flush the wait status for the event LWP. */
930 (*orig_lp)->status = 0;
b1aeb4c5
MS
931}
932
fce0e6e1
MK
933/* Return non-zero if LP has been resumed. */
934
935static int
936resumed_callback (struct lwp_info *lp, void *data)
937{
938 return lp->resumed;
939}
940
cacab7c4
MK
941#ifdef CHILD_WAIT
942
943/* We need to override child_wait to support attaching to cloned
944 processes, since a normal wait (as done by the default version)
945 ignores those processes. */
946
947/* Wait for child PTID to do something. Return id of the child,
948 minus_one_ptid in case of error; store status into *OURSTATUS. */
949
950ptid_t
951child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
952{
953 int save_errno;
954 int status;
955 pid_t pid;
956
957 do
958 {
959 set_sigint_trap (); /* Causes SIGINT to be passed on to the
960 attached process. */
961 set_sigio_trap ();
962
963 pid = waitpid (GET_PID (ptid), &status, 0);
964 if (pid == -1 && errno == ECHILD)
965 /* Try again with __WCLONE to check cloned processes. */
966 pid = waitpid (GET_PID (ptid), &status, __WCLONE);
967 save_errno = errno;
968
969 clear_sigio_trap ();
970 clear_sigint_trap ();
971 }
2d1bfe2e 972 while (pid == -1 && save_errno == EINTR);
cacab7c4
MK
973
974 if (pid == -1)
975 {
dc672865 976 warning ("Child process unexpectedly missing: %s", safe_strerror (errno));
cacab7c4
MK
977
978 /* Claim it exited with unknown signal. */
979 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
980 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
981 return minus_one_ptid;
982 }
983
984 store_waitstatus (ourstatus, status);
985 return pid_to_ptid (pid);
986}
987
988#endif
989
39f77062
KB
990static ptid_t
991lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
fb0e1ba7
MK
992{
993 struct lwp_info *lp = NULL;
994 int options = 0;
995 int status = 0;
39f77062 996 pid_t pid = PIDGET (ptid);
de4ca854
MK
997 sigset_t flush_mask;
998
999 sigemptyset (&flush_mask);
fb0e1ba7 1000
3f07c44b
MK
1001 /* Make sure SIGCHLD is blocked. */
1002 if (! sigismember (&blocked_mask, SIGCHLD))
1003 {
1004 sigaddset (&blocked_mask, SIGCHLD);
1005 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1006 }
1007
fb0e1ba7
MK
1008 retry:
1009
9a973a8f
MK
1010 /* Make sure there is at least one LWP that has been resumed, at
1011 least if there are any LWPs at all. */
1012 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
fce0e6e1 1013
fb0e1ba7
MK
1014 /* First check if there is a LWP with a wait status pending. */
1015 if (pid == -1)
1016 {
b1aeb4c5 1017 /* Any LWP that's been resumed will do. */
fb0e1ba7
MK
1018 lp = iterate_over_lwps (status_callback, NULL);
1019 if (lp)
1020 {
fb0e1ba7
MK
1021 status = lp->status;
1022 lp->status = 0;
b1aeb4c5
MS
1023
1024 if (debug_lin_lwp && status)
58eeadba
MK
1025 fprintf_unfiltered (gdb_stdlog,
1026 "Using pending wait status %s for LWP %ld.\n",
1027 status_to_str (status), GET_LWP (lp->ptid));
fb0e1ba7
MK
1028 }
1029
1030 /* But if we don't fine one, we'll have to wait, and check both
1031 cloned and uncloned processes. We start with the cloned
1032 processes. */
1033 options = __WCLONE | WNOHANG;
1034 }
39f77062 1035 else if (is_lwp (ptid))
fb0e1ba7 1036 {
7ca673cd 1037 if (debug_lin_lwp)
9085700c 1038 fprintf_unfiltered (gdb_stdlog,
b08cfdb6 1039 "Waiting for specific LWP %ld.\n",
ce696e05 1040 GET_LWP (ptid));
7ca673cd 1041
fb0e1ba7 1042 /* We have a specific LWP to check. */
39f77062 1043 lp = find_lwp_pid (ptid);
fb0e1ba7
MK
1044 gdb_assert (lp);
1045 status = lp->status;
1046 lp->status = 0;
7ca673cd 1047
b1aeb4c5 1048 if (debug_lin_lwp && status)
58eeadba
MK
1049 fprintf_unfiltered (gdb_stdlog,
1050 "Using pending wait status %s for LWP %ld.\n",
1051 status_to_str (status), GET_LWP (lp->ptid));
fb0e1ba7
MK
1052
1053 /* If we have to wait, take into account whether PID is a cloned
1054 process or not. And we have to convert it to something that
1055 the layer beneath us can understand. */
cacab7c4 1056 options = lp->cloned ? __WCLONE : 0;
39f77062 1057 pid = GET_LWP (ptid);
fb0e1ba7
MK
1058 }
1059
1060 if (status && lp->signalled)
1061 {
1062 /* A pending SIGSTOP may interfere with the normal stream of
1063 events. In a typical case where interference is a problem,
1064 we have a SIGSTOP signal pending for LWP A while
1065 single-stepping it, encounter an event in LWP B, and take the
1066 pending SIGSTOP while trying to stop LWP A. After processing
1067 the event in LWP B, LWP A is continued, and we'll never see
1068 the SIGTRAP associated with the last time we were
1069 single-stepping LWP A. */
1070
1071 /* Resume the thread. It should halt immediately returning the
1072 pending SIGSTOP. */
39f77062
KB
1073 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1074 TARGET_SIGNAL_0);
fb0e1ba7 1075 lp->stopped = 0;
fce0e6e1 1076 gdb_assert (lp->resumed);
fb0e1ba7
MK
1077
1078 /* This should catch the pending SIGSTOP. */
1079 stop_wait_callback (lp, NULL);
1080 }
1081
1082 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1083 attached process. */
1084 set_sigio_trap ();
1085
1086 while (status == 0)
1087 {
1088 pid_t lwpid;
1089
1090 lwpid = waitpid (pid, &status, options);
1091 if (lwpid > 0)
1092 {
1093 gdb_assert (pid == -1 || lwpid == pid);
1094
39f77062 1095 lp = find_lwp_pid (pid_to_ptid (lwpid));
fb0e1ba7
MK
1096 if (! lp)
1097 {
39f77062 1098 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
cacab7c4
MK
1099 if (options & __WCLONE)
1100 lp->cloned = 1;
1101
fb0e1ba7
MK
1102 if (threaded)
1103 {
3f07c44b
MK
1104 gdb_assert (WIFSTOPPED (status)
1105 && WSTOPSIG (status) == SIGSTOP);
fb0e1ba7
MK
1106 lp->signalled = 1;
1107
39f77062 1108 if (! in_thread_list (inferior_ptid))
fb0e1ba7 1109 {
39f77062
KB
1110 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1111 GET_PID (inferior_ptid));
1112 add_thread (inferior_ptid);
fb0e1ba7
MK
1113 }
1114
39f77062 1115 add_thread (lp->ptid);
fb0e1ba7 1116 printf_unfiltered ("[New %s]\n",
39f77062 1117 target_pid_to_str (lp->ptid));
fb0e1ba7
MK
1118 }
1119 }
1120
1121 /* Make sure we don't report a TARGET_WAITKIND_EXITED or
1122 TARGET_WAITKIND_SIGNALLED event if there are still LWP's
1123 left in the process. */
1124 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
1125 {
39f77062 1126 if (in_thread_list (lp->ptid))
fb0e1ba7 1127 {
e6328671 1128 /* Core GDB cannot deal with us deleting the current
fb0e1ba7 1129 thread. */
39f77062
KB
1130 if (! ptid_equal (lp->ptid, inferior_ptid))
1131 delete_thread (lp->ptid);
fb0e1ba7 1132 printf_unfiltered ("[%s exited]\n",
39f77062 1133 target_pid_to_str (lp->ptid));
fb0e1ba7 1134 }
7ca673cd 1135 if (debug_lin_lwp)
9085700c
MS
1136 fprintf_unfiltered (gdb_stdlog,
1137 "%s exited.\n",
39f77062 1138 target_pid_to_str (lp->ptid));
7ca673cd 1139
39f77062 1140 delete_lwp (lp->ptid);
fb0e1ba7
MK
1141
1142 /* Make sure there is at least one thread running. */
1143 gdb_assert (iterate_over_lwps (running_callback, NULL));
1144
1145 /* Discard the event. */
1146 status = 0;
1147 continue;
1148 }
1149
1150 /* Make sure we don't report a SIGSTOP that we sent
1151 ourselves in an attempt to stop an LWP. */
1152 if (lp->signalled && WIFSTOPPED (status)
1153 && WSTOPSIG (status) == SIGSTOP)
1154 {
7ca673cd 1155 if (debug_lin_lwp)
9085700c
MS
1156 fprintf_unfiltered (gdb_stdlog,
1157 "Delayed SIGSTOP caught for %s.\n",
39f77062 1158 target_pid_to_str (lp->ptid));
7ca673cd 1159
fb0e1ba7
MK
1160 /* This is a delayed SIGSTOP. */
1161 lp->signalled = 0;
1162
39f77062
KB
1163 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1164 TARGET_SIGNAL_0);
fb0e1ba7 1165 lp->stopped = 0;
fce0e6e1 1166 gdb_assert (lp->resumed);
fb0e1ba7
MK
1167
1168 /* Discard the event. */
1169 status = 0;
1170 continue;
1171 }
1172
1173 break;
1174 }
1175
1176 if (pid == -1)
1177 {
1178 /* Alternate between checking cloned and uncloned processes. */
1179 options ^= __WCLONE;
1180
1181 /* And suspend every time we have checked both. */
1182 if (options & __WCLONE)
1183 sigsuspend (&suspend_mask);
1184 }
1185
1186 /* We shouldn't end up here unless we want to try again. */
1187 gdb_assert (status == 0);
1188 }
1189
1190 clear_sigio_trap ();
1191 clear_sigint_trap ();
1192
1193 gdb_assert (lp);
1194
1195 /* Don't report signals that GDB isn't interested in, such as
1196 signals that are neither printed nor stopped upon. Stopping all
1197 threads can be a bit time-consuming so if we want decent
1198 performance with heavily multi-threaded programs, especially when
1199 they're using a high frequency timer, we'd better avoid it if we
1200 can. */
1201
1202 if (WIFSTOPPED (status))
1203 {
1204 int signo = target_signal_from_host (WSTOPSIG (status));
1205
1206 if (signal_stop_state (signo) == 0
1207 && signal_print_state (signo) == 0
1208 && signal_pass_state (signo) == 1)
1209 {
fce0e6e1
MK
1210 /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1211 here? It is not clear we should. GDB may not expect
1212 other threads to run. On the other hand, not resuming
1213 newly attached threads may cause an unwanted delay in
1214 getting them running. */
c4365b19 1215 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
fce0e6e1 1216 lp->stopped = 0;
fb0e1ba7
MK
1217 status = 0;
1218 goto retry;
1219 }
de4ca854
MK
1220
1221 if (signo == TARGET_SIGNAL_INT
1222 && signal_pass_state (signo) == 0)
1223 {
1224 /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1225 forwarded to the entire process group, that is, all LWP's
1226 will receive it. Since we only want to report it once,
1227 we try to flush it from all LWPs except this one. */
1228 sigaddset (&flush_mask, SIGINT);
1229 }
fb0e1ba7
MK
1230 }
1231
1232 /* This LWP is stopped now. */
1233 lp->stopped = 1;
1234
b1aeb4c5 1235 if (debug_lin_lwp)
58eeadba
MK
1236 fprintf_unfiltered (gdb_stdlog, "Candidate event %s in LWP %ld.\n",
1237 status_to_str (status), GET_LWP (lp->ptid));
b1aeb4c5 1238
fb0e1ba7
MK
1239 /* Now stop all other LWP's ... */
1240 iterate_over_lwps (stop_callback, NULL);
1241
1242 /* ... and wait until all of them have reported back that they're no
1243 longer running. */
de4ca854 1244 iterate_over_lwps (stop_wait_callback, &flush_mask);
fb0e1ba7 1245
00d4fce6
MK
1246 /* If we're not waiting for a specific LWP, choose an event LWP from
1247 among those that have had events. Giving equal priority to all
1248 LWPs that have had events helps prevent starvation. */
1249 if (pid == -1)
1250 select_event_lwp (&lp, &status);
b1aeb4c5 1251
00d4fce6
MK
1252 /* Now that we've selected our final event LWP, cancel any
1253 breakpoints in other LWPs that have hit a GDB breakpoint. See
1254 the comment in cancel_breakpoints_callback to find out why. */
1255 iterate_over_lwps (cancel_breakpoints_callback, lp);
b1aeb4c5 1256
fb0e1ba7
MK
1257 /* If we're not running in "threaded" mode, we'll report the bare
1258 process id. */
1259
1260 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
b1aeb4c5
MS
1261 {
1262 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
1263 if (debug_lin_lwp)
1264 fprintf_unfiltered (gdb_stdlog,
1265 "LLW: trap_ptid is %ld\n",
1266 GET_LWP (trap_ptid));
1267 }
fb0e1ba7 1268 else
39f77062 1269 trap_ptid = null_ptid;
fb0e1ba7
MK
1270
1271 store_waitstatus (ourstatus, status);
39f77062 1272 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
fb0e1ba7
MK
1273}
1274
1275static int
1276kill_callback (struct lwp_info *lp, void *data)
1277{
39f77062 1278 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
fb0e1ba7
MK
1279 return 0;
1280}
1281
1282static int
1283kill_wait_callback (struct lwp_info *lp, void *data)
1284{
1285 pid_t pid;
1286
1287 /* We must make sure that there are no pending events (delayed
1288 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
1289 program doesn't interfere with any following debugging session. */
1290
1291 /* For cloned processes we must check both with __WCLONE and
1292 without, since the exit status of a cloned process isn't reported
1293 with __WCLONE. */
cacab7c4 1294 if (lp->cloned)
fb0e1ba7
MK
1295 {
1296 do
1297 {
39f77062 1298 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
fb0e1ba7 1299 }
39f77062 1300 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1301
1302 gdb_assert (pid == -1 && errno == ECHILD);
1303 }
1304
1305 do
1306 {
39f77062 1307 pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
fb0e1ba7 1308 }
39f77062 1309 while (pid == GET_LWP (lp->ptid));
fb0e1ba7
MK
1310
1311 gdb_assert (pid == -1 && errno == ECHILD);
1312 return 0;
1313}
1314
1315static void
1316lin_lwp_kill (void)
1317{
1318 /* Kill all LWP's ... */
1319 iterate_over_lwps (kill_callback, NULL);
1320
1321 /* ... and wait until we've flushed all events. */
1322 iterate_over_lwps (kill_wait_callback, NULL);
1323
1324 target_mourn_inferior ();
1325}
1326
1327static void
1328lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
1329{
c194fbe1 1330 child_ops.to_create_inferior (exec_file, allargs, env);
fb0e1ba7
MK
1331}
1332
1333static void
1334lin_lwp_mourn_inferior (void)
1335{
c194fbe1 1336 trap_ptid = null_ptid;
fb0e1ba7 1337
c194fbe1 1338 /* Destroy LWP info; it's no longer valid. */
fb0e1ba7
MK
1339 init_lwp_list ();
1340
4c8de859 1341 /* Restore the original signal mask. */
3f07c44b
MK
1342 sigprocmask (SIG_SETMASK, &normal_mask, NULL);
1343 sigemptyset (&blocked_mask);
1344
c194fbe1 1345 child_ops.to_mourn_inferior ();
fb0e1ba7
MK
1346}
1347
1348static void
1349lin_lwp_fetch_registers (int regno)
1350{
39f77062 1351 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7 1352
39f77062
KB
1353 if (is_lwp (inferior_ptid))
1354 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7
MK
1355
1356 fetch_inferior_registers (regno);
1357
1358 do_cleanups (old_chain);
1359}
1360
1361static void
1362lin_lwp_store_registers (int regno)
1363{
39f77062 1364 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7 1365
39f77062
KB
1366 if (is_lwp (inferior_ptid))
1367 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7
MK
1368
1369 store_inferior_registers (regno);
1370
1371 do_cleanups (old_chain);
1372}
1373
1374static int
1375lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
e5da8f38 1376 struct mem_attrib *attrib,
fb0e1ba7
MK
1377 struct target_ops *target)
1378{
39f77062 1379 struct cleanup *old_chain = save_inferior_ptid ();
fb0e1ba7
MK
1380 int xfer;
1381
39f77062
KB
1382 if (is_lwp (inferior_ptid))
1383 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fb0e1ba7 1384
e5da8f38 1385 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
fb0e1ba7
MK
1386
1387 do_cleanups (old_chain);
1388 return xfer;
1389}
1390
1391static int
39f77062 1392lin_lwp_thread_alive (ptid_t ptid)
fb0e1ba7 1393{
39f77062 1394 gdb_assert (is_lwp (ptid));
fb0e1ba7
MK
1395
1396 errno = 0;
39f77062 1397 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
fb0e1ba7
MK
1398 if (errno)
1399 return 0;
1400
1401 return 1;
1402}
1403
1404static char *
39f77062 1405lin_lwp_pid_to_str (ptid_t ptid)
fb0e1ba7
MK
1406{
1407 static char buf[64];
1408
39f77062 1409 if (is_lwp (ptid))
fb0e1ba7 1410 {
b08cfdb6 1411 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
fb0e1ba7
MK
1412 return buf;
1413 }
1414
39f77062 1415 return normal_pid_to_str (ptid);
fb0e1ba7
MK
1416}
1417
1418static void
1419init_lin_lwp_ops (void)
1420{
1421#if 0
1422 lin_lwp_ops.to_open = lin_lwp_open;
1423#endif
1424 lin_lwp_ops.to_shortname = "lwp-layer";
1425 lin_lwp_ops.to_longname = "lwp-layer";
1426 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
1427 lin_lwp_ops.to_attach = lin_lwp_attach;
1428 lin_lwp_ops.to_detach = lin_lwp_detach;
1429 lin_lwp_ops.to_resume = lin_lwp_resume;
1430 lin_lwp_ops.to_wait = lin_lwp_wait;
1431 lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
1432 lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
1433 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
1434 lin_lwp_ops.to_kill = lin_lwp_kill;
1435 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
1436 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
1437 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
1438 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1439 lin_lwp_ops.to_stratum = thread_stratum;
1440 lin_lwp_ops.to_has_thread_control = tc_schedlock;
1441 lin_lwp_ops.to_magic = OPS_MAGIC;
1442}
1443
1444static void
1445sigchld_handler (int signo)
1446{
1447 /* Do nothing. The only reason for this handler is that it allows
1448 us to use sigsuspend in lin_lwp_wait above to wait for the
1449 arrival of a SIGCHLD. */
1450}
1451
1452void
1453_initialize_lin_lwp (void)
1454{
1455 struct sigaction action;
fb0e1ba7
MK
1456
1457 extern void thread_db_init (struct target_ops *);
1458
1459 init_lin_lwp_ops ();
1460 add_target (&lin_lwp_ops);
1461 thread_db_init (&lin_lwp_ops);
1462
4c8de859 1463 /* Save the original signal mask. */
3f07c44b
MK
1464 sigprocmask (SIG_SETMASK, NULL, &normal_mask);
1465
fb0e1ba7
MK
1466 action.sa_handler = sigchld_handler;
1467 sigemptyset (&action.sa_mask);
1468 action.sa_flags = 0;
1469 sigaction (SIGCHLD, &action, NULL);
1470
3f07c44b
MK
1471 /* Make sure we don't block SIGCHLD during a sigsuspend. */
1472 sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
fb0e1ba7 1473 sigdelset (&suspend_mask, SIGCHLD);
3f07c44b
MK
1474
1475 sigemptyset (&blocked_mask);
7ca673cd
MS
1476
1477 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1478 (char *) &debug_lin_lwp,
8605d56e 1479 "Set debugging of GNU/Linux lwp module.\n\
7ca673cd
MS
1480Enables printf debugging output.\n",
1481 &setdebuglist),
1482 &showdebuglist);
fb0e1ba7
MK
1483}
1484\f
1485
1486/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
8605d56e
AC
1487 the GNU/Linux Threads library and therefore doesn't really belong
1488 here. */
fb0e1ba7
MK
1489
1490/* Read variable NAME in the target and return its value if found.
1491 Otherwise return zero. It is assumed that the type of the variable
1492 is `int'. */
1493
1494static int
1495get_signo (const char *name)
1496{
1497 struct minimal_symbol *ms;
1498 int signo;
1499
1500 ms = lookup_minimal_symbol (name, NULL, NULL);
1501 if (ms == NULL)
1502 return 0;
1503
1504 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
1505 sizeof (signo)) != 0)
1506 return 0;
1507
1508 return signo;
1509}
1510
1511/* Return the set of signals used by the threads library in *SET. */
1512
1513void
1514lin_thread_get_thread_signals (sigset_t *set)
1515{
3f07c44b
MK
1516 struct sigaction action;
1517 int restart, cancel;
fb0e1ba7
MK
1518
1519 sigemptyset (set);
1520
1521 restart = get_signo ("__pthread_sig_restart");
1522 if (restart == 0)
1523 return;
1524
1525 cancel = get_signo ("__pthread_sig_cancel");
1526 if (cancel == 0)
1527 return;
1528
1529 sigaddset (set, restart);
1530 sigaddset (set, cancel);
3f07c44b 1531
8605d56e
AC
1532 /* The GNU/Linux Threads library makes terminating threads send a
1533 special "cancel" signal instead of SIGCHLD. Make sure we catch
1534 those (to prevent them from terminating GDB itself, which is
1535 likely to be their default action) and treat them the same way as
1536 SIGCHLD. */
3f07c44b
MK
1537
1538 action.sa_handler = sigchld_handler;
1539 sigemptyset (&action.sa_mask);
1540 action.sa_flags = 0;
1541 sigaction (cancel, &action, NULL);
1542
1543 /* We block the "cancel" signal throughout this code ... */
1544 sigaddset (&blocked_mask, cancel);
1545 sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
1546
1547 /* ... except during a sigsuspend. */
1548 sigdelset (&suspend_mask, cancel);
fb0e1ba7 1549}
This page took 0.378701 seconds and 4 git commands to generate.