]> granicus.if.org Git - strace/blob - mem.c
Merge Trillian patches (Linux ia64)
[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         { 0,            NULL            },
138 };
139
140 static
141 int
142 print_mmap(tcp,u_arg)
143 struct tcb *tcp;
144 long *u_arg;
145 {
146         if (entering(tcp)) {
147                 /* addr */
148                 if (!u_arg[0])
149                         tprintf("NULL, ");
150                 else
151                         tprintf("%#lx, ", u_arg[0]);
152                 /* len */
153                 tprintf("%lu, ", u_arg[1]);
154                 /* prot */
155                 printflags(mmap_prot, u_arg[2]);
156                 tprintf(", ");
157                 /* flags */
158                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
159                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
160                 /* fd */
161                 tprintf(", %ld, ", u_arg[4]);
162                 /* offset */
163                 tprintf("%#lx", u_arg[5]);
164         }
165         return RVAL_HEX;
166 }
167
168 #ifdef LINUX
169 int sys_old_mmap(tcp)
170 struct tcb *tcp;
171 {
172     long u_arg[6];
173     int i, v;
174
175 #if     defined(IA64)
176     /*
177      *  IA64 processes never call this routine, they only use the
178      *  new `sys_mmap' interface.  This code converts the integer
179      *  arguments that the IA32 process pushed onto the stack into
180      *  longs.
181      *
182      *  Note that addresses with bit 31 set will be sign extended.
183      *  Fortunately, those addresses are not currently being generated
184      *  for IA32 processes so it's not a problem.
185      */
186     for (i = 0; i < 6; i++)
187         if (umove(tcp, tcp->u_arg[0] + (i * sizeof(int)), &v) == -1)
188                 return 0;
189         else
190                 u_arg[i] = v;
191 #else   // defined(IA64)
192     if (umoven(tcp, tcp->u_arg[0], sizeof u_arg, (char *) u_arg) == -1)
193             return 0;
194 #endif  // defined(IA64)
195     return print_mmap(tcp, u_arg);
196    
197 }
198 #endif
199
200 int
201 sys_mmap(tcp)
202 struct tcb *tcp;
203 {
204     return print_mmap(tcp, tcp->u_arg);
205 }
206
207 int
208 sys_munmap(tcp)
209 struct tcb *tcp;
210 {
211         if (entering(tcp)) {
212                 tprintf("%#lx, %lu",
213                         tcp->u_arg[0], tcp->u_arg[1]);
214         }
215         return 0;
216 }
217
218 int
219 sys_mprotect(tcp)
220 struct tcb *tcp;
221 {
222         if (entering(tcp)) {
223                 tprintf("%#lx, %lu, ",
224                         tcp->u_arg[0], tcp->u_arg[1]);
225                 if (!printflags(mmap_prot, tcp->u_arg[2]))
226                         tprintf("PROT_???");
227         }
228         return 0;
229 }
230
231 #ifdef LINUX
232
233 static struct xlat mremap_flags[] = {
234         { MREMAP_MAYMOVE, "MREMAP_MAYMOVE" },
235 };
236
237 int
238 sys_mremap(tcp)
239 struct tcb *tcp;
240 {
241         if (entering(tcp)) {
242                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
243                         tcp->u_arg[2]);
244                 printflags(mremap_flags, tcp->u_arg[3]);
245         }
246         return RVAL_HEX;
247 }
248
249 #endif /* LINUX */
250
251 #ifdef MS_ASYNC
252
253 static struct xlat mctl_sync[] = {
254         { MS_ASYNC,     "MS_ASYNC"      },
255         { MS_INVALIDATE,"MS_INVALIDATE" },
256 #ifdef MS_SYNC
257         { MS_SYNC,      "MS_SYNC"       },
258 #endif
259         { 0,            NULL            },
260 };
261
262 int
263 sys_msync(tcp)
264 struct tcb *tcp;
265 {
266         if (entering(tcp)) {
267                 /* addr */
268                 tprintf("%#lx", tcp->u_arg[0]);
269                 /* len */
270                 tprintf(", %lu, ", tcp->u_arg[1]);
271                 /* flags */
272                 if (!printflags(mctl_sync, tcp->u_arg[2]))
273                         tprintf("MS_???");
274         }
275         return 0;
276 }
277
278 #endif /* MS_ASYNC */
279
280 #ifdef MC_SYNC
281
282 static struct xlat mctl_funcs[] = {
283         { MC_LOCK,      "MC_LOCK"       },
284         { MC_LOCKAS,    "MC_LOCKAS"     },
285         { MC_SYNC,      "MC_SYNC"       },
286         { MC_UNLOCK,    "MC_UNLOCK"     },
287         { MC_UNLOCKAS,  "MC_UNLOCKAS"   },
288         { 0,            NULL            },
289 };
290
291 static struct xlat mctl_lockas[] = {
292         { MCL_CURRENT,  "MCL_CURRENT"   },
293         { MCL_FUTURE,   "MCL_FUTURE"    },
294         { 0,            NULL            },
295 };
296
297 int
298 sys_mctl(tcp)
299 struct tcb *tcp;
300 {
301         int arg, function;
302
303         if (entering(tcp)) {
304                 /* addr */
305                 tprintf("%#lx", tcp->u_arg[0]);
306                 /* len */
307                 tprintf(", %lu, ", tcp->u_arg[1]);
308                 /* function */
309                 function = tcp->u_arg[2];
310                 if (!printflags(mctl_funcs, function))
311                         tprintf("MC_???");
312                 /* arg */
313                 arg = tcp->u_arg[3];
314                 tprintf(", ");
315                 switch (function) {
316                 case MC_SYNC:
317                         if (!printflags(mctl_sync, arg))
318                                 tprintf("MS_???");
319                         break;
320                 case MC_LOCKAS:
321                         if (!printflags(mctl_lockas, arg))
322                                 tprintf("MCL_???");
323                         break;
324                 default:
325                         tprintf("%#x", arg);
326                         break;
327                 }
328         }
329         return 0;
330 }
331
332 #endif /* MC_SYNC */
333
334 int
335 sys_mincore(tcp)
336 struct tcb *tcp;
337 {
338         int i, len;
339         char *vec = NULL;
340
341         if (entering(tcp)) {
342                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
343         } else {
344                 len = tcp->u_arg[1];
345                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
346                         (vec = malloc((u_int)len)) == NULL ||
347                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
348                         tprintf("%#lx", tcp->u_arg[2]);
349                 else {
350                         tprintf("[");
351                         for (i = 0; i < len; i++) {
352                                 if (abbrev(tcp) && i >= max_strlen) {
353                                         tprintf("...");
354                                         break;
355                                 }
356                                 tprintf((vec[i] & 1) ? "1" : "0");
357                         }
358                         tprintf("]");
359                 }
360                 if (vec)
361                         free(vec);
362         }
363         return 0;
364 }
365
366 int
367 sys_getpagesize(tcp)
368 struct tcb *tcp;
369 {
370         if (exiting(tcp))
371                 return RVAL_HEX;
372         return 0;
373 }
374
375 #if defined(LINUX) && defined(__i386__)
376 int
377 sys_modify_ldt(tcp)
378 struct tcb *tcp;
379 {
380         if (entering(tcp)) {
381                 struct modify_ldt_ldt_s copy;
382                 tprintf("%ld", tcp->u_arg[0]);
383                 if (tcp->u_arg[1] == 0
384                                 || tcp->u_arg[2] != sizeof (struct modify_ldt_ldt_s)
385                                 || umove(tcp, tcp->u_arg[1], &copy) == -1)
386                         tprintf(", %lx", tcp->u_arg[1]);
387                 else {
388                         tprintf(", {entry_number:%d, ", copy.entry_number);
389                         if (!verbose(tcp))
390                                 tprintf("...}");
391                         else {
392                                 tprintf("base_addr:%#08lx, "
393                                                 "limit:%d, "
394                                                 "seg_32bit:%d, "
395                                                 "contents:%d, "
396                                                 "read_exec_only:%d, "
397                                                 "limit_in_pages:%d, "
398                                                 "seg_not_present:%d, "
399                                                 "useable:%d}",
400                                                 copy.base_addr,
401                                                 copy.limit,
402                                                 copy.seg_32bit,
403                                                 copy.contents,
404                                                 copy.read_exec_only,
405                                                 copy.limit_in_pages,
406                                                 copy.seg_not_present,
407                                                 copy.useable);
408                         }
409                 }
410                 tprintf(", %lu", tcp->u_arg[2]);
411         }
412         return 0;
413 }
414 #endif /* LINUX && __i386__ */
415