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