Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <signal.h> |
| 8 | #include <sys/ptrace.h> |
Juan Cespedes | 64c6dfb | 1998-07-14 13:49:47 +0200 | [diff] [blame] | 9 | #include <asm/ptrace.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 10 | |
| 11 | #include "ltrace.h" |
| 12 | |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 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 | |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 21 | /* Returns 1 if syscall, 2 if sysret, 0 otherwise. |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 22 | */ |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 23 | int syscall_p(struct process * proc, int status, int * sysnum) |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 24 | { |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 25 | int depth; |
| 26 | |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 27 | if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) { |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 28 | *sysnum = ptrace(PTRACE_PEEKUSER, proc->pid, 4*ORIG_EAX, 0); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 29 | if (*sysnum>=0) { |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 30 | depth = proc->callstack_depth; |
| 31 | if (depth>0 && |
| 32 | proc->callstack[depth-1].is_syscall && |
| 33 | proc->callstack[depth-1].c_un.syscall==*sysnum) { |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 34 | return 2; |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 35 | } else { |
| 36 | return 1; |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 37 | } |
Juan Cespedes | f0fdae9 | 1998-03-11 00:03:00 +0100 | [diff] [blame] | 38 | } |
| 39 | } |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 40 | return 0; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 43 | void continue_after_breakpoint(struct process *proc, struct breakpoint * sbp) |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 44 | { |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 45 | if (sbp->enabled) disable_breakpoint(proc->pid, sbp); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 46 | ptrace(PTRACE_POKEUSER, proc->pid, 4*EIP, sbp->addr); |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame^] | 47 | if (sbp->enabled == 0) { |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 48 | continue_process(proc->pid); |
| 49 | } else { |
| 50 | proc->breakpoint_being_enabled = sbp; |
| 51 | ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | long gimme_arg(enum tof type, struct process * proc, int arg_num) |
| 56 | { |
| 57 | if (arg_num==-1) { /* return value */ |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 58 | return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EAX, 0); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (type==LT_TOF_FUNCTION) { |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 62 | return ptrace(PTRACE_PEEKTEXT, proc->pid, proc->stack_pointer+4*(arg_num+1), 0); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 63 | } else if (type==LT_TOF_SYSCALL) { |
| 64 | #if 0 |
| 65 | switch(arg_num) { |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 66 | case 0: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EBX, 0); |
| 67 | case 1: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*ECX, 0); |
| 68 | case 2: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EDX, 0); |
| 69 | case 3: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*ESI, 0); |
| 70 | case 4: return ptrace(PTRACE_PEEKUSER, proc->pid, 4*EDI, 0); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 71 | default: |
| 72 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 73 | exit(2); |
| 74 | } |
| 75 | #else |
Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 76 | return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 77 | #endif |
| 78 | } else { |
| 79 | fprintf(stderr, "gimme_arg called with wrong arguments\n"); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int umovestr(struct process * proc, void * addr, int len, void * laddr) |
| 87 | { |
| 88 | long a; |
| 89 | int i; |
| 90 | int offset=0; |
| 91 | |
| 92 | while(offset<len) { |
| 93 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr+offset, 0); |
| 94 | for(i=0; i<sizeof(long); i++) { |
| 95 | if (((char*)&a)[i] && offset+i < len) { |
| 96 | *(char *)(laddr+offset+i) = ((char*)&a)[i]; |
| 97 | } else { |
| 98 | *(char *)(laddr+offset+i) = '\0'; |
| 99 | return 0; |
| 100 | } |
| 101 | } |
| 102 | offset += sizeof(long); |
| 103 | } |
| 104 | *(char *)(laddr+offset) = '\0'; |
| 105 | return 0; |
| 106 | } |