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