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