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/mman.h> 43 #include <sys/types.h> 44 #include <sys/socket.h> 45 #include <sys/time.h> 46 #include <sys/un.h> 47 #include <netinet/in.h> 48 #include <arpa/inet.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <fcntl.h> 53 #include <poll.h> 54 #include <signal.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <time.h> 60 #include <unistd.h> 61 62 #include "truss.h" 63 #include "extern.h" 64 #include "syscall.h" 65 66 /* 67 * This should probably be in its own file. 68 */ 69 70 struct syscall syscalls[] = { 71 { "fcntl", 1, 3, 72 { { Int, 0 } , { Fcntl, 1 }, { Hex, 2 }}}, 73 { "readlink", 1, 3, 74 { { String, 0 } , { String | OUT, 1 }, { Int, 2 }}}, 75 { "lseek", 2, 3, 76 { { Int, 0 }, {Quad, 2 }, { Int, 4 }}}, 77 { "mmap", 2, 6, 78 { { Ptr, 0 }, {Int, 1}, {Mprot, 2}, {Mmapflags, 3}, {Int, 4}, {Quad, 6}}}, 79 { "mprotect", 1, 3, 80 { { Ptr, 0 }, {Int, 1}, {Mprot, 2}}}, 81 { "open", 1, 3, 82 { { String | IN, 0} , { Hex, 1}, {Octal, 2}}}, 83 { "linux_open", 1, 3, 84 { { String, 0 }, { Hex, 1}, { Octal, 2 }}}, 85 { "close", 1, 1, 86 { { Int, 0 } } }, 87 { "link", 0, 2, 88 { { String, 0 }, { String, 1 }}}, 89 { "unlink", 0, 1, 90 { { String, 0 }}}, 91 { "chdir", 0, 1, 92 { { String, 0 }}}, 93 { "mknod", 0, 3, 94 { { String, 0 }, { Octal, 1 }, { Int, 3 }}}, 95 { "chmod", 0, 2, 96 { { String, 0 }, { Octal, 1 }}}, 97 { "chown", 0, 3, 98 { { String, 0 }, { Int, 1 }, { Int, 2 }}}, 99 { "mount", 0, 4, 100 { { String, 0 }, { String, 1 }, { Int, 2 }, { Ptr, 3 }}}, 101 { "umount", 0, 2, 102 { { String, 0 }, { Int, 2 }}}, 103 { "fstat", 1, 2, 104 { { Int, 0}, {Ptr | OUT , 1 }}}, 105 { "stat", 1, 2, 106 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 107 { "lstat", 1, 2, 108 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 109 { "linux_newstat", 1, 2, 110 { { String | IN, 0 }, { Ptr | OUT, 1 }}}, 111 { "linux_newfstat", 1, 2, 112 { { Int, 0 }, { Ptr | OUT, 1 }}}, 113 { "write", 1, 3, 114 { { Int, 0 }, { Ptr | IN, 1 }, { Int, 2 }}}, 115 { "ioctl", 1, 3, 116 { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 }}}, 117 { "break", 1, 1, { { Hex, 0 }}}, 118 { "exit", 0, 1, { { Hex, 0 }}}, 119 { "access", 1, 2, { { String | IN, 0 }, { Int, 1 }}}, 120 { "sigaction", 1, 3, 121 { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 }}}, 122 { "accept", 1, 3, 123 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 124 { "bind", 1, 3, 125 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 126 { "connect", 1, 3, 127 { { Hex, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, 128 { "getpeername", 1, 3, 129 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 130 { "getsockname", 1, 3, 131 { { Hex, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 132 { "recvfrom", 1, 6, 133 { { Hex, 0 }, { Ptr | IN, 1 }, { Int, 3 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } }, 134 { "sendto", 1, 6, 135 { { Hex, 0 }, { Ptr | IN, 1 }, { Int, 3 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } }, 136 { "execve", 1, 3, 137 { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } }, 138 { "linux_execve", 1, 3, 139 { { String | IN, 0 }, { StringArray | IN, 1 }, { StringArray | IN, 2 } } }, 140 { "kldload", 0, 1, { { String | IN, 0 }}}, 141 { "kldunload", 0, 1, { { Int, 0 }}}, 142 { "kldfind", 0, 1, { { String | IN, 0 }}}, 143 { "kldnext", 0, 1, { { Int, 0 }}}, 144 { "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}}, 145 { "kldfirstmod", 0, 1, { { Int, 0 }}}, 146 { "nanosleep", 0, 1, { { Timespec, 0 }}}, 147 { "select", 1, 5, { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 }}}, 148 { "poll", 1, 3, { { Pollfd, 0 }, { Int, 1 }, { Int, 2 }}}, 149 { "gettimeofday", 1, 2, { { Timeval | OUT, 0 }, { Ptr, 1 }}}, 150 { "clock_gettime", 1, 2, { { Int, 0 }, { Timeval | OUT, 1 }}}, 151 { "recvfrom", 1, 6, { { Int, 0 }, { Ptr | OUT, 1 }, { Int, 2 }, { Int, 3 }, { Sockaddr | OUT, 4}, {Ptr | OUT, 5}}}, 152 { "getitimer", 1, 2, { { Int, 0 }, { Itimerval | OUT, 2 }}}, 153 { "setitimer", 1, 3, { { Int, 0 }, { Itimerval, 1} , { Itimerval | OUT, 2 }}}, 154 { 0, 0, 0, { { 0, 0 }}}, 155 }; 156 157 /* 158 * If/when the list gets big, it might be desirable to do it 159 * as a hash table or binary search. 160 */ 161 162 struct syscall * 163 get_syscall(const char *name) { 164 struct syscall *sc = syscalls; 165 166 while (sc->name) { 167 if (!strcmp(name, sc->name)) 168 return sc; 169 sc++; 170 } 171 return NULL; 172 } 173 174 /* 175 * get_struct 176 * 177 * Copy a fixed amount of bytes from the process. 178 */ 179 180 static int 181 get_struct(int procfd, void *offset, void *buf, int len) { 182 183 if (pread(procfd, buf, len, (uintptr_t)offset) != len) 184 return -1; 185 return 0; 186 } 187 188 /* 189 * get_string 190 * Copy a string from the process. Note that it is 191 * expected to be a C string, but if max is set, it will 192 * only get that much. 193 */ 194 195 char * 196 get_string(int procfd, void *offset, int max) { 197 char *buf; 198 int size, len, c, fd; 199 FILE *p; 200 201 if ((fd = dup(procfd)) == -1) 202 err(1, "dup"); 203 if ((p = fdopen(fd, "r")) == NULL) 204 err(1, "fdopen"); 205 buf = malloc( size = (max ? max : 64 ) ); 206 len = 0; 207 buf[0] = 0; 208 if (fseeko(p, (uintptr_t)offset, SEEK_SET) == 0) { 209 while ((c = fgetc(p)) != EOF) { 210 buf[len++] = c; 211 if (c == 0 || len == max) { 212 buf[len] = 0; 213 break; 214 } 215 if (len == size) { 216 char *tmp; 217 tmp = realloc(buf, size+64); 218 if (tmp == NULL) { 219 buf[len] = 0; 220 break; 221 } 222 size += 64; 223 buf = tmp; 224 } 225 } 226 } 227 fclose(p); 228 return (buf); 229 } 230 231 232 /* 233 * Gag. This is really unportable. Multiplication is more portable. 234 * But slower, from the code I saw. 235 */ 236 237 static long long 238 make_quad(unsigned long p1, unsigned long p2) { 239 union { 240 long long ll; 241 unsigned long l[2]; 242 } t; 243 t.l[0] = p1; 244 t.l[1] = p2; 245 return t.ll; 246 } 247 248 /* 249 * Remove a trailing '|' in a string, useful for fixup after decoding 250 * a "flags" argument. 251 */ 252 253 void 254 remove_trailing_or(char *str) 255 { 256 257 if (str != NULL && (str = rindex(str, '|')) != NULL && str[1] == '\0') 258 *str = '\0'; 259 } 260 261 /* 262 * print_arg 263 * Converts a syscall argument into a string. Said string is 264 * allocated via malloc(), so needs to be free()'d. The file 265 * descriptor is for the process' memory (via /proc), and is used 266 * to get any data (where the argument is a pointer). sc is 267 * a pointer to the syscall description (see above); args is 268 * an array of all of the system call arguments. 269 */ 270 271 char * 272 print_arg(int fd, struct syscall_args *sc, unsigned long *args) { 273 char *tmp = NULL; 274 switch (sc->type & ARG_MASK) { 275 case Hex: 276 asprintf(&tmp, "0x%lx", args[sc->offset]); 277 break; 278 case Octal: 279 asprintf(&tmp, "0%lo", args[sc->offset]); 280 break; 281 case Int: 282 asprintf(&tmp, "%ld", args[sc->offset]); 283 break; 284 case String: 285 { 286 char *tmp2; 287 tmp2 = get_string(fd, (void*)args[sc->offset], 0); 288 asprintf(&tmp, "\"%s\"", tmp2); 289 free(tmp2); 290 } 291 break; 292 case StringArray: 293 { 294 int num, size, i; 295 char *tmp2; 296 char *string; 297 char *strarray[100]; /* XXX This is ugly. */ 298 299 if (get_struct(fd, (void *)args[sc->offset], (void *)&strarray, 300 sizeof(strarray)) == -1) { 301 err(1, "get_struct %p", (void *)args[sc->offset]); 302 } 303 num = 0; 304 size = 0; 305 306 /* Find out how large of a buffer we'll need. */ 307 while (strarray[num] != NULL) { 308 string = get_string(fd, (void*)strarray[num], 0); 309 size += strlen(string); 310 free(string); 311 num++; 312 } 313 size += 4 + (num * 4); 314 tmp = (char *)malloc(size); 315 tmp2 = tmp; 316 317 tmp2 += sprintf(tmp2, " ["); 318 for (i = 0; i < num; i++) { 319 string = get_string(fd, (void*)strarray[i], 0); 320 tmp2 += sprintf(tmp2, " \"%s\"%c", string, (i+1 == num) ? ' ' : ','); 321 free(string); 322 } 323 tmp2 += sprintf(tmp2, "]"); 324 } 325 break; 326 case Quad: 327 { 328 unsigned long long t; 329 unsigned long l1, l2; 330 l1 = args[sc->offset]; 331 l2 = args[sc->offset+1]; 332 t = make_quad(l1, l2); 333 asprintf(&tmp, "0x%qx", t); 334 break; 335 } 336 case Ptr: 337 asprintf(&tmp, "0x%lx", args[sc->offset]); 338 break; 339 case Ioctl: 340 { 341 const char *temp = ioctlname(args[sc->offset]); 342 if (temp) 343 tmp = strdup(temp); 344 else 345 asprintf(&tmp, "0x%lx", args[sc->offset]); 346 } 347 break; 348 case Timespec: 349 { 350 struct timespec ts; 351 if (get_struct(fd, (void *)args[sc->offset], &ts, sizeof(ts)) != -1) 352 asprintf(&tmp, "{%jd %jd}", (intmax_t)ts.tv_sec, (intmax_t)ts.tv_nsec); 353 else 354 asprintf(&tmp, "0x%lx", args[sc->offset]); 355 } 356 break; 357 case Timeval: 358 { 359 struct timeval tv; 360 if (get_struct(fd, (void *)args[sc->offset], &tv, sizeof(tv)) != -1) 361 asprintf(&tmp, "{%jd %jd}", (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec); 362 else 363 asprintf(&tmp, "0x%lx", args[sc->offset]); 364 } 365 break; 366 case Itimerval: 367 { 368 struct itimerval itv; 369 if (get_struct(fd, (void *)args[sc->offset], &itv, sizeof(itv)) != -1) 370 asprintf(&tmp, "{%jd %jd, %jd %jd}", 371 (intmax_t)itv.it_interval.tv_sec, 372 (intmax_t)itv.it_interval.tv_usec, 373 (intmax_t)itv.it_value.tv_sec, 374 (intmax_t)itv.it_value.tv_usec); 375 else 376 asprintf(&tmp, "0x%lx", args[sc->offset]); 377 } 378 break; 379 case Pollfd: 380 { 381 /* 382 * XXX: A Pollfd argument expects the /next/ syscall argument to be 383 * the number of fds in the array. This matches the poll syscall. 384 */ 385 struct pollfd *pfd; 386 int numfds = args[sc->offset+1]; 387 int bytes = sizeof(struct pollfd) * numfds; 388 int i, tmpsize, u, used; 389 const int per_fd = 100; 390 391 if ((pfd = malloc(bytes)) == NULL) 392 err(1, "Cannot malloc %d bytes for pollfd array", bytes); 393 if (get_struct(fd, (void *)args[sc->offset], pfd, bytes) != -1) { 394 395 used = 0; 396 tmpsize = 1 + per_fd * numfds + 2; 397 if ((tmp = malloc(tmpsize)) == NULL) 398 err(1, "Cannot alloc %d bytes for poll output", tmpsize); 399 400 tmp[used++] = '{'; 401 for (i = 0; i < numfds; i++) { 402 #define POLLKNOWN_EVENTS \ 403 (POLLIN | POLLPRI | POLLOUT | POLLERR | POLLHUP | POLLNVAL | \ 404 POLLRDNORM |POLLRDBAND | POLLWRBAND | POLLINIGNEOF) 405 406 u += snprintf(tmp + used, per_fd, 407 "%s%d 0x%hx%s%s%s%s%s%s%s%s%s ", 408 i > 0 ? " " : "", 409 pfd[i].fd, 410 pfd[i].events & ~POLLKNOWN_EVENTS, 411 pfd[i].events & POLLIN ? "" : "|IN", 412 pfd[i].events & POLLPRI ? "" : "|PRI", 413 pfd[i].events & POLLOUT ? "" : "|OUT", 414 pfd[i].events & POLLERR ? "" : "|ERR", 415 pfd[i].events & POLLHUP ? "" : "|HUP", 416 pfd[i].events & POLLNVAL ? "" : "|NVAL", 417 pfd[i].events & POLLRDNORM ? "" : "|RDNORM", 418 pfd[i].events & POLLRDBAND ? "" : "|RDBAND", 419 pfd[i].events & POLLWRBAND ? "" : "|WRBAND"); 420 if (u > 0) 421 used += u < per_fd ? u : per_fd; 422 } 423 tmp[used++] = '}'; 424 tmp[used++] = '\0'; 425 } else 426 asprintf(&tmp, "0x%lx", args[sc->offset]); 427 free(pfd); 428 } 429 break; 430 case Fd_set: 431 { 432 /* 433 * XXX: A Fd_set argument expects the /first/ syscall argument to be 434 * the number of fds in the array. This matches the select syscall. 435 */ 436 fd_set *fds; 437 int numfds = args[0]; 438 int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS; 439 int i, tmpsize, u, used; 440 const int per_fd = 20; 441 442 if ((fds = malloc(bytes)) == NULL) 443 err(1, "Cannot malloc %d bytes for fd_set array", bytes); 444 if (get_struct(fd, (void *)args[sc->offset], fds, bytes) != -1) { 445 used = 0; 446 tmpsize = 1 + numfds * per_fd + 2; 447 if ((tmp = malloc(tmpsize)) == NULL) 448 err(1, "Cannot alloc %d bytes for fd_set output", tmpsize); 449 450 tmp[used++] = '{'; 451 for (i = 0; i < numfds; i++) { 452 if (FD_ISSET(i, fds)) { 453 u = snprintf(tmp + used, per_fd, "%d ", i); 454 if (u > 0) 455 used += u < per_fd ? u : per_fd; 456 } 457 } 458 if (tmp[used-1] == ' ') 459 used--; 460 tmp[used++] = '}'; 461 tmp[used++] = '\0'; 462 } else 463 asprintf(&tmp, "0x%lx", args[sc->offset]); 464 free(fds); 465 } 466 break; 467 case Signal: 468 { 469 long sig; 470 471 sig = args[sc->offset]; 472 if (sig > 0 && sig < NSIG) { 473 int i; 474 asprintf(&tmp, "sig%s", sys_signame[sig]); 475 for (i = 0; tmp[i] != '\0'; ++i) 476 tmp[i] = toupper(tmp[i]); 477 } else 478 asprintf(&tmp, "%ld", sig); 479 } 480 break; 481 case Fcntl: 482 { 483 switch (args[sc->offset]) { 484 #define S(a) case a: tmp = strdup(#a); break; 485 S(F_DUPFD); 486 S(F_GETFD); 487 S(F_SETFD); 488 S(F_GETFL); 489 S(F_SETFL); 490 S(F_GETOWN); 491 S(F_SETOWN); 492 S(F_GETLK); 493 S(F_SETLK); 494 S(F_SETLKW); 495 #undef S 496 } 497 if (tmp == NULL) 498 asprintf(&tmp, "0x%lx", args[sc->offset]); 499 } 500 break; 501 502 case Mprot: 503 { 504 505 #define S(a) ((args[sc->offset] & a) ? #a "|" : "") 506 asprintf(&tmp, "(0x%lx)%s%s%s%s", args[sc->offset], 507 S(PROT_NONE), S(PROT_READ), S(PROT_WRITE), S(PROT_EXEC)); 508 #undef S 509 remove_trailing_or(tmp); 510 511 } 512 break; 513 514 case Mmapflags: 515 { 516 #define S(a) ((args[sc->offset] & a) ? #a "|" : "") 517 asprintf(&tmp, "(0x%lx)%s%s%s%s%s%s%s%s", args[sc->offset], 518 S(MAP_ANON), S(MAP_FIXED), S(MAP_HASSEMAPHORE), 519 S(MAP_NOCORE), S(MAP_NOSYNC), S(MAP_PRIVATE), 520 S(MAP_SHARED), S(MAP_STACK)); 521 #undef S 522 523 remove_trailing_or(tmp); 524 } 525 break; 526 527 case Sockaddr: 528 { 529 struct sockaddr_storage ss; 530 char addr[64]; 531 struct sockaddr_in *lsin; 532 struct sockaddr_in6 *lsin6; 533 struct sockaddr_un *sun; 534 struct sockaddr *sa; 535 char *p; 536 u_char *q; 537 int i; 538 539 if (args[sc->offset] == 0) { 540 asprintf(&tmp, "NULL"); 541 break; 542 } 543 544 /* yuck: get ss_len */ 545 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, 546 sizeof(ss.ss_len) + sizeof(ss.ss_family)) == -1) 547 err(1, "get_struct %p", (void *)args[sc->offset]); 548 /* sockaddr_un never have the length filled in! */ 549 if (ss.ss_family == AF_UNIX) { 550 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, 551 sizeof(*sun)) 552 == -1) 553 err(2, "get_struct %p", (void *)args[sc->offset]); 554 } else { 555 if (get_struct(fd, (void *)args[sc->offset], (void *)&ss, ss.ss_len) 556 == -1) 557 err(2, "get_struct %p", (void *)args[sc->offset]); 558 } 559 560 switch (ss.ss_family) { 561 case AF_INET: 562 lsin = (struct sockaddr_in *)&ss; 563 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof addr); 564 asprintf(&tmp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port)); 565 break; 566 case AF_INET6: 567 lsin6 = (struct sockaddr_in6 *)&ss; 568 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof addr); 569 asprintf(&tmp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port)); 570 break; 571 case AF_UNIX: 572 sun = (struct sockaddr_un *)&ss; 573 asprintf(&tmp, "{ AF_UNIX \"%s\" }", sun->sun_path); 574 break; 575 default: 576 sa = (struct sockaddr *)&ss; 577 asprintf(&tmp, "{ sa_len = %d, sa_family = %d, sa_data = {%n%*s } }", 578 (int)sa->sa_len, (int)sa->sa_family, &i, 579 6 * (int)(sa->sa_len - ((char *)&sa->sa_data - (char *)sa)), ""); 580 if (tmp != NULL) { 581 p = tmp + i; 582 for (q = (u_char *)&sa->sa_data; q < (u_char *)sa + sa->sa_len; q++) 583 p += sprintf(p, " %#02x,", *q); 584 } 585 } 586 } 587 break; 588 case Sigaction: 589 { 590 struct sigaction sa; 591 char *hand; 592 const char *h; 593 #define SA_KNOWN_FLAGS \ 594 (SA_ONSTACK | SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER | \ 595 SA_NOCLDWAIT | SA_SIGINFO) 596 597 598 if (get_struct(fd, (void *)args[sc->offset], &sa, sizeof(sa)) != -1) { 599 600 asprintf(&hand, "%p", sa.sa_handler); 601 if (sa.sa_handler == SIG_DFL) 602 h = "SIG_DFL"; 603 else if (sa.sa_handler == SIG_IGN) 604 h = "SIG_IGN"; 605 else 606 h = hand; 607 asprintf(&tmp, "{ %s 0x%x%s%s%s%s%s%s%s ss_t }", 608 h, 609 sa.sa_flags & ~SA_KNOWN_FLAGS, 610 sa.sa_flags & SA_ONSTACK ? "" : "|ONSTACK", 611 sa.sa_flags & SA_RESTART ? "" : "|RESTART", 612 sa.sa_flags & SA_RESETHAND ? "" : "|RESETHAND", 613 sa.sa_flags & SA_NOCLDSTOP ? "" : "|NOCLDSTOP", 614 sa.sa_flags & SA_NODEFER ? "" : "|NODEFER", 615 sa.sa_flags & SA_NOCLDWAIT ? "" : "|NOCLDWAIT", 616 sa.sa_flags & SA_SIGINFO ? "" : "|SIGINFO"); 617 free(hand); 618 } else 619 asprintf(&tmp, "0x%lx", args[sc->offset]); 620 621 } 622 break; 623 } 624 return tmp; 625 } 626 627 #define timespecsubt(tvp, uvp, vvp) \ 628 do { \ 629 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 630 (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \ 631 if ((vvp)->tv_nsec < 0) { \ 632 (vvp)->tv_sec--; \ 633 (vvp)->tv_nsec += 1000000000; \ 634 } \ 635 } while (0) 636 637 /* 638 * print_syscall 639 * Print (to outfile) the system call and its arguments. Note that 640 * nargs is the number of arguments (not the number of words; this is 641 * potentially confusing, I know). 642 */ 643 644 void 645 print_syscall(struct trussinfo *trussinfo, const char *name, int nargs, char **s_args) { 646 int i; 647 int len = 0; 648 struct timespec timediff; 649 650 if (trussinfo->flags & FOLLOWFORKS) 651 len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid); 652 653 if (name != NULL && (!strcmp(name, "execve") || !strcmp(name, "exit"))) { 654 clock_gettime(CLOCK_REALTIME, &trussinfo->after); 655 } 656 657 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 658 timespecsubt(&trussinfo->after, &trussinfo->start_time, &timediff); 659 len += fprintf(trussinfo->outfile, "%ld.%09ld ", 660 (long)timediff.tv_sec, timediff.tv_nsec); 661 } 662 663 if (trussinfo->flags & RELATIVETIMESTAMPS) { 664 timespecsubt(&trussinfo->after, &trussinfo->before, &timediff); 665 len += fprintf(trussinfo->outfile, "%ld.%09ld ", 666 (long)timediff.tv_sec, timediff.tv_nsec); 667 } 668 669 len += fprintf(trussinfo->outfile, "%s(", name); 670 671 for (i = 0; i < nargs; i++) { 672 if (s_args[i]) 673 len += fprintf(trussinfo->outfile, "%s", s_args[i]); 674 else 675 len += fprintf(trussinfo->outfile, "<missing argument>"); 676 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : ""); 677 } 678 len += fprintf(trussinfo->outfile, ")"); 679 for (i = 0; i < 6 - (len / 8); i++) 680 fprintf(trussinfo->outfile, "\t"); 681 } 682 683 void 684 print_syscall_ret(struct trussinfo *trussinfo, const char *name, int nargs, 685 char **s_args, int errorp, long retval) 686 { 687 print_syscall(trussinfo, name, nargs, s_args); 688 if (errorp) { 689 fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval, strerror(retval)); 690 } else { 691 fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval, retval); 692 } 693 } 694