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