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