aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/linux-gnu/aarch64/trace.c
blob: 4f3111302a6bb9b6490847b0bcb6d5048a4bdbd8 (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
/*
 * This file is part of ltrace.
 * Copyright (C) 2014 Petr Machata, Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <asm/ptrace.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include "backend.h"
#include "proc.h"

void
get_arch_dep(struct process *proc)
{
}

int aarch64_read_gregs(struct process *proc, struct user_pt_regs *regs);

/* The syscall instruction is:
 * | 31                   21 | 20    5 | 4       0 |
 * | 1 1 0 1 0 1 0 0 | 0 0 0 |  imm16  | 0 0 0 0 1 | */
#define SVC_MASK  0xffe0001f
#define SVC_VALUE 0xd4000001

int
syscall_p(struct process *proc, int status, int *sysnum)
{
	if (WIFSTOPPED(status)
	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {

		struct user_pt_regs regs;
		if (aarch64_read_gregs(proc, &regs) < 0) {
			fprintf(stderr, "syscall_p: "
				"Couldn't read registers of %d.\n", proc->pid);
			return -1;
		}

		errno = 0;
		unsigned long insn = (unsigned long) ptrace(PTRACE_PEEKTEXT,
							    proc->pid,
							    regs.pc - 4, 0);
		if (insn == -1UL && errno != 0) {
			fprintf(stderr, "syscall_p: "
				"Couldn't peek into %d: %s\n", proc->pid,
				strerror(errno));
			return -1;
		}

		insn &= 0xffffffffUL;
		if ((insn & SVC_MASK) == SVC_VALUE) {
			*sysnum = regs.regs[8];

			size_t d1 = proc->callstack_depth - 1;
			if (proc->callstack_depth > 0
			    && proc->callstack[d1].is_syscall
			    && proc->callstack[d1].c_un.syscall == *sysnum)
				return 2;

			return 1;
		}
	}

	return 0;
}