1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * make sure PEEKUSER matches GETREGS
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2008 Analog Devices Inc.
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * Licensed under the GPL-2 or later
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdbool.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ptrace.h>
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "spawn_ptrace_child.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "ptrace04";
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker static void cleanup();
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #define R(r) { .name = "PT_" #r, .off = PT_##r },
26*49cdfc7eSAndroid Build Coastguard Worker static struct {
27*49cdfc7eSAndroid Build Coastguard Worker const char *name;
28*49cdfc7eSAndroid Build Coastguard Worker long off;
29*49cdfc7eSAndroid Build Coastguard Worker } regs[] = {
30*49cdfc7eSAndroid Build Coastguard Worker #ifdef __bfin__
31*49cdfc7eSAndroid Build Coastguard Worker R(ORIG_R0) R(ORIG_P0)
32*49cdfc7eSAndroid Build Coastguard Worker R(R0) R(R1) R(R2) R(R3) R(R4) R(R5) R(R6) R(R7)
33*49cdfc7eSAndroid Build Coastguard Worker R(P0) R(P1) R(P2) R(P3) R(P4) R(P5) R(FP) R(USP)
34*49cdfc7eSAndroid Build Coastguard Worker R(I0) R(I1) R(I2) R(I3)
35*49cdfc7eSAndroid Build Coastguard Worker R(M0) R(M1) R(M2) R(M3)
36*49cdfc7eSAndroid Build Coastguard Worker R(L0) R(L1) R(L2) R(L3)
37*49cdfc7eSAndroid Build Coastguard Worker R(B0) R(B1) R(B2) R(B3)
38*49cdfc7eSAndroid Build Coastguard Worker R(A0X) R(A0W) R(A1X) R(A1W)
39*49cdfc7eSAndroid Build Coastguard Worker R(LC0) R(LC1) R(LT0) R(LT1) R(LB0) R(LB1)
40*49cdfc7eSAndroid Build Coastguard Worker R(ASTAT)
41*49cdfc7eSAndroid Build Coastguard Worker R(RETS) R(PC) R(RETX) R(RETN) R(RETE)
42*49cdfc7eSAndroid Build Coastguard Worker R(SEQSTAT) R(IPEND) R(SYSCFG)
43*49cdfc7eSAndroid Build Coastguard Worker #endif
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 2;
47*49cdfc7eSAndroid Build Coastguard Worker
compare_registers(unsigned char poison)48*49cdfc7eSAndroid Build Coastguard Worker void compare_registers(unsigned char poison)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_STRUCT_PTRACE_REGS) && defined(PTRACE_GETREGS)
51*49cdfc7eSAndroid Build Coastguard Worker ptrace_regs _pt_regs;
52*49cdfc7eSAndroid Build Coastguard Worker size_t i;
53*49cdfc7eSAndroid Build Coastguard Worker long ret;
54*49cdfc7eSAndroid Build Coastguard Worker bool failed = false;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker memset(&_pt_regs, poison, sizeof(_pt_regs));
57*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
58*49cdfc7eSAndroid Build Coastguard Worker ret = ptrace(PTRACE_GETREGS, pid, NULL, &_pt_regs);
59*49cdfc7eSAndroid Build Coastguard Worker if (ret && errno) {
60*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TERRNO, "PTRACE_GETREGS failed");
61*49cdfc7eSAndroid Build Coastguard Worker } else {
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(regs); ++i) {
64*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
65*49cdfc7eSAndroid Build Coastguard Worker ret = ptrace(PTRACE_PEEKUSER, pid,
66*49cdfc7eSAndroid Build Coastguard Worker (void *)regs[i].off, NULL);
67*49cdfc7eSAndroid Build Coastguard Worker if (ret && errno) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TERRNO,
69*49cdfc7eSAndroid Build Coastguard Worker "PTRACE_PEEKUSER: register %s "
70*49cdfc7eSAndroid Build Coastguard Worker "(offset %li) failed",
71*49cdfc7eSAndroid Build Coastguard Worker regs[i].name, regs[i].off);
72*49cdfc7eSAndroid Build Coastguard Worker failed = true;
73*49cdfc7eSAndroid Build Coastguard Worker continue;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker long *pt_val = (void *)&_pt_regs + regs[i].off;
77*49cdfc7eSAndroid Build Coastguard Worker if (*pt_val != ret) {
78*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
79*49cdfc7eSAndroid Build Coastguard Worker "register %s (offset %li) did not "
80*49cdfc7eSAndroid Build Coastguard Worker "match\n\tGETREGS: 0x%08lx "
81*49cdfc7eSAndroid Build Coastguard Worker "PEEKUSER: 0x%08lx",
82*49cdfc7eSAndroid Build Coastguard Worker regs[i].name, regs[i].off, *pt_val,
83*49cdfc7eSAndroid Build Coastguard Worker ret);
84*49cdfc7eSAndroid Build Coastguard Worker failed = true;
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker tst_resm((failed ? TFAIL : TPASS),
92*49cdfc7eSAndroid Build Coastguard Worker "PTRACE PEEKUSER/GETREGS (poison 0x%02x)", poison);
93*49cdfc7eSAndroid Build Coastguard Worker #else
94*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, cleanup, "System doesn't have ptrace_regs structure");
95*49cdfc7eSAndroid Build Coastguard Worker #endif
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])98*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (ARRAY_SIZE(regs) == 0)
103*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, NULL, "test not supported for your arch (yet)");
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker make_a_baby(argc, argv);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker /* first compare register states when execl() syscall starts */
108*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Before exec() in child");
109*49cdfc7eSAndroid Build Coastguard Worker compare_registers(0x00);
110*49cdfc7eSAndroid Build Coastguard Worker compare_registers(0xff);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker /* then compare register states after execl() syscall finishes */
113*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "After exec() in child");
114*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
115*49cdfc7eSAndroid Build Coastguard Worker if (ptrace(PTRACE_SYSCALL, pid, NULL, NULL) && errno) {
116*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL, "PTRACE_SYSCALL failed: %s",
117*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker compare_registers(0x00);
120*49cdfc7eSAndroid Build Coastguard Worker compare_registers(0xff);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker /* hopefully this worked */
123*49cdfc7eSAndroid Build Coastguard Worker ptrace(PTRACE_KILL, pid, NULL, NULL);
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)128*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker }
131