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>
36 #include <sys/sysctl.h>
39 #include <machine/elf.h>
40 #include <vm/vm_param.h>
43 #include <vm/vm_map.h>
57 * Code for generating ELF core dumps.
60 typedef void (*segment_callback)(vm_map_entry_t, void *);
62 /* Closure for cb_put_phdr(). */
64 Elf_Phdr *phdr; /* Program header to fill in */
65 Elf_Off offset; /* Offset of segment in core file */
68 /* Closure for cb_size_segment(). */
70 int count; /* Count of writable segments. */
71 size_t size; /* Total size of all writable segments. */
74 static void cb_put_phdr(vm_map_entry_t, void *);
75 static void cb_size_segment(vm_map_entry_t, void *);
76 static void each_writable_segment(vm_map_entry_t, segment_callback,
78 static void elf_detach(void); /* atexit() handler. */
79 static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t *, int numsegs);
80 static void elf_putnote(void *dst, size_t *off, const char *name, int type,
81 const void *desc, size_t descsz);
82 static void freemap(vm_map_entry_t);
83 static vm_map_entry_t readmap(pid_t);
85 static pid_t g_pid; /* Pid being dumped, global for elf_detach */
88 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
93 cnt = read(efd, &hdr, sizeof(hdr));
94 if (cnt != sizeof(hdr))
106 ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
110 * Write an ELF coredump for the given pid to the given fd.
113 elf_coredump(int efd __unused, int fd, pid_t pid)
116 struct sseg_closure seginfo;
122 /* Attach to process to dump. */
124 if (atexit(elf_detach) != 0)
127 ptrace(PT_ATTACH, pid, NULL, 0);
130 if (waitpid(pid, NULL, 0) == -1)
133 /* Get the program's memory map. */
136 /* Size the program segments. */
139 each_writable_segment(map, cb_size_segment, &seginfo);
142 * Calculate the size of the core file header area by making
143 * a dry run of generating it. Nothing is written, but the
144 * size is calculated.
147 elf_puthdr(pid, map, NULL, &hdrsize, seginfo.count);
150 * Allocate memory for building the header, fill it up,
153 if ((hdr = calloc(1, hdrsize)) == NULL)
154 errx(1, "out of memory");
156 /* Fill in the header. */
158 elf_puthdr(pid, map, hdr, &hdrsize, seginfo.count);
160 /* Write it to the core file. */
161 if (write(fd, hdr, hdrsize) == -1)
164 /* Write the contents of all of the writable segments. */
165 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
166 for (i = 0; i < seginfo.count; i++) {
167 struct ptrace_io_desc iorequest;
168 uintmax_t nleft = php->p_filesz;
170 iorequest.piod_op = PIOD_READ_D;
171 iorequest.piod_offs = (caddr_t)php->p_vaddr;
177 if (nleft > sizeof(buf))
181 iorequest.piod_addr = buf;
182 iorequest.piod_len = nwant;
183 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
184 ngot = iorequest.piod_len;
185 if ((size_t)ngot < nwant)
186 errx(1, "short read wanted %zu, got %zd",
188 ngot = write(fd, buf, nwant);
190 err(1, "write of segment %d failed", i);
191 if ((size_t)ngot != nwant)
192 errx(1, "short write");
194 iorequest.piod_offs += ngot;
203 * A callback for each_writable_segment() to write out the segment's
204 * program header entry.
207 cb_put_phdr(vm_map_entry_t entry, void *closure)
209 struct phdr_closure *phc = (struct phdr_closure *)closure;
210 Elf_Phdr *phdr = phc->phdr;
212 phc->offset = round_page(phc->offset);
214 phdr->p_type = PT_LOAD;
215 phdr->p_offset = phc->offset;
216 phdr->p_vaddr = entry->start;
218 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
219 phdr->p_align = PAGE_SIZE;
221 if (entry->protection & VM_PROT_READ)
222 phdr->p_flags |= PF_R;
223 if (entry->protection & VM_PROT_WRITE)
224 phdr->p_flags |= PF_W;
225 if (entry->protection & VM_PROT_EXECUTE)
226 phdr->p_flags |= PF_X;
228 phc->offset += phdr->p_filesz;
233 * A callback for each_writable_segment() to gather information about
234 * the number of segments and their total size.
237 cb_size_segment(vm_map_entry_t entry, void *closure)
239 struct sseg_closure *ssc = (struct sseg_closure *)closure;
242 ssc->size += entry->end - entry->start;
246 * For each segment in the given memory map, call the given function
247 * with a pointer to the map entry and some arbitrary caller-supplied
251 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
253 vm_map_entry_t entry;
255 for (entry = map; entry != NULL; entry = entry->next)
256 (*func)(entry, closure);
260 elf_getstatus(pid_t pid, prpsinfo_t *psinfo)
262 struct kinfo_proc kobj;
268 name[2] = KERN_PROC_PID;
272 if (sysctl(name, 4, &kobj, &len, NULL, 0) == -1)
273 err(1, "error accessing kern.proc.pid.%u sysctl", pid);
274 if (kobj.ki_pid != pid)
275 err(1, "error accessing kern.proc.pid.%u sysctl datas", pid);
276 strncpy(psinfo->pr_fname, kobj.ki_comm, MAXCOMLEN);
277 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
281 * Generate the ELF coredump header into the buffer at "dst". "dst" may
282 * be NULL, in which case the header is sized but not actually generated.
285 elf_puthdr(pid_t pid, vm_map_entry_t map, void *dst, size_t *off, int numsegs)
287 struct ptrace_lwpinfo lwpinfo;
290 prfpregset_t fpregset;
303 prfpregset_t *fpregset;
308 *off += sizeof(Elf_Ehdr);
311 *off += (numsegs + 1) * sizeof(Elf_Phdr);
316 if ((tempdata = calloc(1, sizeof(*tempdata))) == NULL)
317 errx(1, "out of memory");
318 status = &tempdata->status;
319 fpregset = &tempdata->fpregset;
320 psinfo = &tempdata->psinfo;
321 thrmisc = &tempdata->thrmisc;
331 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
333 err(1, "PT_GETNUMLWPS");
336 psinfo->pr_version = PRPSINFO_VERSION;
337 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
338 elf_getstatus(pid, psinfo);
341 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
345 tids = malloc(threads * sizeof(*tids));
347 errx(1, "out of memory");
349 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
351 err(1, "PT_GETLWPLIST");
353 for (i = 0; i < threads; ++i) {
355 status->pr_version = PRSTATUS_VERSION;
356 status->pr_statussz = sizeof(prstatus_t);
357 status->pr_gregsetsz = sizeof(gregset_t);
358 status->pr_fpregsetsz = sizeof(fpregset_t);
359 status->pr_osreldate = __FreeBSD_version;
360 status->pr_pid = tids[i];
362 ptrace(PT_GETREGS, tids[i], (void *)&status->pr_reg, 0);
363 ptrace(PT_GETFPREGS, tids[i], (void *)fpregset, 0);
364 ptrace(PT_LWPINFO, tids[i], (void *)&lwpinfo,
366 memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
367 strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
369 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
371 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
373 elf_putnote(dst, off, "FreeBSD", NT_THRMISC, thrmisc,
377 notesz = *off - noteoff;
384 /* Align up to a page boundary for the program segments. */
385 *off = round_page(*off);
390 struct phdr_closure phc;
393 * Fill in the ELF header.
395 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
396 ehdr->e_ident[EI_MAG0] = ELFMAG0;
397 ehdr->e_ident[EI_MAG1] = ELFMAG1;
398 ehdr->e_ident[EI_MAG2] = ELFMAG2;
399 ehdr->e_ident[EI_MAG3] = ELFMAG3;
400 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
401 ehdr->e_ident[EI_DATA] = ELF_DATA;
402 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
403 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
404 ehdr->e_ident[EI_ABIVERSION] = 0;
405 ehdr->e_ident[EI_PAD] = 0;
406 ehdr->e_type = ET_CORE;
407 ehdr->e_machine = ELF_ARCH;
408 ehdr->e_version = EV_CURRENT;
410 ehdr->e_phoff = phoff;
412 ehdr->e_ehsize = sizeof(Elf_Ehdr);
413 ehdr->e_phentsize = sizeof(Elf_Phdr);
414 ehdr->e_phnum = numsegs + 1;
415 ehdr->e_shentsize = sizeof(Elf_Shdr);
417 ehdr->e_shstrndx = SHN_UNDEF;
420 * Fill in the program header entries.
422 phdr = (Elf_Phdr *)((char *)dst + phoff);
424 /* The note segment. */
425 phdr->p_type = PT_NOTE;
426 phdr->p_offset = noteoff;
429 phdr->p_filesz = notesz;
435 /* All the writable segments from the program. */
438 each_writable_segment(map, cb_put_phdr, &phc);
443 * Emit one note section to "dst", or just size it if "dst" is NULL.
446 elf_putnote(void *dst, size_t *off, const char *name, int type,
447 const void *desc, size_t descsz)
451 note.n_namesz = strlen(name) + 1;
452 note.n_descsz = descsz;
455 bcopy(¬e, (char *)dst + *off, sizeof note);
458 bcopy(name, (char *)dst + *off, note.n_namesz);
459 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
461 bcopy(desc, (char *)dst + *off, note.n_descsz);
462 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
466 * Free the memory map.
469 freemap(vm_map_entry_t map)
472 while (map != NULL) {
473 vm_map_entry_t next = map->next;
480 * Read the process's memory map using kinfo_getvmmap(), and return a list of
481 * VM map entries. Only the non-device read/writable segments are
482 * returned. The map entries in the list aren't fully filled in; only
483 * the items we need are present.
485 static vm_map_entry_t
488 vm_map_entry_t ent, *linkp, map;
489 struct kinfo_vmentry *vmentl, *kve;
492 vmentl = kinfo_getvmmap(pid, &nitems);
494 err(1, "cannot retrieve mappings for %u process", pid);
498 for (i = 0; i < nitems; i++) {
502 * Ignore 'malformed' segments or ones representing memory
503 * mapping with MAP_NOCORE on.
504 * If the 'full' support is disabled, just dump the most
505 * meaningful data segments.
507 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
508 (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
509 kve->kve_type == KVME_TYPE_DEAD ||
510 kve->kve_type == KVME_TYPE_UNKNOWN ||
511 ((pflags & PFLAGS_FULL) == 0 &&
512 kve->kve_type != KVME_TYPE_DEFAULT &&
513 kve->kve_type != KVME_TYPE_VNODE &&
514 kve->kve_type != KVME_TYPE_SWAP))
517 ent = calloc(1, sizeof(*ent));
519 errx(1, "out of memory");
520 ent->start = (vm_offset_t)kve->kve_start;
521 ent->end = (vm_offset_t)kve->kve_end;
522 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
523 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
524 ent->protection |= VM_PROT_EXECUTE;
533 struct dumpers elfdump = { elf_ident, elf_coredump };
534 TEXT_SET(dumpset, elfdump);