| Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame^] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <signal.h> |
| 8 | #include <sys/ptrace.h> |
| 9 | #include <asm/ptrace.h> |
| 10 | #include "debug.h" |
| 11 | #include "ltrace.h" |
| 12 | #include "mipsel.h" |
| 13 | #if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR)) |
| 14 | # define PTRACE_PEEKUSER PTRACE_PEEKUSR |
| 15 | #endif |
| 16 | |
| 17 | #if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR)) |
| 18 | # define PTRACE_POKEUSER PTRACE_POKEUSR |
| 19 | #endif |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | \addtogroup mipsel Mipsel specific functions. |
| 24 | |
| 25 | These are the functions that it looks like I need to implement in |
| 26 | order to get ltrace to work on our target. |
| 27 | |
| 28 | @{ |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | \param proc The process that had an event. |
| 33 | |
| 34 | Called by \c wait_for_something() right after the return from wait. |
| 35 | |
| 36 | Most targets just return here. A couple use proc->arch_ptr for a |
| 37 | private data area. |
| 38 | */ |
| 39 | void get_arch_dep(struct process *proc) |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | \param proc Process that had event. |
| 46 | \param status From \c wait() |
| 47 | \param sysnum 0-based syscall number. |
| 48 | \return 1 if syscall, 2 if sysret, 0 otherwise. |
| 49 | |
| 50 | Called by \c wait_for_something() after the call to get_arch_dep() |
| 51 | |
| 52 | It seems that the ptrace call trips twice on a system call, once |
| 53 | just before the system call and once when it returns. Both times, |
| 54 | the pc points at the instruction just after the mipsel "syscall" |
| 55 | instruction. |
| 56 | |
| 57 | There are several possiblities for system call sets, each is offset |
| 58 | by a base from the others. On our system, it looks like the base |
| 59 | for the system calls is 4000. |
| 60 | */ |
| 61 | int syscall_p(struct process *proc, int status, int *sysnum) |
| 62 | { |
| 63 | if (WIFSTOPPED(status) |
| 64 | && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { |
| 65 | /* get the user's pc (plus 8) */ |
| 66 | long pc = (long)get_instruction_pointer(proc); |
| 67 | /* fetch the SWI instruction */ |
| 68 | int insn = ptrace(PTRACE_PEEKTEXT, proc->pid, pc - 4, 0); |
| 69 | int num = ptrace(PTRACE_PEEKTEXT, proc->pid, pc - 8, 0); |
| 70 | |
| 71 | /* |
| 72 | On a mipsel, syscall looks like: |
| 73 | 24040fa1 li v0, 0x0fa1 # 4001 --> _exit syscall |
| 74 | 0000000c syscall |
| 75 | */ |
| 76 | if(insn!=0x0000000c){ |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | *sysnum = (num & 0xFFFF) - 4000; |
| 81 | /* if it is a syscall, return 1 or 2 */ |
| 82 | if (proc->callstack_depth > 0 && |
| 83 | proc->callstack[proc->callstack_depth - 1].is_syscall) { |
| 84 | return 2; |
| 85 | } |
| 86 | |
| 87 | if (*sysnum >= 0) { |
| 88 | return 1; |
| 89 | } |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | /** |
| 94 | \param type Function/syscall call or return. |
| 95 | \param proc The process that had an event. |
| 96 | \param arg_num -1 for return value, |
| 97 | \return The argument to fetch. |
| 98 | |
| 99 | A couple of assumptions. |
| 100 | |
| 101 | - Type is LT_TOF_FUNCTIONR or LT_TOF_SYSCALLR if arg_num==-1. These |
| 102 | types are only used in calls for output_right(), which only uses -1 |
| 103 | for arg_num. |
| 104 | - Type is LT_TOF_FUNCTION or LT_TOF_SYSCALL for args 0...4. |
| 105 | - I'm only displaying the first 4 args (Registers a0..a3). Good |
| 106 | enough for now. |
| 107 | |
| 108 | Mipsel conventions seem to be: |
| 109 | - syscall parameters: r4...r9 |
| 110 | - syscall return: if(!a3){ return v0;} else{ errno=v0;return -1;} |
| 111 | - function call: r4..r7. Not sure how to get arg number 5. |
| 112 | - function return: v0 |
| 113 | |
| 114 | The argument registers are wiped by a call, so it is a mistake to ask |
| 115 | for arguments on a return. If ltrace does this, we will need to cache |
| 116 | arguments somewhere on the call. |
| 117 | |
| 118 | I'm not doing any floating point support here. |
| 119 | |
| 120 | */ |
| 121 | long gimme_arg(enum tof type, struct process *proc, int arg_num) |
| 122 | { |
| 123 | long ret; |
| 124 | debug(2,"type %d arg %d",type,arg_num); |
| 125 | if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL){ |
| 126 | if(arg_num <4){ |
| 127 | ret=ptrace(PTRACE_PEEKUSER,proc->pid,off_a0+arg_num,0); |
| 128 | debug(2,"ret = %#lx",ret); |
| 129 | return ret; |
| 130 | } else { |
| 131 | // If we need this, I think we can look at [sp+16] for arg_num==4. |
| 132 | CP; |
| 133 | return 0; |
| 134 | } |
| 135 | } |
| 136 | if(arg_num>=0){ |
| 137 | fprintf(stderr,"args on return?"); |
| 138 | } |
| 139 | if(type == LT_TOF_FUNCTIONR) { |
| 140 | return ptrace(PTRACE_PEEKUSER,proc->pid,off_v0,0); |
| 141 | } |
| 142 | if (type == LT_TOF_SYSCALLR) { |
| 143 | unsigned a3=ptrace(PTRACE_PEEKUSER, proc->pid,off_a3,0); |
| 144 | unsigned v0=ptrace(PTRACE_PEEKUSER, proc->pid,off_v0,0); |
| 145 | if(!a3){ |
| 146 | return v0; |
| 147 | } |
| 148 | return -1; |
| 149 | } |
| 150 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | \param type Type of call/return |
| 156 | \param proc Process to work with. |
| 157 | |
| 158 | Called by \c output_left(), which is called on a syscall or |
| 159 | function. |
| 160 | |
| 161 | The other architectures stub this out, but seems to be the place to |
| 162 | stash off the arguments on a call so we have them on the return. |
| 163 | |
| 164 | */ |
| 165 | void save_register_args(enum tof type, struct process *proc) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | /**@}*/ |