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