Commit | Line | Data |
---|---|---|
d4f3574e SS |
1 | /* Low level interface for debugging GNU/Linux threads for GDB, |
2 | the GNU debugger. | |
3 | Copyright 1998, 1999 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This module implements the debugging interface of the linuxthreads package | |
22 | of the glibc. This package implements a simple clone()-based implementation | |
23 | of Posix threads for Linux. To use this module, be sure that you have at | |
24 | least the version of the linuxthreads package that holds the support of | |
25 | GDB (currently 0.8 included in the glibc-2.0.7). | |
26 | ||
27 | Right now, the linuxthreads package does not care of priority scheduling, | |
28 | so, neither this module does; In particular, the threads are resumed | |
29 | in any order, which could lead to different scheduling than the one | |
30 | happening when GDB does not control the execution. | |
31 | ||
32 | The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux: | |
33 | When a process is attached, then the attaching process becomes the current | |
34 | parent of the attached process, and the old parent has lost this child. | |
35 | If the old parent does a wait[...](), then this child is no longer | |
36 | considered by the kernel as a child of the old parent, thus leading to | |
37 | results of the call different when the child is attached and when it's not. | |
38 | ||
39 | A fix has been submitted to the Linux community to solve this problem, | |
40 | which consequences are not visible to the application itself, but on the | |
41 | process which may wait() for the completion of the application (mostly, | |
42 | it may consider that the application no longer exists (errno == ECHILD), | |
43 | although it does, and thus being unable to get the exit status and resource | |
44 | usage of the child. If by chance, it is able to wait() for the application | |
45 | after it has died (by receiving first a SIGCHILD, and then doing a wait(), | |
46 | then the exit status and resource usage may be wrong, because the | |
47 | linuxthreads package heavily relies on wait() synchronization to keep | |
48 | them correct. */ | |
49 | ||
50 | #include <sys/types.h> /* for pid_t */ | |
51 | #include <sys/ptrace.h> /* for PT_* flags */ | |
52 | #include <sys/wait.h> /* for WUNTRACED and __WCLONE flags */ | |
53 | #include <signal.h> /* for struct sigaction and NSIG */ | |
54 | #include <sys/utsname.h> | |
55 | ||
56 | #include "defs.h" | |
57 | #include "target.h" | |
58 | #include "inferior.h" | |
59 | #include "gdbcore.h" | |
60 | #include "gdbthread.h" | |
61 | #include "wait.h" | |
62 | #include "gdbcmd.h" | |
63 | #include "breakpoint.h" | |
64 | ||
65 | #ifndef PT_ATTACH | |
66 | #define PT_ATTACH PTRACE_ATTACH | |
67 | #endif | |
68 | #ifndef PT_KILL | |
69 | #define PT_KILL PTRACE_KILL | |
70 | #endif | |
71 | #ifndef PT_READ_U | |
72 | #define PT_READ_U PTRACE_PEEKUSR | |
73 | #endif | |
74 | ||
75 | #ifdef NSIG | |
76 | #define LINUXTHREAD_NSIG NSIG | |
77 | #else | |
78 | #ifdef _NSIG | |
79 | #define LINUXTHREAD_NSIG _NSIG | |
80 | #endif | |
81 | #endif | |
82 | ||
83 | extern int child_suppress_run; /* make inftarg.c non-runnable */ | |
84 | struct target_ops linuxthreads_ops; /* Forward declaration */ | |
85 | extern struct target_ops child_ops; /* target vector for inftarg.c */ | |
86 | ||
87 | static CORE_ADDR linuxthreads_handles; /* array of linuxthreads handles */ | |
88 | static CORE_ADDR linuxthreads_manager; /* pid of linuxthreads manager thread */ | |
89 | static CORE_ADDR linuxthreads_initial; /* pid of linuxthreads initial thread */ | |
90 | static CORE_ADDR linuxthreads_debug; /* linuxthreads internal debug flag */ | |
91 | static CORE_ADDR linuxthreads_num; /* number of valid handle entries */ | |
92 | ||
93 | static int linuxthreads_max; /* Maximum number of linuxthreads. | |
94 | Zero if this executable doesn't use | |
95 | threads, or wasn't linked with a | |
96 | debugger-friendly version of the | |
97 | linuxthreads library. */ | |
98 | ||
99 | static int linuxthreads_sizeof_handle; /* size of a linuxthreads handle */ | |
100 | static int linuxthreads_offset_descr; /* h_descr offset of the linuxthreads | |
101 | handle */ | |
102 | static int linuxthreads_offset_pid; /* p_pid offset of the linuxthreads | |
103 | descr */ | |
104 | ||
105 | static int linuxthreads_manager_pid; /* manager pid */ | |
106 | static int linuxthreads_initial_pid; /* initial pid */ | |
107 | ||
108 | /* These variables form a bag of threads with interesting status. If | |
109 | wait_thread (PID) finds that PID stopped for some interesting | |
110 | reason (i.e. anything other than stopped with SIGSTOP), then it | |
111 | records its status in this queue. linuxthreads_wait and | |
112 | linuxthreads_find_trap extract processes from here. */ | |
113 | static int *linuxthreads_wait_pid; /* wait array of pid */ | |
114 | static int *linuxthreads_wait_status; /* wait array of status */ | |
115 | static int linuxthreads_wait_last; /* index of last valid elt in | |
116 | linuxthreads_wait_{pid,status} */ | |
117 | ||
118 | static sigset_t linuxthreads_wait_mask; /* sigset with SIGCHLD */ | |
119 | ||
120 | static int linuxthreads_step_pid; /* current stepped pid */ | |
121 | static int linuxthreads_step_signo; /* current stepped target signal */ | |
122 | static int linuxthreads_exit_status; /* exit status of initial thread */ | |
123 | ||
124 | static int linuxthreads_inferior_pid; /* temporary internal inferior pid */ | |
125 | static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */ | |
126 | static int linuxthreads_attach_pending; /* attach command without wait */ | |
127 | ||
128 | static int linuxthreads_breakpoints_inserted; /* any breakpoints inserted */ | |
129 | ||
130 | /* LinuxThreads uses certain signals for communication between | |
131 | processes; we need to tell GDB to pass them through silently to the | |
132 | inferior. The LinuxThreads library has global variables we can | |
133 | read containing the relevant signal numbers, but since the signal | |
134 | numbers are chosen at run-time, those variables aren't initialized | |
135 | until the shared library's constructors have had a chance to run. */ | |
136 | ||
137 | struct linuxthreads_signal { | |
138 | ||
139 | /* The name of the LinuxThreads library variable that contains | |
140 | the signal number. */ | |
141 | char *var; | |
142 | ||
143 | /* True if this variable must exist for us to debug properly. */ | |
144 | int required; | |
145 | ||
146 | /* The variable's address in the inferior, or zero if the | |
147 | LinuxThreads library hasn't been loaded into this inferior yet. */ | |
148 | CORE_ADDR addr; | |
149 | ||
150 | /* The signal number, or zero if we don't know yet (either because | |
151 | we haven't found the variable, or it hasn't been initialized). | |
152 | This is an actual target signal number that you could pass to | |
153 | `kill', not a GDB signal number. */ | |
154 | int signal; | |
155 | ||
156 | /* GDB's original settings for `stop' and `print' for this signal. | |
157 | We restore them when the user selects a different executable. | |
158 | Invariant: if sig->signal != 0, then sig->{stop,print} contain | |
159 | the original settings. */ | |
160 | int stop, print; | |
161 | }; | |
162 | ||
163 | struct linuxthreads_signal linuxthreads_sig_restart = { | |
164 | "__pthread_sig_restart", 1, 0, 0, 0 | |
165 | }; | |
166 | struct linuxthreads_signal linuxthreads_sig_cancel = { | |
167 | "__pthread_sig_cancel", 1, 0, 0, 0 | |
168 | }; | |
169 | struct linuxthreads_signal linuxthreads_sig_debug = { | |
170 | "__pthread_sig_debug", 0, 0, 0, 0 | |
171 | }; | |
172 | ||
173 | /* A table of breakpoint locations, one per PID. */ | |
174 | static struct linuxthreads_breakpoint { | |
175 | CORE_ADDR pc; /* PC of breakpoint */ | |
176 | int pid; /* pid of breakpoint */ | |
177 | int step; /* whether the pc has been reached after sstep */ | |
178 | } *linuxthreads_breakpoint_zombie; /* Zombie breakpoints array */ | |
179 | static int linuxthreads_breakpoint_last; /* Last zombie breakpoint */ | |
180 | ||
181 | /* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address | |
182 | to {insert,remove}_breakpoint via this variable, since | |
183 | iterate_active_threads doesn't provide any way to pass values | |
184 | through to the worker function. */ | |
185 | static CORE_ADDR linuxthreads_breakpoint_addr; | |
186 | ||
187 | #define REMOVE_BREAKPOINT_ZOMBIE(_i) \ | |
188 | { \ | |
189 | if ((_i) < linuxthreads_breakpoint_last) \ | |
190 | linuxthreads_breakpoint_zombie[(_i)] = \ | |
191 | linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \ | |
192 | linuxthreads_breakpoint_last--; \ | |
193 | } | |
194 | ||
195 | ||
196 | \f | |
197 | #ifndef PTRACE_XFER_TYPE | |
198 | #define PTRACE_XFER_TYPE int | |
199 | #endif | |
200 | /* Check to see if the given thread is alive. */ | |
201 | static int | |
202 | linuxthreads_thread_alive (pid) | |
203 | int pid; | |
204 | { | |
205 | errno = 0; | |
206 | return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0; | |
207 | } | |
208 | ||
209 | /* On detach(), find a SIGTRAP status. If stop is non-zero, find a | |
210 | SIGSTOP one, too. | |
211 | ||
212 | Make sure PID is ready to run, and free of interference from our | |
213 | efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals). If | |
214 | STOP is zero, just look for a SIGTRAP. If STOP is non-zero, look | |
215 | for a SIGSTOP, too. Return non-zero if PID is alive and ready to | |
216 | run; return zero if PID is dead. | |
217 | ||
218 | PID may or may not be stopped at the moment, and we may or may not | |
219 | have waited for it already. We check the linuxthreads_wait bag in | |
220 | case we've already got a status for it. We may possibly wait for | |
221 | it ourselves. | |
222 | ||
223 | PID may have signals waiting to be delivered. If they're caused by | |
224 | our efforts to debug it, accept them with wait, but don't pass them | |
225 | through to PID. Do pass all other signals through. */ | |
226 | static int | |
227 | linuxthreads_find_trap (pid, stop) | |
228 | int pid; | |
229 | int stop; | |
230 | { | |
231 | int i; | |
232 | int rpid; | |
233 | int status; | |
234 | int found_stop = 0; | |
235 | int found_trap = 0; | |
236 | ||
237 | /* PID may have any number of signals pending. The kernel will | |
238 | report each of them to us via wait, and then it's up to us to | |
239 | pass them along to the process via ptrace, if we so choose. | |
240 | ||
241 | We need to paw through the whole set until we've found a SIGTRAP | |
242 | (or a SIGSTOP, if `stop' is set). We don't pass the SIGTRAP (or | |
243 | SIGSTOP) through, but we do re-send all the others, so PID will | |
244 | receive them when we resume it. */ | |
245 | int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int)); | |
246 | int last = 0; | |
247 | ||
248 | /* Look at the pending status */ | |
249 | for (i = linuxthreads_wait_last; i >= 0; i--) | |
250 | if (linuxthreads_wait_pid[i] == pid) | |
251 | { | |
252 | status = linuxthreads_wait_status[i]; | |
253 | ||
254 | /* Delete the i'th member of the table. Since the table is | |
255 | unordered, we can do this simply by copying the table's | |
256 | last element to the i'th position, and shrinking the table | |
257 | by one element. */ | |
258 | if (i < linuxthreads_wait_last) | |
259 | { | |
260 | linuxthreads_wait_status[i] = | |
261 | linuxthreads_wait_status[linuxthreads_wait_last]; | |
262 | linuxthreads_wait_pid[i] = | |
263 | linuxthreads_wait_pid[linuxthreads_wait_last]; | |
264 | } | |
265 | linuxthreads_wait_last--; | |
266 | ||
267 | if (!WIFSTOPPED(status)) /* Thread has died */ | |
268 | return 0; | |
269 | ||
270 | if (WSTOPSIG(status) == SIGTRAP) | |
271 | { | |
272 | if (stop) | |
273 | found_trap = 1; | |
274 | else | |
275 | return 1; | |
276 | } | |
277 | else if (WSTOPSIG(status) == SIGSTOP) | |
278 | { | |
279 | if (stop) | |
280 | found_stop = 1; | |
281 | } | |
282 | else | |
283 | { | |
284 | wstatus[0] = status; | |
285 | last = 1; | |
286 | } | |
287 | ||
288 | break; | |
289 | } | |
290 | ||
291 | if (stop) | |
292 | { | |
293 | /* Make sure that we'll find what we're looking for. */ | |
294 | if (!found_trap) | |
295 | kill (pid, SIGTRAP); | |
296 | if (!found_stop) | |
297 | kill (pid, SIGSTOP); | |
298 | } | |
299 | ||
300 | /* Catch all status until SIGTRAP and optionally SIGSTOP show up. */ | |
301 | for (;;) | |
302 | { | |
303 | child_resume (pid, 1, TARGET_SIGNAL_0); | |
304 | ||
305 | for (;;) | |
306 | { | |
307 | rpid = waitpid (pid, &status, __WCLONE); | |
308 | if (rpid > 0) | |
309 | break; | |
310 | if (errno == EINTR) | |
311 | continue; | |
312 | ||
313 | /* There are a few reasons the wait call above may have | |
314 | failed. If the thread manager dies, its children get | |
315 | reparented, and this interferes with GDB waiting for | |
316 | them, in some cases. Another possibility is that the | |
317 | initial thread was not cloned, so calling wait with | |
318 | __WCLONE won't find it. I think neither of these should | |
319 | occur in modern Linux kernels --- they don't seem to in | |
320 | 2.0.36. */ | |
321 | rpid = waitpid (pid, &status, 0); | |
322 | if (rpid > 0) | |
323 | break; | |
324 | if (errno != EINTR) | |
325 | perror_with_name ("waitpid"); | |
326 | } | |
327 | ||
328 | if (!WIFSTOPPED(status)) /* Thread has died */ | |
329 | return 0; | |
330 | ||
331 | if (WSTOPSIG(status) == SIGTRAP) | |
332 | if (!stop || found_stop) | |
333 | break; | |
334 | else | |
335 | found_trap = 1; | |
336 | else if (WSTOPSIG(status) != SIGSTOP) | |
337 | wstatus[last++] = status; | |
338 | else if (stop) | |
339 | if (found_trap) | |
340 | break; | |
341 | else | |
342 | found_stop = 1; | |
343 | } | |
344 | ||
345 | /* Resend any other signals we noticed to the thread, to be received | |
346 | when we continue it. */ | |
347 | while (--last >= 0) | |
348 | kill (pid, WSTOPSIG(wstatus[last])); | |
349 | ||
350 | return 1; | |
351 | } | |
352 | ||
353 | /* Cleanup stub for save_inferior_pid. */ | |
354 | static void | |
355 | restore_inferior_pid (arg) | |
356 | void *arg; | |
357 | { | |
358 | int pid = (int) arg; | |
359 | inferior_pid = pid; | |
360 | } | |
361 | ||
362 | /* Register a cleanup to restore the value of inferior_pid. */ | |
363 | static struct cleanup * | |
364 | save_inferior_pid () | |
365 | { | |
366 | return make_cleanup (restore_inferior_pid, (void *) inferior_pid); | |
367 | } | |
368 | ||
369 | static void | |
370 | sigchld_handler (signo) | |
371 | int signo; | |
372 | { | |
373 | /* This handler is used to get an EINTR while doing waitpid() | |
374 | when an event is received */ | |
375 | } | |
376 | ||
377 | /* Have we already collected a wait status for PID in the | |
378 | linuxthreads_wait bag? */ | |
379 | static int | |
380 | linuxthreads_pending_status (pid) | |
381 | int pid; | |
382 | { | |
383 | int i; | |
384 | for (i = linuxthreads_wait_last; i >= 0; i--) | |
385 | if (linuxthreads_wait_pid[i] == pid) | |
386 | return 1; | |
387 | return 0; | |
388 | } | |
389 | ||
390 | \f | |
391 | /* Internal linuxthreads signal management */ | |
392 | ||
393 | /* Check in OBJFILE for the variable that holds the number for signal SIG. | |
394 | We assume that we've already found other LinuxThreads-ish variables | |
395 | in OBJFILE, so we complain if it's required, but not there. | |
396 | Return true iff things are okay. */ | |
397 | static int | |
398 | find_signal_var (sig, objfile) | |
399 | struct linuxthreads_signal *sig; | |
400 | struct objfile *objfile; | |
401 | { | |
402 | struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile); | |
403 | ||
404 | if (! ms) | |
405 | { | |
406 | if (sig->required) | |
407 | { | |
408 | fprintf_unfiltered (gdb_stderr, | |
409 | "Unable to find linuxthreads symbol \"%s\"\n", | |
410 | sig->var); | |
411 | return 0; | |
412 | } | |
413 | else | |
414 | { | |
415 | sig->addr = 0; | |
416 | return 1; | |
417 | } | |
418 | } | |
419 | ||
420 | sig->addr = SYMBOL_VALUE_ADDRESS (ms); | |
421 | ||
422 | return 1; | |
423 | } | |
424 | ||
425 | static int | |
426 | find_all_signal_vars (objfile) | |
427 | struct objfile *objfile; | |
428 | { | |
429 | return ( find_signal_var (&linuxthreads_sig_restart, objfile) | |
430 | && find_signal_var (&linuxthreads_sig_cancel, objfile) | |
431 | && find_signal_var (&linuxthreads_sig_debug, objfile)); | |
432 | } | |
433 | ||
434 | /* A struct complaint isn't appropriate here. */ | |
435 | static int complained_cannot_determine_thread_signal_number = 0; | |
436 | ||
437 | /* Check to see if the variable holding the signal number for SIG has | |
438 | been initialized yet. If it has, tell GDB to pass that signal | |
439 | through to the inferior silently. */ | |
440 | static void | |
441 | check_signal_number (sig) | |
442 | struct linuxthreads_signal *sig; | |
443 | { | |
444 | int num; | |
445 | ||
446 | if (sig->signal) | |
447 | /* We already know this signal number. */ | |
448 | return; | |
449 | ||
450 | if (! sig->addr) | |
451 | /* We don't know the variable's address yet. */ | |
452 | return; | |
453 | ||
454 | if (target_read_memory (sig->addr, (char *)&num, sizeof (num)) | |
455 | != 0) | |
456 | { | |
457 | /* If this happens once, it'll probably happen for all the | |
458 | signals, so only complain once. */ | |
459 | if (! complained_cannot_determine_thread_signal_number) | |
460 | warning ("Cannot determine thread signal number; " | |
461 | "GDB may report spurious signals."); | |
462 | complained_cannot_determine_thread_signal_number = 1; | |
463 | return; | |
464 | } | |
465 | ||
466 | if (num == 0) | |
467 | /* It hasn't been initialized yet. */ | |
468 | return; | |
469 | ||
470 | /* We know sig->signal was zero, and is becoming non-zero, so it's | |
471 | okay to sample GDB's original settings. */ | |
472 | sig->signal = num; | |
473 | sig->stop = signal_stop_update (target_signal_from_host (num), 0); | |
474 | sig->print = signal_print_update (target_signal_from_host (num), 0); | |
475 | } | |
476 | ||
477 | ||
478 | static void | |
479 | check_all_signal_numbers () | |
480 | { | |
481 | /* If this isn't a LinuxThreads program, quit early. */ | |
482 | if (! linuxthreads_max) | |
483 | return; | |
484 | ||
485 | check_signal_number (&linuxthreads_sig_restart); | |
486 | check_signal_number (&linuxthreads_sig_cancel); | |
487 | check_signal_number (&linuxthreads_sig_debug); | |
488 | ||
489 | /* handle linuxthread exit */ | |
490 | if (linuxthreads_sig_debug.signal | |
491 | || linuxthreads_sig_restart.signal) | |
492 | { | |
493 | struct sigaction sact; | |
494 | ||
495 | sact.sa_handler = sigchld_handler; | |
496 | sigemptyset(&sact.sa_mask); | |
497 | sact.sa_flags = 0; | |
498 | if (linuxthreads_sig_debug.signal > 0) | |
499 | sigaction(linuxthreads_sig_cancel.signal, &sact, NULL); | |
500 | else | |
501 | sigaction(linuxthreads_sig_restart.signal, &sact, NULL); | |
502 | } | |
503 | } | |
504 | ||
505 | ||
506 | /* Restore GDB's original settings for SIG. | |
507 | This should only be called when we're no longer sure if we're | |
508 | talking to an executable that uses LinuxThreads, so we clear the | |
509 | signal number and variable address too. */ | |
510 | static void | |
511 | restore_signal (sig) | |
512 | struct linuxthreads_signal *sig; | |
513 | { | |
514 | if (! sig->signal) | |
515 | return; | |
516 | ||
517 | /* We know sig->signal was non-zero, and is becoming zero, so it's | |
518 | okay to restore GDB's original settings. */ | |
519 | signal_stop_update (target_signal_from_host (sig->signal), sig->stop); | |
520 | signal_print_update (target_signal_from_host (sig->signal), sig->print); | |
521 | ||
522 | sig->signal = 0; | |
523 | sig->addr = 0; | |
524 | } | |
525 | ||
526 | ||
527 | /* Restore GDB's original settings for all LinuxThreads signals. | |
528 | This should only be called when we're no longer sure if we're | |
529 | talking to an executable that uses LinuxThreads, so we clear the | |
530 | signal number and variable address too. */ | |
531 | static void | |
532 | restore_all_signals () | |
533 | { | |
534 | restore_signal (&linuxthreads_sig_restart); | |
535 | restore_signal (&linuxthreads_sig_cancel); | |
536 | restore_signal (&linuxthreads_sig_debug); | |
537 | ||
538 | /* If it happens again, we should complain again. */ | |
539 | complained_cannot_determine_thread_signal_number = 0; | |
540 | } | |
541 | ||
542 | ||
543 | \f | |
544 | ||
545 | /* Apply FUNC to the pid of each active thread. This consults the | |
546 | inferior's handle table to find active threads. | |
547 | ||
548 | If ALL is non-zero, process all threads. | |
549 | If ALL is zero, skip threads with pending status. */ | |
550 | static void | |
551 | iterate_active_threads (func, all) | |
552 | void (*func)(int); | |
553 | int all; | |
554 | { | |
555 | CORE_ADDR descr; | |
556 | int pid; | |
557 | int i; | |
558 | int num; | |
559 | ||
560 | read_memory (linuxthreads_num, (char *)&num, sizeof (int)); | |
561 | ||
562 | for (i = 0; i < linuxthreads_max && num > 0; i++) | |
563 | { | |
564 | read_memory (linuxthreads_handles + | |
565 | linuxthreads_sizeof_handle * i + linuxthreads_offset_descr, | |
566 | (char *)&descr, sizeof (void *)); | |
567 | if (descr) | |
568 | { | |
569 | num--; | |
570 | read_memory (descr + linuxthreads_offset_pid, | |
571 | (char *)&pid, sizeof (pid_t)); | |
572 | if (pid > 0 && pid != linuxthreads_manager_pid | |
573 | && (all || (!linuxthreads_pending_status (pid)))) | |
574 | (*func)(pid); | |
575 | } | |
576 | } | |
577 | ||
578 | } | |
579 | ||
580 | /* Insert a thread breakpoint at linuxthreads_breakpoint_addr. | |
581 | This is the worker function for linuxthreads_insert_breakpoint, | |
582 | which passes it to iterate_active_threads. */ | |
583 | static void | |
584 | insert_breakpoint (pid) | |
585 | int pid; | |
586 | { | |
587 | int j; | |
588 | ||
589 | /* Remove (if any) the positive zombie breakpoint. */ | |
590 | for (j = linuxthreads_breakpoint_last; j >= 0; j--) | |
591 | if (linuxthreads_breakpoint_zombie[j].pid == pid) | |
592 | { | |
593 | if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK | |
594 | == linuxthreads_breakpoint_addr) | |
595 | && !linuxthreads_breakpoint_zombie[j].step) | |
596 | REMOVE_BREAKPOINT_ZOMBIE(j); | |
597 | break; | |
598 | } | |
599 | } | |
600 | ||
601 | /* Note that we're about to remove a thread breakpoint at | |
602 | linuxthreads_breakpoint_addr. | |
603 | ||
604 | This is the worker function for linuxthreads_remove_breakpoint, | |
605 | which passes it to iterate_active_threads. The actual work of | |
606 | overwriting the breakpoint instruction is done by | |
607 | child_ops.to_remove_breakpoint; here, we simply create a zombie | |
608 | breakpoint if the thread's PC is pointing at the breakpoint being | |
609 | removed. */ | |
610 | static void | |
611 | remove_breakpoint (pid) | |
612 | int pid; | |
613 | { | |
614 | int j; | |
615 | ||
616 | /* Insert a positive zombie breakpoint (if needed). */ | |
617 | for (j = 0; j <= linuxthreads_breakpoint_last; j++) | |
618 | if (linuxthreads_breakpoint_zombie[j].pid == pid) | |
619 | break; | |
620 | ||
621 | if (in_thread_list (pid) && linuxthreads_thread_alive (pid)) | |
622 | { | |
623 | CORE_ADDR pc = read_pc_pid (pid); | |
624 | if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK | |
625 | && j > linuxthreads_breakpoint_last) | |
626 | { | |
627 | linuxthreads_breakpoint_zombie[j].pid = pid; | |
628 | linuxthreads_breakpoint_zombie[j].pc = pc; | |
629 | linuxthreads_breakpoint_zombie[j].step = 0; | |
630 | linuxthreads_breakpoint_last++; | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | /* Kill a thread */ | |
636 | static void | |
637 | kill_thread (pid) | |
638 | int pid; | |
639 | { | |
640 | if (in_thread_list (pid)) | |
641 | ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
642 | else | |
643 | kill (pid, SIGKILL); | |
644 | } | |
645 | ||
646 | /* Resume a thread */ | |
647 | static void | |
648 | resume_thread (pid) | |
649 | int pid; | |
650 | { | |
651 | if (pid != inferior_pid | |
652 | && in_thread_list (pid) | |
653 | && linuxthreads_thread_alive (pid)) | |
654 | if (pid == linuxthreads_step_pid) | |
655 | child_resume (pid, 1, linuxthreads_step_signo); | |
656 | else | |
657 | child_resume (pid, 0, TARGET_SIGNAL_0); | |
658 | } | |
659 | ||
660 | /* Detach a thread */ | |
661 | static void | |
662 | detach_thread (pid) | |
663 | int pid; | |
664 | { | |
665 | if (in_thread_list (pid) && linuxthreads_thread_alive (pid)) | |
666 | { | |
667 | /* Remove pending SIGTRAP and SIGSTOP */ | |
668 | linuxthreads_find_trap (pid, 1); | |
669 | ||
670 | inferior_pid = pid; | |
671 | detach (TARGET_SIGNAL_0); | |
672 | inferior_pid = linuxthreads_manager_pid; | |
673 | } | |
674 | } | |
675 | ||
676 | /* Stop a thread */ | |
677 | static void | |
678 | stop_thread (pid) | |
679 | int pid; | |
680 | { | |
681 | if (pid != inferior_pid) | |
682 | if (in_thread_list (pid)) | |
683 | kill (pid, SIGSTOP); | |
684 | else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0) | |
685 | { | |
686 | if (!linuxthreads_attach_pending) | |
687 | printf_unfiltered ("[New %s]\n", target_pid_to_str (pid)); | |
688 | add_thread (pid); | |
689 | if (linuxthreads_sig_debug.signal) | |
690 | /* After a new thread in glibc 2.1 signals gdb its existence, | |
691 | it suspends itself and wait for linuxthreads_sig_restart, | |
692 | now we can wake up it. */ | |
693 | kill (pid, linuxthreads_sig_restart.signal); | |
694 | } | |
695 | else | |
696 | perror_with_name ("ptrace in stop_thread"); | |
697 | } | |
698 | ||
699 | /* Wait for a thread */ | |
700 | static void | |
701 | wait_thread (pid) | |
702 | int pid; | |
703 | { | |
704 | int status; | |
705 | int rpid; | |
706 | ||
707 | if (pid != inferior_pid && in_thread_list (pid)) | |
708 | { | |
709 | for (;;) | |
710 | { | |
711 | /* Get first pid status. */ | |
712 | rpid = waitpid(pid, &status, __WCLONE); | |
713 | if (rpid > 0) | |
714 | break; | |
715 | if (errno == EINTR) | |
716 | continue; | |
717 | ||
718 | /* There are two reasons this might have failed: | |
719 | ||
720 | 1) PID is the initial thread, which wasn't cloned, so | |
721 | passing the __WCLONE flag to waitpid prevented us from | |
722 | finding it. | |
723 | ||
724 | 2) The manager thread is the parent of all but the | |
725 | initial thread; if it dies, the children will all be | |
726 | reparented to init, which will wait for them. This means | |
727 | our call to waitpid won't find them. | |
728 | ||
729 | Actually, based on a casual look at the 2.0.36 kernel | |
730 | code, I don't think either of these cases happen. But I | |
731 | don't have things set up for remotely debugging the | |
732 | kernel, so I'm not sure. And perhaps older kernels | |
733 | didn't work. */ | |
734 | rpid = waitpid(pid, &status, 0); | |
735 | if (rpid > 0) | |
736 | break; | |
737 | if (errno != EINTR && linuxthreads_thread_alive (pid)) | |
738 | perror_with_name ("waitpid"); | |
739 | ||
740 | /* the thread is dead. */ | |
741 | return; | |
742 | } | |
743 | if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) | |
744 | { | |
745 | linuxthreads_wait_pid[++linuxthreads_wait_last] = pid; | |
746 | linuxthreads_wait_status[linuxthreads_wait_last] = status; | |
747 | } | |
748 | } | |
749 | } | |
750 | ||
751 | /* Walk through the linuxthreads handles in order to detect all | |
752 | threads and stop them */ | |
753 | static void | |
754 | update_stop_threads (test_pid) | |
755 | int test_pid; | |
756 | { | |
757 | struct cleanup *old_chain = NULL; | |
758 | ||
759 | check_all_signal_numbers (); | |
760 | ||
761 | if (linuxthreads_manager_pid == 0) | |
762 | { | |
763 | if (linuxthreads_manager) | |
764 | { | |
765 | if (test_pid > 0 && test_pid != inferior_pid) | |
766 | { | |
767 | old_chain = save_inferior_pid (); | |
768 | inferior_pid = test_pid; | |
769 | } | |
770 | read_memory (linuxthreads_manager, | |
771 | (char *)&linuxthreads_manager_pid, sizeof (pid_t)); | |
772 | } | |
773 | if (linuxthreads_initial) | |
774 | { | |
775 | if (test_pid > 0 && test_pid != inferior_pid) | |
776 | { | |
777 | old_chain = save_inferior_pid (); | |
778 | inferior_pid = test_pid; | |
779 | } | |
780 | read_memory(linuxthreads_initial, | |
781 | (char *)&linuxthreads_initial_pid, sizeof (pid_t)); | |
782 | } | |
783 | } | |
784 | ||
785 | if (linuxthreads_manager_pid != 0) | |
786 | { | |
787 | if (old_chain == NULL && test_pid > 0 && | |
788 | test_pid != inferior_pid && linuxthreads_thread_alive (test_pid)) | |
789 | { | |
790 | old_chain = save_inferior_pid (); | |
791 | inferior_pid = test_pid; | |
792 | } | |
793 | ||
794 | if (linuxthreads_thread_alive (inferior_pid)) | |
795 | { | |
796 | if (test_pid > 0) | |
797 | { | |
798 | if (test_pid != linuxthreads_manager_pid | |
799 | && !linuxthreads_pending_status (linuxthreads_manager_pid)) | |
800 | { | |
801 | stop_thread (linuxthreads_manager_pid); | |
802 | wait_thread (linuxthreads_manager_pid); | |
803 | } | |
804 | if (!in_thread_list (test_pid)) | |
805 | { | |
806 | if (!linuxthreads_attach_pending) | |
807 | printf_unfiltered ("[New %s]\n", | |
808 | target_pid_to_str (test_pid)); | |
809 | add_thread (test_pid); | |
810 | if (linuxthreads_sig_debug.signal | |
811 | && inferior_pid == test_pid) | |
812 | /* After a new thread in glibc 2.1 signals gdb its | |
813 | existence, it suspends itself and wait for | |
814 | linuxthreads_sig_restart, now we can wake up | |
815 | it. */ | |
816 | kill (test_pid, linuxthreads_sig_restart.signal); | |
817 | } | |
818 | } | |
819 | iterate_active_threads (stop_thread, 0); | |
820 | iterate_active_threads (wait_thread, 0); | |
821 | } | |
822 | } | |
823 | ||
824 | if (old_chain != NULL) | |
825 | do_cleanups (old_chain); | |
826 | } | |
827 | ||
828 | /* This routine is called whenever a new symbol table is read in, or when all | |
829 | symbol tables are removed. libpthread can only be initialized when it | |
830 | finds the right variables in libpthread.so. Since it's a shared library, | |
831 | those variables don't show up until the library gets mapped and the symbol | |
832 | table is read in. */ | |
833 | ||
834 | void | |
835 | linuxthreads_new_objfile (objfile) | |
836 | struct objfile *objfile; | |
837 | { | |
838 | struct minimal_symbol *ms; | |
839 | ||
840 | if (!objfile) | |
841 | { | |
842 | /* We're starting an entirely new executable, so we can no | |
843 | longer be sure that it uses LinuxThreads. Restore the signal | |
844 | flags to their original states. */ | |
845 | restore_all_signals (); | |
846 | ||
847 | /* Indicate that we don't know anything's address any more. */ | |
848 | linuxthreads_max = 0; | |
849 | ||
850 | return; | |
851 | } | |
852 | ||
853 | /* If we've already found our variables in another objfile, don't | |
854 | bother looking for them again. */ | |
855 | if (linuxthreads_max) | |
856 | return; | |
857 | ||
858 | if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile)) | |
859 | /* This object file isn't the pthreads library. */ | |
860 | return; | |
861 | ||
862 | if ((ms = lookup_minimal_symbol ("__pthread_threads_debug", | |
863 | NULL, objfile)) == NULL) | |
864 | { | |
865 | /* The debugging-aware libpthreads is not present in this objfile */ | |
866 | warning ("\ | |
867 | This program seems to use POSIX threads, but the thread library used\n\ | |
868 | does not support debugging. This may make using GDB difficult. Don't\n\ | |
869 | set breakpoints or single-step through code that might be executed by\n\ | |
870 | any thread other than the main thread."); | |
871 | return; | |
872 | } | |
873 | linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms); | |
874 | ||
875 | /* Read internal structures configuration */ | |
876 | if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle", | |
877 | NULL, objfile)) == NULL | |
878 | || target_read_memory (SYMBOL_VALUE_ADDRESS (ms), | |
879 | (char *)&linuxthreads_sizeof_handle, | |
880 | sizeof (linuxthreads_sizeof_handle)) != 0) | |
881 | { | |
882 | fprintf_unfiltered (gdb_stderr, | |
883 | "Unable to find linuxthreads symbol \"%s\"\n", | |
884 | "__pthread_sizeof_handle"); | |
885 | return; | |
886 | } | |
887 | ||
888 | if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr", | |
889 | NULL, objfile)) == NULL | |
890 | || target_read_memory (SYMBOL_VALUE_ADDRESS (ms), | |
891 | (char *)&linuxthreads_offset_descr, | |
892 | sizeof (linuxthreads_offset_descr)) != 0) | |
893 | { | |
894 | fprintf_unfiltered (gdb_stderr, | |
895 | "Unable to find linuxthreads symbol \"%s\"\n", | |
896 | "__pthread_offsetof_descr"); | |
897 | return; | |
898 | } | |
899 | ||
900 | if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid", | |
901 | NULL, objfile)) == NULL | |
902 | || target_read_memory (SYMBOL_VALUE_ADDRESS (ms), | |
903 | (char *)&linuxthreads_offset_pid, | |
904 | sizeof (linuxthreads_offset_pid)) != 0) | |
905 | { | |
906 | fprintf_unfiltered (gdb_stderr, | |
907 | "Unable to find linuxthreads symbol \"%s\"\n", | |
908 | "__pthread_offsetof_pid"); | |
909 | return; | |
910 | } | |
911 | ||
912 | if (! find_all_signal_vars (objfile)) | |
913 | return; | |
914 | ||
915 | /* Read adresses of internal structures to access */ | |
916 | if ((ms = lookup_minimal_symbol ("__pthread_handles", | |
917 | NULL, objfile)) == NULL) | |
918 | { | |
919 | fprintf_unfiltered (gdb_stderr, | |
920 | "Unable to find linuxthreads symbol \"%s\"\n", | |
921 | "__pthread_handles"); | |
922 | return; | |
923 | } | |
924 | linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms); | |
925 | ||
926 | if ((ms = lookup_minimal_symbol ("__pthread_handles_num", | |
927 | NULL, objfile)) == NULL) | |
928 | { | |
929 | fprintf_unfiltered (gdb_stderr, | |
930 | "Unable to find linuxthreads symbol \"%s\"\n", | |
931 | "__pthread_handles_num"); | |
932 | return; | |
933 | } | |
934 | linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms); | |
935 | ||
936 | if ((ms = lookup_minimal_symbol ("__pthread_manager_thread", | |
937 | NULL, objfile)) == NULL) | |
938 | { | |
939 | fprintf_unfiltered (gdb_stderr, | |
940 | "Unable to find linuxthreads symbol \"%s\"\n", | |
941 | "__pthread_manager_thread"); | |
942 | return; | |
943 | } | |
944 | linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid; | |
945 | ||
946 | if ((ms = lookup_minimal_symbol ("__pthread_initial_thread", | |
947 | NULL, objfile)) == NULL) | |
948 | { | |
949 | fprintf_unfiltered (gdb_stderr, | |
950 | "Unable to find linuxthreads symbol \"%s\"\n", | |
951 | "__pthread_initial_thread"); | |
952 | return; | |
953 | } | |
954 | linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid; | |
955 | ||
956 | /* Search for this last, so it won't be set to a non-zero value unless | |
957 | we successfully found all the symbols above. */ | |
958 | if ((ms = lookup_minimal_symbol ("__pthread_threads_max", | |
959 | NULL, objfile)) == NULL | |
960 | || target_read_memory (SYMBOL_VALUE_ADDRESS (ms), | |
961 | (char *)&linuxthreads_max, | |
962 | sizeof (linuxthreads_max)) != 0) | |
963 | { | |
964 | fprintf_unfiltered (gdb_stderr, | |
965 | "Unable to find linuxthreads symbol \"%s\"\n", | |
966 | "__pthread_threads_max"); | |
967 | return; | |
968 | } | |
969 | ||
970 | /* Allocate gdb internal structures */ | |
971 | linuxthreads_wait_pid = | |
972 | (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1)); | |
973 | linuxthreads_wait_status = | |
974 | (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1)); | |
975 | linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *) | |
976 | xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1)); | |
977 | ||
978 | if (inferior_pid && !linuxthreads_attach_pending) | |
979 | { | |
980 | int on = 1; | |
981 | target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on)); | |
982 | linuxthreads_attach_pending = 1; | |
983 | update_stop_threads (inferior_pid); | |
984 | linuxthreads_attach_pending = 0; | |
985 | } | |
986 | } | |
987 | ||
988 | /* If we have switched threads from a one that stopped at breakpoint, | |
989 | return 1 otherwise 0. */ | |
990 | ||
991 | int | |
992 | linuxthreads_prepare_to_proceed (step) | |
993 | int step; | |
994 | { | |
995 | if (!linuxthreads_max | |
996 | || !linuxthreads_manager_pid | |
997 | || !linuxthreads_breakpoint_pid | |
998 | || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid))) | |
999 | return 0; | |
1000 | ||
1001 | if (step) | |
1002 | { | |
1003 | /* Mark the current inferior as single stepping process. */ | |
1004 | linuxthreads_step_pid = inferior_pid; | |
1005 | } | |
1006 | ||
1007 | linuxthreads_inferior_pid = linuxthreads_breakpoint_pid; | |
1008 | return linuxthreads_breakpoint_pid; | |
1009 | } | |
1010 | ||
1011 | /* Convert a pid to printable form. */ | |
1012 | ||
1013 | char * | |
1014 | linuxthreads_pid_to_str (pid) | |
1015 | int pid; | |
1016 | { | |
1017 | static char buf[100]; | |
1018 | ||
1019 | sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid, | |
1020 | (pid == linuxthreads_manager_pid) ? " (manager thread)" | |
1021 | : (pid == linuxthreads_initial_pid) ? " (initial thread)" | |
1022 | : ""); | |
1023 | ||
1024 | return buf; | |
1025 | } | |
1026 | ||
1027 | /* Attach to process PID, then initialize for debugging it | |
1028 | and wait for the trace-trap that results from attaching. */ | |
1029 | ||
1030 | static void | |
1031 | linuxthreads_attach (args, from_tty) | |
1032 | char *args; | |
1033 | int from_tty; | |
1034 | { | |
1035 | if (!args) | |
1036 | error_no_arg ("process-id to attach"); | |
1037 | ||
1038 | push_target (&linuxthreads_ops); | |
1039 | linuxthreads_breakpoints_inserted = 1; | |
1040 | linuxthreads_breakpoint_last = -1; | |
1041 | linuxthreads_wait_last = -1; | |
1042 | linuxthreads_exit_status = __W_STOPCODE(0); | |
1043 | ||
1044 | child_ops.to_attach (args, from_tty); | |
1045 | ||
1046 | if (linuxthreads_max) | |
1047 | linuxthreads_attach_pending = 1; | |
1048 | } | |
1049 | ||
1050 | /* Take a program previously attached to and detaches it. | |
1051 | The program resumes execution and will no longer stop | |
1052 | on signals, etc. We'd better not have left any breakpoints | |
1053 | in the program or it'll die when it hits one. For this | |
1054 | to work, it may be necessary for the process to have been | |
1055 | previously attached. It *might* work if the program was | |
1056 | started via the normal ptrace (PTRACE_TRACEME). */ | |
1057 | ||
1058 | static void | |
1059 | linuxthreads_detach (args, from_tty) | |
1060 | char *args; | |
1061 | int from_tty; | |
1062 | { | |
1063 | if (linuxthreads_max) | |
1064 | { | |
1065 | int i; | |
1066 | int pid; | |
1067 | int off = 0; | |
1068 | target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off)); | |
1069 | ||
1070 | /* Walk through linuxthreads array in order to detach known threads. */ | |
1071 | if (linuxthreads_manager_pid != 0) | |
1072 | { | |
1073 | /* Get rid of all positive zombie breakpoints. */ | |
1074 | for (i = 0; i <= linuxthreads_breakpoint_last; i++) | |
1075 | { | |
1076 | if (linuxthreads_breakpoint_zombie[i].step) | |
1077 | continue; | |
1078 | ||
1079 | pid = linuxthreads_breakpoint_zombie[i].pid; | |
1080 | if (!linuxthreads_thread_alive (pid)) | |
1081 | continue; | |
1082 | ||
1083 | if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid)) | |
1084 | continue; | |
1085 | ||
1086 | /* Continue in STEP mode until the thread pc has moved or | |
1087 | until SIGTRAP is found on the same PC. */ | |
1088 | if (linuxthreads_find_trap (pid, 0) | |
1089 | && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid)) | |
1090 | write_pc_pid (linuxthreads_breakpoint_zombie[i].pc | |
1091 | - DECR_PC_AFTER_BREAK, pid); | |
1092 | } | |
1093 | ||
1094 | /* Detach thread after thread. */ | |
1095 | inferior_pid = linuxthreads_manager_pid; | |
1096 | iterate_active_threads (detach_thread, 1); | |
1097 | ||
1098 | /* Remove pending SIGTRAP and SIGSTOP */ | |
1099 | linuxthreads_find_trap (inferior_pid, 1); | |
1100 | ||
1101 | linuxthreads_wait_last = -1; | |
1102 | linuxthreads_exit_status = __W_STOPCODE(0); | |
1103 | } | |
1104 | ||
1105 | linuxthreads_inferior_pid = 0; | |
1106 | linuxthreads_breakpoint_pid = 0; | |
1107 | linuxthreads_step_pid = 0; | |
1108 | linuxthreads_step_signo = TARGET_SIGNAL_0; | |
1109 | linuxthreads_manager_pid = 0; | |
1110 | linuxthreads_initial_pid = 0; | |
1111 | linuxthreads_attach_pending = 0; | |
1112 | init_thread_list (); /* Destroy thread info */ | |
1113 | } | |
1114 | ||
1115 | child_ops.to_detach (args, from_tty); | |
1116 | ||
1117 | unpush_target (&linuxthreads_ops); | |
1118 | } | |
1119 | ||
1120 | /* Resume execution of process PID. If STEP is nozero, then | |
1121 | just single step it. If SIGNAL is nonzero, restart it with that | |
1122 | signal activated. */ | |
1123 | ||
1124 | static void | |
1125 | linuxthreads_resume (pid, step, signo) | |
1126 | int pid; | |
1127 | int step; | |
1128 | enum target_signal signo; | |
1129 | { | |
1130 | if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0) | |
1131 | child_ops.to_resume (pid, step, signo); | |
1132 | else | |
1133 | { | |
1134 | int rpid; | |
1135 | if (linuxthreads_inferior_pid) | |
1136 | { | |
1137 | /* Prepare resume of the last thread that hit a breakpoint */ | |
1138 | linuxthreads_breakpoints_inserted = 0; | |
1139 | rpid = linuxthreads_inferior_pid; | |
1140 | linuxthreads_step_signo = signo; | |
1141 | } | |
1142 | else | |
1143 | { | |
1144 | struct cleanup *old_chain = NULL; | |
1145 | int i; | |
1146 | ||
1147 | if (pid < 0) | |
1148 | { | |
1149 | linuxthreads_step_pid = step ? inferior_pid : 0; | |
1150 | linuxthreads_step_signo = signo; | |
1151 | rpid = inferior_pid; | |
1152 | } | |
1153 | else | |
1154 | rpid = pid; | |
1155 | ||
1156 | if (pid < 0 || !step) | |
1157 | { | |
1158 | linuxthreads_breakpoints_inserted = 1; | |
1159 | ||
1160 | /* Walk through linuxthreads array in order to resume threads */ | |
1161 | if (pid >= 0 && inferior_pid != pid) | |
1162 | { | |
1163 | old_chain = save_inferior_pid (); | |
1164 | inferior_pid = pid; | |
1165 | } | |
1166 | ||
1167 | iterate_active_threads (resume_thread, 0); | |
1168 | if (linuxthreads_manager_pid != inferior_pid | |
1169 | && !linuxthreads_pending_status (linuxthreads_manager_pid)) | |
1170 | resume_thread (linuxthreads_manager_pid); | |
1171 | } | |
1172 | else | |
1173 | linuxthreads_breakpoints_inserted = 0; | |
1174 | ||
1175 | /* Deal with zombie breakpoint */ | |
1176 | for (i = 0; i <= linuxthreads_breakpoint_last; i++) | |
1177 | if (linuxthreads_breakpoint_zombie[i].pid == rpid) | |
1178 | { | |
1179 | if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid)) | |
1180 | { | |
1181 | /* The current pc is out of zombie breakpoint. */ | |
1182 | REMOVE_BREAKPOINT_ZOMBIE(i); | |
1183 | } | |
1184 | break; | |
1185 | } | |
1186 | ||
1187 | if (old_chain != NULL) | |
1188 | do_cleanups (old_chain); | |
1189 | } | |
1190 | ||
1191 | /* Resume initial thread. */ | |
1192 | if (!linuxthreads_pending_status (rpid)) | |
1193 | child_ops.to_resume (rpid, step, signo); | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | /* Wait for any threads to stop. We may have to convert PID from a thread id | |
1198 | to a LWP id, and vice versa on the way out. */ | |
1199 | ||
1200 | static int | |
1201 | linuxthreads_wait (pid, ourstatus) | |
1202 | int pid; | |
1203 | struct target_waitstatus *ourstatus; | |
1204 | { | |
1205 | int status; | |
1206 | int rpid; | |
1207 | int i; | |
1208 | int last; | |
1209 | int *wstatus; | |
1210 | ||
1211 | if (linuxthreads_max && !linuxthreads_breakpoints_inserted) | |
1212 | wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int)); | |
1213 | ||
1214 | /* See if the inferior has chosen values for its signals yet. By | |
1215 | checking for them here, we can be sure we've updated GDB's signal | |
1216 | handling table before the inferior ever gets one of them. (Well, | |
1217 | before we notice, anyway.) */ | |
1218 | check_all_signal_numbers (); | |
1219 | ||
1220 | for (;;) | |
1221 | { | |
1222 | if (!linuxthreads_max) | |
1223 | rpid = 0; | |
1224 | else if (!linuxthreads_breakpoints_inserted) | |
1225 | { | |
1226 | if (linuxthreads_inferior_pid) | |
1227 | pid = linuxthreads_inferior_pid; | |
1228 | else if (pid < 0) | |
1229 | pid = inferior_pid; | |
1230 | last = rpid = 0; | |
1231 | } | |
1232 | else if (pid < 0 && linuxthreads_wait_last >= 0) | |
1233 | { | |
1234 | status = linuxthreads_wait_status[linuxthreads_wait_last]; | |
1235 | rpid = linuxthreads_wait_pid[linuxthreads_wait_last--]; | |
1236 | } | |
1237 | else if (pid > 0 && linuxthreads_pending_status (pid)) | |
1238 | { | |
1239 | for (i = linuxthreads_wait_last; i >= 0; i--) | |
1240 | if (linuxthreads_wait_pid[i] == pid) | |
1241 | break; | |
1242 | if (i < 0) | |
1243 | rpid = 0; | |
1244 | else | |
1245 | { | |
1246 | status = linuxthreads_wait_status[i]; | |
1247 | rpid = pid; | |
1248 | if (i < linuxthreads_wait_last) | |
1249 | { | |
1250 | linuxthreads_wait_status[i] = | |
1251 | linuxthreads_wait_status[linuxthreads_wait_last]; | |
1252 | linuxthreads_wait_pid[i] = | |
1253 | linuxthreads_wait_pid[linuxthreads_wait_last]; | |
1254 | } | |
1255 | linuxthreads_wait_last--; | |
1256 | } | |
1257 | } | |
1258 | else | |
1259 | rpid = 0; | |
1260 | ||
1261 | if (rpid == 0) | |
1262 | { | |
1263 | int save_errno; | |
1264 | sigset_t omask; | |
1265 | ||
1266 | set_sigint_trap(); /* Causes SIGINT to be passed on to the | |
1267 | attached process. */ | |
1268 | set_sigio_trap (); | |
1269 | ||
1270 | sigprocmask(SIG_BLOCK, &linuxthreads_wait_mask, &omask); | |
1271 | for (;;) | |
1272 | { | |
1273 | rpid = waitpid (pid, &status, __WCLONE | WNOHANG); | |
1274 | if (rpid > 0) | |
1275 | break; | |
1276 | if (rpid == 0) | |
1277 | save_errno = 0; | |
1278 | else if (errno != EINTR) | |
1279 | save_errno = errno; | |
1280 | else | |
1281 | continue; | |
1282 | ||
1283 | rpid = waitpid (pid, &status, WNOHANG); | |
1284 | if (rpid > 0) | |
1285 | break; | |
1286 | if (rpid < 0) | |
1287 | if (errno == EINTR) | |
1288 | continue; | |
1289 | else if (save_errno != 0) | |
1290 | break; | |
1291 | ||
1292 | sigsuspend(&omask); | |
1293 | } | |
1294 | sigprocmask(SIG_SETMASK, &omask, NULL); | |
1295 | ||
1296 | save_errno = errno; | |
1297 | clear_sigio_trap (); | |
1298 | ||
1299 | clear_sigint_trap(); | |
1300 | ||
1301 | if (rpid == -1) | |
1302 | { | |
1303 | if (WIFEXITED(linuxthreads_exit_status)) | |
1304 | { | |
1305 | store_waitstatus (ourstatus, linuxthreads_exit_status); | |
1306 | return inferior_pid; | |
1307 | } | |
1308 | else | |
1309 | { | |
1310 | fprintf_unfiltered | |
1311 | (gdb_stderr, "Child process unexpectedly missing: %s.\n", | |
1312 | safe_strerror (save_errno)); | |
1313 | /* Claim it exited with unknown signal. */ | |
1314 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1315 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
1316 | return -1; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | /* Signals arrive in any order. So get all signals until SIGTRAP | |
1321 | and resend previous ones to be held after. */ | |
1322 | if (linuxthreads_max | |
1323 | && !linuxthreads_breakpoints_inserted | |
1324 | && WIFSTOPPED(status)) | |
1325 | if (WSTOPSIG(status) == SIGTRAP) | |
1326 | { | |
1327 | while (--last >= 0) | |
1328 | kill (rpid, WSTOPSIG(wstatus[last])); | |
1329 | ||
1330 | /* insert negative zombie breakpoint */ | |
1331 | for (i = 0; i <= linuxthreads_breakpoint_last; i++) | |
1332 | if (linuxthreads_breakpoint_zombie[i].pid == rpid) | |
1333 | break; | |
1334 | if (i > linuxthreads_breakpoint_last) | |
1335 | { | |
1336 | linuxthreads_breakpoint_zombie[i].pid = rpid; | |
1337 | linuxthreads_breakpoint_last++; | |
1338 | } | |
1339 | linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid); | |
1340 | linuxthreads_breakpoint_zombie[i].step = 1; | |
1341 | } | |
1342 | else | |
1343 | { | |
1344 | if (WSTOPSIG(status) != SIGSTOP) | |
1345 | { | |
1346 | for (i = 0; i < last; i++) | |
1347 | if (wstatus[i] == status) | |
1348 | break; | |
1349 | if (i >= last) | |
1350 | wstatus[last++] = status; | |
1351 | } | |
1352 | child_resume (rpid, 1, TARGET_SIGNAL_0); | |
1353 | continue; | |
1354 | } | |
1355 | if (linuxthreads_inferior_pid) | |
1356 | linuxthreads_inferior_pid = 0; | |
1357 | } | |
1358 | ||
1359 | if (linuxthreads_max && !stop_soon_quietly) | |
1360 | { | |
1361 | if (linuxthreads_max | |
1362 | && WIFSTOPPED(status) | |
1363 | && WSTOPSIG(status) == SIGSTOP) | |
1364 | { | |
1365 | /* Skip SIGSTOP signals. */ | |
1366 | if (!linuxthreads_pending_status (rpid)) | |
1367 | if (linuxthreads_step_pid == rpid) | |
1368 | child_resume (rpid, 1, linuxthreads_step_signo); | |
1369 | else | |
1370 | child_resume (rpid, 0, TARGET_SIGNAL_0); | |
1371 | continue; | |
1372 | } | |
1373 | ||
1374 | /* Do no report exit status of cloned threads. */ | |
1375 | if (WIFEXITED(status)) | |
1376 | { | |
1377 | if (rpid == linuxthreads_initial_pid) | |
1378 | linuxthreads_exit_status = status; | |
1379 | ||
1380 | /* Remove any zombie breakpoint. */ | |
1381 | for (i = 0; i <= linuxthreads_breakpoint_last; i++) | |
1382 | if (linuxthreads_breakpoint_zombie[i].pid == rpid) | |
1383 | { | |
1384 | REMOVE_BREAKPOINT_ZOMBIE(i); | |
1385 | break; | |
1386 | } | |
1387 | if (pid > 0) | |
1388 | pid = -1; | |
1389 | continue; | |
1390 | } | |
1391 | ||
1392 | /* Deal with zombie breakpoint */ | |
1393 | for (i = 0; i <= linuxthreads_breakpoint_last; i++) | |
1394 | if (linuxthreads_breakpoint_zombie[i].pid == rpid) | |
1395 | break; | |
1396 | ||
1397 | if (i <= linuxthreads_breakpoint_last) | |
1398 | { | |
1399 | /* There is a potential zombie breakpoint */ | |
1400 | if (WIFEXITED(status) | |
1401 | || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid)) | |
1402 | { | |
1403 | /* The current pc is out of zombie breakpoint. */ | |
1404 | REMOVE_BREAKPOINT_ZOMBIE(i); | |
1405 | } | |
1406 | else if (!linuxthreads_breakpoint_zombie[i].step | |
1407 | && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) | |
1408 | { | |
1409 | /* This is a real one ==> decrement PC and restart. */ | |
1410 | write_pc_pid (linuxthreads_breakpoint_zombie[i].pc | |
1411 | - DECR_PC_AFTER_BREAK, rpid); | |
1412 | if (linuxthreads_step_pid == rpid) | |
1413 | child_resume (rpid, 1, linuxthreads_step_signo); | |
1414 | else | |
1415 | child_resume (rpid, 0, TARGET_SIGNAL_0); | |
1416 | continue; | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | /* Walk through linuxthreads array in order to stop them */ | |
1421 | if (linuxthreads_breakpoints_inserted) | |
1422 | update_stop_threads (rpid); | |
1423 | ||
1424 | } | |
1425 | else if (rpid != inferior_pid) | |
1426 | continue; | |
1427 | ||
1428 | store_waitstatus (ourstatus, status); | |
1429 | ||
1430 | if (linuxthreads_attach_pending && !stop_soon_quietly) | |
1431 | { | |
1432 | int on = 1; | |
1433 | target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on)); | |
1434 | update_stop_threads (rpid); | |
1435 | linuxthreads_attach_pending = 0; | |
1436 | } | |
1437 | ||
1438 | if (linuxthreads_breakpoints_inserted | |
1439 | && WIFSTOPPED(status) | |
1440 | && WSTOPSIG(status) == SIGTRAP) | |
1441 | linuxthreads_breakpoint_pid = rpid; | |
1442 | else if (linuxthreads_breakpoint_pid) | |
1443 | linuxthreads_breakpoint_pid = 0; | |
1444 | ||
1445 | return rpid; | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | /* Fork an inferior process, and start debugging it with ptrace. */ | |
1450 | ||
1451 | static void | |
1452 | linuxthreads_create_inferior (exec_file, allargs, env) | |
1453 | char *exec_file; | |
1454 | char *allargs; | |
1455 | char **env; | |
1456 | { | |
1457 | if (!exec_file && !exec_bfd) | |
1458 | { | |
1459 | error ("No executable file specified.\n\ | |
1460 | Use the \"file\" or \"exec-file\" command."); | |
1461 | return; | |
1462 | } | |
1463 | ||
1464 | push_target (&linuxthreads_ops); | |
1465 | linuxthreads_breakpoints_inserted = 1; | |
1466 | linuxthreads_breakpoint_last = -1; | |
1467 | linuxthreads_wait_last = -1; | |
1468 | linuxthreads_exit_status = __W_STOPCODE(0); | |
1469 | ||
1470 | if (linuxthreads_max) | |
1471 | linuxthreads_attach_pending = 1; | |
1472 | ||
1473 | child_ops.to_create_inferior (exec_file, allargs, env); | |
1474 | } | |
1475 | ||
1476 | /* Clean up after the inferior dies. */ | |
1477 | ||
1478 | static void | |
1479 | linuxthreads_mourn_inferior () | |
1480 | { | |
1481 | if (linuxthreads_max) | |
1482 | { | |
1483 | int off = 0; | |
1484 | target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off)); | |
1485 | ||
1486 | linuxthreads_inferior_pid = 0; | |
1487 | linuxthreads_breakpoint_pid = 0; | |
1488 | linuxthreads_step_pid = 0; | |
1489 | linuxthreads_step_signo = TARGET_SIGNAL_0; | |
1490 | linuxthreads_manager_pid = 0; | |
1491 | linuxthreads_initial_pid = 0; | |
1492 | linuxthreads_attach_pending = 0; | |
1493 | init_thread_list(); /* Destroy thread info */ | |
1494 | } | |
1495 | ||
1496 | child_ops.to_mourn_inferior (); | |
1497 | ||
1498 | unpush_target (&linuxthreads_ops); | |
1499 | } | |
1500 | ||
1501 | /* Kill the inferior process */ | |
1502 | ||
1503 | static void | |
1504 | linuxthreads_kill () | |
1505 | { | |
1506 | int rpid; | |
1507 | int status; | |
1508 | ||
1509 | if (inferior_pid == 0) | |
1510 | return; | |
1511 | ||
1512 | if (linuxthreads_max && linuxthreads_manager_pid != 0) | |
1513 | { | |
1514 | /* Remove all threads status. */ | |
1515 | inferior_pid = linuxthreads_manager_pid; | |
1516 | iterate_active_threads (kill_thread, 1); | |
1517 | } | |
1518 | ||
1519 | kill_thread (inferior_pid); | |
1520 | ||
1521 | #if 0 | |
1522 | /* doing_quit_force solves a real problem, but I think a properly | |
1523 | placed call to catch_errors would do the trick much more cleanly. */ | |
1524 | if (doing_quit_force >= 0) | |
1525 | { | |
1526 | if (linuxthreads_max && linuxthreads_manager_pid != 0) | |
1527 | { | |
1528 | /* Wait for thread to complete */ | |
1529 | while ((rpid = waitpid (-1, &status, __WCLONE)) > 0) | |
1530 | if (!WIFEXITED(status)) | |
1531 | kill_thread (rpid); | |
1532 | ||
1533 | while ((rpid = waitpid (-1, &status, 0)) > 0) | |
1534 | if (!WIFEXITED(status)) | |
1535 | kill_thread (rpid); | |
1536 | } | |
1537 | else | |
1538 | while ((rpid = waitpid (inferior_pid, &status, 0)) > 0) | |
1539 | if (!WIFEXITED(status)) | |
1540 | ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0); | |
1541 | } | |
1542 | #endif | |
1543 | ||
1544 | /* Wait for all threads. */ | |
1545 | do | |
1546 | rpid = waitpid (-1, &status, __WCLONE | WNOHANG); | |
1547 | while (rpid > 0 || errno == EINTR); | |
1548 | ||
1549 | do | |
1550 | rpid = waitpid (-1, &status, WNOHANG); | |
1551 | while (rpid > 0 || errno == EINTR); | |
1552 | ||
1553 | linuxthreads_mourn_inferior (); | |
1554 | } | |
1555 | ||
1556 | /* Insert a breakpoint */ | |
1557 | ||
1558 | static int | |
1559 | linuxthreads_insert_breakpoint (addr, contents_cache) | |
1560 | CORE_ADDR addr; | |
1561 | char *contents_cache; | |
1562 | { | |
1563 | if (linuxthreads_max && linuxthreads_manager_pid != 0) | |
1564 | { | |
1565 | linuxthreads_breakpoint_addr = addr; | |
1566 | iterate_active_threads (insert_breakpoint, 1); | |
1567 | insert_breakpoint (linuxthreads_manager_pid); | |
1568 | } | |
1569 | ||
1570 | return child_ops.to_insert_breakpoint (addr, contents_cache); | |
1571 | } | |
1572 | ||
1573 | /* Remove a breakpoint */ | |
1574 | ||
1575 | static int | |
1576 | linuxthreads_remove_breakpoint (addr, contents_cache) | |
1577 | CORE_ADDR addr; | |
1578 | char *contents_cache; | |
1579 | { | |
1580 | if (linuxthreads_max && linuxthreads_manager_pid != 0) | |
1581 | { | |
1582 | linuxthreads_breakpoint_addr = addr; | |
1583 | iterate_active_threads (remove_breakpoint, 1); | |
1584 | remove_breakpoint (linuxthreads_manager_pid); | |
1585 | } | |
1586 | ||
1587 | return child_ops.to_remove_breakpoint (addr, contents_cache); | |
1588 | } | |
1589 | ||
1590 | /* Mark our target-struct as eligible for stray "run" and "attach" commands. */ | |
1591 | ||
1592 | static int | |
1593 | linuxthreads_can_run () | |
1594 | { | |
1595 | return child_suppress_run; | |
1596 | } | |
1597 | \f | |
1598 | static void | |
1599 | init_linuxthreads_ops () | |
1600 | { | |
1601 | linuxthreads_ops.to_shortname = "linuxthreads"; | |
1602 | linuxthreads_ops.to_longname = "LINUX threads and pthread."; | |
1603 | linuxthreads_ops.to_doc = "LINUX threads and pthread support."; | |
1604 | linuxthreads_ops.to_attach = linuxthreads_attach; | |
1605 | linuxthreads_ops.to_detach = linuxthreads_detach; | |
1606 | linuxthreads_ops.to_resume = linuxthreads_resume; | |
1607 | linuxthreads_ops.to_wait = linuxthreads_wait; | |
1608 | linuxthreads_ops.to_kill = linuxthreads_kill; | |
1609 | linuxthreads_ops.to_can_run = linuxthreads_can_run; | |
1610 | linuxthreads_ops.to_stratum = thread_stratum; | |
1611 | linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint; | |
1612 | linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint; | |
1613 | linuxthreads_ops.to_create_inferior = linuxthreads_create_inferior; | |
1614 | linuxthreads_ops.to_mourn_inferior = linuxthreads_mourn_inferior; | |
1615 | linuxthreads_ops.to_thread_alive = linuxthreads_thread_alive; | |
1616 | linuxthreads_ops.to_magic = OPS_MAGIC; | |
1617 | } | |
1618 | ||
1619 | void | |
1620 | _initialize_linuxthreads () | |
1621 | { | |
1622 | struct sigaction sact; | |
1623 | ||
1624 | init_linuxthreads_ops (); | |
1625 | add_target (&linuxthreads_ops); | |
1626 | child_suppress_run = 1; | |
1627 | ||
1628 | /* Attach SIGCHLD handler */ | |
1629 | sact.sa_handler = sigchld_handler; | |
1630 | sigemptyset (&sact.sa_mask); | |
1631 | sact.sa_flags = 0; | |
1632 | sigaction (SIGCHLD, &sact, NULL); | |
1633 | ||
1634 | /* initialize SIGCHLD mask */ | |
1635 | sigemptyset (&linuxthreads_wait_mask); | |
1636 | sigaddset (&linuxthreads_wait_mask, SIGCHLD); | |
1637 | } |