1 /* Testing s390x PTRACE_SINGLEBLOCK ptrace request.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
27 #include <support/xstdlib.h>
28 #include <support/xunistd.h>
29 #include <support/check.h>
33 /* Ensure that we use the PTRACE_SINGLEBLOCK definition from glibc ptrace.h
34 in tracer_func. We need the kernel ptrace.h for structs ptrace_area
36 #include <sys/ptrace.h>
37 static const enum __ptrace_request req_singleblock
= PTRACE_SINGLEBLOCK
;
38 #include <asm/ptrace.h>
43 /* Dump the mapping information for manual inspection of the printed
46 sprintf (str
, "cat /proc/%d/maps", pid
);
51 TEST_VERIFY_EXIT (ptrace (PTRACE_TRACEME
) == 0);
52 /* Stop tracee. Afterwards the tracer_func can operate. */
55 puts ("The PTRACE_SINGLEBLOCK of the tracer will stop after: "
56 "brasl %r14,<puts@plt>!");
62 unsigned long last_break
;
70 #define MAX_CHARS_IN_BUF 4096
71 char buf
[MAX_CHARS_IN_BUF
+ 1];
76 /* Wait for the tracee to be stopped or exited. */
78 if (WIFEXITED (status
))
81 /* Get information about tracee: gprs, last breaking address. */
82 parea
.len
= sizeof (regs
);
83 parea
.process_addr
= (unsigned long) ®s
;
84 parea
.kernel_addr
= 0;
85 TEST_VERIFY_EXIT (ptrace (PTRACE_PEEKUSR_AREA
, pid
, &parea
) == 0);
86 TEST_VERIFY_EXIT (ptrace (PTRACE_GET_LAST_BREAK
, pid
, NULL
, &last_break
)
89 parea2
.iov_len
= sizeof (regs2
);
90 parea2
.iov_base
= ®s2
;
91 TEST_VERIFY_EXIT (ptrace (PTRACE_GETREGSET
, pid
, NT_PRSTATUS
, &parea2
)
93 TEST_VERIFY_EXIT (parea2
.iov_len
== sizeof (regs2
));
95 /* Test if gprs obtained by PTRACE_PEEKUSR_AREA and PTRACE_GETREGESET
96 have the same values. */
97 TEST_VERIFY_EXIT (memcmp (®s
, ®s2
, sizeof (regs
)) == 0);
99 printf ("child IA: %p last_break: %p\n",
100 (void *) regs
[1], (void *) last_break
);
102 /* Execute tracee until next taken branch.
105 Before the commit which introduced this testcase,
106 <glibc>/sysdeps/unix/sysv/linux/s390/sys/ptrace.h
107 uses ptrace-request 12 for PTRACE_GETREGS,
108 but <kernel>/include/uapi/linux/ptrace.h
109 uses 12 for PTRACE_SINGLEBLOCK.
111 The s390 kernel has no support for PTRACE_GETREGS!
112 Thus glibc ptrace.h is adjusted to match kernel ptrace.h.
114 The glibc sys/ptrace.h header contains the identifier
115 PTRACE_SINGLEBLOCK in enum __ptrace_request. In contrast, the kernel
116 asm/ptrace.h header defines PTRACE_SINGLEBLOCK.
118 This test ensures, that PTRACE_SINGLEBLOCK defined in glibc
119 works as expected. If the kernel would interpret it as
120 PTRACE_GETREGS, then the tracee will not make any progress
121 and this testcase will time out or the ptrace call will fail with
124 /* Ptrace request 12 is first done with data argument pointing to
126 -If request 12 is interpreted as PTRACE_GETREGS, it will store the regs
127 to buffer without an error.
129 -If request 12 is interpreted as PTRACE_SINGLEBLOCK, it will fail
130 as data argument is used as signal-number and the address of
131 buf is no valid signal.
133 -If request 12 is not implemented, it will also fail.
135 Here the test expects that the buffer is untouched and an error is
137 memset (buf
, 'a', MAX_CHARS_IN_BUF
);
138 ret
= ptrace (req_singleblock
, pid
, NULL
, buf
);
139 buf
[MAX_CHARS_IN_BUF
] = '\0';
140 buf_count
= strspn (buf
, "a");
141 TEST_VERIFY_EXIT (buf_count
== MAX_CHARS_IN_BUF
);
142 TEST_VERIFY_EXIT (ret
== -1);
144 /* If request 12 is interpreted as PTRACE_GETREGS, the first ptrace
145 call will touch the buffer which is detected by this test. */
147 ret
= ptrace (req_singleblock
, pid
, NULL
, NULL
);
150 /* The kernel has support for PTRACE_SINGLEBLOCK ptrace request. */
151 TEST_VERIFY_EXIT (errno
== 0);
155 /* The kernel (< 3.15) has no support for PTRACE_SINGLEBLOCK ptrace
157 TEST_VERIFY_EXIT (errno
== EIO
);
158 TEST_VERIFY_EXIT (ret
== -1);
160 /* Just continue tracee until it exits normally. */
161 TEST_VERIFY_EXIT (ptrace (PTRACE_CONT
, pid
, NULL
, NULL
) == 0);
174 tracee_func (getpid ());
179 #include <support/test-driver.c>