xref: /freebsd/usr.bin/gcore/elfcore.c (revision 6966ac055c3b7a39266fb982493330df7a097997)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Dell EMC
5  * Copyright (c) 2007 Sandvine Incorporated
6  * Copyright (c) 1998 John D. Polstra
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/endian.h>
35 #include <sys/param.h>
36 #include <sys/procfs.h>
37 #include <sys/ptrace.h>
38 #include <sys/queue.h>
39 #include <sys/linker_set.h>
40 #include <sys/sbuf.h>
41 #include <sys/sysctl.h>
42 #include <sys/user.h>
43 #include <sys/wait.h>
44 #include <machine/elf.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm.h>
47 #include <assert.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdbool.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <libutil.h>
58 
59 #include "extern.h"
60 
61 /*
62  * Code for generating ELF core dumps.
63  */
64 
65 struct map_entry {
66 	struct map_entry *next;
67 	vm_offset_t start;
68 	vm_offset_t end;
69 	vm_prot_t protection;
70 };
71 
72 typedef void (*segment_callback)(struct map_entry *, void *);
73 
74 /* Closure for cb_put_phdr(). */
75 struct phdr_closure {
76 	Elf_Phdr *phdr;		/* Program header to fill in */
77 	Elf_Off offset;		/* Offset of segment in core file */
78 };
79 
80 /* Closure for cb_size_segment(). */
81 struct sseg_closure {
82 	int count;		/* Count of writable segments. */
83 	size_t size;		/* Total size of all writable segments. */
84 };
85 
86 #ifdef ELFCORE_COMPAT_32
87 typedef struct fpreg32 elfcore_fpregset_t;
88 typedef struct reg32   elfcore_gregset_t;
89 typedef struct prpsinfo32 elfcore_prpsinfo_t;
90 typedef struct prstatus32 elfcore_prstatus_t;
91 typedef struct ptrace_lwpinfo32 elfcore_lwpinfo_t;
92 static void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs);
93 static void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs);
94 static void elf_convert_lwpinfo(struct ptrace_lwpinfo32 *pld,
95     struct ptrace_lwpinfo *pls);
96 #else
97 typedef fpregset_t elfcore_fpregset_t;
98 typedef gregset_t  elfcore_gregset_t;
99 typedef prpsinfo_t elfcore_prpsinfo_t;
100 typedef prstatus_t elfcore_prstatus_t;
101 typedef struct ptrace_lwpinfo elfcore_lwpinfo_t;
102 #define elf_convert_gregset(d,s)	*d = *s
103 #define elf_convert_fpregset(d,s)	*d = *s
104 #define	elf_convert_lwpinfo(d,s)	*d = *s
105 #endif
106 
107 typedef void* (*notefunc_t)(void *, size_t *);
108 
109 static void cb_put_phdr(struct map_entry *, void *);
110 static void cb_size_segment(struct map_entry *, void *);
111 static void each_dumpable_segment(struct map_entry *, segment_callback,
112     void *closure);
113 static void elf_detach(void);	/* atexit() handler. */
114 static void *elf_note_fpregset(void *, size_t *);
115 static void *elf_note_prpsinfo(void *, size_t *);
116 static void *elf_note_prstatus(void *, size_t *);
117 static void *elf_note_thrmisc(void *, size_t *);
118 static void *elf_note_ptlwpinfo(void *, size_t *);
119 #if defined(__arm__)
120 static void *elf_note_arm_vfp(void *, size_t *);
121 #endif
122 #if defined(__i386__) || defined(__amd64__)
123 static void *elf_note_x86_xstate(void *, size_t *);
124 #endif
125 #if defined(__powerpc__)
126 static void *elf_note_powerpc_vmx(void *, size_t *);
127 static void *elf_note_powerpc_vsx(void *, size_t *);
128 #endif
129 static void *elf_note_procstat_auxv(void *, size_t *);
130 static void *elf_note_procstat_files(void *, size_t *);
131 static void *elf_note_procstat_groups(void *, size_t *);
132 static void *elf_note_procstat_osrel(void *, size_t *);
133 static void *elf_note_procstat_proc(void *, size_t *);
134 static void *elf_note_procstat_psstrings(void *, size_t *);
135 static void *elf_note_procstat_rlimit(void *, size_t *);
136 static void *elf_note_procstat_umask(void *, size_t *);
137 static void *elf_note_procstat_vmmap(void *, size_t *);
138 static void elf_puthdr(int, pid_t, struct map_entry *, void *, size_t, size_t,
139     size_t, int);
140 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
141 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
142 static void freemap(struct map_entry *);
143 static struct map_entry *readmap(pid_t);
144 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
145 
146 static pid_t g_pid;		/* Pid being dumped, global for elf_detach */
147 static int g_status;		/* proc status after ptrace attach */
148 
149 static int
150 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
151 {
152 	Elf_Ehdr hdr;
153 	int cnt;
154 	uint16_t machine;
155 
156 	cnt = read(efd, &hdr, sizeof(hdr));
157 	if (cnt != sizeof(hdr))
158 		return (0);
159 	if (!IS_ELF(hdr))
160 		return (0);
161 	switch (hdr.e_ident[EI_DATA]) {
162 	case ELFDATA2LSB:
163 		machine = le16toh(hdr.e_machine);
164 		break;
165 	case ELFDATA2MSB:
166 		machine = be16toh(hdr.e_machine);
167 		break;
168 	default:
169 		return (0);
170 	}
171 	if (!ELF_MACHINE_OK(machine))
172 		return (0);
173 
174 	/* Looks good. */
175 	return (1);
176 }
177 
178 static void
179 elf_detach(void)
180 {
181 	int sig;
182 
183 	if (g_pid != 0) {
184 		/*
185 		 * Forward any pending signals. SIGSTOP is generated by ptrace
186 		 * itself, so ignore it.
187 		 */
188 		sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0;
189 		if (sig == SIGSTOP)
190 			sig = 0;
191 		ptrace(PT_DETACH, g_pid, (caddr_t)1, sig);
192 	}
193 }
194 
195 /*
196  * Write an ELF coredump for the given pid to the given fd.
197  */
198 static void
199 elf_coredump(int efd, int fd, pid_t pid)
200 {
201 	struct map_entry *map;
202 	struct sseg_closure seginfo;
203 	struct sbuf *sb;
204 	void *hdr;
205 	size_t hdrsize, notesz, segoff;
206 	ssize_t n, old_len;
207 	Elf_Phdr *php;
208 	int i;
209 
210 	/* Attach to process to dump. */
211 	g_pid = pid;
212 	if (atexit(elf_detach) != 0)
213 		err(1, "atexit");
214 	errno = 0;
215 	ptrace(PT_ATTACH, pid, NULL, 0);
216 	if (errno)
217 		err(1, "PT_ATTACH");
218 	if (waitpid(pid, &g_status, 0) == -1)
219 		err(1, "waitpid");
220 
221 	/* Get the program's memory map. */
222 	map = readmap(pid);
223 
224 	/* Size the program segments. */
225 	seginfo.count = 0;
226 	seginfo.size = 0;
227 	each_dumpable_segment(map, cb_size_segment, &seginfo);
228 
229 	/*
230 	 * Build the header and the notes using sbuf and write to the file.
231 	 */
232 	sb = sbuf_new_auto();
233 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
234 	if (seginfo.count + 1 >= PN_XNUM)
235 		hdrsize += sizeof(Elf_Shdr);
236 	/* Start header + notes section. */
237 	sbuf_start_section(sb, NULL);
238 	/* Make empty header subsection. */
239 	sbuf_start_section(sb, &old_len);
240 	sbuf_putc(sb, 0);
241 	sbuf_end_section(sb, old_len, hdrsize, 0);
242 	/* Put notes. */
243 	elf_putnotes(pid, sb, &notesz);
244 	/* Align up to a page boundary for the program segments. */
245 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
246 	if (sbuf_finish(sb) != 0)
247 		err(1, "sbuf_finish");
248 	hdr = sbuf_data(sb);
249 	segoff = sbuf_len(sb);
250 	/* Fill in the header. */
251 	elf_puthdr(efd, pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
252 
253 	n = write(fd, hdr, segoff);
254 	if (n == -1)
255 		err(1, "write");
256 	if (n < segoff)
257               errx(1, "short write");
258 
259 	/* Write the contents of all of the writable segments. */
260 	php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
261 	for (i = 0;  i < seginfo.count;  i++) {
262 		struct ptrace_io_desc iorequest;
263 		uintmax_t nleft = php->p_filesz;
264 
265 		iorequest.piod_op = PIOD_READ_D;
266 		iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
267 		while (nleft > 0) {
268 			char buf[8*1024];
269 			size_t nwant;
270 			ssize_t ngot;
271 
272 			if (nleft > sizeof(buf))
273 				nwant = sizeof buf;
274 			else
275 				nwant = nleft;
276 			iorequest.piod_addr = buf;
277 			iorequest.piod_len = nwant;
278 			ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
279 			ngot = iorequest.piod_len;
280 			if ((size_t)ngot < nwant)
281 				errx(1, "short read wanted %zu, got %zd",
282 				    nwant, ngot);
283 			ngot = write(fd, buf, nwant);
284 			if (ngot == -1)
285 				err(1, "write of segment %d failed", i);
286 			if ((size_t)ngot != nwant)
287 				errx(1, "short write");
288 			nleft -= nwant;
289 			iorequest.piod_offs += ngot;
290 		}
291 		php++;
292 	}
293 	sbuf_delete(sb);
294 	freemap(map);
295 }
296 
297 /*
298  * A callback for each_dumpable_segment() to write out the segment's
299  * program header entry.
300  */
301 static void
302 cb_put_phdr(struct map_entry *entry, void *closure)
303 {
304 	struct phdr_closure *phc = (struct phdr_closure *)closure;
305 	Elf_Phdr *phdr = phc->phdr;
306 
307 	phc->offset = round_page(phc->offset);
308 
309 	phdr->p_type = PT_LOAD;
310 	phdr->p_offset = phc->offset;
311 	phdr->p_vaddr = entry->start;
312 	phdr->p_paddr = 0;
313 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
314 	phdr->p_align = PAGE_SIZE;
315 	phdr->p_flags = 0;
316 	if (entry->protection & VM_PROT_READ)
317 		phdr->p_flags |= PF_R;
318 	if (entry->protection & VM_PROT_WRITE)
319 		phdr->p_flags |= PF_W;
320 	if (entry->protection & VM_PROT_EXECUTE)
321 		phdr->p_flags |= PF_X;
322 
323 	phc->offset += phdr->p_filesz;
324 	phc->phdr++;
325 }
326 
327 /*
328  * A callback for each_dumpable_segment() to gather information about
329  * the number of segments and their total size.
330  */
331 static void
332 cb_size_segment(struct map_entry *entry, void *closure)
333 {
334 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
335 
336 	ssc->count++;
337 	ssc->size += entry->end - entry->start;
338 }
339 
340 /*
341  * For each segment in the given memory map, call the given function
342  * with a pointer to the map entry and some arbitrary caller-supplied
343  * data.
344  */
345 static void
346 each_dumpable_segment(struct map_entry *map, segment_callback func,
347     void *closure)
348 {
349 	struct map_entry *entry;
350 
351 	for (entry = map; entry != NULL; entry = entry->next)
352 		(*func)(entry, closure);
353 }
354 
355 static void
356 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
357 {
358 	lwpid_t *tids;
359 	size_t threads, old_len;
360 	ssize_t size;
361 	int i;
362 
363 	errno = 0;
364 	threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
365 	if (errno)
366 		err(1, "PT_GETNUMLWPS");
367 	tids = malloc(threads * sizeof(*tids));
368 	if (tids == NULL)
369 		errx(1, "out of memory");
370 	errno = 0;
371 	ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
372 	if (errno)
373 		err(1, "PT_GETLWPLIST");
374 
375 	sbuf_start_section(sb, &old_len);
376 	elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
377 
378 	for (i = 0; i < threads; ++i) {
379 		elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
380 		elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
381 		elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
382 		elf_putnote(NT_PTLWPINFO, elf_note_ptlwpinfo, tids + i, sb);
383 #if defined(__arm__)
384 		elf_putnote(NT_ARM_VFP, elf_note_arm_vfp, tids + i, sb);
385 #endif
386 #if defined(__i386__) || defined(__amd64__)
387 		elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
388 #endif
389 #if defined(__powerpc__)
390 		elf_putnote(NT_PPC_VMX, elf_note_powerpc_vmx, tids + i, sb);
391 #ifndef __SPE__
392 		elf_putnote(NT_PPC_VSX, elf_note_powerpc_vsx, tids + i, sb);
393 #endif
394 #endif
395 	}
396 
397 #ifndef ELFCORE_COMPAT_32
398 	elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
399 	elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
400 	elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
401 	elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
402 	elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
403 	elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
404 	elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
405 	elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
406 	    sb);
407 	elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
408 #endif
409 
410 	size = sbuf_end_section(sb, old_len, 1, 0);
411 	if (size == -1)
412 		err(1, "sbuf_end_section");
413 	free(tids);
414 	*sizep = size;
415 }
416 
417 /*
418  * Emit one note section to sbuf.
419  */
420 static void
421 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
422 {
423 	Elf_Note note;
424 	size_t descsz;
425 	ssize_t old_len;
426 	void *desc;
427 
428 	desc = notefunc(arg, &descsz);
429 	note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
430 	note.n_descsz = descsz;
431 	note.n_type = type;
432 
433 	sbuf_bcat(sb, &note, sizeof(note));
434 	sbuf_start_section(sb, &old_len);
435 	sbuf_bcat(sb, "FreeBSD", note.n_namesz);
436 	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
437 	if (descsz == 0)
438 		return;
439 	sbuf_start_section(sb, &old_len);
440 	sbuf_bcat(sb, desc, descsz);
441 	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
442 	free(desc);
443 }
444 
445 /*
446  * Generate the ELF coredump header.
447  */
448 static void
449 elf_puthdr(int efd, pid_t pid, struct map_entry *map, void *hdr, size_t hdrsize,
450     size_t notesz, size_t segoff, int numsegs)
451 {
452 	Elf_Ehdr *ehdr, binhdr;
453 	Elf_Phdr *phdr;
454 	Elf_Shdr *shdr;
455 	struct phdr_closure phc;
456 	ssize_t cnt;
457 
458 	cnt = read(efd, &binhdr, sizeof(binhdr));
459 	if (cnt < 0)
460 		err(1, "Failed to re-read ELF header");
461 	else if (cnt != sizeof(binhdr))
462 		errx(1, "Failed to re-read ELF header");
463 
464 	ehdr = (Elf_Ehdr *)hdr;
465 
466 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
467 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
468 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
469 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
470 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
471 	ehdr->e_ident[EI_DATA] = ELF_DATA;
472 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
473 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
474 	ehdr->e_ident[EI_ABIVERSION] = 0;
475 	ehdr->e_ident[EI_PAD] = 0;
476 	ehdr->e_type = ET_CORE;
477 	ehdr->e_machine = binhdr.e_machine;
478 	ehdr->e_version = EV_CURRENT;
479 	ehdr->e_entry = 0;
480 	ehdr->e_phoff = sizeof(Elf_Ehdr);
481 	ehdr->e_flags = binhdr.e_flags;
482 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
483 	ehdr->e_phentsize = sizeof(Elf_Phdr);
484 	ehdr->e_shentsize = sizeof(Elf_Shdr);
485 	ehdr->e_shstrndx = SHN_UNDEF;
486 	if (numsegs + 1 < PN_XNUM) {
487 		ehdr->e_phnum = numsegs + 1;
488 		ehdr->e_shnum = 0;
489 	} else {
490 		ehdr->e_phnum = PN_XNUM;
491 		ehdr->e_shnum = 1;
492 
493 		ehdr->e_shoff = ehdr->e_phoff +
494 		    (numsegs + 1) * ehdr->e_phentsize;
495 
496 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
497 		memset(shdr, 0, sizeof(*shdr));
498 		/*
499 		 * A special first section is used to hold large segment and
500 		 * section counts.  This was proposed by Sun Microsystems in
501 		 * Solaris and has been adopted by Linux; the standard ELF
502 		 * tools are already familiar with the technique.
503 		 *
504 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
505 		 * (or 12-7 depending on the version of the document) for more
506 		 * details.
507 		 */
508 		shdr->sh_type = SHT_NULL;
509 		shdr->sh_size = ehdr->e_shnum;
510 		shdr->sh_link = ehdr->e_shstrndx;
511 		shdr->sh_info = numsegs + 1;
512 	}
513 
514 	/*
515 	 * Fill in the program header entries.
516 	 */
517 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
518 
519 	/* The note segement. */
520 	phdr->p_type = PT_NOTE;
521 	phdr->p_offset = hdrsize;
522 	phdr->p_vaddr = 0;
523 	phdr->p_paddr = 0;
524 	phdr->p_filesz = notesz;
525 	phdr->p_memsz = 0;
526 	phdr->p_flags = PF_R;
527 	phdr->p_align = sizeof(Elf32_Size);
528 	phdr++;
529 
530 	/* All the writable segments from the program. */
531 	phc.phdr = phdr;
532 	phc.offset = segoff;
533 	each_dumpable_segment(map, cb_put_phdr, &phc);
534 }
535 
536 /*
537  * Free the memory map.
538  */
539 static void
540 freemap(struct map_entry *map)
541 {
542 	struct map_entry *next;
543 
544 	while (map != NULL) {
545 		next = map->next;
546 		free(map);
547 		map = next;
548 	}
549 }
550 
551 /*
552  * Read the process's memory map using kinfo_getvmmap(), and return a list of
553  * VM map entries.  Only the non-device read/writable segments are
554  * returned.  The map entries in the list aren't fully filled in; only
555  * the items we need are present.
556  */
557 static struct map_entry *
558 readmap(pid_t pid)
559 {
560 	struct map_entry *ent, **linkp, *map;
561 	struct kinfo_vmentry *vmentl, *kve;
562 	int i, nitems;
563 
564 	vmentl = kinfo_getvmmap(pid, &nitems);
565 	if (vmentl == NULL)
566 		err(1, "cannot retrieve mappings for %u process", pid);
567 
568 	map = NULL;
569 	linkp = &map;
570 	for (i = 0; i < nitems; i++) {
571 		kve = &vmentl[i];
572 
573 		/*
574 		 * Ignore 'malformed' segments or ones representing memory
575 		 * mapping with MAP_NOCORE on.
576 		 * If the 'full' support is disabled, just dump the most
577 		 * meaningful data segments.
578 		 */
579 		if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
580 		    (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
581 		    kve->kve_type == KVME_TYPE_DEAD ||
582 		    kve->kve_type == KVME_TYPE_UNKNOWN ||
583 		    ((pflags & PFLAGS_FULL) == 0 &&
584 		    kve->kve_type != KVME_TYPE_DEFAULT &&
585 		    kve->kve_type != KVME_TYPE_VNODE &&
586 		    kve->kve_type != KVME_TYPE_SWAP &&
587 		    kve->kve_type != KVME_TYPE_PHYS))
588 			continue;
589 
590 		ent = calloc(1, sizeof(*ent));
591 		if (ent == NULL)
592 			errx(1, "out of memory");
593 		ent->start = (vm_offset_t)kve->kve_start;
594 		ent->end = (vm_offset_t)kve->kve_end;
595 		ent->protection = VM_PROT_READ | VM_PROT_WRITE;
596 		if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
597 			ent->protection |= VM_PROT_EXECUTE;
598 
599 		*linkp = ent;
600 		linkp = &ent->next;
601 	}
602 	free(vmentl);
603 	return (map);
604 }
605 
606 /*
607  * Miscellaneous note out functions.
608  */
609 
610 static void *
611 elf_note_prpsinfo(void *arg, size_t *sizep)
612 {
613 	char *cp, *end;
614 	pid_t pid;
615 	elfcore_prpsinfo_t *psinfo;
616 	struct kinfo_proc kip;
617 	size_t len;
618 	int name[4];
619 
620 	pid = *(pid_t *)arg;
621 	psinfo = calloc(1, sizeof(*psinfo));
622 	if (psinfo == NULL)
623 		errx(1, "out of memory");
624 	psinfo->pr_version = PRPSINFO_VERSION;
625 	psinfo->pr_psinfosz = sizeof(*psinfo);
626 
627 	name[0] = CTL_KERN;
628 	name[1] = KERN_PROC;
629 	name[2] = KERN_PROC_PID;
630 	name[3] = pid;
631 	len = sizeof(kip);
632 	if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
633 		err(1, "kern.proc.pid.%u", pid);
634 	if (kip.ki_pid != pid)
635 		err(1, "kern.proc.pid.%u", pid);
636 	strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname));
637 	name[2] = KERN_PROC_ARGS;
638 	len = sizeof(psinfo->pr_psargs) - 1;
639 	if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) {
640 		cp = psinfo->pr_psargs;
641 		end = cp + len - 1;
642 		for (;;) {
643 			cp = memchr(cp, '\0', end - cp);
644 			if (cp == NULL)
645 				break;
646 			*cp = ' ';
647 		}
648 	} else
649 		strlcpy(psinfo->pr_psargs, kip.ki_comm,
650 		    sizeof(psinfo->pr_psargs));
651 	psinfo->pr_pid = pid;
652 
653 	*sizep = sizeof(*psinfo);
654 	return (psinfo);
655 }
656 
657 static void *
658 elf_note_prstatus(void *arg, size_t *sizep)
659 {
660 	lwpid_t tid;
661 	elfcore_prstatus_t *status;
662 	struct reg greg;
663 
664 	tid = *(lwpid_t *)arg;
665 	status = calloc(1, sizeof(*status));
666 	if (status == NULL)
667 		errx(1, "out of memory");
668 	status->pr_version = PRSTATUS_VERSION;
669 	status->pr_statussz = sizeof(*status);
670 	status->pr_gregsetsz = sizeof(elfcore_gregset_t);
671 	status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
672 	status->pr_osreldate = __FreeBSD_version;
673 	status->pr_pid = tid;
674 	ptrace(PT_GETREGS, tid, (void *)&greg, 0);
675 	elf_convert_gregset(&status->pr_reg, &greg);
676 
677 	*sizep = sizeof(*status);
678 	return (status);
679 }
680 
681 static void *
682 elf_note_fpregset(void *arg, size_t *sizep)
683 {
684 	lwpid_t tid;
685 	elfcore_fpregset_t *fpregset;
686 	fpregset_t fpreg;
687 
688 	tid = *(lwpid_t *)arg;
689 	fpregset = calloc(1, sizeof(*fpregset));
690 	if (fpregset == NULL)
691 		errx(1, "out of memory");
692 	ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
693 	elf_convert_fpregset(fpregset, &fpreg);
694 
695 	*sizep = sizeof(*fpregset);
696 	return (fpregset);
697 }
698 
699 static void *
700 elf_note_thrmisc(void *arg, size_t *sizep)
701 {
702 	lwpid_t tid;
703 	struct ptrace_lwpinfo lwpinfo;
704 	thrmisc_t *thrmisc;
705 
706 	tid = *(lwpid_t *)arg;
707 	thrmisc = calloc(1, sizeof(*thrmisc));
708 	if (thrmisc == NULL)
709 		errx(1, "out of memory");
710 	ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
711 	    sizeof(lwpinfo));
712 	memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
713 	strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
714 
715 	*sizep = sizeof(*thrmisc);
716 	return (thrmisc);
717 }
718 
719 static void *
720 elf_note_ptlwpinfo(void *arg, size_t *sizep)
721 {
722 	lwpid_t tid;
723 	elfcore_lwpinfo_t *elf_info;
724 	struct ptrace_lwpinfo lwpinfo;
725 	void *p;
726 
727 	tid = *(lwpid_t *)arg;
728 	p = calloc(1, sizeof(int) + sizeof(elfcore_lwpinfo_t));
729 	if (p == NULL)
730 		errx(1, "out of memory");
731 	*(int *)p = sizeof(elfcore_lwpinfo_t);
732 	elf_info = (void *)((int *)p + 1);
733 	ptrace(PT_LWPINFO, tid, (void *)&lwpinfo, sizeof(lwpinfo));
734 	elf_convert_lwpinfo(elf_info, &lwpinfo);
735 
736 	*sizep = sizeof(int) + sizeof(struct ptrace_lwpinfo);
737 	return (p);
738 }
739 
740 #if defined(__arm__)
741 static void *
742 elf_note_arm_vfp(void *arg, size_t *sizep)
743 {
744 	lwpid_t tid;
745 	struct vfpreg *vfp;
746 	static bool has_vfp = true;
747 	struct vfpreg info;
748 
749 	tid = *(lwpid_t *)arg;
750 	if (has_vfp) {
751 		if (ptrace(PT_GETVFPREGS, tid, (void *)&info, 0) != 0)
752 			has_vfp = false;
753 	}
754 	if (!has_vfp) {
755 		*sizep = 0;
756 		return (NULL);
757 	}
758 	vfp = calloc(1, sizeof(*vfp));
759 	memcpy(vfp, &info, sizeof(*vfp));
760 	*sizep = sizeof(*vfp);
761 	return (vfp);
762 }
763 #endif
764 
765 #if defined(__i386__) || defined(__amd64__)
766 static void *
767 elf_note_x86_xstate(void *arg, size_t *sizep)
768 {
769 	lwpid_t tid;
770 	char *xstate;
771 	static bool xsave_checked = false;
772 	static struct ptrace_xstate_info info;
773 
774 	tid = *(lwpid_t *)arg;
775 	if (!xsave_checked) {
776 		if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
777 		    sizeof(info)) != 0)
778 			info.xsave_len = 0;
779 		xsave_checked = true;
780 	}
781 	if (info.xsave_len == 0) {
782 		*sizep = 0;
783 		return (NULL);
784 	}
785 	xstate = calloc(1, info.xsave_len);
786 	ptrace(PT_GETXSTATE, tid, xstate, 0);
787 	*(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
788 	*sizep = info.xsave_len;
789 	return (xstate);
790 }
791 #endif
792 
793 #if defined(__powerpc__)
794 static void *
795 elf_note_powerpc_vmx(void *arg, size_t *sizep)
796 {
797 	lwpid_t tid;
798 	struct vmxreg *vmx;
799 	static bool has_vmx = true;
800 	struct vmxreg info;
801 
802 	tid = *(lwpid_t *)arg;
803 	if (has_vmx) {
804 		if (ptrace(PT_GETVRREGS, tid, (void *)&info,
805 		    sizeof(info)) != 0)
806 			has_vmx = false;
807 	}
808 	if (!has_vmx) {
809 		*sizep = 0;
810 		return (NULL);
811 	}
812 	vmx = calloc(1, sizeof(*vmx));
813 	memcpy(vmx, &info, sizeof(*vmx));
814 	*sizep = sizeof(*vmx);
815 	return (vmx);
816 }
817 
818 static void *
819 elf_note_powerpc_vsx(void *arg, size_t *sizep)
820 {
821 	lwpid_t tid;
822 	char *vshr_data;
823 	static bool has_vsx = true;
824 	uint64_t vshr[32];
825 
826 	tid = *(lwpid_t *)arg;
827 	if (has_vsx) {
828 		if (ptrace(PT_GETVSRREGS, tid, (void *)vshr,
829 		    sizeof(vshr)) != 0)
830 			has_vsx = false;
831 	}
832 	if (!has_vsx) {
833 		*sizep = 0;
834 		return (NULL);
835 	}
836 	vshr_data = calloc(1, sizeof(vshr));
837 	memcpy(vshr_data, vshr, sizeof(vshr));
838 	*sizep = sizeof(vshr);
839 	return (vshr_data);
840 }
841 #endif
842 
843 static void *
844 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
845 {
846 	size_t len;
847 	pid_t pid;
848 	int name[4], structsize;
849 	void *buf, *p;
850 
851 	pid = *(pid_t *)arg;
852 	structsize = structsz;
853 	name[0] = CTL_KERN;
854 	name[1] = KERN_PROC;
855 	name[2] = what;
856 	name[3] = pid;
857 	len = 0;
858 	if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
859 		err(1, "kern.proc.%d.%u", what, pid);
860 	buf = calloc(1, sizeof(structsize) + len * 4 / 3);
861 	if (buf == NULL)
862 		errx(1, "out of memory");
863 	bcopy(&structsize, buf, sizeof(structsize));
864 	p = (char *)buf + sizeof(structsize);
865 	if (sysctl(name, 4, p, &len, NULL, 0) == -1)
866 		err(1, "kern.proc.%d.%u", what, pid);
867 
868 	*sizep = sizeof(structsize) + len;
869 	return (buf);
870 }
871 
872 static void *
873 elf_note_procstat_proc(void *arg, size_t *sizep)
874 {
875 
876 	return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
877 	    sizeof(struct kinfo_proc), sizep));
878 }
879 
880 static void *
881 elf_note_procstat_files(void *arg, size_t *sizep)
882 {
883 
884 	return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
885 	    sizeof(struct kinfo_file), sizep));
886 }
887 
888 static void *
889 elf_note_procstat_vmmap(void *arg, size_t *sizep)
890 {
891 
892 	return (procstat_sysctl(arg, KERN_PROC_VMMAP,
893 	    sizeof(struct kinfo_vmentry), sizep));
894 }
895 
896 static void *
897 elf_note_procstat_groups(void *arg, size_t *sizep)
898 {
899 
900 	return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
901 }
902 
903 static void *
904 elf_note_procstat_umask(void *arg, size_t *sizep)
905 {
906 
907 	return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
908 }
909 
910 static void *
911 elf_note_procstat_osrel(void *arg, size_t *sizep)
912 {
913 
914 	return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
915 }
916 
917 static void *
918 elf_note_procstat_psstrings(void *arg, size_t *sizep)
919 {
920 
921 	return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
922 	    sizeof(vm_offset_t), sizep));
923 }
924 
925 static void *
926 elf_note_procstat_auxv(void *arg, size_t *sizep)
927 {
928 
929 	return (procstat_sysctl(arg, KERN_PROC_AUXV,
930 	    sizeof(Elf_Auxinfo), sizep));
931 }
932 
933 static void *
934 elf_note_procstat_rlimit(void *arg, size_t *sizep)
935 {
936 	pid_t pid;
937 	size_t len;
938 	int i, name[5], structsize;
939 	void *buf, *p;
940 
941 	pid = *(pid_t *)arg;
942 	structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
943 	buf = calloc(1, sizeof(structsize) + structsize);
944 	if (buf == NULL)
945 		errx(1, "out of memory");
946 	bcopy(&structsize, buf, sizeof(structsize));
947 	p = (char *)buf + sizeof(structsize);
948 	name[0] = CTL_KERN;
949 	name[1] = KERN_PROC;
950 	name[2] = KERN_PROC_RLIMIT;
951 	name[3] = pid;
952 	len = sizeof(struct rlimit);
953 	for (i = 0; i < RLIM_NLIMITS; i++) {
954 		name[4] = i;
955 		if (sysctl(name, 5, p, &len, NULL, 0) == -1)
956 			err(1, "kern.proc.rlimit.%u", pid);
957 		if (len != sizeof(struct rlimit))
958 			errx(1, "kern.proc.rlimit.%u: short read", pid);
959 		p += len;
960 	}
961 
962 	*sizep = sizeof(structsize) + structsize;
963 	return (buf);
964 }
965 
966 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
967 TEXT_SET(dumpset, __elfN(dump));
968