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