2 * Copyright (c) 1998 John D. Polstra
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/procfs.h>
32 #include <sys/queue.h>
33 #include <sys/linker_set.h>
34 #include <machine/elf.h>
35 #include <vm/vm_param.h>
38 #include <vm/vm_map.h>
51 * Code for generating ELF core dumps.
54 typedef void (*segment_callback)(vm_map_entry_t, void *);
56 /* Closure for cb_put_phdr(). */
58 Elf_Phdr *phdr; /* Program header to fill in */
59 Elf_Off offset; /* Offset of segment in core file */
62 /* Closure for cb_size_segment(). */
64 int count; /* Count of writable segments. */
65 size_t size; /* Total size of all writable segments. */
68 static void cb_put_phdr(vm_map_entry_t, void *);
69 static void cb_size_segment(vm_map_entry_t, void *);
70 static void each_writable_segment(vm_map_entry_t, segment_callback,
72 static void elf_corehdr(int fd, pid_t, vm_map_entry_t, int numsegs,
73 void *hdr, size_t hdrsize);
74 static void elf_puthdr(vm_map_entry_t, void *, size_t *,
75 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int numsegs);
76 static void elf_putnote(void *dst, size_t *off, const char *name, int type,
77 const void *desc, size_t descsz);
78 static void freemap(vm_map_entry_t);
79 static void readhdrinfo(pid_t, prstatus_t *, prfpregset_t *, prpsinfo_t *);
80 static vm_map_entry_t readmap(pid_t);
83 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
88 cnt = read(efd, &hdr, sizeof(hdr));
89 if (cnt != sizeof(hdr))
97 * Write an ELF coredump for the given pid to the given fd.
100 elf_coredump(int efd __unused, int fd, pid_t pid)
103 struct sseg_closure seginfo;
111 /* Get the program's memory map. */
114 /* Size the program segments. */
117 each_writable_segment(map, cb_size_segment, &seginfo);
120 * Calculate the size of the core file header area by making
121 * a dry run of generating it. Nothing is written, but the
122 * size is calculated.
125 elf_puthdr(map, (void *)NULL, &hdrsize,
126 (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
127 (const prpsinfo_t *)NULL, seginfo.count);
130 * Allocate memory for building the header, fill it up,
133 if ((hdr = malloc(hdrsize)) == NULL)
134 errx(1, "out of memory");
135 elf_corehdr(fd, pid, map, seginfo.count, hdr, hdrsize);
137 /* Write the contents of all of the writable segments. */
138 snprintf(memname, sizeof memname, "/proc/%d/mem", pid);
139 if ((memfd = open(memname, O_RDONLY)) == -1)
140 err(1, "cannot open %s", memname);
142 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
143 for (i = 0; i < seginfo.count; i++) {
144 uintmax_t nleft = php->p_filesz;
146 lseek(memfd, (off_t)php->p_vaddr, SEEK_SET);
152 if (nleft > sizeof(buf))
156 ngot = read(memfd, buf, nwant);
158 err(1, "read from %s", memname);
159 if ((size_t)ngot < nwant)
160 errx(1, "short read from %s:"
161 " wanted %zd, got %zd", memname,
163 ngot = write(fd, buf, nwant);
165 err(1, "write of segment %d failed", i);
166 if ((size_t)ngot != nwant)
167 errx(1, "short write");
178 * A callback for each_writable_segment() to write out the segment's
179 * program header entry.
182 cb_put_phdr(vm_map_entry_t entry, void *closure)
184 struct phdr_closure *phc = (struct phdr_closure *)closure;
185 Elf_Phdr *phdr = phc->phdr;
187 phc->offset = round_page(phc->offset);
189 phdr->p_type = PT_LOAD;
190 phdr->p_offset = phc->offset;
191 phdr->p_vaddr = entry->start;
193 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
194 phdr->p_align = PAGE_SIZE;
196 if (entry->protection & VM_PROT_READ)
197 phdr->p_flags |= PF_R;
198 if (entry->protection & VM_PROT_WRITE)
199 phdr->p_flags |= PF_W;
200 if (entry->protection & VM_PROT_EXECUTE)
201 phdr->p_flags |= PF_X;
203 phc->offset += phdr->p_filesz;
208 * A callback for each_writable_segment() to gather information about
209 * the number of segments and their total size.
212 cb_size_segment(vm_map_entry_t entry, void *closure)
214 struct sseg_closure *ssc = (struct sseg_closure *)closure;
217 ssc->size += entry->end - entry->start;
221 * For each segment in the given memory map, call the given function
222 * with a pointer to the map entry and some arbitrary caller-supplied
226 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
228 vm_map_entry_t entry;
230 for (entry = map; entry != NULL; entry = entry->next)
231 (*func)(entry, closure);
235 * Write the core file header to the file, including padding up to
239 elf_corehdr(int fd, pid_t pid, vm_map_entry_t map, int numsegs, void *hdr,
244 prfpregset_t fpregset;
247 /* Gather the information for the header. */
248 readhdrinfo(pid, &status, &fpregset, &psinfo);
250 /* Fill in the header. */
251 memset(hdr, 0, hdrsize);
253 elf_puthdr(map, hdr, &off, &status, &fpregset, &psinfo, numsegs);
255 /* Write it to the core file. */
256 if (write(fd, hdr, hdrsize) == -1)
261 * Generate the ELF coredump header into the buffer at "dst". "dst" may
262 * be NULL, in which case the header is sized but not actually generated.
265 elf_puthdr(vm_map_entry_t map, void *dst, size_t *off, const prstatus_t *status,
266 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
274 *off += sizeof(Elf_Ehdr);
277 *off += (numsegs + 1) * sizeof(Elf_Phdr);
280 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
282 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
284 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
286 notesz = *off - noteoff;
288 /* Align up to a page boundary for the program segments. */
289 *off = round_page(*off);
294 struct phdr_closure phc;
297 * Fill in the ELF header.
299 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
300 ehdr->e_ident[EI_MAG0] = ELFMAG0;
301 ehdr->e_ident[EI_MAG1] = ELFMAG1;
302 ehdr->e_ident[EI_MAG2] = ELFMAG2;
303 ehdr->e_ident[EI_MAG3] = ELFMAG3;
304 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
305 ehdr->e_ident[EI_DATA] = ELF_DATA;
306 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
307 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
308 ehdr->e_ident[EI_ABIVERSION] = 0;
309 ehdr->e_ident[EI_PAD] = 0;
310 ehdr->e_type = ET_CORE;
311 ehdr->e_machine = ELF_ARCH;
312 ehdr->e_version = EV_CURRENT;
314 ehdr->e_phoff = phoff;
316 ehdr->e_ehsize = sizeof(Elf_Ehdr);
317 ehdr->e_phentsize = sizeof(Elf_Phdr);
318 ehdr->e_phnum = numsegs + 1;
319 ehdr->e_shentsize = sizeof(Elf_Shdr);
321 ehdr->e_shstrndx = SHN_UNDEF;
324 * Fill in the program header entries.
326 phdr = (Elf_Phdr *)((char *)dst + phoff);
328 /* The note segment. */
329 phdr->p_type = PT_NOTE;
330 phdr->p_offset = noteoff;
333 phdr->p_filesz = notesz;
339 /* All the writable segments from the program. */
342 each_writable_segment(map, cb_put_phdr, &phc);
347 * Emit one note section to "dst", or just size it if "dst" is NULL.
350 elf_putnote(void *dst, size_t *off, const char *name, int type,
351 const void *desc, size_t descsz)
355 note.n_namesz = strlen(name) + 1;
356 note.n_descsz = descsz;
359 bcopy(¬e, (char *)dst + *off, sizeof note);
362 bcopy(name, (char *)dst + *off, note.n_namesz);
363 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
365 bcopy(desc, (char *)dst + *off, note.n_descsz);
366 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
370 * Free the memory map.
373 freemap(vm_map_entry_t map)
376 while (map != NULL) {
377 vm_map_entry_t next = map->next;
384 * Read the process information necessary to fill in the core file's header.
387 readhdrinfo(pid_t pid, prstatus_t *status, prfpregset_t *fpregset,
396 memset(status, 0, sizeof *status);
397 status->pr_version = PRSTATUS_VERSION;
398 status->pr_statussz = sizeof(prstatus_t);
399 status->pr_gregsetsz = sizeof(gregset_t);
400 status->pr_fpregsetsz = sizeof(fpregset_t);
401 status->pr_osreldate = __FreeBSD_version;
402 status->pr_pid = pid;
404 memset(fpregset, 0, sizeof *fpregset);
406 memset(psinfo, 0, sizeof *psinfo);
407 psinfo->pr_version = PRPSINFO_VERSION;
408 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
410 /* Read the general registers. */
411 snprintf(name, sizeof name, "/proc/%d/regs", pid);
412 if ((fd = open(name, O_RDONLY)) == -1)
413 err(1, "cannot open %s", name);
414 if ((n = read(fd, &status->pr_reg, sizeof status->pr_reg)) == -1)
415 err(1, "read error from %s", name);
416 if ((size_t)n < sizeof(status->pr_reg))
417 errx(1, "short read from %s: wanted %zd, got %d", name,
418 sizeof status->pr_reg, n);
421 /* Read the floating point registers. */
422 snprintf(name, sizeof name, "/proc/%d/fpregs", pid);
423 if ((fd = open(name, O_RDONLY)) == -1)
424 err(1, "cannot open %s", name);
425 if ((n = read(fd, fpregset, sizeof *fpregset)) == -1)
426 err(1, "read error from %s", name);
427 if ((size_t)n < sizeof(*fpregset))
428 errx(1, "short read from %s: wanted %zd, got %d", name,
429 sizeof *fpregset, n);
432 /* Read and parse the process status. */
433 snprintf(name, sizeof name, "/proc/%d/status", pid);
434 if ((fd = open(name, O_RDONLY)) == -1)
435 err(1, "cannot open %s", name);
436 if ((n = read(fd, line, sizeof line - 1)) == -1)
437 err(1, "read error from %s", name);
440 for (i = 0; i < n && line[i] != ' '; i++)
441 psinfo->pr_fname[i] = line[i];
442 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
447 * Read the process's memory map using procfs, and return a list of
448 * VM map entries. Only the non-device read/writable segments are
449 * returned. The map entries in the list aren't fully filled in; only
450 * the items we need are present.
452 static vm_map_entry_t
462 vm_map_entry_t *linkp;
464 snprintf(mapname, sizeof mapname, "/proc/%d/map", pid);
465 if ((mapfd = open(mapname, O_RDONLY)) == -1)
466 err(1, "cannot open %s", mapname);
469 * Procfs requires (for consistency) that the entire memory map
470 * be read with a single read() call. Start with a reasonably sized
471 * buffer, and double it until it is big enough.
476 if ((mapbuf = realloc(mapbuf, bufsize + 1)) == NULL)
477 errx(1, "out of memory");
478 mapsize = read(mapfd, mapbuf, bufsize);
479 if (mapsize != -1 || errno != EFBIG)
482 /* This lseek shouldn't be necessary, but it is. */
483 lseek(mapfd, (off_t)0, SEEK_SET);
486 err(1, "read error from %s", mapname);
488 errx(1, "empty map file %s", mapname);
495 while (pos < mapsize) {
505 n = sscanf(mapbuf + pos, "%lx %lx %*d %*d %*x %3[-rwx]"
506 " %*d %*d %*x %*s %*s %16s %*s%*[\n]%n",
507 &start, &end, prot, type, &len);
508 if (n != 4 || len == 0)
509 errx(1, "ill-formed line in %s starting at character %d", mapname, pos + 1);
512 /* Ignore segments of the wrong kind, and unwritable ones */
513 if (strncmp(prot, "rw", 2) != 0 ||
514 (strcmp(type, "default") != 0 &&
515 strcmp(type, "vnode") != 0 &&
516 strcmp(type, "swap") != 0))
519 if ((ent = (vm_map_entry_t)calloc(1, sizeof *ent)) == NULL)
520 errx(1, "out of memory");
523 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
525 ent->protection |= VM_PROT_EXECUTE;
534 struct dumpers elfdump = { elf_ident, elf_coredump };
535 TEXT_SET(dumpset, elfdump);