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