2 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
44 #include <libprocstat.h>
48 static int getelfclass(int);
49 static int proc_init(pid_t, int, int, struct proc_handle **);
60 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
62 if (gelf_getehdr(e, &ehdr) == NULL)
64 class = ehdr.e_ident[EI_CLASS];
71 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
73 struct kinfo_proc *kp;
74 struct proc_handle *phdl;
75 int error, class, count, fd;
78 if ((phdl = malloc(sizeof(*phdl))) == NULL)
81 memset(phdl, 0, sizeof(*phdl));
82 phdl->public.pid = pid;
84 phdl->status = status;
85 phdl->procstat = procstat_open_sysctl();
86 if (phdl->procstat == NULL)
89 /* Obtain a path to the executable. */
90 if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
93 error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
94 sizeof(phdl->execpath));
95 procstat_freeprocs(phdl->procstat, kp);
99 /* Use it to determine the data model for the process. */
100 if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
104 class = getelfclass(fd);
107 phdl->model = PR_MODEL_LP64;
110 phdl->model = PR_MODEL_ILP32;
125 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
127 struct proc_handle *phdl;
130 if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
132 if (elf_version(EV_CURRENT) == EV_NONE)
136 * Allocate memory for the process handle, a structure containing
137 * all things related to the process.
139 error = proc_init(pid, flags, PS_RUN, &phdl);
143 if ((flags & PATTACH_RDONLY) == 0) {
144 if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
146 DPRINTF("ERROR: cannot ptrace child process %d", pid);
150 /* Wait for the child process to stop. */
151 if (waitpid(pid, &status, WUNTRACED) == -1) {
153 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
157 /* Check for an unexpected status. */
158 if (!WIFSTOPPED(status))
159 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
161 phdl->status = PS_STOP;
163 if ((flags & PATTACH_NOSTOP) != 0)
168 if (error != 0 && phdl != NULL) {
177 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
178 void *child_arg, struct proc_handle **pphdl)
180 struct proc_handle *phdl;
184 if (elf_version(EV_CURRENT) == EV_NONE)
190 /* Fork a new process. */
191 if ((pid = vfork()) == -1)
194 /* The child expects to be traced. */
195 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
201 /* Execute the specified file: */
204 /* Couldn't execute the file. */
208 /* The parent owns the process handle. */
209 error = proc_init(pid, 0, PS_IDLE, &phdl);
213 /* Wait for the child process to stop. */
214 if (waitpid(pid, &status, WUNTRACED) == -1) {
216 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
220 /* Check for an unexpected status. */
221 if (!WIFSTOPPED(status)) {
223 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
226 phdl->status = PS_STOP;
229 if (error != 0 && phdl != NULL) {
239 proc_free(struct proc_handle *phdl)
241 struct file_info *file;
244 for (i = 0; i < phdl->nmappings; i++) {
245 file = phdl->mappings[i].file;
246 if (file != NULL && --file->refs == 0) {
247 if (file->elf != NULL) {
248 (void)elf_end(file->elf);
249 (void)close(file->fd);
250 if (file->symtab.nsyms > 0)
251 free(file->symtab.index);
252 if (file->dynsymtab.nsyms > 0)
253 free(file->dynsymtab.index);
258 if (phdl->maparrsz > 0)
259 free(phdl->mappings);
260 if (phdl->procstat != NULL)
261 procstat_close(phdl->procstat);
262 if (phdl->rdap != NULL)
263 rd_delete(phdl->rdap);