powerpc-freebsd.c revision 168569
1154047Sgrehan/* 2154047Sgrehan * Copyright 2006 Peter Grehan <grehan@freebsd.org> 3154047Sgrehan * Copryight 2005 Orlando Bassotto <orlando@break.net> 4154047Sgrehan * Copryight 1998 Sean Eric Fagan 5154047Sgrehan * 6154047Sgrehan * Redistribution and use in source and binary forms, with or without 7154047Sgrehan * modification, are permitted provided that the following conditions 8154047Sgrehan * are met: 9154047Sgrehan * 1. Redistributions of source code must retain the above copyright 10154047Sgrehan * notice, this list of conditions and the following disclaimer. 11154047Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 12154047Sgrehan * notice, this list of conditions and the following disclaimer in the 13154047Sgrehan * documentation and/or other materials provided with the distribution. 14154047Sgrehan * 15154047Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16154047Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17154047Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18154047Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19154047Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20154047Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21154047Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22154047Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23154047Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24154047Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25154047Sgrehan * SUCH DAMAGE. 26154047Sgrehan */ 27154047Sgrehan 28154047Sgrehan#ifndef lint 29154047Sgrehanstatic const char rcsid[] = 30154047Sgrehan "$FreeBSD: head/usr.bin/truss/powerpc-fbsd.c 168569 2007-04-10 04:03:34Z delphij $"; 31154047Sgrehan#endif /* not lint */ 32154047Sgrehan 33154047Sgrehan/* 34154047Sgrehan * FreeBSD/powerpc-specific system call handling. This is probably the most 35154047Sgrehan * complex part of the entire truss program, although I've got lots of 36154047Sgrehan * it handled relatively cleanly now. The system call names are generated 37154047Sgrehan * automatically, thanks to /usr/src/sys/kern/syscalls.master. The 38154047Sgrehan * names used for the various structures are confusing, I sadly admit. 39154047Sgrehan * 40154047Sgrehan * This file is almost nothing more than a slightly-edited i386-fbsd.c. 41154047Sgrehan */ 42154047Sgrehan 43154047Sgrehan#include <sys/types.h> 44168569Sdelphij#include <sys/ptrace.h> 45154047Sgrehan#include <sys/syscall.h> 46154047Sgrehan 47154047Sgrehan#include <machine/reg.h> 48154047Sgrehan#include <machine/frame.h> 49154047Sgrehan 50154047Sgrehan#include <err.h> 51154047Sgrehan#include <errno.h> 52154047Sgrehan#include <fcntl.h> 53154047Sgrehan#include <signal.h> 54154047Sgrehan#include <stdio.h> 55154047Sgrehan#include <stdlib.h> 56154047Sgrehan#include <string.h> 57154047Sgrehan#include <time.h> 58154047Sgrehan#include <unistd.h> 59154047Sgrehan 60154047Sgrehan#include "truss.h" 61154047Sgrehan#include "syscall.h" 62154047Sgrehan#include "extern.h" 63154047Sgrehan 64154047Sgrehanstatic int cpid = -1; 65154047Sgrehan 66154047Sgrehan#include "syscalls.h" 67154047Sgrehan 68154047Sgrehanstatic int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); 69154047Sgrehan 70154047Sgrehan/* 71154047Sgrehan * This is what this particular file uses to keep track of a system call. 72154047Sgrehan * It is probably not quite sufficient -- I can probably use the same 73154047Sgrehan * structure for the various syscall personalities, and I also probably 74154047Sgrehan * need to nest system calls (for signal handlers). 75154047Sgrehan * 76154047Sgrehan * 'struct syscall' describes the system call; it may be NULL, however, 77154047Sgrehan * if we don't know about this particular system call yet. 78154047Sgrehan */ 79154047Sgrehanstatic struct freebsd_syscall { 80154047Sgrehan struct syscall *sc; 81154047Sgrehan const char *name; 82154047Sgrehan int number; 83154047Sgrehan unsigned long *args; 84154047Sgrehan int nargs; /* number of arguments -- *not* number of words! */ 85154047Sgrehan char **s_args; /* the printable arguments */ 86154047Sgrehan} fsc; 87154047Sgrehan 88154047Sgrehan/* Clear up and free parts of the fsc structure. */ 89154047Sgrehanstatic __inline void 90154047Sgrehanclear_fsc(void) { 91154047Sgrehan if (fsc.args) { 92154047Sgrehan free(fsc.args); 93154047Sgrehan } 94154047Sgrehan if (fsc.s_args) { 95154047Sgrehan int i; 96154047Sgrehan for (i = 0; i < fsc.nargs; i++) 97154047Sgrehan if (fsc.s_args[i]) 98154047Sgrehan free(fsc.s_args[i]); 99154047Sgrehan free(fsc.s_args); 100154047Sgrehan } 101154047Sgrehan memset(&fsc, 0, sizeof(fsc)); 102154047Sgrehan} 103154047Sgrehan 104154047Sgrehan/* 105154047Sgrehan * Called when a process has entered a system call. nargs is the 106154047Sgrehan * number of words, not number of arguments (a necessary distinction 107154047Sgrehan * in some cases). Note that if the STOPEVENT() code in powerpc/powerpc/trap.c 108154047Sgrehan * is ever changed these functions need to keep up. 109154047Sgrehan */ 110154047Sgrehan 111154047Sgrehanvoid 112154047Sgrehanpowerpc_syscall_entry(struct trussinfo *trussinfo, int nargs) { 113154047Sgrehan char buf[32]; 114154047Sgrehan struct reg regs; 115154047Sgrehan void *args; 116154047Sgrehan int syscall_num; 117154047Sgrehan int i; 118154047Sgrehan unsigned int regargs; 119154047Sgrehan struct syscall *sc; 120154047Sgrehan 121168569Sdelphij cpid = trussinfo->curthread->tid; 122154047Sgrehan 123154047Sgrehan clear_fsc(); 124168569Sdelphij if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { 125154047Sgrehan fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); 126154047Sgrehan return; 127154047Sgrehan } 128154047Sgrehan 129154047Sgrehan /* 130154047Sgrehan * FreeBSD has two special kinds of system call redirctions -- 131154047Sgrehan * SYS_syscall, and SYS___syscall. The former is the old syscall() 132154047Sgrehan * routine, basicly; the latter is for quad-aligned arguments. 133154047Sgrehan */ 134154047Sgrehan regargs = NARGREG; 135154047Sgrehan syscall_num = regs.fixreg[0]; 136154047Sgrehan args = ®s.fixreg[3]; 137154047Sgrehan if (syscall_num == SYS_syscall) { 138154047Sgrehan args = ®s.fixreg[4]; 139154047Sgrehan regargs -= 1; 140154047Sgrehan syscall_num = regs.fixreg[3]; 141154047Sgrehan } else if (syscall_num == SYS___syscall) { 142154047Sgrehan args = ®s.fixreg[5]; 143154047Sgrehan regargs -= 2; 144154047Sgrehan syscall_num = regs.fixreg[4]; 145154047Sgrehan } 146154047Sgrehan 147154047Sgrehan fsc.number = syscall_num; 148154047Sgrehan fsc.name = 149154047Sgrehan (syscall_num < 0 || syscall_num > nsyscalls) ? NULL : syscallnames[syscall_num]; 150154047Sgrehan if (!fsc.name) { 151154047Sgrehan fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); 152154047Sgrehan } 153154047Sgrehan 154154047Sgrehan if (fsc.name && (trussinfo->flags & FOLLOWFORKS) 155154047Sgrehan && ((!strcmp(fsc.name, "fork") 156154047Sgrehan || !strcmp(fsc.name, "rfork") 157154047Sgrehan || !strcmp(fsc.name, "vfork")))) 158154047Sgrehan { 159168569Sdelphij trussinfo->curthread->in_fork = 1; 160154047Sgrehan } 161154047Sgrehan 162154047Sgrehan if (nargs == 0) 163154047Sgrehan return; 164154047Sgrehan 165154047Sgrehan fsc.args = malloc((1+nargs) * sizeof(unsigned long)); 166154047Sgrehan 167154047Sgrehan if (nargs > regargs) { 168168569Sdelphij struct ptrace_io_desc iorequest; 169154047Sgrehan memmove(&fsc.args[0], args, regargs * sizeof(fsc.args[0])); 170168569Sdelphij 171168569Sdelphij iorequest.piod_op = PIOD_READ_D; 172168569Sdelphij iorequest.piod_offs = (void *)(regs.fixreg[1] + 8); 173168569Sdelphij iorequest.piod_addr = &fsc.args[regargs]; 174168569Sdelphij iorequest.piod_len = (nargs - regargs) * sizeof(fsc.args[0]); 175168569Sdelphij ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); 176168569Sdelphij if (iorequest.piod_len == 0) 177168569Sdelphij return; 178154047Sgrehan } else { 179154047Sgrehan memmove(&fsc.args[0], args, nargs * sizeof(fsc.args[0])); 180154047Sgrehan } 181154047Sgrehan 182154047Sgrehan sc = get_syscall(fsc.name); 183154047Sgrehan if (sc) { 184154047Sgrehan fsc.nargs = sc->nargs; 185154047Sgrehan } else { 186154047Sgrehan#if DEBUG 187154047Sgrehan fprintf(trussinfo->outfile, "unknown syscall %s -- setting args to %d\n", 188154047Sgrehan fsc.name, nargs); 189154047Sgrehan#endif 190154047Sgrehan fsc.nargs = nargs; 191154047Sgrehan } 192154047Sgrehan 193154047Sgrehan fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*)); 194154047Sgrehan memset(fsc.s_args, 0, fsc.nargs * sizeof(char*)); 195154047Sgrehan fsc.sc = sc; 196154047Sgrehan 197154047Sgrehan /* 198154047Sgrehan * At this point, we set up the system call arguments. 199154047Sgrehan * We ignore any OUT ones, however -- those are arguments that 200154047Sgrehan * are set by the system call, and so are probably meaningless 201154047Sgrehan * now. This doesn't currently support arguments that are 202154047Sgrehan * passed in *and* out, however. 203154047Sgrehan */ 204154047Sgrehan 205154047Sgrehan if (fsc.name) { 206154047Sgrehan 207154047Sgrehan#if DEBUG 208154047Sgrehan fprintf(stderr, "syscall %s(", fsc.name); 209154047Sgrehan#endif 210154047Sgrehan for (i = 0; i < fsc.nargs; i++) { 211154047Sgrehan#if DEBUG 212154047Sgrehan fprintf(stderr, "0x%x%s", 213154047Sgrehan sc 214154047Sgrehan ? fsc.args[sc->args[i].offset] 215154047Sgrehan : fsc.args[i], 216154047Sgrehan i < (fsc.nargs - 1) ? "," : ""); 217154047Sgrehan#endif 218154047Sgrehan if (sc && !(sc->args[i].type & OUT)) { 219168569Sdelphij fsc.s_args[i] = print_arg(&sc->args[i], fsc.args, 0, trussinfo); 220154047Sgrehan } 221154047Sgrehan } 222154047Sgrehan#if DEBUG 223154047Sgrehan fprintf(stderr, ")\n"); 224154047Sgrehan#endif 225154047Sgrehan } 226154047Sgrehan 227154047Sgrehan#if DEBUG 228154047Sgrehan fprintf(trussinfo->outfile, "\n"); 229154047Sgrehan#endif 230154047Sgrehan 231154047Sgrehan /* 232154047Sgrehan * Some system calls should be printed out before they are done -- 233154047Sgrehan * execve() and exit(), for example, never return. Possibly change 234154047Sgrehan * this to work for any system call that doesn't have an OUT 235154047Sgrehan * parameter? 236154047Sgrehan */ 237154047Sgrehan 238154047Sgrehan if (fsc.name && (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit"))) { 239154047Sgrehan 240154047Sgrehan /* XXX 241154047Sgrehan * This could be done in a more general 242154047Sgrehan * manner but it still wouldn't be very pretty. 243154047Sgrehan */ 244154047Sgrehan if (!strcmp(fsc.name, "execve")) { 245154047Sgrehan if ((trussinfo->flags & EXECVEARGS) == 0) 246154047Sgrehan if (fsc.s_args[1]) { 247154047Sgrehan free(fsc.s_args[1]); 248154047Sgrehan fsc.s_args[1] = NULL; 249154047Sgrehan } 250154047Sgrehan if ((trussinfo->flags & EXECVEENVS) == 0) 251154047Sgrehan if (fsc.s_args[2]) { 252154047Sgrehan free(fsc.s_args[2]); 253154047Sgrehan fsc.s_args[2] = NULL; 254154047Sgrehan } 255154047Sgrehan } 256154047Sgrehan 257154047Sgrehan print_syscall(trussinfo, fsc.name, fsc.nargs, fsc.s_args); 258154047Sgrehan fprintf(trussinfo->outfile, "\n"); 259154047Sgrehan } 260154047Sgrehan 261154047Sgrehan return; 262154047Sgrehan} 263154047Sgrehan 264154047Sgrehan/* 265154047Sgrehan * And when the system call is done, we handle it here. 266154047Sgrehan * Currently, no attempt is made to ensure that the system calls 267154047Sgrehan * match -- this needs to be fixed (and is, in fact, why S_SCX includes 268154047Sgrehan * the sytem call number instead of, say, an error status). 269154047Sgrehan */ 270154047Sgrehan 271154047Sgrehanlong 272154047Sgrehanpowerpc_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) 273154047Sgrehan{ 274154047Sgrehan struct reg regs; 275154047Sgrehan long retval; 276154047Sgrehan int i; 277154047Sgrehan int errorp; 278154047Sgrehan struct syscall *sc; 279154047Sgrehan 280168569Sdelphij cpid = trussinfo->curthread->tid; 281154047Sgrehan 282168569Sdelphij if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { 283154047Sgrehan fprintf(trussinfo->outfile, "\n"); 284154047Sgrehan return (-1); 285154047Sgrehan } 286154047Sgrehan retval = regs.fixreg[3]; 287154047Sgrehan errorp = !!(regs.cr & 0x10000000); 288154047Sgrehan 289154047Sgrehan /* 290154047Sgrehan * This code, while simpler than the initial versions I used, could 291154047Sgrehan * stand some significant cleaning. 292154047Sgrehan */ 293154047Sgrehan 294154047Sgrehan sc = fsc.sc; 295154047Sgrehan if (!sc) { 296154047Sgrehan for (i = 0; i < fsc.nargs; i++) 297154047Sgrehan asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); 298154047Sgrehan } else { 299154047Sgrehan /* 300154047Sgrehan * On 32-bit big-endian, the low word of a 64-bit return is 301154047Sgrehan * in the greater address. Switch to this. XXX note that 302154047Sgrehan * print_syscall_ret can't handle 64-bit return values (llseek) 303154047Sgrehan */ 304154047Sgrehan if (sc->ret_type == 2) 305154047Sgrehan retval = regs.fixreg[4]; 306154047Sgrehan 307154047Sgrehan /* 308154047Sgrehan * Here, we only look for arguments that have OUT masked in -- 309154047Sgrehan * otherwise, they were handled in the syscall_entry function. 310154047Sgrehan */ 311154047Sgrehan for (i = 0; i < sc->nargs; i++) { 312154047Sgrehan char *temp; 313154047Sgrehan if (sc->args[i].type & OUT) { 314154047Sgrehan /* 315154047Sgrehan * If an error occurred, than don't bothe getting the data; 316154047Sgrehan * it may not be valid. 317154047Sgrehan */ 318154047Sgrehan if (errorp) 319154047Sgrehan asprintf(&temp, "0x%lx", fsc.args[sc->args[i].offset]); 320154047Sgrehan else 321168569Sdelphij temp = print_arg(&sc->args[i], fsc.args, retval, trussinfo); 322154047Sgrehan fsc.s_args[i] = temp; 323154047Sgrehan } 324154047Sgrehan } 325154047Sgrehan } 326154047Sgrehan 327154047Sgrehan /* 328154047Sgrehan * It would probably be a good idea to merge the error handling, 329154047Sgrehan * but that complicates things considerably. 330154047Sgrehan */ 331154047Sgrehan 332154047Sgrehan print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, retval); 333154047Sgrehan clear_fsc(); 334154047Sgrehan 335154047Sgrehan return (retval); 336154047Sgrehan} 337