1 1.1 christos /* GNU/Linux native-dependent code common to multiple platforms. 2 1.1 christos 3 1.11 christos Copyright (C) 2001-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "inferior.h" 21 1.3 christos #include "infrun.h" 22 1.1 christos #include "target.h" 23 1.1 christos #include "nat/linux-nat.h" 24 1.1 christos #include "nat/linux-waitpid.h" 25 1.9 christos #include "gdbsupport/gdb_wait.h" 26 1.1 christos #include <unistd.h> 27 1.1 christos #include <sys/syscall.h> 28 1.6 christos #include "nat/gdb_ptrace.h" 29 1.1 christos #include "linux-nat.h" 30 1.3 christos #include "nat/linux-ptrace.h" 31 1.3 christos #include "nat/linux-procfs.h" 32 1.5 christos #include "nat/linux-personality.h" 33 1.1 christos #include "linux-fork.h" 34 1.1 christos #include "gdbthread.h" 35 1.11 christos #include "cli/cli-cmds.h" 36 1.1 christos #include "regcache.h" 37 1.1 christos #include "regset.h" 38 1.1 christos #include "inf-child.h" 39 1.1 christos #include "inf-ptrace.h" 40 1.1 christos #include "auxv.h" 41 1.11 christos #include <sys/procfs.h> 42 1.11 christos #include "elf-bfd.h" 43 1.11 christos #include "gregset.h" 44 1.11 christos #include "gdbcore.h" 45 1.11 christos #include <ctype.h> 46 1.11 christos #include <sys/stat.h> 47 1.11 christos #include <fcntl.h> 48 1.1 christos #include "inf-loop.h" 49 1.9 christos #include "gdbsupport/event-loop.h" 50 1.1 christos #include "event-top.h" 51 1.1 christos #include <pwd.h> 52 1.1 christos #include <sys/types.h> 53 1.1 christos #include <dirent.h> 54 1.1 christos #include "xml-support.h" 55 1.1 christos #include <sys/vfs.h> 56 1.1 christos #include "solib.h" 57 1.3 christos #include "nat/linux-osdata.h" 58 1.1 christos #include "linux-tdep.h" 59 1.1 christos #include "symfile.h" 60 1.9 christos #include "gdbsupport/agent.h" 61 1.1 christos #include "tracepoint.h" 62 1.1 christos #include "target-descriptions.h" 63 1.9 christos #include "gdbsupport/filestuff.h" 64 1.3 christos #include "objfiles.h" 65 1.5 christos #include "nat/linux-namespaces.h" 66 1.10 christos #include "gdbsupport/block-signals.h" 67 1.9 christos #include "gdbsupport/fileio.h" 68 1.9 christos #include "gdbsupport/scope-exit.h" 69 1.9 christos #include "gdbsupport/gdb-sigmask.h" 70 1.10 christos #include "gdbsupport/common-debug.h" 71 1.10 christos #include <unordered_map> 72 1.1 christos 73 1.1 christos /* This comment documents high-level logic of this file. 74 1.1 christos 75 1.1 christos Waiting for events in sync mode 76 1.1 christos =============================== 77 1.1 christos 78 1.6 christos When waiting for an event in a specific thread, we just use waitpid, 79 1.6 christos passing the specific pid, and not passing WNOHANG. 80 1.6 christos 81 1.6 christos When waiting for an event in all threads, waitpid is not quite good: 82 1.6 christos 83 1.6 christos - If the thread group leader exits while other threads in the thread 84 1.6 christos group still exist, waitpid(TGID, ...) hangs. That waitpid won't 85 1.6 christos return an exit status until the other threads in the group are 86 1.6 christos reaped. 87 1.6 christos 88 1.6 christos - When a non-leader thread execs, that thread just vanishes without 89 1.6 christos reporting an exit (so we'd hang if we waited for it explicitly in 90 1.6 christos that case). The exec event is instead reported to the TGID pid. 91 1.1 christos 92 1.6 christos The solution is to always use -1 and WNOHANG, together with 93 1.6 christos sigsuspend. 94 1.6 christos 95 1.6 christos First, we use non-blocking waitpid to check for events. If nothing is 96 1.6 christos found, we use sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, 97 1.6 christos it means something happened to a child process. As soon as we know 98 1.6 christos there's an event, we get back to calling nonblocking waitpid. 99 1.6 christos 100 1.6 christos Note that SIGCHLD should be blocked between waitpid and sigsuspend 101 1.6 christos calls, so that we don't miss a signal. If SIGCHLD arrives in between, 102 1.6 christos when it's blocked, the signal becomes pending and sigsuspend 103 1.6 christos immediately notices it and returns. 104 1.1 christos 105 1.6 christos Waiting for events in async mode (TARGET_WNOHANG) 106 1.6 christos ================================================= 107 1.1 christos 108 1.1 christos In async mode, GDB should always be ready to handle both user input 109 1.1 christos and target events, so neither blocking waitpid nor sigsuspend are 110 1.1 christos viable options. Instead, we should asynchronously notify the GDB main 111 1.1 christos event loop whenever there's an unprocessed event from the target. We 112 1.1 christos detect asynchronous target events by handling SIGCHLD signals. To 113 1.10 christos notify the event loop about target events, an event pipe is used 114 1.10 christos --- the pipe is registered as waitable event source in the event loop, 115 1.1 christos the event loop select/poll's on the read end of this pipe (as well on 116 1.10 christos other event sources, e.g., stdin), and the SIGCHLD handler marks the 117 1.10 christos event pipe to raise an event. This is more portable than relying on 118 1.1 christos pselect/ppoll, since on kernels that lack those syscalls, libc 119 1.1 christos emulates them with select/poll+sigprocmask, and that is racy 120 1.1 christos (a.k.a. plain broken). 121 1.1 christos 122 1.1 christos Obviously, if we fail to notify the event loop if there's a target 123 1.1 christos event, it's bad. OTOH, if we notify the event loop when there's no 124 1.1 christos event from the target, linux_nat_wait will detect that there's no real 125 1.1 christos event to report, and return event of type TARGET_WAITKIND_IGNORE. 126 1.1 christos This is mostly harmless, but it will waste time and is better avoided. 127 1.1 christos 128 1.1 christos The main design point is that every time GDB is outside linux-nat.c, 129 1.1 christos we have a SIGCHLD handler installed that is called when something 130 1.1 christos happens to the target and notifies the GDB event loop. Whenever GDB 131 1.1 christos core decides to handle the event, and calls into linux-nat.c, we 132 1.1 christos process things as in sync mode, except that the we never block in 133 1.1 christos sigsuspend. 134 1.1 christos 135 1.1 christos While processing an event, we may end up momentarily blocked in 136 1.1 christos waitpid calls. Those waitpid calls, while blocking, are guarantied to 137 1.1 christos return quickly. E.g., in all-stop mode, before reporting to the core 138 1.1 christos that an LWP hit a breakpoint, all LWPs are stopped by sending them 139 1.1 christos SIGSTOP, and synchronously waiting for the SIGSTOP to be reported. 140 1.1 christos Note that this is different from blocking indefinitely waiting for the 141 1.1 christos next event --- here, we're already handling an event. 142 1.1 christos 143 1.1 christos Use of signals 144 1.1 christos ============== 145 1.1 christos 146 1.1 christos We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another 147 1.1 christos signal is not entirely significant; we just need for a signal to be delivered, 148 1.1 christos so that we can intercept it. SIGSTOP's advantage is that it can not be 149 1.1 christos blocked. A disadvantage is that it is not a real-time signal, so it can only 150 1.1 christos be queued once; we do not keep track of other sources of SIGSTOP. 151 1.1 christos 152 1.1 christos Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't 153 1.1 christos use them, because they have special behavior when the signal is generated - 154 1.1 christos not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL 155 1.1 christos kills the entire thread group. 156 1.1 christos 157 1.1 christos A delivered SIGSTOP would stop the entire thread group, not just the thread we 158 1.1 christos tkill'd. But we never let the SIGSTOP be delivered; we always intercept and 159 1.1 christos cancel it (by PTRACE_CONT without passing SIGSTOP). 160 1.1 christos 161 1.1 christos We could use a real-time signal instead. This would solve those problems; we 162 1.1 christos could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB. 163 1.1 christos But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH 164 1.1 christos generates it, and there are races with trying to find a signal that is not 165 1.6 christos blocked. 166 1.6 christos 167 1.6 christos Exec events 168 1.6 christos =========== 169 1.6 christos 170 1.6 christos The case of a thread group (process) with 3 or more threads, and a 171 1.6 christos thread other than the leader execs is worth detailing: 172 1.6 christos 173 1.6 christos On an exec, the Linux kernel destroys all threads except the execing 174 1.6 christos one in the thread group, and resets the execing thread's tid to the 175 1.6 christos tgid. No exit notification is sent for the execing thread -- from the 176 1.6 christos ptracer's perspective, it appears as though the execing thread just 177 1.6 christos vanishes. Until we reap all other threads except the leader and the 178 1.6 christos execing thread, the leader will be zombie, and the execing thread will 179 1.6 christos be in `D (disc sleep)' state. As soon as all other threads are 180 1.6 christos reaped, the execing thread changes its tid to the tgid, and the 181 1.6 christos previous (zombie) leader vanishes, giving place to the "new" 182 1.11 christos leader. 183 1.11 christos 184 1.11 christos Accessing inferior memory 185 1.11 christos ========================= 186 1.11 christos 187 1.11 christos To access inferior memory, we strongly prefer /proc/PID/mem. We 188 1.11 christos fallback to ptrace if and only if /proc/PID/mem is not writable, as a 189 1.11 christos concession for obsolescent kernels (such as found in RHEL6). For 190 1.11 christos modern kernels, the fallback shouldn't trigger. GDBserver does not 191 1.11 christos have the ptrace fallback already, and at some point, we'll consider 192 1.11 christos removing it from native GDB too. 193 1.11 christos 194 1.11 christos /proc/PID/mem has a few advantages over alternatives like 195 1.11 christos PTRACE_PEEKTEXT/PTRACE_POKETEXT or process_vm_readv/process_vm_writev: 196 1.11 christos 197 1.11 christos - Because we can use a single read/write call, /proc/PID/mem can be 198 1.11 christos much more efficient than banging away at 199 1.11 christos PTRACE_PEEKTEXT/PTRACE_POKETEXT, one word at a time. 200 1.11 christos 201 1.11 christos - /proc/PID/mem allows writing to read-only pages, which we need to 202 1.11 christos e.g., plant breakpoint instructions. process_vm_writev does not 203 1.11 christos allow this. 204 1.11 christos 205 1.11 christos - /proc/PID/mem allows memory access even if all threads are running. 206 1.11 christos OTOH, PTRACE_PEEKTEXT/PTRACE_POKETEXT require passing down the tid 207 1.11 christos of a stopped task. This lets us e.g., install breakpoints while the 208 1.11 christos inferior is running, clear a displaced stepping scratch pad when the 209 1.11 christos thread that was displaced stepping exits, print inferior globals, 210 1.11 christos etc., all without having to worry about temporarily pausing some 211 1.11 christos thread. 212 1.11 christos 213 1.11 christos - /proc/PID/mem does not suffer from a race that could cause us to 214 1.11 christos access memory of the wrong address space when the inferior execs. 215 1.11 christos 216 1.11 christos process_vm_readv/process_vm_writev have this problem. 217 1.11 christos 218 1.11 christos E.g., say GDB decides to write to memory just while the inferior 219 1.11 christos execs. In this scenario, GDB could write memory to the post-exec 220 1.11 christos address space thinking it was writing to the pre-exec address space, 221 1.11 christos with high probability of corrupting the inferior. Or if GDB decides 222 1.11 christos instead to read memory just while the inferior execs, it could read 223 1.11 christos bogus contents out of the wrong address space. 224 1.11 christos 225 1.11 christos ptrace used to have this problem too, but no longer has since Linux 226 1.11 christos commit dbb5afad100a ("ptrace: make ptrace() fail if the tracee 227 1.11 christos changed its pid unexpectedly"), in Linux 5.13. (And if ptrace were 228 1.11 christos ever changed to allow access memory via zombie or running threads, 229 1.11 christos it would better not forget to consider this scenario.) 230 1.11 christos 231 1.11 christos We avoid this race with /proc/PID/mem, by opening the file as soon 232 1.11 christos as we start debugging the inferior, when it is known the inferior is 233 1.11 christos stopped, and holding on to the open file descriptor, to be used 234 1.11 christos whenever we need to access inferior memory. If the inferior execs 235 1.11 christos or exits, reading/writing from/to the file returns 0 (EOF), 236 1.11 christos indicating the address space is gone, and so we return 237 1.11 christos TARGET_XFER_EOF to the core. We close the old file and open a new 238 1.11 christos one when we finally see the PTRACE_EVENT_EXEC event. */ 239 1.1 christos 240 1.1 christos #ifndef O_LARGEFILE 241 1.1 christos #define O_LARGEFILE 0 242 1.1 christos #endif 243 1.1 christos 244 1.8 christos struct linux_nat_target *linux_target; 245 1.8 christos 246 1.12 christos /* See nat/linux-nat.h. */ 247 1.5 christos enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; 248 1.5 christos 249 1.10 christos /* When true, print debug messages relating to the linux native target. */ 250 1.10 christos 251 1.10 christos static bool debug_linux_nat; 252 1.10 christos 253 1.10 christos /* Implement 'show debug linux-nat'. */ 254 1.10 christos 255 1.1 christos static void 256 1.1 christos show_debug_linux_nat (struct ui_file *file, int from_tty, 257 1.1 christos struct cmd_list_element *c, const char *value) 258 1.1 christos { 259 1.10 christos gdb_printf (file, _("Debugging of GNU/Linux native targets is %s.\n"), 260 1.10 christos value); 261 1.1 christos } 262 1.1 christos 263 1.10 christos /* Print a linux-nat debug statement. */ 264 1.9 christos 265 1.10 christos #define linux_nat_debug_printf(fmt, ...) \ 266 1.10 christos debug_prefixed_printf_cond (debug_linux_nat, "linux-nat", fmt, ##__VA_ARGS__) 267 1.10 christos 268 1.10 christos /* Print "linux-nat" enter/exit debug statements. */ 269 1.9 christos 270 1.10 christos #define LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT \ 271 1.10 christos scoped_debug_enter_exit (debug_linux_nat, "linux-nat") 272 1.9 christos 273 1.1 christos struct simple_pid_list 274 1.1 christos { 275 1.1 christos int pid; 276 1.1 christos int status; 277 1.1 christos struct simple_pid_list *next; 278 1.1 christos }; 279 1.9 christos static struct simple_pid_list *stopped_pids; 280 1.1 christos 281 1.6 christos /* Whether target_thread_events is in effect. */ 282 1.12 christos static bool report_thread_events; 283 1.6 christos 284 1.1 christos static int kill_lwp (int lwpid, int signo); 285 1.1 christos 286 1.9 christos static int stop_callback (struct lwp_info *lp); 287 1.1 christos 288 1.1 christos static void block_child_signals (sigset_t *prev_mask); 289 1.1 christos static void restore_child_signals_mask (sigset_t *prev_mask); 290 1.1 christos 291 1.1 christos struct lwp_info; 292 1.1 christos static struct lwp_info *add_lwp (ptid_t ptid); 293 1.1 christos static void purge_lwp_list (int pid); 294 1.1 christos static void delete_lwp (ptid_t ptid); 295 1.1 christos static struct lwp_info *find_lwp_pid (ptid_t ptid); 296 1.1 christos 297 1.3 christos static int lwp_status_pending_p (struct lwp_info *lp); 298 1.3 christos 299 1.11 christos static bool is_lwp_marked_dead (lwp_info *lp); 300 1.11 christos 301 1.6 christos static void save_stop_reason (struct lwp_info *lp); 302 1.6 christos 303 1.10 christos static bool proc_mem_file_is_writable (); 304 1.10 christos static void close_proc_mem_file (pid_t pid); 305 1.10 christos static void open_proc_mem_file (ptid_t ptid); 306 1.10 christos 307 1.10 christos /* Return TRUE if LWP is the leader thread of the process. */ 308 1.10 christos 309 1.10 christos static bool 310 1.10 christos is_leader (lwp_info *lp) 311 1.10 christos { 312 1.10 christos return lp->ptid.pid () == lp->ptid.lwp (); 313 1.10 christos } 314 1.10 christos 315 1.11 christos /* Convert an LWP's pending status to a std::string. */ 316 1.11 christos 317 1.11 christos static std::string 318 1.11 christos pending_status_str (lwp_info *lp) 319 1.11 christos { 320 1.11 christos gdb_assert (lwp_status_pending_p (lp)); 321 1.11 christos 322 1.11 christos if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 323 1.11 christos return lp->waitstatus.to_string (); 324 1.11 christos else 325 1.11 christos return status_to_str (lp->status); 326 1.11 christos } 327 1.11 christos 328 1.11 christos /* Return true if we should report exit events for LP. */ 329 1.11 christos 330 1.11 christos static bool 331 1.11 christos report_exit_events_for (lwp_info *lp) 332 1.11 christos { 333 1.11 christos thread_info *thr = linux_target->find_thread (lp->ptid); 334 1.11 christos gdb_assert (thr != nullptr); 335 1.11 christos 336 1.11 christos return (report_thread_events 337 1.11 christos || (thr->thread_options () & GDB_THREAD_OPTION_EXIT) != 0); 338 1.11 christos } 339 1.11 christos 340 1.1 christos 341 1.5 christos /* LWP accessors. */ 343 1.5 christos 344 1.5 christos /* See nat/linux-nat.h. */ 345 1.5 christos 346 1.5 christos ptid_t 347 1.5 christos ptid_of_lwp (struct lwp_info *lwp) 348 1.5 christos { 349 1.5 christos return lwp->ptid; 350 1.5 christos } 351 1.5 christos 352 1.5 christos /* See nat/linux-nat.h. */ 353 1.5 christos 354 1.5 christos void 355 1.5 christos lwp_set_arch_private_info (struct lwp_info *lwp, 356 1.5 christos struct arch_lwp_info *info) 357 1.5 christos { 358 1.5 christos lwp->arch_private = info; 359 1.5 christos } 360 1.5 christos 361 1.5 christos /* See nat/linux-nat.h. */ 362 1.5 christos 363 1.5 christos struct arch_lwp_info * 364 1.5 christos lwp_arch_private_info (struct lwp_info *lwp) 365 1.5 christos { 366 1.5 christos return lwp->arch_private; 367 1.5 christos } 368 1.5 christos 369 1.5 christos /* See nat/linux-nat.h. */ 370 1.5 christos 371 1.5 christos int 372 1.5 christos lwp_is_stopped (struct lwp_info *lwp) 373 1.5 christos { 374 1.5 christos return lwp->stopped; 375 1.5 christos } 376 1.5 christos 377 1.5 christos /* See nat/linux-nat.h. */ 378 1.5 christos 379 1.5 christos enum target_stop_reason 380 1.5 christos lwp_stop_reason (struct lwp_info *lwp) 381 1.5 christos { 382 1.5 christos return lwp->stop_reason; 383 1.5 christos } 384 1.7 christos 385 1.7 christos /* See nat/linux-nat.h. */ 386 1.7 christos 387 1.7 christos int 388 1.7 christos lwp_is_stepping (struct lwp_info *lwp) 389 1.7 christos { 390 1.7 christos return lwp->step; 391 1.7 christos } 392 1.5 christos 393 1.1 christos 394 1.1 christos /* Trivial list manipulation functions to keep track of a list of 396 1.1 christos new stopped processes. */ 397 1.1 christos static void 398 1.6 christos add_to_pid_list (struct simple_pid_list **listp, int pid, int status) 399 1.1 christos { 400 1.1 christos struct simple_pid_list *new_pid = XNEW (struct simple_pid_list); 401 1.1 christos 402 1.1 christos new_pid->pid = pid; 403 1.1 christos new_pid->status = status; 404 1.1 christos new_pid->next = *listp; 405 1.1 christos *listp = new_pid; 406 1.1 christos } 407 1.1 christos 408 1.1 christos static int 409 1.1 christos pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp) 410 1.1 christos { 411 1.1 christos struct simple_pid_list **p; 412 1.1 christos 413 1.1 christos for (p = listp; *p != NULL; p = &(*p)->next) 414 1.1 christos if ((*p)->pid == pid) 415 1.1 christos { 416 1.1 christos struct simple_pid_list *next = (*p)->next; 417 1.1 christos 418 1.1 christos *statusp = (*p)->status; 419 1.1 christos xfree (*p); 420 1.1 christos *p = next; 421 1.1 christos return 1; 422 1.1 christos } 423 1.1 christos return 0; 424 1.5 christos } 425 1.5 christos 426 1.5 christos /* Return the ptrace options that we want to try to enable. */ 427 1.5 christos 428 1.5 christos static int 429 1.5 christos linux_nat_ptrace_options (int attached) 430 1.5 christos { 431 1.5 christos int options = 0; 432 1.5 christos 433 1.5 christos if (!attached) 434 1.5 christos options |= PTRACE_O_EXITKILL; 435 1.5 christos 436 1.5 christos options |= (PTRACE_O_TRACESYSGOOD 437 1.5 christos | PTRACE_O_TRACEVFORKDONE 438 1.5 christos | PTRACE_O_TRACEVFORK 439 1.5 christos | PTRACE_O_TRACEFORK 440 1.5 christos | PTRACE_O_TRACEEXEC); 441 1.5 christos 442 1.5 christos return options; 443 1.8 christos } 444 1.8 christos 445 1.3 christos /* Initialize ptrace and procfs warnings and check for supported 446 1.3 christos ptrace features given PID. 447 1.1 christos 448 1.1 christos ATTACHED should be nonzero iff we attached to the inferior. */ 449 1.8 christos 450 1.1 christos static void 451 1.5 christos linux_init_ptrace_procfs (pid_t pid, int attached) 452 1.5 christos { 453 1.5 christos int options = linux_nat_ptrace_options (attached); 454 1.1 christos 455 1.8 christos linux_enable_event_reporting (pid, options); 456 1.10 christos linux_ptrace_init_warnings (); 457 1.11 christos linux_proc_init_warnings (); 458 1.11 christos proc_mem_file_is_writable (); 459 1.11 christos 460 1.11 christos /* Let the arch-specific native code do any needed initialization. 461 1.11 christos Some architectures need to call ptrace to check for hardware 462 1.11 christos watchpoints support, etc. Call it now, when we know the tracee 463 1.1 christos is ptrace-stopped. */ 464 1.1 christos linux_target->low_init_process (pid); 465 1.8 christos } 466 1.8 christos 467 1.8 christos linux_nat_target::~linux_nat_target () 468 1.8 christos {} 469 1.8 christos 470 1.1 christos void 471 1.8 christos linux_nat_target::post_attach (int pid) 472 1.1 christos { 473 1.1 christos linux_init_ptrace_procfs (pid, 1); 474 1.10 christos } 475 1.10 christos 476 1.8 christos /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ 477 1.8 christos 478 1.1 christos void 479 1.8 christos linux_nat_target::post_startup_inferior (ptid_t ptid) 480 1.1 christos { 481 1.1 christos linux_init_ptrace_procfs (ptid.pid (), 0); 482 1.1 christos } 483 1.1 christos 484 1.1 christos /* Return the number of known LWPs in the tgid given by PID. */ 485 1.1 christos 486 1.1 christos static int 487 1.1 christos num_lwps (int pid) 488 1.1 christos { 489 1.10 christos int count = 0; 490 1.8 christos 491 1.1 christos for (const lwp_info *lp ATTRIBUTE_UNUSED : all_lwps ()) 492 1.1 christos if (lp->ptid.pid () == pid) 493 1.1 christos count++; 494 1.1 christos 495 1.1 christos return count; 496 1.8 christos } 497 1.1 christos 498 1.8 christos /* Deleter for lwp_info unique_ptr specialisation. */ 499 1.1 christos 500 1.8 christos struct lwp_deleter 501 1.8 christos { 502 1.8 christos void operator() (struct lwp_info *lwp) const 503 1.8 christos { 504 1.8 christos delete_lwp (lwp->ptid); 505 1.1 christos } 506 1.8 christos }; 507 1.8 christos 508 1.8 christos /* A unique_ptr specialisation for lwp_info. */ 509 1.1 christos 510 1.10 christos typedef std::unique_ptr<struct lwp_info, lwp_deleter> lwp_info_up; 511 1.3 christos 512 1.10 christos /* Target hook for follow_fork. */ 513 1.10 christos 514 1.10 christos void 515 1.10 christos linux_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid, 516 1.1 christos target_waitkind fork_kind, bool follow_child, 517 1.10 christos bool detach_fork) 518 1.10 christos { 519 1.10 christos inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind, 520 1.3 christos follow_child, detach_fork); 521 1.1 christos 522 1.10 christos if (!follow_child) 523 1.10 christos { 524 1.10 christos bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED; 525 1.10 christos ptid_t parent_ptid = inferior_ptid; 526 1.1 christos int parent_pid = parent_ptid.lwp (); 527 1.1 christos int child_pid = child_ptid.lwp (); 528 1.10 christos 529 1.3 christos /* We're already attached to the parent, by default. */ 530 1.3 christos lwp_info *child_lp = add_lwp (child_ptid); 531 1.1 christos child_lp->stopped = 1; 532 1.1 christos child_lp->last_resume_kind = resume_stop; 533 1.1 christos 534 1.1 christos /* Detach new forked process? */ 535 1.8 christos if (detach_fork) 536 1.8 christos { 537 1.8 christos int child_stop_signal = 0; 538 1.8 christos bool detach_child = true; 539 1.8 christos 540 1.8 christos /* Move CHILD_LP into a unique_ptr and clear the source pointer 541 1.8 christos to prevent us doing anything stupid with it. */ 542 1.3 christos lwp_info_up child_lp_ptr (child_lp); 543 1.8 christos child_lp = nullptr; 544 1.1 christos 545 1.3 christos linux_target->low_prepare_to_resume (child_lp_ptr.get ()); 546 1.3 christos 547 1.3 christos /* When debugging an inferior in an architecture that supports 548 1.3 christos hardware single stepping on a kernel without commit 549 1.3 christos 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child 550 1.3 christos process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits 551 1.3 christos set if the parent process had them set. 552 1.3 christos To work around this, single step the child process 553 1.8 christos once before detaching to clear the flags. */ 554 1.8 christos 555 1.8 christos /* Note that we consult the parent's architecture instead of 556 1.3 christos the child's because there's no inferior for the child at 557 1.8 christos this point. */ 558 1.1 christos if (!gdbarch_software_single_step_p (target_thread_architecture 559 1.8 christos (parent_ptid))) 560 1.8 christos { 561 1.3 christos int status; 562 1.3 christos 563 1.3 christos linux_disable_event_reporting (child_pid); 564 1.3 christos if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) 565 1.3 christos perror_with_name (_("Couldn't do single step")); 566 1.8 christos if (my_waitpid (child_pid, &status, 0) < 0) 567 1.8 christos perror_with_name (_("Couldn't wait vfork process")); 568 1.8 christos else 569 1.8 christos { 570 1.8 christos detach_child = WIFSTOPPED (status); 571 1.1 christos child_stop_signal = WSTOPSIG (status); 572 1.1 christos } 573 1.8 christos } 574 1.1 christos 575 1.8 christos if (detach_child) 576 1.3 christos { 577 1.3 christos int signo = child_stop_signal; 578 1.3 christos 579 1.3 christos if (signo != 0 580 1.3 christos && !signal_pass_state (gdb_signal_from_host (signo))) 581 1.10 christos signo = 0; 582 1.10 christos ptrace (PTRACE_DETACH, child_pid, 0, signo); 583 1.1 christos 584 1.1 christos close_proc_mem_file (child_pid); 585 1.1 christos } 586 1.1 christos } 587 1.1 christos 588 1.10 christos if (has_vforked) 589 1.10 christos { 590 1.10 christos lwp_info *parent_lp = find_lwp_pid (parent_ptid); 591 1.1 christos linux_nat_debug_printf ("waiting for VFORK_DONE on %d", parent_pid); 592 1.10 christos parent_lp->stopped = 1; 593 1.10 christos 594 1.1 christos /* We'll handle the VFORK_DONE event like any other 595 1.1 christos event, in target_wait. */ 596 1.1 christos } 597 1.1 christos } 598 1.1 christos else 599 1.1 christos { 600 1.10 christos struct lwp_info *child_lp; 601 1.1 christos 602 1.1 christos child_lp = add_lwp (child_ptid); 603 1.1 christos child_lp->stopped = 1; 604 1.1 christos child_lp->last_resume_kind = resume_stop; 605 1.1 christos } 606 1.1 christos } 607 1.8 christos 608 1.8 christos 609 1.1 christos int 611 1.1 christos linux_nat_target::insert_fork_catchpoint (int pid) 612 1.1 christos { 613 1.8 christos return 0; 614 1.8 christos } 615 1.1 christos 616 1.1 christos int 617 1.1 christos linux_nat_target::remove_fork_catchpoint (int pid) 618 1.1 christos { 619 1.8 christos return 0; 620 1.8 christos } 621 1.1 christos 622 1.10 christos int 623 1.1 christos linux_nat_target::insert_vfork_catchpoint (int pid) 624 1.1 christos { 625 1.8 christos return 0; 626 1.8 christos } 627 1.1 christos 628 1.1 christos int 629 1.1 christos linux_nat_target::remove_vfork_catchpoint (int pid) 630 1.1 christos { 631 1.8 christos return 0; 632 1.8 christos } 633 1.1 christos 634 1.10 christos int 635 1.1 christos linux_nat_target::insert_exec_catchpoint (int pid) 636 1.1 christos { 637 1.8 christos return 0; 638 1.8 christos } 639 1.1 christos 640 1.1 christos int 641 1.1 christos linux_nat_target::remove_exec_catchpoint (int pid) 642 1.1 christos { 643 1.8 christos return 0; 644 1.8 christos } 645 1.8 christos 646 1.1 christos int 647 1.1 christos linux_nat_target::set_syscall_catchpoint (int pid, bool needed, int any_count, 648 1.1 christos gdb::array_view<const int> syscall_counts) 649 1.1 christos { 650 1.8 christos /* On GNU/Linux, we ignore the arguments. It means that we only 651 1.1 christos enable the syscall catchpoints, but do not disable them. 652 1.1 christos 653 1.1 christos Also, we do not use the `syscall_counts' information because we do not 654 1.1 christos filter system calls here. We let GDB do the logic for us. */ 655 1.6 christos return 0; 656 1.6 christos } 657 1.6 christos 658 1.6 christos /* List of known LWPs, keyed by LWP PID. This speeds up the common 659 1.6 christos case of mapping a PID returned from the kernel to our corresponding 660 1.6 christos lwp_info data structure. */ 661 1.6 christos static htab_t lwp_lwpid_htab; 662 1.6 christos 663 1.6 christos /* Calculate a hash from a lwp_info's LWP PID. */ 664 1.6 christos 665 1.6 christos static hashval_t 666 1.8 christos lwp_info_hash (const void *ap) 667 1.6 christos { 668 1.6 christos const struct lwp_info *lp = (struct lwp_info *) ap; 669 1.6 christos pid_t pid = lp->ptid.lwp (); 670 1.6 christos 671 1.6 christos return iterative_hash_object (pid, 0); 672 1.6 christos } 673 1.1 christos 674 1.6 christos /* Equality function for the lwp_info hash table. Compares the LWP's 675 1.6 christos PID. */ 676 1.6 christos 677 1.6 christos static int 678 1.6 christos lwp_lwpid_htab_eq (const void *a, const void *b) 679 1.6 christos { 680 1.8 christos const struct lwp_info *entry = (const struct lwp_info *) a; 681 1.6 christos const struct lwp_info *element = (const struct lwp_info *) b; 682 1.6 christos 683 1.6 christos return entry->ptid.lwp () == element->ptid.lwp (); 684 1.6 christos } 685 1.6 christos 686 1.6 christos /* Create the lwp_lwpid_htab hash table. */ 687 1.6 christos 688 1.6 christos static void 689 1.6 christos lwp_lwpid_htab_create (void) 690 1.6 christos { 691 1.6 christos lwp_lwpid_htab = htab_create (100, lwp_info_hash, lwp_lwpid_htab_eq, NULL); 692 1.6 christos } 693 1.6 christos 694 1.6 christos /* Add LP to the hash table. */ 695 1.6 christos 696 1.6 christos static void 697 1.6 christos lwp_lwpid_htab_add_lwp (struct lwp_info *lp) 698 1.6 christos { 699 1.6 christos void **slot; 700 1.6 christos 701 1.6 christos slot = htab_find_slot (lwp_lwpid_htab, lp, INSERT); 702 1.6 christos gdb_assert (slot != NULL && *slot == NULL); 703 1.6 christos *slot = lp; 704 1.6 christos } 705 1.6 christos 706 1.6 christos /* Head of doubly-linked list of known LWPs. Sorted by reverse 707 1.10 christos creation order. This order is assumed in some cases. E.g., 708 1.10 christos reaping status after killing alls lwps of a process: the leader LWP 709 1.10 christos must be reaped last. */ 710 1.10 christos 711 1.10 christos static intrusive_list<lwp_info> lwp_list; 712 1.10 christos 713 1.10 christos /* See linux-nat.h. */ 714 1.10 christos 715 1.10 christos lwp_info_range 716 1.10 christos all_lwps () 717 1.10 christos { 718 1.10 christos return lwp_info_range (lwp_list.begin ()); 719 1.10 christos } 720 1.10 christos 721 1.10 christos /* See linux-nat.h. */ 722 1.10 christos 723 1.10 christos lwp_info_safe_range 724 1.10 christos all_lwps_safe () 725 1.6 christos { 726 1.6 christos return lwp_info_safe_range (lwp_list.begin ()); 727 1.6 christos } 728 1.6 christos 729 1.6 christos /* Add LP to sorted-by-reverse-creation-order doubly-linked list. */ 730 1.6 christos 731 1.10 christos static void 732 1.6 christos lwp_list_add (struct lwp_info *lp) 733 1.6 christos { 734 1.6 christos lwp_list.push_front (*lp); 735 1.6 christos } 736 1.6 christos 737 1.6 christos /* Remove LP from sorted-by-reverse-creation-order doubly-linked 738 1.6 christos list. */ 739 1.6 christos 740 1.6 christos static void 741 1.10 christos lwp_list_remove (struct lwp_info *lp) 742 1.6 christos { 743 1.6 christos /* Remove from sorted-by-creation-order list. */ 744 1.1 christos lwp_list.erase (lwp_list.iterator_to (*lp)); 745 1.1 christos } 746 1.1 christos 747 1.1 christos 748 1.1 christos 750 1.1 christos /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in 751 1.1 christos _initialize_linux_nat. */ 752 1.1 christos static sigset_t suspend_mask; 753 1.1 christos 754 1.10 christos /* Signals to block to make that sigsuspend work. */ 755 1.1 christos static sigset_t blocked_mask; 756 1.1 christos 757 1.1 christos /* SIGCHLD action. */ 758 1.1 christos static struct sigaction sigchld_action; 759 1.1 christos 760 1.1 christos /* Block child signals (SIGCHLD and linux threads signals), and store 761 1.1 christos the previous mask in PREV_MASK. */ 762 1.1 christos 763 1.1 christos static void 764 1.1 christos block_child_signals (sigset_t *prev_mask) 765 1.1 christos { 766 1.9 christos /* Make sure SIGCHLD is blocked. */ 767 1.1 christos if (!sigismember (&blocked_mask, SIGCHLD)) 768 1.1 christos sigaddset (&blocked_mask, SIGCHLD); 769 1.1 christos 770 1.1 christos gdb_sigmask (SIG_BLOCK, &blocked_mask, prev_mask); 771 1.1 christos } 772 1.1 christos 773 1.1 christos /* Restore child signals mask, previously returned by 774 1.1 christos block_child_signals. */ 775 1.9 christos 776 1.1 christos static void 777 1.1 christos restore_child_signals_mask (sigset_t *prev_mask) 778 1.1 christos { 779 1.1 christos gdb_sigmask (SIG_SETMASK, prev_mask, NULL); 780 1.1 christos } 781 1.1 christos 782 1.8 christos /* Mask of signals to pass directly to the inferior. */ 783 1.8 christos static sigset_t pass_mask; 784 1.8 christos 785 1.1 christos /* Update signals to pass to the inferior. */ 786 1.1 christos void 787 1.1 christos linux_nat_target::pass_signals 788 1.1 christos (gdb::array_view<const unsigned char> pass_signals) 789 1.1 christos { 790 1.1 christos int signo; 791 1.1 christos 792 1.1 christos sigemptyset (&pass_mask); 793 1.8 christos 794 1.10 christos for (signo = 1; signo < NSIG; signo++) 795 1.1 christos { 796 1.1 christos int target_signo = gdb_signal_from_host (signo); 797 1.1 christos if (target_signo < pass_signals.size () && pass_signals[target_signo]) 798 1.1 christos sigaddset (&pass_mask, signo); 799 1.1 christos } 800 1.1 christos } 801 1.9 christos 802 1.9 christos 803 1.6 christos 805 1.1 christos /* Prototypes for local functions. */ 806 1.1 christos static int stop_wait_callback (struct lwp_info *lp); 807 1.1 christos static int resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid); 808 1.1 christos static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp); 809 1.10 christos 810 1.1 christos 811 1.8 christos 813 1.1 christos /* Destroy and free LP. */ 814 1.1 christos 815 1.6 christos lwp_info::~lwp_info () 816 1.1 christos { 817 1.6 christos /* Let the arch specific bits release arch_lwp_info. */ 818 1.6 christos linux_target->low_delete_thread (this->arch_private); 819 1.1 christos } 820 1.6 christos 821 1.6 christos /* Traversal function for purge_lwp_list. */ 822 1.1 christos 823 1.8 christos static int 824 1.6 christos lwp_lwpid_htab_remove_pid (void **slot, void *info) 825 1.6 christos { 826 1.6 christos struct lwp_info *lp = (struct lwp_info *) *slot; 827 1.10 christos int pid = *(int *) info; 828 1.6 christos 829 1.1 christos if (lp->ptid.pid () == pid) 830 1.6 christos { 831 1.6 christos htab_clear_slot (lwp_lwpid_htab, slot); 832 1.1 christos lwp_list_remove (lp); 833 1.6 christos delete lp; 834 1.1 christos } 835 1.6 christos 836 1.6 christos return 1; 837 1.6 christos } 838 1.6 christos 839 1.1 christos /* Remove all LWPs belong to PID from the lwp list. */ 840 1.1 christos 841 1.1 christos static void 842 1.1 christos purge_lwp_list (int pid) 843 1.1 christos { 844 1.1 christos htab_traverse_noresize (lwp_lwpid_htab, lwp_lwpid_htab_remove_pid, &pid); 845 1.1 christos } 846 1.1 christos 847 1.1 christos /* Add the LWP specified by PTID to the list. PTID is the first LWP 848 1.1 christos in the process. Return a pointer to the structure describing the 849 1.1 christos new LWP. 850 1.1 christos 851 1.1 christos This differs from add_lwp in that we don't let the arch specific 852 1.1 christos bits know about this new thread. Current clients of this callback 853 1.1 christos take the opportunity to install watchpoints in the new thread, and 854 1.1 christos we shouldn't do that for the first thread. If we're spawning a 855 1.1 christos child ("run"), the thread executes the shell wrapper first, and we 856 1.1 christos shouldn't touch it until it execs the program we want to debug. 857 1.1 christos For "attach", it'd be okay to call the callback, but it's not 858 1.8 christos necessary, because watchpoints can't yet have been inserted into 859 1.1 christos the inferior. */ 860 1.10 christos 861 1.1 christos static struct lwp_info * 862 1.1 christos add_initial_lwp (ptid_t ptid) 863 1.6 christos { 864 1.6 christos gdb_assert (ptid.lwp_p ()); 865 1.6 christos 866 1.6 christos lwp_info *lp = new lwp_info (ptid); 867 1.6 christos 868 1.1 christos 869 1.1 christos /* Add to sorted-by-reverse-creation-order list. */ 870 1.1 christos lwp_list_add (lp); 871 1.1 christos 872 1.1 christos /* Add to keyed-by-pid htab. */ 873 1.1 christos lwp_lwpid_htab_add_lwp (lp); 874 1.1 christos 875 1.1 christos return lp; 876 1.1 christos } 877 1.1 christos 878 1.1 christos /* Add the LWP specified by PID to the list. Return a pointer to the 879 1.1 christos structure describing the new LWP. The LWP should already be 880 1.1 christos stopped. */ 881 1.1 christos 882 1.1 christos static struct lwp_info * 883 1.1 christos add_lwp (ptid_t ptid) 884 1.1 christos { 885 1.1 christos struct lwp_info *lp; 886 1.1 christos 887 1.8 christos lp = add_initial_lwp (ptid); 888 1.1 christos 889 1.1 christos /* Let the arch specific bits know about this new thread. Current 890 1.1 christos clients of this callback take the opportunity to install 891 1.1 christos watchpoints in the new thread. We don't do this for the first 892 1.1 christos thread though. See add_initial_lwp. */ 893 1.1 christos linux_target->low_new_thread (lp); 894 1.1 christos 895 1.1 christos return lp; 896 1.1 christos } 897 1.10 christos 898 1.1 christos /* Remove the LWP specified by PID from the list. */ 899 1.10 christos 900 1.6 christos static void 901 1.6 christos delete_lwp (ptid_t ptid) 902 1.1 christos { 903 1.10 christos lwp_info dummy (ptid); 904 1.6 christos 905 1.1 christos void **slot = htab_find_slot (lwp_lwpid_htab, &dummy, NO_INSERT); 906 1.6 christos if (slot == NULL) 907 1.1 christos return; 908 1.6 christos 909 1.6 christos lwp_info *lp = *(struct lwp_info **) slot; 910 1.1 christos gdb_assert (lp != NULL); 911 1.6 christos 912 1.10 christos htab_clear_slot (lwp_lwpid_htab, slot); 913 1.1 christos 914 1.1 christos /* Remove from sorted-by-creation-order list. */ 915 1.1 christos lwp_list_remove (lp); 916 1.1 christos 917 1.1 christos /* Release. */ 918 1.1 christos delete lp; 919 1.1 christos } 920 1.1 christos 921 1.1 christos /* Return a pointer to the structure describing the LWP corresponding 922 1.1 christos to PID. If no corresponding LWP could be found, return NULL. */ 923 1.8 christos 924 1.8 christos static struct lwp_info * 925 1.1 christos find_lwp_pid (ptid_t ptid) 926 1.8 christos { 927 1.1 christos int lwp; 928 1.10 christos 929 1.10 christos if (ptid.lwp_p ()) 930 1.1 christos lwp = ptid.lwp (); 931 1.1 christos else 932 1.5 christos lwp = ptid.pid (); 933 1.1 christos 934 1.1 christos lwp_info dummy (ptid_t (0, lwp)); 935 1.1 christos return (struct lwp_info *) htab_find (lwp_lwpid_htab, &dummy); 936 1.9 christos } 937 1.1 christos 938 1.10 christos /* See nat/linux-nat.h. */ 939 1.1 christos 940 1.8 christos struct lwp_info * 941 1.1 christos iterate_over_lwps (ptid_t filter, 942 1.9 christos gdb::function_view<iterate_over_lwps_ftype> callback) 943 1.1 christos { 944 1.1 christos for (lwp_info *lp : all_lwps_safe ()) 945 1.1 christos { 946 1.1 christos if (lp->ptid.matches (filter)) 947 1.1 christos { 948 1.1 christos if (callback (lp) != 0) 949 1.1 christos return lp; 950 1.1 christos } 951 1.1 christos } 952 1.1 christos 953 1.1 christos return NULL; 954 1.1 christos } 955 1.1 christos 956 1.1 christos /* Update our internal state when changing from one checkpoint to 957 1.1 christos another indicated by NEW_PTID. We can only switch single-threaded 958 1.1 christos applications, so we only create one new LWP, and the previous list 959 1.1 christos is discarded. */ 960 1.8 christos 961 1.1 christos void 962 1.1 christos linux_nat_switch_fork (ptid_t new_ptid) 963 1.1 christos { 964 1.1 christos struct lwp_info *lp; 965 1.1 christos 966 1.1 christos purge_lwp_list (inferior_ptid.pid ()); 967 1.1 christos 968 1.9 christos lp = add_lwp (new_ptid); 969 1.1 christos lp->stopped = 1; 970 1.1 christos 971 1.1 christos /* This changes the thread's ptid while preserving the gdb thread 972 1.1 christos num. Also changes the inferior pid, while preserving the 973 1.1 christos inferior num. */ 974 1.1 christos thread_change_ptid (linux_target, inferior_ptid, new_ptid); 975 1.1 christos 976 1.11 christos /* We've just told GDB core that the thread changed target id, but, 977 1.11 christos in fact, it really is a different thread, with different register 978 1.1 christos contents. */ 979 1.1 christos registers_changed (); 980 1.11 christos } 981 1.1 christos 982 1.11 christos /* Handle the exit of a single thread LP. If DEL_THREAD is true, 983 1.1 christos delete the thread_info associated to LP, if it exists. */ 984 1.11 christos 985 1.11 christos static void 986 1.1 christos exit_lwp (struct lwp_info *lp, bool del_thread = true) 987 1.1 christos { 988 1.1 christos struct thread_info *th = linux_target->find_thread (lp->ptid); 989 1.1 christos 990 1.1 christos if (th != nullptr && del_thread) 991 1.1 christos delete_thread (th); 992 1.1 christos 993 1.1 christos delete_lwp (lp->ptid); 994 1.8 christos } 995 1.1 christos 996 1.8 christos /* Wait for the LWP specified by LP, which we have just attached to. 997 1.1 christos Returns a wait status for that LWP, to cache. */ 998 1.1 christos 999 1.1 christos static int 1000 1.1 christos linux_nat_post_attach_wait (ptid_t ptid, int *signalled) 1001 1.9 christos { 1002 1.1 christos pid_t new_pid, pid = ptid.lwp (); 1003 1.1 christos int status; 1004 1.1 christos 1005 1.1 christos if (linux_proc_pid_is_stopped (pid)) 1006 1.1 christos { 1007 1.1 christos linux_nat_debug_printf ("Attaching to a stopped process"); 1008 1.1 christos 1009 1.10 christos /* The process is definitely stopped. It is in a job control 1010 1.1 christos stop, unless the kernel predates the TASK_STOPPED / 1011 1.1 christos TASK_TRACED distinction, in which case it might be in a 1012 1.1 christos ptrace stop. Make sure it is in a ptrace stop; from there we 1013 1.1 christos can kill it, signal it, et cetera. 1014 1.1 christos 1015 1.1 christos First make sure there is a pending SIGSTOP. Since we are 1016 1.1 christos already attached, the process can not transition from stopped 1017 1.1 christos to running without a PTRACE_CONT; so we know this signal will 1018 1.1 christos go into the queue. The SIGSTOP generated by PTRACE_ATTACH is 1019 1.1 christos probably already in the queue (unless this kernel is old 1020 1.1 christos enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP 1021 1.1 christos is not an RT signal, it can only be queued once. */ 1022 1.1 christos kill_lwp (pid, SIGSTOP); 1023 1.1 christos 1024 1.1 christos /* Finally, resume the stopped process. This will deliver the SIGSTOP 1025 1.1 christos (or a higher priority signal, just like normal PTRACE_ATTACH). */ 1026 1.6 christos ptrace (PTRACE_CONT, pid, 0, 0); 1027 1.1 christos } 1028 1.1 christos 1029 1.1 christos /* Make sure the initial process is stopped. The user-level threads 1030 1.1 christos layer might want to poke around in the inferior, and that won't 1031 1.1 christos work if things haven't stabilized yet. */ 1032 1.9 christos new_pid = my_waitpid (pid, &status, __WALL); 1033 1.10 christos gdb_assert (pid == new_pid); 1034 1.1 christos 1035 1.1 christos if (!WIFSTOPPED (status)) 1036 1.1 christos { 1037 1.1 christos /* The pid we tried to attach has apparently just exited. */ 1038 1.1 christos linux_nat_debug_printf ("Failed to stop %d: %s", pid, 1039 1.1 christos status_to_str (status).c_str ()); 1040 1.9 christos return status; 1041 1.10 christos } 1042 1.1 christos 1043 1.1 christos if (WSTOPSIG (status) != SIGSTOP) 1044 1.1 christos { 1045 1.1 christos *signalled = 1; 1046 1.1 christos linux_nat_debug_printf ("Received %s after attaching", 1047 1.8 christos status_to_str (status).c_str ()); 1048 1.8 christos } 1049 1.8 christos 1050 1.8 christos return status; 1051 1.1 christos } 1052 1.8 christos 1053 1.8 christos void 1054 1.1 christos linux_nat_target::create_inferior (const char *exec_file, 1055 1.1 christos const std::string &allargs, 1056 1.1 christos char **env, int from_tty) 1057 1.1 christos { 1058 1.1 christos maybe_disable_address_space_randomization restore_personality 1059 1.8 christos (disable_randomization); 1060 1.1 christos 1061 1.8 christos /* The fork_child mechanism is synchronous and calls target_wait, so 1062 1.10 christos we have to mask the async mode. */ 1063 1.10 christos 1064 1.1 christos /* Make sure we report all signals during startup. */ 1065 1.1 christos pass_signals ({}); 1066 1.3 christos 1067 1.3 christos inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty); 1068 1.3 christos 1069 1.3 christos open_proc_mem_file (inferior_ptid); 1070 1.3 christos } 1071 1.3 christos 1072 1.3 christos /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not 1073 1.3 christos already attached. Returns true if a new LWP is found, false 1074 1.3 christos otherwise. */ 1075 1.3 christos 1076 1.3 christos static int 1077 1.3 christos attach_proc_task_lwp_callback (ptid_t ptid) 1078 1.3 christos { 1079 1.8 christos struct lwp_info *lp; 1080 1.3 christos 1081 1.3 christos /* Ignore LWPs we're already attached to. */ 1082 1.3 christos lp = find_lwp_pid (ptid); 1083 1.3 christos if (lp == NULL) 1084 1.3 christos { 1085 1.3 christos int lwpid = ptid.lwp (); 1086 1.3 christos 1087 1.3 christos if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0) 1088 1.3 christos { 1089 1.3 christos int err = errno; 1090 1.3 christos 1091 1.3 christos /* Be quiet if we simply raced with the thread exiting. 1092 1.3 christos EPERM is returned if the thread's task still exists, and 1093 1.9 christos is marked as exited or zombie, as well as other 1094 1.9 christos conditions, so in that case, confirm the status in 1095 1.9 christos /proc/PID/status. */ 1096 1.9 christos if (err == ESRCH 1097 1.3 christos || (err == EPERM && linux_proc_pid_is_gone (lwpid))) 1098 1.3 christos { 1099 1.3 christos linux_nat_debug_printf 1100 1.8 christos ("Cannot attach to lwp %d: thread is gone (%d: %s)", 1101 1.8 christos lwpid, err, safe_strerror (err)); 1102 1.8 christos 1103 1.11 christos } 1104 1.11 christos else 1105 1.3 christos { 1106 1.3 christos std::string reason 1107 1.3 christos = linux_ptrace_attach_fail_reason_string (ptid, err); 1108 1.3 christos 1109 1.9 christos error (_("Cannot attach to lwp %d: %s"), 1110 1.10 christos lwpid, reason.c_str ()); 1111 1.3 christos } 1112 1.3 christos } 1113 1.3 christos else 1114 1.3 christos { 1115 1.3 christos linux_nat_debug_printf ("PTRACE_ATTACH %s, 0, 0 (OK)", 1116 1.3 christos ptid.to_string ().c_str ()); 1117 1.3 christos 1118 1.3 christos lp = add_lwp (ptid); 1119 1.3 christos 1120 1.3 christos /* The next time we wait for this LWP we'll see a SIGSTOP as 1121 1.6 christos PTRACE_ATTACH brings it to a halt. */ 1122 1.6 christos lp->signalled = 1; 1123 1.6 christos 1124 1.3 christos /* We need to wait for a stop before being able to make the 1125 1.3 christos next ptrace call on this LWP. */ 1126 1.3 christos lp->must_set_ptrace_flags = 1; 1127 1.3 christos 1128 1.3 christos /* So that wait collects the SIGSTOP. */ 1129 1.3 christos lp->resumed = 1; 1130 1.3 christos } 1131 1.8 christos 1132 1.8 christos return 1; 1133 1.1 christos } 1134 1.1 christos return 0; 1135 1.1 christos } 1136 1.1 christos 1137 1.1 christos void 1138 1.1 christos linux_nat_target::attach (const char *args, int from_tty) 1139 1.8 christos { 1140 1.1 christos struct lwp_info *lp; 1141 1.9 christos int status; 1142 1.1 christos ptid_t ptid; 1143 1.8 christos 1144 1.1 christos /* Make sure we report all signals during attach. */ 1145 1.9 christos pass_signals ({}); 1146 1.1 christos 1147 1.1 christos try 1148 1.8 christos { 1149 1.1 christos inf_ptrace_target::attach (args, from_tty); 1150 1.8 christos } 1151 1.9 christos catch (const gdb_exception_error &ex) 1152 1.9 christos { 1153 1.3 christos pid_t pid = parse_pid_to_attach (args); 1154 1.9 christos std::string reason = linux_ptrace_attach_fail_reason (pid); 1155 1.1 christos 1156 1.1 christos if (!reason.empty ()) 1157 1.1 christos throw_error (ex.error, "warning: %s\n%s", reason.c_str (), 1158 1.1 christos ex.what ()); 1159 1.8 christos else 1160 1.10 christos throw_error (ex.error, "%s", ex.what ()); 1161 1.9 christos } 1162 1.1 christos 1163 1.1 christos /* The ptrace base target adds the main thread with (pid,0,0) 1164 1.1 christos format. Decorate it with lwp info. */ 1165 1.1 christos ptid = ptid_t (inferior_ptid.pid (), 1166 1.8 christos inferior_ptid.pid ()); 1167 1.1 christos thread_change_ptid (linux_target, inferior_ptid, ptid); 1168 1.1 christos 1169 1.1 christos /* Add the initial process as the first LWP to the list. */ 1170 1.1 christos lp = add_initial_lwp (ptid); 1171 1.1 christos 1172 1.1 christos status = linux_nat_post_attach_wait (lp->ptid, &lp->signalled); 1173 1.8 christos if (!WIFSTOPPED (status)) 1174 1.7 christos { 1175 1.1 christos if (WIFEXITED (status)) 1176 1.1 christos { 1177 1.1 christos int exit_code = WEXITSTATUS (status); 1178 1.1 christos 1179 1.1 christos target_terminal::ours (); 1180 1.1 christos target_mourn_inferior (inferior_ptid); 1181 1.1 christos if (exit_code == 0) 1182 1.1 christos error (_("Unable to attach: program exited normally.")); 1183 1.1 christos else 1184 1.1 christos error (_("Unable to attach: program exited with code %d."), 1185 1.8 christos exit_code); 1186 1.7 christos } 1187 1.1 christos else if (WIFSIGNALED (status)) 1188 1.1 christos { 1189 1.1 christos enum gdb_signal signo; 1190 1.1 christos 1191 1.1 christos target_terminal::ours (); 1192 1.1 christos target_mourn_inferior (inferior_ptid); 1193 1.1 christos 1194 1.1 christos signo = gdb_signal_from_host (WTERMSIG (status)); 1195 1.10 christos error (_("Unable to attach: program terminated with signal " 1196 1.8 christos "%s, %s."), 1197 1.1 christos gdb_signal_to_name (signo), 1198 1.1 christos gdb_signal_to_string (signo)); 1199 1.1 christos } 1200 1.1 christos 1201 1.10 christos internal_error (_("unexpected status %d for PID %ld"), 1202 1.10 christos status, (long) ptid.lwp ()); 1203 1.1 christos } 1204 1.1 christos 1205 1.9 christos lp->stopped = 1; 1206 1.10 christos 1207 1.10 christos open_proc_mem_file (lp->ptid); 1208 1.1 christos 1209 1.1 christos /* Save the wait status to report later. */ 1210 1.1 christos lp->resumed = 1; 1211 1.3 christos linux_nat_debug_printf ("waitpid %ld, saving status %s", 1212 1.3 christos (long) lp->ptid.pid (), 1213 1.3 christos status_to_str (status).c_str ()); 1214 1.3 christos 1215 1.3 christos lp->status = status; 1216 1.3 christos 1217 1.3 christos /* We must attach to every LWP. If /proc is mounted, use that to 1218 1.11 christos find them now. The inferior may be using raw clone instead of 1219 1.11 christos using pthreads. But even if it is using pthreads, thread_db 1220 1.11 christos walks structures in the inferior's address space to find the list 1221 1.11 christos of threads/LWPs, and those structures may well be corrupted. 1222 1.11 christos Note that once thread_db is loaded, we'll still use it to list 1223 1.11 christos threads and associate pthread info with each LWP. */ 1224 1.11 christos try 1225 1.11 christos { 1226 1.11 christos linux_proc_attach_tgid_threads (lp->ptid.pid (), 1227 1.11 christos attach_proc_task_lwp_callback); 1228 1.11 christos } 1229 1.11 christos catch (const gdb_exception_error &) 1230 1.11 christos { 1231 1.11 christos /* Failed to attach to some LWP. Detach any we've already 1232 1.11 christos attached to. */ 1233 1.11 christos iterate_over_lwps (ptid_t (ptid.pid ()), 1234 1.11 christos [] (struct lwp_info *lwp) -> int 1235 1.11 christos { 1236 1.11 christos /* Ignore errors when detaching. */ 1237 1.11 christos ptrace (PTRACE_DETACH, lwp->ptid.lwp (), 0, 0); 1238 1.11 christos delete_lwp (lwp->ptid); 1239 1.11 christos return 0; 1240 1.11 christos }); 1241 1.11 christos 1242 1.11 christos target_terminal::ours (); 1243 1.11 christos target_mourn_inferior (inferior_ptid); 1244 1.11 christos 1245 1.11 christos throw; 1246 1.11 christos } 1247 1.11 christos 1248 1.11 christos /* Add all the LWPs to gdb's thread list. */ 1249 1.11 christos iterate_over_lwps (ptid_t (ptid.pid ()), 1250 1.11 christos [] (struct lwp_info *lwp) -> int 1251 1.11 christos { 1252 1.11 christos if (lwp->ptid.pid () != lwp->ptid.lwp ()) 1253 1.11 christos { 1254 1.10 christos add_thread (linux_target, lwp->ptid); 1255 1.10 christos set_running (linux_target, lwp->ptid, true); 1256 1.10 christos set_executing (linux_target, lwp->ptid, true); 1257 1.3 christos } 1258 1.10 christos return 0; 1259 1.10 christos }); 1260 1.10 christos } 1261 1.10 christos 1262 1.10 christos /* Ptrace-detach the thread with pid PID. */ 1263 1.10 christos 1264 1.10 christos static void 1265 1.10 christos detach_one_pid (int pid, int signo) 1266 1.10 christos { 1267 1.10 christos if (ptrace (PTRACE_DETACH, pid, 0, signo) < 0) 1268 1.10 christos { 1269 1.10 christos int save_errno = errno; 1270 1.10 christos 1271 1.10 christos /* We know the thread exists, so ESRCH must mean the lwp is 1272 1.10 christos zombie. This can happen if one of the already-detached 1273 1.10 christos threads exits the whole thread group. In that case we're 1274 1.10 christos still attached, and must reap the lwp. */ 1275 1.10 christos if (save_errno == ESRCH) 1276 1.10 christos { 1277 1.10 christos int ret, status; 1278 1.10 christos 1279 1.10 christos ret = my_waitpid (pid, &status, __WALL); 1280 1.10 christos if (ret == -1) 1281 1.10 christos { 1282 1.10 christos warning (_("Couldn't reap LWP %d while detaching: %s"), 1283 1.10 christos pid, safe_strerror (errno)); 1284 1.10 christos } 1285 1.10 christos else if (!WIFEXITED (status) && !WIFSIGNALED (status)) 1286 1.10 christos { 1287 1.10 christos warning (_("Reaping LWP %d while detaching " 1288 1.10 christos "returned unexpected status 0x%x"), 1289 1.10 christos pid, status); 1290 1.10 christos } 1291 1.10 christos } 1292 1.10 christos else 1293 1.1 christos error (_("Can't detach %d: %s"), 1294 1.1 christos pid, safe_strerror (save_errno)); 1295 1.6 christos } 1296 1.6 christos else 1297 1.6 christos linux_nat_debug_printf ("PTRACE_DETACH (%d, %s, 0) (OK)", 1298 1.6 christos pid, strsignal (signo)); 1299 1.6 christos } 1300 1.1 christos 1301 1.6 christos /* Get pending signal of THREAD as a host signal number, for detaching 1302 1.1 christos purposes. This is the signal the thread last stopped for, which we 1303 1.1 christos need to deliver to the thread when detaching, otherwise, it'd be 1304 1.1 christos suppressed/lost. */ 1305 1.1 christos 1306 1.1 christos static int 1307 1.1 christos get_detach_signal (struct lwp_info *lp) 1308 1.1 christos { 1309 1.1 christos enum gdb_signal signo = GDB_SIGNAL_0; 1310 1.1 christos 1311 1.1 christos /* If we paused threads momentarily, we may have stored pending 1312 1.1 christos events in lp->status or lp->waitstatus (see stop_wait_callback), 1313 1.1 christos and GDB core hasn't seen any signal for those threads. 1314 1.1 christos Otherwise, the last signal reported to the core is found in the 1315 1.1 christos thread object's stop_signal. 1316 1.1 christos 1317 1.1 christos There's a corner case that isn't handled here at present. Only 1318 1.1 christos if the thread stopped with a TARGET_WAITKIND_STOPPED does 1319 1.1 christos stop_signal make sense as a real signal to pass to the inferior. 1320 1.1 christos Some catchpoint related events, like 1321 1.1 christos TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set 1322 1.1 christos to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But, 1323 1.1 christos those traps are debug API (ptrace in our case) related and 1324 1.1 christos induced; the inferior wouldn't see them if it wasn't being 1325 1.1 christos traced. Hence, we should never pass them to the inferior, even 1326 1.10 christos when set to pass state. Since this corner case isn't handled by 1327 1.1 christos infrun.c when proceeding with a signal, for consistency, neither 1328 1.1 christos do we handle it here (or elsewhere in the file we check for 1329 1.1 christos signal pass state). Normally SIGTRAP isn't set to pass state, so 1330 1.8 christos this is really a corner case. */ 1331 1.1 christos 1332 1.11 christos if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 1333 1.1 christos signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */ 1334 1.10 christos else if (lp->status) 1335 1.8 christos signo = gdb_signal_from_host (WSTOPSIG (lp->status)); 1336 1.10 christos else 1337 1.10 christos { 1338 1.10 christos thread_info *tp = linux_target->find_thread (lp->ptid); 1339 1.11 christos 1340 1.10 christos if (target_is_non_stop_p () && !tp->executing ()) 1341 1.10 christos { 1342 1.10 christos if (tp->has_pending_waitstatus ()) 1343 1.10 christos { 1344 1.10 christos /* If the thread has a pending event, and it was stopped with a 1345 1.10 christos signal, use that signal to resume it. If it has a pending 1346 1.10 christos event of another kind, it was not stopped with a signal, so 1347 1.8 christos resume it without a signal. */ 1348 1.10 christos if (tp->pending_waitstatus ().kind () == TARGET_WAITKIND_STOPPED) 1349 1.8 christos signo = tp->pending_waitstatus ().sig (); 1350 1.8 christos else 1351 1.8 christos signo = GDB_SIGNAL_0; 1352 1.8 christos } 1353 1.9 christos else 1354 1.1 christos signo = tp->stop_signal (); 1355 1.9 christos } 1356 1.1 christos else if (!target_is_non_stop_p ()) 1357 1.9 christos { 1358 1.9 christos ptid_t last_ptid; 1359 1.10 christos process_stratum_target *last_target; 1360 1.1 christos 1361 1.1 christos get_last_target_status (&last_target, &last_ptid, nullptr); 1362 1.1 christos 1363 1.1 christos if (last_target == linux_target 1364 1.1 christos && lp->ptid.lwp () == last_ptid.lwp ()) 1365 1.9 christos signo = tp->stop_signal (); 1366 1.10 christos } 1367 1.1 christos } 1368 1.1 christos 1369 1.1 christos if (signo == GDB_SIGNAL_0) 1370 1.9 christos { 1371 1.9 christos linux_nat_debug_printf ("lwp %s has no pending signal", 1372 1.10 christos lp->ptid.to_string ().c_str ()); 1373 1.1 christos } 1374 1.1 christos else if (!signal_pass_state (signo)) 1375 1.1 christos { 1376 1.9 christos linux_nat_debug_printf 1377 1.10 christos ("lwp %s had signal %s but it is in no pass state", 1378 1.9 christos lp->ptid.to_string ().c_str (), gdb_signal_to_string (signo)); 1379 1.6 christos } 1380 1.6 christos else 1381 1.1 christos { 1382 1.1 christos linux_nat_debug_printf ("lwp %s has pending signal %s", 1383 1.1 christos lp->ptid.to_string ().c_str (), 1384 1.1 christos gdb_signal_to_string (signo)); 1385 1.1 christos 1386 1.11 christos return gdb_signal_to_host (signo); 1387 1.6 christos } 1388 1.11 christos 1389 1.11 christos return 0; 1390 1.1 christos } 1391 1.11 christos 1392 1.1 christos /* If LP has a pending fork/vfork/clone status, return it. */ 1393 1.11 christos 1394 1.11 christos static std::optional<target_waitstatus> 1395 1.10 christos get_pending_child_status (lwp_info *lp) 1396 1.10 christos { 1397 1.10 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 1398 1.10 christos 1399 1.10 christos linux_nat_debug_printf ("lwp %s (stopped = %d)", 1400 1.10 christos lp->ptid.to_string ().c_str (), lp->stopped); 1401 1.11 christos 1402 1.11 christos /* Check in lwp_info::status. */ 1403 1.11 christos if (WIFSTOPPED (lp->status) && linux_is_extended_waitstatus (lp->status)) 1404 1.10 christos { 1405 1.10 christos int event = linux_ptrace_get_extended_event (lp->status); 1406 1.10 christos 1407 1.10 christos if (event == PTRACE_EVENT_FORK 1408 1.11 christos || event == PTRACE_EVENT_VFORK 1409 1.11 christos || event == PTRACE_EVENT_CLONE) 1410 1.11 christos { 1411 1.11 christos unsigned long child_pid; 1412 1.11 christos int ret = ptrace (PTRACE_GETEVENTMSG, lp->ptid.lwp (), 0, &child_pid); 1413 1.11 christos if (ret == 0) 1414 1.11 christos { 1415 1.11 christos target_waitstatus ws; 1416 1.11 christos 1417 1.11 christos if (event == PTRACE_EVENT_FORK) 1418 1.11 christos ws.set_forked (ptid_t (child_pid, child_pid)); 1419 1.11 christos else if (event == PTRACE_EVENT_VFORK) 1420 1.11 christos ws.set_vforked (ptid_t (child_pid, child_pid)); 1421 1.11 christos else if (event == PTRACE_EVENT_CLONE) 1422 1.10 christos ws.set_thread_cloned (ptid_t (lp->ptid.pid (), child_pid)); 1423 1.11 christos else 1424 1.11 christos gdb_assert_not_reached ("unhandled"); 1425 1.11 christos 1426 1.11 christos return ws; 1427 1.10 christos } 1428 1.10 christos else 1429 1.10 christos { 1430 1.10 christos perror_warning_with_name (_("Failed to retrieve event msg")); 1431 1.11 christos return {}; 1432 1.11 christos } 1433 1.10 christos } 1434 1.11 christos } 1435 1.10 christos 1436 1.10 christos /* Check in lwp_info::waitstatus. */ 1437 1.11 christos if (is_new_child_status (lp->waitstatus.kind ())) 1438 1.11 christos return lp->waitstatus; 1439 1.11 christos 1440 1.11 christos thread_info *tp = linux_target->find_thread (lp->ptid); 1441 1.11 christos 1442 1.11 christos /* Check in thread_info::pending_waitstatus. */ 1443 1.11 christos if (tp->has_pending_waitstatus () 1444 1.11 christos && is_new_child_status (tp->pending_waitstatus ().kind ())) 1445 1.11 christos return tp->pending_waitstatus (); 1446 1.11 christos 1447 1.11 christos /* Check in thread_info::pending_follow. */ 1448 1.11 christos if (is_new_child_status (tp->pending_follow.kind ())) 1449 1.11 christos return tp->pending_follow; 1450 1.11 christos 1451 1.10 christos return {}; 1452 1.11 christos } 1453 1.11 christos 1454 1.11 christos /* Detach from LP. If SIGNO_P is non-NULL, then it points to the 1455 1.11 christos signal number that should be passed to the LWP when detaching. 1456 1.11 christos Otherwise pass any pending signal the LWP may have, if any. */ 1457 1.10 christos 1458 1.11 christos static void 1459 1.11 christos detach_one_lwp (struct lwp_info *lp, int *signo_p) 1460 1.11 christos { 1461 1.11 christos int lwpid = lp->ptid.lwp (); 1462 1.11 christos int signo; 1463 1.11 christos 1464 1.11 christos /* If the lwp/thread we are about to detach has a pending fork/clone 1465 1.1 christos event, there is a process/thread GDB is attached to that the core 1466 1.1 christos of GDB doesn't know about. Detach from it. */ 1467 1.1 christos 1468 1.1 christos std::optional<target_waitstatus> ws = get_pending_child_status (lp); 1469 1.9 christos if (ws.has_value ()) 1470 1.10 christos detach_one_pid (ws->child_ptid ().lwp (), 0); 1471 1.1 christos 1472 1.6 christos /* If there is a pending SIGSTOP, get rid of it. */ 1473 1.1 christos if (lp->signalled) 1474 1.1 christos { 1475 1.1 christos linux_nat_debug_printf ("Sending SIGCONT to %s", 1476 1.11 christos lp->ptid.to_string ().c_str ()); 1477 1.11 christos 1478 1.11 christos kill_lwp (lwpid, SIGCONT); 1479 1.11 christos lp->signalled = 0; 1480 1.11 christos } 1481 1.11 christos 1482 1.11 christos /* If the lwp has exited or was terminated due to a signal, there's 1483 1.11 christos nothing left to do. */ 1484 1.11 christos if (is_lwp_marked_dead (lp)) 1485 1.11 christos { 1486 1.11 christos linux_nat_debug_printf 1487 1.11 christos ("Can't detach %s - it has exited or was terminated: %s.", 1488 1.6 christos lp->ptid.to_string ().c_str (), 1489 1.1 christos lp->waitstatus.to_string ().c_str ()); 1490 1.1 christos delete_lwp (lp->ptid); 1491 1.6 christos return; 1492 1.6 christos } 1493 1.6 christos 1494 1.6 christos if (signo_p == NULL) 1495 1.1 christos { 1496 1.11 christos /* Pass on any pending signal for this LWP. */ 1497 1.11 christos signo = get_detach_signal (lp); 1498 1.11 christos } 1499 1.11 christos else 1500 1.6 christos signo = *signo_p; 1501 1.6 christos 1502 1.6 christos linux_nat_debug_printf ("preparing to resume lwp %s (stopped = %d)", 1503 1.9 christos lp->ptid.to_string ().c_str (), 1504 1.6 christos lp->stopped); 1505 1.8 christos 1506 1.6 christos /* Preparing to resume may try to write registers, and fail if the 1507 1.9 christos lwp is zombie. If that happens, ignore the error. We'll handle 1508 1.6 christos it below, when detach fails with ESRCH. */ 1509 1.6 christos try 1510 1.9 christos { 1511 1.6 christos linux_target->low_prepare_to_resume (lp); 1512 1.6 christos } 1513 1.10 christos catch (const gdb_exception_error &ex) 1514 1.1 christos { 1515 1.6 christos if (!check_ptrace_stopped_lwp_gone (lp)) 1516 1.6 christos throw; 1517 1.6 christos } 1518 1.6 christos 1519 1.9 christos detach_one_pid (lwpid, signo); 1520 1.6 christos 1521 1.6 christos delete_lwp (lp->ptid); 1522 1.6 christos } 1523 1.6 christos 1524 1.8 christos static int 1525 1.6 christos detach_callback (struct lwp_info *lp) 1526 1.1 christos { 1527 1.1 christos /* We don't actually detach from the thread group leader just yet. 1528 1.1 christos If the thread group exits, we must reap the zombie clone lwps 1529 1.8 christos before we're able to reap the leader. */ 1530 1.8 christos if (lp->ptid.lwp () != lp->ptid.pid ()) 1531 1.1 christos detach_one_lwp (lp, NULL); 1532 1.11 christos return 0; 1533 1.11 christos } 1534 1.1 christos 1535 1.8 christos void 1536 1.1 christos linux_nat_target::detach (inferior *inf, int from_tty) 1537 1.1 christos { 1538 1.1 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 1539 1.1 christos 1540 1.1 christos struct lwp_info *main_lwp; 1541 1.9 christos int pid = inf->pid; 1542 1.9 christos 1543 1.1 christos /* Don't unregister from the event loop, as there may be other 1544 1.1 christos inferiors running. */ 1545 1.9 christos 1546 1.1 christos /* Stop all threads before detaching. ptrace requires that the 1547 1.10 christos thread is stopped to successfully detach. */ 1548 1.10 christos iterate_over_lwps (ptid_t (pid), stop_callback); 1549 1.10 christos /* ... and wait until all of them have reported back that 1550 1.10 christos they're no longer running. */ 1551 1.10 christos iterate_over_lwps (ptid_t (pid), stop_wait_callback); 1552 1.9 christos 1553 1.1 christos /* We can now safely remove breakpoints. We don't this in earlier 1554 1.11 christos in common code because this target doesn't currently support 1555 1.11 christos writing memory while the inferior is running. */ 1556 1.11 christos remove_breakpoints_inf (current_inferior ()); 1557 1.11 christos 1558 1.11 christos iterate_over_lwps (ptid_t (pid), detach_callback); 1559 1.11 christos 1560 1.1 christos /* We have detached from everything except the main thread now, so 1561 1.11 christos should only have one thread left. However, in non-stop mode the 1562 1.1 christos main thread might have exited, in which case we'll have no threads 1563 1.1 christos left. */ 1564 1.1 christos gdb_assert (num_lwps (pid) == 1 1565 1.1 christos || (target_is_non_stop_p () && num_lwps (pid) == 0)); 1566 1.1 christos 1567 1.11 christos if (pid == inferior_ptid.pid () && forks_exist_p ()) 1568 1.1 christos { 1569 1.1 christos /* Multi-fork case. The current inferior_ptid is being detached 1570 1.6 christos from, but there are other viable forks to debug. Detach from 1571 1.6 christos the current fork, and context-switch to the first 1572 1.6 christos available. */ 1573 1.11 christos linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid))); 1574 1.11 christos } 1575 1.11 christos else 1576 1.11 christos { 1577 1.11 christos target_announce_detach (from_tty); 1578 1.11 christos 1579 1.11 christos /* In non-stop mode it is possible that the main thread has exited, 1580 1.6 christos in which case we don't try to detach. */ 1581 1.11 christos main_lwp = find_lwp_pid (ptid_t (pid)); 1582 1.11 christos if (main_lwp != nullptr) 1583 1.11 christos { 1584 1.11 christos /* Pass on any pending signal for the last LWP. */ 1585 1.6 christos int signo = get_detach_signal (main_lwp); 1586 1.8 christos 1587 1.6 christos detach_one_lwp (main_lwp, &signo); 1588 1.10 christos } 1589 1.10 christos else 1590 1.1 christos gdb_assert (target_is_non_stop_p ()); 1591 1.1 christos 1592 1.3 christos detach_success (inf); 1593 1.3 christos } 1594 1.3 christos 1595 1.3 christos close_proc_mem_file (pid); 1596 1.5 christos } 1597 1.5 christos 1598 1.3 christos /* Resume execution of the inferior process. If STEP is nonzero, 1599 1.3 christos single-step it. If SIGNAL is nonzero, give it that signal. */ 1600 1.3 christos 1601 1.3 christos static void 1602 1.3 christos linux_resume_one_lwp_throw (struct lwp_info *lp, int step, 1603 1.3 christos enum gdb_signal signo) 1604 1.3 christos { 1605 1.3 christos lp->step = step; 1606 1.9 christos 1607 1.3 christos /* stop_pc doubles as the PC the LWP had when it was last resumed. 1608 1.3 christos We only presently need that if the LWP is stepped though (to 1609 1.3 christos handle the case of stepping a breakpoint instruction). */ 1610 1.3 christos if (step) 1611 1.3 christos { 1612 1.3 christos struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 1613 1.8 christos 1614 1.8 christos lp->stop_pc = regcache_read_pc (regcache); 1615 1.5 christos } 1616 1.5 christos else 1617 1.5 christos lp->stop_pc = 0; 1618 1.5 christos 1619 1.5 christos linux_target->low_prepare_to_resume (lp); 1620 1.5 christos linux_target->low_resume (lp->ptid, step, signo); 1621 1.5 christos 1622 1.3 christos /* Successfully resumed. Clear state that no longer makes sense, 1623 1.6 christos and mark the LWP as running. Must not do this before resuming 1624 1.5 christos otherwise if that fails other code will be confused. E.g., we'd 1625 1.9 christos later try to stop the LWP and hang forever waiting for a stop 1626 1.3 christos status. Note that we must not throw after this is cleared, 1627 1.3 christos otherwise handle_zombie_lwp_error would get confused. */ 1628 1.5 christos lp->stopped = 0; 1629 1.5 christos lp->core = -1; 1630 1.5 christos lp->stop_reason = TARGET_STOPPED_BY_NO_REASON; 1631 1.5 christos registers_changed_ptid (linux_target, lp->ptid); 1632 1.5 christos } 1633 1.5 christos 1634 1.5 christos /* Called when we try to resume a stopped LWP and that errors out. If 1635 1.5 christos the LWP is no longer in ptrace-stopped state (meaning it's zombie, 1636 1.5 christos or about to become), discard the error, clear any pending status 1637 1.5 christos the LWP may have, and return true (we'll collect the exit status 1638 1.5 christos soon enough). Otherwise, return false. */ 1639 1.5 christos 1640 1.5 christos static int 1641 1.5 christos check_ptrace_stopped_lwp_gone (struct lwp_info *lp) 1642 1.5 christos { 1643 1.5 christos /* If we get an error after resuming the LWP successfully, we'd 1644 1.5 christos confuse !T state for the LWP being gone. */ 1645 1.5 christos gdb_assert (lp->stopped); 1646 1.5 christos 1647 1.5 christos /* We can't just check whether the LWP is in 'Z (Zombie)' state, 1648 1.5 christos because even if ptrace failed with ESRCH, the tracee may be "not 1649 1.5 christos yet fully dead", but already refusing ptrace requests. In that 1650 1.8 christos case the tracee has 'R (Running)' state for a little bit 1651 1.5 christos (observed in Linux 3.18). See also the note on ESRCH in the 1652 1.5 christos ptrace(2) man page. Instead, check whether the LWP has any state 1653 1.5 christos other than ptrace-stopped. */ 1654 1.10 christos 1655 1.5 christos /* Don't assume anything if /proc/PID/status can't be read. */ 1656 1.5 christos if (linux_proc_pid_is_trace_stopped_nowarn (lp->ptid.lwp ()) == 0) 1657 1.5 christos { 1658 1.5 christos lp->stop_reason = TARGET_STOPPED_BY_NO_REASON; 1659 1.5 christos lp->status = 0; 1660 1.5 christos lp->waitstatus.set_ignore (); 1661 1.5 christos return 1; 1662 1.5 christos } 1663 1.5 christos return 0; 1664 1.5 christos } 1665 1.5 christos 1666 1.9 christos /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP 1667 1.5 christos disappears while we try to resume it. */ 1668 1.5 christos 1669 1.5 christos static void 1670 1.9 christos linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo) 1671 1.5 christos { 1672 1.5 christos try 1673 1.9 christos { 1674 1.5 christos linux_resume_one_lwp_throw (lp, step, signo); 1675 1.5 christos } 1676 1.5 christos catch (const gdb_exception_error &ex) 1677 1.1 christos { 1678 1.1 christos if (!check_ptrace_stopped_lwp_gone (lp)) 1679 1.1 christos throw; 1680 1.1 christos } 1681 1.1 christos } 1682 1.1 christos 1683 1.1 christos /* Resume LP. */ 1684 1.9 christos 1685 1.1 christos static void 1686 1.1 christos resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo) 1687 1.1 christos { 1688 1.11 christos if (lp->stopped) 1689 1.10 christos { 1690 1.1 christos struct inferior *inf = find_inferior_ptid (linux_target, lp->ptid); 1691 1.3 christos 1692 1.1 christos if (inf->vfork_child != NULL) 1693 1.9 christos { 1694 1.10 christos linux_nat_debug_printf ("Not resuming sibling %s (vfork parent)", 1695 1.9 christos lp->ptid.to_string ().c_str ()); 1696 1.9 christos } 1697 1.9 christos else if (!lwp_status_pending_p (lp)) 1698 1.9 christos { 1699 1.1 christos linux_nat_debug_printf ("Resuming sibling %s, %s, %s", 1700 1.3 christos lp->ptid.to_string ().c_str (), 1701 1.1 christos (signo != GDB_SIGNAL_0 1702 1.1 christos ? strsignal (gdb_signal_to_host (signo)) 1703 1.1 christos : "0"), 1704 1.9 christos step ? "step" : "resume"); 1705 1.10 christos 1706 1.1 christos linux_resume_one_lwp (lp, step, signo); 1707 1.1 christos } 1708 1.1 christos else 1709 1.9 christos { 1710 1.10 christos linux_nat_debug_printf ("Not resuming sibling %s (has pending)", 1711 1.1 christos lp->ptid.to_string ().c_str ()); 1712 1.1 christos } 1713 1.3 christos } 1714 1.3 christos else 1715 1.1 christos linux_nat_debug_printf ("Not resuming sibling %s (not stopped)", 1716 1.1 christos lp->ptid.to_string ().c_str ()); 1717 1.9 christos } 1718 1.1 christos 1719 1.1 christos /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing. 1720 1.1 christos Resume LWP with the last stop signal, if it is in pass state. */ 1721 1.3 christos 1722 1.3 christos static int 1723 1.3 christos linux_nat_resume_callback (struct lwp_info *lp, struct lwp_info *except) 1724 1.1 christos { 1725 1.1 christos enum gdb_signal signo = GDB_SIGNAL_0; 1726 1.1 christos 1727 1.1 christos if (lp == except) 1728 1.11 christos return 0; 1729 1.1 christos 1730 1.1 christos if (lp->stopped) 1731 1.10 christos { 1732 1.10 christos struct thread_info *thread; 1733 1.1 christos 1734 1.1 christos thread = linux_target->find_thread (lp->ptid); 1735 1.1 christos if (thread != NULL) 1736 1.1 christos { 1737 1.1 christos signo = thread->stop_signal (); 1738 1.1 christos thread->set_stop_signal (GDB_SIGNAL_0); 1739 1.1 christos } 1740 1.1 christos } 1741 1.9 christos 1742 1.1 christos resume_lwp (lp, 0, signo); 1743 1.1 christos return 0; 1744 1.1 christos } 1745 1.1 christos 1746 1.1 christos static int 1747 1.1 christos resume_clear_callback (struct lwp_info *lp) 1748 1.1 christos { 1749 1.9 christos lp->resumed = 0; 1750 1.1 christos lp->last_resume_kind = resume_stop; 1751 1.1 christos return 0; 1752 1.1 christos } 1753 1.1 christos 1754 1.1 christos static int 1755 1.1 christos resume_set_callback (struct lwp_info *lp) 1756 1.8 christos { 1757 1.10 christos lp->resumed = 1; 1758 1.1 christos lp->last_resume_kind = resume_continue; 1759 1.1 christos return 0; 1760 1.1 christos } 1761 1.9 christos 1762 1.9 christos void 1763 1.10 christos linux_nat_target::resume (ptid_t scope_ptid, int step, enum gdb_signal signo) 1764 1.9 christos { 1765 1.9 christos struct lwp_info *lp; 1766 1.10 christos 1767 1.1 christos linux_nat_debug_printf ("Preparing to %s %s, %s, inferior_ptid %s", 1768 1.9 christos step ? "step" : "resume", 1769 1.9 christos scope_ptid.to_string ().c_str (), 1770 1.10 christos (signo != GDB_SIGNAL_0 1771 1.1 christos ? strsignal (gdb_signal_to_host (signo)) : "0"), 1772 1.10 christos inferior_ptid.to_string ().c_str ()); 1773 1.1 christos 1774 1.1 christos /* Mark the lwps we're resuming as resumed and update their 1775 1.1 christos last_resume_kind to resume_continue. */ 1776 1.1 christos iterate_over_lwps (scope_ptid, resume_set_callback); 1777 1.1 christos 1778 1.1 christos lp = find_lwp_pid (inferior_ptid); 1779 1.1 christos gdb_assert (lp != NULL); 1780 1.1 christos 1781 1.1 christos /* Remember if we're stepping. */ 1782 1.1 christos lp->last_resume_kind = step ? resume_step : resume_continue; 1783 1.1 christos 1784 1.1 christos /* If we have a pending wait status for this thread, there is no 1785 1.1 christos point in resuming the process. But first make sure that 1786 1.1 christos linux_nat_wait won't preemptively handle the event - we 1787 1.1 christos should never take this short-circuit if we are going to 1788 1.1 christos leave LP running, since we have skipped resuming all the 1789 1.1 christos other threads. This bit of code needs to be synchronized 1790 1.1 christos with linux_nat_wait. */ 1791 1.1 christos 1792 1.9 christos if (lp->status && WIFSTOPPED (lp->status)) 1793 1.9 christos { 1794 1.1 christos if (!lp->step 1795 1.1 christos && WSTOPSIG (lp->status) 1796 1.1 christos && sigismember (&pass_mask, WSTOPSIG (lp->status))) 1797 1.1 christos { 1798 1.1 christos linux_nat_debug_printf 1799 1.1 christos ("Not short circuiting for ignored status 0x%x", lp->status); 1800 1.1 christos 1801 1.1 christos /* FIXME: What should we do if we are supposed to continue 1802 1.1 christos this thread with a signal? */ 1803 1.3 christos gdb_assert (signo == GDB_SIGNAL_0); 1804 1.1 christos signo = gdb_signal_from_host (WSTOPSIG (lp->status)); 1805 1.1 christos lp->status = 0; 1806 1.1 christos } 1807 1.1 christos } 1808 1.1 christos 1809 1.11 christos if (lwp_status_pending_p (lp)) 1810 1.11 christos { 1811 1.1 christos /* FIXME: What should we do if we are supposed to continue 1812 1.1 christos this thread with a signal? */ 1813 1.1 christos gdb_assert (signo == GDB_SIGNAL_0); 1814 1.10 christos 1815 1.1 christos linux_nat_debug_printf ("Short circuiting for status %s", 1816 1.1 christos pending_status_str (lp).c_str ()); 1817 1.1 christos 1818 1.1 christos if (target_can_async_p ()) 1819 1.1 christos { 1820 1.1 christos target_async (true); 1821 1.10 christos /* Tell the event loop we have something to process. */ 1822 1.10 christos async_file_mark (); 1823 1.10 christos } 1824 1.10 christos return; 1825 1.10 christos } 1826 1.10 christos 1827 1.9 christos /* No use iterating unless we're resuming other threads. */ 1828 1.9 christos if (scope_ptid != lp->ptid) 1829 1.9 christos iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info) 1830 1.10 christos { 1831 1.9 christos return linux_nat_resume_callback (info, lp); 1832 1.9 christos }); 1833 1.1 christos 1834 1.5 christos linux_nat_debug_printf ("%s %s, %s (resume event thread)", 1835 1.1 christos step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 1836 1.1 christos lp->ptid.to_string ().c_str (), 1837 1.1 christos (signo != GDB_SIGNAL_0 1838 1.1 christos ? strsignal (gdb_signal_to_host (signo)) : "0")); 1839 1.1 christos 1840 1.1 christos linux_resume_one_lwp (lp, step, signo); 1841 1.1 christos } 1842 1.6 christos 1843 1.1 christos /* Send a signal to an LWP. */ 1844 1.6 christos 1845 1.6 christos static int 1846 1.6 christos kill_lwp (int lwpid, int signo) 1847 1.6 christos { 1848 1.6 christos int ret; 1849 1.6 christos 1850 1.6 christos errno = 0; 1851 1.6 christos ret = syscall (__NR_tkill, lwpid, signo); 1852 1.6 christos if (errno == ENOSYS) 1853 1.1 christos { 1854 1.1 christos /* If tkill fails, then we are not using nptl threads, a 1855 1.1 christos configuration we no longer support. */ 1856 1.1 christos perror_with_name (("tkill")); 1857 1.1 christos } 1858 1.1 christos return ret; 1859 1.1 christos } 1860 1.1 christos 1861 1.1 christos /* Handle a GNU/Linux syscall trap wait response. If we see a syscall 1862 1.1 christos event, check if the core is interested in it: if not, ignore the 1863 1.1 christos event, and keep waiting; otherwise, we need to toggle the LWP's 1864 1.1 christos syscall entry/exit status, since the ptrace event itself doesn't 1865 1.1 christos indicate it, and report the trap to higher layers. */ 1866 1.11 christos 1867 1.8 christos static int 1868 1.1 christos linux_handle_syscall_trap (struct lwp_info *lp, int stopping) 1869 1.1 christos { 1870 1.1 christos struct target_waitstatus *ourstatus = &lp->waitstatus; 1871 1.1 christos struct gdbarch *gdbarch = target_thread_architecture (lp->ptid); 1872 1.1 christos thread_info *thread = linux_target->find_thread (lp->ptid); 1873 1.1 christos int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, thread); 1874 1.1 christos 1875 1.1 christos if (stopping) 1876 1.1 christos { 1877 1.1 christos /* If we're stopping threads, there's a SIGSTOP pending, which 1878 1.1 christos makes it so that the LWP reports an immediate syscall return, 1879 1.1 christos followed by the SIGSTOP. Skip seeing that "return" using 1880 1.1 christos PTRACE_CONT directly, and let stop_wait_callback collect the 1881 1.1 christos SIGSTOP. Later when the thread is resumed, a new syscall 1882 1.1 christos entry event. If we didn't do this (and returned 0), we'd 1883 1.1 christos leave a syscall entry pending, and our caller, by using 1884 1.1 christos PTRACE_CONT to collect the SIGSTOP, skips the syscall return 1885 1.1 christos itself. Later, when the user re-resumes this LWP, we'd see 1886 1.1 christos another syscall entry event and we'd mistake it for a return. 1887 1.1 christos 1888 1.1 christos If stop_wait_callback didn't force the SIGSTOP out of the LWP 1889 1.1 christos (leaving immediately with LWP->signalled set, without issuing 1890 1.1 christos a PTRACE_CONT), it would still be problematic to leave this 1891 1.9 christos syscall enter pending, as later when the thread is resumed, 1892 1.9 christos it would then see the same syscall exit mentioned above, 1893 1.9 christos followed by the delayed SIGSTOP, while the syscall didn't 1894 1.1 christos actually get to execute. It seems it would be even more 1895 1.1 christos confusing to the user. */ 1896 1.8 christos 1897 1.3 christos linux_nat_debug_printf 1898 1.1 christos ("ignoring syscall %d for LWP %ld (stopping threads), resuming with " 1899 1.1 christos "PTRACE_CONT for SIGSTOP", syscall_number, lp->ptid.lwp ()); 1900 1.1 christos 1901 1.6 christos lp->syscall_state = TARGET_WAITKIND_IGNORE; 1902 1.6 christos ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0); 1903 1.6 christos lp->stopped = 0; 1904 1.6 christos return 1; 1905 1.6 christos } 1906 1.6 christos 1907 1.6 christos /* Always update the entry/return state, even if this particular 1908 1.6 christos syscall isn't interesting to the core now. In async mode, 1909 1.6 christos the user could install a new catchpoint for this syscall 1910 1.1 christos between syscall enter/return, and we'll need to know to 1911 1.1 christos report a syscall return if that happens. */ 1912 1.1 christos lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1913 1.1 christos ? TARGET_WAITKIND_SYSCALL_RETURN 1914 1.1 christos : TARGET_WAITKIND_SYSCALL_ENTRY); 1915 1.10 christos 1916 1.10 christos if (catch_syscall_enabled ()) 1917 1.10 christos { 1918 1.10 christos if (catching_syscall_number (syscall_number)) 1919 1.10 christos { 1920 1.10 christos /* Alright, an event to report. */ 1921 1.1 christos if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY) 1922 1.9 christos ourstatus->set_syscall_entry (syscall_number); 1923 1.9 christos else if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN) 1924 1.9 christos ourstatus->set_syscall_return (syscall_number); 1925 1.9 christos else 1926 1.9 christos gdb_assert_not_reached ("unexpected syscall state"); 1927 1.1 christos 1928 1.1 christos linux_nat_debug_printf 1929 1.1 christos ("stopping for %s of syscall %d for LWP %ld", 1930 1.9 christos (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1931 1.9 christos ? "entry" : "return"), syscall_number, lp->ptid.lwp ()); 1932 1.9 christos 1933 1.9 christos return 0; 1934 1.1 christos } 1935 1.1 christos 1936 1.1 christos linux_nat_debug_printf 1937 1.1 christos ("ignoring %s of syscall %d for LWP %ld", 1938 1.1 christos (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY 1939 1.1 christos ? "entry" : "return"), syscall_number, lp->ptid.lwp ()); 1940 1.1 christos } 1941 1.1 christos else 1942 1.1 christos { 1943 1.1 christos /* If we had been syscall tracing, and hence used PT_SYSCALL 1944 1.1 christos before on this LWP, it could happen that the user removes all 1945 1.1 christos syscall catchpoints before we get to process this event. 1946 1.1 christos There are two noteworthy issues here: 1947 1.1 christos 1948 1.1 christos - When stopped at a syscall entry event, resuming with 1949 1.1 christos PT_STEP still resumes executing the syscall and reports a 1950 1.1 christos syscall return. 1951 1.1 christos 1952 1.1 christos - Only PT_SYSCALL catches syscall enters. If we last 1953 1.9 christos single-stepped this thread, then this event can't be a 1954 1.9 christos syscall enter. If we last single-stepped this thread, this 1955 1.9 christos has to be a syscall exit. 1956 1.1 christos 1957 1.1 christos The points above mean that the next resume, be it PT_STEP or 1958 1.1 christos PT_CONTINUE, can not trigger a syscall trace event. */ 1959 1.1 christos linux_nat_debug_printf 1960 1.1 christos ("caught syscall event with no syscall catchpoints. %d for LWP %ld, " 1961 1.1 christos "ignoring", syscall_number, lp->ptid.lwp ()); 1962 1.1 christos lp->syscall_state = TARGET_WAITKIND_IGNORE; 1963 1.1 christos } 1964 1.1 christos 1965 1.1 christos /* The core isn't interested in this event. For efficiency, avoid 1966 1.3 christos stopping all threads only to have the core resume them all again. 1967 1.1 christos Since we're not stopping threads, if we're still syscall tracing 1968 1.1 christos and not stepping, we can't use PTRACE_CONT here, as we'd miss any 1969 1.1 christos subsequent syscall. Simply resume using the inf-ptrace layer, 1970 1.11 christos which knows when to use PT_SYSCALL or PT_CONTINUE. */ 1971 1.11 christos 1972 1.11 christos linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 1973 1.11 christos return 1; 1974 1.11 christos } 1975 1.11 christos 1976 1.11 christos /* See target.h. */ 1977 1.11 christos 1978 1.11 christos void 1979 1.11 christos linux_nat_target::follow_clone (ptid_t child_ptid) 1980 1.11 christos { 1981 1.11 christos lwp_info *new_lp = add_lwp (child_ptid); 1982 1.11 christos new_lp->stopped = 1; 1983 1.11 christos 1984 1.11 christos /* If the thread_db layer is active, let it record the user 1985 1.11 christos level thread id and status, and add the thread to GDB's 1986 1.11 christos list. */ 1987 1.11 christos if (!thread_db_notice_clone (inferior_ptid, new_lp->ptid)) 1988 1.11 christos { 1989 1.11 christos /* The process is not using thread_db. Add the LWP to 1990 1.11 christos GDB's list. */ 1991 1.11 christos add_thread (linux_target, new_lp->ptid); 1992 1.11 christos } 1993 1.11 christos 1994 1.11 christos /* We just created NEW_LP so it cannot yet contain STATUS. */ 1995 1.11 christos gdb_assert (new_lp->status == 0); 1996 1.11 christos 1997 1.11 christos if (!pull_pid_from_list (&stopped_pids, child_ptid.lwp (), &new_lp->status)) 1998 1.11 christos internal_error (_("no saved status for clone lwp")); 1999 1.11 christos 2000 1.11 christos if (WSTOPSIG (new_lp->status) != SIGSTOP) 2001 1.11 christos { 2002 1.11 christos /* This can happen if someone starts sending signals to 2003 1.11 christos the new thread before it gets a chance to run, which 2004 1.11 christos have a lower number than SIGSTOP (e.g. SIGUSR1). 2005 1.11 christos This is an unlikely case, and harder to handle for 2006 1.11 christos fork / vfork than for clone, so we do not try - but 2007 1.11 christos we handle it for clone events here. */ 2008 1.11 christos 2009 1.11 christos new_lp->signalled = 1; 2010 1.11 christos 2011 1.11 christos /* Save the wait status to report later. */ 2012 1.11 christos linux_nat_debug_printf 2013 1.11 christos ("waitpid of new LWP %ld, saving status %s", 2014 1.11 christos (long) new_lp->ptid.lwp (), status_to_str (new_lp->status).c_str ()); 2015 1.11 christos } 2016 1.11 christos else 2017 1.11 christos { 2018 1.11 christos new_lp->status = 0; 2019 1.1 christos 2020 1.1 christos if (report_thread_events) 2021 1.1 christos new_lp->waitstatus.set_thread_created (); 2022 1.1 christos } 2023 1.1 christos } 2024 1.1 christos 2025 1.1 christos /* Handle a GNU/Linux extended wait response. If we see a clone 2026 1.5 christos event, we need to add the new LWP to our list (and not report the 2027 1.1 christos trap to higher layers). This function returns non-zero if the 2028 1.8 christos event should be ignored and we should wait again. If STOPPING is 2029 1.1 christos true, the new LWP remains stopped, otherwise it is continued. */ 2030 1.3 christos 2031 1.1 christos static int 2032 1.6 christos linux_handle_extended_wait (struct lwp_info *lp, int status) 2033 1.6 christos { 2034 1.6 christos int pid = lp->ptid.lwp (); 2035 1.6 christos struct target_waitstatus *ourstatus = &lp->waitstatus; 2036 1.6 christos int event = linux_ptrace_get_extended_event (status); 2037 1.1 christos 2038 1.1 christos /* All extended events we currently use are mid-syscall. Only 2039 1.1 christos PTRACE_EVENT_STOP is delivered more like a signal-stop, but 2040 1.1 christos you have to be using PTRACE_SEIZE to get that. */ 2041 1.1 christos lp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY; 2042 1.1 christos 2043 1.1 christos if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK 2044 1.1 christos || event == PTRACE_EVENT_CLONE) 2045 1.1 christos { 2046 1.1 christos unsigned long new_pid; 2047 1.1 christos int ret; 2048 1.1 christos 2049 1.1 christos ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid); 2050 1.6 christos 2051 1.1 christos /* If we haven't already seen the new PID stop, wait for it now. */ 2052 1.1 christos if (! pull_pid_from_list (&stopped_pids, new_pid, &status)) 2053 1.1 christos { 2054 1.10 christos /* The new child has a pending SIGSTOP. We can't affect it until it 2055 1.1 christos hits the SIGSTOP, but we're already attached. */ 2056 1.10 christos ret = my_waitpid (new_pid, &status, __WALL); 2057 1.1 christos if (ret == -1) 2058 1.1 christos perror_with_name (_("waiting for new child")); 2059 1.1 christos else if (ret != new_pid) 2060 1.1 christos internal_error (_("wait returned unexpected PID %d"), ret); 2061 1.11 christos else if (!WIFSTOPPED (status)) 2062 1.10 christos internal_error (_("wait returned unexpected status 0x%x"), status); 2063 1.1 christos } 2064 1.1 christos 2065 1.1 christos if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK) 2066 1.8 christos { 2067 1.1 christos open_proc_mem_file (ptid_t (new_pid, new_pid)); 2068 1.9 christos 2069 1.9 christos /* The arch-specific native code may need to know about new 2070 1.9 christos forks even if those end up never mapped to an 2071 1.9 christos inferior. */ 2072 1.1 christos linux_target->low_new_fork (lp, new_pid); 2073 1.1 christos } 2074 1.8 christos else if (event == PTRACE_EVENT_CLONE) 2075 1.1 christos { 2076 1.1 christos linux_target->low_new_clone (lp, new_pid); 2077 1.1 christos } 2078 1.1 christos 2079 1.1 christos if (event == PTRACE_EVENT_FORK 2080 1.1 christos && linux_fork_checkpointing_p (lp->ptid.pid ())) 2081 1.1 christos { 2082 1.10 christos /* Handle checkpointing by linux-fork.c here as a special 2083 1.1 christos case. We don't want the follow-fork-mode or 'catch fork' 2084 1.1 christos to interfere with this. */ 2085 1.1 christos 2086 1.1 christos /* This won't actually modify the breakpoint list, but will 2087 1.1 christos physically remove the breakpoints from the child. */ 2088 1.1 christos detach_breakpoints (ptid_t (new_pid, new_pid)); 2089 1.1 christos 2090 1.1 christos /* Retain child fork in ptrace (stopped) state. */ 2091 1.10 christos if (!find_fork_pid (new_pid)) 2092 1.1 christos add_fork (new_pid); 2093 1.1 christos 2094 1.1 christos /* Report as spurious, so that infrun doesn't want to follow 2095 1.1 christos this fork. We're actually doing an infcall in 2096 1.1 christos linux-fork.c. */ 2097 1.1 christos ourstatus->set_spurious (); 2098 1.11 christos 2099 1.1 christos /* Report the stop to the core. */ 2100 1.11 christos return 0; 2101 1.5 christos } 2102 1.1 christos 2103 1.9 christos if (event == PTRACE_EVENT_FORK) 2104 1.9 christos ourstatus->set_forked (ptid_t (new_pid, new_pid)); 2105 1.1 christos else if (event == PTRACE_EVENT_VFORK) 2106 1.11 christos ourstatus->set_vforked (ptid_t (new_pid, new_pid)); 2107 1.11 christos else if (event == PTRACE_EVENT_CLONE) 2108 1.1 christos { 2109 1.11 christos linux_nat_debug_printf 2110 1.1 christos ("Got clone event from LWP %d, new child is LWP %ld", pid, new_pid); 2111 1.1 christos 2112 1.1 christos /* Save the status again, we'll use it in follow_clone. */ 2113 1.1 christos add_to_pid_list (&stopped_pids, new_pid, status); 2114 1.1 christos 2115 1.1 christos ourstatus->set_thread_cloned (ptid_t (lp->ptid.pid (), new_pid)); 2116 1.1 christos } 2117 1.9 christos 2118 1.1 christos return 0; 2119 1.10 christos } 2120 1.10 christos 2121 1.10 christos if (event == PTRACE_EVENT_EXEC) 2122 1.10 christos { 2123 1.10 christos linux_nat_debug_printf ("Got exec event from LWP %ld", lp->ptid.lwp ()); 2124 1.10 christos 2125 1.10 christos /* Close the previous /proc/PID/mem file for this inferior, 2126 1.10 christos which was using the address space which is now gone. 2127 1.10 christos Reading/writing from this file would return 0/EOF. */ 2128 1.10 christos close_proc_mem_file (lp->ptid.pid ()); 2129 1.1 christos 2130 1.3 christos /* Open a new file for the new address space. */ 2131 1.3 christos open_proc_mem_file (lp->ptid); 2132 1.3 christos 2133 1.3 christos ourstatus->set_execd 2134 1.11 christos (make_unique_xstrdup (linux_proc_pid_to_exec_file (pid))); 2135 1.11 christos 2136 1.11 christos /* The thread that execed must have been resumed, but, when a 2137 1.11 christos thread execs, it changes its tid to the tgid, and the old 2138 1.11 christos tgid thread might have not been resumed. */ 2139 1.11 christos lp->resumed = 1; 2140 1.11 christos 2141 1.11 christos /* All other LWPs are gone now. We'll have received a thread 2142 1.11 christos exit notification for all threads other the execing one. 2143 1.11 christos That one, if it wasn't the leader, just silently changes its 2144 1.11 christos tid to the tgid, and the previous leader vanishes. Since 2145 1.11 christos Linux 3.0, the former thread ID can be retrieved with 2146 1.11 christos PTRACE_GETEVENTMSG, but since we support older kernels, don't 2147 1.11 christos bother with it, and just walk the LWP list. Even with 2148 1.11 christos PTRACE_GETEVENTMSG, we'd still need to lookup the 2149 1.1 christos corresponding LWP object, and it would be an extra ptrace 2150 1.1 christos syscall, so this way may even be more efficient. */ 2151 1.1 christos for (lwp_info *other_lp : all_lwps_safe ()) 2152 1.1 christos if (other_lp != lp && other_lp->ptid.pid () == lp->ptid.pid ()) 2153 1.1 christos exit_lwp (other_lp); 2154 1.9 christos 2155 1.10 christos return 0; 2156 1.10 christos } 2157 1.10 christos 2158 1.10 christos if (event == PTRACE_EVENT_VFORK_DONE) 2159 1.1 christos { 2160 1.1 christos linux_nat_debug_printf 2161 1.10 christos ("Got PTRACE_EVENT_VFORK_DONE from LWP %ld", 2162 1.1 christos lp->ptid.lwp ()); 2163 1.1 christos ourstatus->set_vfork_done (); 2164 1.8 christos return 0; 2165 1.8 christos } 2166 1.8 christos 2167 1.8 christos internal_error (_("unknown ptrace event %d"), event); 2168 1.8 christos } 2169 1.8 christos 2170 1.9 christos /* Suspend waiting for a signal. We're mostly interested in 2171 1.8 christos SIGCHLD/SIGINT. */ 2172 1.8 christos 2173 1.8 christos static void 2174 1.8 christos wait_for_signal () 2175 1.8 christos { 2176 1.8 christos linux_nat_debug_printf ("about to sigsuspend"); 2177 1.8 christos sigsuspend (&suspend_mask); 2178 1.8 christos 2179 1.8 christos /* If the quit flag is set, it means that the user pressed Ctrl-C 2180 1.8 christos and we're debugging a process that is running on a separate 2181 1.8 christos terminal, so we must forward the Ctrl-C to the inferior. (If the 2182 1.8 christos inferior is sharing GDB's terminal, then the Ctrl-C reaches the 2183 1.8 christos inferior directly.) We must do this here because functions that 2184 1.8 christos need to block waiting for a signal loop forever until there's an 2185 1.8 christos event to report before returning back to the event loop. */ 2186 1.8 christos if (!target_terminal::is_ours ()) 2187 1.11 christos { 2188 1.11 christos if (check_quit_flag ()) 2189 1.11 christos target_pass_ctrlc (); 2190 1.11 christos } 2191 1.11 christos } 2192 1.11 christos 2193 1.11 christos /* Mark LWP dead, with STATUS as exit status pending to report 2194 1.11 christos later. */ 2195 1.11 christos 2196 1.11 christos static void 2197 1.11 christos mark_lwp_dead (lwp_info *lp, int status) 2198 1.11 christos { 2199 1.11 christos /* Store the exit status lp->waitstatus, because lp->status would be 2200 1.11 christos ambiguous (W_EXITCODE(0,0) == 0). */ 2201 1.11 christos lp->waitstatus = host_status_to_waitstatus (status); 2202 1.11 christos 2203 1.11 christos /* If we're processing LP's status, there should be no other event 2204 1.11 christos already recorded as pending. */ 2205 1.11 christos gdb_assert (lp->status == 0); 2206 1.11 christos 2207 1.11 christos /* Dead LWPs aren't expected to report a pending sigstop. */ 2208 1.11 christos lp->signalled = 0; 2209 1.11 christos 2210 1.11 christos /* Prevent trying to stop it. */ 2211 1.11 christos lp->stopped = 1; 2212 1.11 christos } 2213 1.11 christos 2214 1.11 christos /* Return true if LP is dead, with a pending exit/signalled event. */ 2215 1.11 christos 2216 1.11 christos static bool 2217 1.11 christos is_lwp_marked_dead (lwp_info *lp) 2218 1.11 christos { 2219 1.11 christos switch (lp->waitstatus.kind ()) 2220 1.11 christos { 2221 1.11 christos case TARGET_WAITKIND_EXITED: 2222 1.11 christos case TARGET_WAITKIND_THREAD_EXITED: 2223 1.1 christos case TARGET_WAITKIND_SIGNALLED: 2224 1.1 christos return true; 2225 1.1 christos } 2226 1.1 christos return false; 2227 1.1 christos } 2228 1.1 christos 2229 1.1 christos /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has 2230 1.1 christos exited. */ 2231 1.1 christos 2232 1.1 christos static int 2233 1.1 christos wait_lwp (struct lwp_info *lp) 2234 1.1 christos { 2235 1.1 christos pid_t pid; 2236 1.1 christos int status = 0; 2237 1.1 christos int thread_dead = 0; 2238 1.1 christos sigset_t prev_mask; 2239 1.1 christos 2240 1.1 christos gdb_assert (!lp->stopped); 2241 1.1 christos gdb_assert (lp->status == 0); 2242 1.8 christos 2243 1.1 christos /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */ 2244 1.1 christos block_child_signals (&prev_mask); 2245 1.1 christos 2246 1.6 christos for (;;) 2247 1.6 christos { 2248 1.6 christos pid = my_waitpid (lp->ptid.lwp (), &status, __WALL | WNOHANG); 2249 1.1 christos if (pid == -1 && errno == ECHILD) 2250 1.9 christos { 2251 1.10 christos /* The thread has previously exited. We need to delete it 2252 1.1 christos now because if this was a non-leader thread execing, we 2253 1.1 christos won't get an exit event. See comments on exec events at 2254 1.1 christos the top of the file. */ 2255 1.1 christos thread_dead = 1; 2256 1.1 christos linux_nat_debug_printf ("%s vanished.", 2257 1.1 christos lp->ptid.to_string ().c_str ()); 2258 1.1 christos } 2259 1.1 christos if (pid != 0) 2260 1.6 christos break; 2261 1.6 christos 2262 1.1 christos /* Bugs 10970, 12702. 2263 1.1 christos Thread group leader may have exited in which case we'll lock up in 2264 1.1 christos waitpid if there are other threads, even if they are all zombies too. 2265 1.1 christos Basically, we're not supposed to use waitpid this way. 2266 1.1 christos tkill(pid,0) cannot be used here as it gets ESRCH for both 2267 1.1 christos for zombie and running processes. 2268 1.1 christos 2269 1.1 christos As a workaround, check if we're waiting for the thread group leader and 2270 1.8 christos if it's a zombie, and avoid calling waitpid if it is. 2271 1.8 christos 2272 1.1 christos This is racy, what if the tgl becomes a zombie right after we check? 2273 1.1 christos Therefore always use WNOHANG with sigsuspend - it is equivalent to 2274 1.9 christos waiting waitpid but linux_proc_pid_is_zombie is safe this way. */ 2275 1.10 christos 2276 1.1 christos if (lp->ptid.pid () == lp->ptid.lwp () 2277 1.1 christos && linux_proc_pid_is_zombie (lp->ptid.lwp ())) 2278 1.1 christos { 2279 1.1 christos thread_dead = 1; 2280 1.1 christos linux_nat_debug_printf ("Thread group leader %s vanished.", 2281 1.1 christos lp->ptid.to_string ().c_str ()); 2282 1.1 christos break; 2283 1.1 christos } 2284 1.1 christos 2285 1.8 christos /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers 2286 1.1 christos get invoked despite our caller had them intentionally blocked by 2287 1.1 christos block_child_signals. This is sensitive only to the loop of 2288 1.1 christos linux_nat_wait_1 and there if we get called my_waitpid gets called 2289 1.1 christos again before it gets to sigsuspend so we can safely let the handlers 2290 1.1 christos get executed here. */ 2291 1.1 christos wait_for_signal (); 2292 1.8 christos } 2293 1.1 christos 2294 1.9 christos restore_child_signals_mask (&prev_mask); 2295 1.10 christos 2296 1.10 christos if (!thread_dead) 2297 1.1 christos { 2298 1.1 christos gdb_assert (pid == lp->ptid.lwp ()); 2299 1.1 christos 2300 1.1 christos linux_nat_debug_printf ("waitpid %s received %s", 2301 1.11 christos lp->ptid.to_string ().c_str (), 2302 1.5 christos status_to_str (status).c_str ()); 2303 1.9 christos 2304 1.5 christos /* Check if the thread has exited. */ 2305 1.6 christos if (WIFEXITED (status) || WIFSIGNALED (status)) 2306 1.5 christos { 2307 1.11 christos if (report_exit_events_for (lp) || is_leader (lp)) 2308 1.11 christos { 2309 1.5 christos linux_nat_debug_printf ("LWP %d exited.", lp->ptid.pid ()); 2310 1.5 christos 2311 1.5 christos /* If this is the leader exiting, it means the whole 2312 1.1 christos process is gone. Store the status to report to the 2313 1.9 christos core. */ 2314 1.10 christos mark_lwp_dead (lp, status); 2315 1.1 christos return 0; 2316 1.1 christos } 2317 1.1 christos 2318 1.1 christos thread_dead = 1; 2319 1.1 christos linux_nat_debug_printf ("%s exited.", 2320 1.1 christos lp->ptid.to_string ().c_str ()); 2321 1.1 christos } 2322 1.1 christos } 2323 1.1 christos 2324 1.1 christos if (thread_dead) 2325 1.3 christos { 2326 1.3 christos exit_lwp (lp); 2327 1.3 christos return 0; 2328 1.3 christos } 2329 1.9 christos 2330 1.5 christos gdb_assert (WIFSTOPPED (status)); 2331 1.3 christos lp->stopped = 1; 2332 1.8 christos 2333 1.3 christos if (lp->must_set_ptrace_flags) 2334 1.3 christos { 2335 1.1 christos inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ()); 2336 1.1 christos int options = linux_nat_ptrace_options (inf->attach_flag); 2337 1.1 christos 2338 1.1 christos linux_enable_event_reporting (lp->ptid.lwp (), options); 2339 1.1 christos lp->must_set_ptrace_flags = 0; 2340 1.1 christos } 2341 1.1 christos 2342 1.1 christos /* Handle GNU/Linux's syscall SIGTRAPs. */ 2343 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP) 2344 1.1 christos { 2345 1.1 christos /* No longer need the sysgood bit. The ptrace event ends up 2346 1.1 christos recorded in lp->waitstatus if we care for it. We can carry 2347 1.6 christos on handling the event like a regular SIGTRAP from here 2348 1.6 christos on. */ 2349 1.6 christos status = W_STOPCODE (SIGTRAP); 2350 1.6 christos if (linux_handle_syscall_trap (lp, 1)) 2351 1.6 christos return wait_lwp (lp); 2352 1.6 christos } 2353 1.1 christos else 2354 1.1 christos { 2355 1.3 christos /* Almost all other ptrace-stops are known to be outside of system 2356 1.3 christos calls, with further exceptions in linux_handle_extended_wait. */ 2357 1.1 christos lp->syscall_state = TARGET_WAITKIND_IGNORE; 2358 1.9 christos } 2359 1.5 christos 2360 1.5 christos /* Handle GNU/Linux's extended waitstatus for trace events. */ 2361 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP 2362 1.1 christos && linux_is_extended_waitstatus (status)) 2363 1.1 christos { 2364 1.1 christos linux_nat_debug_printf ("Handling extended status 0x%06x", status); 2365 1.1 christos linux_handle_extended_wait (lp, status); 2366 1.1 christos return 0; 2367 1.1 christos } 2368 1.1 christos 2369 1.9 christos return status; 2370 1.1 christos } 2371 1.1 christos 2372 1.1 christos /* Send a SIGSTOP to LP. */ 2373 1.1 christos 2374 1.1 christos static int 2375 1.9 christos stop_callback (struct lwp_info *lp) 2376 1.10 christos { 2377 1.9 christos if (!lp->stopped && !lp->signalled) 2378 1.1 christos { 2379 1.8 christos int ret; 2380 1.9 christos 2381 1.1 christos linux_nat_debug_printf ("kill %s **<SIGSTOP>**", 2382 1.1 christos lp->ptid.to_string ().c_str ()); 2383 1.1 christos 2384 1.1 christos errno = 0; 2385 1.1 christos ret = kill_lwp (lp->ptid.lwp (), SIGSTOP); 2386 1.1 christos linux_nat_debug_printf ("lwp kill %d %s", ret, 2387 1.1 christos errno ? safe_strerror (errno) : "ERRNO-OK"); 2388 1.1 christos 2389 1.1 christos lp->signalled = 1; 2390 1.1 christos gdb_assert (lp->status == 0); 2391 1.1 christos } 2392 1.1 christos 2393 1.1 christos return 0; 2394 1.1 christos } 2395 1.9 christos 2396 1.1 christos /* Request a stop on LWP. */ 2397 1.1 christos 2398 1.5 christos void 2399 1.5 christos linux_stop_lwp (struct lwp_info *lwp) 2400 1.5 christos { 2401 1.5 christos stop_callback (lwp); 2402 1.5 christos } 2403 1.5 christos 2404 1.9 christos /* See linux-nat.h */ 2405 1.5 christos 2406 1.5 christos void 2407 1.5 christos linux_stop_and_wait_all_lwps (void) 2408 1.9 christos { 2409 1.5 christos /* Stop all LWP's ... */ 2410 1.5 christos iterate_over_lwps (minus_one_ptid, stop_callback); 2411 1.5 christos 2412 1.5 christos /* ... and wait until all of them have reported back that 2413 1.5 christos they're no longer running. */ 2414 1.5 christos iterate_over_lwps (minus_one_ptid, stop_wait_callback); 2415 1.5 christos } 2416 1.5 christos 2417 1.9 christos /* See linux-nat.h */ 2418 1.9 christos 2419 1.9 christos void 2420 1.9 christos linux_unstop_all_lwps (void) 2421 1.5 christos { 2422 1.5 christos iterate_over_lwps (minus_one_ptid, 2423 1.1 christos [] (struct lwp_info *info) 2424 1.1 christos { 2425 1.1 christos return resume_stopped_resumed_lwps (info, minus_one_ptid); 2426 1.1 christos }); 2427 1.1 christos } 2428 1.1 christos 2429 1.1 christos /* Return non-zero if LWP PID has a pending SIGINT. */ 2430 1.1 christos 2431 1.1 christos static int 2432 1.1 christos linux_nat_has_pending_sigint (int pid) 2433 1.1 christos { 2434 1.1 christos sigset_t pending, blocked, ignored; 2435 1.1 christos 2436 1.1 christos linux_proc_pending_signals (pid, &pending, &blocked, &ignored); 2437 1.1 christos 2438 1.1 christos if (sigismember (&pending, SIGINT) 2439 1.1 christos && !sigismember (&ignored, SIGINT)) 2440 1.1 christos return 1; 2441 1.1 christos 2442 1.9 christos return 0; 2443 1.1 christos } 2444 1.1 christos 2445 1.1 christos /* Set a flag in LP indicating that we should ignore its next SIGINT. */ 2446 1.1 christos 2447 1.1 christos static int 2448 1.1 christos set_ignore_sigint (struct lwp_info *lp) 2449 1.1 christos { 2450 1.1 christos /* If a thread has a pending SIGINT, consume it; otherwise, set a 2451 1.1 christos flag to consume the next one. */ 2452 1.1 christos if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status) 2453 1.1 christos && WSTOPSIG (lp->status) == SIGINT) 2454 1.1 christos lp->status = 0; 2455 1.1 christos else 2456 1.1 christos lp->ignore_sigint = 1; 2457 1.1 christos 2458 1.1 christos return 0; 2459 1.1 christos } 2460 1.1 christos 2461 1.1 christos /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag. 2462 1.1 christos This function is called after we know the LWP has stopped; if the LWP 2463 1.1 christos stopped before the expected SIGINT was delivered, then it will never have 2464 1.1 christos arrived. Also, if the signal was delivered to a shared queue and consumed 2465 1.1 christos by a different thread, it will never be delivered to this LWP. */ 2466 1.1 christos 2467 1.8 christos static void 2468 1.1 christos maybe_clear_ignore_sigint (struct lwp_info *lp) 2469 1.9 christos { 2470 1.10 christos if (!lp->ignore_sigint) 2471 1.1 christos return; 2472 1.1 christos 2473 1.1 christos if (!linux_nat_has_pending_sigint (lp->ptid.lwp ())) 2474 1.1 christos { 2475 1.1 christos linux_nat_debug_printf ("Clearing bogus flag for %s", 2476 1.1 christos lp->ptid.to_string ().c_str ()); 2477 1.1 christos lp->ignore_sigint = 0; 2478 1.1 christos } 2479 1.1 christos } 2480 1.1 christos 2481 1.1 christos /* Fetch the possible triggered data watchpoint info and store it in 2482 1.1 christos LP. 2483 1.1 christos 2484 1.1 christos On some archs, like x86, that use debug registers to set 2485 1.1 christos watchpoints, it's possible that the way to know which watched 2486 1.1 christos address trapped, is to check the register that is used to select 2487 1.1 christos which address to watch. Problem is, between setting the watchpoint 2488 1.1 christos and reading back which data address trapped, the user may change 2489 1.1 christos the set of watchpoints, and, as a consequence, GDB changes the 2490 1.3 christos debug registers in the inferior. To avoid reading back a stale 2491 1.3 christos stopped-data-address when that happens, we cache in LP the fact 2492 1.1 christos that a watchpoint trapped, and the corresponding data address, as 2493 1.8 christos soon as we see LP stop with a SIGTRAP. If GDB changes the debug 2494 1.1 christos registers meanwhile, we have the cached data we can rely on. */ 2495 1.1 christos 2496 1.8 christos static int 2497 1.3 christos check_stopped_by_watchpoint (struct lwp_info *lp) 2498 1.5 christos { 2499 1.8 christos scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); 2500 1.8 christos inferior_ptid = lp->ptid; 2501 1.1 christos 2502 1.1 christos if (linux_target->low_stopped_by_watchpoint ()) 2503 1.5 christos { 2504 1.3 christos lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT; 2505 1.3 christos lp->stopped_data_address_p 2506 1.3 christos = linux_target->low_stopped_data_address (&lp->stopped_data_address); 2507 1.1 christos } 2508 1.8 christos 2509 1.8 christos return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT; 2510 1.1 christos } 2511 1.1 christos 2512 1.1 christos /* Returns true if the LWP had stopped for a watchpoint. */ 2513 1.1 christos 2514 1.1 christos bool 2515 1.5 christos linux_nat_target::stopped_by_watchpoint () 2516 1.1 christos { 2517 1.1 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2518 1.8 christos 2519 1.8 christos gdb_assert (lp != NULL); 2520 1.1 christos 2521 1.1 christos return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT; 2522 1.1 christos } 2523 1.1 christos 2524 1.1 christos bool 2525 1.1 christos linux_nat_target::stopped_data_address (CORE_ADDR *addr_p) 2526 1.1 christos { 2527 1.1 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2528 1.1 christos 2529 1.1 christos gdb_assert (lp != NULL); 2530 1.1 christos 2531 1.1 christos *addr_p = lp->stopped_data_address; 2532 1.8 christos 2533 1.8 christos return lp->stopped_data_address_p; 2534 1.1 christos } 2535 1.1 christos 2536 1.1 christos /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */ 2537 1.1 christos 2538 1.1 christos bool 2539 1.1 christos linux_nat_target::low_status_is_event (int status) 2540 1.1 christos { 2541 1.9 christos return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP; 2542 1.1 christos } 2543 1.9 christos 2544 1.1 christos /* Wait until LP is stopped. */ 2545 1.1 christos 2546 1.1 christos static int 2547 1.1 christos stop_wait_callback (struct lwp_info *lp) 2548 1.1 christos { 2549 1.1 christos inferior *inf = find_inferior_ptid (linux_target, lp->ptid); 2550 1.1 christos 2551 1.1 christos /* If this is a vfork parent, bail out, it is not going to report 2552 1.1 christos any SIGSTOP until the vfork is done with. */ 2553 1.1 christos if (inf->vfork_child != NULL) 2554 1.1 christos return 0; 2555 1.1 christos 2556 1.1 christos if (!lp->stopped) 2557 1.1 christos { 2558 1.1 christos int status; 2559 1.1 christos 2560 1.1 christos status = wait_lwp (lp); 2561 1.1 christos if (status == 0) 2562 1.1 christos return 0; 2563 1.1 christos 2564 1.8 christos if (lp->ignore_sigint && WIFSTOPPED (status) 2565 1.3 christos && WSTOPSIG (status) == SIGINT) 2566 1.9 christos { 2567 1.9 christos lp->ignore_sigint = 0; 2568 1.10 christos 2569 1.9 christos errno = 0; 2570 1.1 christos ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0); 2571 1.9 christos lp->stopped = 0; 2572 1.1 christos linux_nat_debug_printf 2573 1.1 christos ("PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)", 2574 1.1 christos lp->ptid.to_string ().c_str (), 2575 1.1 christos errno ? safe_strerror (errno) : "OK"); 2576 1.1 christos 2577 1.1 christos return stop_wait_callback (lp); 2578 1.1 christos } 2579 1.1 christos 2580 1.9 christos maybe_clear_ignore_sigint (lp); 2581 1.10 christos 2582 1.10 christos if (WSTOPSIG (status) != SIGSTOP) 2583 1.1 christos { 2584 1.1 christos /* The thread was stopped with a signal other than SIGSTOP. */ 2585 1.1 christos 2586 1.1 christos linux_nat_debug_printf ("Pending event %s in %s", 2587 1.6 christos status_to_str ((int) status).c_str (), 2588 1.1 christos lp->ptid.to_string ().c_str ()); 2589 1.1 christos 2590 1.1 christos /* Save the sigtrap event. */ 2591 1.8 christos lp->status = status; 2592 1.1 christos gdb_assert (lp->signalled); 2593 1.9 christos save_stop_reason (lp); 2594 1.10 christos } 2595 1.1 christos else 2596 1.1 christos { 2597 1.8 christos /* We caught the SIGSTOP that we intended to catch. */ 2598 1.8 christos 2599 1.8 christos linux_nat_debug_printf ("Expected SIGSTOP caught for %s.", 2600 1.8 christos lp->ptid.to_string ().c_str ()); 2601 1.8 christos 2602 1.8 christos lp->signalled = 0; 2603 1.8 christos 2604 1.8 christos /* If we are waiting for this stop so we can report the thread 2605 1.8 christos stopped then we need to record this status. Otherwise, we can 2606 1.1 christos now discard this stop event. */ 2607 1.1 christos if (lp->last_resume_kind == resume_stop) 2608 1.1 christos { 2609 1.1 christos lp->status = status; 2610 1.1 christos save_stop_reason (lp); 2611 1.1 christos } 2612 1.11 christos } 2613 1.11 christos } 2614 1.11 christos 2615 1.11 christos return 0; 2616 1.11 christos } 2617 1.11 christos 2618 1.11 christos /* Get the inferior associated to LWP. Must be called with an LWP that has 2619 1.11 christos an associated inferior. Always return non-nullptr. */ 2620 1.11 christos 2621 1.11 christos static inferior * 2622 1.11 christos lwp_inferior (const lwp_info *lwp) 2623 1.3 christos { 2624 1.3 christos inferior *inf = find_inferior_ptid (linux_target, lwp->ptid); 2625 1.3 christos gdb_assert (inf != nullptr); 2626 1.1 christos return inf; 2627 1.1 christos } 2628 1.9 christos 2629 1.1 christos /* Return non-zero if LP has a wait status pending. Discard the 2630 1.1 christos pending event and resume the LWP if the event that originally 2631 1.1 christos caused the stop became uninteresting. */ 2632 1.1 christos 2633 1.1 christos static int 2634 1.1 christos status_callback (struct lwp_info *lp) 2635 1.5 christos { 2636 1.5 christos /* Only report a pending wait status if we pretend that this has 2637 1.5 christos indeed been resumed. */ 2638 1.5 christos if (!lp->resumed) 2639 1.5 christos return 0; 2640 1.1 christos 2641 1.9 christos if (!lwp_status_pending_p (lp)) 2642 1.3 christos return 0; 2643 1.3 christos 2644 1.3 christos if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT 2645 1.3 christos || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT) 2646 1.3 christos { 2647 1.3 christos struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 2648 1.3 christos CORE_ADDR pc; 2649 1.9 christos int discard = 0; 2650 1.10 christos 2651 1.11 christos pc = regcache_read_pc (regcache); 2652 1.11 christos 2653 1.11 christos if (pc != lp->stop_pc) 2654 1.3 christos { 2655 1.3 christos linux_nat_debug_printf ("PC of %s changed. was=%s, now=%s", 2656 1.5 christos lp->ptid.to_string ().c_str (), 2657 1.3 christos paddress (current_inferior ()->arch (), 2658 1.3 christos lp->stop_pc), 2659 1.9 christos paddress (current_inferior ()->arch (), pc)); 2660 1.10 christos discard = 1; 2661 1.3 christos } 2662 1.3 christos 2663 1.3 christos if (discard) 2664 1.3 christos { 2665 1.3 christos linux_nat_debug_printf ("pending event of %s cancelled.", 2666 1.1 christos lp->ptid.to_string ().c_str ()); 2667 1.1 christos 2668 1.5 christos lp->status = 0; 2669 1.1 christos linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 2670 1.1 christos return 0; 2671 1.1 christos } 2672 1.1 christos } 2673 1.1 christos 2674 1.9 christos return 1; 2675 1.1 christos } 2676 1.1 christos 2677 1.1 christos /* Count the LWP's that have had events. */ 2678 1.3 christos 2679 1.3 christos static int 2680 1.1 christos count_events_callback (struct lwp_info *lp, int *count) 2681 1.1 christos { 2682 1.1 christos gdb_assert (count != NULL); 2683 1.1 christos 2684 1.1 christos /* Select only resumed LWPs that have an event pending. */ 2685 1.1 christos if (lp->resumed && lwp_status_pending_p (lp)) 2686 1.1 christos (*count)++; 2687 1.1 christos 2688 1.9 christos return 0; 2689 1.1 christos } 2690 1.1 christos 2691 1.1 christos /* Select the LWP (if any) that is currently being single-stepped. */ 2692 1.1 christos 2693 1.1 christos static int 2694 1.1 christos select_singlestep_lwp_callback (struct lwp_info *lp) 2695 1.1 christos { 2696 1.1 christos if (lp->last_resume_kind == resume_step 2697 1.3 christos && lp->status != 0) 2698 1.3 christos return 1; 2699 1.3 christos else 2700 1.3 christos return 0; 2701 1.3 christos } 2702 1.3 christos 2703 1.3 christos /* Returns true if LP has a status pending. */ 2704 1.3 christos 2705 1.10 christos static int 2706 1.3 christos lwp_status_pending_p (struct lwp_info *lp) 2707 1.3 christos { 2708 1.5 christos /* We check for lp->waitstatus in addition to lp->status, because we 2709 1.1 christos can have pending process exits recorded in lp->status and 2710 1.1 christos W_EXITCODE(0,0) happens to be 0. */ 2711 1.9 christos return lp->status != 0 || lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE; 2712 1.1 christos } 2713 1.1 christos 2714 1.1 christos /* Select the Nth LWP that has had an event. */ 2715 1.3 christos 2716 1.3 christos static int 2717 1.1 christos select_event_lwp_callback (struct lwp_info *lp, int *selector) 2718 1.1 christos { 2719 1.1 christos gdb_assert (selector != NULL); 2720 1.1 christos 2721 1.1 christos /* Select only resumed LWPs that have an event pending. */ 2722 1.1 christos if (lp->resumed && lwp_status_pending_p (lp)) 2723 1.6 christos if ((*selector)-- == 0) 2724 1.6 christos return 1; 2725 1.6 christos 2726 1.6 christos return 0; 2727 1.6 christos } 2728 1.3 christos 2729 1.6 christos /* Called when the LWP stopped for a signal/trap. If it stopped for a 2730 1.6 christos trap check what caused it (breakpoint, watchpoint, trace, etc.), 2731 1.1 christos and save the result in the LWP's stop_reason field. If it stopped 2732 1.6 christos for a breakpoint, decrement the PC if necessary on the lwp's 2733 1.6 christos architecture. */ 2734 1.1 christos 2735 1.3 christos static void 2736 1.5 christos save_stop_reason (struct lwp_info *lp) 2737 1.3 christos { 2738 1.6 christos struct regcache *regcache; 2739 1.6 christos struct gdbarch *gdbarch; 2740 1.6 christos CORE_ADDR pc; 2741 1.8 christos CORE_ADDR sw_bp_pc; 2742 1.6 christos siginfo_t siginfo; 2743 1.6 christos 2744 1.11 christos gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON); 2745 1.10 christos gdb_assert (lp->status != 0); 2746 1.10 christos 2747 1.10 christos if (!linux_target->low_status_is_event (lp->status)) 2748 1.9 christos return; 2749 1.8 christos 2750 1.6 christos inferior *inf = lwp_inferior (lp); 2751 1.3 christos if (inf->starting_up) 2752 1.5 christos return; 2753 1.1 christos 2754 1.5 christos regcache = get_thread_regcache (linux_target, lp->ptid); 2755 1.5 christos gdbarch = regcache->arch (); 2756 1.5 christos 2757 1.5 christos pc = regcache_read_pc (regcache); 2758 1.6 christos sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch); 2759 1.6 christos 2760 1.6 christos if (linux_nat_get_siginfo (lp->ptid, &siginfo)) 2761 1.6 christos { 2762 1.6 christos if (siginfo.si_signo == SIGTRAP) 2763 1.6 christos { 2764 1.6 christos if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code) 2765 1.6 christos && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code)) 2766 1.6 christos { 2767 1.5 christos /* The si_code is ambiguous on this arch -- check debug 2768 1.6 christos registers. */ 2769 1.6 christos if (!check_stopped_by_watchpoint (lp)) 2770 1.9 christos lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT; 2771 1.6 christos } 2772 1.6 christos else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)) 2773 1.5 christos { 2774 1.5 christos /* If we determine the LWP stopped for a SW breakpoint, 2775 1.6 christos trust it. Particularly don't check watchpoint 2776 1.5 christos registers, because, at least on s390, we'd find 2777 1.6 christos stopped-by-watchpoint as long as there's a watchpoint 2778 1.6 christos set. */ 2779 1.6 christos lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT; 2780 1.6 christos } 2781 1.5 christos else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code)) 2782 1.5 christos { 2783 1.5 christos /* This can indicate either a hardware breakpoint or 2784 1.9 christos hardware watchpoint. Check debug registers. */ 2785 1.10 christos if (!check_stopped_by_watchpoint (lp)) 2786 1.6 christos lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT; 2787 1.6 christos } 2788 1.6 christos else if (siginfo.si_code == TRAP_TRACE) 2789 1.6 christos { 2790 1.6 christos linux_nat_debug_printf ("%s stopped by trace", 2791 1.6 christos lp->ptid.to_string ().c_str ()); 2792 1.6 christos 2793 1.5 christos /* We may have single stepped an instruction that 2794 1.5 christos triggered a watchpoint. In that case, on some 2795 1.5 christos architectures (such as x86), instead of TRAP_HWBKPT, 2796 1.6 christos si_code indicates TRAP_TRACE, and we need to check 2797 1.6 christos the debug registers separately. */ 2798 1.6 christos check_stopped_by_watchpoint (lp); 2799 1.9 christos } 2800 1.10 christos } 2801 1.1 christos } 2802 1.1 christos 2803 1.3 christos if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT) 2804 1.3 christos { 2805 1.1 christos linux_nat_debug_printf ("%s stopped by software breakpoint", 2806 1.6 christos lp->ptid.to_string ().c_str ()); 2807 1.6 christos 2808 1.6 christos /* Back up the PC if necessary. */ 2809 1.6 christos if (pc != sw_bp_pc) 2810 1.6 christos regcache_write_pc (regcache, sw_bp_pc); 2811 1.9 christos 2812 1.10 christos /* Update this so we record the correct stop PC below. */ 2813 1.1 christos pc = sw_bp_pc; 2814 1.6 christos } 2815 1.3 christos else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT) 2816 1.9 christos { 2817 1.10 christos linux_nat_debug_printf ("%s stopped by hardware breakpoint", 2818 1.3 christos lp->ptid.to_string ().c_str ()); 2819 1.1 christos } 2820 1.6 christos else if (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT) 2821 1.1 christos { 2822 1.1 christos linux_nat_debug_printf ("%s stopped by hardware watchpoint", 2823 1.5 christos lp->ptid.to_string ().c_str ()); 2824 1.5 christos } 2825 1.5 christos 2826 1.8 christos lp->stop_pc = pc; 2827 1.8 christos } 2828 1.5 christos 2829 1.5 christos 2830 1.5 christos /* Returns true if the LWP had stopped for a software breakpoint. */ 2831 1.5 christos 2832 1.5 christos bool 2833 1.5 christos linux_nat_target::stopped_by_sw_breakpoint () 2834 1.5 christos { 2835 1.5 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2836 1.5 christos 2837 1.5 christos gdb_assert (lp != NULL); 2838 1.8 christos 2839 1.8 christos return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT; 2840 1.5 christos } 2841 1.11 christos 2842 1.5 christos /* Implement the supports_stopped_by_sw_breakpoint method. */ 2843 1.5 christos 2844 1.5 christos bool 2845 1.5 christos linux_nat_target::supports_stopped_by_sw_breakpoint () 2846 1.5 christos { 2847 1.8 christos return true; 2848 1.8 christos } 2849 1.5 christos 2850 1.5 christos /* Returns true if the LWP had stopped for a hardware 2851 1.5 christos breakpoint/watchpoint. */ 2852 1.5 christos 2853 1.5 christos bool 2854 1.5 christos linux_nat_target::stopped_by_hw_breakpoint () 2855 1.5 christos { 2856 1.5 christos struct lwp_info *lp = find_lwp_pid (inferior_ptid); 2857 1.5 christos 2858 1.5 christos gdb_assert (lp != NULL); 2859 1.8 christos 2860 1.8 christos return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT; 2861 1.5 christos } 2862 1.11 christos 2863 1.5 christos /* Implement the supports_stopped_by_hw_breakpoint method. */ 2864 1.5 christos 2865 1.1 christos bool 2866 1.1 christos linux_nat_target::supports_stopped_by_hw_breakpoint () 2867 1.1 christos { 2868 1.1 christos return true; 2869 1.1 christos } 2870 1.1 christos 2871 1.1 christos /* Select one LWP out of those that have events pending. */ 2872 1.3 christos 2873 1.1 christos static void 2874 1.1 christos select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status) 2875 1.1 christos { 2876 1.1 christos int num_events = 0; 2877 1.3 christos int random_selector; 2878 1.3 christos struct lwp_info *event_lp = NULL; 2879 1.3 christos 2880 1.3 christos /* Record the wait status for the original LWP. */ 2881 1.3 christos (*orig_lp)->status = *status; 2882 1.3 christos 2883 1.3 christos /* In all-stop, give preference to the LWP that is being 2884 1.3 christos single-stepped. There will be at most one, and it will be the 2885 1.3 christos LWP that the core is most interested in. If we didn't do this, 2886 1.6 christos then we'd have to handle pending step SIGTRAPs somehow in case 2887 1.1 christos the core later continues the previously-stepped thread, as 2888 1.9 christos otherwise we'd report the pending SIGTRAP then, and the core, not 2889 1.3 christos having stepped the thread, wouldn't understand what the trap was 2890 1.3 christos for, and therefore would report it to the user as a random 2891 1.9 christos signal. */ 2892 1.10 christos if (!target_is_non_stop_p ()) 2893 1.3 christos { 2894 1.1 christos event_lp = iterate_over_lwps (filter, select_singlestep_lwp_callback); 2895 1.3 christos if (event_lp != NULL) 2896 1.3 christos { 2897 1.1 christos linux_nat_debug_printf ("Select single-step %s", 2898 1.3 christos event_lp->ptid.to_string ().c_str ()); 2899 1.1 christos } 2900 1.3 christos } 2901 1.9 christos 2902 1.9 christos if (event_lp == NULL) 2903 1.9 christos { 2904 1.9 christos /* Pick one at random, out of those which have had events. */ 2905 1.9 christos 2906 1.5 christos /* First see how many events we have. */ 2907 1.1 christos iterate_over_lwps (filter, 2908 1.3 christos [&] (struct lwp_info *info) 2909 1.3 christos { 2910 1.1 christos return count_events_callback (info, &num_events); 2911 1.1 christos }); 2912 1.1 christos gdb_assert (num_events > 0); 2913 1.9 christos 2914 1.9 christos /* Now randomly pick a LWP out of those that have had 2915 1.9 christos events. */ 2916 1.9 christos random_selector = (int) 2917 1.9 christos ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); 2918 1.9 christos 2919 1.9 christos if (num_events > 1) 2920 1.9 christos linux_nat_debug_printf ("Found %d events, selecting #%d", 2921 1.9 christos num_events, random_selector); 2922 1.9 christos 2923 1.9 christos event_lp 2924 1.9 christos = (iterate_over_lwps 2925 1.1 christos (filter, 2926 1.1 christos [&] (struct lwp_info *info) 2927 1.1 christos { 2928 1.1 christos return select_event_lwp_callback (info, 2929 1.1 christos &random_selector); 2930 1.1 christos })); 2931 1.1 christos } 2932 1.1 christos 2933 1.1 christos if (event_lp != NULL) 2934 1.1 christos { 2935 1.1 christos /* Switch the event LWP. */ 2936 1.1 christos *orig_lp = event_lp; 2937 1.1 christos *status = event_lp->status; 2938 1.1 christos } 2939 1.1 christos 2940 1.1 christos /* Flush the wait status for the event LWP. */ 2941 1.9 christos (*orig_lp)->status = 0; 2942 1.1 christos } 2943 1.1 christos 2944 1.1 christos /* Return non-zero if LP has been resumed. */ 2945 1.1 christos 2946 1.1 christos static int 2947 1.1 christos resumed_callback (struct lwp_info *lp) 2948 1.10 christos { 2949 1.10 christos return lp->resumed; 2950 1.10 christos } 2951 1.3 christos 2952 1.1 christos /* Check if we should go on and pass this event to common code. 2953 1.1 christos 2954 1.3 christos If so, save the status to the lwp_info structure associated to LWPID. */ 2955 1.1 christos 2956 1.8 christos static void 2957 1.1 christos linux_nat_filter_event (int lwpid, int status) 2958 1.10 christos { 2959 1.10 christos struct lwp_info *lp; 2960 1.1 christos int event = linux_ptrace_get_extended_event (status); 2961 1.10 christos 2962 1.10 christos lp = find_lwp_pid (ptid_t (lwpid)); 2963 1.10 christos 2964 1.10 christos /* Check for events reported by anything not in our LWP list. */ 2965 1.10 christos if (lp == nullptr) 2966 1.10 christos { 2967 1.10 christos if (WIFSTOPPED (status)) 2968 1.10 christos { 2969 1.10 christos if (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC) 2970 1.10 christos { 2971 1.10 christos /* A non-leader thread exec'ed after we've seen the 2972 1.1 christos leader zombie, and removed it from our lists (in 2973 1.10 christos check_zombie_leaders). The non-leader thread changes 2974 1.10 christos its tid to the tgid. */ 2975 1.10 christos linux_nat_debug_printf 2976 1.10 christos ("Re-adding thread group leader LWP %d after exec.", 2977 1.10 christos lwpid); 2978 1.10 christos 2979 1.10 christos lp = add_lwp (ptid_t (lwpid, lwpid)); 2980 1.10 christos lp->stopped = 1; 2981 1.10 christos lp->resumed = 1; 2982 1.10 christos add_thread (linux_target, lp->ptid); 2983 1.10 christos } 2984 1.10 christos else 2985 1.10 christos { 2986 1.10 christos /* A process we are controlling has forked and the new 2987 1.10 christos child's stop was reported to us by the kernel. Save 2988 1.10 christos its PID and go back to waiting for the fork event to 2989 1.10 christos be reported - the stopped process might be returned 2990 1.10 christos from waitpid before or after the fork event is. */ 2991 1.10 christos linux_nat_debug_printf 2992 1.10 christos ("Saving LWP %d status %s in stopped_pids list", 2993 1.10 christos lwpid, status_to_str (status).c_str ()); 2994 1.10 christos add_to_pid_list (&stopped_pids, lwpid, status); 2995 1.10 christos } 2996 1.10 christos } 2997 1.10 christos else 2998 1.10 christos { 2999 1.10 christos /* Don't report an event for the exit of an LWP not in our 3000 1.10 christos list, i.e. not part of any inferior we're debugging. 3001 1.10 christos This can happen if we detach from a program we originally 3002 1.10 christos forked and then it exits. However, note that we may have 3003 1.10 christos earlier deleted a leader of an inferior we're debugging, 3004 1.10 christos in check_zombie_leaders. Re-add it back here if so. */ 3005 1.10 christos for (inferior *inf : all_inferiors (linux_target)) 3006 1.10 christos { 3007 1.10 christos if (inf->pid == lwpid) 3008 1.10 christos { 3009 1.10 christos linux_nat_debug_printf 3010 1.10 christos ("Re-adding thread group leader LWP %d after exit.", 3011 1.10 christos lwpid); 3012 1.10 christos 3013 1.10 christos lp = add_lwp (ptid_t (lwpid, lwpid)); 3014 1.1 christos lp->resumed = 1; 3015 1.10 christos add_thread (linux_target, lp->ptid); 3016 1.10 christos break; 3017 1.1 christos } 3018 1.1 christos } 3019 1.3 christos } 3020 1.3 christos 3021 1.3 christos if (lp == nullptr) 3022 1.3 christos return; 3023 1.3 christos } 3024 1.3 christos 3025 1.9 christos /* This LWP is stopped now. (And if dead, this prevents it from 3026 1.5 christos ever being continued.) */ 3027 1.3 christos lp->stopped = 1; 3028 1.8 christos 3029 1.3 christos if (WIFSTOPPED (status) && lp->must_set_ptrace_flags) 3030 1.3 christos { 3031 1.3 christos inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ()); 3032 1.1 christos int options = linux_nat_ptrace_options (inf->attach_flag); 3033 1.1 christos 3034 1.1 christos linux_enable_event_reporting (lp->ptid.lwp (), options); 3035 1.1 christos lp->must_set_ptrace_flags = 0; 3036 1.1 christos } 3037 1.1 christos 3038 1.1 christos /* Handle GNU/Linux's syscall SIGTRAPs. */ 3039 1.1 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP) 3040 1.1 christos { 3041 1.10 christos /* No longer need the sysgood bit. The ptrace event ends up 3042 1.1 christos recorded in lp->waitstatus if we care for it. We can carry 3043 1.6 christos on handling the event like a regular SIGTRAP from here 3044 1.6 christos on. */ 3045 1.6 christos status = W_STOPCODE (SIGTRAP); 3046 1.6 christos if (linux_handle_syscall_trap (lp, 0)) 3047 1.6 christos return; 3048 1.6 christos } 3049 1.1 christos else 3050 1.1 christos { 3051 1.3 christos /* Almost all other ptrace-stops are known to be outside of system 3052 1.3 christos calls, with further exceptions in linux_handle_extended_wait. */ 3053 1.1 christos lp->syscall_state = TARGET_WAITKIND_IGNORE; 3054 1.9 christos } 3055 1.9 christos 3056 1.5 christos /* Handle GNU/Linux's extended waitstatus for trace events. */ 3057 1.10 christos if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP 3058 1.1 christos && linux_is_extended_waitstatus (status)) 3059 1.1 christos { 3060 1.1 christos linux_nat_debug_printf ("Handling extended status 0x%06x", status); 3061 1.3 christos 3062 1.1 christos if (linux_handle_extended_wait (lp, status)) 3063 1.11 christos return; 3064 1.3 christos } 3065 1.9 christos 3066 1.10 christos /* Check if the thread has exited. */ 3067 1.3 christos if (WIFEXITED (status) || WIFSIGNALED (status)) 3068 1.10 christos { 3069 1.6 christos if (!report_exit_events_for (lp) && !is_leader (lp)) 3070 1.6 christos { 3071 1.6 christos linux_nat_debug_printf ("%s exited.", 3072 1.10 christos lp->ptid.to_string ().c_str ()); 3073 1.1 christos 3074 1.1 christos /* If this was not the leader exiting, then the exit signal 3075 1.6 christos was not the end of the debugged application and should be 3076 1.6 christos ignored. */ 3077 1.6 christos exit_lwp (lp); 3078 1.6 christos return; 3079 1.9 christos } 3080 1.9 christos 3081 1.3 christos /* Note that even if the leader was ptrace-stopped, it can still 3082 1.11 christos exit, if e.g., some other thread brings down the whole 3083 1.10 christos process (calls `exit'). So don't assert that the lwp is 3084 1.1 christos resumed. */ 3085 1.1 christos linux_nat_debug_printf ("LWP %ld exited (resumed=%d)", 3086 1.1 christos lp->ptid.lwp (), lp->resumed); 3087 1.1 christos 3088 1.1 christos mark_lwp_dead (lp, status); 3089 1.1 christos return; 3090 1.1 christos } 3091 1.1 christos 3092 1.1 christos /* Make sure we don't report a SIGSTOP that we sent ourselves in 3093 1.5 christos an attempt to stop an LWP. */ 3094 1.5 christos if (lp->signalled 3095 1.9 christos && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) 3096 1.10 christos { 3097 1.5 christos lp->signalled = 0; 3098 1.5 christos 3099 1.1 christos if (lp->last_resume_kind == resume_stop) 3100 1.5 christos { 3101 1.1 christos linux_nat_debug_printf ("resume_stop SIGSTOP caught for %s.", 3102 1.9 christos lp->ptid.to_string ().c_str ()); 3103 1.9 christos } 3104 1.9 christos else 3105 1.10 christos { 3106 1.1 christos /* This is a delayed SIGSTOP. Filter out the event. */ 3107 1.5 christos 3108 1.1 christos linux_nat_debug_printf 3109 1.10 christos ("%s %s, 0, 0 (discard delayed SIGSTOP)", 3110 1.1 christos lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3111 1.1 christos lp->ptid.to_string ().c_str ()); 3112 1.1 christos 3113 1.1 christos linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 3114 1.1 christos gdb_assert (lp->resumed); 3115 1.1 christos return; 3116 1.1 christos } 3117 1.1 christos } 3118 1.9 christos 3119 1.10 christos /* Make sure we don't report a SIGINT that we have already displayed 3120 1.1 christos for another thread. */ 3121 1.1 christos if (lp->ignore_sigint 3122 1.1 christos && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT) 3123 1.1 christos { 3124 1.3 christos linux_nat_debug_printf ("Delayed SIGINT caught for %s.", 3125 1.9 christos lp->ptid.to_string ().c_str ()); 3126 1.9 christos 3127 1.10 christos /* This is a delayed SIGINT. */ 3128 1.1 christos lp->ignore_sigint = 0; 3129 1.1 christos 3130 1.1 christos linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0); 3131 1.10 christos linux_nat_debug_printf ("%s %s, 0, 0 (discard SIGINT)", 3132 1.1 christos lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3133 1.1 christos lp->ptid.to_string ().c_str ()); 3134 1.3 christos gdb_assert (lp->resumed); 3135 1.3 christos 3136 1.9 christos /* Discard the event. */ 3137 1.3 christos return; 3138 1.3 christos } 3139 1.3 christos 3140 1.3 christos /* Don't report signals that GDB isn't interested in, such as 3141 1.3 christos signals that are neither printed nor stopped upon. Stopping all 3142 1.3 christos threads can be a bit time-consuming, so if we want decent 3143 1.3 christos performance with heavily multi-threaded programs, especially when 3144 1.6 christos they're using a high frequency timer, we'd better avoid it if we 3145 1.3 christos can. */ 3146 1.3 christos if (WIFSTOPPED (status)) 3147 1.3 christos { 3148 1.3 christos enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status)); 3149 1.3 christos 3150 1.3 christos if (!target_is_non_stop_p ()) 3151 1.3 christos { 3152 1.3 christos /* Only do the below in all-stop, as we currently use SIGSTOP 3153 1.3 christos to implement target_stop (see linux_nat_stop) in 3154 1.3 christos non-stop. */ 3155 1.3 christos if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0) 3156 1.9 christos { 3157 1.3 christos /* If ^C/BREAK is typed at the tty/console, SIGINT gets 3158 1.3 christos forwarded to the entire process group, that is, all LWPs 3159 1.3 christos will receive it - unless they're using CLONE_THREAD to 3160 1.3 christos share signals. Since we only want to report it once, we 3161 1.3 christos mark it as ignored for all LWPs except this one. */ 3162 1.3 christos iterate_over_lwps (ptid_t (lp->ptid.pid ()), set_ignore_sigint); 3163 1.3 christos lp->ignore_sigint = 0; 3164 1.5 christos } 3165 1.9 christos else 3166 1.9 christos maybe_clear_ignore_sigint (lp); 3167 1.3 christos } 3168 1.5 christos 3169 1.9 christos /* When using hardware single-step, we need to report every signal. 3170 1.11 christos Otherwise, signals in pass_mask may be short-circuited 3171 1.5 christos except signals that might be caused by a breakpoint, or SIGSTOP 3172 1.3 christos if we sent the SIGSTOP and are waiting for it to arrive. */ 3173 1.3 christos if (!lp->step 3174 1.9 christos && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)) 3175 1.9 christos && (WSTOPSIG (status) != SIGSTOP 3176 1.9 christos || !linux_target->find_thread (lp->ptid)->stop_requested) 3177 1.10 christos && !linux_wstatus_maybe_breakpoint (status)) 3178 1.9 christos { 3179 1.9 christos linux_resume_one_lwp (lp, lp->step, signo); 3180 1.10 christos linux_nat_debug_printf 3181 1.3 christos ("%s %s, %s (preempt 'handle')", 3182 1.3 christos lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 3183 1.3 christos lp->ptid.to_string ().c_str (), 3184 1.1 christos (signo != GDB_SIGNAL_0 3185 1.1 christos ? strsignal (gdb_signal_to_host (signo)) : "0")); 3186 1.1 christos return; 3187 1.6 christos } 3188 1.1 christos } 3189 1.1 christos 3190 1.1 christos /* An interesting event. */ 3191 1.1 christos gdb_assert (lp); 3192 1.1 christos lp->status = status; 3193 1.1 christos save_stop_reason (lp); 3194 1.1 christos } 3195 1.1 christos 3196 1.8 christos /* Detect zombie thread group leaders, and "exit" them. We can't reap 3197 1.1 christos their exits until all other threads in the group have exited. */ 3198 1.1 christos 3199 1.1 christos static void 3200 1.1 christos check_zombie_leaders (void) 3201 1.1 christos { 3202 1.1 christos for (inferior *inf : all_inferiors ()) 3203 1.8 christos { 3204 1.1 christos struct lwp_info *leader_lp; 3205 1.1 christos 3206 1.10 christos if (inf->pid == 0) 3207 1.10 christos continue; 3208 1.10 christos 3209 1.10 christos leader_lp = find_lwp_pid (ptid_t (inf->pid)); 3210 1.10 christos if (leader_lp != NULL 3211 1.1 christos /* Check if there are other threads in the group, as we may 3212 1.1 christos have raced with the inferior simply exiting. Note this 3213 1.1 christos isn't a watertight check. If the inferior is 3214 1.10 christos multi-threaded and is exiting, it may be we see the 3215 1.10 christos leader as zombie before we reap all the non-leader 3216 1.10 christos threads. See comments below. */ 3217 1.10 christos && num_lwps (inf->pid) > 1 3218 1.10 christos && linux_proc_pid_is_zombie (inf->pid)) 3219 1.10 christos { 3220 1.10 christos /* A zombie leader in a multi-threaded program can mean one 3221 1.10 christos of three things: 3222 1.10 christos 3223 1.10 christos #1 - Only the leader exited, not the whole program, e.g., 3224 1.10 christos with pthread_exit. Since we can't reap the leader's exit 3225 1.10 christos status until all other threads are gone and reaped too, 3226 1.10 christos we want to delete the zombie leader right away, as it 3227 1.10 christos can't be debugged, we can't read its registers, etc. 3228 1.10 christos This is the main reason we check for zombie leaders 3229 1.10 christos disappearing. 3230 1.10 christos 3231 1.10 christos #2 - The whole thread-group/process exited (a group exit, 3232 1.10 christos via e.g. exit(3), and there is (or will be shortly) an 3233 1.10 christos exit reported for each thread in the process, and then 3234 1.10 christos finally an exit for the leader once the non-leaders are 3235 1.10 christos reaped. 3236 1.10 christos 3237 1.10 christos #3 - There are 3 or more threads in the group, and a 3238 1.10 christos thread other than the leader exec'd. See comments on 3239 1.10 christos exec events at the top of the file. 3240 1.10 christos 3241 1.10 christos Ideally we would never delete the leader for case #2. 3242 1.10 christos Instead, we want to collect the exit status of each 3243 1.10 christos non-leader thread, and then finally collect the exit 3244 1.10 christos status of the leader as normal and use its exit code as 3245 1.10 christos whole-process exit code. Unfortunately, there's no 3246 1.10 christos race-free way to distinguish cases #1 and #2. We can't 3247 1.10 christos assume the exit events for the non-leaders threads are 3248 1.10 christos already pending in the kernel, nor can we assume the 3249 1.10 christos non-leader threads are in zombie state already. Between 3250 1.10 christos the leader becoming zombie and the non-leaders exiting 3251 1.10 christos and becoming zombie themselves, there's a small time 3252 1.10 christos window, so such a check would be racy. Temporarily 3253 1.10 christos pausing all threads and checking to see if all threads 3254 1.10 christos exit or not before re-resuming them would work in the 3255 1.10 christos case that all threads are running right now, but it 3256 1.10 christos wouldn't work if some thread is currently already 3257 1.10 christos ptrace-stopped, e.g., due to scheduler-locking. 3258 1.10 christos 3259 1.9 christos So what we do is we delete the leader anyhow, and then 3260 1.10 christos later on when we see its exit status, we re-add it back. 3261 1.10 christos We also make sure that we only report a whole-process 3262 1.9 christos exit when we see the leader exiting, as opposed to when 3263 1.1 christos the last LWP in the LWP list exits, which can be a 3264 1.1 christos non-leader if we deleted the leader here. */ 3265 1.1 christos linux_nat_debug_printf ("Thread group leader %d zombie " 3266 1.1 christos "(it exited, or another thread execd), " 3267 1.1 christos "deleting it.", 3268 1.11 christos inf->pid); 3269 1.11 christos exit_lwp (leader_lp); 3270 1.11 christos } 3271 1.11 christos } 3272 1.11 christos } 3273 1.6 christos 3274 1.6 christos /* Convenience function that is called when we're about to return an 3275 1.6 christos event to the core. If the event is an exit or signalled event, 3276 1.6 christos then this decides whether to report it as process-wide event, as a 3277 1.6 christos thread exit event, or to suppress it. All other event kinds are 3278 1.6 christos passed through unmodified. */ 3279 1.6 christos 3280 1.11 christos static ptid_t 3281 1.11 christos filter_exit_event (struct lwp_info *event_child, 3282 1.11 christos struct target_waitstatus *ourstatus) 3283 1.11 christos { 3284 1.11 christos ptid_t ptid = event_child->ptid; 3285 1.11 christos 3286 1.11 christos /* Note we must filter TARGET_WAITKIND_SIGNALLED as well, otherwise 3287 1.11 christos if a non-leader thread exits with a signal, we'd report it to the 3288 1.10 christos core which would interpret it as the whole-process exiting. 3289 1.6 christos There is no TARGET_WAITKIND_THREAD_SIGNALLED event kind. */ 3290 1.11 christos if (ourstatus->kind () != TARGET_WAITKIND_EXITED 3291 1.11 christos && ourstatus->kind () != TARGET_WAITKIND_SIGNALLED) 3292 1.11 christos return ptid; 3293 1.11 christos 3294 1.11 christos if (!is_leader (event_child)) 3295 1.11 christos { 3296 1.11 christos if (report_exit_events_for (event_child)) 3297 1.6 christos { 3298 1.11 christos ourstatus->set_thread_exited (0); 3299 1.11 christos /* Delete lwp, but not thread_info, infrun will need it to 3300 1.11 christos process the event. */ 3301 1.11 christos exit_lwp (event_child, false); 3302 1.6 christos } 3303 1.6 christos else 3304 1.6 christos { 3305 1.6 christos ourstatus->set_ignore (); 3306 1.6 christos exit_lwp (event_child); 3307 1.1 christos } 3308 1.8 christos } 3309 1.10 christos 3310 1.1 christos return ptid; 3311 1.11 christos } 3312 1.11 christos 3313 1.3 christos static ptid_t 3314 1.1 christos linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus, 3315 1.1 christos target_wait_flags target_options) 3316 1.1 christos { 3317 1.1 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3318 1.1 christos 3319 1.1 christos sigset_t prev_mask; 3320 1.1 christos enum resume_kind last_resume_kind; 3321 1.10 christos struct lwp_info *lp; 3322 1.1 christos int status; 3323 1.10 christos 3324 1.10 christos /* The first time we get here after starting a new inferior, we may 3325 1.1 christos not have added it to the LWP list yet - this is the earliest 3326 1.10 christos moment at which we know its PID. */ 3327 1.10 christos if (ptid.is_pid () && find_lwp_pid (ptid) == nullptr) 3328 1.1 christos { 3329 1.1 christos ptid_t lwp_ptid (ptid.pid (), ptid.pid ()); 3330 1.1 christos 3331 1.1 christos /* Upgrade the main thread's ptid. */ 3332 1.1 christos thread_change_ptid (linux_target, ptid, lwp_ptid); 3333 1.1 christos lp = add_initial_lwp (lwp_ptid); 3334 1.1 christos lp->resumed = 1; 3335 1.9 christos } 3336 1.3 christos 3337 1.1 christos /* Make sure SIGCHLD is blocked until the sigsuspend below. */ 3338 1.9 christos block_child_signals (&prev_mask); 3339 1.11 christos 3340 1.10 christos /* First check if there is a LWP with a wait status pending. */ 3341 1.1 christos lp = iterate_over_lwps (ptid, status_callback); 3342 1.1 christos if (lp != NULL) 3343 1.3 christos { 3344 1.3 christos linux_nat_debug_printf ("Using pending wait status %s for %s.", 3345 1.3 christos pending_status_str (lp).c_str (), 3346 1.1 christos lp->ptid.to_string ().c_str ()); 3347 1.1 christos } 3348 1.1 christos 3349 1.1 christos /* But if we don't find a pending event, we'll have to wait. Always 3350 1.1 christos pull all events out of the kernel. We'll randomly select an 3351 1.1 christos event LWP out of all that have events, to prevent starvation. */ 3352 1.1 christos 3353 1.1 christos while (lp == NULL) 3354 1.1 christos { 3355 1.1 christos pid_t lwpid; 3356 1.1 christos 3357 1.9 christos /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace 3358 1.1 christos quirks: 3359 1.1 christos 3360 1.1 christos - If the thread group leader exits while other threads in the 3361 1.1 christos thread group still exist, waitpid(TGID, ...) hangs. That 3362 1.1 christos waitpid won't return an exit status until the other threads 3363 1.1 christos in the group are reaped. 3364 1.1 christos 3365 1.6 christos - When a non-leader thread execs, that thread just vanishes 3366 1.1 christos without reporting an exit (so we'd hang if we waited for it 3367 1.9 christos explicitly in that case). The exec event is reported to 3368 1.9 christos the TGID pid. */ 3369 1.9 christos 3370 1.1 christos errno = 0; 3371 1.1 christos lwpid = my_waitpid (-1, &status, __WALL | WNOHANG); 3372 1.1 christos 3373 1.9 christos linux_nat_debug_printf ("waitpid(-1, ...) returned %d, %s", 3374 1.10 christos lwpid, 3375 1.10 christos errno ? safe_strerror (errno) : "ERRNO-OK"); 3376 1.1 christos 3377 1.3 christos if (lwpid > 0) 3378 1.1 christos { 3379 1.1 christos linux_nat_debug_printf ("waitpid %ld received %s", 3380 1.1 christos (long) lwpid, 3381 1.1 christos status_to_str (status).c_str ()); 3382 1.1 christos 3383 1.5 christos linux_nat_filter_event (lwpid, status); 3384 1.5 christos /* Retry until nothing comes out of waitpid. A single 3385 1.5 christos SIGCHLD can indicate more than one child stopped. */ 3386 1.9 christos continue; 3387 1.9 christos } 3388 1.9 christos 3389 1.9 christos /* Now that we've pulled all events out of the kernel, resume 3390 1.5 christos LWPs that don't have an interesting event to report. */ 3391 1.5 christos iterate_over_lwps (minus_one_ptid, 3392 1.5 christos [] (struct lwp_info *info) 3393 1.9 christos { 3394 1.3 christos return resume_stopped_resumed_lwps (info, minus_one_ptid); 3395 1.3 christos }); 3396 1.3 christos 3397 1.1 christos /* ... and find an LWP with a status to report to the core, if 3398 1.1 christos any. */ 3399 1.1 christos lp = iterate_over_lwps (ptid, status_callback); 3400 1.1 christos if (lp != NULL) 3401 1.1 christos break; 3402 1.1 christos 3403 1.9 christos /* Check for zombie thread group leaders. Those can't be reaped 3404 1.1 christos until all other threads in the thread group are. */ 3405 1.9 christos check_zombie_leaders (); 3406 1.1 christos 3407 1.10 christos /* If there are no resumed children left, bail. We'd be stuck 3408 1.1 christos forever in the sigsuspend call below otherwise. */ 3409 1.1 christos if (iterate_over_lwps (ptid, resumed_callback) == NULL) 3410 1.1 christos { 3411 1.1 christos linux_nat_debug_printf ("exit (no resumed LWP)"); 3412 1.1 christos 3413 1.1 christos ourstatus->set_no_resumed (); 3414 1.1 christos 3415 1.1 christos restore_child_signals_mask (&prev_mask); 3416 1.1 christos return minus_one_ptid; 3417 1.11 christos } 3418 1.1 christos 3419 1.10 christos /* No interesting event to report to the core. */ 3420 1.1 christos 3421 1.1 christos if (target_options & TARGET_WNOHANG) 3422 1.1 christos { 3423 1.1 christos linux_nat_debug_printf ("no interesting events found"); 3424 1.1 christos 3425 1.1 christos ourstatus->set_ignore (); 3426 1.1 christos restore_child_signals_mask (&prev_mask); 3427 1.1 christos return minus_one_ptid; 3428 1.8 christos } 3429 1.1 christos 3430 1.1 christos /* We shouldn't end up here unless we want to try again. */ 3431 1.1 christos gdb_assert (lp == NULL); 3432 1.11 christos 3433 1.1 christos /* Block until we get an event reported with SIGCHLD. */ 3434 1.1 christos wait_for_signal (); 3435 1.1 christos } 3436 1.1 christos 3437 1.6 christos gdb_assert (lp); 3438 1.3 christos gdb_assert (lp->stopped); 3439 1.3 christos 3440 1.9 christos status = lp->status; 3441 1.3 christos lp->status = 0; 3442 1.3 christos 3443 1.3 christos if (!target_is_non_stop_p ()) 3444 1.9 christos { 3445 1.3 christos /* Now stop all other LWP's ... */ 3446 1.3 christos iterate_over_lwps (minus_one_ptid, stop_callback); 3447 1.3 christos 3448 1.3 christos /* ... and wait until all of them have reported back that 3449 1.3 christos they're no longer running. */ 3450 1.8 christos iterate_over_lwps (minus_one_ptid, stop_wait_callback); 3451 1.3 christos } 3452 1.3 christos 3453 1.3 christos /* If we're not waiting for a specific LWP, choose an event LWP from 3454 1.1 christos among those that have had events. Giving equal priority to all 3455 1.3 christos LWPs that have had events helps prevent starvation. */ 3456 1.3 christos if (ptid == minus_one_ptid || ptid.is_pid ()) 3457 1.3 christos select_event_lwp (ptid, &lp, &status); 3458 1.3 christos 3459 1.1 christos gdb_assert (lp != NULL); 3460 1.6 christos 3461 1.1 christos /* We'll need this to determine whether to report a SIGSTOP as 3462 1.1 christos GDB_SIGNAL_0. Need to take a copy because resume_clear_callback 3463 1.1 christos clears it. */ 3464 1.9 christos last_resume_kind = lp->last_resume_kind; 3465 1.1 christos 3466 1.1 christos if (!target_is_non_stop_p ()) 3467 1.1 christos { 3468 1.9 christos /* In all-stop, from the core's perspective, all LWPs are now 3469 1.1 christos stopped until a new resume action is sent over. */ 3470 1.1 christos iterate_over_lwps (minus_one_ptid, resume_clear_callback); 3471 1.8 christos } 3472 1.1 christos else 3473 1.9 christos { 3474 1.10 christos resume_clear_callback (lp); 3475 1.1 christos } 3476 1.1 christos 3477 1.10 christos if (linux_target->low_status_is_event (status)) 3478 1.1 christos { 3479 1.1 christos linux_nat_debug_printf ("trap ptid is %s.", 3480 1.10 christos lp->ptid.to_string ().c_str ()); 3481 1.1 christos } 3482 1.1 christos 3483 1.10 christos if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE) 3484 1.1 christos { 3485 1.11 christos *ourstatus = lp->waitstatus; 3486 1.1 christos lp->waitstatus.set_ignore (); 3487 1.1 christos } 3488 1.1 christos else 3489 1.1 christos *ourstatus = host_status_to_waitstatus (status); 3490 1.10 christos 3491 1.1 christos linux_nat_debug_printf ("event found"); 3492 1.1 christos 3493 1.1 christos restore_child_signals_mask (&prev_mask); 3494 1.1 christos 3495 1.1 christos if (last_resume_kind == resume_stop 3496 1.10 christos && ourstatus->kind () == TARGET_WAITKIND_STOPPED 3497 1.1 christos && WSTOPSIG (status) == SIGSTOP) 3498 1.1 christos { 3499 1.10 christos /* A thread that has been requested to stop by GDB with 3500 1.10 christos target_stop, and it stopped cleanly, so report as SIG0. The 3501 1.1 christos use of SIGSTOP is an implementation detail. */ 3502 1.1 christos ourstatus->set_stopped (GDB_SIGNAL_0); 3503 1.1 christos } 3504 1.1 christos 3505 1.11 christos if (ourstatus->kind () == TARGET_WAITKIND_EXITED 3506 1.1 christos || ourstatus->kind () == TARGET_WAITKIND_SIGNALLED) 3507 1.1 christos lp->core = -1; 3508 1.1 christos else 3509 1.1 christos lp->core = linux_common_core_of_thread (lp->ptid); 3510 1.1 christos 3511 1.1 christos return filter_exit_event (lp, ourstatus); 3512 1.9 christos } 3513 1.1 christos 3514 1.11 christos /* Resume LWPs that are currently stopped without any pending status 3515 1.11 christos to report, but are resumed from the core's perspective. */ 3516 1.5 christos 3517 1.5 christos static int 3518 1.9 christos resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid) 3519 1.10 christos { 3520 1.5 christos inferior *inf = lwp_inferior (lp); 3521 1.5 christos 3522 1.5 christos if (!lp->stopped) 3523 1.9 christos { 3524 1.10 christos linux_nat_debug_printf ("NOT resuming LWP %s, not stopped", 3525 1.5 christos lp->ptid.to_string ().c_str ()); 3526 1.5 christos } 3527 1.5 christos else if (!lp->resumed) 3528 1.9 christos { 3529 1.10 christos linux_nat_debug_printf ("NOT resuming LWP %s, not resumed", 3530 1.5 christos lp->ptid.to_string ().c_str ()); 3531 1.11 christos } 3532 1.11 christos else if (lwp_status_pending_p (lp)) 3533 1.11 christos { 3534 1.11 christos linux_nat_debug_printf ("NOT resuming LWP %s, has pending status", 3535 1.11 christos lp->ptid.to_string ().c_str ()); 3536 1.5 christos } 3537 1.1 christos else if (inf->vfork_child != nullptr) 3538 1.9 christos { 3539 1.8 christos linux_nat_debug_printf ("NOT resuming LWP %s (vfork parent)", 3540 1.1 christos lp->ptid.to_string ().c_str ()); 3541 1.9 christos } 3542 1.5 christos else 3543 1.5 christos { 3544 1.5 christos struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid); 3545 1.5 christos struct gdbarch *gdbarch = regcache->arch (); 3546 1.5 christos 3547 1.5 christos try 3548 1.9 christos { 3549 1.5 christos CORE_ADDR pc = regcache_read_pc (regcache); 3550 1.11 christos int leave_stopped = 0; 3551 1.5 christos 3552 1.5 christos /* Don't bother if there's a breakpoint at PC that we'd hit 3553 1.5 christos immediately, and we're not waiting for this LWP. */ 3554 1.5 christos if (!lp->ptid.matches (wait_ptid)) 3555 1.5 christos { 3556 1.9 christos if (breakpoint_inserted_here_p (inf->aspace.get (), pc)) 3557 1.9 christos leave_stopped = 1; 3558 1.10 christos } 3559 1.9 christos 3560 1.1 christos if (!leave_stopped) 3561 1.5 christos { 3562 1.5 christos linux_nat_debug_printf 3563 1.5 christos ("resuming stopped-resumed LWP %s at %s: step=%d", 3564 1.9 christos lp->ptid.to_string ().c_str (), paddress (gdbarch, pc), 3565 1.1 christos lp->step); 3566 1.5 christos 3567 1.9 christos linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0); 3568 1.1 christos } 3569 1.1 christos } 3570 1.1 christos catch (const gdb_exception_error &ex) 3571 1.1 christos { 3572 1.1 christos if (!check_ptrace_stopped_lwp_gone (lp)) 3573 1.1 christos throw; 3574 1.8 christos } 3575 1.8 christos } 3576 1.10 christos 3577 1.1 christos return 0; 3578 1.11 christos } 3579 1.11 christos 3580 1.1 christos ptid_t 3581 1.1 christos linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, 3582 1.10 christos target_wait_flags target_options) 3583 1.9 christos { 3584 1.1 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3585 1.1 christos 3586 1.3 christos ptid_t event_ptid; 3587 1.1 christos 3588 1.1 christos linux_nat_debug_printf ("[%s], [%s]", ptid.to_string ().c_str (), 3589 1.1 christos target_options_to_string (target_options).c_str ()); 3590 1.1 christos 3591 1.1 christos /* Flush the async file first. */ 3592 1.1 christos if (target_is_async_p ()) 3593 1.1 christos async_file_flush (); 3594 1.1 christos 3595 1.1 christos /* Resume LWPs that are currently stopped without any pending status 3596 1.6 christos to report, but are resumed from the core's perspective. LWPs get 3597 1.9 christos in this state if we find them stopping at a time we're not 3598 1.9 christos interested in reporting the event (target_wait on a 3599 1.9 christos specific_process, for example, see linux_nat_wait_1), and 3600 1.9 christos meanwhile the event became uninteresting. Don't bother resuming 3601 1.9 christos LWPs we're not going to wait for if they'd stop immediately. */ 3602 1.1 christos if (target_is_non_stop_p ()) 3603 1.8 christos iterate_over_lwps (minus_one_ptid, 3604 1.1 christos [=] (struct lwp_info *info) 3605 1.1 christos { 3606 1.1 christos return resume_stopped_resumed_lwps (info, ptid); 3607 1.1 christos }); 3608 1.3 christos 3609 1.10 christos event_ptid = linux_nat_wait_1 (ptid, ourstatus, target_options); 3610 1.10 christos 3611 1.8 christos /* If we requested any event, and something came out, assume there 3612 1.1 christos may be more. If we requested a specific lwp or process, also 3613 1.1 christos assume there may be more. */ 3614 1.1 christos if (target_is_async_p () 3615 1.1 christos && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE 3616 1.1 christos && ourstatus->kind () != TARGET_WAITKIND_NO_RESUMED) 3617 1.6 christos || ptid != minus_one_ptid)) 3618 1.6 christos async_file_mark (); 3619 1.6 christos 3620 1.6 christos return event_ptid; 3621 1.1 christos } 3622 1.1 christos 3623 1.1 christos /* Kill one LWP. */ 3624 1.1 christos 3625 1.6 christos static void 3626 1.9 christos kill_one_lwp (pid_t pid) 3627 1.1 christos { 3628 1.3 christos /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */ 3629 1.3 christos 3630 1.3 christos errno = 0; 3631 1.9 christos kill_lwp (pid, SIGKILL); 3632 1.9 christos 3633 1.9 christos if (debug_linux_nat) 3634 1.3 christos { 3635 1.1 christos int save_errno = errno; 3636 1.1 christos 3637 1.1 christos linux_nat_debug_printf 3638 1.1 christos ("kill (SIGKILL) %ld, 0, 0 (%s)", (long) pid, 3639 1.6 christos save_errno != 0 ? safe_strerror (save_errno) : "OK"); 3640 1.1 christos } 3641 1.3 christos 3642 1.3 christos /* Some kernels ignore even SIGKILL for processes under ptrace. */ 3643 1.3 christos 3644 1.9 christos errno = 0; 3645 1.9 christos ptrace (PTRACE_KILL, pid, 0, 0); 3646 1.9 christos if (debug_linux_nat) 3647 1.3 christos { 3648 1.6 christos int save_errno = errno; 3649 1.1 christos 3650 1.6 christos linux_nat_debug_printf 3651 1.1 christos ("PTRACE_KILL %ld, 0, 0 (%s)", (long) pid, 3652 1.6 christos save_errno ? safe_strerror (save_errno) : "OK"); 3653 1.6 christos } 3654 1.1 christos } 3655 1.6 christos 3656 1.1 christos /* Wait for an LWP to die. */ 3657 1.1 christos 3658 1.1 christos static void 3659 1.1 christos kill_wait_one_lwp (pid_t pid) 3660 1.1 christos { 3661 1.1 christos pid_t res; 3662 1.1 christos 3663 1.6 christos /* We must make sure that there are no pending events (delayed 3664 1.6 christos SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current 3665 1.1 christos program doesn't interfere with any following debugging session. */ 3666 1.9 christos 3667 1.9 christos do 3668 1.6 christos { 3669 1.6 christos res = my_waitpid (pid, NULL, __WALL); 3670 1.6 christos if (res != (pid_t) -1) 3671 1.6 christos { 3672 1.6 christos linux_nat_debug_printf ("wait %ld received unknown.", (long) pid); 3673 1.1 christos 3674 1.1 christos /* The Linux kernel sometimes fails to kill a thread 3675 1.6 christos completely after PTRACE_KILL; that goes from the stop 3676 1.6 christos point in do_fork out to the one in get_signal_to_deliver 3677 1.6 christos and waits again. So kill it again. */ 3678 1.6 christos kill_one_lwp (pid); 3679 1.1 christos } 3680 1.6 christos } 3681 1.6 christos while (res == pid); 3682 1.6 christos 3683 1.9 christos gdb_assert (res == -1 && errno == ECHILD); 3684 1.6 christos } 3685 1.8 christos 3686 1.1 christos /* Callback for iterate_over_lwps. */ 3687 1.1 christos 3688 1.1 christos static int 3689 1.6 christos kill_callback (struct lwp_info *lp) 3690 1.6 christos { 3691 1.6 christos kill_one_lwp (lp->ptid.lwp ()); 3692 1.9 christos return 0; 3693 1.6 christos } 3694 1.8 christos 3695 1.6 christos /* Callback for iterate_over_lwps. */ 3696 1.6 christos 3697 1.6 christos static int 3698 1.11 christos kill_wait_callback (struct lwp_info *lp) 3699 1.6 christos { 3700 1.11 christos kill_wait_one_lwp (lp->ptid.lwp ()); 3701 1.11 christos return 0; 3702 1.6 christos } 3703 1.11 christos 3704 1.11 christos /* Kill the fork/clone child of LP if it has an unfollowed child. */ 3705 1.8 christos 3706 1.11 christos static int 3707 1.11 christos kill_unfollowed_child_callback (lwp_info *lp) 3708 1.11 christos { 3709 1.11 christos std::optional<target_waitstatus> ws = get_pending_child_status (lp); 3710 1.11 christos if (ws.has_value ()) 3711 1.11 christos { 3712 1.11 christos ptid_t child_ptid = ws->child_ptid (); 3713 1.11 christos int child_pid = child_ptid.pid (); 3714 1.11 christos int child_lwp = child_ptid.lwp (); 3715 1.11 christos 3716 1.11 christos kill_one_lwp (child_lwp); 3717 1.11 christos kill_wait_one_lwp (child_lwp); 3718 1.6 christos 3719 1.11 christos /* Let the arch-specific native code know this process is 3720 1.6 christos gone. */ 3721 1.6 christos if (ws->kind () != TARGET_WAITKIND_THREAD_CLONED) 3722 1.8 christos linux_target->low_forget_process (child_pid); 3723 1.8 christos } 3724 1.1 christos 3725 1.11 christos return 0; 3726 1.11 christos } 3727 1.11 christos 3728 1.11 christos void 3729 1.1 christos linux_nat_target::kill () 3730 1.11 christos { 3731 1.1 christos ptid_t pid_ptid (inferior_ptid.pid ()); 3732 1.1 christos 3733 1.1 christos /* If we're stopped while forking/cloning and we haven't followed 3734 1.1 christos yet, kill the child task. We need to do this first because the 3735 1.1 christos parent will be sleeping if this is a vfork. */ 3736 1.1 christos iterate_over_lwps (pid_ptid, kill_unfollowed_child_callback); 3737 1.9 christos 3738 1.11 christos if (forks_exist_p ()) 3739 1.1 christos linux_fork_killall (); 3740 1.1 christos else 3741 1.11 christos { 3742 1.1 christos /* Stop all threads before killing them, since ptrace requires 3743 1.1 christos that the thread is stopped to successfully PTRACE_KILL. */ 3744 1.11 christos iterate_over_lwps (pid_ptid, stop_callback); 3745 1.1 christos /* ... and wait until all of them have reported back that 3746 1.1 christos they're no longer running. */ 3747 1.11 christos iterate_over_lwps (pid_ptid, stop_wait_callback); 3748 1.1 christos 3749 1.1 christos /* Kill all LWP's ... */ 3750 1.7 christos iterate_over_lwps (pid_ptid, kill_callback); 3751 1.1 christos 3752 1.1 christos /* ... and wait until we've flushed all events. */ 3753 1.8 christos iterate_over_lwps (pid_ptid, kill_wait_callback); 3754 1.8 christos } 3755 1.1 christos 3756 1.11 christos target_mourn_inferior (inferior_ptid); 3757 1.11 christos } 3758 1.8 christos 3759 1.1 christos void 3760 1.1 christos linux_nat_target::mourn_inferior () 3761 1.1 christos { 3762 1.10 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 3763 1.10 christos 3764 1.1 christos int pid = inferior_ptid.pid (); 3765 1.1 christos 3766 1.8 christos purge_lwp_list (pid); 3767 1.1 christos 3768 1.1 christos close_proc_mem_file (pid); 3769 1.1 christos 3770 1.1 christos if (! forks_exist_p ()) 3771 1.1 christos /* Normal case, no other forks available. */ 3772 1.1 christos inf_ptrace_target::mourn_inferior (); 3773 1.1 christos else 3774 1.8 christos /* Multi-fork case. The current inferior_ptid has exited, but 3775 1.1 christos there are other viable forks to debug. Delete the exiting 3776 1.1 christos one and context-switch to the first available. */ 3777 1.1 christos linux_fork_mourn_inferior (); 3778 1.1 christos 3779 1.1 christos /* Let the arch-specific native code know this process is gone. */ 3780 1.1 christos linux_target->low_forget_process (pid); 3781 1.1 christos } 3782 1.1 christos 3783 1.8 christos /* Convert a native/host siginfo object, into/from the siginfo in the 3784 1.8 christos layout of the inferiors' architecture. */ 3785 1.8 christos 3786 1.1 christos static void 3787 1.1 christos siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction) 3788 1.1 christos { 3789 1.1 christos /* If the low target didn't do anything, then just do a straight 3790 1.1 christos memcpy. */ 3791 1.1 christos if (!linux_target->low_siginfo_fixup (siginfo, inf_siginfo, direction)) 3792 1.1 christos { 3793 1.1 christos if (direction == 1) 3794 1.3 christos memcpy (siginfo, inf_siginfo, sizeof (siginfo_t)); 3795 1.10 christos else 3796 1.10 christos memcpy (inf_siginfo, siginfo, sizeof (siginfo_t)); 3797 1.3 christos } 3798 1.3 christos } 3799 1.1 christos 3800 1.1 christos static enum target_xfer_status 3801 1.1 christos linux_xfer_siginfo (ptid_t ptid, enum target_object object, 3802 1.1 christos const char *annex, gdb_byte *readbuf, 3803 1.1 christos const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 3804 1.1 christos ULONGEST *xfered_len) 3805 1.1 christos { 3806 1.1 christos siginfo_t siginfo; 3807 1.3 christos gdb_byte inf_siginfo[sizeof (siginfo_t)]; 3808 1.1 christos 3809 1.10 christos gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO); 3810 1.3 christos gdb_assert (readbuf || writebuf); 3811 1.1 christos 3812 1.1 christos if (offset > sizeof (siginfo)) 3813 1.1 christos return TARGET_XFER_E_IO; 3814 1.1 christos 3815 1.1 christos if (!linux_nat_get_siginfo (ptid, &siginfo)) 3816 1.1 christos return TARGET_XFER_E_IO; 3817 1.1 christos 3818 1.1 christos /* When GDB is built as a 64-bit application, ptrace writes into 3819 1.1 christos SIGINFO an object with 64-bit layout. Since debugging a 32-bit 3820 1.1 christos inferior with a 64-bit GDB should look the same as debugging it 3821 1.1 christos with a 32-bit GDB, we need to convert it. GDB core always sees 3822 1.1 christos the converted layout, so any read/write will have to be done 3823 1.1 christos post-conversion. */ 3824 1.1 christos siginfo_fixup (&siginfo, inf_siginfo, 0); 3825 1.1 christos 3826 1.1 christos if (offset + len > sizeof (siginfo)) 3827 1.1 christos len = sizeof (siginfo) - offset; 3828 1.1 christos 3829 1.1 christos if (readbuf != NULL) 3830 1.1 christos memcpy (readbuf, inf_siginfo + offset, len); 3831 1.1 christos else 3832 1.10 christos { 3833 1.1 christos memcpy (inf_siginfo + offset, writebuf, len); 3834 1.1 christos 3835 1.1 christos /* Convert back to ptrace layout before flushing it out. */ 3836 1.3 christos siginfo_fixup (&siginfo, inf_siginfo, 1); 3837 1.1 christos 3838 1.1 christos int pid = get_ptrace_pid (ptid); 3839 1.3 christos errno = 0; 3840 1.3 christos ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo); 3841 1.1 christos if (errno != 0) 3842 1.1 christos return TARGET_XFER_E_IO; 3843 1.3 christos } 3844 1.8 christos 3845 1.8 christos *xfered_len = len; 3846 1.8 christos return TARGET_XFER_OK; 3847 1.8 christos } 3848 1.8 christos 3849 1.8 christos static enum target_xfer_status 3850 1.10 christos linux_nat_xfer_osdata (enum target_object object, 3851 1.10 christos const char *annex, gdb_byte *readbuf, 3852 1.10 christos const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 3853 1.8 christos ULONGEST *xfered_len); 3854 1.11 christos 3855 1.11 christos static enum target_xfer_status 3856 1.11 christos linux_proc_xfer_memory_partial (int pid, gdb_byte *readbuf, 3857 1.11 christos const gdb_byte *writebuf, ULONGEST offset, 3858 1.11 christos LONGEST len, ULONGEST *xfered_len); 3859 1.11 christos 3860 1.11 christos /* Look for an LWP of PID that we know is ptrace-stopped. Returns 3861 1.11 christos NULL if none is found. */ 3862 1.11 christos 3863 1.11 christos static lwp_info * 3864 1.11 christos find_stopped_lwp (int pid) 3865 1.11 christos { 3866 1.11 christos for (lwp_info *lp : all_lwps ()) 3867 1.11 christos if (lp->ptid.pid () == pid 3868 1.8 christos && lp->stopped 3869 1.8 christos && !is_lwp_marked_dead (lp)) 3870 1.8 christos return lp; 3871 1.8 christos return nullptr; 3872 1.8 christos } 3873 1.1 christos 3874 1.1 christos enum target_xfer_status 3875 1.10 christos linux_nat_target::xfer_partial (enum target_object object, 3876 1.3 christos const char *annex, gdb_byte *readbuf, 3877 1.1 christos const gdb_byte *writebuf, 3878 1.1 christos ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 3879 1.1 christos { 3880 1.1 christos if (object == TARGET_OBJECT_SIGNAL_INFO) 3881 1.8 christos return linux_xfer_siginfo (inferior_ptid, object, annex, readbuf, writebuf, 3882 1.3 christos offset, len, xfered_len); 3883 1.1 christos 3884 1.8 christos /* The target is connected but no live inferior is selected. Pass 3885 1.8 christos this request down to a lower stratum (e.g., the executable 3886 1.8 christos file). */ 3887 1.8 christos if (object == TARGET_OBJECT_MEMORY && inferior_ptid == null_ptid) 3888 1.8 christos return TARGET_XFER_EOF; 3889 1.8 christos 3890 1.8 christos if (object == TARGET_OBJECT_AUXV) 3891 1.8 christos return memory_xfer_auxv (this, object, annex, readbuf, writebuf, 3892 1.8 christos offset, len, xfered_len); 3893 1.8 christos 3894 1.10 christos if (object == TARGET_OBJECT_OSDATA) 3895 1.10 christos return linux_nat_xfer_osdata (object, annex, readbuf, writebuf, 3896 1.10 christos offset, len, xfered_len); 3897 1.10 christos 3898 1.10 christos if (object == TARGET_OBJECT_MEMORY) 3899 1.11 christos { 3900 1.8 christos /* GDB calculates all addresses in the largest possible address 3901 1.8 christos width. The address width must be masked before its final use 3902 1.8 christos by linux_proc_xfer_partial. 3903 1.10 christos 3904 1.10 christos Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */ 3905 1.10 christos int addr_bit = gdbarch_addr_bit (current_inferior ()->arch ()); 3906 1.10 christos 3907 1.10 christos if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT)) 3908 1.10 christos offset &= ((ULONGEST) 1 << addr_bit) - 1; 3909 1.10 christos 3910 1.10 christos /* If /proc/pid/mem is writable, don't fallback to ptrace. If 3911 1.10 christos the write via /proc/pid/mem fails because the inferior execed 3912 1.10 christos (and we haven't seen the exec event yet), a subsequent ptrace 3913 1.10 christos poke would incorrectly write memory to the post-exec address 3914 1.11 christos space, while the core was trying to write to the pre-exec 3915 1.11 christos address space. */ 3916 1.11 christos if (proc_mem_file_is_writable ()) 3917 1.11 christos return linux_proc_xfer_memory_partial (inferior_ptid.pid (), readbuf, 3918 1.11 christos writebuf, offset, len, 3919 1.11 christos xfered_len); 3920 1.11 christos 3921 1.11 christos /* Fallback to ptrace. This should only really trigger on old 3922 1.11 christos systems. See "Accessing inferior memory" at the top. 3923 1.11 christos 3924 1.11 christos The target_xfer interface for memory access uses 3925 1.11 christos inferior_ptid as sideband argument to indicate which process 3926 1.11 christos to access. Memory access is process-wide, it is not 3927 1.11 christos thread-specific, so inferior_ptid sometimes points at a 3928 1.11 christos process ptid_t. If we fallback to inf_ptrace_target with 3929 1.11 christos that inferior_ptid, then the ptrace code will do the ptrace 3930 1.11 christos call targeting inferior_ptid.pid(), the leader LWP. That 3931 1.11 christos may fail with ESRCH if the leader is currently running, or 3932 1.11 christos zombie. So if we get a pid-ptid, we try to find a stopped 3933 1.11 christos LWP to use with ptrace. 3934 1.11 christos 3935 1.11 christos Note that inferior_ptid may not exist in the lwp / thread / 3936 1.11 christos inferior lists. This can happen when we're removing 3937 1.11 christos breakpoints from a fork child that we're not going to stay 3938 1.11 christos attached to. So if we don't find a stopped LWP, still do the 3939 1.11 christos ptrace call, targeting the inferior_ptid we had on entry. */ 3940 1.8 christos scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); 3941 1.8 christos lwp_info *stopped = find_stopped_lwp (inferior_ptid.pid ()); 3942 1.8 christos if (stopped != nullptr) 3943 1.8 christos inferior_ptid = stopped->ptid; 3944 1.1 christos return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, 3945 1.1 christos offset, len, xfered_len); 3946 1.8 christos } 3947 1.8 christos 3948 1.1 christos return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, 3949 1.6 christos offset, len, xfered_len); 3950 1.6 christos } 3951 1.1 christos 3952 1.1 christos bool 3953 1.5 christos linux_nat_target::thread_alive (ptid_t ptid) 3954 1.5 christos { 3955 1.5 christos /* As long as a PTID is in lwp list, consider it alive. */ 3956 1.8 christos return find_lwp_pid (ptid) != NULL; 3957 1.8 christos } 3958 1.5 christos 3959 1.6 christos /* Implement the to_update_thread_list target method for this 3960 1.6 christos target. */ 3961 1.6 christos 3962 1.6 christos void 3963 1.6 christos linux_nat_target::update_thread_list () 3964 1.6 christos { 3965 1.6 christos /* We add/delete threads from the list as clone/exit events are 3966 1.10 christos processed, so just try deleting exited threads still in the 3967 1.6 christos thread list. */ 3968 1.6 christos delete_exited_threads (); 3969 1.6 christos 3970 1.6 christos /* Update the processor core that each lwp/thread was last seen 3971 1.6 christos running on. */ 3972 1.6 christos for (lwp_info *lwp : all_lwps ()) 3973 1.5 christos { 3974 1.5 christos /* Avoid accessing /proc if the thread hasn't run since we last 3975 1.5 christos time we fetched the thread's core. Accessing /proc becomes 3976 1.9 christos noticeably expensive when we have thousands of LWPs. */ 3977 1.8 christos if (lwp->core == -1) 3978 1.1 christos lwp->core = linux_common_core_of_thread (lwp->ptid); 3979 1.8 christos } 3980 1.8 christos } 3981 1.8 christos 3982 1.9 christos std::string 3983 1.1 christos linux_nat_target::pid_to_str (ptid_t ptid) 3984 1.1 christos { 3985 1.1 christos if (ptid.lwp_p () 3986 1.1 christos && (ptid.pid () != ptid.lwp () 3987 1.8 christos || num_lwps (ptid.pid ()) > 1)) 3988 1.8 christos return string_printf ("LWP %ld", ptid.lwp ()); 3989 1.1 christos 3990 1.6 christos return normal_pid_to_str (ptid); 3991 1.1 christos } 3992 1.1 christos 3993 1.1 christos const char * 3994 1.1 christos linux_nat_target::thread_name (struct thread_info *thr) 3995 1.1 christos { 3996 1.10 christos return linux_proc_tid_get_name (thr->ptid); 3997 1.8 christos } 3998 1.1 christos 3999 1.5 christos /* Accepts an integer PID; Returns a string representing a file that 4000 1.1 christos can be opened to get the symbols for the child process. */ 4001 1.1 christos 4002 1.10 christos const char * 4003 1.10 christos linux_nat_target::pid_to_exec_file (int pid) 4004 1.10 christos { 4005 1.10 christos return linux_proc_pid_to_exec_file (pid); 4006 1.10 christos } 4007 1.10 christos 4008 1.10 christos /* Object representing an /proc/PID/mem open file. We keep one such 4009 1.10 christos file open per inferior. 4010 1.10 christos 4011 1.10 christos It might be tempting to think about only ever opening one file at 4012 1.10 christos most for all inferiors, closing/reopening the file as we access 4013 1.10 christos memory of different inferiors, to minimize number of file 4014 1.10 christos descriptors open, which can otherwise run into resource limits. 4015 1.10 christos However, that does not work correctly -- if the inferior execs and 4016 1.10 christos we haven't processed the exec event yet, and, we opened a 4017 1.10 christos /proc/PID/mem file, we will get a mem file accessing the post-exec 4018 1.10 christos address space, thinking we're opening it for the pre-exec address 4019 1.10 christos space. That is dangerous as we can poke memory (e.g. clearing 4020 1.10 christos breakpoints) in the post-exec memory by mistake, corrupting the 4021 1.10 christos inferior. For that reason, we open the mem file as early as 4022 1.10 christos possible, right after spawning, forking or attaching to the 4023 1.10 christos inferior, when the inferior is stopped and thus before it has a 4024 1.10 christos chance of execing. 4025 1.10 christos 4026 1.10 christos Note that after opening the file, even if the thread we opened it 4027 1.10 christos for subsequently exits, the open file is still usable for accessing 4028 1.10 christos memory. It's only when the whole process exits or execs that the 4029 1.10 christos file becomes invalid, at which point reads/writes return EOF. */ 4030 1.10 christos 4031 1.10 christos class proc_mem_file 4032 1.10 christos { 4033 1.10 christos public: 4034 1.10 christos proc_mem_file (ptid_t ptid, int fd) 4035 1.10 christos : m_ptid (ptid), m_fd (fd) 4036 1.10 christos { 4037 1.10 christos gdb_assert (m_fd != -1); 4038 1.10 christos } 4039 1.10 christos 4040 1.10 christos ~proc_mem_file () 4041 1.10 christos { 4042 1.10 christos linux_nat_debug_printf ("closing fd %d for /proc/%d/task/%ld/mem", 4043 1.10 christos m_fd, m_ptid.pid (), m_ptid.lwp ()); 4044 1.10 christos close (m_fd); 4045 1.10 christos } 4046 1.10 christos 4047 1.10 christos DISABLE_COPY_AND_ASSIGN (proc_mem_file); 4048 1.10 christos 4049 1.10 christos int fd () 4050 1.10 christos { 4051 1.10 christos return m_fd; 4052 1.10 christos } 4053 1.10 christos 4054 1.10 christos private: 4055 1.10 christos /* The LWP this file was opened for. Just for debugging 4056 1.10 christos purposes. */ 4057 1.10 christos ptid_t m_ptid; 4058 1.10 christos 4059 1.10 christos /* The file descriptor. */ 4060 1.10 christos int m_fd = -1; 4061 1.10 christos }; 4062 1.10 christos 4063 1.10 christos /* The map between an inferior process id, and the open /proc/PID/mem 4064 1.10 christos file. This is stored in a map instead of in a per-inferior 4065 1.10 christos structure because we need to be able to access memory of processes 4066 1.10 christos which don't have a corresponding struct inferior object. E.g., 4067 1.10 christos with "detach-on-fork on" (the default), and "follow-fork parent" 4068 1.10 christos (also default), we don't create an inferior for the fork child, but 4069 1.10 christos we still need to remove breakpoints from the fork child's 4070 1.10 christos memory. */ 4071 1.10 christos static std::unordered_map<int, proc_mem_file> proc_mem_file_map; 4072 1.10 christos 4073 1.10 christos /* Close the /proc/PID/mem file for PID. */ 4074 1.10 christos 4075 1.10 christos static void 4076 1.10 christos close_proc_mem_file (pid_t pid) 4077 1.10 christos { 4078 1.10 christos proc_mem_file_map.erase (pid); 4079 1.10 christos } 4080 1.10 christos 4081 1.1 christos /* Open the /proc/PID/mem file for the process (thread group) of PTID. 4082 1.10 christos We actually open /proc/PID/task/LWP/mem, as that's the LWP we know 4083 1.10 christos exists and is stopped right now. We prefer the 4084 1.1 christos /proc/PID/task/LWP/mem form over /proc/LWP/mem to avoid tid-reuse 4085 1.10 christos races, just in case this is ever called on an already-waited 4086 1.10 christos LWP. */ 4087 1.10 christos 4088 1.1 christos static void 4089 1.10 christos open_proc_mem_file (ptid_t ptid) 4090 1.10 christos { 4091 1.10 christos auto iter = proc_mem_file_map.find (ptid.pid ()); 4092 1.10 christos gdb_assert (iter == proc_mem_file_map.end ()); 4093 1.10 christos 4094 1.10 christos char filename[64]; 4095 1.10 christos xsnprintf (filename, sizeof filename, 4096 1.10 christos "/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ()); 4097 1.10 christos 4098 1.10 christos int fd = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release (); 4099 1.10 christos 4100 1.10 christos if (fd == -1) 4101 1.1 christos { 4102 1.10 christos warning (_("opening /proc/PID/mem file for lwp %d.%ld failed: %s (%d)"), 4103 1.10 christos ptid.pid (), ptid.lwp (), 4104 1.10 christos safe_strerror (errno), errno); 4105 1.10 christos return; 4106 1.10 christos } 4107 1.10 christos 4108 1.10 christos proc_mem_file_map.emplace (std::piecewise_construct, 4109 1.10 christos std::forward_as_tuple (ptid.pid ()), 4110 1.10 christos std::forward_as_tuple (ptid, fd)); 4111 1.10 christos 4112 1.10 christos linux_nat_debug_printf ("opened fd %d for lwp %d.%ld", 4113 1.10 christos fd, ptid.pid (), ptid.lwp ()); 4114 1.1 christos } 4115 1.10 christos 4116 1.10 christos /* Helper for linux_proc_xfer_memory_partial and 4117 1.10 christos proc_mem_file_is_writable. FD is the already opened /proc/pid/mem 4118 1.10 christos file, and PID is the pid of the corresponding process. The rest of 4119 1.10 christos the arguments are like linux_proc_xfer_memory_partial's. */ 4120 1.10 christos 4121 1.10 christos static enum target_xfer_status 4122 1.1 christos linux_proc_xfer_memory_partial_fd (int fd, int pid, 4123 1.10 christos gdb_byte *readbuf, const gdb_byte *writebuf, 4124 1.1 christos ULONGEST offset, LONGEST len, 4125 1.11 christos ULONGEST *xfered_len) 4126 1.11 christos { 4127 1.11 christos ssize_t ret; 4128 1.11 christos 4129 1.11 christos gdb_assert (fd != -1); 4130 1.11 christos 4131 1.1 christos /* Use pread64/pwrite64 if available, since they save a syscall and 4132 1.11 christos can handle 64-bit offsets even on 32-bit platforms (for instance, 4133 1.11 christos SPARC debugging a SPARC64 application). But only use them if the 4134 1.11 christos offset isn't so high that when cast to off_t it'd be negative, as 4135 1.11 christos seen on SPARC64. pread64/pwrite64 outright reject such offsets. 4136 1.11 christos lseek does not. */ 4137 1.1 christos #ifdef HAVE_PREAD64 4138 1.11 christos if ((off_t) offset >= 0) 4139 1.11 christos ret = (readbuf != nullptr 4140 1.11 christos ? pread64 (fd, readbuf, len, offset) 4141 1.11 christos : pwrite64 (fd, writebuf, len, offset)); 4142 1.11 christos else 4143 1.11 christos #endif 4144 1.11 christos { 4145 1.1 christos ret = lseek (fd, offset, SEEK_SET); 4146 1.10 christos if (ret != -1) 4147 1.10 christos ret = (readbuf != nullptr 4148 1.10 christos ? read (fd, readbuf, len) 4149 1.10 christos : write (fd, writebuf, len)); 4150 1.10 christos } 4151 1.10 christos 4152 1.10 christos if (ret == -1) 4153 1.10 christos { 4154 1.10 christos linux_nat_debug_printf ("accessing fd %d for pid %d failed: %s (%d)", 4155 1.10 christos fd, pid, safe_strerror (errno), errno); 4156 1.10 christos return TARGET_XFER_E_IO; 4157 1.10 christos } 4158 1.10 christos else if (ret == 0) 4159 1.10 christos { 4160 1.3 christos /* EOF means the address space is gone, the whole process exited 4161 1.3 christos or execed. */ 4162 1.3 christos linux_nat_debug_printf ("accessing fd %d for pid %d got EOF", 4163 1.3 christos fd, pid); 4164 1.3 christos return TARGET_XFER_EOF; 4165 1.1 christos } 4166 1.1 christos else 4167 1.10 christos { 4168 1.10 christos *xfered_len = ret; 4169 1.10 christos return TARGET_XFER_OK; 4170 1.10 christos } 4171 1.10 christos } 4172 1.10 christos 4173 1.10 christos /* Implement the to_xfer_partial target method using /proc/PID/mem. 4174 1.10 christos Because we can use a single read/write call, this can be much more 4175 1.10 christos efficient than banging away at PTRACE_PEEKTEXT. Also, unlike 4176 1.10 christos PTRACE_PEEKTEXT/PTRACE_POKETEXT, this works with running 4177 1.10 christos threads. */ 4178 1.10 christos 4179 1.10 christos static enum target_xfer_status 4180 1.10 christos linux_proc_xfer_memory_partial (int pid, gdb_byte *readbuf, 4181 1.10 christos const gdb_byte *writebuf, ULONGEST offset, 4182 1.10 christos LONGEST len, ULONGEST *xfered_len) 4183 1.10 christos { 4184 1.10 christos auto iter = proc_mem_file_map.find (pid); 4185 1.10 christos if (iter == proc_mem_file_map.end ()) 4186 1.10 christos return TARGET_XFER_EOF; 4187 1.10 christos 4188 1.10 christos int fd = iter->second.fd (); 4189 1.10 christos 4190 1.10 christos return linux_proc_xfer_memory_partial_fd (fd, pid, readbuf, writebuf, offset, 4191 1.10 christos len, xfered_len); 4192 1.11 christos } 4193 1.10 christos 4194 1.10 christos /* Check whether /proc/pid/mem is writable in the current kernel, and 4195 1.10 christos return true if so. It wasn't writable before Linux 2.6.39, but 4196 1.10 christos there's no way to know whether the feature was backported to older 4197 1.10 christos kernels. So we check to see if it works. The result is cached, 4198 1.10 christos and this is guaranteed to be called once early during inferior 4199 1.10 christos startup, so that any warning is printed out consistently between 4200 1.10 christos GDB invocations. Note we don't call it during GDB startup instead 4201 1.11 christos though, because then we might warn with e.g. just "gdb --version" 4202 1.10 christos on sandboxed systems. See PR gdb/29907. */ 4203 1.10 christos 4204 1.10 christos static bool 4205 1.10 christos proc_mem_file_is_writable () 4206 1.10 christos { 4207 1.10 christos static std::optional<bool> writable; 4208 1.10 christos 4209 1.10 christos if (writable.has_value ()) 4210 1.10 christos return *writable; 4211 1.10 christos 4212 1.10 christos writable.emplace (false); 4213 1.10 christos 4214 1.10 christos /* We check whether /proc/pid/mem is writable by trying to write to 4215 1.10 christos one of our variables via /proc/self/mem. */ 4216 1.10 christos 4217 1.10 christos int fd = gdb_open_cloexec ("/proc/self/mem", O_RDWR | O_LARGEFILE, 0).release (); 4218 1.10 christos 4219 1.10 christos if (fd == -1) 4220 1.10 christos { 4221 1.10 christos warning (_("opening /proc/self/mem file failed: %s (%d)"), 4222 1.10 christos safe_strerror (errno), errno); 4223 1.10 christos return *writable; 4224 1.10 christos } 4225 1.10 christos 4226 1.10 christos SCOPE_EXIT { close (fd); }; 4227 1.10 christos 4228 1.10 christos /* This is the variable we try to write to. Note OFFSET below. */ 4229 1.10 christos volatile gdb_byte test_var = 0; 4230 1.10 christos 4231 1.10 christos gdb_byte writebuf[] = {0x55}; 4232 1.10 christos ULONGEST offset = (uintptr_t) &test_var; 4233 1.10 christos ULONGEST xfered_len; 4234 1.10 christos 4235 1.10 christos enum target_xfer_status res 4236 1.10 christos = linux_proc_xfer_memory_partial_fd (fd, getpid (), nullptr, writebuf, 4237 1.10 christos offset, 1, &xfered_len); 4238 1.10 christos 4239 1.10 christos if (res == TARGET_XFER_OK) 4240 1.10 christos { 4241 1.10 christos gdb_assert (xfered_len == 1); 4242 1.10 christos gdb_assert (test_var == 0x55); 4243 1.1 christos /* Success. */ 4244 1.1 christos *writable = true; 4245 1.1 christos } 4246 1.1 christos 4247 1.1 christos return *writable; 4248 1.1 christos } 4249 1.1 christos 4250 1.1 christos /* Parse LINE as a signal set and add its set bits to SIGS. */ 4251 1.1 christos 4252 1.1 christos static void 4253 1.1 christos add_line_to_sigset (const char *line, sigset_t *sigs) 4254 1.1 christos { 4255 1.1 christos int len = strlen (line) - 1; 4256 1.1 christos const char *p; 4257 1.1 christos int signum; 4258 1.1 christos 4259 1.1 christos if (line[len] != '\n') 4260 1.1 christos error (_("Could not parse signal set: %s"), line); 4261 1.1 christos 4262 1.1 christos p = line; 4263 1.1 christos signum = len * 4; 4264 1.1 christos while (len-- > 0) 4265 1.1 christos { 4266 1.1 christos int digit; 4267 1.1 christos 4268 1.1 christos if (*p >= '0' && *p <= '9') 4269 1.1 christos digit = *p - '0'; 4270 1.1 christos else if (*p >= 'a' && *p <= 'f') 4271 1.1 christos digit = *p - 'a' + 10; 4272 1.1 christos else 4273 1.1 christos error (_("Could not parse signal set: %s"), line); 4274 1.1 christos 4275 1.1 christos signum -= 4; 4276 1.1 christos 4277 1.1 christos if (digit & 1) 4278 1.1 christos sigaddset (sigs, signum + 1); 4279 1.1 christos if (digit & 2) 4280 1.1 christos sigaddset (sigs, signum + 2); 4281 1.1 christos if (digit & 4) 4282 1.1 christos sigaddset (sigs, signum + 3); 4283 1.1 christos if (digit & 8) 4284 1.1 christos sigaddset (sigs, signum + 4); 4285 1.1 christos 4286 1.1 christos p++; 4287 1.1 christos } 4288 1.1 christos } 4289 1.1 christos 4290 1.1 christos /* Find process PID's pending signals from /proc/pid/status and set 4291 1.1 christos SIGS to match. */ 4292 1.1 christos 4293 1.1 christos void 4294 1.1 christos linux_proc_pending_signals (int pid, sigset_t *pending, 4295 1.1 christos sigset_t *blocked, sigset_t *ignored) 4296 1.1 christos { 4297 1.8 christos char buffer[PATH_MAX], fname[PATH_MAX]; 4298 1.1 christos 4299 1.1 christos sigemptyset (pending); 4300 1.1 christos sigemptyset (blocked); 4301 1.8 christos sigemptyset (ignored); 4302 1.1 christos xsnprintf (fname, sizeof fname, "/proc/%d/status", pid); 4303 1.1 christos gdb_file_up procfile = gdb_fopen_cloexec (fname, "r"); 4304 1.1 christos if (procfile == NULL) 4305 1.1 christos error (_("Could not open %s"), fname); 4306 1.1 christos 4307 1.1 christos while (fgets (buffer, PATH_MAX, procfile.get ()) != NULL) 4308 1.1 christos { 4309 1.1 christos /* Normal queued signals are on the SigPnd line in the status 4310 1.1 christos file. However, 2.6 kernels also have a "shared" pending 4311 1.5 christos queue for delivering signals to a thread group, so check for 4312 1.1 christos a ShdPnd line also. 4313 1.5 christos 4314 1.1 christos Unfortunately some Red Hat kernels include the shared pending 4315 1.5 christos queue but not the ShdPnd status field. */ 4316 1.1 christos 4317 1.5 christos if (startswith (buffer, "SigPnd:\t")) 4318 1.1 christos add_line_to_sigset (buffer + 8, pending); 4319 1.1 christos else if (startswith (buffer, "ShdPnd:\t")) 4320 1.1 christos add_line_to_sigset (buffer + 8, pending); 4321 1.1 christos else if (startswith (buffer, "SigBlk:\t")) 4322 1.3 christos add_line_to_sigset (buffer + 8, blocked); 4323 1.8 christos else if (startswith (buffer, "SigIgn:\t")) 4324 1.1 christos add_line_to_sigset (buffer + 8, ignored); 4325 1.3 christos } 4326 1.3 christos } 4327 1.1 christos 4328 1.1 christos static enum target_xfer_status 4329 1.1 christos linux_nat_xfer_osdata (enum target_object object, 4330 1.3 christos const char *annex, gdb_byte *readbuf, 4331 1.3 christos const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, 4332 1.3 christos ULONGEST *xfered_len) 4333 1.3 christos { 4334 1.3 christos gdb_assert (object == TARGET_OBJECT_OSDATA); 4335 1.1 christos 4336 1.1 christos *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len); 4337 1.8 christos if (*xfered_len == 0) 4338 1.8 christos return TARGET_XFER_EOF; 4339 1.1 christos else 4340 1.1 christos return TARGET_XFER_OK; 4341 1.8 christos } 4342 1.8 christos 4343 1.8 christos std::vector<static_tracepoint_marker> 4344 1.10 christos linux_nat_target::static_tracepoint_markers_by_strid (const char *strid) 4345 1.8 christos { 4346 1.1 christos char s[IPA_CMD_BUF_SIZE]; 4347 1.1 christos int pid = inferior_ptid.pid (); 4348 1.1 christos std::vector<static_tracepoint_marker> markers; 4349 1.1 christos const char *p = s; 4350 1.11 christos ptid_t ptid = ptid_t (pid, 0); 4351 1.1 christos static_tracepoint_marker marker; 4352 1.1 christos 4353 1.8 christos /* Pause all */ 4354 1.8 christos target_stop (ptid); 4355 1.1 christos 4356 1.1 christos strcpy (s, "qTfSTM"); 4357 1.1 christos agent_run_command (pid, s, strlen (s) + 1); 4358 1.1 christos 4359 1.1 christos /* Unpause all. */ 4360 1.8 christos SCOPE_EXIT { target_continue_no_signal (ptid); }; 4361 1.1 christos 4362 1.8 christos while (*p++ == 'm') 4363 1.8 christos { 4364 1.1 christos do 4365 1.1 christos { 4366 1.1 christos parse_static_tracepoint_marker_definition (p, &p, &marker); 4367 1.11 christos 4368 1.1 christos if (strid == NULL || marker.str_id == strid) 4369 1.1 christos markers.push_back (std::move (marker)); 4370 1.1 christos } 4371 1.1 christos while (*p++ == ','); /* comma-separated list */ 4372 1.1 christos 4373 1.1 christos strcpy (s, "qTsSTM"); 4374 1.1 christos agent_run_command (pid, s, strlen (s) + 1); 4375 1.1 christos p = s; 4376 1.1 christos } 4377 1.8 christos 4378 1.8 christos return markers; 4379 1.1 christos } 4380 1.10 christos 4381 1.10 christos /* target_can_async_p implementation. */ 4382 1.10 christos 4383 1.10 christos bool 4384 1.10 christos linux_nat_target::can_async_p () 4385 1.1 christos { 4386 1.1 christos /* This flag should be checked in the common target.c code. */ 4387 1.8 christos gdb_assert (target_async_permitted); 4388 1.8 christos 4389 1.1 christos /* Otherwise, this targets is always able to support async mode. */ 4390 1.9 christos return true; 4391 1.1 christos } 4392 1.1 christos 4393 1.6 christos bool 4394 1.6 christos linux_nat_target::supports_non_stop () 4395 1.8 christos { 4396 1.8 christos return true; 4397 1.6 christos } 4398 1.9 christos 4399 1.6 christos /* to_always_non_stop_p implementation. */ 4400 1.6 christos 4401 1.8 christos bool 4402 1.8 christos linux_nat_target::always_non_stop_p () 4403 1.1 christos { 4404 1.9 christos return true; 4405 1.1 christos } 4406 1.1 christos 4407 1.8 christos bool 4408 1.8 christos linux_nat_target::supports_multi_process () 4409 1.1 christos { 4410 1.9 christos return true; 4411 1.1 christos } 4412 1.1 christos 4413 1.1 christos bool 4414 1.1 christos linux_nat_target::supports_disable_randomization () 4415 1.1 christos { 4416 1.1 christos return true; 4417 1.1 christos } 4418 1.1 christos 4419 1.1 christos /* SIGCHLD handler that serves two purposes: In non-stop/async mode, 4420 1.1 christos so we notice when any child changes state, and notify the 4421 1.1 christos event-loop; it allows us to use sigsuspend in linux_nat_wait_1 4422 1.1 christos above to wait for the arrival of a SIGCHLD. */ 4423 1.1 christos 4424 1.9 christos static void 4425 1.1 christos sigchld_handler (int signo) 4426 1.10 christos { 4427 1.10 christos int old_errno = errno; 4428 1.10 christos 4429 1.10 christos if (debug_linux_nat) 4430 1.10 christos gdb_stdlog->write_async_safe ("sigchld\n", sizeof ("sigchld\n") - 1); 4431 1.1 christos 4432 1.1 christos if (signo == SIGCHLD) 4433 1.1 christos { 4434 1.1 christos /* Let the event loop know that there are events to handle. */ 4435 1.1 christos linux_nat_target::async_file_mark_if_open (); 4436 1.1 christos } 4437 1.1 christos 4438 1.1 christos errno = old_errno; 4439 1.1 christos } 4440 1.9 christos 4441 1.1 christos /* Callback registered with the target events file descriptor. */ 4442 1.1 christos 4443 1.10 christos static void 4444 1.1 christos handle_target_event (int error, gdb_client_data client_data) 4445 1.10 christos { 4446 1.10 christos inferior_event_handler (INF_REG_EVENT); 4447 1.1 christos } 4448 1.10 christos 4449 1.10 christos /* target_async implementation. */ 4450 1.1 christos 4451 1.10 christos void 4452 1.10 christos linux_nat_target::async (bool enable) 4453 1.10 christos { 4454 1.10 christos if (enable == is_async_p ()) 4455 1.10 christos return; 4456 1.1 christos 4457 1.10 christos /* Block child signals while we create/destroy the pipe, as their 4458 1.10 christos handler writes to it. */ 4459 1.1 christos gdb::block_signals blocker; 4460 1.10 christos 4461 1.10 christos if (enable) 4462 1.1 christos { 4463 1.10 christos if (!async_file_open ()) 4464 1.10 christos internal_error ("creating event pipe failed."); 4465 1.10 christos 4466 1.1 christos add_file_handler (async_wait_fd (), handle_target_event, NULL, 4467 1.1 christos "linux-nat"); 4468 1.1 christos 4469 1.10 christos /* There may be pending events to handle. Tell the event loop 4470 1.10 christos to poll them. */ 4471 1.1 christos async_file_mark (); 4472 1.1 christos } 4473 1.1 christos else 4474 1.1 christos { 4475 1.1 christos delete_file_handler (async_wait_fd ()); 4476 1.1 christos async_file_close (); 4477 1.1 christos } 4478 1.9 christos } 4479 1.1 christos 4480 1.1 christos /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other 4481 1.1 christos event came out. */ 4482 1.9 christos 4483 1.10 christos static int 4484 1.1 christos linux_nat_stop_lwp (struct lwp_info *lwp) 4485 1.1 christos { 4486 1.1 christos if (!lwp->stopped) 4487 1.1 christos { 4488 1.9 christos linux_nat_debug_printf ("running -> suspending %s", 4489 1.9 christos lwp->ptid.to_string ().c_str ()); 4490 1.1 christos 4491 1.1 christos 4492 1.1 christos if (lwp->last_resume_kind == resume_stop) 4493 1.9 christos { 4494 1.1 christos linux_nat_debug_printf ("already stopping LWP %ld at GDB's request", 4495 1.1 christos lwp->ptid.lwp ()); 4496 1.1 christos return 0; 4497 1.1 christos } 4498 1.1 christos 4499 1.1 christos stop_callback (lwp); 4500 1.1 christos lwp->last_resume_kind = resume_stop; 4501 1.1 christos } 4502 1.11 christos else 4503 1.9 christos { 4504 1.10 christos /* Already known to be stopped; do nothing. */ 4505 1.1 christos 4506 1.9 christos if (debug_linux_nat) 4507 1.10 christos { 4508 1.1 christos if (linux_target->find_thread (lwp->ptid)->stop_requested) 4509 1.1 christos linux_nat_debug_printf ("already stopped/stop_requested %s", 4510 1.1 christos lwp->ptid.to_string ().c_str ()); 4511 1.1 christos else 4512 1.1 christos linux_nat_debug_printf ("already stopped/no stop_requested yet %s", 4513 1.8 christos lwp->ptid.to_string ().c_str ()); 4514 1.8 christos } 4515 1.1 christos } 4516 1.10 christos return 0; 4517 1.9 christos } 4518 1.1 christos 4519 1.1 christos void 4520 1.1 christos linux_nat_target::stop (ptid_t ptid) 4521 1.1 christos { 4522 1.8 christos LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT; 4523 1.8 christos iterate_over_lwps (ptid, linux_nat_stop_lwp); 4524 1.1 christos } 4525 1.1 christos 4526 1.1 christos /* Return the cached value of the processor core for thread PTID. */ 4527 1.1 christos 4528 1.1 christos int 4529 1.1 christos linux_nat_target::core_of_thread (ptid_t ptid) 4530 1.1 christos { 4531 1.1 christos struct lwp_info *info = find_lwp_pid (ptid); 4532 1.5 christos 4533 1.5 christos if (info) 4534 1.8 christos return info->core; 4535 1.8 christos return -1; 4536 1.5 christos } 4537 1.5 christos 4538 1.5 christos /* Implementation of to_filesystem_is_local. */ 4539 1.5 christos 4540 1.8 christos bool 4541 1.5 christos linux_nat_target::filesystem_is_local () 4542 1.5 christos { 4543 1.5 christos struct inferior *inf = current_inferior (); 4544 1.5 christos 4545 1.5 christos if (inf->fake_pid_p || inf->pid == 0) 4546 1.5 christos return true; 4547 1.5 christos 4548 1.5 christos return linux_ns_same (inf->pid, LINUX_NS_MNT); 4549 1.5 christos } 4550 1.5 christos 4551 1.5 christos /* Convert the INF argument passed to a to_fileio_* method 4552 1.5 christos to a process ID suitable for passing to its corresponding 4553 1.5 christos linux_mntns_* function. If INF is non-NULL then the 4554 1.5 christos caller is requesting the filesystem seen by INF. If INF 4555 1.5 christos is NULL then the caller is requesting the filesystem seen 4556 1.5 christos by the GDB. We fall back to GDB's filesystem in the case 4557 1.5 christos that INF is non-NULL but its PID is unknown. */ 4558 1.5 christos 4559 1.5 christos static pid_t 4560 1.5 christos linux_nat_fileio_pid_of (struct inferior *inf) 4561 1.5 christos { 4562 1.5 christos if (inf == NULL || inf->fake_pid_p || inf->pid == 0) 4563 1.5 christos return getpid (); 4564 1.8 christos else 4565 1.8 christos return inf->pid; 4566 1.8 christos } 4567 1.10 christos 4568 1.5 christos /* Implementation of to_fileio_open. */ 4569 1.5 christos 4570 1.5 christos int 4571 1.5 christos linux_nat_target::fileio_open (struct inferior *inf, const char *filename, 4572 1.5 christos int flags, int mode, int warn_if_slow, 4573 1.5 christos fileio_error *target_errno) 4574 1.5 christos { 4575 1.5 christos int nat_flags; 4576 1.5 christos mode_t nat_mode; 4577 1.5 christos int fd; 4578 1.5 christos 4579 1.5 christos if (fileio_to_host_openflags (flags, &nat_flags) == -1 4580 1.5 christos || fileio_to_host_mode (mode, &nat_mode) == -1) 4581 1.5 christos { 4582 1.5 christos *target_errno = FILEIO_EINVAL; 4583 1.5 christos return -1; 4584 1.5 christos } 4585 1.5 christos 4586 1.5 christos fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf), 4587 1.5 christos filename, nat_flags, nat_mode); 4588 1.5 christos if (fd == -1) 4589 1.5 christos *target_errno = host_to_fileio_error (errno); 4590 1.11 christos 4591 1.8 christos return fd; 4592 1.10 christos } 4593 1.5 christos 4594 1.5 christos /* Implementation of to_fileio_readlink. */ 4595 1.5 christos 4596 1.5 christos std::optional<std::string> 4597 1.5 christos linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename, 4598 1.5 christos fileio_error *target_errno) 4599 1.5 christos { 4600 1.5 christos char buf[PATH_MAX]; 4601 1.5 christos int len; 4602 1.8 christos 4603 1.5 christos len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf), 4604 1.5 christos filename, buf, sizeof (buf)); 4605 1.8 christos if (len < 0) 4606 1.5 christos { 4607 1.5 christos *target_errno = host_to_fileio_error (errno); 4608 1.5 christos return {}; 4609 1.5 christos } 4610 1.8 christos 4611 1.8 christos return std::string (buf, len); 4612 1.10 christos } 4613 1.5 christos 4614 1.5 christos /* Implementation of to_fileio_unlink. */ 4615 1.5 christos 4616 1.5 christos int 4617 1.5 christos linux_nat_target::fileio_unlink (struct inferior *inf, const char *filename, 4618 1.5 christos fileio_error *target_errno) 4619 1.5 christos { 4620 1.5 christos int ret; 4621 1.5 christos 4622 1.5 christos ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf), 4623 1.5 christos filename); 4624 1.6 christos if (ret == -1) 4625 1.6 christos *target_errno = host_to_fileio_error (errno); 4626 1.8 christos 4627 1.12 christos return ret; 4628 1.6 christos } 4629 1.6 christos 4630 1.6 christos /* Implementation of the to_thread_events method. */ 4631 1.6 christos 4632 1.11 christos void 4633 1.11 christos linux_nat_target::thread_events (bool enable) 4634 1.11 christos { 4635 1.11 christos report_thread_events = enable; 4636 1.11 christos } 4637 1.11 christos 4638 1.11 christos bool 4639 1.11 christos linux_nat_target::supports_set_thread_options (gdb_thread_options options) 4640 1.8 christos { 4641 1.1 christos constexpr gdb_thread_options supported_options 4642 1.1 christos = GDB_THREAD_OPTION_CLONE | GDB_THREAD_OPTION_EXIT; 4643 1.1 christos return ((options & supported_options) == options); 4644 1.1 christos } 4645 1.1 christos 4646 1.1 christos linux_nat_target::linux_nat_target () 4647 1.1 christos { 4648 1.1 christos /* We don't change the stratum; this target will sit at 4649 1.1 christos process_stratum and thread_db will set at thread_stratum. This 4650 1.1 christos is a little strange, since this is a multi-threaded-capable 4651 1.10 christos target, but we want to be on the stack below thread_db, and we 4652 1.1 christos also want to be used for single-threaded processes. */ 4653 1.1 christos } 4654 1.10 christos 4655 1.10 christos /* See linux-nat.h. */ 4656 1.1 christos 4657 1.1 christos bool 4658 1.5 christos linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo) 4659 1.5 christos { 4660 1.5 christos int pid = get_ptrace_pid (ptid); 4661 1.5 christos return ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo) == 0; 4662 1.5 christos } 4663 1.8 christos 4664 1.5 christos /* See nat/linux-nat.h. */ 4665 1.5 christos 4666 1.5 christos ptid_t 4667 1.11 christos current_lwp_ptid (void) 4668 1.11 christos { 4669 1.11 christos gdb_assert (inferior_ptid.lwp_p ()); 4670 1.11 christos return inferior_ptid; 4671 1.11 christos } 4672 1.11 christos 4673 1.11 christos /* Implement 'maintenance info linux-lwps'. Displays some basic 4674 1.11 christos information about all the current lwp_info objects. */ 4675 1.11 christos 4676 1.11 christos static void 4677 1.11 christos maintenance_info_lwps (const char *arg, int from_tty) 4678 1.11 christos { 4679 1.11 christos if (all_lwps ().size () == 0) 4680 1.11 christos { 4681 1.11 christos gdb_printf ("No Linux LWPs\n"); 4682 1.11 christos return; 4683 1.11 christos } 4684 1.11 christos 4685 1.11 christos /* Start the width at 8 to match the column heading below, then 4686 1.11 christos figure out the widest ptid string. We'll use this to build our 4687 1.11 christos output table below. */ 4688 1.11 christos size_t ptid_width = 8; 4689 1.11 christos for (lwp_info *lp : all_lwps ()) 4690 1.11 christos ptid_width = std::max (ptid_width, lp->ptid.to_string ().size ()); 4691 1.11 christos 4692 1.11 christos /* Setup the table headers. */ 4693 1.11 christos struct ui_out *uiout = current_uiout; 4694 1.11 christos ui_out_emit_table table_emitter (uiout, 2, -1, "linux-lwps"); 4695 1.11 christos uiout->table_header (ptid_width, ui_left, "lwp-ptid", _("LWP Ptid")); 4696 1.11 christos uiout->table_header (9, ui_left, "thread-info", _("Thread ID")); 4697 1.11 christos uiout->table_body (); 4698 1.11 christos 4699 1.11 christos /* Display one table row for each lwp_info. */ 4700 1.11 christos for (lwp_info *lp : all_lwps ()) 4701 1.11 christos { 4702 1.11 christos ui_out_emit_tuple tuple_emitter (uiout, "lwp-entry"); 4703 1.11 christos 4704 1.11 christos thread_info *th = linux_target->find_thread (lp->ptid); 4705 1.11 christos 4706 1.11 christos uiout->field_string ("lwp-ptid", lp->ptid.to_string ().c_str ()); 4707 1.11 christos if (th == nullptr) 4708 1.11 christos uiout->field_string ("thread-info", "None"); 4709 1.11 christos else 4710 1.9 christos uiout->field_string ("thread-info", print_full_thread_id (th)); 4711 1.1 christos 4712 1.9 christos uiout->message ("\n"); 4713 1.1 christos } 4714 1.10 christos } 4715 1.10 christos 4716 1.11 christos void _initialize_linux_nat (); 4717 1.11 christos void 4718 1.10 christos _initialize_linux_nat () 4719 1.10 christos { 4720 1.10 christos add_setshow_boolean_cmd ("linux-nat", class_maintenance, 4721 1.10 christos &debug_linux_nat, _("\ 4722 1.1 christos Set debugging of GNU/Linux native target."), _("\ 4723 1.5 christos Show debugging of GNU/Linux native target."), _("\ 4724 1.5 christos When on, print debug messages relating to the GNU/Linux native target."), 4725 1.5 christos nullptr, 4726 1.5 christos show_debug_linux_nat, 4727 1.5 christos &setdebuglist, &showdebuglist); 4728 1.5 christos 4729 1.5 christos add_setshow_boolean_cmd ("linux-namespaces", class_maintenance, 4730 1.5 christos &debug_linux_namespaces, _("\ 4731 1.5 christos Set debugging of GNU/Linux namespaces module."), _("\ 4732 1.1 christos Show debugging of GNU/Linux namespaces module."), _("\ 4733 1.1 christos Enables printf debugging output."), 4734 1.1 christos NULL, 4735 1.1 christos NULL, 4736 1.1 christos &setdebuglist, &showdebuglist); 4737 1.1 christos 4738 1.1 christos /* Install a SIGCHLD handler. */ 4739 1.1 christos sigchld_action.sa_handler = sigchld_handler; 4740 1.1 christos sigemptyset (&sigchld_action.sa_mask); 4741 1.9 christos sigchld_action.sa_flags = SA_RESTART; 4742 1.1 christos 4743 1.1 christos /* Make it the default. */ 4744 1.1 christos sigaction (SIGCHLD, &sigchld_action, NULL); 4745 1.6 christos 4746 1.6 christos /* Make sure we don't block SIGCHLD during a sigsuspend. */ 4747 1.11 christos gdb_sigmask (SIG_SETMASK, NULL, &suspend_mask); 4748 1.11 christos sigdelset (&suspend_mask, SIGCHLD); 4749 1.11 christos 4750 1.1 christos sigemptyset (&blocked_mask); 4751 1.1 christos 4752 1.1 christos lwp_lwpid_htab_create (); 4753 1.1 christos 4754 1.1 christos add_cmd ("linux-lwps", class_maintenance, maintenance_info_lwps, 4755 1.1 christos _("List the Linux LWPS."), &maintenanceinfolist); 4756 1.1 christos } 4757 1.10 christos 4758 1.10 christos 4760 1.10 christos /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to 4761 1.1 christos the GNU/Linux Threads library and therefore doesn't really belong 4762 1.10 christos here. */ 4763 1.10 christos 4764 1.10 christos /* NPTL reserves the first two RT signals, but does not provide any 4765 1.10 christos way for the debugger to query the signal numbers - fortunately 4766 1.1 christos they don't change. */ 4767 1.10 christos static int lin_thread_signals[] = { __SIGRTMIN, __SIGRTMIN + 1 }; 4768 1.10 christos 4769 1.10 christos /* See linux-nat.h. */ 4770 1.10 christos 4771 1.1 christos unsigned int 4772 1.10 christos lin_thread_get_thread_signal_num (void) 4773 1.10 christos { 4774 1.10 christos return sizeof (lin_thread_signals) / sizeof (lin_thread_signals[0]); 4775 1.10 christos } 4776 1.10 christos 4777 1.1 christos /* See linux-nat.h. */ 4778 4779 int 4780 lin_thread_get_thread_signal (unsigned int i) 4781 { 4782 gdb_assert (i < lin_thread_get_thread_signal_num ()); 4783 return lin_thread_signals[i]; 4784 } 4785