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 <sys/types.h> 43 #include <sys/socket.h> 44 #include <sys/time.h> 45 #include <sys/un.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 58 #include "truss.h" 59 #include "extern.h" 60 #include "syscall.h" 61 62 /* 63 * This should probably be in its own file. 64 */ 65 66 struct syscall syscalls[] = { 67 { "readlink", 1, 3, 68 { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}}, 69 { "lseek", 2, 3, 70 { { Int, 0 }, {Quad, 2 }, { Int, 4 }}}, 71 { "mmap", 2, 6, 72 { { Hex, 0 }, {Int, 1}, {Hex, 2}, {Hex, 3}, {Int, 4}, {Quad, 6}}}, 73 { "open", 1, 3, 74 { { String | IN, 0} , { Hex, 1}, {Octal, 2}}}, 75 { "linux_open", 1, 3, 76 { { String, 0 }, { Hex, 1}, { Octal, 2 }}}, 77 { "close", 1, 1, 78 { { Int, 0 } } }, 79 { "link", 0, 2, 80 { { String, 0 }, { String, 1 }}}, 81 { "unlink", 0, 1, 82 { { String, 0 }}}, 83 { "chdir", 0, 1, 84 { { String, 0 }}}, 85 { "mknod", 0, 3, 86 { { String, 0 }, { Octal, 1 }, { Int, 3 }}}, 87 { "chmod", 0, 2, 88 { { String, 0 }, { Octal, 1 }}}, 89 { "chown", 0, 3, 90 { { String, 0 }, { Int, 1 }, { Int, 2 }}}, 91 { "mount", 0, 4, 92 { { String, 0 }, { String, 1 }, { Int, 2 }, { Ptr, 3 }}}, 93 { "umount", 0, 2, 94 { { String, 0 }, { Int, 2 }}}, 95 { "fstat", 1, 2, 96 { { Int, 0}, {Ptr | OUT , 1 }}}, 97 { "stat", 1, 2, 98 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 99 { "lstat", 1, 2, 100 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 101 { "linux_newstat", 1, 2, 102 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 103 { "linux_newfstat", 1, 2, 104 { { Int, 0 }, { Ptr | OUT, 1 }}}, 105 { "write", 1, 3, 106 { { Int, 0}, { Ptr | IN, 1 }, { Int, 2 }}}, 107 { "ioctl", 1, 3, 108 { { Int, 0}, { Ioctl, 1 }, { Hex, 2 }}}, 109 { "break", 1, 1, { { Hex, 0 }}}, 110 { "exit", 0, 1, { { Hex, 0 }}}, 111 { "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}}, 112 { "sigaction", 1, 3, 113 { { Signal, 0 }, { Ptr | IN, 1 }, { Ptr | OUT, 2 }}}, 114 { "accept", 1, 3, 115 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 116 { "bind", 1, 3, 117 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 118 { "connect", 1, 3, 119 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 120 { "getpeername", 1, 3, 121 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 122 { "getsockname", 1, 3, 123 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 124 { "execve", 1, 3, 125 { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } }, 126 { "linux_execve", 1, 3, 127 { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } }, 128 { "kldload", 0, 1, { { String | IN, 0 }}}, 129 { "kldunload", 0, 1, { { Int, 0 }}}, 130 { "kldfind", 0, 1, { { String | IN, 0 }}}, 131 { "kldnext", 0, 1, { { Int, 0 }}}, 132 { "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}}, 133 { "kldfirstmod", 0, 1, { { Int, 0 }}}, 134 { 0, 0, 0, { { 0, 0 }}}, 135 }; 136 137 /* 138 * If/when the list gets big, it might be desirable to do it 139 * as a hash table or binary search. 140 */ 141 142 struct syscall * 143 get_syscall(const char *name) { 144 struct syscall *sc = syscalls; 145 146 while (sc->name) { 147 if (!strcmp(name, sc->name)) 148 return sc; 149 sc++; 150 } 151 return NULL; 152 } 153 154 /* 155 * get_struct 156 * 157 * Copy a fixed amount of bytes from the process. 158 */ 159 160 static int 161 get_struct(int procfd, void *offset, void *buf, int len) { 162 char *pos; 163 FILE *p; 164 int c, fd; 165 166 if ((fd = dup(procfd)) == -1) 167 err(1, "dup"); 168 if ((p = fdopen(fd, "r")) == NULL) 169 err(1, "fdopen"); 170 fseeko(p, (uintptr_t)offset, SEEK_SET); 171 for (pos = (char *)buf; len--; pos++) { 172 if ((c = fgetc(p)) == EOF) 173 return -1; 174 *pos = c; 175 } 176 fclose(p); 177 return 0; 178 } 179 180 /* 181 * get_string 182 * Copy a string from the process. Note that it is 183 * expected to be a C string, but if max is set, it will 184 * only get that much. 185 */ 186 187 char * 188 get_string(int procfd, void *offset, int max) { 189 char *buf; 190 int size, len, c, fd; 191 FILE *p; 192 193 if ((fd = dup(procfd)) == -1) 194 err(1, "dup"); 195 if ((p = fdopen(fd, "r")) == NULL) 196 err(1, "fdopen"); 197 buf = malloc( size = (max ? max : 64 ) ); 198 len = 0; 199 buf[0] = 0; 200 fseeko(p, (uintptr_t)offset, SEEK_SET); 201 while ((c = fgetc(p)) != EOF) { 202 buf[len++] = c; 203 if (c == 0 || len == max) { 204 buf[len] = 0; 205 break; 206 } 207 if (len == size) { 208 char *tmp; 209 tmp = realloc(buf, size+64); 210 if (tmp == NULL) { 211 buf[len] = 0; 212 fclose(p); 213 return buf; 214 } 215 size += 64; 216 buf = tmp; 217 } 218 } 219 fclose(p); 220 return buf; 221 } 222 223 224 /* 225 * Gag. This is really unportable. Multiplication is more portable. 226 * But slower, from the code I saw. 227 */ 228 229 static long long 230 make_quad(unsigned long p1, unsigned long p2) { 231 union { 232 long long ll; 233 unsigned long l[2]; 234 } t; 235 t.l[0] = p1; 236 t.l[1] = p2; 237 return t.ll; 238 } 239 240 241 /* 242 * print_arg 243 * Converts a syscall argument into a string. Said string is 244 * allocated via malloc(), so needs to be free()'d. The file 245 * descriptor is for the process' memory (via /proc), and is used 246 * to get any data (where the argument is a pointer). sc is 247 * a pointer to the syscall description (see above); args is 248 * an array of all of the system call arguments. 249 */ 250 251 char * 252 print_arg(int fd, struct syscall_args *sc, unsigned long *args) { 253 char *tmp = NULL; 254 switch (sc->type & ARG_MASK) { 255 case Hex: 256 tmp = malloc(12); 257 sprintf(tmp, "0x%lx", args[sc->offset]); 258 break; 259 case Octal: 260 tmp = malloc(13); 261 sprintf(tmp, "0%lo", args[sc->offset]); 262 break; 263 case Int: 264 tmp = malloc(12); 265 sprintf(tmp, "%ld", args[sc->offset]); 266 break; 267 case String: 268 { 269 char *tmp2; 270 tmp2 = get_string(fd, (void*)args[sc->offset], 0); 271 tmp = malloc(strlen(tmp2) + 3); 272 sprintf(tmp, "\"%s\"", tmp2); 273 free(tmp2); 274 } 275 break; 276 case StringArray: 277 { 278 int num, size, i; 279 char *tmp2; 280 char *string; 281 char *strarray[100]; /* XXX This is ugly. */ 282 283 if (get_struct(fd, (void *)args[sc->offset], (void *)&strarray, 284 sizeof(strarray)) == -1) { 285 err(1, "get_struct %p", (void *)args[sc->offset]); 286 } 287 num = 0; 288 size = 0; 289 290 /* Find out how large of a buffer we'll need. */ 291 while (strarray[num] != NULL) { 292 string = get_string(fd, (void*)strarray[num], 0); 293 size += strlen(string); 294 free(string); 295 num++; 296 } 297 size += 4 + (num * 4); 298 tmp = (char *)malloc(size); 299 tmp2 = tmp; 300 301 tmp2 += sprintf(tmp2, " ["); 302 for (i = 0; i < num; i++) { 303 string = get_string(fd, (void*)strarray[i], 0); 304 tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ','); 305 free(string); 306 } 307 tmp2 += sprintf(tmp2, "]"); 308 } 309 break; 310 case Quad: 311 { 312 unsigned long long t; 313 unsigned long l1, l2; 314 l1 = args[sc->offset]; 315 l2 = args[sc->offset+1]; 316 t = make_quad(l1, l2); 317 tmp = malloc(24); 318 sprintf(tmp, "0x%qx", t); 319 break; 320 } 321 case Ptr: 322 tmp = malloc(12); 323 sprintf(tmp, "0x%lx", args[sc->offset]); 324 break; 325 case Ioctl: 326 { 327 const char *temp = ioctlname(args[sc->offset]); 328 if (temp) 329 tmp = strdup(temp); 330 else { 331 tmp = malloc(12); 332 sprintf(tmp, "0x%lx", args[sc->offset]); 333 } 334 } 335 break; 336 case Signal: 337 { 338 long sig; 339 340 sig = args[sc->offset]; 341 tmp = malloc(12); 342 if (sig > 0 && sig < NSIG) { 343 int i; 344 sprintf(tmp, "sig%s", sys_signame[sig]); 345 for (i = 0; tmp[i] != '\0'; ++i) 346 tmp[i] = toupper(tmp[i]); 347 } else { 348 sprintf(tmp, "%ld", sig); 349 } 350 } 351 break; 352 case Sockaddr: 353 { 354 struct sockaddr_storage ss; 355 char addr[64]; 356 struct sockaddr_in *lsin; 357 struct sockaddr_in6 *lsin6; 358 struct sockaddr_un *sun; 359 struct sockaddr *sa; 360 char *p; 361 u_char *q; 362 int i; 363 364 /* yuck: get ss_len */ 365 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, 366 sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1) 367 err(1, "get_struct %p", (void *)args[sc->offset]); 368 /* sockaddr_un never have the length filled in! */ 369 if (ss.ss_family == AF_UNIX) { 370 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, 371 sizeof(*sun)) 372 == -1) 373 err(2, "get_struct %p", (void *)args[sc->offset]); 374 } else { 375 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, ss.ss_len) 376 == -1) 377 err(2, "get_struct %p", (void *)args[sc->offset]); 378 } 379 380 switch (ss.ss_family) { 381 case AF_INET: 382 lsin = (struct sockaddr_in *)&ss; 383 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr); 384 asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port)); 385 break; 386 case AF_INET6: 387 lsin6 = (struct sockaddr_in6 *)&ss; 388 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr); 389 asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port)); 390 break; 391 case AF_UNIX: 392 sun = (struct sockaddr_un *)&ss; 393 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path); 394 break; 395 default: 396 sa = (struct sockaddr *)&ss; 397 asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }", 398 (int)sa->sa_len, (int)sa->sa_family, &i, 399 6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), ""); 400 if (tmp != NULL) { 401 p = tmp + i; 402 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++) 403 p += sprintf(p, " %#02x,", *q); 404 } 405 } 406 } 407 break; 408 } 409 return tmp; 410 } 411 412 #define timespecsubt(tvp, uvp, vvp) \ 413 do { \ 414 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 415 (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \ 416 if ((vvp)->tv_nsec < 0) { \ 417 (vvp)->tv_sec--; \ 418 (vvp)->tv_nsec += 1000000000; \ 419 } \ 420 } while (0) 421 422 /* 423 * print_syscall 424 * Print (to outfile) the system call and its arguments. Note that 425 * nargs is the number of arguments (not the number of words; this is 426 * potentially confusing, I know). 427 */ 428 429 void 430 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) { 431 int i; 432 int len = 0; 433 struct timespec timediff; 434 435 if (trussinfo->flags & FOLLOWFORKS) 436 len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid); 437 438 if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) { 439 clock_gettime(CLOCK_REALTIME, &trussinfo->after); 440 } 441 442 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 443 timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff); 444 len += fprintf(trussinfo->outfile, "%ld.%09ld ", 445 (long)timediff.tv_sec, timediff.tv_nsec); 446 } 447 448 if (trussinfo->flags & RELATIVETIMESTAMPS) { 449 timespecsubt(&trussinfo->after, &trussinfo->before, &timediff); 450 len += fprintf(trussinfo->outfile, "%ld.%09ld ", 451 (long)timediff.tv_sec, timediff.tv_nsec); 452 } 453 454 len += fprintf(trussinfo->outfile, "%s(", name); 455 456 for (i = 0; i < nargs; i++) { 457 if (s_args[i]) 458 len += fprintf(trussinfo->outfile, "%s", s_args[i]); 459 else 460 len += fprintf(trussinfo->outfile, "<missing argument>"); 461 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : ""); 462 } 463 len += fprintf(trussinfo->outfile, ")"); 464 for (i = 0; i < 6 - (len / 8); i++) 465 fprintf(trussinfo->outfile, "\t"); 466 } 467 468 void 469 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args, int errorp, int retval) { 470 print_syscall(trussinfo, name, nargs, s_args); 471 if (errorp) { 472 fprintf(trussinfo->outfile, " ERR#%d '%s'\n", retval, strerror(retval)); 473 } else { 474 fprintf(trussinfo->outfile, " = %d (0x%x)\n", retval, retval); 475 } 476 } 477