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