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