2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/ptrace.h>
53 #define PROCSTAT_CORE_MAGIC 0x012DADB8
63 static struct psc_type_info {
66 } psc_type_info[PSC_TYPE_MAX] = {
67 { .n_type = NT_PROCSTAT_PROC, .structsize = sizeof(struct kinfo_proc) },
68 { .n_type = NT_PROCSTAT_FILES, .structsize = sizeof(struct kinfo_file) },
69 { .n_type = NT_PROCSTAT_VMMAP, .structsize = sizeof(struct kinfo_vmentry) },
70 { .n_type = NT_PROCSTAT_GROUPS, .structsize = sizeof(gid_t) },
71 { .n_type = NT_PROCSTAT_UMASK, .structsize = sizeof(u_short) },
72 { .n_type = NT_PROCSTAT_RLIMIT, .structsize = sizeof(struct rlimit) * RLIM_NLIMITS },
73 { .n_type = NT_PROCSTAT_OSREL, .structsize = sizeof(int) },
74 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
75 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
76 { .n_type = NT_PROCSTAT_PSSTRINGS, .structsize = sizeof(vm_offset_t) },
77 { .n_type = NT_PROCSTAT_AUXV, .structsize = sizeof(Elf_Auxinfo) },
78 { .n_type = NT_PTLWPINFO, .structsize = sizeof(struct ptrace_lwpinfo) },
81 static bool core_offset(struct procstat_core *core, off_t offset);
82 static bool core_read(struct procstat_core *core, void *buf, size_t len);
83 static ssize_t core_read_mem(struct procstat_core *core, void *buf,
84 size_t len, vm_offset_t addr, bool readall);
85 static void *get_args(struct procstat_core *core, vm_offset_t psstrings,
86 enum psc_type type, void *buf, size_t *lenp);
88 struct procstat_core *
89 procstat_core_open(const char *filename)
91 struct procstat_core *core;
98 if (elf_version(EV_CURRENT) == EV_NONE) {
99 warnx("ELF library too old");
102 fd = open(filename, O_RDONLY, 0);
104 warn("open(%s)", filename);
107 e = elf_begin(fd, ELF_C_READ, NULL);
109 warnx("elf_begin: %s", elf_errmsg(-1));
112 if (elf_kind(e) != ELF_K_ELF) {
113 warnx("%s is not an ELF object", filename);
116 if (gelf_getehdr(e, &ehdr) == NULL) {
117 warnx("gelf_getehdr: %s", elf_errmsg(-1));
120 if (ehdr.e_type != ET_CORE) {
121 warnx("%s is not a CORE file", filename);
124 if (elf_getphnum(e, &nph) == 0) {
125 warnx("program headers not found");
128 for (i = 0; i < ehdr.e_phnum; i++) {
129 if (gelf_getphdr(e, i, &phdr) != &phdr) {
130 warnx("gelf_getphdr: %s", elf_errmsg(-1));
133 if (phdr.p_type == PT_NOTE)
136 if (i == ehdr.e_phnum) {
137 warnx("NOTE program header not found");
140 core = malloc(sizeof(struct procstat_core));
142 warn("malloc(%zu)", sizeof(struct procstat_core));
145 core->pc_magic = PROCSTAT_CORE_MAGIC;
148 core->pc_ehdr = ehdr;
149 core->pc_phdr = phdr;
161 procstat_core_close(struct procstat_core *core)
164 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
166 elf_end(core->pc_elf);
172 procstat_core_get(struct procstat_core *core, enum psc_type type, void *buf,
176 off_t offset, eoffset;
177 vm_offset_t psstrings;
183 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
185 if (type >= PSC_TYPE_MAX) {
186 warnx("unknown core stat type: %d", type);
190 offset = core->pc_phdr.p_offset;
191 eoffset = offset + core->pc_phdr.p_filesz;
194 while (offset < eoffset) {
195 if (!core_offset(core, offset))
197 if (!core_read(core, &nhdr, sizeof(nhdr)))
200 offset += sizeof(nhdr) +
201 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
202 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
204 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
206 if (nhdr.n_type != psc_type_info[type].n_type)
208 if (nhdr.n_namesz != 8)
210 if (!core_read(core, nbuf, sizeof(nbuf)))
212 if (strcmp(nbuf, "FreeBSD") != 0)
214 if (nhdr.n_descsz < sizeof(cstructsize)) {
215 warnx("corrupted core file");
218 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
220 if (cstructsize != psc_type_info[type].structsize) {
221 warnx("version mismatch");
224 len = nhdr.n_descsz - sizeof(cstructsize);
228 len = MIN(len, *lenp);
231 freebuf = buf = malloc(len);
233 warn("malloc(%zu)", len);
237 if (!core_read(core, (char *)buf + curlen, len)) {
241 if (type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV) {
242 if (len < sizeof(psstrings)) {
246 psstrings = *(vm_offset_t *)buf;
252 buf = get_args(core, psstrings, type, buf, &len);
253 } else if (type == PSC_TYPE_PTLWPINFO) {
271 core_offset(struct procstat_core *core, off_t offset)
274 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
276 if (lseek(core->pc_fd, offset, SEEK_SET) == -1) {
277 warn("core: lseek(%jd)", (intmax_t)offset);
284 core_read(struct procstat_core *core, void *buf, size_t len)
288 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
290 n = read(core->pc_fd, buf, len);
295 if (n < (ssize_t)len) {
296 warnx("core: short read");
303 core_read_mem(struct procstat_core *core, void *buf, size_t len,
304 vm_offset_t addr, bool readall)
310 assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
312 for (i = 0; i < core->pc_ehdr.e_phnum; i++) {
313 if (gelf_getphdr(core->pc_elf, i, &phdr) != &phdr) {
314 warnx("gelf_getphdr: %s", elf_errmsg(-1));
317 if (phdr.p_type != PT_LOAD)
319 if (addr < phdr.p_vaddr || addr > phdr.p_vaddr + phdr.p_memsz)
321 offset = phdr.p_offset + (addr - phdr.p_vaddr);
322 if ((phdr.p_vaddr + phdr.p_memsz) - addr < len) {
324 warnx("format error: "
325 "attempt to read out of segment");
328 len = (phdr.p_vaddr + phdr.p_memsz) - addr;
330 if (!core_offset(core, offset))
332 if (!core_read(core, buf, len))
336 warnx("format error: address %ju not found", (uintmax_t)addr);
340 #define ARGS_CHUNK_SZ 256 /* Chunk size (bytes) for get_args operations. */
343 get_args(struct procstat_core *core, vm_offset_t psstrings, enum psc_type type,
344 void *args, size_t *lenp)
346 struct ps_strings pss;
350 size_t chunksz, done, len, nchr, size;
354 assert(type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV);
356 if (core_read_mem(core, &pss, sizeof(pss), psstrings, true) == -1)
358 if (type == PSC_TYPE_ARGV) {
359 addr = (vm_offset_t)pss.ps_argvstr;
360 nstr = pss.ps_nargvstr;
361 } else /* type == PSC_TYPE_ENVV */ {
362 addr = (vm_offset_t)pss.ps_envstr;
363 nstr = pss.ps_nenvstr;
365 if (addr == 0 || nstr == 0)
367 if (nstr > ARG_MAX) {
368 warnx("format error");
371 size = nstr * sizeof(char *);
374 warn("malloc(%zu)", size);
379 if (core_read_mem(core, argv, size, addr, true) == -1)
382 nchr = MIN(ARG_MAX, *lenp);
385 freeargs = args = malloc(nchr);
387 warn("malloc(%zu)", nchr);
396 * The program may have scribbled into its argv array, e.g. to
397 * remove some arguments. If that has happened, break out
398 * before trying to read from NULL.
402 for (addr = (vm_offset_t)argv[i]; ; addr += chunksz) {
403 chunksz = MIN(ARGS_CHUNK_SZ, nchr - 1 - done);
406 n = core_read_mem(core, p, chunksz, addr, false);
409 len = strnlen(p, chunksz);
428 procstat_core_note_count(struct procstat_core *core, enum psc_type type)
431 off_t offset, eoffset;
436 if (type >= PSC_TYPE_MAX) {
437 warnx("unknown core stat type: %d", type);
441 offset = core->pc_phdr.p_offset;
442 eoffset = offset + core->pc_phdr.p_filesz;
444 for (n = 0; offset < eoffset; n++) {
445 if (!core_offset(core, offset))
447 if (!core_read(core, &nhdr, sizeof(nhdr)))
450 offset += sizeof(nhdr) +
451 roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
452 roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
454 if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
456 if (nhdr.n_type != psc_type_info[type].n_type)
458 if (nhdr.n_namesz != 8)
460 if (!core_read(core, nbuf, sizeof(nbuf)))
462 if (strcmp(nbuf, "FreeBSD") != 0)
464 if (nhdr.n_descsz < sizeof(cstructsize)) {
465 warnx("corrupted core file");
468 if (!core_read(core, &cstructsize, sizeof(cstructsize)))
470 if (cstructsize != psc_type_info[type].structsize) {
471 warnx("version mismatch");
474 if (nhdr.n_descsz - sizeof(cstructsize) == 0)