truss - Adapt to the new ELF naming.
[dragonfly.git] / usr.bin / truss / main.c
CommitLineData
984263bc
MD
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.
1de703da
MD
30 *
31 * $FreeBSD: src/usr.bin/truss/main.c,v 1.15.2.3 2002/05/16 23:41:23 peter Exp $
ccc9802b 32 * $DragonFly: src/usr.bin/truss/main.c,v 1.5 2005/05/28 00:22:04 swildner Exp $
984263bc
MD
33 */
34
984263bc
MD
35/*
36 * The main module for truss. Suprisingly simple, but, then, the other
37 * files handle the bulk of the work. And, of course, the kernel has to
38 * do a lot of the work :).
39 */
40
41#include <sys/param.h>
42#include <sys/ioctl.h>
43#include <sys/pioctl.h>
3488a1f5
EN
44#include <sys/ucred.h>
45#include <sys/mount.h>
984263bc
MD
46
47#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <signal.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56#include "extern.h"
57
58/*
59 * These should really be parameterized -- I don't like having globals,
60 * but this is the easiest way, right now, to deal with them.
61 */
62
63int pid = 0;
64int nosigs = 0;
65FILE *outfile;
66int Procfd;
67
68static inline void
69usage(void)
70{
71 fprintf(stderr, "%s\n%s\n",
72 "usage: truss [-S] [-o file] -p pid",
73 " truss [-S] [-o file] command [args]");
74 exit(1);
75}
76
77/*
78 * WARNING! "FreeBSD a.out" must be first, or set_etype will not
79 * work correctly.
80 */
81struct ex_types {
82 const char *type;
83 void (*enter_syscall)(int, int);
84 void (*exit_syscall)(int, int);
85} ex_types[] = {
984263bc
MD
86#ifdef __i386__
87 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
bd8e2ea2
AHJ
88 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
89 { "Linux ELF32", i386_linux_syscall_entry, i386_linux_syscall_exit },
984263bc
MD
90#endif
91 { 0, 0, 0 },
92};
93
94/*
95 * Set the execution type. This is called after every exec, and when
96 * a process is first monitored. The procfs pseudo-file "etype" has
97 * the execution module type -- see /proc/curproc/etype for an example.
98 */
99
100static struct ex_types *
101set_etype(void) {
102 struct ex_types *funcs;
3488a1f5 103 char *etype;
984263bc
MD
104 char progt[32];
105 int fd;
106
3488a1f5
EN
107 asprintf(&etype, "%s/%d/etype", procfs_path, pid);
108 if (etype == NULL)
109 err(1, "Out of memory");
984263bc
MD
110 if ((fd = open(etype, O_RDONLY)) == -1) {
111 strcpy(progt, "FreeBSD a.out");
112 } else {
113 int len = read(fd, progt, sizeof(progt));
114 progt[len-1] = '\0';
115 close(fd);
116 }
3488a1f5 117 free(etype);
984263bc
MD
118
119 for (funcs = ex_types; funcs->type; funcs++)
120 if (!strcmp(funcs->type, progt))
121 break;
122
123 if (funcs->type == NULL) {
124 funcs = &ex_types[0];
125 warn("Execution type %s is not supported -- using %s\n",
126 progt, funcs->type);
127 }
128 return funcs;
129}
130
131int
132main(int ac, char **av) {
3488a1f5 133 int c, i, mntsize;
984263bc
MD
134 char **command;
135 struct procfs_status pfs;
136 struct ex_types *funcs;
3488a1f5 137 struct statfs *mntbuf;
984263bc
MD
138 int in_exec = 0;
139 char *fname = NULL;
140 int sigexit = 0;
141
3488a1f5
EN
142 /* Check where procfs is mounted if it is mounted */
143 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
144 err(1, "getmntinfo");
145 for (i = 0; i < mntsize; i++) {
146 if (strcasecmp(mntbuf[i].f_mntfromname, "procfs") == 0) {
147 strlcpy(procfs_path, mntbuf[i].f_mntonname, sizeof(procfs_path));
148 have_procfs = 1;
149 break;
150 }
151 }
152 if (!have_procfs) {
153 errno = 2;
154 err(1, "You must have a mounted procfs to use truss");
155 }
156
984263bc
MD
157 outfile = stderr;
158 while ((c = getopt(ac, av, "p:o:S")) != -1) {
159 switch (c) {
160 case 'p': /* specified pid */
161 pid = atoi(optarg);
042ceb8d
HP
162 if (pid == getpid()) {
163 /* make sure truss doesn't trace itself */
164 fprintf(stderr, "truss: attempt to self trace: %d\n", pid);
165 exit(2);
166 }
984263bc
MD
167 break;
168 case 'o': /* Specified output file */
169 fname = optarg;
170 break;
171 case 'S': /* Don't trace signals */
172 nosigs = 1;
173 break;
174 default:
175 usage();
176 }
177 }
178
179 ac -= optind; av += optind;
180 if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0))
181 usage();
182
183 if (fname != NULL) { /* Use output file */
184 if ((outfile = fopen(fname, "w")) == NULL)
185 errx(1, "cannot open %s", fname);
186 }
187
188 /*
189 * If truss starts the process itself, it will ignore some signals --
190 * they should be passed off to the process, which may or may not
191 * exit. If, however, we are examining an already-running process,
192 * then we restore the event mask on these same signals.
193 */
194
195 if (pid == 0) { /* Start a command ourselves */
196 command = av;
197 pid = setup_and_wait(command);
198 signal(SIGINT, SIG_IGN);
199 signal(SIGTERM, SIG_IGN);
200 signal(SIGQUIT, SIG_IGN);
201 } else {
202 signal(SIGINT, restore_proc);
203 signal(SIGTERM, restore_proc);
204 signal(SIGQUIT, restore_proc);
205 }
206
207
208 /*
209 * At this point, if we started the process, it is stopped waiting to
210 * be woken up, either in exit() or in execve().
211 */
212
213 Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
214 (nosigs ? 0 : S_SIG));
215 if (Procfd == -1)
216 return 0;
217
218 pfs.why = 0;
219
220 funcs = set_etype();
221 /*
222 * At this point, it's a simple loop, waiting for the process to
223 * stop, finding out why, printing out why, and then continuing it.
224 * All of the grunt work is done in the support routines.
225 */
226
227 do {
228 int val = 0;
229
230 if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
231 warn("PIOCWAIT top of loop");
232 else {
233 switch(i = pfs.why) {
234 case S_SCE:
235 funcs->enter_syscall(pid, pfs.val);
236 break;
237 case S_SCX:
238 /*
239 * This is so we don't get two messages for an exec -- one
240 * for the S_EXEC, and one for the syscall exit. It also,
241 * conveniently, ensures that the first message printed out
242 * isn't the return-from-syscall used to create the process.
243 */
244
245 if (in_exec) {
246 in_exec = 0;
247 break;
248 }
249 funcs->exit_syscall(pid, pfs.val);
250 break;
251 case S_SIG:
252 fprintf(outfile, "SIGNAL %lu\n", pfs.val);
253 sigexit = pfs.val;
254 break;
255 case S_EXIT:
256 fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
257 break;
258 case S_EXEC:
259 funcs = set_etype();
260 in_exec = 1;
261 break;
262 default:
263 fprintf (outfile, "Process stopped because of: %d\n", i);
264 break;
265 }
266 }
267 if (ioctl(Procfd, PIOCCONT, val) == -1) {
268 if (kill(pid, 0) == -1 && errno == ESRCH)
269 break;
270 else
271 warn("PIOCCONT");
272 }
273 } while (pfs.why != S_EXIT);
274 fflush(outfile);
275 if (sigexit) {
276 if (sigexit == SIGQUIT)
277 exit(sigexit);
278 (void) signal(sigexit, SIG_DFL);
279 (void) kill(getpid(), sigexit);
280 }
281 return 0;
282}