elfcore.c revision 279211
140525Sjdp/*-
2199805Sattilio * Copyright (c) 2007 Sandvine Incorporated
340525Sjdp * Copyright (c) 1998 John D. Polstra
440525Sjdp * All rights reserved.
540525Sjdp *
640525Sjdp * Redistribution and use in source and binary forms, with or without
740525Sjdp * modification, are permitted provided that the following conditions
840525Sjdp * are met:
940525Sjdp * 1. Redistributions of source code must retain the above copyright
1040525Sjdp *    notice, this list of conditions and the following disclaimer.
1140525Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1240525Sjdp *    notice, this list of conditions and the following disclaimer in the
1340525Sjdp *    documentation and/or other materials provided with the distribution.
1440525Sjdp *
1540525Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1640525Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1740525Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1840525Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1940525Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2040525Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2140525Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2240525Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2340525Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2440525Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2540525Sjdp * SUCH DAMAGE.
2640525Sjdp */
2740525Sjdp
2893215Scharnier#include <sys/cdefs.h>
2993215Scharnier__FBSDID("$FreeBSD: stable/10/usr.bin/gcore/elfcore.c 279211 2015-02-23 18:38:41Z jhb $");
3093215Scharnier
3140525Sjdp#include <sys/param.h>
3240525Sjdp#include <sys/procfs.h>
33199805Sattilio#include <sys/ptrace.h>
34103302Speter#include <sys/queue.h>
35103299Speter#include <sys/linker_set.h>
36249687Strociny#include <sys/sbuf.h>
37199805Sattilio#include <sys/sysctl.h>
38199805Sattilio#include <sys/user.h>
39199805Sattilio#include <sys/wait.h>
4076224Sobrien#include <machine/elf.h>
4140525Sjdp#include <vm/vm_param.h>
4240525Sjdp#include <vm/vm.h>
4340525Sjdp#include <vm/pmap.h>
4440525Sjdp#include <vm/vm_map.h>
45249687Strociny#include <assert.h>
4640525Sjdp#include <err.h>
4740525Sjdp#include <errno.h>
4840525Sjdp#include <fcntl.h>
49279211Sjhb#include <stdbool.h>
50102951Siedowse#include <stdint.h>
5140525Sjdp#include <stdio.h>
5240525Sjdp#include <stdlib.h>
5340525Sjdp#include <string.h>
5440525Sjdp#include <unistd.h>
55199805Sattilio#include <libutil.h>
5640525Sjdp
5740525Sjdp#include "extern.h"
5840525Sjdp
5940525Sjdp/*
6040525Sjdp * Code for generating ELF core dumps.
6140525Sjdp */
6240525Sjdp
6340525Sjdptypedef void (*segment_callback)(vm_map_entry_t, void *);
6440525Sjdp
6540525Sjdp/* Closure for cb_put_phdr(). */
6640525Sjdpstruct phdr_closure {
6740525Sjdp	Elf_Phdr *phdr;		/* Program header to fill in */
6840525Sjdp	Elf_Off offset;		/* Offset of segment in core file */
6940525Sjdp};
7040525Sjdp
7140525Sjdp/* Closure for cb_size_segment(). */
7240525Sjdpstruct sseg_closure {
7340525Sjdp	int count;		/* Count of writable segments. */
7440525Sjdp	size_t size;		/* Total size of all writable segments. */
7540525Sjdp};
7640525Sjdp
77249687Strocinytypedef void* (*notefunc_t)(void *, size_t *);
78249687Strociny
7940525Sjdpstatic void cb_put_phdr(vm_map_entry_t, void *);
8040525Sjdpstatic void cb_size_segment(vm_map_entry_t, void *);
8140525Sjdpstatic void each_writable_segment(vm_map_entry_t, segment_callback,
8240525Sjdp    void *closure);
83199805Sattiliostatic void elf_detach(void);	/* atexit() handler. */
84249687Strocinystatic void *elf_note_fpregset(void *, size_t *);
85249687Strocinystatic void *elf_note_prpsinfo(void *, size_t *);
86249687Strocinystatic void *elf_note_prstatus(void *, size_t *);
87249687Strocinystatic void *elf_note_thrmisc(void *, size_t *);
88279211Sjhb#if defined(__i386__) || defined(__amd64__)
89279211Sjhbstatic void *elf_note_x86_xstate(void *, size_t *);
90279211Sjhb#endif
91249687Strocinystatic void *elf_note_procstat_auxv(void *, size_t *);
92249687Strocinystatic void *elf_note_procstat_files(void *, size_t *);
93249687Strocinystatic void *elf_note_procstat_groups(void *, size_t *);
94249687Strocinystatic void *elf_note_procstat_osrel(void *, size_t *);
95249687Strocinystatic void *elf_note_procstat_proc(void *, size_t *);
96249687Strocinystatic void *elf_note_procstat_psstrings(void *, size_t *);
97249687Strocinystatic void *elf_note_procstat_rlimit(void *, size_t *);
98249687Strocinystatic void *elf_note_procstat_umask(void *, size_t *);
99249687Strocinystatic void *elf_note_procstat_vmmap(void *, size_t *);
100249687Strocinystatic void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
101249687Strociny    int);
102249687Strocinystatic void elf_putnote(int, notefunc_t, void *, struct sbuf *);
103249687Strocinystatic void elf_putnotes(pid_t, struct sbuf *, size_t *);
10440525Sjdpstatic void freemap(vm_map_entry_t);
10540525Sjdpstatic vm_map_entry_t readmap(pid_t);
106249687Strocinystatic void *procstat_sysctl(void *, int, size_t, size_t *sizep);
10740525Sjdp
108199805Sattiliostatic pid_t g_pid;		/* Pid being dumped, global for elf_detach */
109199805Sattilio
110103299Speterstatic int
111125859Sdwmaloneelf_ident(int efd, pid_t pid __unused, char *binfile __unused)
112103299Speter{
113103299Speter	Elf_Ehdr hdr;
114103299Speter	int cnt;
115103299Speter
116103299Speter	cnt = read(efd, &hdr, sizeof(hdr));
117103299Speter	if (cnt != sizeof(hdr))
118103299Speter		return (0);
119103299Speter	if (IS_ELF(hdr))
120103299Speter		return (1);
121103299Speter	return (0);
122103299Speter}
123103299Speter
124199805Sattiliostatic void
125199805Sattilioelf_detach(void)
126199805Sattilio{
127199805Sattilio
128199805Sattilio	if (g_pid != 0)
129199805Sattilio		ptrace(PT_DETACH, g_pid, (caddr_t)1, 0);
130199805Sattilio}
131199805Sattilio
13240525Sjdp/*
13340525Sjdp * Write an ELF coredump for the given pid to the given fd.
13440525Sjdp */
135125859Sdwmalonestatic void
136125859Sdwmaloneelf_coredump(int efd __unused, int fd, pid_t pid)
13740525Sjdp{
13840525Sjdp	vm_map_entry_t map;
13940525Sjdp	struct sseg_closure seginfo;
140249687Strociny	struct sbuf *sb;
14140525Sjdp	void *hdr;
142249687Strociny	size_t hdrsize, notesz, segoff;
143249687Strociny	ssize_t n, old_len;
14440525Sjdp	Elf_Phdr *php;
14540525Sjdp	int i;
14640525Sjdp
147199805Sattilio	/* Attach to process to dump. */
148199805Sattilio	g_pid = pid;
149199805Sattilio	if (atexit(elf_detach) != 0)
150199805Sattilio		err(1, "atexit");
151199805Sattilio	errno = 0;
152199805Sattilio	ptrace(PT_ATTACH, pid, NULL, 0);
153199805Sattilio	if (errno)
154199805Sattilio		err(1, "PT_ATTACH");
155199805Sattilio	if (waitpid(pid, NULL, 0) == -1)
156199805Sattilio		err(1, "waitpid");
157199805Sattilio
15840525Sjdp	/* Get the program's memory map. */
15940525Sjdp	map = readmap(pid);
16040525Sjdp
16140525Sjdp	/* Size the program segments. */
16240525Sjdp	seginfo.count = 0;
16340525Sjdp	seginfo.size = 0;
16440525Sjdp	each_writable_segment(map, cb_size_segment, &seginfo);
16540525Sjdp
16640525Sjdp	/*
167249687Strociny	 * Build the header and the notes using sbuf and write to the file.
16840525Sjdp	 */
169249687Strociny	sb = sbuf_new_auto();
170249687Strociny	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
171249687Strociny	/* Start header + notes section. */
172249687Strociny	sbuf_start_section(sb, NULL);
173249687Strociny	/* Make empty header subsection. */
174249687Strociny	sbuf_start_section(sb, &old_len);
175249687Strociny	sbuf_putc(sb, 0);
176249687Strociny	sbuf_end_section(sb, old_len, hdrsize, 0);
177249687Strociny	/* Put notes. */
178249687Strociny	elf_putnotes(pid, sb, &notesz);
179249687Strociny	/* Align up to a page boundary for the program segments. */
180249687Strociny	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
181249687Strociny	if (sbuf_finish(sb) != 0)
182249687Strociny		err(1, "sbuf_finish");
183249687Strociny	hdr = sbuf_data(sb);
184249687Strociny	segoff = sbuf_len(sb);
185199805Sattilio	/* Fill in the header. */
186249687Strociny	elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
187199805Sattilio
188249687Strociny	n = write(fd, hdr, segoff);
189249687Strociny	if (n == -1)
190199805Sattilio		err(1, "write");
191249687Strociny	if (n < segoff)
192249687Strociny              errx(1, "short write");
193199805Sattilio
19440525Sjdp	/* Write the contents of all of the writable segments. */
19540525Sjdp	php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
19640525Sjdp	for (i = 0;  i < seginfo.count;  i++) {
197199805Sattilio		struct ptrace_io_desc iorequest;
198102944Sdwmalone		uintmax_t nleft = php->p_filesz;
19940525Sjdp
200199805Sattilio		iorequest.piod_op = PIOD_READ_D;
201199805Sattilio		iorequest.piod_offs = (caddr_t)php->p_vaddr;
20240525Sjdp		while (nleft > 0) {
20340525Sjdp			char buf[8*1024];
204102944Sdwmalone			size_t nwant;
205102944Sdwmalone			ssize_t ngot;
20640525Sjdp
207102944Sdwmalone			if (nleft > sizeof(buf))
20840525Sjdp				nwant = sizeof buf;
209102944Sdwmalone			else
210102944Sdwmalone				nwant = nleft;
211199805Sattilio			iorequest.piod_addr = buf;
212199805Sattilio			iorequest.piod_len = nwant;
213199805Sattilio			ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
214199805Sattilio			ngot = iorequest.piod_len;
215102944Sdwmalone			if ((size_t)ngot < nwant)
216223924Sdelphij				errx(1, "short read wanted %zu, got %zd",
21740803Sjdp				    nwant, ngot);
21840525Sjdp			ngot = write(fd, buf, nwant);
21940525Sjdp			if (ngot == -1)
22040525Sjdp				err(1, "write of segment %d failed", i);
221102944Sdwmalone			if ((size_t)ngot != nwant)
22240525Sjdp				errx(1, "short write");
22340525Sjdp			nleft -= nwant;
224199805Sattilio			iorequest.piod_offs += ngot;
22540525Sjdp		}
22640525Sjdp		php++;
22740525Sjdp	}
228249687Strociny	sbuf_delete(sb);
22940525Sjdp	freemap(map);
23040525Sjdp}
23140525Sjdp
23240525Sjdp/*
23340525Sjdp * A callback for each_writable_segment() to write out the segment's
23440525Sjdp * program header entry.
23540525Sjdp */
23640525Sjdpstatic void
23740525Sjdpcb_put_phdr(vm_map_entry_t entry, void *closure)
23840525Sjdp{
23940525Sjdp	struct phdr_closure *phc = (struct phdr_closure *)closure;
24040525Sjdp	Elf_Phdr *phdr = phc->phdr;
24140525Sjdp
24240525Sjdp	phc->offset = round_page(phc->offset);
24340525Sjdp
24440525Sjdp	phdr->p_type = PT_LOAD;
24540525Sjdp	phdr->p_offset = phc->offset;
24640525Sjdp	phdr->p_vaddr = entry->start;
24740525Sjdp	phdr->p_paddr = 0;
24840525Sjdp	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
24940525Sjdp	phdr->p_align = PAGE_SIZE;
25040525Sjdp	phdr->p_flags = 0;
25140525Sjdp	if (entry->protection & VM_PROT_READ)
25240525Sjdp		phdr->p_flags |= PF_R;
25340525Sjdp	if (entry->protection & VM_PROT_WRITE)
25440525Sjdp		phdr->p_flags |= PF_W;
25540525Sjdp	if (entry->protection & VM_PROT_EXECUTE)
25640525Sjdp		phdr->p_flags |= PF_X;
25740525Sjdp
25840525Sjdp	phc->offset += phdr->p_filesz;
25940525Sjdp	phc->phdr++;
26040525Sjdp}
26140525Sjdp
26240525Sjdp/*
26340525Sjdp * A callback for each_writable_segment() to gather information about
26440525Sjdp * the number of segments and their total size.
26540525Sjdp */
26640525Sjdpstatic void
26740525Sjdpcb_size_segment(vm_map_entry_t entry, void *closure)
26840525Sjdp{
26940525Sjdp	struct sseg_closure *ssc = (struct sseg_closure *)closure;
27040525Sjdp
27140525Sjdp	ssc->count++;
27240525Sjdp	ssc->size += entry->end - entry->start;
27340525Sjdp}
27440525Sjdp
27540525Sjdp/*
27640525Sjdp * For each segment in the given memory map, call the given function
27740525Sjdp * with a pointer to the map entry and some arbitrary caller-supplied
27840525Sjdp * data.
27940525Sjdp */
28040525Sjdpstatic void
28140525Sjdpeach_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
28240525Sjdp{
28340525Sjdp	vm_map_entry_t entry;
28440525Sjdp
28540525Sjdp	for (entry = map;  entry != NULL;  entry = entry->next)
28640525Sjdp		(*func)(entry, closure);
28740525Sjdp}
28840525Sjdp
28940525Sjdpstatic void
290249687Strocinyelf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
29140525Sjdp{
292199805Sattilio	lwpid_t *tids;
293249687Strociny	size_t threads, old_len;
294249687Strociny	ssize_t size;
295199805Sattilio	int i;
29640525Sjdp
297199805Sattilio	errno = 0;
298199805Sattilio	threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
299199805Sattilio	if (errno)
300199805Sattilio		err(1, "PT_GETNUMLWPS");
301249687Strociny	tids = malloc(threads * sizeof(*tids));
302249687Strociny	if (tids == NULL)
303249687Strociny		errx(1, "out of memory");
304249687Strociny	errno = 0;
305249687Strociny	ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
306249687Strociny	if (errno)
307249687Strociny		err(1, "PT_GETLWPLIST");
308199805Sattilio
309249687Strociny	sbuf_start_section(sb, &old_len);
310249687Strociny	elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
311199805Sattilio
312199805Sattilio	for (i = 0; i < threads; ++i) {
313249687Strociny		elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
314249687Strociny		elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
315249687Strociny		elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
316279211Sjhb#if defined(__i386__) || defined(__amd64__)
317279211Sjhb		elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
318279211Sjhb#endif
319199805Sattilio	}
320199805Sattilio
321249687Strociny	elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
322249687Strociny	elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
323249687Strociny	elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
324249687Strociny	elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
325249687Strociny	elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
326249687Strociny	elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
327249687Strociny	elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
328249687Strociny	elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
329249687Strociny	    sb);
330249687Strociny	elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
33140525Sjdp
332249687Strociny	size = sbuf_end_section(sb, old_len, 1, 0);
333249687Strociny	if (size == -1)
334249687Strociny		err(1, "sbuf_end_section");
335249687Strociny	free(tids);
336249687Strociny	*sizep = size;
33740525Sjdp}
33840525Sjdp
33940525Sjdp/*
340249687Strociny * Emit one note section to sbuf.
34140525Sjdp */
34240525Sjdpstatic void
343249687Strocinyelf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
34440525Sjdp{
34540525Sjdp	Elf_Note note;
346249687Strociny	size_t descsz;
347249687Strociny	ssize_t old_len;
348249687Strociny	void *desc;
34940525Sjdp
350249687Strociny	desc = notefunc(arg, &descsz);
351249687Strociny	note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
35240525Sjdp	note.n_descsz = descsz;
35340525Sjdp	note.n_type = type;
354249687Strociny
355249687Strociny	sbuf_bcat(sb, &note, sizeof(note));
356249687Strociny	sbuf_start_section(sb, &old_len);
357249687Strociny	sbuf_bcat(sb, "FreeBSD", note.n_namesz);
358249687Strociny	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
359249687Strociny	if (descsz == 0)
360249687Strociny		return;
361249687Strociny	sbuf_start_section(sb, &old_len);
362249687Strociny	sbuf_bcat(sb, desc, descsz);
363249687Strociny	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
364249687Strociny	free(desc);
36540525Sjdp}
36640525Sjdp
36740525Sjdp/*
368249687Strociny * Generate the ELF coredump header.
369249687Strociny */
370249687Strocinystatic void
371249687Strocinyelf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
372249687Strociny    size_t notesz, size_t segoff, int numsegs)
373249687Strociny{
374249687Strociny	Elf_Ehdr *ehdr;
375249687Strociny	Elf_Phdr *phdr;
376249687Strociny	struct phdr_closure phc;
377249687Strociny
378249687Strociny	ehdr = (Elf_Ehdr *)hdr;
379249687Strociny	phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
380249687Strociny
381249687Strociny	ehdr->e_ident[EI_MAG0] = ELFMAG0;
382249687Strociny	ehdr->e_ident[EI_MAG1] = ELFMAG1;
383249687Strociny	ehdr->e_ident[EI_MAG2] = ELFMAG2;
384249687Strociny	ehdr->e_ident[EI_MAG3] = ELFMAG3;
385249687Strociny	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
386249687Strociny	ehdr->e_ident[EI_DATA] = ELF_DATA;
387249687Strociny	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
388249687Strociny	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
389249687Strociny	ehdr->e_ident[EI_ABIVERSION] = 0;
390249687Strociny	ehdr->e_ident[EI_PAD] = 0;
391249687Strociny	ehdr->e_type = ET_CORE;
392249687Strociny	ehdr->e_machine = ELF_ARCH;
393249687Strociny	ehdr->e_version = EV_CURRENT;
394249687Strociny	ehdr->e_entry = 0;
395249687Strociny	ehdr->e_phoff = sizeof(Elf_Ehdr);
396249687Strociny	ehdr->e_flags = 0;
397249687Strociny	ehdr->e_ehsize = sizeof(Elf_Ehdr);
398249687Strociny	ehdr->e_phentsize = sizeof(Elf_Phdr);
399249687Strociny	ehdr->e_phnum = numsegs + 1;
400249687Strociny	ehdr->e_shentsize = sizeof(Elf_Shdr);
401249687Strociny	ehdr->e_shnum = 0;
402249687Strociny	ehdr->e_shstrndx = SHN_UNDEF;
403249687Strociny
404249687Strociny	/*
405249687Strociny	 * Fill in the program header entries.
406249687Strociny	 */
407249687Strociny
408249687Strociny	/* The note segement. */
409249687Strociny	phdr->p_type = PT_NOTE;
410249687Strociny	phdr->p_offset = hdrsize;
411249687Strociny	phdr->p_vaddr = 0;
412249687Strociny	phdr->p_paddr = 0;
413249687Strociny	phdr->p_filesz = notesz;
414249687Strociny	phdr->p_memsz = 0;
415249687Strociny	phdr->p_flags = PF_R;
416249687Strociny	phdr->p_align = sizeof(Elf32_Size);
417249687Strociny	phdr++;
418249687Strociny
419249687Strociny	/* All the writable segments from the program. */
420249687Strociny	phc.phdr = phdr;
421249687Strociny	phc.offset = segoff;
422249687Strociny	each_writable_segment(map, cb_put_phdr, &phc);
423249687Strociny}
424249687Strociny
425249687Strociny/*
42640525Sjdp * Free the memory map.
42740525Sjdp */
42840525Sjdpstatic void
42940525Sjdpfreemap(vm_map_entry_t map)
43040525Sjdp{
431103299Speter
43240525Sjdp	while (map != NULL) {
43340525Sjdp		vm_map_entry_t next = map->next;
43440525Sjdp		free(map);
43540525Sjdp		map = next;
43640525Sjdp	}
43740525Sjdp}
43840525Sjdp
43940525Sjdp/*
440199805Sattilio * Read the process's memory map using kinfo_getvmmap(), and return a list of
44140525Sjdp * VM map entries.  Only the non-device read/writable segments are
44240525Sjdp * returned.  The map entries in the list aren't fully filled in; only
44340525Sjdp * the items we need are present.
44440525Sjdp */
44540525Sjdpstatic vm_map_entry_t
44640525Sjdpreadmap(pid_t pid)
44740525Sjdp{
448199805Sattilio	vm_map_entry_t ent, *linkp, map;
449199805Sattilio	struct kinfo_vmentry *vmentl, *kve;
450199805Sattilio	int i, nitems;
45140525Sjdp
452199805Sattilio	vmentl = kinfo_getvmmap(pid, &nitems);
453199805Sattilio	if (vmentl == NULL)
454199805Sattilio		err(1, "cannot retrieve mappings for %u process", pid);
45540525Sjdp
45640525Sjdp	map = NULL;
45740525Sjdp	linkp = &map;
458199805Sattilio	for (i = 0; i < nitems; i++) {
459199805Sattilio		kve = &vmentl[i];
46040525Sjdp
461199805Sattilio		/*
462210063Sattilio		 * Ignore 'malformed' segments or ones representing memory
463210063Sattilio		 * mapping with MAP_NOCORE on.
464210063Sattilio		 * If the 'full' support is disabled, just dump the most
465210063Sattilio		 * meaningful data segments.
466199805Sattilio		 */
467210063Sattilio		if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
468210063Sattilio		    (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
469210063Sattilio		    kve->kve_type == KVME_TYPE_DEAD ||
470210063Sattilio		    kve->kve_type == KVME_TYPE_UNKNOWN ||
471210063Sattilio		    ((pflags & PFLAGS_FULL) == 0 &&
472210063Sattilio		    kve->kve_type != KVME_TYPE_DEFAULT &&
473199805Sattilio		    kve->kve_type != KVME_TYPE_VNODE &&
474199805Sattilio		    kve->kve_type != KVME_TYPE_SWAP))
47540525Sjdp			continue;
47640525Sjdp
477199805Sattilio		ent = calloc(1, sizeof(*ent));
478199805Sattilio		if (ent == NULL)
47940525Sjdp			errx(1, "out of memory");
480199805Sattilio		ent->start = (vm_offset_t)kve->kve_start;
481199805Sattilio		ent->end = (vm_offset_t)kve->kve_end;
48240525Sjdp		ent->protection = VM_PROT_READ | VM_PROT_WRITE;
483199805Sattilio		if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
484199805Sattilio			ent->protection |= VM_PROT_EXECUTE;
48540525Sjdp
48640525Sjdp		*linkp = ent;
48740525Sjdp		linkp = &ent->next;
48840525Sjdp	}
489199805Sattilio	free(vmentl);
490199805Sattilio	return (map);
49140525Sjdp}
492103299Speter
493249687Strociny/*
494249687Strociny * Miscellaneous note out functions.
495249687Strociny */
496249687Strociny
497249687Strocinystatic void *
498249687Strocinyelf_note_prpsinfo(void *arg, size_t *sizep)
499249687Strociny{
500249687Strociny	pid_t pid;
501249687Strociny	prpsinfo_t *psinfo;
502249687Strociny	struct kinfo_proc kip;
503249687Strociny	size_t len;
504249687Strociny	int name[4];
505249687Strociny
506249687Strociny	pid = *(pid_t *)arg;
507249687Strociny	psinfo = calloc(1, sizeof(*psinfo));
508249687Strociny	if (psinfo == NULL)
509249687Strociny		errx(1, "out of memory");
510249687Strociny	psinfo->pr_version = PRPSINFO_VERSION;
511249687Strociny	psinfo->pr_psinfosz = sizeof(prpsinfo_t);
512249687Strociny
513249687Strociny	name[0] = CTL_KERN;
514249687Strociny	name[1] = KERN_PROC;
515249687Strociny	name[2] = KERN_PROC_PID;
516249687Strociny	name[3] = pid;
517249687Strociny	len = sizeof(kip);
518249687Strociny	if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
519249687Strociny		err(1, "kern.proc.pid.%u", pid);
520249687Strociny	if (kip.ki_pid != pid)
521249687Strociny		err(1, "kern.proc.pid.%u", pid);
522249687Strociny	strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN);
523249687Strociny	strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
524249687Strociny
525249687Strociny	*sizep = sizeof(*psinfo);
526249687Strociny	return (psinfo);
527249687Strociny}
528249687Strociny
529249687Strocinystatic void *
530249687Strocinyelf_note_prstatus(void *arg, size_t *sizep)
531249687Strociny{
532249687Strociny	lwpid_t tid;
533249687Strociny	prstatus_t *status;
534249687Strociny
535249687Strociny	tid = *(lwpid_t *)arg;
536249687Strociny	status = calloc(1, sizeof(*status));
537249687Strociny	if (status == NULL)
538249687Strociny		errx(1, "out of memory");
539249687Strociny	status->pr_version = PRSTATUS_VERSION;
540249687Strociny	status->pr_statussz = sizeof(prstatus_t);
541249687Strociny	status->pr_gregsetsz = sizeof(gregset_t);
542249687Strociny	status->pr_fpregsetsz = sizeof(fpregset_t);
543249687Strociny	status->pr_osreldate = __FreeBSD_version;
544249687Strociny	status->pr_pid = tid;
545249687Strociny	ptrace(PT_GETREGS, tid, (void *)&status->pr_reg, 0);
546249687Strociny
547249687Strociny	*sizep = sizeof(*status);
548249687Strociny	return (status);
549249687Strociny}
550249687Strociny
551249687Strocinystatic void *
552249687Strocinyelf_note_fpregset(void *arg, size_t *sizep)
553249687Strociny{
554249687Strociny	lwpid_t tid;
555249687Strociny	prfpregset_t *fpregset;
556249687Strociny
557249687Strociny	tid = *(lwpid_t *)arg;
558249687Strociny	fpregset = calloc(1, sizeof(*fpregset));
559249687Strociny	if (fpregset == NULL)
560249687Strociny		errx(1, "out of memory");
561249687Strociny	ptrace(PT_GETFPREGS, tid, (void *)fpregset, 0);
562249687Strociny
563249687Strociny	*sizep = sizeof(*fpregset);
564249687Strociny	return (fpregset);
565249687Strociny}
566249687Strociny
567249687Strocinystatic void *
568249687Strocinyelf_note_thrmisc(void *arg, size_t *sizep)
569249687Strociny{
570249687Strociny	lwpid_t tid;
571249687Strociny	struct ptrace_lwpinfo lwpinfo;
572249687Strociny	thrmisc_t *thrmisc;
573249687Strociny
574249687Strociny	tid = *(lwpid_t *)arg;
575249687Strociny	thrmisc = calloc(1, sizeof(*thrmisc));
576249687Strociny	if (thrmisc == NULL)
577249687Strociny		errx(1, "out of memory");
578249687Strociny	ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
579249687Strociny	    sizeof(lwpinfo));
580249687Strociny	memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
581249687Strociny	strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
582249687Strociny
583249687Strociny	*sizep = sizeof(*thrmisc);
584249687Strociny	return (thrmisc);
585249687Strociny}
586249687Strociny
587279211Sjhb#if defined(__i386__) || defined(__amd64__)
588249687Strocinystatic void *
589279211Sjhbelf_note_x86_xstate(void *arg, size_t *sizep)
590279211Sjhb{
591279211Sjhb	lwpid_t tid;
592279211Sjhb	char *xstate;
593279211Sjhb	static bool xsave_checked = false;
594279211Sjhb	static struct ptrace_xstate_info info;
595279211Sjhb
596279211Sjhb	tid = *(lwpid_t *)arg;
597279211Sjhb	if (!xsave_checked) {
598279211Sjhb		if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
599279211Sjhb		    sizeof(info)) != 0)
600279211Sjhb			info.xsave_len = 0;
601279211Sjhb		xsave_checked = true;
602279211Sjhb	}
603279211Sjhb	if (info.xsave_len == 0) {
604279211Sjhb		*sizep = 0;
605279211Sjhb		return (NULL);
606279211Sjhb	}
607279211Sjhb	xstate = calloc(1, info.xsave_len);
608279211Sjhb	ptrace(PT_GETXSTATE, tid, xstate, 0);
609279211Sjhb	*(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
610279211Sjhb	*sizep = info.xsave_len;
611279211Sjhb	return (xstate);
612279211Sjhb}
613279211Sjhb#endif
614279211Sjhb
615279211Sjhbstatic void *
616249687Strocinyprocstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
617249687Strociny{
618249687Strociny	size_t len, oldlen;
619249687Strociny	pid_t pid;
620249687Strociny	int name[4], structsize;
621249687Strociny	void *buf, *p;
622249687Strociny
623249687Strociny	pid = *(pid_t *)arg;
624249687Strociny	structsize = structsz;
625249687Strociny	name[0] = CTL_KERN;
626249687Strociny	name[1] = KERN_PROC;
627249687Strociny	name[2] = what;
628249687Strociny	name[3] = pid;
629249687Strociny	len = 0;
630249687Strociny	if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
631249687Strociny		err(1, "kern.proc.%d.%u", what, pid);
632249687Strociny	buf = calloc(1, sizeof(structsize) + len * 4 / 3);
633249687Strociny	if (buf == NULL)
634249687Strociny		errx(1, "out of memory");
635249687Strociny	bcopy(&structsize, buf, sizeof(structsize));
636249687Strociny	p = (char *)buf + sizeof(structsize);
637249687Strociny	if (sysctl(name, 4, p, &len, NULL, 0) == -1)
638249687Strociny		err(1, "kern.proc.%d.%u", what, pid);
639249687Strociny
640249687Strociny	*sizep = sizeof(structsize) + len;
641249687Strociny	return (buf);
642249687Strociny}
643249687Strociny
644249687Strocinystatic void *
645249687Strocinyelf_note_procstat_proc(void *arg, size_t *sizep)
646249687Strociny{
647249687Strociny
648249687Strociny	return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
649249687Strociny	    sizeof(struct kinfo_proc), sizep));
650249687Strociny}
651249687Strociny
652249687Strocinystatic void *
653249687Strocinyelf_note_procstat_files(void *arg, size_t *sizep)
654249687Strociny{
655249687Strociny
656249687Strociny	return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
657249687Strociny	    sizeof(struct kinfo_file), sizep));
658249687Strociny}
659249687Strociny
660249687Strocinystatic void *
661249687Strocinyelf_note_procstat_vmmap(void *arg, size_t *sizep)
662249687Strociny{
663249687Strociny
664249687Strociny	return (procstat_sysctl(arg, KERN_PROC_VMMAP,
665249687Strociny	    sizeof(struct kinfo_vmentry), sizep));
666249687Strociny}
667249687Strociny
668249687Strocinystatic void *
669249687Strocinyelf_note_procstat_groups(void *arg, size_t *sizep)
670249687Strociny{
671249687Strociny
672249704Strociny	return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
673249687Strociny}
674249687Strociny
675249687Strocinystatic void *
676249687Strocinyelf_note_procstat_umask(void *arg, size_t *sizep)
677249687Strociny{
678249687Strociny
679249687Strociny	return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
680249687Strociny}
681249687Strociny
682249687Strocinystatic void *
683249687Strocinyelf_note_procstat_osrel(void *arg, size_t *sizep)
684249687Strociny{
685249687Strociny
686249687Strociny	return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
687249687Strociny}
688249687Strociny
689249687Strocinystatic void *
690249687Strocinyelf_note_procstat_psstrings(void *arg, size_t *sizep)
691249687Strociny{
692249687Strociny
693249687Strociny	return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
694249687Strociny	    sizeof(vm_offset_t), sizep));
695249687Strociny}
696249687Strociny
697249687Strocinystatic void *
698249687Strocinyelf_note_procstat_auxv(void *arg, size_t *sizep)
699249687Strociny{
700249687Strociny
701249687Strociny	return (procstat_sysctl(arg, KERN_PROC_AUXV,
702249687Strociny	    sizeof(Elf_Auxinfo), sizep));
703249687Strociny}
704249687Strociny
705249687Strocinystatic void *
706249687Strocinyelf_note_procstat_rlimit(void *arg, size_t *sizep)
707249687Strociny{
708249687Strociny	pid_t pid;
709249687Strociny	size_t len;
710249687Strociny	int i, name[5], structsize;
711249687Strociny	void *buf, *p;
712249687Strociny
713249687Strociny	pid = *(pid_t *)arg;
714249687Strociny	structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
715249687Strociny	buf = calloc(1, sizeof(structsize) + structsize);
716249687Strociny	if (buf == NULL)
717249687Strociny		errx(1, "out of memory");
718249687Strociny	bcopy(&structsize, buf, sizeof(structsize));
719249687Strociny	p = (char *)buf + sizeof(structsize);
720249687Strociny	name[0] = CTL_KERN;
721249687Strociny	name[1] = KERN_PROC;
722249687Strociny	name[2] = KERN_PROC_RLIMIT;
723249687Strociny	name[3] = pid;
724249687Strociny	len = sizeof(struct rlimit);
725249687Strociny	for (i = 0; i < RLIM_NLIMITS; i++) {
726249687Strociny		name[4] = i;
727249687Strociny		if (sysctl(name, 5, p, &len, NULL, 0) == -1)
728249687Strociny			err(1, "kern.proc.rlimit.%u", pid);
729249687Strociny		if (len != sizeof(struct rlimit))
730249687Strociny			errx(1, "kern.proc.rlimit.%u: short read", pid);
731249687Strociny		p += len;
732249687Strociny	}
733249687Strociny
734249687Strociny	*sizep = sizeof(structsize) + structsize;
735249687Strociny	return (buf);
736249687Strociny}
737249687Strociny
738103299Speterstruct dumpers elfdump = { elf_ident, elf_coredump };
739103299SpeterTEXT_SET(dumpset, elfdump);
740