xref: /freebsd/usr.bin/truss/setup.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copryight 1997 Sean Eric Fagan
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *	This product includes software developed by Sean Eric Fagan
15  * 4. Neither the name of the author may be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /*
36  * Various setup functions for truss.  Not the cleanest-written code,
37  * I'm afraid.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/ptrace.h>
43 #include <sys/wait.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 #include <machine/reg.h>
56 
57 #include "truss.h"
58 #include "extern.h"
59 
60 static int child_pid;
61 
62 /*
63  * setup_and_wait() is called to start a process.  All it really does
64  * is fork(), set itself up to stop on exec or exit, and then exec
65  * the given command.  At that point, the child process stops, and
66  * the parent can wake up and deal with it.
67  */
68 
69 int
70 setup_and_wait(char *command[])
71 {
72 	int pid;
73 	int waitval;
74 
75 	pid = vfork();
76 	if (pid == -1) {
77 		err(1, "fork failed");
78 	}
79 	if (pid == 0) {	/* Child */
80 		ptrace(PT_TRACE_ME, 0, 0, 0);
81 		setpgid (0, 0);
82 		execvp(command[0], command);
83 		err(1, "execvp %s", command[0]);
84 	}
85 
86 	/* Only in the parent here */
87 	if (waitpid(pid, &waitval, 0) < -1) {
88 		err(1, "unexpect stop in waitpid");
89 		return 0;
90 	}
91 
92 	child_pid = pid;
93 
94 	return (pid);
95 }
96 
97 /*
98  * start_tracing picks up where setup_and_wait() dropped off -- namely,
99  * it sets the event mask for the given process id.  Called for both
100  * monitoring an existing process and when we create our own.
101  */
102 
103 int
104 start_tracing(int pid)
105 {
106 	int waitval;
107 	int ret;
108 	int retry = 10;
109 
110 	do {
111 		ret = ptrace(PT_ATTACH, pid, NULL, 0);
112 		usleep(200);
113 	} while(ret && retry-- > 0);
114 	if (ret)
115 		err(1, "can not attach to target process");
116 
117 	child_pid = pid;
118 	if (waitpid(pid, &waitval, 0) < -1)
119 		err(1, "Unexpect stop in waitpid");
120 
121 	return (0);
122 }
123 
124 /*
125  * Restore a process back to it's pre-truss state.
126  * Called for SIGINT, SIGTERM, SIGQUIT.  This only
127  * applies if truss was told to monitor an already-existing
128  * process.
129  */
130 void
131 restore_proc(int signo __unused)
132 {
133 	int waitval;
134 
135 	/* stop the child so that we can detach */
136 	kill(child_pid, SIGSTOP);
137 	if (waitpid(child_pid, &waitval, 0) < -1)
138 		err(1, "Unexpected stop in waitpid");
139 
140 	if (ptrace(PT_DETACH, child_pid, (caddr_t)1, 0) < 0)
141 		err(1, "Can not detach the process");
142 
143 	kill(child_pid, SIGCONT);
144 	exit(0);
145 }
146 
147 /*
148  * Change curthread member based on lwpid.
149  * If it is a new thread, create a threadinfo structure
150  */
151 static void
152 find_thread(struct trussinfo *info, lwpid_t lwpid)
153 {
154 	info->curthread = NULL;
155 	struct threadinfo *np;
156 	SLIST_FOREACH(np, &info->threadlist, entries) {
157 	if (np->tid == lwpid) {
158 		info->curthread = np;
159 		return;
160 		}
161 	}
162 
163 	np = (struct threadinfo *)malloc(sizeof(struct threadinfo));
164 	if (np == NULL)
165 		errx(1, "malloc() failed");
166 	np->tid = lwpid;
167 	np->in_fork = 0;
168 	np->in_syscall = 0;
169 	SLIST_INSERT_HEAD(&info->threadlist, np, entries);
170 	info->curthread = np;
171 }
172 
173 /*
174  * Start the traced process and wait until it stoped.
175  * Fill trussinfo structure.
176  * When this even returns, the traced process is in stop state.
177  */
178 void
179 waitevent(struct trussinfo *info)
180 {
181 	int waitval;
182 	static int pending_signal = 0;
183 
184 	ptrace(PT_SYSCALL, info->pid, (caddr_t)1, pending_signal);
185 	pending_signal = 0;
186 
187 	if (waitpid(info->pid, &waitval, 0) < -1) {
188 		err(1, "Unexpected stop in waitpid");
189 	}
190 
191 	if (WIFCONTINUED(waitval)) {
192 		info->pr_why = S_NONE;
193 		return;
194 	}
195 	if (WIFEXITED(waitval)) {
196 		info->pr_why = S_EXIT;
197 		info->pr_data = WEXITSTATUS(waitval);
198 		return;
199 	}
200 	if (WIFSTOPPED(waitval)) {
201 		struct ptrace_lwpinfo lwpinfo;
202 		ptrace(PT_LWPINFO, info->pid, (caddr_t)&lwpinfo, sizeof(lwpinfo));
203 		find_thread(info, lwpinfo.pl_lwpid);
204 		switch(WSTOPSIG(waitval)) {
205 		case SIGTRAP:
206 			info->pr_why = info->curthread->in_syscall?S_SCX:S_SCE;
207 			info->curthread->in_syscall = 1 - info->curthread->in_syscall;
208 			break;
209 		default:
210 			info->pr_why = S_SIG;
211 			info->pr_data = WSTOPSIG(waitval);
212 			pending_signal = info->pr_data;
213 			break;
214 		}
215 	}
216 	if (WIFSIGNALED(waitval)) {
217 	        info->pr_why = S_EXIT;
218 		info->pr_why = 0;
219                 return;
220 	}
221 }
222