1 1.1 christos /* Native-dependent code for FreeBSD. 2 1.1 christos 3 1.11 christos Copyright (C) 2002-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.10 christos #include "gdbsupport/block-signals.h" 21 1.9 christos #include "gdbsupport/byte-vector.h" 22 1.10 christos #include "gdbsupport/event-loop.h" 23 1.1 christos #include "gdbcore.h" 24 1.1 christos #include "inferior.h" 25 1.1 christos #include "regcache.h" 26 1.1 christos #include "regset.h" 27 1.9 christos #include "gdbarch.h" 28 1.11 christos #include "cli/cli-cmds.h" 29 1.1 christos #include "gdbthread.h" 30 1.10 christos #include "gdbsupport/buildargv.h" 31 1.9 christos #include "gdbsupport/gdb_wait.h" 32 1.10 christos #include "inf-loop.h" 33 1.8 christos #include "inf-ptrace.h" 34 1.1 christos #include <sys/types.h> 35 1.10 christos #ifdef HAVE_SYS_PROCCTL_H 36 1.10 christos #include <sys/procctl.h> 37 1.10 christos #endif 38 1.1 christos #include <sys/procfs.h> 39 1.5 christos #include <sys/ptrace.h> 40 1.8 christos #include <sys/signal.h> 41 1.1 christos #include <sys/sysctl.h> 42 1.5 christos #include <sys/user.h> 43 1.5 christos #include <libutil.h> 44 1.1 christos 45 1.1 christos #include "elf-bfd.h" 46 1.1 christos #include "fbsd-nat.h" 47 1.8 christos #include "fbsd-tdep.h" 48 1.12 christos #include "gdbsupport/eintr.h" 49 1.8 christos 50 1.10 christos #ifndef PT_GETREGSET 51 1.10 christos #define PT_GETREGSET 42 /* Get a target register set */ 52 1.10 christos #define PT_SETREGSET 43 /* Set a target register set */ 53 1.10 christos #endif 54 1.10 christos 55 1.11 christos /* Information stored about each inferior. */ 56 1.11 christos struct fbsd_inferior : public private_inferior 57 1.11 christos { 58 1.11 christos /* Filter for resumed LWPs which can report events from wait. */ 59 1.11 christos ptid_t resumed_lwps = null_ptid; 60 1.11 christos 61 1.11 christos /* Number of LWPs this process contains. */ 62 1.11 christos unsigned int num_lwps = 0; 63 1.11 christos 64 1.11 christos /* Number of LWPs currently running. */ 65 1.11 christos unsigned int running_lwps = 0; 66 1.11 christos 67 1.11 christos /* Have a pending SIGSTOP event that needs to be discarded. */ 68 1.11 christos bool pending_sigstop = false; 69 1.11 christos }; 70 1.11 christos 71 1.11 christos /* Return the fbsd_inferior attached to INF. */ 72 1.11 christos 73 1.11 christos static inline fbsd_inferior * 74 1.11 christos get_fbsd_inferior (inferior *inf) 75 1.11 christos { 76 1.11 christos return gdb::checked_static_cast<fbsd_inferior *> (inf->priv.get ()); 77 1.11 christos } 78 1.11 christos 79 1.11 christos /* See fbsd-nat.h. */ 80 1.11 christos 81 1.11 christos void 82 1.11 christos fbsd_nat_target::add_pending_event (const ptid_t &ptid, 83 1.11 christos const target_waitstatus &status) 84 1.11 christos { 85 1.11 christos gdb_assert (find_inferior_ptid (this, ptid) != nullptr); 86 1.11 christos m_pending_events.emplace_back (ptid, status); 87 1.11 christos } 88 1.11 christos 89 1.11 christos /* See fbsd-nat.h. */ 90 1.11 christos 91 1.11 christos bool 92 1.11 christos fbsd_nat_target::have_pending_event (ptid_t filter) 93 1.11 christos { 94 1.11 christos for (const pending_event &event : m_pending_events) 95 1.11 christos if (event.ptid.matches (filter)) 96 1.11 christos return true; 97 1.11 christos return false; 98 1.11 christos } 99 1.11 christos 100 1.11 christos /* See fbsd-nat.h. */ 101 1.11 christos 102 1.11 christos std::optional<fbsd_nat_target::pending_event> 103 1.11 christos fbsd_nat_target::take_pending_event (ptid_t filter) 104 1.11 christos { 105 1.11 christos for (auto it = m_pending_events.begin (); it != m_pending_events.end (); it++) 106 1.11 christos if (it->ptid.matches (filter)) 107 1.11 christos { 108 1.11 christos inferior *inf = find_inferior_ptid (this, it->ptid); 109 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 110 1.11 christos if (it->ptid.matches (fbsd_inf->resumed_lwps)) 111 1.11 christos { 112 1.11 christos pending_event event = *it; 113 1.11 christos m_pending_events.erase (it); 114 1.11 christos return event; 115 1.11 christos } 116 1.11 christos } 117 1.11 christos return {}; 118 1.11 christos } 119 1.11 christos 120 1.1 christos /* Return the name of a file that can be opened to get the symbols for 121 1.1 christos the child process identified by PID. */ 122 1.1 christos 123 1.10 christos const char * 124 1.8 christos fbsd_nat_target::pid_to_exec_file (int pid) 125 1.1 christos { 126 1.3 christos static char buf[PATH_MAX]; 127 1.6 christos size_t buflen; 128 1.1 christos int mib[4]; 129 1.1 christos 130 1.1 christos mib[0] = CTL_KERN; 131 1.1 christos mib[1] = KERN_PROC; 132 1.1 christos mib[2] = KERN_PROC_PATHNAME; 133 1.1 christos mib[3] = pid; 134 1.6 christos buflen = sizeof buf; 135 1.6 christos if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) 136 1.8 christos /* The kern.proc.pathname.<pid> sysctl returns a length of zero 137 1.8 christos for processes without an associated executable such as kernel 138 1.8 christos processes. */ 139 1.8 christos return buflen == 0 ? NULL : buf; 140 1.1 christos 141 1.3 christos return NULL; 142 1.1 christos } 143 1.1 christos 144 1.5 christos /* Iterate over all the memory regions in the current inferior, 145 1.9 christos calling FUNC for each memory region. DATA is passed as the last 146 1.5 christos argument to FUNC. */ 147 1.5 christos 148 1.8 christos int 149 1.8 christos fbsd_nat_target::find_memory_regions (find_memory_region_ftype func, 150 1.9 christos void *data) 151 1.5 christos { 152 1.8 christos pid_t pid = inferior_ptid.pid (); 153 1.8 christos struct kinfo_vmentry *kve; 154 1.5 christos uint64_t size; 155 1.5 christos int i, nitems; 156 1.5 christos 157 1.8 christos gdb::unique_xmalloc_ptr<struct kinfo_vmentry> 158 1.8 christos vmentl (kinfo_getvmmap (pid, &nitems)); 159 1.5 christos if (vmentl == NULL) 160 1.10 christos perror_with_name (_("Couldn't fetch VM map entries")); 161 1.5 christos 162 1.8 christos for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++) 163 1.5 christos { 164 1.5 christos /* Skip unreadable segments and those where MAP_NOCORE has been set. */ 165 1.5 christos if (!(kve->kve_protection & KVME_PROT_READ) 166 1.5 christos || kve->kve_flags & KVME_FLAG_NOCOREDUMP) 167 1.5 christos continue; 168 1.5 christos 169 1.5 christos /* Skip segments with an invalid type. */ 170 1.5 christos if (kve->kve_type != KVME_TYPE_DEFAULT 171 1.5 christos && kve->kve_type != KVME_TYPE_VNODE 172 1.5 christos && kve->kve_type != KVME_TYPE_SWAP 173 1.5 christos && kve->kve_type != KVME_TYPE_PHYS) 174 1.5 christos continue; 175 1.5 christos 176 1.5 christos size = kve->kve_end - kve->kve_start; 177 1.5 christos if (info_verbose) 178 1.5 christos { 179 1.10 christos gdb_printf ("Save segment, %ld bytes at %s (%c%c%c)\n", 180 1.10 christos (long) size, 181 1.11 christos paddress (current_inferior ()->arch (), kve->kve_start), 182 1.10 christos kve->kve_protection & KVME_PROT_READ ? 'r' : '-', 183 1.10 christos kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-', 184 1.10 christos kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-'); 185 1.5 christos } 186 1.5 christos 187 1.5 christos /* Invoke the callback function to create the corefile segment. 188 1.5 christos Pass MODIFIED as true, we do not know the real modification state. */ 189 1.5 christos func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ, 190 1.5 christos kve->kve_protection & KVME_PROT_WRITE, 191 1.10 christos kve->kve_protection & KVME_PROT_EXEC, 1, false, data); 192 1.5 christos } 193 1.5 christos return 0; 194 1.5 christos } 195 1.5 christos 196 1.8 christos /* Fetch the command line for a running process. */ 197 1.8 christos 198 1.8 christos static gdb::unique_xmalloc_ptr<char> 199 1.8 christos fbsd_fetch_cmdline (pid_t pid) 200 1.8 christos { 201 1.8 christos size_t len; 202 1.8 christos int mib[4]; 203 1.8 christos 204 1.8 christos len = 0; 205 1.8 christos mib[0] = CTL_KERN; 206 1.8 christos mib[1] = KERN_PROC; 207 1.8 christos mib[2] = KERN_PROC_ARGS; 208 1.8 christos mib[3] = pid; 209 1.8 christos if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1) 210 1.8 christos return nullptr; 211 1.8 christos 212 1.8 christos if (len == 0) 213 1.8 christos return nullptr; 214 1.8 christos 215 1.8 christos gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len)); 216 1.8 christos if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1) 217 1.8 christos return nullptr; 218 1.8 christos 219 1.8 christos /* Join the arguments with spaces to form a single string. */ 220 1.8 christos char *cp = cmdline.get (); 221 1.8 christos for (size_t i = 0; i < len - 1; i++) 222 1.8 christos if (cp[i] == '\0') 223 1.8 christos cp[i] = ' '; 224 1.8 christos cp[len - 1] = '\0'; 225 1.8 christos 226 1.8 christos return cmdline; 227 1.8 christos } 228 1.8 christos 229 1.8 christos /* Fetch the external variant of the kernel's internal process 230 1.8 christos structure for the process PID into KP. */ 231 1.8 christos 232 1.8 christos static bool 233 1.8 christos fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp) 234 1.8 christos { 235 1.8 christos size_t len; 236 1.8 christos int mib[4]; 237 1.8 christos 238 1.8 christos len = sizeof *kp; 239 1.8 christos mib[0] = CTL_KERN; 240 1.8 christos mib[1] = KERN_PROC; 241 1.8 christos mib[2] = KERN_PROC_PID; 242 1.8 christos mib[3] = pid; 243 1.8 christos return (sysctl (mib, 4, kp, &len, NULL, 0) == 0); 244 1.8 christos } 245 1.8 christos 246 1.8 christos /* Implement the "info_proc" target_ops method. */ 247 1.8 christos 248 1.8 christos bool 249 1.8 christos fbsd_nat_target::info_proc (const char *args, enum info_proc_what what) 250 1.8 christos { 251 1.8 christos gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl; 252 1.8 christos int nfd = 0; 253 1.8 christos struct kinfo_proc kp; 254 1.8 christos pid_t pid; 255 1.8 christos bool do_cmdline = false; 256 1.8 christos bool do_cwd = false; 257 1.8 christos bool do_exe = false; 258 1.8 christos bool do_files = false; 259 1.8 christos bool do_mappings = false; 260 1.8 christos bool do_status = false; 261 1.8 christos 262 1.8 christos switch (what) 263 1.8 christos { 264 1.8 christos case IP_MINIMAL: 265 1.8 christos do_cmdline = true; 266 1.8 christos do_cwd = true; 267 1.8 christos do_exe = true; 268 1.8 christos break; 269 1.8 christos case IP_MAPPINGS: 270 1.8 christos do_mappings = true; 271 1.8 christos break; 272 1.8 christos case IP_STATUS: 273 1.8 christos case IP_STAT: 274 1.8 christos do_status = true; 275 1.8 christos break; 276 1.8 christos case IP_CMDLINE: 277 1.8 christos do_cmdline = true; 278 1.8 christos break; 279 1.8 christos case IP_EXE: 280 1.8 christos do_exe = true; 281 1.8 christos break; 282 1.8 christos case IP_CWD: 283 1.8 christos do_cwd = true; 284 1.8 christos break; 285 1.8 christos case IP_FILES: 286 1.8 christos do_files = true; 287 1.8 christos break; 288 1.8 christos case IP_ALL: 289 1.8 christos do_cmdline = true; 290 1.8 christos do_cwd = true; 291 1.8 christos do_exe = true; 292 1.8 christos do_files = true; 293 1.8 christos do_mappings = true; 294 1.8 christos do_status = true; 295 1.8 christos break; 296 1.8 christos default: 297 1.8 christos error (_("Not supported on this target.")); 298 1.8 christos } 299 1.8 christos 300 1.8 christos gdb_argv built_argv (args); 301 1.8 christos if (built_argv.count () == 0) 302 1.8 christos { 303 1.8 christos pid = inferior_ptid.pid (); 304 1.8 christos if (pid == 0) 305 1.8 christos error (_("No current process: you must name one.")); 306 1.8 christos } 307 1.11 christos else if (built_argv.count () == 1 && isdigit ((unsigned char)built_argv[0][0])) 308 1.8 christos pid = strtol (built_argv[0], NULL, 10); 309 1.8 christos else 310 1.8 christos error (_("Invalid arguments.")); 311 1.8 christos 312 1.10 christos gdb_printf (_("process %d\n"), pid); 313 1.8 christos if (do_cwd || do_exe || do_files) 314 1.8 christos fdtbl.reset (kinfo_getfile (pid, &nfd)); 315 1.8 christos 316 1.8 christos if (do_cmdline) 317 1.8 christos { 318 1.8 christos gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid); 319 1.8 christos if (cmdline != nullptr) 320 1.10 christos gdb_printf ("cmdline = '%s'\n", cmdline.get ()); 321 1.8 christos else 322 1.8 christos warning (_("unable to fetch command line")); 323 1.8 christos } 324 1.8 christos if (do_cwd) 325 1.8 christos { 326 1.8 christos const char *cwd = NULL; 327 1.8 christos struct kinfo_file *kf = fdtbl.get (); 328 1.8 christos for (int i = 0; i < nfd; i++, kf++) 329 1.8 christos { 330 1.8 christos if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD) 331 1.8 christos { 332 1.8 christos cwd = kf->kf_path; 333 1.8 christos break; 334 1.8 christos } 335 1.8 christos } 336 1.8 christos if (cwd != NULL) 337 1.10 christos gdb_printf ("cwd = '%s'\n", cwd); 338 1.8 christos else 339 1.8 christos warning (_("unable to fetch current working directory")); 340 1.8 christos } 341 1.8 christos if (do_exe) 342 1.8 christos { 343 1.8 christos const char *exe = NULL; 344 1.8 christos struct kinfo_file *kf = fdtbl.get (); 345 1.8 christos for (int i = 0; i < nfd; i++, kf++) 346 1.8 christos { 347 1.8 christos if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT) 348 1.8 christos { 349 1.8 christos exe = kf->kf_path; 350 1.8 christos break; 351 1.8 christos } 352 1.8 christos } 353 1.8 christos if (exe == NULL) 354 1.8 christos exe = pid_to_exec_file (pid); 355 1.8 christos if (exe != NULL) 356 1.10 christos gdb_printf ("exe = '%s'\n", exe); 357 1.8 christos else 358 1.8 christos warning (_("unable to fetch executable path name")); 359 1.8 christos } 360 1.8 christos if (do_files) 361 1.8 christos { 362 1.8 christos struct kinfo_file *kf = fdtbl.get (); 363 1.8 christos 364 1.8 christos if (nfd > 0) 365 1.8 christos { 366 1.8 christos fbsd_info_proc_files_header (); 367 1.8 christos for (int i = 0; i < nfd; i++, kf++) 368 1.8 christos fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags, 369 1.8 christos kf->kf_offset, kf->kf_vnode_type, 370 1.8 christos kf->kf_sock_domain, kf->kf_sock_type, 371 1.8 christos kf->kf_sock_protocol, &kf->kf_sa_local, 372 1.8 christos &kf->kf_sa_peer, kf->kf_path); 373 1.8 christos } 374 1.8 christos else 375 1.8 christos warning (_("unable to fetch list of open files")); 376 1.8 christos } 377 1.8 christos if (do_mappings) 378 1.8 christos { 379 1.8 christos int nvment; 380 1.8 christos gdb::unique_xmalloc_ptr<struct kinfo_vmentry> 381 1.8 christos vmentl (kinfo_getvmmap (pid, &nvment)); 382 1.8 christos 383 1.8 christos if (vmentl != nullptr) 384 1.8 christos { 385 1.8 christos int addr_bit = TARGET_CHAR_BIT * sizeof (void *); 386 1.8 christos fbsd_info_proc_mappings_header (addr_bit); 387 1.8 christos 388 1.8 christos struct kinfo_vmentry *kve = vmentl.get (); 389 1.8 christos for (int i = 0; i < nvment; i++, kve++) 390 1.8 christos fbsd_info_proc_mappings_entry (addr_bit, kve->kve_start, 391 1.8 christos kve->kve_end, kve->kve_offset, 392 1.8 christos kve->kve_flags, kve->kve_protection, 393 1.8 christos kve->kve_path); 394 1.8 christos } 395 1.8 christos else 396 1.8 christos warning (_("unable to fetch virtual memory map")); 397 1.8 christos } 398 1.8 christos if (do_status) 399 1.8 christos { 400 1.8 christos if (!fbsd_fetch_kinfo_proc (pid, &kp)) 401 1.8 christos warning (_("Failed to fetch process information")); 402 1.8 christos else 403 1.8 christos { 404 1.8 christos const char *state; 405 1.8 christos int pgtok; 406 1.8 christos 407 1.10 christos gdb_printf ("Name: %s\n", kp.ki_comm); 408 1.8 christos switch (kp.ki_stat) 409 1.8 christos { 410 1.8 christos case SIDL: 411 1.8 christos state = "I (idle)"; 412 1.8 christos break; 413 1.8 christos case SRUN: 414 1.8 christos state = "R (running)"; 415 1.8 christos break; 416 1.8 christos case SSTOP: 417 1.8 christos state = "T (stopped)"; 418 1.8 christos break; 419 1.8 christos case SZOMB: 420 1.8 christos state = "Z (zombie)"; 421 1.8 christos break; 422 1.8 christos case SSLEEP: 423 1.8 christos state = "S (sleeping)"; 424 1.8 christos break; 425 1.8 christos case SWAIT: 426 1.8 christos state = "W (interrupt wait)"; 427 1.8 christos break; 428 1.8 christos case SLOCK: 429 1.8 christos state = "L (blocked on lock)"; 430 1.8 christos break; 431 1.8 christos default: 432 1.8 christos state = "? (unknown)"; 433 1.8 christos break; 434 1.8 christos } 435 1.10 christos gdb_printf ("State: %s\n", state); 436 1.10 christos gdb_printf ("Parent process: %d\n", kp.ki_ppid); 437 1.10 christos gdb_printf ("Process group: %d\n", kp.ki_pgid); 438 1.10 christos gdb_printf ("Session id: %d\n", kp.ki_sid); 439 1.10 christos gdb_printf ("TTY: %s\n", pulongest (kp.ki_tdev)); 440 1.10 christos gdb_printf ("TTY owner process group: %d\n", kp.ki_tpgid); 441 1.10 christos gdb_printf ("User IDs (real, effective, saved): %d %d %d\n", 442 1.10 christos kp.ki_ruid, kp.ki_uid, kp.ki_svuid); 443 1.10 christos gdb_printf ("Group IDs (real, effective, saved): %d %d %d\n", 444 1.10 christos kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid); 445 1.10 christos gdb_printf ("Groups: "); 446 1.8 christos for (int i = 0; i < kp.ki_ngroups; i++) 447 1.10 christos gdb_printf ("%d ", kp.ki_groups[i]); 448 1.10 christos gdb_printf ("\n"); 449 1.10 christos gdb_printf ("Minor faults (no memory page): %ld\n", 450 1.10 christos kp.ki_rusage.ru_minflt); 451 1.10 christos gdb_printf ("Minor faults, children: %ld\n", 452 1.10 christos kp.ki_rusage_ch.ru_minflt); 453 1.10 christos gdb_printf ("Major faults (memory page faults): %ld\n", 454 1.10 christos kp.ki_rusage.ru_majflt); 455 1.10 christos gdb_printf ("Major faults, children: %ld\n", 456 1.10 christos kp.ki_rusage_ch.ru_majflt); 457 1.10 christos gdb_printf ("utime: %s.%06ld\n", 458 1.10 christos plongest (kp.ki_rusage.ru_utime.tv_sec), 459 1.10 christos kp.ki_rusage.ru_utime.tv_usec); 460 1.10 christos gdb_printf ("stime: %s.%06ld\n", 461 1.10 christos plongest (kp.ki_rusage.ru_stime.tv_sec), 462 1.10 christos kp.ki_rusage.ru_stime.tv_usec); 463 1.10 christos gdb_printf ("utime, children: %s.%06ld\n", 464 1.10 christos plongest (kp.ki_rusage_ch.ru_utime.tv_sec), 465 1.10 christos kp.ki_rusage_ch.ru_utime.tv_usec); 466 1.10 christos gdb_printf ("stime, children: %s.%06ld\n", 467 1.10 christos plongest (kp.ki_rusage_ch.ru_stime.tv_sec), 468 1.10 christos kp.ki_rusage_ch.ru_stime.tv_usec); 469 1.10 christos gdb_printf ("'nice' value: %d\n", kp.ki_nice); 470 1.10 christos gdb_printf ("Start time: %s.%06ld\n", 471 1.10 christos plongest (kp.ki_start.tv_sec), 472 1.10 christos kp.ki_start.tv_usec); 473 1.8 christos pgtok = getpagesize () / 1024; 474 1.10 christos gdb_printf ("Virtual memory size: %s kB\n", 475 1.10 christos pulongest (kp.ki_size / 1024)); 476 1.10 christos gdb_printf ("Data size: %s kB\n", 477 1.10 christos pulongest (kp.ki_dsize * pgtok)); 478 1.10 christos gdb_printf ("Stack size: %s kB\n", 479 1.10 christos pulongest (kp.ki_ssize * pgtok)); 480 1.10 christos gdb_printf ("Text size: %s kB\n", 481 1.10 christos pulongest (kp.ki_tsize * pgtok)); 482 1.10 christos gdb_printf ("Resident set size: %s kB\n", 483 1.10 christos pulongest (kp.ki_rssize * pgtok)); 484 1.10 christos gdb_printf ("Maximum RSS: %s kB\n", 485 1.10 christos pulongest (kp.ki_rusage.ru_maxrss)); 486 1.10 christos gdb_printf ("Pending Signals: "); 487 1.8 christos for (int i = 0; i < _SIG_WORDS; i++) 488 1.10 christos gdb_printf ("%08x ", kp.ki_siglist.__bits[i]); 489 1.10 christos gdb_printf ("\n"); 490 1.10 christos gdb_printf ("Ignored Signals: "); 491 1.8 christos for (int i = 0; i < _SIG_WORDS; i++) 492 1.10 christos gdb_printf ("%08x ", kp.ki_sigignore.__bits[i]); 493 1.10 christos gdb_printf ("\n"); 494 1.10 christos gdb_printf ("Caught Signals: "); 495 1.8 christos for (int i = 0; i < _SIG_WORDS; i++) 496 1.10 christos gdb_printf ("%08x ", kp.ki_sigcatch.__bits[i]); 497 1.10 christos gdb_printf ("\n"); 498 1.8 christos } 499 1.8 christos } 500 1.8 christos 501 1.8 christos return true; 502 1.8 christos } 503 1.8 christos 504 1.8 christos /* Return the size of siginfo for the current inferior. */ 505 1.8 christos 506 1.8 christos #ifdef __LP64__ 507 1.8 christos union sigval32 { 508 1.8 christos int sival_int; 509 1.8 christos uint32_t sival_ptr; 510 1.8 christos }; 511 1.8 christos 512 1.8 christos /* This structure matches the naming and layout of `siginfo_t' in 513 1.8 christos <sys/signal.h>. In particular, the `si_foo' macros defined in that 514 1.8 christos header can be used with both types to copy fields in the `_reason' 515 1.8 christos union. */ 516 1.8 christos 517 1.8 christos struct siginfo32 518 1.8 christos { 519 1.8 christos int si_signo; 520 1.8 christos int si_errno; 521 1.8 christos int si_code; 522 1.8 christos __pid_t si_pid; 523 1.8 christos __uid_t si_uid; 524 1.8 christos int si_status; 525 1.8 christos uint32_t si_addr; 526 1.8 christos union sigval32 si_value; 527 1.8 christos union 528 1.8 christos { 529 1.8 christos struct 530 1.8 christos { 531 1.8 christos int _trapno; 532 1.8 christos } _fault; 533 1.8 christos struct 534 1.8 christos { 535 1.8 christos int _timerid; 536 1.8 christos int _overrun; 537 1.8 christos } _timer; 538 1.8 christos struct 539 1.8 christos { 540 1.8 christos int _mqd; 541 1.8 christos } _mesgq; 542 1.8 christos struct 543 1.8 christos { 544 1.8 christos int32_t _band; 545 1.8 christos } _poll; 546 1.8 christos struct 547 1.8 christos { 548 1.8 christos int32_t __spare1__; 549 1.8 christos int __spare2__[7]; 550 1.8 christos } __spare__; 551 1.8 christos } _reason; 552 1.8 christos }; 553 1.8 christos #endif 554 1.8 christos 555 1.8 christos static size_t 556 1.8 christos fbsd_siginfo_size () 557 1.8 christos { 558 1.8 christos #ifdef __LP64__ 559 1.8 christos struct gdbarch *gdbarch = get_frame_arch (get_current_frame ()); 560 1.8 christos 561 1.8 christos /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */ 562 1.8 christos if (gdbarch_long_bit (gdbarch) == 32) 563 1.8 christos return sizeof (struct siginfo32); 564 1.8 christos #endif 565 1.8 christos return sizeof (siginfo_t); 566 1.8 christos } 567 1.8 christos 568 1.8 christos /* Convert a native 64-bit siginfo object to a 32-bit object. Note 569 1.8 christos that FreeBSD doesn't support writing to $_siginfo, so this only 570 1.8 christos needs to convert one way. */ 571 1.8 christos 572 1.8 christos static void 573 1.8 christos fbsd_convert_siginfo (siginfo_t *si) 574 1.8 christos { 575 1.8 christos #ifdef __LP64__ 576 1.8 christos struct gdbarch *gdbarch = get_frame_arch (get_current_frame ()); 577 1.8 christos 578 1.8 christos /* Is the inferior 32-bit? If not, nothing to do. */ 579 1.8 christos if (gdbarch_long_bit (gdbarch) != 32) 580 1.8 christos return; 581 1.8 christos 582 1.8 christos struct siginfo32 si32; 583 1.8 christos 584 1.8 christos si32.si_signo = si->si_signo; 585 1.8 christos si32.si_errno = si->si_errno; 586 1.8 christos si32.si_code = si->si_code; 587 1.8 christos si32.si_pid = si->si_pid; 588 1.8 christos si32.si_uid = si->si_uid; 589 1.8 christos si32.si_status = si->si_status; 590 1.8 christos si32.si_addr = (uintptr_t) si->si_addr; 591 1.8 christos 592 1.8 christos /* If sival_ptr is being used instead of sival_int on a big-endian 593 1.8 christos platform, then sival_int will be zero since it holds the upper 594 1.8 christos 32-bits of the pointer value. */ 595 1.8 christos #if _BYTE_ORDER == _BIG_ENDIAN 596 1.8 christos if (si->si_value.sival_int == 0) 597 1.8 christos si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr; 598 1.8 christos else 599 1.8 christos si32.si_value.sival_int = si->si_value.sival_int; 600 1.8 christos #else 601 1.8 christos si32.si_value.sival_int = si->si_value.sival_int; 602 1.8 christos #endif 603 1.8 christos 604 1.8 christos /* Always copy the spare fields and then possibly overwrite them for 605 1.8 christos signal-specific or code-specific fields. */ 606 1.8 christos si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__; 607 1.8 christos for (int i = 0; i < 7; i++) 608 1.8 christos si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i]; 609 1.8 christos switch (si->si_signo) { 610 1.8 christos case SIGILL: 611 1.8 christos case SIGFPE: 612 1.8 christos case SIGSEGV: 613 1.8 christos case SIGBUS: 614 1.8 christos si32.si_trapno = si->si_trapno; 615 1.8 christos break; 616 1.8 christos } 617 1.8 christos switch (si->si_code) { 618 1.8 christos case SI_TIMER: 619 1.8 christos si32.si_timerid = si->si_timerid; 620 1.8 christos si32.si_overrun = si->si_overrun; 621 1.8 christos break; 622 1.8 christos case SI_MESGQ: 623 1.8 christos si32.si_mqd = si->si_mqd; 624 1.8 christos break; 625 1.8 christos } 626 1.8 christos 627 1.8 christos memcpy(si, &si32, sizeof (si32)); 628 1.8 christos #endif 629 1.8 christos } 630 1.8 christos 631 1.8 christos /* Implement the "xfer_partial" target_ops method. */ 632 1.8 christos 633 1.8 christos enum target_xfer_status 634 1.8 christos fbsd_nat_target::xfer_partial (enum target_object object, 635 1.8 christos const char *annex, gdb_byte *readbuf, 636 1.8 christos const gdb_byte *writebuf, 637 1.8 christos ULONGEST offset, ULONGEST len, 638 1.8 christos ULONGEST *xfered_len) 639 1.6 christos { 640 1.8 christos pid_t pid = inferior_ptid.pid (); 641 1.6 christos 642 1.6 christos switch (object) 643 1.6 christos { 644 1.8 christos case TARGET_OBJECT_SIGNAL_INFO: 645 1.8 christos { 646 1.8 christos struct ptrace_lwpinfo pl; 647 1.8 christos size_t siginfo_size; 648 1.8 christos 649 1.8 christos /* FreeBSD doesn't support writing to $_siginfo. */ 650 1.8 christos if (writebuf != NULL) 651 1.8 christos return TARGET_XFER_E_IO; 652 1.8 christos 653 1.8 christos if (inferior_ptid.lwp_p ()) 654 1.8 christos pid = inferior_ptid.lwp (); 655 1.8 christos 656 1.8 christos siginfo_size = fbsd_siginfo_size (); 657 1.8 christos if (offset > siginfo_size) 658 1.8 christos return TARGET_XFER_E_IO; 659 1.8 christos 660 1.8 christos if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1) 661 1.8 christos return TARGET_XFER_E_IO; 662 1.8 christos 663 1.8 christos if (!(pl.pl_flags & PL_FLAG_SI)) 664 1.8 christos return TARGET_XFER_E_IO; 665 1.8 christos 666 1.8 christos fbsd_convert_siginfo (&pl.pl_siginfo); 667 1.8 christos if (offset + len > siginfo_size) 668 1.8 christos len = siginfo_size - offset; 669 1.8 christos 670 1.8 christos memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len); 671 1.8 christos *xfered_len = len; 672 1.8 christos return TARGET_XFER_OK; 673 1.8 christos } 674 1.8 christos #ifdef KERN_PROC_AUXV 675 1.6 christos case TARGET_OBJECT_AUXV: 676 1.6 christos { 677 1.8 christos gdb::byte_vector buf_storage; 678 1.8 christos gdb_byte *buf; 679 1.6 christos size_t buflen; 680 1.6 christos int mib[4]; 681 1.6 christos 682 1.6 christos if (writebuf != NULL) 683 1.6 christos return TARGET_XFER_E_IO; 684 1.6 christos mib[0] = CTL_KERN; 685 1.6 christos mib[1] = KERN_PROC; 686 1.6 christos mib[2] = KERN_PROC_AUXV; 687 1.6 christos mib[3] = pid; 688 1.6 christos if (offset == 0) 689 1.6 christos { 690 1.6 christos buf = readbuf; 691 1.6 christos buflen = len; 692 1.6 christos } 693 1.6 christos else 694 1.6 christos { 695 1.6 christos buflen = offset + len; 696 1.8 christos buf_storage.resize (buflen); 697 1.8 christos buf = buf_storage.data (); 698 1.6 christos } 699 1.6 christos if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) 700 1.6 christos { 701 1.6 christos if (offset != 0) 702 1.6 christos { 703 1.6 christos if (buflen > offset) 704 1.6 christos { 705 1.6 christos buflen -= offset; 706 1.6 christos memcpy (readbuf, buf + offset, buflen); 707 1.6 christos } 708 1.6 christos else 709 1.6 christos buflen = 0; 710 1.6 christos } 711 1.6 christos *xfered_len = buflen; 712 1.6 christos return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; 713 1.6 christos } 714 1.6 christos return TARGET_XFER_E_IO; 715 1.6 christos } 716 1.8 christos #endif 717 1.8 christos #if defined(KERN_PROC_VMMAP) && defined(KERN_PROC_PS_STRINGS) 718 1.8 christos case TARGET_OBJECT_FREEBSD_VMMAP: 719 1.8 christos case TARGET_OBJECT_FREEBSD_PS_STRINGS: 720 1.8 christos { 721 1.8 christos gdb::byte_vector buf_storage; 722 1.8 christos gdb_byte *buf; 723 1.8 christos size_t buflen; 724 1.8 christos int mib[4]; 725 1.8 christos 726 1.8 christos int proc_target; 727 1.8 christos uint32_t struct_size; 728 1.8 christos switch (object) 729 1.8 christos { 730 1.8 christos case TARGET_OBJECT_FREEBSD_VMMAP: 731 1.8 christos proc_target = KERN_PROC_VMMAP; 732 1.8 christos struct_size = sizeof (struct kinfo_vmentry); 733 1.8 christos break; 734 1.8 christos case TARGET_OBJECT_FREEBSD_PS_STRINGS: 735 1.8 christos proc_target = KERN_PROC_PS_STRINGS; 736 1.8 christos struct_size = sizeof (void *); 737 1.8 christos break; 738 1.8 christos } 739 1.8 christos 740 1.8 christos if (writebuf != NULL) 741 1.8 christos return TARGET_XFER_E_IO; 742 1.8 christos 743 1.8 christos mib[0] = CTL_KERN; 744 1.8 christos mib[1] = KERN_PROC; 745 1.8 christos mib[2] = proc_target; 746 1.8 christos mib[3] = pid; 747 1.8 christos 748 1.8 christos if (sysctl (mib, 4, NULL, &buflen, NULL, 0) != 0) 749 1.8 christos return TARGET_XFER_E_IO; 750 1.8 christos buflen += sizeof (struct_size); 751 1.8 christos 752 1.8 christos if (offset >= buflen) 753 1.8 christos { 754 1.8 christos *xfered_len = 0; 755 1.8 christos return TARGET_XFER_EOF; 756 1.8 christos } 757 1.8 christos 758 1.8 christos buf_storage.resize (buflen); 759 1.8 christos buf = buf_storage.data (); 760 1.8 christos 761 1.8 christos memcpy (buf, &struct_size, sizeof (struct_size)); 762 1.8 christos buflen -= sizeof (struct_size); 763 1.8 christos if (sysctl (mib, 4, buf + sizeof (struct_size), &buflen, NULL, 0) != 0) 764 1.8 christos return TARGET_XFER_E_IO; 765 1.8 christos buflen += sizeof (struct_size); 766 1.8 christos 767 1.8 christos if (buflen - offset < len) 768 1.8 christos len = buflen - offset; 769 1.8 christos memcpy (readbuf, buf + offset, len); 770 1.8 christos *xfered_len = len; 771 1.8 christos return TARGET_XFER_OK; 772 1.8 christos } 773 1.8 christos #endif 774 1.6 christos default: 775 1.8 christos return inf_ptrace_target::xfer_partial (object, annex, 776 1.8 christos readbuf, writebuf, offset, 777 1.8 christos len, xfered_len); 778 1.6 christos } 779 1.6 christos } 780 1.6 christos 781 1.9 christos static bool debug_fbsd_lwp; 782 1.9 christos static bool debug_fbsd_nat; 783 1.5 christos 784 1.6 christos static void 785 1.6 christos show_fbsd_lwp_debug (struct ui_file *file, int from_tty, 786 1.6 christos struct cmd_list_element *c, const char *value) 787 1.6 christos { 788 1.10 christos gdb_printf (file, _("Debugging of FreeBSD lwp module is %s.\n"), value); 789 1.6 christos } 790 1.6 christos 791 1.6 christos static void 792 1.8 christos show_fbsd_nat_debug (struct ui_file *file, int from_tty, 793 1.8 christos struct cmd_list_element *c, const char *value) 794 1.6 christos { 795 1.10 christos gdb_printf (file, _("Debugging of FreeBSD native target is %s.\n"), 796 1.10 christos value); 797 1.6 christos } 798 1.6 christos 799 1.10 christos #define fbsd_lwp_debug_printf(fmt, ...) \ 800 1.10 christos debug_prefixed_printf_cond (debug_fbsd_lwp, "fbsd-lwp", fmt, ##__VA_ARGS__) 801 1.10 christos 802 1.10 christos #define fbsd_nat_debug_printf(fmt, ...) \ 803 1.10 christos debug_prefixed_printf_cond (debug_fbsd_nat, "fbsd-nat", fmt, ##__VA_ARGS__) 804 1.10 christos 805 1.11 christos #define fbsd_nat_debug_start_end(fmt, ...) \ 806 1.11 christos scoped_debug_start_end (debug_fbsd_nat, "fbsd-nat", fmt, ##__VA_ARGS__) 807 1.10 christos 808 1.6 christos /* 809 1.6 christos FreeBSD's first thread support was via a "reentrant" version of libc 810 1.6 christos (libc_r) that first shipped in 2.2.7. This library multiplexed all 811 1.6 christos of the threads in a process onto a single kernel thread. This 812 1.7 christos library was supported via the bsd-uthread target. 813 1.6 christos 814 1.6 christos FreeBSD 5.1 introduced two new threading libraries that made use of 815 1.6 christos multiple kernel threads. The first (libkse) scheduled M user 816 1.6 christos threads onto N (<= M) kernel threads (LWPs). The second (libthr) 817 1.6 christos bound each user thread to a dedicated kernel thread. libkse shipped 818 1.6 christos as the default threading library (libpthread). 819 1.6 christos 820 1.6 christos FreeBSD 5.3 added a libthread_db to abstract the interface across 821 1.6 christos the various thread libraries (libc_r, libkse, and libthr). 822 1.6 christos 823 1.6 christos FreeBSD 7.0 switched the default threading library from from libkse 824 1.6 christos to libpthread and removed libc_r. 825 1.6 christos 826 1.6 christos FreeBSD 8.0 removed libkse and the in-kernel support for it. The 827 1.6 christos only threading library supported by 8.0 and later is libthr which 828 1.6 christos ties each user thread directly to an LWP. To simplify the 829 1.6 christos implementation, this target only supports LWP-backed threads using 830 1.6 christos ptrace directly rather than libthread_db. 831 1.6 christos 832 1.6 christos FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS. 833 1.6 christos */ 834 1.6 christos 835 1.6 christos /* Return true if PTID is still active in the inferior. */ 836 1.6 christos 837 1.8 christos bool 838 1.8 christos fbsd_nat_target::thread_alive (ptid_t ptid) 839 1.6 christos { 840 1.8 christos if (ptid.lwp_p ()) 841 1.6 christos { 842 1.6 christos struct ptrace_lwpinfo pl; 843 1.6 christos 844 1.8 christos if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl) 845 1.6 christos == -1) 846 1.11 christos { 847 1.11 christos /* EBUSY means the associated process is running which means 848 1.11 christos the LWP does exist and belongs to a running process. */ 849 1.11 christos if (errno == EBUSY) 850 1.11 christos return true; 851 1.11 christos return false; 852 1.11 christos } 853 1.6 christos #ifdef PL_FLAG_EXITED 854 1.6 christos if (pl.pl_flags & PL_FLAG_EXITED) 855 1.8 christos return false; 856 1.6 christos #endif 857 1.6 christos } 858 1.6 christos 859 1.8 christos return true; 860 1.6 christos } 861 1.6 christos 862 1.9 christos /* Convert PTID to a string. */ 863 1.6 christos 864 1.9 christos std::string 865 1.8 christos fbsd_nat_target::pid_to_str (ptid_t ptid) 866 1.6 christos { 867 1.6 christos lwpid_t lwp; 868 1.6 christos 869 1.8 christos lwp = ptid.lwp (); 870 1.6 christos if (lwp != 0) 871 1.6 christos { 872 1.8 christos int pid = ptid.pid (); 873 1.6 christos 874 1.9 christos return string_printf ("LWP %d of process %d", lwp, pid); 875 1.6 christos } 876 1.6 christos 877 1.6 christos return normal_pid_to_str (ptid); 878 1.6 christos } 879 1.6 christos 880 1.6 christos #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME 881 1.6 christos /* Return the name assigned to a thread by an application. Returns 882 1.6 christos the string in a static buffer. */ 883 1.6 christos 884 1.8 christos const char * 885 1.8 christos fbsd_nat_target::thread_name (struct thread_info *thr) 886 1.6 christos { 887 1.6 christos struct ptrace_lwpinfo pl; 888 1.6 christos struct kinfo_proc kp; 889 1.8 christos int pid = thr->ptid.pid (); 890 1.8 christos long lwp = thr->ptid.lwp (); 891 1.6 christos static char buf[sizeof pl.pl_tdname + 1]; 892 1.6 christos 893 1.6 christos /* Note that ptrace_lwpinfo returns the process command in pl_tdname 894 1.6 christos if a name has not been set explicitly. Return a NULL name in 895 1.6 christos that case. */ 896 1.8 christos if (!fbsd_fetch_kinfo_proc (pid, &kp)) 897 1.10 christos return nullptr; 898 1.6 christos if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1) 899 1.10 christos return nullptr; 900 1.6 christos if (strcmp (kp.ki_comm, pl.pl_tdname) == 0) 901 1.6 christos return NULL; 902 1.6 christos xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname); 903 1.6 christos return buf; 904 1.6 christos } 905 1.6 christos #endif 906 1.6 christos 907 1.6 christos /* Enable additional event reporting on new processes. 908 1.6 christos 909 1.6 christos To catch fork events, PTRACE_FORK is set on every traced process 910 1.6 christos to enable stops on returns from fork or vfork. Note that both the 911 1.6 christos parent and child will always stop, even if system call stops are 912 1.6 christos not enabled. 913 1.6 christos 914 1.6 christos To catch LWP events, PTRACE_EVENTS is set on every traced process. 915 1.6 christos This enables stops on the birth for new LWPs (excluding the "main" LWP) 916 1.6 christos and the death of LWPs (excluding the last LWP in a process). Note 917 1.6 christos that unlike fork events, the LWP that creates a new LWP does not 918 1.6 christos report an event. */ 919 1.6 christos 920 1.6 christos static void 921 1.6 christos fbsd_enable_proc_events (pid_t pid) 922 1.6 christos { 923 1.6 christos #ifdef PT_GET_EVENT_MASK 924 1.6 christos int events; 925 1.6 christos 926 1.11 christos if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3) &events, 927 1.6 christos sizeof (events)) == -1) 928 1.10 christos perror_with_name (("ptrace (PT_GET_EVENT_MASK)")); 929 1.6 christos events |= PTRACE_FORK | PTRACE_LWP; 930 1.6 christos #ifdef PTRACE_VFORK 931 1.6 christos events |= PTRACE_VFORK; 932 1.6 christos #endif 933 1.11 christos if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3) &events, 934 1.6 christos sizeof (events)) == -1) 935 1.10 christos perror_with_name (("ptrace (PT_SET_EVENT_MASK)")); 936 1.6 christos #else 937 1.6 christos #ifdef TDP_RFPPWAIT 938 1.11 christos if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3) 0, 1) == -1) 939 1.10 christos perror_with_name (("ptrace (PT_FOLLOW_FORK)")); 940 1.6 christos #endif 941 1.6 christos #ifdef PT_LWP_EVENTS 942 1.11 christos if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3) 0, 1) == -1) 943 1.10 christos perror_with_name (("ptrace (PT_LWP_EVENTS)")); 944 1.6 christos #endif 945 1.6 christos #endif 946 1.6 christos } 947 1.6 christos 948 1.6 christos /* Add threads for any new LWPs in a process. 949 1.6 christos 950 1.6 christos When LWP events are used, this function is only used to detect existing 951 1.6 christos threads when attaching to a process. On older systems, this function is 952 1.6 christos called to discover new threads each time the thread list is updated. */ 953 1.6 christos 954 1.6 christos static void 955 1.9 christos fbsd_add_threads (fbsd_nat_target *target, pid_t pid) 956 1.6 christos { 957 1.6 christos int i, nlwps; 958 1.6 christos 959 1.9 christos gdb_assert (!in_thread_list (target, ptid_t (pid))); 960 1.6 christos nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0); 961 1.6 christos if (nlwps == -1) 962 1.10 christos perror_with_name (("ptrace (PT_GETNUMLWPS)")); 963 1.6 christos 964 1.8 christos gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps)); 965 1.6 christos 966 1.8 christos nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps); 967 1.6 christos if (nlwps == -1) 968 1.10 christos perror_with_name (("ptrace (PT_GETLWPLIST)")); 969 1.6 christos 970 1.11 christos inferior *inf = find_inferior_ptid (target, ptid_t (pid)); 971 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 972 1.11 christos gdb_assert (fbsd_inf != nullptr); 973 1.6 christos for (i = 0; i < nlwps; i++) 974 1.6 christos { 975 1.10 christos ptid_t ptid = ptid_t (pid, lwps[i]); 976 1.6 christos 977 1.9 christos if (!in_thread_list (target, ptid)) 978 1.6 christos { 979 1.6 christos #ifdef PT_LWP_EVENTS 980 1.6 christos struct ptrace_lwpinfo pl; 981 1.6 christos 982 1.6 christos /* Don't add exited threads. Note that this is only called 983 1.6 christos when attaching to a multi-threaded process. */ 984 1.6 christos if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1) 985 1.10 christos perror_with_name (("ptrace (PT_LWPINFO)")); 986 1.6 christos if (pl.pl_flags & PL_FLAG_EXITED) 987 1.6 christos continue; 988 1.6 christos #endif 989 1.10 christos fbsd_lwp_debug_printf ("adding thread for LWP %u", lwps[i]); 990 1.9 christos add_thread (target, ptid); 991 1.11 christos #ifdef PT_LWP_EVENTS 992 1.11 christos fbsd_inf->num_lwps++; 993 1.11 christos #endif 994 1.6 christos } 995 1.6 christos } 996 1.11 christos #ifndef PT_LWP_EVENTS 997 1.11 christos fbsd_inf->num_lwps = nlwps; 998 1.11 christos #endif 999 1.6 christos } 1000 1.6 christos 1001 1.8 christos /* Implement the "update_thread_list" target_ops method. */ 1002 1.6 christos 1003 1.8 christos void 1004 1.8 christos fbsd_nat_target::update_thread_list () 1005 1.6 christos { 1006 1.6 christos #ifdef PT_LWP_EVENTS 1007 1.6 christos /* With support for thread events, threads are added/deleted from the 1008 1.6 christos list as events are reported, so just try deleting exited threads. */ 1009 1.6 christos delete_exited_threads (); 1010 1.6 christos #else 1011 1.6 christos prune_threads (); 1012 1.6 christos 1013 1.9 christos fbsd_add_threads (this, inferior_ptid.pid ()); 1014 1.6 christos #endif 1015 1.6 christos } 1016 1.6 christos 1017 1.10 christos /* Async mode support. */ 1018 1.10 christos 1019 1.10 christos /* Implement the "can_async_p" target method. */ 1020 1.10 christos 1021 1.10 christos bool 1022 1.10 christos fbsd_nat_target::can_async_p () 1023 1.10 christos { 1024 1.10 christos /* This flag should be checked in the common target.c code. */ 1025 1.10 christos gdb_assert (target_async_permitted); 1026 1.10 christos 1027 1.10 christos /* Otherwise, this targets is always able to support async mode. */ 1028 1.10 christos return true; 1029 1.10 christos } 1030 1.10 christos 1031 1.10 christos /* SIGCHLD handler notifies the event-loop in async mode. */ 1032 1.10 christos 1033 1.10 christos static void 1034 1.10 christos sigchld_handler (int signo) 1035 1.10 christos { 1036 1.10 christos int old_errno = errno; 1037 1.10 christos 1038 1.10 christos fbsd_nat_target::async_file_mark_if_open (); 1039 1.10 christos 1040 1.10 christos errno = old_errno; 1041 1.10 christos } 1042 1.10 christos 1043 1.10 christos /* Callback registered with the target events file descriptor. */ 1044 1.10 christos 1045 1.10 christos static void 1046 1.10 christos handle_target_event (int error, gdb_client_data client_data) 1047 1.10 christos { 1048 1.10 christos inferior_event_handler (INF_REG_EVENT); 1049 1.10 christos } 1050 1.10 christos 1051 1.10 christos /* Implement the "async" target method. */ 1052 1.10 christos 1053 1.10 christos void 1054 1.10 christos fbsd_nat_target::async (bool enable) 1055 1.10 christos { 1056 1.10 christos if (enable == is_async_p ()) 1057 1.10 christos return; 1058 1.10 christos 1059 1.10 christos /* Block SIGCHILD while we create/destroy the pipe, as the handler 1060 1.10 christos writes to it. */ 1061 1.10 christos gdb::block_signals blocker; 1062 1.10 christos 1063 1.10 christos if (enable) 1064 1.10 christos { 1065 1.10 christos if (!async_file_open ()) 1066 1.10 christos internal_error ("failed to create event pipe."); 1067 1.10 christos 1068 1.10 christos add_file_handler (async_wait_fd (), handle_target_event, NULL, "fbsd-nat"); 1069 1.10 christos 1070 1.10 christos /* Trigger a poll in case there are pending events to 1071 1.10 christos handle. */ 1072 1.10 christos async_file_mark (); 1073 1.10 christos } 1074 1.10 christos else 1075 1.10 christos { 1076 1.10 christos delete_file_handler (async_wait_fd ()); 1077 1.10 christos async_file_close (); 1078 1.10 christos } 1079 1.10 christos } 1080 1.10 christos 1081 1.5 christos #ifdef TDP_RFPPWAIT 1082 1.5 christos /* 1083 1.5 christos To catch fork events, PT_FOLLOW_FORK is set on every traced process 1084 1.5 christos to enable stops on returns from fork or vfork. Note that both the 1085 1.5 christos parent and child will always stop, even if system call stops are not 1086 1.5 christos enabled. 1087 1.5 christos 1088 1.5 christos After a fork, both the child and parent process will stop and report 1089 1.5 christos an event. However, there is no guarantee of order. If the parent 1090 1.5 christos reports its stop first, then fbsd_wait explicitly waits for the new 1091 1.5 christos child before returning. If the child reports its stop first, then 1092 1.5 christos the event is saved on a list and ignored until the parent's stop is 1093 1.5 christos reported. fbsd_wait could have been changed to fetch the parent PID 1094 1.5 christos of the new child and used that to wait for the parent explicitly. 1095 1.5 christos However, if two threads in the parent fork at the same time, then 1096 1.5 christos the wait on the parent might return the "wrong" fork event. 1097 1.5 christos 1098 1.5 christos The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for 1099 1.5 christos the new child process. This flag could be inferred by treating any 1100 1.5 christos events for an unknown pid as a new child. 1101 1.5 christos 1102 1.5 christos In addition, the initial version of PT_FOLLOW_FORK did not report a 1103 1.5 christos stop event for the parent process of a vfork until after the child 1104 1.5 christos process executed a new program or exited. The kernel was changed to 1105 1.5 christos defer the wait for exit or exec of the child until after posting the 1106 1.5 christos stop event shortly after the change to introduce PL_FLAG_CHILD. 1107 1.5 christos This could be worked around by reporting a vfork event when the 1108 1.5 christos child event posted and ignoring the subsequent event from the 1109 1.5 christos parent. 1110 1.5 christos 1111 1.5 christos This implementation requires both of these fixes for simplicity's 1112 1.5 christos sake. FreeBSD versions newer than 9.1 contain both fixes. 1113 1.5 christos */ 1114 1.5 christos 1115 1.8 christos static std::list<ptid_t> fbsd_pending_children; 1116 1.5 christos 1117 1.5 christos /* Record a new child process event that is reported before the 1118 1.5 christos corresponding fork event in the parent. */ 1119 1.5 christos 1120 1.5 christos static void 1121 1.6 christos fbsd_remember_child (ptid_t pid) 1122 1.5 christos { 1123 1.8 christos fbsd_pending_children.push_front (pid); 1124 1.5 christos } 1125 1.5 christos 1126 1.5 christos /* Check for a previously-recorded new child process event for PID. 1127 1.6 christos If one is found, remove it from the list and return the PTID. */ 1128 1.5 christos 1129 1.6 christos static ptid_t 1130 1.5 christos fbsd_is_child_pending (pid_t pid) 1131 1.5 christos { 1132 1.8 christos for (auto it = fbsd_pending_children.begin (); 1133 1.8 christos it != fbsd_pending_children.end (); it++) 1134 1.8 christos if (it->pid () == pid) 1135 1.8 christos { 1136 1.8 christos ptid_t ptid = *it; 1137 1.8 christos fbsd_pending_children.erase (it); 1138 1.8 christos return ptid; 1139 1.8 christos } 1140 1.6 christos return null_ptid; 1141 1.6 christos } 1142 1.6 christos 1143 1.11 christos /* Wait for a child of a fork to report its stop. Returns the PTID of 1144 1.11 christos the new child process. */ 1145 1.11 christos 1146 1.11 christos static ptid_t 1147 1.11 christos fbsd_wait_for_fork_child (pid_t pid) 1148 1.11 christos { 1149 1.11 christos ptid_t ptid = fbsd_is_child_pending (pid); 1150 1.11 christos if (ptid != null_ptid) 1151 1.11 christos return ptid; 1152 1.11 christos 1153 1.11 christos int status; 1154 1.12 christos pid_t wpid = gdb::waitpid (pid, &status, 0); 1155 1.11 christos if (wpid == -1) 1156 1.11 christos perror_with_name (("waitpid")); 1157 1.11 christos 1158 1.11 christos gdb_assert (wpid == pid); 1159 1.11 christos 1160 1.11 christos struct ptrace_lwpinfo pl; 1161 1.11 christos if (ptrace (PT_LWPINFO, wpid, (caddr_t) &pl, sizeof pl) == -1) 1162 1.11 christos perror_with_name (("ptrace (PT_LWPINFO)")); 1163 1.11 christos 1164 1.11 christos gdb_assert (pl.pl_flags & PL_FLAG_CHILD); 1165 1.11 christos return ptid_t (wpid, pl.pl_lwpid); 1166 1.11 christos } 1167 1.11 christos 1168 1.6 christos #ifndef PTRACE_VFORK 1169 1.6 christos /* Record a pending vfork done event. */ 1170 1.6 christos 1171 1.6 christos static void 1172 1.6 christos fbsd_add_vfork_done (ptid_t pid) 1173 1.6 christos { 1174 1.11 christos add_pending_event (ptid, target_waitstatus ().set_vfork_done ()); 1175 1.10 christos 1176 1.10 christos /* If we're in async mode, need to tell the event loop there's 1177 1.10 christos something here to process. */ 1178 1.10 christos if (target_is_async_p ()) 1179 1.10 christos async_file_mark (); 1180 1.6 christos } 1181 1.6 christos #endif 1182 1.6 christos #endif 1183 1.6 christos 1184 1.11 christos /* Resume a single process. */ 1185 1.5 christos 1186 1.8 christos void 1187 1.11 christos fbsd_nat_target::resume_one_process (ptid_t ptid, int step, 1188 1.11 christos enum gdb_signal signo) 1189 1.5 christos { 1190 1.10 christos fbsd_nat_debug_printf ("[%s], step %d, signo %d (%s)", 1191 1.10 christos target_pid_to_str (ptid).c_str (), step, signo, 1192 1.10 christos gdb_signal_to_name (signo)); 1193 1.7 christos 1194 1.11 christos inferior *inf = find_inferior_ptid (this, ptid); 1195 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 1196 1.11 christos fbsd_inf->resumed_lwps = ptid; 1197 1.11 christos gdb_assert (fbsd_inf->running_lwps == 0); 1198 1.7 christos 1199 1.11 christos /* Don't PT_CONTINUE a thread or process which has a pending event. */ 1200 1.11 christos if (have_pending_event (ptid)) 1201 1.11 christos { 1202 1.11 christos fbsd_nat_debug_printf ("found pending event"); 1203 1.11 christos return; 1204 1.6 christos } 1205 1.11 christos 1206 1.11 christos for (thread_info *tp : inf->non_exited_threads ()) 1207 1.6 christos { 1208 1.11 christos /* If ptid is a specific LWP, suspend all other LWPs in the 1209 1.11 christos process, otherwise resume all LWPs in the process.. */ 1210 1.11 christos if (!ptid.lwp_p() || tp->ptid.lwp () == ptid.lwp ()) 1211 1.10 christos { 1212 1.10 christos if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1) 1213 1.10 christos perror_with_name (("ptrace (PT_RESUME)")); 1214 1.10 christos low_prepare_to_resume (tp); 1215 1.11 christos fbsd_inf->running_lwps++; 1216 1.11 christos } 1217 1.11 christos else 1218 1.11 christos { 1219 1.11 christos if (ptrace (PT_SUSPEND, tp->ptid.lwp (), NULL, 0) == -1) 1220 1.11 christos perror_with_name (("ptrace (PT_SUSPEND)")); 1221 1.10 christos } 1222 1.8 christos } 1223 1.7 christos 1224 1.11 christos if (ptid.pid () != inferior_ptid.pid ()) 1225 1.8 christos { 1226 1.8 christos step = 0; 1227 1.11 christos signo = GDB_SIGNAL_0; 1228 1.11 christos gdb_assert (!ptid.lwp_p ()); 1229 1.8 christos } 1230 1.11 christos else 1231 1.11 christos { 1232 1.11 christos ptid = inferior_ptid; 1233 1.11 christos #if __FreeBSD_version < 1200052 1234 1.11 christos /* When multiple threads within a process wish to report STOPPED 1235 1.11 christos events from wait(), the kernel picks one thread event as the 1236 1.11 christos thread event to report. The chosen thread event is retrieved 1237 1.11 christos via PT_LWPINFO by passing the process ID as the request pid. 1238 1.11 christos If multiple events are pending, then the subsequent wait() 1239 1.11 christos after resuming a process will report another STOPPED event 1240 1.11 christos after resuming the process to handle the next thread event 1241 1.11 christos and so on. 1242 1.11 christos 1243 1.11 christos A single thread event is cleared as a side effect of resuming 1244 1.11 christos the process with PT_CONTINUE, PT_STEP, etc. In older 1245 1.11 christos kernels, however, the request pid was used to select which 1246 1.11 christos thread's event was cleared rather than always clearing the 1247 1.11 christos event that was just reported. To avoid clearing the event of 1248 1.11 christos the wrong LWP, always pass the process ID instead of an LWP 1249 1.11 christos ID to PT_CONTINUE or PT_SYSCALL. 1250 1.11 christos 1251 1.11 christos In the case of stepping, the process ID cannot be used with 1252 1.11 christos PT_STEP since it would step the thread that reported an event 1253 1.11 christos which may not be the thread indicated by PTID. For stepping, 1254 1.11 christos use PT_SETSTEP to enable stepping on the desired thread 1255 1.11 christos before resuming the process via PT_CONTINUE instead of using 1256 1.11 christos PT_STEP. */ 1257 1.11 christos if (step) 1258 1.11 christos { 1259 1.11 christos if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1) 1260 1.11 christos perror_with_name (("ptrace (PT_SETSTEP)")); 1261 1.11 christos step = 0; 1262 1.11 christos } 1263 1.11 christos ptid = ptid_t (ptid.pid ()); 1264 1.8 christos #endif 1265 1.11 christos } 1266 1.11 christos 1267 1.8 christos inf_ptrace_target::resume (ptid, step, signo); 1268 1.8 christos } 1269 1.8 christos 1270 1.11 christos /* Implement the "resume" target_ops method. */ 1271 1.11 christos 1272 1.11 christos void 1273 1.11 christos fbsd_nat_target::resume (ptid_t scope_ptid, int step, enum gdb_signal signo) 1274 1.11 christos { 1275 1.11 christos fbsd_nat_debug_start_end ("[%s], step %d, signo %d (%s)", 1276 1.11 christos target_pid_to_str (scope_ptid).c_str (), step, signo, 1277 1.11 christos gdb_signal_to_name (signo)); 1278 1.11 christos 1279 1.11 christos gdb_assert (inferior_ptid.matches (scope_ptid)); 1280 1.11 christos gdb_assert (!scope_ptid.tid_p ()); 1281 1.11 christos 1282 1.11 christos if (scope_ptid == minus_one_ptid) 1283 1.11 christos { 1284 1.11 christos for (inferior *inf : all_non_exited_inferiors (this)) 1285 1.11 christos resume_one_process (ptid_t (inf->pid), step, signo); 1286 1.11 christos } 1287 1.11 christos else 1288 1.11 christos { 1289 1.11 christos resume_one_process (scope_ptid, step, signo); 1290 1.11 christos } 1291 1.11 christos } 1292 1.11 christos 1293 1.8 christos #ifdef USE_SIGTRAP_SIGINFO 1294 1.8 christos /* Handle breakpoint and trace traps reported via SIGTRAP. If the 1295 1.8 christos trap was a breakpoint or trace trap that should be reported to the 1296 1.8 christos core, return true. */ 1297 1.8 christos 1298 1.8 christos static bool 1299 1.9 christos fbsd_handle_debug_trap (fbsd_nat_target *target, ptid_t ptid, 1300 1.9 christos const struct ptrace_lwpinfo &pl) 1301 1.8 christos { 1302 1.8 christos 1303 1.8 christos /* Ignore traps without valid siginfo or for signals other than 1304 1.8 christos SIGTRAP. 1305 1.8 christos 1306 1.8 christos FreeBSD kernels prior to r341800 can return stale siginfo for at 1307 1.8 christos least some events, but those events can be identified by 1308 1.8 christos additional flags set in pl_flags. True breakpoint and 1309 1.8 christos single-step traps should not have other flags set in 1310 1.8 christos pl_flags. */ 1311 1.8 christos if (pl.pl_flags != PL_FLAG_SI || pl.pl_siginfo.si_signo != SIGTRAP) 1312 1.8 christos return false; 1313 1.8 christos 1314 1.8 christos /* Trace traps are either a single step or a hardware watchpoint or 1315 1.8 christos breakpoint. */ 1316 1.8 christos if (pl.pl_siginfo.si_code == TRAP_TRACE) 1317 1.8 christos { 1318 1.10 christos fbsd_nat_debug_printf ("trace trap for LWP %ld", ptid.lwp ()); 1319 1.8 christos return true; 1320 1.8 christos } 1321 1.8 christos 1322 1.8 christos if (pl.pl_siginfo.si_code == TRAP_BRKPT) 1323 1.8 christos { 1324 1.8 christos /* Fixup PC for the software breakpoint. */ 1325 1.9 christos struct regcache *regcache = get_thread_regcache (target, ptid); 1326 1.8 christos struct gdbarch *gdbarch = regcache->arch (); 1327 1.8 christos int decr_pc = gdbarch_decr_pc_after_break (gdbarch); 1328 1.8 christos 1329 1.10 christos fbsd_nat_debug_printf ("sw breakpoint trap for LWP %ld", ptid.lwp ()); 1330 1.8 christos if (decr_pc != 0) 1331 1.8 christos { 1332 1.8 christos CORE_ADDR pc; 1333 1.7 christos 1334 1.8 christos pc = regcache_read_pc (regcache); 1335 1.8 christos regcache_write_pc (regcache, pc - decr_pc); 1336 1.7 christos } 1337 1.8 christos return true; 1338 1.6 christos } 1339 1.8 christos 1340 1.8 christos return false; 1341 1.5 christos } 1342 1.8 christos #endif 1343 1.5 christos 1344 1.5 christos /* Wait for the child specified by PTID to do something. Return the 1345 1.5 christos process ID of the child, or MINUS_ONE_PTID in case of error; store 1346 1.5 christos the status in *OURSTATUS. */ 1347 1.5 christos 1348 1.8 christos ptid_t 1349 1.10 christos fbsd_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus, 1350 1.10 christos target_wait_flags target_options) 1351 1.5 christos { 1352 1.5 christos ptid_t wptid; 1353 1.5 christos 1354 1.5 christos while (1) 1355 1.5 christos { 1356 1.8 christos wptid = inf_ptrace_target::wait (ptid, ourstatus, target_options); 1357 1.10 christos if (ourstatus->kind () == TARGET_WAITKIND_STOPPED) 1358 1.5 christos { 1359 1.5 christos struct ptrace_lwpinfo pl; 1360 1.11 christos pid_t pid = wptid.pid (); 1361 1.6 christos if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1) 1362 1.10 christos perror_with_name (("ptrace (PT_LWPINFO)")); 1363 1.5 christos 1364 1.10 christos wptid = ptid_t (pid, pl.pl_lwpid); 1365 1.8 christos 1366 1.8 christos if (debug_fbsd_nat) 1367 1.8 christos { 1368 1.10 christos fbsd_nat_debug_printf ("stop for LWP %u event %d flags %#x", 1369 1.10 christos pl.pl_lwpid, pl.pl_event, pl.pl_flags); 1370 1.8 christos if (pl.pl_flags & PL_FLAG_SI) 1371 1.10 christos fbsd_nat_debug_printf ("si_signo %u si_code %u", 1372 1.10 christos pl.pl_siginfo.si_signo, 1373 1.10 christos pl.pl_siginfo.si_code); 1374 1.8 christos } 1375 1.6 christos 1376 1.11 christos /* There may not be an inferior for this pid if this is a 1377 1.11 christos PL_FLAG_CHILD event. */ 1378 1.11 christos inferior *inf = find_inferior_ptid (this, wptid); 1379 1.11 christos fbsd_inferior *fbsd_inf = inf == nullptr ? nullptr 1380 1.11 christos : get_fbsd_inferior (inf); 1381 1.11 christos gdb_assert (fbsd_inf != nullptr || pl.pl_flags & PL_FLAG_CHILD); 1382 1.11 christos 1383 1.6 christos #ifdef PT_LWP_EVENTS 1384 1.6 christos if (pl.pl_flags & PL_FLAG_EXITED) 1385 1.6 christos { 1386 1.6 christos /* If GDB attaches to a multi-threaded process, exiting 1387 1.8 christos threads might be skipped during post_attach that 1388 1.6 christos have not yet reported their PL_FLAG_EXITED event. 1389 1.6 christos Ignore EXITED events for an unknown LWP. */ 1390 1.11 christos thread_info *thr = this->find_thread (wptid); 1391 1.8 christos if (thr != nullptr) 1392 1.6 christos { 1393 1.10 christos fbsd_lwp_debug_printf ("deleting thread for LWP %u", 1394 1.10 christos pl.pl_lwpid); 1395 1.10 christos low_delete_thread (thr); 1396 1.8 christos delete_thread (thr); 1397 1.11 christos fbsd_inf->num_lwps--; 1398 1.11 christos 1399 1.11 christos /* If this LWP was the only resumed LWP from the 1400 1.11 christos process, report an event to the core. */ 1401 1.11 christos if (wptid == fbsd_inf->resumed_lwps) 1402 1.11 christos { 1403 1.11 christos ourstatus->set_spurious (); 1404 1.11 christos return wptid; 1405 1.11 christos } 1406 1.11 christos 1407 1.11 christos /* During process exit LWPs that were not resumed 1408 1.11 christos will report exit events. */ 1409 1.11 christos if (wptid.matches (fbsd_inf->resumed_lwps)) 1410 1.11 christos fbsd_inf->running_lwps--; 1411 1.6 christos } 1412 1.6 christos if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) 1413 1.10 christos perror_with_name (("ptrace (PT_CONTINUE)")); 1414 1.6 christos continue; 1415 1.6 christos } 1416 1.6 christos #endif 1417 1.6 christos 1418 1.6 christos /* Switch to an LWP PTID on the first stop in a new process. 1419 1.6 christos This is done after handling PL_FLAG_EXITED to avoid 1420 1.6 christos switching to an exited LWP. It is done before checking 1421 1.6 christos PL_FLAG_BORN in case the first stop reported after 1422 1.6 christos attaching to an existing process is a PL_FLAG_BORN 1423 1.6 christos event. */ 1424 1.9 christos if (in_thread_list (this, ptid_t (pid))) 1425 1.6 christos { 1426 1.10 christos fbsd_lwp_debug_printf ("using LWP %u for first thread", 1427 1.10 christos pl.pl_lwpid); 1428 1.9 christos thread_change_ptid (this, ptid_t (pid), wptid); 1429 1.6 christos } 1430 1.6 christos 1431 1.6 christos #ifdef PT_LWP_EVENTS 1432 1.6 christos if (pl.pl_flags & PL_FLAG_BORN) 1433 1.6 christos { 1434 1.6 christos /* If GDB attaches to a multi-threaded process, newborn 1435 1.6 christos threads might be added by fbsd_add_threads that have 1436 1.6 christos not yet reported their PL_FLAG_BORN event. Ignore 1437 1.6 christos BORN events for an already-known LWP. */ 1438 1.9 christos if (!in_thread_list (this, wptid)) 1439 1.6 christos { 1440 1.10 christos fbsd_lwp_debug_printf ("adding thread for LWP %u", 1441 1.10 christos pl.pl_lwpid); 1442 1.9 christos add_thread (this, wptid); 1443 1.11 christos fbsd_inf->num_lwps++; 1444 1.11 christos 1445 1.11 christos if (wptid.matches(fbsd_inf->resumed_lwps)) 1446 1.11 christos fbsd_inf->running_lwps++; 1447 1.6 christos } 1448 1.10 christos ourstatus->set_spurious (); 1449 1.6 christos return wptid; 1450 1.6 christos } 1451 1.6 christos #endif 1452 1.6 christos 1453 1.5 christos #ifdef TDP_RFPPWAIT 1454 1.5 christos if (pl.pl_flags & PL_FLAG_FORKED) 1455 1.5 christos { 1456 1.6 christos #ifndef PTRACE_VFORK 1457 1.5 christos struct kinfo_proc kp; 1458 1.6 christos #endif 1459 1.10 christos bool is_vfork = false; 1460 1.6 christos ptid_t child_ptid; 1461 1.5 christos pid_t child; 1462 1.5 christos 1463 1.5 christos child = pl.pl_child_pid; 1464 1.6 christos #ifdef PTRACE_VFORK 1465 1.6 christos if (pl.pl_flags & PL_FLAG_VFORKED) 1466 1.10 christos is_vfork = true; 1467 1.6 christos #endif 1468 1.5 christos 1469 1.5 christos /* Make sure the other end of the fork is stopped too. */ 1470 1.11 christos child_ptid = fbsd_wait_for_fork_child (child); 1471 1.5 christos 1472 1.6 christos /* Enable additional events on the child process. */ 1473 1.8 christos fbsd_enable_proc_events (child_ptid.pid ()); 1474 1.6 christos 1475 1.6 christos #ifndef PTRACE_VFORK 1476 1.5 christos /* For vfork, the child process will have the P_PPWAIT 1477 1.5 christos flag set. */ 1478 1.8 christos if (fbsd_fetch_kinfo_proc (child, &kp)) 1479 1.8 christos { 1480 1.8 christos if (kp.ki_flag & P_PPWAIT) 1481 1.10 christos is_vfork = true; 1482 1.8 christos } 1483 1.8 christos else 1484 1.8 christos warning (_("Failed to fetch process information")); 1485 1.6 christos #endif 1486 1.10 christos 1487 1.10 christos low_new_fork (wptid, child); 1488 1.10 christos 1489 1.10 christos if (is_vfork) 1490 1.10 christos ourstatus->set_vforked (child_ptid); 1491 1.10 christos else 1492 1.10 christos ourstatus->set_forked (child_ptid); 1493 1.5 christos 1494 1.5 christos return wptid; 1495 1.5 christos } 1496 1.5 christos 1497 1.5 christos if (pl.pl_flags & PL_FLAG_CHILD) 1498 1.5 christos { 1499 1.5 christos /* Remember that this child forked, but do not report it 1500 1.5 christos until the parent reports its corresponding fork 1501 1.5 christos event. */ 1502 1.6 christos fbsd_remember_child (wptid); 1503 1.5 christos continue; 1504 1.5 christos } 1505 1.6 christos 1506 1.6 christos #ifdef PTRACE_VFORK 1507 1.6 christos if (pl.pl_flags & PL_FLAG_VFORK_DONE) 1508 1.6 christos { 1509 1.10 christos ourstatus->set_vfork_done (); 1510 1.6 christos return wptid; 1511 1.6 christos } 1512 1.6 christos #endif 1513 1.5 christos #endif 1514 1.5 christos 1515 1.5 christos if (pl.pl_flags & PL_FLAG_EXEC) 1516 1.5 christos { 1517 1.10 christos ourstatus->set_execd 1518 1.10 christos (make_unique_xstrdup (pid_to_exec_file (pid))); 1519 1.5 christos return wptid; 1520 1.5 christos } 1521 1.6 christos 1522 1.8 christos #ifdef USE_SIGTRAP_SIGINFO 1523 1.9 christos if (fbsd_handle_debug_trap (this, wptid, pl)) 1524 1.8 christos return wptid; 1525 1.8 christos #endif 1526 1.8 christos 1527 1.6 christos /* Note that PL_FLAG_SCE is set for any event reported while 1528 1.6 christos a thread is executing a system call in the kernel. In 1529 1.6 christos particular, signals that interrupt a sleep in a system 1530 1.6 christos call will report this flag as part of their event. Stops 1531 1.6 christos explicitly for system call entry and exit always use 1532 1.6 christos SIGTRAP, so only treat SIGTRAP events as system call 1533 1.6 christos entry/exit events. */ 1534 1.6 christos if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX) 1535 1.11 christos && ourstatus->sig () == GDB_SIGNAL_TRAP) 1536 1.6 christos { 1537 1.6 christos #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE 1538 1.6 christos if (catch_syscall_enabled ()) 1539 1.6 christos { 1540 1.6 christos if (catching_syscall_number (pl.pl_syscall_code)) 1541 1.6 christos { 1542 1.6 christos if (pl.pl_flags & PL_FLAG_SCE) 1543 1.10 christos ourstatus->set_syscall_entry (pl.pl_syscall_code); 1544 1.6 christos else 1545 1.10 christos ourstatus->set_syscall_return (pl.pl_syscall_code); 1546 1.10 christos 1547 1.6 christos return wptid; 1548 1.6 christos } 1549 1.6 christos } 1550 1.6 christos #endif 1551 1.6 christos /* If the core isn't interested in this event, just 1552 1.6 christos continue the process explicitly and wait for another 1553 1.6 christos event. Note that PT_SYSCALL is "sticky" on FreeBSD 1554 1.6 christos and once system call stops are enabled on a process 1555 1.6 christos it stops for all system call entries and exits. */ 1556 1.6 christos if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) 1557 1.10 christos perror_with_name (("ptrace (PT_CONTINUE)")); 1558 1.6 christos continue; 1559 1.6 christos } 1560 1.11 christos 1561 1.11 christos /* If this is a pending SIGSTOP event from an earlier call 1562 1.11 christos to stop_process, discard the event and wait for another 1563 1.11 christos event. */ 1564 1.11 christos if (ourstatus->sig () == GDB_SIGNAL_STOP && fbsd_inf->pending_sigstop) 1565 1.11 christos { 1566 1.11 christos fbsd_nat_debug_printf ("ignoring SIGSTOP for pid %u", pid); 1567 1.11 christos fbsd_inf->pending_sigstop = false; 1568 1.11 christos if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) 1569 1.11 christos perror_with_name (("ptrace (PT_CONTINUE)")); 1570 1.11 christos continue; 1571 1.11 christos } 1572 1.5 christos } 1573 1.11 christos else 1574 1.11 christos fbsd_nat_debug_printf ("event [%s], [%s]", 1575 1.11 christos target_pid_to_str (wptid).c_str (), 1576 1.11 christos ourstatus->to_string ().c_str ()); 1577 1.5 christos return wptid; 1578 1.5 christos } 1579 1.5 christos } 1580 1.5 christos 1581 1.11 christos /* Stop a given process. If the process is already stopped, record 1582 1.11 christos its pending event instead. */ 1583 1.11 christos 1584 1.11 christos void 1585 1.11 christos fbsd_nat_target::stop_process (inferior *inf) 1586 1.11 christos { 1587 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 1588 1.11 christos gdb_assert (fbsd_inf != nullptr); 1589 1.11 christos 1590 1.11 christos fbsd_inf->resumed_lwps = null_ptid; 1591 1.11 christos if (fbsd_inf->running_lwps == 0) 1592 1.11 christos return; 1593 1.11 christos 1594 1.11 christos ptid_t ptid (inf->pid); 1595 1.11 christos target_waitstatus status; 1596 1.11 christos ptid_t wptid = wait_1 (ptid, &status, TARGET_WNOHANG); 1597 1.11 christos 1598 1.11 christos if (wptid != minus_one_ptid) 1599 1.11 christos { 1600 1.11 christos /* Save the current event as a pending event. */ 1601 1.11 christos add_pending_event (wptid, status); 1602 1.11 christos fbsd_inf->running_lwps = 0; 1603 1.11 christos return; 1604 1.11 christos } 1605 1.11 christos 1606 1.11 christos /* If a SIGSTOP is already pending, don't send a new one, but tell 1607 1.11 christos wait_1 to report a SIGSTOP. */ 1608 1.11 christos if (fbsd_inf->pending_sigstop) 1609 1.11 christos { 1610 1.11 christos fbsd_nat_debug_printf ("waiting for existing pending SIGSTOP for %u", 1611 1.11 christos inf->pid); 1612 1.11 christos fbsd_inf->pending_sigstop = false; 1613 1.11 christos } 1614 1.11 christos else 1615 1.11 christos { 1616 1.11 christos /* Ignore errors from kill as process exit might race with kill. */ 1617 1.11 christos fbsd_nat_debug_printf ("killing %u with SIGSTOP", inf->pid); 1618 1.11 christos ::kill (inf->pid, SIGSTOP); 1619 1.11 christos } 1620 1.11 christos 1621 1.11 christos /* Wait for SIGSTOP (or some other event) to be reported. */ 1622 1.11 christos wptid = wait_1 (ptid, &status, 0); 1623 1.11 christos 1624 1.11 christos switch (status.kind ()) 1625 1.11 christos { 1626 1.11 christos case TARGET_WAITKIND_EXITED: 1627 1.11 christos case TARGET_WAITKIND_SIGNALLED: 1628 1.11 christos /* If the process has exited, we aren't going to get an 1629 1.11 christos event for the SIGSTOP. Save the current event and 1630 1.11 christos return. */ 1631 1.11 christos add_pending_event (wptid, status); 1632 1.11 christos break; 1633 1.11 christos case TARGET_WAITKIND_IGNORE: 1634 1.11 christos /* wait() failed with ECHILD meaning the process no longer 1635 1.11 christos exists. This means a bug happened elsewhere, but at least 1636 1.11 christos the process is no longer running. */ 1637 1.11 christos break; 1638 1.11 christos case TARGET_WAITKIND_STOPPED: 1639 1.11 christos /* If this is the SIGSTOP event, discard it and return 1640 1.11 christos leaving the process stopped. */ 1641 1.11 christos if (status.sig () == GDB_SIGNAL_STOP) 1642 1.11 christos break; 1643 1.11 christos 1644 1.11 christos [[fallthrough]]; 1645 1.11 christos default: 1646 1.11 christos /* Some other event has occurred. Save the current 1647 1.11 christos event. */ 1648 1.11 christos add_pending_event (wptid, status); 1649 1.11 christos 1650 1.11 christos /* Ignore the next SIGSTOP for this process. */ 1651 1.11 christos fbsd_nat_debug_printf ("ignoring next SIGSTOP for %u", inf->pid); 1652 1.11 christos fbsd_inf->pending_sigstop = true; 1653 1.11 christos break; 1654 1.11 christos } 1655 1.11 christos fbsd_inf->running_lwps = 0; 1656 1.11 christos } 1657 1.11 christos 1658 1.10 christos ptid_t 1659 1.10 christos fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, 1660 1.10 christos target_wait_flags target_options) 1661 1.10 christos { 1662 1.10 christos fbsd_nat_debug_printf ("[%s], [%s]", target_pid_to_str (ptid).c_str (), 1663 1.10 christos target_options_to_string (target_options).c_str ()); 1664 1.10 christos 1665 1.11 christos /* If there is a valid pending event, return it. */ 1666 1.11 christos std::optional<pending_event> event = take_pending_event (ptid); 1667 1.11 christos if (event.has_value ()) 1668 1.11 christos { 1669 1.11 christos /* Stop any other inferiors currently running. */ 1670 1.11 christos for (inferior *inf : all_non_exited_inferiors (this)) 1671 1.11 christos stop_process (inf); 1672 1.11 christos 1673 1.11 christos fbsd_nat_debug_printf ("returning pending event [%s], [%s]", 1674 1.11 christos target_pid_to_str (event->ptid).c_str (), 1675 1.11 christos event->status.to_string ().c_str ()); 1676 1.11 christos gdb_assert (event->ptid.matches (ptid)); 1677 1.11 christos *ourstatus = event->status; 1678 1.11 christos return event->ptid; 1679 1.11 christos } 1680 1.11 christos 1681 1.10 christos /* Ensure any subsequent events trigger a new event in the loop. */ 1682 1.10 christos if (is_async_p ()) 1683 1.10 christos async_file_flush (); 1684 1.10 christos 1685 1.11 christos ptid_t wptid; 1686 1.11 christos while (1) 1687 1.11 christos { 1688 1.11 christos wptid = wait_1 (ptid, ourstatus, target_options); 1689 1.11 christos 1690 1.11 christos /* If no event was found, just return. */ 1691 1.11 christos if (ourstatus->kind () == TARGET_WAITKIND_IGNORE 1692 1.11 christos || ourstatus->kind () == TARGET_WAITKIND_NO_RESUMED) 1693 1.11 christos break; 1694 1.11 christos 1695 1.11 christos inferior *winf = find_inferior_ptid (this, wptid); 1696 1.11 christos gdb_assert (winf != nullptr); 1697 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (winf); 1698 1.11 christos gdb_assert (fbsd_inf != nullptr); 1699 1.11 christos gdb_assert (fbsd_inf->resumed_lwps != null_ptid); 1700 1.11 christos gdb_assert (fbsd_inf->running_lwps > 0); 1701 1.11 christos 1702 1.11 christos /* If an event is reported for a thread or process while 1703 1.11 christos stepping some other thread, suspend the thread reporting the 1704 1.11 christos event and defer the event until it can be reported to the 1705 1.11 christos core. */ 1706 1.11 christos if (!wptid.matches (fbsd_inf->resumed_lwps)) 1707 1.11 christos { 1708 1.11 christos add_pending_event (wptid, *ourstatus); 1709 1.11 christos fbsd_nat_debug_printf ("deferring event [%s], [%s]", 1710 1.11 christos target_pid_to_str (wptid).c_str (), 1711 1.11 christos ourstatus->to_string ().c_str ()); 1712 1.11 christos if (ptrace (PT_SUSPEND, wptid.lwp (), NULL, 0) == -1) 1713 1.11 christos perror_with_name (("ptrace (PT_SUSPEND)")); 1714 1.11 christos if (ptrace (PT_CONTINUE, wptid.pid (), (caddr_t) 1, 0) == -1) 1715 1.11 christos perror_with_name (("ptrace (PT_CONTINUE)")); 1716 1.11 christos continue; 1717 1.11 christos } 1718 1.11 christos 1719 1.11 christos /* This process is no longer running. */ 1720 1.11 christos fbsd_inf->resumed_lwps = null_ptid; 1721 1.11 christos fbsd_inf->running_lwps = 0; 1722 1.11 christos 1723 1.11 christos /* Stop any other inferiors currently running. */ 1724 1.11 christos for (inferior *inf : all_non_exited_inferiors (this)) 1725 1.11 christos stop_process (inf); 1726 1.11 christos 1727 1.11 christos break; 1728 1.11 christos } 1729 1.10 christos 1730 1.10 christos /* If we are in async mode and found an event, there may still be 1731 1.10 christos another event pending. Trigger the event pipe so that that the 1732 1.10 christos event loop keeps polling until no event is returned. */ 1733 1.10 christos if (is_async_p () 1734 1.10 christos && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE 1735 1.11 christos && ourstatus->kind () != TARGET_WAITKIND_NO_RESUMED) 1736 1.10 christos || ptid != minus_one_ptid)) 1737 1.10 christos async_file_mark (); 1738 1.10 christos 1739 1.10 christos fbsd_nat_debug_printf ("returning [%s], [%s]", 1740 1.10 christos target_pid_to_str (wptid).c_str (), 1741 1.10 christos ourstatus->to_string ().c_str ()); 1742 1.10 christos return wptid; 1743 1.10 christos } 1744 1.10 christos 1745 1.8 christos #ifdef USE_SIGTRAP_SIGINFO 1746 1.8 christos /* Implement the "stopped_by_sw_breakpoint" target_ops method. */ 1747 1.8 christos 1748 1.8 christos bool 1749 1.8 christos fbsd_nat_target::stopped_by_sw_breakpoint () 1750 1.8 christos { 1751 1.8 christos struct ptrace_lwpinfo pl; 1752 1.8 christos 1753 1.8 christos if (ptrace (PT_LWPINFO, get_ptrace_pid (inferior_ptid), (caddr_t) &pl, 1754 1.8 christos sizeof pl) == -1) 1755 1.8 christos return false; 1756 1.8 christos 1757 1.8 christos return (pl.pl_flags == PL_FLAG_SI 1758 1.8 christos && pl.pl_siginfo.si_signo == SIGTRAP 1759 1.8 christos && pl.pl_siginfo.si_code == TRAP_BRKPT); 1760 1.8 christos } 1761 1.8 christos 1762 1.8 christos /* Implement the "supports_stopped_by_sw_breakpoint" target_ops 1763 1.8 christos method. */ 1764 1.8 christos 1765 1.8 christos bool 1766 1.8 christos fbsd_nat_target::supports_stopped_by_sw_breakpoint () 1767 1.8 christos { 1768 1.8 christos return true; 1769 1.8 christos } 1770 1.8 christos #endif 1771 1.8 christos 1772 1.10 christos #ifdef PROC_ASLR_CTL 1773 1.10 christos class maybe_disable_address_space_randomization 1774 1.10 christos { 1775 1.10 christos public: 1776 1.10 christos explicit maybe_disable_address_space_randomization (bool disable_randomization) 1777 1.10 christos { 1778 1.10 christos if (disable_randomization) 1779 1.10 christos { 1780 1.10 christos if (procctl (P_PID, getpid (), PROC_ASLR_STATUS, &m_aslr_ctl) == -1) 1781 1.10 christos { 1782 1.10 christos warning (_("Failed to fetch current address space randomization " 1783 1.10 christos "status: %s"), safe_strerror (errno)); 1784 1.10 christos return; 1785 1.10 christos } 1786 1.10 christos 1787 1.10 christos m_aslr_ctl &= ~PROC_ASLR_ACTIVE; 1788 1.10 christos if (m_aslr_ctl == PROC_ASLR_FORCE_DISABLE) 1789 1.10 christos return; 1790 1.10 christos 1791 1.10 christos int ctl = PROC_ASLR_FORCE_DISABLE; 1792 1.10 christos if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &ctl) == -1) 1793 1.10 christos { 1794 1.10 christos warning (_("Error disabling address space randomization: %s"), 1795 1.10 christos safe_strerror (errno)); 1796 1.10 christos return; 1797 1.10 christos } 1798 1.10 christos 1799 1.10 christos m_aslr_ctl_set = true; 1800 1.10 christos } 1801 1.10 christos } 1802 1.10 christos 1803 1.10 christos ~maybe_disable_address_space_randomization () 1804 1.10 christos { 1805 1.10 christos if (m_aslr_ctl_set) 1806 1.10 christos { 1807 1.10 christos if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &m_aslr_ctl) == -1) 1808 1.10 christos warning (_("Error restoring address space randomization: %s"), 1809 1.10 christos safe_strerror (errno)); 1810 1.10 christos } 1811 1.10 christos } 1812 1.10 christos 1813 1.10 christos DISABLE_COPY_AND_ASSIGN (maybe_disable_address_space_randomization); 1814 1.10 christos 1815 1.10 christos private: 1816 1.10 christos bool m_aslr_ctl_set = false; 1817 1.10 christos int m_aslr_ctl = 0; 1818 1.10 christos }; 1819 1.10 christos #endif 1820 1.10 christos 1821 1.10 christos void 1822 1.10 christos fbsd_nat_target::create_inferior (const char *exec_file, 1823 1.10 christos const std::string &allargs, 1824 1.10 christos char **env, int from_tty) 1825 1.10 christos { 1826 1.10 christos #ifdef PROC_ASLR_CTL 1827 1.10 christos maybe_disable_address_space_randomization restore_aslr_ctl 1828 1.10 christos (disable_randomization); 1829 1.10 christos #endif 1830 1.10 christos 1831 1.11 christos fbsd_inferior *fbsd_inf = new fbsd_inferior; 1832 1.11 christos current_inferior ()->priv.reset (fbsd_inf); 1833 1.11 christos fbsd_inf->resumed_lwps = minus_one_ptid; 1834 1.11 christos fbsd_inf->num_lwps = 1; 1835 1.11 christos fbsd_inf->running_lwps = 1; 1836 1.10 christos inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty); 1837 1.10 christos } 1838 1.10 christos 1839 1.11 christos void 1840 1.11 christos fbsd_nat_target::attach (const char *args, int from_tty) 1841 1.11 christos { 1842 1.11 christos fbsd_inferior *fbsd_inf = new fbsd_inferior; 1843 1.11 christos current_inferior ()->priv.reset (fbsd_inf); 1844 1.11 christos fbsd_inf->resumed_lwps = minus_one_ptid; 1845 1.11 christos fbsd_inf->num_lwps = 1; 1846 1.11 christos fbsd_inf->running_lwps = 1; 1847 1.11 christos inf_ptrace_target::attach (args, from_tty); 1848 1.11 christos } 1849 1.11 christos 1850 1.11 christos /* If this thread has a pending fork event, there is a child process 1851 1.11 christos GDB is attached to that the core of GDB doesn't know about. 1852 1.11 christos Detach from it. */ 1853 1.11 christos 1854 1.11 christos void 1855 1.11 christos fbsd_nat_target::detach_fork_children (thread_info *tp) 1856 1.11 christos { 1857 1.11 christos /* Check in thread_info::pending_waitstatus. */ 1858 1.11 christos if (tp->has_pending_waitstatus ()) 1859 1.11 christos { 1860 1.11 christos const target_waitstatus &ws = tp->pending_waitstatus (); 1861 1.11 christos 1862 1.11 christos if (ws.kind () == TARGET_WAITKIND_VFORKED 1863 1.11 christos || ws.kind () == TARGET_WAITKIND_FORKED) 1864 1.11 christos { 1865 1.11 christos pid_t pid = ws.child_ptid ().pid (); 1866 1.11 christos fbsd_nat_debug_printf ("detaching from child %d", pid); 1867 1.11 christos (void) ptrace (PT_DETACH, pid, (caddr_t) 1, 0); 1868 1.11 christos } 1869 1.11 christos } 1870 1.11 christos 1871 1.11 christos /* Check in thread_info::pending_follow. */ 1872 1.11 christos if (tp->pending_follow.kind () == TARGET_WAITKIND_VFORKED 1873 1.11 christos || tp->pending_follow.kind () == TARGET_WAITKIND_FORKED) 1874 1.11 christos { 1875 1.11 christos pid_t pid = tp->pending_follow.child_ptid ().pid (); 1876 1.11 christos fbsd_nat_debug_printf ("detaching from child %d", pid); 1877 1.11 christos (void) ptrace (PT_DETACH, pid, (caddr_t) 1, 0); 1878 1.11 christos } 1879 1.11 christos } 1880 1.11 christos 1881 1.11 christos /* Detach from any child processes associated with pending fork events 1882 1.11 christos for a stopped process. Returns true if the process has terminated 1883 1.11 christos and false if it is still alive. */ 1884 1.11 christos 1885 1.11 christos bool 1886 1.11 christos fbsd_nat_target::detach_fork_children (inferior *inf) 1887 1.11 christos { 1888 1.11 christos /* Detach any child processes associated with pending fork events in 1889 1.11 christos threads belonging to this process. */ 1890 1.11 christos for (thread_info *tp : inf->non_exited_threads ()) 1891 1.11 christos detach_fork_children (tp); 1892 1.11 christos 1893 1.11 christos /* Unwind state associated with any pending events. Reset 1894 1.11 christos fbsd_inf->resumed_lwps so that take_pending_event will harvest 1895 1.11 christos events. */ 1896 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 1897 1.11 christos ptid_t ptid = ptid_t (inf->pid); 1898 1.11 christos fbsd_inf->resumed_lwps = ptid; 1899 1.11 christos 1900 1.11 christos while (1) 1901 1.11 christos { 1902 1.11 christos std::optional<pending_event> event = take_pending_event (ptid); 1903 1.11 christos if (!event.has_value ()) 1904 1.11 christos break; 1905 1.11 christos 1906 1.11 christos switch (event->status.kind ()) 1907 1.11 christos { 1908 1.11 christos case TARGET_WAITKIND_EXITED: 1909 1.11 christos case TARGET_WAITKIND_SIGNALLED: 1910 1.11 christos return true; 1911 1.11 christos case TARGET_WAITKIND_FORKED: 1912 1.11 christos case TARGET_WAITKIND_VFORKED: 1913 1.11 christos { 1914 1.11 christos pid_t pid = event->status.child_ptid ().pid (); 1915 1.11 christos fbsd_nat_debug_printf ("detaching from child %d", pid); 1916 1.11 christos (void) ptrace (PT_DETACH, pid, (caddr_t) 1, 0); 1917 1.11 christos } 1918 1.11 christos break; 1919 1.11 christos } 1920 1.11 christos } 1921 1.11 christos return false; 1922 1.11 christos } 1923 1.11 christos 1924 1.11 christos /* Scan all of the threads for a stopped process invoking the supplied 1925 1.11 christos callback on the ptrace_lwpinfo object for threads other than the 1926 1.11 christos thread which reported the current stop. The callback can return 1927 1.11 christos true to terminate the iteration early. This function returns true 1928 1.11 christos if the callback returned true, otherwise it returns false. */ 1929 1.11 christos 1930 1.11 christos typedef bool (ptrace_event_ftype) (const struct ptrace_lwpinfo &pl); 1931 1.11 christos 1932 1.11 christos static bool 1933 1.11 christos iterate_other_ptrace_events (pid_t pid, 1934 1.11 christos gdb::function_view<ptrace_event_ftype> callback) 1935 1.11 christos { 1936 1.11 christos /* Fetch the LWP ID of the thread that just reported the last stop 1937 1.11 christos and ignore that LWP in the following loop. */ 1938 1.11 christos ptrace_lwpinfo pl; 1939 1.11 christos if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof (pl)) != 0) 1940 1.11 christos perror_with_name (("ptrace (PT_LWPINFO)")); 1941 1.11 christos lwpid_t lwpid = pl.pl_lwpid; 1942 1.11 christos 1943 1.11 christos int nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0); 1944 1.11 christos if (nlwps == -1) 1945 1.11 christos perror_with_name (("ptrace (PT_GETLWPLIST)")); 1946 1.11 christos if (nlwps == 1) 1947 1.11 christos return false; 1948 1.11 christos 1949 1.11 christos gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps)); 1950 1.11 christos 1951 1.11 christos nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps); 1952 1.11 christos if (nlwps == -1) 1953 1.11 christos perror_with_name (("ptrace (PT_GETLWPLIST)")); 1954 1.11 christos 1955 1.11 christos for (int i = 0; i < nlwps; i++) 1956 1.11 christos { 1957 1.11 christos if (lwps[i] == lwpid) 1958 1.11 christos continue; 1959 1.11 christos 1960 1.11 christos if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof (pl)) != 0) 1961 1.11 christos perror_with_name (("ptrace (PT_LWPINFO)")); 1962 1.11 christos 1963 1.11 christos if (callback (pl)) 1964 1.11 christos return true; 1965 1.11 christos } 1966 1.11 christos return false; 1967 1.11 christos } 1968 1.11 christos 1969 1.11 christos /* True if there are any stopped threads with an interesting event. */ 1970 1.11 christos 1971 1.11 christos static bool 1972 1.11 christos pending_ptrace_events (inferior *inf) 1973 1.11 christos { 1974 1.11 christos auto lambda = [] (const struct ptrace_lwpinfo &pl) 1975 1.11 christos { 1976 1.11 christos #if defined(PT_LWP_EVENTS) && __FreeBSD_kernel_version < 1400090 1977 1.11 christos if (pl.pl_flags == PL_FLAG_BORN) 1978 1.11 christos return true; 1979 1.11 christos #endif 1980 1.11 christos #ifdef TDP_RFPPWAIT 1981 1.11 christos if (pl.pl_flags & PL_FLAG_FORKED) 1982 1.11 christos return true; 1983 1.11 christos #endif 1984 1.11 christos if (pl.pl_event == PL_EVENT_SIGNAL) 1985 1.11 christos { 1986 1.11 christos if ((pl.pl_flags & PL_FLAG_SI) == 0) 1987 1.11 christos { 1988 1.11 christos /* Not sure which signal, assume it matters. */ 1989 1.11 christos return true; 1990 1.11 christos } 1991 1.11 christos if (pl.pl_siginfo.si_signo == SIGTRAP) 1992 1.11 christos return true; 1993 1.11 christos } 1994 1.11 christos return false; 1995 1.11 christos }; 1996 1.11 christos return iterate_other_ptrace_events (inf->pid, 1997 1.11 christos gdb::make_function_view (lambda)); 1998 1.11 christos } 1999 1.11 christos 2000 1.11 christos void 2001 1.11 christos fbsd_nat_target::detach (inferior *inf, int from_tty) 2002 1.11 christos { 2003 1.11 christos fbsd_nat_debug_start_end ("pid %d", inf->pid); 2004 1.11 christos 2005 1.11 christos stop_process (inf); 2006 1.11 christos 2007 1.11 christos remove_breakpoints_inf (inf); 2008 1.11 christos 2009 1.11 christos if (detach_fork_children (inf)) { 2010 1.11 christos /* No need to detach now. */ 2011 1.11 christos target_announce_detach (from_tty); 2012 1.11 christos 2013 1.11 christos detach_success (inf); 2014 1.11 christos return; 2015 1.11 christos } 2016 1.11 christos 2017 1.11 christos /* If there are any pending events (SIGSTOP from stop_process or a 2018 1.11 christos breakpoint hit that needs a PC fixup), drain events until the 2019 1.11 christos process can be safely detached. */ 2020 1.11 christos fbsd_inferior *fbsd_inf = get_fbsd_inferior (inf); 2021 1.11 christos ptid_t ptid = ptid_t (inf->pid); 2022 1.11 christos if (fbsd_inf->pending_sigstop || pending_ptrace_events (inf)) 2023 1.11 christos { 2024 1.11 christos bool pending_sigstop = fbsd_inf->pending_sigstop; 2025 1.11 christos int sig = 0; 2026 1.11 christos 2027 1.11 christos if (pending_sigstop) 2028 1.11 christos fbsd_nat_debug_printf ("waiting for SIGSTOP"); 2029 1.11 christos 2030 1.11 christos /* Force wait_1 to report the SIGSTOP instead of swallowing it. */ 2031 1.11 christos fbsd_inf->pending_sigstop = false; 2032 1.11 christos 2033 1.11 christos /* Report event for all threads from wait_1. */ 2034 1.11 christos fbsd_inf->resumed_lwps = ptid; 2035 1.11 christos 2036 1.11 christos do 2037 1.11 christos { 2038 1.11 christos if (ptrace (PT_CONTINUE, inf->pid, (caddr_t) 1, sig) != 0) 2039 1.11 christos perror_with_name (("ptrace(PT_CONTINUE)")); 2040 1.11 christos 2041 1.11 christos target_waitstatus ws; 2042 1.11 christos ptid_t wptid = wait_1 (ptid, &ws, 0); 2043 1.11 christos 2044 1.11 christos switch (ws.kind ()) 2045 1.11 christos { 2046 1.11 christos case TARGET_WAITKIND_EXITED: 2047 1.11 christos case TARGET_WAITKIND_SIGNALLED: 2048 1.11 christos /* No need to detach now. */ 2049 1.11 christos target_announce_detach (from_tty); 2050 1.11 christos 2051 1.11 christos detach_success (inf); 2052 1.11 christos return; 2053 1.11 christos case TARGET_WAITKIND_FORKED: 2054 1.11 christos case TARGET_WAITKIND_VFORKED: 2055 1.11 christos { 2056 1.11 christos pid_t pid = ws.child_ptid ().pid (); 2057 1.11 christos fbsd_nat_debug_printf ("detaching from child %d", pid); 2058 1.11 christos (void) ptrace (PT_DETACH, pid, (caddr_t) 1, 0); 2059 1.11 christos sig = 0; 2060 1.11 christos } 2061 1.11 christos break; 2062 1.11 christos case TARGET_WAITKIND_STOPPED: 2063 1.11 christos sig = gdb_signal_to_host (ws.sig ()); 2064 1.11 christos switch (sig) 2065 1.11 christos { 2066 1.11 christos case SIGSTOP: 2067 1.11 christos if (pending_sigstop) 2068 1.11 christos { 2069 1.11 christos sig = 0; 2070 1.11 christos pending_sigstop = false; 2071 1.11 christos } 2072 1.11 christos break; 2073 1.11 christos case SIGTRAP: 2074 1.11 christos #ifndef USE_SIGTRAP_SIGINFO 2075 1.11 christos { 2076 1.11 christos /* Update PC from software breakpoint hit. */ 2077 1.11 christos struct regcache *regcache = get_thread_regcache (this, wptid); 2078 1.11 christos struct gdbarch *gdbarch = regcache->arch (); 2079 1.11 christos int decr_pc = gdbarch_decr_pc_after_break (gdbarch); 2080 1.11 christos 2081 1.11 christos if (decr_pc != 0) 2082 1.11 christos { 2083 1.11 christos CORE_ADDR pc; 2084 1.11 christos 2085 1.11 christos pc = regcache_read_pc (regcache); 2086 1.11 christos if (breakpoint_inserted_here_p (regcache->aspace (), 2087 1.11 christos pc - decr_pc)) 2088 1.11 christos { 2089 1.11 christos fbsd_nat_debug_printf ("adjusted PC for LWP %ld", 2090 1.11 christos wptid.lwp ()); 2091 1.11 christos regcache_write_pc (regcache, pc - decr_pc); 2092 1.11 christos } 2093 1.11 christos } 2094 1.11 christos } 2095 1.11 christos #else 2096 1.11 christos /* pacify gcc */ 2097 1.11 christos (void) wptid; 2098 1.11 christos #endif 2099 1.11 christos sig = 0; 2100 1.11 christos break; 2101 1.11 christos } 2102 1.11 christos } 2103 1.11 christos } 2104 1.11 christos while (pending_sigstop || pending_ptrace_events (inf)); 2105 1.11 christos } 2106 1.11 christos 2107 1.11 christos target_announce_detach (from_tty); 2108 1.11 christos 2109 1.11 christos if (ptrace (PT_DETACH, inf->pid, (caddr_t) 1, 0) == -1) 2110 1.11 christos perror_with_name (("ptrace (PT_DETACH)")); 2111 1.11 christos 2112 1.11 christos detach_success (inf); 2113 1.11 christos } 2114 1.11 christos 2115 1.11 christos /* Implement the "kill" target method. */ 2116 1.11 christos 2117 1.11 christos void 2118 1.11 christos fbsd_nat_target::kill () 2119 1.11 christos { 2120 1.11 christos pid_t pid = inferior_ptid.pid (); 2121 1.11 christos if (pid == 0) 2122 1.11 christos return; 2123 1.11 christos 2124 1.11 christos inferior *inf = current_inferior (); 2125 1.11 christos stop_process (inf); 2126 1.11 christos 2127 1.11 christos if (detach_fork_children (inf)) { 2128 1.11 christos /* No need to kill now. */ 2129 1.11 christos target_mourn_inferior (inferior_ptid); 2130 1.11 christos 2131 1.11 christos return; 2132 1.11 christos } 2133 1.11 christos 2134 1.11 christos #ifdef TDP_RFPPWAIT 2135 1.11 christos /* If there are any threads that have forked a new child but not yet 2136 1.11 christos reported it because other threads reported events first, detach 2137 1.11 christos from the children before killing the parent. */ 2138 1.11 christos auto lambda = [] (const struct ptrace_lwpinfo &pl) 2139 1.11 christos { 2140 1.11 christos if (pl.pl_flags & PL_FLAG_FORKED) 2141 1.11 christos { 2142 1.11 christos pid_t child = pl.pl_child_pid; 2143 1.11 christos 2144 1.11 christos /* If the child hasn't reported its stop yet, wait for it to 2145 1.11 christos stop. */ 2146 1.11 christos fbsd_wait_for_fork_child (child); 2147 1.11 christos 2148 1.11 christos /* Detach from the child. */ 2149 1.11 christos (void) ptrace (PT_DETACH, child, (caddr_t) 1, 0); 2150 1.11 christos } 2151 1.11 christos return false; 2152 1.11 christos }; 2153 1.11 christos iterate_other_ptrace_events (pid, gdb::make_function_view (lambda)); 2154 1.11 christos #endif 2155 1.11 christos 2156 1.11 christos if (ptrace (PT_KILL, pid, NULL, 0) == -1) 2157 1.11 christos perror_with_name (("ptrace (PT_KILL)")); 2158 1.11 christos 2159 1.11 christos int status; 2160 1.12 christos gdb::waitpid (pid, &status, 0); 2161 1.11 christos 2162 1.11 christos target_mourn_inferior (inferior_ptid); 2163 1.11 christos } 2164 1.11 christos 2165 1.11 christos void 2166 1.11 christos fbsd_nat_target::mourn_inferior () 2167 1.11 christos { 2168 1.11 christos gdb_assert (!have_pending_event (ptid_t (current_inferior ()->pid))); 2169 1.11 christos inf_ptrace_target::mourn_inferior (); 2170 1.11 christos } 2171 1.11 christos 2172 1.11 christos void 2173 1.11 christos fbsd_nat_target::follow_exec (inferior *follow_inf, ptid_t ptid, 2174 1.11 christos const char *execd_pathname) 2175 1.11 christos { 2176 1.11 christos inferior *orig_inf = current_inferior (); 2177 1.11 christos 2178 1.11 christos inf_ptrace_target::follow_exec (follow_inf, ptid, execd_pathname); 2179 1.11 christos 2180 1.11 christos if (orig_inf != follow_inf) 2181 1.11 christos { 2182 1.11 christos /* Migrate the fbsd_inferior to the new inferior. */ 2183 1.11 christos follow_inf->priv.reset (orig_inf->priv.release ()); 2184 1.11 christos } 2185 1.11 christos } 2186 1.11 christos 2187 1.5 christos #ifdef TDP_RFPPWAIT 2188 1.5 christos /* Target hook for follow_fork. On entry and at return inferior_ptid is 2189 1.5 christos the ptid of the followed inferior. */ 2190 1.5 christos 2191 1.10 christos void 2192 1.10 christos fbsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid, 2193 1.10 christos target_waitkind fork_kind, bool follow_child, 2194 1.10 christos bool detach_fork) 2195 1.5 christos { 2196 1.10 christos inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind, 2197 1.10 christos follow_child, detach_fork); 2198 1.10 christos 2199 1.11 christos if (child_inf != nullptr) 2200 1.11 christos { 2201 1.11 christos fbsd_inferior *fbsd_inf = new fbsd_inferior; 2202 1.11 christos child_inf->priv.reset (fbsd_inf); 2203 1.11 christos fbsd_inf->num_lwps = 1; 2204 1.11 christos } 2205 1.11 christos 2206 1.6 christos if (!follow_child && detach_fork) 2207 1.5 christos { 2208 1.10 christos pid_t child_pid = child_ptid.pid (); 2209 1.5 christos 2210 1.5 christos /* Breakpoints have already been detached from the child by 2211 1.5 christos infrun.c. */ 2212 1.5 christos 2213 1.11 christos if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3) 1, 0) == -1) 2214 1.10 christos perror_with_name (("ptrace (PT_DETACH)")); 2215 1.6 christos 2216 1.6 christos #ifndef PTRACE_VFORK 2217 1.10 christos if (fork_kind () == TARGET_WAITKIND_VFORKED) 2218 1.6 christos { 2219 1.6 christos /* We can't insert breakpoints until the child process has 2220 1.6 christos finished with the shared memory region. The parent 2221 1.6 christos process doesn't wait for the child process to exit or 2222 1.6 christos exec until after it has been resumed from the ptrace stop 2223 1.6 christos to report the fork. Once it has been resumed it doesn't 2224 1.6 christos stop again before returning to userland, so there is no 2225 1.6 christos reliable way to wait on the parent. 2226 1.6 christos 2227 1.6 christos We can't stay attached to the child to wait for an exec 2228 1.6 christos or exit because it may invoke ptrace(PT_TRACE_ME) 2229 1.6 christos (e.g. if the parent process is a debugger forking a new 2230 1.6 christos child process). 2231 1.6 christos 2232 1.6 christos In the end, the best we can do is to make sure it runs 2233 1.6 christos for a little while. Hopefully it will be out of range of 2234 1.6 christos any breakpoints we reinsert. Usually this is only the 2235 1.6 christos single-step breakpoint at vfork's return point. */ 2236 1.6 christos 2237 1.6 christos usleep (10000); 2238 1.6 christos 2239 1.6 christos /* Schedule a fake VFORK_DONE event to report on the next 2240 1.6 christos wait. */ 2241 1.6 christos fbsd_add_vfork_done (inferior_ptid); 2242 1.6 christos } 2243 1.6 christos #endif 2244 1.5 christos } 2245 1.5 christos } 2246 1.5 christos 2247 1.8 christos int 2248 1.8 christos fbsd_nat_target::insert_fork_catchpoint (int pid) 2249 1.5 christos { 2250 1.5 christos return 0; 2251 1.5 christos } 2252 1.5 christos 2253 1.8 christos int 2254 1.8 christos fbsd_nat_target::remove_fork_catchpoint (int pid) 2255 1.5 christos { 2256 1.5 christos return 0; 2257 1.5 christos } 2258 1.5 christos 2259 1.8 christos int 2260 1.8 christos fbsd_nat_target::insert_vfork_catchpoint (int pid) 2261 1.5 christos { 2262 1.5 christos return 0; 2263 1.5 christos } 2264 1.5 christos 2265 1.8 christos int 2266 1.8 christos fbsd_nat_target::remove_vfork_catchpoint (int pid) 2267 1.5 christos { 2268 1.5 christos return 0; 2269 1.5 christos } 2270 1.6 christos #endif 2271 1.5 christos 2272 1.10 christos /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ 2273 1.5 christos 2274 1.8 christos void 2275 1.8 christos fbsd_nat_target::post_startup_inferior (ptid_t pid) 2276 1.5 christos { 2277 1.8 christos fbsd_enable_proc_events (pid.pid ()); 2278 1.5 christos } 2279 1.5 christos 2280 1.8 christos /* Implement the "post_attach" target_ops method. */ 2281 1.5 christos 2282 1.8 christos void 2283 1.8 christos fbsd_nat_target::post_attach (int pid) 2284 1.5 christos { 2285 1.6 christos fbsd_enable_proc_events (pid); 2286 1.9 christos fbsd_add_threads (this, pid); 2287 1.5 christos } 2288 1.5 christos 2289 1.10 christos /* Traced processes always stop after exec. */ 2290 1.5 christos 2291 1.8 christos int 2292 1.8 christos fbsd_nat_target::insert_exec_catchpoint (int pid) 2293 1.5 christos { 2294 1.5 christos return 0; 2295 1.5 christos } 2296 1.5 christos 2297 1.8 christos int 2298 1.8 christos fbsd_nat_target::remove_exec_catchpoint (int pid) 2299 1.5 christos { 2300 1.5 christos return 0; 2301 1.5 christos } 2302 1.6 christos 2303 1.6 christos #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE 2304 1.8 christos int 2305 1.8 christos fbsd_nat_target::set_syscall_catchpoint (int pid, bool needed, 2306 1.8 christos int any_count, 2307 1.8 christos gdb::array_view<const int> syscall_counts) 2308 1.6 christos { 2309 1.6 christos 2310 1.6 christos /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which 2311 1.6 christos will catch all system call entries and exits. The system calls 2312 1.6 christos are filtered by GDB rather than the kernel. */ 2313 1.6 christos return 0; 2314 1.6 christos } 2315 1.6 christos #endif 2316 1.5 christos 2317 1.9 christos bool 2318 1.9 christos fbsd_nat_target::supports_multi_process () 2319 1.9 christos { 2320 1.9 christos return true; 2321 1.9 christos } 2322 1.9 christos 2323 1.10 christos bool 2324 1.10 christos fbsd_nat_target::supports_disable_randomization () 2325 1.10 christos { 2326 1.10 christos #ifdef PROC_ASLR_CTL 2327 1.10 christos return true; 2328 1.10 christos #else 2329 1.10 christos return false; 2330 1.10 christos #endif 2331 1.10 christos } 2332 1.10 christos 2333 1.10 christos /* See fbsd-nat.h. */ 2334 1.10 christos 2335 1.10 christos bool 2336 1.10 christos fbsd_nat_target::fetch_register_set (struct regcache *regcache, int regnum, 2337 1.10 christos int fetch_op, const struct regset *regset, 2338 1.10 christos int regbase, void *regs, size_t size) 2339 1.10 christos { 2340 1.10 christos const struct regcache_map_entry *map 2341 1.10 christos = (const struct regcache_map_entry *) regset->regmap; 2342 1.10 christos pid_t pid = get_ptrace_pid (regcache->ptid ()); 2343 1.10 christos 2344 1.10 christos if (regnum == -1 2345 1.10 christos || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase, 2346 1.11 christos regcache->arch (), size))) 2347 1.10 christos { 2348 1.10 christos if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1) 2349 1.10 christos perror_with_name (_("Couldn't get registers")); 2350 1.10 christos 2351 1.10 christos regset->supply_regset (regset, regcache, regnum, regs, size); 2352 1.10 christos return true; 2353 1.10 christos } 2354 1.10 christos return false; 2355 1.10 christos } 2356 1.10 christos 2357 1.10 christos /* See fbsd-nat.h. */ 2358 1.10 christos 2359 1.10 christos bool 2360 1.10 christos fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum, 2361 1.10 christos int fetch_op, int store_op, 2362 1.10 christos const struct regset *regset, int regbase, 2363 1.10 christos void *regs, size_t size) 2364 1.10 christos { 2365 1.10 christos const struct regcache_map_entry *map 2366 1.10 christos = (const struct regcache_map_entry *) regset->regmap; 2367 1.10 christos pid_t pid = get_ptrace_pid (regcache->ptid ()); 2368 1.10 christos 2369 1.10 christos if (regnum == -1 2370 1.10 christos || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase, 2371 1.11 christos regcache->arch (), size))) 2372 1.10 christos { 2373 1.10 christos if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1) 2374 1.10 christos perror_with_name (_("Couldn't get registers")); 2375 1.10 christos 2376 1.10 christos regset->collect_regset (regset, regcache, regnum, regs, size); 2377 1.10 christos 2378 1.10 christos if (ptrace (store_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1) 2379 1.10 christos perror_with_name (_("Couldn't write registers")); 2380 1.10 christos return true; 2381 1.10 christos } 2382 1.10 christos return false; 2383 1.10 christos } 2384 1.10 christos 2385 1.10 christos /* See fbsd-nat.h. */ 2386 1.10 christos 2387 1.10 christos size_t 2388 1.10 christos fbsd_nat_target::have_regset (ptid_t ptid, int note) 2389 1.10 christos { 2390 1.10 christos pid_t pid = get_ptrace_pid (ptid); 2391 1.10 christos struct iovec iov; 2392 1.10 christos 2393 1.10 christos iov.iov_base = nullptr; 2394 1.10 christos iov.iov_len = 0; 2395 1.10 christos if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1) 2396 1.10 christos return 0; 2397 1.10 christos return iov.iov_len; 2398 1.10 christos } 2399 1.10 christos 2400 1.10 christos /* See fbsd-nat.h. */ 2401 1.10 christos 2402 1.10 christos bool 2403 1.10 christos fbsd_nat_target::fetch_regset (struct regcache *regcache, int regnum, int note, 2404 1.10 christos const struct regset *regset, int regbase, 2405 1.10 christos void *regs, size_t size) 2406 1.10 christos { 2407 1.10 christos const struct regcache_map_entry *map 2408 1.10 christos = (const struct regcache_map_entry *) regset->regmap; 2409 1.10 christos pid_t pid = get_ptrace_pid (regcache->ptid ()); 2410 1.10 christos 2411 1.10 christos if (regnum == -1 2412 1.10 christos || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase, 2413 1.11 christos regcache->arch (), size))) 2414 1.10 christos { 2415 1.10 christos struct iovec iov; 2416 1.10 christos 2417 1.10 christos iov.iov_base = regs; 2418 1.10 christos iov.iov_len = size; 2419 1.10 christos if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1) 2420 1.10 christos perror_with_name (_("Couldn't get registers")); 2421 1.10 christos 2422 1.10 christos regset->supply_regset (regset, regcache, regnum, regs, size); 2423 1.10 christos return true; 2424 1.10 christos } 2425 1.10 christos return false; 2426 1.10 christos } 2427 1.10 christos 2428 1.10 christos bool 2429 1.10 christos fbsd_nat_target::store_regset (struct regcache *regcache, int regnum, int note, 2430 1.10 christos const struct regset *regset, int regbase, 2431 1.10 christos void *regs, size_t size) 2432 1.10 christos { 2433 1.10 christos const struct regcache_map_entry *map 2434 1.10 christos = (const struct regcache_map_entry *) regset->regmap; 2435 1.10 christos pid_t pid = get_ptrace_pid (regcache->ptid ()); 2436 1.10 christos 2437 1.10 christos if (regnum == -1 2438 1.10 christos || (regnum >= regbase && regcache_map_supplies (map, regnum - regbase, 2439 1.11 christos regcache->arch (), size))) 2440 1.10 christos { 2441 1.10 christos struct iovec iov; 2442 1.10 christos 2443 1.10 christos iov.iov_base = regs; 2444 1.10 christos iov.iov_len = size; 2445 1.10 christos if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1) 2446 1.10 christos perror_with_name (_("Couldn't get registers")); 2447 1.10 christos 2448 1.10 christos regset->collect_regset (regset, regcache, regnum, regs, size); 2449 1.10 christos 2450 1.10 christos if (ptrace (PT_SETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1) 2451 1.10 christos perror_with_name (_("Couldn't write registers")); 2452 1.10 christos return true; 2453 1.10 christos } 2454 1.10 christos return false; 2455 1.10 christos } 2456 1.10 christos 2457 1.10 christos /* See fbsd-nat.h. */ 2458 1.10 christos 2459 1.10 christos bool 2460 1.10 christos fbsd_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo) 2461 1.10 christos { 2462 1.10 christos struct ptrace_lwpinfo pl; 2463 1.10 christos pid_t pid = get_ptrace_pid (ptid); 2464 1.10 christos 2465 1.10 christos if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1) 2466 1.10 christos return false; 2467 1.10 christos if (!(pl.pl_flags & PL_FLAG_SI)) 2468 1.10 christos return false;; 2469 1.10 christos *siginfo = pl.pl_siginfo; 2470 1.10 christos return (true); 2471 1.10 christos } 2472 1.10 christos 2473 1.9 christos void _initialize_fbsd_nat (); 2474 1.5 christos void 2475 1.9 christos _initialize_fbsd_nat () 2476 1.6 christos { 2477 1.6 christos add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance, 2478 1.6 christos &debug_fbsd_lwp, _("\ 2479 1.6 christos Set debugging of FreeBSD lwp module."), _("\ 2480 1.6 christos Show debugging of FreeBSD lwp module."), _("\ 2481 1.6 christos Enables printf debugging output."), 2482 1.6 christos NULL, 2483 1.6 christos &show_fbsd_lwp_debug, 2484 1.6 christos &setdebuglist, &showdebuglist); 2485 1.8 christos add_setshow_boolean_cmd ("fbsd-nat", class_maintenance, 2486 1.8 christos &debug_fbsd_nat, _("\ 2487 1.8 christos Set debugging of FreeBSD native target."), _("\ 2488 1.8 christos Show debugging of FreeBSD native target."), _("\ 2489 1.8 christos Enables printf debugging output."), 2490 1.8 christos NULL, 2491 1.8 christos &show_fbsd_nat_debug, 2492 1.8 christos &setdebuglist, &showdebuglist); 2493 1.10 christos 2494 1.10 christos /* Install a SIGCHLD handler. */ 2495 1.10 christos signal (SIGCHLD, sigchld_handler); 2496 1.6 christos } 2497