trace.c revision 1b9cfd6ad305ad909e8ff17139111a7c78f01464
1#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <signal.h>
8#include <sys/ptrace.h>
9#include <asm/ptrace.h>
10
11#include "ltrace.h"
12
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
21/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
22 */
23int syscall_p(struct process * proc, int status, int * sysnum)
24{
25	if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) {
26		*sysnum = ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_ORIG_D0, 0);
27		if (*sysnum == -1) return 0;
28		if (*sysnum>=0) {
29			if (proc->current_syscall!=*sysnum) {
30				return 1;
31			} else {
32				return 2;
33			}
34		}
35	}
36	return 0;
37}
38
39void continue_after_breakpoint(struct process *proc, struct breakpoint * sbp, int delete_it)
40{
41	delete_breakpoint(proc->pid, sbp);
42	ptrace(PTRACE_POKEUSER, proc->pid, 4*PT_PC, sbp->addr);
43	if (delete_it) {
44		continue_process(proc->pid);
45	} else {
46		proc->breakpoint_being_enabled = sbp;
47		ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0);
48	}
49}
50
51long gimme_arg(enum tof type, struct process * proc, int arg_num)
52{
53	if (arg_num==-1) {		/* return value */
54		return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D0, 0);
55	}
56
57	if (type==LT_TOF_FUNCTION) {
58		return ptrace(PTRACE_PEEKTEXT, proc->pid, proc->stack_pointer+4*(arg_num+1), 0);
59	} else if (type==LT_TOF_SYSCALL) {
60#if 0
61		switch(arg_num) {
62			case 0:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D1, 0);
63			case 1:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D2, 0);
64			case 2:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D3, 0);
65			case 3:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D4, 0);
66			case 4:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D5, 0);
67			default:
68				fprintf(stderr, "gimme_arg called with wrong arguments\n");
69				exit(2);
70		}
71#else
72		/* That hack works on m68k, too */
73		return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
74#endif
75	} else {
76		fprintf(stderr, "gimme_arg called with wrong arguments\n");
77		exit(1);
78	}
79
80	return 0;
81}
82
83int umovestr(struct process * proc, void * addr, int len, void * laddr)
84{
85	long a;
86	int i;
87	int offset=0;
88
89	while(offset<len) {
90		a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr+offset, 0);
91		for(i=0; i<sizeof(long); i++) {
92			if (((char*)&a)[i] && offset+i < len) {
93				*(char *)(laddr+offset+i) = ((char*)&a)[i];
94			} else {
95				*(char *)(laddr+offset+i) = '\0';
96				return 0;
97			}
98		}
99		offset += sizeof(long);
100	}
101	*(char *)(laddr+offset) = '\0';
102	return 0;
103}
104