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