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