xref: /openbsd/regress/sys/ptrace/ptrace.c (revision 09467b48)
1 /*	$OpenBSD: ptrace.c,v 1.9 2020/07/06 07:31:51 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2004, Mark Kettenis.
4  * Copyright (c) 2004, Miodrag Vallat.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <sys/wait.h>
31 
32 #include <machine/reg.h>
33 
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 /*
40  * This tests checks whether ptrace will correctly cope with unaligned pc.
41  */
42 int
43 main(void)
44 {
45 	pid_t pid;
46 	int status;
47 
48 	pid = fork();
49 	if (pid == 0) {
50 		ptrace(PT_TRACE_ME, 0, 0, 0);
51 		raise(SIGTRAP);
52 		exit(EXIT_FAILURE);
53 	} else {
54 		struct reg regs;
55 
56 		waitpid(pid, &status, 0);
57 		ptrace(PT_GETREGS, pid, (caddr_t)&regs, 0);
58 
59 		/*
60 		 * Make sure amd64 is tested before i386
61 		 */
62 
63 #if defined(__alpha__)
64 		regs.r_regs[R_ZERO]|= 0x07;
65 #elif defined(__x86_64__)
66 		regs.r_rip |= 0x07;
67 #elif defined(__arm__)
68 		regs.r_pc |= 0x03;
69 #elif defined(__hppa__)
70 		regs.r_pcoqh |= 0x03;
71 		regs.r_pcoqt |= 0x03;
72 #elif defined(__i386__)
73 		regs.r_eip |= 0x03;
74 #elif defined(__mips64__)
75 		regs.r_regs[PC] |= 0x03;
76 #elif defined(__powerpc64__)
77 		regs.r_pc |= 0x03;
78 #elif defined(__powerpc__)
79 		regs.pc |= 0x03;
80 #elif defined(__sh__)
81 		regs.r_spc |= 0x01;
82 #elif defined( __sparcv9__)
83 		regs.r_pc |= 0x07;
84 		regs.r_npc |= 0x07;
85 #elif defined( __m88k__)
86 		/*
87 		 * The following code is for 88100 only, but should work with
88 		 * 88110 too, even though it sets the DELAY bit in exip.
89 		 */
90 		regs.sxip |= 0x03;
91 		regs.snip |= 0x03;
92 		regs.sfip |= 0x03;
93 #endif
94 		ptrace(PT_SETREGS, pid, (caddr_t)&regs, 0);
95 		ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
96 	}
97 	exit(EXIT_SUCCESS);
98 }
99