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