2 * Copryight 1997 Sean Eric Fagan
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
31 * $FreeBSD: src/usr.bin/truss/main.c,v 1.15.2.3 2002/05/16 23:41:23 peter Exp $
35 * The main module for truss. Suprisingly simple, but, then, the other
36 * files handle the bulk of the work. And, of course, the kernel has to
37 * do a lot of the work :).
40 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/pioctl.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
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.
68 fprintf(stderr, "%s\n%s\n",
69 "usage: truss [-S] [-o file] -p pid",
70 " truss [-S] [-o file] command [args]");
76 void (*enter_syscall)(struct trussinfo *, int);
77 int (*exit_syscall)(struct trussinfo *, int);
80 { "DragonFly ELF64", x86_64_syscall_entry, x86_64_syscall_exit },
81 { "FreeBSD ELF64", x86_64_syscall_entry, x86_64_syscall_exit },
87 * Set the execution type. This is called after every exec, and when
88 * a process is first monitored. The procfs pseudo-file "etype" has
89 * the execution module type -- see /proc/curproc/etype for an example.
92 static struct ex_types *
93 set_etype(struct trussinfo *trussinfo) {
94 struct ex_types *funcs;
99 asprintf(&etype, "%s/%d/etype", procfs_path, trussinfo->pid);
101 err(1, "Out of memory");
102 if ((fd = open(etype, O_RDONLY)) == -1) {
103 strcpy(progt, "FreeBSD a.out");
105 int len = read(fd, progt, sizeof(progt));
111 for (funcs = ex_types; funcs->type; funcs++)
112 if (!strcmp(funcs->type, progt))
115 if (funcs->type == NULL) {
116 funcs = &ex_types[0];
117 warn("Execution type %s is not supported -- using %s\n",
124 main(int ac, char **av) {
127 struct procfs_status pfs;
128 struct ex_types *funcs;
129 struct statfs *mntbuf;
133 struct trussinfo *trussinfo;
135 /* Initialize the trussinfo struct */
136 trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
137 if (trussinfo == NULL)
138 errx(1, "malloc() failed");
139 bzero(trussinfo, sizeof(struct trussinfo));
140 trussinfo->outfile = stderr;
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));
154 err(1, "You must have a mounted procfs to use truss");
157 while ((c = getopt(ac, av, "p:o:S")) != -1) {
159 case 'p': /* specified pid */
160 trussinfo->pid = atoi(optarg);
161 if (trussinfo->pid == getpid()) {
162 /* make sure truss doesn't trace itself */
163 fprintf(stderr, "truss: attempt to self trace: %d\n", trussinfo->pid);
167 case 'o': /* Specified output file */
170 case 'S': /* Don't trace signals */
171 trussinfo->flags |= NOSIGS;
178 ac -= optind; av += optind;
179 if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
182 if (fname != NULL) { /* Use output file */
183 if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
184 errx(1, "cannot open %s", fname);
188 * If truss starts the process itself, it will ignore some signals --
189 * they should be passed off to the process, which may or may not
190 * exit. If, however, we are examining an already-running process,
191 * then we restore the event mask on these same signals.
194 if (trussinfo->pid == 0) { /* Start a command ourselves */
196 trussinfo->pid = setup_and_wait(command);
197 signal(SIGINT, SIG_IGN);
198 signal(SIGTERM, SIG_IGN);
199 signal(SIGQUIT, SIG_IGN);
201 signal(SIGINT, restore_proc);
202 signal(SIGTERM, restore_proc);
203 signal(SIGQUIT, restore_proc);
208 * At this point, if we started the process, it is stopped waiting to
209 * be woken up, either in exit() or in execve().
212 Procfd = start_tracing(
213 trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
214 ((trussinfo->flags & NOSIGS) ? 0 : S_SIG));
220 funcs = set_etype(trussinfo);
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.
230 if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
231 warn("PIOCWAIT top of loop");
233 switch(i = pfs.why) {
235 funcs->enter_syscall(trussinfo, pfs.val);
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.
249 funcs->exit_syscall(trussinfo, pfs.val);
252 fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
256 fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
259 funcs = set_etype(trussinfo);
263 fprintf (trussinfo->outfile, "Process stopped because of: %d\n", i);
267 if (ioctl(Procfd, PIOCCONT, val) == -1) {
268 if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
273 } while (pfs.why != S_EXIT);
274 fflush(trussinfo->outfile);
276 if (sigexit == SIGQUIT)
278 (void) signal(sigexit, SIG_DFL);
279 (void) kill(getpid(), sigexit);