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