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-2018 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 nread = 0;
110 unsigned int residue = addr & (sizeof(long) - 1);
113 addr &= -sizeof(long); /* aligned address */
118 char x[sizeof(long)];
119 } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
124 case ESRCH: case EINVAL:
125 /* these could be seen if the process is gone */
127 case EFAULT: case EIO: case EPERM:
128 /* address space is inaccessible */
130 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
131 nread, nread + len, addr - nread);
135 /* all the rest is strange and should be reported */
136 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
141 unsigned int m = MIN(sizeof(long) - residue, len);
142 memcpy(laddr, &u.x[residue], m);
144 addr += sizeof(long);
154 * Copy `len' bytes of data from process `pid'
155 * at address `addr' to our space at `our_addr'.
158 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
159 void *const our_addr)
161 if (tracee_addr_is_invalid(addr))
164 const int pid = tcp->pid;
166 if (process_vm_readv_not_supported)
167 return umoven_peekdata(pid, addr, len, our_addr);
169 int r = vm_read_mem(pid, our_addr, addr, len);
170 if ((unsigned int) r == len)
173 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
174 (unsigned int) r, len, addr);
180 /* try PTRACE_PEEKDATA */
181 return umoven_peekdata(pid, addr, len, our_addr);
183 /* the process is gone */
185 case EFAULT: case EIO:
186 /* address space is inaccessible */
189 /* all the rest is strange and should be reported */
190 perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
197 * Like umoven_peekdata but make the additional effort of looking
198 * for a terminating zero byte.
201 umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
204 unsigned int nread = 0;
205 unsigned int residue = addr & (sizeof(long) - 1);
206 void *const orig_addr = laddr;
209 addr &= -sizeof(long); /* aligned address */
214 char x[sizeof(long)];
215 } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
220 case ESRCH: case EINVAL:
221 /* these could be seen if the process is gone */
223 case EFAULT: case EIO: case EPERM:
224 /* address space is inaccessible */
226 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
227 nread, nread + len, addr - nread);
231 /* all the rest is strange and should be reported */
232 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
237 unsigned int m = MIN(sizeof(long) - residue, len);
238 memcpy(laddr, &u.x[residue], m);
239 while (residue < sizeof(long))
240 if (u.x[residue++] == '\0')
241 return (laddr - orig_addr) + residue;
243 addr += sizeof(long);
253 * Like `umove' but make the additional effort of looking
254 * for a terminating zero byte.
256 * Returns < 0 on error, strlen + 1 if NUL was seen,
257 * else 0 if len bytes were read but no NUL byte seen.
259 * Note: there is no guarantee we won't overwrite some bytes
260 * in laddr[] _after_ terminating NUL (but, of course,
261 * we never write past laddr[len-1]).
264 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
267 if (tracee_addr_is_invalid(addr))
270 const int pid = tcp->pid;
272 if (process_vm_readv_not_supported)
273 return umovestr_peekdata(pid, addr, len, laddr);
275 const size_t page_size = get_pagesize();
276 const size_t page_mask = page_size - 1;
277 unsigned int nread = 0;
281 * Don't cross pages, otherwise we can get EFAULT
282 * and fail to notice that terminating NUL lies
283 * in the existing (first) page.
285 unsigned int chunk_len = len > page_size ? page_size : len;
286 unsigned int end_in_page = (addr + chunk_len) & page_mask;
287 if (chunk_len > end_in_page) /* crosses to the next page */
288 chunk_len -= end_in_page;
290 int r = vm_read_mem(pid, laddr, addr, chunk_len);
292 char *nul_addr = memchr(laddr, '\0', r);
295 return (nul_addr - laddr) + 1;
305 /* try PTRACE_PEEKDATA */
307 return umovestr_peekdata(pid, addr,
309 ATTRIBUTE_FALLTHROUGH;
310 case EFAULT: case EIO:
311 /* address space is inaccessible */
313 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
314 nread, nread + len, addr - nread);
317 /* the process is gone */
320 /* all the rest is strange and should be reported */
321 perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,