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 * $FreeBSD: src/usr.bin/truss/syscalls.c,v 1.10.2.6 2003/04/14 18:24:38 mdodd Exp $ 32 */ 33 34 /* 35 * This file has routines used to print out system calls and their 36 * arguments. 37 */ 38 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <sys/un.h> 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <signal.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "truss.h" 54 #include "extern.h" 55 #include "syscall.h" 56 57 /* 58 * This should probably be in its own file. 59 */ 60 61 struct syscall syscalls[] = { 62 { "readlink", 1, 3, 63 { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}}, 64 { "lseek", 2, 3, 65 { { Int, 0 }, {Quad, 2 }, { Int, 4 }}}, 66 { "mmap", 2, 6, 67 { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}}, 68 { "open", 1, 3, 69 { { String | IN, 0} , { Hex, 1}, {Octal, 2}}}, 70 { "close", 1, 1, { { Int, 0 } } }, 71 { "fstat", 1, 2, 72 { { Int, 0}, {Ptr | OUT , 1 }}}, 73 { "stat", 1, 2, 74 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 75 { "lstat", 1, 2, 76 { { String | IN, 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 { "accept", 1, 3, 87 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 88 { "bind", 1, 3, 89 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 90 { "connect", 1, 3, 91 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 92 { "getpeername", 1, 3, 93 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 94 { "getsockname", 1, 3, 95 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 96 { 0, 0, 0, { { 0, 0 }}}, 97 }; 98 99 /* 100 * If/when the list gets big, it might be desirable to do it 101 * as a hash table or binary search. 102 */ 103 104 struct syscall * 105 get_syscall(const char *name) { 106 struct syscall *sc = syscalls; 107 108 while (sc->name) { 109 if (!strcmp(name, sc->name)) 110 return sc; 111 sc++; 112 } 113 return NULL; 114 } 115 116 /* 117 * get_struct 118 * 119 * Copy a fixed amount of bytes from the process. 120 */ 121 122 static int 123 get_struct(int procfd, void *offset, void *buf, int len) { 124 char *pos; 125 FILE *p; 126 int c, fd; 127 128 if ((fd = dup(procfd)) == -1) 129 err(1, "dup"); 130 if ((p = fdopen(fd, "r")) == NULL) 131 err(1, "fdopen"); 132 fseeko(p, (uintptr_t)offset, SEEK_SET); 133 for (pos = (char *)buf; len--; pos++) { 134 if ((c = fgetc(p)) == EOF) 135 return -1; 136 *pos = c; 137 } 138 fclose(p); 139 return 0; 140 } 141 142 /* 143 * get_string 144 * Copy a string from the process. Note that it is 145 * expected to be a C string, but if max is set, it will 146 * only get that much. 147 */ 148 149 char * 150 get_string(int procfd, void *offset, int max) { 151 char *buf; 152 int size, len, c, fd; 153 FILE *p; 154 155 if ((fd = dup(procfd)) == -1) 156 err(1, "dup"); 157 if ((p = fdopen(fd, "r")) == NULL) 158 err(1, "fdopen"); 159 buf = malloc( size = (max ? max : 64 ) ); 160 len = 0; 161 buf[0] = 0; 162 fseeko(p, (uintptr_t)offset, SEEK_SET); 163 while ((c = fgetc(p)) != EOF) { 164 buf[len++] = c; 165 if (c == 0 || len == max) { 166 buf[len] = 0; 167 break; 168 } 169 if (len == size) { 170 char *tmp; 171 tmp = realloc(buf, size+64); 172 if (tmp == NULL) { 173 buf[len] = 0; 174 fclose(p); 175 return buf; 176 } 177 size += 64; 178 buf = tmp; 179 } 180 } 181 fclose(p); 182 return buf; 183 } 184 185 186 /* 187 * Gag. This is really unportable. Multiplication is more portable. 188 * But slower, from the code I saw. 189 */ 190 191 static long long 192 make_quad(unsigned long p1, unsigned long p2) { 193 union { 194 long long ll; 195 unsigned long l[2]; 196 } t; 197 t.l[0] = p1; 198 t.l[1] = p2; 199 return t.ll; 200 } 201 202 203 /* 204 * print_arg 205 * Converts a syscall argument into a string. Said string is 206 * allocated via malloc(), so needs to be free()'d. The file 207 * descriptor is for the process' memory (via /proc), and is used 208 * to get any data (where the argument is a pointer). sc is 209 * a pointer to the syscall description (see above); args is 210 * an array of all of the system call arguments. 211 */ 212 213 char * 214 print_arg(int fd, struct syscall_args *sc, unsigned long *args) { 215 char *tmp = NULL; 216 switch (sc->type & ARG_MASK) { 217 case Hex: 218 tmp = malloc(12); 219 sprintf(tmp, "0x%lx", args[sc->offset]); 220 break; 221 case Octal: 222 tmp = malloc(13); 223 sprintf(tmp, "0%lo", args[sc->offset]); 224 break; 225 case Int: 226 tmp = malloc(12); 227 sprintf(tmp, "%ld", args[sc->offset]); 228 break; 229 case String: 230 { 231 char *tmp2; 232 tmp2 = get_string(fd, (void*)args[sc->offset], 0); 233 tmp = malloc(strlen(tmp2) + 3); 234 sprintf(tmp, "\"%s\"", tmp2); 235 free(tmp2); 236 } 237 break; 238 case Quad: 239 { 240 unsigned long long t; 241 unsigned long l1, l2; 242 l1 = args[sc->offset]; 243 l2 = args[sc->offset+1]; 244 t = make_quad(l1, l2); 245 tmp = malloc(24); 246 sprintf(tmp, "0x%qx", t); 247 break; 248 } 249 case Ptr: 250 tmp = malloc(12); 251 sprintf(tmp, "0x%lx", args[sc->offset]); 252 break; 253 case Ioctl: 254 { 255 const char *temp = ioctlname(args[sc->offset]); 256 if (temp) 257 tmp = strdup(temp); 258 else { 259 tmp = malloc(12); 260 sprintf(tmp, "0x%lx", args[sc->offset]); 261 } 262 } 263 break; 264 case Signal: 265 { 266 long sig; 267 268 sig = args[sc->offset]; 269 tmp = malloc(12); 270 if (sig > 0 && sig < NSIG) { 271 int i; 272 sprintf(tmp, "sig%s", sys_signame[sig]); 273 for (i = 0; tmp[i] != '\0'; ++i) 274 tmp[i] = toupper(tmp[i]); 275 } else { 276 sprintf(tmp, "%ld", sig); 277 } 278 } 279 break; 280 case Sockaddr: 281 { 282 struct sockaddr_storage ss; 283 char addr[64]; 284 struct sockaddr_in *lsin; 285 struct sockaddr_in6 *lsin6; 286 struct sockaddr_un *sun; 287 struct sockaddr *sa; 288 char *p; 289 u_char *q; 290 int i; 291 292 /* yuck: get ss_len */ 293 if (get_struct(fd, (void *)args[sc->offset], &ss, 294 sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1) 295 err(1, "get_struct %p", (void *)args[sc->offset]); 296 /* sockaddr_un never have the length filled in! */ 297 if (ss.ss_family == AF_UNIX) { 298 if (get_struct(fd, (void *)args[sc->offset], &ss, 299 sizeof(*sun)) 300 == -1) 301 err(2, "get_struct %p", (void *)args[sc->offset]); 302 } else { 303 if (get_struct(fd, (void *)args[sc->offset], &ss, 304 ss.ss_len < sizeof(ss) ? ss.ss_len : sizeof(ss)) 305 == -1) 306 err(2, "get_struct %p", (void *)args[sc->offset]); 307 } 308 309 switch (ss.ss_family) { 310 case AF_INET: 311 lsin = (struct sockaddr_in *)&ss; 312 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr); 313 asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port)); 314 break; 315 case AF_INET6: 316 lsin6 = (struct sockaddr_in6 *)&ss; 317 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr); 318 asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port)); 319 break; 320 case AF_UNIX: 321 sun = (struct sockaddr_un *)&ss; 322 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path); 323 break; 324 default: 325 sa = (struct sockaddr *)&ss; 326 asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }", 327 (int)sa->sa_len, (int)sa->sa_family, &i, 328 6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), ""); 329 if (tmp != NULL) { 330 p = tmp + i; 331 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++) 332 p += sprintf(p, " %#02x,", *q); 333 } 334 } 335 } 336 break; 337 } 338 return tmp; 339 } 340 341 /* 342 * print_syscall 343 * Print (to trussinfo->outfile) the system call and its arguments. Note that 344 * nargs is the number of arguments (not the number of words; this is 345 * potentially confusing, I know). 346 */ 347 348 void 349 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) { 350 int i; 351 int len = 0; 352 len += fprintf(trussinfo->outfile, "%s(", name); 353 for (i = 0; i < nargs; i++) { 354 if (s_args[i]) 355 len += fprintf(trussinfo->outfile, "%s", s_args[i]); 356 else 357 len += fprintf(trussinfo->outfile, "<missing argument>"); 358 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : ""); 359 } 360 len += fprintf(trussinfo->outfile, ")"); 361 for (i = 0; i < 6 - (len / 8); i++) 362 fprintf(trussinfo->outfile, "\t"); 363 } 364 365 void 366 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args, int errorp, int retval) { 367 print_syscall(trussinfo, name, nargs, s_args); 368 if (errorp) { 369 fprintf(trussinfo->outfile, " ERR#%d '%s'\n", retval, strerror(retval)); 370 } else { 371 fprintf(trussinfo->outfile, " = %d (0x%x)\n", retval, retval); 372 } 373 } 374