]> granicus.if.org Git - strace/blob - mem.c
Remove linux/ptp_clock.h
[strace] / mem.c
1 /*
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) 2000 PocketPenguins Inc.  Linux for Hitachi SuperH
7  *                    port by Greg Banks <gbanks@pocketpenguins.com>
8  * Copyright (c) 1999-2017 The strace developers.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include <linux/mman.h>
36 #include <sys/mman.h>
37
38 unsigned long
39 get_pagesize(void)
40 {
41         static unsigned long pagesize;
42
43         if (!pagesize)
44                 pagesize = sysconf(_SC_PAGESIZE);
45         return pagesize;
46 }
47
48 SYS_FUNC(brk)
49 {
50         printaddr(tcp->u_arg[0]);
51
52         return RVAL_DECODED | RVAL_HEX;
53 }
54
55 #include "xlat/mmap_prot.h"
56 #include "xlat/mmap_flags.h"
57
58 #ifndef MAP_HUGE_SHIFT
59 # define MAP_HUGE_SHIFT 26
60 #endif
61
62 #ifndef MAP_HUGE_MASK
63 # define MAP_HUGE_MASK 0x3f
64 #endif
65
66 static void
67 print_mmap_flags(kernel_ulong_t flags)
68 {
69         printxval64(mmap_flags, flags & MAP_TYPE, "MAP_???");
70         flags &= ~MAP_TYPE;
71
72         const unsigned int mask = MAP_HUGE_MASK << MAP_HUGE_SHIFT;
73         const unsigned int hugetlb_value = flags & mask;
74
75         flags &= ~mask;
76         addflags(mmap_flags, flags);
77
78         if (hugetlb_value)
79                 tprintf("|%u<<MAP_HUGE_SHIFT",
80                         hugetlb_value >> MAP_HUGE_SHIFT);
81 }
82
83 static void
84 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
85 {
86         const kernel_ulong_t addr = u_arg[0];
87         const kernel_ulong_t len = u_arg[1];
88         const kernel_ulong_t prot = u_arg[2];
89         const kernel_ulong_t flags = u_arg[3];
90         const int fd = u_arg[4];
91
92         printaddr(addr);
93         tprintf(", %" PRI_klu ", ", len);
94         printflags64(mmap_prot, prot, "PROT_???");
95         tprints(", ");
96         print_mmap_flags(flags);
97         tprints(", ");
98         printfd(tcp, fd);
99         tprintf(", %#llx", offset);
100 }
101
102 /* Syscall name<->function correspondence is messed up on many arches.
103  * For example:
104  * i386 has __NR_mmap == 90, and it is "old mmap", and
105  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
106  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
107  * Confused? Me too!
108  */
109
110 #if defined AARCH64 || defined ARM \
111  || defined I386 || defined X86_64 || defined X32 \
112  || defined M68K \
113  || defined S390 || defined S390X
114 /* Params are pointed to by u_arg[0], offset is in bytes */
115 SYS_FUNC(old_mmap)
116 {
117         kernel_ulong_t u_arg[6];
118 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
119         /* We are here only in a 32-bit personality. */
120         unsigned int narrow_arg[6];
121         if (umove_or_printaddr(tcp, tcp->u_arg[0], &narrow_arg))
122                 return RVAL_DECODED | RVAL_HEX;
123         unsigned int i;
124         for (i = 0; i < 6; i++)
125                 u_arg[i] = narrow_arg[i];
126 # else
127         if (umove_or_printaddr(tcp, tcp->u_arg[0], &u_arg))
128                 return RVAL_DECODED | RVAL_HEX;
129 # endif
130         print_mmap(tcp, u_arg, u_arg[5]);
131
132         return RVAL_DECODED | RVAL_HEX;
133 }
134 #endif /* old_mmap architectures */
135
136 #ifdef S390
137 /* Params are pointed to by u_arg[0], offset is in pages */
138 SYS_FUNC(old_mmap_pgoff)
139 {
140         kernel_ulong_t u_arg[5];
141         int i;
142         unsigned int narrow_arg[6];
143         unsigned long long offset;
144         if (umove_or_printaddr(tcp, tcp->u_arg[0], &narrow_arg))
145                 return RVAL_DECODED | RVAL_HEX;
146         for (i = 0; i < 5; i++)
147                 u_arg[i] = narrow_arg[i];
148         offset = narrow_arg[5];
149         offset *= get_pagesize();
150         print_mmap(tcp, u_arg, offset);
151
152         return RVAL_DECODED | RVAL_HEX;
153 }
154 #endif /* S390 */
155
156 /* Params are passed directly, offset is in bytes */
157 SYS_FUNC(mmap)
158 {
159         /* Example of kernel-side handling of this variety of mmap:
160          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
161          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
162          * since the above code converts off to pages.
163          */
164         print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
165
166         return RVAL_DECODED | RVAL_HEX;
167 }
168
169 /* Params are passed directly, offset is in pages */
170 SYS_FUNC(mmap_pgoff)
171 {
172         /* Try test/mmap_offset_decode.c */
173         unsigned long long offset;
174         offset = tcp->u_arg[5];
175         offset *= get_pagesize();
176         print_mmap(tcp, tcp->u_arg, offset);
177
178         return RVAL_DECODED | RVAL_HEX;
179 }
180
181 /* Params are passed directly, offset is in 4k units */
182 SYS_FUNC(mmap_4koff)
183 {
184         unsigned long long offset;
185         offset = tcp->u_arg[5];
186         offset <<= 12;
187         print_mmap(tcp, tcp->u_arg, offset);
188
189         return RVAL_DECODED | RVAL_HEX;
190 }
191
192 SYS_FUNC(munmap)
193 {
194         printaddr(tcp->u_arg[0]);
195         tprintf(", %" PRI_klu, tcp->u_arg[1]);
196
197         return RVAL_DECODED;
198 }
199
200 static int
201 do_mprotect(struct tcb *tcp, bool has_pkey)
202 {
203         printaddr(tcp->u_arg[0]);
204         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
205         printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
206
207         if (has_pkey)
208                 tprintf(", %d", (int) tcp->u_arg[3]);
209
210         return RVAL_DECODED;
211 }
212
213 SYS_FUNC(mprotect)
214 {
215         return do_mprotect(tcp, false);
216 }
217
218 SYS_FUNC(pkey_mprotect)
219 {
220         return do_mprotect(tcp, true);
221 }
222
223 #include "xlat/mremap_flags.h"
224
225 SYS_FUNC(mremap)
226 {
227         printaddr(tcp->u_arg[0]);
228         tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
229         printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
230 #ifdef MREMAP_FIXED
231         if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
232             (MREMAP_MAYMOVE | MREMAP_FIXED)) {
233                 tprints(", ");
234                 printaddr(tcp->u_arg[4]);
235         }
236 #endif
237         return RVAL_DECODED | RVAL_HEX;
238 }
239
240 #include "xlat/madvise_cmds.h"
241
242 SYS_FUNC(madvise)
243 {
244         printaddr(tcp->u_arg[0]);
245         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
246         printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
247
248         return RVAL_DECODED;
249 }
250
251 #include "xlat/mlockall_flags.h"
252
253 SYS_FUNC(mlockall)
254 {
255         printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
256
257         return RVAL_DECODED;
258 }
259
260 #include "xlat/mctl_sync.h"
261
262 SYS_FUNC(msync)
263 {
264         /* addr */
265         printaddr(tcp->u_arg[0]);
266         /* len */
267         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
268         /* flags */
269         printflags(mctl_sync, tcp->u_arg[2], "MS_???");
270
271         return RVAL_DECODED;
272 }
273
274 #include "xlat/mlock_flags.h"
275
276 SYS_FUNC(mlock2)
277 {
278         printaddr(tcp->u_arg[0]);
279         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
280         printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
281
282         return RVAL_DECODED;
283 }
284
285 SYS_FUNC(mincore)
286 {
287         if (entering(tcp)) {
288                 printaddr(tcp->u_arg[0]);
289                 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
290         } else {
291                 const unsigned long page_size = get_pagesize();
292                 const unsigned long page_mask = page_size - 1;
293                 unsigned long len = tcp->u_arg[1];
294                 unsigned char *vec = NULL;
295
296                 len = len / page_size + (len & page_mask ? 1 : 0);
297                 if (syserror(tcp) || !verbose(tcp) ||
298                     !tcp->u_arg[2] || !(vec = malloc(len)) ||
299                     umoven(tcp, tcp->u_arg[2], len, vec) < 0)
300                         printaddr(tcp->u_arg[2]);
301                 else {
302                         unsigned long i;
303                         tprints("[");
304                         for (i = 0; i < len; i++) {
305                                 if (i)
306                                         tprints(", ");
307                                 if (abbrev(tcp) && i >= max_strlen) {
308                                         tprints("...");
309                                         break;
310                                 }
311                                 tprints((vec[i] & 1) ? "1" : "0");
312                         }
313                         tprints("]");
314                 }
315                 free(vec);
316         }
317         return 0;
318 }
319
320 #if defined ALPHA || defined IA64 || defined M68K \
321  || defined SPARC || defined SPARC64
322 SYS_FUNC(getpagesize)
323 {
324         return RVAL_DECODED | RVAL_HEX;
325 }
326 #endif
327
328 SYS_FUNC(remap_file_pages)
329 {
330         const kernel_ulong_t addr = tcp->u_arg[0];
331         const kernel_ulong_t size = tcp->u_arg[1];
332         const kernel_ulong_t prot = tcp->u_arg[2];
333         const kernel_ulong_t pgoff = tcp->u_arg[3];
334         const kernel_ulong_t flags = tcp->u_arg[4];
335
336         printaddr(addr);
337         tprintf(", %" PRI_klu ", ", size);
338         printflags64(mmap_prot, prot, "PROT_???");
339         tprintf(", %" PRI_klu ", ", pgoff);
340         print_mmap_flags(flags);
341
342         return RVAL_DECODED;
343 }
344
345 #if defined(POWERPC)
346 static bool
347 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
348 {
349         tprintf("%#08x", *(unsigned int *) elem_buf);
350
351         return true;
352 }
353
354 SYS_FUNC(subpage_prot)
355 {
356         kernel_ulong_t addr = tcp->u_arg[0];
357         kernel_ulong_t len = tcp->u_arg[1];
358         kernel_ulong_t nmemb = len >> 16;
359         kernel_ulong_t map = tcp->u_arg[2];
360
361         printaddr(addr);
362         tprintf(", %" PRI_klu ", ", len);
363
364         unsigned int entry;
365         print_array(tcp, map, nmemb, &entry, sizeof(entry),
366                     umoven_or_printaddr, print_protmap_entry, 0);
367
368         return RVAL_DECODED;
369 }
370 #endif