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