]> granicus.if.org Git - strace/blob - mem.c
Generate xlat/*.in files
[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 static unsigned long
38 get_pagesize()
39 {
40         static unsigned long pagesize;
41
42         if (!pagesize)
43                 pagesize = sysconf(_SC_PAGESIZE);
44         return pagesize;
45 }
46
47 int
48 sys_brk(struct tcb *tcp)
49 {
50         if (entering(tcp)) {
51                 tprintf("%#lx", tcp->u_arg[0]);
52         }
53         return RVAL_HEX;
54 }
55
56 #include "xlat/mmap_prot.h"
57
58 #include "xlat/mmap_flags.h"
59
60 static int
61 print_mmap(struct tcb *tcp, long *u_arg, unsigned long long offset)
62 {
63         if (entering(tcp)) {
64                 /* addr */
65                 if (!u_arg[0])
66                         tprints("NULL, ");
67                 else
68                         tprintf("%#lx, ", u_arg[0]);
69                 /* len */
70                 tprintf("%lu, ", u_arg[1]);
71                 /* prot */
72                 printflags(mmap_prot, u_arg[2], "PROT_???");
73                 tprints(", ");
74                 /* flags */
75 #ifdef MAP_TYPE
76                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
77                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
78 #else
79                 printflags(mmap_flags, u_arg[3], "MAP_???");
80 #endif
81                 tprints(", ");
82                 /* fd */
83                 printfd(tcp, u_arg[4]);
84                 /* offset */
85                 tprintf(", %#llx", offset);
86         }
87         return RVAL_HEX;
88 }
89
90 /* Syscall name<->function correspondence is messed up on many arches.
91  * For example:
92  * i386 has __NR_mmap == 90, and it is "old mmap", and
93  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
94  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
95  * Confused? Me too!
96  */
97
98 /* Params are pointed to by u_arg[0], offset is in bytes */
99 int
100 sys_old_mmap(struct tcb *tcp)
101 {
102         long u_arg[6];
103 #if defined(IA64)
104         /*
105          * IA64 processes never call this routine, they only use the
106          * new 'sys_mmap' interface. Only IA32 processes come here.
107          */
108         int i;
109         unsigned narrow_arg[6];
110         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
111                 return 0;
112         for (i = 0; i < 6; i++)
113                 u_arg[i] = (unsigned long) narrow_arg[i];
114 #elif defined(X86_64)
115         /* We are here only in personality 1 (i386) */
116         int i;
117         unsigned narrow_arg[6];
118         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
119                 return 0;
120         for (i = 0; i < 6; ++i)
121                 u_arg[i] = (unsigned long) narrow_arg[i];
122 #else
123         if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
124                 return 0;
125 #endif
126         return print_mmap(tcp, u_arg, (unsigned long) u_arg[5]);
127 }
128
129 #if defined(S390)
130 /* Params are pointed to by u_arg[0], offset is in pages */
131 int
132 sys_old_mmap_pgoff(struct tcb *tcp)
133 {
134         long u_arg[5];
135         int i;
136         unsigned narrow_arg[6];
137         unsigned long long offset;
138         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
139                 return 0;
140         for (i = 0; i < 5; i++)
141                 u_arg[i] = (unsigned long) narrow_arg[i];
142         offset = narrow_arg[5];
143         offset *= get_pagesize();
144         return print_mmap(tcp, u_arg, offset);
145 }
146 #endif
147
148 /* Params are passed directly, offset is in bytes */
149 int
150 sys_mmap(struct tcb *tcp)
151 {
152         unsigned long long offset = (unsigned long) tcp->u_arg[5];
153 #if defined(LINUX_MIPSN32) || defined(X32)
154         /* Try test/x32_mmap.c */
155         offset = tcp->ext_arg[5];
156 #endif
157         /* Example of kernel-side handling of this variety of mmap:
158          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
159          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
160          * since the above code converts off to pages.
161          */
162         return print_mmap(tcp, tcp->u_arg, offset);
163 }
164
165 /* Params are passed directly, offset is in pages */
166 int
167 sys_mmap_pgoff(struct tcb *tcp)
168 {
169         /* Try test/mmap_offset_decode.c */
170         unsigned long long offset;
171         offset = (unsigned long) tcp->u_arg[5];
172         offset *= get_pagesize();
173         return print_mmap(tcp, tcp->u_arg, offset);
174 }
175
176 /* Params are passed directly, offset is in 4k units */
177 int
178 sys_mmap_4koff(struct tcb *tcp)
179 {
180         unsigned long long offset;
181         offset = (unsigned long) tcp->u_arg[5];
182         offset <<= 12;
183         return print_mmap(tcp, tcp->u_arg, offset);
184 }
185
186 int
187 sys_munmap(struct tcb *tcp)
188 {
189         if (entering(tcp)) {
190                 tprintf("%#lx, %lu",
191                         tcp->u_arg[0], tcp->u_arg[1]);
192         }
193         return 0;
194 }
195
196 int
197 sys_mprotect(struct tcb *tcp)
198 {
199         if (entering(tcp)) {
200                 tprintf("%#lx, %lu, ",
201                         tcp->u_arg[0], tcp->u_arg[1]);
202                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
203         }
204         return 0;
205 }
206
207 #include "xlat/mremap_flags.h"
208
209 int
210 sys_mremap(struct tcb *tcp)
211 {
212         if (entering(tcp)) {
213                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
214                         tcp->u_arg[2]);
215                 printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
216 #ifdef MREMAP_FIXED
217                 if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
218                     (MREMAP_MAYMOVE | MREMAP_FIXED))
219                         tprintf(", %#lx", tcp->u_arg[4]);
220 #endif
221         }
222         return RVAL_HEX;
223 }
224
225 #include "xlat/madvise_cmds.h"
226
227 int
228 sys_madvise(struct tcb *tcp)
229 {
230         if (entering(tcp)) {
231                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
232                 printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
233         }
234         return 0;
235 }
236
237 #include "xlat/mlockall_flags.h"
238
239 int
240 sys_mlockall(struct tcb *tcp)
241 {
242         if (entering(tcp)) {
243                 printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
244         }
245         return 0;
246 }
247
248 #ifdef MS_ASYNC
249
250 #include "xlat/mctl_sync.h"
251
252 int
253 sys_msync(struct tcb *tcp)
254 {
255         if (entering(tcp)) {
256                 /* addr */
257                 tprintf("%#lx", tcp->u_arg[0]);
258                 /* len */
259                 tprintf(", %lu, ", tcp->u_arg[1]);
260                 /* flags */
261                 printflags(mctl_sync, tcp->u_arg[2], "MS_???");
262         }
263         return 0;
264 }
265
266 #endif /* MS_ASYNC */
267
268 #ifdef MC_SYNC
269
270 #include "xlat/mctl_funcs.h"
271
272 #include "xlat/mctl_lockas.h"
273
274 int
275 sys_mctl(struct tcb *tcp)
276 {
277         int arg, function;
278
279         if (entering(tcp)) {
280                 /* addr */
281                 tprintf("%#lx", tcp->u_arg[0]);
282                 /* len */
283                 tprintf(", %lu, ", tcp->u_arg[1]);
284                 /* function */
285                 function = tcp->u_arg[2];
286                 printflags(mctl_funcs, function, "MC_???");
287                 /* arg */
288                 arg = tcp->u_arg[3];
289                 tprints(", ");
290                 switch (function) {
291                 case MC_SYNC:
292                         printflags(mctl_sync, arg, "MS_???");
293                         break;
294                 case MC_LOCKAS:
295                         printflags(mctl_lockas, arg, "MCL_???");
296                         break;
297                 default:
298                         tprintf("%#x", arg);
299                         break;
300                 }
301         }
302         return 0;
303 }
304
305 #endif /* MC_SYNC */
306
307 int
308 sys_mincore(struct tcb *tcp)
309 {
310         if (entering(tcp)) {
311                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
312         } else {
313                 unsigned long i, len;
314                 char *vec = NULL;
315
316                 len = tcp->u_arg[1];
317                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
318                         (vec = malloc(len)) == NULL ||
319                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
320                         tprintf("%#lx", tcp->u_arg[2]);
321                 else {
322                         tprints("[");
323                         for (i = 0; i < len; i++) {
324                                 if (abbrev(tcp) && i >= max_strlen) {
325                                         tprints("...");
326                                         break;
327                                 }
328                                 tprints((vec[i] & 1) ? "1" : "0");
329                         }
330                         tprints("]");
331                 }
332                 free(vec);
333         }
334         return 0;
335 }
336
337 #if defined(ALPHA) || defined(IA64) || defined(SPARC) || defined(SPARC64)
338 int
339 sys_getpagesize(struct tcb *tcp)
340 {
341         if (exiting(tcp))
342                 return RVAL_HEX;
343         return 0;
344 }
345 #endif
346
347 int
348 sys_remap_file_pages(struct tcb *tcp)
349 {
350         if (entering(tcp)) {
351                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
352                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
353                 tprintf(", %lu, ", tcp->u_arg[3]);
354 #ifdef MAP_TYPE
355                 printxval(mmap_flags, tcp->u_arg[4] & MAP_TYPE, "MAP_???");
356                 addflags(mmap_flags, tcp->u_arg[4] & ~MAP_TYPE);
357 #else
358                 printflags(mmap_flags, tcp->u_arg[4], "MAP_???");
359 #endif
360         }
361         return 0;
362 }
363
364 #define MPOL_DEFAULT    0
365 #define MPOL_PREFERRED  1
366 #define MPOL_BIND       2
367 #define MPOL_INTERLEAVE 3
368
369 #define MPOL_F_NODE     (1<<0)
370 #define MPOL_F_ADDR     (1<<1)
371
372 #define MPOL_MF_STRICT  (1<<0)
373 #define MPOL_MF_MOVE    (1<<1)
374 #define MPOL_MF_MOVE_ALL (1<<2)
375
376 #include "xlat/policies.h"
377
378 #include "xlat/mbindflags.h"
379
380 #include "xlat/mempolicyflags.h"
381
382 #include "xlat/move_pages_flags.h"
383
384 static void
385 get_nodes(struct tcb *tcp, unsigned long ptr, unsigned long maxnodes, int err)
386 {
387         unsigned long nlongs, size, end;
388
389         nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
390         size = nlongs * sizeof(long);
391         end = ptr + size;
392         if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
393                             && (end > ptr))) {
394                 unsigned long n, cur, abbrev_end;
395                 int failed = 0;
396
397                 if (abbrev(tcp)) {
398                         abbrev_end = ptr + max_strlen * sizeof(long);
399                         if (abbrev_end < ptr)
400                                 abbrev_end = end;
401                 } else {
402                         abbrev_end = end;
403                 }
404                 tprints(", {");
405                 for (cur = ptr; cur < end; cur += sizeof(long)) {
406                         if (cur > ptr)
407                                 tprints(", ");
408                         if (cur >= abbrev_end) {
409                                 tprints("...");
410                                 break;
411                         }
412                         if (umoven(tcp, cur, sizeof(n), (char *) &n) < 0) {
413                                 tprints("?");
414                                 failed = 1;
415                                 break;
416                         }
417                         tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
418                 }
419                 tprints("}");
420                 if (failed)
421                         tprintf(" %#lx", ptr);
422         } else
423                 tprintf(", %#lx", ptr);
424         tprintf(", %lu", maxnodes);
425 }
426
427 int
428 sys_mbind(struct tcb *tcp)
429 {
430         if (entering(tcp)) {
431                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
432                 printxval(policies, tcp->u_arg[2], "MPOL_???");
433                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
434                 tprints(", ");
435                 printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
436         }
437         return 0;
438 }
439
440 int
441 sys_set_mempolicy(struct tcb *tcp)
442 {
443         if (entering(tcp)) {
444                 printxval(policies, tcp->u_arg[0], "MPOL_???");
445                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
446         }
447         return 0;
448 }
449
450 int
451 sys_get_mempolicy(struct tcb *tcp)
452 {
453         if (exiting(tcp)) {
454                 int pol;
455                 if (tcp->u_arg[0] == 0)
456                         tprints("NULL");
457                 else if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &pol) < 0)
458                         tprintf("%#lx", tcp->u_arg[0]);
459                 else
460                         printxval(policies, pol, "MPOL_???");
461                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
462                 tprintf(", %#lx, ", tcp->u_arg[3]);
463                 printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
464         }
465         return 0;
466 }
467
468 int
469 sys_migrate_pages(struct tcb *tcp)
470 {
471         if (entering(tcp)) {
472                 tprintf("%ld, ", (long) (pid_t) tcp->u_arg[0]);
473                 get_nodes(tcp, tcp->u_arg[2], tcp->u_arg[1], 0);
474                 tprints(", ");
475                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[1], 0);
476         }
477         return 0;
478 }
479
480 int
481 sys_move_pages(struct tcb *tcp)
482 {
483         if (entering(tcp)) {
484                 unsigned long npages = tcp->u_arg[1];
485                 tprintf("%ld, %lu, ", tcp->u_arg[0], npages);
486                 if (tcp->u_arg[2] == 0)
487                         tprints("NULL, ");
488                 else {
489                         int i;
490                         long puser = tcp->u_arg[2];
491                         tprints("{");
492                         for (i = 0; i < npages; ++i) {
493                                 void *p;
494                                 if (i > 0)
495                                         tprints(", ");
496                                 if (umove(tcp, puser, &p) < 0) {
497                                         tprints("???");
498                                         break;
499                                 }
500                                 tprintf("%p", p);
501                                 puser += sizeof(void *);
502                         }
503                         tprints("}, ");
504                 }
505                 if (tcp->u_arg[3] == 0)
506                         tprints("NULL, ");
507                 else {
508                         int i;
509                         long nodeuser = tcp->u_arg[3];
510                         tprints("{");
511                         for (i = 0; i < npages; ++i) {
512                                 int node;
513                                 if (i > 0)
514                                         tprints(", ");
515                                 if (umove(tcp, nodeuser, &node) < 0) {
516                                         tprints("???");
517                                         break;
518                                 }
519                                 tprintf("%#x", node);
520                                 nodeuser += sizeof(int);
521                         }
522                         tprints("}, ");
523                 }
524         }
525         if (exiting(tcp)) {
526                 unsigned long npages = tcp->u_arg[1];
527                 if (tcp->u_arg[4] == 0)
528                         tprints("NULL, ");
529                 else {
530                         int i;
531                         long statususer = tcp->u_arg[4];
532                         tprints("{");
533                         for (i = 0; i < npages; ++i) {
534                                 int status;
535                                 if (i > 0)
536                                         tprints(", ");
537                                 if (umove(tcp, statususer, &status) < 0) {
538                                         tprints("???");
539                                         break;
540                                 }
541                                 tprintf("%#x", status);
542                                 statususer += sizeof(int);
543                         }
544                         tprints("}, ");
545                 }
546                 printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
547         }
548         return 0;
549 }
550
551 #if defined(POWERPC)
552 int
553 sys_subpage_prot(struct tcb *tcp)
554 {
555         if (entering(tcp)) {
556                 unsigned long cur, end, abbrev_end, entries;
557                 unsigned int entry;
558
559                 tprintf("%#lx, %#lx, ", tcp->u_arg[0], tcp->u_arg[1]);
560                 entries = tcp->u_arg[1] >> 16;
561                 if (!entries || !tcp->u_arg[2]) {
562                         tprints("{}");
563                         return 0;
564                 }
565                 cur = tcp->u_arg[2];
566                 end = cur + (sizeof(int) * entries);
567                 if (!verbose(tcp) || end < tcp->u_arg[2]) {
568                         tprintf("%#lx", tcp->u_arg[2]);
569                         return 0;
570                 }
571                 if (abbrev(tcp)) {
572                         abbrev_end = cur + (sizeof(int) * max_strlen);
573                         if (abbrev_end > end)
574                                 abbrev_end = end;
575                 }
576                 else
577                         abbrev_end = end;
578                 tprints("{");
579                 for (; cur < end; cur += sizeof(int)) {
580                         if (cur > tcp->u_arg[2])
581                                 tprints(", ");
582                         if (cur >= abbrev_end) {
583                                 tprints("...");
584                                 break;
585                         }
586                         if (umove(tcp, cur, &entry) < 0) {
587                                 tprintf("??? [%#lx]", cur);
588                                 break;
589                         }
590                         else
591                                 tprintf("%#08x", entry);
592                 }
593                 tprints("}");
594         }
595
596         return 0;
597 }
598 #endif