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