]> granicus.if.org Git - strace/blob - mem.c
Remove XLAT_END
[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-2018 The strace developers.
9  * All rights reserved.
10  *
11  * SPDX-License-Identifier: LGPL-2.1-or-later
12  */
13
14 #include "defs.h"
15 #include <linux/mman.h>
16 #include <sys/mman.h>
17
18 unsigned long
19 get_pagesize(void)
20 {
21         static unsigned long pagesize;
22
23         if (!pagesize) {
24                 long ret = sysconf(_SC_PAGESIZE);
25
26                 if (ret < 0)
27                         perror_func_msg_and_die("sysconf(_SC_PAGESIZE)");
28                 if (ret == 0)
29                         error_func_msg_and_die("sysconf(_SC_PAGESIZE) "
30                                                "returned 0");
31
32                 pagesize = (unsigned long) ret;
33         }
34
35         return pagesize;
36 }
37
38 SYS_FUNC(brk)
39 {
40         printaddr(tcp->u_arg[0]);
41
42         return RVAL_DECODED | RVAL_HEX;
43 }
44
45 #include "xlat/mmap_prot.h"
46 #include "xlat/mmap_flags.h"
47
48 #ifndef MAP_HUGE_SHIFT
49 # define MAP_HUGE_SHIFT 26
50 #endif
51
52 #ifndef MAP_HUGE_MASK
53 # define MAP_HUGE_MASK 0x3f
54 #endif
55
56 static void
57 print_mmap_flags(kernel_ulong_t flags)
58 {
59         if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
60                 tprintf("%#" PRI_klx, flags);
61
62         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
63                 return;
64
65         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
66                 tprints(" /* ");
67
68         printxvals_ex(flags & MAP_TYPE, "MAP_???", XLAT_STYLE_ABBREV,
69                       mmap_flags, NULL);
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         if (flags) {
77                 tprints("|");
78                 printflags_ex(flags, NULL, XLAT_STYLE_ABBREV,
79                               mmap_flags, NULL);
80         }
81
82         if (hugetlb_value)
83                 tprintf("|%u<<MAP_HUGE_SHIFT",
84                         hugetlb_value >> MAP_HUGE_SHIFT);
85
86         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
87                 tprints(" */");
88 }
89
90 static void
91 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
92 {
93         const kernel_ulong_t addr = u_arg[0];
94         const kernel_ulong_t len = u_arg[1];
95         const kernel_ulong_t prot = u_arg[2];
96         const kernel_ulong_t flags = u_arg[3];
97         const int fd = u_arg[4];
98
99         printaddr(addr);
100         tprintf(", %" PRI_klu ", ", len);
101         printflags64(mmap_prot, prot, "PROT_???");
102         tprints(", ");
103         print_mmap_flags(flags);
104         tprints(", ");
105         printfd(tcp, fd);
106         tprintf(", %#llx", offset);
107 }
108
109 /* Syscall name<->function correspondence is messed up on many arches.
110  * For example:
111  * i386 has __NR_mmap == 90, and it is "old mmap", and
112  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
113  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
114  * Confused? Me too!
115  */
116
117 #if HAVE_ARCH_OLD_MMAP
118 /* Params are pointed to by u_arg[0], offset is in bytes */
119 SYS_FUNC(old_mmap)
120 {
121         kernel_ulong_t *args =
122                 fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
123
124         if (args)
125                 print_mmap(tcp, args, args[5]);
126         else
127                 printaddr(tcp->u_arg[0]);
128
129         return RVAL_DECODED | RVAL_HEX;
130 }
131
132 # if HAVE_ARCH_OLD_MMAP_PGOFF
133 /* Params are pointed to by u_arg[0], offset is in pages */
134 SYS_FUNC(old_mmap_pgoff)
135 {
136         kernel_ulong_t *args =
137                 fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
138
139         if (args) {
140                 unsigned long long offset;
141
142                 offset = args[5];
143                 offset *= get_pagesize();
144
145                 print_mmap(tcp, args, offset);
146         } else {
147                 printaddr(tcp->u_arg[0]);
148         }
149
150         return RVAL_DECODED | RVAL_HEX;
151 }
152 # endif /* HAVE_ARCH_OLD_MMAP_PGOFF */
153 #endif /* HAVE_ARCH_OLD_MMAP */
154
155 /* Params are passed directly, offset is in bytes */
156 SYS_FUNC(mmap)
157 {
158         /* Example of kernel-side handling of this variety of mmap:
159          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
160          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
161          * since the above code converts off to pages.
162          */
163         print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
164
165         return RVAL_DECODED | RVAL_HEX;
166 }
167
168 /* Params are passed directly, offset is in pages */
169 SYS_FUNC(mmap_pgoff)
170 {
171         /* Try test/mmap_offset_decode.c */
172         unsigned long long offset;
173         offset = tcp->u_arg[5];
174         offset *= get_pagesize();
175         print_mmap(tcp, tcp->u_arg, offset);
176
177         return RVAL_DECODED | RVAL_HEX;
178 }
179
180 /* Params are passed directly, offset is in 4k units */
181 SYS_FUNC(mmap_4koff)
182 {
183         unsigned long long offset;
184         offset = tcp->u_arg[5];
185         offset <<= 12;
186         print_mmap(tcp, tcp->u_arg, offset);
187
188         return RVAL_DECODED | RVAL_HEX;
189 }
190
191 SYS_FUNC(munmap)
192 {
193         printaddr(tcp->u_arg[0]);
194         tprintf(", %" PRI_klu, tcp->u_arg[1]);
195
196         return RVAL_DECODED;
197 }
198
199 static int
200 do_mprotect(struct tcb *tcp, bool has_pkey)
201 {
202         printaddr(tcp->u_arg[0]);
203         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
204         printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
205
206         if (has_pkey)
207                 tprintf(", %d", (int) tcp->u_arg[3]);
208
209         return RVAL_DECODED;
210 }
211
212 SYS_FUNC(mprotect)
213 {
214         return do_mprotect(tcp, false);
215 }
216
217 SYS_FUNC(pkey_mprotect)
218 {
219         return do_mprotect(tcp, true);
220 }
221
222 #include "xlat/mremap_flags.h"
223
224 SYS_FUNC(mremap)
225 {
226         printaddr(tcp->u_arg[0]);
227         tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
228         printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
229 #ifdef MREMAP_FIXED
230         if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
231             (MREMAP_MAYMOVE | MREMAP_FIXED)) {
232                 tprints(", ");
233                 printaddr(tcp->u_arg[4]);
234         }
235 #endif
236         return RVAL_DECODED | RVAL_HEX;
237 }
238
239 #include "xlat/madvise_cmds.h"
240
241 SYS_FUNC(madvise)
242 {
243         printaddr(tcp->u_arg[0]);
244         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
245         printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
246
247         return RVAL_DECODED;
248 }
249
250 #include "xlat/mlockall_flags.h"
251
252 SYS_FUNC(mlockall)
253 {
254         printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
255
256         return RVAL_DECODED;
257 }
258
259 #include "xlat/mctl_sync.h"
260
261 SYS_FUNC(msync)
262 {
263         /* addr */
264         printaddr(tcp->u_arg[0]);
265         /* len */
266         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
267         /* flags */
268         printflags(mctl_sync, tcp->u_arg[2], "MS_???");
269
270         return RVAL_DECODED;
271 }
272
273 #include "xlat/mlock_flags.h"
274
275 SYS_FUNC(mlock2)
276 {
277         printaddr(tcp->u_arg[0]);
278         tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
279         printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
280
281         return RVAL_DECODED;
282 }
283
284 SYS_FUNC(mincore)
285 {
286         if (entering(tcp)) {
287                 printaddr(tcp->u_arg[0]);
288                 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
289         } else {
290                 const unsigned long page_size = get_pagesize();
291                 const unsigned long page_mask = page_size - 1;
292                 unsigned long len = tcp->u_arg[1];
293                 unsigned char *vec = NULL;
294
295                 len = len / page_size + (len & page_mask ? 1 : 0);
296                 if (syserror(tcp) || !verbose(tcp) ||
297                     !tcp->u_arg[2] || !(vec = malloc(len)) ||
298                     umoven(tcp, tcp->u_arg[2], len, vec) < 0)
299                         printaddr(tcp->u_arg[2]);
300                 else {
301                         unsigned long i;
302                         tprints("[");
303                         for (i = 0; i < len; i++) {
304                                 if (i)
305                                         tprints(", ");
306                                 if (abbrev(tcp) && i >= max_strlen) {
307                                         tprints("...");
308                                         break;
309                                 }
310                                 tprints((vec[i] & 1) ? "1" : "0");
311                         }
312                         tprints("]");
313                 }
314                 free(vec);
315         }
316         return 0;
317 }
318
319 SYS_FUNC(remap_file_pages)
320 {
321         const kernel_ulong_t addr = tcp->u_arg[0];
322         const kernel_ulong_t size = tcp->u_arg[1];
323         const kernel_ulong_t prot = tcp->u_arg[2];
324         const kernel_ulong_t pgoff = tcp->u_arg[3];
325         const kernel_ulong_t flags = tcp->u_arg[4];
326
327         printaddr(addr);
328         tprintf(", %" PRI_klu ", ", size);
329         printflags64(mmap_prot, prot, "PROT_???");
330         tprintf(", %" PRI_klu ", ", pgoff);
331         print_mmap_flags(flags);
332
333         return RVAL_DECODED;
334 }
335
336 #if defined(POWERPC)
337 static bool
338 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
339 {
340         tprintf("%#08x", *(unsigned int *) elem_buf);
341
342         return true;
343 }
344
345 SYS_FUNC(subpage_prot)
346 {
347         kernel_ulong_t addr = tcp->u_arg[0];
348         kernel_ulong_t len = tcp->u_arg[1];
349         kernel_ulong_t nmemb = len >> 16;
350         kernel_ulong_t map = tcp->u_arg[2];
351
352         printaddr(addr);
353         tprintf(", %" PRI_klu ", ", len);
354
355         unsigned int entry;
356         print_array(tcp, map, nmemb, &entry, sizeof(entry),
357                     tfetch_mem, print_protmap_entry, 0);
358
359         return RVAL_DECODED;
360 }
361 #endif