]> granicus.if.org Git - strace/blob - time.c
sched.c: use printnum_int and printaddr
[strace] / time.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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31 #include <fcntl.h>
32 #include <linux/version.h>
33 #include <sys/timex.h>
34
35 #ifndef UTIME_NOW
36 #define UTIME_NOW ((1l << 30) - 1l)
37 #endif
38 #ifndef UTIME_OMIT
39 #define UTIME_OMIT ((1l << 30) - 2l)
40 #endif
41
42 #if SUPPORTED_PERSONALITIES > 1
43 # if defined X86_64 || defined X32
44 #  define current_time_t_is_compat (current_personality == 1)
45 # else
46 #  define current_time_t_is_compat (current_wordsize == 4)
47 # endif
48 #else
49 # define current_time_t_is_compat 0
50 #endif
51
52 struct timeval32
53 {
54         u_int32_t tv_sec, tv_usec;
55 };
56
57 static void
58 tprint_timeval32(struct tcb *tcp, const struct timeval32 *tv)
59 {
60         tprintf("{%u, %u}", tv->tv_sec, tv->tv_usec);
61 }
62
63 static void
64 tprint_timeval(struct tcb *tcp, const struct timeval *tv)
65 {
66         tprintf("{%ju, %ju}", (uintmax_t) tv->tv_sec, (uintmax_t) tv->tv_usec);
67 }
68
69 void
70 printtv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness, int special)
71 {
72         char buf[TIMEVAL_TEXT_BUFSIZE];
73         sprinttv(buf, tcp, addr, bitness, special);
74         tprints(buf);
75 }
76
77 static char *
78 do_sprinttv(char *buf, const uintmax_t sec, const uintmax_t usec,
79             const int special)
80 {
81         if (special) {
82                 switch (usec) {
83                         case UTIME_NOW:
84                                 return stpcpy(buf, "UTIME_NOW");
85                         case UTIME_OMIT:
86                                 return stpcpy(buf, "UTIME_OMIT");
87                 }
88         }
89         return buf + sprintf(buf, "{%ju, %ju}", sec, usec);
90 }
91
92 char *
93 sprinttv(char *buf, struct tcb *tcp, long addr, enum bitness_t bitness, int special)
94 {
95         if (addr == 0)
96                 return stpcpy(buf, "NULL");
97
98         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)))
99                 return buf + sprintf(buf, "%#lx", addr);
100
101         if (bitness == BITNESS_32 || current_time_t_is_compat)
102         {
103                 struct timeval32 tv;
104
105                 if (umove(tcp, addr, &tv) >= 0)
106                         return do_sprinttv(buf, tv.tv_sec, tv.tv_usec, special);
107         } else {
108                 struct timeval tv;
109
110                 if (umove(tcp, addr, &tv) >= 0)
111                         return do_sprinttv(buf, tv.tv_sec, tv.tv_usec, special);
112         }
113
114         return buf + sprintf(buf, "%#lx", addr);
115 }
116
117 void
118 print_timespec(struct tcb *tcp, long addr)
119 {
120         char buf[TIMESPEC_TEXT_BUFSIZE];
121         sprint_timespec(buf, tcp, addr);
122         tprints(buf);
123 }
124
125 void
126 sprint_timespec(char *buf, struct tcb *tcp, long addr)
127 {
128         if (addr == 0)
129                 strcpy(buf, "NULL");
130         else if (!verbose(tcp))
131                 sprintf(buf, "%#lx", addr);
132         else {
133                 int rc;
134
135 #if SUPPORTED_PERSONALITIES > 1
136                 if (current_time_t_is_compat) {
137                         struct timeval32 tv;
138
139                         rc = umove(tcp, addr, &tv);
140                         if (rc >= 0)
141                                 sprintf(buf, "{%u, %u}",
142                                         tv.tv_sec, tv.tv_usec);
143                 } else
144 #endif
145                 {
146                         struct timespec ts;
147
148                         rc = umove(tcp, addr, &ts);
149                         if (rc >= 0)
150                                 sprintf(buf, "{%ju, %ju}",
151                                         (uintmax_t) ts.tv_sec,
152                                         (uintmax_t) ts.tv_nsec);
153                 }
154                 if (rc < 0)
155                         strcpy(buf, "{...}");
156         }
157 }
158
159 SYS_FUNC(time)
160 {
161         if (exiting(tcp)) {
162                 printnum_long(tcp, tcp->u_arg[0], "%ld");
163         }
164         return 0;
165 }
166
167 SYS_FUNC(gettimeofday)
168 {
169         if (exiting(tcp)) {
170                 printtv(tcp, tcp->u_arg[0]);
171                 tprints(", ");
172                 printtv(tcp, tcp->u_arg[1]);
173         }
174         return 0;
175 }
176
177 #ifdef ALPHA
178 SYS_FUNC(osf_gettimeofday)
179 {
180         if (exiting(tcp)) {
181                 printtv_bitness(tcp, tcp->u_arg[0], BITNESS_32, 0);
182                 tprints(", ");
183                 printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32, 0);
184         }
185         return 0;
186 }
187 #endif
188
189 SYS_FUNC(settimeofday)
190 {
191         printtv(tcp, tcp->u_arg[0]);
192         tprints(", ");
193         printtv(tcp, tcp->u_arg[1]);
194
195         return RVAL_DECODED;
196 }
197
198 #ifdef ALPHA
199 SYS_FUNC(osf_settimeofday)
200 {
201         printtv_bitness(tcp, tcp->u_arg[0], BITNESS_32, 0);
202         tprints(", ");
203         printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32, 0);
204
205         return RVAL_DECODED;
206 }
207 #endif
208
209 SYS_FUNC(adjtime)
210 {
211         if (entering(tcp)) {
212                 printtv(tcp, tcp->u_arg[0]);
213                 tprints(", ");
214         } else {
215                 printtv(tcp, tcp->u_arg[1]);
216         }
217         return 0;
218 }
219
220 SYS_FUNC(nanosleep)
221 {
222         if (entering(tcp)) {
223                 print_timespec(tcp, tcp->u_arg[0]);
224                 tprints(", ");
225         } else {
226                 /* Second (returned) timespec is only significant
227                  * if syscall was interrupted. On success, we print
228                  * only its address, since kernel doesn't modify it,
229                  * and printing the value may show uninitialized data.
230                  */
231                 switch (tcp->u_error) {
232                 default:
233                         /* Not interrupted (slept entire interval) */
234                         printaddr(tcp->u_arg[1]);
235                         break;
236                 case ERESTARTSYS:
237                 case ERESTARTNOINTR:
238                 case ERESTARTNOHAND:
239                 case ERESTART_RESTARTBLOCK:
240                         /* Interrupted */
241                         print_timespec(tcp, tcp->u_arg[1]);
242                 }
243         }
244         return 0;
245 }
246
247 #include "xlat/itimer_which.h"
248
249 static void
250 printitv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness)
251 {
252         if (bitness == BITNESS_32 || current_time_t_is_compat) {
253                 struct {
254                         struct timeval32 it_interval, it_value;
255                 } itv;
256
257                 if (!umove_or_printaddr(tcp, addr, &itv)) {
258                         tprints("{it_interval=");
259                         tprint_timeval32(tcp, &itv.it_interval);
260                         tprints(", it_value=");
261                         tprint_timeval32(tcp, &itv.it_value);
262                         tprints("}");
263                 }
264         } else {
265                 struct itimerval itv;
266
267                 if (!umove_or_printaddr(tcp, addr, &itv)) {
268                         tprints("{it_interval=");
269                         tprint_timeval(tcp, &itv.it_interval);
270                         tprints(", it_value=");
271                         tprint_timeval(tcp, &itv.it_value);
272                         tprints("}");
273                 }
274         }
275 }
276
277 #define printitv(tcp, addr)     \
278         printitv_bitness((tcp), (addr), BITNESS_CURRENT)
279
280 SYS_FUNC(getitimer)
281 {
282         if (entering(tcp)) {
283                 printxval(itimer_which, tcp->u_arg[0], "ITIMER_???");
284                 tprints(", ");
285         } else {
286                 printitv(tcp, tcp->u_arg[1]);
287         }
288         return 0;
289 }
290
291 #ifdef ALPHA
292 SYS_FUNC(osf_getitimer)
293 {
294         if (entering(tcp)) {
295                 printxval(itimer_which, tcp->u_arg[0], "ITIMER_???");
296                 tprints(", ");
297         } else {
298                 printitv_bitness(tcp, tcp->u_arg[1], BITNESS_32);
299         }
300         return 0;
301 }
302 #endif
303
304 SYS_FUNC(setitimer)
305 {
306         if (entering(tcp)) {
307                 printxval(itimer_which, tcp->u_arg[0], "ITIMER_???");
308                 tprints(", ");
309                 printitv(tcp, tcp->u_arg[1]);
310                 tprints(", ");
311         } else {
312                 printitv(tcp, tcp->u_arg[2]);
313         }
314         return 0;
315 }
316
317 #ifdef ALPHA
318 SYS_FUNC(osf_setitimer)
319 {
320         if (entering(tcp)) {
321                 printxval(itimer_which, tcp->u_arg[0], "ITIMER_???");
322                 tprints(", ");
323                 printitv_bitness(tcp, tcp->u_arg[1], BITNESS_32);
324                 tprints(", ");
325         } else {
326                 printitv_bitness(tcp, tcp->u_arg[2], BITNESS_32);
327         }
328         return 0;
329 }
330 #endif
331
332 #include "xlat/adjtimex_modes.h"
333 #include "xlat/adjtimex_status.h"
334 #include "xlat/adjtimex_state.h"
335
336 #if SUPPORTED_PERSONALITIES > 1
337 static int
338 tprint_timex32(struct tcb *tcp, long addr)
339 {
340         struct {
341                 unsigned int modes;
342                 int     offset;
343                 int     freq;
344                 int     maxerror;
345                 int     esterror;
346                 int     status;
347                 int     constant;
348                 int     precision;
349                 int     tolerance;
350                 struct timeval32 time;
351                 int     tick;
352                 int     ppsfreq;
353                 int     jitter;
354                 int     shift;
355                 int     stabil;
356                 int     jitcnt;
357                 int     calcnt;
358                 int     errcnt;
359                 int     stbcnt;
360         } tx;
361
362         if (umove_or_printaddr(tcp, addr, &tx))
363                 return -1;
364
365         tprints("{modes=");
366         printflags(adjtimex_modes, tx.modes, "ADJ_???");
367         tprintf(", offset=%d, freq=%d, maxerror=%d, ",
368                 tx.offset, tx.freq, tx.maxerror);
369         tprintf("esterror=%u, status=", tx.esterror);
370         printflags(adjtimex_status, tx.status, "STA_???");
371         tprintf(", constant=%d, precision=%u, ",
372                 tx.constant, tx.precision);
373         tprintf("tolerance=%d, time=", tx.tolerance);
374         tprint_timeval32(tcp, &tx.time);
375         tprintf(", tick=%d, ppsfreq=%d, jitter=%d",
376                 tx.tick, tx.ppsfreq, tx.jitter);
377         tprintf(", shift=%d, stabil=%d, jitcnt=%d",
378                 tx.shift, tx.stabil, tx.jitcnt);
379         tprintf(", calcnt=%d, errcnt=%d, stbcnt=%d",
380                 tx.calcnt, tx.errcnt, tx.stbcnt);
381         tprints("}");
382         return 0;
383 }
384 #endif /* SUPPORTED_PERSONALITIES > 1 */
385
386 static int
387 tprint_timex(struct tcb *tcp, long addr)
388 {
389         struct timex tx;
390
391 #if SUPPORTED_PERSONALITIES > 1
392         if (current_time_t_is_compat)
393                 return tprint_timex32(tcp, addr);
394 #endif
395         if (umove_or_printaddr(tcp, addr, &tx))
396                 return -1;
397
398 #if LINUX_VERSION_CODE < 66332
399         tprintf("{mode=%d, offset=%ld, frequency=%ld, ",
400                 tx.mode, tx.offset, tx.frequency);
401         tprintf("maxerror=%ld, esterror=%lu, status=%u, ",
402                 tx.maxerror, tx.esterror, tx.status);
403         tprintf("time_constant=%ld, precision=%lu, ",
404                 tx.time_constant, tx.precision);
405         tprintf("tolerance=%ld, time=", tx.tolerance);
406         tprint_timeval(tcp, &tx.time);
407 #else
408         tprints("{modes=");
409         printflags(adjtimex_modes, tx.modes, "ADJ_???");
410         tprintf(", offset=%jd, freq=%jd, maxerror=%ju, esterror=%ju, status=",
411                 (intmax_t) tx.offset, (intmax_t) tx.freq,
412                 (uintmax_t) tx.maxerror, (uintmax_t) tx.esterror);
413         printflags(adjtimex_status, tx.status, "STA_???");
414         tprintf(", constant=%jd, precision=%ju, tolerance=%jd, time=",
415                 (intmax_t) tx.constant, (uintmax_t) tx.precision,
416                 (intmax_t) tx.tolerance);
417         tprint_timeval(tcp, &tx.time);
418         tprintf(", tick=%jd, ppsfreq=%jd, jitter=%jd",
419                 (intmax_t) tx.tick, (intmax_t) tx.ppsfreq, (intmax_t) tx.jitter);
420         tprintf(", shift=%d, stabil=%jd, jitcnt=%jd",
421                 tx.shift, (intmax_t) tx.stabil, (intmax_t) tx.jitcnt);
422         tprintf(", calcnt=%jd, errcnt=%jd, stbcnt=%jd",
423                 (intmax_t) tx.calcnt, (intmax_t) tx.errcnt, (intmax_t) tx.stbcnt);
424 #endif
425         tprints("}");
426         return 0;
427 }
428
429 static int
430 do_adjtimex(struct tcb *tcp, long addr)
431 {
432         if (tprint_timex(tcp, addr))
433                 return 0;
434         tcp->auxstr = xlookup(adjtimex_state, tcp->u_rval);
435         if (tcp->auxstr)
436                 return RVAL_STR;
437         return 0;
438 }
439
440 SYS_FUNC(adjtimex)
441 {
442         if (exiting(tcp))
443                 return do_adjtimex(tcp, tcp->u_arg[0]);
444         return 0;
445 }
446
447 #include "xlat/clockflags.h"
448 #include "xlat/clocknames.h"
449
450 static void
451 printclockname(int clockid)
452 {
453 #ifdef CLOCKID_TO_FD
454 # include "xlat/cpuclocknames.h"
455
456         if (clockid < 0) {
457                 if ((clockid & CLOCKFD_MASK) == CLOCKFD)
458                         tprintf("FD_TO_CLOCKID(%d)", CLOCKID_TO_FD(clockid));
459                 else {
460                         if(CPUCLOCK_PERTHREAD(clockid))
461                                 tprintf("MAKE_THREAD_CPUCLOCK(%d,", CPUCLOCK_PID(clockid));
462                         else
463                                 tprintf("MAKE_PROCESS_CPUCLOCK(%d,", CPUCLOCK_PID(clockid));
464                         printxval(cpuclocknames, clockid & CLOCKFD_MASK, "CPUCLOCK_???");
465                         tprints(")");
466                 }
467         }
468         else
469 #endif
470                 printxval(clocknames, clockid, "CLOCK_???");
471 }
472
473 SYS_FUNC(clock_settime)
474 {
475         printclockname(tcp->u_arg[0]);
476         tprints(", ");
477         printtv(tcp, tcp->u_arg[1]);
478
479         return RVAL_DECODED;
480 }
481
482 SYS_FUNC(clock_gettime)
483 {
484         if (entering(tcp)) {
485                 printclockname(tcp->u_arg[0]);
486                 tprints(", ");
487         } else {
488                 printtv(tcp, tcp->u_arg[1]);
489         }
490         return 0;
491 }
492
493 SYS_FUNC(clock_nanosleep)
494 {
495         if (entering(tcp)) {
496                 printclockname(tcp->u_arg[0]);
497                 tprints(", ");
498                 printflags(clockflags, tcp->u_arg[1], "TIMER_???");
499                 tprints(", ");
500                 printtv(tcp, tcp->u_arg[2]);
501                 tprints(", ");
502         } else {
503                 printtv(tcp, tcp->u_arg[3]);
504         }
505         return 0;
506 }
507
508 SYS_FUNC(clock_adjtime)
509 {
510         if (exiting(tcp))
511                 return do_adjtimex(tcp, tcp->u_arg[1]);
512         printclockname(tcp->u_arg[0]);
513         tprints(", ");
514         return 0;
515 }
516
517 #ifndef SIGEV_THREAD_ID
518 # define SIGEV_THREAD_ID 4
519 #endif
520 #include "xlat/sigev_value.h"
521
522 #if SUPPORTED_PERSONALITIES > 1
523 static void
524 printsigevent32(struct tcb *tcp, long arg)
525 {
526         struct {
527                 int     sigev_value;
528                 int     sigev_signo;
529                 int     sigev_notify;
530
531                 union {
532                         int     tid;
533                         struct {
534                                 int     function, attribute;
535                         } thread;
536                 } un;
537         } sev;
538
539         if (!umove_or_printaddr(tcp, arg, &sev)) {
540                 tprintf("{%#x, ", sev.sigev_value);
541                 if (sev.sigev_notify == SIGEV_SIGNAL)
542                         tprintf("%s, ", signame(sev.sigev_signo));
543                 else
544                         tprintf("%u, ", sev.sigev_signo);
545                 printxval(sigev_value, sev.sigev_notify, "SIGEV_???");
546                 tprints(", ");
547                 if (sev.sigev_notify == SIGEV_THREAD_ID)
548                         tprintf("{%d}", sev.un.tid);
549                 else if (sev.sigev_notify == SIGEV_THREAD)
550                         tprintf("{%#x, %#x}",
551                                 sev.un.thread.function,
552                                 sev.un.thread.attribute);
553                 else
554                         tprints("{...}");
555                 tprints("}");
556         }
557 }
558 #endif
559
560 void
561 printsigevent(struct tcb *tcp, long arg)
562 {
563         struct sigevent sev;
564
565 #if SUPPORTED_PERSONALITIES > 1
566         if (current_wordsize == 4) {
567                 printsigevent32(tcp, arg);
568                 return;
569         }
570 #endif
571         if (!umove_or_printaddr(tcp, arg, &sev)) {
572                 tprintf("{%p, ", sev.sigev_value.sival_ptr);
573                 if (sev.sigev_notify == SIGEV_SIGNAL)
574                         tprintf("%s, ", signame(sev.sigev_signo));
575                 else
576                         tprintf("%u, ", sev.sigev_signo);
577                 printxval(sigev_value, sev.sigev_notify, "SIGEV_???");
578                 tprints(", ");
579                 if (sev.sigev_notify == SIGEV_THREAD_ID)
580 #if defined(HAVE_STRUCT_SIGEVENT__SIGEV_UN__PAD)
581                         /* _pad[0] is the _tid field which might not be
582                            present in the userlevel definition of the
583                            struct.  */
584                         tprintf("{%d}", sev._sigev_un._pad[0]);
585 #elif defined(HAVE_STRUCT_SIGEVENT___PAD)
586                         tprintf("{%d}", sev.__pad[0]);
587 #else
588 # warning unfamiliar struct sigevent => incomplete SIGEV_THREAD_ID decoding
589                         tprints("{...}");
590 #endif
591                 else if (sev.sigev_notify == SIGEV_THREAD)
592                         tprintf("{%p, %p}", sev.sigev_notify_function,
593                                 sev.sigev_notify_attributes);
594                 else
595                         tprints("{...}");
596                 tprints("}");
597         }
598 }
599
600 SYS_FUNC(timer_create)
601 {
602         if (entering(tcp)) {
603                 printclockname(tcp->u_arg[0]);
604                 tprints(", ");
605                 printsigevent(tcp, tcp->u_arg[1]);
606                 tprints(", ");
607         } else {
608                 printnum_int(tcp, tcp->u_arg[2], "%d");
609         }
610         return 0;
611 }
612
613 SYS_FUNC(timer_settime)
614 {
615         if (entering(tcp)) {
616                 tprintf("%d, ", (int) tcp->u_arg[0]);
617                 printflags(clockflags, tcp->u_arg[1], "TIMER_???");
618                 tprints(", ");
619                 printitv(tcp, tcp->u_arg[2]);
620                 tprints(", ");
621         } else {
622                 printitv(tcp, tcp->u_arg[3]);
623         }
624         return 0;
625 }
626
627 SYS_FUNC(timer_gettime)
628 {
629         if (entering(tcp)) {
630                 tprintf("%d, ", (int) tcp->u_arg[0]);
631         } else {
632                 printitv(tcp, tcp->u_arg[1]);
633         }
634         return 0;
635 }
636
637 #include "xlat/timerfdflags.h"
638
639 SYS_FUNC(timerfd)
640 {
641         /* It does not matter that the kernel uses itimerspec.  */
642         tprintf("%ld, ", tcp->u_arg[0]);
643         printclockname(tcp->u_arg[0]);
644         tprints(", ");
645         printflags(timerfdflags, tcp->u_arg[2], "TFD_???");
646         tprints(", ");
647         printitv(tcp, tcp->u_arg[3]);
648
649         return RVAL_DECODED;
650 }
651
652 SYS_FUNC(timerfd_create)
653 {
654         printclockname(tcp->u_arg[0]);
655         tprints(", ");
656         printflags(timerfdflags, tcp->u_arg[1], "TFD_???");
657
658         return RVAL_DECODED;
659 }
660
661 SYS_FUNC(timerfd_settime)
662 {
663         printfd(tcp, tcp->u_arg[0]);
664         tprints(", ");
665         printflags(timerfdflags, tcp->u_arg[1], "TFD_???");
666         tprints(", ");
667         printitv(tcp, tcp->u_arg[2]);
668         tprints(", ");
669         printitv(tcp, tcp->u_arg[3]);
670
671         return RVAL_DECODED;
672 }
673
674 SYS_FUNC(timerfd_gettime)
675 {
676         if (entering(tcp)) {
677                 printfd(tcp, tcp->u_arg[0]);
678                 tprints(", ");
679         } else {
680                 printitv(tcp, tcp->u_arg[1]);
681         }
682         return 0;
683 }