| 1 | /* Multi-threaded debugging support for GNU/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 "gdb_string.h" |
| 25 | #include <errno.h> |
| 26 | #include <signal.h> |
| 27 | #include <sys/ptrace.h> |
| 28 | #include "gdb_wait.h" |
| 29 | |
| 30 | #include "gdbthread.h" |
| 31 | #include "inferior.h" |
| 32 | #include "target.h" |
| 33 | #include "regcache.h" |
| 34 | #include "gdbcmd.h" |
| 35 | |
| 36 | static int debug_lin_lwp; |
| 37 | extern const char *strsignal (int sig); |
| 38 | |
| 39 | /* On GNU/Linux there are no real LWP's. The closest thing to LWP's |
| 40 | are processes sharing the same VM space. A multi-threaded process |
| 41 | is basically a group of such processes. However, such a grouping |
| 42 | is almost entirely a user-space issue; the kernel doesn't enforce |
| 43 | such a grouping at all (this might change in the future). In |
| 44 | general, we'll rely on the threads library (i.e. the GNU/Linux |
| 45 | Threads library) to provide such a grouping. |
| 46 | |
| 47 | It is perfectly well possible to write a multi-threaded application |
| 48 | without the assistance of a threads library, by using the clone |
| 49 | system call directly. This module should be able to give some |
| 50 | rudimentary support for debugging such applications if developers |
| 51 | specify the CLONE_PTRACE flag in the clone system call, and are |
| 52 | using the Linux kernel 2.4 or above. |
| 53 | |
| 54 | Note that there are some peculiarities in GNU/Linux that affect |
| 55 | this code: |
| 56 | |
| 57 | - In general one should specify the __WCLONE flag to waitpid in |
| 58 | order to make it report events for any of the cloned processes |
| 59 | (and leave it out for the initial process). However, if a cloned |
| 60 | process has exited the exit status is only reported if the |
| 61 | __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but |
| 62 | we cannot use it since GDB must work on older systems too. |
| 63 | |
| 64 | - When a traced, cloned process exits and is waited for by the |
| 65 | debugger, the kernel reassigns it to the original parent and |
| 66 | keeps it around as a "zombie". Somehow, the GNU/Linux Threads |
| 67 | library doesn't notice this, which leads to the "zombie problem": |
| 68 | When debugged a multi-threaded process that spawns a lot of |
| 69 | threads will run out of processes, even if the threads exit, |
| 70 | because the "zombies" stay around. */ |
| 71 | |
| 72 | /* Structure describing a LWP. */ |
| 73 | struct lwp_info |
| 74 | { |
| 75 | /* The process id of the LWP. This is a combination of the LWP id |
| 76 | and overall process id. */ |
| 77 | ptid_t ptid; |
| 78 | |
| 79 | /* Non-zero if this LWP is cloned. In this context "cloned" means |
| 80 | that the LWP is reporting to its parent using a signal other than |
| 81 | SIGCHLD. */ |
| 82 | int cloned; |
| 83 | |
| 84 | /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report |
| 85 | it back yet). */ |
| 86 | int signalled; |
| 87 | |
| 88 | /* Non-zero if this LWP is stopped. */ |
| 89 | int stopped; |
| 90 | |
| 91 | /* Non-zero if this LWP will be/has been resumed. Note that an LWP |
| 92 | can be marked both as stopped and resumed at the same time. This |
| 93 | happens if we try to resume an LWP that has a wait status |
| 94 | pending. We shouldn't let the LWP run until that wait status has |
| 95 | been processed, but we should not report that wait status if GDB |
| 96 | didn't try to let the LWP run. */ |
| 97 | int resumed; |
| 98 | |
| 99 | /* If non-zero, a pending wait status. */ |
| 100 | int status; |
| 101 | |
| 102 | /* Non-zero if we were stepping this LWP. */ |
| 103 | int step; |
| 104 | |
| 105 | /* Next LWP in list. */ |
| 106 | struct lwp_info *next; |
| 107 | }; |
| 108 | |
| 109 | /* List of known LWPs. */ |
| 110 | static struct lwp_info *lwp_list; |
| 111 | |
| 112 | /* Number of LWPs in the list. */ |
| 113 | static int num_lwps; |
| 114 | |
| 115 | /* Non-zero if we're running in "threaded" mode. */ |
| 116 | static int threaded; |
| 117 | \f |
| 118 | |
| 119 | #define GET_LWP(ptid) ptid_get_lwp (ptid) |
| 120 | #define GET_PID(ptid) ptid_get_pid (ptid) |
| 121 | #define is_lwp(ptid) (GET_LWP (ptid) != 0) |
| 122 | #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) |
| 123 | |
| 124 | /* If the last reported event was a SIGTRAP, this variable is set to |
| 125 | the process id of the LWP/thread that got it. */ |
| 126 | ptid_t trap_ptid; |
| 127 | \f |
| 128 | |
| 129 | /* This module's target-specific operations. */ |
| 130 | static struct target_ops lin_lwp_ops; |
| 131 | |
| 132 | /* The standard child operations. */ |
| 133 | extern struct target_ops child_ops; |
| 134 | |
| 135 | /* Since we cannot wait (in lin_lwp_wait) for the initial process and |
| 136 | any cloned processes with a single call to waitpid, we have to use |
| 137 | the WNOHANG flag and call waitpid in a loop. To optimize |
| 138 | things a bit we use `sigsuspend' to wake us up when a process has |
| 139 | something to report (it will send us a SIGCHLD if it has). To make |
| 140 | this work we have to juggle with the signal mask. We save the |
| 141 | original signal mask such that we can restore it before creating a |
| 142 | new process in order to avoid blocking certain signals in the |
| 143 | inferior. We then block SIGCHLD during the waitpid/sigsuspend |
| 144 | loop. */ |
| 145 | |
| 146 | /* Original signal mask. */ |
| 147 | static sigset_t normal_mask; |
| 148 | |
| 149 | /* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in |
| 150 | _initialize_lin_lwp. */ |
| 151 | static sigset_t suspend_mask; |
| 152 | |
| 153 | /* Signals to block to make that sigsuspend work. */ |
| 154 | static sigset_t blocked_mask; |
| 155 | \f |
| 156 | |
| 157 | /* Prototypes for local functions. */ |
| 158 | static int stop_wait_callback (struct lwp_info *lp, void *data); |
| 159 | \f |
| 160 | /* Convert wait status STATUS to a string. Used for printing debug |
| 161 | messages only. */ |
| 162 | |
| 163 | static char * |
| 164 | status_to_str (int status) |
| 165 | { |
| 166 | static char buf[64]; |
| 167 | |
| 168 | if (WIFSTOPPED (status)) |
| 169 | snprintf (buf, sizeof (buf), "%s (stopped)", |
| 170 | strsignal (WSTOPSIG (status))); |
| 171 | else if (WIFSIGNALED (status)) |
| 172 | snprintf (buf, sizeof (buf), "%s (terminated)", |
| 173 | strsignal (WSTOPSIG (status))); |
| 174 | else |
| 175 | snprintf (buf, sizeof (buf), "%d (exited)", |
| 176 | WEXITSTATUS (status)); |
| 177 | |
| 178 | return buf; |
| 179 | } |
| 180 | \f |
| 181 | /* Initialize the list of LWPs. Note that this module, contrary to |
| 182 | what GDB's generic threads layer does for its thread list, |
| 183 | re-initializes the LWP lists whenever we mourn or detach (which |
| 184 | doesn't involve mourning) the inferior. */ |
| 185 | |
| 186 | static void |
| 187 | init_lwp_list (void) |
| 188 | { |
| 189 | struct lwp_info *lp, *lpnext; |
| 190 | |
| 191 | for (lp = lwp_list; lp; lp = lpnext) |
| 192 | { |
| 193 | lpnext = lp->next; |
| 194 | xfree (lp); |
| 195 | } |
| 196 | |
| 197 | lwp_list = NULL; |
| 198 | num_lwps = 0; |
| 199 | threaded = 0; |
| 200 | } |
| 201 | |
| 202 | /* Add the LWP specified by PID to the list. If this causes the |
| 203 | number of LWPs to become larger than one, go into "threaded" mode. |
| 204 | Return a pointer to the structure describing the new LWP. */ |
| 205 | |
| 206 | static struct lwp_info * |
| 207 | add_lwp (ptid_t ptid) |
| 208 | { |
| 209 | struct lwp_info *lp; |
| 210 | |
| 211 | gdb_assert (is_lwp (ptid)); |
| 212 | |
| 213 | lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); |
| 214 | |
| 215 | memset (lp, 0, sizeof (struct lwp_info)); |
| 216 | |
| 217 | lp->ptid = ptid; |
| 218 | |
| 219 | lp->next = lwp_list; |
| 220 | lwp_list = lp; |
| 221 | if (++num_lwps > 1) |
| 222 | threaded = 1; |
| 223 | |
| 224 | return lp; |
| 225 | } |
| 226 | |
| 227 | /* Remove the LWP specified by PID from the list. */ |
| 228 | |
| 229 | static void |
| 230 | delete_lwp (ptid_t ptid) |
| 231 | { |
| 232 | struct lwp_info *lp, *lpprev; |
| 233 | |
| 234 | lpprev = NULL; |
| 235 | |
| 236 | for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) |
| 237 | if (ptid_equal (lp->ptid, ptid)) |
| 238 | break; |
| 239 | |
| 240 | if (!lp) |
| 241 | return; |
| 242 | |
| 243 | /* We don't go back to "non-threaded" mode if the number of threads |
| 244 | becomes less than two. */ |
| 245 | num_lwps--; |
| 246 | |
| 247 | if (lpprev) |
| 248 | lpprev->next = lp->next; |
| 249 | else |
| 250 | lwp_list = lp->next; |
| 251 | |
| 252 | xfree (lp); |
| 253 | } |
| 254 | |
| 255 | /* Return a pointer to the structure describing the LWP corresponding |
| 256 | to PID. If no corresponding LWP could be found, return NULL. */ |
| 257 | |
| 258 | static struct lwp_info * |
| 259 | find_lwp_pid (ptid_t ptid) |
| 260 | { |
| 261 | struct lwp_info *lp; |
| 262 | int lwp; |
| 263 | |
| 264 | if (is_lwp (ptid)) |
| 265 | lwp = GET_LWP (ptid); |
| 266 | else |
| 267 | lwp = GET_PID (ptid); |
| 268 | |
| 269 | for (lp = lwp_list; lp; lp = lp->next) |
| 270 | if (lwp == GET_LWP (lp->ptid)) |
| 271 | return lp; |
| 272 | |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | /* Call CALLBACK with its second argument set to DATA for every LWP in |
| 277 | the list. If CALLBACK returns 1 for a particular LWP, return a |
| 278 | pointer to the structure describing that LWP immediately. |
| 279 | Otherwise return NULL. */ |
| 280 | |
| 281 | struct lwp_info * |
| 282 | iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) |
| 283 | { |
| 284 | struct lwp_info *lp, *lpnext; |
| 285 | |
| 286 | for (lp = lwp_list; lp; lp = lpnext) |
| 287 | { |
| 288 | lpnext = lp->next; |
| 289 | if ((*callback) (lp, data)) |
| 290 | return lp; |
| 291 | } |
| 292 | |
| 293 | return NULL; |
| 294 | } |
| 295 | \f |
| 296 | |
| 297 | /* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP |
| 298 | layer. |
| 299 | |
| 300 | Note that this implementation is potentially redundant now that |
| 301 | default_prepare_to_proceed() has been added. |
| 302 | |
| 303 | FIXME This may not support switching threads after Ctrl-C |
| 304 | correctly. The default implementation does support this. */ |
| 305 | |
| 306 | int |
| 307 | lin_lwp_prepare_to_proceed (void) |
| 308 | { |
| 309 | if (! ptid_equal (trap_ptid, null_ptid) |
| 310 | && ! ptid_equal (inferior_ptid, trap_ptid)) |
| 311 | { |
| 312 | /* Switched over from TRAP_PID. */ |
| 313 | CORE_ADDR stop_pc = read_pc (); |
| 314 | CORE_ADDR trap_pc; |
| 315 | |
| 316 | /* Avoid switching where it wouldn't do any good, i.e. if both |
| 317 | threads are at the same breakpoint. */ |
| 318 | trap_pc = read_pc_pid (trap_ptid); |
| 319 | if (trap_pc != stop_pc && breakpoint_here_p (trap_pc)) |
| 320 | { |
| 321 | /* User hasn't deleted the breakpoint. Return non-zero, and |
| 322 | switch back to TRAP_PID. */ |
| 323 | inferior_ptid = trap_ptid; |
| 324 | |
| 325 | /* FIXME: Is this stuff really necessary? */ |
| 326 | flush_cached_frames (); |
| 327 | registers_changed (); |
| 328 | |
| 329 | return 1; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | \f |
| 336 | |
| 337 | #if 0 |
| 338 | static void |
| 339 | lin_lwp_open (char *args, int from_tty) |
| 340 | { |
| 341 | push_target (&lin_lwp_ops); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print |
| 346 | a message telling the user that a new LWP has been added to the |
| 347 | process. */ |
| 348 | |
| 349 | void |
| 350 | lin_lwp_attach_lwp (ptid_t ptid, int verbose) |
| 351 | { |
| 352 | struct lwp_info *lp; |
| 353 | |
| 354 | gdb_assert (is_lwp (ptid)); |
| 355 | |
| 356 | /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events |
| 357 | to interrupt either the ptrace() or waitpid() calls below. */ |
| 358 | if (! sigismember (&blocked_mask, SIGCHLD)) |
| 359 | { |
| 360 | sigaddset (&blocked_mask, SIGCHLD); |
| 361 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); |
| 362 | } |
| 363 | |
| 364 | if (verbose) |
| 365 | printf_filtered ("[New %s]\n", target_pid_to_str (ptid)); |
| 366 | |
| 367 | lp = find_lwp_pid (ptid); |
| 368 | if (lp == NULL) |
| 369 | lp = add_lwp (ptid); |
| 370 | |
| 371 | /* We assume that we're already attached to any LWP that has an |
| 372 | id equal to the overall process id. */ |
| 373 | if (GET_LWP (ptid) != GET_PID (ptid)) |
| 374 | { |
| 375 | pid_t pid; |
| 376 | int status; |
| 377 | |
| 378 | if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) |
| 379 | error ("Can't attach %s: %s", target_pid_to_str (ptid), |
| 380 | safe_strerror (errno)); |
| 381 | |
| 382 | pid = waitpid (GET_LWP (ptid), &status, 0); |
| 383 | if (pid == -1 && errno == ECHILD) |
| 384 | { |
| 385 | /* Try again with __WCLONE to check cloned processes. */ |
| 386 | pid = waitpid (GET_LWP (ptid), &status, __WCLONE); |
| 387 | lp->cloned = 1; |
| 388 | } |
| 389 | |
| 390 | gdb_assert (pid == GET_LWP (ptid) |
| 391 | && WIFSTOPPED (status) && WSTOPSIG (status)); |
| 392 | |
| 393 | lp->stopped = 1; |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | /* We assume that the LWP representing the original process |
| 398 | is already stopped. Mark it as stopped in the data structure |
| 399 | that the lin-lwp layer uses to keep track of threads. Note |
| 400 | that this won't have already been done since the main thread |
| 401 | will have, we assume, been stopped by an attach from a |
| 402 | different layer. */ |
| 403 | lp->stopped = 1; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | lin_lwp_attach (char *args, int from_tty) |
| 409 | { |
| 410 | struct lwp_info *lp; |
| 411 | pid_t pid; |
| 412 | int status; |
| 413 | |
| 414 | /* FIXME: We should probably accept a list of process id's, and |
| 415 | attach all of them. */ |
| 416 | child_ops.to_attach (args, from_tty); |
| 417 | |
| 418 | /* Add the initial process as the first LWP to the list. */ |
| 419 | lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid))); |
| 420 | |
| 421 | /* Make sure the initial process is stopped. The user-level threads |
| 422 | layer might want to poke around in the inferior, and that won't |
| 423 | work if things haven't stabilized yet. */ |
| 424 | pid = waitpid (GET_PID (inferior_ptid), &status, 0); |
| 425 | if (pid == -1 && errno == ECHILD) |
| 426 | { |
| 427 | warning ("%s is a cloned process", target_pid_to_str (inferior_ptid)); |
| 428 | |
| 429 | /* Try again with __WCLONE to check cloned processes. */ |
| 430 | pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE); |
| 431 | lp->cloned = 1; |
| 432 | } |
| 433 | |
| 434 | gdb_assert (pid == GET_PID (inferior_ptid) |
| 435 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP); |
| 436 | |
| 437 | lp->stopped = 1; |
| 438 | |
| 439 | /* Fake the SIGSTOP that core GDB expects. */ |
| 440 | lp->status = W_STOPCODE (SIGSTOP); |
| 441 | lp->resumed = 1; |
| 442 | } |
| 443 | |
| 444 | static int |
| 445 | detach_callback (struct lwp_info *lp, void *data) |
| 446 | { |
| 447 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); |
| 448 | |
| 449 | if (debug_lin_lwp && lp->status) |
| 450 | fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n", |
| 451 | strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid)); |
| 452 | |
| 453 | while (lp->signalled && lp->stopped) |
| 454 | { |
| 455 | if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, |
| 456 | WSTOPSIG (lp->status)) < 0) |
| 457 | error ("Can't continue %s: %s", target_pid_to_str (lp->ptid), |
| 458 | safe_strerror (errno)); |
| 459 | |
| 460 | lp->stopped = 0; |
| 461 | lp->signalled = 0; |
| 462 | lp->status = 0; |
| 463 | stop_wait_callback (lp, NULL); |
| 464 | |
| 465 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); |
| 466 | } |
| 467 | |
| 468 | /* We don't actually detach from the LWP that has an id equal to the |
| 469 | overall process id just yet. */ |
| 470 | if (GET_LWP (lp->ptid) != GET_PID (lp->ptid)) |
| 471 | { |
| 472 | if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, |
| 473 | WSTOPSIG (lp->status)) < 0) |
| 474 | error ("Can't detach %s: %s", target_pid_to_str (lp->ptid), |
| 475 | safe_strerror (errno)); |
| 476 | |
| 477 | delete_lwp (lp->ptid); |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static void |
| 484 | lin_lwp_detach (char *args, int from_tty) |
| 485 | { |
| 486 | iterate_over_lwps (detach_callback, NULL); |
| 487 | |
| 488 | /* Only the initial process should be left right now. */ |
| 489 | gdb_assert (num_lwps == 1); |
| 490 | |
| 491 | trap_ptid = null_ptid; |
| 492 | |
| 493 | /* Destroy LWP info; it's no longer valid. */ |
| 494 | init_lwp_list (); |
| 495 | |
| 496 | /* Restore the original signal mask. */ |
| 497 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
| 498 | sigemptyset (&blocked_mask); |
| 499 | |
| 500 | inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); |
| 501 | child_ops.to_detach (args, from_tty); |
| 502 | } |
| 503 | \f |
| 504 | |
| 505 | struct private_thread_info |
| 506 | { |
| 507 | int lwpid; |
| 508 | }; |
| 509 | |
| 510 | /* Return non-zero if TP corresponds to the LWP specified by DATA |
| 511 | (which is assumed to be a pointer to a `struct lwp_info'. */ |
| 512 | |
| 513 | static int |
| 514 | find_lwp_callback (struct thread_info *tp, void *data) |
| 515 | { |
| 516 | struct lwp_info *lp = data; |
| 517 | |
| 518 | if (tp->private->lwpid == GET_LWP (lp->ptid)) |
| 519 | return 1; |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* Resume LP. */ |
| 525 | |
| 526 | static int |
| 527 | resume_callback (struct lwp_info *lp, void *data) |
| 528 | { |
| 529 | if (lp->stopped && lp->status == 0) |
| 530 | { |
| 531 | struct thread_info *tp; |
| 532 | |
| 533 | #if 0 |
| 534 | /* FIXME: kettenis/2000-08-26: This should really be handled |
| 535 | properly by core GDB. */ |
| 536 | |
| 537 | tp = find_thread_pid (lp->ptid); |
| 538 | if (tp == NULL) |
| 539 | tp = iterate_over_threads (find_lwp_callback, lp); |
| 540 | gdb_assert (tp); |
| 541 | |
| 542 | /* If we were previously stepping the thread, and now continue |
| 543 | the thread we must invalidate the stepping range. However, |
| 544 | if there is a step_resume breakpoint for this thread, we must |
| 545 | preserve the stepping range to make it possible to continue |
| 546 | stepping once we hit it. */ |
| 547 | if (tp->step_range_end && tp->step_resume_breakpoint == NULL) |
| 548 | { |
| 549 | gdb_assert (lp->step); |
| 550 | tp->step_range_start = tp->step_range_end = 0; |
| 551 | } |
| 552 | #endif |
| 553 | |
| 554 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0); |
| 555 | lp->stopped = 0; |
| 556 | lp->step = 0; |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int |
| 563 | resume_clear_callback (struct lwp_info *lp, void *data) |
| 564 | { |
| 565 | lp->resumed = 0; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int |
| 570 | resume_set_callback (struct lwp_info *lp, void *data) |
| 571 | { |
| 572 | lp->resumed = 1; |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static void |
| 577 | lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo) |
| 578 | { |
| 579 | struct lwp_info *lp; |
| 580 | int resume_all; |
| 581 | |
| 582 | /* Apparently the interpretation of PID is dependent on STEP: If |
| 583 | STEP is non-zero, a specific PID means `step only this process |
| 584 | id'. But if STEP is zero, then PID means `continue *all* |
| 585 | processes, but give the signal only to this one'. */ |
| 586 | resume_all = (PIDGET (ptid) == -1) || !step; |
| 587 | |
| 588 | if (resume_all) |
| 589 | iterate_over_lwps (resume_set_callback, NULL); |
| 590 | else |
| 591 | iterate_over_lwps (resume_clear_callback, NULL); |
| 592 | |
| 593 | /* If PID is -1, it's the current inferior that should be |
| 594 | handled specially. */ |
| 595 | if (PIDGET (ptid) == -1) |
| 596 | ptid = inferior_ptid; |
| 597 | |
| 598 | lp = find_lwp_pid (ptid); |
| 599 | if (lp) |
| 600 | { |
| 601 | ptid = pid_to_ptid (GET_LWP (lp->ptid)); |
| 602 | |
| 603 | /* Remember if we're stepping. */ |
| 604 | lp->step = step; |
| 605 | |
| 606 | /* Mark this LWP as resumed. */ |
| 607 | lp->resumed = 1; |
| 608 | |
| 609 | /* If we have a pending wait status for this thread, there is no |
| 610 | point in resuming the process. */ |
| 611 | if (lp->status) |
| 612 | { |
| 613 | /* FIXME: What should we do if we are supposed to continue |
| 614 | this thread with a signal? */ |
| 615 | gdb_assert (signo == TARGET_SIGNAL_0); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | /* Mark LWP as not stopped to prevent it from being continued by |
| 620 | resume_callback. */ |
| 621 | lp->stopped = 0; |
| 622 | } |
| 623 | |
| 624 | if (resume_all) |
| 625 | iterate_over_lwps (resume_callback, NULL); |
| 626 | |
| 627 | child_resume (ptid, step, signo); |
| 628 | } |
| 629 | \f |
| 630 | |
| 631 | /* Send a SIGSTOP to LP. */ |
| 632 | |
| 633 | static int |
| 634 | stop_callback (struct lwp_info *lp, void *data) |
| 635 | { |
| 636 | if (! lp->stopped && ! lp->signalled) |
| 637 | { |
| 638 | int ret; |
| 639 | |
| 640 | ret = kill (GET_LWP (lp->ptid), SIGSTOP); |
| 641 | gdb_assert (ret == 0); |
| 642 | |
| 643 | lp->signalled = 1; |
| 644 | gdb_assert (lp->status == 0); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* Wait until LP is stopped. If DATA is non-null it is interpreted as |
| 651 | a pointer to a set of signals to be flushed immediately. */ |
| 652 | |
| 653 | static int |
| 654 | stop_wait_callback (struct lwp_info *lp, void *data) |
| 655 | { |
| 656 | sigset_t *flush_mask = data; |
| 657 | |
| 658 | if (! lp->stopped && lp->signalled) |
| 659 | { |
| 660 | pid_t pid; |
| 661 | int status; |
| 662 | |
| 663 | gdb_assert (lp->status == 0); |
| 664 | |
| 665 | pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0); |
| 666 | if (pid == -1 && errno == ECHILD) |
| 667 | /* OK, the proccess has disappeared. We'll catch the actual |
| 668 | exit event in lin_lwp_wait. */ |
| 669 | return 0; |
| 670 | |
| 671 | gdb_assert (pid == GET_LWP (lp->ptid)); |
| 672 | |
| 673 | if (WIFEXITED (status) || WIFSIGNALED (status)) |
| 674 | { |
| 675 | gdb_assert (num_lwps > 1); |
| 676 | |
| 677 | if (in_thread_list (lp->ptid)) |
| 678 | { |
| 679 | /* Core GDB cannot deal with us deleting the current |
| 680 | thread. */ |
| 681 | if (!ptid_equal (lp->ptid, inferior_ptid)) |
| 682 | delete_thread (lp->ptid); |
| 683 | printf_unfiltered ("[%s exited]\n", |
| 684 | target_pid_to_str (lp->ptid)); |
| 685 | } |
| 686 | if (debug_lin_lwp) |
| 687 | fprintf_unfiltered (gdb_stdlog, |
| 688 | "%s exited.\n", target_pid_to_str (lp->ptid)); |
| 689 | |
| 690 | delete_lwp (lp->ptid); |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | gdb_assert (WIFSTOPPED (status)); |
| 695 | |
| 696 | /* Ignore any signals in FLUSH_MASK. */ |
| 697 | if (flush_mask && sigismember (flush_mask, WSTOPSIG (status))) |
| 698 | { |
| 699 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 700 | return stop_wait_callback (lp, flush_mask); |
| 701 | } |
| 702 | |
| 703 | if (WSTOPSIG (status) != SIGSTOP) |
| 704 | { |
| 705 | if (WSTOPSIG (status) == SIGTRAP) |
| 706 | { |
| 707 | /* If a LWP other than the LWP that we're reporting an |
| 708 | event for has hit a GDB breakpoint (as opposed to |
| 709 | some random trap signal), then just arrange for it to |
| 710 | hit it again later. We don't keep the SIGTRAP status |
| 711 | and don't forward the SIGTRAP signal to the LWP. We |
| 712 | will handle the current event, eventually we will |
| 713 | resume all LWPs, and this one will get its breakpoint |
| 714 | trap again. |
| 715 | |
| 716 | If we do not do this, then we run the risk that the |
| 717 | user will delete or disable the breakpoint, but the |
| 718 | thread will have already tripped on it. */ |
| 719 | |
| 720 | /* Now resume this LWP and get the SIGSTOP event. */ |
| 721 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 722 | if (debug_lin_lwp) |
| 723 | { |
| 724 | fprintf_unfiltered (gdb_stderr, |
| 725 | "SWC: Candidate SIGTRAP event in %ld\n", |
| 726 | GET_LWP (lp->ptid)); |
| 727 | } |
| 728 | /* Hold the SIGTRAP for handling by lin_lwp_wait. */ |
| 729 | stop_wait_callback (lp, data); |
| 730 | /* If there's another event, throw it back into the queue. */ |
| 731 | if (lp->status) |
| 732 | kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); |
| 733 | /* Save the sigtrap event. */ |
| 734 | lp->status = status; |
| 735 | return 0; |
| 736 | } |
| 737 | else |
| 738 | { |
| 739 | /* The thread was stopped with a signal other than |
| 740 | SIGSTOP, and didn't accidentally trip a breakpoint. */ |
| 741 | |
| 742 | if (debug_lin_lwp) |
| 743 | { |
| 744 | fprintf_unfiltered (gdb_stderr, |
| 745 | "SWC: Pending event %d in %ld\n", |
| 746 | WSTOPSIG (status), GET_LWP (lp->ptid)); |
| 747 | } |
| 748 | /* Now resume this LWP and get the SIGSTOP event. */ |
| 749 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 750 | |
| 751 | /* Hold this event/waitstatus while we check to see if |
| 752 | there are any more (we still want to get that SIGSTOP). */ |
| 753 | stop_wait_callback (lp, data); |
| 754 | /* If the lp->status field is still empty, use it to hold |
| 755 | this event. If not, then this event must be returned |
| 756 | to the event queue of the LWP. */ |
| 757 | if (lp->status == 0) |
| 758 | lp->status = status; |
| 759 | else |
| 760 | kill (GET_LWP (lp->ptid), WSTOPSIG (status)); |
| 761 | return 0; |
| 762 | } |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | /* We caught the SIGSTOP that we intended to catch, so |
| 767 | there's no SIGSTOP pending. */ |
| 768 | lp->stopped = 1; |
| 769 | lp->signalled = 0; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* Return non-zero if LP has a wait status pending. */ |
| 777 | |
| 778 | static int |
| 779 | status_callback (struct lwp_info *lp, void *data) |
| 780 | { |
| 781 | /* Only report a pending wait status if we pretend that this has |
| 782 | indeed been resumed. */ |
| 783 | return (lp->status != 0 && lp->resumed); |
| 784 | } |
| 785 | |
| 786 | /* Return non-zero if LP isn't stopped. */ |
| 787 | |
| 788 | static int |
| 789 | running_callback (struct lwp_info *lp, void *data) |
| 790 | { |
| 791 | return (lp->stopped == 0); |
| 792 | } |
| 793 | |
| 794 | /* Count the LWP's that have had events. */ |
| 795 | |
| 796 | static int |
| 797 | count_events_callback (struct lwp_info *lp, void *data) |
| 798 | { |
| 799 | int *count = data; |
| 800 | |
| 801 | gdb_assert (count != NULL); |
| 802 | |
| 803 | /* Count only LWPs that have a SIGTRAP event pending. */ |
| 804 | if (lp->status != 0 |
| 805 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) |
| 806 | (*count)++; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | /* Select the LWP (if any) that is currently being single-stepped. */ |
| 812 | |
| 813 | static int |
| 814 | select_singlestep_lwp_callback (struct lwp_info *lp, void *data) |
| 815 | { |
| 816 | if (lp->step && lp->status != 0) |
| 817 | return 1; |
| 818 | else |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* Select the Nth LWP that has had a SIGTRAP event. */ |
| 823 | |
| 824 | static int |
| 825 | select_event_lwp_callback (struct lwp_info *lp, void *data) |
| 826 | { |
| 827 | int *selector = data; |
| 828 | |
| 829 | gdb_assert (selector != NULL); |
| 830 | |
| 831 | /* Select only LWPs that have a SIGTRAP event pending. */ |
| 832 | if (lp->status != 0 |
| 833 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) |
| 834 | if ((*selector)-- == 0) |
| 835 | return 1; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static int |
| 841 | cancel_breakpoints_callback (struct lwp_info *lp, void *data) |
| 842 | { |
| 843 | struct lwp_info *event_lp = data; |
| 844 | |
| 845 | /* Leave the LWP that has been elected to receive a SIGTRAP alone. */ |
| 846 | if (lp == event_lp) |
| 847 | return 0; |
| 848 | |
| 849 | /* If a LWP other than the LWP that we're reporting an event for has |
| 850 | hit a GDB breakpoint (as opposed to some random trap signal), |
| 851 | then just arrange for it to hit it again later. We don't keep |
| 852 | the SIGTRAP status and don't forward the SIGTRAP signal to the |
| 853 | LWP. We will handle the current event, eventually we will resume |
| 854 | all LWPs, and this one will get its breakpoint trap again. |
| 855 | |
| 856 | If we do not do this, then we run the risk that the user will |
| 857 | delete or disable the breakpoint, but the LWP will have already |
| 858 | tripped on it. */ |
| 859 | |
| 860 | if (lp->status != 0 |
| 861 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP |
| 862 | && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - |
| 863 | DECR_PC_AFTER_BREAK)) |
| 864 | { |
| 865 | if (debug_lin_lwp) |
| 866 | fprintf_unfiltered (gdb_stdlog, |
| 867 | "Push back breakpoint for LWP %ld\n", |
| 868 | GET_LWP (lp->ptid)); |
| 869 | |
| 870 | /* Back up the PC if necessary. */ |
| 871 | if (DECR_PC_AFTER_BREAK) |
| 872 | write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid); |
| 873 | |
| 874 | /* Throw away the SIGTRAP. */ |
| 875 | lp->status = 0; |
| 876 | } |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* Select one LWP out of those that have events pending. */ |
| 882 | |
| 883 | static void |
| 884 | select_event_lwp (struct lwp_info **orig_lp, int *status) |
| 885 | { |
| 886 | int num_events = 0; |
| 887 | int random_selector; |
| 888 | struct lwp_info *event_lp; |
| 889 | |
| 890 | /* Record the wait status for the origional LWP. */ |
| 891 | (*orig_lp)->status = *status; |
| 892 | |
| 893 | /* Give preference to any LWP that is being single-stepped. */ |
| 894 | event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); |
| 895 | if (event_lp != NULL) |
| 896 | { |
| 897 | if (debug_lin_lwp) |
| 898 | fprintf_unfiltered (gdb_stdlog, |
| 899 | "Select single-step LWP %ld\n", |
| 900 | GET_LWP (event_lp->ptid)); |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | /* No single-stepping LWP. Select one at random, out of those |
| 905 | which have had SIGTRAP events. */ |
| 906 | |
| 907 | /* First see how many SIGTRAP events we have. */ |
| 908 | iterate_over_lwps (count_events_callback, &num_events); |
| 909 | |
| 910 | /* Now randomly pick a LWP out of those that have had a SIGTRAP. */ |
| 911 | random_selector = (int) |
| 912 | ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); |
| 913 | |
| 914 | if (debug_lin_lwp && num_events > 1) |
| 915 | fprintf_unfiltered (gdb_stdlog, |
| 916 | "Found %d SIGTRAP events, selecting #%d\n", |
| 917 | num_events, random_selector); |
| 918 | |
| 919 | event_lp = iterate_over_lwps (select_event_lwp_callback, |
| 920 | &random_selector); |
| 921 | } |
| 922 | |
| 923 | if (event_lp != NULL) |
| 924 | { |
| 925 | /* Switch the event LWP. */ |
| 926 | *orig_lp = event_lp; |
| 927 | *status = event_lp->status; |
| 928 | } |
| 929 | |
| 930 | /* Flush the wait status for the event LWP. */ |
| 931 | (*orig_lp)->status = 0; |
| 932 | } |
| 933 | |
| 934 | /* Return non-zero if LP has been resumed. */ |
| 935 | |
| 936 | static int |
| 937 | resumed_callback (struct lwp_info *lp, void *data) |
| 938 | { |
| 939 | return lp->resumed; |
| 940 | } |
| 941 | |
| 942 | #ifdef CHILD_WAIT |
| 943 | |
| 944 | /* We need to override child_wait to support attaching to cloned |
| 945 | processes, since a normal wait (as done by the default version) |
| 946 | ignores those processes. */ |
| 947 | |
| 948 | /* Wait for child PTID to do something. Return id of the child, |
| 949 | minus_one_ptid in case of error; store status into *OURSTATUS. */ |
| 950 | |
| 951 | ptid_t |
| 952 | child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) |
| 953 | { |
| 954 | int save_errno; |
| 955 | int status; |
| 956 | pid_t pid; |
| 957 | |
| 958 | do |
| 959 | { |
| 960 | set_sigint_trap (); /* Causes SIGINT to be passed on to the |
| 961 | attached process. */ |
| 962 | set_sigio_trap (); |
| 963 | |
| 964 | pid = waitpid (GET_PID (ptid), &status, 0); |
| 965 | if (pid == -1 && errno == ECHILD) |
| 966 | /* Try again with __WCLONE to check cloned processes. */ |
| 967 | pid = waitpid (GET_PID (ptid), &status, __WCLONE); |
| 968 | save_errno = errno; |
| 969 | |
| 970 | clear_sigio_trap (); |
| 971 | clear_sigint_trap (); |
| 972 | } |
| 973 | while (pid == -1 && save_errno == EINTR); |
| 974 | |
| 975 | if (pid == -1) |
| 976 | { |
| 977 | warning ("Child process unexpectedly missing: %s", safe_strerror (errno)); |
| 978 | |
| 979 | /* Claim it exited with unknown signal. */ |
| 980 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; |
| 981 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; |
| 982 | return minus_one_ptid; |
| 983 | } |
| 984 | |
| 985 | store_waitstatus (ourstatus, status); |
| 986 | return pid_to_ptid (pid); |
| 987 | } |
| 988 | |
| 989 | #endif |
| 990 | |
| 991 | static ptid_t |
| 992 | lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus) |
| 993 | { |
| 994 | struct lwp_info *lp = NULL; |
| 995 | int options = 0; |
| 996 | int status = 0; |
| 997 | pid_t pid = PIDGET (ptid); |
| 998 | sigset_t flush_mask; |
| 999 | |
| 1000 | sigemptyset (&flush_mask); |
| 1001 | |
| 1002 | /* Make sure SIGCHLD is blocked. */ |
| 1003 | if (! sigismember (&blocked_mask, SIGCHLD)) |
| 1004 | { |
| 1005 | sigaddset (&blocked_mask, SIGCHLD); |
| 1006 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); |
| 1007 | } |
| 1008 | |
| 1009 | retry: |
| 1010 | |
| 1011 | /* Make sure there is at least one LWP that has been resumed, at |
| 1012 | least if there are any LWPs at all. */ |
| 1013 | gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL)); |
| 1014 | |
| 1015 | /* First check if there is a LWP with a wait status pending. */ |
| 1016 | if (pid == -1) |
| 1017 | { |
| 1018 | /* Any LWP that's been resumed will do. */ |
| 1019 | lp = iterate_over_lwps (status_callback, NULL); |
| 1020 | if (lp) |
| 1021 | { |
| 1022 | status = lp->status; |
| 1023 | lp->status = 0; |
| 1024 | |
| 1025 | if (debug_lin_lwp && status) |
| 1026 | fprintf_unfiltered (gdb_stdlog, |
| 1027 | "Using pending wait status %s for LWP %ld.\n", |
| 1028 | status_to_str (status), GET_LWP (lp->ptid)); |
| 1029 | } |
| 1030 | |
| 1031 | /* But if we don't fine one, we'll have to wait, and check both |
| 1032 | cloned and uncloned processes. We start with the cloned |
| 1033 | processes. */ |
| 1034 | options = __WCLONE | WNOHANG; |
| 1035 | } |
| 1036 | else if (is_lwp (ptid)) |
| 1037 | { |
| 1038 | if (debug_lin_lwp) |
| 1039 | fprintf_unfiltered (gdb_stdlog, |
| 1040 | "Waiting for specific LWP %ld.\n", |
| 1041 | GET_LWP (ptid)); |
| 1042 | |
| 1043 | /* We have a specific LWP to check. */ |
| 1044 | lp = find_lwp_pid (ptid); |
| 1045 | gdb_assert (lp); |
| 1046 | status = lp->status; |
| 1047 | lp->status = 0; |
| 1048 | |
| 1049 | if (debug_lin_lwp && status) |
| 1050 | fprintf_unfiltered (gdb_stdlog, |
| 1051 | "Using pending wait status %s for LWP %ld.\n", |
| 1052 | status_to_str (status), GET_LWP (lp->ptid)); |
| 1053 | |
| 1054 | /* If we have to wait, take into account whether PID is a cloned |
| 1055 | process or not. And we have to convert it to something that |
| 1056 | the layer beneath us can understand. */ |
| 1057 | options = lp->cloned ? __WCLONE : 0; |
| 1058 | pid = GET_LWP (ptid); |
| 1059 | } |
| 1060 | |
| 1061 | if (status && lp->signalled) |
| 1062 | { |
| 1063 | /* A pending SIGSTOP may interfere with the normal stream of |
| 1064 | events. In a typical case where interference is a problem, |
| 1065 | we have a SIGSTOP signal pending for LWP A while |
| 1066 | single-stepping it, encounter an event in LWP B, and take the |
| 1067 | pending SIGSTOP while trying to stop LWP A. After processing |
| 1068 | the event in LWP B, LWP A is continued, and we'll never see |
| 1069 | the SIGTRAP associated with the last time we were |
| 1070 | single-stepping LWP A. */ |
| 1071 | |
| 1072 | /* Resume the thread. It should halt immediately returning the |
| 1073 | pending SIGSTOP. */ |
| 1074 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
| 1075 | TARGET_SIGNAL_0); |
| 1076 | lp->stopped = 0; |
| 1077 | gdb_assert (lp->resumed); |
| 1078 | |
| 1079 | /* This should catch the pending SIGSTOP. */ |
| 1080 | stop_wait_callback (lp, NULL); |
| 1081 | } |
| 1082 | |
| 1083 | set_sigint_trap (); /* Causes SIGINT to be passed on to the |
| 1084 | attached process. */ |
| 1085 | set_sigio_trap (); |
| 1086 | |
| 1087 | while (status == 0) |
| 1088 | { |
| 1089 | pid_t lwpid; |
| 1090 | |
| 1091 | lwpid = waitpid (pid, &status, options); |
| 1092 | if (lwpid > 0) |
| 1093 | { |
| 1094 | gdb_assert (pid == -1 || lwpid == pid); |
| 1095 | |
| 1096 | lp = find_lwp_pid (pid_to_ptid (lwpid)); |
| 1097 | if (! lp) |
| 1098 | { |
| 1099 | lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); |
| 1100 | if (options & __WCLONE) |
| 1101 | lp->cloned = 1; |
| 1102 | |
| 1103 | if (threaded) |
| 1104 | { |
| 1105 | gdb_assert (WIFSTOPPED (status) |
| 1106 | && WSTOPSIG (status) == SIGSTOP); |
| 1107 | lp->signalled = 1; |
| 1108 | |
| 1109 | if (! in_thread_list (inferior_ptid)) |
| 1110 | { |
| 1111 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), |
| 1112 | GET_PID (inferior_ptid)); |
| 1113 | add_thread (inferior_ptid); |
| 1114 | } |
| 1115 | |
| 1116 | add_thread (lp->ptid); |
| 1117 | printf_unfiltered ("[New %s]\n", |
| 1118 | target_pid_to_str (lp->ptid)); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* Make sure we don't report a TARGET_WAITKIND_EXITED or |
| 1123 | TARGET_WAITKIND_SIGNALLED event if there are still LWP's |
| 1124 | left in the process. */ |
| 1125 | if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) |
| 1126 | { |
| 1127 | if (in_thread_list (lp->ptid)) |
| 1128 | { |
| 1129 | /* Core GDB cannot deal with us deleting the current |
| 1130 | thread. */ |
| 1131 | if (! ptid_equal (lp->ptid, inferior_ptid)) |
| 1132 | delete_thread (lp->ptid); |
| 1133 | printf_unfiltered ("[%s exited]\n", |
| 1134 | target_pid_to_str (lp->ptid)); |
| 1135 | } |
| 1136 | if (debug_lin_lwp) |
| 1137 | fprintf_unfiltered (gdb_stdlog, |
| 1138 | "%s exited.\n", |
| 1139 | target_pid_to_str (lp->ptid)); |
| 1140 | |
| 1141 | delete_lwp (lp->ptid); |
| 1142 | |
| 1143 | /* Make sure there is at least one thread running. */ |
| 1144 | gdb_assert (iterate_over_lwps (running_callback, NULL)); |
| 1145 | |
| 1146 | /* Discard the event. */ |
| 1147 | status = 0; |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | /* Make sure we don't report a SIGSTOP that we sent |
| 1152 | ourselves in an attempt to stop an LWP. */ |
| 1153 | if (lp->signalled && WIFSTOPPED (status) |
| 1154 | && WSTOPSIG (status) == SIGSTOP) |
| 1155 | { |
| 1156 | if (debug_lin_lwp) |
| 1157 | fprintf_unfiltered (gdb_stdlog, |
| 1158 | "Delayed SIGSTOP caught for %s.\n", |
| 1159 | target_pid_to_str (lp->ptid)); |
| 1160 | |
| 1161 | /* This is a delayed SIGSTOP. */ |
| 1162 | lp->signalled = 0; |
| 1163 | |
| 1164 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, |
| 1165 | TARGET_SIGNAL_0); |
| 1166 | lp->stopped = 0; |
| 1167 | gdb_assert (lp->resumed); |
| 1168 | |
| 1169 | /* Discard the event. */ |
| 1170 | status = 0; |
| 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | if (pid == -1) |
| 1178 | { |
| 1179 | /* Alternate between checking cloned and uncloned processes. */ |
| 1180 | options ^= __WCLONE; |
| 1181 | |
| 1182 | /* And suspend every time we have checked both. */ |
| 1183 | if (options & __WCLONE) |
| 1184 | sigsuspend (&suspend_mask); |
| 1185 | } |
| 1186 | |
| 1187 | /* We shouldn't end up here unless we want to try again. */ |
| 1188 | gdb_assert (status == 0); |
| 1189 | } |
| 1190 | |
| 1191 | clear_sigio_trap (); |
| 1192 | clear_sigint_trap (); |
| 1193 | |
| 1194 | gdb_assert (lp); |
| 1195 | |
| 1196 | /* Don't report signals that GDB isn't interested in, such as |
| 1197 | signals that are neither printed nor stopped upon. Stopping all |
| 1198 | threads can be a bit time-consuming so if we want decent |
| 1199 | performance with heavily multi-threaded programs, especially when |
| 1200 | they're using a high frequency timer, we'd better avoid it if we |
| 1201 | can. */ |
| 1202 | |
| 1203 | if (WIFSTOPPED (status)) |
| 1204 | { |
| 1205 | int signo = target_signal_from_host (WSTOPSIG (status)); |
| 1206 | |
| 1207 | if (signal_stop_state (signo) == 0 |
| 1208 | && signal_print_state (signo) == 0 |
| 1209 | && signal_pass_state (signo) == 1) |
| 1210 | { |
| 1211 | /* FIMXE: kettenis/2001-06-06: Should we resume all threads |
| 1212 | here? It is not clear we should. GDB may not expect |
| 1213 | other threads to run. On the other hand, not resuming |
| 1214 | newly attached threads may cause an unwanted delay in |
| 1215 | getting them running. */ |
| 1216 | child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo); |
| 1217 | lp->stopped = 0; |
| 1218 | status = 0; |
| 1219 | goto retry; |
| 1220 | } |
| 1221 | |
| 1222 | if (signo == TARGET_SIGNAL_INT |
| 1223 | && signal_pass_state (signo) == 0) |
| 1224 | { |
| 1225 | /* If ^C/BREAK is typed at the tty/console, SIGINT gets |
| 1226 | forwarded to the entire process group, that is, all LWP's |
| 1227 | will receive it. Since we only want to report it once, |
| 1228 | we try to flush it from all LWPs except this one. */ |
| 1229 | sigaddset (&flush_mask, SIGINT); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /* This LWP is stopped now. */ |
| 1234 | lp->stopped = 1; |
| 1235 | |
| 1236 | if (debug_lin_lwp) |
| 1237 | fprintf_unfiltered (gdb_stdlog, "Candidate event %s in LWP %ld.\n", |
| 1238 | status_to_str (status), GET_LWP (lp->ptid)); |
| 1239 | |
| 1240 | /* Now stop all other LWP's ... */ |
| 1241 | iterate_over_lwps (stop_callback, NULL); |
| 1242 | |
| 1243 | /* ... and wait until all of them have reported back that they're no |
| 1244 | longer running. */ |
| 1245 | iterate_over_lwps (stop_wait_callback, &flush_mask); |
| 1246 | |
| 1247 | /* If we're not waiting for a specific LWP, choose an event LWP from |
| 1248 | among those that have had events. Giving equal priority to all |
| 1249 | LWPs that have had events helps prevent starvation. */ |
| 1250 | if (pid == -1) |
| 1251 | select_event_lwp (&lp, &status); |
| 1252 | |
| 1253 | /* Now that we've selected our final event LWP, cancel any |
| 1254 | breakpoints in other LWPs that have hit a GDB breakpoint. See |
| 1255 | the comment in cancel_breakpoints_callback to find out why. */ |
| 1256 | iterate_over_lwps (cancel_breakpoints_callback, lp); |
| 1257 | |
| 1258 | /* If we're not running in "threaded" mode, we'll report the bare |
| 1259 | process id. */ |
| 1260 | |
| 1261 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) |
| 1262 | { |
| 1263 | trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); |
| 1264 | if (debug_lin_lwp) |
| 1265 | fprintf_unfiltered (gdb_stdlog, |
| 1266 | "LLW: trap_ptid is %ld\n", |
| 1267 | GET_LWP (trap_ptid)); |
| 1268 | } |
| 1269 | else |
| 1270 | trap_ptid = null_ptid; |
| 1271 | |
| 1272 | store_waitstatus (ourstatus, status); |
| 1273 | return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); |
| 1274 | } |
| 1275 | |
| 1276 | static int |
| 1277 | kill_callback (struct lwp_info *lp, void *data) |
| 1278 | { |
| 1279 | ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int |
| 1284 | kill_wait_callback (struct lwp_info *lp, void *data) |
| 1285 | { |
| 1286 | pid_t pid; |
| 1287 | |
| 1288 | /* We must make sure that there are no pending events (delayed |
| 1289 | SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current |
| 1290 | program doesn't interfere with any following debugging session. */ |
| 1291 | |
| 1292 | /* For cloned processes we must check both with __WCLONE and |
| 1293 | without, since the exit status of a cloned process isn't reported |
| 1294 | with __WCLONE. */ |
| 1295 | if (lp->cloned) |
| 1296 | { |
| 1297 | do |
| 1298 | { |
| 1299 | pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); |
| 1300 | } |
| 1301 | while (pid == GET_LWP (lp->ptid)); |
| 1302 | |
| 1303 | gdb_assert (pid == -1 && errno == ECHILD); |
| 1304 | } |
| 1305 | |
| 1306 | do |
| 1307 | { |
| 1308 | pid = waitpid (GET_LWP (lp->ptid), NULL, 0); |
| 1309 | } |
| 1310 | while (pid == GET_LWP (lp->ptid)); |
| 1311 | |
| 1312 | gdb_assert (pid == -1 && errno == ECHILD); |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static void |
| 1317 | lin_lwp_kill (void) |
| 1318 | { |
| 1319 | /* Kill all LWP's ... */ |
| 1320 | iterate_over_lwps (kill_callback, NULL); |
| 1321 | |
| 1322 | /* ... and wait until we've flushed all events. */ |
| 1323 | iterate_over_lwps (kill_wait_callback, NULL); |
| 1324 | |
| 1325 | target_mourn_inferior (); |
| 1326 | } |
| 1327 | |
| 1328 | static void |
| 1329 | lin_lwp_create_inferior (char *exec_file, char *allargs, char **env) |
| 1330 | { |
| 1331 | child_ops.to_create_inferior (exec_file, allargs, env); |
| 1332 | } |
| 1333 | |
| 1334 | static void |
| 1335 | lin_lwp_mourn_inferior (void) |
| 1336 | { |
| 1337 | trap_ptid = null_ptid; |
| 1338 | |
| 1339 | /* Destroy LWP info; it's no longer valid. */ |
| 1340 | init_lwp_list (); |
| 1341 | |
| 1342 | /* Restore the original signal mask. */ |
| 1343 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
| 1344 | sigemptyset (&blocked_mask); |
| 1345 | |
| 1346 | child_ops.to_mourn_inferior (); |
| 1347 | } |
| 1348 | |
| 1349 | static void |
| 1350 | lin_lwp_fetch_registers (int regno) |
| 1351 | { |
| 1352 | struct cleanup *old_chain = save_inferior_ptid (); |
| 1353 | |
| 1354 | if (is_lwp (inferior_ptid)) |
| 1355 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); |
| 1356 | |
| 1357 | fetch_inferior_registers (regno); |
| 1358 | |
| 1359 | do_cleanups (old_chain); |
| 1360 | } |
| 1361 | |
| 1362 | static void |
| 1363 | lin_lwp_store_registers (int regno) |
| 1364 | { |
| 1365 | struct cleanup *old_chain = save_inferior_ptid (); |
| 1366 | |
| 1367 | if (is_lwp (inferior_ptid)) |
| 1368 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); |
| 1369 | |
| 1370 | store_inferior_registers (regno); |
| 1371 | |
| 1372 | do_cleanups (old_chain); |
| 1373 | } |
| 1374 | |
| 1375 | static int |
| 1376 | lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
| 1377 | struct mem_attrib *attrib, |
| 1378 | struct target_ops *target) |
| 1379 | { |
| 1380 | struct cleanup *old_chain = save_inferior_ptid (); |
| 1381 | int xfer; |
| 1382 | |
| 1383 | if (is_lwp (inferior_ptid)) |
| 1384 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); |
| 1385 | |
| 1386 | xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target); |
| 1387 | |
| 1388 | do_cleanups (old_chain); |
| 1389 | return xfer; |
| 1390 | } |
| 1391 | |
| 1392 | static int |
| 1393 | lin_lwp_thread_alive (ptid_t ptid) |
| 1394 | { |
| 1395 | gdb_assert (is_lwp (ptid)); |
| 1396 | |
| 1397 | errno = 0; |
| 1398 | ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); |
| 1399 | if (errno) |
| 1400 | return 0; |
| 1401 | |
| 1402 | return 1; |
| 1403 | } |
| 1404 | |
| 1405 | static char * |
| 1406 | lin_lwp_pid_to_str (ptid_t ptid) |
| 1407 | { |
| 1408 | static char buf[64]; |
| 1409 | |
| 1410 | if (is_lwp (ptid)) |
| 1411 | { |
| 1412 | snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); |
| 1413 | return buf; |
| 1414 | } |
| 1415 | |
| 1416 | return normal_pid_to_str (ptid); |
| 1417 | } |
| 1418 | |
| 1419 | static void |
| 1420 | init_lin_lwp_ops (void) |
| 1421 | { |
| 1422 | #if 0 |
| 1423 | lin_lwp_ops.to_open = lin_lwp_open; |
| 1424 | #endif |
| 1425 | lin_lwp_ops.to_shortname = "lwp-layer"; |
| 1426 | lin_lwp_ops.to_longname = "lwp-layer"; |
| 1427 | lin_lwp_ops.to_doc = "Low level threads support (LWP layer)"; |
| 1428 | lin_lwp_ops.to_attach = lin_lwp_attach; |
| 1429 | lin_lwp_ops.to_detach = lin_lwp_detach; |
| 1430 | lin_lwp_ops.to_resume = lin_lwp_resume; |
| 1431 | lin_lwp_ops.to_wait = lin_lwp_wait; |
| 1432 | lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers; |
| 1433 | lin_lwp_ops.to_store_registers = lin_lwp_store_registers; |
| 1434 | lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory; |
| 1435 | lin_lwp_ops.to_kill = lin_lwp_kill; |
| 1436 | lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior; |
| 1437 | lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior; |
| 1438 | lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive; |
| 1439 | lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str; |
| 1440 | lin_lwp_ops.to_stratum = thread_stratum; |
| 1441 | lin_lwp_ops.to_has_thread_control = tc_schedlock; |
| 1442 | lin_lwp_ops.to_magic = OPS_MAGIC; |
| 1443 | } |
| 1444 | |
| 1445 | static void |
| 1446 | sigchld_handler (int signo) |
| 1447 | { |
| 1448 | /* Do nothing. The only reason for this handler is that it allows |
| 1449 | us to use sigsuspend in lin_lwp_wait above to wait for the |
| 1450 | arrival of a SIGCHLD. */ |
| 1451 | } |
| 1452 | |
| 1453 | void |
| 1454 | _initialize_lin_lwp (void) |
| 1455 | { |
| 1456 | struct sigaction action; |
| 1457 | |
| 1458 | extern void thread_db_init (struct target_ops *); |
| 1459 | |
| 1460 | init_lin_lwp_ops (); |
| 1461 | add_target (&lin_lwp_ops); |
| 1462 | thread_db_init (&lin_lwp_ops); |
| 1463 | |
| 1464 | /* Save the original signal mask. */ |
| 1465 | sigprocmask (SIG_SETMASK, NULL, &normal_mask); |
| 1466 | |
| 1467 | action.sa_handler = sigchld_handler; |
| 1468 | sigemptyset (&action.sa_mask); |
| 1469 | action.sa_flags = 0; |
| 1470 | sigaction (SIGCHLD, &action, NULL); |
| 1471 | |
| 1472 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
| 1473 | sigprocmask (SIG_SETMASK, NULL, &suspend_mask); |
| 1474 | sigdelset (&suspend_mask, SIGCHLD); |
| 1475 | |
| 1476 | sigemptyset (&blocked_mask); |
| 1477 | |
| 1478 | add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger, |
| 1479 | (char *) &debug_lin_lwp, |
| 1480 | "Set debugging of GNU/Linux lwp module.\n\ |
| 1481 | Enables printf debugging output.\n", |
| 1482 | &setdebuglist), |
| 1483 | &showdebuglist); |
| 1484 | } |
| 1485 | \f |
| 1486 | |
| 1487 | /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to |
| 1488 | the GNU/Linux Threads library and therefore doesn't really belong |
| 1489 | here. */ |
| 1490 | |
| 1491 | /* Read variable NAME in the target and return its value if found. |
| 1492 | Otherwise return zero. It is assumed that the type of the variable |
| 1493 | is `int'. */ |
| 1494 | |
| 1495 | static int |
| 1496 | get_signo (const char *name) |
| 1497 | { |
| 1498 | struct minimal_symbol *ms; |
| 1499 | int signo; |
| 1500 | |
| 1501 | ms = lookup_minimal_symbol (name, NULL, NULL); |
| 1502 | if (ms == NULL) |
| 1503 | return 0; |
| 1504 | |
| 1505 | if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo, |
| 1506 | sizeof (signo)) != 0) |
| 1507 | return 0; |
| 1508 | |
| 1509 | return signo; |
| 1510 | } |
| 1511 | |
| 1512 | /* Return the set of signals used by the threads library in *SET. */ |
| 1513 | |
| 1514 | void |
| 1515 | lin_thread_get_thread_signals (sigset_t *set) |
| 1516 | { |
| 1517 | struct sigaction action; |
| 1518 | int restart, cancel; |
| 1519 | |
| 1520 | sigemptyset (set); |
| 1521 | |
| 1522 | restart = get_signo ("__pthread_sig_restart"); |
| 1523 | if (restart == 0) |
| 1524 | return; |
| 1525 | |
| 1526 | cancel = get_signo ("__pthread_sig_cancel"); |
| 1527 | if (cancel == 0) |
| 1528 | return; |
| 1529 | |
| 1530 | sigaddset (set, restart); |
| 1531 | sigaddset (set, cancel); |
| 1532 | |
| 1533 | /* The GNU/Linux Threads library makes terminating threads send a |
| 1534 | special "cancel" signal instead of SIGCHLD. Make sure we catch |
| 1535 | those (to prevent them from terminating GDB itself, which is |
| 1536 | likely to be their default action) and treat them the same way as |
| 1537 | SIGCHLD. */ |
| 1538 | |
| 1539 | action.sa_handler = sigchld_handler; |
| 1540 | sigemptyset (&action.sa_mask); |
| 1541 | action.sa_flags = 0; |
| 1542 | sigaction (cancel, &action, NULL); |
| 1543 | |
| 1544 | /* We block the "cancel" signal throughout this code ... */ |
| 1545 | sigaddset (&blocked_mask, cancel); |
| 1546 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); |
| 1547 | |
| 1548 | /* ... except during a sigsuspend. */ |
| 1549 | sigdelset (&suspend_mask, cancel); |
| 1550 | } |