]> granicus.if.org Git - strace/blob - mem.c
Fix old_mmap output when mmap arguments are unfetchable
[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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "defs.h"
34 #include <asm/mman.h>
35 #include <sys/mman.h>
36
37 unsigned long
38 get_pagesize(void)
39 {
40         static unsigned long pagesize;
41
42         if (!pagesize)
43                 pagesize = sysconf(_SC_PAGESIZE);
44         return pagesize;
45 }
46
47 SYS_FUNC(brk)
48 {
49         printaddr(tcp->u_arg[0]);
50
51         return RVAL_DECODED | RVAL_HEX;
52 }
53
54 #include "xlat/mmap_prot.h"
55 #include "xlat/mmap_flags.h"
56
57 static void
58 print_mmap(struct tcb *tcp, long *u_arg, unsigned long long offset)
59 {
60         const unsigned long addr = u_arg[0];
61         const unsigned long len = u_arg[1];
62         const unsigned long prot = u_arg[2];
63         const unsigned long flags = u_arg[3];
64         const int fd = u_arg[4];
65
66         printaddr(addr);
67         tprintf(", %lu, ", len);
68         printflags(mmap_prot, prot, "PROT_???");
69         tprints(", ");
70 #ifdef MAP_TYPE
71         printxval(mmap_flags, flags & MAP_TYPE, "MAP_???");
72         addflags(mmap_flags, flags & ~MAP_TYPE);
73 #else
74         printflags(mmap_flags, flags, "MAP_???");
75 #endif
76         tprints(", ");
77         printfd(tcp, fd);
78         tprintf(", %#llx", offset);
79 }
80
81 /* Syscall name<->function correspondence is messed up on many arches.
82  * For example:
83  * i386 has __NR_mmap == 90, and it is "old mmap", and
84  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
85  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
86  * Confused? Me too!
87  */
88
89 #if defined AARCH64 || defined ARM \
90  || defined I386 || defined X86_64 || defined X32 \
91  || defined M68K \
92  || defined S390 || defined S390X
93 /* Params are pointed to by u_arg[0], offset is in bytes */
94 SYS_FUNC(old_mmap)
95 {
96         long u_arg[6];
97 # if defined AARCH64 || defined X86_64
98         /* We are here only in a 32-bit personality. */
99         unsigned int narrow_arg[6];
100         if (umove_or_printaddr(tcp, tcp->u_arg[0], &narrow_arg))
101                 return RVAL_DECODED | RVAL_HEX;
102         unsigned int i;
103         for (i = 0; i < 6; i++)
104                 u_arg[i] = narrow_arg[i];
105 # else
106         if (umove_or_printaddr(tcp, tcp->u_arg[0], &u_arg))
107                 return RVAL_DECODED | RVAL_HEX;
108 # endif
109         print_mmap(tcp, u_arg, (unsigned long) u_arg[5]);
110
111         return RVAL_DECODED | RVAL_HEX;
112 }
113 #endif /* old_mmap architectures */
114
115 #if defined(S390)
116 /* Params are pointed to by u_arg[0], offset is in pages */
117 SYS_FUNC(old_mmap_pgoff)
118 {
119         long u_arg[5];
120         int i;
121         unsigned narrow_arg[6];
122         unsigned long long offset;
123         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), narrow_arg) == -1)
124                 return 0;
125         for (i = 0; i < 5; i++)
126                 u_arg[i] = (unsigned long) narrow_arg[i];
127         offset = narrow_arg[5];
128         offset *= get_pagesize();
129         print_mmap(tcp, u_arg, offset);
130
131         return RVAL_DECODED | RVAL_HEX;
132 }
133 #endif
134
135 /* Params are passed directly, offset is in bytes */
136 SYS_FUNC(mmap)
137 {
138         unsigned long long offset = (unsigned long) tcp->u_arg[5];
139 #if defined(LINUX_MIPSN32) || defined(X32)
140         /* Try test/x32_mmap.c */
141         offset = tcp->ext_arg[5];
142 #endif
143         /* Example of kernel-side handling of this variety of mmap:
144          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
145          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
146          * since the above code converts off to pages.
147          */
148         print_mmap(tcp, tcp->u_arg, offset);
149
150         return RVAL_DECODED | RVAL_HEX;
151 }
152
153 /* Params are passed directly, offset is in pages */
154 SYS_FUNC(mmap_pgoff)
155 {
156         /* Try test/mmap_offset_decode.c */
157         unsigned long long offset;
158         offset = (unsigned long) tcp->u_arg[5];
159         offset *= get_pagesize();
160         print_mmap(tcp, tcp->u_arg, offset);
161
162         return RVAL_DECODED | RVAL_HEX;
163 }
164
165 /* Params are passed directly, offset is in 4k units */
166 SYS_FUNC(mmap_4koff)
167 {
168         unsigned long long offset;
169         offset = (unsigned long) tcp->u_arg[5];
170         offset <<= 12;
171         print_mmap(tcp, tcp->u_arg, offset);
172
173         return RVAL_DECODED | RVAL_HEX;
174 }
175
176 SYS_FUNC(munmap)
177 {
178         printaddr(tcp->u_arg[0]);
179         tprintf(", %lu", tcp->u_arg[1]);
180
181         return RVAL_DECODED;
182 }
183
184 SYS_FUNC(mprotect)
185 {
186         printaddr(tcp->u_arg[0]);
187         tprintf(", %lu, ", tcp->u_arg[1]);
188         printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
189
190         return RVAL_DECODED;
191 }
192
193 #include "xlat/mremap_flags.h"
194
195 SYS_FUNC(mremap)
196 {
197         printaddr(tcp->u_arg[0]);
198         tprintf(", %lu, %lu, ", tcp->u_arg[1], tcp->u_arg[2]);
199         printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
200 #ifdef MREMAP_FIXED
201         if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
202             (MREMAP_MAYMOVE | MREMAP_FIXED)) {
203                 tprints(", ");
204                 printaddr(tcp->u_arg[4]);
205         }
206 #endif
207         return RVAL_DECODED | RVAL_HEX;
208 }
209
210 #include "xlat/madvise_cmds.h"
211
212 SYS_FUNC(madvise)
213 {
214         printaddr(tcp->u_arg[0]);
215         tprintf(", %lu, ", tcp->u_arg[1]);
216         printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
217
218         return RVAL_DECODED;
219 }
220
221 #include "xlat/mlockall_flags.h"
222
223 SYS_FUNC(mlockall)
224 {
225         printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
226
227         return RVAL_DECODED;
228 }
229
230 #include "xlat/mctl_sync.h"
231
232 SYS_FUNC(msync)
233 {
234         /* addr */
235         printaddr(tcp->u_arg[0]);
236         /* len */
237         tprintf(", %lu, ", tcp->u_arg[1]);
238         /* flags */
239         printflags(mctl_sync, tcp->u_arg[2], "MS_???");
240
241         return RVAL_DECODED;
242 }
243
244 #include "xlat/mlock_flags.h"
245
246 SYS_FUNC(mlock2)
247 {
248         printaddr(tcp->u_arg[0]);
249         tprintf(", %lu, ", tcp->u_arg[1]);
250         printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
251
252         return RVAL_DECODED;
253 }
254
255 SYS_FUNC(mincore)
256 {
257         if (entering(tcp)) {
258                 printaddr(tcp->u_arg[0]);
259                 tprintf(", %lu, ", tcp->u_arg[1]);
260         } else {
261                 const unsigned long page_size = get_pagesize();
262                 const unsigned long page_mask = page_size - 1;
263                 unsigned long len = tcp->u_arg[1];
264                 unsigned char *vec = NULL;
265
266                 len = len / page_size + (len & page_mask ? 1 : 0);
267                 if (syserror(tcp) || !verbose(tcp) ||
268                     !tcp->u_arg[2] || !(vec = malloc(len)) ||
269                     umoven(tcp, tcp->u_arg[2], len, vec) < 0)
270                         printaddr(tcp->u_arg[2]);
271                 else {
272                         unsigned long i;
273                         tprints("[");
274                         for (i = 0; i < len; i++) {
275                                 if (abbrev(tcp) && i >= max_strlen) {
276                                         tprints("...");
277                                         break;
278                                 }
279                                 tprints((vec[i] & 1) ? "1" : "0");
280                         }
281                         tprints("]");
282                 }
283                 free(vec);
284         }
285         return 0;
286 }
287
288 #if defined ALPHA || defined IA64 || defined M68K \
289  || defined SPARC || defined SPARC64
290 SYS_FUNC(getpagesize)
291 {
292         return RVAL_DECODED | RVAL_HEX;
293 }
294 #endif
295
296 SYS_FUNC(remap_file_pages)
297 {
298         const unsigned long addr = tcp->u_arg[0];
299         const unsigned long size = tcp->u_arg[1];
300         const unsigned long prot = tcp->u_arg[2];
301         const unsigned long pgoff = tcp->u_arg[3];
302         const unsigned long flags = tcp->u_arg[4];
303
304         printaddr(addr);
305         tprintf(", %lu, ", size);
306         printflags(mmap_prot, prot, "PROT_???");
307         tprintf(", %lu, ", pgoff);
308 #ifdef MAP_TYPE
309         printxval(mmap_flags, flags & MAP_TYPE, "MAP_???");
310         addflags(mmap_flags, flags & ~MAP_TYPE);
311 #else
312         printflags(mmap_flags, flags, "MAP_???");
313 #endif
314
315         return RVAL_DECODED;
316 }
317
318 #define MPOL_DEFAULT    0
319 #define MPOL_PREFERRED  1
320 #define MPOL_BIND       2
321 #define MPOL_INTERLEAVE 3
322
323 #define MPOL_F_NODE     (1<<0)
324 #define MPOL_F_ADDR     (1<<1)
325
326 #define MPOL_MF_STRICT  (1<<0)
327 #define MPOL_MF_MOVE    (1<<1)
328 #define MPOL_MF_MOVE_ALL (1<<2)
329
330 #include "xlat/policies.h"
331 #include "xlat/mbindflags.h"
332 #include "xlat/mempolicyflags.h"
333 #include "xlat/move_pages_flags.h"
334
335 static void
336 get_nodes(struct tcb *tcp, unsigned long ptr, unsigned long maxnodes, int err)
337 {
338         unsigned long nlongs, size, end;
339
340         nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
341         size = nlongs * sizeof(long);
342         end = ptr + size;
343         if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
344                             && (end > ptr))) {
345                 unsigned long n, cur, abbrev_end;
346                 int failed = 0;
347
348                 if (abbrev(tcp)) {
349                         abbrev_end = ptr + max_strlen * sizeof(long);
350                         if (abbrev_end < ptr)
351                                 abbrev_end = end;
352                 } else {
353                         abbrev_end = end;
354                 }
355                 tprints(", {");
356                 for (cur = ptr; cur < end; cur += sizeof(long)) {
357                         if (cur > ptr)
358                                 tprints(", ");
359                         if (cur >= abbrev_end) {
360                                 tprints("...");
361                                 break;
362                         }
363                         if (umoven(tcp, cur, sizeof(n), &n) < 0) {
364                                 tprints("?");
365                                 failed = 1;
366                                 break;
367                         }
368                         tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
369                 }
370                 tprints("}");
371                 if (failed) {
372                         tprints(" ");
373                         printaddr(ptr);
374                 }
375         } else {
376                 tprints(" ");
377                 printaddr(ptr);
378         }
379         tprintf(", %lu", maxnodes);
380 }
381
382 SYS_FUNC(mbind)
383 {
384         printaddr(tcp->u_arg[0]);
385         tprintf(", %lu, ", tcp->u_arg[1]);
386         printxval(policies, tcp->u_arg[2], "MPOL_???");
387         get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
388         tprints(", ");
389         printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
390
391         return RVAL_DECODED;
392 }
393
394 SYS_FUNC(set_mempolicy)
395 {
396         printxval(policies, tcp->u_arg[0], "MPOL_???");
397         get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
398
399         return RVAL_DECODED;
400 }
401
402 SYS_FUNC(get_mempolicy)
403 {
404         if (exiting(tcp)) {
405                 int pol;
406                 if (!umove_or_printaddr(tcp, tcp->u_arg[0], &pol))
407                         printxval(policies, pol, "MPOL_???");
408                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
409                 tprints(", ");
410                 printaddr(tcp->u_arg[3]);
411                 tprints(", ");
412                 printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
413         }
414         return 0;
415 }
416
417 SYS_FUNC(migrate_pages)
418 {
419         tprintf("%ld, ", (long) (pid_t) tcp->u_arg[0]);
420         get_nodes(tcp, tcp->u_arg[2], tcp->u_arg[1], 0);
421         tprints(", ");
422         get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[1], 0);
423
424         return RVAL_DECODED;
425 }
426
427 SYS_FUNC(move_pages)
428 {
429         if (entering(tcp)) {
430                 unsigned long npages = tcp->u_arg[1];
431                 tprintf("%ld, %lu, ", tcp->u_arg[0], npages);
432                 if (tcp->u_arg[2] == 0)
433                         tprints("NULL, ");
434                 else {
435                         unsigned int i;
436                         long puser = tcp->u_arg[2];
437                         tprints("{");
438                         for (i = 0; i < npages; ++i) {
439                                 void *p;
440                                 if (i > 0)
441                                         tprints(", ");
442                                 if (umove(tcp, puser, &p) < 0) {
443                                         tprints("???");
444                                         break;
445                                 }
446                                 tprintf("%p", p);
447                                 puser += sizeof(void *);
448                         }
449                         tprints("}, ");
450                 }
451                 if (tcp->u_arg[3] == 0)
452                         tprints("NULL, ");
453                 else {
454                         unsigned int i;
455                         long nodeuser = tcp->u_arg[3];
456                         tprints("{");
457                         for (i = 0; i < npages; ++i) {
458                                 int node;
459                                 if (i > 0)
460                                         tprints(", ");
461                                 if (umove(tcp, nodeuser, &node) < 0) {
462                                         tprints("???");
463                                         break;
464                                 }
465                                 tprintf("%#x", node);
466                                 nodeuser += sizeof(int);
467                         }
468                         tprints("}, ");
469                 }
470         } else {
471                 unsigned long npages = tcp->u_arg[1];
472                 if (tcp->u_arg[4] == 0)
473                         tprints("NULL, ");
474                 else {
475                         unsigned int i;
476                         long statususer = tcp->u_arg[4];
477                         tprints("{");
478                         for (i = 0; i < npages; ++i) {
479                                 int status;
480                                 if (i > 0)
481                                         tprints(", ");
482                                 if (umove(tcp, statususer, &status) < 0) {
483                                         tprints("???");
484                                         break;
485                                 }
486                                 tprintf("%#x", status);
487                                 statususer += sizeof(int);
488                         }
489                         tprints("}, ");
490                 }
491                 printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
492         }
493         return 0;
494 }
495
496 #if defined(POWERPC)
497 SYS_FUNC(subpage_prot)
498 {
499         unsigned long cur, end, abbrev_end, entries;
500         unsigned int entry;
501
502         printaddr(tcp->u_arg[0]);
503         tprints(", ");
504         printaddr(tcp->u_arg[1]);
505         tprints(", ");
506         entries = tcp->u_arg[1] >> 16;
507         if (!entries || !tcp->u_arg[2]) {
508                 tprints("{}");
509                 return 0;
510         }
511         cur = tcp->u_arg[2];
512         end = cur + (sizeof(int) * entries);
513         if (!verbose(tcp) || end < (unsigned long) tcp->u_arg[2]) {
514                 printaddr(tcp->u_arg[2]);
515                 return 0;
516         }
517         if (abbrev(tcp)) {
518                 abbrev_end = cur + (sizeof(int) * max_strlen);
519                 if (abbrev_end > end)
520                         abbrev_end = end;
521         }
522         else
523                 abbrev_end = end;
524         tprints("{");
525         for (; cur < end; cur += sizeof(int)) {
526                 if (cur > (unsigned long) tcp->u_arg[2])
527                         tprints(", ");
528                 if (cur >= abbrev_end) {
529                         tprints("...");
530                         break;
531                 }
532                 if (umove(tcp, cur, &entry) < 0) {
533                         tprintf("??? [%#lx]", cur);
534                         break;
535                 }
536                 else
537                         tprintf("%#08x", entry);
538         }
539         tprints("}");
540
541         return RVAL_DECODED;
542 }
543 #endif