]> granicus.if.org Git - strace/blob - mem.c
Cache tcp->u_nargs in a local variable for for() loops
[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  *      $Id$
33  */
34
35 #include "defs.h"
36
37 #ifdef LINUX
38 #include <asm/mman.h>
39 #endif
40 #include <sys/mman.h>
41
42 #if defined(LINUX) && defined(I386)
43 # include <asm/ldt.h>
44 # ifdef HAVE_STRUCT_USER_DESC
45 #  define modify_ldt_ldt_s user_desc
46 # endif
47 #endif
48 #if defined(LINUX) && defined(SH64)
49 # include <asm/page.h>      /* for PAGE_SHIFT */
50 #endif
51
52 #ifdef HAVE_LONG_LONG_OFF_T
53 /*
54  * Ugly hacks for systems that have a long long off_t
55  */
56 #define sys_mmap64      sys_mmap
57 #endif
58
59 int
60 sys_brk(struct tcb *tcp)
61 {
62         if (entering(tcp)) {
63                 tprintf("%#lx", tcp->u_arg[0]);
64         }
65 #ifdef LINUX
66         return RVAL_HEX;
67 #else
68         return 0;
69 #endif
70 }
71
72 #if defined(FREEBSD) || defined(SUNOS4)
73 int
74 sys_sbrk(struct tcb *tcp)
75 {
76         if (entering(tcp)) {
77                 tprintf("%lu", tcp->u_arg[0]);
78         }
79         return RVAL_HEX;
80 }
81 #endif /* FREEBSD || SUNOS4 */
82
83 static const struct xlat mmap_prot[] = {
84         { PROT_NONE,    "PROT_NONE",    },
85         { PROT_READ,    "PROT_READ"     },
86         { PROT_WRITE,   "PROT_WRITE"    },
87         { PROT_EXEC,    "PROT_EXEC"     },
88 #ifdef PROT_SEM
89         { PROT_SEM,     "PROT_SEM"      },
90 #endif
91 #ifdef PROT_GROWSDOWN
92         { PROT_GROWSDOWN,"PROT_GROWSDOWN"},
93 #endif
94 #ifdef PROT_GROWSUP
95         { PROT_GROWSUP, "PROT_GROWSUP"  },
96 #endif
97 #ifdef PROT_SAO
98         { PROT_SAO,     "PROT_SAO"      },
99 #endif
100         { 0,            NULL            },
101 };
102
103 static const struct xlat mmap_flags[] = {
104         { MAP_SHARED,   "MAP_SHARED"    },
105         { MAP_PRIVATE,  "MAP_PRIVATE"   },
106         { MAP_FIXED,    "MAP_FIXED"     },
107 #ifdef MAP_ANONYMOUS
108         { MAP_ANONYMOUS,"MAP_ANONYMOUS" },
109 #endif
110 #ifdef MAP_32BIT
111         { MAP_32BIT,    "MAP_32BIT"     },
112 #endif
113 #ifdef MAP_RENAME
114         { MAP_RENAME,   "MAP_RENAME"    },
115 #endif
116 #ifdef MAP_NORESERVE
117         { MAP_NORESERVE,"MAP_NORESERVE" },
118 #endif
119 #ifdef MAP_POPULATE
120         { MAP_POPULATE, "MAP_POPULATE" },
121 #endif
122 #ifdef MAP_NONBLOCK
123         { MAP_NONBLOCK, "MAP_NONBLOCK" },
124 #endif
125         /*
126          * XXX - this was introduced in SunOS 4.x to distinguish between
127          * the old pre-4.x "mmap()", which:
128          *
129          *      only let you map devices with an "mmap" routine (e.g.,
130          *      frame buffers) in;
131          *
132          *      required you to specify the mapping address;
133          *
134          *      returned 0 on success and -1 on failure;
135          *
136          * memory and which, and the 4.x "mmap()" which:
137          *
138          *      can map plain files;
139          *
140          *      can be asked to pick where to map the file;
141          *
142          *      returns the address where it mapped the file on success
143          *      and -1 on failure.
144          *
145          * It's not actually used in source code that calls "mmap()"; the
146          * "mmap()" routine adds it for you.
147          *
148          * It'd be nice to come up with some way of eliminating it from
149          * the flags, e.g. reporting calls *without* it as "old_mmap()"
150          * and calls with it as "mmap()".
151          */
152 #ifdef _MAP_NEW
153         { _MAP_NEW,     "_MAP_NEW"      },
154 #endif
155 #ifdef MAP_GROWSDOWN
156         { MAP_GROWSDOWN,"MAP_GROWSDOWN" },
157 #endif
158 #ifdef MAP_DENYWRITE
159         { MAP_DENYWRITE,"MAP_DENYWRITE" },
160 #endif
161 #ifdef MAP_EXECUTABLE
162         { MAP_EXECUTABLE,"MAP_EXECUTABLE"},
163 #endif
164 #ifdef MAP_INHERIT
165         { MAP_INHERIT,"MAP_INHERIT"     },
166 #endif
167 #ifdef MAP_FILE
168         { MAP_FILE,"MAP_FILE"},
169 #endif
170 #ifdef MAP_LOCKED
171         { MAP_LOCKED,"MAP_LOCKED"},
172 #endif
173         /* FreeBSD ones */
174 #ifdef MAP_ANON
175         { MAP_ANON,             "MAP_ANON"      },
176 #endif
177 #ifdef MAP_HASSEMAPHORE
178         { MAP_HASSEMAPHORE,     "MAP_HASSEMAPHORE"      },
179 #endif
180 #ifdef MAP_STACK
181         { MAP_STACK,            "MAP_STACK"     },
182 #endif
183 #ifdef MAP_NOSYNC
184         { MAP_NOSYNC,           "MAP_NOSYNC"    },
185 #endif
186 #ifdef MAP_NOCORE
187         { MAP_NOCORE,           "MAP_NOCORE"    },
188 #endif
189 #ifdef TILE
190         { MAP_CACHE_NO_LOCAL, "MAP_CACHE_NO_LOCAL" },
191         { MAP_CACHE_NO_L2, "MAP_CACHE_NO_L2" },
192         { MAP_CACHE_NO_L1, "MAP_CACHE_NO_L1" },
193 #endif
194         { 0,            NULL            },
195 };
196
197 #ifdef TILE
198 static int
199 addtileflags(long flags)
200 {
201         long home = flags & _MAP_CACHE_MKHOME(_MAP_CACHE_HOME_MASK);
202         flags &= ~_MAP_CACHE_MKHOME(_MAP_CACHE_HOME_MASK);
203
204         if (flags & _MAP_CACHE_INCOHERENT) {
205                 flags &= ~_MAP_CACHE_INCOHERENT;
206                 if (home == MAP_CACHE_HOME_NONE) {
207                         tprintf("|MAP_CACHE_INCOHERENT");
208                         return flags;
209                 }
210                 tprintf("|_MAP_CACHE_INCOHERENT");
211         }
212
213         switch (home) {
214         case 0: break;
215         case MAP_CACHE_HOME_HERE: tprintf("|MAP_CACHE_HOME_HERE"); break;
216         case MAP_CACHE_HOME_NONE: tprintf("|MAP_CACHE_HOME_NONE"); break;
217         case MAP_CACHE_HOME_SINGLE: tprintf("|MAP_CACHE_HOME_SINGLE"); break;
218         case MAP_CACHE_HOME_TASK: tprintf("|MAP_CACHE_HOME_TASK"); break;
219         case MAP_CACHE_HOME_HASH: tprintf("|MAP_CACHE_HOME_HASH"); break;
220         default:
221                 tprintf("|MAP_CACHE_HOME(%d)",
222                         (home >> _MAP_CACHE_HOME_SHIFT) );
223                 break;
224         }
225
226         return flags;
227 }
228 #endif
229
230 #if !HAVE_LONG_LONG_OFF_T
231 static int
232 print_mmap(struct tcb *tcp, long *u_arg, long long offset)
233 {
234         if (entering(tcp)) {
235                 /* addr */
236                 if (!u_arg[0])
237                         tprintf("NULL, ");
238                 else
239                         tprintf("%#lx, ", u_arg[0]);
240                 /* len */
241                 tprintf("%lu, ", u_arg[1]);
242                 /* prot */
243                 printflags(mmap_prot, u_arg[2], "PROT_???");
244                 tprintf(", ");
245                 /* flags */
246 #ifdef MAP_TYPE
247                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
248 #ifdef TILE
249                 addflags(mmap_flags, addtileflags(u_arg[3] & ~MAP_TYPE));
250 #else
251                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
252 #endif
253 #else
254                 printflags(mmap_flags, u_arg[3], "MAP_???");
255 #endif
256                 /* fd */
257                 tprintf(", ");
258                 printfd(tcp, u_arg[4]);
259                 /* offset */
260                 tprintf(", %#llx", offset);
261         }
262         return RVAL_HEX;
263 }
264
265 #ifdef LINUX
266 int sys_old_mmap(struct tcb *tcp)
267 {
268 #if defined(IA64)
269         /*
270          * IA64 processes never call this routine, they only use the
271          * new `sys_mmap' interface.
272          * For IA32 processes, this code converts the integer arguments
273          * that they pushed onto the stack, into longs.
274          *
275          * Note that addresses with bit 31 set will be sign extended.
276          * Fortunately, those addresses are not currently being generated
277          * for IA32 processes so it's not a problem.
278          */
279         int i;
280         long u_arg[6];
281         int narrow_arg[6];
282         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
283                 return 0;
284         for (i = 0; i < 6; i++)
285                 u_arg[i] = narrow_arg[i];
286 #elif defined(SH) || defined(SH64)
287         /* SH has always passed the args in registers */
288         long *u_arg = tcp->u_arg;
289 #else
290         long u_arg[6];
291 # if defined(X86_64)
292         if (current_personality == 1) {
293                 int i;
294                 unsigned narrow_arg[6];
295                 if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
296                         return 0;
297                 for (i = 0; i < 6; ++i)
298                         u_arg[i] = narrow_arg[i];
299         }
300         else
301 # endif
302         if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
303                 return 0;
304 #endif /* other architectures */
305
306         return print_mmap(tcp, u_arg, u_arg[5]);
307 }
308 #endif /* LINUX */
309
310 int
311 sys_mmap(struct tcb *tcp)
312 {
313         long long offset = tcp->u_arg[5];
314
315         /* FIXME: why only SH64? i386 mmap2 syscall ends up
316          * in this function, but does not convert offset
317          * from pages to bytes. See test/mmap_offset_decode.c
318          * Why SH64 and i386 are handled differently?
319          */
320 #if defined(LINUX) && defined(SH64)
321         /*
322          * Old mmap differs from new mmap in specifying the
323          * offset in units of bytes rather than pages.  We
324          * pretend it's in byte units so the user only ever
325          * sees bytes in the printout.
326          */
327         offset <<= PAGE_SHIFT;
328 #endif
329 #if defined(LINUX_MIPSN32)
330         offset = tcp->ext_arg[5];
331 #endif
332         return print_mmap(tcp, tcp->u_arg, offset);
333 }
334 #endif /* !HAVE_LONG_LONG_OFF_T */
335
336 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
337 int
338 sys_mmap64(struct tcb *tcp)
339 {
340         if (entering(tcp)) {
341 #if !defined(LINUX) || defined(ALPHA)
342                 long *u_arg = tcp->u_arg;
343 #else
344                 long u_arg[7];
345                 if (umoven(tcp, tcp->u_arg[0], sizeof u_arg,
346                                 (char *) u_arg) == -1)
347                         return 0;
348 #endif
349                 /* addr */
350                 tprintf("%#lx, ", u_arg[0]);
351                 /* len */
352                 tprintf("%lu, ", u_arg[1]);
353                 /* prot */
354                 printflags(mmap_prot, u_arg[2], "PROT_???");
355                 tprintf(", ");
356                 /* flags */
357 #ifdef MAP_TYPE
358                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
359                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
360 #else
361                 printflags(mmap_flags, u_arg[3], "MAP_???");
362 #endif
363                 /* fd */
364                 tprintf(", ");
365         /* BUG?! should be u_arg[4] (without tcp->)? */
366                 printfd(tcp, tcp->u_arg[4]);
367                 /* offset */
368         /* BUG?! on non-ALPHA linux, offset will be not in tcp->u_arg,
369          * but in local u_arg, but printllval prints tcp->u_arg! */
370                 printllval(tcp, ", %#llx", 5);
371         }
372         return RVAL_HEX;
373 }
374 #endif /* _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T */
375
376
377 int
378 sys_munmap(struct tcb *tcp)
379 {
380         if (entering(tcp)) {
381                 tprintf("%#lx, %lu",
382                         tcp->u_arg[0], tcp->u_arg[1]);
383         }
384         return 0;
385 }
386
387 int
388 sys_mprotect(struct tcb *tcp)
389 {
390         if (entering(tcp)) {
391                 tprintf("%#lx, %lu, ",
392                         tcp->u_arg[0], tcp->u_arg[1]);
393                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
394         }
395         return 0;
396 }
397
398 #ifdef LINUX
399
400 static const struct xlat mremap_flags[] = {
401         { MREMAP_MAYMOVE,       "MREMAP_MAYMOVE"        },
402 #ifdef MREMAP_FIXED
403         { MREMAP_FIXED,         "MREMAP_FIXED"          },
404 #endif
405         { 0,                    NULL                    }
406 };
407
408 int
409 sys_mremap(struct tcb *tcp)
410 {
411         if (entering(tcp)) {
412                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
413                         tcp->u_arg[2]);
414                 printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
415 #ifdef MREMAP_FIXED
416                 if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
417                     (MREMAP_MAYMOVE | MREMAP_FIXED))
418                         tprintf(", %#lx", tcp->u_arg[4]);
419 #endif
420         }
421         return RVAL_HEX;
422 }
423
424 static const struct xlat madvise_cmds[] = {
425 #ifdef MADV_NORMAL
426         { MADV_NORMAL,          "MADV_NORMAL" },
427 #endif
428 #ifdef MADV_RANDOM
429         { MADV_RANDOM,          "MADV_RANDOM" },
430 #endif
431 #ifdef MADV_SEQUENTIAL
432         { MADV_SEQUENTIAL,      "MADV_SEQUENTIAL" },
433 #endif
434 #ifdef MADV_WILLNEED
435         { MADV_WILLNEED,        "MADV_WILLNEED" },
436 #endif
437 #ifdef MADV_DONTNEED
438         { MADV_DONTNEED,        "MADV_DONTNEED" },
439 #endif
440         { 0,                    NULL },
441 };
442
443
444 int
445 sys_madvise(struct tcb *tcp)
446 {
447         if (entering(tcp)) {
448                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
449                 printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
450         }
451         return 0;
452 }
453
454
455 static const struct xlat mlockall_flags[] = {
456 #ifdef MCL_CURRENT
457         { MCL_CURRENT,  "MCL_CURRENT" },
458 #endif
459 #ifdef MCL_FUTURE
460         { MCL_FUTURE,   "MCL_FUTURE" },
461 #endif
462         { 0,            NULL}
463 };
464
465 int
466 sys_mlockall(struct tcb *tcp)
467 {
468         if (entering(tcp)) {
469                 printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
470         }
471         return 0;
472 }
473
474
475 #endif /* LINUX */
476
477 #ifdef MS_ASYNC
478
479 static const struct xlat mctl_sync[] = {
480 #ifdef MS_SYNC
481         { MS_SYNC,      "MS_SYNC"       },
482 #endif
483         { MS_ASYNC,     "MS_ASYNC"      },
484         { MS_INVALIDATE,"MS_INVALIDATE" },
485         { 0,            NULL            },
486 };
487
488 int
489 sys_msync(struct tcb *tcp)
490 {
491         if (entering(tcp)) {
492                 /* addr */
493                 tprintf("%#lx", tcp->u_arg[0]);
494                 /* len */
495                 tprintf(", %lu, ", tcp->u_arg[1]);
496                 /* flags */
497                 printflags(mctl_sync, tcp->u_arg[2], "MS_???");
498         }
499         return 0;
500 }
501
502 #endif /* MS_ASYNC */
503
504 #ifdef MC_SYNC
505
506 static const struct xlat mctl_funcs[] = {
507         { MC_LOCK,      "MC_LOCK"       },
508         { MC_LOCKAS,    "MC_LOCKAS"     },
509         { MC_SYNC,      "MC_SYNC"       },
510         { MC_UNLOCK,    "MC_UNLOCK"     },
511         { MC_UNLOCKAS,  "MC_UNLOCKAS"   },
512         { 0,            NULL            },
513 };
514
515 static const struct xlat mctl_lockas[] = {
516         { MCL_CURRENT,  "MCL_CURRENT"   },
517         { MCL_FUTURE,   "MCL_FUTURE"    },
518         { 0,            NULL            },
519 };
520
521 int
522 sys_mctl(struct tcb *tcp)
523 {
524         int arg, function;
525
526         if (entering(tcp)) {
527                 /* addr */
528                 tprintf("%#lx", tcp->u_arg[0]);
529                 /* len */
530                 tprintf(", %lu, ", tcp->u_arg[1]);
531                 /* function */
532                 function = tcp->u_arg[2];
533                 printflags(mctl_funcs, function, "MC_???");
534                 /* arg */
535                 arg = tcp->u_arg[3];
536                 tprintf(", ");
537                 switch (function) {
538                 case MC_SYNC:
539                         printflags(mctl_sync, arg, "MS_???");
540                         break;
541                 case MC_LOCKAS:
542                         printflags(mctl_lockas, arg, "MCL_???");
543                         break;
544                 default:
545                         tprintf("%#x", arg);
546                         break;
547                 }
548         }
549         return 0;
550 }
551
552 #endif /* MC_SYNC */
553
554 int
555 sys_mincore(struct tcb *tcp)
556 {
557         unsigned long i, len;
558         char *vec = NULL;
559
560         if (entering(tcp)) {
561                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
562         } else {
563                 len = tcp->u_arg[1];
564                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
565                         (vec = malloc(len)) == NULL ||
566                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
567                         tprintf("%#lx", tcp->u_arg[2]);
568                 else {
569                         tprintf("[");
570                         for (i = 0; i < len; i++) {
571                                 if (abbrev(tcp) && i >= max_strlen) {
572                                         tprintf("...");
573                                         break;
574                                 }
575                                 tprintf((vec[i] & 1) ? "1" : "0");
576                         }
577                         tprintf("]");
578                 }
579                 if (vec)
580                         free(vec);
581         }
582         return 0;
583 }
584
585 #if defined(ALPHA) || defined(FREEBSD) || defined(IA64) || defined(SUNOS4) || defined(SVR4) || defined(SPARC) || defined(SPARC64)
586 int
587 sys_getpagesize(struct tcb *tcp)
588 {
589         if (exiting(tcp))
590                 return RVAL_HEX;
591         return 0;
592 }
593 #endif /* ALPHA || FREEBSD || IA64 || SUNOS4 || SVR4 */
594
595 #if defined(LINUX) && defined(__i386__)
596 void
597 print_ldt_entry(struct modify_ldt_ldt_s *ldt_entry)
598 {
599         tprintf("base_addr:%#08lx, "
600                 "limit:%d, "
601                 "seg_32bit:%d, "
602                 "contents:%d, "
603                 "read_exec_only:%d, "
604                 "limit_in_pages:%d, "
605                 "seg_not_present:%d, "
606                 "useable:%d}",
607                 (long) ldt_entry->base_addr,
608                 ldt_entry->limit,
609                 ldt_entry->seg_32bit,
610                 ldt_entry->contents,
611                 ldt_entry->read_exec_only,
612                 ldt_entry->limit_in_pages,
613                 ldt_entry->seg_not_present,
614                 ldt_entry->useable);
615 }
616
617 int
618 sys_modify_ldt(struct tcb *tcp)
619 {
620         if (entering(tcp)) {
621                 struct modify_ldt_ldt_s copy;
622                 tprintf("%ld", tcp->u_arg[0]);
623                 if (tcp->u_arg[1] == 0
624                                 || tcp->u_arg[2] != sizeof(struct modify_ldt_ldt_s)
625                                 || umove(tcp, tcp->u_arg[1], &copy) == -1)
626                         tprintf(", %lx", tcp->u_arg[1]);
627                 else {
628                         tprintf(", {entry_number:%d, ", copy.entry_number);
629                         if (!verbose(tcp))
630                                 tprintf("...}");
631                         else {
632                                 print_ldt_entry(&copy);
633                         }
634                 }
635                 tprintf(", %lu", tcp->u_arg[2]);
636         }
637         return 0;
638 }
639
640 int
641 sys_set_thread_area(struct tcb *tcp)
642 {
643         struct modify_ldt_ldt_s copy;
644         if (entering(tcp)) {
645                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
646                         if (copy.entry_number == -1)
647                                 tprintf("{entry_number:%d -> ",
648                                         copy.entry_number);
649                         else
650                                 tprintf("{entry_number:");
651                 }
652         } else {
653                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
654                         tprintf("%d, ", copy.entry_number);
655                         if (!verbose(tcp))
656                                 tprintf("...}");
657                         else {
658                                 print_ldt_entry(&copy);
659                         }
660                 } else {
661                         tprintf("%lx", tcp->u_arg[0]);
662                 }
663         }
664         return 0;
665
666 }
667
668 int
669 sys_get_thread_area(struct tcb *tcp)
670 {
671         struct modify_ldt_ldt_s copy;
672         if (exiting(tcp)) {
673                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
674                         tprintf("{entry_number:%d, ", copy.entry_number);
675                         if (!verbose(tcp))
676                                 tprintf("...}");
677                         else {
678                                 print_ldt_entry(&copy);
679                         }
680                 } else {
681                         tprintf("%lx", tcp->u_arg[0]);
682                 }
683         }
684         return 0;
685
686 }
687 #endif /* LINUX && __i386__ */
688
689 #if defined(LINUX) && defined(M68K)
690
691 int
692 sys_set_thread_area(struct tcb *tcp)
693 {
694         if (entering(tcp))
695                 tprintf("%#lx", tcp->u_arg[0]);
696         return 0;
697
698 }
699
700 int
701 sys_get_thread_area(struct tcb *tcp)
702 {
703         return RVAL_HEX;
704 }
705 #endif
706
707 #if defined(LINUX)
708 int
709 sys_remap_file_pages(struct tcb *tcp)
710 {
711         if (entering(tcp)) {
712                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
713                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
714                 tprintf(", %lu, ", tcp->u_arg[3]);
715 #ifdef MAP_TYPE
716                 printxval(mmap_flags, tcp->u_arg[4] & MAP_TYPE, "MAP_???");
717                 addflags(mmap_flags, tcp->u_arg[4] & ~MAP_TYPE);
718 #else
719                 printflags(mmap_flags, tcp->u_arg[4], "MAP_???");
720 #endif
721         }
722         return 0;
723 }
724
725
726 #define MPOL_DEFAULT    0
727 #define MPOL_PREFERRED  1
728 #define MPOL_BIND       2
729 #define MPOL_INTERLEAVE 3
730
731 #define MPOL_F_NODE     (1<<0)
732 #define MPOL_F_ADDR     (1<<1)
733
734 #define MPOL_MF_STRICT  (1<<0)
735 #define MPOL_MF_MOVE    (1<<1)
736 #define MPOL_MF_MOVE_ALL (1<<2)
737
738
739 static const struct xlat policies[] = {
740         { MPOL_DEFAULT,         "MPOL_DEFAULT"          },
741         { MPOL_PREFERRED,       "MPOL_PREFERRED"        },
742         { MPOL_BIND,            "MPOL_BIND"             },
743         { MPOL_INTERLEAVE,      "MPOL_INTERLEAVE"       },
744         { 0,                    NULL                    }
745 };
746
747 static const struct xlat mbindflags[] = {
748         { MPOL_MF_STRICT,       "MPOL_MF_STRICT"        },
749         { MPOL_MF_MOVE,         "MPOL_MF_MOVE"          },
750         { MPOL_MF_MOVE_ALL,     "MPOL_MF_MOVE_ALL"      },
751         { 0,                    NULL                    }
752 };
753
754 static const struct xlat mempolicyflags[] = {
755         { MPOL_F_NODE,          "MPOL_F_NODE"           },
756         { MPOL_F_ADDR,          "MPOL_F_ADDR"           },
757         { 0,                    NULL                    }
758 };
759
760 static const struct xlat move_pages_flags[] = {
761         { MPOL_MF_MOVE,         "MPOL_MF_MOVE"          },
762         { MPOL_MF_MOVE_ALL,     "MPOL_MF_MOVE_ALL"      },
763         { 0,                    NULL                    }
764 };
765
766
767 static void
768 get_nodes(struct tcb *tcp, unsigned long ptr, unsigned long maxnodes, int err)
769 {
770         unsigned long nlongs, size, end;
771
772         nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
773         size = nlongs * sizeof(long);
774         end = ptr + size;
775         if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
776                             && (end > ptr))) {
777                 unsigned long n, cur, abbrev_end;
778                 int failed = 0;
779
780                 if (abbrev(tcp)) {
781                         abbrev_end = ptr + max_strlen * sizeof(long);
782                         if (abbrev_end < ptr)
783                                 abbrev_end = end;
784                 } else {
785                         abbrev_end = end;
786                 }
787                 tprintf(", {");
788                 for (cur = ptr; cur < end; cur += sizeof(long)) {
789                         if (cur > ptr)
790                                 tprintf(", ");
791                         if (cur >= abbrev_end) {
792                                 tprintf("...");
793                                 break;
794                         }
795                         if (umoven(tcp, cur, sizeof(n), (char *) &n) < 0) {
796                                 tprintf("?");
797                                 failed = 1;
798                                 break;
799                         }
800                         tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
801                 }
802                 tprintf("}");
803                 if (failed)
804                         tprintf(" %#lx", ptr);
805         } else
806                 tprintf(", %#lx", ptr);
807         tprintf(", %lu", maxnodes);
808 }
809
810 int
811 sys_mbind(struct tcb *tcp)
812 {
813         if (entering(tcp)) {
814                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
815                 printxval(policies, tcp->u_arg[2], "MPOL_???");
816                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
817                 tprintf(", ");
818                 printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
819         }
820         return 0;
821 }
822
823 int
824 sys_set_mempolicy(struct tcb *tcp)
825 {
826         if (entering(tcp)) {
827                 printxval(policies, tcp->u_arg[0], "MPOL_???");
828                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
829         }
830         return 0;
831 }
832
833 int
834 sys_get_mempolicy(struct tcb *tcp)
835 {
836         if (exiting(tcp)) {
837                 int pol;
838                 if (tcp->u_arg[0] == 0)
839                         tprintf("NULL");
840                 else if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &pol) < 0)
841                         tprintf("%#lx", tcp->u_arg[0]);
842                 else
843                         printxval(policies, pol, "MPOL_???");
844                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
845                 tprintf(", %#lx, ", tcp->u_arg[3]);
846                 printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
847         }
848         return 0;
849 }
850
851 int
852 sys_move_pages(struct tcb *tcp)
853 {
854         if (entering(tcp)) {
855                 unsigned long npages = tcp->u_arg[1];
856                 tprintf("%ld, %lu, ", tcp->u_arg[0], npages);
857                 if (tcp->u_arg[2] == 0)
858                         tprintf("NULL, ");
859                 else {
860                         int i;
861                         long puser = tcp->u_arg[2];
862                         tprintf("{");
863                         for (i = 0; i < npages; ++i) {
864                                 void *p;
865                                 if (i > 0)
866                                         tprintf(", ");
867                                 if (umove(tcp, puser, &p) < 0) {
868                                         tprintf("???");
869                                         break;
870                                 }
871                                 tprintf("%p", p);
872                                 puser += sizeof(void *);
873                         }
874                         tprintf("}, ");
875                 }
876                 if (tcp->u_arg[3] == 0)
877                         tprintf("NULL, ");
878                 else {
879                         int i;
880                         long nodeuser = tcp->u_arg[3];
881                         tprintf("{");
882                         for (i = 0; i < npages; ++i) {
883                                 int node;
884                                 if (i > 0)
885                                         tprintf(", ");
886                                 if (umove(tcp, nodeuser, &node) < 0) {
887                                         tprintf("???");
888                                         break;
889                                 }
890                                 tprintf("%#x", node);
891                                 nodeuser += sizeof(int);
892                         }
893                         tprintf("}, ");
894                 }
895         }
896         if (exiting(tcp)) {
897                 unsigned long npages = tcp->u_arg[1];
898                 if (tcp->u_arg[4] == 0)
899                         tprintf("NULL, ");
900                 else {
901                         int i;
902                         long statususer = tcp->u_arg[4];
903                         tprintf("{");
904                         for (i = 0; i < npages; ++i) {
905                                 int status;
906                                 if (i > 0)
907                                         tprintf(", ");
908                                 if (umove(tcp, statususer, &status) < 0) {
909                                         tprintf("???");
910                                         break;
911                                 }
912                                 tprintf("%#x", status);
913                                 statususer += sizeof(int);
914                         }
915                         tprintf("}, ");
916                 }
917                 printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
918         }
919         return 0;
920 }
921 #endif
922
923 #if defined(LINUX) && defined(POWERPC)
924 int
925 sys_subpage_prot(struct tcb *tcp)
926 {
927         if (entering(tcp)) {
928                 unsigned long cur, end, abbrev_end, entries;
929                 unsigned int entry;
930
931                 tprintf("%#lx, %#lx, ", tcp->u_arg[0], tcp->u_arg[1]);
932                 entries = tcp->u_arg[1] >> 16;
933                 if (!entries || !tcp->u_arg[2]) {
934                         tprintf("{}");
935                         return 0;
936                 }
937                 cur = tcp->u_arg[2];
938                 end = cur + (sizeof(int) * entries);
939                 if (!verbose(tcp) || end < tcp->u_arg[2]) {
940                         tprintf("%#lx", tcp->u_arg[2]);
941                         return 0;
942                 }
943                 if (abbrev(tcp)) {
944                         abbrev_end = cur + (sizeof(int) * max_strlen);
945                         if (abbrev_end > end)
946                                 abbrev_end = end;
947                 }
948                 else
949                         abbrev_end = end;
950                 tprintf("{");
951                 for (; cur < end; cur += sizeof(int)) {
952                         if (cur > tcp->u_arg[2])
953                                 tprintf(", ");
954                         if (cur >= abbrev_end) {
955                                 tprintf("...");
956                                 break;
957                         }
958                         if (umove(tcp, cur, &entry) < 0) {
959                                 tprintf("??? [%#lx]", cur);
960                                 break;
961                         }
962                         else
963                                 tprintf("%#08x", entry);
964                 }
965                 tprintf("}");
966         }
967
968         return 0;
969 }
970 #endif