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