xref: /freebsd/usr.bin/truss/syscalls.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 #ifndef lint
33 static const char rcsid[] =
34 	"$Id: syscalls.c,v 1.4 1998/01/05 07:30:25 charnier Exp $";
35 #endif /* not lint */
36 
37 /*
38  * This file has routines used to print out system calls and their
39  * arguments.
40  */
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include "syscall.h"
49 
50 /*
51  * This should probably be in its own file.
52  */
53 
54 struct syscall syscalls[] = {
55 	{ "readlink", 1, 3,
56 	  { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}},
57 	{ "lseek", 2, 3,
58 	  { { Int, 0 }, {Quad, 2 }, { Int, 4 }}},
59 	{ "mmap", 2, 6,
60 	  { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}},
61 	{ "open", 1, 3,
62 	  { { String | IN, 0} , { Int, 1}, {Octal, 2}}},
63 	{ "linux_open", 1, 3,
64 	  { { String, 0 }, { Int, 1}, { Octal, 2 }}},
65 	{ "close", 1, 1, { { Int, 0 } } },
66 	{ "fstat", 1, 2,
67 	  { { Int, 0},  {Ptr | OUT , 1 }}},
68 	{ "stat", 1, 2,
69 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
70 	{ "linux_newstat", 1, 2,
71 	  { { String | IN, 0 }, { Ptr | OUT, 1 }}},
72 	{ "linux_newfstat", 1, 2,
73 	  { { Int, 0 }, { Ptr | OUT, 1 }}},
74 	{ "write", 1, 3,
75 	  { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}},
76 	{ "ioctl", 1, 3,
77 	  { { Int, 0}, { Ioctl, 1 }, { Hex, 2 }}},
78 	{ "break", 1, 1, { { Hex, 0 }}},
79 	{ "exit", 0, 1, { { Hex, 0 }}},
80 	{ 0, 0, 0, { { 0, 0 }}},
81 };
82 
83 char * ioctlname __P((int));
84 
85 /*
86  * If/when the list gets big, it might be desirable to do it
87  * as a hash table or binary search.
88  */
89 
90 struct syscall *
91 get_syscall(const char *name) {
92 	struct syscall *sc = syscalls;
93 
94 	while (sc->name) {
95 		if (!strcmp(name, sc->name))
96 			return sc;
97 		sc++;
98 	}
99 	return NULL;
100 }
101 
102 /*
103  * get_string
104  * Copy a string from the process.  Note that it is
105  * expected to be a C string, but if max is set, it will
106  * only get that much.
107  */
108 
109 char *
110 get_string(int procfd, void *offset, int max) {
111 	char *buf;
112 	int size, len, c;
113 	FILE *p;
114 
115 	if ((p = fdopen(procfd, "r")) == NULL)
116 		err(1, "fdopen");
117 	buf = malloc( size = (max ? max : 64 ) );
118 	len = 0;
119 	fseek(p, (long)offset, SEEK_SET);
120 	while ((c = fgetc(p)) != EOF) {
121 		buf[len++] = c;
122 		if (c == 0 || len == max) {
123 			buf[len] = 0;
124 			break;
125 		}
126 		if (len == size) {
127 			char *tmp = buf;
128 			tmp = realloc(buf, size+64);
129 			if (tmp == NULL) {
130 				buf[len] = 0;
131 				return buf;
132 			}
133 			size += 64;
134 		}
135 	}
136 	return buf;
137 }
138 
139 
140 /*
141  * Gag.  This is really unportable.  Multiplication is more portable.
142  * But slower, from the code I saw.
143  */
144 
145 static long long
146 make_quad(unsigned long p1, unsigned long p2) {
147   union {
148     long long ll;
149     unsigned long l[2];
150   } t;
151   t.l[0] = p1;
152   t.l[1] = p2;
153   return t.ll;
154 }
155 
156 
157 /*
158  * print_arg
159  * Converts a syscall argument into a string.  Said string is
160  * allocated via malloc(), so needs to be free()'d.  The file
161  * descriptor is for the process' memory (via /proc), and is used
162  * to get any data (where the argument is a pointer).  sc is
163  * a pointer to the syscall description (see above); args is
164  * an array of all of the system call arguments.
165  */
166 
167 char *
168 print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
169   char *tmp = NULL;
170   switch (sc->type & ARG_MASK) {
171   case Hex:
172     tmp = malloc(12);
173     sprintf(tmp, "0x%lx", args[sc->offset]);
174     break;
175   case Octal:
176     tmp = malloc(13);
177     sprintf(tmp, "0%lo", args[sc->offset]);
178     break;
179   case Int:
180     tmp = malloc(12);
181     sprintf(tmp, "%ld", args[sc->offset]);
182     break;
183   case String:
184     {
185       char *tmp2;
186       tmp2 = get_string(fd, (void*)args[sc->offset], 0);
187       tmp = malloc(strlen(tmp2) + 3);
188       sprintf(tmp, "\"%s\"", tmp2);
189       free(tmp2);
190     }
191   break;
192   case Quad:
193     {
194       unsigned long long t;
195       unsigned long l1, l2;
196       l1 = args[sc->offset];
197       l2 = args[sc->offset+1];
198       t = make_quad(l1, l2);
199       tmp = malloc(24);
200       sprintf(tmp, "0x%qx", t);
201       break;
202     }
203   case Ptr:
204     tmp = malloc(12);
205     sprintf(tmp, "0x%lx", args[sc->offset]);
206     break;
207   case Ioctl:
208     {
209       char *temp = ioctlname(args[sc->offset]);
210       if (temp)
211 	tmp = strdup(temp);
212       else {
213 	tmp = malloc(12);
214 	sprintf(tmp, "0x%lx", args[sc->offset]);
215       }
216     }
217   }
218   return tmp;
219 }
220 
221 /*
222  * print_syscall
223  * Print (to outfile) the system call and its arguments.  Note that
224  * nargs is the number of arguments (not the number of words; this is
225  * potentially confusing, I know).
226  */
227 
228 void
229 print_syscall(FILE *outfile, const char *name, int nargs, char **s_args) {
230   int i;
231   fprintf(outfile, "syscall %s(", name);
232   for (i = 0; i < nargs; i++) {
233     if (s_args[i])
234       fprintf(outfile, "%s", s_args[i]);
235     else
236       fprintf(outfile, "<missing argument>");
237     fprintf(outfile, "%s", i < (nargs - 1) ? "," : "");
238   }
239   fprintf(outfile, ")\n\t");
240 }
241