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/param.h>
32 #include <sys/procfs.h>
33 #include <sys/ptrace.h>
34 #include <sys/queue.h>
35 #include <sys/linker_set.h>
37 #include <sys/sysctl.h>
40 #include <machine/elf.h>
41 #include <vm/vm_param.h>
44 #include <vm/vm_map.h>
59 * Code for generating ELF core dumps.
62 typedef void (*segment_callback)(vm_map_entry_t, void *);
64 /* Closure for cb_put_phdr(). */
66 Elf_Phdr *phdr; /* Program header to fill in */
67 Elf_Off offset; /* Offset of segment in core file */
70 /* Closure for cb_size_segment(). */
72 int count; /* Count of writable segments. */
73 size_t size; /* Total size of all writable segments. */
76 typedef void* (*notefunc_t)(void *, size_t *);
78 static void cb_put_phdr(vm_map_entry_t, void *);
79 static void cb_size_segment(vm_map_entry_t, void *);
80 static void each_writable_segment(vm_map_entry_t, segment_callback,
82 static void elf_detach(void); /* atexit() handler. */
83 static void *elf_note_fpregset(void *, size_t *);
84 static void *elf_note_prpsinfo(void *, size_t *);
85 static void *elf_note_prstatus(void *, size_t *);
86 static void *elf_note_thrmisc(void *, size_t *);
87 static void *elf_note_procstat_auxv(void *, size_t *);
88 static void *elf_note_procstat_files(void *, size_t *);
89 static void *elf_note_procstat_groups(void *, size_t *);
90 static void *elf_note_procstat_osrel(void *, size_t *);
91 static void *elf_note_procstat_proc(void *, size_t *);
92 static void *elf_note_procstat_psstrings(void *, size_t *);
93 static void *elf_note_procstat_rlimit(void *, size_t *);
94 static void *elf_note_procstat_umask(void *, size_t *);
95 static void *elf_note_procstat_vmmap(void *, size_t *);
96 static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
98 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
99 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
100 static void freemap(vm_map_entry_t);
101 static vm_map_entry_t readmap(pid_t);
102 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
104 static pid_t g_pid; /* Pid being dumped, global for elf_detach */
107 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
112 cnt = read(efd, &hdr, sizeof(hdr));
113 if (cnt != sizeof(hdr))
125 ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
129 * Write an ELF coredump for the given pid to the given fd.
132 elf_coredump(int efd __unused, int fd, pid_t pid)
135 struct sseg_closure seginfo;
138 size_t hdrsize, notesz, segoff;
143 /* Attach to process to dump. */
145 if (atexit(elf_detach) != 0)
148 ptrace(PT_ATTACH, pid, NULL, 0);
151 if (waitpid(pid, NULL, 0) == -1)
154 /* Get the program's memory map. */
157 /* Size the program segments. */
160 each_writable_segment(map, cb_size_segment, &seginfo);
163 * Build the header and the notes using sbuf and write to the file.
165 sb = sbuf_new_auto();
166 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
167 /* Start header + notes section. */
168 sbuf_start_section(sb, NULL);
169 /* Make empty header subsection. */
170 sbuf_start_section(sb, &old_len);
172 sbuf_end_section(sb, old_len, hdrsize, 0);
174 elf_putnotes(pid, sb, ¬esz);
175 /* Align up to a page boundary for the program segments. */
176 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
177 if (sbuf_finish(sb) != 0)
178 err(1, "sbuf_finish");
180 segoff = sbuf_len(sb);
181 /* Fill in the header. */
182 elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
184 n = write(fd, hdr, segoff);
188 errx(1, "short write");
190 /* Write the contents of all of the writable segments. */
191 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
192 for (i = 0; i < seginfo.count; i++) {
193 struct ptrace_io_desc iorequest;
194 uintmax_t nleft = php->p_filesz;
196 iorequest.piod_op = PIOD_READ_D;
197 iorequest.piod_offs = (caddr_t)php->p_vaddr;
203 if (nleft > sizeof(buf))
207 iorequest.piod_addr = buf;
208 iorequest.piod_len = nwant;
209 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
210 ngot = iorequest.piod_len;
211 if ((size_t)ngot < nwant)
212 errx(1, "short read wanted %zu, got %zd",
214 ngot = write(fd, buf, nwant);
216 err(1, "write of segment %d failed", i);
217 if ((size_t)ngot != nwant)
218 errx(1, "short write");
220 iorequest.piod_offs += ngot;
229 * A callback for each_writable_segment() to write out the segment's
230 * program header entry.
233 cb_put_phdr(vm_map_entry_t entry, void *closure)
235 struct phdr_closure *phc = (struct phdr_closure *)closure;
236 Elf_Phdr *phdr = phc->phdr;
238 phc->offset = round_page(phc->offset);
240 phdr->p_type = PT_LOAD;
241 phdr->p_offset = phc->offset;
242 phdr->p_vaddr = entry->start;
244 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
245 phdr->p_align = PAGE_SIZE;
247 if (entry->protection & VM_PROT_READ)
248 phdr->p_flags |= PF_R;
249 if (entry->protection & VM_PROT_WRITE)
250 phdr->p_flags |= PF_W;
251 if (entry->protection & VM_PROT_EXECUTE)
252 phdr->p_flags |= PF_X;
254 phc->offset += phdr->p_filesz;
259 * A callback for each_writable_segment() to gather information about
260 * the number of segments and their total size.
263 cb_size_segment(vm_map_entry_t entry, void *closure)
265 struct sseg_closure *ssc = (struct sseg_closure *)closure;
268 ssc->size += entry->end - entry->start;
272 * For each segment in the given memory map, call the given function
273 * with a pointer to the map entry and some arbitrary caller-supplied
277 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
279 vm_map_entry_t entry;
281 for (entry = map; entry != NULL; entry = entry->next)
282 (*func)(entry, closure);
286 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
289 size_t threads, old_len;
294 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
296 err(1, "PT_GETNUMLWPS");
297 tids = malloc(threads * sizeof(*tids));
299 errx(1, "out of memory");
301 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
303 err(1, "PT_GETLWPLIST");
305 sbuf_start_section(sb, &old_len);
306 elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
308 for (i = 0; i < threads; ++i) {
309 elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
310 elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
311 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
314 elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
315 elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
316 elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
317 elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
318 elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
319 elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
320 elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
321 elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
323 elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
325 size = sbuf_end_section(sb, old_len, 1, 0);
327 err(1, "sbuf_end_section");
333 * Emit one note section to sbuf.
336 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
343 desc = notefunc(arg, &descsz);
344 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
345 note.n_descsz = descsz;
348 sbuf_bcat(sb, ¬e, sizeof(note));
349 sbuf_start_section(sb, &old_len);
350 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
351 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
354 sbuf_start_section(sb, &old_len);
355 sbuf_bcat(sb, desc, descsz);
356 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
361 * Generate the ELF coredump header.
364 elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
365 size_t notesz, size_t segoff, int numsegs)
369 struct phdr_closure phc;
371 ehdr = (Elf_Ehdr *)hdr;
372 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
374 ehdr->e_ident[EI_MAG0] = ELFMAG0;
375 ehdr->e_ident[EI_MAG1] = ELFMAG1;
376 ehdr->e_ident[EI_MAG2] = ELFMAG2;
377 ehdr->e_ident[EI_MAG3] = ELFMAG3;
378 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
379 ehdr->e_ident[EI_DATA] = ELF_DATA;
380 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
381 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
382 ehdr->e_ident[EI_ABIVERSION] = 0;
383 ehdr->e_ident[EI_PAD] = 0;
384 ehdr->e_type = ET_CORE;
385 ehdr->e_machine = ELF_ARCH;
386 ehdr->e_version = EV_CURRENT;
388 ehdr->e_phoff = sizeof(Elf_Ehdr);
390 ehdr->e_ehsize = sizeof(Elf_Ehdr);
391 ehdr->e_phentsize = sizeof(Elf_Phdr);
392 ehdr->e_phnum = numsegs + 1;
393 ehdr->e_shentsize = sizeof(Elf_Shdr);
395 ehdr->e_shstrndx = SHN_UNDEF;
398 * Fill in the program header entries.
401 /* The note segement. */
402 phdr->p_type = PT_NOTE;
403 phdr->p_offset = hdrsize;
406 phdr->p_filesz = notesz;
408 phdr->p_flags = PF_R;
409 phdr->p_align = sizeof(Elf32_Size);
412 /* All the writable segments from the program. */
415 each_writable_segment(map, cb_put_phdr, &phc);
419 * Free the memory map.
422 freemap(vm_map_entry_t map)
425 while (map != NULL) {
426 vm_map_entry_t next = map->next;
433 * Read the process's memory map using kinfo_getvmmap(), and return a list of
434 * VM map entries. Only the non-device read/writable segments are
435 * returned. The map entries in the list aren't fully filled in; only
436 * the items we need are present.
438 static vm_map_entry_t
441 vm_map_entry_t ent, *linkp, map;
442 struct kinfo_vmentry *vmentl, *kve;
445 vmentl = kinfo_getvmmap(pid, &nitems);
447 err(1, "cannot retrieve mappings for %u process", pid);
451 for (i = 0; i < nitems; i++) {
455 * Ignore 'malformed' segments or ones representing memory
456 * mapping with MAP_NOCORE on.
457 * If the 'full' support is disabled, just dump the most
458 * meaningful data segments.
460 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
461 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
462 kve->kve_type == KVME_TYPE_DEAD ||
463 kve->kve_type == KVME_TYPE_UNKNOWN ||
464 ((pflags & PFLAGS_FULL) == 0 &&
465 kve->kve_type != KVME_TYPE_DEFAULT &&
466 kve->kve_type != KVME_TYPE_VNODE &&
467 kve->kve_type != KVME_TYPE_SWAP))
470 ent = calloc(1, sizeof(*ent));
472 errx(1, "out of memory");
473 ent->start = (vm_offset_t)kve->kve_start;
474 ent->end = (vm_offset_t)kve->kve_end;
475 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
476 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
477 ent->protection |= VM_PROT_EXECUTE;
487 * Miscellaneous note out functions.
491 elf_note_prpsinfo(void *arg, size_t *sizep)
495 struct kinfo_proc kip;
500 psinfo = calloc(1, sizeof(*psinfo));
502 errx(1, "out of memory");
503 psinfo->pr_version = PRPSINFO_VERSION;
504 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
508 name[2] = KERN_PROC_PID;
511 if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
512 err(1, "kern.proc.pid.%u", pid);
513 if (kip.ki_pid != pid)
514 err(1, "kern.proc.pid.%u", pid);
515 strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN);
516 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
518 *sizep = sizeof(*psinfo);
523 elf_note_prstatus(void *arg, size_t *sizep)
528 tid = *(lwpid_t *)arg;
529 status = calloc(1, sizeof(*status));
531 errx(1, "out of memory");
532 status->pr_version = PRSTATUS_VERSION;
533 status->pr_statussz = sizeof(prstatus_t);
534 status->pr_gregsetsz = sizeof(gregset_t);
535 status->pr_fpregsetsz = sizeof(fpregset_t);
536 status->pr_osreldate = __FreeBSD_version;
537 status->pr_pid = tid;
538 ptrace(PT_GETREGS, tid, (void *)&status->pr_reg, 0);
540 *sizep = sizeof(*status);
545 elf_note_fpregset(void *arg, size_t *sizep)
548 prfpregset_t *fpregset;
550 tid = *(lwpid_t *)arg;
551 fpregset = calloc(1, sizeof(*fpregset));
552 if (fpregset == NULL)
553 errx(1, "out of memory");
554 ptrace(PT_GETFPREGS, tid, (void *)fpregset, 0);
556 *sizep = sizeof(*fpregset);
561 elf_note_thrmisc(void *arg, size_t *sizep)
564 struct ptrace_lwpinfo lwpinfo;
567 tid = *(lwpid_t *)arg;
568 thrmisc = calloc(1, sizeof(*thrmisc));
570 errx(1, "out of memory");
571 ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
573 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
574 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
576 *sizep = sizeof(*thrmisc);
581 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
585 int name[4], structsize;
589 structsize = structsz;
595 if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
596 err(1, "kern.proc.%d.%u", what, pid);
597 buf = calloc(1, sizeof(structsize) + len * 4 / 3);
599 errx(1, "out of memory");
600 bcopy(&structsize, buf, sizeof(structsize));
601 p = (char *)buf + sizeof(structsize);
602 if (sysctl(name, 4, p, &len, NULL, 0) == -1)
603 err(1, "kern.proc.%d.%u", what, pid);
605 *sizep = sizeof(structsize) + len;
610 elf_note_procstat_proc(void *arg, size_t *sizep)
613 return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
614 sizeof(struct kinfo_proc), sizep));
618 elf_note_procstat_files(void *arg, size_t *sizep)
621 return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
622 sizeof(struct kinfo_file), sizep));
626 elf_note_procstat_vmmap(void *arg, size_t *sizep)
629 return (procstat_sysctl(arg, KERN_PROC_VMMAP,
630 sizeof(struct kinfo_vmentry), sizep));
634 elf_note_procstat_groups(void *arg, size_t *sizep)
637 return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
641 elf_note_procstat_umask(void *arg, size_t *sizep)
644 return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
648 elf_note_procstat_osrel(void *arg, size_t *sizep)
651 return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
655 elf_note_procstat_psstrings(void *arg, size_t *sizep)
658 return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
659 sizeof(vm_offset_t), sizep));
663 elf_note_procstat_auxv(void *arg, size_t *sizep)
666 return (procstat_sysctl(arg, KERN_PROC_AUXV,
667 sizeof(Elf_Auxinfo), sizep));
671 elf_note_procstat_rlimit(void *arg, size_t *sizep)
675 int i, name[5], structsize;
679 structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
680 buf = calloc(1, sizeof(structsize) + structsize);
682 errx(1, "out of memory");
683 bcopy(&structsize, buf, sizeof(structsize));
684 p = (char *)buf + sizeof(structsize);
687 name[2] = KERN_PROC_RLIMIT;
689 len = sizeof(struct rlimit);
690 for (i = 0; i < RLIM_NLIMITS; i++) {
692 if (sysctl(name, 5, p, &len, NULL, 0) == -1)
693 err(1, "kern.proc.rlimit.%u", pid);
694 if (len != sizeof(struct rlimit))
695 errx(1, "kern.proc.rlimit.%u: short read", pid);
699 *sizep = sizeof(structsize) + structsize;
703 struct dumpers elfdump = { elf_ident, elf_coredump };
704 TEXT_SET(dumpset, elfdump);