i386-linux.c revision 101374
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 33static const char rcsid[] = 34 "$FreeBSD: head/usr.bin/truss/i386-linux.c 101374 2002-08-05 13:23:41Z mdodd $"; 35#endif /* not lint */ 36 37/* 38 * Linux/i386-specific system call handling. Given how much of this code 39 * is taken from the freebsd equivalent, I can probably put even more of 40 * it in support routines that can be used by any personality support. 41 */ 42 43#include <sys/types.h> 44#include <sys/ioctl.h> 45#include <sys/pioctl.h> 46#include <sys/time.h> 47 48#include <machine/reg.h> 49#include <machine/psl.h> 50 51#include <errno.h> 52#include <fcntl.h> 53#include <signal.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57#include <unistd.h> 58 59#include "truss.h" 60#include "syscall.h" 61 62static int fd = -1; 63static int cpid = -1; 64extern int Procfd; 65 66#include "linux_syscalls.h" 67 68static int nsyscalls = 69 sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]); 70 71/* 72 * This is what this particular file uses to keep track of a system call. 73 * It is probably not quite sufficient -- I can probably use the same 74 * structure for the various syscall personalities, and I also probably 75 * need to nest system calls (for signal handlers). 76 * 77 * 'struct syscall' describes the system call; it may be NULL, however, 78 * if we don't know about this particular system call yet. 79 */ 80static struct linux_syscall { 81 struct syscall *sc; 82 char *name; 83 int number; 84 unsigned long args[5]; 85 int nargs; /* number of arguments -- *not* number of words! */ 86 char **s_args; /* the printable arguments */ 87} fsc; 88 89/* Clear up and free parts of the fsc structure. */ 90static __inline void 91clear_fsc() { 92 if (fsc.s_args) { 93 int i; 94 for (i = 0; i < fsc.nargs; i++) 95 if (fsc.s_args[i]) 96 free(fsc.s_args[i]); 97 free(fsc.s_args); 98 } 99 memset(&fsc, 0, sizeof(fsc)); 100} 101 102/* 103 * Called when a process has entered a system call. nargs is the 104 * number of words, not number of arguments (a necessary distinction 105 * in some cases). Note that if the STOPEVENT() code in i386/i386/trap.c 106 * is ever changed these functions need to keep up. 107 */ 108 109void 110i386_linux_syscall_entry(struct trussinfo *trussinfo, int nargs) { 111 char buf[32]; 112 struct reg regs = { 0 }; 113 int syscall; 114 int i; 115 struct syscall *sc; 116 117 if (fd == -1 || trussinfo->pid != cpid) { 118 sprintf(buf, "/proc/%d/regs", trussinfo->pid); 119 fd = open(buf, O_RDWR); 120 if (fd == -1) { 121 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); 122 return; 123 } 124 cpid = trussinfo->pid; 125 } 126 127 clear_fsc(); 128 lseek(fd, 0L, 0); 129 i = read(fd, ®s, sizeof(regs)); 130 syscall = regs.r_eax; 131 132 fsc.number = syscall; 133 fsc.name = 134 (syscall < 0 || syscall > nsyscalls) ? NULL : linux_syscallnames[syscall]; 135 if (!fsc.name) { 136 fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall); 137 } 138 139 if (fsc.name && (trussinfo->flags & FOLLOWFORKS) 140 && ((!strcmp(fsc.name, "linux_fork") 141 || !strcmp(fsc.name, "linux_vfork")))) 142 { 143 trussinfo->in_fork = 1; 144 } 145 146 if (nargs == 0) 147 return; 148 149 /* 150 * Linux passes syscall arguments in registers, not 151 * on the stack. Fortunately, we've got access to the 152 * register set. Note that we don't bother checking the 153 * number of arguments. And what does linux do for syscalls 154 * that have more than five arguments? 155 */ 156 157 fsc.args[0] = regs.r_ebx; 158 fsc.args[1] = regs.r_ecx; 159 fsc.args[2] = regs.r_edx; 160 fsc.args[3] = regs.r_esi; 161 fsc.args[4] = regs.r_edi; 162 163 sc = get_syscall(fsc.name); 164 if (sc) { 165 fsc.nargs = sc->nargs; 166 } else { 167#if DEBUG 168 fprintf(trussinfo->outfile, "unknown syscall %s -- setting args to %d\n", 169 fsc.name, nargs); 170#endif 171 fsc.nargs = nargs; 172 } 173 174 fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*)); 175 memset(fsc.s_args, 0, fsc.nargs * sizeof(char*)); 176 fsc.sc = sc; 177 178 /* 179 * At this point, we set up the system call arguments. 180 * We ignore any OUT ones, however -- those are arguments that 181 * are set by the system call, and so are probably meaningless 182 * now. This doesn't currently support arguments that are 183 * passed in *and* out, however. 184 */ 185 186 if (fsc.name) { 187 188#if DEBUG 189 fprintf(stderr, "syscall %s(", fsc.name); 190#endif 191 for (i = 0; i < fsc.nargs; i++) { 192#if DEBUG 193 fprintf(stderr, "0x%x%s", 194 sc 195 ? fsc.args[sc->args[i].offset] 196 : fsc.args[i], 197 i < (fsc.nargs - 1) ? "," : ""); 198#endif 199 if (sc && !(sc->args[i].type & OUT)) { 200 fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args); 201 } 202 } 203#if DEBUG 204 fprintf(stderr, ")\n"); 205#endif 206 } 207 208#if DEBUG 209 fprintf(trussinfo->outfile, "\n"); 210#endif 211 212 /* 213 * Some system calls should be printed out before they are done -- 214 * execve() and exit(), for example, never return. Possibly change 215 * this to work for any system call that doesn't have an OUT 216 * parameter? 217 */ 218 219 if (!strcmp(fsc.name, "linux_execve") || !strcmp(fsc.name, "exit")) { 220 221 /* XXX 222 * This could be done in a more general 223 * manner but it still wouldn't be very pretty. 224 */ 225 if (!strcmp(fsc.name, "linux_execve")) { 226 if ((trussinfo->flags & EXECVEARGS) == 0) 227 if (fsc.s_args[1]) { 228 free(fsc.s_args[1]); 229 fsc.s_args[1] = NULL; 230 } 231 if ((trussinfo->flags & EXECVEENVS) == 0) 232 if (fsc.s_args[2]) { 233 free(fsc.s_args[2]); 234 fsc.s_args[2] = NULL; 235 } 236 } 237 238 print_syscall(trussinfo, fsc.name, fsc.nargs, fsc.s_args); 239 fprintf(trussinfo->outfile, "\n"); 240 } 241 242 return; 243} 244 245/* 246 * Linux syscalls return negative errno's, we do positive and map them 247 */ 248const int bsd_to_linux_errno[] = { 249 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 250 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 251 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 252 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 253 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 254 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 255 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 256 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 257 -6, 258}; 259 260int 261i386_linux_syscall_exit(struct trussinfo *trussinfo, int syscall) { 262 char buf[32]; 263 struct reg regs; 264 int retval; 265 int i; 266 int errorp; 267 struct syscall *sc; 268 269 if (fd == -1 || trussinfo->pid != cpid) { 270 sprintf(buf, "/proc/%d/regs", trussinfo->pid); 271 fd = open(buf, O_RDONLY); 272 if (fd == -1) { 273 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); 274 return; 275 } 276 cpid = trussinfo->pid; 277 } 278 279 lseek(fd, 0L, 0); 280 if (read(fd, ®s, sizeof(regs)) != sizeof(regs)) { 281 fprintf(trussinfo->outfile, "\n"); 282 return; 283 } 284 retval = regs.r_eax; 285 errorp = !!(regs.r_eflags & PSL_C); 286 287 /* 288 * This code, while simpler than the initial versions I used, could 289 * stand some significant cleaning. 290 */ 291 292 sc = fsc.sc; 293 if (!sc) { 294 for (i = 0; i < fsc.nargs; i++) { 295 fsc.s_args[i] = malloc(12); 296 sprintf(fsc.s_args[i], "0x%lx", fsc.args[i]); 297 } 298 } else { 299 /* 300 * Here, we only look for arguments that have OUT masked in -- 301 * otherwise, they were handled in the syscall_entry function. 302 */ 303 for (i = 0; i < sc->nargs; i++) { 304 char *temp; 305 if (sc->args[i].type & OUT) { 306 /* 307 * If an error occurred, than don't bothe getting the data; 308 * it may not be valid. 309 */ 310 if (errorp) { 311 temp = malloc(12); 312 sprintf(temp, "0x%lx", fsc.args[sc->args[i].offset]); 313 } else { 314 temp = print_arg(Procfd, &sc->args[i], fsc.args); 315 } 316 fsc.s_args[i] = temp; 317 } 318 } 319 } 320 321 /* 322 * It would probably be a good idea to merge the error handling, 323 * but that complicates things considerably. 324 */ 325 if (errorp) { 326 for (i = 0; i < sizeof(bsd_to_linux_errno) / sizeof(int); i++) 327 if (retval == bsd_to_linux_errno[i]) 328 break; 329 } 330 print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, 331 errorp ? i : retval); 332 clear_fsc(); 333 334 return (retval); 335} 336