Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame^] | 1 | #include <sys/types.h> |
| 2 | #include <sys/wait.h> |
| 3 | #include <signal.h> |
| 4 | #include <sys/ptrace.h> |
| 5 | |
| 6 | #include "ltrace.h" |
| 7 | |
| 8 | /* Returns syscall number if `pid' stopped because of a syscall. |
| 9 | * Returns -1 otherwise |
| 10 | */ |
| 11 | int syscall_p(pid_t pid, int status) |
| 12 | { |
| 13 | if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) { |
| 14 | int tmp = ptrace(PTRACE_PEEKUSER, pid, 4*ORIG_EAX); |
| 15 | if (tmp>=0) { |
| 16 | return tmp; |
| 17 | } |
| 18 | } |
| 19 | return -1; |
| 20 | } |
| 21 | |
| 22 | void continue_after_breakpoint(struct process *proc, struct breakpoint * sbp, int delete_it) |
| 23 | { |
| 24 | delete_breakpoint(proc->pid, sbp); |
| 25 | ptrace(PTRACE_POKEUSER, proc->pid, 4*EIP, sbp->addr); |
| 26 | if (delete_it) { |
| 27 | continue_process(proc->pid); |
| 28 | } else { |
| 29 | proc->breakpoint_being_enabled = sbp; |
| 30 | ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | long gimme_arg(enum tof type, struct process * proc, int arg_num) |
| 35 | { |
| 36 | if (arg_num==-1) { /* return value */ |
| 37 | return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EAX); |
| 38 | } |
| 39 | |
| 40 | if (type==LT_TOF_FUNCTION) { |
| 41 | return ptrace(PTRACE_PEEKTEXT, proc->pid, proc->stack_pointer+4*(arg_num+1)); |
| 42 | } else if (type==LT_TOF_SYSCALL) { |
| 43 | #if 0 |
| 44 | switch(arg_num) { |
| 45 | case 0: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EBX); |
| 46 | case 1: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*ECX); |
| 47 | case 2: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EDX); |
| 48 | case 3: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*ESI); |
| 49 | case 4: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EDI); |
| 50 | default: |
| 51 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 52 | exit(2); |
| 53 | } |
| 54 | #else |
| 55 | return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num); |
| 56 | #endif |
| 57 | } else { |
| 58 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int umovestr(struct process * proc, void * addr, int len, void * laddr) |
| 66 | { |
| 67 | long a; |
| 68 | int i; |
| 69 | int offset=0; |
| 70 | |
| 71 | while(offset<len) { |
| 72 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr+offset, 0); |
| 73 | for(i=0; i<sizeof(long); i++) { |
| 74 | if (((char*)&a)[i] && offset+i < len) { |
| 75 | *(char *)(laddr+offset+i) = ((char*)&a)[i]; |
| 76 | } else { |
| 77 | *(char *)(laddr+offset+i) = '\0'; |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | offset += sizeof(long); |
| 82 | } |
| 83 | *(char *)(laddr+offset) = '\0'; |
| 84 | return 0; |
| 85 | } |