]> granicus.if.org Git - strace/blob - resource.c
Introduce memory allocation wrappers
[strace] / resource.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
31 #include "defs.h"
32 #include <sys/resource.h>
33 #include <sys/times.h>
34 #include <linux/kernel.h>
35
36 #include "xlat/resources.h"
37
38 static const char *
39 sprint_rlim64(uint64_t lim)
40 {
41         static char buf[sizeof(uint64_t)*3 + sizeof("*1024")];
42
43         if (lim == UINT64_MAX)
44                 return "RLIM64_INFINITY";
45
46         if (lim > 1024 && lim % 1024 == 0)
47                 sprintf(buf, "%" PRIu64 "*1024", lim / 1024);
48         else
49                 sprintf(buf, "%" PRIu64, lim);
50         return buf;
51 }
52
53 static void
54 print_rlimit64(struct tcb *tcp, unsigned long addr)
55 {
56         struct rlimit_64 {
57                 uint64_t rlim_cur;
58                 uint64_t rlim_max;
59         } rlim;
60
61         if (umove(tcp, addr, &rlim) < 0)
62                 tprintf("%#lx", addr);
63         else {
64                 tprintf("{rlim_cur=%s,", sprint_rlim64(rlim.rlim_cur));
65                 tprintf(" rlim_max=%s}", sprint_rlim64(rlim.rlim_max));
66         }
67 }
68
69 static void
70 decode_rlimit64(struct tcb *tcp, unsigned long addr)
71 {
72         if (!addr)
73                 tprints("NULL");
74         else if (!verbose(tcp) ||
75                  (exiting(tcp) && syserror(tcp)))
76                 tprintf("%#lx", addr);
77         else
78                 print_rlimit64(tcp, addr);
79 }
80
81 #if !defined(current_wordsize) || current_wordsize == 4
82
83 static const char *
84 sprint_rlim32(uint32_t lim)
85 {
86         static char buf[sizeof(uint32_t)*3 + sizeof("*1024")];
87
88         if (lim == UINT32_MAX)
89                 return "RLIM_INFINITY";
90
91         if (lim > 1024 && lim % 1024 == 0)
92                 sprintf(buf, "%" PRIu32 "*1024", lim / 1024);
93         else
94                 sprintf(buf, "%" PRIu32, lim);
95         return buf;
96 }
97
98 static void
99 print_rlimit32(struct tcb *tcp, unsigned long addr)
100 {
101         struct rlimit_32 {
102                 uint32_t rlim_cur;
103                 uint32_t rlim_max;
104         } rlim;
105
106         if (umove(tcp, addr, &rlim) < 0)
107                 tprintf("%#lx", addr);
108         else {
109                 tprintf("{rlim_cur=%s,", sprint_rlim32(rlim.rlim_cur));
110                 tprintf(" rlim_max=%s}", sprint_rlim32(rlim.rlim_max));
111         }
112 }
113
114 static void
115 decode_rlimit(struct tcb *tcp, unsigned long addr)
116 {
117         if (!addr)
118                 tprints("NULL");
119         else if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)))
120                 tprintf("%#lx", addr);
121         else {
122 # if defined(X86_64) || defined(X32)
123                 /*
124                  * i386 is the only personality on X86_64 and X32
125                  * with 32-bit rlim_t.
126                  * When current_personality is X32, current_wordsize
127                  * equals to 4 but rlim_t is 64-bit.
128                  */
129                 if (current_personality == 1)
130 # else
131                 if (current_wordsize == 4)
132 # endif
133                         print_rlimit32(tcp, addr);
134                 else
135                         print_rlimit64(tcp, addr);
136         }
137 }
138
139 #else /* defined(current_wordsize) && current_wordsize != 4 */
140
141 # define decode_rlimit decode_rlimit64
142
143 #endif
144
145 SYS_FUNC(getrlimit)
146 {
147         if (entering(tcp)) {
148                 printxval(resources, tcp->u_arg[0], "RLIMIT_???");
149                 tprints(", ");
150         }
151         else {
152                 decode_rlimit(tcp, tcp->u_arg[1]);
153         }
154         return 0;
155 }
156
157 SYS_FUNC(setrlimit)
158 {
159         if (entering(tcp)) {
160                 printxval(resources, tcp->u_arg[0], "RLIMIT_???");
161                 tprints(", ");
162                 decode_rlimit(tcp, tcp->u_arg[1]);
163         }
164         return 0;
165 }
166
167 SYS_FUNC(prlimit64)
168 {
169         if (entering(tcp)) {
170                 tprintf("%ld, ", tcp->u_arg[0]);
171                 printxval(resources, tcp->u_arg[1], "RLIMIT_???");
172                 tprints(", ");
173                 decode_rlimit64(tcp, tcp->u_arg[2]);
174                 tprints(", ");
175         } else {
176                 decode_rlimit64(tcp, tcp->u_arg[3]);
177         }
178         return 0;
179 }
180
181 #include "xlat/usagewho.h"
182
183 #ifdef ALPHA
184 void
185 printrusage32(struct tcb *tcp, long addr)
186 {
187         struct timeval32 {
188                 unsigned tv_sec;
189                 unsigned tv_usec;
190         };
191         struct rusage32 {
192                 struct timeval32 ru_utime;      /* user time used */
193                 struct timeval32 ru_stime;      /* system time used */
194                 long    ru_maxrss;              /* maximum resident set size */
195                 long    ru_ixrss;               /* integral shared memory size */
196                 long    ru_idrss;               /* integral unshared data size */
197                 long    ru_isrss;               /* integral unshared stack size */
198                 long    ru_minflt;              /* page reclaims */
199                 long    ru_majflt;              /* page faults */
200                 long    ru_nswap;               /* swaps */
201                 long    ru_inblock;             /* block input operations */
202                 long    ru_oublock;             /* block output operations */
203                 long    ru_msgsnd;              /* messages sent */
204                 long    ru_msgrcv;              /* messages received */
205                 long    ru_nsignals;            /* signals received */
206                 long    ru_nvcsw;               /* voluntary context switches */
207                 long    ru_nivcsw;              /* involuntary " */
208         } ru;
209
210         if (!addr)
211                 tprints("NULL");
212         else if (syserror(tcp) || !verbose(tcp))
213                 tprintf("%#lx", addr);
214         else if (umove(tcp, addr, &ru) < 0)
215                 tprints("{...}");
216         else if (!abbrev(tcp)) {
217                 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ",
218                         (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec,
219                         (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec);
220                 tprintf("ru_maxrss=%lu, ru_ixrss=%lu, ",
221                         ru.ru_maxrss, ru.ru_ixrss);
222                 tprintf("ru_idrss=%lu, ru_isrss=%lu, ",
223                         ru.ru_idrss, ru.ru_isrss);
224                 tprintf("ru_minflt=%lu, ru_majflt=%lu, ru_nswap=%lu, ",
225                         ru.ru_minflt, ru.ru_majflt, ru.ru_nswap);
226                 tprintf("ru_inblock=%lu, ru_oublock=%lu, ",
227                         ru.ru_inblock, ru.ru_oublock);
228                 tprintf("ru_msgsnd=%lu, ru_msgrcv=%lu, ",
229                         ru.ru_msgsnd, ru.ru_msgrcv);
230                 tprintf("ru_nsignals=%lu, ru_nvcsw=%lu, ru_nivcsw=%lu}",
231                         ru.ru_nsignals, ru.ru_nvcsw, ru.ru_nivcsw);
232         }
233         else {
234                 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ...}",
235                         (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec,
236                         (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec);
237         }
238 }
239 #endif
240
241 void
242 printrusage(struct tcb *tcp, long addr)
243 {
244         struct rusage ru;
245
246         if (!addr)
247                 tprints("NULL");
248         else if (syserror(tcp) || !verbose(tcp))
249                 tprintf("%#lx", addr);
250         else if (umove(tcp, addr, &ru) < 0)
251                 tprints("{...}");
252         else if (!abbrev(tcp)) {
253                 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ",
254                         (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec,
255                         (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec);
256                 tprintf("ru_maxrss=%lu, ru_ixrss=%lu, ",
257                         ru.ru_maxrss, ru.ru_ixrss);
258                 tprintf("ru_idrss=%lu, ru_isrss=%lu, ",
259                         ru.ru_idrss, ru.ru_isrss);
260                 tprintf("ru_minflt=%lu, ru_majflt=%lu, ru_nswap=%lu, ",
261                         ru.ru_minflt, ru.ru_majflt, ru.ru_nswap);
262                 tprintf("ru_inblock=%lu, ru_oublock=%lu, ",
263                         ru.ru_inblock, ru.ru_oublock);
264                 tprintf("ru_msgsnd=%lu, ru_msgrcv=%lu, ",
265                         ru.ru_msgsnd, ru.ru_msgrcv);
266                 tprintf("ru_nsignals=%lu, ru_nvcsw=%lu, ru_nivcsw=%lu}",
267                         ru.ru_nsignals, ru.ru_nvcsw, ru.ru_nivcsw);
268         }
269         else {
270                 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ...}",
271                         (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec,
272                         (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec);
273         }
274 }
275
276 SYS_FUNC(getrusage)
277 {
278         if (entering(tcp)) {
279                 printxval(usagewho, tcp->u_arg[0], "RUSAGE_???");
280                 tprints(", ");
281         }
282         else
283                 printrusage(tcp, tcp->u_arg[1]);
284         return 0;
285 }
286
287 #ifdef ALPHA
288 SYS_FUNC(osf_getrusage)
289 {
290         if (entering(tcp)) {
291                 printxval(usagewho, tcp->u_arg[0], "RUSAGE_???");
292                 tprints(", ");
293         }
294         else
295                 printrusage32(tcp, tcp->u_arg[1]);
296         return 0;
297 }
298 #endif /* ALPHA */
299
300 #include "xlat/priorities.h"
301
302 SYS_FUNC(getpriority)
303 {
304         if (entering(tcp)) {
305                 printxval(priorities, tcp->u_arg[0], "PRIO_???");
306                 tprintf(", %lu", tcp->u_arg[1]);
307         }
308         return 0;
309 }
310
311 SYS_FUNC(setpriority)
312 {
313         if (entering(tcp)) {
314                 printxval(priorities, tcp->u_arg[0], "PRIO_???");
315                 tprintf(", %lu, %ld", tcp->u_arg[1], tcp->u_arg[2]);
316         }
317         return 0;
318 }
319
320 SYS_FUNC(times)
321 {
322         struct tms tbuf;
323
324         if (exiting(tcp)) {
325                 if (tcp->u_arg[0] == 0)
326                         tprints("NULL");
327                 else if (syserror(tcp))
328                         tprintf("%#lx", tcp->u_arg[0]);
329                 else if (umove(tcp, tcp->u_arg[0], &tbuf) < 0)
330                         tprints("{...}");
331                 else {
332                         tprintf("{tms_utime=%llu, tms_stime=%llu, ",
333                                 (unsigned long long) tbuf.tms_utime,
334                                 (unsigned long long) tbuf.tms_stime);
335                         tprintf("tms_cutime=%llu, tms_cstime=%llu}",
336                                 (unsigned long long) tbuf.tms_cutime,
337                                 (unsigned long long) tbuf.tms_cstime);
338                 }
339         }
340         return 0;
341 }