2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * Copyright (c) 1999-2017 The strace developers.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <asm/unistd.h>
42 static bool process_vm_readv_not_supported;
44 #ifndef HAVE_PROCESS_VM_READV
46 * Need to do this since process_vm_readv() is not yet available in libc.
47 * When libc is updated, only "static bool process_vm_readv_not_supported"
49 * The name is different to avoid potential collision with OS headers.
51 static ssize_t strace_process_vm_readv(pid_t pid,
52 const struct iovec *lvec,
53 unsigned long liovcnt,
54 const struct iovec *rvec,
55 unsigned long riovcnt,
58 return syscall(__NR_process_vm_readv,
59 (long) pid, lvec, liovcnt, rvec, riovcnt, flags);
61 # define process_vm_readv strace_process_vm_readv
62 #endif /* !HAVE_PROCESS_VM_READV */
65 vm_read_mem(const pid_t pid, void *const laddr,
66 const kernel_ulong_t raddr, const size_t len)
68 const unsigned long truncated_raddr = raddr;
70 #if SIZEOF_LONG < SIZEOF_KERNEL_LONG_T
71 if (raddr != (kernel_ulong_t) truncated_raddr) {
77 const struct iovec local = {
81 const struct iovec remote = {
82 .iov_base = (void *) truncated_raddr,
86 const ssize_t rc = process_vm_readv(pid, &local, 1, &remote, 1, 0);
87 if (rc < 0 && errno == ENOSYS)
88 process_vm_readv_not_supported = true;
94 tracee_addr_is_invalid(kernel_ulong_t addr)
97 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
98 current_wordsize < sizeof(addr) && addr & ~(kernel_ulong_t) -1U;
104 /* legacy method of copying from tracee */
106 umoven_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
109 unsigned int n, m, nread;
112 char x[sizeof(long)];
116 if (addr & (sizeof(long) - 1)) {
117 /* addr not a multiple of sizeof(long) */
118 n = addr & (sizeof(long) - 1); /* residue */
119 addr &= -sizeof(long); /* aligned address */
121 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
125 case ESRCH: case EINVAL:
126 /* these could be seen if the process is gone */
128 case EFAULT: case EIO: case EPERM:
129 /* address space is inaccessible */
132 /* all the rest is strange and should be reported */
133 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
137 m = MIN(sizeof(long) - n, len);
138 memcpy(laddr, &u.x[n], m);
139 addr += sizeof(long);
146 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
150 case ESRCH: case EINVAL:
151 /* these could be seen if the process is gone */
153 case EFAULT: case EIO: case EPERM:
154 /* address space is inaccessible */
156 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
157 nread, nread + len, addr - nread);
161 /* all the rest is strange and should be reported */
162 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
166 m = MIN(sizeof(long), len);
167 memcpy(laddr, u.x, m);
168 addr += sizeof(long);
178 * Copy `len' bytes of data from process `pid'
179 * at address `addr' to our space at `our_addr'.
182 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
183 void *const our_addr)
185 if (tracee_addr_is_invalid(addr))
188 const int pid = tcp->pid;
190 if (process_vm_readv_not_supported)
191 return umoven_peekdata(pid, addr, len, our_addr);
193 int r = vm_read_mem(pid, our_addr, addr, len);
194 if ((unsigned int) r == len)
197 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
198 (unsigned int) r, len, addr);
204 /* try PTRACE_PEEKDATA */
205 return umoven_peekdata(pid, addr, len, our_addr);
207 /* the process is gone */
209 case EFAULT: case EIO:
210 /* address space is inaccessible */
213 /* all the rest is strange and should be reported */
214 perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
221 * Like umoven_peekdata but make the additional effort of looking
222 * for a terminating zero byte.
225 umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
228 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
229 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
231 unsigned int n, m, nread;
234 char x[sizeof(long)];
238 if (addr & (sizeof(long) - 1)) {
239 /* addr not a multiple of sizeof(long) */
240 n = addr & (sizeof(long) - 1); /* residue */
241 addr &= -sizeof(long); /* aligned address */
243 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
247 case ESRCH: case EINVAL:
248 /* these could be seen if the process is gone */
250 case EFAULT: case EIO: case EPERM:
251 /* address space is inaccessible */
254 /* all the rest is strange and should be reported */
255 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
259 m = MIN(sizeof(long) - n, len);
260 memcpy(laddr, &u.x[n], m);
261 while (n & (sizeof(long) - 1))
262 if (u.x[n++] == '\0')
264 addr += sizeof(long);
272 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
276 case ESRCH: case EINVAL:
277 /* these could be seen if the process is gone */
279 case EFAULT: case EIO: case EPERM:
280 /* address space is inaccessible */
282 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
283 nread, nread + len, addr - nread);
287 /* all the rest is strange and should be reported */
288 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
292 m = MIN(sizeof(long), len);
293 memcpy(laddr, u.x, m);
294 /* "If a NUL char exists in this word" */
295 if ((u.val - x01010101) & ~u.val & x80808080)
297 addr += sizeof(long);
307 * Like `umove' but make the additional effort of looking
308 * for a terminating zero byte.
310 * Returns < 0 on error, > 0 if NUL was seen,
311 * (TODO if useful: return count of bytes including NUL),
312 * else 0 if len bytes were read but no NUL byte seen.
314 * Note: there is no guarantee we won't overwrite some bytes
315 * in laddr[] _after_ terminating NUL (but, of course,
316 * we never write past laddr[len-1]).
319 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
322 if (tracee_addr_is_invalid(addr))
325 const int pid = tcp->pid;
327 if (process_vm_readv_not_supported)
328 return umovestr_peekdata(pid, addr, len, laddr);
330 const size_t page_size = get_pagesize();
331 const size_t page_mask = page_size - 1;
332 unsigned int nread = 0;
336 * Don't cross pages, otherwise we can get EFAULT
337 * and fail to notice that terminating NUL lies
338 * in the existing (first) page.
340 unsigned int chunk_len = len > page_size ? page_size : len;
341 unsigned int end_in_page = (addr + chunk_len) & page_mask;
342 if (chunk_len > end_in_page) /* crosses to the next page */
343 chunk_len -= end_in_page;
345 int r = vm_read_mem(pid, laddr, addr, chunk_len);
347 if (memchr(laddr, '\0', r))
358 /* try PTRACE_PEEKDATA */
360 return umovestr_peekdata(pid, addr,
363 case EFAULT: case EIO:
364 /* address space is inaccessible */
366 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
367 nread, nread + len, addr - nread);
370 /* the process is gone */
373 /* all the rest is strange and should be reported */
374 perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,