1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2017 Dell EMC 5 * Copyright (c) 2007 Sandvine Incorporated 6 * Copyright (c) 1998 John D. Polstra 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/endian.h> 35 #include <sys/param.h> 36 #include <sys/procfs.h> 37 #include <sys/ptrace.h> 38 #include <sys/queue.h> 39 #include <sys/linker_set.h> 40 #include <sys/sbuf.h> 41 #include <sys/sysctl.h> 42 #include <sys/user.h> 43 #include <sys/wait.h> 44 #include <machine/elf.h> 45 #include <vm/vm_param.h> 46 #include <vm/vm.h> 47 #include <assert.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <stdbool.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 #include <libutil.h> 58 59 #include "extern.h" 60 61 /* 62 * Code for generating ELF core dumps. 63 */ 64 65 struct map_entry { 66 struct map_entry *next; 67 vm_offset_t start; 68 vm_offset_t end; 69 vm_prot_t protection; 70 }; 71 72 typedef void (*segment_callback)(struct map_entry *, void *); 73 74 /* Closure for cb_put_phdr(). */ 75 struct phdr_closure { 76 Elf_Phdr *phdr; /* Program header to fill in */ 77 Elf_Off offset; /* Offset of segment in core file */ 78 }; 79 80 /* Closure for cb_size_segment(). */ 81 struct sseg_closure { 82 int count; /* Count of writable segments. */ 83 size_t size; /* Total size of all writable segments. */ 84 }; 85 86 #ifdef ELFCORE_COMPAT_32 87 typedef struct fpreg32 elfcore_fpregset_t; 88 typedef struct reg32 elfcore_gregset_t; 89 typedef struct prpsinfo32 elfcore_prpsinfo_t; 90 typedef struct prstatus32 elfcore_prstatus_t; 91 typedef struct ptrace_lwpinfo32 elfcore_lwpinfo_t; 92 static void elf_convert_lwpinfo(struct ptrace_lwpinfo32 *pld, 93 struct ptrace_lwpinfo *pls); 94 #else 95 typedef fpregset_t elfcore_fpregset_t; 96 typedef gregset_t elfcore_gregset_t; 97 typedef prpsinfo_t elfcore_prpsinfo_t; 98 typedef prstatus_t elfcore_prstatus_t; 99 typedef struct ptrace_lwpinfo elfcore_lwpinfo_t; 100 #define elf_convert_lwpinfo(d,s) *d = *s 101 #endif 102 103 typedef void* (*notefunc_t)(void *, size_t *); 104 105 static void cb_put_phdr(struct map_entry *, void *); 106 static void cb_size_segment(struct map_entry *, void *); 107 static void each_dumpable_segment(struct map_entry *, segment_callback, 108 void *closure); 109 static void elf_detach(void); /* atexit() handler. */ 110 static void *elf_note_prpsinfo(void *, size_t *); 111 static void *elf_note_thrmisc(void *, size_t *); 112 static void *elf_note_ptlwpinfo(void *, size_t *); 113 #if defined(__arm__) 114 static void *elf_note_arm_vfp(void *, size_t *); 115 #endif 116 #if defined(__i386__) || defined(__amd64__) 117 static void *elf_note_x86_xstate(void *, size_t *); 118 #endif 119 #if defined(__powerpc__) 120 static void *elf_note_powerpc_vmx(void *, size_t *); 121 static void *elf_note_powerpc_vsx(void *, size_t *); 122 #endif 123 static void *elf_note_procstat_auxv(void *, size_t *); 124 static void *elf_note_procstat_files(void *, size_t *); 125 static void *elf_note_procstat_groups(void *, size_t *); 126 static void *elf_note_procstat_osrel(void *, size_t *); 127 static void *elf_note_procstat_proc(void *, size_t *); 128 static void *elf_note_procstat_psstrings(void *, size_t *); 129 static void *elf_note_procstat_rlimit(void *, size_t *); 130 static void *elf_note_procstat_umask(void *, size_t *); 131 static void *elf_note_procstat_vmmap(void *, size_t *); 132 static void elf_puthdr(int, pid_t, struct map_entry *, void *, size_t, size_t, 133 size_t, int); 134 static void elf_putnote(int, notefunc_t, void *, struct sbuf *); 135 static void elf_putnotes(pid_t, struct sbuf *, size_t *); 136 static void elf_putregnote(int, lwpid_t, struct sbuf *); 137 static void freemap(struct map_entry *); 138 static struct map_entry *readmap(pid_t); 139 static void *procstat_sysctl(void *, int, size_t, size_t *sizep); 140 141 static pid_t g_pid; /* Pid being dumped, global for elf_detach */ 142 static int g_status; /* proc status after ptrace attach */ 143 144 static int 145 elf_ident(int efd, pid_t pid __unused, char *binfile __unused) 146 { 147 Elf_Ehdr hdr; 148 int cnt; 149 uint16_t machine; 150 151 cnt = read(efd, &hdr, sizeof(hdr)); 152 if (cnt != sizeof(hdr)) 153 return (0); 154 if (!IS_ELF(hdr)) 155 return (0); 156 switch (hdr.e_ident[EI_DATA]) { 157 case ELFDATA2LSB: 158 machine = le16toh(hdr.e_machine); 159 break; 160 case ELFDATA2MSB: 161 machine = be16toh(hdr.e_machine); 162 break; 163 default: 164 return (0); 165 } 166 if (!ELF_MACHINE_OK(machine)) 167 return (0); 168 169 /* Looks good. */ 170 return (1); 171 } 172 173 static void 174 elf_detach(void) 175 { 176 int sig; 177 178 if (g_pid != 0) { 179 /* 180 * Forward any pending signals. SIGSTOP is generated by ptrace 181 * itself, so ignore it. 182 */ 183 sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0; 184 if (sig == SIGSTOP) 185 sig = 0; 186 ptrace(PT_DETACH, g_pid, (caddr_t)1, sig); 187 } 188 } 189 190 /* 191 * Write an ELF coredump for the given pid to the given fd. 192 */ 193 static void 194 elf_coredump(int efd, int fd, pid_t pid) 195 { 196 struct map_entry *map; 197 struct sseg_closure seginfo; 198 struct sbuf *sb; 199 void *hdr; 200 size_t hdrsize, notesz, segoff; 201 ssize_t n, old_len; 202 Elf_Phdr *php; 203 int i; 204 205 /* Attach to process to dump. */ 206 g_pid = pid; 207 if (atexit(elf_detach) != 0) 208 err(1, "atexit"); 209 errno = 0; 210 ptrace(PT_ATTACH, pid, NULL, 0); 211 if (errno) 212 err(1, "PT_ATTACH"); 213 if (waitpid(pid, &g_status, 0) == -1) 214 err(1, "waitpid"); 215 216 /* Get the program's memory map. */ 217 map = readmap(pid); 218 219 /* Size the program segments. */ 220 seginfo.count = 0; 221 seginfo.size = 0; 222 each_dumpable_segment(map, cb_size_segment, &seginfo); 223 224 /* 225 * Build the header and the notes using sbuf and write to the file. 226 */ 227 sb = sbuf_new_auto(); 228 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 229 if (seginfo.count + 1 >= PN_XNUM) 230 hdrsize += sizeof(Elf_Shdr); 231 /* Start header + notes section. */ 232 sbuf_start_section(sb, NULL); 233 /* Make empty header subsection. */ 234 sbuf_start_section(sb, &old_len); 235 sbuf_putc(sb, 0); 236 sbuf_end_section(sb, old_len, hdrsize, 0); 237 /* Put notes. */ 238 elf_putnotes(pid, sb, ¬esz); 239 /* Align up to a page boundary for the program segments. */ 240 sbuf_end_section(sb, -1, PAGE_SIZE, 0); 241 if (sbuf_finish(sb) != 0) 242 err(1, "sbuf_finish"); 243 hdr = sbuf_data(sb); 244 segoff = sbuf_len(sb); 245 /* Fill in the header. */ 246 elf_puthdr(efd, pid, map, hdr, hdrsize, notesz, segoff, seginfo.count); 247 248 n = write(fd, hdr, segoff); 249 if (n == -1) 250 err(1, "write"); 251 if (n < segoff) 252 errx(1, "short write"); 253 254 /* Write the contents of all of the writable segments. */ 255 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 256 for (i = 0; i < seginfo.count; i++) { 257 struct ptrace_io_desc iorequest; 258 uintmax_t nleft = php->p_filesz; 259 260 iorequest.piod_op = PIOD_READ_D; 261 iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr; 262 while (nleft > 0) { 263 char buf[8*1024]; 264 size_t nwant; 265 ssize_t ngot; 266 267 if (nleft > sizeof(buf)) 268 nwant = sizeof buf; 269 else 270 nwant = nleft; 271 iorequest.piod_addr = buf; 272 iorequest.piod_len = nwant; 273 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0); 274 ngot = iorequest.piod_len; 275 if ((size_t)ngot < nwant) 276 errx(1, "short read wanted %zu, got %zd", 277 nwant, ngot); 278 ngot = write(fd, buf, nwant); 279 if (ngot == -1) 280 err(1, "write of segment %d failed", i); 281 if ((size_t)ngot != nwant) 282 errx(1, "short write"); 283 nleft -= nwant; 284 iorequest.piod_offs += ngot; 285 } 286 php++; 287 } 288 sbuf_delete(sb); 289 freemap(map); 290 } 291 292 /* 293 * A callback for each_dumpable_segment() to write out the segment's 294 * program header entry. 295 */ 296 static void 297 cb_put_phdr(struct map_entry *entry, void *closure) 298 { 299 struct phdr_closure *phc = (struct phdr_closure *)closure; 300 Elf_Phdr *phdr = phc->phdr; 301 302 phc->offset = round_page(phc->offset); 303 304 phdr->p_type = PT_LOAD; 305 phdr->p_offset = phc->offset; 306 phdr->p_vaddr = entry->start; 307 phdr->p_paddr = 0; 308 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 309 phdr->p_align = PAGE_SIZE; 310 phdr->p_flags = 0; 311 if (entry->protection & VM_PROT_READ) 312 phdr->p_flags |= PF_R; 313 if (entry->protection & VM_PROT_WRITE) 314 phdr->p_flags |= PF_W; 315 if (entry->protection & VM_PROT_EXECUTE) 316 phdr->p_flags |= PF_X; 317 318 phc->offset += phdr->p_filesz; 319 phc->phdr++; 320 } 321 322 /* 323 * A callback for each_dumpable_segment() to gather information about 324 * the number of segments and their total size. 325 */ 326 static void 327 cb_size_segment(struct map_entry *entry, void *closure) 328 { 329 struct sseg_closure *ssc = (struct sseg_closure *)closure; 330 331 ssc->count++; 332 ssc->size += entry->end - entry->start; 333 } 334 335 /* 336 * For each segment in the given memory map, call the given function 337 * with a pointer to the map entry and some arbitrary caller-supplied 338 * data. 339 */ 340 static void 341 each_dumpable_segment(struct map_entry *map, segment_callback func, 342 void *closure) 343 { 344 struct map_entry *entry; 345 346 for (entry = map; entry != NULL; entry = entry->next) 347 (*func)(entry, closure); 348 } 349 350 static void 351 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep) 352 { 353 lwpid_t *tids; 354 size_t threads, old_len; 355 ssize_t size; 356 int i; 357 358 errno = 0; 359 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0); 360 if (errno) 361 err(1, "PT_GETNUMLWPS"); 362 tids = malloc(threads * sizeof(*tids)); 363 if (tids == NULL) 364 errx(1, "out of memory"); 365 errno = 0; 366 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads); 367 if (errno) 368 err(1, "PT_GETLWPLIST"); 369 370 sbuf_start_section(sb, &old_len); 371 elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb); 372 373 for (i = 0; i < threads; ++i) { 374 elf_putregnote(NT_PRSTATUS, tids[i], sb); 375 elf_putregnote(NT_FPREGSET, tids[i], sb); 376 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb); 377 elf_putnote(NT_PTLWPINFO, elf_note_ptlwpinfo, tids + i, sb); 378 #if defined(__arm__) 379 elf_putnote(NT_ARM_VFP, elf_note_arm_vfp, tids + i, sb); 380 #endif 381 #if defined(__i386__) || defined(__amd64__) 382 elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb); 383 #endif 384 #if defined(__powerpc__) 385 elf_putnote(NT_PPC_VMX, elf_note_powerpc_vmx, tids + i, sb); 386 #ifndef __SPE__ 387 elf_putnote(NT_PPC_VSX, elf_note_powerpc_vsx, tids + i, sb); 388 #endif 389 #endif 390 } 391 392 #ifndef ELFCORE_COMPAT_32 393 elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb); 394 elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb); 395 elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb); 396 elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb); 397 elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb); 398 elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb); 399 elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb); 400 elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid, 401 sb); 402 elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb); 403 #endif 404 405 size = sbuf_end_section(sb, old_len, 1, 0); 406 if (size == -1) 407 err(1, "sbuf_end_section"); 408 free(tids); 409 *sizep = size; 410 } 411 412 /* 413 * Emit one register set note section to sbuf. 414 */ 415 static void 416 elf_putregnote(int type, lwpid_t tid, struct sbuf *sb) 417 { 418 Elf_Note note; 419 struct iovec iov; 420 ssize_t old_len; 421 422 iov.iov_base = NULL; 423 iov.iov_len = 0; 424 if (ptrace(PT_GETREGSET, tid, (void *)&iov, type) != 0) 425 return; 426 iov.iov_base = calloc(1, iov.iov_len); 427 if (iov.iov_base == NULL) 428 errx(1, "out of memory"); 429 if (ptrace(PT_GETREGSET, tid, (void *)&iov, type) != 0) 430 errx(1, "failed to fetch register set %d", type); 431 432 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */ 433 note.n_descsz = iov.iov_len; 434 note.n_type = type; 435 436 sbuf_bcat(sb, ¬e, sizeof(note)); 437 sbuf_start_section(sb, &old_len); 438 sbuf_bcat(sb, "FreeBSD", note.n_namesz); 439 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 440 sbuf_start_section(sb, &old_len); 441 sbuf_bcat(sb, iov.iov_base, iov.iov_len); 442 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 443 free(iov.iov_base); 444 } 445 446 /* 447 * Emit one note section to sbuf. 448 */ 449 static void 450 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb) 451 { 452 Elf_Note note; 453 size_t descsz; 454 ssize_t old_len; 455 void *desc; 456 457 desc = notefunc(arg, &descsz); 458 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */ 459 note.n_descsz = descsz; 460 note.n_type = type; 461 462 sbuf_bcat(sb, ¬e, sizeof(note)); 463 sbuf_start_section(sb, &old_len); 464 sbuf_bcat(sb, "FreeBSD", note.n_namesz); 465 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 466 if (descsz == 0) 467 return; 468 sbuf_start_section(sb, &old_len); 469 sbuf_bcat(sb, desc, descsz); 470 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 471 free(desc); 472 } 473 474 /* 475 * Generate the ELF coredump header. 476 */ 477 static void 478 elf_puthdr(int efd, pid_t pid, struct map_entry *map, void *hdr, size_t hdrsize, 479 size_t notesz, size_t segoff, int numsegs) 480 { 481 Elf_Ehdr *ehdr, binhdr; 482 Elf_Phdr *phdr; 483 Elf_Shdr *shdr; 484 struct phdr_closure phc; 485 ssize_t cnt; 486 487 cnt = read(efd, &binhdr, sizeof(binhdr)); 488 if (cnt < 0) 489 err(1, "Failed to re-read ELF header"); 490 else if (cnt != sizeof(binhdr)) 491 errx(1, "Failed to re-read ELF header"); 492 493 ehdr = (Elf_Ehdr *)hdr; 494 495 ehdr->e_ident[EI_MAG0] = ELFMAG0; 496 ehdr->e_ident[EI_MAG1] = ELFMAG1; 497 ehdr->e_ident[EI_MAG2] = ELFMAG2; 498 ehdr->e_ident[EI_MAG3] = ELFMAG3; 499 ehdr->e_ident[EI_CLASS] = ELF_CLASS; 500 ehdr->e_ident[EI_DATA] = ELF_DATA; 501 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 502 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 503 ehdr->e_ident[EI_ABIVERSION] = 0; 504 ehdr->e_ident[EI_PAD] = 0; 505 ehdr->e_type = ET_CORE; 506 ehdr->e_machine = binhdr.e_machine; 507 ehdr->e_version = EV_CURRENT; 508 ehdr->e_entry = 0; 509 ehdr->e_phoff = sizeof(Elf_Ehdr); 510 ehdr->e_flags = binhdr.e_flags; 511 ehdr->e_ehsize = sizeof(Elf_Ehdr); 512 ehdr->e_phentsize = sizeof(Elf_Phdr); 513 ehdr->e_shentsize = sizeof(Elf_Shdr); 514 ehdr->e_shstrndx = SHN_UNDEF; 515 if (numsegs + 1 < PN_XNUM) { 516 ehdr->e_phnum = numsegs + 1; 517 ehdr->e_shnum = 0; 518 } else { 519 ehdr->e_phnum = PN_XNUM; 520 ehdr->e_shnum = 1; 521 522 ehdr->e_shoff = ehdr->e_phoff + 523 (numsegs + 1) * ehdr->e_phentsize; 524 525 shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff); 526 memset(shdr, 0, sizeof(*shdr)); 527 /* 528 * A special first section is used to hold large segment and 529 * section counts. This was proposed by Sun Microsystems in 530 * Solaris and has been adopted by Linux; the standard ELF 531 * tools are already familiar with the technique. 532 * 533 * See table 7-7 of the Solaris "Linker and Libraries Guide" 534 * (or 12-7 depending on the version of the document) for more 535 * details. 536 */ 537 shdr->sh_type = SHT_NULL; 538 shdr->sh_size = ehdr->e_shnum; 539 shdr->sh_link = ehdr->e_shstrndx; 540 shdr->sh_info = numsegs + 1; 541 } 542 543 /* 544 * Fill in the program header entries. 545 */ 546 phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff); 547 548 /* The note segment. */ 549 phdr->p_type = PT_NOTE; 550 phdr->p_offset = hdrsize; 551 phdr->p_vaddr = 0; 552 phdr->p_paddr = 0; 553 phdr->p_filesz = notesz; 554 phdr->p_memsz = 0; 555 phdr->p_flags = PF_R; 556 phdr->p_align = sizeof(Elf32_Size); 557 phdr++; 558 559 /* All the writable segments from the program. */ 560 phc.phdr = phdr; 561 phc.offset = segoff; 562 each_dumpable_segment(map, cb_put_phdr, &phc); 563 } 564 565 /* 566 * Free the memory map. 567 */ 568 static void 569 freemap(struct map_entry *map) 570 { 571 struct map_entry *next; 572 573 while (map != NULL) { 574 next = map->next; 575 free(map); 576 map = next; 577 } 578 } 579 580 /* 581 * Read the process's memory map using kinfo_getvmmap(), and return a list of 582 * VM map entries. Only the non-device read/writable segments are 583 * returned. The map entries in the list aren't fully filled in; only 584 * the items we need are present. 585 */ 586 static struct map_entry * 587 readmap(pid_t pid) 588 { 589 struct map_entry *ent, **linkp, *map; 590 struct kinfo_vmentry *vmentl, *kve; 591 int i, nitems; 592 593 vmentl = kinfo_getvmmap(pid, &nitems); 594 if (vmentl == NULL) 595 err(1, "cannot retrieve mappings for %u process", pid); 596 597 map = NULL; 598 linkp = ↦ 599 for (i = 0; i < nitems; i++) { 600 kve = &vmentl[i]; 601 602 /* 603 * Ignore 'malformed' segments or ones representing memory 604 * mapping with MAP_NOCORE on. 605 * If the 'full' support is disabled, just dump the most 606 * meaningful data segments. 607 */ 608 if ((kve->kve_protection & KVME_PROT_READ) == 0 || 609 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 || 610 kve->kve_type == KVME_TYPE_DEAD || 611 kve->kve_type == KVME_TYPE_UNKNOWN || 612 ((pflags & PFLAGS_FULL) == 0 && 613 kve->kve_type != KVME_TYPE_DEFAULT && 614 kve->kve_type != KVME_TYPE_VNODE && 615 kve->kve_type != KVME_TYPE_SWAP && 616 kve->kve_type != KVME_TYPE_PHYS)) 617 continue; 618 619 ent = calloc(1, sizeof(*ent)); 620 if (ent == NULL) 621 errx(1, "out of memory"); 622 ent->start = (vm_offset_t)kve->kve_start; 623 ent->end = (vm_offset_t)kve->kve_end; 624 ent->protection = VM_PROT_READ | VM_PROT_WRITE; 625 if ((kve->kve_protection & KVME_PROT_EXEC) != 0) 626 ent->protection |= VM_PROT_EXECUTE; 627 628 *linkp = ent; 629 linkp = &ent->next; 630 } 631 free(vmentl); 632 return (map); 633 } 634 635 /* 636 * Miscellaneous note out functions. 637 */ 638 639 static void * 640 elf_note_prpsinfo(void *arg, size_t *sizep) 641 { 642 char *cp, *end; 643 pid_t pid; 644 elfcore_prpsinfo_t *psinfo; 645 struct kinfo_proc kip; 646 size_t len; 647 int name[4]; 648 649 pid = *(pid_t *)arg; 650 psinfo = calloc(1, sizeof(*psinfo)); 651 if (psinfo == NULL) 652 errx(1, "out of memory"); 653 psinfo->pr_version = PRPSINFO_VERSION; 654 psinfo->pr_psinfosz = sizeof(*psinfo); 655 656 name[0] = CTL_KERN; 657 name[1] = KERN_PROC; 658 name[2] = KERN_PROC_PID; 659 name[3] = pid; 660 len = sizeof(kip); 661 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1) 662 err(1, "kern.proc.pid.%u", pid); 663 if (kip.ki_pid != pid) 664 err(1, "kern.proc.pid.%u", pid); 665 strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname)); 666 name[2] = KERN_PROC_ARGS; 667 len = sizeof(psinfo->pr_psargs) - 1; 668 if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) { 669 cp = psinfo->pr_psargs; 670 end = cp + len - 1; 671 for (;;) { 672 cp = memchr(cp, '\0', end - cp); 673 if (cp == NULL) 674 break; 675 *cp = ' '; 676 } 677 } else 678 strlcpy(psinfo->pr_psargs, kip.ki_comm, 679 sizeof(psinfo->pr_psargs)); 680 psinfo->pr_pid = pid; 681 682 *sizep = sizeof(*psinfo); 683 return (psinfo); 684 } 685 686 static void * 687 elf_note_thrmisc(void *arg, size_t *sizep) 688 { 689 lwpid_t tid; 690 struct ptrace_lwpinfo lwpinfo; 691 thrmisc_t *thrmisc; 692 693 tid = *(lwpid_t *)arg; 694 thrmisc = calloc(1, sizeof(*thrmisc)); 695 if (thrmisc == NULL) 696 errx(1, "out of memory"); 697 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, 698 sizeof(lwpinfo)); 699 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad)); 700 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname); 701 702 *sizep = sizeof(*thrmisc); 703 return (thrmisc); 704 } 705 706 static void * 707 elf_note_ptlwpinfo(void *arg, size_t *sizep) 708 { 709 lwpid_t tid; 710 elfcore_lwpinfo_t *elf_info; 711 struct ptrace_lwpinfo lwpinfo; 712 void *p; 713 714 tid = *(lwpid_t *)arg; 715 p = calloc(1, sizeof(int) + sizeof(elfcore_lwpinfo_t)); 716 if (p == NULL) 717 errx(1, "out of memory"); 718 *(int *)p = sizeof(elfcore_lwpinfo_t); 719 elf_info = (void *)((int *)p + 1); 720 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, sizeof(lwpinfo)); 721 elf_convert_lwpinfo(elf_info, &lwpinfo); 722 723 *sizep = sizeof(int) + sizeof(struct ptrace_lwpinfo); 724 return (p); 725 } 726 727 #if defined(__arm__) 728 static void * 729 elf_note_arm_vfp(void *arg, size_t *sizep) 730 { 731 lwpid_t tid; 732 struct vfpreg *vfp; 733 static bool has_vfp = true; 734 struct vfpreg info; 735 736 tid = *(lwpid_t *)arg; 737 if (has_vfp) { 738 if (ptrace(PT_GETVFPREGS, tid, (void *)&info, 0) != 0) 739 has_vfp = false; 740 } 741 if (!has_vfp) { 742 *sizep = 0; 743 return (NULL); 744 } 745 vfp = calloc(1, sizeof(*vfp)); 746 memcpy(vfp, &info, sizeof(*vfp)); 747 *sizep = sizeof(*vfp); 748 return (vfp); 749 } 750 #endif 751 752 #if defined(__i386__) || defined(__amd64__) 753 static void * 754 elf_note_x86_xstate(void *arg, size_t *sizep) 755 { 756 lwpid_t tid; 757 char *xstate; 758 static bool xsave_checked = false; 759 static struct ptrace_xstate_info info; 760 761 tid = *(lwpid_t *)arg; 762 if (!xsave_checked) { 763 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info, 764 sizeof(info)) != 0) 765 info.xsave_len = 0; 766 xsave_checked = true; 767 } 768 if (info.xsave_len == 0) { 769 *sizep = 0; 770 return (NULL); 771 } 772 xstate = calloc(1, info.xsave_len); 773 ptrace(PT_GETXSTATE, tid, xstate, 0); 774 *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask; 775 *sizep = info.xsave_len; 776 return (xstate); 777 } 778 #endif 779 780 #if defined(__powerpc__) 781 static void * 782 elf_note_powerpc_vmx(void *arg, size_t *sizep) 783 { 784 lwpid_t tid; 785 struct vmxreg *vmx; 786 static bool has_vmx = true; 787 struct vmxreg info; 788 789 tid = *(lwpid_t *)arg; 790 if (has_vmx) { 791 if (ptrace(PT_GETVRREGS, tid, (void *)&info, 792 sizeof(info)) != 0) 793 has_vmx = false; 794 } 795 if (!has_vmx) { 796 *sizep = 0; 797 return (NULL); 798 } 799 vmx = calloc(1, sizeof(*vmx)); 800 memcpy(vmx, &info, sizeof(*vmx)); 801 *sizep = sizeof(*vmx); 802 return (vmx); 803 } 804 805 static void * 806 elf_note_powerpc_vsx(void *arg, size_t *sizep) 807 { 808 lwpid_t tid; 809 char *vshr_data; 810 static bool has_vsx = true; 811 uint64_t vshr[32]; 812 813 tid = *(lwpid_t *)arg; 814 if (has_vsx) { 815 if (ptrace(PT_GETVSRREGS, tid, (void *)vshr, 816 sizeof(vshr)) != 0) 817 has_vsx = false; 818 } 819 if (!has_vsx) { 820 *sizep = 0; 821 return (NULL); 822 } 823 vshr_data = calloc(1, sizeof(vshr)); 824 memcpy(vshr_data, vshr, sizeof(vshr)); 825 *sizep = sizeof(vshr); 826 return (vshr_data); 827 } 828 #endif 829 830 static void * 831 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep) 832 { 833 size_t len; 834 pid_t pid; 835 int name[4], structsize; 836 void *buf, *p; 837 838 pid = *(pid_t *)arg; 839 structsize = structsz; 840 name[0] = CTL_KERN; 841 name[1] = KERN_PROC; 842 name[2] = what; 843 name[3] = pid; 844 len = 0; 845 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1) 846 err(1, "kern.proc.%d.%u", what, pid); 847 buf = calloc(1, sizeof(structsize) + len * 4 / 3); 848 if (buf == NULL) 849 errx(1, "out of memory"); 850 bcopy(&structsize, buf, sizeof(structsize)); 851 p = (char *)buf + sizeof(structsize); 852 if (sysctl(name, 4, p, &len, NULL, 0) == -1) 853 err(1, "kern.proc.%d.%u", what, pid); 854 855 *sizep = sizeof(structsize) + len; 856 return (buf); 857 } 858 859 static void * 860 elf_note_procstat_proc(void *arg, size_t *sizep) 861 { 862 863 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD, 864 sizeof(struct kinfo_proc), sizep)); 865 } 866 867 static void * 868 elf_note_procstat_files(void *arg, size_t *sizep) 869 { 870 871 return (procstat_sysctl(arg, KERN_PROC_FILEDESC, 872 sizeof(struct kinfo_file), sizep)); 873 } 874 875 static void * 876 elf_note_procstat_vmmap(void *arg, size_t *sizep) 877 { 878 879 return (procstat_sysctl(arg, KERN_PROC_VMMAP, 880 sizeof(struct kinfo_vmentry), sizep)); 881 } 882 883 static void * 884 elf_note_procstat_groups(void *arg, size_t *sizep) 885 { 886 887 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep)); 888 } 889 890 static void * 891 elf_note_procstat_umask(void *arg, size_t *sizep) 892 { 893 894 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep)); 895 } 896 897 static void * 898 elf_note_procstat_osrel(void *arg, size_t *sizep) 899 { 900 901 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep)); 902 } 903 904 static void * 905 elf_note_procstat_psstrings(void *arg, size_t *sizep) 906 { 907 908 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS, 909 sizeof(vm_offset_t), sizep)); 910 } 911 912 static void * 913 elf_note_procstat_auxv(void *arg, size_t *sizep) 914 { 915 916 return (procstat_sysctl(arg, KERN_PROC_AUXV, 917 sizeof(Elf_Auxinfo), sizep)); 918 } 919 920 static void * 921 elf_note_procstat_rlimit(void *arg, size_t *sizep) 922 { 923 pid_t pid; 924 size_t len; 925 int i, name[5], structsize; 926 void *buf, *p; 927 928 pid = *(pid_t *)arg; 929 structsize = sizeof(struct rlimit) * RLIM_NLIMITS; 930 buf = calloc(1, sizeof(structsize) + structsize); 931 if (buf == NULL) 932 errx(1, "out of memory"); 933 bcopy(&structsize, buf, sizeof(structsize)); 934 p = (char *)buf + sizeof(structsize); 935 name[0] = CTL_KERN; 936 name[1] = KERN_PROC; 937 name[2] = KERN_PROC_RLIMIT; 938 name[3] = pid; 939 len = sizeof(struct rlimit); 940 for (i = 0; i < RLIM_NLIMITS; i++) { 941 name[4] = i; 942 if (sysctl(name, 5, p, &len, NULL, 0) == -1) 943 err(1, "kern.proc.rlimit.%u", pid); 944 if (len != sizeof(struct rlimit)) 945 errx(1, "kern.proc.rlimit.%u: short read", pid); 946 p += len; 947 } 948 949 *sizep = sizeof(structsize) + structsize; 950 return (buf); 951 } 952 953 struct dumpers __elfN(dump) = { elf_ident, elf_coredump }; 954 TEXT_SET(dumpset, __elfN(dump)); 955