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