elfcore.c revision 303058
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2007 Sandvine Incorporated
31541Srgrimes * Copyright (c) 1998 John D. Polstra
41541Srgrimes * All rights reserved.
51541Srgrimes *
61541Srgrimes * Redistribution and use in source and binary forms, with or without
71541Srgrimes * modification, are permitted provided that the following conditions
81541Srgrimes * are met:
91541Srgrimes * 1. Redistributions of source code must retain the above copyright
101541Srgrimes *    notice, this list of conditions and the following disclaimer.
111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in the
131541Srgrimes *    documentation and/or other materials provided with the distribution.
141541Srgrimes *
151541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251541Srgrimes * SUCH DAMAGE.
261541Srgrimes */
271541Srgrimes
281541Srgrimes#include <sys/cdefs.h>
291541Srgrimes__FBSDID("$FreeBSD: stable/10/usr.bin/gcore/elfcore.c 303058 2016-07-20 00:53:21Z markj $");
301541Srgrimes
311541Srgrimes#include <sys/endian.h>
321541Srgrimes#include <sys/param.h>
331541Srgrimes#include <sys/procfs.h>
3415026Sphk#include <sys/ptrace.h>
351541Srgrimes#include <sys/queue.h>
361541Srgrimes#include <sys/linker_set.h>
372169Spaul#include <sys/sbuf.h>
382169Spaul#include <sys/sysctl.h>
392169Spaul#include <sys/user.h>
401541Srgrimes#include <sys/wait.h>
411541Srgrimes#include <machine/elf.h>
421541Srgrimes#include <vm/vm_param.h>
431541Srgrimes#include <vm/vm.h>
441541Srgrimes#include <vm/pmap.h>
451541Srgrimes#include <vm/vm_map.h>
461541Srgrimes#include <assert.h>
471541Srgrimes#include <err.h>
481541Srgrimes#include <errno.h>
491541Srgrimes#include <fcntl.h>
501541Srgrimes#include <stdbool.h>
511541Srgrimes#include <stdint.h>
529209Swollman#include <stdio.h>
531541Srgrimes#include <stdlib.h>
541541Srgrimes#include <string.h>
551541Srgrimes#include <unistd.h>
561541Srgrimes#include <libutil.h>
571541Srgrimes
581541Srgrimes#include "extern.h"
592531Swollman
601541Srgrimes/*
611541Srgrimes * Code for generating ELF core dumps.
621541Srgrimes */
631541Srgrimes
641541Srgrimestypedef void (*segment_callback)(vm_map_entry_t, void *);
651541Srgrimes
661541Srgrimes/* Closure for cb_put_phdr(). */
671541Srgrimesstruct phdr_closure {
681541Srgrimes	Elf_Phdr *phdr;		/* Program header to fill in */
6914195Speter	Elf_Off offset;		/* Offset of segment in core file */
7014195Speter};
7114195Speter
7214195Speter/* Closure for cb_size_segment(). */
7314195Speterstruct sseg_closure {
7414195Speter	int count;		/* Count of writable segments. */
7514195Speter	size_t size;		/* Total size of all writable segments. */
7614195Speter};
7714195Speter
7814195Speter#ifdef ELFCORE_COMPAT_32
7914195Spetertypedef struct fpreg32 elfcore_fpregset_t;
8014195Spetertypedef struct reg32   elfcore_gregset_t;
8114195Spetertypedef struct prpsinfo32 elfcore_prpsinfo_t;
8214195Spetertypedef struct prstatus32 elfcore_prstatus_t;
8314195Speterstatic void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs);
8414195Speterstatic void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs);
8514195Speter#else
8614195Spetertypedef fpregset_t elfcore_fpregset_t;
8714195Spetertypedef gregset_t  elfcore_gregset_t;
8814195Spetertypedef prpsinfo_t elfcore_prpsinfo_t;
8914195Spetertypedef prstatus_t elfcore_prstatus_t;
9014195Speter#define elf_convert_gregset(d,s)	*d = *s
9114195Speter#define elf_convert_fpregset(d,s)	*d = *s
9214195Speter#endif
9314195Speter
9414195Spetertypedef void* (*notefunc_t)(void *, size_t *);
9514195Speter
9614195Speterstatic void cb_put_phdr(vm_map_entry_t, void *);
9714195Speterstatic void cb_size_segment(vm_map_entry_t, void *);
9814195Speterstatic void each_writable_segment(vm_map_entry_t, segment_callback,
9914195Speter    void *closure);
10014195Speterstatic void elf_detach(void);	/* atexit() handler. */
10114195Speterstatic void *elf_note_fpregset(void *, size_t *);
1021541Srgrimesstatic void *elf_note_prpsinfo(void *, size_t *);
10314195Speterstatic void *elf_note_prstatus(void *, size_t *);
1041541Srgrimesstatic void *elf_note_thrmisc(void *, size_t *);
10514195Speter#if defined(__i386__) || defined(__amd64__)
1061541Srgrimesstatic void *elf_note_x86_xstate(void *, size_t *);
1071541Srgrimes#endif
1081541Srgrimesstatic void *elf_note_procstat_auxv(void *, size_t *);
1091541Srgrimesstatic void *elf_note_procstat_files(void *, size_t *);
1101541Srgrimesstatic void *elf_note_procstat_groups(void *, size_t *);
11114195Speterstatic void *elf_note_procstat_osrel(void *, size_t *);
11213491Speterstatic void *elf_note_procstat_proc(void *, size_t *);
11314195Speterstatic void *elf_note_procstat_psstrings(void *, size_t *);
11414195Speterstatic void *elf_note_procstat_rlimit(void *, size_t *);
11513491Speterstatic void *elf_note_procstat_umask(void *, size_t *);
11613491Speterstatic void *elf_note_procstat_vmmap(void *, size_t *);
1171541Srgrimesstatic void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
1181541Srgrimes    int);
1191541Srgrimesstatic void elf_putnote(int, notefunc_t, void *, struct sbuf *);
1201541Srgrimesstatic void elf_putnotes(pid_t, struct sbuf *, size_t *);
1211541Srgrimesstatic void freemap(vm_map_entry_t);
1221541Srgrimesstatic vm_map_entry_t readmap(pid_t);
1231541Srgrimesstatic void *procstat_sysctl(void *, int, size_t, size_t *sizep);
1241541Srgrimes
1251541Srgrimesstatic pid_t g_pid;		/* Pid being dumped, global for elf_detach */
1261541Srgrimesstatic int g_status;		/* proc status after ptrace attach */
1271541Srgrimes
1281541Srgrimesstatic int
1291541Srgrimeself_ident(int efd, pid_t pid __unused, char *binfile __unused)
1301541Srgrimes{
1311541Srgrimes	Elf_Ehdr hdr;
1321541Srgrimes	int cnt;
1331541Srgrimes	uint16_t machine;
1341541Srgrimes
1351541Srgrimes	cnt = read(efd, &hdr, sizeof(hdr));
1361541Srgrimes	if (cnt != sizeof(hdr))
1371541Srgrimes		return (0);
1381541Srgrimes	if (!IS_ELF(hdr))
1391541Srgrimes		return (0);
1401541Srgrimes	switch (hdr.e_ident[EI_DATA]) {
1411541Srgrimes	case ELFDATA2LSB:
1421541Srgrimes		machine = le16toh(hdr.e_machine);
1431541Srgrimes		break;
1441541Srgrimes	case ELFDATA2MSB:
1451541Srgrimes		machine = be16toh(hdr.e_machine);
1461541Srgrimes		break;
1471541Srgrimes	default:
1481541Srgrimes		return (0);
1491541Srgrimes	}
1501541Srgrimes	if (!ELF_MACHINE_OK(machine))
1511541Srgrimes		return (0);
1521541Srgrimes
1531541Srgrimes	/* Looks good. */
1541541Srgrimes	return (1);
1551541Srgrimes}
1561541Srgrimes
1571541Srgrimesstatic void
1581541Srgrimeself_detach(void)
1591541Srgrimes{
1601541Srgrimes	int sig;
1611541Srgrimes
16214622Sfenner	if (g_pid != 0) {
1631541Srgrimes		/*
1641541Srgrimes		 * Forward any pending signals. SIGSTOP is generated by ptrace
1651541Srgrimes		 * itself, so ignore it.
1661541Srgrimes		 */
1671541Srgrimes		sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0;
1681541Srgrimes		if (sig == SIGSTOP)
1691541Srgrimes			sig = 0;
1701541Srgrimes		ptrace(PT_DETACH, g_pid, (caddr_t)1, sig);
1711541Srgrimes	}
1721541Srgrimes}
1731541Srgrimes
1741541Srgrimes/*
1751541Srgrimes * Write an ELF coredump for the given pid to the given fd.
1761541Srgrimes */
1771541Srgrimesstatic void
1781541Srgrimeself_coredump(int efd __unused, int fd, pid_t pid)
1791541Srgrimes{
1801541Srgrimes	vm_map_entry_t map;
1811541Srgrimes	struct sseg_closure seginfo;
1821541Srgrimes	struct sbuf *sb;
1831541Srgrimes	void *hdr;
1841541Srgrimes	size_t hdrsize, notesz, segoff;
1851541Srgrimes	ssize_t n, old_len;
1861541Srgrimes	Elf_Phdr *php;
1871541Srgrimes	int i;
1881541Srgrimes
1891541Srgrimes	/* Attach to process to dump. */
1901541Srgrimes	g_pid = pid;
1911541Srgrimes	if (atexit(elf_detach) != 0)
1921541Srgrimes		err(1, "atexit");
1931541Srgrimes	errno = 0;
1941541Srgrimes	ptrace(PT_ATTACH, pid, NULL, 0);
1951541Srgrimes	if (errno)
1961541Srgrimes		err(1, "PT_ATTACH");
1971541Srgrimes	if (waitpid(pid, &g_status, 0) == -1)
1981541Srgrimes		err(1, "waitpid");
1991541Srgrimes
2001541Srgrimes	/* Get the program's memory map. */
2011541Srgrimes	map = readmap(pid);
2021541Srgrimes
2031541Srgrimes	/* Size the program segments. */
2041541Srgrimes	seginfo.count = 0;
2051541Srgrimes	seginfo.size = 0;
2061541Srgrimes	each_writable_segment(map, cb_size_segment, &seginfo);
2072531Swollman
2082531Swollman	/*
2092531Swollman	 * Build the header and the notes using sbuf and write to the file.
2109209Swollman	 */
2119209Swollman	sb = sbuf_new_auto();
21214195Speter	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
2131541Srgrimes	/* Start header + notes section. */
2141541Srgrimes	sbuf_start_section(sb, NULL);
2151541Srgrimes	/* Make empty header subsection. */
2161541Srgrimes	sbuf_start_section(sb, &old_len);
2171541Srgrimes	sbuf_putc(sb, 0);
2181541Srgrimes	sbuf_end_section(sb, old_len, hdrsize, 0);
2199209Swollman	/* Put notes. */
2201541Srgrimes	elf_putnotes(pid, sb, &notesz);
2211541Srgrimes	/* Align up to a page boundary for the program segments. */
2221541Srgrimes	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
2231541Srgrimes	if (sbuf_finish(sb) != 0)
2241541Srgrimes		err(1, "sbuf_finish");
2251541Srgrimes	hdr = sbuf_data(sb);
2261541Srgrimes	segoff = sbuf_len(sb);
2271541Srgrimes	/* Fill in the header. */
2281541Srgrimes	elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
2291541Srgrimes
23014195Speter	n = write(fd, hdr, segoff);
23114195Speter	if (n == -1)
23214195Speter		err(1, "write");
23314195Speter	if (n < segoff)
23414195Speter              errx(1, "short write");
23514195Speter
23614195Speter	/* Write the contents of all of the writable segments. */
23714195Speter	php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
2381541Srgrimes	for (i = 0;  i < seginfo.count;  i++) {
2391541Srgrimes		struct ptrace_io_desc iorequest;
2401541Srgrimes		uintmax_t nleft = php->p_filesz;
2411541Srgrimes
2421541Srgrimes		iorequest.piod_op = PIOD_READ_D;
2431541Srgrimes		iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
2441541Srgrimes		while (nleft > 0) {
2451541Srgrimes			char buf[8*1024];
2461541Srgrimes			size_t nwant;
2471541Srgrimes			ssize_t ngot;
2481541Srgrimes
2491541Srgrimes			if (nleft > sizeof(buf))
2501541Srgrimes				nwant = sizeof buf;
2511541Srgrimes			else
2521541Srgrimes				nwant = nleft;
2531541Srgrimes			iorequest.piod_addr = buf;
2541541Srgrimes			iorequest.piod_len = nwant;
2551541Srgrimes			ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
2561541Srgrimes			ngot = iorequest.piod_len;
2571541Srgrimes			if ((size_t)ngot < nwant)
2581541Srgrimes				errx(1, "short read wanted %zu, got %zd",
2591541Srgrimes				    nwant, ngot);
2601541Srgrimes			ngot = write(fd, buf, nwant);
2611541Srgrimes			if (ngot == -1)
2621541Srgrimes				err(1, "write of segment %d failed", i);
2631541Srgrimes			if ((size_t)ngot != nwant)
2641541Srgrimes				errx(1, "short write");
2651541Srgrimes			nleft -= nwant;
2661541Srgrimes			iorequest.piod_offs += ngot;
2671541Srgrimes		}
2681541Srgrimes		php++;
2691541Srgrimes	}
2701541Srgrimes	sbuf_delete(sb);
2711541Srgrimes	freemap(map);
2721541Srgrimes}
2731541Srgrimes
2741541Srgrimes/*
2751541Srgrimes * A callback for each_writable_segment() to write out the segment's
2761541Srgrimes * program header entry.
2771541Srgrimes */
2781541Srgrimesstatic void
2791541Srgrimescb_put_phdr(vm_map_entry_t entry, void *closure)
2805109Swollman{
2816399Swollman	struct phdr_closure *phc = (struct phdr_closure *)closure;
2826399Swollman	Elf_Phdr *phdr = phc->phdr;
2837091Swollman
2849575Speter	phc->offset = round_page(phc->offset);
28512003Swollman
28612003Swollman	phdr->p_type = PT_LOAD;
28712003Swollman	phdr->p_offset = phc->offset;
2881541Srgrimes	phdr->p_vaddr = entry->start;
2891541Srgrimes	phdr->p_paddr = 0;
2901541Srgrimes	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
2911541Srgrimes	phdr->p_align = PAGE_SIZE;
2921541Srgrimes	phdr->p_flags = 0;
2931541Srgrimes	if (entry->protection & VM_PROT_READ)
2941541Srgrimes		phdr->p_flags |= PF_R;
2955109Swollman	if (entry->protection & VM_PROT_WRITE)
2966399Swollman		phdr->p_flags |= PF_W;
2976399Swollman	if (entry->protection & VM_PROT_EXECUTE)
2987091Swollman		phdr->p_flags |= PF_X;
2999575Speter
30012003Swollman	phc->offset += phdr->p_filesz;
30112003Swollman	phc->phdr++;
3021541Srgrimes}
3031541Srgrimes
3041541Srgrimes/*
3051541Srgrimes * A callback for each_writable_segment() to gather information about
3067088Swollman * the number of segments and their total size.
3077088Swollman */
3081541Srgrimesstatic void
3091541Srgrimescb_size_segment(vm_map_entry_t entry, void *closure)
3101541Srgrimes{
3111541Srgrimes	struct sseg_closure *ssc = (struct sseg_closure *)closure;
3127088Swollman
3132169Spaul	ssc->count++;
31415026Sphk	ssc->size += entry->end - entry->start;
31515026Sphk}
31615026Sphk
31715026Sphk/*
31815026Sphk * For each segment in the given memory map, call the given function
31915026Sphk * with a pointer to the map entry and some arbitrary caller-supplied
32015026Sphk * data.
32115026Sphk */
3222169Spaulstatic void
323each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
324{
325	vm_map_entry_t entry;
326
327	for (entry = map;  entry != NULL;  entry = entry->next)
328		(*func)(entry, closure);
329}
330
331static void
332elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
333{
334	lwpid_t *tids;
335	size_t threads, old_len;
336	ssize_t size;
337	int i;
338
339	errno = 0;
340	threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
341	if (errno)
342		err(1, "PT_GETNUMLWPS");
343	tids = malloc(threads * sizeof(*tids));
344	if (tids == NULL)
345		errx(1, "out of memory");
346	errno = 0;
347	ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
348	if (errno)
349		err(1, "PT_GETLWPLIST");
350
351	sbuf_start_section(sb, &old_len);
352	elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
353
354	for (i = 0; i < threads; ++i) {
355		elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
356		elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
357		elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
358#if defined(__i386__) || defined(__amd64__)
359		elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
360#endif
361	}
362
363#ifndef ELFCORE_COMPAT_32
364	elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
365	elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
366	elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
367	elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
368	elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
369	elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
370	elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
371	elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
372	    sb);
373	elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
374#endif
375
376	size = sbuf_end_section(sb, old_len, 1, 0);
377	if (size == -1)
378		err(1, "sbuf_end_section");
379	free(tids);
380	*sizep = size;
381}
382
383/*
384 * Emit one note section to sbuf.
385 */
386static void
387elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
388{
389	Elf_Note note;
390	size_t descsz;
391	ssize_t old_len;
392	void *desc;
393
394	desc = notefunc(arg, &descsz);
395	note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
396	note.n_descsz = descsz;
397	note.n_type = type;
398
399	sbuf_bcat(sb, &note, sizeof(note));
400	sbuf_start_section(sb, &old_len);
401	sbuf_bcat(sb, "FreeBSD", note.n_namesz);
402	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
403	if (descsz == 0)
404		return;
405	sbuf_start_section(sb, &old_len);
406	sbuf_bcat(sb, desc, descsz);
407	sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
408	free(desc);
409}
410
411/*
412 * Generate the ELF coredump header.
413 */
414static void
415elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
416    size_t notesz, size_t segoff, int numsegs)
417{
418	Elf_Ehdr *ehdr;
419	Elf_Phdr *phdr;
420	struct phdr_closure phc;
421
422	ehdr = (Elf_Ehdr *)hdr;
423	phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
424
425	ehdr->e_ident[EI_MAG0] = ELFMAG0;
426	ehdr->e_ident[EI_MAG1] = ELFMAG1;
427	ehdr->e_ident[EI_MAG2] = ELFMAG2;
428	ehdr->e_ident[EI_MAG3] = ELFMAG3;
429	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
430	ehdr->e_ident[EI_DATA] = ELF_DATA;
431	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
432	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
433	ehdr->e_ident[EI_ABIVERSION] = 0;
434	ehdr->e_ident[EI_PAD] = 0;
435	ehdr->e_type = ET_CORE;
436	ehdr->e_machine = ELF_ARCH;
437	ehdr->e_version = EV_CURRENT;
438	ehdr->e_entry = 0;
439	ehdr->e_phoff = sizeof(Elf_Ehdr);
440	ehdr->e_flags = 0;
441	ehdr->e_ehsize = sizeof(Elf_Ehdr);
442	ehdr->e_phentsize = sizeof(Elf_Phdr);
443	ehdr->e_phnum = numsegs + 1;
444	ehdr->e_shentsize = sizeof(Elf_Shdr);
445	ehdr->e_shnum = 0;
446	ehdr->e_shstrndx = SHN_UNDEF;
447
448	/*
449	 * Fill in the program header entries.
450	 */
451
452	/* The note segement. */
453	phdr->p_type = PT_NOTE;
454	phdr->p_offset = hdrsize;
455	phdr->p_vaddr = 0;
456	phdr->p_paddr = 0;
457	phdr->p_filesz = notesz;
458	phdr->p_memsz = 0;
459	phdr->p_flags = PF_R;
460	phdr->p_align = sizeof(Elf32_Size);
461	phdr++;
462
463	/* All the writable segments from the program. */
464	phc.phdr = phdr;
465	phc.offset = segoff;
466	each_writable_segment(map, cb_put_phdr, &phc);
467}
468
469/*
470 * Free the memory map.
471 */
472static void
473freemap(vm_map_entry_t map)
474{
475
476	while (map != NULL) {
477		vm_map_entry_t next = map->next;
478		free(map);
479		map = next;
480	}
481}
482
483/*
484 * Read the process's memory map using kinfo_getvmmap(), and return a list of
485 * VM map entries.  Only the non-device read/writable segments are
486 * returned.  The map entries in the list aren't fully filled in; only
487 * the items we need are present.
488 */
489static vm_map_entry_t
490readmap(pid_t pid)
491{
492	vm_map_entry_t ent, *linkp, map;
493	struct kinfo_vmentry *vmentl, *kve;
494	int i, nitems;
495
496	vmentl = kinfo_getvmmap(pid, &nitems);
497	if (vmentl == NULL)
498		err(1, "cannot retrieve mappings for %u process", pid);
499
500	map = NULL;
501	linkp = &map;
502	for (i = 0; i < nitems; i++) {
503		kve = &vmentl[i];
504
505		/*
506		 * Ignore 'malformed' segments or ones representing memory
507		 * mapping with MAP_NOCORE on.
508		 * If the 'full' support is disabled, just dump the most
509		 * meaningful data segments.
510		 */
511		if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
512		    (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
513		    kve->kve_type == KVME_TYPE_DEAD ||
514		    kve->kve_type == KVME_TYPE_UNKNOWN ||
515		    ((pflags & PFLAGS_FULL) == 0 &&
516		    kve->kve_type != KVME_TYPE_DEFAULT &&
517		    kve->kve_type != KVME_TYPE_VNODE &&
518		    kve->kve_type != KVME_TYPE_SWAP &&
519		    kve->kve_type != KVME_TYPE_PHYS))
520			continue;
521
522		ent = calloc(1, sizeof(*ent));
523		if (ent == NULL)
524			errx(1, "out of memory");
525		ent->start = (vm_offset_t)kve->kve_start;
526		ent->end = (vm_offset_t)kve->kve_end;
527		ent->protection = VM_PROT_READ | VM_PROT_WRITE;
528		if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
529			ent->protection |= VM_PROT_EXECUTE;
530
531		*linkp = ent;
532		linkp = &ent->next;
533	}
534	free(vmentl);
535	return (map);
536}
537
538/*
539 * Miscellaneous note out functions.
540 */
541
542static void *
543elf_note_prpsinfo(void *arg, size_t *sizep)
544{
545	pid_t pid;
546	elfcore_prpsinfo_t *psinfo;
547	struct kinfo_proc kip;
548	size_t len;
549	int name[4];
550
551	pid = *(pid_t *)arg;
552	psinfo = calloc(1, sizeof(*psinfo));
553	if (psinfo == NULL)
554		errx(1, "out of memory");
555	psinfo->pr_version = PRPSINFO_VERSION;
556	psinfo->pr_psinfosz = sizeof(*psinfo);
557
558	name[0] = CTL_KERN;
559	name[1] = KERN_PROC;
560	name[2] = KERN_PROC_PID;
561	name[3] = pid;
562	len = sizeof(kip);
563	if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
564		err(1, "kern.proc.pid.%u", pid);
565	if (kip.ki_pid != pid)
566		err(1, "kern.proc.pid.%u", pid);
567	strncpy(psinfo->pr_fname, kip.ki_comm, MAXCOMLEN);
568	strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ);
569
570	*sizep = sizeof(*psinfo);
571	return (psinfo);
572}
573
574static void *
575elf_note_prstatus(void *arg, size_t *sizep)
576{
577	lwpid_t tid;
578	elfcore_prstatus_t *status;
579	struct reg greg;
580
581	tid = *(lwpid_t *)arg;
582	status = calloc(1, sizeof(*status));
583	if (status == NULL)
584		errx(1, "out of memory");
585	status->pr_version = PRSTATUS_VERSION;
586	status->pr_statussz = sizeof(*status);
587	status->pr_gregsetsz = sizeof(elfcore_gregset_t);
588	status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
589	status->pr_osreldate = __FreeBSD_version;
590	status->pr_pid = tid;
591	ptrace(PT_GETREGS, tid, (void *)&greg, 0);
592	elf_convert_gregset(&status->pr_reg, &greg);
593
594	*sizep = sizeof(*status);
595	return (status);
596}
597
598static void *
599elf_note_fpregset(void *arg, size_t *sizep)
600{
601	lwpid_t tid;
602	elfcore_fpregset_t *fpregset;
603	fpregset_t fpreg;
604
605	tid = *(lwpid_t *)arg;
606	fpregset = calloc(1, sizeof(*fpregset));
607	if (fpregset == NULL)
608		errx(1, "out of memory");
609	ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
610	elf_convert_fpregset(fpregset, &fpreg);
611
612	*sizep = sizeof(*fpregset);
613	return (fpregset);
614}
615
616static void *
617elf_note_thrmisc(void *arg, size_t *sizep)
618{
619	lwpid_t tid;
620	struct ptrace_lwpinfo lwpinfo;
621	thrmisc_t *thrmisc;
622
623	tid = *(lwpid_t *)arg;
624	thrmisc = calloc(1, sizeof(*thrmisc));
625	if (thrmisc == NULL)
626		errx(1, "out of memory");
627	ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
628	    sizeof(lwpinfo));
629	memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
630	strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
631
632	*sizep = sizeof(*thrmisc);
633	return (thrmisc);
634}
635
636#if defined(__i386__) || defined(__amd64__)
637static void *
638elf_note_x86_xstate(void *arg, size_t *sizep)
639{
640	lwpid_t tid;
641	char *xstate;
642	static bool xsave_checked = false;
643	static struct ptrace_xstate_info info;
644
645	tid = *(lwpid_t *)arg;
646	if (!xsave_checked) {
647		if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
648		    sizeof(info)) != 0)
649			info.xsave_len = 0;
650		xsave_checked = true;
651	}
652	if (info.xsave_len == 0) {
653		*sizep = 0;
654		return (NULL);
655	}
656	xstate = calloc(1, info.xsave_len);
657	ptrace(PT_GETXSTATE, tid, xstate, 0);
658	*(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
659	*sizep = info.xsave_len;
660	return (xstate);
661}
662#endif
663
664static void *
665procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
666{
667	size_t len, oldlen;
668	pid_t pid;
669	int name[4], structsize;
670	void *buf, *p;
671
672	pid = *(pid_t *)arg;
673	structsize = structsz;
674	name[0] = CTL_KERN;
675	name[1] = KERN_PROC;
676	name[2] = what;
677	name[3] = pid;
678	len = 0;
679	if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
680		err(1, "kern.proc.%d.%u", what, pid);
681	buf = calloc(1, sizeof(structsize) + len * 4 / 3);
682	if (buf == NULL)
683		errx(1, "out of memory");
684	bcopy(&structsize, buf, sizeof(structsize));
685	p = (char *)buf + sizeof(structsize);
686	if (sysctl(name, 4, p, &len, NULL, 0) == -1)
687		err(1, "kern.proc.%d.%u", what, pid);
688
689	*sizep = sizeof(structsize) + len;
690	return (buf);
691}
692
693static void *
694elf_note_procstat_proc(void *arg, size_t *sizep)
695{
696
697	return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
698	    sizeof(struct kinfo_proc), sizep));
699}
700
701static void *
702elf_note_procstat_files(void *arg, size_t *sizep)
703{
704
705	return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
706	    sizeof(struct kinfo_file), sizep));
707}
708
709static void *
710elf_note_procstat_vmmap(void *arg, size_t *sizep)
711{
712
713	return (procstat_sysctl(arg, KERN_PROC_VMMAP,
714	    sizeof(struct kinfo_vmentry), sizep));
715}
716
717static void *
718elf_note_procstat_groups(void *arg, size_t *sizep)
719{
720
721	return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
722}
723
724static void *
725elf_note_procstat_umask(void *arg, size_t *sizep)
726{
727
728	return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
729}
730
731static void *
732elf_note_procstat_osrel(void *arg, size_t *sizep)
733{
734
735	return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
736}
737
738static void *
739elf_note_procstat_psstrings(void *arg, size_t *sizep)
740{
741
742	return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
743	    sizeof(vm_offset_t), sizep));
744}
745
746static void *
747elf_note_procstat_auxv(void *arg, size_t *sizep)
748{
749
750	return (procstat_sysctl(arg, KERN_PROC_AUXV,
751	    sizeof(Elf_Auxinfo), sizep));
752}
753
754static void *
755elf_note_procstat_rlimit(void *arg, size_t *sizep)
756{
757	pid_t pid;
758	size_t len;
759	int i, name[5], structsize;
760	void *buf, *p;
761
762	pid = *(pid_t *)arg;
763	structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
764	buf = calloc(1, sizeof(structsize) + structsize);
765	if (buf == NULL)
766		errx(1, "out of memory");
767	bcopy(&structsize, buf, sizeof(structsize));
768	p = (char *)buf + sizeof(structsize);
769	name[0] = CTL_KERN;
770	name[1] = KERN_PROC;
771	name[2] = KERN_PROC_RLIMIT;
772	name[3] = pid;
773	len = sizeof(struct rlimit);
774	for (i = 0; i < RLIM_NLIMITS; i++) {
775		name[4] = i;
776		if (sysctl(name, 5, p, &len, NULL, 0) == -1)
777			err(1, "kern.proc.rlimit.%u", pid);
778		if (len != sizeof(struct rlimit))
779			errx(1, "kern.proc.rlimit.%u: short read", pid);
780		p += len;
781	}
782
783	*sizep = sizeof(structsize) + structsize;
784	return (buf);
785}
786
787struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
788TEXT_SET(dumpset, __elfN(dump));
789