2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
46 #include <libprocstat.h>
50 extern char * const *environ;
52 static int getelfclass(int);
53 static int proc_init(pid_t, int, int, struct proc_handle **);
64 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
66 if (gelf_getehdr(e, &ehdr) == NULL)
68 class = ehdr.e_ident[EI_CLASS];
75 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
77 struct kinfo_proc *kp;
78 struct proc_handle *phdl;
79 int error, class, count, fd;
82 if ((phdl = malloc(sizeof(*phdl))) == NULL)
85 memset(phdl, 0, sizeof(*phdl));
86 phdl->public.pid = pid;
88 phdl->status = status;
89 phdl->procstat = procstat_open_sysctl();
90 if (phdl->procstat == NULL)
93 /* Obtain a path to the executable. */
94 if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
97 error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
98 sizeof(phdl->execpath));
99 procstat_freeprocs(phdl->procstat, kp);
103 /* Use it to determine the data model for the process. */
104 if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
108 class = getelfclass(fd);
111 phdl->model = PR_MODEL_LP64;
114 phdl->model = PR_MODEL_ILP32;
129 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
131 struct proc_handle *phdl;
134 if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
136 if (elf_version(EV_CURRENT) == EV_NONE)
140 * Allocate memory for the process handle, a structure containing
141 * all things related to the process.
143 error = proc_init(pid, flags, PS_RUN, &phdl);
147 if ((flags & PATTACH_RDONLY) == 0) {
148 if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
150 DPRINTF("ERROR: cannot ptrace child process %d", pid);
154 /* Wait for the child process to stop. */
155 if (waitpid(pid, &status, WUNTRACED) == -1) {
157 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
161 /* Check for an unexpected status. */
162 if (!WIFSTOPPED(status))
163 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
165 phdl->status = PS_STOP;
167 if ((flags & PATTACH_NOSTOP) != 0)
172 if (error != 0 && phdl != NULL) {
181 proc_create(const char *file, char * const *argv, char * const *envp,
182 proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl)
184 struct proc_handle *phdl;
188 if (elf_version(EV_CURRENT) == EV_NONE)
194 if ((pid = fork()) == -1)
197 /* The child expects to be traced. */
198 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
212 /* Wait for the child process to stop. */
213 if (waitpid(pid, &status, WUNTRACED) == -1) {
215 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
219 /* Check for an unexpected status. */
220 if (!WIFSTOPPED(status)) {
222 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
226 /* The parent owns the process handle. */
227 error = proc_init(pid, 0, PS_IDLE, &phdl);
229 phdl->status = PS_STOP;
232 if (error != 0 && phdl != NULL) {
242 proc_free(struct proc_handle *phdl)
244 struct file_info *file;
247 for (i = 0; i < phdl->nmappings; i++) {
248 file = phdl->mappings[i].file;
249 if (file != NULL && --file->refs == 0) {
250 if (file->elf != NULL) {
251 (void)elf_end(file->elf);
252 (void)close(file->fd);
253 if (file->symtab.nsyms > 0)
254 free(file->symtab.index);
255 if (file->dynsymtab.nsyms > 0)
256 free(file->dynsymtab.index);
261 if (phdl->maparrsz > 0)
262 free(phdl->mappings);
263 if (phdl->procstat != NULL)
264 procstat_close(phdl->procstat);
265 if (phdl->rdap != NULL)
266 rd_delete(phdl->rdap);