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 %d, got %d",
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)
289 prfpregset_t fpregset;
301 prfpregset_t *fpregset;
305 *off += sizeof(Elf_Ehdr);
308 *off += (numsegs + 1) * sizeof(Elf_Phdr);
313 if ((tempdata = calloc(1, sizeof(*tempdata))) == NULL)
314 errx(1, "out of memory");
315 status = &tempdata->status;
316 fpregset = &tempdata->fpregset;
317 psinfo = &tempdata->psinfo;
326 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
328 err(1, "PT_GETNUMLWPS");
331 psinfo->pr_version = PRPSINFO_VERSION;
332 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
333 elf_getstatus(pid, psinfo);
336 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
340 tids = malloc(threads * sizeof(*tids));
342 errx(1, "out of memory");
344 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
346 err(1, "PT_GETLWPLIST");
348 for (i = 0; i < threads; ++i) {
350 status->pr_version = PRSTATUS_VERSION;
351 status->pr_statussz = sizeof(prstatus_t);
352 status->pr_gregsetsz = sizeof(gregset_t);
353 status->pr_fpregsetsz = sizeof(fpregset_t);
354 status->pr_osreldate = __FreeBSD_version;
355 status->pr_pid = tids[i];
357 ptrace(PT_GETREGS, tids[i], (void *)&status->pr_reg, 0);
358 ptrace(PT_GETFPREGS, tids[i], (void *)fpregset, 0);
360 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
362 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
366 notesz = *off - noteoff;
373 /* Align up to a page boundary for the program segments. */
374 *off = round_page(*off);
379 struct phdr_closure phc;
382 * Fill in the ELF header.
384 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
385 ehdr->e_ident[EI_MAG0] = ELFMAG0;
386 ehdr->e_ident[EI_MAG1] = ELFMAG1;
387 ehdr->e_ident[EI_MAG2] = ELFMAG2;
388 ehdr->e_ident[EI_MAG3] = ELFMAG3;
389 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
390 ehdr->e_ident[EI_DATA] = ELF_DATA;
391 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
392 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
393 ehdr->e_ident[EI_ABIVERSION] = 0;
394 ehdr->e_ident[EI_PAD] = 0;
395 ehdr->e_type = ET_CORE;
396 ehdr->e_machine = ELF_ARCH;
397 ehdr->e_version = EV_CURRENT;
399 ehdr->e_phoff = phoff;
401 ehdr->e_ehsize = sizeof(Elf_Ehdr);
402 ehdr->e_phentsize = sizeof(Elf_Phdr);
403 ehdr->e_phnum = numsegs + 1;
404 ehdr->e_shentsize = sizeof(Elf_Shdr);
406 ehdr->e_shstrndx = SHN_UNDEF;
409 * Fill in the program header entries.
411 phdr = (Elf_Phdr *)((char *)dst + phoff);
413 /* The note segment. */
414 phdr->p_type = PT_NOTE;
415 phdr->p_offset = noteoff;
418 phdr->p_filesz = notesz;
424 /* All the writable segments from the program. */
427 each_writable_segment(map, cb_put_phdr, &phc);
432 * Emit one note section to "dst", or just size it if "dst" is NULL.
435 elf_putnote(void *dst, size_t *off, const char *name, int type,
436 const void *desc, size_t descsz)
440 note.n_namesz = strlen(name) + 1;
441 note.n_descsz = descsz;
444 bcopy(¬e, (char *)dst + *off, sizeof note);
447 bcopy(name, (char *)dst + *off, note.n_namesz);
448 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
450 bcopy(desc, (char *)dst + *off, note.n_descsz);
451 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
455 * Free the memory map.
458 freemap(vm_map_entry_t map)
461 while (map != NULL) {
462 vm_map_entry_t next = map->next;
469 * Read the process's memory map using kinfo_getvmmap(), and return a list of
470 * VM map entries. Only the non-device read/writable segments are
471 * returned. The map entries in the list aren't fully filled in; only
472 * the items we need are present.
474 static vm_map_entry_t
477 vm_map_entry_t ent, *linkp, map;
478 struct kinfo_vmentry *vmentl, *kve;
481 vmentl = kinfo_getvmmap(pid, &nitems);
483 err(1, "cannot retrieve mappings for %u process", pid);
487 for (i = 0; i < nitems; i++) {
491 * Ignore segments of the wrong kind and ones which are not
492 * readable and writable.
494 if ((kve->kve_protection & KVME_PROT_WRITE) == 0 ||
495 (kve->kve_protection & KVME_PROT_READ) == 0 ||
496 (kve->kve_type != KVME_TYPE_DEFAULT &&
497 kve->kve_type != KVME_TYPE_VNODE &&
498 kve->kve_type != KVME_TYPE_SWAP))
501 ent = calloc(1, sizeof(*ent));
503 errx(1, "out of memory");
504 ent->start = (vm_offset_t)kve->kve_start;
505 ent->end = (vm_offset_t)kve->kve_end;
506 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
507 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
508 ent->protection |= VM_PROT_EXECUTE;
517 struct dumpers elfdump = { elf_ident, elf_coredump };
518 TEXT_SET(dumpset, elfdump);