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