]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/truss/main.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / truss / main.c
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  * 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/types.h>
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <sys/sysctl.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 #include "truss.h"
59 #include "extern.h"
60
61 #define MAXARGS 6
62
63 static void
64 usage(void)
65 {
66         fprintf(stderr, "%s\n%s\n",
67             "usage: truss [-faedDS] [-o file] [-s strsize] -p pid",
68             "       truss [-faedDS] [-o file] [-s strsize] command [args]");
69         exit(1);
70 }
71
72 /*
73  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
74  * work correctly.
75  */
76 struct ex_types {
77         const char *type;
78         void (*enter_syscall)(struct trussinfo *, int);
79         long (*exit_syscall)(struct trussinfo *, int);
80 } ex_types[] = {
81 #ifdef __amd64__
82         { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
83         { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
84         { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
85 #endif
86 #ifdef __i386__
87         { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
88         { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
89         { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
90         { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
91 #endif
92 #ifdef __ia64__
93         { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
94 #endif
95 #ifdef __powerpc__
96         { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
97         { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
98 #endif
99 #ifdef __sparc64__
100         { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
101 #endif
102         { 0, 0, 0 },
103 };
104
105 /*
106  * Set the execution type.  This is called after every exec, and when
107  * a process is first monitored. 
108  */
109
110 static struct ex_types *
111 set_etype(struct trussinfo *trussinfo)
112 {
113         struct ex_types *funcs;
114         char progt[32];
115         
116         size_t len = sizeof(progt);
117         int mib[4];
118         int error;
119
120         mib[0] = CTL_KERN;
121         mib[1] = KERN_PROC;
122         mib[2] = KERN_PROC_SV_NAME;
123         mib[3] = trussinfo->pid;
124         error = sysctl(mib, 4, progt, &len, NULL, 0);
125         if (error != 0)
126                 err(2, "can not get etype");
127
128         for (funcs = ex_types; funcs->type; funcs++)
129                 if (!strcmp(funcs->type, progt))
130                         break;
131
132         if (funcs->type == NULL) {
133                 funcs = &ex_types[0];
134                 warn("execution type %s is not supported -- using %s",
135                     progt, funcs->type);
136         }
137         return (funcs);
138 }
139
140 char *
141 strsig(int sig)
142 {
143         char *ret;
144
145         ret = NULL;
146         if (sig > 0 && sig < NSIG) {
147                 int i;
148                 asprintf(&ret, "sig%s", sys_signame[sig]);
149                 if (ret == NULL)
150                         return (NULL);
151                 for (i = 0; ret[i] != '\0'; ++i)
152                         ret[i] = toupper(ret[i]);
153         }
154         return (ret);
155 }
156
157 int
158 main(int ac, char **av)
159 {
160         int c;
161         int i;
162         char **command;
163         struct ex_types *funcs;
164         int initial_open;
165         char *fname;
166         struct trussinfo *trussinfo;
167         char *signame;
168
169         fname = NULL;
170         initial_open = 1;
171
172         /* Initialize the trussinfo struct */
173         trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
174         if (trussinfo == NULL)
175                 errx(1, "malloc() failed");
176         bzero(trussinfo, sizeof(struct trussinfo));
177         
178         trussinfo->outfile = stderr;
179         trussinfo->strsize = 32;
180         trussinfo->pr_why = S_NONE;
181         trussinfo->curthread = NULL;
182         SLIST_INIT(&trussinfo->threadlist);
183         while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) {
184                 switch (c) {
185                 case 'p':       /* specified pid */
186                         trussinfo->pid = atoi(optarg);
187                         /* make sure i don't trace me */
188                         if(trussinfo->pid == getpid()) {
189                                 fprintf(stderr, "attempt to grab self.\n");
190                                 exit(2);
191                         }
192                         break;
193                 case 'f': /* Follow fork()'s */
194                         trussinfo->flags |= FOLLOWFORKS;
195                         break;
196                 case 'a': /* Print execve() argument strings. */
197                         trussinfo->flags |= EXECVEARGS;
198                         break;
199                 case 'e': /* Print execve() environment strings. */
200                         trussinfo->flags |= EXECVEENVS;
201                         break;
202                 case 'd': /* Absolute timestamps */
203                         trussinfo->flags |= ABSOLUTETIMESTAMPS;
204                         break;
205                 case 'D': /* Relative timestamps */
206                         trussinfo->flags |= RELATIVETIMESTAMPS;
207                         break;
208                 case 'o':       /* Specified output file */
209                         fname = optarg;
210                         break;
211                 case 's':       /* Specified string size */
212                         trussinfo->strsize = atoi(optarg);
213                         break;
214                 case 'S':       /* Don't trace signals */ 
215                         trussinfo->flags |= NOSIGS;
216                         break;
217                 default:
218                         usage();
219                 }
220         }
221
222         ac -= optind; av += optind;
223         if ((trussinfo->pid == 0 && ac == 0) ||
224             (trussinfo->pid != 0 && ac != 0))
225                 usage();
226
227         if (fname != NULL) { /* Use output file */
228                 if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
229                         errx(1, "cannot open %s", fname);
230         }
231
232         /*
233          * If truss starts the process itself, it will ignore some signals --
234          * they should be passed off to the process, which may or may not
235          * exit.  If, however, we are examining an already-running process,
236          * then we restore the event mask on these same signals.
237          */
238
239         if (trussinfo->pid == 0) {      /* Start a command ourselves */
240                 command = av;
241                 trussinfo->pid = setup_and_wait(command);
242                 signal(SIGINT, SIG_IGN);
243                 signal(SIGTERM, SIG_IGN);
244                 signal(SIGQUIT, SIG_IGN);
245         } else {
246                 start_tracing(trussinfo->pid);
247                 signal(SIGINT, restore_proc);
248                 signal(SIGTERM, restore_proc);
249                 signal(SIGQUIT, restore_proc);
250         }
251
252
253         /*
254          * At this point, if we started the process, it is stopped waiting to
255          * be woken up, either in exit() or in execve().
256          */
257
258 START_TRACE:
259         funcs = set_etype(trussinfo);
260
261         initial_open = 0;
262         /*
263          * At this point, it's a simple loop, waiting for the process to
264          * stop, finding out why, printing out why, and then continuing it.
265          * All of the grunt work is done in the support routines.
266          */
267
268         clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
269
270         do {
271                 struct timespec timediff;
272                 waitevent(trussinfo);
273
274                 switch(i = trussinfo->pr_why) {
275                 case S_SCE:
276                         funcs->enter_syscall(trussinfo, MAXARGS);
277                         clock_gettime(CLOCK_REALTIME,
278                             &trussinfo->before);
279                         break;
280                 case S_SCX:
281                         clock_gettime(CLOCK_REALTIME,
282                             &trussinfo->after);
283
284                         if (trussinfo->curthread->in_fork &&
285                             (trussinfo->flags & FOLLOWFORKS)) {
286                                 int childpid;
287
288                                 trussinfo->curthread->in_fork = 0;
289                                 childpid =
290                                     funcs->exit_syscall(trussinfo,
291                                         trussinfo->pr_data);
292
293                                 /*
294                                  * Fork a new copy of ourself to trace
295                                  * the child of the original traced
296                                  * process.
297                                  */
298                                 if (fork() == 0) {
299                                         trussinfo->pid = childpid;
300                                         start_tracing(trussinfo->pid);
301                                         goto START_TRACE;
302                                 }
303                                 break;
304                         }
305                         funcs->exit_syscall(trussinfo, MAXARGS);
306                         break;
307                 case S_SIG:
308                         if (trussinfo->flags & NOSIGS)
309                                 break;
310                         if (trussinfo->flags & FOLLOWFORKS)
311                                 fprintf(trussinfo->outfile, "%5d: ",
312                                     trussinfo->pid);
313                         if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
314                                 timespecsubt(&trussinfo->after,
315                                     &trussinfo->start_time, &timediff);
316                                 fprintf(trussinfo->outfile, "%ld.%09ld ",
317                                     (long)timediff.tv_sec,
318                                     timediff.tv_nsec);
319                         }
320                         if (trussinfo->flags & RELATIVETIMESTAMPS) {
321                                 timespecsubt(&trussinfo->after,
322                                     &trussinfo->before, &timediff);
323                                 fprintf(trussinfo->outfile, "%ld.%09ld ",
324                                     (long)timediff.tv_sec,
325                                     timediff.tv_nsec);
326                         }
327                         signame = strsig(trussinfo->pr_data);
328                         fprintf(trussinfo->outfile,
329                             "SIGNAL %u (%s)\n", trussinfo->pr_data,
330                             signame == NULL ? "?" : signame);
331                         free(signame);
332                         break;
333                 case S_EXIT:
334                         if (trussinfo->flags & FOLLOWFORKS)
335                                 fprintf(trussinfo->outfile, "%5d: ",
336                                     trussinfo->pid);
337                         if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
338                                 timespecsubt(&trussinfo->after,
339                                     &trussinfo->start_time, &timediff);
340                                 fprintf(trussinfo->outfile, "%ld.%09ld ",
341                                     (long)timediff.tv_sec,
342                                     timediff.tv_nsec);
343                         }
344                         if (trussinfo->flags & RELATIVETIMESTAMPS) {
345                           timespecsubt(&trussinfo->after,
346                               &trussinfo->before, &timediff);
347                           fprintf(trussinfo->outfile, "%ld.%09ld ",
348                             (long)timediff.tv_sec, timediff.tv_nsec);
349                         }
350                         fprintf(trussinfo->outfile,
351                             "process exit, rval = %u\n", trussinfo->pr_data);
352                         break;
353                 default:
354                         break;
355                 }
356         } while (trussinfo->pr_why != S_EXIT);
357         fflush(trussinfo->outfile);
358         
359         return (0);
360 }