2 * Copyright (c) 2007 Sandvine Incorporated
3 * Copyright (c) 1998 John D. Polstra
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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>
38 #include <sys/sysctl.h>
41 #include <machine/elf.h>
42 #include <vm/vm_param.h>
45 #include <vm/vm_map.h>
61 * Code for generating ELF core dumps.
64 typedef void (*segment_callback)(vm_map_entry_t, void *);
66 /* Closure for cb_put_phdr(). */
68 Elf_Phdr *phdr; /* Program header to fill in */
69 Elf_Off offset; /* Offset of segment in core file */
72 /* Closure for cb_size_segment(). */
74 int count; /* Count of writable segments. */
75 size_t size; /* Total size of all writable segments. */
78 #ifdef ELFCORE_COMPAT_32
79 typedef struct fpreg32 elfcore_fpregset_t;
80 typedef struct reg32 elfcore_gregset_t;
81 typedef struct prpsinfo32 elfcore_prpsinfo_t;
82 typedef struct prstatus32 elfcore_prstatus_t;
83 static void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs);
84 static void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs);
86 typedef fpregset_t elfcore_fpregset_t;
87 typedef gregset_t elfcore_gregset_t;
88 typedef prpsinfo_t elfcore_prpsinfo_t;
89 typedef prstatus_t elfcore_prstatus_t;
90 #define elf_convert_gregset(d,s) *d = *s
91 #define elf_convert_fpregset(d,s) *d = *s
94 typedef void* (*notefunc_t)(void *, size_t *);
96 static void cb_put_phdr(vm_map_entry_t, void *);
97 static void cb_size_segment(vm_map_entry_t, void *);
98 static void each_writable_segment(vm_map_entry_t, segment_callback,
100 static void elf_detach(void); /* atexit() handler. */
101 static void *elf_note_fpregset(void *, size_t *);
102 static void *elf_note_prpsinfo(void *, size_t *);
103 static void *elf_note_prstatus(void *, size_t *);
104 static void *elf_note_thrmisc(void *, size_t *);
105 #if defined(__i386__) || defined(__amd64__)
106 static void *elf_note_x86_xstate(void *, size_t *);
108 static void *elf_note_procstat_auxv(void *, size_t *);
109 static void *elf_note_procstat_files(void *, size_t *);
110 static void *elf_note_procstat_groups(void *, size_t *);
111 static void *elf_note_procstat_osrel(void *, size_t *);
112 static void *elf_note_procstat_proc(void *, size_t *);
113 static void *elf_note_procstat_psstrings(void *, size_t *);
114 static void *elf_note_procstat_rlimit(void *, size_t *);
115 static void *elf_note_procstat_umask(void *, size_t *);
116 static void *elf_note_procstat_vmmap(void *, size_t *);
117 static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
119 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
120 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
121 static void freemap(vm_map_entry_t);
122 static vm_map_entry_t readmap(pid_t);
123 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
125 static pid_t g_pid; /* Pid being dumped, global for elf_detach */
128 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
134 cnt = read(efd, &hdr, sizeof(hdr));
135 if (cnt != sizeof(hdr))
139 switch (hdr.e_ident[EI_DATA]) {
141 machine = le16toh(hdr.e_machine);
144 machine = be16toh(hdr.e_machine);
149 if (!ELF_MACHINE_OK(machine))
161 ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
165 * Write an ELF coredump for the given pid to the given fd.
168 elf_coredump(int efd __unused, int fd, pid_t pid)
171 struct sseg_closure seginfo;
174 size_t hdrsize, notesz, segoff;
179 /* Attach to process to dump. */
181 if (atexit(elf_detach) != 0)
184 ptrace(PT_ATTACH, pid, NULL, 0);
187 if (waitpid(pid, NULL, 0) == -1)
190 /* Get the program's memory map. */
193 /* Size the program segments. */
196 each_writable_segment(map, cb_size_segment, &seginfo);
199 * Build the header and the notes using sbuf and write to the file.
201 sb = sbuf_new_auto();
202 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
203 /* Start header + notes section. */
204 sbuf_start_section(sb, NULL);
205 /* Make empty header subsection. */
206 sbuf_start_section(sb, &old_len);
208 sbuf_end_section(sb, old_len, hdrsize, 0);
210 elf_putnotes(pid, sb, ¬esz);
211 /* Align up to a page boundary for the program segments. */
212 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
213 if (sbuf_finish(sb) != 0)
214 err(1, "sbuf_finish");
216 segoff = sbuf_len(sb);
217 /* Fill in the header. */
218 elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
220 n = write(fd, hdr, segoff);
224 errx(1, "short write");
226 /* Write the contents of all of the writable segments. */
227 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
228 for (i = 0; i < seginfo.count; i++) {
229 struct ptrace_io_desc iorequest;
230 uintmax_t nleft = php->p_filesz;
232 iorequest.piod_op = PIOD_READ_D;
233 iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
239 if (nleft > sizeof(buf))
243 iorequest.piod_addr = buf;
244 iorequest.piod_len = nwant;
245 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
246 ngot = iorequest.piod_len;
247 if ((size_t)ngot < nwant)
248 errx(1, "short read wanted %zu, got %zd",
250 ngot = write(fd, buf, nwant);
252 err(1, "write of segment %d failed", i);
253 if ((size_t)ngot != nwant)
254 errx(1, "short write");
256 iorequest.piod_offs += ngot;
265 * A callback for each_writable_segment() to write out the segment's
266 * program header entry.
269 cb_put_phdr(vm_map_entry_t entry, void *closure)
271 struct phdr_closure *phc = (struct phdr_closure *)closure;
272 Elf_Phdr *phdr = phc->phdr;
274 phc->offset = round_page(phc->offset);
276 phdr->p_type = PT_LOAD;
277 phdr->p_offset = phc->offset;
278 phdr->p_vaddr = entry->start;
280 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
281 phdr->p_align = PAGE_SIZE;
283 if (entry->protection & VM_PROT_READ)
284 phdr->p_flags |= PF_R;
285 if (entry->protection & VM_PROT_WRITE)
286 phdr->p_flags |= PF_W;
287 if (entry->protection & VM_PROT_EXECUTE)
288 phdr->p_flags |= PF_X;
290 phc->offset += phdr->p_filesz;
295 * A callback for each_writable_segment() to gather information about
296 * the number of segments and their total size.
299 cb_size_segment(vm_map_entry_t entry, void *closure)
301 struct sseg_closure *ssc = (struct sseg_closure *)closure;
304 ssc->size += entry->end - entry->start;
308 * For each segment in the given memory map, call the given function
309 * with a pointer to the map entry and some arbitrary caller-supplied
313 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
315 vm_map_entry_t entry;
317 for (entry = map; entry != NULL; entry = entry->next)
318 (*func)(entry, closure);
322 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
325 size_t threads, old_len;
330 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
332 err(1, "PT_GETNUMLWPS");
333 tids = malloc(threads * sizeof(*tids));
335 errx(1, "out of memory");
337 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
339 err(1, "PT_GETLWPLIST");
341 sbuf_start_section(sb, &old_len);
342 elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
344 for (i = 0; i < threads; ++i) {
345 elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
346 elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
347 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
348 #if defined(__i386__) || defined(__amd64__)
349 elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
353 #ifndef ELFCORE_COMPAT_32
354 elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
355 elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
356 elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
357 elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
358 elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
359 elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
360 elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
361 elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
363 elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
366 size = sbuf_end_section(sb, old_len, 1, 0);
368 err(1, "sbuf_end_section");
374 * Emit one note section to sbuf.
377 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
384 desc = notefunc(arg, &descsz);
385 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
386 note.n_descsz = descsz;
389 sbuf_bcat(sb, ¬e, sizeof(note));
390 sbuf_start_section(sb, &old_len);
391 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
392 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
395 sbuf_start_section(sb, &old_len);
396 sbuf_bcat(sb, desc, descsz);
397 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
402 * Generate the ELF coredump header.
405 elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
406 size_t notesz, size_t segoff, int numsegs)
410 struct phdr_closure phc;
412 ehdr = (Elf_Ehdr *)hdr;
413 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
415 ehdr->e_ident[EI_MAG0] = ELFMAG0;
416 ehdr->e_ident[EI_MAG1] = ELFMAG1;
417 ehdr->e_ident[EI_MAG2] = ELFMAG2;
418 ehdr->e_ident[EI_MAG3] = ELFMAG3;
419 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
420 ehdr->e_ident[EI_DATA] = ELF_DATA;
421 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
422 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
423 ehdr->e_ident[EI_ABIVERSION] = 0;
424 ehdr->e_ident[EI_PAD] = 0;
425 ehdr->e_type = ET_CORE;
426 ehdr->e_machine = ELF_ARCH;
427 ehdr->e_version = EV_CURRENT;
429 ehdr->e_phoff = sizeof(Elf_Ehdr);
431 ehdr->e_ehsize = sizeof(Elf_Ehdr);
432 ehdr->e_phentsize = sizeof(Elf_Phdr);
433 ehdr->e_phnum = numsegs + 1;
434 ehdr->e_shentsize = sizeof(Elf_Shdr);
436 ehdr->e_shstrndx = SHN_UNDEF;
439 * Fill in the program header entries.
442 /* The note segement. */
443 phdr->p_type = PT_NOTE;
444 phdr->p_offset = hdrsize;
447 phdr->p_filesz = notesz;
449 phdr->p_flags = PF_R;
450 phdr->p_align = sizeof(Elf32_Size);
453 /* All the writable segments from the program. */
456 each_writable_segment(map, cb_put_phdr, &phc);
460 * Free the memory map.
463 freemap(vm_map_entry_t map)
466 while (map != NULL) {
467 vm_map_entry_t next = map->next;
474 * Read the process's memory map using kinfo_getvmmap(), and return a list of
475 * VM map entries. Only the non-device read/writable segments are
476 * returned. The map entries in the list aren't fully filled in; only
477 * the items we need are present.
479 static vm_map_entry_t
482 vm_map_entry_t ent, *linkp, map;
483 struct kinfo_vmentry *vmentl, *kve;
486 vmentl = kinfo_getvmmap(pid, &nitems);
488 err(1, "cannot retrieve mappings for %u process", pid);
492 for (i = 0; i < nitems; i++) {
496 * Ignore 'malformed' segments or ones representing memory
497 * mapping with MAP_NOCORE on.
498 * If the 'full' support is disabled, just dump the most
499 * meaningful data segments.
501 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
502 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
503 kve->kve_type == KVME_TYPE_DEAD ||
504 kve->kve_type == KVME_TYPE_UNKNOWN ||
505 ((pflags & PFLAGS_FULL) == 0 &&
506 kve->kve_type != KVME_TYPE_DEFAULT &&
507 kve->kve_type != KVME_TYPE_VNODE &&
508 kve->kve_type != KVME_TYPE_SWAP &&
509 kve->kve_type != KVME_TYPE_PHYS))
512 ent = calloc(1, sizeof(*ent));
514 errx(1, "out of memory");
515 ent->start = (vm_offset_t)kve->kve_start;
516 ent->end = (vm_offset_t)kve->kve_end;
517 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
518 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
519 ent->protection |= VM_PROT_EXECUTE;
529 * Miscellaneous note out functions.
533 elf_note_prpsinfo(void *arg, size_t *sizep)
536 elfcore_prpsinfo_t *psinfo;
537 struct kinfo_proc kip;
542 psinfo = calloc(1, sizeof(*psinfo));
544 errx(1, "out of memory");
545 psinfo->pr_version = PRPSINFO_VERSION;
546 psinfo->pr_psinfosz = sizeof(*psinfo);
550 name[2] = KERN_PROC_PID;
553 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
554 err(1, "kern.proc.pid.%u", pid);
555 if (kip.ki_pid != pid)
556 err(1, "kern.proc.pid.%u", pid);
557 strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN);
558 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
560 *sizep = sizeof(*psinfo);
565 elf_note_prstatus(void *arg, size_t *sizep)
568 elfcore_prstatus_t *status;
571 tid = *(lwpid_t *)arg;
572 status = calloc(1, sizeof(*status));
574 errx(1, "out of memory");
575 status->pr_version = PRSTATUS_VERSION;
576 status->pr_statussz = sizeof(*status);
577 status->pr_gregsetsz = sizeof(elfcore_gregset_t);
578 status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
579 status->pr_osreldate = __FreeBSD_version;
580 status->pr_pid = tid;
581 ptrace(PT_GETREGS, tid, (void *)&greg, 0);
582 elf_convert_gregset(&status->pr_reg, &greg);
584 *sizep = sizeof(*status);
589 elf_note_fpregset(void *arg, size_t *sizep)
592 elfcore_fpregset_t *fpregset;
595 tid = *(lwpid_t *)arg;
596 fpregset = calloc(1, sizeof(*fpregset));
597 if (fpregset == NULL)
598 errx(1, "out of memory");
599 ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
600 elf_convert_fpregset(fpregset, &fpreg);
602 *sizep = sizeof(*fpregset);
607 elf_note_thrmisc(void *arg, size_t *sizep)
610 struct ptrace_lwpinfo lwpinfo;
613 tid = *(lwpid_t *)arg;
614 thrmisc = calloc(1, sizeof(*thrmisc));
616 errx(1, "out of memory");
617 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
619 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
620 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
622 *sizep = sizeof(*thrmisc);
626 #if defined(__i386__) || defined(__amd64__)
628 elf_note_x86_xstate(void *arg, size_t *sizep)
632 static bool xsave_checked = false;
633 static struct ptrace_xstate_info info;
635 tid = *(lwpid_t *)arg;
636 if (!xsave_checked) {
637 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
640 xsave_checked = true;
642 if (info.xsave_len == 0) {
646 xstate = calloc(1, info.xsave_len);
647 ptrace(PT_GETXSTATE, tid, xstate, 0);
648 *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
649 *sizep = info.xsave_len;
655 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
659 int name[4], structsize;
663 structsize = structsz;
669 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
670 err(1, "kern.proc.%d.%u", what, pid);
671 buf = calloc(1, sizeof(structsize) + len * 4 / 3);
673 errx(1, "out of memory");
674 bcopy(&structsize, buf, sizeof(structsize));
675 p = (char *)buf + sizeof(structsize);
676 if (sysctl(name, 4, p, &len, NULL, 0) == -1)
677 err(1, "kern.proc.%d.%u", what, pid);
679 *sizep = sizeof(structsize) + len;
684 elf_note_procstat_proc(void *arg, size_t *sizep)
687 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
688 sizeof(struct kinfo_proc), sizep));
692 elf_note_procstat_files(void *arg, size_t *sizep)
695 return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
696 sizeof(struct kinfo_file), sizep));
700 elf_note_procstat_vmmap(void *arg, size_t *sizep)
703 return (procstat_sysctl(arg, KERN_PROC_VMMAP,
704 sizeof(struct kinfo_vmentry), sizep));
708 elf_note_procstat_groups(void *arg, size_t *sizep)
711 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
715 elf_note_procstat_umask(void *arg, size_t *sizep)
718 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
722 elf_note_procstat_osrel(void *arg, size_t *sizep)
725 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
729 elf_note_procstat_psstrings(void *arg, size_t *sizep)
732 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
733 sizeof(vm_offset_t), sizep));
737 elf_note_procstat_auxv(void *arg, size_t *sizep)
740 return (procstat_sysctl(arg, KERN_PROC_AUXV,
741 sizeof(Elf_Auxinfo), sizep));
745 elf_note_procstat_rlimit(void *arg, size_t *sizep)
749 int i, name[5], structsize;
753 structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
754 buf = calloc(1, sizeof(structsize) + structsize);
756 errx(1, "out of memory");
757 bcopy(&structsize, buf, sizeof(structsize));
758 p = (char *)buf + sizeof(structsize);
761 name[2] = KERN_PROC_RLIMIT;
763 len = sizeof(struct rlimit);
764 for (i = 0; i < RLIM_NLIMITS; i++) {
766 if (sysctl(name, 5, p, &len, NULL, 0) == -1)
767 err(1, "kern.proc.rlimit.%u", pid);
768 if (len != sizeof(struct rlimit))
769 errx(1, "kern.proc.rlimit.%u: short read", pid);
773 *sizep = sizeof(structsize) + structsize;
777 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
778 TEXT_SET(dumpset, __elfN(dump));