]> granicus.if.org Git - strace/blob - mem.c
2006-12-10 Dmitry V. Levin <ldv@altlinux.org>
[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(tcp)
61 struct tcb *tcp;
62 {
63         if (entering(tcp)) {
64                 tprintf("%#lx", tcp->u_arg[0]);
65         }
66 #ifdef LINUX
67         return RVAL_HEX;
68 #else
69         return 0;
70 #endif
71 }
72
73 #if defined(FREEBSD) || defined(SUNOS4)
74 int
75 sys_sbrk(tcp)
76 struct tcb *tcp;
77 {
78         if (entering(tcp)) {
79                 tprintf("%lu", tcp->u_arg[0]);
80         }
81         return RVAL_HEX;
82 }
83 #endif /* FREEBSD || SUNOS4 */
84
85 static const struct xlat mmap_prot[] = {
86         { PROT_NONE,    "PROT_NONE",    },
87         { PROT_READ,    "PROT_READ"     },
88         { PROT_WRITE,   "PROT_WRITE"    },
89         { PROT_EXEC,    "PROT_EXEC"     },
90 #ifdef PROT_SEM
91         { PROT_SEM,     "PROT_SEM"      },
92 #endif
93 #ifdef PROT_GROWSDOWN
94         { PROT_GROWSDOWN,"PROT_GROWSDOWN"},
95 #endif
96 #ifdef PROT_GROWSUP
97         { PROT_GROWSUP, "PROT_GROWSUP"  },
98 #endif
99         { 0,            NULL            },
100 };
101
102 static const struct xlat mmap_flags[] = {
103         { MAP_SHARED,   "MAP_SHARED"    },
104         { MAP_PRIVATE,  "MAP_PRIVATE"   },
105         { MAP_FIXED,    "MAP_FIXED"     },
106 #ifdef MAP_ANONYMOUS
107         { MAP_ANONYMOUS,"MAP_ANONYMOUS" },
108 #endif
109 #ifdef MAP_RENAME
110         { MAP_RENAME,   "MAP_RENAME"    },
111 #endif
112 #ifdef MAP_NORESERVE
113         { MAP_NORESERVE,"MAP_NORESERVE" },
114 #endif
115 #ifdef MAP_POPULATE
116         { MAP_POPULATE, "MAP_POPULATE" },
117 #endif
118 #ifdef MAP_NONBLOCK
119         { MAP_NONBLOCK, "MAP_NONBLOCK" },
120 #endif
121         /*
122          * XXX - this was introduced in SunOS 4.x to distinguish between
123          * the old pre-4.x "mmap()", which:
124          *
125          *      only let you map devices with an "mmap" routine (e.g.,
126          *      frame buffers) in;
127          *
128          *      required you to specify the mapping address;
129          *
130          *      returned 0 on success and -1 on failure;
131          *
132          * memory and which, and the 4.x "mmap()" which:
133          *
134          *      can map plain files;
135          *
136          *      can be asked to pick where to map the file;
137          *
138          *      returns the address where it mapped the file on success
139          *      and -1 on failure.
140          *
141          * It's not actually used in source code that calls "mmap()"; the
142          * "mmap()" routine adds it for you.
143          *
144          * It'd be nice to come up with some way of eliminating it from
145          * the flags, e.g. reporting calls *without* it as "old_mmap()"
146          * and calls with it as "mmap()".
147          */
148 #ifdef _MAP_NEW
149         { _MAP_NEW,     "_MAP_NEW"      },
150 #endif
151 #ifdef MAP_GROWSDOWN
152         { MAP_GROWSDOWN,"MAP_GROWSDOWN" },
153 #endif
154 #ifdef MAP_DENYWRITE
155         { MAP_DENYWRITE,"MAP_DENYWRITE" },
156 #endif
157 #ifdef MAP_EXECUTABLE
158         { MAP_EXECUTABLE,"MAP_EXECUTABLE"},
159 #endif
160 #ifdef MAP_INHERIT
161         { MAP_INHERIT,"MAP_INHERIT"     },
162 #endif
163 #ifdef MAP_FILE
164         { MAP_FILE,"MAP_FILE"},
165 #endif
166 #ifdef MAP_LOCKED
167         { MAP_LOCKED,"MAP_LOCKED"},
168 #endif
169         /* FreeBSD ones */
170 #ifdef MAP_ANON
171         { MAP_ANON,             "MAP_ANON"      },
172 #endif
173 #ifdef MAP_HASSEMAPHORE
174         { MAP_HASSEMAPHORE,     "MAP_HASSEMAPHORE"      },
175 #endif
176 #ifdef MAP_STACK
177         { MAP_STACK,            "MAP_STACK"     },
178 #endif
179 #ifdef MAP_NOSYNC
180         { MAP_NOSYNC,           "MAP_NOSYNC"    },
181 #endif
182 #ifdef MAP_NOCORE
183         { MAP_NOCORE,           "MAP_NOCORE"    },
184 #endif
185         { 0,            NULL            },
186 };
187
188 #if !HAVE_LONG_LONG_OFF_T
189 static
190 int
191 print_mmap(tcp,u_arg)
192 struct tcb *tcp;
193 long *u_arg;
194 {
195         if (entering(tcp)) {
196                 /* addr */
197                 if (!u_arg[0])
198                         tprintf("NULL, ");
199                 else
200                         tprintf("%#lx, ", u_arg[0]);
201                 /* len */
202                 tprintf("%lu, ", u_arg[1]);
203                 /* prot */
204                 printflags(mmap_prot, u_arg[2], "PROT_???");
205                 tprintf(", ");
206                 /* flags */
207 #ifdef MAP_TYPE
208                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
209                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
210 #else
211                 printflags(mmap_flags, u_arg[3], "MAP_???");
212 #endif
213                 /* fd (is always int, not long) */
214                 tprintf(", %d, ", (int)u_arg[4]);
215                 /* offset */
216                 tprintf("%#lx", u_arg[5]);
217         }
218         return RVAL_HEX;
219 }
220
221 #ifdef LINUX
222 int sys_old_mmap(tcp)
223 struct tcb *tcp;
224 {
225     long u_arg[6];
226
227 #if     defined(IA64)
228     int i, v;
229     /*
230      *  IA64 processes never call this routine, they only use the
231      *  new `sys_mmap' interface.  This code converts the integer
232      *  arguments that the IA32 process pushed onto the stack into
233      *  longs.
234      *
235      *  Note that addresses with bit 31 set will be sign extended.
236      *  Fortunately, those addresses are not currently being generated
237      *  for IA32 processes so it's not a problem.
238      */
239     for (i = 0; i < 6; i++)
240         if (umove(tcp, tcp->u_arg[0] + (i * sizeof(int)), &v) == -1)
241                 return 0;
242         else
243                 u_arg[i] = v;
244 #elif defined(SH) || defined(SH64)
245     /* SH has always passed the args in registers */
246     int i;
247     for (i=0; i<6; i++)
248         u_arg[i] = tcp->u_arg[i];
249 #else
250 # if defined(X86_64)
251     if (current_personality == 1) {
252             int i;
253             for (i = 0; i < 6; ++i) {
254                     unsigned int val;
255                     if (umove(tcp, tcp->u_arg[0] + i * 4, &val) == -1)
256                             return 0;
257                     u_arg[i] = val;
258             }
259     }
260     else
261 # endif
262     if (umoven(tcp, tcp->u_arg[0], sizeof u_arg, (char *) u_arg) == -1)
263             return 0;
264 #endif  // defined(IA64)
265     return print_mmap(tcp, u_arg);
266
267 }
268 #endif
269
270 int
271 sys_mmap(tcp)
272 struct tcb *tcp;
273 {
274 #if defined(LINUX) && defined(SH64)
275     /*
276      * Old mmap differs from new mmap in specifying the
277      * offset in units of bytes rather than pages.  We
278      * pretend it's in byte units so the user only ever
279      * sees bytes in the printout.
280      */
281     tcp->u_arg[5] <<= PAGE_SHIFT;
282 #endif
283     return print_mmap(tcp, tcp->u_arg);
284 }
285 #endif /* !HAVE_LONG_LONG_OFF_T */
286
287 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
288 int
289 sys_mmap64(tcp)
290 struct tcb *tcp;
291 {
292 #ifdef linux
293 #ifdef ALPHA
294         long *u_arg = tcp->u_arg;
295 #else /* !ALPHA */
296         long u_arg[7];
297 #endif /* !ALPHA */
298 #else /* !linux */
299         long *u_arg = tcp->u_arg;
300 #endif /* !linux */
301
302         if (entering(tcp)) {
303 #ifdef linux
304 #ifndef ALPHA
305                 if (umoven(tcp, tcp->u_arg[0], sizeof u_arg,
306                                 (char *) u_arg) == -1)
307                         return 0;
308 #endif /* ALPHA */
309 #endif /* linux */
310                 ALIGN64 (tcp, 5);       /* FreeBSD wierdies */
311
312                 /* addr */
313                 tprintf("%#lx, ", u_arg[0]);
314                 /* len */
315                 tprintf("%lu, ", u_arg[1]);
316                 /* prot */
317                 printflags(mmap_prot, u_arg[2], "PROT_???");
318                 tprintf(", ");
319                 /* flags */
320 #ifdef MAP_TYPE
321                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
322                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
323 #else
324                 printflags(mmap_flags, u_arg[3], "MAP_???");
325 #endif
326                 /* fd */
327                 tprintf(", %ld, ", u_arg[4]);
328                 /* offset */
329                 tprintf("%#llx", LONG_LONG(u_arg[5], u_arg[6]));
330         }
331         return RVAL_HEX;
332 }
333 #endif
334
335
336 int
337 sys_munmap(tcp)
338 struct tcb *tcp;
339 {
340         if (entering(tcp)) {
341                 tprintf("%#lx, %lu",
342                         tcp->u_arg[0], tcp->u_arg[1]);
343         }
344         return 0;
345 }
346
347 int
348 sys_mprotect(tcp)
349 struct tcb *tcp;
350 {
351         if (entering(tcp)) {
352                 tprintf("%#lx, %lu, ",
353                         tcp->u_arg[0], tcp->u_arg[1]);
354                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
355         }
356         return 0;
357 }
358
359 #ifdef LINUX
360
361 static const struct xlat mremap_flags[] = {
362         { MREMAP_MAYMOVE,       "MREMAP_MAYMOVE"        },
363         { 0,                    NULL                    }
364 };
365
366 int
367 sys_mremap(tcp)
368 struct tcb *tcp;
369 {
370         if (entering(tcp)) {
371                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
372                         tcp->u_arg[2]);
373                 printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
374         }
375         return RVAL_HEX;
376 }
377
378 static const struct xlat madvise_flags[] = {
379 #ifdef MADV_NORMAL
380         { MADV_NORMAL,          "MADV_NORMAL" },
381 #endif
382 #ifdef MADZV_RANDOM
383         { MADV_RANDOM,          "MADV_RANDOM" },
384 #endif
385 #ifdef MADV_SEQUENTIAL
386         { MADV_SEQUENTIAL,      "MADV_SEQUENTIAL" },
387 #endif
388 #ifdef MADV_WILLNEED
389         { MADV_WILLNEED,        "MADV_WILLNEED" },
390 #endif
391 #ifdef MADV_DONTNED
392         { MADV_DONTNEED,        "MADV_DONTNEED" },
393 #endif
394         { 0,                    NULL },
395 };
396
397
398 int
399 sys_madvise(tcp)
400 struct tcb *tcp;
401 {
402         if (entering(tcp)) {
403                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
404                 printflags(madvise_flags, tcp->u_arg[2], "MADV_???");
405         }
406         return 0;
407 }
408
409
410 static const struct xlat mlockall_flags[] = {
411 #ifdef MCL_CURRENT
412         { MCL_CURRENT,  "MCL_CURRENT" },
413 #endif
414 #ifdef MCL_FUTURE
415         { MCL_FUTURE,   "MCL_FUTURE" },
416 #endif
417         { 0,            NULL}
418 };
419
420 int
421 sys_mlockall(tcp)
422 struct tcb *tcp;
423 {
424         if (entering(tcp)) {
425                 printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
426         }
427         return 0;
428 }
429
430
431 #endif /* LINUX */
432
433 #ifdef MS_ASYNC
434
435 static const struct xlat mctl_sync[] = {
436 #ifdef MS_SYNC
437         { MS_SYNC,      "MS_SYNC"       },
438 #endif
439         { MS_ASYNC,     "MS_ASYNC"      },
440         { MS_INVALIDATE,"MS_INVALIDATE" },
441         { 0,            NULL            },
442 };
443
444 int
445 sys_msync(tcp)
446 struct tcb *tcp;
447 {
448         if (entering(tcp)) {
449                 /* addr */
450                 tprintf("%#lx", tcp->u_arg[0]);
451                 /* len */
452                 tprintf(", %lu, ", tcp->u_arg[1]);
453                 /* flags */
454                 printflags(mctl_sync, tcp->u_arg[2], "MS_???");
455         }
456         return 0;
457 }
458
459 #endif /* MS_ASYNC */
460
461 #ifdef MC_SYNC
462
463 static const struct xlat mctl_funcs[] = {
464         { MC_LOCK,      "MC_LOCK"       },
465         { MC_LOCKAS,    "MC_LOCKAS"     },
466         { MC_SYNC,      "MC_SYNC"       },
467         { MC_UNLOCK,    "MC_UNLOCK"     },
468         { MC_UNLOCKAS,  "MC_UNLOCKAS"   },
469         { 0,            NULL            },
470 };
471
472 static const struct xlat mctl_lockas[] = {
473         { MCL_CURRENT,  "MCL_CURRENT"   },
474         { MCL_FUTURE,   "MCL_FUTURE"    },
475         { 0,            NULL            },
476 };
477
478 int
479 sys_mctl(tcp)
480 struct tcb *tcp;
481 {
482         int arg, function;
483
484         if (entering(tcp)) {
485                 /* addr */
486                 tprintf("%#lx", tcp->u_arg[0]);
487                 /* len */
488                 tprintf(", %lu, ", tcp->u_arg[1]);
489                 /* function */
490                 function = tcp->u_arg[2];
491                 printflags(mctl_funcs, function, "MC_???");
492                 /* arg */
493                 arg = tcp->u_arg[3];
494                 tprintf(", ");
495                 switch (function) {
496                 case MC_SYNC:
497                         printflags(mctl_sync, arg, "MS_???");
498                         break;
499                 case MC_LOCKAS:
500                         printflags(mctl_lockas, arg, "MCL_???");
501                         break;
502                 default:
503                         tprintf("%#x", arg);
504                         break;
505                 }
506         }
507         return 0;
508 }
509
510 #endif /* MC_SYNC */
511
512 int
513 sys_mincore(tcp)
514 struct tcb *tcp;
515 {
516         unsigned long i, len;
517         char *vec = NULL;
518
519         if (entering(tcp)) {
520                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
521         } else {
522                 len = tcp->u_arg[1];
523                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
524                         (vec = malloc(len)) == NULL ||
525                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
526                         tprintf("%#lx", tcp->u_arg[2]);
527                 else {
528                         tprintf("[");
529                         for (i = 0; i < len; i++) {
530                                 if (abbrev(tcp) && i >= max_strlen) {
531                                         tprintf("...");
532                                         break;
533                                 }
534                                 tprintf((vec[i] & 1) ? "1" : "0");
535                         }
536                         tprintf("]");
537                 }
538                 if (vec)
539                         free(vec);
540         }
541         return 0;
542 }
543
544 #if defined(ALPHA) || defined(FREEBSD) || defined(IA64) || defined(SUNOS4) || defined(SVR4)
545 int
546 sys_getpagesize(tcp)
547 struct tcb *tcp;
548 {
549         if (exiting(tcp))
550                 return RVAL_HEX;
551         return 0;
552 }
553 #endif /* ALPHA || FREEBSD || IA64 || SUNOS4 || SVR4 */
554
555 #if defined(LINUX) && defined(__i386__)
556 void
557 print_ldt_entry (ldt_entry)
558 struct modify_ldt_ldt_s *ldt_entry;
559 {
560         tprintf("base_addr:%#08lx, "
561                 "limit:%d, "
562                 "seg_32bit:%d, "
563                 "contents:%d, "
564                 "read_exec_only:%d, "
565                 "limit_in_pages:%d, "
566                 "seg_not_present:%d, "
567                 "useable:%d}",
568                 ldt_entry->base_addr,
569                 ldt_entry->limit,
570                 ldt_entry->seg_32bit,
571                 ldt_entry->contents,
572                 ldt_entry->read_exec_only,
573                 ldt_entry->limit_in_pages,
574                 ldt_entry->seg_not_present,
575                 ldt_entry->useable);
576 }
577
578 int
579 sys_modify_ldt(tcp)
580 struct tcb *tcp;
581 {
582         if (entering(tcp)) {
583                 struct modify_ldt_ldt_s copy;
584                 tprintf("%ld", tcp->u_arg[0]);
585                 if (tcp->u_arg[1] == 0
586                                 || tcp->u_arg[2] != sizeof (struct modify_ldt_ldt_s)
587                                 || umove(tcp, tcp->u_arg[1], &copy) == -1)
588                         tprintf(", %lx", tcp->u_arg[1]);
589                 else {
590                         tprintf(", {entry_number:%d, ", copy.entry_number);
591                         if (!verbose(tcp))
592                                 tprintf("...}");
593                         else {
594                                 print_ldt_entry(&copy);
595                         }
596                 }
597                 tprintf(", %lu", tcp->u_arg[2]);
598         }
599         return 0;
600 }
601
602 int
603 sys_set_thread_area(tcp)
604 struct tcb *tcp;
605 {
606         struct modify_ldt_ldt_s copy;
607         if (entering(tcp)) {
608                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
609                         if (copy.entry_number == -1)
610                                 tprintf("{entry_number:%d -> ",
611                                         copy.entry_number);
612                         else
613                                 tprintf("{entry_number:");
614                 }
615         } else {
616                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
617                         tprintf("%d, ", copy.entry_number);
618                         if (!verbose(tcp))
619                                 tprintf("...}");
620                         else {
621                                 print_ldt_entry(&copy);
622                         }
623                 } else {
624                         tprintf("%lx", tcp->u_arg[0]);
625                 }
626         }
627         return 0;
628
629 }
630
631 int
632 sys_get_thread_area(tcp)
633 struct tcb *tcp;
634 {
635         struct modify_ldt_ldt_s copy;
636         if (exiting(tcp)) {
637                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
638                         tprintf("{entry_number:%d, ", copy.entry_number);
639                         if (!verbose(tcp))
640                                 tprintf("...}");
641                         else {
642                                 print_ldt_entry(&copy);
643                         }
644                 } else {
645                         tprintf("%lx", tcp->u_arg[0]);
646                 }
647         }
648         return 0;
649
650 }
651 #endif /* LINUX && __i386__ */
652
653 #if defined(LINUX)
654 int
655 sys_remap_file_pages(tcp)
656 struct tcb *tcp;
657 {
658         if (entering(tcp)) {
659                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
660                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
661                 tprintf(", %lu, ", tcp->u_arg[3]);
662 #ifdef MAP_TYPE
663                 printxval(mmap_flags, tcp->u_arg[4] & MAP_TYPE, "MAP_???");
664                 addflags(mmap_flags, tcp->u_arg[4] & ~MAP_TYPE);
665 #else
666                 printflags(mmap_flags, tcp->u_arg[4], "MAP_???");
667 #endif
668         }
669         return 0;
670 }
671
672
673 #define MPOL_DEFAULT    0
674 #define MPOL_PREFERRED  1
675 #define MPOL_BIND       2
676 #define MPOL_INTERLEAVE 3
677
678 #define MPOL_F_NODE     (1<<0)
679 #define MPOL_F_ADDR     (1<<1)
680
681 #define MPOL_MF_STRICT  (1<<0)
682
683
684 static const struct xlat policies[] = {
685         { MPOL_DEFAULT,         "MPOL_DEFAULT"          },
686         { MPOL_PREFERRED,       "MPOL_PREFERRED"        },
687         { MPOL_BIND,            "MPOL_BIND"             },
688         { MPOL_INTERLEAVE,      "MPOL_INTERLEAVE"       },
689         { 0,                    NULL                    }
690 };
691
692 static const struct xlat mbindflags[] = {
693         { MPOL_MF_STRICT,       "MPOL_MF_STRICT"        },
694         { 0,                    NULL                    }
695 };
696
697 static const struct xlat mempolicyflags[] = {
698         { MPOL_F_NODE,          "MPOL_F_NODE"           },
699         { MPOL_F_ADDR,          "MPOL_F_ADDR"           },
700         { 0,                    NULL                    }
701 };
702
703
704 static void
705 get_nodes(tcp, ptr, maxnodes, err)
706 struct tcb *tcp;
707 unsigned long ptr;
708 unsigned long maxnodes;
709 int err;
710 {
711         unsigned long nlongs, size, end;
712
713         nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
714         size = nlongs * sizeof(long);
715         end = ptr + size;
716         if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
717                             && (end > ptr))) {
718                 unsigned long n, cur, abbrev_end;
719                 int failed = 0;
720
721                 if (abbrev(tcp)) {
722                         abbrev_end = ptr + max_strlen * sizeof(long);
723                         if (abbrev_end < ptr)
724                                 abbrev_end = end;
725                 } else {
726                         abbrev_end = end;
727                 }
728                 tprintf(", {");
729                 for (cur = ptr; cur < end; cur += sizeof(long)) {
730                         if (cur > ptr)
731                                 tprintf(", ");
732                         if (cur >= abbrev_end) {
733                                 tprintf("...");
734                                 break;
735                         }
736                         if (umoven(tcp, cur, sizeof(n), (char *) &n) < 0) {
737                                 tprintf("?");
738                                 failed = 1;
739                                 break;
740                         }
741                         tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
742                 }
743                 tprintf("}");
744                 if (failed)
745                         tprintf(" %#lx", ptr);
746         } else
747                 tprintf(", %#lx", ptr);
748         tprintf(", %lu", maxnodes);
749 }
750
751 int
752 sys_mbind(tcp)
753 struct tcb *tcp;
754 {
755         if (entering(tcp)) {
756                 tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
757                 printxval(policies, tcp->u_arg[2], "MPOL_???");
758                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
759                 tprintf(", ");
760                 printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
761         }
762         return 0;
763 }
764
765 int
766 sys_set_mempolicy(tcp)
767 struct tcb *tcp;
768 {
769         if (entering(tcp)) {
770                 printxval(policies, tcp->u_arg[0], "MPOL_???");
771                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
772         }
773         return 0;
774 }
775
776 int
777 sys_get_mempolicy(tcp)
778 struct tcb *tcp;
779 {
780         if (exiting(tcp)) {
781                 int pol;
782                 if (tcp->u_arg[0] == 0)
783                         tprintf("NULL");
784                 else if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &pol) < 0)
785                         tprintf("%#lx", tcp->u_arg[0]);
786                 else
787                         printxval(policies, pol, "MPOL_???");
788                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
789                 tprintf(", %#lx, ", tcp->u_arg[3]);
790                 printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
791         }
792         return 0;
793 }
794 #endif