xref: /freebsd/usr.bin/gcore/elfcore.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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/endian.h>
32 #include <sys/param.h>
33 #include <sys/procfs.h>
34 #include <sys/ptrace.h>
35 #include <sys/queue.h>
36 #include <sys/linker_set.h>
37 #include <sys/sbuf.h>
38 #include <sys/sysctl.h>
39 #include <sys/user.h>
40 #include <sys/wait.h>
41 #include <machine/elf.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_map.h>
46 #include <assert.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdbool.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <libutil.h>
57 
58 #include "extern.h"
59 
60 /*
61  * Code for generating ELF core dumps.
62  */
63 
64 typedef void (*segment_callback)(vm_map_entry_t, void *);
65 
66 /* Closure for cb_put_phdr(). */
67 struct phdr_closure {
68 	Elf_Phdr *phdr;		/* Program header to fill in */
69 	Elf_Off offset;		/* Offset of segment in core file */
70 };
71 
72 /* Closure for cb_size_segment(). */
73 struct sseg_closure {
74 	int count;		/* Count of writable segments. */
75 	size_t size;		/* Total size of all writable segments. */
76 };
77 
78 #ifdef ELFCORE_COMPAT_32
79 typedef struct fpreg32 elfcore_fpregset_t;
80 typedef struct reg32   elfcore_gregset_t;
81 typedef struct prpsinfo32 elfcore_prpsinfo_t;
82 typedef struct prstatus32 elfcore_prstatus_t;
83 static void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs);
84 static void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs);
85 #else
86 typedef fpregset_t elfcore_fpregset_t;
87 typedef gregset_t  elfcore_gregset_t;
88 typedef prpsinfo_t elfcore_prpsinfo_t;
89 typedef prstatus_t elfcore_prstatus_t;
90 #define elf_convert_gregset(d,s)	*d = *s
91 #define elf_convert_fpregset(d,s)	*d = *s
92 #endif
93 
94 typedef void* (*notefunc_t)(void *, size_t *);
95 
96 static void cb_put_phdr(vm_map_entry_t, void *);
97 static void cb_size_segment(vm_map_entry_t, void *);
98 static void each_writable_segment(vm_map_entry_t, segment_callback,
99     void *closure);
100 static void elf_detach(void);	/* atexit() handler. */
101 static void *elf_note_fpregset(void *, size_t *);
102 static void *elf_note_prpsinfo(void *, size_t *);
103 static void *elf_note_prstatus(void *, size_t *);
104 static void *elf_note_thrmisc(void *, size_t *);
105 #if defined(__i386__) || defined(__amd64__)
106 static void *elf_note_x86_xstate(void *, size_t *);
107 #endif
108 #if defined(__powerpc__)
109 static void *elf_note_powerpc_vmx(void *, size_t *);
110 #endif
111 static void *elf_note_procstat_auxv(void *, size_t *);
112 static void *elf_note_procstat_files(void *, size_t *);
113 static void *elf_note_procstat_groups(void *, size_t *);
114 static void *elf_note_procstat_osrel(void *, size_t *);
115 static void *elf_note_procstat_proc(void *, size_t *);
116 static void *elf_note_procstat_psstrings(void *, size_t *);
117 static void *elf_note_procstat_rlimit(void *, size_t *);
118 static void *elf_note_procstat_umask(void *, size_t *);
119 static void *elf_note_procstat_vmmap(void *, size_t *);
120 static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
121     int);
122 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
123 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
124 static void freemap(vm_map_entry_t);
125 static vm_map_entry_t readmap(pid_t);
126 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
127 
128 static pid_t g_pid;		/* Pid being dumped, global for elf_detach */
129 
130 static int
131 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
132 {
133 	Elf_Ehdr hdr;
134 	int cnt;
135 	uint16_t machine;
136 
137 	cnt = read(efd, &hdr, sizeof(hdr));
138 	if (cnt != sizeof(hdr))
139 		return (0);
140 	if (!IS_ELF(hdr))
141 		return (0);
142 	switch (hdr.e_ident[EI_DATA]) {
143 	case ELFDATA2LSB:
144 		machine = le16toh(hdr.e_machine);
145 		break;
146 	case ELFDATA2MSB:
147 		machine = be16toh(hdr.e_machine);
148 		break;
149 	default:
150 		return (0);
151 	}
152 	if (!ELF_MACHINE_OK(machine))
153 		return (0);
154 
155 	/* Looks good. */
156 	return (1);
157 }
158 
159 static void
160 elf_detach(void)
161 {
162 
163 	if (g_pid != 0)
164 		ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
165 }
166 
167 /*
168  * Write an ELF coredump for the given pid to the given fd.
169  */
170 static void
171 elf_coredump(int efd __unused, int fd, pid_t pid)
172 {
173 	vm_map_entry_t map;
174 	struct sseg_closure seginfo;
175 	struct sbuf *sb;
176 	void *hdr;
177 	size_t hdrsize, notesz, segoff;
178 	ssize_t n, old_len;
179 	Elf_Phdr *php;
180 	int i;
181 
182 	/* Attach to process to dump. */
183 	g_pid = pid;
184 	if (atexit(elf_detach) != 0)
185 		err(1, "atexit");
186 	errno = 0;
187 	ptrace(PT_ATTACH, pid, NULL, 0);
188 	if (errno)
189 		err(1, "PT_ATTACH");
190 	if (waitpid(pid, NULL, 0) == -1)
191 		err(1, "waitpid");
192 
193 	/* Get the program's memory map. */
194 	map = readmap(pid);
195 
196 	/* Size the program segments. */
197 	seginfo.count = 0;
198 	seginfo.size = 0;
199 	each_writable_segment(map, cb_size_segment, &seginfo);
200 
201 	/*
202 	 * Build the header and the notes using sbuf and write to the file.
203 	 */
204 	sb = sbuf_new_auto();
205 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
206 	/* Start header + notes section. */
207 	sbuf_start_section(sb, NULL);
208 	/* Make empty header subsection. */
209 	sbuf_start_section(sb, &old_len);
210 	sbuf_putc(sb, 0);
211 	sbuf_end_section(sb, old_len, hdrsize, 0);
212 	/* Put notes. */
213 	elf_putnotes(pid, sb, &notesz);
214 	/* Align up to a page boundary for the program segments. */
215 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
216 	if (sbuf_finish(sb) != 0)
217 		err(1, "sbuf_finish");
218 	hdr = sbuf_data(sb);
219 	segoff = sbuf_len(sb);
220 	/* Fill in the header. */
221 	elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
222 
223 	n = write(fd, hdr, segoff);
224 	if (n == -1)
225 		err(1, "write");
226 	if (n < segoff)
227               errx(1, "short write");
228 
229 	/* Write the contents of all of the writable segments. */
230 	php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
231 	for (i = 0;  i < seginfo.count;  i++) {
232 		struct ptrace_io_desc iorequest;
233 		uintmax_t nleft = php->p_filesz;
234 
235 		iorequest.piod_op = PIOD_READ_D;
236 		iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
237 		while (nleft > 0) {
238 			char buf[8*1024];
239 			size_t nwant;
240 			ssize_t ngot;
241 
242 			if (nleft > sizeof(buf))
243 				nwant = sizeof buf;
244 			else
245 				nwant = nleft;
246 			iorequest.piod_addr = buf;
247 			iorequest.piod_len = nwant;
248 			ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
249 			ngot = iorequest.piod_len;
250 			if ((size_t)ngot < nwant)
251 				errx(1, "short read wanted %zu, got %zd",
252 				    nwant, ngot);
253 			ngot = write(fd, buf, nwant);
254 			if (ngot == -1)
255 				err(1, "write of segment %d failed", i);
256 			if ((size_t)ngot != nwant)
257 				errx(1, "short write");
258 			nleft -= nwant;
259 			iorequest.piod_offs += ngot;
260 		}
261 		php++;
262 	}
263 	sbuf_delete(sb);
264 	freemap(map);
265 }
266 
267 /*
268  * A callback for each_writable_segment() to write out the segment's
269  * program header entry.
270  */
271 static void
272 cb_put_phdr(vm_map_entry_t entry, void *closure)
273 {
274 	struct phdr_closure *phc = (struct phdr_closure *)closure;
275 	Elf_Phdr *phdr = phc->phdr;
276 
277 	phc->offset = round_page(phc->offset);
278 
279 	phdr->p_type = PT_LOAD;
280 	phdr->p_offset = phc->offset;
281 	phdr->p_vaddr = entry->start;
282 	phdr->p_paddr = 0;
283 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
284 	phdr->p_align = PAGE_SIZE;
285 	phdr->p_flags = 0;
286 	if (entry->protection & VM_PROT_READ)
287 		phdr->p_flags |= PF_R;
288 	if (entry->protection & VM_PROT_WRITE)
289 		phdr->p_flags |= PF_W;
290 	if (entry->protection & VM_PROT_EXECUTE)
291 		phdr->p_flags |= PF_X;
292 
293 	phc->offset += phdr->p_filesz;
294 	phc->phdr++;
295 }
296 
297 /*
298  * A callback for each_writable_segment() to gather information about
299  * the number of segments and their total size.
300  */
301 static void
302 cb_size_segment(vm_map_entry_t entry, void *closure)
303 {
304 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
305 
306 	ssc->count++;
307 	ssc->size += entry->end - entry->start;
308 }
309 
310 /*
311  * For each segment in the given memory map, call the given function
312  * with a pointer to the map entry and some arbitrary caller-supplied
313  * data.
314  */
315 static void
316 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
317 {
318 	vm_map_entry_t entry;
319 
320 	for (entry = map;  entry != NULL;  entry = entry->next)
321 		(*func)(entry, closure);
322 }
323 
324 static void
325 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
326 {
327 	lwpid_t *tids;
328 	size_t threads, old_len;
329 	ssize_t size;
330 	int i;
331 
332 	errno = 0;
333 	threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
334 	if (errno)
335 		err(1, "PT_GETNUMLWPS");
336 	tids = malloc(threads * sizeof(*tids));
337 	if (tids == NULL)
338 		errx(1, "out of memory");
339 	errno = 0;
340 	ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
341 	if (errno)
342 		err(1, "PT_GETLWPLIST");
343 
344 	sbuf_start_section(sb, &old_len);
345 	elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
346 
347 	for (i = 0; i < threads; ++i) {
348 		elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
349 		elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
350 		elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
351 #if defined(__i386__) || defined(__amd64__)
352 		elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
353 #endif
354 #if defined(__powerpc__)
355 		elf_putnote(NT_PPC_VMX, elf_note_powerpc_vmx, tids + i, sb);
356 #endif
357 	}
358 
359 #ifndef ELFCORE_COMPAT_32
360 	elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
361 	elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
362 	elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
363 	elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
364 	elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
365 	elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
366 	elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
367 	elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
368 	    sb);
369 	elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
370 #endif
371 
372 	size = sbuf_end_section(sb, old_len, 1, 0);
373 	if (size == -1)
374 		err(1, "sbuf_end_section");
375 	free(tids);
376 	*sizep = size;
377 }
378 
379 /*
380  * Emit one note section to sbuf.
381  */
382 static void
383 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
384 {
385 	Elf_Note note;
386 	size_t descsz;
387 	ssize_t old_len;
388 	void *desc;
389 
390 	desc = notefunc(arg, &descsz);
391 	note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
392 	note.n_descsz = descsz;
393 	note.n_type = type;
394 
395 	sbuf_bcat(sb, &note, sizeof(note));
396 	sbuf_start_section(sb, &old_len);
397 	sbuf_bcat(sb, "FreeBSD", note.n_namesz);
398 	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
399 	if (descsz == 0)
400 		return;
401 	sbuf_start_section(sb, &old_len);
402 	sbuf_bcat(sb, desc, descsz);
403 	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
404 	free(desc);
405 }
406 
407 /*
408  * Generate the ELF coredump header.
409  */
410 static void
411 elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
412     size_t notesz, size_t segoff, int numsegs)
413 {
414 	Elf_Ehdr *ehdr;
415 	Elf_Phdr *phdr;
416 	struct phdr_closure phc;
417 
418 	ehdr = (Elf_Ehdr *)hdr;
419 	phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
420 
421 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
422 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
423 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
424 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
425 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
426 	ehdr->e_ident[EI_DATA] = ELF_DATA;
427 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
428 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
429 	ehdr->e_ident[EI_ABIVERSION] = 0;
430 	ehdr->e_ident[EI_PAD] = 0;
431 	ehdr->e_type = ET_CORE;
432 	ehdr->e_machine = ELF_ARCH;
433 	ehdr->e_version = EV_CURRENT;
434 	ehdr->e_entry = 0;
435 	ehdr->e_phoff = sizeof(Elf_Ehdr);
436 	ehdr->e_flags = 0;
437 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
438 	ehdr->e_phentsize = sizeof(Elf_Phdr);
439 	ehdr->e_phnum = numsegs + 1;
440 	ehdr->e_shentsize = sizeof(Elf_Shdr);
441 	ehdr->e_shnum = 0;
442 	ehdr->e_shstrndx = SHN_UNDEF;
443 
444 	/*
445 	 * Fill in the program header entries.
446 	 */
447 
448 	/* The note segement. */
449 	phdr->p_type = PT_NOTE;
450 	phdr->p_offset = hdrsize;
451 	phdr->p_vaddr = 0;
452 	phdr->p_paddr = 0;
453 	phdr->p_filesz = notesz;
454 	phdr->p_memsz = 0;
455 	phdr->p_flags = PF_R;
456 	phdr->p_align = sizeof(Elf32_Size);
457 	phdr++;
458 
459 	/* All the writable segments from the program. */
460 	phc.phdr = phdr;
461 	phc.offset = segoff;
462 	each_writable_segment(map, cb_put_phdr, &phc);
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 /*
534  * Miscellaneous note out functions.
535  */
536 
537 static void *
538 elf_note_prpsinfo(void *arg, size_t *sizep)
539 {
540 	pid_t pid;
541 	elfcore_prpsinfo_t *psinfo;
542 	struct kinfo_proc kip;
543 	size_t len;
544 	int name[4];
545 
546 	pid = *(pid_t *)arg;
547 	psinfo = calloc(1, sizeof(*psinfo));
548 	if (psinfo == NULL)
549 		errx(1, "out of memory");
550 	psinfo->pr_version = PRPSINFO_VERSION;
551 	psinfo->pr_psinfosz = sizeof(*psinfo);
552 
553 	name[0] = CTL_KERN;
554 	name[1] = KERN_PROC;
555 	name[2] = KERN_PROC_PID;
556 	name[3] = pid;
557 	len = sizeof(kip);
558 	if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
559 		err(1, "kern.proc.pid.%u", pid);
560 	if (kip.ki_pid != pid)
561 		err(1, "kern.proc.pid.%u", pid);
562 	strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN);
563 	strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
564 
565 	*sizep = sizeof(*psinfo);
566 	return (psinfo);
567 }
568 
569 static void *
570 elf_note_prstatus(void *arg, size_t *sizep)
571 {
572 	lwpid_t tid;
573 	elfcore_prstatus_t *status;
574 	struct reg greg;
575 
576 	tid = *(lwpid_t *)arg;
577 	status = calloc(1, sizeof(*status));
578 	if (status == NULL)
579 		errx(1, "out of memory");
580 	status->pr_version = PRSTATUS_VERSION;
581 	status->pr_statussz = sizeof(*status);
582 	status->pr_gregsetsz = sizeof(elfcore_gregset_t);
583 	status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
584 	status->pr_osreldate = __FreeBSD_version;
585 	status->pr_pid = tid;
586 	ptrace(PT_GETREGS, tid, (void *)&greg, 0);
587 	elf_convert_gregset(&status->pr_reg, &greg);
588 
589 	*sizep = sizeof(*status);
590 	return (status);
591 }
592 
593 static void *
594 elf_note_fpregset(void *arg, size_t *sizep)
595 {
596 	lwpid_t tid;
597 	elfcore_fpregset_t *fpregset;
598 	fpregset_t fpreg;
599 
600 	tid = *(lwpid_t *)arg;
601 	fpregset = calloc(1, sizeof(*fpregset));
602 	if (fpregset == NULL)
603 		errx(1, "out of memory");
604 	ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
605 	elf_convert_fpregset(fpregset, &fpreg);
606 
607 	*sizep = sizeof(*fpregset);
608 	return (fpregset);
609 }
610 
611 static void *
612 elf_note_thrmisc(void *arg, size_t *sizep)
613 {
614 	lwpid_t tid;
615 	struct ptrace_lwpinfo lwpinfo;
616 	thrmisc_t *thrmisc;
617 
618 	tid = *(lwpid_t *)arg;
619 	thrmisc = calloc(1, sizeof(*thrmisc));
620 	if (thrmisc == NULL)
621 		errx(1, "out of memory");
622 	ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
623 	    sizeof(lwpinfo));
624 	memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
625 	strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
626 
627 	*sizep = sizeof(*thrmisc);
628 	return (thrmisc);
629 }
630 
631 #if defined(__i386__) || defined(__amd64__)
632 static void *
633 elf_note_x86_xstate(void *arg, size_t *sizep)
634 {
635 	lwpid_t tid;
636 	char *xstate;
637 	static bool xsave_checked = false;
638 	static struct ptrace_xstate_info info;
639 
640 	tid = *(lwpid_t *)arg;
641 	if (!xsave_checked) {
642 		if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
643 		    sizeof(info)) != 0)
644 			info.xsave_len = 0;
645 		xsave_checked = true;
646 	}
647 	if (info.xsave_len == 0) {
648 		*sizep = 0;
649 		return (NULL);
650 	}
651 	xstate = calloc(1, info.xsave_len);
652 	ptrace(PT_GETXSTATE, tid, xstate, 0);
653 	*(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
654 	*sizep = info.xsave_len;
655 	return (xstate);
656 }
657 #endif
658 
659 #if defined(__powerpc__)
660 static void *
661 elf_note_powerpc_vmx(void *arg, size_t *sizep)
662 {
663 	lwpid_t tid;
664 	struct vmxreg *vmx;
665 	static bool has_vmx = true;
666 	struct vmxreg info;
667 
668 	tid = *(lwpid_t *)arg;
669 	if (has_vmx) {
670 		if (ptrace(PT_GETVRREGS, tid, (void *)&info,
671 		    sizeof(info)) != 0)
672 			has_vmx = false;
673 	}
674 	if (!has_vmx) {
675 		*sizep = 0;
676 		return (NULL);
677 	}
678 	vmx = calloc(1, sizeof(*vmx));
679 	memcpy(vmx, &info, sizeof(*vmx));
680 	*sizep = sizeof(*vmx);
681 	return (vmx);
682 }
683 #endif
684 
685 static void *
686 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
687 {
688 	size_t len;
689 	pid_t pid;
690 	int name[4], structsize;
691 	void *buf, *p;
692 
693 	pid = *(pid_t *)arg;
694 	structsize = structsz;
695 	name[0] = CTL_KERN;
696 	name[1] = KERN_PROC;
697 	name[2] = what;
698 	name[3] = pid;
699 	len = 0;
700 	if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
701 		err(1, "kern.proc.%d.%u", what, pid);
702 	buf = calloc(1, sizeof(structsize) + len * 4 / 3);
703 	if (buf == NULL)
704 		errx(1, "out of memory");
705 	bcopy(&structsize, buf, sizeof(structsize));
706 	p = (char *)buf + sizeof(structsize);
707 	if (sysctl(name, 4, p, &len, NULL, 0) == -1)
708 		err(1, "kern.proc.%d.%u", what, pid);
709 
710 	*sizep = sizeof(structsize) + len;
711 	return (buf);
712 }
713 
714 static void *
715 elf_note_procstat_proc(void *arg, size_t *sizep)
716 {
717 
718 	return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
719 	    sizeof(struct kinfo_proc), sizep));
720 }
721 
722 static void *
723 elf_note_procstat_files(void *arg, size_t *sizep)
724 {
725 
726 	return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
727 	    sizeof(struct kinfo_file), sizep));
728 }
729 
730 static void *
731 elf_note_procstat_vmmap(void *arg, size_t *sizep)
732 {
733 
734 	return (procstat_sysctl(arg, KERN_PROC_VMMAP,
735 	    sizeof(struct kinfo_vmentry), sizep));
736 }
737 
738 static void *
739 elf_note_procstat_groups(void *arg, size_t *sizep)
740 {
741 
742 	return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
743 }
744 
745 static void *
746 elf_note_procstat_umask(void *arg, size_t *sizep)
747 {
748 
749 	return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
750 }
751 
752 static void *
753 elf_note_procstat_osrel(void *arg, size_t *sizep)
754 {
755 
756 	return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
757 }
758 
759 static void *
760 elf_note_procstat_psstrings(void *arg, size_t *sizep)
761 {
762 
763 	return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
764 	    sizeof(vm_offset_t), sizep));
765 }
766 
767 static void *
768 elf_note_procstat_auxv(void *arg, size_t *sizep)
769 {
770 
771 	return (procstat_sysctl(arg, KERN_PROC_AUXV,
772 	    sizeof(Elf_Auxinfo), sizep));
773 }
774 
775 static void *
776 elf_note_procstat_rlimit(void *arg, size_t *sizep)
777 {
778 	pid_t pid;
779 	size_t len;
780 	int i, name[5], structsize;
781 	void *buf, *p;
782 
783 	pid = *(pid_t *)arg;
784 	structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
785 	buf = calloc(1, sizeof(structsize) + structsize);
786 	if (buf == NULL)
787 		errx(1, "out of memory");
788 	bcopy(&structsize, buf, sizeof(structsize));
789 	p = (char *)buf + sizeof(structsize);
790 	name[0] = CTL_KERN;
791 	name[1] = KERN_PROC;
792 	name[2] = KERN_PROC_RLIMIT;
793 	name[3] = pid;
794 	len = sizeof(struct rlimit);
795 	for (i = 0; i < RLIM_NLIMITS; i++) {
796 		name[4] = i;
797 		if (sysctl(name, 5, p, &len, NULL, 0) == -1)
798 			err(1, "kern.proc.rlimit.%u", pid);
799 		if (len != sizeof(struct rlimit))
800 			errx(1, "kern.proc.rlimit.%u: short read", pid);
801 		p += len;
802 	}
803 
804 	*sizep = sizeof(structsize) + structsize;
805 	return (buf);
806 }
807 
808 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
809 TEXT_SET(dumpset, __elfN(dump));
810