]> granicus.if.org Git - strace/blob - mem.c
Fix off_t args on FreeBSD
[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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #ifdef LINUX
36 #include <linux/mman.h>
37 #endif
38 #include <sys/mman.h>
39
40 #if defined(LINUX) && defined(__i386__)
41 #include <asm/ldt.h>
42 #endif
43
44 int
45 sys_brk(tcp)
46 struct tcb *tcp;
47 {
48         if (entering(tcp)) {
49                 tprintf("%#lx", tcp->u_arg[0]);
50         }
51 #ifdef LINUX
52         return RVAL_HEX;
53 #else
54         return 0;
55 #endif
56 }
57
58 int
59 sys_sbrk(tcp)
60 struct tcb *tcp;
61 {
62         if (entering(tcp)) {
63                 tprintf("%lu", tcp->u_arg[0]);
64         }
65         return RVAL_HEX;
66 }
67
68 static struct xlat mmap_prot[] = {
69         { PROT_NONE,    "PROT_NONE",    },
70         { PROT_READ,    "PROT_READ"     },
71         { PROT_WRITE,   "PROT_WRITE"    },
72         { PROT_EXEC,    "PROT_EXEC"     },
73         { 0,            NULL            },
74 };
75
76 static struct xlat mmap_flags[] = {
77         { MAP_SHARED,   "MAP_SHARED"    },
78         { MAP_PRIVATE,  "MAP_PRIVATE"   },
79         { MAP_FIXED,    "MAP_FIXED"     },
80 #ifdef MAP_ANONYMOUS
81         { MAP_ANONYMOUS,"MAP_ANONYMOUS" },
82 #endif
83 #ifdef MAP_RENAME
84         { MAP_RENAME,   "MAP_RENAME"    },
85 #endif
86 #ifdef MAP_NORESERVE
87         { MAP_NORESERVE,"MAP_NORESERVE" },
88 #endif
89         /*
90          * XXX - this was introduced in SunOS 4.x to distinguish between
91          * the old pre-4.x "mmap()", which:
92          *
93          *      only let you map devices with an "mmap" routine (e.g.,
94          *      frame buffers) in;
95          *
96          *      required you to specify the mapping address;
97          *
98          *      returned 0 on success and -1 on failure;
99          *
100          * memory and which, and the 4.x "mmap()" which:
101          *
102          *      can map plain files;
103          *
104          *      can be asked to pick where to map the file;
105          *
106          *      returns the address where it mapped the file on success
107          *      and -1 on failure.
108          *
109          * It's not actually used in source code that calls "mmap()"; the
110          * "mmap()" routine adds it for you.
111          *
112          * It'd be nice to come up with some way of eliminating it from
113          * the flags, e.g. reporting calls *without* it as "old_mmap()"
114          * and calls with it as "mmap()".
115          */
116 #ifdef _MAP_NEW
117         { _MAP_NEW,     "_MAP_NEW"      },
118 #endif
119 #ifdef MAP_GROWSDOWN
120         { MAP_GROWSDOWN,"MAP_GROWSDOWN" },
121 #endif
122 #ifdef MAP_DENYWRITE
123         { MAP_DENYWRITE,"MAP_DENYWRITE" },
124 #endif
125 #ifdef MAP_EXECUTABLE
126         { MAP_EXECUTABLE,"MAP_EXECUTABLE"},
127 #endif
128 #ifdef MAP_INHERIT
129         { MAP_INHERIT,"MAP_INHERIT"     },
130 #endif
131 #ifdef MAP_FILE
132         { MAP_FILE,"MAP_FILE"},
133 #endif
134 #ifdef MAP_LOCKED
135         { MAP_LOCKED,"MAP_LOCKED"},
136 #endif
137         /* FreeBSD ones */
138 #ifdef MAP_ANON
139         { MAP_ANON,             "MAP_ANON"      },
140 #endif
141 #ifdef MAP_HASSEMAPHORE
142         { MAP_HASSEMAPHORE,     "MAP_HASSEMAPHORE"      },
143 #endif
144 #ifdef MAP_STACK
145         { MAP_STACK,            "MAP_STACK"     },
146 #endif
147 #ifdef MAP_NOSYNC
148         { MAP_NOSYNC,           "MAP_NOSYNC"    },
149 #endif
150 #ifdef MAP_NOCORE
151         { MAP_NOCORE,           "MAP_NOCORE"    },
152 #endif
153         { 0,            NULL            },
154 };
155
156 static
157 int
158 print_mmap(tcp,u_arg)
159 struct tcb *tcp;
160 long *u_arg;
161 {
162         if (entering(tcp)) {
163                 /* addr */
164                 if (!u_arg[0])
165                         tprintf("NULL, ");
166                 else
167                         tprintf("%#lx, ", u_arg[0]);
168                 /* len */
169                 tprintf("%lu, ", u_arg[1]);
170                 /* prot */
171                 printflags(mmap_prot, u_arg[2]);
172                 tprintf(", ");
173                 /* flags */
174 #ifdef MAP_TYPE
175                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
176                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
177 #else
178                 printflags(mmap_flags, u_arg[3]);
179 #endif
180                 /* fd */
181                 tprintf(", %ld, ", u_arg[4]);
182                 /* offset */
183                 tprintf("%#lx", u_arg[5]);
184         }
185         return RVAL_HEX;
186 }
187
188 #ifdef LINUX
189 int sys_old_mmap(tcp)
190 struct tcb *tcp;
191 {
192     long u_arg[6];
193
194 #if     defined(IA64)
195     int i, v;
196     /*
197      *  IA64 processes never call this routine, they only use the
198      *  new `sys_mmap' interface.  This code converts the integer
199      *  arguments that the IA32 process pushed onto the stack into
200      *  longs.
201      *
202      *  Note that addresses with bit 31 set will be sign extended.
203      *  Fortunately, those addresses are not currently being generated
204      *  for IA32 processes so it's not a problem.
205      */
206     for (i = 0; i < 6; i++)
207         if (umove(tcp, tcp->u_arg[0] + (i * sizeof(int)), &v) == -1)
208                 return 0;
209         else
210                 u_arg[i] = v;
211 #else   // defined(IA64)
212     if (umoven(tcp, tcp->u_arg[0], sizeof u_arg, (char *) u_arg) == -1)
213             return 0;
214 #endif  // defined(IA64)
215     return print_mmap(tcp, u_arg);
216    
217 }
218 #endif
219
220 int
221 sys_mmap(tcp)
222 struct tcb *tcp;
223 {
224     return print_mmap(tcp, tcp->u_arg);
225 }
226
227 #if _LFS64_LARGEFILE || FREEBSD
228 int
229 sys_mmap64(tcp)
230 struct tcb *tcp;
231 {
232 #ifdef linux
233 #ifdef ALPHA
234         long *u_arg = tcp->u_arg;
235 #else /* !ALPHA */
236         long u_arg[7];
237 #endif /* !ALPHA */
238 #else /* !linux */
239         long *u_arg = tcp->u_arg;
240 #endif /* !linux */
241
242         if (entering(tcp)) {
243 #ifdef linux
244 #ifndef ALPHA
245                 if (umoven(tcp, tcp->u_arg[0], sizeof u_arg,
246                                 (char *) u_arg) == -1)
247                         return 0;
248 #endif /* ALPHA */
249 #endif /* linux */
250                 ALIGN64 (tcp, 5);       /* FreeBSD wierdies */
251
252                 /* addr */
253                 tprintf("%#lx, ", u_arg[0]);
254                 /* len */
255                 tprintf("%lu, ", u_arg[1]);
256                 /* prot */
257                 printflags(mmap_prot, u_arg[2]);
258                 tprintf(", ");
259                 /* flags */
260 #ifdef MAP_TYPE
261                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
262                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
263 #else
264                 printflags(mmap_flags, u_arg[3]);
265 #endif
266                 /* fd */
267                 tprintf(", %ld, ", u_arg[4]);
268                 /* offset */
269                 tprintf("%#llx", get64(u_arg[5], u_arg[6]));
270         }
271         return RVAL_HEX;
272 }
273 #endif
274
275  
276 int
277 sys_munmap(tcp)
278 struct tcb *tcp;
279 {
280         if (entering(tcp)) {
281                 tprintf("%#lx, %lu",
282                         tcp->u_arg[0], tcp->u_arg[1]);
283         }
284         return 0;
285 }
286
287 int
288 sys_mprotect(tcp)
289 struct tcb *tcp;
290 {
291         if (entering(tcp)) {
292                 tprintf("%#lx, %lu, ",
293                         tcp->u_arg[0], tcp->u_arg[1]);
294                 if (!printflags(mmap_prot, tcp->u_arg[2]))
295                         tprintf("PROT_???");
296         }
297         return 0;
298 }
299
300 #ifdef LINUX
301
302 static struct xlat mremap_flags[] = {
303         { MREMAP_MAYMOVE,       "MREMAP_MAYMOVE"        },
304         { 0,                    NULL                    }
305 };
306
307 int
308 sys_mremap(tcp)
309 struct tcb *tcp;
310 {
311         if (entering(tcp)) {
312                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
313                         tcp->u_arg[2]);
314                 printflags(mremap_flags, tcp->u_arg[3]);
315         }
316         return RVAL_HEX;
317 }
318
319 static struct xlat madvise_flags[] = {
320 #ifdef MADV_NORMAL
321         { MADV_NORMAL,          "MADV_NORMAL" },
322 #endif
323 #ifdef MADZV_RANDOM
324         { MADV_RANDOM,          "MADV_RANDOM" },
325 #endif
326 #ifdef MADV_SEQUENTIAL
327         { MADV_SEQUENTIAL,      "MADV_SEQUENTIAL" },
328 #endif
329 #ifdef MADV_WILLNEED
330         { MADV_WILLNEED,        "MADV_WILLNEED" },
331 #endif
332 #ifdef MADV_DONTNED
333         { MADV_DONTNEED,        "MADV_DONTNEED" },
334 #endif
335         { 0,                    NULL },
336 };
337
338
339 int
340 sys_madvise(tcp)
341 struct tcb *tcp;
342 {
343         if (entering(tcp)) {
344                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
345                 printflags(madvise_flags, tcp->u_arg[2]);
346         }
347         return 0;
348 }
349
350
351 static struct xlat mlockall_flags[] = {
352 #ifdef MCL_CURRENT
353         { MCL_CURRENT,  "MCL_CURRENT" },
354 #endif
355 #ifdef MCL_FUTURE
356         { MCL_FUTURE,   "MCL_FUTURE" },
357 #endif
358         { 0,            NULL}
359 };
360
361 int
362 sys_mlockall(tcp)
363 struct tcb *tcp;
364 {
365         if (entering(tcp)) {
366                 printflags(mlockall_flags, tcp->u_arg[0]);
367         }
368         return 0;
369 }
370
371
372 #endif /* LINUX */
373
374 #ifdef MS_ASYNC
375
376 static struct xlat mctl_sync[] = {
377         { MS_ASYNC,     "MS_ASYNC"      },
378         { MS_INVALIDATE,"MS_INVALIDATE" },
379 #ifdef MS_SYNC
380         { MS_SYNC,      "MS_SYNC"       },
381 #endif
382         { 0,            NULL            },
383 };
384
385 int
386 sys_msync(tcp)
387 struct tcb *tcp;
388 {
389         if (entering(tcp)) {
390                 /* addr */
391                 tprintf("%#lx", tcp->u_arg[0]);
392                 /* len */
393                 tprintf(", %lu, ", tcp->u_arg[1]);
394                 /* flags */
395                 if (!printflags(mctl_sync, tcp->u_arg[2]))
396                         tprintf("MS_???");
397         }
398         return 0;
399 }
400
401 #endif /* MS_ASYNC */
402
403 #ifdef MC_SYNC
404
405 static struct xlat mctl_funcs[] = {
406         { MC_LOCK,      "MC_LOCK"       },
407         { MC_LOCKAS,    "MC_LOCKAS"     },
408         { MC_SYNC,      "MC_SYNC"       },
409         { MC_UNLOCK,    "MC_UNLOCK"     },
410         { MC_UNLOCKAS,  "MC_UNLOCKAS"   },
411         { 0,            NULL            },
412 };
413
414 static struct xlat mctl_lockas[] = {
415         { MCL_CURRENT,  "MCL_CURRENT"   },
416         { MCL_FUTURE,   "MCL_FUTURE"    },
417         { 0,            NULL            },
418 };
419
420 int
421 sys_mctl(tcp)
422 struct tcb *tcp;
423 {
424         int arg, function;
425
426         if (entering(tcp)) {
427                 /* addr */
428                 tprintf("%#lx", tcp->u_arg[0]);
429                 /* len */
430                 tprintf(", %lu, ", tcp->u_arg[1]);
431                 /* function */
432                 function = tcp->u_arg[2];
433                 if (!printflags(mctl_funcs, function))
434                         tprintf("MC_???");
435                 /* arg */
436                 arg = tcp->u_arg[3];
437                 tprintf(", ");
438                 switch (function) {
439                 case MC_SYNC:
440                         if (!printflags(mctl_sync, arg))
441                                 tprintf("MS_???");
442                         break;
443                 case MC_LOCKAS:
444                         if (!printflags(mctl_lockas, arg))
445                                 tprintf("MCL_???");
446                         break;
447                 default:
448                         tprintf("%#x", arg);
449                         break;
450                 }
451         }
452         return 0;
453 }
454
455 #endif /* MC_SYNC */
456
457 int
458 sys_mincore(tcp)
459 struct tcb *tcp;
460 {
461         int i, len;
462         char *vec = NULL;
463
464         if (entering(tcp)) {
465                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
466         } else {
467                 len = tcp->u_arg[1];
468                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
469                         (vec = malloc((u_int)len)) == NULL ||
470                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
471                         tprintf("%#lx", tcp->u_arg[2]);
472                 else {
473                         tprintf("[");
474                         for (i = 0; i < len; i++) {
475                                 if (abbrev(tcp) && i >= max_strlen) {
476                                         tprintf("...");
477                                         break;
478                                 }
479                                 tprintf((vec[i] & 1) ? "1" : "0");
480                         }
481                         tprintf("]");
482                 }
483                 if (vec)
484                         free(vec);
485         }
486         return 0;
487 }
488
489 int
490 sys_getpagesize(tcp)
491 struct tcb *tcp;
492 {
493         if (exiting(tcp))
494                 return RVAL_HEX;
495         return 0;
496 }
497
498 #if defined(LINUX) && defined(__i386__)
499 int
500 sys_modify_ldt(tcp)
501 struct tcb *tcp;
502 {
503         if (entering(tcp)) {
504                 struct modify_ldt_ldt_s copy;
505                 tprintf("%ld", tcp->u_arg[0]);
506                 if (tcp->u_arg[1] == 0
507                                 || tcp->u_arg[2] != sizeof (struct modify_ldt_ldt_s)
508                                 || umove(tcp, tcp->u_arg[1], &copy) == -1)
509                         tprintf(", %lx", tcp->u_arg[1]);
510                 else {
511                         tprintf(", {entry_number:%d, ", copy.entry_number);
512                         if (!verbose(tcp))
513                                 tprintf("...}");
514                         else {
515                                 tprintf("base_addr:%#08lx, "
516                                                 "limit:%d, "
517                                                 "seg_32bit:%d, "
518                                                 "contents:%d, "
519                                                 "read_exec_only:%d, "
520                                                 "limit_in_pages:%d, "
521                                                 "seg_not_present:%d, "
522                                                 "useable:%d}",
523                                                 copy.base_addr,
524                                                 copy.limit,
525                                                 copy.seg_32bit,
526                                                 copy.contents,
527                                                 copy.read_exec_only,
528                                                 copy.limit_in_pages,
529                                                 copy.seg_not_present,
530                                                 copy.useable);
531                         }
532                 }
533                 tprintf(", %lu", tcp->u_arg[2]);
534         }
535         return 0;
536 }
537 #endif /* LINUX && __i386__ */
538