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