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