trace.c revision 8f8282f72eaeadc5419cd5470100e8dcaba5b7fd
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
24syscall_p(struct process * proc, int status, int * sysnum) {
25	int depth;
26
27	if (WIFSTOPPED(status) && WSTOPSIG(status)==SIGTRAP) {
28		*sysnum = ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_ORIG_D0, 0);
29		if (*sysnum == -1) return 0;
30		if (*sysnum>=0) {
31			depth = proc->callstack_depth;
32			if (depth>0 &&
33					proc->callstack[depth-1].is_syscall &&
34					proc->callstack[depth-1].c_un.syscall==*sysnum) {
35				return 2;
36			} else {
37				return 1;
38			}
39		}
40	}
41	return 0;
42}
43
44long
45gimme_arg(enum tof type, struct process * proc, int arg_num) {
46	if (arg_num==-1) {		/* return value */
47		return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D0, 0);
48	}
49
50	if (type==LT_TOF_FUNCTION) {
51		return ptrace(PTRACE_PEEKTEXT, proc->pid, proc->stack_pointer+4*(arg_num+1), 0);
52	} else if (type==LT_TOF_SYSCALL) {
53#if 0
54		switch(arg_num) {
55			case 0:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D1, 0);
56			case 1:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D2, 0);
57			case 2:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D3, 0);
58			case 3:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D4, 0);
59			case 4:	return ptrace(PTRACE_PEEKUSER, proc->pid, 4*PT_D5, 0);
60			default:
61				fprintf(stderr, "gimme_arg called with wrong arguments\n");
62				exit(2);
63		}
64#else
65		/* That hack works on m68k, too */
66		return ptrace(PTRACE_PEEKUSER, proc->pid, 4*arg_num, 0);
67#endif
68	} else {
69		fprintf(stderr, "gimme_arg called with wrong arguments\n");
70		exit(1);
71	}
72
73	return 0;
74}
75