blob: 95d2c4786b05340107c708a6b63e12954e2fc1ec [file] [log] [blame]
Juan Cespedes8e3e0821998-09-24 13:49:55 +02001#include <sys/types.h>
2#include <sys/wait.h>
3#include <signal.h>
4#include <sys/ptrace.h>
5#include <asm/ptrace.h>
6
7#include "ltrace.h"
8
9#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
10# define PTRACE_PEEKUSER PTRACE_PEEKUSR
11#endif
12
13#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
14# define PTRACE_POKEUSER PTRACE_POKEUSR
15#endif
16
17/* syscall tracing protocol: ArmLinux
18 on the way in, ip is 0
19 on the way out, ip is non-zero
20*/
21#define off_r0 0
22#define off_ip 48
23#define off_pc 60
24
25/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
26 */
27int syscall_p(struct process * proc, int status, int * sysnum)
28{
29 if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) {
30 /* get the user's pc (plus 8) */
31 int pc = ptrace(PTRACE_PEEKUSER, proc->pid, off_pc, 0);
32 /* fetch the SWI instruction */
33 int insn = ptrace(PTRACE_PEEKTEXT, proc->pid, pc-4, 0) ;
34
35 *sysnum = insn & 0xFFFF;
36 /* if it is a syscall, return 1 or 2 */
37 if ((insn & 0xFFFF0000) == 0xef900000) {
38 return ptrace(PTRACE_PEEKUSER, proc->pid, off_ip, 0) ? 2 : 1;
39 }
40 }
41 return 0;
42}
43
44
45void continue_after_breakpoint(struct process *proc, struct breakpoint * sbp, int delete_it)
46{
47 delete_breakpoint(proc->pid, sbp);
48 ptrace(PTRACE_POKEUSER, proc->pid, off_pc, sbp->addr);
49 if (delete_it) {
50 continue_process(proc->pid);
51 } else {
52 proc->breakpoint_being_enabled = sbp;
53 ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0);
54 }
55}
56
57long gimme_arg(enum tof type, struct process * proc, int arg_num)
58{
59 if (arg_num==-1) { /* return value */
60 return ptrace(PTRACE_PEEKUSER, proc->pid, off_r0, 0);
61 }
62
63 /* deal with the ARM calling conventions */
64 if (type==LT_TOF_FUNCTION) {
65 if (arg_num<4) {
66 return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
67 } else {
68 return ptrace(PTRACE_PEEKDATA, proc->pid, proc->stack_pointer+4*(arg_num-4), 0);
69 }
70 } else if (type==LT_TOF_SYSCALL) {
71 if (arg_num<5) {
72 return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
73 } else {
74 return ptrace(PTRACE_PEEKDATA, proc->pid, proc->stack_pointer+4*(arg_num-5), 0);
75 }
76 } else {
77 fprintf(stderr, "gimme_arg called with wrong arguments\n");
78 exit(1);
79 }
80
81 return 0;
82}
83
84int umovestr(struct process * proc, void * addr, int len, void * laddr)
85{
86 long a;
87 int i;
88 int offset=0;
89
90 while(offset<len) {
91 a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr+offset, 0);
92 for(i=0; i<sizeof(long); i++) {
93 if (((char*)&a)[i] && offset+i < len) {
94 *(char *)(laddr+offset+i) = ((char*)&a)[i];
95 } else {
96 *(char *)(laddr+offset+i) = '\0';
97 return 0;
98 }
99 }
100 offset += sizeof(long);
101 }
102 *(char *)(laddr+offset) = '\0';
103 return 0;
104}