syscalls.c revision 58224
11558Srgrimes/*
254263Sshin * Copryight 1997 Sean Eric Fagan
31558Srgrimes *
4265420Simp * Redistribution and use in source and binary forms, with or without
5160747Syar * modification, are permitted provided that the following conditions
6298107Sgjb * are met:
71558Srgrimes * 1. Redistributions of source code must retain the above copyright
874815Sru *    notice, this list of conditions and the following disclaimer.
924554Sphk * 2. Redistributions in binary form must reproduce the above copyright
10204406Suqs *    notice, this list of conditions and the following disclaimer in the
11253424Shrs *    documentation and/or other materials provided with the distribution.
121558Srgrimes * 3. All advertising materials mentioning features or use of this software
13160747Syar *    must display the following acknowledgement:
14253427Shrs *	This product includes software developed by Sean Eric Fagan
15253427Shrs * 4. Neither the name of the author may be used to endorse or promote
16253427Shrs *    products derived from this software without specific prior written
17160747Syar *    permission.
18160747Syar *
19160747Syar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20160747Syar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21160747Syar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23253424Shrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24253424Shrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25253424Shrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26253424Shrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char rcsid[] =
34  "$FreeBSD: head/usr.bin/truss/syscalls.c 58224 2000-03-18 08:49:41Z sef $";
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
55struct 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
89char * 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
96struct syscall *
97get_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
115char *
116get_string(int procfd, void *offset, int max) {
117	char *buf;
118	int size, len, c, fd;
119	FILE *p;
120
121	if ((fd = dup(procfd)) == -1)
122		err(1, "dup");
123	if ((p = fdopen(fd, "r")) == NULL)
124		err(1, "fdopen");
125	buf = malloc( size = (max ? max : 64 ) );
126	len = 0;
127	fseek(p, (long)offset, SEEK_SET);
128	while ((c = fgetc(p)) != EOF) {
129		buf[len++] = c;
130		if (c == 0 || len == max) {
131			buf[len] = 0;
132			break;
133		}
134		if (len == size) {
135			char *tmp;
136			tmp = realloc(buf, size+64);
137			if (tmp == NULL) {
138				buf[len] = 0;
139				fclose(p);
140				return buf;
141			}
142			size += 64;
143			buf = tmp;
144		}
145	}
146	fclose(p);
147	return buf;
148}
149
150
151/*
152 * Gag.  This is really unportable.  Multiplication is more portable.
153 * But slower, from the code I saw.
154 */
155
156static long long
157make_quad(unsigned long p1, unsigned long p2) {
158  union {
159    long long ll;
160    unsigned long l[2];
161  } t;
162  t.l[0] = p1;
163  t.l[1] = p2;
164  return t.ll;
165}
166
167
168/*
169 * print_arg
170 * Converts a syscall argument into a string.  Said string is
171 * allocated via malloc(), so needs to be free()'d.  The file
172 * descriptor is for the process' memory (via /proc), and is used
173 * to get any data (where the argument is a pointer).  sc is
174 * a pointer to the syscall description (see above); args is
175 * an array of all of the system call arguments.
176 */
177
178char *
179print_arg(int fd, struct syscall_args *sc, unsigned long *args) {
180  char *tmp = NULL;
181  switch (sc->type & ARG_MASK) {
182  case Hex:
183    tmp = malloc(12);
184    sprintf(tmp, "0x%lx", args[sc->offset]);
185    break;
186  case Octal:
187    tmp = malloc(13);
188    sprintf(tmp, "0%lo", args[sc->offset]);
189    break;
190  case Int:
191    tmp = malloc(12);
192    sprintf(tmp, "%ld", args[sc->offset]);
193    break;
194  case String:
195    {
196      char *tmp2;
197      tmp2 = get_string(fd, (void*)args[sc->offset], 0);
198      tmp = malloc(strlen(tmp2) + 3);
199      sprintf(tmp, "\"%s\"", tmp2);
200      free(tmp2);
201    }
202  break;
203  case Quad:
204    {
205      unsigned long long t;
206      unsigned long l1, l2;
207      l1 = args[sc->offset];
208      l2 = args[sc->offset+1];
209      t = make_quad(l1, l2);
210      tmp = malloc(24);
211      sprintf(tmp, "0x%qx", t);
212      break;
213    }
214  case Ptr:
215    tmp = malloc(12);
216    sprintf(tmp, "0x%lx", args[sc->offset]);
217    break;
218  case Ioctl:
219    {
220      char *temp = ioctlname(args[sc->offset]);
221      if (temp)
222	tmp = strdup(temp);
223      else {
224	tmp = malloc(12);
225	sprintf(tmp, "0x%lx", args[sc->offset]);
226      }
227    }
228    break;
229  case Signal:
230    {
231      long sig;
232
233      sig = args[sc->offset];
234      tmp = malloc(12);
235      if (sig > 0 && sig < NSIG) {
236	int i;
237	sprintf(tmp, "sig%s", sys_signame[sig]);
238	for (i = 0; tmp[i] != '\0'; ++i)
239	  tmp[i] = toupper(tmp[i]);
240      } else {
241        sprintf(tmp, "%ld", sig);
242      }
243    }
244    break;
245  }
246  return tmp;
247}
248
249/*
250 * print_syscall
251 * Print (to outfile) the system call and its arguments.  Note that
252 * nargs is the number of arguments (not the number of words; this is
253 * potentially confusing, I know).
254 */
255
256void
257print_syscall(FILE *outfile, const char *name, int nargs, char **s_args) {
258  int i;
259  int len = 0;
260  len += fprintf(outfile, "%s(", name);
261  for (i = 0; i < nargs; i++) {
262    if (s_args[i])
263      len += fprintf(outfile, "%s", s_args[i]);
264    else
265      len += fprintf(outfile, "<missing argument>");
266    len += fprintf(outfile, "%s", i < (nargs - 1) ? "," : "");
267  }
268  len += fprintf(outfile, ")");
269  for (i = 0; i < 6 - (len / 8); i++)
270	fprintf(outfile, "\t");
271}
272
273void
274print_syscall_ret(FILE *outfile, const char *name, int nargs, char **s_args, int errorp, int retval) {
275  print_syscall(outfile, name, nargs, s_args);
276  if (errorp) {
277    fprintf(outfile, " ERR#%d '%s'\n", retval, strerror(retval));
278  } else {
279    fprintf(outfile, " = %d (0x%x)\n", retval, retval);
280  }
281}
282