2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Dell EMC
5 * Copyright (c) 2007 Sandvine Incorporated
6 * Copyright (c) 1998 John D. Polstra
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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>
41 #include <sys/sysctl.h>
44 #include <machine/elf.h>
45 #include <vm/vm_param.h>
48 #include <vm/vm_map.h>
64 * Code for generating ELF core dumps.
67 typedef void (*segment_callback)(vm_map_entry_t, void *);
69 /* Closure for cb_put_phdr(). */
71 Elf_Phdr *phdr; /* Program header to fill in */
72 Elf_Off offset; /* Offset of segment in core file */
75 /* Closure for cb_size_segment(). */
77 int count; /* Count of writable segments. */
78 size_t size; /* Total size of all writable segments. */
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);
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
102 typedef void* (*notefunc_t)(void *, size_t *);
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,
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 *);
115 static void *elf_note_arm_vfp(void *, size_t *);
117 #if defined(__i386__) || defined(__amd64__)
118 static void *elf_note_x86_xstate(void *, size_t *);
120 #if defined(__powerpc__)
121 static void *elf_note_powerpc_vmx(void *, size_t *);
122 static void *elf_note_powerpc_vsx(void *, size_t *);
124 static void *elf_note_procstat_auxv(void *, size_t *);
125 static void *elf_note_procstat_files(void *, size_t *);
126 static void *elf_note_procstat_groups(void *, size_t *);
127 static void *elf_note_procstat_osrel(void *, size_t *);
128 static void *elf_note_procstat_proc(void *, size_t *);
129 static void *elf_note_procstat_psstrings(void *, size_t *);
130 static void *elf_note_procstat_rlimit(void *, size_t *);
131 static void *elf_note_procstat_umask(void *, size_t *);
132 static void *elf_note_procstat_vmmap(void *, size_t *);
133 static void elf_puthdr(int, pid_t, vm_map_entry_t, void *, size_t, size_t,
135 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
136 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
137 static void freemap(vm_map_entry_t);
138 static vm_map_entry_t readmap(pid_t);
139 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
141 static pid_t g_pid; /* Pid being dumped, global for elf_detach */
142 static int g_status; /* proc status after ptrace attach */
145 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
151 cnt = read(efd, &hdr, sizeof(hdr));
152 if (cnt != sizeof(hdr))
156 switch (hdr.e_ident[EI_DATA]) {
158 machine = le16toh(hdr.e_machine);
161 machine = be16toh(hdr.e_machine);
166 if (!ELF_MACHINE_OK(machine))
180 * Forward any pending signals. SIGSTOP is generated by ptrace
181 * itself, so ignore it.
183 sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0;
186 ptrace(PT_DETACH, g_pid, (caddr_t)1, sig);
191 * Write an ELF coredump for the given pid to the given fd.
194 elf_coredump(int efd, int fd, pid_t pid)
197 struct sseg_closure seginfo;
200 size_t hdrsize, notesz, segoff;
205 /* Attach to process to dump. */
207 if (atexit(elf_detach) != 0)
210 ptrace(PT_ATTACH, pid, NULL, 0);
213 if (waitpid(pid, &g_status, 0) == -1)
216 /* Get the program's memory map. */
219 /* Size the program segments. */
222 each_dumpable_segment(map, cb_size_segment, &seginfo);
225 * Build the header and the notes using sbuf and write to the file.
227 sb = sbuf_new_auto();
228 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
229 if (seginfo.count + 1 >= PN_XNUM)
230 hdrsize += sizeof(Elf_Shdr);
231 /* Start header + notes section. */
232 sbuf_start_section(sb, NULL);
233 /* Make empty header subsection. */
234 sbuf_start_section(sb, &old_len);
236 sbuf_end_section(sb, old_len, hdrsize, 0);
238 elf_putnotes(pid, sb, ¬esz);
239 /* Align up to a page boundary for the program segments. */
240 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
241 if (sbuf_finish(sb) != 0)
242 err(1, "sbuf_finish");
244 segoff = sbuf_len(sb);
245 /* Fill in the header. */
246 elf_puthdr(efd, pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
248 n = write(fd, hdr, segoff);
252 errx(1, "short write");
254 /* Write the contents of all of the writable segments. */
255 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
256 for (i = 0; i < seginfo.count; i++) {
257 struct ptrace_io_desc iorequest;
258 uintmax_t nleft = php->p_filesz;
260 iorequest.piod_op = PIOD_READ_D;
261 iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
267 if (nleft > sizeof(buf))
271 iorequest.piod_addr = buf;
272 iorequest.piod_len = nwant;
273 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
274 ngot = iorequest.piod_len;
275 if ((size_t)ngot < nwant)
276 errx(1, "short read wanted %zu, got %zd",
278 ngot = write(fd, buf, nwant);
280 err(1, "write of segment %d failed", i);
281 if ((size_t)ngot != nwant)
282 errx(1, "short write");
284 iorequest.piod_offs += ngot;
293 * A callback for each_dumpable_segment() to write out the segment's
294 * program header entry.
297 cb_put_phdr(vm_map_entry_t entry, void *closure)
299 struct phdr_closure *phc = (struct phdr_closure *)closure;
300 Elf_Phdr *phdr = phc->phdr;
302 phc->offset = round_page(phc->offset);
304 phdr->p_type = PT_LOAD;
305 phdr->p_offset = phc->offset;
306 phdr->p_vaddr = entry->start;
308 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
309 phdr->p_align = PAGE_SIZE;
311 if (entry->protection & VM_PROT_READ)
312 phdr->p_flags |= PF_R;
313 if (entry->protection & VM_PROT_WRITE)
314 phdr->p_flags |= PF_W;
315 if (entry->protection & VM_PROT_EXECUTE)
316 phdr->p_flags |= PF_X;
318 phc->offset += phdr->p_filesz;
323 * A callback for each_dumpable_segment() to gather information about
324 * the number of segments and their total size.
327 cb_size_segment(vm_map_entry_t entry, void *closure)
329 struct sseg_closure *ssc = (struct sseg_closure *)closure;
332 ssc->size += entry->end - entry->start;
336 * For each segment in the given memory map, call the given function
337 * with a pointer to the map entry and some arbitrary caller-supplied
341 each_dumpable_segment(vm_map_entry_t map, segment_callback func, void *closure)
343 vm_map_entry_t entry;
345 for (entry = map; entry != NULL; entry = entry->next)
346 (*func)(entry, closure);
350 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
353 size_t threads, old_len;
358 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
360 err(1, "PT_GETNUMLWPS");
361 tids = malloc(threads * sizeof(*tids));
363 errx(1, "out of memory");
365 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
367 err(1, "PT_GETLWPLIST");
369 sbuf_start_section(sb, &old_len);
370 elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
372 for (i = 0; i < threads; ++i) {
373 elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
374 elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
375 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
376 elf_putnote(NT_PTLWPINFO, elf_note_ptlwpinfo, tids + i, sb);
378 elf_putnote(NT_ARM_VFP, elf_note_arm_vfp, tids + i, sb);
380 #if defined(__i386__) || defined(__amd64__)
381 elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
383 #if defined(__powerpc__)
384 elf_putnote(NT_PPC_VMX, elf_note_powerpc_vmx, tids + i, sb);
385 elf_putnote(NT_PPC_VSX, elf_note_powerpc_vsx, tids + i, sb);
389 #ifndef ELFCORE_COMPAT_32
390 elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
391 elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
392 elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
393 elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
394 elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
395 elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
396 elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
397 elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
399 elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
402 size = sbuf_end_section(sb, old_len, 1, 0);
404 err(1, "sbuf_end_section");
410 * Emit one note section to sbuf.
413 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
420 desc = notefunc(arg, &descsz);
421 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
422 note.n_descsz = descsz;
425 sbuf_bcat(sb, ¬e, sizeof(note));
426 sbuf_start_section(sb, &old_len);
427 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
428 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
431 sbuf_start_section(sb, &old_len);
432 sbuf_bcat(sb, desc, descsz);
433 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
438 * Generate the ELF coredump header.
441 elf_puthdr(int efd, pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
442 size_t notesz, size_t segoff, int numsegs)
444 Elf_Ehdr *ehdr, binhdr;
447 struct phdr_closure phc;
450 cnt = read(efd, &binhdr, sizeof(binhdr));
452 err(1, "Failed to re-read ELF header");
453 else if (cnt != sizeof(binhdr))
454 errx(1, "Failed to re-read ELF header");
456 ehdr = (Elf_Ehdr *)hdr;
458 ehdr->e_ident[EI_MAG0] = ELFMAG0;
459 ehdr->e_ident[EI_MAG1] = ELFMAG1;
460 ehdr->e_ident[EI_MAG2] = ELFMAG2;
461 ehdr->e_ident[EI_MAG3] = ELFMAG3;
462 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
463 ehdr->e_ident[EI_DATA] = ELF_DATA;
464 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
465 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
466 ehdr->e_ident[EI_ABIVERSION] = 0;
467 ehdr->e_ident[EI_PAD] = 0;
468 ehdr->e_type = ET_CORE;
469 ehdr->e_machine = binhdr.e_machine;
470 ehdr->e_version = EV_CURRENT;
472 ehdr->e_phoff = sizeof(Elf_Ehdr);
473 ehdr->e_flags = binhdr.e_flags;
474 ehdr->e_ehsize = sizeof(Elf_Ehdr);
475 ehdr->e_phentsize = sizeof(Elf_Phdr);
476 ehdr->e_shentsize = sizeof(Elf_Shdr);
477 ehdr->e_shstrndx = SHN_UNDEF;
478 if (numsegs + 1 < PN_XNUM) {
479 ehdr->e_phnum = numsegs + 1;
482 ehdr->e_phnum = PN_XNUM;
485 ehdr->e_shoff = ehdr->e_phoff +
486 (numsegs + 1) * ehdr->e_phentsize;
488 shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
489 memset(shdr, 0, sizeof(*shdr));
491 * A special first section is used to hold large segment and
492 * section counts. This was proposed by Sun Microsystems in
493 * Solaris and has been adopted by Linux; the standard ELF
494 * tools are already familiar with the technique.
496 * See table 7-7 of the Solaris "Linker and Libraries Guide"
497 * (or 12-7 depending on the version of the document) for more
500 shdr->sh_type = SHT_NULL;
501 shdr->sh_size = ehdr->e_shnum;
502 shdr->sh_link = ehdr->e_shstrndx;
503 shdr->sh_info = numsegs + 1;
507 * Fill in the program header entries.
509 phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
511 /* The note segement. */
512 phdr->p_type = PT_NOTE;
513 phdr->p_offset = hdrsize;
516 phdr->p_filesz = notesz;
518 phdr->p_flags = PF_R;
519 phdr->p_align = sizeof(Elf32_Size);
522 /* All the writable segments from the program. */
525 each_dumpable_segment(map, cb_put_phdr, &phc);
529 * Free the memory map.
532 freemap(vm_map_entry_t map)
535 while (map != NULL) {
536 vm_map_entry_t next = map->next;
543 * Read the process's memory map using kinfo_getvmmap(), and return a list of
544 * VM map entries. Only the non-device read/writable segments are
545 * returned. The map entries in the list aren't fully filled in; only
546 * the items we need are present.
548 static vm_map_entry_t
551 vm_map_entry_t ent, *linkp, map;
552 struct kinfo_vmentry *vmentl, *kve;
555 vmentl = kinfo_getvmmap(pid, &nitems);
557 err(1, "cannot retrieve mappings for %u process", pid);
561 for (i = 0; i < nitems; i++) {
565 * Ignore 'malformed' segments or ones representing memory
566 * mapping with MAP_NOCORE on.
567 * If the 'full' support is disabled, just dump the most
568 * meaningful data segments.
570 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
571 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
572 kve->kve_type == KVME_TYPE_DEAD ||
573 kve->kve_type == KVME_TYPE_UNKNOWN ||
574 ((pflags & PFLAGS_FULL) == 0 &&
575 kve->kve_type != KVME_TYPE_DEFAULT &&
576 kve->kve_type != KVME_TYPE_VNODE &&
577 kve->kve_type != KVME_TYPE_SWAP &&
578 kve->kve_type != KVME_TYPE_PHYS))
581 ent = calloc(1, sizeof(*ent));
583 errx(1, "out of memory");
584 ent->start = (vm_offset_t)kve->kve_start;
585 ent->end = (vm_offset_t)kve->kve_end;
586 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
587 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
588 ent->protection |= VM_PROT_EXECUTE;
598 * Miscellaneous note out functions.
602 elf_note_prpsinfo(void *arg, size_t *sizep)
606 elfcore_prpsinfo_t *psinfo;
607 struct kinfo_proc kip;
612 psinfo = calloc(1, sizeof(*psinfo));
614 errx(1, "out of memory");
615 psinfo->pr_version = PRPSINFO_VERSION;
616 psinfo->pr_psinfosz = sizeof(*psinfo);
620 name[2] = KERN_PROC_PID;
623 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
624 err(1, "kern.proc.pid.%u", pid);
625 if (kip.ki_pid != pid)
626 err(1, "kern.proc.pid.%u", pid);
627 strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname));
628 name[2] = KERN_PROC_ARGS;
629 len = sizeof(psinfo->pr_psargs) - 1;
630 if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) {
631 cp = psinfo->pr_psargs;
634 cp = memchr(cp, '\0', end - cp);
640 strlcpy(psinfo->pr_psargs, kip.ki_comm,
641 sizeof(psinfo->pr_psargs));
642 psinfo->pr_pid = pid;
644 *sizep = sizeof(*psinfo);
649 elf_note_prstatus(void *arg, size_t *sizep)
652 elfcore_prstatus_t *status;
655 tid = *(lwpid_t *)arg;
656 status = calloc(1, sizeof(*status));
658 errx(1, "out of memory");
659 status->pr_version = PRSTATUS_VERSION;
660 status->pr_statussz = sizeof(*status);
661 status->pr_gregsetsz = sizeof(elfcore_gregset_t);
662 status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
663 status->pr_osreldate = __FreeBSD_version;
664 status->pr_pid = tid;
665 ptrace(PT_GETREGS, tid, (void *)&greg, 0);
666 elf_convert_gregset(&status->pr_reg, &greg);
668 *sizep = sizeof(*status);
673 elf_note_fpregset(void *arg, size_t *sizep)
676 elfcore_fpregset_t *fpregset;
679 tid = *(lwpid_t *)arg;
680 fpregset = calloc(1, sizeof(*fpregset));
681 if (fpregset == NULL)
682 errx(1, "out of memory");
683 ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
684 elf_convert_fpregset(fpregset, &fpreg);
686 *sizep = sizeof(*fpregset);
691 elf_note_thrmisc(void *arg, size_t *sizep)
694 struct ptrace_lwpinfo lwpinfo;
697 tid = *(lwpid_t *)arg;
698 thrmisc = calloc(1, sizeof(*thrmisc));
700 errx(1, "out of memory");
701 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
703 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
704 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
706 *sizep = sizeof(*thrmisc);
711 elf_note_ptlwpinfo(void *arg, size_t *sizep)
714 elfcore_lwpinfo_t *elf_info;
715 struct ptrace_lwpinfo lwpinfo;
718 tid = *(lwpid_t *)arg;
719 p = calloc(1, sizeof(int) + sizeof(elfcore_lwpinfo_t));
721 errx(1, "out of memory");
722 *(int *)p = sizeof(elfcore_lwpinfo_t);
723 elf_info = (void *)((int *)p + 1);
724 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, sizeof(lwpinfo));
725 elf_convert_lwpinfo(elf_info, &lwpinfo);
727 *sizep = sizeof(int) + sizeof(struct ptrace_lwpinfo);
733 elf_note_arm_vfp(void *arg, size_t *sizep)
737 static bool has_vfp = true;
740 tid = *(lwpid_t *)arg;
742 if (ptrace(PT_GETVFPREGS, tid, (void *)&info, 0) != 0)
749 vfp = calloc(1, sizeof(*vfp));
750 memcpy(vfp, &info, sizeof(*vfp));
751 *sizep = sizeof(*vfp);
756 #if defined(__i386__) || defined(__amd64__)
758 elf_note_x86_xstate(void *arg, size_t *sizep)
762 static bool xsave_checked = false;
763 static struct ptrace_xstate_info info;
765 tid = *(lwpid_t *)arg;
766 if (!xsave_checked) {
767 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
770 xsave_checked = true;
772 if (info.xsave_len == 0) {
776 xstate = calloc(1, info.xsave_len);
777 ptrace(PT_GETXSTATE, tid, xstate, 0);
778 *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
779 *sizep = info.xsave_len;
784 #if defined(__powerpc__)
786 elf_note_powerpc_vmx(void *arg, size_t *sizep)
790 static bool has_vmx = true;
793 tid = *(lwpid_t *)arg;
795 if (ptrace(PT_GETVRREGS, tid, (void *)&info,
803 vmx = calloc(1, sizeof(*vmx));
804 memcpy(vmx, &info, sizeof(*vmx));
805 *sizep = sizeof(*vmx);
810 elf_note_powerpc_vsx(void *arg, size_t *sizep)
814 static bool has_vsx = true;
817 tid = *(lwpid_t *)arg;
819 if (ptrace(PT_GETVSRREGS, tid, (void *)vshr,
827 vshr_data = calloc(1, sizeof(vshr));
828 memcpy(vshr_data, vshr, sizeof(vshr));
829 *sizep = sizeof(vshr);
835 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
839 int name[4], structsize;
843 structsize = structsz;
849 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
850 err(1, "kern.proc.%d.%u", what, pid);
851 buf = calloc(1, sizeof(structsize) + len * 4 / 3);
853 errx(1, "out of memory");
854 bcopy(&structsize, buf, sizeof(structsize));
855 p = (char *)buf + sizeof(structsize);
856 if (sysctl(name, 4, p, &len, NULL, 0) == -1)
857 err(1, "kern.proc.%d.%u", what, pid);
859 *sizep = sizeof(structsize) + len;
864 elf_note_procstat_proc(void *arg, size_t *sizep)
867 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
868 sizeof(struct kinfo_proc), sizep));
872 elf_note_procstat_files(void *arg, size_t *sizep)
875 return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
876 sizeof(struct kinfo_file), sizep));
880 elf_note_procstat_vmmap(void *arg, size_t *sizep)
883 return (procstat_sysctl(arg, KERN_PROC_VMMAP,
884 sizeof(struct kinfo_vmentry), sizep));
888 elf_note_procstat_groups(void *arg, size_t *sizep)
891 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
895 elf_note_procstat_umask(void *arg, size_t *sizep)
898 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
902 elf_note_procstat_osrel(void *arg, size_t *sizep)
905 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
909 elf_note_procstat_psstrings(void *arg, size_t *sizep)
912 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
913 sizeof(vm_offset_t), sizep));
917 elf_note_procstat_auxv(void *arg, size_t *sizep)
920 return (procstat_sysctl(arg, KERN_PROC_AUXV,
921 sizeof(Elf_Auxinfo), sizep));
925 elf_note_procstat_rlimit(void *arg, size_t *sizep)
929 int i, name[5], structsize;
933 structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
934 buf = calloc(1, sizeof(structsize) + structsize);
936 errx(1, "out of memory");
937 bcopy(&structsize, buf, sizeof(structsize));
938 p = (char *)buf + sizeof(structsize);
941 name[2] = KERN_PROC_RLIMIT;
943 len = sizeof(struct rlimit);
944 for (i = 0; i < RLIM_NLIMITS; i++) {
946 if (sysctl(name, 5, p, &len, NULL, 0) == -1)
947 err(1, "kern.proc.rlimit.%u", pid);
948 if (len != sizeof(struct rlimit))
949 errx(1, "kern.proc.rlimit.%u: short read", pid);
953 *sizep = sizeof(structsize) + structsize;
957 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
958 TEXT_SET(dumpset, __elfN(dump));