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