2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
44 #include <libprocstat.h>
48 extern char * const *environ;
50 static int getelfclass(int);
51 static int proc_init(pid_t, int, int, struct proc_handle **);
62 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
64 if (gelf_getehdr(e, &ehdr) == NULL)
66 class = ehdr.e_ident[EI_CLASS];
73 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
75 struct kinfo_proc *kp;
76 struct proc_handle *phdl;
77 int error, class, count, fd;
80 if ((phdl = malloc(sizeof(*phdl))) == NULL)
83 memset(phdl, 0, sizeof(*phdl));
84 phdl->public.pid = pid;
86 phdl->status = status;
87 phdl->procstat = procstat_open_sysctl();
88 if (phdl->procstat == NULL)
91 /* Obtain a path to the executable. */
92 if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
95 error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
96 sizeof(phdl->execpath));
97 procstat_freeprocs(phdl->procstat, kp);
101 /* Use it to determine the data model for the process. */
102 if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
106 class = getelfclass(fd);
109 phdl->model = PR_MODEL_LP64;
112 phdl->model = PR_MODEL_ILP32;
127 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
129 struct proc_handle *phdl;
132 if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
134 if (elf_version(EV_CURRENT) == EV_NONE)
138 * Allocate memory for the process handle, a structure containing
139 * all things related to the process.
141 error = proc_init(pid, flags, PS_RUN, &phdl);
145 if ((flags & PATTACH_RDONLY) == 0) {
146 if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
148 DPRINTF("ERROR: cannot ptrace child process %d", pid);
152 /* Wait for the child process to stop. */
153 if (waitpid(pid, &status, WUNTRACED) == -1) {
155 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
159 /* Check for an unexpected status. */
160 if (!WIFSTOPPED(status))
161 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
163 phdl->status = PS_STOP;
165 if ((flags & PATTACH_NOSTOP) != 0)
170 if (error != 0 && phdl != NULL) {
179 proc_create(const char *file, char * const *argv, char * const *envp,
180 proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl)
182 struct proc_handle *phdl;
186 if (elf_version(EV_CURRENT) == EV_NONE)
192 if ((pid = fork()) == -1)
195 /* The child expects to be traced. */
196 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
210 /* Wait for the child process to stop. */
211 if (waitpid(pid, &status, WUNTRACED) == -1) {
213 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
217 /* Check for an unexpected status. */
218 if (!WIFSTOPPED(status)) {
220 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
224 /* The parent owns the process handle. */
225 error = proc_init(pid, 0, PS_IDLE, &phdl);
227 phdl->status = PS_STOP;
230 if (error != 0 && phdl != NULL) {
240 proc_free(struct proc_handle *phdl)
242 struct file_info *file;
245 for (i = 0; i < phdl->nmappings; i++) {
246 file = phdl->mappings[i].file;
247 if (file != NULL && --file->refs == 0) {
248 if (file->elf != NULL) {
249 (void)elf_end(file->elf);
250 (void)close(file->fd);
251 if (file->symtab.nsyms > 0)
252 free(file->symtab.index);
253 if (file->dynsymtab.nsyms > 0)
254 free(file->dynsymtab.index);
259 if (phdl->maparrsz > 0)
260 free(phdl->mappings);
261 if (phdl->procstat != NULL)
262 procstat_close(phdl->procstat);
263 if (phdl->rdap != NULL)
264 rd_delete(phdl->rdap);