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