aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/linux-gnu/s390/trace.c
blob: 0e93f39e744ca6c1e2bc80d18ceb996a50c9d557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
** S390 specific part of trace.c
**
** Other routines are in ../trace.c and need to be combined
** at link time with this code.
**
** Copyright (C) 2001,2005 IBM Corp.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <asm/ptrace.h>

#include "ltrace.h"

#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
# define PTRACE_PEEKUSER PTRACE_PEEKUSR
#endif

#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
# define PTRACE_POKEUSER PTRACE_POKEUSR
#endif

void get_arch_dep(struct process *proc)
{
#ifdef __s390x__
	unsigned long psw;

	if (proc->arch_ptr)
		return;

	psw = ptrace(PTRACE_PEEKUSER, proc->pid, PT_PSWMASK, 0);

	if ((psw & 0x000000180000000) == 0x000000080000000) {
		proc->mask_32bit = 1;
		proc->personality = 1;
	}

	proc->arch_ptr = (void *)1;
#endif
}

/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
 */
int syscall_p(struct process *proc, int status, int *sysnum)
{
	long pc, opcode, offset_reg, scno, tmp;
	void *svc_addr;
	int gpr_offset[16] = { PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
		PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
		PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
		PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
	};

	if (WIFSTOPPED(status)
	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {

		/*
		 * If we have PTRACE_O_TRACESYSGOOD and we have the new style
		 * of passing the system call number to user space via PT_GPR2
		 * then the task is quite easy.
		 */

		*sysnum = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR2, 0);

		if (proc->tracesysgood) {
			/* System call was encountered... */
			if (proc->callstack_depth > 0 &&
			    proc->callstack[proc->callstack_depth -
					    1].is_syscall) {
				/* syscall exit */
				*sysnum =
				    proc->callstack[proc->callstack_depth -
						    1].c_un.syscall;
				return 2;
			} else {
				/* syscall enter */
				if (*sysnum != -ENOSYS)
					return 1;
			}
		}

		/*
		 * At least one of the two requirements mentioned above is not
		 * met. Therefore the fun part starts here:
		 * We try to do some instruction decoding without even knowing
		 * the instruction code length of the last instruction executed.
		 * Needs to be done to get the system call number or to decide
		 * if we reached a breakpoint or even checking for a completely
		 * unrelated instruction.
		 * Just a heuristic that most of the time appears to work...
		 */

		pc = ptrace(PTRACE_PEEKUSER, proc->pid, PT_PSWADDR, 0);
		opcode = ptrace(PTRACE_PEEKTEXT, proc->pid,
				(char *)(pc - sizeof(long)), 0);

		if ((opcode & 0xffff) == 0x0001) {
			/* Breakpoint */
			return 0;
		} else if ((opcode & 0xff00) == 0x0a00) {
			/* SVC opcode */
			scno = opcode & 0xff;
		} else if ((opcode & 0xff000000) == 0x44000000) {
			/* Instruction decoding of EXECUTE... */
			svc_addr = (void *)(opcode & 0xfff);

			offset_reg = (opcode & 0x000f0000) >> 16;
			if (offset_reg)
				svc_addr += ptrace(PTRACE_PEEKUSER, proc->pid,
						   gpr_offset[offset_reg], 0);

			offset_reg = (opcode & 0x0000f000) >> 12;
			if (offset_reg)
				svc_addr += ptrace(PTRACE_PEEKUSER, proc->pid,
						   gpr_offset[offset_reg], 0);

			scno = ptrace(PTRACE_PEEKTEXT, proc->pid, svc_addr, 0);
#ifdef __s390x__
			scno >>= 48;
#else
			scno >>= 16;
#endif
			if ((scno & 0xff00) != 0x0a000)
				return 0;

			tmp = 0;
			offset_reg = (opcode & 0x00f00000) >> 20;
			if (offset_reg)
				tmp = ptrace(PTRACE_PEEKUSER, proc->pid,
					     gpr_offset[offset_reg], 0);

			scno = (scno | tmp) & 0xff;
		} else {
			/* No opcode related to syscall handling */
			return 0;
		}

		if (scno == 0)
			scno = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR1, 0);

		*sysnum = scno;

		/* System call was encountered... */
		if (proc->callstack_depth > 0 &&
		    proc->callstack[proc->callstack_depth - 1].is_syscall) {
			return 2;
		} else {
			return 1;
		}
	}
	/* Unknown status... */
	return 0;
}

long gimme_arg(enum tof type, struct process *proc, arg_type_info *info)
{
	int arg_num = info->arg_num;
	long ret;

	switch (arg_num) {
	case -1:		/* return value */
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR2, 0);
		break;
	case 0:
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_ORIGGPR2, 0);
		break;
	case 1:
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR3, 0);
		break;
	case 2:
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR4, 0);
		break;
	case 3:
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR5, 0);
		break;
	case 4:
		ret = ptrace(PTRACE_PEEKUSER, proc->pid, PT_GPR6, 0);
		break;
	default:
		fprintf(stderr, "gimme_arg called with wrong arguments\n");
		exit(2);
	}
#ifdef __s390x__
	if (proc->mask_32bit)
		ret &= 0xffffffff;
#endif
	return ret;
}

void save_register_args(enum tof type, struct process *proc)
{
}