2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013 Mikolaj Golub <trociny@FreeBSD.org>
5 * Copyright (c) 2017 Dell EMC
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
33 #include <sys/ptrace.h>
50 #define PROCSTAT_CORE_MAGIC 0x012DADB8
60 static struct psc_type_info {
63 } psc_type_info[PSC_TYPE_MAX] = {
64 { .n_type = NT_PROCSTAT_PROC, .structsize = sizeof(struct kinfo_proc) },
65 { .n_type = NT_PROCSTAT_FILES, .structsize = sizeof(struct kinfo_file) },
66 { .n_type = NT_PROCSTAT_VMMAP, .structsize = sizeof(struct kinfo_vmentry) },
67 { .n_type = NT_PROCSTAT_GROUPS, .structsize = sizeof(gid_t) },
68 { .n_type = NT_PROCSTAT_UMASK, .structsize = sizeof(u_short) },
69 { .n_type = NT_PROCSTAT_RLIMIT, .structsize = sizeof(struct rlimit) * RLIM_NLIMITS },
70 { .n_type = NT_PROCSTAT_OSREL, .structsize = sizeof(int) },
71 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
72 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
73 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
74 { .n_type = NT_PROCSTAT_AUXV, .structsize = sizeof(Elf_Auxinfo) },
75 { .n_type = NT_PTLWPINFO, .structsize = sizeof(struct ptrace_lwpinfo) },
78 static bool core_offset(struct procstat_core *core, off_t offset);
79 static bool core_read(struct procstat_core *core, void *buf, size_t len);
80 static ssize_t core_read_mem(struct procstat_core *core, void *buf,
81 size_t len, vm_offset_t addr, bool readall);
82 static void *get_args(struct procstat_core *core, vm_offset_t psstrings,
83 enum psc_type type, void *buf, size_t *lenp);
85 struct procstat_core *
86 procstat_core_open(const char *filename)
88 struct procstat_core *core;
95 if (elf_version(EV_CURRENT) == EV_NONE) {
96 warnx("ELF library too old");
99 fd = open(filename, O_RDONLY, 0);
101 warn("open(%s)", filename);
104 e = elf_begin(fd, ELF_C_READ, NULL);
106 warnx("elf_begin: %s", elf_errmsg(-1));
109 if (elf_kind(e) != ELF_K_ELF) {
110 warnx("%s is not an ELF object", filename);
113 if (gelf_getehdr(e, &ehdr) == NULL) {
114 warnx("gelf_getehdr: %s", elf_errmsg(-1));
117 if (ehdr.e_type != ET_CORE) {
118 warnx("%s is not a CORE file", filename);
121 if (elf_getphdrnum(e, &nph) == -1) {
122 warnx("program headers not found");
125 for (i = 0; i < ehdr.e_phnum; i++) {
126 if (gelf_getphdr(e, i, &phdr) != &phdr) {
127 warnx("gelf_getphdr: %s", elf_errmsg(-1));
130 if (phdr.p_type == PT_NOTE)
133 if (i == ehdr.e_phnum) {
134 warnx("NOTE program header not found");
137 core = malloc(sizeof(struct procstat_core));
139 warn("malloc(%zu)", sizeof(struct procstat_core));
142 core->pc_magic = PROCSTAT_CORE_MAGIC;
145 core->pc_ehdr = ehdr;
146 core->pc_phdr = phdr;
158 procstat_core_close(struct procstat_core *core)
161 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
163 elf_end(core->pc_elf);
169 procstat_core_get(struct procstat_core *core, enum psc_type type, void *buf,
173 off_t offset, eoffset;
174 vm_offset_t psstrings;
180 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
182 if (type >= PSC_TYPE_MAX) {
183 warnx("unknown core stat type: %d", type);
187 offset = core->pc_phdr.p_offset;
188 eoffset = offset + core->pc_phdr.p_filesz;
191 while (offset < eoffset) {
192 if (!core_offset(core, offset))
194 if (!core_read(core, &nhdr, sizeof(nhdr)))
197 offset += sizeof(nhdr) +
198 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
199 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
201 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
203 if (nhdr.n_type != psc_type_info[type].n_type)
205 if (nhdr.n_namesz != 8)
207 if (!core_read(core, nbuf, sizeof(nbuf)))
209 if (strcmp(nbuf, "FreeBSD") != 0)
211 if (nhdr.n_descsz < sizeof(cstructsize)) {
212 warnx("corrupted core file");
215 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
217 if (cstructsize != psc_type_info[type].structsize) {
218 warnx("version mismatch");
221 len = nhdr.n_descsz - sizeof(cstructsize);
225 len = MIN(len, *lenp);
228 freebuf = buf = malloc(len);
230 warn("malloc(%zu)", len);
234 if (!core_read(core, (char *)buf + curlen, len)) {
238 if (type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV) {
239 if (len < sizeof(psstrings)) {
243 psstrings = *(vm_offset_t *)buf;
249 buf = get_args(core, psstrings, type, buf, &len);
250 } else if (type == PSC_TYPE_PTLWPINFO) {
268 core_offset(struct procstat_core *core, off_t offset)
271 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
273 if (lseek(core->pc_fd, offset, SEEK_SET) == -1) {
274 warn("core: lseek(%jd)", (intmax_t)offset);
281 core_read(struct procstat_core *core, void *buf, size_t len)
285 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
287 n = read(core->pc_fd, buf, len);
292 if (n < (ssize_t)len) {
293 warnx("core: short read");
300 core_read_mem(struct procstat_core *core, void *buf, size_t len,
301 vm_offset_t addr, bool readall)
307 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
309 for (i = 0; i < core->pc_ehdr.e_phnum; i++) {
310 if (gelf_getphdr(core->pc_elf, i, &phdr) != &phdr) {
311 warnx("gelf_getphdr: %s", elf_errmsg(-1));
314 if (phdr.p_type != PT_LOAD)
316 if (addr < phdr.p_vaddr || addr > phdr.p_vaddr + phdr.p_memsz)
318 offset = phdr.p_offset + (addr - phdr.p_vaddr);
319 if ((phdr.p_vaddr + phdr.p_memsz) - addr < len) {
321 warnx("format error: "
322 "attempt to read out of segment");
325 len = (phdr.p_vaddr + phdr.p_memsz) - addr;
327 if (!core_offset(core, offset))
329 if (!core_read(core, buf, len))
333 warnx("format error: address %ju not found", (uintmax_t)addr);
337 #define ARGS_CHUNK_SZ 256 /* Chunk size (bytes) for get_args operations. */
340 get_args(struct procstat_core *core, vm_offset_t psstrings, enum psc_type type,
341 void *args, size_t *lenp)
343 struct ps_strings pss;
347 size_t chunksz, done, len, nchr, size;
351 assert(type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV);
353 if (core_read_mem(core, &pss, sizeof(pss), psstrings, true) == -1)
355 if (type == PSC_TYPE_ARGV) {
356 addr = (vm_offset_t)pss.ps_argvstr;
357 nstr = pss.ps_nargvstr;
358 } else /* type == PSC_TYPE_ENVV */ {
359 addr = (vm_offset_t)pss.ps_envstr;
360 nstr = pss.ps_nenvstr;
362 if (addr == 0 || nstr == 0)
364 if (nstr > ARG_MAX) {
365 warnx("format error");
368 size = nstr * sizeof(char *);
371 warn("malloc(%zu)", size);
376 if (core_read_mem(core, argv, size, addr, true) == -1)
379 nchr = MIN(ARG_MAX, *lenp);
382 freeargs = args = malloc(nchr);
384 warn("malloc(%zu)", nchr);
393 * The program may have scribbled into its argv array, e.g. to
394 * remove some arguments. If that has happened, break out
395 * before trying to read from NULL.
399 for (addr = (vm_offset_t)argv[i]; ; addr += chunksz) {
400 chunksz = MIN(ARGS_CHUNK_SZ, nchr - 1 - done);
403 n = core_read_mem(core, p, chunksz, addr, false);
406 len = strnlen(p, chunksz);
425 procstat_core_note_count(struct procstat_core *core, enum psc_type type)
428 off_t offset, eoffset;
433 if (type >= PSC_TYPE_MAX) {
434 warnx("unknown core stat type: %d", type);
438 offset = core->pc_phdr.p_offset;
439 eoffset = offset + core->pc_phdr.p_filesz;
441 for (n = 0; offset < eoffset; n++) {
442 if (!core_offset(core, offset))
444 if (!core_read(core, &nhdr, sizeof(nhdr)))
447 offset += sizeof(nhdr) +
448 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
449 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
451 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
453 if (nhdr.n_type != psc_type_info[type].n_type)
455 if (nhdr.n_namesz != 8)
457 if (!core_read(core, nbuf, sizeof(nbuf)))
459 if (strcmp(nbuf, "FreeBSD") != 0)
461 if (nhdr.n_descsz < sizeof(cstructsize)) {
462 warnx("corrupted core file");
465 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
467 if (cstructsize != psc_type_info[type].structsize) {
468 warnx("version mismatch");
471 if (nhdr.n_descsz - sizeof(cstructsize) == 0)