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