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