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