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