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