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>
62 * Code for generating ELF core dumps.
66 struct map_entry *next;
72 typedef void (*segment_callback)(struct map_entry *, void *);
74 /* Closure for cb_put_phdr(). */
76 Elf_Phdr *phdr; /* Program header to fill in */
77 Elf_Off offset; /* Offset of segment in core file */
80 /* Closure for cb_size_segment(). */
82 int count; /* Count of writable segments. */
83 size_t size; /* Total size of all writable segments. */
86 #ifdef ELFCORE_COMPAT_32
87 typedef struct fpreg32 elfcore_fpregset_t;
88 typedef struct reg32 elfcore_gregset_t;
89 typedef struct prpsinfo32 elfcore_prpsinfo_t;
90 typedef struct prstatus32 elfcore_prstatus_t;
91 typedef struct ptrace_lwpinfo32 elfcore_lwpinfo_t;
92 static void elf_convert_lwpinfo(struct ptrace_lwpinfo32 *pld,
93 struct ptrace_lwpinfo *pls);
95 typedef fpregset_t elfcore_fpregset_t;
96 typedef gregset_t elfcore_gregset_t;
97 typedef prpsinfo_t elfcore_prpsinfo_t;
98 typedef prstatus_t elfcore_prstatus_t;
99 typedef struct ptrace_lwpinfo elfcore_lwpinfo_t;
100 #define elf_convert_lwpinfo(d,s) *d = *s
103 typedef void* (*notefunc_t)(void *, size_t *);
105 static void cb_put_phdr(struct map_entry *, void *);
106 static void cb_size_segment(struct map_entry *, void *);
107 static void each_dumpable_segment(struct map_entry *, segment_callback,
109 static void elf_detach(void); /* atexit() handler. */
110 static void *elf_note_prpsinfo(void *, size_t *);
111 static void *elf_note_thrmisc(void *, size_t *);
112 static void *elf_note_ptlwpinfo(void *, size_t *);
113 #if defined(__i386__) || defined(__amd64__)
114 static void *elf_note_x86_xstate(void *, size_t *);
116 #if defined(__powerpc__)
117 static void *elf_note_powerpc_vmx(void *, size_t *);
118 static void *elf_note_powerpc_vsx(void *, size_t *);
120 static void *elf_note_procstat_auxv(void *, size_t *);
121 static void *elf_note_procstat_files(void *, size_t *);
122 static void *elf_note_procstat_groups(void *, size_t *);
123 static void *elf_note_procstat_osrel(void *, size_t *);
124 static void *elf_note_procstat_proc(void *, size_t *);
125 static void *elf_note_procstat_psstrings(void *, size_t *);
126 static void *elf_note_procstat_rlimit(void *, size_t *);
127 static void *elf_note_procstat_umask(void *, size_t *);
128 static void *elf_note_procstat_vmmap(void *, size_t *);
129 static void elf_puthdr(int, pid_t, struct map_entry *, void *, size_t, size_t,
131 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
132 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
133 static void elf_putregnote(int, lwpid_t, struct sbuf *);
134 static void freemap(struct map_entry *);
135 static struct map_entry *readmap(pid_t);
136 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
138 static pid_t g_pid; /* Pid being dumped, global for elf_detach */
139 static int g_status; /* proc status after ptrace attach */
142 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
148 cnt = read(efd, &hdr, sizeof(hdr));
149 if (cnt != sizeof(hdr))
153 switch (hdr.e_ident[EI_DATA]) {
155 machine = le16toh(hdr.e_machine);
158 machine = be16toh(hdr.e_machine);
163 if (!ELF_MACHINE_OK(machine))
177 * Forward any pending signals. SIGSTOP is generated by ptrace
178 * itself, so ignore it.
180 sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0;
183 ptrace(PT_DETACH, g_pid, (caddr_t)1, sig);
188 * Write an ELF coredump for the given pid to the given fd.
191 elf_coredump(int efd, int fd, pid_t pid)
193 struct map_entry *map;
194 struct sseg_closure seginfo;
197 size_t hdrsize, notesz, segoff;
202 /* Attach to process to dump. */
204 if (atexit(elf_detach) != 0)
207 ptrace(PT_ATTACH, pid, NULL, 0);
210 if (waitpid(pid, &g_status, 0) == -1)
213 /* Get the program's memory map. */
216 /* Size the program segments. */
219 each_dumpable_segment(map, cb_size_segment, &seginfo);
222 * Build the header and the notes using sbuf and write to the file.
224 sb = sbuf_new_auto();
225 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
226 if (seginfo.count + 1 >= PN_XNUM)
227 hdrsize += sizeof(Elf_Shdr);
228 /* Start header + notes section. */
229 sbuf_start_section(sb, NULL);
230 /* Make empty header subsection. */
231 sbuf_start_section(sb, &old_len);
233 sbuf_end_section(sb, old_len, hdrsize, 0);
235 elf_putnotes(pid, sb, ¬esz);
236 /* Align up to a page boundary for the program segments. */
237 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
238 if (sbuf_finish(sb) != 0)
239 err(1, "sbuf_finish");
241 segoff = sbuf_len(sb);
242 /* Fill in the header. */
243 elf_puthdr(efd, pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
245 n = write(fd, hdr, segoff);
249 errx(1, "short write");
251 /* Write the contents of all of the writable segments. */
252 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
253 for (i = 0; i < seginfo.count; i++) {
254 struct ptrace_io_desc iorequest;
255 uintmax_t nleft = php->p_filesz;
257 iorequest.piod_op = PIOD_READ_D;
258 iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
264 if (nleft > sizeof(buf))
268 iorequest.piod_addr = buf;
269 iorequest.piod_len = nwant;
270 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
271 ngot = iorequest.piod_len;
272 if ((size_t)ngot < nwant)
273 errx(1, "short read wanted %zu, got %zd",
275 ngot = write(fd, buf, nwant);
277 err(1, "write of segment %d failed", i);
278 if ((size_t)ngot != nwant)
279 errx(1, "short write");
281 iorequest.piod_offs += ngot;
290 * A callback for each_dumpable_segment() to write out the segment's
291 * program header entry.
294 cb_put_phdr(struct map_entry *entry, void *closure)
296 struct phdr_closure *phc = (struct phdr_closure *)closure;
297 Elf_Phdr *phdr = phc->phdr;
299 phc->offset = round_page(phc->offset);
301 phdr->p_type = PT_LOAD;
302 phdr->p_offset = phc->offset;
303 phdr->p_vaddr = entry->start;
305 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
306 phdr->p_align = PAGE_SIZE;
308 if (entry->protection & VM_PROT_READ)
309 phdr->p_flags |= PF_R;
310 if (entry->protection & VM_PROT_WRITE)
311 phdr->p_flags |= PF_W;
312 if (entry->protection & VM_PROT_EXECUTE)
313 phdr->p_flags |= PF_X;
315 phc->offset += phdr->p_filesz;
320 * A callback for each_dumpable_segment() to gather information about
321 * the number of segments and their total size.
324 cb_size_segment(struct map_entry *entry, void *closure)
326 struct sseg_closure *ssc = (struct sseg_closure *)closure;
329 ssc->size += entry->end - entry->start;
333 * For each segment in the given memory map, call the given function
334 * with a pointer to the map entry and some arbitrary caller-supplied
338 each_dumpable_segment(struct map_entry *map, segment_callback func,
341 struct map_entry *entry;
343 for (entry = map; entry != NULL; entry = entry->next)
344 (*func)(entry, closure);
348 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
351 size_t threads, old_len;
356 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
358 err(1, "PT_GETNUMLWPS");
359 tids = malloc(threads * sizeof(*tids));
361 errx(1, "out of memory");
363 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
365 err(1, "PT_GETLWPLIST");
367 sbuf_start_section(sb, &old_len);
368 elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
370 for (i = 0; i < threads; ++i) {
371 elf_putregnote(NT_PRSTATUS, tids[i], sb);
372 elf_putregnote(NT_FPREGSET, tids[i], sb);
373 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
374 elf_putnote(NT_PTLWPINFO, elf_note_ptlwpinfo, tids + i, sb);
375 #if defined(__aarch64__) || defined(__arm__)
376 elf_putregnote(NT_ARM_TLS, tids[i], sb);
378 #if (defined(ELFCORE_COMPAT_32) && defined(__aarch64__)) || defined(__arm__)
379 elf_putregnote(NT_ARM_VFP, tids[i], sb);
381 #if defined(__i386__) || defined(__amd64__)
382 elf_putregnote(NT_X86_SEGBASES, tids[i], sb);
383 elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
385 #if defined(__powerpc__)
386 elf_putnote(NT_PPC_VMX, elf_note_powerpc_vmx, tids + i, sb);
388 elf_putnote(NT_PPC_VSX, elf_note_powerpc_vsx, tids + i, sb);
393 #ifndef ELFCORE_COMPAT_32
394 elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
395 elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
396 elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
397 elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
398 elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
399 elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
400 elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
401 elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
403 elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
406 size = sbuf_end_section(sb, old_len, 1, 0);
408 err(1, "sbuf_end_section");
414 * Emit one register set note section to sbuf.
417 elf_putregnote(int type, lwpid_t tid, struct sbuf *sb)
425 if (ptrace(PT_GETREGSET, tid, (void *)&iov, type) != 0)
427 iov.iov_base = calloc(1, iov.iov_len);
428 if (iov.iov_base == NULL)
429 errx(1, "out of memory");
430 if (ptrace(PT_GETREGSET, tid, (void *)&iov, type) != 0)
431 errx(1, "failed to fetch register set %d", type);
433 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
434 note.n_descsz = iov.iov_len;
437 sbuf_bcat(sb, ¬e, sizeof(note));
438 sbuf_start_section(sb, &old_len);
439 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
440 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
441 sbuf_start_section(sb, &old_len);
442 sbuf_bcat(sb, iov.iov_base, iov.iov_len);
443 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
448 * Emit one note section to sbuf.
451 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
458 desc = notefunc(arg, &descsz);
459 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
460 note.n_descsz = descsz;
463 sbuf_bcat(sb, ¬e, sizeof(note));
464 sbuf_start_section(sb, &old_len);
465 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
466 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
469 sbuf_start_section(sb, &old_len);
470 sbuf_bcat(sb, desc, descsz);
471 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
476 * Generate the ELF coredump header.
479 elf_puthdr(int efd, pid_t pid, struct map_entry *map, void *hdr, size_t hdrsize,
480 size_t notesz, size_t segoff, int numsegs)
482 Elf_Ehdr *ehdr, binhdr;
485 struct phdr_closure phc;
488 cnt = read(efd, &binhdr, sizeof(binhdr));
490 err(1, "Failed to re-read ELF header");
491 else if (cnt != sizeof(binhdr))
492 errx(1, "Failed to re-read ELF header");
494 ehdr = (Elf_Ehdr *)hdr;
496 ehdr->e_ident[EI_MAG0] = ELFMAG0;
497 ehdr->e_ident[EI_MAG1] = ELFMAG1;
498 ehdr->e_ident[EI_MAG2] = ELFMAG2;
499 ehdr->e_ident[EI_MAG3] = ELFMAG3;
500 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
501 ehdr->e_ident[EI_DATA] = ELF_DATA;
502 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
503 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
504 ehdr->e_ident[EI_ABIVERSION] = 0;
505 ehdr->e_ident[EI_PAD] = 0;
506 ehdr->e_type = ET_CORE;
507 ehdr->e_machine = binhdr.e_machine;
508 ehdr->e_version = EV_CURRENT;
510 ehdr->e_phoff = sizeof(Elf_Ehdr);
511 ehdr->e_flags = binhdr.e_flags;
512 ehdr->e_ehsize = sizeof(Elf_Ehdr);
513 ehdr->e_phentsize = sizeof(Elf_Phdr);
514 ehdr->e_shentsize = sizeof(Elf_Shdr);
515 ehdr->e_shstrndx = SHN_UNDEF;
516 if (numsegs + 1 < PN_XNUM) {
517 ehdr->e_phnum = numsegs + 1;
520 ehdr->e_phnum = PN_XNUM;
523 ehdr->e_shoff = ehdr->e_phoff +
524 (numsegs + 1) * ehdr->e_phentsize;
526 shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
527 memset(shdr, 0, sizeof(*shdr));
529 * A special first section is used to hold large segment and
530 * section counts. This was proposed by Sun Microsystems in
531 * Solaris and has been adopted by Linux; the standard ELF
532 * tools are already familiar with the technique.
534 * See table 7-7 of the Solaris "Linker and Libraries Guide"
535 * (or 12-7 depending on the version of the document) for more
538 shdr->sh_type = SHT_NULL;
539 shdr->sh_size = ehdr->e_shnum;
540 shdr->sh_link = ehdr->e_shstrndx;
541 shdr->sh_info = numsegs + 1;
545 * Fill in the program header entries.
547 phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
549 /* The note segment. */
550 phdr->p_type = PT_NOTE;
551 phdr->p_offset = hdrsize;
554 phdr->p_filesz = notesz;
556 phdr->p_flags = PF_R;
557 phdr->p_align = sizeof(Elf32_Size);
560 /* All the writable segments from the program. */
563 each_dumpable_segment(map, cb_put_phdr, &phc);
567 * Free the memory map.
570 freemap(struct map_entry *map)
572 struct map_entry *next;
574 while (map != NULL) {
582 * Read the process's memory map using kinfo_getvmmap(), and return a list of
583 * VM map entries. Only the non-device read/writable segments are
584 * returned. The map entries in the list aren't fully filled in; only
585 * the items we need are present.
587 static struct map_entry *
590 struct map_entry *ent, **linkp, *map;
591 struct kinfo_vmentry *vmentl, *kve;
594 vmentl = kinfo_getvmmap(pid, &nitems);
596 err(1, "cannot retrieve mappings for %u process", pid);
600 for (i = 0; i < nitems; i++) {
604 * Ignore 'malformed' segments or ones representing memory
605 * mapping with MAP_NOCORE on.
606 * If the 'full' support is disabled, just dump the most
607 * meaningful data segments.
609 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
610 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
611 kve->kve_type == KVME_TYPE_DEAD ||
612 kve->kve_type == KVME_TYPE_UNKNOWN ||
613 ((pflags & PFLAGS_FULL) == 0 &&
614 kve->kve_type != KVME_TYPE_DEFAULT &&
615 kve->kve_type != KVME_TYPE_VNODE &&
616 kve->kve_type != KVME_TYPE_SWAP &&
617 kve->kve_type != KVME_TYPE_PHYS))
620 ent = calloc(1, sizeof(*ent));
622 errx(1, "out of memory");
623 ent->start = (vm_offset_t)kve->kve_start;
624 ent->end = (vm_offset_t)kve->kve_end;
625 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
626 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
627 ent->protection |= VM_PROT_EXECUTE;
637 * Miscellaneous note out functions.
641 elf_note_prpsinfo(void *arg, size_t *sizep)
645 elfcore_prpsinfo_t *psinfo;
646 struct kinfo_proc kip;
651 psinfo = calloc(1, sizeof(*psinfo));
653 errx(1, "out of memory");
654 psinfo->pr_version = PRPSINFO_VERSION;
655 psinfo->pr_psinfosz = sizeof(*psinfo);
659 name[2] = KERN_PROC_PID;
662 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
663 err(1, "kern.proc.pid.%u", pid);
664 if (kip.ki_pid != pid)
665 err(1, "kern.proc.pid.%u", pid);
666 strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname));
667 name[2] = KERN_PROC_ARGS;
668 len = sizeof(psinfo->pr_psargs) - 1;
669 if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) {
670 cp = psinfo->pr_psargs;
673 cp = memchr(cp, '\0', end - cp);
679 strlcpy(psinfo->pr_psargs, kip.ki_comm,
680 sizeof(psinfo->pr_psargs));
681 psinfo->pr_pid = pid;
683 *sizep = sizeof(*psinfo);
688 elf_note_thrmisc(void *arg, size_t *sizep)
691 struct ptrace_lwpinfo lwpinfo;
694 tid = *(lwpid_t *)arg;
695 thrmisc = calloc(1, sizeof(*thrmisc));
697 errx(1, "out of memory");
698 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
700 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
701 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
703 *sizep = sizeof(*thrmisc);
708 elf_note_ptlwpinfo(void *arg, size_t *sizep)
711 elfcore_lwpinfo_t *elf_info;
712 struct ptrace_lwpinfo lwpinfo;
715 tid = *(lwpid_t *)arg;
716 p = calloc(1, sizeof(int) + sizeof(elfcore_lwpinfo_t));
718 errx(1, "out of memory");
719 *(int *)p = sizeof(elfcore_lwpinfo_t);
720 elf_info = (void *)((int *)p + 1);
721 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, sizeof(lwpinfo));
722 elf_convert_lwpinfo(elf_info, &lwpinfo);
724 *sizep = sizeof(int) + sizeof(struct ptrace_lwpinfo);
730 elf_note_arm_vfp(void *arg, size_t *sizep)
734 static bool has_vfp = true;
737 tid = *(lwpid_t *)arg;
739 if (ptrace(PT_GETVFPREGS, tid, (void *)&info, 0) != 0)
746 vfp = calloc(1, sizeof(*vfp));
747 memcpy(vfp, &info, sizeof(*vfp));
748 *sizep = sizeof(*vfp);
753 #if defined(__i386__) || defined(__amd64__)
755 elf_note_x86_xstate(void *arg, size_t *sizep)
759 static bool xsave_checked = false;
760 static struct ptrace_xstate_info info;
762 tid = *(lwpid_t *)arg;
763 if (!xsave_checked) {
764 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
767 xsave_checked = true;
769 if (info.xsave_len == 0) {
773 xstate = calloc(1, info.xsave_len);
774 ptrace(PT_GETXSTATE, tid, xstate, 0);
775 *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
776 *sizep = info.xsave_len;
781 #if defined(__powerpc__)
783 elf_note_powerpc_vmx(void *arg, size_t *sizep)
787 static bool has_vmx = true;
790 tid = *(lwpid_t *)arg;
792 if (ptrace(PT_GETVRREGS, tid, (void *)&info,
800 vmx = calloc(1, sizeof(*vmx));
801 memcpy(vmx, &info, sizeof(*vmx));
802 *sizep = sizeof(*vmx);
807 elf_note_powerpc_vsx(void *arg, size_t *sizep)
811 static bool has_vsx = true;
814 tid = *(lwpid_t *)arg;
816 if (ptrace(PT_GETVSRREGS, tid, (void *)vshr,
824 vshr_data = calloc(1, sizeof(vshr));
825 memcpy(vshr_data, vshr, sizeof(vshr));
826 *sizep = sizeof(vshr);
832 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
836 int name[4], structsize;
840 structsize = structsz;
846 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
847 err(1, "kern.proc.%d.%u", what, pid);
848 buf = calloc(1, sizeof(structsize) + len * 4 / 3);
850 errx(1, "out of memory");
851 bcopy(&structsize, buf, sizeof(structsize));
852 p = (char *)buf + sizeof(structsize);
853 if (sysctl(name, 4, p, &len, NULL, 0) == -1)
854 err(1, "kern.proc.%d.%u", what, pid);
856 *sizep = sizeof(structsize) + len;
861 elf_note_procstat_proc(void *arg, size_t *sizep)
864 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
865 sizeof(struct kinfo_proc), sizep));
869 elf_note_procstat_files(void *arg, size_t *sizep)
872 return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
873 sizeof(struct kinfo_file), sizep));
877 elf_note_procstat_vmmap(void *arg, size_t *sizep)
880 return (procstat_sysctl(arg, KERN_PROC_VMMAP,
881 sizeof(struct kinfo_vmentry), sizep));
885 elf_note_procstat_groups(void *arg, size_t *sizep)
888 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
892 elf_note_procstat_umask(void *arg, size_t *sizep)
895 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
899 elf_note_procstat_osrel(void *arg, size_t *sizep)
902 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
906 elf_note_procstat_psstrings(void *arg, size_t *sizep)
909 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
910 sizeof(vm_offset_t), sizep));
914 elf_note_procstat_auxv(void *arg, size_t *sizep)
917 return (procstat_sysctl(arg, KERN_PROC_AUXV,
918 sizeof(Elf_Auxinfo), sizep));
922 elf_note_procstat_rlimit(void *arg, size_t *sizep)
926 int i, name[5], structsize;
930 structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
931 buf = calloc(1, sizeof(structsize) + structsize);
933 errx(1, "out of memory");
934 bcopy(&structsize, buf, sizeof(structsize));
935 p = (char *)buf + sizeof(structsize);
938 name[2] = KERN_PROC_RLIMIT;
940 len = sizeof(struct rlimit);
941 for (i = 0; i < RLIM_NLIMITS; i++) {
943 if (sysctl(name, 5, p, &len, NULL, 0) == -1)
944 err(1, "kern.proc.rlimit.%u", pid);
945 if (len != sizeof(struct rlimit))
946 errx(1, "kern.proc.rlimit.%u: short read", pid);
950 *sizep = sizeof(structsize) + structsize;
954 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
955 TEXT_SET(dumpset, __elfN(dump));