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: releng/10.3/usr.bin/gcore/elfcore.c 283909 2015-06-02 13:07:22Z jhb $"); 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 64typedef void (*segment_callback)(vm_map_entry_t, void *); 65 66/* Closure for cb_put_phdr(). */ 67struct 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(). */ 73struct 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 79typedef struct fpreg32 elfcore_fpregset_t; 80typedef struct reg32 elfcore_gregset_t; 81typedef struct prpsinfo32 elfcore_prpsinfo_t; 82typedef struct prstatus32 elfcore_prstatus_t; 83static void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs); 84static void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs); 85#else 86typedef fpregset_t elfcore_fpregset_t; 87typedef gregset_t elfcore_gregset_t; 88typedef prpsinfo_t elfcore_prpsinfo_t; 89typedef 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 94typedef void* (*notefunc_t)(void *, size_t *); 95 96static void cb_put_phdr(vm_map_entry_t, void *); 97static void cb_size_segment(vm_map_entry_t, void *); 98static void each_writable_segment(vm_map_entry_t, segment_callback, 99 void *closure); 100static void elf_detach(void); /* atexit() handler. */ 101static void *elf_note_fpregset(void *, size_t *); 102static void *elf_note_prpsinfo(void *, size_t *); 103static void *elf_note_prstatus(void *, size_t *); 104static void *elf_note_thrmisc(void *, size_t *); 105#if defined(__i386__) || defined(__amd64__) 106static void *elf_note_x86_xstate(void *, size_t *); 107#endif 108static void *elf_note_procstat_auxv(void *, size_t *); 109static void *elf_note_procstat_files(void *, size_t *); 110static void *elf_note_procstat_groups(void *, size_t *); 111static void *elf_note_procstat_osrel(void *, size_t *); 112static void *elf_note_procstat_proc(void *, size_t *); 113static void *elf_note_procstat_psstrings(void *, size_t *); 114static void *elf_note_procstat_rlimit(void *, size_t *); 115static void *elf_note_procstat_umask(void *, size_t *); 116static void *elf_note_procstat_vmmap(void *, size_t *); 117static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t, 118 int); 119static void elf_putnote(int, notefunc_t, void *, struct sbuf *); 120static void elf_putnotes(pid_t, struct sbuf *, size_t *); 121static void freemap(vm_map_entry_t); 122static vm_map_entry_t readmap(pid_t); 123static void *procstat_sysctl(void *, int, size_t, size_t *sizep); 124 125static pid_t g_pid; /* Pid being dumped, global for elf_detach */ 126 127static int 128elf_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 156static void 157elf_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 */ 167static void 168elf_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 */ 268static void 269cb_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 */ 298static void 299cb_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 */ 312static void 313each_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 321static void 322elf_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 */ 376static void 377elf_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 */ 404static void 405elf_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 */ 462static void 463freemap(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 */ 479static vm_map_entry_t 480readmap(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 kve->kve_type != KVME_TYPE_PHYS)) 510 continue; 511 512 ent = calloc(1, sizeof(*ent)); 513 if (ent == NULL) 514 errx(1, "out of memory"); 515 ent->start = (vm_offset_t)kve->kve_start; 516 ent->end = (vm_offset_t)kve->kve_end; 517 ent->protection = VM_PROT_READ | VM_PROT_WRITE; 518 if ((kve->kve_protection & KVME_PROT_EXEC) != 0) 519 ent->protection |= VM_PROT_EXECUTE; 520 521 *linkp = ent; 522 linkp = &ent->next; 523 } 524 free(vmentl); 525 return (map); 526} 527 528/* 529 * Miscellaneous note out functions. 530 */ 531 532static void * 533elf_note_prpsinfo(void *arg, size_t *sizep) 534{ 535 pid_t pid; 536 elfcore_prpsinfo_t *psinfo; 537 struct kinfo_proc kip; 538 size_t len; 539 int name[4]; 540 541 pid = *(pid_t *)arg; 542 psinfo = calloc(1, sizeof(*psinfo)); 543 if (psinfo == NULL) 544 errx(1, "out of memory"); 545 psinfo->pr_version = PRPSINFO_VERSION; 546 psinfo->pr_psinfosz = sizeof(*psinfo); 547 548 name[0] = CTL_KERN; 549 name[1] = KERN_PROC; 550 name[2] = KERN_PROC_PID; 551 name[3] = pid; 552 len = sizeof(kip); 553 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1) 554 err(1, "kern.proc.pid.%u", pid); 555 if (kip.ki_pid != pid) 556 err(1, "kern.proc.pid.%u", pid); 557 strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN); 558 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ); 559 560 *sizep = sizeof(*psinfo); 561 return (psinfo); 562} 563 564static void * 565elf_note_prstatus(void *arg, size_t *sizep) 566{ 567 lwpid_t tid; 568 elfcore_prstatus_t *status; 569 struct reg greg; 570 571 tid = *(lwpid_t *)arg; 572 status = calloc(1, sizeof(*status)); 573 if (status == NULL) 574 errx(1, "out of memory"); 575 status->pr_version = PRSTATUS_VERSION; 576 status->pr_statussz = sizeof(*status); 577 status->pr_gregsetsz = sizeof(elfcore_gregset_t); 578 status->pr_fpregsetsz = sizeof(elfcore_fpregset_t); 579 status->pr_osreldate = __FreeBSD_version; 580 status->pr_pid = tid; 581 ptrace(PT_GETREGS, tid, (void *)&greg, 0); 582 elf_convert_gregset(&status->pr_reg, &greg); 583 584 *sizep = sizeof(*status); 585 return (status); 586} 587 588static void * 589elf_note_fpregset(void *arg, size_t *sizep) 590{ 591 lwpid_t tid; 592 elfcore_fpregset_t *fpregset; 593 fpregset_t fpreg; 594 595 tid = *(lwpid_t *)arg; 596 fpregset = calloc(1, sizeof(*fpregset)); 597 if (fpregset == NULL) 598 errx(1, "out of memory"); 599 ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0); 600 elf_convert_fpregset(fpregset, &fpreg); 601 602 *sizep = sizeof(*fpregset); 603 return (fpregset); 604} 605 606static void * 607elf_note_thrmisc(void *arg, size_t *sizep) 608{ 609 lwpid_t tid; 610 struct ptrace_lwpinfo lwpinfo; 611 thrmisc_t *thrmisc; 612 613 tid = *(lwpid_t *)arg; 614 thrmisc = calloc(1, sizeof(*thrmisc)); 615 if (thrmisc == NULL) 616 errx(1, "out of memory"); 617 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, 618 sizeof(lwpinfo)); 619 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad)); 620 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname); 621 622 *sizep = sizeof(*thrmisc); 623 return (thrmisc); 624} 625 626#if defined(__i386__) || defined(__amd64__) 627static void * 628elf_note_x86_xstate(void *arg, size_t *sizep) 629{ 630 lwpid_t tid; 631 char *xstate; 632 static bool xsave_checked = false; 633 static struct ptrace_xstate_info info; 634 635 tid = *(lwpid_t *)arg; 636 if (!xsave_checked) { 637 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info, 638 sizeof(info)) != 0) 639 info.xsave_len = 0; 640 xsave_checked = true; 641 } 642 if (info.xsave_len == 0) { 643 *sizep = 0; 644 return (NULL); 645 } 646 xstate = calloc(1, info.xsave_len); 647 ptrace(PT_GETXSTATE, tid, xstate, 0); 648 *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask; 649 *sizep = info.xsave_len; 650 return (xstate); 651} 652#endif 653 654static void * 655procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep) 656{ 657 size_t len, oldlen; 658 pid_t pid; 659 int name[4], structsize; 660 void *buf, *p; 661 662 pid = *(pid_t *)arg; 663 structsize = structsz; 664 name[0] = CTL_KERN; 665 name[1] = KERN_PROC; 666 name[2] = what; 667 name[3] = pid; 668 len = 0; 669 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1) 670 err(1, "kern.proc.%d.%u", what, pid); 671 buf = calloc(1, sizeof(structsize) + len * 4 / 3); 672 if (buf == NULL) 673 errx(1, "out of memory"); 674 bcopy(&structsize, buf, sizeof(structsize)); 675 p = (char *)buf + sizeof(structsize); 676 if (sysctl(name, 4, p, &len, NULL, 0) == -1) 677 err(1, "kern.proc.%d.%u", what, pid); 678 679 *sizep = sizeof(structsize) + len; 680 return (buf); 681} 682 683static void * 684elf_note_procstat_proc(void *arg, size_t *sizep) 685{ 686 687 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD, 688 sizeof(struct kinfo_proc), sizep)); 689} 690 691static void * 692elf_note_procstat_files(void *arg, size_t *sizep) 693{ 694 695 return (procstat_sysctl(arg, KERN_PROC_FILEDESC, 696 sizeof(struct kinfo_file), sizep)); 697} 698 699static void * 700elf_note_procstat_vmmap(void *arg, size_t *sizep) 701{ 702 703 return (procstat_sysctl(arg, KERN_PROC_VMMAP, 704 sizeof(struct kinfo_vmentry), sizep)); 705} 706 707static void * 708elf_note_procstat_groups(void *arg, size_t *sizep) 709{ 710 711 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep)); 712} 713 714static void * 715elf_note_procstat_umask(void *arg, size_t *sizep) 716{ 717 718 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep)); 719} 720 721static void * 722elf_note_procstat_osrel(void *arg, size_t *sizep) 723{ 724 725 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep)); 726} 727 728static void * 729elf_note_procstat_psstrings(void *arg, size_t *sizep) 730{ 731 732 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS, 733 sizeof(vm_offset_t), sizep)); 734} 735 736static void * 737elf_note_procstat_auxv(void *arg, size_t *sizep) 738{ 739 740 return (procstat_sysctl(arg, KERN_PROC_AUXV, 741 sizeof(Elf_Auxinfo), sizep)); 742} 743 744static void * 745elf_note_procstat_rlimit(void *arg, size_t *sizep) 746{ 747 pid_t pid; 748 size_t len; 749 int i, name[5], structsize; 750 void *buf, *p; 751 752 pid = *(pid_t *)arg; 753 structsize = sizeof(struct rlimit) * RLIM_NLIMITS; 754 buf = calloc(1, sizeof(structsize) + structsize); 755 if (buf == NULL) 756 errx(1, "out of memory"); 757 bcopy(&structsize, buf, sizeof(structsize)); 758 p = (char *)buf + sizeof(structsize); 759 name[0] = CTL_KERN; 760 name[1] = KERN_PROC; 761 name[2] = KERN_PROC_RLIMIT; 762 name[3] = pid; 763 len = sizeof(struct rlimit); 764 for (i = 0; i < RLIM_NLIMITS; i++) { 765 name[4] = i; 766 if (sysctl(name, 5, p, &len, NULL, 0) == -1) 767 err(1, "kern.proc.rlimit.%u", pid); 768 if (len != sizeof(struct rlimit)) 769 errx(1, "kern.proc.rlimit.%u: short read", pid); 770 p += len; 771 } 772 773 *sizep = sizeof(structsize) + structsize; 774 return (buf); 775} 776 777struct dumpers __elfN(dump) = { elf_ident, elf_coredump }; 778TEXT_SET(dumpset, __elfN(dump)); 779