140525Sjdp/*- 2199805Sattilio * Copyright (c) 2007 Sandvine Incorporated 340525Sjdp * Copyright (c) 1998 John D. Polstra 440525Sjdp * All rights reserved. 540525Sjdp * 640525Sjdp * Redistribution and use in source and binary forms, with or without 740525Sjdp * modification, are permitted provided that the following conditions 840525Sjdp * are met: 940525Sjdp * 1. Redistributions of source code must retain the above copyright 1040525Sjdp * notice, this list of conditions and the following disclaimer. 1140525Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1240525Sjdp * notice, this list of conditions and the following disclaimer in the 1340525Sjdp * documentation and/or other materials provided with the distribution. 1440525Sjdp * 1540525Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1640525Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1740525Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1840525Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1940525Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2040525Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2140525Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2240525Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2340525Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2440525Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2540525Sjdp * SUCH DAMAGE. 2640525Sjdp */ 2740525Sjdp 2893215Scharnier#include <sys/cdefs.h> 2993215Scharnier__FBSDID("$FreeBSD$"); 3093215Scharnier 3140525Sjdp#include <sys/param.h> 3240525Sjdp#include <sys/procfs.h> 33199805Sattilio#include <sys/ptrace.h> 34103302Speter#include <sys/queue.h> 35103299Speter#include <sys/linker_set.h> 36249687Strociny#include <sys/sbuf.h> 37199805Sattilio#include <sys/sysctl.h> 38199805Sattilio#include <sys/user.h> 39199805Sattilio#include <sys/wait.h> 4076224Sobrien#include <machine/elf.h> 4140525Sjdp#include <vm/vm_param.h> 4240525Sjdp#include <vm/vm.h> 4340525Sjdp#include <vm/pmap.h> 4440525Sjdp#include <vm/vm_map.h> 45249687Strociny#include <assert.h> 4640525Sjdp#include <err.h> 4740525Sjdp#include <errno.h> 4840525Sjdp#include <fcntl.h> 49102951Siedowse#include <stdint.h> 5040525Sjdp#include <stdio.h> 5140525Sjdp#include <stdlib.h> 5240525Sjdp#include <string.h> 5340525Sjdp#include <unistd.h> 54199805Sattilio#include <libutil.h> 5540525Sjdp 5640525Sjdp#include "extern.h" 5740525Sjdp 5840525Sjdp/* 5940525Sjdp * Code for generating ELF core dumps. 6040525Sjdp */ 6140525Sjdp 6240525Sjdptypedef void (*segment_callback)(vm_map_entry_t, void *); 6340525Sjdp 6440525Sjdp/* Closure for cb_put_phdr(). */ 6540525Sjdpstruct phdr_closure { 6640525Sjdp Elf_Phdr *phdr; /* Program header to fill in */ 6740525Sjdp Elf_Off offset; /* Offset of segment in core file */ 6840525Sjdp}; 6940525Sjdp 7040525Sjdp/* Closure for cb_size_segment(). */ 7140525Sjdpstruct sseg_closure { 7240525Sjdp int count; /* Count of writable segments. */ 7340525Sjdp size_t size; /* Total size of all writable segments. */ 7440525Sjdp}; 7540525Sjdp 76249687Strocinytypedef void* (*notefunc_t)(void *, size_t *); 77249687Strociny 7840525Sjdpstatic void cb_put_phdr(vm_map_entry_t, void *); 7940525Sjdpstatic void cb_size_segment(vm_map_entry_t, void *); 8040525Sjdpstatic void each_writable_segment(vm_map_entry_t, segment_callback, 8140525Sjdp void *closure); 82199805Sattiliostatic void elf_detach(void); /* atexit() handler. */ 83249687Strocinystatic void *elf_note_fpregset(void *, size_t *); 84249687Strocinystatic void *elf_note_prpsinfo(void *, size_t *); 85249687Strocinystatic void *elf_note_prstatus(void *, size_t *); 86249687Strocinystatic void *elf_note_thrmisc(void *, size_t *); 87249687Strocinystatic void *elf_note_procstat_auxv(void *, size_t *); 88249687Strocinystatic void *elf_note_procstat_files(void *, size_t *); 89249687Strocinystatic void *elf_note_procstat_groups(void *, size_t *); 90249687Strocinystatic void *elf_note_procstat_osrel(void *, size_t *); 91249687Strocinystatic void *elf_note_procstat_proc(void *, size_t *); 92249687Strocinystatic void *elf_note_procstat_psstrings(void *, size_t *); 93249687Strocinystatic void *elf_note_procstat_rlimit(void *, size_t *); 94249687Strocinystatic void *elf_note_procstat_umask(void *, size_t *); 95249687Strocinystatic void *elf_note_procstat_vmmap(void *, size_t *); 96249687Strocinystatic void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t, 97249687Strociny int); 98249687Strocinystatic void elf_putnote(int, notefunc_t, void *, struct sbuf *); 99249687Strocinystatic void elf_putnotes(pid_t, struct sbuf *, size_t *); 10040525Sjdpstatic void freemap(vm_map_entry_t); 10140525Sjdpstatic vm_map_entry_t readmap(pid_t); 102249687Strocinystatic void *procstat_sysctl(void *, int, size_t, size_t *sizep); 10340525Sjdp 104199805Sattiliostatic pid_t g_pid; /* Pid being dumped, global for elf_detach */ 105199805Sattilio 106103299Speterstatic int 107125859Sdwmaloneelf_ident(int efd, pid_t pid __unused, char *binfile __unused) 108103299Speter{ 109103299Speter Elf_Ehdr hdr; 110103299Speter int cnt; 111103299Speter 112103299Speter cnt = read(efd, &hdr, sizeof(hdr)); 113103299Speter if (cnt != sizeof(hdr)) 114103299Speter return (0); 115103299Speter if (IS_ELF(hdr)) 116103299Speter return (1); 117103299Speter return (0); 118103299Speter} 119103299Speter 120199805Sattiliostatic void 121199805Sattilioelf_detach(void) 122199805Sattilio{ 123199805Sattilio 124199805Sattilio if (g_pid != 0) 125199805Sattilio ptrace(PT_DETACH, g_pid, (caddr_t)1, 0); 126199805Sattilio} 127199805Sattilio 12840525Sjdp/* 12940525Sjdp * Write an ELF coredump for the given pid to the given fd. 13040525Sjdp */ 131125859Sdwmalonestatic void 132125859Sdwmaloneelf_coredump(int efd __unused, int fd, pid_t pid) 13340525Sjdp{ 13440525Sjdp vm_map_entry_t map; 13540525Sjdp struct sseg_closure seginfo; 136249687Strociny struct sbuf *sb; 13740525Sjdp void *hdr; 138249687Strociny size_t hdrsize, notesz, segoff; 139249687Strociny ssize_t n, old_len; 14040525Sjdp Elf_Phdr *php; 14140525Sjdp int i; 14240525Sjdp 143199805Sattilio /* Attach to process to dump. */ 144199805Sattilio g_pid = pid; 145199805Sattilio if (atexit(elf_detach) != 0) 146199805Sattilio err(1, "atexit"); 147199805Sattilio errno = 0; 148199805Sattilio ptrace(PT_ATTACH, pid, NULL, 0); 149199805Sattilio if (errno) 150199805Sattilio err(1, "PT_ATTACH"); 151199805Sattilio if (waitpid(pid, NULL, 0) == -1) 152199805Sattilio err(1, "waitpid"); 153199805Sattilio 15440525Sjdp /* Get the program's memory map. */ 15540525Sjdp map = readmap(pid); 15640525Sjdp 15740525Sjdp /* Size the program segments. */ 15840525Sjdp seginfo.count = 0; 15940525Sjdp seginfo.size = 0; 16040525Sjdp each_writable_segment(map, cb_size_segment, &seginfo); 16140525Sjdp 16240525Sjdp /* 163249687Strociny * Build the header and the notes using sbuf and write to the file. 16440525Sjdp */ 165249687Strociny sb = sbuf_new_auto(); 166249687Strociny hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 167249687Strociny /* Start header + notes section. */ 168249687Strociny sbuf_start_section(sb, NULL); 169249687Strociny /* Make empty header subsection. */ 170249687Strociny sbuf_start_section(sb, &old_len); 171249687Strociny sbuf_putc(sb, 0); 172249687Strociny sbuf_end_section(sb, old_len, hdrsize, 0); 173249687Strociny /* Put notes. */ 174249687Strociny elf_putnotes(pid, sb, ¬esz); 175249687Strociny /* Align up to a page boundary for the program segments. */ 176249687Strociny sbuf_end_section(sb, -1, PAGE_SIZE, 0); 177249687Strociny if (sbuf_finish(sb) != 0) 178249687Strociny err(1, "sbuf_finish"); 179249687Strociny hdr = sbuf_data(sb); 180249687Strociny segoff = sbuf_len(sb); 181199805Sattilio /* Fill in the header. */ 182249687Strociny elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count); 183199805Sattilio 184249687Strociny n = write(fd, hdr, segoff); 185249687Strociny if (n == -1) 186199805Sattilio err(1, "write"); 187249687Strociny if (n < segoff) 188249687Strociny errx(1, "short write"); 189199805Sattilio 19040525Sjdp /* Write the contents of all of the writable segments. */ 19140525Sjdp php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 19240525Sjdp for (i = 0; i < seginfo.count; i++) { 193199805Sattilio struct ptrace_io_desc iorequest; 194102944Sdwmalone uintmax_t nleft = php->p_filesz; 19540525Sjdp 196199805Sattilio iorequest.piod_op = PIOD_READ_D; 197199805Sattilio iorequest.piod_offs = (caddr_t)php->p_vaddr; 19840525Sjdp while (nleft > 0) { 19940525Sjdp char buf[8*1024]; 200102944Sdwmalone size_t nwant; 201102944Sdwmalone ssize_t ngot; 20240525Sjdp 203102944Sdwmalone if (nleft > sizeof(buf)) 20440525Sjdp nwant = sizeof buf; 205102944Sdwmalone else 206102944Sdwmalone nwant = nleft; 207199805Sattilio iorequest.piod_addr = buf; 208199805Sattilio iorequest.piod_len = nwant; 209199805Sattilio ptrace(PT_IO, pid, (caddr_t)&iorequest, 0); 210199805Sattilio ngot = iorequest.piod_len; 211102944Sdwmalone if ((size_t)ngot < nwant) 212223924Sdelphij errx(1, "short read wanted %zu, got %zd", 21340803Sjdp nwant, ngot); 21440525Sjdp ngot = write(fd, buf, nwant); 21540525Sjdp if (ngot == -1) 21640525Sjdp err(1, "write of segment %d failed", i); 217102944Sdwmalone if ((size_t)ngot != nwant) 21840525Sjdp errx(1, "short write"); 21940525Sjdp nleft -= nwant; 220199805Sattilio iorequest.piod_offs += ngot; 22140525Sjdp } 22240525Sjdp php++; 22340525Sjdp } 224249687Strociny sbuf_delete(sb); 22540525Sjdp freemap(map); 22640525Sjdp} 22740525Sjdp 22840525Sjdp/* 22940525Sjdp * A callback for each_writable_segment() to write out the segment's 23040525Sjdp * program header entry. 23140525Sjdp */ 23240525Sjdpstatic void 23340525Sjdpcb_put_phdr(vm_map_entry_t entry, void *closure) 23440525Sjdp{ 23540525Sjdp struct phdr_closure *phc = (struct phdr_closure *)closure; 23640525Sjdp Elf_Phdr *phdr = phc->phdr; 23740525Sjdp 23840525Sjdp phc->offset = round_page(phc->offset); 23940525Sjdp 24040525Sjdp phdr->p_type = PT_LOAD; 24140525Sjdp phdr->p_offset = phc->offset; 24240525Sjdp phdr->p_vaddr = entry->start; 24340525Sjdp phdr->p_paddr = 0; 24440525Sjdp phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 24540525Sjdp phdr->p_align = PAGE_SIZE; 24640525Sjdp phdr->p_flags = 0; 24740525Sjdp if (entry->protection & VM_PROT_READ) 24840525Sjdp phdr->p_flags |= PF_R; 24940525Sjdp if (entry->protection & VM_PROT_WRITE) 25040525Sjdp phdr->p_flags |= PF_W; 25140525Sjdp if (entry->protection & VM_PROT_EXECUTE) 25240525Sjdp phdr->p_flags |= PF_X; 25340525Sjdp 25440525Sjdp phc->offset += phdr->p_filesz; 25540525Sjdp phc->phdr++; 25640525Sjdp} 25740525Sjdp 25840525Sjdp/* 25940525Sjdp * A callback for each_writable_segment() to gather information about 26040525Sjdp * the number of segments and their total size. 26140525Sjdp */ 26240525Sjdpstatic void 26340525Sjdpcb_size_segment(vm_map_entry_t entry, void *closure) 26440525Sjdp{ 26540525Sjdp struct sseg_closure *ssc = (struct sseg_closure *)closure; 26640525Sjdp 26740525Sjdp ssc->count++; 26840525Sjdp ssc->size += entry->end - entry->start; 26940525Sjdp} 27040525Sjdp 27140525Sjdp/* 27240525Sjdp * For each segment in the given memory map, call the given function 27340525Sjdp * with a pointer to the map entry and some arbitrary caller-supplied 27440525Sjdp * data. 27540525Sjdp */ 27640525Sjdpstatic void 27740525Sjdpeach_writable_segment(vm_map_entry_t map, segment_callback func, void *closure) 27840525Sjdp{ 27940525Sjdp vm_map_entry_t entry; 28040525Sjdp 28140525Sjdp for (entry = map; entry != NULL; entry = entry->next) 28240525Sjdp (*func)(entry, closure); 28340525Sjdp} 28440525Sjdp 28540525Sjdpstatic void 286249687Strocinyelf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep) 28740525Sjdp{ 288199805Sattilio lwpid_t *tids; 289249687Strociny size_t threads, old_len; 290249687Strociny ssize_t size; 291199805Sattilio int i; 29240525Sjdp 293199805Sattilio errno = 0; 294199805Sattilio threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0); 295199805Sattilio if (errno) 296199805Sattilio err(1, "PT_GETNUMLWPS"); 297249687Strociny tids = malloc(threads * sizeof(*tids)); 298249687Strociny if (tids == NULL) 299249687Strociny errx(1, "out of memory"); 300249687Strociny errno = 0; 301249687Strociny ptrace(PT_GETLWPLIST, pid, (void *)tids, threads); 302249687Strociny if (errno) 303249687Strociny err(1, "PT_GETLWPLIST"); 304199805Sattilio 305249687Strociny sbuf_start_section(sb, &old_len); 306249687Strociny elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb); 307199805Sattilio 308199805Sattilio for (i = 0; i < threads; ++i) { 309249687Strociny elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb); 310249687Strociny elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb); 311249687Strociny elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb); 312199805Sattilio } 313199805Sattilio 314249687Strociny elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb); 315249687Strociny elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb); 316249687Strociny elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb); 317249687Strociny elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb); 318249687Strociny elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb); 319249687Strociny elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb); 320249687Strociny elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb); 321249687Strociny elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid, 322249687Strociny sb); 323249687Strociny elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb); 32440525Sjdp 325249687Strociny size = sbuf_end_section(sb, old_len, 1, 0); 326249687Strociny if (size == -1) 327249687Strociny err(1, "sbuf_end_section"); 328249687Strociny free(tids); 329249687Strociny *sizep = size; 33040525Sjdp} 33140525Sjdp 33240525Sjdp/* 333249687Strociny * Emit one note section to sbuf. 33440525Sjdp */ 33540525Sjdpstatic void 336249687Strocinyelf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb) 33740525Sjdp{ 33840525Sjdp Elf_Note note; 339249687Strociny size_t descsz; 340249687Strociny ssize_t old_len; 341249687Strociny void *desc; 34240525Sjdp 343249687Strociny desc = notefunc(arg, &descsz); 344249687Strociny note.n_namesz = 8; /* strlen("FreeBSD") + 1 */ 34540525Sjdp note.n_descsz = descsz; 34640525Sjdp note.n_type = type; 347249687Strociny 348249687Strociny sbuf_bcat(sb, ¬e, sizeof(note)); 349249687Strociny sbuf_start_section(sb, &old_len); 350249687Strociny sbuf_bcat(sb, "FreeBSD", note.n_namesz); 351249687Strociny sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 352249687Strociny if (descsz == 0) 353249687Strociny return; 354249687Strociny sbuf_start_section(sb, &old_len); 355249687Strociny sbuf_bcat(sb, desc, descsz); 356249687Strociny sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0); 357249687Strociny free(desc); 35840525Sjdp} 35940525Sjdp 36040525Sjdp/* 361249687Strociny * Generate the ELF coredump header. 362249687Strociny */ 363249687Strocinystatic void 364249687Strocinyelf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize, 365249687Strociny size_t notesz, size_t segoff, int numsegs) 366249687Strociny{ 367249687Strociny Elf_Ehdr *ehdr; 368249687Strociny Elf_Phdr *phdr; 369249687Strociny struct phdr_closure phc; 370249687Strociny 371249687Strociny ehdr = (Elf_Ehdr *)hdr; 372249687Strociny phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)); 373249687Strociny 374249687Strociny ehdr->e_ident[EI_MAG0] = ELFMAG0; 375249687Strociny ehdr->e_ident[EI_MAG1] = ELFMAG1; 376249687Strociny ehdr->e_ident[EI_MAG2] = ELFMAG2; 377249687Strociny ehdr->e_ident[EI_MAG3] = ELFMAG3; 378249687Strociny ehdr->e_ident[EI_CLASS] = ELF_CLASS; 379249687Strociny ehdr->e_ident[EI_DATA] = ELF_DATA; 380249687Strociny ehdr->e_ident[EI_VERSION] = EV_CURRENT; 381249687Strociny ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 382249687Strociny ehdr->e_ident[EI_ABIVERSION] = 0; 383249687Strociny ehdr->e_ident[EI_PAD] = 0; 384249687Strociny ehdr->e_type = ET_CORE; 385249687Strociny ehdr->e_machine = ELF_ARCH; 386249687Strociny ehdr->e_version = EV_CURRENT; 387249687Strociny ehdr->e_entry = 0; 388249687Strociny ehdr->e_phoff = sizeof(Elf_Ehdr); 389249687Strociny ehdr->e_flags = 0; 390249687Strociny ehdr->e_ehsize = sizeof(Elf_Ehdr); 391249687Strociny ehdr->e_phentsize = sizeof(Elf_Phdr); 392249687Strociny ehdr->e_phnum = numsegs + 1; 393249687Strociny ehdr->e_shentsize = sizeof(Elf_Shdr); 394249687Strociny ehdr->e_shnum = 0; 395249687Strociny ehdr->e_shstrndx = SHN_UNDEF; 396249687Strociny 397249687Strociny /* 398249687Strociny * Fill in the program header entries. 399249687Strociny */ 400249687Strociny 401249687Strociny /* The note segement. */ 402249687Strociny phdr->p_type = PT_NOTE; 403249687Strociny phdr->p_offset = hdrsize; 404249687Strociny phdr->p_vaddr = 0; 405249687Strociny phdr->p_paddr = 0; 406249687Strociny phdr->p_filesz = notesz; 407249687Strociny phdr->p_memsz = 0; 408249687Strociny phdr->p_flags = PF_R; 409249687Strociny phdr->p_align = sizeof(Elf32_Size); 410249687Strociny phdr++; 411249687Strociny 412249687Strociny /* All the writable segments from the program. */ 413249687Strociny phc.phdr = phdr; 414249687Strociny phc.offset = segoff; 415249687Strociny each_writable_segment(map, cb_put_phdr, &phc); 416249687Strociny} 417249687Strociny 418249687Strociny/* 41940525Sjdp * Free the memory map. 42040525Sjdp */ 42140525Sjdpstatic void 42240525Sjdpfreemap(vm_map_entry_t map) 42340525Sjdp{ 424103299Speter 42540525Sjdp while (map != NULL) { 42640525Sjdp vm_map_entry_t next = map->next; 42740525Sjdp free(map); 42840525Sjdp map = next; 42940525Sjdp } 43040525Sjdp} 43140525Sjdp 43240525Sjdp/* 433199805Sattilio * Read the process's memory map using kinfo_getvmmap(), and return a list of 43440525Sjdp * VM map entries. Only the non-device read/writable segments are 43540525Sjdp * returned. The map entries in the list aren't fully filled in; only 43640525Sjdp * the items we need are present. 43740525Sjdp */ 43840525Sjdpstatic vm_map_entry_t 43940525Sjdpreadmap(pid_t pid) 44040525Sjdp{ 441199805Sattilio vm_map_entry_t ent, *linkp, map; 442199805Sattilio struct kinfo_vmentry *vmentl, *kve; 443199805Sattilio int i, nitems; 44440525Sjdp 445199805Sattilio vmentl = kinfo_getvmmap(pid, &nitems); 446199805Sattilio if (vmentl == NULL) 447199805Sattilio err(1, "cannot retrieve mappings for %u process", pid); 44840525Sjdp 44940525Sjdp map = NULL; 45040525Sjdp linkp = ↦ 451199805Sattilio for (i = 0; i < nitems; i++) { 452199805Sattilio kve = &vmentl[i]; 45340525Sjdp 454199805Sattilio /* 455210063Sattilio * Ignore 'malformed' segments or ones representing memory 456210063Sattilio * mapping with MAP_NOCORE on. 457210063Sattilio * If the 'full' support is disabled, just dump the most 458210063Sattilio * meaningful data segments. 459199805Sattilio */ 460210063Sattilio if ((kve->kve_protection & KVME_PROT_READ) == 0 || 461210063Sattilio (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 || 462210063Sattilio kve->kve_type == KVME_TYPE_DEAD || 463210063Sattilio kve->kve_type == KVME_TYPE_UNKNOWN || 464210063Sattilio ((pflags & PFLAGS_FULL) == 0 && 465210063Sattilio kve->kve_type != KVME_TYPE_DEFAULT && 466199805Sattilio kve->kve_type != KVME_TYPE_VNODE && 467199805Sattilio kve->kve_type != KVME_TYPE_SWAP)) 46840525Sjdp continue; 46940525Sjdp 470199805Sattilio ent = calloc(1, sizeof(*ent)); 471199805Sattilio if (ent == NULL) 47240525Sjdp errx(1, "out of memory"); 473199805Sattilio ent->start = (vm_offset_t)kve->kve_start; 474199805Sattilio ent->end = (vm_offset_t)kve->kve_end; 47540525Sjdp ent->protection = VM_PROT_READ | VM_PROT_WRITE; 476199805Sattilio if ((kve->kve_protection & KVME_PROT_EXEC) != 0) 477199805Sattilio ent->protection |= VM_PROT_EXECUTE; 47840525Sjdp 47940525Sjdp *linkp = ent; 48040525Sjdp linkp = &ent->next; 48140525Sjdp } 482199805Sattilio free(vmentl); 483199805Sattilio return (map); 48440525Sjdp} 485103299Speter 486249687Strociny/* 487249687Strociny * Miscellaneous note out functions. 488249687Strociny */ 489249687Strociny 490249687Strocinystatic void * 491249687Strocinyelf_note_prpsinfo(void *arg, size_t *sizep) 492249687Strociny{ 493249687Strociny pid_t pid; 494249687Strociny prpsinfo_t *psinfo; 495249687Strociny struct kinfo_proc kip; 496249687Strociny size_t len; 497249687Strociny int name[4]; 498249687Strociny 499249687Strociny pid = *(pid_t *)arg; 500249687Strociny psinfo = calloc(1, sizeof(*psinfo)); 501249687Strociny if (psinfo == NULL) 502249687Strociny errx(1, "out of memory"); 503249687Strociny psinfo->pr_version = PRPSINFO_VERSION; 504249687Strociny psinfo->pr_psinfosz = sizeof(prpsinfo_t); 505249687Strociny 506249687Strociny name[0] = CTL_KERN; 507249687Strociny name[1] = KERN_PROC; 508249687Strociny name[2] = KERN_PROC_PID; 509249687Strociny name[3] = pid; 510249687Strociny len = sizeof(kip); 511249687Strociny if (sysctl(name, 4, &kip, &len, NULL, 0) == -1) 512249687Strociny err(1, "kern.proc.pid.%u", pid); 513249687Strociny if (kip.ki_pid != pid) 514249687Strociny err(1, "kern.proc.pid.%u", pid); 515249687Strociny strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN); 516249687Strociny strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ); 517249687Strociny 518249687Strociny *sizep = sizeof(*psinfo); 519249687Strociny return (psinfo); 520249687Strociny} 521249687Strociny 522249687Strocinystatic void * 523249687Strocinyelf_note_prstatus(void *arg, size_t *sizep) 524249687Strociny{ 525249687Strociny lwpid_t tid; 526249687Strociny prstatus_t *status; 527249687Strociny 528249687Strociny tid = *(lwpid_t *)arg; 529249687Strociny status = calloc(1, sizeof(*status)); 530249687Strociny if (status == NULL) 531249687Strociny errx(1, "out of memory"); 532249687Strociny status->pr_version = PRSTATUS_VERSION; 533249687Strociny status->pr_statussz = sizeof(prstatus_t); 534249687Strociny status->pr_gregsetsz = sizeof(gregset_t); 535249687Strociny status->pr_fpregsetsz = sizeof(fpregset_t); 536249687Strociny status->pr_osreldate = __FreeBSD_version; 537249687Strociny status->pr_pid = tid; 538249687Strociny ptrace(PT_GETREGS, tid, (void *)&status->pr_reg, 0); 539249687Strociny 540249687Strociny *sizep = sizeof(*status); 541249687Strociny return (status); 542249687Strociny} 543249687Strociny 544249687Strocinystatic void * 545249687Strocinyelf_note_fpregset(void *arg, size_t *sizep) 546249687Strociny{ 547249687Strociny lwpid_t tid; 548249687Strociny prfpregset_t *fpregset; 549249687Strociny 550249687Strociny tid = *(lwpid_t *)arg; 551249687Strociny fpregset = calloc(1, sizeof(*fpregset)); 552249687Strociny if (fpregset == NULL) 553249687Strociny errx(1, "out of memory"); 554249687Strociny ptrace(PT_GETFPREGS, tid, (void *)fpregset, 0); 555249687Strociny 556249687Strociny *sizep = sizeof(*fpregset); 557249687Strociny return (fpregset); 558249687Strociny} 559249687Strociny 560249687Strocinystatic void * 561249687Strocinyelf_note_thrmisc(void *arg, size_t *sizep) 562249687Strociny{ 563249687Strociny lwpid_t tid; 564249687Strociny struct ptrace_lwpinfo lwpinfo; 565249687Strociny thrmisc_t *thrmisc; 566249687Strociny 567249687Strociny tid = *(lwpid_t *)arg; 568249687Strociny thrmisc = calloc(1, sizeof(*thrmisc)); 569249687Strociny if (thrmisc == NULL) 570249687Strociny errx(1, "out of memory"); 571249687Strociny ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, 572249687Strociny sizeof(lwpinfo)); 573249687Strociny memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad)); 574249687Strociny strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname); 575249687Strociny 576249687Strociny *sizep = sizeof(*thrmisc); 577249687Strociny return (thrmisc); 578249687Strociny} 579249687Strociny 580249687Strocinystatic void * 581249687Strocinyprocstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep) 582249687Strociny{ 583249687Strociny size_t len, oldlen; 584249687Strociny pid_t pid; 585249687Strociny int name[4], structsize; 586249687Strociny void *buf, *p; 587249687Strociny 588249687Strociny pid = *(pid_t *)arg; 589249687Strociny structsize = structsz; 590249687Strociny name[0] = CTL_KERN; 591249687Strociny name[1] = KERN_PROC; 592249687Strociny name[2] = what; 593249687Strociny name[3] = pid; 594249687Strociny len = 0; 595249687Strociny if (sysctl(name, 4, NULL, &len, NULL, 0) == -1) 596249687Strociny err(1, "kern.proc.%d.%u", what, pid); 597249687Strociny buf = calloc(1, sizeof(structsize) + len * 4 / 3); 598249687Strociny if (buf == NULL) 599249687Strociny errx(1, "out of memory"); 600249687Strociny bcopy(&structsize, buf, sizeof(structsize)); 601249687Strociny p = (char *)buf + sizeof(structsize); 602249687Strociny if (sysctl(name, 4, p, &len, NULL, 0) == -1) 603249687Strociny err(1, "kern.proc.%d.%u", what, pid); 604249687Strociny 605249687Strociny *sizep = sizeof(structsize) + len; 606249687Strociny return (buf); 607249687Strociny} 608249687Strociny 609249687Strocinystatic void * 610249687Strocinyelf_note_procstat_proc(void *arg, size_t *sizep) 611249687Strociny{ 612249687Strociny 613249687Strociny return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD, 614249687Strociny sizeof(struct kinfo_proc), sizep)); 615249687Strociny} 616249687Strociny 617249687Strocinystatic void * 618249687Strocinyelf_note_procstat_files(void *arg, size_t *sizep) 619249687Strociny{ 620249687Strociny 621249687Strociny return (procstat_sysctl(arg, KERN_PROC_FILEDESC, 622249687Strociny sizeof(struct kinfo_file), sizep)); 623249687Strociny} 624249687Strociny 625249687Strocinystatic void * 626249687Strocinyelf_note_procstat_vmmap(void *arg, size_t *sizep) 627249687Strociny{ 628249687Strociny 629249687Strociny return (procstat_sysctl(arg, KERN_PROC_VMMAP, 630249687Strociny sizeof(struct kinfo_vmentry), sizep)); 631249687Strociny} 632249687Strociny 633249687Strocinystatic void * 634249687Strocinyelf_note_procstat_groups(void *arg, size_t *sizep) 635249687Strociny{ 636249687Strociny 637249704Strociny return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep)); 638249687Strociny} 639249687Strociny 640249687Strocinystatic void * 641249687Strocinyelf_note_procstat_umask(void *arg, size_t *sizep) 642249687Strociny{ 643249687Strociny 644249687Strociny return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep)); 645249687Strociny} 646249687Strociny 647249687Strocinystatic void * 648249687Strocinyelf_note_procstat_osrel(void *arg, size_t *sizep) 649249687Strociny{ 650249687Strociny 651249687Strociny return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep)); 652249687Strociny} 653249687Strociny 654249687Strocinystatic void * 655249687Strocinyelf_note_procstat_psstrings(void *arg, size_t *sizep) 656249687Strociny{ 657249687Strociny 658249687Strociny return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS, 659249687Strociny sizeof(vm_offset_t), sizep)); 660249687Strociny} 661249687Strociny 662249687Strocinystatic void * 663249687Strocinyelf_note_procstat_auxv(void *arg, size_t *sizep) 664249687Strociny{ 665249687Strociny 666249687Strociny return (procstat_sysctl(arg, KERN_PROC_AUXV, 667249687Strociny sizeof(Elf_Auxinfo), sizep)); 668249687Strociny} 669249687Strociny 670249687Strocinystatic void * 671249687Strocinyelf_note_procstat_rlimit(void *arg, size_t *sizep) 672249687Strociny{ 673249687Strociny pid_t pid; 674249687Strociny size_t len; 675249687Strociny int i, name[5], structsize; 676249687Strociny void *buf, *p; 677249687Strociny 678249687Strociny pid = *(pid_t *)arg; 679249687Strociny structsize = sizeof(struct rlimit) * RLIM_NLIMITS; 680249687Strociny buf = calloc(1, sizeof(structsize) + structsize); 681249687Strociny if (buf == NULL) 682249687Strociny errx(1, "out of memory"); 683249687Strociny bcopy(&structsize, buf, sizeof(structsize)); 684249687Strociny p = (char *)buf + sizeof(structsize); 685249687Strociny name[0] = CTL_KERN; 686249687Strociny name[1] = KERN_PROC; 687249687Strociny name[2] = KERN_PROC_RLIMIT; 688249687Strociny name[3] = pid; 689249687Strociny len = sizeof(struct rlimit); 690249687Strociny for (i = 0; i < RLIM_NLIMITS; i++) { 691249687Strociny name[4] = i; 692249687Strociny if (sysctl(name, 5, p, &len, NULL, 0) == -1) 693249687Strociny err(1, "kern.proc.rlimit.%u", pid); 694249687Strociny if (len != sizeof(struct rlimit)) 695249687Strociny errx(1, "kern.proc.rlimit.%u: short read", pid); 696249687Strociny p += len; 697249687Strociny } 698249687Strociny 699249687Strociny *sizep = sizeof(structsize) + structsize; 700249687Strociny return (buf); 701249687Strociny} 702249687Strociny 703103299Speterstruct dumpers elfdump = { elf_ident, elf_coredump }; 704103299SpeterTEXT_SET(dumpset, elfdump); 705