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/cdefs.h>
31 #include <sys/param.h>
34 #include <sys/ptrace.h>
51 #define PROCSTAT_CORE_MAGIC 0x012DADB8
61 static struct psc_type_info {
64 } psc_type_info[PSC_TYPE_MAX] = {
65 { .n_type = NT_PROCSTAT_PROC, .structsize = sizeof(struct kinfo_proc) },
66 { .n_type = NT_PROCSTAT_FILES, .structsize = sizeof(struct kinfo_file) },
67 { .n_type = NT_PROCSTAT_VMMAP, .structsize = sizeof(struct kinfo_vmentry) },
68 { .n_type = NT_PROCSTAT_GROUPS, .structsize = sizeof(gid_t) },
69 { .n_type = NT_PROCSTAT_UMASK, .structsize = sizeof(u_short) },
70 { .n_type = NT_PROCSTAT_RLIMIT, .structsize = sizeof(struct rlimit) * RLIM_NLIMITS },
71 { .n_type = NT_PROCSTAT_OSREL, .structsize = sizeof(int) },
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_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
75 { .n_type = NT_PROCSTAT_AUXV, .structsize = sizeof(Elf_Auxinfo) },
76 { .n_type = NT_PTLWPINFO, .structsize = sizeof(struct ptrace_lwpinfo) },
79 static bool core_offset(struct procstat_core *core, off_t offset);
80 static bool core_read(struct procstat_core *core, void *buf, size_t len);
81 static ssize_t core_read_mem(struct procstat_core *core, void *buf,
82 size_t len, vm_offset_t addr, bool readall);
83 static void *get_args(struct procstat_core *core, vm_offset_t psstrings,
84 enum psc_type type, void *buf, size_t *lenp);
86 struct procstat_core *
87 procstat_core_open(const char *filename)
89 struct procstat_core *core;
96 if (elf_version(EV_CURRENT) == EV_NONE) {
97 warnx("ELF library too old");
100 fd = open(filename, O_RDONLY, 0);
102 warn("open(%s)", filename);
105 e = elf_begin(fd, ELF_C_READ, NULL);
107 warnx("elf_begin: %s", elf_errmsg(-1));
110 if (elf_kind(e) != ELF_K_ELF) {
111 warnx("%s is not an ELF object", filename);
114 if (gelf_getehdr(e, &ehdr) == NULL) {
115 warnx("gelf_getehdr: %s", elf_errmsg(-1));
118 if (ehdr.e_type != ET_CORE) {
119 warnx("%s is not a CORE file", filename);
122 if (elf_getphdrnum(e, &nph) == -1) {
123 warnx("program headers not found");
126 for (i = 0; i < ehdr.e_phnum; i++) {
127 if (gelf_getphdr(e, i, &phdr) != &phdr) {
128 warnx("gelf_getphdr: %s", elf_errmsg(-1));
131 if (phdr.p_type == PT_NOTE)
134 if (i == ehdr.e_phnum) {
135 warnx("NOTE program header not found");
138 core = malloc(sizeof(struct procstat_core));
140 warn("malloc(%zu)", sizeof(struct procstat_core));
143 core->pc_magic = PROCSTAT_CORE_MAGIC;
146 core->pc_ehdr = ehdr;
147 core->pc_phdr = phdr;
159 procstat_core_close(struct procstat_core *core)
162 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
164 elf_end(core->pc_elf);
170 procstat_core_get(struct procstat_core *core, enum psc_type type, void *buf,
174 off_t offset, eoffset;
175 vm_offset_t psstrings;
181 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
183 if (type >= PSC_TYPE_MAX) {
184 warnx("unknown core stat type: %d", type);
188 offset = core->pc_phdr.p_offset;
189 eoffset = offset + core->pc_phdr.p_filesz;
192 while (offset < eoffset) {
193 if (!core_offset(core, offset))
195 if (!core_read(core, &nhdr, sizeof(nhdr)))
198 offset += sizeof(nhdr) +
199 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
200 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
202 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
204 if (nhdr.n_type != psc_type_info[type].n_type)
206 if (nhdr.n_namesz != 8)
208 if (!core_read(core, nbuf, sizeof(nbuf)))
210 if (strcmp(nbuf, "FreeBSD") != 0)
212 if (nhdr.n_descsz < sizeof(cstructsize)) {
213 warnx("corrupted core file");
216 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
218 if (cstructsize != psc_type_info[type].structsize) {
219 warnx("version mismatch");
222 len = nhdr.n_descsz - sizeof(cstructsize);
226 len = MIN(len, *lenp);
229 freebuf = buf = malloc(len);
231 warn("malloc(%zu)", len);
235 if (!core_read(core, (char *)buf + curlen, len)) {
239 if (type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV) {
240 if (len < sizeof(psstrings)) {
244 psstrings = *(vm_offset_t *)buf;
250 buf = get_args(core, psstrings, type, buf, &len);
251 } else if (type == PSC_TYPE_PTLWPINFO) {
269 core_offset(struct procstat_core *core, off_t offset)
272 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
274 if (lseek(core->pc_fd, offset, SEEK_SET) == -1) {
275 warn("core: lseek(%jd)", (intmax_t)offset);
282 core_read(struct procstat_core *core, void *buf, size_t len)
286 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
288 n = read(core->pc_fd, buf, len);
293 if (n < (ssize_t)len) {
294 warnx("core: short read");
301 core_read_mem(struct procstat_core *core, void *buf, size_t len,
302 vm_offset_t addr, bool readall)
308 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
310 for (i = 0; i < core->pc_ehdr.e_phnum; i++) {
311 if (gelf_getphdr(core->pc_elf, i, &phdr) != &phdr) {
312 warnx("gelf_getphdr: %s", elf_errmsg(-1));
315 if (phdr.p_type != PT_LOAD)
317 if (addr < phdr.p_vaddr || addr > phdr.p_vaddr + phdr.p_memsz)
319 offset = phdr.p_offset + (addr - phdr.p_vaddr);
320 if ((phdr.p_vaddr + phdr.p_memsz) - addr < len) {
322 warnx("format error: "
323 "attempt to read out of segment");
326 len = (phdr.p_vaddr + phdr.p_memsz) - addr;
328 if (!core_offset(core, offset))
330 if (!core_read(core, buf, len))
334 warnx("format error: address %ju not found", (uintmax_t)addr);
338 #define ARGS_CHUNK_SZ 256 /* Chunk size (bytes) for get_args operations. */
341 get_args(struct procstat_core *core, vm_offset_t psstrings, enum psc_type type,
342 void *args, size_t *lenp)
344 struct ps_strings pss;
348 size_t chunksz, done, len, nchr, size;
352 assert(type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV);
354 if (core_read_mem(core, &pss, sizeof(pss), psstrings, true) == -1)
356 if (type == PSC_TYPE_ARGV) {
357 addr = (vm_offset_t)pss.ps_argvstr;
358 nstr = pss.ps_nargvstr;
359 } else /* type == PSC_TYPE_ENVV */ {
360 addr = (vm_offset_t)pss.ps_envstr;
361 nstr = pss.ps_nenvstr;
363 if (addr == 0 || nstr == 0)
365 if (nstr > ARG_MAX) {
366 warnx("format error");
369 size = nstr * sizeof(char *);
372 warn("malloc(%zu)", size);
377 if (core_read_mem(core, argv, size, addr, true) == -1)
380 nchr = MIN(ARG_MAX, *lenp);
383 freeargs = args = malloc(nchr);
385 warn("malloc(%zu)", nchr);
394 * The program may have scribbled into its argv array, e.g. to
395 * remove some arguments. If that has happened, break out
396 * before trying to read from NULL.
400 for (addr = (vm_offset_t)argv[i]; ; addr += chunksz) {
401 chunksz = MIN(ARGS_CHUNK_SZ, nchr - 1 - done);
404 n = core_read_mem(core, p, chunksz, addr, false);
407 len = strnlen(p, chunksz);
426 procstat_core_note_count(struct procstat_core *core, enum psc_type type)
429 off_t offset, eoffset;
434 if (type >= PSC_TYPE_MAX) {
435 warnx("unknown core stat type: %d", type);
439 offset = core->pc_phdr.p_offset;
440 eoffset = offset + core->pc_phdr.p_filesz;
442 for (n = 0; offset < eoffset; n++) {
443 if (!core_offset(core, offset))
445 if (!core_read(core, &nhdr, sizeof(nhdr)))
448 offset += sizeof(nhdr) +
449 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
450 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
452 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
454 if (nhdr.n_type != psc_type_info[type].n_type)
456 if (nhdr.n_namesz != 8)
458 if (!core_read(core, nbuf, sizeof(nbuf)))
460 if (strcmp(nbuf, "FreeBSD") != 0)
462 if (nhdr.n_descsz < sizeof(cstructsize)) {
463 warnx("corrupted core file");
466 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
468 if (cstructsize != psc_type_info[type].structsize) {
469 warnx("version mismatch");
472 if (nhdr.n_descsz - sizeof(cstructsize) == 0)