xref: /freebsd/usr.bin/gcore/elfcore.c (revision 7f08176ee8b672feebd8a12572b43dddcb88046e)
1 /*-
2  * Copyright (c) 2007 Sandvine Incorporated
3  * Copyright (c) 1998 John D. Polstra
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
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>
37 #include <sys/user.h>
38 #include <sys/wait.h>
39 #include <machine/elf.h>
40 #include <vm/vm_param.h>
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_map.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <libutil.h>
53 
54 #include "extern.h"
55 
56 /*
57  * Code for generating ELF core dumps.
58  */
59 
60 typedef void (*segment_callback)(vm_map_entry_t, void *);
61 
62 /* Closure for cb_put_phdr(). */
63 struct phdr_closure {
64 	Elf_Phdr *phdr;		/* Program header to fill in */
65 	Elf_Off offset;		/* Offset of segment in core file */
66 };
67 
68 /* Closure for cb_size_segment(). */
69 struct sseg_closure {
70 	int count;		/* Count of writable segments. */
71 	size_t size;		/* Total size of all writable segments. */
72 };
73 
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,
77     void *closure);
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);
84 
85 static pid_t g_pid;		/* Pid being dumped, global for elf_detach */
86 
87 static int
88 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
89 {
90 	Elf_Ehdr hdr;
91 	int cnt;
92 
93 	cnt = read(efd, &hdr, sizeof(hdr));
94 	if (cnt != sizeof(hdr))
95 		return (0);
96 	if (IS_ELF(hdr))
97 		return (1);
98 	return (0);
99 }
100 
101 static void
102 elf_detach(void)
103 {
104 
105 	if (g_pid != 0)
106 		ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
107 }
108 
109 /*
110  * Write an ELF coredump for the given pid to the given fd.
111  */
112 static void
113 elf_coredump(int efd __unused, int fd, pid_t pid)
114 {
115 	vm_map_entry_t map;
116 	struct sseg_closure seginfo;
117 	void *hdr;
118 	size_t hdrsize;
119 	Elf_Phdr *php;
120 	int i;
121 
122 	/* Attach to process to dump. */
123 	g_pid = pid;
124 	if (atexit(elf_detach) != 0)
125 		err(1, "atexit");
126 	errno = 0;
127 	ptrace(PT_ATTACH, pid, NULL, 0);
128 	if (errno)
129 		err(1, "PT_ATTACH");
130 	if (waitpid(pid, NULL, 0) == -1)
131 		err(1, "waitpid");
132 
133 	/* Get the program's memory map. */
134 	map = readmap(pid);
135 
136 	/* Size the program segments. */
137 	seginfo.count = 0;
138 	seginfo.size = 0;
139 	each_writable_segment(map, cb_size_segment, &seginfo);
140 
141 	/*
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.
145 	 */
146 	hdrsize = 0;
147 	elf_puthdr(pid, map, NULL, &hdrsize, seginfo.count);
148 
149 	/*
150 	 * Allocate memory for building the header, fill it up,
151 	 * and write it out.
152 	 */
153 	if ((hdr = calloc(1, hdrsize)) == NULL)
154 		errx(1, "out of memory");
155 
156 	/* Fill in the header. */
157 	hdrsize = 0;
158 	elf_puthdr(pid, map, hdr, &hdrsize, seginfo.count);
159 
160 	/* Write it to the core file. */
161 	if (write(fd, hdr, hdrsize) == -1)
162 		err(1, "write");
163 
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;
169 
170 		iorequest.piod_op = PIOD_READ_D;
171 		iorequest.piod_offs = (caddr_t)php->p_vaddr;
172 		while (nleft > 0) {
173 			char buf[8*1024];
174 			size_t nwant;
175 			ssize_t ngot;
176 
177 			if (nleft > sizeof(buf))
178 				nwant = sizeof buf;
179 			else
180 				nwant = nleft;
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",
187 				    nwant, ngot);
188 			ngot = write(fd, buf, nwant);
189 			if (ngot == -1)
190 				err(1, "write of segment %d failed", i);
191 			if ((size_t)ngot != nwant)
192 				errx(1, "short write");
193 			nleft -= nwant;
194 			iorequest.piod_offs += ngot;
195 		}
196 		php++;
197 	}
198 	free(hdr);
199 	freemap(map);
200 }
201 
202 /*
203  * A callback for each_writable_segment() to write out the segment's
204  * program header entry.
205  */
206 static void
207 cb_put_phdr(vm_map_entry_t entry, void *closure)
208 {
209 	struct phdr_closure *phc = (struct phdr_closure *)closure;
210 	Elf_Phdr *phdr = phc->phdr;
211 
212 	phc->offset = round_page(phc->offset);
213 
214 	phdr->p_type = PT_LOAD;
215 	phdr->p_offset = phc->offset;
216 	phdr->p_vaddr = entry->start;
217 	phdr->p_paddr = 0;
218 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
219 	phdr->p_align = PAGE_SIZE;
220 	phdr->p_flags = 0;
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;
227 
228 	phc->offset += phdr->p_filesz;
229 	phc->phdr++;
230 }
231 
232 /*
233  * A callback for each_writable_segment() to gather information about
234  * the number of segments and their total size.
235  */
236 static void
237 cb_size_segment(vm_map_entry_t entry, void *closure)
238 {
239 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
240 
241 	ssc->count++;
242 	ssc->size += entry->end - entry->start;
243 }
244 
245 /*
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
248  * data.
249  */
250 static void
251 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
252 {
253 	vm_map_entry_t entry;
254 
255 	for (entry = map;  entry != NULL;  entry = entry->next)
256 		(*func)(entry, closure);
257 }
258 
259 static void
260 elf_getstatus(pid_t pid, prpsinfo_t *psinfo)
261 {
262 	struct kinfo_proc kobj;
263 	int name[4];
264 	size_t len;
265 
266 	name[0] = CTL_KERN;
267 	name[1] = KERN_PROC;
268 	name[2] = KERN_PROC_PID;
269 	name[3] = pid;
270 
271 	len = sizeof(kobj);
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);
278 }
279 
280 /*
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.
283  */
284 static void
285 elf_puthdr(pid_t pid, vm_map_entry_t map, void *dst, size_t *off, int numsegs)
286 {
287 	struct ptrace_lwpinfo lwpinfo;
288 	struct {
289 		prstatus_t status;
290 		prfpregset_t fpregset;
291 		prpsinfo_t psinfo;
292 		thrmisc_t thrmisc;
293 	} *tempdata;
294 	size_t ehoff;
295 	size_t phoff;
296 	size_t noteoff;
297 	size_t notesz;
298 	size_t threads;
299 	lwpid_t *tids;
300 	int i;
301 
302 	prstatus_t *status;
303 	prfpregset_t *fpregset;
304 	prpsinfo_t *psinfo;
305 	thrmisc_t *thrmisc;
306 
307 	ehoff = *off;
308 	*off += sizeof(Elf_Ehdr);
309 
310 	phoff = *off;
311 	*off += (numsegs + 1) * sizeof(Elf_Phdr);
312 
313 	noteoff = *off;
314 
315 	if (dst != NULL) {
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;
322 	} else {
323 		tempdata = NULL;
324 		status = NULL;
325 		fpregset = NULL;
326 		psinfo = NULL;
327 		thrmisc = NULL;
328 	}
329 
330 	errno = 0;
331 	threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
332 	if (errno)
333 		err(1, "PT_GETNUMLWPS");
334 
335 	if (dst != NULL) {
336 		psinfo->pr_version = PRPSINFO_VERSION;
337 		psinfo->pr_psinfosz = sizeof(prpsinfo_t);
338 		elf_getstatus(pid, psinfo);
339 
340 	}
341 	elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
342 	    sizeof *psinfo);
343 
344 	if (dst != NULL) {
345 		tids = malloc(threads * sizeof(*tids));
346 		if (tids == NULL)
347 			errx(1, "out of memory");
348 		errno = 0;
349 		ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
350 		if (errno)
351 			err(1, "PT_GETLWPLIST");
352 	}
353 	for (i = 0; i < threads; ++i) {
354 		if (dst != NULL) {
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];
361 
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,
365 			    sizeof(lwpinfo));
366 			memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
367 			strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
368 		}
369 		elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
370 		    sizeof *status);
371 		elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
372 		    sizeof *fpregset);
373 		elf_putnote(dst, off, "FreeBSD", NT_THRMISC, thrmisc,
374 		    sizeof *thrmisc);
375 	}
376 
377 	notesz = *off - noteoff;
378 
379 	if (dst != NULL) {
380 		free(tids);
381 		free(tempdata);
382 	}
383 
384 	/* Align up to a page boundary for the program segments. */
385 	*off = round_page(*off);
386 
387 	if (dst != NULL) {
388 		Elf_Ehdr *ehdr;
389 		Elf_Phdr *phdr;
390 		struct phdr_closure phc;
391 
392 		/*
393 		 * Fill in the ELF header.
394 		 */
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;
409 		ehdr->e_entry = 0;
410 		ehdr->e_phoff = phoff;
411 		ehdr->e_flags = 0;
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);
416 		ehdr->e_shnum = 0;
417 		ehdr->e_shstrndx = SHN_UNDEF;
418 
419 		/*
420 		 * Fill in the program header entries.
421 		 */
422 		phdr = (Elf_Phdr *)((char *)dst + phoff);
423 
424 		/* The note segment. */
425 		phdr->p_type = PT_NOTE;
426 		phdr->p_offset = noteoff;
427 		phdr->p_vaddr = 0;
428 		phdr->p_paddr = 0;
429 		phdr->p_filesz = notesz;
430 		phdr->p_memsz = 0;
431 		phdr->p_flags = 0;
432 		phdr->p_align = 0;
433 		phdr++;
434 
435 		/* All the writable segments from the program. */
436 		phc.phdr = phdr;
437 		phc.offset = *off;
438 		each_writable_segment(map, cb_put_phdr, &phc);
439 	}
440 }
441 
442 /*
443  * Emit one note section to "dst", or just size it if "dst" is NULL.
444  */
445 static void
446 elf_putnote(void *dst, size_t *off, const char *name, int type,
447     const void *desc, size_t descsz)
448 {
449 	Elf_Note note;
450 
451 	note.n_namesz = strlen(name) + 1;
452 	note.n_descsz = descsz;
453 	note.n_type = type;
454 	if (dst != NULL)
455 		bcopy(&note, (char *)dst + *off, sizeof note);
456 	*off += sizeof note;
457 	if (dst != NULL)
458 		bcopy(name, (char *)dst + *off, note.n_namesz);
459 	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
460 	if (dst != NULL)
461 		bcopy(desc, (char *)dst + *off, note.n_descsz);
462 	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
463 }
464 
465 /*
466  * Free the memory map.
467  */
468 static void
469 freemap(vm_map_entry_t map)
470 {
471 
472 	while (map != NULL) {
473 		vm_map_entry_t next = map->next;
474 		free(map);
475 		map = next;
476 	}
477 }
478 
479 /*
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.
484  */
485 static vm_map_entry_t
486 readmap(pid_t pid)
487 {
488 	vm_map_entry_t ent, *linkp, map;
489 	struct kinfo_vmentry *vmentl, *kve;
490 	int i, nitems;
491 
492 	vmentl = kinfo_getvmmap(pid, &nitems);
493 	if (vmentl == NULL)
494 		err(1, "cannot retrieve mappings for %u process", pid);
495 
496 	map = NULL;
497 	linkp = &map;
498 	for (i = 0; i < nitems; i++) {
499 		kve = &vmentl[i];
500 
501 		/*
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.
506 		 */
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))
515 			continue;
516 
517 		ent = calloc(1, sizeof(*ent));
518 		if (ent == NULL)
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;
525 
526 		*linkp = ent;
527 		linkp = &ent->next;
528 	}
529 	free(vmentl);
530 	return (map);
531 }
532 
533 struct dumpers elfdump = { elf_ident, elf_coredump };
534 TEXT_SET(dumpset, elfdump);
535