| 1 | /* GNU/Linux native-dependent code common to multiple platforms. |
| 2 | |
| 3 | Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "inferior.h" |
| 23 | #include "target.h" |
| 24 | #include "gdb_string.h" |
| 25 | #include "gdb_wait.h" |
| 26 | #include "gdb_assert.h" |
| 27 | #ifdef HAVE_TKILL_SYSCALL |
| 28 | #include <unistd.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #endif |
| 31 | #include <sys/ptrace.h> |
| 32 | #include "linux-nat.h" |
| 33 | #include "linux-fork.h" |
| 34 | #include "gdbthread.h" |
| 35 | #include "gdbcmd.h" |
| 36 | #include "regcache.h" |
| 37 | #include "regset.h" |
| 38 | #include "inf-ptrace.h" |
| 39 | #include "auxv.h" |
| 40 | #include <sys/param.h> /* for MAXPATHLEN */ |
| 41 | #include <sys/procfs.h> /* for elf_gregset etc. */ |
| 42 | #include "elf-bfd.h" /* for elfcore_write_* */ |
| 43 | #include "gregset.h" /* for gregset */ |
| 44 | #include "gdbcore.h" /* for get_exec_file */ |
| 45 | #include <ctype.h> /* for isdigit */ |
| 46 | #include "gdbthread.h" /* for struct thread_info etc. */ |
| 47 | #include "gdb_stat.h" /* for struct stat */ |
| 48 | #include <fcntl.h> /* for O_RDONLY */ |
| 49 | #include "inf-loop.h" |
| 50 | #include "event-loop.h" |
| 51 | #include "event-top.h" |
| 52 | |
| 53 | #ifndef O_LARGEFILE |
| 54 | #define O_LARGEFILE 0 |
| 55 | #endif |
| 56 | |
| 57 | /* If the system headers did not provide the constants, hard-code the normal |
| 58 | values. */ |
| 59 | #ifndef PTRACE_EVENT_FORK |
| 60 | |
| 61 | #define PTRACE_SETOPTIONS 0x4200 |
| 62 | #define PTRACE_GETEVENTMSG 0x4201 |
| 63 | |
| 64 | /* options set using PTRACE_SETOPTIONS */ |
| 65 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| 66 | #define PTRACE_O_TRACEFORK 0x00000002 |
| 67 | #define PTRACE_O_TRACEVFORK 0x00000004 |
| 68 | #define PTRACE_O_TRACECLONE 0x00000008 |
| 69 | #define PTRACE_O_TRACEEXEC 0x00000010 |
| 70 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
| 71 | #define PTRACE_O_TRACEEXIT 0x00000040 |
| 72 | |
| 73 | /* Wait extended result codes for the above trace options. */ |
| 74 | #define PTRACE_EVENT_FORK 1 |
| 75 | #define PTRACE_EVENT_VFORK 2 |
| 76 | #define PTRACE_EVENT_CLONE 3 |
| 77 | #define PTRACE_EVENT_EXEC 4 |
| 78 | #define PTRACE_EVENT_VFORK_DONE 5 |
| 79 | #define PTRACE_EVENT_EXIT 6 |
| 80 | |
| 81 | #endif /* PTRACE_EVENT_FORK */ |
| 82 | |
| 83 | /* We can't always assume that this flag is available, but all systems |
| 84 | with the ptrace event handlers also have __WALL, so it's safe to use |
| 85 | here. */ |
| 86 | #ifndef __WALL |
| 87 | #define __WALL 0x40000000 /* Wait for any child. */ |
| 88 | #endif |
| 89 | |
| 90 | #ifndef PTRACE_GETSIGINFO |
| 91 | #define PTRACE_GETSIGINFO 0x4202 |
| 92 | #endif |
| 93 | |
| 94 | /* The single-threaded native GNU/Linux target_ops. We save a pointer for |
| 95 | the use of the multi-threaded target. */ |
| 96 | static struct target_ops *linux_ops; |
| 97 | static struct target_ops linux_ops_saved; |
| 98 | |
| 99 | /* The method to call, if any, when a new thread is attached. */ |
| 100 | static void (*linux_nat_new_thread) (ptid_t); |
| 101 | |
| 102 | /* The saved to_xfer_partial method, inherited from inf-ptrace.c. |
| 103 | Called by our to_xfer_partial. */ |
| 104 | static LONGEST (*super_xfer_partial) (struct target_ops *, |
| 105 | enum target_object, |
| 106 | const char *, gdb_byte *, |
| 107 | const gdb_byte *, |
| 108 | ULONGEST, LONGEST); |
| 109 | |
| 110 | static int debug_linux_nat; |
| 111 | static void |
| 112 | show_debug_linux_nat (struct ui_file *file, int from_tty, |
| 113 | struct cmd_list_element *c, const char *value) |
| 114 | { |
| 115 | fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"), |
| 116 | value); |
| 117 | } |
| 118 | |
| 119 | static int debug_linux_nat_async = 0; |
| 120 | static void |
| 121 | show_debug_linux_nat_async (struct ui_file *file, int from_tty, |
| 122 | struct cmd_list_element *c, const char *value) |
| 123 | { |
| 124 | fprintf_filtered (file, _("Debugging of GNU/Linux async lwp module is %s.\n"), |
| 125 | value); |
| 126 | } |
| 127 | |
| 128 | static int linux_parent_pid; |
| 129 | |
| 130 | struct simple_pid_list |
| 131 | { |
| 132 | int pid; |
| 133 | int status; |
| 134 | struct simple_pid_list *next; |
| 135 | }; |
| 136 | struct simple_pid_list *stopped_pids; |
| 137 | |
| 138 | /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK |
| 139 | can not be used, 1 if it can. */ |
| 140 | |
| 141 | static int linux_supports_tracefork_flag = -1; |
| 142 | |
| 143 | /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have |
| 144 | PTRACE_O_TRACEVFORKDONE. */ |
| 145 | |
| 146 | static int linux_supports_tracevforkdone_flag = -1; |
| 147 | |
| 148 | /* Async mode support */ |
| 149 | |
| 150 | /* To listen to target events asynchronously, we install a SIGCHLD |
| 151 | handler whose duty is to call waitpid (-1, ..., WNOHANG) to get all |
| 152 | the pending events into a pipe. Whenever we're ready to handle |
| 153 | events asynchronously, this pipe is registered as the waitable file |
| 154 | handle in the event loop. When we get to entry target points |
| 155 | coming out of the common code (target_wait, target_resume, ...), |
| 156 | that are going to call waitpid, we block SIGCHLD signals, and |
| 157 | remove all the events placed in the pipe into a local queue. All |
| 158 | the subsequent calls to my_waitpid (a waitpid wrapper) check this |
| 159 | local queue first. */ |
| 160 | |
| 161 | /* True if async mode is currently on. */ |
| 162 | static int linux_nat_async_enabled; |
| 163 | |
| 164 | /* Zero if the async mode, although enabled, is masked, which means |
| 165 | linux_nat_wait should behave as if async mode was off. */ |
| 166 | static int linux_nat_async_mask_value = 1; |
| 167 | |
| 168 | /* The read/write ends of the pipe registered as waitable file in the |
| 169 | event loop. */ |
| 170 | static int linux_nat_event_pipe[2] = { -1, -1 }; |
| 171 | |
| 172 | /* Number of queued events in the pipe. */ |
| 173 | static volatile int linux_nat_num_queued_events; |
| 174 | |
| 175 | /* If async mode is on, true if we're listening for events; false if |
| 176 | target events are blocked. */ |
| 177 | static int linux_nat_async_events_enabled; |
| 178 | |
| 179 | static int linux_nat_async_events (int enable); |
| 180 | static void pipe_to_local_event_queue (void); |
| 181 | static void local_event_queue_to_pipe (void); |
| 182 | static void linux_nat_event_pipe_push (int pid, int status, int options); |
| 183 | static int linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options); |
| 184 | static void linux_nat_set_async_mode (int on); |
| 185 | static void linux_nat_async (void (*callback) |
| 186 | (enum inferior_event_type event_type, void *context), |
| 187 | void *context); |
| 188 | static int linux_nat_async_mask (int mask); |
| 189 | |
| 190 | /* Captures the result of a successful waitpid call, along with the |
| 191 | options used in that call. */ |
| 192 | struct waitpid_result |
| 193 | { |
| 194 | int pid; |
| 195 | int status; |
| 196 | int options; |
| 197 | struct waitpid_result *next; |
| 198 | }; |
| 199 | |
| 200 | /* A singly-linked list of the results of the waitpid calls performed |
| 201 | in the async SIGCHLD handler. */ |
| 202 | static struct waitpid_result *waitpid_queue = NULL; |
| 203 | |
| 204 | static int |
| 205 | queued_waitpid (int pid, int *status, int flags) |
| 206 | { |
| 207 | struct waitpid_result *msg = waitpid_queue, *prev = NULL; |
| 208 | |
| 209 | if (debug_linux_nat_async) |
| 210 | fprintf_unfiltered (gdb_stdlog, |
| 211 | "\ |
| 212 | QWPID: linux_nat_async_events_enabled(%d), linux_nat_num_queued_events(%d)\n", |
| 213 | linux_nat_async_events_enabled, |
| 214 | linux_nat_num_queued_events); |
| 215 | |
| 216 | if (flags & __WALL) |
| 217 | { |
| 218 | for (; msg; prev = msg, msg = msg->next) |
| 219 | if (pid == -1 || pid == msg->pid) |
| 220 | break; |
| 221 | } |
| 222 | else if (flags & __WCLONE) |
| 223 | { |
| 224 | for (; msg; prev = msg, msg = msg->next) |
| 225 | if (msg->options & __WCLONE |
| 226 | && (pid == -1 || pid == msg->pid)) |
| 227 | break; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | for (; msg; prev = msg, msg = msg->next) |
| 232 | if ((msg->options & __WCLONE) == 0 |
| 233 | && (pid == -1 || pid == msg->pid)) |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | if (msg) |
| 238 | { |
| 239 | int pid; |
| 240 | |
| 241 | if (prev) |
| 242 | prev->next = msg->next; |
| 243 | else |
| 244 | waitpid_queue = msg->next; |
| 245 | |
| 246 | msg->next = NULL; |
| 247 | if (status) |
| 248 | *status = msg->status; |
| 249 | pid = msg->pid; |
| 250 | |
| 251 | if (debug_linux_nat_async) |
| 252 | fprintf_unfiltered (gdb_stdlog, "QWPID: pid(%d), status(%x)\n", |
| 253 | pid, msg->status); |
| 254 | xfree (msg); |
| 255 | |
| 256 | return pid; |
| 257 | } |
| 258 | |
| 259 | if (debug_linux_nat_async) |
| 260 | fprintf_unfiltered (gdb_stdlog, "QWPID: miss\n"); |
| 261 | |
| 262 | if (status) |
| 263 | *status = 0; |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | static void |
| 268 | push_waitpid (int pid, int status, int options) |
| 269 | { |
| 270 | struct waitpid_result *event, *new_event; |
| 271 | |
| 272 | new_event = xmalloc (sizeof (*new_event)); |
| 273 | new_event->pid = pid; |
| 274 | new_event->status = status; |
| 275 | new_event->options = options; |
| 276 | new_event->next = NULL; |
| 277 | |
| 278 | if (waitpid_queue) |
| 279 | { |
| 280 | for (event = waitpid_queue; |
| 281 | event && event->next; |
| 282 | event = event->next) |
| 283 | ; |
| 284 | |
| 285 | event->next = new_event; |
| 286 | } |
| 287 | else |
| 288 | waitpid_queue = new_event; |
| 289 | } |
| 290 | |
| 291 | /* Drain all queued events of PID. If PID is -1, the effect is of |
| 292 | draining all events. */ |
| 293 | static void |
| 294 | drain_queued_events (int pid) |
| 295 | { |
| 296 | while (queued_waitpid (pid, NULL, __WALL) != -1) |
| 297 | ; |
| 298 | } |
| 299 | |
| 300 | \f |
| 301 | /* Trivial list manipulation functions to keep track of a list of |
| 302 | new stopped processes. */ |
| 303 | static void |
| 304 | add_to_pid_list (struct simple_pid_list **listp, int pid, int status) |
| 305 | { |
| 306 | struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list)); |
| 307 | new_pid->pid = pid; |
| 308 | new_pid->status = status; |
| 309 | new_pid->next = *listp; |
| 310 | *listp = new_pid; |
| 311 | } |
| 312 | |
| 313 | static int |
| 314 | pull_pid_from_list (struct simple_pid_list **listp, int pid, int *status) |
| 315 | { |
| 316 | struct simple_pid_list **p; |
| 317 | |
| 318 | for (p = listp; *p != NULL; p = &(*p)->next) |
| 319 | if ((*p)->pid == pid) |
| 320 | { |
| 321 | struct simple_pid_list *next = (*p)->next; |
| 322 | *status = (*p)->status; |
| 323 | xfree (*p); |
| 324 | *p = next; |
| 325 | return 1; |
| 326 | } |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | linux_record_stopped_pid (int pid, int status) |
| 332 | { |
| 333 | add_to_pid_list (&stopped_pids, pid, status); |
| 334 | } |
| 335 | |
| 336 | \f |
| 337 | /* A helper function for linux_test_for_tracefork, called after fork (). */ |
| 338 | |
| 339 | static void |
| 340 | linux_tracefork_child (void) |
| 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | ptrace (PTRACE_TRACEME, 0, 0, 0); |
| 345 | kill (getpid (), SIGSTOP); |
| 346 | fork (); |
| 347 | _exit (0); |
| 348 | } |
| 349 | |
| 350 | /* Wrapper function for waitpid which handles EINTR, and checks for |
| 351 | locally queued events. */ |
| 352 | |
| 353 | static int |
| 354 | my_waitpid (int pid, int *status, int flags) |
| 355 | { |
| 356 | int ret; |
| 357 | |
| 358 | /* There should be no concurrent calls to waitpid. */ |
| 359 | gdb_assert (!linux_nat_async_events_enabled); |
| 360 | |
| 361 | ret = queued_waitpid (pid, status, flags); |
| 362 | if (ret != -1) |
| 363 | return ret; |
| 364 | |
| 365 | do |
| 366 | { |
| 367 | ret = waitpid (pid, status, flags); |
| 368 | } |
| 369 | while (ret == -1 && errno == EINTR); |
| 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. |
| 375 | |
| 376 | First, we try to enable fork tracing on ORIGINAL_PID. If this fails, |
| 377 | we know that the feature is not available. This may change the tracing |
| 378 | options for ORIGINAL_PID, but we'll be setting them shortly anyway. |
| 379 | |
| 380 | However, if it succeeds, we don't know for sure that the feature is |
| 381 | available; old versions of PTRACE_SETOPTIONS ignored unknown options. We |
| 382 | create a child process, attach to it, use PTRACE_SETOPTIONS to enable |
| 383 | fork tracing, and let it fork. If the process exits, we assume that we |
| 384 | can't use TRACEFORK; if we get the fork notification, and we can extract |
| 385 | the new child's PID, then we assume that we can. */ |
| 386 | |
| 387 | static void |
| 388 | linux_test_for_tracefork (int original_pid) |
| 389 | { |
| 390 | int child_pid, ret, status; |
| 391 | long second_pid; |
| 392 | |
| 393 | linux_supports_tracefork_flag = 0; |
| 394 | linux_supports_tracevforkdone_flag = 0; |
| 395 | |
| 396 | ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK); |
| 397 | if (ret != 0) |
| 398 | return; |
| 399 | |
| 400 | child_pid = fork (); |
| 401 | if (child_pid == -1) |
| 402 | perror_with_name (("fork")); |
| 403 | |
| 404 | if (child_pid == 0) |
| 405 | linux_tracefork_child (); |
| 406 | |
| 407 | ret = my_waitpid (child_pid, &status, 0); |
| 408 | if (ret == -1) |
| 409 | perror_with_name (("waitpid")); |
| 410 | else if (ret != child_pid) |
| 411 | error (_("linux_test_for_tracefork: waitpid: unexpected result %d."), ret); |
| 412 | if (! WIFSTOPPED (status)) |
| 413 | error (_("linux_test_for_tracefork: waitpid: unexpected status %d."), status); |
| 414 | |
| 415 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); |
| 416 | if (ret != 0) |
| 417 | { |
| 418 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); |
| 419 | if (ret != 0) |
| 420 | { |
| 421 | warning (_("linux_test_for_tracefork: failed to kill child")); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | ret = my_waitpid (child_pid, &status, 0); |
| 426 | if (ret != child_pid) |
| 427 | warning (_("linux_test_for_tracefork: failed to wait for killed child")); |
| 428 | else if (!WIFSIGNALED (status)) |
| 429 | warning (_("linux_test_for_tracefork: unexpected wait status 0x%x from " |
| 430 | "killed child"), status); |
| 431 | |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* Check whether PTRACE_O_TRACEVFORKDONE is available. */ |
| 436 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, |
| 437 | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE); |
| 438 | linux_supports_tracevforkdone_flag = (ret == 0); |
| 439 | |
| 440 | ret = ptrace (PTRACE_CONT, child_pid, 0, 0); |
| 441 | if (ret != 0) |
| 442 | warning (_("linux_test_for_tracefork: failed to resume child")); |
| 443 | |
| 444 | ret = my_waitpid (child_pid, &status, 0); |
| 445 | |
| 446 | if (ret == child_pid && WIFSTOPPED (status) |
| 447 | && status >> 16 == PTRACE_EVENT_FORK) |
| 448 | { |
| 449 | second_pid = 0; |
| 450 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); |
| 451 | if (ret == 0 && second_pid != 0) |
| 452 | { |
| 453 | int second_status; |
| 454 | |
| 455 | linux_supports_tracefork_flag = 1; |
| 456 | my_waitpid (second_pid, &second_status, 0); |
| 457 | ret = ptrace (PTRACE_KILL, second_pid, 0, 0); |
| 458 | if (ret != 0) |
| 459 | warning (_("linux_test_for_tracefork: failed to kill second child")); |
| 460 | my_waitpid (second_pid, &status, 0); |
| 461 | } |
| 462 | } |
| 463 | else |
| 464 | warning (_("linux_test_for_tracefork: unexpected result from waitpid " |
| 465 | "(%d, status 0x%x)"), ret, status); |
| 466 | |
| 467 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); |
| 468 | if (ret != 0) |
| 469 | warning (_("linux_test_for_tracefork: failed to kill child")); |
| 470 | my_waitpid (child_pid, &status, 0); |
| 471 | } |
| 472 | |
| 473 | /* Return non-zero iff we have tracefork functionality available. |
| 474 | This function also sets linux_supports_tracefork_flag. */ |
| 475 | |
| 476 | static int |
| 477 | linux_supports_tracefork (int pid) |
| 478 | { |
| 479 | if (linux_supports_tracefork_flag == -1) |
| 480 | linux_test_for_tracefork (pid); |
| 481 | return linux_supports_tracefork_flag; |
| 482 | } |
| 483 | |
| 484 | static int |
| 485 | linux_supports_tracevforkdone (int pid) |
| 486 | { |
| 487 | if (linux_supports_tracefork_flag == -1) |
| 488 | linux_test_for_tracefork (pid); |
| 489 | return linux_supports_tracevforkdone_flag; |
| 490 | } |
| 491 | |
| 492 | \f |
| 493 | void |
| 494 | linux_enable_event_reporting (ptid_t ptid) |
| 495 | { |
| 496 | int pid = ptid_get_lwp (ptid); |
| 497 | int options; |
| 498 | |
| 499 | if (pid == 0) |
| 500 | pid = ptid_get_pid (ptid); |
| 501 | |
| 502 | if (! linux_supports_tracefork (pid)) |
| 503 | return; |
| 504 | |
| 505 | options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC |
| 506 | | PTRACE_O_TRACECLONE; |
| 507 | if (linux_supports_tracevforkdone (pid)) |
| 508 | options |= PTRACE_O_TRACEVFORKDONE; |
| 509 | |
| 510 | /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support |
| 511 | read-only process state. */ |
| 512 | |
| 513 | ptrace (PTRACE_SETOPTIONS, pid, 0, options); |
| 514 | } |
| 515 | |
| 516 | static void |
| 517 | linux_child_post_attach (int pid) |
| 518 | { |
| 519 | linux_enable_event_reporting (pid_to_ptid (pid)); |
| 520 | check_for_thread_db (); |
| 521 | } |
| 522 | |
| 523 | static void |
| 524 | linux_child_post_startup_inferior (ptid_t ptid) |
| 525 | { |
| 526 | linux_enable_event_reporting (ptid); |
| 527 | check_for_thread_db (); |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | linux_child_follow_fork (struct target_ops *ops, int follow_child) |
| 532 | { |
| 533 | ptid_t last_ptid; |
| 534 | struct target_waitstatus last_status; |
| 535 | int has_vforked; |
| 536 | int parent_pid, child_pid; |
| 537 | |
| 538 | if (target_can_async_p ()) |
| 539 | target_async (NULL, 0); |
| 540 | |
| 541 | get_last_target_status (&last_ptid, &last_status); |
| 542 | has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED); |
| 543 | parent_pid = ptid_get_lwp (last_ptid); |
| 544 | if (parent_pid == 0) |
| 545 | parent_pid = ptid_get_pid (last_ptid); |
| 546 | child_pid = last_status.value.related_pid; |
| 547 | |
| 548 | if (! follow_child) |
| 549 | { |
| 550 | /* We're already attached to the parent, by default. */ |
| 551 | |
| 552 | /* Before detaching from the child, remove all breakpoints from |
| 553 | it. (This won't actually modify the breakpoint list, but will |
| 554 | physically remove the breakpoints from the child.) */ |
| 555 | /* If we vforked this will remove the breakpoints from the parent |
| 556 | also, but they'll be reinserted below. */ |
| 557 | detach_breakpoints (child_pid); |
| 558 | |
| 559 | /* Detach new forked process? */ |
| 560 | if (detach_fork) |
| 561 | { |
| 562 | if (info_verbose || debug_linux_nat) |
| 563 | { |
| 564 | target_terminal_ours (); |
| 565 | fprintf_filtered (gdb_stdlog, |
| 566 | "Detaching after fork from child process %d.\n", |
| 567 | child_pid); |
| 568 | } |
| 569 | |
| 570 | ptrace (PTRACE_DETACH, child_pid, 0, 0); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | struct fork_info *fp; |
| 575 | /* Retain child fork in ptrace (stopped) state. */ |
| 576 | fp = find_fork_pid (child_pid); |
| 577 | if (!fp) |
| 578 | fp = add_fork (child_pid); |
| 579 | fork_save_infrun_state (fp, 0); |
| 580 | } |
| 581 | |
| 582 | if (has_vforked) |
| 583 | { |
| 584 | gdb_assert (linux_supports_tracefork_flag >= 0); |
| 585 | if (linux_supports_tracevforkdone (0)) |
| 586 | { |
| 587 | int status; |
| 588 | |
| 589 | ptrace (PTRACE_CONT, parent_pid, 0, 0); |
| 590 | my_waitpid (parent_pid, &status, __WALL); |
| 591 | if ((status >> 16) != PTRACE_EVENT_VFORK_DONE) |
| 592 | warning (_("Unexpected waitpid result %06x when waiting for " |
| 593 | "vfork-done"), status); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | /* We can't insert breakpoints until the child has |
| 598 | finished with the shared memory region. We need to |
| 599 | wait until that happens. Ideal would be to just |
| 600 | call: |
| 601 | - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0); |
| 602 | - waitpid (parent_pid, &status, __WALL); |
| 603 | However, most architectures can't handle a syscall |
| 604 | being traced on the way out if it wasn't traced on |
| 605 | the way in. |
| 606 | |
| 607 | We might also think to loop, continuing the child |
| 608 | until it exits or gets a SIGTRAP. One problem is |
| 609 | that the child might call ptrace with PTRACE_TRACEME. |
| 610 | |
| 611 | There's no simple and reliable way to figure out when |
| 612 | the vforked child will be done with its copy of the |
| 613 | shared memory. We could step it out of the syscall, |
| 614 | two instructions, let it go, and then single-step the |
| 615 | parent once. When we have hardware single-step, this |
| 616 | would work; with software single-step it could still |
| 617 | be made to work but we'd have to be able to insert |
| 618 | single-step breakpoints in the child, and we'd have |
| 619 | to insert -just- the single-step breakpoint in the |
| 620 | parent. Very awkward. |
| 621 | |
| 622 | In the end, the best we can do is to make sure it |
| 623 | runs for a little while. Hopefully it will be out of |
| 624 | range of any breakpoints we reinsert. Usually this |
| 625 | is only the single-step breakpoint at vfork's return |
| 626 | point. */ |
| 627 | |
| 628 | usleep (10000); |
| 629 | } |
| 630 | |
| 631 | /* Since we vforked, breakpoints were removed in the parent |
| 632 | too. Put them back. */ |
| 633 | reattach_breakpoints (parent_pid); |
| 634 | } |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | char child_pid_spelling[40]; |
| 639 | |
| 640 | /* Needed to keep the breakpoint lists in sync. */ |
| 641 | if (! has_vforked) |
| 642 | detach_breakpoints (child_pid); |
| 643 | |
| 644 | /* Before detaching from the parent, remove all breakpoints from it. */ |
| 645 | remove_breakpoints (); |
| 646 | |
| 647 | if (info_verbose || debug_linux_nat) |
| 648 | { |
| 649 | target_terminal_ours (); |
| 650 | fprintf_filtered (gdb_stdlog, |
| 651 | "Attaching after fork to child process %d.\n", |
| 652 | child_pid); |
| 653 | } |
| 654 | |
| 655 | /* If we're vforking, we may want to hold on to the parent until |
| 656 | the child exits or execs. At exec time we can remove the old |
| 657 | breakpoints from the parent and detach it; at exit time we |
| 658 | could do the same (or even, sneakily, resume debugging it - the |
| 659 | child's exec has failed, or something similar). |
| 660 | |
| 661 | This doesn't clean up "properly", because we can't call |
| 662 | target_detach, but that's OK; if the current target is "child", |
| 663 | then it doesn't need any further cleanups, and lin_lwp will |
| 664 | generally not encounter vfork (vfork is defined to fork |
| 665 | in libpthread.so). |
| 666 | |
| 667 | The holding part is very easy if we have VFORKDONE events; |
| 668 | but keeping track of both processes is beyond GDB at the |
| 669 | moment. So we don't expose the parent to the rest of GDB. |
| 670 | Instead we quietly hold onto it until such time as we can |
| 671 | safely resume it. */ |
| 672 | |
| 673 | if (has_vforked) |
| 674 | linux_parent_pid = parent_pid; |
| 675 | else if (!detach_fork) |
| 676 | { |
| 677 | struct fork_info *fp; |
| 678 | /* Retain parent fork in ptrace (stopped) state. */ |
| 679 | fp = find_fork_pid (parent_pid); |
| 680 | if (!fp) |
| 681 | fp = add_fork (parent_pid); |
| 682 | fork_save_infrun_state (fp, 0); |
| 683 | } |
| 684 | else |
| 685 | target_detach (NULL, 0); |
| 686 | |
| 687 | inferior_ptid = ptid_build (child_pid, child_pid, 0); |
| 688 | |
| 689 | /* Reinstall ourselves, since we might have been removed in |
| 690 | target_detach (which does other necessary cleanup). */ |
| 691 | |
| 692 | push_target (ops); |
| 693 | linux_nat_switch_fork (inferior_ptid); |
| 694 | check_for_thread_db (); |
| 695 | |
| 696 | /* Reset breakpoints in the child as appropriate. */ |
| 697 | follow_inferior_reset_breakpoints (); |
| 698 | } |
| 699 | |
| 700 | if (target_can_async_p ()) |
| 701 | target_async (inferior_event_handler, 0); |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | \f |
| 707 | static void |
| 708 | linux_child_insert_fork_catchpoint (int pid) |
| 709 | { |
| 710 | if (! linux_supports_tracefork (pid)) |
| 711 | error (_("Your system does not support fork catchpoints.")); |
| 712 | } |
| 713 | |
| 714 | static void |
| 715 | linux_child_insert_vfork_catchpoint (int pid) |
| 716 | { |
| 717 | if (!linux_supports_tracefork (pid)) |
| 718 | error (_("Your system does not support vfork catchpoints.")); |
| 719 | } |
| 720 | |
| 721 | static void |
| 722 | linux_child_insert_exec_catchpoint (int pid) |
| 723 | { |
| 724 | if (!linux_supports_tracefork (pid)) |
| 725 | error (_("Your system does not support exec catchpoints.")); |
| 726 | } |
| 727 | |
| 728 | /* On GNU/Linux there are no real LWP's. The closest thing to LWP's |
| 729 | are processes sharing the same VM space. A multi-threaded process |
| 730 | is basically a group of such processes. However, such a grouping |
| 731 | is almost entirely a user-space issue; the kernel doesn't enforce |
| 732 | such a grouping at all (this might change in the future). In |
| 733 | general, we'll rely on the threads library (i.e. the GNU/Linux |
| 734 | Threads library) to provide such a grouping. |
| 735 | |
| 736 | It is perfectly well possible to write a multi-threaded application |
| 737 | without the assistance of a threads library, by using the clone |
| 738 | system call directly. This module should be able to give some |
| 739 | rudimentary support for debugging such applications if developers |
| 740 | specify the CLONE_PTRACE flag in the clone system call, and are |
| 741 | using the Linux kernel 2.4 or above. |
| 742 | |
| 743 | Note that there are some peculiarities in GNU/Linux that affect |
| 744 | this code: |
| 745 | |
| 746 | - In general one should specify the __WCLONE flag to waitpid in |
| 747 | order to make it report events for any of the cloned processes |
| 748 | (and leave it out for the initial process). However, if a cloned |
| 749 | process has exited the exit status is only reported if the |
| 750 | __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but |
| 751 | we cannot use it since GDB must work on older systems too. |
| 752 | |
| 753 | - When a traced, cloned process exits and is waited for by the |
| 754 | debugger, the kernel reassigns it to the original parent and |
| 755 | keeps it around as a "zombie". Somehow, the GNU/Linux Threads |
| 756 | library doesn't notice this, which leads to the "zombie problem": |
| 757 | When debugged a multi-threaded process that spawns a lot of |
| 758 | threads will run out of processes, even if the threads exit, |
| 759 | because the "zombies" stay around. */ |
| 760 | |
| 761 | /* List of known LWPs. */ |
| 762 | struct lwp_info *lwp_list; |
| 763 | |
| 764 | /* Number of LWPs in the list. */ |
| 765 | static int num_lwps; |
| 766 | \f |
| 767 | |
| 768 | /* If the last reported event was a SIGTRAP, this variable is set to |
| 769 | the process id of the LWP/thread that got it. */ |
| 770 | ptid_t trap_ptid; |
| 771 | \f |
| 772 | |
| 773 | /* Since we cannot wait (in linux_nat_wait) for the initial process and |
| 774 | any cloned processes with a single call to waitpid, we have to use |
| 775 | the WNOHANG flag and call waitpid in a loop. To optimize |
| 776 | things a bit we use `sigsuspend' to wake us up when a process has |
| 777 | something to report (it will send us a SIGCHLD if it has). To make |
| 778 | this work we have to juggle with the signal mask. We save the |
| 779 | original signal mask such that we can restore it before creating a |
| 780 | new process in order to avoid blocking certain signals in the |
| 781 | inferior. We then block SIGCHLD during the waitpid/sigsuspend |
| 782 | loop. */ |
| 783 | |
| 784 | /* Original signal mask. */ |
| 785 | static sigset_t normal_mask; |
| 786 | |
| 787 | /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in |
| 788 | _initialize_linux_nat. */ |
| 789 | static sigset_t suspend_mask; |
| 790 | |
| 791 | /* SIGCHLD action for synchronous mode. */ |
| 792 | struct sigaction sync_sigchld_action; |
| 793 | |
| 794 | /* SIGCHLD action for asynchronous mode. */ |
| 795 | static struct sigaction async_sigchld_action; |
| 796 | \f |
| 797 | |
| 798 | /* Prototypes for local functions. */ |
| 799 | static int stop_wait_callback (struct lwp_info *lp, void *data); |
| 800 | static int linux_nat_thread_alive (ptid_t ptid); |
| 801 | static char *linux_child_pid_to_exec_file (int pid); |
| 802 | static int cancel_breakpoint (struct lwp_info *lp); |
| 803 | |
| 804 | \f |
| 805 | /* Convert wait status STATUS to a string. Used for printing debug |
| 806 | messages only. */ |
| 807 | |
| 808 | static char * |
| 809 | status_to_str (int status) |
| 810 | { |
| 811 | static char buf[64]; |
| 812 | |
| 813 | if (WIFSTOPPED (status)) |
| 814 | snprintf (buf, sizeof (buf), "%s (stopped)", |
| 815 | strsignal (WSTOPSIG (status))); |
| 816 | else if (WIFSIGNALED (status)) |
| 817 | snprintf (buf, sizeof (buf), "%s (terminated)", |
| 818 | strsignal (WSTOPSIG (status))); |
| 819 | else |
| 820 | snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status)); |
| 821 | |
| 822 | return buf; |
| 823 | } |
| 824 | |
| 825 | /* Initialize the list of LWPs. Note that this module, contrary to |
| 826 | what GDB's generic threads layer does for its thread list, |
| 827 | re-initializes the LWP lists whenever we mourn or detach (which |
| 828 | doesn't involve mourning) the inferior. */ |
| 829 | |
| 830 | static void |
| 831 | init_lwp_list (void) |
| 832 | { |
| 833 | struct lwp_info *lp, *lpnext; |
| 834 | |
| 835 | for (lp = lwp_list; lp; lp = lpnext) |
| 836 | { |
| 837 | lpnext = lp->next; |
| 838 | xfree (lp); |
| 839 | } |
| 840 | |
| 841 | lwp_list = NULL; |
| 842 | num_lwps = 0; |
| 843 | } |
| 844 | |
| 845 | /* Add the LWP specified by PID to the list. Return a pointer to the |
| 846 | structure describing the new LWP. The LWP should already be stopped |
| 847 | (with an exception for the very first LWP). */ |
| 848 | |
| 849 | static struct lwp_info * |
| 850 | add_lwp (ptid_t ptid) |
| 851 | { |
| 852 | struct lwp_info *lp; |
| 853 | |
| 854 | gdb_assert (is_lwp (ptid)); |
| 855 | |
| 856 | lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); |
| 857 | |
| 858 | memset (lp, 0, sizeof (struct lwp_info)); |
| 859 | |
| 860 | lp->waitstatus.kind = TARGET_WAITKIND_IGNORE; |
| 861 | |
| 862 | lp->ptid = ptid; |
| 863 | |
| 864 | lp->next = lwp_list; |
| 865 | lwp_list = lp; |
| 866 | ++num_lwps; |
| 867 | |
| 868 | if (num_lwps > 1 && linux_nat_new_thread != NULL) |
| 869 | linux_nat_new_thread (ptid); |
| 870 | |
| 871 | return lp; |
| 872 | } |
| 873 | |
| 874 | /* Remove the LWP specified by PID from the list. */ |
| 875 | |
| 876 | static void |
| 877 | delete_lwp (ptid_t ptid) |
| 878 | { |
| 879 | struct lwp_info *lp, *lpprev; |
| 880 | |
| 881 | lpprev = NULL; |
| 882 | |
| 883 | for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) |
| 884 | if (ptid_equal (lp->ptid, ptid)) |
| 885 | break; |
| 886 | |
| 887 | if (!lp) |
| 888 | return; |
| 889 | |
| 890 | num_lwps--; |
| 891 | |
| 892 | if (lpprev) |
| 893 | lpprev->next = lp->next; |
| 894 | else |
| 895 | lwp_list = lp->next; |
| 896 | |
| 897 | xfree (lp); |
| 898 | } |
| 899 | |
| 900 | /* Return a pointer to the structure describing the LWP corresponding |
| 901 | to PID. If no corresponding LWP could be found, return NULL. */ |
| 902 | |
| 903 | static struct lwp_info * |
| 904 | find_lwp_pid (ptid_t ptid) |
| 905 | { |
| 906 | struct lwp_info *lp; |
| 907 | int lwp; |
| 908 | |
| 909 | if (is_lwp (ptid)) |
| 910 | lwp = GET_LWP (ptid); |
| 911 | else |
| 912 | lwp = GET_PID (ptid); |
| 913 | |
| 914 | for (lp = lwp_list; lp; lp = lp->next) |
| 915 | if (lwp == GET_LWP (lp->ptid)) |
| 916 | return lp; |
| 917 | |
| 918 | return NULL; |
| 919 | } |
| 920 | |
| 921 | /* Call CALLBACK with its second argument set to DATA for every LWP in |
| 922 | the list. If CALLBACK returns 1 for a particular LWP, return a |
| 923 | pointer to the structure describing that LWP immediately. |
| 924 | Otherwise return NULL. */ |
| 925 | |
| 926 | struct lwp_info * |
| 927 | iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) |
| 928 | { |
| 929 | struct lwp_info *lp, *lpnext; |
| 930 | |
| 931 | for (lp = lwp_list; lp; lp = lpnext) |
| 932 | { |
| 933 | lpnext = lp->next; |
| 934 | if ((*callback) (lp, data)) |
| 935 | return lp; |
| 936 | } |
| 937 | |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | /* Update our internal state when changing from one fork (checkpoint, |
| 942 | et cetera) to another indicated by NEW_PTID. We can only switch |
| 943 | single-threaded applications, so we only create one new LWP, and |
| 944 | the previous list is discarded. */ |
| 945 | |
| 946 | void |
| 947 | linux_nat_switch_fork (ptid_t new_ptid) |
| 948 | { |
| 949 | struct lwp_info *lp; |
| 950 | |
| 951 | init_lwp_list (); |
| 952 | lp = add_lwp (new_ptid); |
| 953 | lp->stopped = 1; |
| 954 | } |
| 955 | |
| 956 | /* Record a PTID for later deletion. */ |
| 957 | |
| 958 | struct saved_ptids |
| 959 | { |
| 960 | ptid_t ptid; |
| 961 | struct saved_ptids *next; |
| 962 | }; |
| 963 | static struct saved_ptids *threads_to_delete; |
| 964 | |
| 965 | static void |
| 966 | record_dead_thread (ptid_t ptid) |
| 967 | { |
| 968 | struct saved_ptids *p = xmalloc (sizeof (struct saved_ptids)); |
| 969 | p->ptid = ptid; |
| 970 | p->next = threads_to_delete; |
| 971 | threads_to_delete = p; |
| 972 | } |
| 973 | |
| 974 | /* Delete any dead threads which are not the current thread. */ |
| 975 | |
| 976 | static void |
| 977 | prune_lwps (void) |
| 978 | { |
| 979 | struct saved_ptids **p = &threads_to_delete; |
| 980 | |
| 981 | while (*p) |
| 982 | if (! ptid_equal ((*p)->ptid, inferior_ptid)) |
| 983 | { |
| 984 | struct saved_ptids *tmp = *p; |
| 985 | delete_thread (tmp->ptid); |
| 986 | *p = tmp->next; |
| 987 | xfree (tmp); |
| 988 | } |
| 989 | else |
| 990 | p = &(*p)->next; |
| 991 | } |
| 992 | |
| 993 | /* Handle the exit of a single thread LP. */ |
| 994 | |
| 995 | static void |
| 996 | exit_lwp (struct lwp_info *lp) |
| 997 | { |
| 998 | if (in_thread_list (lp->ptid)) |
| 999 | { |
| 1000 | if (print_thread_events) |
| 1001 | printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid)); |
| 1002 | |
| 1003 | /* Core GDB cannot deal with us deleting the current thread. */ |
| 1004 | if (!ptid_equal (lp->ptid, inferior_ptid)) |
| 1005 | delete_thread (lp->ptid); |
| 1006 | else |
| 1007 | record_dead_thread (lp->ptid); |
| 1008 | } |
| 1009 | |
| 1010 | delete_lwp (lp->ptid); |
| 1011 | } |
| 1012 | |
| 1013 | /* Attach to the LWP specified by PID. If VERBOSE is non-zero, print |
| 1014 | a message telling the user that a new LWP has been added to the |
| 1015 | process. Return 0 if successful or -1 if the new LWP could not |
| 1016 | be attached. */ |
| 1017 | |
| 1018 | int |
| 1019 | lin_lwp_attach_lwp (ptid_t ptid) |
| 1020 | { |
| 1021 | struct lwp_info *lp; |
| 1022 | int async_events_were_enabled = 0; |
| 1023 | |
| 1024 | gdb_assert (is_lwp (ptid)); |
| 1025 | |
| 1026 | if (target_can_async_p ()) |
| 1027 | async_events_were_enabled = linux_nat_async_events (0); |
| 1028 | |
| 1029 | lp = find_lwp_pid (ptid); |
| 1030 | |
| 1031 | /* We assume that we're already attached to any LWP that has an id |
| 1032 | equal to the overall process id, and to any LWP that is already |
| 1033 | in our list of LWPs. If we're not seeing exit events from threads |
| 1034 | and we've had PID wraparound since we last tried to stop all threads, |
| 1035 | this assumption might be wrong; fortunately, this is very unlikely |
| 1036 | to happen. */ |
| 1037 | if (GET_LWP (ptid) != GET_PID (ptid) && lp == NULL) |
| 1038 | { |
| 1039 | pid_t pid; |
| 1040 | int status; |
| 1041 | int cloned = 0; |
| 1042 | |
| 1043 | if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) |
| 1044 | { |
| 1045 | /* If we fail to attach to the thread, issue a warning, |
| 1046 | but continue. One way this can happen is if thread |
| 1047 | creation is interrupted; as of Linux kernel 2.6.19, a |
| 1048 | bug may place threads in the thread list and then fail |
| 1049 | to create them. */ |
| 1050 | warning (_("Can't attach %s: %s"), target_pid_to_str (ptid), |
| 1051 | safe_strerror (errno)); |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | if (debug_linux_nat) |
| 1056 | fprintf_unfiltered (gdb_stdlog, |
| 1057 | "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n", |
| 1058 | target_pid_to_str (ptid)); |
| 1059 | |
| 1060 | pid = my_waitpid (GET_LWP (ptid), &status, 0); |
| 1061 | if (pid == -1 && errno == ECHILD) |
| 1062 | { |
| 1063 | /* Try again with __WCLONE to check cloned processes. */ |
| 1064 | pid = my_waitpid (GET_LWP (ptid), &status, __WCLONE); |
| 1065 | cloned = 1; |
| 1066 | } |
| 1067 | |
| 1068 | gdb_assert (pid == GET_LWP (ptid) |
| 1069 | && WIFSTOPPED (status) && WSTOPSIG (status)); |
| 1070 | |
| 1071 | if (lp == NULL) |
| 1072 | lp = add_lwp (ptid); |
| 1073 | lp->cloned = cloned; |
| 1074 | |
| 1075 | target_post_attach (pid); |
| 1076 | |
| 1077 | lp->stopped = 1; |
| 1078 | |
| 1079 | if (debug_linux_nat) |
| 1080 | { |
| 1081 | fprintf_unfiltered (gdb_stdlog, |
| 1082 | "LLAL: waitpid %s received %s\n", |
| 1083 | target_pid_to_str (ptid), |
| 1084 | status_to_str (status)); |
| 1085 | } |
| 1086 | } |
| 1087 | else |
| 1088 | { |
| 1089 | /* We assume that the LWP representing the original process is |
| 1090 | already stopped. Mark it as stopped in the data structure |
| 1091 | that the GNU/linux ptrace layer uses to keep track of |
| 1092 | threads. Note that this won't have already been done since |
| 1093 | the main thread will have, we assume, been stopped by an |
| 1094 | attach from a different layer. */ |
| 1095 | if (lp == NULL) |
| 1096 | lp = add_lwp (ptid); |
| 1097 | lp->stopped = 1; |
| 1098 | } |
| 1099 | |
| 1100 | if (async_events_were_enabled) |
| 1101 | linux_nat_async_events (1); |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static void |
| 1107 | linux_nat_create_inferior (char *exec_file, char *allargs, char **env, |
| 1108 | int from_tty) |
| 1109 | { |
| 1110 | int saved_async = 0; |
| 1111 | |
| 1112 | /* The fork_child mechanism is synchronous and calls target_wait, so |
| 1113 | we have to mask the async mode. */ |
| 1114 | |
| 1115 | if (target_can_async_p ()) |
| 1116 | saved_async = linux_nat_async_mask (0); |
| 1117 | else |
| 1118 | { |
| 1119 | /* Restore the original signal mask. */ |
| 1120 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
| 1121 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
| 1122 | suspend_mask = normal_mask; |
| 1123 | sigdelset (&suspend_mask, SIGCHLD); |
| 1124 | } |
| 1125 | |
| 1126 | linux_ops->to_create_inferior (exec_file, allargs, env, from_tty); |
| 1127 | |
| 1128 | if (saved_async) |
| 1129 | linux_nat_async_mask (saved_async); |
| 1130 | } |
| 1131 | |
| 1132 | static void |
| 1133 | linux_nat_attach (char *args, int from_tty) |
| 1134 | { |
| 1135 | struct lwp_info *lp; |
| 1136 | pid_t pid; |
| 1137 | int status; |
| 1138 | int cloned = 0; |
| 1139 | int options = 0; |
| 1140 | |
| 1141 | /* FIXME: We should probably accept a list of process id's, and |
| 1142 | attach all of them. */ |
| 1143 | linux_ops->to_attach (args, from_tty); |
| 1144 | |
| 1145 | if (!target_can_async_p ()) |
| 1146 | { |
| 1147 | /* Restore the original signal mask. */ |
| 1148 | sigprocmask (SIG_SETMASK, &normal_mask, NULL); |
| 1149 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
| 1150 | suspend_mask = normal_mask; |
| 1151 | sigdelset (&suspend_mask, SIGCHLD); |
| 1152 | } |
| 1153 | |
| 1154 | /* Make sure the initial process is stopped. The user-level threads |
| 1155 | layer might want to poke around in the inferior, and that won't |
| 1156 | work if things haven't stabilized yet. */ |
| 1157 | pid = my_waitpid (GET_PID (inferior_ptid), &status, options); |
| 1158 | if (pid == -1 && errno == ECHILD) |
| 1159 | { |
| 1160 | warning (_("%s is a cloned process"), target_pid_to_str (inferior_ptid)); |
| 1161 | |
| 1162 | /* Try again with __WCLONE to check cloned processes. */ |
| 1163 | options = __WCLONE; |
| 1164 | pid = my_waitpid (GET_PID (inferior_ptid), &status, options); |
| 1165 | cloned = 1; |
| 1166 | } |
| 1167 | |
| 1168 | gdb_assert (pid == GET_PID (inferior_ptid) |
| 1169 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP); |
| 1170 | |
| 1171 | /* Add the initial process as the first LWP to the list. */ |
| 1172 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)); |
| 1173 | lp = add_lwp (inferior_ptid); |
| 1174 | lp->cloned = cloned; |
| 1175 | |
| 1176 | lp->stopped = 1; |
| 1177 | lp->resumed = 1; |
| 1178 | |
| 1179 | if (!target_can_async_p ()) |
| 1180 | { |
| 1181 | /* Fake the SIGSTOP that core GDB expects. */ |
| 1182 | lp->status = W_STOPCODE (SIGSTOP); |
| 1183 | if (debug_linux_nat) |
| 1184 | fprintf_unfiltered (gdb_stdlog, |
| 1185 | "LNA: waitpid %ld, faking SIGSTOP\n", (long) pid); |
| 1186 | } |
| 1187 | else |
| 1188 | { |
| 1189 | /* We already waited for this LWP, so put the wait result on the |
| 1190 | pipe. The event loop will wake up and gets us to handling |
| 1191 | this event. */ |
| 1192 | linux_nat_event_pipe_push (pid, status, options); |
| 1193 | /* Register in the event loop. */ |
| 1194 | target_async (inferior_event_handler, 0); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | static int |
| 1199 | detach_callback (struct lwp_info *lp, void *data) |
| 1200 | { |
| 1201 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); |
| 1202 | |
| 1203 | if (debug_linux_nat && lp->status) |
| 1204 | fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n", |
| 1205 | strsignal (WSTOPSIG (lp->status)), |
| 1206 | target_pid_to_str (lp->ptid)); |
| 1207 | |
| 1208 | while (lp->signalled && lp->stopped) |
| 1209 | { |
| 1210 | errno = 0; |
| 1211 | if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, |
| 1212 | WSTOPSIG (lp->status)) < 0) |
| 1213 | error (_("Can't continue %s: %s"), target_pid_to_str (lp->ptid), |
| 1214 | safe_strerror (errno)); |
| 1215 | |
| 1216 | if (debug_linux_nat) |
| 1217 | fprintf_unfiltered (gdb_stdlog, |
| 1218 | "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n", |
| 1219 | target_pid_to_str (lp->ptid), |
| 1220 | status_to_str (lp->status)); |
| 1221 | |
| 1222 | lp->stopped = 0; |
| 1223 | lp->signalled = 0; |
| 1224 | lp->status = 0; |
| 1225 | /* FIXME drow/2003-08-26: There was a call to stop_wait_callback |
| 1226 | here. But since lp->signalled was cleared above, |
| 1227 | stop_wait_callback didn't do anything; the process was left |
| 1228 | running. Shouldn't we be waiting for it to stop? |
| 1229 | I've removed the call, since stop_wait_callback now does do |
| 1230 | something when called with lp->signalled == 0. */ |
| 1231 | |
| 1232 | gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); |
| 1233 | } |
| 1234 | |
| 1235 | /* We don't actually detach from the LWP that has an id equal to the |
| 1236 | overall process id just yet. */ |
| 1237 | if (GET_LWP (lp->ptid) != GET_PID (lp->ptid)) |
| 1238 | { |
| 1239 | errno = 0; |
| 1240 | if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, |
| 1241 | WSTOPSIG (lp->status)) < 0) |
| 1242 | error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid), |
| 1243 | safe_strerror (errno)); |
| 1244 | |
| 1245 | if (debug_linux_nat) |
| 1246 | fprintf_unfiltered (gdb_stdlog, |
| 1247 | "PTRACE_DETACH (%s, %s, 0) (OK)\n", |
| 1248 | target_pid_to_str (lp->ptid), |
| 1249 | strsignal (WSTOPSIG (lp->status))); |
| 1250 | |
| 1251 | drain_queued_events (GET_LWP (lp->ptid)); |
| 1252 | delete_lwp (lp->ptid); |
| 1253 | } |
| 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static void |
| 1259 | linux_nat_detach (char *args, int from_tty) |
| 1260 | { |
| 1261 | int pid; |
| 1262 | if (target_can_async_p ()) |
| 1263 | linux_nat_async (NULL, 0); |
| 1264 | |
| 1265 | iterate_over_lwps (detach_callback, NULL); |
| 1266 | |
| 1267 | /* Only the initial process should be left right now. */ |
| 1268 | gdb_assert (num_lwps == 1); |
| 1269 | |
| 1270 | trap_ptid = null_ptid; |
| 1271 | |
| 1272 | /* Destroy LWP info; it's no longer valid. */ |
| 1273 | init_lwp_list (); |
| 1274 | |
| 1275 | pid = GET_PID (inferior_ptid); |
| 1276 | inferior_ptid = pid_to_ptid (pid); |
| 1277 | linux_ops->to_detach (args, from_tty); |
| 1278 | |
| 1279 | if (target_can_async_p ()) |
| 1280 | drain_queued_events (pid); |
| 1281 | } |
| 1282 | |
| 1283 | /* Resume LP. */ |
| 1284 | |
| 1285 | static int |
| 1286 | resume_callback (struct lwp_info *lp, void *data) |
| 1287 | { |
| 1288 | if (lp->stopped && lp->status == 0) |
| 1289 | { |
| 1290 | linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)), |
| 1291 | 0, TARGET_SIGNAL_0); |
| 1292 | if (debug_linux_nat) |
| 1293 | fprintf_unfiltered (gdb_stdlog, |
| 1294 | "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n", |
| 1295 | target_pid_to_str (lp->ptid)); |
| 1296 | lp->stopped = 0; |
| 1297 | lp->step = 0; |
| 1298 | memset (&lp->siginfo, 0, sizeof (lp->siginfo)); |
| 1299 | } |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static int |
| 1305 | resume_clear_callback (struct lwp_info *lp, void *data) |
| 1306 | { |
| 1307 | lp->resumed = 0; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static int |
| 1312 | resume_set_callback (struct lwp_info *lp, void *data) |
| 1313 | { |
| 1314 | lp->resumed = 1; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static void |
| 1319 | linux_nat_resume (ptid_t ptid, int step, enum target_signal signo) |
| 1320 | { |
| 1321 | struct lwp_info *lp; |
| 1322 | int resume_all; |
| 1323 | |
| 1324 | if (debug_linux_nat) |
| 1325 | fprintf_unfiltered (gdb_stdlog, |
| 1326 | "LLR: Preparing to %s %s, %s, inferior_ptid %s\n", |
| 1327 | step ? "step" : "resume", |
| 1328 | target_pid_to_str (ptid), |
| 1329 | signo ? strsignal (signo) : "0", |
| 1330 | target_pid_to_str (inferior_ptid)); |
| 1331 | |
| 1332 | prune_lwps (); |
| 1333 | |
| 1334 | if (target_can_async_p ()) |
| 1335 | /* Block events while we're here. */ |
| 1336 | linux_nat_async_events (0); |
| 1337 | |
| 1338 | /* A specific PTID means `step only this process id'. */ |
| 1339 | resume_all = (PIDGET (ptid) == -1); |
| 1340 | |
| 1341 | if (resume_all) |
| 1342 | iterate_over_lwps (resume_set_callback, NULL); |
| 1343 | else |
| 1344 | iterate_over_lwps (resume_clear_callback, NULL); |
| 1345 | |
| 1346 | /* If PID is -1, it's the current inferior that should be |
| 1347 | handled specially. */ |
| 1348 | if (PIDGET (ptid) == -1) |
| 1349 | ptid = inferior_ptid; |
| 1350 | |
| 1351 | lp = find_lwp_pid (ptid); |
| 1352 | gdb_assert (lp != NULL); |
| 1353 | |
| 1354 | ptid = pid_to_ptid (GET_LWP (lp->ptid)); |
| 1355 | |
| 1356 | /* Remember if we're stepping. */ |
| 1357 | lp->step = step; |
| 1358 | |
| 1359 | /* Mark this LWP as resumed. */ |
| 1360 | lp->resumed = 1; |
| 1361 | |
| 1362 | /* If we have a pending wait status for this thread, there is no |
| 1363 | point in resuming the process. But first make sure that |
| 1364 | linux_nat_wait won't preemptively handle the event - we |
| 1365 | should never take this short-circuit if we are going to |
| 1366 | leave LP running, since we have skipped resuming all the |
| 1367 | other threads. This bit of code needs to be synchronized |
| 1368 | with linux_nat_wait. */ |
| 1369 | |
| 1370 | /* In async mode, we never have pending wait status. */ |
| 1371 | if (target_can_async_p () && lp->status) |
| 1372 | internal_error (__FILE__, __LINE__, "Pending status in async mode"); |
| 1373 | |
| 1374 | if (lp->status && WIFSTOPPED (lp->status)) |
| 1375 | { |
| 1376 | int saved_signo = target_signal_from_host (WSTOPSIG (lp->status)); |
| 1377 | |
| 1378 | if (signal_stop_state (saved_signo) == 0 |
| 1379 | && signal_print_state (saved_signo) == 0 |
| 1380 | && signal_pass_state (saved_signo) == 1) |
| 1381 | { |
| 1382 | if (debug_linux_nat) |
| 1383 | fprintf_unfiltered (gdb_stdlog, |
| 1384 | "LLR: Not short circuiting for ignored " |
| 1385 | "status 0x%x\n", lp->status); |
| 1386 | |
| 1387 | /* FIXME: What should we do if we are supposed to continue |
| 1388 | this thread with a signal? */ |
| 1389 | gdb_assert (signo == TARGET_SIGNAL_0); |
| 1390 | signo = saved_signo; |
| 1391 | lp->status = 0; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (lp->status) |
| 1396 | { |
| 1397 | /* FIXME: What should we do if we are supposed to continue |
| 1398 | this thread with a signal? */ |
| 1399 | gdb_assert (signo == TARGET_SIGNAL_0); |
| 1400 | |
| 1401 | if (debug_linux_nat) |
| 1402 | fprintf_unfiltered (gdb_stdlog, |
| 1403 | "LLR: Short circuiting for status 0x%x\n", |
| 1404 | lp->status); |
| 1405 | |
| 1406 | return; |
| 1407 | } |
| 1408 | |
| 1409 | /* Mark LWP as not stopped to prevent it from being continued by |
| 1410 | resume_callback. */ |
| 1411 | lp->stopped = 0; |
| 1412 | |
| 1413 | if (resume_all) |
| 1414 | iterate_over_lwps (resume_callback, NULL); |
| 1415 | |
| 1416 | linux_ops->to_resume (ptid, step, signo); |
| 1417 | memset (&lp->siginfo, 0, sizeof (lp->siginfo)); |
| 1418 | |
| 1419 | if (debug_linux_nat) |
| 1420 | fprintf_unfiltered (gdb_stdlog, |
| 1421 | "LLR: %s %s, %s (resume event thread)\n", |
| 1422 | step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", |
| 1423 | target_pid_to_str (ptid), |
| 1424 | signo ? strsignal (signo) : "0"); |
| 1425 | |
| 1426 | if (target_can_async_p ()) |
| 1427 | { |
| 1428 | target_executing = 1; |
| 1429 | target_async (inferior_event_handler, 0); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* Issue kill to specified lwp. */ |
| 1434 | |
| 1435 | static int tkill_failed; |
| 1436 | |
| 1437 | static int |
| 1438 | kill_lwp (int lwpid, int signo) |
| 1439 | { |
| 1440 | errno = 0; |
| 1441 | |
| 1442 | /* Use tkill, if possible, in case we are using nptl threads. If tkill |
| 1443 | fails, then we are not using nptl threads and we should be using kill. */ |
| 1444 | |
| 1445 | #ifdef HAVE_TKILL_SYSCALL |
| 1446 | if (!tkill_failed) |
| 1447 | { |
| 1448 | int ret = syscall (__NR_tkill, lwpid, signo); |
| 1449 | if (errno != ENOSYS) |
| 1450 | return ret; |
| 1451 | errno = 0; |
| 1452 | tkill_failed = 1; |
| 1453 | } |
| 1454 | #endif |
| 1455 | |
| 1456 | return kill (lwpid, signo); |
| 1457 | } |
| 1458 | |
| 1459 | /* Handle a GNU/Linux extended wait response. If we see a clone |
| 1460 | event, we need to add the new LWP to our list (and not report the |
| 1461 | trap to higher layers). This function returns non-zero if the |
| 1462 | event should be ignored and we should wait again. If STOPPING is |
| 1463 | true, the new LWP remains stopped, otherwise it is continued. */ |
| 1464 | |
| 1465 | static int |
| 1466 | linux_handle_extended_wait (struct lwp_info *lp, int status, |
| 1467 | int stopping) |
| 1468 | { |
| 1469 | int pid = GET_LWP (lp->ptid); |
| 1470 | struct target_waitstatus *ourstatus = &lp->waitstatus; |
| 1471 | struct lwp_info *new_lp = NULL; |
| 1472 | int event = status >> 16; |
| 1473 | |
| 1474 | if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK |
| 1475 | || event == PTRACE_EVENT_CLONE) |
| 1476 | { |
| 1477 | unsigned long new_pid; |
| 1478 | int ret; |
| 1479 | |
| 1480 | ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid); |
| 1481 | |
| 1482 | /* If we haven't already seen the new PID stop, wait for it now. */ |
| 1483 | if (! pull_pid_from_list (&stopped_pids, new_pid, &status)) |
| 1484 | { |
| 1485 | /* The new child has a pending SIGSTOP. We can't affect it until it |
| 1486 | hits the SIGSTOP, but we're already attached. */ |
| 1487 | ret = my_waitpid (new_pid, &status, |
| 1488 | (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0); |
| 1489 | if (ret == -1) |
| 1490 | perror_with_name (_("waiting for new child")); |
| 1491 | else if (ret != new_pid) |
| 1492 | internal_error (__FILE__, __LINE__, |
| 1493 | _("wait returned unexpected PID %d"), ret); |
| 1494 | else if (!WIFSTOPPED (status)) |
| 1495 | internal_error (__FILE__, __LINE__, |
| 1496 | _("wait returned unexpected status 0x%x"), status); |
| 1497 | } |
| 1498 | |
| 1499 | ourstatus->value.related_pid = new_pid; |
| 1500 | |
| 1501 | if (event == PTRACE_EVENT_FORK) |
| 1502 | ourstatus->kind = TARGET_WAITKIND_FORKED; |
| 1503 | else if (event == PTRACE_EVENT_VFORK) |
| 1504 | ourstatus->kind = TARGET_WAITKIND_VFORKED; |
| 1505 | else |
| 1506 | { |
| 1507 | ourstatus->kind = TARGET_WAITKIND_IGNORE; |
| 1508 | new_lp = add_lwp (BUILD_LWP (new_pid, GET_PID (inferior_ptid))); |
| 1509 | new_lp->cloned = 1; |
| 1510 | |
| 1511 | if (WSTOPSIG (status) != SIGSTOP) |
| 1512 | { |
| 1513 | /* This can happen if someone starts sending signals to |
| 1514 | the new thread before it gets a chance to run, which |
| 1515 | have a lower number than SIGSTOP (e.g. SIGUSR1). |
| 1516 | This is an unlikely case, and harder to handle for |
| 1517 | fork / vfork than for clone, so we do not try - but |
| 1518 | we handle it for clone events here. We'll send |
| 1519 | the other signal on to the thread below. */ |
| 1520 | |
| 1521 | new_lp->signalled = 1; |
| 1522 | } |
| 1523 | else |
| 1524 | status = 0; |
| 1525 | |
| 1526 | if (stopping) |
| 1527 | new_lp->stopped = 1; |
| 1528 | else |
| 1529 | { |
| 1530 | new_lp->resumed = 1; |
| 1531 | ptrace (PTRACE_CONT, lp->waitstatus.value.related_pid, 0, |
| 1532 | status ? WSTOPSIG (status) : 0); |
| 1533 | } |
| 1534 | |
| 1535 | if (debug_linux_nat) |
| 1536 | fprintf_unfiltered (gdb_stdlog, |
| 1537 | "LHEW: Got clone event from LWP %ld, resuming\n", |
| 1538 | GET_LWP (lp->ptid)); |
| 1539 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 1540 | |
| 1541 | return 1; |
| 1542 | } |
| 1543 | |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | if (event == PTRACE_EVENT_EXEC) |
| 1548 | { |
| 1549 | ourstatus->kind = TARGET_WAITKIND_EXECD; |
| 1550 | ourstatus->value.execd_pathname |
| 1551 | = xstrdup (linux_child_pid_to_exec_file (pid)); |
| 1552 | |
| 1553 | if (linux_parent_pid) |
| 1554 | { |
| 1555 | detach_breakpoints (linux_parent_pid); |
| 1556 | ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0); |
| 1557 | |
| 1558 | linux_parent_pid = 0; |
| 1559 | } |
| 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | internal_error (__FILE__, __LINE__, |
| 1565 | _("unknown ptrace event %d"), event); |
| 1566 | } |
| 1567 | |
| 1568 | /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has |
| 1569 | exited. */ |
| 1570 | |
| 1571 | static int |
| 1572 | wait_lwp (struct lwp_info *lp) |
| 1573 | { |
| 1574 | pid_t pid; |
| 1575 | int status; |
| 1576 | int thread_dead = 0; |
| 1577 | |
| 1578 | gdb_assert (!lp->stopped); |
| 1579 | gdb_assert (lp->status == 0); |
| 1580 | |
| 1581 | pid = my_waitpid (GET_LWP (lp->ptid), &status, 0); |
| 1582 | if (pid == -1 && errno == ECHILD) |
| 1583 | { |
| 1584 | pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE); |
| 1585 | if (pid == -1 && errno == ECHILD) |
| 1586 | { |
| 1587 | /* The thread has previously exited. We need to delete it |
| 1588 | now because, for some vendor 2.4 kernels with NPTL |
| 1589 | support backported, there won't be an exit event unless |
| 1590 | it is the main thread. 2.6 kernels will report an exit |
| 1591 | event for each thread that exits, as expected. */ |
| 1592 | thread_dead = 1; |
| 1593 | if (debug_linux_nat) |
| 1594 | fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n", |
| 1595 | target_pid_to_str (lp->ptid)); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | if (!thread_dead) |
| 1600 | { |
| 1601 | gdb_assert (pid == GET_LWP (lp->ptid)); |
| 1602 | |
| 1603 | if (debug_linux_nat) |
| 1604 | { |
| 1605 | fprintf_unfiltered (gdb_stdlog, |
| 1606 | "WL: waitpid %s received %s\n", |
| 1607 | target_pid_to_str (lp->ptid), |
| 1608 | status_to_str (status)); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /* Check if the thread has exited. */ |
| 1613 | if (WIFEXITED (status) || WIFSIGNALED (status)) |
| 1614 | { |
| 1615 | thread_dead = 1; |
| 1616 | if (debug_linux_nat) |
| 1617 | fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n", |
| 1618 | target_pid_to_str (lp->ptid)); |
| 1619 | } |
| 1620 | |
| 1621 | if (thread_dead) |
| 1622 | { |
| 1623 | exit_lwp (lp); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | gdb_assert (WIFSTOPPED (status)); |
| 1628 | |
| 1629 | /* Handle GNU/Linux's extended waitstatus for trace events. */ |
| 1630 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) |
| 1631 | { |
| 1632 | if (debug_linux_nat) |
| 1633 | fprintf_unfiltered (gdb_stdlog, |
| 1634 | "WL: Handling extended status 0x%06x\n", |
| 1635 | status); |
| 1636 | if (linux_handle_extended_wait (lp, status, 1)) |
| 1637 | return wait_lwp (lp); |
| 1638 | } |
| 1639 | |
| 1640 | return status; |
| 1641 | } |
| 1642 | |
| 1643 | /* Save the most recent siginfo for LP. This is currently only called |
| 1644 | for SIGTRAP; some ports use the si_addr field for |
| 1645 | target_stopped_data_address. In the future, it may also be used to |
| 1646 | restore the siginfo of requeued signals. */ |
| 1647 | |
| 1648 | static void |
| 1649 | save_siginfo (struct lwp_info *lp) |
| 1650 | { |
| 1651 | errno = 0; |
| 1652 | ptrace (PTRACE_GETSIGINFO, GET_LWP (lp->ptid), |
| 1653 | (PTRACE_TYPE_ARG3) 0, &lp->siginfo); |
| 1654 | |
| 1655 | if (errno != 0) |
| 1656 | memset (&lp->siginfo, 0, sizeof (lp->siginfo)); |
| 1657 | } |
| 1658 | |
| 1659 | /* Send a SIGSTOP to LP. */ |
| 1660 | |
| 1661 | static int |
| 1662 | stop_callback (struct lwp_info *lp, void *data) |
| 1663 | { |
| 1664 | if (!lp->stopped && !lp->signalled) |
| 1665 | { |
| 1666 | int ret; |
| 1667 | |
| 1668 | if (debug_linux_nat) |
| 1669 | { |
| 1670 | fprintf_unfiltered (gdb_stdlog, |
| 1671 | "SC: kill %s **<SIGSTOP>**\n", |
| 1672 | target_pid_to_str (lp->ptid)); |
| 1673 | } |
| 1674 | errno = 0; |
| 1675 | ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP); |
| 1676 | if (debug_linux_nat) |
| 1677 | { |
| 1678 | fprintf_unfiltered (gdb_stdlog, |
| 1679 | "SC: lwp kill %d %s\n", |
| 1680 | ret, |
| 1681 | errno ? safe_strerror (errno) : "ERRNO-OK"); |
| 1682 | } |
| 1683 | |
| 1684 | lp->signalled = 1; |
| 1685 | gdb_assert (lp->status == 0); |
| 1686 | } |
| 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* Wait until LP is stopped. If DATA is non-null it is interpreted as |
| 1692 | a pointer to a set of signals to be flushed immediately. */ |
| 1693 | |
| 1694 | static int |
| 1695 | stop_wait_callback (struct lwp_info *lp, void *data) |
| 1696 | { |
| 1697 | sigset_t *flush_mask = data; |
| 1698 | |
| 1699 | if (!lp->stopped) |
| 1700 | { |
| 1701 | int status; |
| 1702 | |
| 1703 | status = wait_lwp (lp); |
| 1704 | if (status == 0) |
| 1705 | return 0; |
| 1706 | |
| 1707 | /* Ignore any signals in FLUSH_MASK. */ |
| 1708 | if (flush_mask && sigismember (flush_mask, WSTOPSIG (status))) |
| 1709 | { |
| 1710 | if (!lp->signalled) |
| 1711 | { |
| 1712 | lp->stopped = 1; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | errno = 0; |
| 1717 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 1718 | if (debug_linux_nat) |
| 1719 | fprintf_unfiltered (gdb_stdlog, |
| 1720 | "PTRACE_CONT %s, 0, 0 (%s)\n", |
| 1721 | target_pid_to_str (lp->ptid), |
| 1722 | errno ? safe_strerror (errno) : "OK"); |
| 1723 | |
| 1724 | return stop_wait_callback (lp, flush_mask); |
| 1725 | } |
| 1726 | |
| 1727 | if (WSTOPSIG (status) != SIGSTOP) |
| 1728 | { |
| 1729 | if (WSTOPSIG (status) == SIGTRAP) |
| 1730 | { |
| 1731 | /* If a LWP other than the LWP that we're reporting an |
| 1732 | event for has hit a GDB breakpoint (as opposed to |
| 1733 | some random trap signal), then just arrange for it to |
| 1734 | hit it again later. We don't keep the SIGTRAP status |
| 1735 | and don't forward the SIGTRAP signal to the LWP. We |
| 1736 | will handle the current event, eventually we will |
| 1737 | resume all LWPs, and this one will get its breakpoint |
| 1738 | trap again. |
| 1739 | |
| 1740 | If we do not do this, then we run the risk that the |
| 1741 | user will delete or disable the breakpoint, but the |
| 1742 | thread will have already tripped on it. */ |
| 1743 | |
| 1744 | /* Save the trap's siginfo in case we need it later. */ |
| 1745 | save_siginfo (lp); |
| 1746 | |
| 1747 | /* Now resume this LWP and get the SIGSTOP event. */ |
| 1748 | errno = 0; |
| 1749 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 1750 | if (debug_linux_nat) |
| 1751 | { |
| 1752 | fprintf_unfiltered (gdb_stdlog, |
| 1753 | "PTRACE_CONT %s, 0, 0 (%s)\n", |
| 1754 | target_pid_to_str (lp->ptid), |
| 1755 | errno ? safe_strerror (errno) : "OK"); |
| 1756 | |
| 1757 | fprintf_unfiltered (gdb_stdlog, |
| 1758 | "SWC: Candidate SIGTRAP event in %s\n", |
| 1759 | target_pid_to_str (lp->ptid)); |
| 1760 | } |
| 1761 | /* Hold this event/waitstatus while we check to see if |
| 1762 | there are any more (we still want to get that SIGSTOP). */ |
| 1763 | stop_wait_callback (lp, data); |
| 1764 | |
| 1765 | if (target_can_async_p ()) |
| 1766 | { |
| 1767 | /* Don't leave a pending wait status in async mode. |
| 1768 | Retrigger the breakpoint. */ |
| 1769 | if (!cancel_breakpoint (lp)) |
| 1770 | { |
| 1771 | /* There was no gdb breakpoint set at pc. Put |
| 1772 | the event back in the queue. */ |
| 1773 | if (debug_linux_nat) |
| 1774 | fprintf_unfiltered (gdb_stdlog, |
| 1775 | "SWC: kill %s, %s\n", |
| 1776 | target_pid_to_str (lp->ptid), |
| 1777 | status_to_str ((int) status)); |
| 1778 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status)); |
| 1779 | } |
| 1780 | } |
| 1781 | else |
| 1782 | { |
| 1783 | /* Hold the SIGTRAP for handling by |
| 1784 | linux_nat_wait. */ |
| 1785 | /* If there's another event, throw it back into the |
| 1786 | queue. */ |
| 1787 | if (lp->status) |
| 1788 | { |
| 1789 | if (debug_linux_nat) |
| 1790 | fprintf_unfiltered (gdb_stdlog, |
| 1791 | "SWC: kill %s, %s\n", |
| 1792 | target_pid_to_str (lp->ptid), |
| 1793 | status_to_str ((int) status)); |
| 1794 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); |
| 1795 | } |
| 1796 | /* Save the sigtrap event. */ |
| 1797 | lp->status = status; |
| 1798 | } |
| 1799 | return 0; |
| 1800 | } |
| 1801 | else |
| 1802 | { |
| 1803 | /* The thread was stopped with a signal other than |
| 1804 | SIGSTOP, and didn't accidentally trip a breakpoint. */ |
| 1805 | |
| 1806 | if (debug_linux_nat) |
| 1807 | { |
| 1808 | fprintf_unfiltered (gdb_stdlog, |
| 1809 | "SWC: Pending event %s in %s\n", |
| 1810 | status_to_str ((int) status), |
| 1811 | target_pid_to_str (lp->ptid)); |
| 1812 | } |
| 1813 | /* Now resume this LWP and get the SIGSTOP event. */ |
| 1814 | errno = 0; |
| 1815 | ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 1816 | if (debug_linux_nat) |
| 1817 | fprintf_unfiltered (gdb_stdlog, |
| 1818 | "SWC: PTRACE_CONT %s, 0, 0 (%s)\n", |
| 1819 | target_pid_to_str (lp->ptid), |
| 1820 | errno ? safe_strerror (errno) : "OK"); |
| 1821 | |
| 1822 | /* Hold this event/waitstatus while we check to see if |
| 1823 | there are any more (we still want to get that SIGSTOP). */ |
| 1824 | stop_wait_callback (lp, data); |
| 1825 | |
| 1826 | /* If the lp->status field is still empty, use it to |
| 1827 | hold this event. If not, then this event must be |
| 1828 | returned to the event queue of the LWP. */ |
| 1829 | if (lp->status || target_can_async_p ()) |
| 1830 | { |
| 1831 | if (debug_linux_nat) |
| 1832 | { |
| 1833 | fprintf_unfiltered (gdb_stdlog, |
| 1834 | "SWC: kill %s, %s\n", |
| 1835 | target_pid_to_str (lp->ptid), |
| 1836 | status_to_str ((int) status)); |
| 1837 | } |
| 1838 | kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status)); |
| 1839 | } |
| 1840 | else |
| 1841 | lp->status = status; |
| 1842 | return 0; |
| 1843 | } |
| 1844 | } |
| 1845 | else |
| 1846 | { |
| 1847 | /* We caught the SIGSTOP that we intended to catch, so |
| 1848 | there's no SIGSTOP pending. */ |
| 1849 | lp->stopped = 1; |
| 1850 | lp->signalled = 0; |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | /* Check whether PID has any pending signals in FLUSH_MASK. If so set |
| 1858 | the appropriate bits in PENDING, and return 1 - otherwise return 0. */ |
| 1859 | |
| 1860 | static int |
| 1861 | linux_nat_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask) |
| 1862 | { |
| 1863 | sigset_t blocked, ignored; |
| 1864 | int i; |
| 1865 | |
| 1866 | linux_proc_pending_signals (pid, pending, &blocked, &ignored); |
| 1867 | |
| 1868 | if (!flush_mask) |
| 1869 | return 0; |
| 1870 | |
| 1871 | for (i = 1; i < NSIG; i++) |
| 1872 | if (sigismember (pending, i)) |
| 1873 | if (!sigismember (flush_mask, i) |
| 1874 | || sigismember (&blocked, i) |
| 1875 | || sigismember (&ignored, i)) |
| 1876 | sigdelset (pending, i); |
| 1877 | |
| 1878 | if (sigisemptyset (pending)) |
| 1879 | return 0; |
| 1880 | |
| 1881 | return 1; |
| 1882 | } |
| 1883 | |
| 1884 | /* DATA is interpreted as a mask of signals to flush. If LP has |
| 1885 | signals pending, and they are all in the flush mask, then arrange |
| 1886 | to flush them. LP should be stopped, as should all other threads |
| 1887 | it might share a signal queue with. */ |
| 1888 | |
| 1889 | static int |
| 1890 | flush_callback (struct lwp_info *lp, void *data) |
| 1891 | { |
| 1892 | sigset_t *flush_mask = data; |
| 1893 | sigset_t pending, intersection, blocked, ignored; |
| 1894 | int pid, status; |
| 1895 | |
| 1896 | /* Normally, when an LWP exits, it is removed from the LWP list. The |
| 1897 | last LWP isn't removed till later, however. So if there is only |
| 1898 | one LWP on the list, make sure it's alive. */ |
| 1899 | if (lwp_list == lp && lp->next == NULL) |
| 1900 | if (!linux_nat_thread_alive (lp->ptid)) |
| 1901 | return 0; |
| 1902 | |
| 1903 | /* Just because the LWP is stopped doesn't mean that new signals |
| 1904 | can't arrive from outside, so this function must be careful of |
| 1905 | race conditions. However, because all threads are stopped, we |
| 1906 | can assume that the pending mask will not shrink unless we resume |
| 1907 | the LWP, and that it will then get another signal. We can't |
| 1908 | control which one, however. */ |
| 1909 | |
| 1910 | if (lp->status) |
| 1911 | { |
| 1912 | if (debug_linux_nat) |
| 1913 | printf_unfiltered (_("FC: LP has pending status %06x\n"), lp->status); |
| 1914 | if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status))) |
| 1915 | lp->status = 0; |
| 1916 | } |
| 1917 | |
| 1918 | /* While there is a pending signal we would like to flush, continue |
| 1919 | the inferior and collect another signal. But if there's already |
| 1920 | a saved status that we don't want to flush, we can't resume the |
| 1921 | inferior - if it stopped for some other reason we wouldn't have |
| 1922 | anywhere to save the new status. In that case, we must leave the |
| 1923 | signal unflushed (and possibly generate an extra SIGINT stop). |
| 1924 | That's much less bad than losing a signal. */ |
| 1925 | while (lp->status == 0 |
| 1926 | && linux_nat_has_pending (GET_LWP (lp->ptid), &pending, flush_mask)) |
| 1927 | { |
| 1928 | int ret; |
| 1929 | |
| 1930 | errno = 0; |
| 1931 | ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); |
| 1932 | if (debug_linux_nat) |
| 1933 | fprintf_unfiltered (gdb_stderr, |
| 1934 | "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno); |
| 1935 | |
| 1936 | lp->stopped = 0; |
| 1937 | stop_wait_callback (lp, flush_mask); |
| 1938 | if (debug_linux_nat) |
| 1939 | fprintf_unfiltered (gdb_stderr, |
| 1940 | "FC: Wait finished; saved status is %d\n", |
| 1941 | lp->status); |
| 1942 | } |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* Return non-zero if LP has a wait status pending. */ |
| 1948 | |
| 1949 | static int |
| 1950 | status_callback (struct lwp_info *lp, void *data) |
| 1951 | { |
| 1952 | /* Only report a pending wait status if we pretend that this has |
| 1953 | indeed been resumed. */ |
| 1954 | return (lp->status != 0 && lp->resumed); |
| 1955 | } |
| 1956 | |
| 1957 | /* Return non-zero if LP isn't stopped. */ |
| 1958 | |
| 1959 | static int |
| 1960 | running_callback (struct lwp_info *lp, void *data) |
| 1961 | { |
| 1962 | return (lp->stopped == 0 || (lp->status != 0 && lp->resumed)); |
| 1963 | } |
| 1964 | |
| 1965 | /* Count the LWP's that have had events. */ |
| 1966 | |
| 1967 | static int |
| 1968 | count_events_callback (struct lwp_info *lp, void *data) |
| 1969 | { |
| 1970 | int *count = data; |
| 1971 | |
| 1972 | gdb_assert (count != NULL); |
| 1973 | |
| 1974 | /* Count only LWPs that have a SIGTRAP event pending. */ |
| 1975 | if (lp->status != 0 |
| 1976 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) |
| 1977 | (*count)++; |
| 1978 | |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
| 1982 | /* Select the LWP (if any) that is currently being single-stepped. */ |
| 1983 | |
| 1984 | static int |
| 1985 | select_singlestep_lwp_callback (struct lwp_info *lp, void *data) |
| 1986 | { |
| 1987 | if (lp->step && lp->status != 0) |
| 1988 | return 1; |
| 1989 | else |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | /* Select the Nth LWP that has had a SIGTRAP event. */ |
| 1994 | |
| 1995 | static int |
| 1996 | select_event_lwp_callback (struct lwp_info *lp, void *data) |
| 1997 | { |
| 1998 | int *selector = data; |
| 1999 | |
| 2000 | gdb_assert (selector != NULL); |
| 2001 | |
| 2002 | /* Select only LWPs that have a SIGTRAP event pending. */ |
| 2003 | if (lp->status != 0 |
| 2004 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) |
| 2005 | if ((*selector)-- == 0) |
| 2006 | return 1; |
| 2007 | |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | static int |
| 2012 | cancel_breakpoint (struct lwp_info *lp) |
| 2013 | { |
| 2014 | /* Arrange for a breakpoint to be hit again later. We don't keep |
| 2015 | the SIGTRAP status and don't forward the SIGTRAP signal to the |
| 2016 | LWP. We will handle the current event, eventually we will resume |
| 2017 | this LWP, and this breakpoint will trap again. |
| 2018 | |
| 2019 | If we do not do this, then we run the risk that the user will |
| 2020 | delete or disable the breakpoint, but the LWP will have already |
| 2021 | tripped on it. */ |
| 2022 | |
| 2023 | if (breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - |
| 2024 | gdbarch_decr_pc_after_break |
| 2025 | (current_gdbarch))) |
| 2026 | { |
| 2027 | if (debug_linux_nat) |
| 2028 | fprintf_unfiltered (gdb_stdlog, |
| 2029 | "CB: Push back breakpoint for %s\n", |
| 2030 | target_pid_to_str (lp->ptid)); |
| 2031 | |
| 2032 | /* Back up the PC if necessary. */ |
| 2033 | if (gdbarch_decr_pc_after_break (current_gdbarch)) |
| 2034 | write_pc_pid (read_pc_pid (lp->ptid) - gdbarch_decr_pc_after_break |
| 2035 | (current_gdbarch), |
| 2036 | lp->ptid); |
| 2037 | return 1; |
| 2038 | } |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | static int |
| 2043 | cancel_breakpoints_callback (struct lwp_info *lp, void *data) |
| 2044 | { |
| 2045 | struct lwp_info *event_lp = data; |
| 2046 | |
| 2047 | /* Leave the LWP that has been elected to receive a SIGTRAP alone. */ |
| 2048 | if (lp == event_lp) |
| 2049 | return 0; |
| 2050 | |
| 2051 | /* If a LWP other than the LWP that we're reporting an event for has |
| 2052 | hit a GDB breakpoint (as opposed to some random trap signal), |
| 2053 | then just arrange for it to hit it again later. We don't keep |
| 2054 | the SIGTRAP status and don't forward the SIGTRAP signal to the |
| 2055 | LWP. We will handle the current event, eventually we will resume |
| 2056 | all LWPs, and this one will get its breakpoint trap again. |
| 2057 | |
| 2058 | If we do not do this, then we run the risk that the user will |
| 2059 | delete or disable the breakpoint, but the LWP will have already |
| 2060 | tripped on it. */ |
| 2061 | |
| 2062 | if (lp->status != 0 |
| 2063 | && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP |
| 2064 | && cancel_breakpoint (lp)) |
| 2065 | /* Throw away the SIGTRAP. */ |
| 2066 | lp->status = 0; |
| 2067 | |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | /* Select one LWP out of those that have events pending. */ |
| 2072 | |
| 2073 | static void |
| 2074 | select_event_lwp (struct lwp_info **orig_lp, int *status) |
| 2075 | { |
| 2076 | int num_events = 0; |
| 2077 | int random_selector; |
| 2078 | struct lwp_info *event_lp; |
| 2079 | |
| 2080 | /* Record the wait status for the original LWP. */ |
| 2081 | (*orig_lp)->status = *status; |
| 2082 | |
| 2083 | /* Give preference to any LWP that is being single-stepped. */ |
| 2084 | event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); |
| 2085 | if (event_lp != NULL) |
| 2086 | { |
| 2087 | if (debug_linux_nat) |
| 2088 | fprintf_unfiltered (gdb_stdlog, |
| 2089 | "SEL: Select single-step %s\n", |
| 2090 | target_pid_to_str (event_lp->ptid)); |
| 2091 | } |
| 2092 | else |
| 2093 | { |
| 2094 | /* No single-stepping LWP. Select one at random, out of those |
| 2095 | which have had SIGTRAP events. */ |
| 2096 | |
| 2097 | /* First see how many SIGTRAP events we have. */ |
| 2098 | iterate_over_lwps (count_events_callback, &num_events); |
| 2099 | |
| 2100 | /* Now randomly pick a LWP out of those that have had a SIGTRAP. */ |
| 2101 | random_selector = (int) |
| 2102 | ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); |
| 2103 | |
| 2104 | if (debug_linux_nat && num_events > 1) |
| 2105 | fprintf_unfiltered (gdb_stdlog, |
| 2106 | "SEL: Found %d SIGTRAP events, selecting #%d\n", |
| 2107 | num_events, random_selector); |
| 2108 | |
| 2109 | event_lp = iterate_over_lwps (select_event_lwp_callback, |
| 2110 | &random_selector); |
| 2111 | } |
| 2112 | |
| 2113 | if (event_lp != NULL) |
| 2114 | { |
| 2115 | /* Switch the event LWP. */ |
| 2116 | *orig_lp = event_lp; |
| 2117 | *status = event_lp->status; |
| 2118 | } |
| 2119 | |
| 2120 | /* Flush the wait status for the event LWP. */ |
| 2121 | (*orig_lp)->status = 0; |
| 2122 | } |
| 2123 | |
| 2124 | /* Return non-zero if LP has been resumed. */ |
| 2125 | |
| 2126 | static int |
| 2127 | resumed_callback (struct lwp_info *lp, void *data) |
| 2128 | { |
| 2129 | return lp->resumed; |
| 2130 | } |
| 2131 | |
| 2132 | /* Stop an active thread, verify it still exists, then resume it. */ |
| 2133 | |
| 2134 | static int |
| 2135 | stop_and_resume_callback (struct lwp_info *lp, void *data) |
| 2136 | { |
| 2137 | struct lwp_info *ptr; |
| 2138 | |
| 2139 | if (!lp->stopped && !lp->signalled) |
| 2140 | { |
| 2141 | stop_callback (lp, NULL); |
| 2142 | stop_wait_callback (lp, NULL); |
| 2143 | /* Resume if the lwp still exists. */ |
| 2144 | for (ptr = lwp_list; ptr; ptr = ptr->next) |
| 2145 | if (lp == ptr) |
| 2146 | { |
| 2147 | resume_callback (lp, NULL); |
| 2148 | resume_set_callback (lp, NULL); |
| 2149 | } |
| 2150 | } |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | /* Check if we should go on and pass this event to common code. |
| 2155 | Return the affected lwp if we are, or NULL otherwise. */ |
| 2156 | static struct lwp_info * |
| 2157 | linux_nat_filter_event (int lwpid, int status, int options) |
| 2158 | { |
| 2159 | struct lwp_info *lp; |
| 2160 | |
| 2161 | lp = find_lwp_pid (pid_to_ptid (lwpid)); |
| 2162 | |
| 2163 | /* Check for stop events reported by a process we didn't already |
| 2164 | know about - anything not already in our LWP list. |
| 2165 | |
| 2166 | If we're expecting to receive stopped processes after |
| 2167 | fork, vfork, and clone events, then we'll just add the |
| 2168 | new one to our list and go back to waiting for the event |
| 2169 | to be reported - the stopped process might be returned |
| 2170 | from waitpid before or after the event is. */ |
| 2171 | if (WIFSTOPPED (status) && !lp) |
| 2172 | { |
| 2173 | linux_record_stopped_pid (lwpid, status); |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | |
| 2177 | /* Make sure we don't report an event for the exit of an LWP not in |
| 2178 | our list, i.e. not part of the current process. This can happen |
| 2179 | if we detach from a program we original forked and then it |
| 2180 | exits. */ |
| 2181 | if (!WIFSTOPPED (status) && !lp) |
| 2182 | return NULL; |
| 2183 | |
| 2184 | /* NOTE drow/2003-06-17: This code seems to be meant for debugging |
| 2185 | CLONE_PTRACE processes which do not use the thread library - |
| 2186 | otherwise we wouldn't find the new LWP this way. That doesn't |
| 2187 | currently work, and the following code is currently unreachable |
| 2188 | due to the two blocks above. If it's fixed some day, this code |
| 2189 | should be broken out into a function so that we can also pick up |
| 2190 | LWPs from the new interface. */ |
| 2191 | if (!lp) |
| 2192 | { |
| 2193 | lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); |
| 2194 | if (options & __WCLONE) |
| 2195 | lp->cloned = 1; |
| 2196 | |
| 2197 | gdb_assert (WIFSTOPPED (status) |
| 2198 | && WSTOPSIG (status) == SIGSTOP); |
| 2199 | lp->signalled = 1; |
| 2200 | |
| 2201 | if (!in_thread_list (inferior_ptid)) |
| 2202 | { |
| 2203 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), |
| 2204 | GET_PID (inferior_ptid)); |
| 2205 | add_thread (inferior_ptid); |
| 2206 | } |
| 2207 | |
| 2208 | add_thread (lp->ptid); |
| 2209 | } |
| 2210 | |
| 2211 | /* Save the trap's siginfo in case we need it later. */ |
| 2212 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) |
| 2213 | save_siginfo (lp); |
| 2214 | |
| 2215 | /* Handle GNU/Linux's extended waitstatus for trace events. */ |
| 2216 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) |
| 2217 | { |
| 2218 | if (debug_linux_nat) |
| 2219 | fprintf_unfiltered (gdb_stdlog, |
| 2220 | "LLW: Handling extended status 0x%06x\n", |
| 2221 | status); |
| 2222 | if (linux_handle_extended_wait (lp, status, 0)) |
| 2223 | return NULL; |
| 2224 | } |
| 2225 | |
| 2226 | /* Check if the thread has exited. */ |
| 2227 | if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) |
| 2228 | { |
| 2229 | /* If this is the main thread, we must stop all threads and |
| 2230 | verify if they are still alive. This is because in the nptl |
| 2231 | thread model, there is no signal issued for exiting LWPs |
| 2232 | other than the main thread. We only get the main thread exit |
| 2233 | signal once all child threads have already exited. If we |
| 2234 | stop all the threads and use the stop_wait_callback to check |
| 2235 | if they have exited we can determine whether this signal |
| 2236 | should be ignored or whether it means the end of the debugged |
| 2237 | application, regardless of which threading model is being |
| 2238 | used. */ |
| 2239 | if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)) |
| 2240 | { |
| 2241 | lp->stopped = 1; |
| 2242 | iterate_over_lwps (stop_and_resume_callback, NULL); |
| 2243 | } |
| 2244 | |
| 2245 | if (debug_linux_nat) |
| 2246 | fprintf_unfiltered (gdb_stdlog, |
| 2247 | "LLW: %s exited.\n", |
| 2248 | target_pid_to_str (lp->ptid)); |
| 2249 | |
| 2250 | exit_lwp (lp); |
| 2251 | |
| 2252 | /* If there is at least one more LWP, then the exit signal was |
| 2253 | not the end of the debugged application and should be |
| 2254 | ignored. */ |
| 2255 | if (num_lwps > 0) |
| 2256 | { |
| 2257 | /* Make sure there is at least one thread running. */ |
| 2258 | gdb_assert (iterate_over_lwps (running_callback, NULL)); |
| 2259 | |
| 2260 | /* Discard the event. */ |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | /* Check if the current LWP has previously exited. In the nptl |
| 2266 | thread model, LWPs other than the main thread do not issue |
| 2267 | signals when they exit so we must check whenever the thread has |
| 2268 | stopped. A similar check is made in stop_wait_callback(). */ |
| 2269 | if (num_lwps > 1 && !linux_nat_thread_alive (lp->ptid)) |
| 2270 | { |
| 2271 | if (debug_linux_nat) |
| 2272 | fprintf_unfiltered (gdb_stdlog, |
| 2273 | "LLW: %s exited.\n", |
| 2274 | target_pid_to_str (lp->ptid)); |
| 2275 | |
| 2276 | exit_lwp (lp); |
| 2277 | |
| 2278 | /* Make sure there is at least one thread running. */ |
| 2279 | gdb_assert (iterate_over_lwps (running_callback, NULL)); |
| 2280 | |
| 2281 | /* Discard the event. */ |
| 2282 | return NULL; |
| 2283 | } |
| 2284 | |
| 2285 | /* Make sure we don't report a SIGSTOP that we sent ourselves in |
| 2286 | an attempt to stop an LWP. */ |
| 2287 | if (lp->signalled |
| 2288 | && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) |
| 2289 | { |
| 2290 | if (debug_linux_nat) |
| 2291 | fprintf_unfiltered (gdb_stdlog, |
| 2292 | "LLW: Delayed SIGSTOP caught for %s.\n", |
| 2293 | target_pid_to_str (lp->ptid)); |
| 2294 | |
| 2295 | /* This is a delayed SIGSTOP. */ |
| 2296 | lp->signalled = 0; |
| 2297 | |
| 2298 | registers_changed (); |
| 2299 | |
| 2300 | linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)), |
| 2301 | lp->step, TARGET_SIGNAL_0); |
| 2302 | if (debug_linux_nat) |
| 2303 | fprintf_unfiltered (gdb_stdlog, |
| 2304 | "LLW: %s %s, 0, 0 (discard SIGSTOP)\n", |
| 2305 | lp->step ? |
| 2306 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", |
| 2307 | target_pid_to_str (lp->ptid)); |
| 2308 | |
| 2309 | lp->stopped = 0; |
| 2310 | gdb_assert (lp->resumed); |
| 2311 | |
| 2312 | /* Discard the event. */ |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | |
| 2316 | /* An interesting event. */ |
| 2317 | gdb_assert (lp); |
| 2318 | return lp; |
| 2319 | } |
| 2320 | |
| 2321 | /* Get the events stored in the pipe into the local queue, so they are |
| 2322 | accessible to queued_waitpid. We need to do this, since it is not |
| 2323 | always the case that the event at the head of the pipe is the event |
| 2324 | we want. */ |
| 2325 | |
| 2326 | static void |
| 2327 | pipe_to_local_event_queue (void) |
| 2328 | { |
| 2329 | if (debug_linux_nat_async) |
| 2330 | fprintf_unfiltered (gdb_stdlog, |
| 2331 | "PTLEQ: linux_nat_num_queued_events(%d)\n", |
| 2332 | linux_nat_num_queued_events); |
| 2333 | while (linux_nat_num_queued_events) |
| 2334 | { |
| 2335 | int lwpid, status, options; |
| 2336 | lwpid = linux_nat_event_pipe_pop (&status, &options); |
| 2337 | gdb_assert (lwpid > 0); |
| 2338 | push_waitpid (lwpid, status, options); |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | /* Get the unprocessed events stored in the local queue back into the |
| 2343 | pipe, so the event loop realizes there's something else to |
| 2344 | process. */ |
| 2345 | |
| 2346 | static void |
| 2347 | local_event_queue_to_pipe (void) |
| 2348 | { |
| 2349 | struct waitpid_result *w = waitpid_queue; |
| 2350 | while (w) |
| 2351 | { |
| 2352 | struct waitpid_result *next = w->next; |
| 2353 | linux_nat_event_pipe_push (w->pid, |
| 2354 | w->status, |
| 2355 | w->options); |
| 2356 | xfree (w); |
| 2357 | w = next; |
| 2358 | } |
| 2359 | waitpid_queue = NULL; |
| 2360 | |
| 2361 | if (debug_linux_nat_async) |
| 2362 | fprintf_unfiltered (gdb_stdlog, |
| 2363 | "LEQTP: linux_nat_num_queued_events(%d)\n", |
| 2364 | linux_nat_num_queued_events); |
| 2365 | } |
| 2366 | |
| 2367 | static ptid_t |
| 2368 | linux_nat_wait (ptid_t ptid, struct target_waitstatus *ourstatus) |
| 2369 | { |
| 2370 | struct lwp_info *lp = NULL; |
| 2371 | int options = 0; |
| 2372 | int status = 0; |
| 2373 | pid_t pid = PIDGET (ptid); |
| 2374 | sigset_t flush_mask; |
| 2375 | |
| 2376 | if (debug_linux_nat_async) |
| 2377 | fprintf_unfiltered (gdb_stdlog, "LLW: enter\n"); |
| 2378 | |
| 2379 | /* The first time we get here after starting a new inferior, we may |
| 2380 | not have added it to the LWP list yet - this is the earliest |
| 2381 | moment at which we know its PID. */ |
| 2382 | if (num_lwps == 0) |
| 2383 | { |
| 2384 | gdb_assert (!is_lwp (inferior_ptid)); |
| 2385 | |
| 2386 | inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), |
| 2387 | GET_PID (inferior_ptid)); |
| 2388 | lp = add_lwp (inferior_ptid); |
| 2389 | lp->resumed = 1; |
| 2390 | } |
| 2391 | |
| 2392 | sigemptyset (&flush_mask); |
| 2393 | |
| 2394 | if (target_can_async_p ()) |
| 2395 | /* Block events while we're here. */ |
| 2396 | target_async (NULL, 0); |
| 2397 | |
| 2398 | retry: |
| 2399 | |
| 2400 | /* Make sure there is at least one LWP that has been resumed. */ |
| 2401 | gdb_assert (iterate_over_lwps (resumed_callback, NULL)); |
| 2402 | |
| 2403 | /* First check if there is a LWP with a wait status pending. */ |
| 2404 | if (pid == -1) |
| 2405 | { |
| 2406 | /* Any LWP that's been resumed will do. */ |
| 2407 | lp = iterate_over_lwps (status_callback, NULL); |
| 2408 | if (lp) |
| 2409 | { |
| 2410 | if (target_can_async_p ()) |
| 2411 | internal_error (__FILE__, __LINE__, |
| 2412 | "Found an LWP with a pending status in async mode."); |
| 2413 | |
| 2414 | status = lp->status; |
| 2415 | lp->status = 0; |
| 2416 | |
| 2417 | if (debug_linux_nat && status) |
| 2418 | fprintf_unfiltered (gdb_stdlog, |
| 2419 | "LLW: Using pending wait status %s for %s.\n", |
| 2420 | status_to_str (status), |
| 2421 | target_pid_to_str (lp->ptid)); |
| 2422 | } |
| 2423 | |
| 2424 | /* But if we don't find one, we'll have to wait, and check both |
| 2425 | cloned and uncloned processes. We start with the cloned |
| 2426 | processes. */ |
| 2427 | options = __WCLONE | WNOHANG; |
| 2428 | } |
| 2429 | else if (is_lwp (ptid)) |
| 2430 | { |
| 2431 | if (debug_linux_nat) |
| 2432 | fprintf_unfiltered (gdb_stdlog, |
| 2433 | "LLW: Waiting for specific LWP %s.\n", |
| 2434 | target_pid_to_str (ptid)); |
| 2435 | |
| 2436 | /* We have a specific LWP to check. */ |
| 2437 | lp = find_lwp_pid (ptid); |
| 2438 | gdb_assert (lp); |
| 2439 | status = lp->status; |
| 2440 | lp->status = 0; |
| 2441 | |
| 2442 | if (debug_linux_nat && status) |
| 2443 | fprintf_unfiltered (gdb_stdlog, |
| 2444 | "LLW: Using pending wait status %s for %s.\n", |
| 2445 | status_to_str (status), |
| 2446 | target_pid_to_str (lp->ptid)); |
| 2447 | |
| 2448 | /* If we have to wait, take into account whether PID is a cloned |
| 2449 | process or not. And we have to convert it to something that |
| 2450 | the layer beneath us can understand. */ |
| 2451 | options = lp->cloned ? __WCLONE : 0; |
| 2452 | pid = GET_LWP (ptid); |
| 2453 | } |
| 2454 | |
| 2455 | if (status && lp->signalled) |
| 2456 | { |
| 2457 | /* A pending SIGSTOP may interfere with the normal stream of |
| 2458 | events. In a typical case where interference is a problem, |
| 2459 | we have a SIGSTOP signal pending for LWP A while |
| 2460 | single-stepping it, encounter an event in LWP B, and take the |
| 2461 | pending SIGSTOP while trying to stop LWP A. After processing |
| 2462 | the event in LWP B, LWP A is continued, and we'll never see |
| 2463 | the SIGTRAP associated with the last time we were |
| 2464 | single-stepping LWP A. */ |
| 2465 | |
| 2466 | /* Resume the thread. It should halt immediately returning the |
| 2467 | pending SIGSTOP. */ |
| 2468 | registers_changed (); |
| 2469 | linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)), |
| 2470 | lp->step, TARGET_SIGNAL_0); |
| 2471 | if (debug_linux_nat) |
| 2472 | fprintf_unfiltered (gdb_stdlog, |
| 2473 | "LLW: %s %s, 0, 0 (expect SIGSTOP)\n", |
| 2474 | lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", |
| 2475 | target_pid_to_str (lp->ptid)); |
| 2476 | lp->stopped = 0; |
| 2477 | gdb_assert (lp->resumed); |
| 2478 | |
| 2479 | /* This should catch the pending SIGSTOP. */ |
| 2480 | stop_wait_callback (lp, NULL); |
| 2481 | } |
| 2482 | |
| 2483 | if (!target_can_async_p ()) |
| 2484 | { |
| 2485 | /* Causes SIGINT to be passed on to the attached process. */ |
| 2486 | set_sigint_trap (); |
| 2487 | set_sigio_trap (); |
| 2488 | } |
| 2489 | |
| 2490 | while (status == 0) |
| 2491 | { |
| 2492 | pid_t lwpid; |
| 2493 | |
| 2494 | if (target_can_async_p ()) |
| 2495 | /* In async mode, don't ever block. Only look at the locally |
| 2496 | queued events. */ |
| 2497 | lwpid = queued_waitpid (pid, &status, options); |
| 2498 | else |
| 2499 | lwpid = my_waitpid (pid, &status, options); |
| 2500 | |
| 2501 | if (lwpid > 0) |
| 2502 | { |
| 2503 | gdb_assert (pid == -1 || lwpid == pid); |
| 2504 | |
| 2505 | if (debug_linux_nat) |
| 2506 | { |
| 2507 | fprintf_unfiltered (gdb_stdlog, |
| 2508 | "LLW: waitpid %ld received %s\n", |
| 2509 | (long) lwpid, status_to_str (status)); |
| 2510 | } |
| 2511 | |
| 2512 | lp = linux_nat_filter_event (lwpid, status, options); |
| 2513 | if (!lp) |
| 2514 | { |
| 2515 | /* A discarded event. */ |
| 2516 | status = 0; |
| 2517 | continue; |
| 2518 | } |
| 2519 | |
| 2520 | break; |
| 2521 | } |
| 2522 | |
| 2523 | if (pid == -1) |
| 2524 | { |
| 2525 | /* Alternate between checking cloned and uncloned processes. */ |
| 2526 | options ^= __WCLONE; |
| 2527 | |
| 2528 | /* And every time we have checked both: |
| 2529 | In async mode, return to event loop; |
| 2530 | In sync mode, suspend waiting for a SIGCHLD signal. */ |
| 2531 | if (options & __WCLONE) |
| 2532 | { |
| 2533 | if (target_can_async_p ()) |
| 2534 | { |
| 2535 | /* No interesting event. */ |
| 2536 | ourstatus->kind = TARGET_WAITKIND_IGNORE; |
| 2537 | |
| 2538 | /* Get ready for the next event. */ |
| 2539 | target_async (inferior_event_handler, 0); |
| 2540 | |
| 2541 | if (debug_linux_nat_async) |
| 2542 | fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n"); |
| 2543 | |
| 2544 | return minus_one_ptid; |
| 2545 | } |
| 2546 | |
| 2547 | sigsuspend (&suspend_mask); |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | /* We shouldn't end up here unless we want to try again. */ |
| 2552 | gdb_assert (status == 0); |
| 2553 | } |
| 2554 | |
| 2555 | if (!target_can_async_p ()) |
| 2556 | { |
| 2557 | clear_sigio_trap (); |
| 2558 | clear_sigint_trap (); |
| 2559 | } |
| 2560 | |
| 2561 | gdb_assert (lp); |
| 2562 | |
| 2563 | /* Don't report signals that GDB isn't interested in, such as |
| 2564 | signals that are neither printed nor stopped upon. Stopping all |
| 2565 | threads can be a bit time-consuming so if we want decent |
| 2566 | performance with heavily multi-threaded programs, especially when |
| 2567 | they're using a high frequency timer, we'd better avoid it if we |
| 2568 | can. */ |
| 2569 | |
| 2570 | if (WIFSTOPPED (status)) |
| 2571 | { |
| 2572 | int signo = target_signal_from_host (WSTOPSIG (status)); |
| 2573 | |
| 2574 | /* If we get a signal while single-stepping, we may need special |
| 2575 | care, e.g. to skip the signal handler. Defer to common code. */ |
| 2576 | if (!lp->step |
| 2577 | && signal_stop_state (signo) == 0 |
| 2578 | && signal_print_state (signo) == 0 |
| 2579 | && signal_pass_state (signo) == 1) |
| 2580 | { |
| 2581 | /* FIMXE: kettenis/2001-06-06: Should we resume all threads |
| 2582 | here? It is not clear we should. GDB may not expect |
| 2583 | other threads to run. On the other hand, not resuming |
| 2584 | newly attached threads may cause an unwanted delay in |
| 2585 | getting them running. */ |
| 2586 | registers_changed (); |
| 2587 | linux_ops->to_resume (pid_to_ptid (GET_LWP (lp->ptid)), |
| 2588 | lp->step, signo); |
| 2589 | if (debug_linux_nat) |
| 2590 | fprintf_unfiltered (gdb_stdlog, |
| 2591 | "LLW: %s %s, %s (preempt 'handle')\n", |
| 2592 | lp->step ? |
| 2593 | "PTRACE_SINGLESTEP" : "PTRACE_CONT", |
| 2594 | target_pid_to_str (lp->ptid), |
| 2595 | signo ? strsignal (signo) : "0"); |
| 2596 | lp->stopped = 0; |
| 2597 | status = 0; |
| 2598 | goto retry; |
| 2599 | } |
| 2600 | |
| 2601 | if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0) |
| 2602 | { |
| 2603 | /* If ^C/BREAK is typed at the tty/console, SIGINT gets |
| 2604 | forwarded to the entire process group, that is, all LWP's |
| 2605 | will receive it. Since we only want to report it once, |
| 2606 | we try to flush it from all LWPs except this one. */ |
| 2607 | sigaddset (&flush_mask, SIGINT); |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | /* This LWP is stopped now. */ |
| 2612 | lp->stopped = 1; |
| 2613 | |
| 2614 | if (debug_linux_nat) |
| 2615 | fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n", |
| 2616 | status_to_str (status), target_pid_to_str (lp->ptid)); |
| 2617 | |
| 2618 | /* Now stop all other LWP's ... */ |
| 2619 | iterate_over_lwps (stop_callback, NULL); |
| 2620 | |
| 2621 | /* ... and wait until all of them have reported back that they're no |
| 2622 | longer running. */ |
| 2623 | iterate_over_lwps (stop_wait_callback, &flush_mask); |
| 2624 | iterate_over_lwps (flush_callback, &flush_mask); |
| 2625 | |
| 2626 | /* If we're not waiting for a specific LWP, choose an event LWP from |
| 2627 | among those that have had events. Giving equal priority to all |
| 2628 | LWPs that have had events helps prevent starvation. */ |
| 2629 | if (pid == -1) |
| 2630 | select_event_lwp (&lp, &status); |
| 2631 | |
| 2632 | /* Now that we've selected our final event LWP, cancel any |
| 2633 | breakpoints in other LWPs that have hit a GDB breakpoint. See |
| 2634 | the comment in cancel_breakpoints_callback to find out why. */ |
| 2635 | iterate_over_lwps (cancel_breakpoints_callback, lp); |
| 2636 | |
| 2637 | if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) |
| 2638 | { |
| 2639 | trap_ptid = lp->ptid; |
| 2640 | if (debug_linux_nat) |
| 2641 | fprintf_unfiltered (gdb_stdlog, |
| 2642 | "LLW: trap_ptid is %s.\n", |
| 2643 | target_pid_to_str (trap_ptid)); |
| 2644 | } |
| 2645 | else |
| 2646 | trap_ptid = null_ptid; |
| 2647 | |
| 2648 | if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE) |
| 2649 | { |
| 2650 | *ourstatus = lp->waitstatus; |
| 2651 | lp->waitstatus.kind = TARGET_WAITKIND_IGNORE; |
| 2652 | } |
| 2653 | else |
| 2654 | store_waitstatus (ourstatus, status); |
| 2655 | |
| 2656 | /* Get ready for the next event. */ |
| 2657 | if (target_can_async_p ()) |
| 2658 | target_async (inferior_event_handler, 0); |
| 2659 | |
| 2660 | if (debug_linux_nat_async) |
| 2661 | fprintf_unfiltered (gdb_stdlog, "LLW: exit\n"); |
| 2662 | |
| 2663 | return lp->ptid; |
| 2664 | } |
| 2665 | |
| 2666 | static int |
| 2667 | kill_callback (struct lwp_info *lp, void *data) |
| 2668 | { |
| 2669 | errno = 0; |
| 2670 | ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); |
| 2671 | if (debug_linux_nat) |
| 2672 | fprintf_unfiltered (gdb_stdlog, |
| 2673 | "KC: PTRACE_KILL %s, 0, 0 (%s)\n", |
| 2674 | target_pid_to_str (lp->ptid), |
| 2675 | errno ? safe_strerror (errno) : "OK"); |
| 2676 | |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
| 2680 | static int |
| 2681 | kill_wait_callback (struct lwp_info *lp, void *data) |
| 2682 | { |
| 2683 | pid_t pid; |
| 2684 | |
| 2685 | /* We must make sure that there are no pending events (delayed |
| 2686 | SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current |
| 2687 | program doesn't interfere with any following debugging session. */ |
| 2688 | |
| 2689 | /* For cloned processes we must check both with __WCLONE and |
| 2690 | without, since the exit status of a cloned process isn't reported |
| 2691 | with __WCLONE. */ |
| 2692 | if (lp->cloned) |
| 2693 | { |
| 2694 | do |
| 2695 | { |
| 2696 | pid = my_waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); |
| 2697 | if (pid != (pid_t) -1) |
| 2698 | { |
| 2699 | if (debug_linux_nat) |
| 2700 | fprintf_unfiltered (gdb_stdlog, |
| 2701 | "KWC: wait %s received unknown.\n", |
| 2702 | target_pid_to_str (lp->ptid)); |
| 2703 | /* The Linux kernel sometimes fails to kill a thread |
| 2704 | completely after PTRACE_KILL; that goes from the stop |
| 2705 | point in do_fork out to the one in |
| 2706 | get_signal_to_deliever and waits again. So kill it |
| 2707 | again. */ |
| 2708 | kill_callback (lp, NULL); |
| 2709 | } |
| 2710 | } |
| 2711 | while (pid == GET_LWP (lp->ptid)); |
| 2712 | |
| 2713 | gdb_assert (pid == -1 && errno == ECHILD); |
| 2714 | } |
| 2715 | |
| 2716 | do |
| 2717 | { |
| 2718 | pid = my_waitpid (GET_LWP (lp->ptid), NULL, 0); |
| 2719 | if (pid != (pid_t) -1) |
| 2720 | { |
| 2721 | if (debug_linux_nat) |
| 2722 | fprintf_unfiltered (gdb_stdlog, |
| 2723 | "KWC: wait %s received unk.\n", |
| 2724 | target_pid_to_str (lp->ptid)); |
| 2725 | /* See the call to kill_callback above. */ |
| 2726 | kill_callback (lp, NULL); |
| 2727 | } |
| 2728 | } |
| 2729 | while (pid == GET_LWP (lp->ptid)); |
| 2730 | |
| 2731 | gdb_assert (pid == -1 && errno == ECHILD); |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | static void |
| 2736 | linux_nat_kill (void) |
| 2737 | { |
| 2738 | struct target_waitstatus last; |
| 2739 | ptid_t last_ptid; |
| 2740 | int status; |
| 2741 | |
| 2742 | if (target_can_async_p ()) |
| 2743 | target_async (NULL, 0); |
| 2744 | |
| 2745 | /* If we're stopped while forking and we haven't followed yet, |
| 2746 | kill the other task. We need to do this first because the |
| 2747 | parent will be sleeping if this is a vfork. */ |
| 2748 | |
| 2749 | get_last_target_status (&last_ptid, &last); |
| 2750 | |
| 2751 | if (last.kind == TARGET_WAITKIND_FORKED |
| 2752 | || last.kind == TARGET_WAITKIND_VFORKED) |
| 2753 | { |
| 2754 | ptrace (PT_KILL, last.value.related_pid, 0, 0); |
| 2755 | wait (&status); |
| 2756 | } |
| 2757 | |
| 2758 | if (forks_exist_p ()) |
| 2759 | { |
| 2760 | linux_fork_killall (); |
| 2761 | drain_queued_events (-1); |
| 2762 | } |
| 2763 | else |
| 2764 | { |
| 2765 | /* Kill all LWP's ... */ |
| 2766 | iterate_over_lwps (kill_callback, NULL); |
| 2767 | |
| 2768 | /* ... and wait until we've flushed all events. */ |
| 2769 | iterate_over_lwps (kill_wait_callback, NULL); |
| 2770 | } |
| 2771 | |
| 2772 | target_mourn_inferior (); |
| 2773 | } |
| 2774 | |
| 2775 | static void |
| 2776 | linux_nat_mourn_inferior (void) |
| 2777 | { |
| 2778 | trap_ptid = null_ptid; |
| 2779 | |
| 2780 | /* Destroy LWP info; it's no longer valid. */ |
| 2781 | init_lwp_list (); |
| 2782 | |
| 2783 | if (! forks_exist_p ()) |
| 2784 | { |
| 2785 | /* Normal case, no other forks available. */ |
| 2786 | if (target_can_async_p ()) |
| 2787 | linux_nat_async (NULL, 0); |
| 2788 | linux_ops->to_mourn_inferior (); |
| 2789 | } |
| 2790 | else |
| 2791 | /* Multi-fork case. The current inferior_ptid has exited, but |
| 2792 | there are other viable forks to debug. Delete the exiting |
| 2793 | one and context-switch to the first available. */ |
| 2794 | linux_fork_mourn_inferior (); |
| 2795 | } |
| 2796 | |
| 2797 | static LONGEST |
| 2798 | linux_nat_xfer_partial (struct target_ops *ops, enum target_object object, |
| 2799 | const char *annex, gdb_byte *readbuf, |
| 2800 | const gdb_byte *writebuf, |
| 2801 | ULONGEST offset, LONGEST len) |
| 2802 | { |
| 2803 | struct cleanup *old_chain = save_inferior_ptid (); |
| 2804 | LONGEST xfer; |
| 2805 | |
| 2806 | if (is_lwp (inferior_ptid)) |
| 2807 | inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); |
| 2808 | |
| 2809 | xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf, |
| 2810 | offset, len); |
| 2811 | |
| 2812 | do_cleanups (old_chain); |
| 2813 | return xfer; |
| 2814 | } |
| 2815 | |
| 2816 | static int |
| 2817 | linux_nat_thread_alive (ptid_t ptid) |
| 2818 | { |
| 2819 | gdb_assert (is_lwp (ptid)); |
| 2820 | |
| 2821 | errno = 0; |
| 2822 | ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); |
| 2823 | if (debug_linux_nat) |
| 2824 | fprintf_unfiltered (gdb_stdlog, |
| 2825 | "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n", |
| 2826 | target_pid_to_str (ptid), |
| 2827 | errno ? safe_strerror (errno) : "OK"); |
| 2828 | |
| 2829 | /* Not every Linux kernel implements PTRACE_PEEKUSER. But we can |
| 2830 | handle that case gracefully since ptrace will first do a lookup |
| 2831 | for the process based upon the passed-in pid. If that fails we |
| 2832 | will get either -ESRCH or -EPERM, otherwise the child exists and |
| 2833 | is alive. */ |
| 2834 | if (errno == ESRCH || errno == EPERM) |
| 2835 | return 0; |
| 2836 | |
| 2837 | return 1; |
| 2838 | } |
| 2839 | |
| 2840 | static char * |
| 2841 | linux_nat_pid_to_str (ptid_t ptid) |
| 2842 | { |
| 2843 | static char buf[64]; |
| 2844 | |
| 2845 | if (lwp_list && lwp_list->next && is_lwp (ptid)) |
| 2846 | { |
| 2847 | snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); |
| 2848 | return buf; |
| 2849 | } |
| 2850 | |
| 2851 | return normal_pid_to_str (ptid); |
| 2852 | } |
| 2853 | |
| 2854 | static void |
| 2855 | sigchld_handler (int signo) |
| 2856 | { |
| 2857 | if (linux_nat_async_enabled |
| 2858 | && linux_nat_async_events_enabled |
| 2859 | && signo == SIGCHLD) |
| 2860 | /* It is *always* a bug to hit this. */ |
| 2861 | internal_error (__FILE__, __LINE__, |
| 2862 | "sigchld_handler called when async events are enabled"); |
| 2863 | |
| 2864 | /* Do nothing. The only reason for this handler is that it allows |
| 2865 | us to use sigsuspend in linux_nat_wait above to wait for the |
| 2866 | arrival of a SIGCHLD. */ |
| 2867 | } |
| 2868 | |
| 2869 | /* Accepts an integer PID; Returns a string representing a file that |
| 2870 | can be opened to get the symbols for the child process. */ |
| 2871 | |
| 2872 | static char * |
| 2873 | linux_child_pid_to_exec_file (int pid) |
| 2874 | { |
| 2875 | char *name1, *name2; |
| 2876 | |
| 2877 | name1 = xmalloc (MAXPATHLEN); |
| 2878 | name2 = xmalloc (MAXPATHLEN); |
| 2879 | make_cleanup (xfree, name1); |
| 2880 | make_cleanup (xfree, name2); |
| 2881 | memset (name2, 0, MAXPATHLEN); |
| 2882 | |
| 2883 | sprintf (name1, "/proc/%d/exe", pid); |
| 2884 | if (readlink (name1, name2, MAXPATHLEN) > 0) |
| 2885 | return name2; |
| 2886 | else |
| 2887 | return name1; |
| 2888 | } |
| 2889 | |
| 2890 | /* Service function for corefiles and info proc. */ |
| 2891 | |
| 2892 | static int |
| 2893 | read_mapping (FILE *mapfile, |
| 2894 | long long *addr, |
| 2895 | long long *endaddr, |
| 2896 | char *permissions, |
| 2897 | long long *offset, |
| 2898 | char *device, long long *inode, char *filename) |
| 2899 | { |
| 2900 | int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx", |
| 2901 | addr, endaddr, permissions, offset, device, inode); |
| 2902 | |
| 2903 | filename[0] = '\0'; |
| 2904 | if (ret > 0 && ret != EOF) |
| 2905 | { |
| 2906 | /* Eat everything up to EOL for the filename. This will prevent |
| 2907 | weird filenames (such as one with embedded whitespace) from |
| 2908 | confusing this code. It also makes this code more robust in |
| 2909 | respect to annotations the kernel may add after the filename. |
| 2910 | |
| 2911 | Note the filename is used for informational purposes |
| 2912 | only. */ |
| 2913 | ret += fscanf (mapfile, "%[^\n]\n", filename); |
| 2914 | } |
| 2915 | |
| 2916 | return (ret != 0 && ret != EOF); |
| 2917 | } |
| 2918 | |
| 2919 | /* Fills the "to_find_memory_regions" target vector. Lists the memory |
| 2920 | regions in the inferior for a corefile. */ |
| 2921 | |
| 2922 | static int |
| 2923 | linux_nat_find_memory_regions (int (*func) (CORE_ADDR, |
| 2924 | unsigned long, |
| 2925 | int, int, int, void *), void *obfd) |
| 2926 | { |
| 2927 | long long pid = PIDGET (inferior_ptid); |
| 2928 | char mapsfilename[MAXPATHLEN]; |
| 2929 | FILE *mapsfile; |
| 2930 | long long addr, endaddr, size, offset, inode; |
| 2931 | char permissions[8], device[8], filename[MAXPATHLEN]; |
| 2932 | int read, write, exec; |
| 2933 | int ret; |
| 2934 | |
| 2935 | /* Compose the filename for the /proc memory map, and open it. */ |
| 2936 | sprintf (mapsfilename, "/proc/%lld/maps", pid); |
| 2937 | if ((mapsfile = fopen (mapsfilename, "r")) == NULL) |
| 2938 | error (_("Could not open %s."), mapsfilename); |
| 2939 | |
| 2940 | if (info_verbose) |
| 2941 | fprintf_filtered (gdb_stdout, |
| 2942 | "Reading memory regions from %s\n", mapsfilename); |
| 2943 | |
| 2944 | /* Now iterate until end-of-file. */ |
| 2945 | while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0], |
| 2946 | &offset, &device[0], &inode, &filename[0])) |
| 2947 | { |
| 2948 | size = endaddr - addr; |
| 2949 | |
| 2950 | /* Get the segment's permissions. */ |
| 2951 | read = (strchr (permissions, 'r') != 0); |
| 2952 | write = (strchr (permissions, 'w') != 0); |
| 2953 | exec = (strchr (permissions, 'x') != 0); |
| 2954 | |
| 2955 | if (info_verbose) |
| 2956 | { |
| 2957 | fprintf_filtered (gdb_stdout, |
| 2958 | "Save segment, %lld bytes at 0x%s (%c%c%c)", |
| 2959 | size, paddr_nz (addr), |
| 2960 | read ? 'r' : ' ', |
| 2961 | write ? 'w' : ' ', exec ? 'x' : ' '); |
| 2962 | if (filename[0]) |
| 2963 | fprintf_filtered (gdb_stdout, " for %s", filename); |
| 2964 | fprintf_filtered (gdb_stdout, "\n"); |
| 2965 | } |
| 2966 | |
| 2967 | /* Invoke the callback function to create the corefile |
| 2968 | segment. */ |
| 2969 | func (addr, size, read, write, exec, obfd); |
| 2970 | } |
| 2971 | fclose (mapsfile); |
| 2972 | return 0; |
| 2973 | } |
| 2974 | |
| 2975 | /* Records the thread's register state for the corefile note |
| 2976 | section. */ |
| 2977 | |
| 2978 | static char * |
| 2979 | linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid, |
| 2980 | char *note_data, int *note_size) |
| 2981 | { |
| 2982 | gdb_gregset_t gregs; |
| 2983 | gdb_fpregset_t fpregs; |
| 2984 | #ifdef FILL_FPXREGSET |
| 2985 | gdb_fpxregset_t fpxregs; |
| 2986 | #endif |
| 2987 | unsigned long lwp = ptid_get_lwp (ptid); |
| 2988 | struct regcache *regcache = get_thread_regcache (ptid); |
| 2989 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 2990 | const struct regset *regset; |
| 2991 | int core_regset_p; |
| 2992 | struct cleanup *old_chain; |
| 2993 | |
| 2994 | old_chain = save_inferior_ptid (); |
| 2995 | inferior_ptid = ptid; |
| 2996 | target_fetch_registers (regcache, -1); |
| 2997 | do_cleanups (old_chain); |
| 2998 | |
| 2999 | core_regset_p = gdbarch_regset_from_core_section_p (gdbarch); |
| 3000 | if (core_regset_p |
| 3001 | && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg", |
| 3002 | sizeof (gregs))) != NULL |
| 3003 | && regset->collect_regset != NULL) |
| 3004 | regset->collect_regset (regset, regcache, -1, |
| 3005 | &gregs, sizeof (gregs)); |
| 3006 | else |
| 3007 | fill_gregset (regcache, &gregs, -1); |
| 3008 | |
| 3009 | note_data = (char *) elfcore_write_prstatus (obfd, |
| 3010 | note_data, |
| 3011 | note_size, |
| 3012 | lwp, |
| 3013 | stop_signal, &gregs); |
| 3014 | |
| 3015 | if (core_regset_p |
| 3016 | && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", |
| 3017 | sizeof (fpregs))) != NULL |
| 3018 | && regset->collect_regset != NULL) |
| 3019 | regset->collect_regset (regset, regcache, -1, |
| 3020 | &fpregs, sizeof (fpregs)); |
| 3021 | else |
| 3022 | fill_fpregset (regcache, &fpregs, -1); |
| 3023 | |
| 3024 | note_data = (char *) elfcore_write_prfpreg (obfd, |
| 3025 | note_data, |
| 3026 | note_size, |
| 3027 | &fpregs, sizeof (fpregs)); |
| 3028 | |
| 3029 | #ifdef FILL_FPXREGSET |
| 3030 | if (core_regset_p |
| 3031 | && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg-xfp", |
| 3032 | sizeof (fpxregs))) != NULL |
| 3033 | && regset->collect_regset != NULL) |
| 3034 | regset->collect_regset (regset, regcache, -1, |
| 3035 | &fpxregs, sizeof (fpxregs)); |
| 3036 | else |
| 3037 | fill_fpxregset (regcache, &fpxregs, -1); |
| 3038 | |
| 3039 | note_data = (char *) elfcore_write_prxfpreg (obfd, |
| 3040 | note_data, |
| 3041 | note_size, |
| 3042 | &fpxregs, sizeof (fpxregs)); |
| 3043 | #endif |
| 3044 | return note_data; |
| 3045 | } |
| 3046 | |
| 3047 | struct linux_nat_corefile_thread_data |
| 3048 | { |
| 3049 | bfd *obfd; |
| 3050 | char *note_data; |
| 3051 | int *note_size; |
| 3052 | int num_notes; |
| 3053 | }; |
| 3054 | |
| 3055 | /* Called by gdbthread.c once per thread. Records the thread's |
| 3056 | register state for the corefile note section. */ |
| 3057 | |
| 3058 | static int |
| 3059 | linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data) |
| 3060 | { |
| 3061 | struct linux_nat_corefile_thread_data *args = data; |
| 3062 | |
| 3063 | args->note_data = linux_nat_do_thread_registers (args->obfd, |
| 3064 | ti->ptid, |
| 3065 | args->note_data, |
| 3066 | args->note_size); |
| 3067 | args->num_notes++; |
| 3068 | |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | /* Records the register state for the corefile note section. */ |
| 3073 | |
| 3074 | static char * |
| 3075 | linux_nat_do_registers (bfd *obfd, ptid_t ptid, |
| 3076 | char *note_data, int *note_size) |
| 3077 | { |
| 3078 | return linux_nat_do_thread_registers (obfd, |
| 3079 | ptid_build (ptid_get_pid (inferior_ptid), |
| 3080 | ptid_get_pid (inferior_ptid), |
| 3081 | 0), |
| 3082 | note_data, note_size); |
| 3083 | } |
| 3084 | |
| 3085 | /* Fills the "to_make_corefile_note" target vector. Builds the note |
| 3086 | section for a corefile, and returns it in a malloc buffer. */ |
| 3087 | |
| 3088 | static char * |
| 3089 | linux_nat_make_corefile_notes (bfd *obfd, int *note_size) |
| 3090 | { |
| 3091 | struct linux_nat_corefile_thread_data thread_args; |
| 3092 | struct cleanup *old_chain; |
| 3093 | /* The variable size must be >= sizeof (prpsinfo_t.pr_fname). */ |
| 3094 | char fname[16] = { '\0' }; |
| 3095 | /* The variable size must be >= sizeof (prpsinfo_t.pr_psargs). */ |
| 3096 | char psargs[80] = { '\0' }; |
| 3097 | char *note_data = NULL; |
| 3098 | ptid_t current_ptid = inferior_ptid; |
| 3099 | gdb_byte *auxv; |
| 3100 | int auxv_len; |
| 3101 | |
| 3102 | if (get_exec_file (0)) |
| 3103 | { |
| 3104 | strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); |
| 3105 | strncpy (psargs, get_exec_file (0), sizeof (psargs)); |
| 3106 | if (get_inferior_args ()) |
| 3107 | { |
| 3108 | char *string_end; |
| 3109 | char *psargs_end = psargs + sizeof (psargs); |
| 3110 | |
| 3111 | /* linux_elfcore_write_prpsinfo () handles zero unterminated |
| 3112 | strings fine. */ |
| 3113 | string_end = memchr (psargs, 0, sizeof (psargs)); |
| 3114 | if (string_end != NULL) |
| 3115 | { |
| 3116 | *string_end++ = ' '; |
| 3117 | strncpy (string_end, get_inferior_args (), |
| 3118 | psargs_end - string_end); |
| 3119 | } |
| 3120 | } |
| 3121 | note_data = (char *) elfcore_write_prpsinfo (obfd, |
| 3122 | note_data, |
| 3123 | note_size, fname, psargs); |
| 3124 | } |
| 3125 | |
| 3126 | /* Dump information for threads. */ |
| 3127 | thread_args.obfd = obfd; |
| 3128 | thread_args.note_data = note_data; |
| 3129 | thread_args.note_size = note_size; |
| 3130 | thread_args.num_notes = 0; |
| 3131 | iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args); |
| 3132 | if (thread_args.num_notes == 0) |
| 3133 | { |
| 3134 | /* iterate_over_threads didn't come up with any threads; just |
| 3135 | use inferior_ptid. */ |
| 3136 | note_data = linux_nat_do_registers (obfd, inferior_ptid, |
| 3137 | note_data, note_size); |
| 3138 | } |
| 3139 | else |
| 3140 | { |
| 3141 | note_data = thread_args.note_data; |
| 3142 | } |
| 3143 | |
| 3144 | auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV, |
| 3145 | NULL, &auxv); |
| 3146 | if (auxv_len > 0) |
| 3147 | { |
| 3148 | note_data = elfcore_write_note (obfd, note_data, note_size, |
| 3149 | "CORE", NT_AUXV, auxv, auxv_len); |
| 3150 | xfree (auxv); |
| 3151 | } |
| 3152 | |
| 3153 | make_cleanup (xfree, note_data); |
| 3154 | return note_data; |
| 3155 | } |
| 3156 | |
| 3157 | /* Implement the "info proc" command. */ |
| 3158 | |
| 3159 | static void |
| 3160 | linux_nat_info_proc_cmd (char *args, int from_tty) |
| 3161 | { |
| 3162 | long long pid = PIDGET (inferior_ptid); |
| 3163 | FILE *procfile; |
| 3164 | char **argv = NULL; |
| 3165 | char buffer[MAXPATHLEN]; |
| 3166 | char fname1[MAXPATHLEN], fname2[MAXPATHLEN]; |
| 3167 | int cmdline_f = 1; |
| 3168 | int cwd_f = 1; |
| 3169 | int exe_f = 1; |
| 3170 | int mappings_f = 0; |
| 3171 | int environ_f = 0; |
| 3172 | int status_f = 0; |
| 3173 | int stat_f = 0; |
| 3174 | int all = 0; |
| 3175 | struct stat dummy; |
| 3176 | |
| 3177 | if (args) |
| 3178 | { |
| 3179 | /* Break up 'args' into an argv array. */ |
| 3180 | if ((argv = buildargv (args)) == NULL) |
| 3181 | nomem (0); |
| 3182 | else |
| 3183 | make_cleanup_freeargv (argv); |
| 3184 | } |
| 3185 | while (argv != NULL && *argv != NULL) |
| 3186 | { |
| 3187 | if (isdigit (argv[0][0])) |
| 3188 | { |
| 3189 | pid = strtoul (argv[0], NULL, 10); |
| 3190 | } |
| 3191 | else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0) |
| 3192 | { |
| 3193 | mappings_f = 1; |
| 3194 | } |
| 3195 | else if (strcmp (argv[0], "status") == 0) |
| 3196 | { |
| 3197 | status_f = 1; |
| 3198 | } |
| 3199 | else if (strcmp (argv[0], "stat") == 0) |
| 3200 | { |
| 3201 | stat_f = 1; |
| 3202 | } |
| 3203 | else if (strcmp (argv[0], "cmd") == 0) |
| 3204 | { |
| 3205 | cmdline_f = 1; |
| 3206 | } |
| 3207 | else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0) |
| 3208 | { |
| 3209 | exe_f = 1; |
| 3210 | } |
| 3211 | else if (strcmp (argv[0], "cwd") == 0) |
| 3212 | { |
| 3213 | cwd_f = 1; |
| 3214 | } |
| 3215 | else if (strncmp (argv[0], "all", strlen (argv[0])) == 0) |
| 3216 | { |
| 3217 | all = 1; |
| 3218 | } |
| 3219 | else |
| 3220 | { |
| 3221 | /* [...] (future options here) */ |
| 3222 | } |
| 3223 | argv++; |
| 3224 | } |
| 3225 | if (pid == 0) |
| 3226 | error (_("No current process: you must name one.")); |
| 3227 | |
| 3228 | sprintf (fname1, "/proc/%lld", pid); |
| 3229 | if (stat (fname1, &dummy) != 0) |
| 3230 | error (_("No /proc directory: '%s'"), fname1); |
| 3231 | |
| 3232 | printf_filtered (_("process %lld\n"), pid); |
| 3233 | if (cmdline_f || all) |
| 3234 | { |
| 3235 | sprintf (fname1, "/proc/%lld/cmdline", pid); |
| 3236 | if ((procfile = fopen (fname1, "r")) != NULL) |
| 3237 | { |
| 3238 | fgets (buffer, sizeof (buffer), procfile); |
| 3239 | printf_filtered ("cmdline = '%s'\n", buffer); |
| 3240 | fclose (procfile); |
| 3241 | } |
| 3242 | else |
| 3243 | warning (_("unable to open /proc file '%s'"), fname1); |
| 3244 | } |
| 3245 | if (cwd_f || all) |
| 3246 | { |
| 3247 | sprintf (fname1, "/proc/%lld/cwd", pid); |
| 3248 | memset (fname2, 0, sizeof (fname2)); |
| 3249 | if (readlink (fname1, fname2, sizeof (fname2)) > 0) |
| 3250 | printf_filtered ("cwd = '%s'\n", fname2); |
| 3251 | else |
| 3252 | warning (_("unable to read link '%s'"), fname1); |
| 3253 | } |
| 3254 | if (exe_f || all) |
| 3255 | { |
| 3256 | sprintf (fname1, "/proc/%lld/exe", pid); |
| 3257 | memset (fname2, 0, sizeof (fname2)); |
| 3258 | if (readlink (fname1, fname2, sizeof (fname2)) > 0) |
| 3259 | printf_filtered ("exe = '%s'\n", fname2); |
| 3260 | else |
| 3261 | warning (_("unable to read link '%s'"), fname1); |
| 3262 | } |
| 3263 | if (mappings_f || all) |
| 3264 | { |
| 3265 | sprintf (fname1, "/proc/%lld/maps", pid); |
| 3266 | if ((procfile = fopen (fname1, "r")) != NULL) |
| 3267 | { |
| 3268 | long long addr, endaddr, size, offset, inode; |
| 3269 | char permissions[8], device[8], filename[MAXPATHLEN]; |
| 3270 | |
| 3271 | printf_filtered (_("Mapped address spaces:\n\n")); |
| 3272 | if (gdbarch_addr_bit (current_gdbarch) == 32) |
| 3273 | { |
| 3274 | printf_filtered ("\t%10s %10s %10s %10s %7s\n", |
| 3275 | "Start Addr", |
| 3276 | " End Addr", |
| 3277 | " Size", " Offset", "objfile"); |
| 3278 | } |
| 3279 | else |
| 3280 | { |
| 3281 | printf_filtered (" %18s %18s %10s %10s %7s\n", |
| 3282 | "Start Addr", |
| 3283 | " End Addr", |
| 3284 | " Size", " Offset", "objfile"); |
| 3285 | } |
| 3286 | |
| 3287 | while (read_mapping (procfile, &addr, &endaddr, &permissions[0], |
| 3288 | &offset, &device[0], &inode, &filename[0])) |
| 3289 | { |
| 3290 | size = endaddr - addr; |
| 3291 | |
| 3292 | /* FIXME: carlton/2003-08-27: Maybe the printf_filtered |
| 3293 | calls here (and possibly above) should be abstracted |
| 3294 | out into their own functions? Andrew suggests using |
| 3295 | a generic local_address_string instead to print out |
| 3296 | the addresses; that makes sense to me, too. */ |
| 3297 | |
| 3298 | if (gdbarch_addr_bit (current_gdbarch) == 32) |
| 3299 | { |
| 3300 | printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n", |
| 3301 | (unsigned long) addr, /* FIXME: pr_addr */ |
| 3302 | (unsigned long) endaddr, |
| 3303 | (int) size, |
| 3304 | (unsigned int) offset, |
| 3305 | filename[0] ? filename : ""); |
| 3306 | } |
| 3307 | else |
| 3308 | { |
| 3309 | printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n", |
| 3310 | (unsigned long) addr, /* FIXME: pr_addr */ |
| 3311 | (unsigned long) endaddr, |
| 3312 | (int) size, |
| 3313 | (unsigned int) offset, |
| 3314 | filename[0] ? filename : ""); |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | fclose (procfile); |
| 3319 | } |
| 3320 | else |
| 3321 | warning (_("unable to open /proc file '%s'"), fname1); |
| 3322 | } |
| 3323 | if (status_f || all) |
| 3324 | { |
| 3325 | sprintf (fname1, "/proc/%lld/status", pid); |
| 3326 | if ((procfile = fopen (fname1, "r")) != NULL) |
| 3327 | { |
| 3328 | while (fgets (buffer, sizeof (buffer), procfile) != NULL) |
| 3329 | puts_filtered (buffer); |
| 3330 | fclose (procfile); |
| 3331 | } |
| 3332 | else |
| 3333 | warning (_("unable to open /proc file '%s'"), fname1); |
| 3334 | } |
| 3335 | if (stat_f || all) |
| 3336 | { |
| 3337 | sprintf (fname1, "/proc/%lld/stat", pid); |
| 3338 | if ((procfile = fopen (fname1, "r")) != NULL) |
| 3339 | { |
| 3340 | int itmp; |
| 3341 | char ctmp; |
| 3342 | long ltmp; |
| 3343 | |
| 3344 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3345 | printf_filtered (_("Process: %d\n"), itmp); |
| 3346 | if (fscanf (procfile, "(%[^)]) ", &buffer[0]) > 0) |
| 3347 | printf_filtered (_("Exec file: %s\n"), buffer); |
| 3348 | if (fscanf (procfile, "%c ", &ctmp) > 0) |
| 3349 | printf_filtered (_("State: %c\n"), ctmp); |
| 3350 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3351 | printf_filtered (_("Parent process: %d\n"), itmp); |
| 3352 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3353 | printf_filtered (_("Process group: %d\n"), itmp); |
| 3354 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3355 | printf_filtered (_("Session id: %d\n"), itmp); |
| 3356 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3357 | printf_filtered (_("TTY: %d\n"), itmp); |
| 3358 | if (fscanf (procfile, "%d ", &itmp) > 0) |
| 3359 | printf_filtered (_("TTY owner process group: %d\n"), itmp); |
| 3360 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3361 | printf_filtered (_("Flags: 0x%lx\n"), ltmp); |
| 3362 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3363 | printf_filtered (_("Minor faults (no memory page): %lu\n"), |
| 3364 | (unsigned long) ltmp); |
| 3365 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3366 | printf_filtered (_("Minor faults, children: %lu\n"), |
| 3367 | (unsigned long) ltmp); |
| 3368 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3369 | printf_filtered (_("Major faults (memory page faults): %lu\n"), |
| 3370 | (unsigned long) ltmp); |
| 3371 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3372 | printf_filtered (_("Major faults, children: %lu\n"), |
| 3373 | (unsigned long) ltmp); |
| 3374 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3375 | printf_filtered (_("utime: %ld\n"), ltmp); |
| 3376 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3377 | printf_filtered (_("stime: %ld\n"), ltmp); |
| 3378 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3379 | printf_filtered (_("utime, children: %ld\n"), ltmp); |
| 3380 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3381 | printf_filtered (_("stime, children: %ld\n"), ltmp); |
| 3382 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3383 | printf_filtered (_("jiffies remaining in current time slice: %ld\n"), |
| 3384 | ltmp); |
| 3385 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3386 | printf_filtered (_("'nice' value: %ld\n"), ltmp); |
| 3387 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3388 | printf_filtered (_("jiffies until next timeout: %lu\n"), |
| 3389 | (unsigned long) ltmp); |
| 3390 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3391 | printf_filtered (_("jiffies until next SIGALRM: %lu\n"), |
| 3392 | (unsigned long) ltmp); |
| 3393 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3394 | printf_filtered (_("start time (jiffies since system boot): %ld\n"), |
| 3395 | ltmp); |
| 3396 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3397 | printf_filtered (_("Virtual memory size: %lu\n"), |
| 3398 | (unsigned long) ltmp); |
| 3399 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3400 | printf_filtered (_("Resident set size: %lu\n"), (unsigned long) ltmp); |
| 3401 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3402 | printf_filtered (_("rlim: %lu\n"), (unsigned long) ltmp); |
| 3403 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3404 | printf_filtered (_("Start of text: 0x%lx\n"), ltmp); |
| 3405 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3406 | printf_filtered (_("End of text: 0x%lx\n"), ltmp); |
| 3407 | if (fscanf (procfile, "%lu ", <mp) > 0) |
| 3408 | printf_filtered (_("Start of stack: 0x%lx\n"), ltmp); |
| 3409 | #if 0 /* Don't know how architecture-dependent the rest is... |
| 3410 | Anyway the signal bitmap info is available from "status". */ |
| 3411 | if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */ |
| 3412 | printf_filtered (_("Kernel stack pointer: 0x%lx\n"), ltmp); |
| 3413 | if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */ |
| 3414 | printf_filtered (_("Kernel instr pointer: 0x%lx\n"), ltmp); |
| 3415 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3416 | printf_filtered (_("Pending signals bitmap: 0x%lx\n"), ltmp); |
| 3417 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3418 | printf_filtered (_("Blocked signals bitmap: 0x%lx\n"), ltmp); |
| 3419 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3420 | printf_filtered (_("Ignored signals bitmap: 0x%lx\n"), ltmp); |
| 3421 | if (fscanf (procfile, "%ld ", <mp) > 0) |
| 3422 | printf_filtered (_("Catched signals bitmap: 0x%lx\n"), ltmp); |
| 3423 | if (fscanf (procfile, "%lu ", <mp) > 0) /* FIXME arch? */ |
| 3424 | printf_filtered (_("wchan (system call): 0x%lx\n"), ltmp); |
| 3425 | #endif |
| 3426 | fclose (procfile); |
| 3427 | } |
| 3428 | else |
| 3429 | warning (_("unable to open /proc file '%s'"), fname1); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | /* Implement the to_xfer_partial interface for memory reads using the /proc |
| 3434 | filesystem. Because we can use a single read() call for /proc, this |
| 3435 | can be much more efficient than banging away at PTRACE_PEEKTEXT, |
| 3436 | but it doesn't support writes. */ |
| 3437 | |
| 3438 | static LONGEST |
| 3439 | linux_proc_xfer_partial (struct target_ops *ops, enum target_object object, |
| 3440 | const char *annex, gdb_byte *readbuf, |
| 3441 | const gdb_byte *writebuf, |
| 3442 | ULONGEST offset, LONGEST len) |
| 3443 | { |
| 3444 | LONGEST ret; |
| 3445 | int fd; |
| 3446 | char filename[64]; |
| 3447 | |
| 3448 | if (object != TARGET_OBJECT_MEMORY || !readbuf) |
| 3449 | return 0; |
| 3450 | |
| 3451 | /* Don't bother for one word. */ |
| 3452 | if (len < 3 * sizeof (long)) |
| 3453 | return 0; |
| 3454 | |
| 3455 | /* We could keep this file open and cache it - possibly one per |
| 3456 | thread. That requires some juggling, but is even faster. */ |
| 3457 | sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid)); |
| 3458 | fd = open (filename, O_RDONLY | O_LARGEFILE); |
| 3459 | if (fd == -1) |
| 3460 | return 0; |
| 3461 | |
| 3462 | /* If pread64 is available, use it. It's faster if the kernel |
| 3463 | supports it (only one syscall), and it's 64-bit safe even on |
| 3464 | 32-bit platforms (for instance, SPARC debugging a SPARC64 |
| 3465 | application). */ |
| 3466 | #ifdef HAVE_PREAD64 |
| 3467 | if (pread64 (fd, readbuf, len, offset) != len) |
| 3468 | #else |
| 3469 | if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len) |
| 3470 | #endif |
| 3471 | ret = 0; |
| 3472 | else |
| 3473 | ret = len; |
| 3474 | |
| 3475 | close (fd); |
| 3476 | return ret; |
| 3477 | } |
| 3478 | |
| 3479 | /* Parse LINE as a signal set and add its set bits to SIGS. */ |
| 3480 | |
| 3481 | static void |
| 3482 | add_line_to_sigset (const char *line, sigset_t *sigs) |
| 3483 | { |
| 3484 | int len = strlen (line) - 1; |
| 3485 | const char *p; |
| 3486 | int signum; |
| 3487 | |
| 3488 | if (line[len] != '\n') |
| 3489 | error (_("Could not parse signal set: %s"), line); |
| 3490 | |
| 3491 | p = line; |
| 3492 | signum = len * 4; |
| 3493 | while (len-- > 0) |
| 3494 | { |
| 3495 | int digit; |
| 3496 | |
| 3497 | if (*p >= '0' && *p <= '9') |
| 3498 | digit = *p - '0'; |
| 3499 | else if (*p >= 'a' && *p <= 'f') |
| 3500 | digit = *p - 'a' + 10; |
| 3501 | else |
| 3502 | error (_("Could not parse signal set: %s"), line); |
| 3503 | |
| 3504 | signum -= 4; |
| 3505 | |
| 3506 | if (digit & 1) |
| 3507 | sigaddset (sigs, signum + 1); |
| 3508 | if (digit & 2) |
| 3509 | sigaddset (sigs, signum + 2); |
| 3510 | if (digit & 4) |
| 3511 | sigaddset (sigs, signum + 3); |
| 3512 | if (digit & 8) |
| 3513 | sigaddset (sigs, signum + 4); |
| 3514 | |
| 3515 | p++; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | /* Find process PID's pending signals from /proc/pid/status and set |
| 3520 | SIGS to match. */ |
| 3521 | |
| 3522 | void |
| 3523 | linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored) |
| 3524 | { |
| 3525 | FILE *procfile; |
| 3526 | char buffer[MAXPATHLEN], fname[MAXPATHLEN]; |
| 3527 | int signum; |
| 3528 | |
| 3529 | sigemptyset (pending); |
| 3530 | sigemptyset (blocked); |
| 3531 | sigemptyset (ignored); |
| 3532 | sprintf (fname, "/proc/%d/status", pid); |
| 3533 | procfile = fopen (fname, "r"); |
| 3534 | if (procfile == NULL) |
| 3535 | error (_("Could not open %s"), fname); |
| 3536 | |
| 3537 | while (fgets (buffer, MAXPATHLEN, procfile) != NULL) |
| 3538 | { |
| 3539 | /* Normal queued signals are on the SigPnd line in the status |
| 3540 | file. However, 2.6 kernels also have a "shared" pending |
| 3541 | queue for delivering signals to a thread group, so check for |
| 3542 | a ShdPnd line also. |
| 3543 | |
| 3544 | Unfortunately some Red Hat kernels include the shared pending |
| 3545 | queue but not the ShdPnd status field. */ |
| 3546 | |
| 3547 | if (strncmp (buffer, "SigPnd:\t", 8) == 0) |
| 3548 | add_line_to_sigset (buffer + 8, pending); |
| 3549 | else if (strncmp (buffer, "ShdPnd:\t", 8) == 0) |
| 3550 | add_line_to_sigset (buffer + 8, pending); |
| 3551 | else if (strncmp (buffer, "SigBlk:\t", 8) == 0) |
| 3552 | add_line_to_sigset (buffer + 8, blocked); |
| 3553 | else if (strncmp (buffer, "SigIgn:\t", 8) == 0) |
| 3554 | add_line_to_sigset (buffer + 8, ignored); |
| 3555 | } |
| 3556 | |
| 3557 | fclose (procfile); |
| 3558 | } |
| 3559 | |
| 3560 | static LONGEST |
| 3561 | linux_xfer_partial (struct target_ops *ops, enum target_object object, |
| 3562 | const char *annex, gdb_byte *readbuf, |
| 3563 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) |
| 3564 | { |
| 3565 | LONGEST xfer; |
| 3566 | |
| 3567 | if (object == TARGET_OBJECT_AUXV) |
| 3568 | return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf, |
| 3569 | offset, len); |
| 3570 | |
| 3571 | xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf, |
| 3572 | offset, len); |
| 3573 | if (xfer != 0) |
| 3574 | return xfer; |
| 3575 | |
| 3576 | return super_xfer_partial (ops, object, annex, readbuf, writebuf, |
| 3577 | offset, len); |
| 3578 | } |
| 3579 | |
| 3580 | /* Create a prototype generic GNU/Linux target. The client can override |
| 3581 | it with local methods. */ |
| 3582 | |
| 3583 | static void |
| 3584 | linux_target_install_ops (struct target_ops *t) |
| 3585 | { |
| 3586 | t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint; |
| 3587 | t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint; |
| 3588 | t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint; |
| 3589 | t->to_pid_to_exec_file = linux_child_pid_to_exec_file; |
| 3590 | t->to_post_startup_inferior = linux_child_post_startup_inferior; |
| 3591 | t->to_post_attach = linux_child_post_attach; |
| 3592 | t->to_follow_fork = linux_child_follow_fork; |
| 3593 | t->to_find_memory_regions = linux_nat_find_memory_regions; |
| 3594 | t->to_make_corefile_notes = linux_nat_make_corefile_notes; |
| 3595 | |
| 3596 | super_xfer_partial = t->to_xfer_partial; |
| 3597 | t->to_xfer_partial = linux_xfer_partial; |
| 3598 | } |
| 3599 | |
| 3600 | struct target_ops * |
| 3601 | linux_target (void) |
| 3602 | { |
| 3603 | struct target_ops *t; |
| 3604 | |
| 3605 | t = inf_ptrace_target (); |
| 3606 | linux_target_install_ops (t); |
| 3607 | |
| 3608 | return t; |
| 3609 | } |
| 3610 | |
| 3611 | struct target_ops * |
| 3612 | linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int)) |
| 3613 | { |
| 3614 | struct target_ops *t; |
| 3615 | |
| 3616 | t = inf_ptrace_trad_target (register_u_offset); |
| 3617 | linux_target_install_ops (t); |
| 3618 | |
| 3619 | return t; |
| 3620 | } |
| 3621 | |
| 3622 | /* Controls if async mode is permitted. */ |
| 3623 | static int linux_async_permitted = 0; |
| 3624 | |
| 3625 | /* The set command writes to this variable. If the inferior is |
| 3626 | executing, linux_nat_async_permitted is *not* updated. */ |
| 3627 | static int linux_async_permitted_1 = 0; |
| 3628 | |
| 3629 | static void |
| 3630 | set_maintenance_linux_async_permitted (char *args, int from_tty, |
| 3631 | struct cmd_list_element *c) |
| 3632 | { |
| 3633 | if (target_has_execution) |
| 3634 | { |
| 3635 | linux_async_permitted_1 = linux_async_permitted; |
| 3636 | error (_("Cannot change this setting while the inferior is running.")); |
| 3637 | } |
| 3638 | |
| 3639 | linux_async_permitted = linux_async_permitted_1; |
| 3640 | linux_nat_set_async_mode (linux_async_permitted); |
| 3641 | } |
| 3642 | |
| 3643 | static void |
| 3644 | show_maintenance_linux_async_permitted (struct ui_file *file, int from_tty, |
| 3645 | struct cmd_list_element *c, const char *value) |
| 3646 | { |
| 3647 | fprintf_filtered (file, _("\ |
| 3648 | Controlling the GNU/Linux inferior in asynchronous mode is %s.\n"), |
| 3649 | value); |
| 3650 | } |
| 3651 | |
| 3652 | /* target_is_async_p implementation. */ |
| 3653 | |
| 3654 | static int |
| 3655 | linux_nat_is_async_p (void) |
| 3656 | { |
| 3657 | /* NOTE: palves 2008-03-21: We're only async when the user requests |
| 3658 | it explicitly with the "maintenance set linux-async" command. |
| 3659 | Someday, linux will always be async. */ |
| 3660 | if (!linux_async_permitted) |
| 3661 | return 0; |
| 3662 | |
| 3663 | return 1; |
| 3664 | } |
| 3665 | |
| 3666 | /* target_can_async_p implementation. */ |
| 3667 | |
| 3668 | static int |
| 3669 | linux_nat_can_async_p (void) |
| 3670 | { |
| 3671 | /* NOTE: palves 2008-03-21: We're only async when the user requests |
| 3672 | it explicitly with the "maintenance set linux-async" command. |
| 3673 | Someday, linux will always be async. */ |
| 3674 | if (!linux_async_permitted) |
| 3675 | return 0; |
| 3676 | |
| 3677 | /* See target.h/target_async_mask. */ |
| 3678 | return linux_nat_async_mask_value; |
| 3679 | } |
| 3680 | |
| 3681 | /* target_async_mask implementation. */ |
| 3682 | |
| 3683 | static int |
| 3684 | linux_nat_async_mask (int mask) |
| 3685 | { |
| 3686 | int current_state; |
| 3687 | current_state = linux_nat_async_mask_value; |
| 3688 | |
| 3689 | if (current_state != mask) |
| 3690 | { |
| 3691 | if (mask == 0) |
| 3692 | { |
| 3693 | linux_nat_async (NULL, 0); |
| 3694 | linux_nat_async_mask_value = mask; |
| 3695 | /* We're in sync mode. Make sure SIGCHLD isn't handled by |
| 3696 | async_sigchld_handler when we come out of sigsuspend in |
| 3697 | linux_nat_wait. */ |
| 3698 | sigaction (SIGCHLD, &sync_sigchld_action, NULL); |
| 3699 | } |
| 3700 | else |
| 3701 | { |
| 3702 | /* Restore the async handler. */ |
| 3703 | sigaction (SIGCHLD, &async_sigchld_action, NULL); |
| 3704 | linux_nat_async_mask_value = mask; |
| 3705 | linux_nat_async (inferior_event_handler, 0); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | return current_state; |
| 3710 | } |
| 3711 | |
| 3712 | /* Pop an event from the event pipe. */ |
| 3713 | |
| 3714 | static int |
| 3715 | linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options) |
| 3716 | { |
| 3717 | struct waitpid_result event = {0}; |
| 3718 | int ret; |
| 3719 | |
| 3720 | do |
| 3721 | { |
| 3722 | ret = read (linux_nat_event_pipe[0], &event, sizeof (event)); |
| 3723 | } |
| 3724 | while (ret == -1 && errno == EINTR); |
| 3725 | |
| 3726 | gdb_assert (ret == sizeof (event)); |
| 3727 | |
| 3728 | *ptr_status = event.status; |
| 3729 | *ptr_options = event.options; |
| 3730 | |
| 3731 | linux_nat_num_queued_events--; |
| 3732 | |
| 3733 | return event.pid; |
| 3734 | } |
| 3735 | |
| 3736 | /* Push an event into the event pipe. */ |
| 3737 | |
| 3738 | static void |
| 3739 | linux_nat_event_pipe_push (int pid, int status, int options) |
| 3740 | { |
| 3741 | int ret; |
| 3742 | struct waitpid_result event = {0}; |
| 3743 | event.pid = pid; |
| 3744 | event.status = status; |
| 3745 | event.options = options; |
| 3746 | |
| 3747 | do |
| 3748 | { |
| 3749 | ret = write (linux_nat_event_pipe[1], &event, sizeof (event)); |
| 3750 | gdb_assert ((ret == -1 && errno == EINTR) || ret == sizeof (event)); |
| 3751 | } while (ret == -1 && errno == EINTR); |
| 3752 | |
| 3753 | linux_nat_num_queued_events++; |
| 3754 | } |
| 3755 | |
| 3756 | static void |
| 3757 | get_pending_events (void) |
| 3758 | { |
| 3759 | int status, options, pid; |
| 3760 | |
| 3761 | if (!linux_nat_async_enabled || !linux_nat_async_events_enabled) |
| 3762 | internal_error (__FILE__, __LINE__, |
| 3763 | "get_pending_events called with async masked"); |
| 3764 | |
| 3765 | while (1) |
| 3766 | { |
| 3767 | status = 0; |
| 3768 | options = __WCLONE | WNOHANG; |
| 3769 | |
| 3770 | do |
| 3771 | { |
| 3772 | pid = waitpid (-1, &status, options); |
| 3773 | } |
| 3774 | while (pid == -1 && errno == EINTR); |
| 3775 | |
| 3776 | if (pid <= 0) |
| 3777 | { |
| 3778 | options = WNOHANG; |
| 3779 | do |
| 3780 | { |
| 3781 | pid = waitpid (-1, &status, options); |
| 3782 | } |
| 3783 | while (pid == -1 && errno == EINTR); |
| 3784 | } |
| 3785 | |
| 3786 | if (pid <= 0) |
| 3787 | /* No more children reporting events. */ |
| 3788 | break; |
| 3789 | |
| 3790 | if (debug_linux_nat_async) |
| 3791 | fprintf_unfiltered (gdb_stdlog, "\ |
| 3792 | get_pending_events: pid(%d), status(%x), options (%x)\n", |
| 3793 | pid, status, options); |
| 3794 | |
| 3795 | linux_nat_event_pipe_push (pid, status, options); |
| 3796 | } |
| 3797 | |
| 3798 | if (debug_linux_nat_async) |
| 3799 | fprintf_unfiltered (gdb_stdlog, "\ |
| 3800 | get_pending_events: linux_nat_num_queued_events(%d)\n", |
| 3801 | linux_nat_num_queued_events); |
| 3802 | } |
| 3803 | |
| 3804 | /* SIGCHLD handler for async mode. */ |
| 3805 | |
| 3806 | static void |
| 3807 | async_sigchld_handler (int signo) |
| 3808 | { |
| 3809 | if (debug_linux_nat_async) |
| 3810 | fprintf_unfiltered (gdb_stdlog, "async_sigchld_handler\n"); |
| 3811 | |
| 3812 | get_pending_events (); |
| 3813 | } |
| 3814 | |
| 3815 | /* Enable or disable async SIGCHLD handling. */ |
| 3816 | |
| 3817 | static int |
| 3818 | linux_nat_async_events (int enable) |
| 3819 | { |
| 3820 | int current_state = linux_nat_async_events_enabled; |
| 3821 | |
| 3822 | if (debug_linux_nat_async) |
| 3823 | fprintf_unfiltered (gdb_stdlog, |
| 3824 | "LNAE: enable(%d): linux_nat_async_events_enabled(%d), " |
| 3825 | "linux_nat_num_queued_events(%d)\n", |
| 3826 | enable, linux_nat_async_events_enabled, |
| 3827 | linux_nat_num_queued_events); |
| 3828 | |
| 3829 | if (current_state != enable) |
| 3830 | { |
| 3831 | sigset_t mask; |
| 3832 | sigemptyset (&mask); |
| 3833 | sigaddset (&mask, SIGCHLD); |
| 3834 | if (enable) |
| 3835 | { |
| 3836 | /* Unblock target events. */ |
| 3837 | linux_nat_async_events_enabled = 1; |
| 3838 | |
| 3839 | local_event_queue_to_pipe (); |
| 3840 | /* While in masked async, we may have not collected all the |
| 3841 | pending events. Get them out now. */ |
| 3842 | get_pending_events (); |
| 3843 | sigprocmask (SIG_UNBLOCK, &mask, NULL); |
| 3844 | } |
| 3845 | else |
| 3846 | { |
| 3847 | /* Block target events. */ |
| 3848 | sigprocmask (SIG_BLOCK, &mask, NULL); |
| 3849 | linux_nat_async_events_enabled = 0; |
| 3850 | /* Get events out of queue, and make them available to |
| 3851 | queued_waitpid / my_waitpid. */ |
| 3852 | pipe_to_local_event_queue (); |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | return current_state; |
| 3857 | } |
| 3858 | |
| 3859 | static int async_terminal_is_ours = 1; |
| 3860 | |
| 3861 | /* target_terminal_inferior implementation. */ |
| 3862 | |
| 3863 | static void |
| 3864 | linux_nat_terminal_inferior (void) |
| 3865 | { |
| 3866 | if (!target_is_async_p ()) |
| 3867 | { |
| 3868 | /* Async mode is disabled. */ |
| 3869 | terminal_inferior (); |
| 3870 | return; |
| 3871 | } |
| 3872 | |
| 3873 | /* GDB should never give the terminal to the inferior, if the |
| 3874 | inferior is running in the background (run&, continue&, etc.). |
| 3875 | This check can be removed when the common code is fixed. */ |
| 3876 | if (!sync_execution) |
| 3877 | return; |
| 3878 | |
| 3879 | terminal_inferior (); |
| 3880 | |
| 3881 | if (!async_terminal_is_ours) |
| 3882 | return; |
| 3883 | |
| 3884 | delete_file_handler (input_fd); |
| 3885 | async_terminal_is_ours = 0; |
| 3886 | set_sigint_trap (); |
| 3887 | } |
| 3888 | |
| 3889 | /* target_terminal_ours implementation. */ |
| 3890 | |
| 3891 | void |
| 3892 | linux_nat_terminal_ours (void) |
| 3893 | { |
| 3894 | if (!target_is_async_p ()) |
| 3895 | { |
| 3896 | /* Async mode is disabled. */ |
| 3897 | terminal_ours (); |
| 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | /* GDB should never give the terminal to the inferior if the |
| 3902 | inferior is running in the background (run&, continue&, etc.), |
| 3903 | but claiming it sure should. */ |
| 3904 | terminal_ours (); |
| 3905 | |
| 3906 | if (!sync_execution) |
| 3907 | return; |
| 3908 | |
| 3909 | if (async_terminal_is_ours) |
| 3910 | return; |
| 3911 | |
| 3912 | clear_sigint_trap (); |
| 3913 | add_file_handler (input_fd, stdin_event_handler, 0); |
| 3914 | async_terminal_is_ours = 1; |
| 3915 | } |
| 3916 | |
| 3917 | static void (*async_client_callback) (enum inferior_event_type event_type, |
| 3918 | void *context); |
| 3919 | static void *async_client_context; |
| 3920 | |
| 3921 | static void |
| 3922 | linux_nat_async_file_handler (int error, gdb_client_data client_data) |
| 3923 | { |
| 3924 | async_client_callback (INF_REG_EVENT, async_client_context); |
| 3925 | } |
| 3926 | |
| 3927 | /* target_async implementation. */ |
| 3928 | |
| 3929 | static void |
| 3930 | linux_nat_async (void (*callback) (enum inferior_event_type event_type, |
| 3931 | void *context), void *context) |
| 3932 | { |
| 3933 | if (linux_nat_async_mask_value == 0 || !linux_nat_async_enabled) |
| 3934 | internal_error (__FILE__, __LINE__, |
| 3935 | "Calling target_async when async is masked"); |
| 3936 | |
| 3937 | if (callback != NULL) |
| 3938 | { |
| 3939 | async_client_callback = callback; |
| 3940 | async_client_context = context; |
| 3941 | add_file_handler (linux_nat_event_pipe[0], |
| 3942 | linux_nat_async_file_handler, NULL); |
| 3943 | |
| 3944 | linux_nat_async_events (1); |
| 3945 | } |
| 3946 | else |
| 3947 | { |
| 3948 | async_client_callback = callback; |
| 3949 | async_client_context = context; |
| 3950 | |
| 3951 | linux_nat_async_events (0); |
| 3952 | delete_file_handler (linux_nat_event_pipe[0]); |
| 3953 | } |
| 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | /* Enable/Disable async mode. */ |
| 3958 | |
| 3959 | static void |
| 3960 | linux_nat_set_async_mode (int on) |
| 3961 | { |
| 3962 | if (linux_nat_async_enabled != on) |
| 3963 | { |
| 3964 | if (on) |
| 3965 | { |
| 3966 | gdb_assert (waitpid_queue == NULL); |
| 3967 | sigaction (SIGCHLD, &async_sigchld_action, NULL); |
| 3968 | |
| 3969 | if (pipe (linux_nat_event_pipe) == -1) |
| 3970 | internal_error (__FILE__, __LINE__, |
| 3971 | "creating event pipe failed."); |
| 3972 | |
| 3973 | fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK); |
| 3974 | fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK); |
| 3975 | } |
| 3976 | else |
| 3977 | { |
| 3978 | sigaction (SIGCHLD, &sync_sigchld_action, NULL); |
| 3979 | |
| 3980 | drain_queued_events (-1); |
| 3981 | |
| 3982 | linux_nat_num_queued_events = 0; |
| 3983 | close (linux_nat_event_pipe[0]); |
| 3984 | close (linux_nat_event_pipe[1]); |
| 3985 | linux_nat_event_pipe[0] = linux_nat_event_pipe[1] = -1; |
| 3986 | |
| 3987 | } |
| 3988 | } |
| 3989 | linux_nat_async_enabled = on; |
| 3990 | } |
| 3991 | |
| 3992 | void |
| 3993 | linux_nat_add_target (struct target_ops *t) |
| 3994 | { |
| 3995 | /* Save the provided single-threaded target. We save this in a separate |
| 3996 | variable because another target we've inherited from (e.g. inf-ptrace) |
| 3997 | may have saved a pointer to T; we want to use it for the final |
| 3998 | process stratum target. */ |
| 3999 | linux_ops_saved = *t; |
| 4000 | linux_ops = &linux_ops_saved; |
| 4001 | |
| 4002 | /* Override some methods for multithreading. */ |
| 4003 | t->to_create_inferior = linux_nat_create_inferior; |
| 4004 | t->to_attach = linux_nat_attach; |
| 4005 | t->to_detach = linux_nat_detach; |
| 4006 | t->to_resume = linux_nat_resume; |
| 4007 | t->to_wait = linux_nat_wait; |
| 4008 | t->to_xfer_partial = linux_nat_xfer_partial; |
| 4009 | t->to_kill = linux_nat_kill; |
| 4010 | t->to_mourn_inferior = linux_nat_mourn_inferior; |
| 4011 | t->to_thread_alive = linux_nat_thread_alive; |
| 4012 | t->to_pid_to_str = linux_nat_pid_to_str; |
| 4013 | t->to_has_thread_control = tc_schedlock; |
| 4014 | |
| 4015 | t->to_can_async_p = linux_nat_can_async_p; |
| 4016 | t->to_is_async_p = linux_nat_is_async_p; |
| 4017 | t->to_async = linux_nat_async; |
| 4018 | t->to_async_mask = linux_nat_async_mask; |
| 4019 | t->to_terminal_inferior = linux_nat_terminal_inferior; |
| 4020 | t->to_terminal_ours = linux_nat_terminal_ours; |
| 4021 | |
| 4022 | /* We don't change the stratum; this target will sit at |
| 4023 | process_stratum and thread_db will set at thread_stratum. This |
| 4024 | is a little strange, since this is a multi-threaded-capable |
| 4025 | target, but we want to be on the stack below thread_db, and we |
| 4026 | also want to be used for single-threaded processes. */ |
| 4027 | |
| 4028 | add_target (t); |
| 4029 | |
| 4030 | /* TODO: Eliminate this and have libthread_db use |
| 4031 | find_target_beneath. */ |
| 4032 | thread_db_init (t); |
| 4033 | } |
| 4034 | |
| 4035 | /* Register a method to call whenever a new thread is attached. */ |
| 4036 | void |
| 4037 | linux_nat_set_new_thread (struct target_ops *t, void (*new_thread) (ptid_t)) |
| 4038 | { |
| 4039 | /* Save the pointer. We only support a single registered instance |
| 4040 | of the GNU/Linux native target, so we do not need to map this to |
| 4041 | T. */ |
| 4042 | linux_nat_new_thread = new_thread; |
| 4043 | } |
| 4044 | |
| 4045 | /* Return the saved siginfo associated with PTID. */ |
| 4046 | struct siginfo * |
| 4047 | linux_nat_get_siginfo (ptid_t ptid) |
| 4048 | { |
| 4049 | struct lwp_info *lp = find_lwp_pid (ptid); |
| 4050 | |
| 4051 | gdb_assert (lp != NULL); |
| 4052 | |
| 4053 | return &lp->siginfo; |
| 4054 | } |
| 4055 | |
| 4056 | void |
| 4057 | _initialize_linux_nat (void) |
| 4058 | { |
| 4059 | sigset_t mask; |
| 4060 | |
| 4061 | add_info ("proc", linux_nat_info_proc_cmd, _("\ |
| 4062 | Show /proc process information about any running process.\n\ |
| 4063 | Specify any process id, or use the program being debugged by default.\n\ |
| 4064 | Specify any of the following keywords for detailed info:\n\ |
| 4065 | mappings -- list of mapped memory regions.\n\ |
| 4066 | stat -- list a bunch of random process info.\n\ |
| 4067 | status -- list a different bunch of random process info.\n\ |
| 4068 | all -- list all available /proc info.")); |
| 4069 | |
| 4070 | add_setshow_zinteger_cmd ("lin-lwp", class_maintenance, |
| 4071 | &debug_linux_nat, _("\ |
| 4072 | Set debugging of GNU/Linux lwp module."), _("\ |
| 4073 | Show debugging of GNU/Linux lwp module."), _("\ |
| 4074 | Enables printf debugging output."), |
| 4075 | NULL, |
| 4076 | show_debug_linux_nat, |
| 4077 | &setdebuglist, &showdebuglist); |
| 4078 | |
| 4079 | add_setshow_zinteger_cmd ("lin-lwp-async", class_maintenance, |
| 4080 | &debug_linux_nat_async, _("\ |
| 4081 | Set debugging of GNU/Linux async lwp module."), _("\ |
| 4082 | Show debugging of GNU/Linux async lwp module."), _("\ |
| 4083 | Enables printf debugging output."), |
| 4084 | NULL, |
| 4085 | show_debug_linux_nat_async, |
| 4086 | &setdebuglist, &showdebuglist); |
| 4087 | |
| 4088 | add_setshow_boolean_cmd ("linux-async", class_maintenance, |
| 4089 | &linux_async_permitted_1, _("\ |
| 4090 | Set whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\ |
| 4091 | Show whether gdb controls the GNU/Linux inferior in asynchronous mode."), _("\ |
| 4092 | Tells gdb whether to control the GNU/Linux inferior in asynchronous mode."), |
| 4093 | set_maintenance_linux_async_permitted, |
| 4094 | show_maintenance_linux_async_permitted, |
| 4095 | &maintenance_set_cmdlist, |
| 4096 | &maintenance_show_cmdlist); |
| 4097 | |
| 4098 | /* Block SIGCHLD by default. Doing this early prevents it getting |
| 4099 | unblocked if an exception is thrown due to an error while the |
| 4100 | inferior is starting (sigsetjmp/siglongjmp). */ |
| 4101 | sigemptyset (&mask); |
| 4102 | sigaddset (&mask, SIGCHLD); |
| 4103 | sigprocmask (SIG_BLOCK, &mask, NULL); |
| 4104 | |
| 4105 | /* Save this mask as the default. */ |
| 4106 | sigprocmask (SIG_SETMASK, NULL, &normal_mask); |
| 4107 | |
| 4108 | /* The synchronous SIGCHLD handler. */ |
| 4109 | sync_sigchld_action.sa_handler = sigchld_handler; |
| 4110 | sigemptyset (&sync_sigchld_action.sa_mask); |
| 4111 | sync_sigchld_action.sa_flags = SA_RESTART; |
| 4112 | |
| 4113 | /* Make it the default. */ |
| 4114 | sigaction (SIGCHLD, &sync_sigchld_action, NULL); |
| 4115 | |
| 4116 | /* Make sure we don't block SIGCHLD during a sigsuspend. */ |
| 4117 | sigprocmask (SIG_SETMASK, NULL, &suspend_mask); |
| 4118 | sigdelset (&suspend_mask, SIGCHLD); |
| 4119 | |
| 4120 | /* SIGCHLD handler for async mode. */ |
| 4121 | async_sigchld_action.sa_handler = async_sigchld_handler; |
| 4122 | sigemptyset (&async_sigchld_action.sa_mask); |
| 4123 | async_sigchld_action.sa_flags = SA_RESTART; |
| 4124 | |
| 4125 | /* Install the default mode. */ |
| 4126 | linux_nat_set_async_mode (linux_async_permitted); |
| 4127 | } |
| 4128 | \f |
| 4129 | |
| 4130 | /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to |
| 4131 | the GNU/Linux Threads library and therefore doesn't really belong |
| 4132 | here. */ |
| 4133 | |
| 4134 | /* Read variable NAME in the target and return its value if found. |
| 4135 | Otherwise return zero. It is assumed that the type of the variable |
| 4136 | is `int'. */ |
| 4137 | |
| 4138 | static int |
| 4139 | get_signo (const char *name) |
| 4140 | { |
| 4141 | struct minimal_symbol *ms; |
| 4142 | int signo; |
| 4143 | |
| 4144 | ms = lookup_minimal_symbol (name, NULL, NULL); |
| 4145 | if (ms == NULL) |
| 4146 | return 0; |
| 4147 | |
| 4148 | if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (gdb_byte *) &signo, |
| 4149 | sizeof (signo)) != 0) |
| 4150 | return 0; |
| 4151 | |
| 4152 | return signo; |
| 4153 | } |
| 4154 | |
| 4155 | /* Return the set of signals used by the threads library in *SET. */ |
| 4156 | |
| 4157 | void |
| 4158 | lin_thread_get_thread_signals (sigset_t *set) |
| 4159 | { |
| 4160 | struct sigaction action; |
| 4161 | int restart, cancel; |
| 4162 | sigset_t blocked_mask; |
| 4163 | |
| 4164 | sigemptyset (&blocked_mask); |
| 4165 | sigemptyset (set); |
| 4166 | |
| 4167 | restart = get_signo ("__pthread_sig_restart"); |
| 4168 | cancel = get_signo ("__pthread_sig_cancel"); |
| 4169 | |
| 4170 | /* LinuxThreads normally uses the first two RT signals, but in some legacy |
| 4171 | cases may use SIGUSR1/SIGUSR2. NPTL always uses RT signals, but does |
| 4172 | not provide any way for the debugger to query the signal numbers - |
| 4173 | fortunately they don't change! */ |
| 4174 | |
| 4175 | if (restart == 0) |
| 4176 | restart = __SIGRTMIN; |
| 4177 | |
| 4178 | if (cancel == 0) |
| 4179 | cancel = __SIGRTMIN + 1; |
| 4180 | |
| 4181 | sigaddset (set, restart); |
| 4182 | sigaddset (set, cancel); |
| 4183 | |
| 4184 | /* The GNU/Linux Threads library makes terminating threads send a |
| 4185 | special "cancel" signal instead of SIGCHLD. Make sure we catch |
| 4186 | those (to prevent them from terminating GDB itself, which is |
| 4187 | likely to be their default action) and treat them the same way as |
| 4188 | SIGCHLD. */ |
| 4189 | |
| 4190 | action.sa_handler = sigchld_handler; |
| 4191 | sigemptyset (&action.sa_mask); |
| 4192 | action.sa_flags = SA_RESTART; |
| 4193 | sigaction (cancel, &action, NULL); |
| 4194 | |
| 4195 | /* We block the "cancel" signal throughout this code ... */ |
| 4196 | sigaddset (&blocked_mask, cancel); |
| 4197 | sigprocmask (SIG_BLOCK, &blocked_mask, NULL); |
| 4198 | |
| 4199 | /* ... except during a sigsuspend. */ |
| 4200 | sigdelset (&suspend_mask, cancel); |
| 4201 | } |
| 4202 | |