]> granicus.if.org Git - strace/blob - print_timespec.h
maint: update for linux v5.3-rc8
[strace] / print_timespec.h
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2019 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: LGPL-2.1-or-later
7  */
8
9 #include "xstring.h"
10
11 #ifndef TIMESPEC_NSEC
12 # define TIMESPEC_NSEC tv_nsec
13 #endif
14
15 #ifndef UTIME_NOW
16 # define UTIME_NOW ((1l << 30) - 1l)
17 #endif
18 #ifndef UTIME_OMIT
19 # define UTIME_OMIT ((1l << 30) - 2l)
20 #endif
21
22 #define TIMESPEC_TO_SEC_NSEC(t_)        \
23         ((long long) (t_)->tv_sec),     \
24          zero_extend_signed_to_ull((t_)->TIMESPEC_NSEC)
25
26 static const char timespec_fmt[] =
27         "{tv_sec=%lld, " STRINGIFY_VAL(TIMESPEC_NSEC) "=%llu}";
28
29 static void
30 print_sec_nsec(long long sec, unsigned long long nsec)
31 {
32         tprintf(timespec_fmt, sec, nsec);
33 }
34
35 static void
36 print_timespec_t(const TIMESPEC_T *t)
37 {
38         print_sec_nsec(TIMESPEC_TO_SEC_NSEC(t));
39 }
40
41 #ifdef PRINT_TIMESPEC_DATA_SIZE
42 bool
43 PRINT_TIMESPEC_DATA_SIZE(const void *arg, const size_t size)
44 {
45         if (size < sizeof(TIMESPEC_T)) {
46                 tprints("?");
47                 return false;
48         }
49
50         print_timespec_t(arg);
51         return true;
52 }
53 #endif /* PRINT_TIMESPEC_DATA_SIZE */
54
55 #ifdef PRINT_TIMESPEC_ARRAY_DATA_SIZE
56 bool
57 PRINT_TIMESPEC_ARRAY_DATA_SIZE(const void *arg, const unsigned int nmemb,
58                                const size_t size)
59 {
60         const TIMESPEC_T *ts = arg;
61         unsigned int i;
62
63         if (nmemb > size / sizeof(TIMESPEC_T)) {
64                 tprints("?");
65                 return false;
66         }
67
68         tprints("[");
69
70         for (i = 0; i < nmemb; i++) {
71                 if (i)
72                         tprints(", ");
73                 print_timespec_t(&ts[i]);
74         }
75
76         tprints("]");
77         return true;
78 }
79 #endif /* PRINT_TIMESPEC_ARRAY_DATA_SIZE */
80
81 #ifdef PRINT_TIMESPEC
82 int
83 PRINT_TIMESPEC(struct tcb *const tcp, const kernel_ulong_t addr)
84 {
85         TIMESPEC_T t;
86
87         if (umove_or_printaddr(tcp, addr, &t))
88                 return -1;
89
90         print_timespec_t(&t);
91         return 0;
92 }
93 #endif /* PRINT_TIMESPEC */
94
95 #ifdef SPRINT_TIMESPEC
96 const char *
97 SPRINT_TIMESPEC(struct tcb *const tcp, const kernel_ulong_t addr)
98 {
99         TIMESPEC_T t;
100         static char buf[sizeof(timespec_fmt) + 3 * sizeof(t)];
101
102         if (!addr) {
103                 strcpy(buf, "NULL");
104         } else if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
105                    umove(tcp, addr, &t)) {
106                 xsprintf(buf, "%#" PRI_klx, addr);
107         } else {
108                 xsprintf(buf, timespec_fmt, TIMESPEC_TO_SEC_NSEC(&t));
109         }
110
111         return buf;
112 }
113 #endif /* SPRINT_TIMESPEC */
114
115 #ifdef PRINT_TIMESPEC_UTIME_PAIR
116 static void
117 print_timespec_t_utime(const TIMESPEC_T *t)
118 {
119         switch (t->TIMESPEC_NSEC) {
120         case UTIME_NOW:
121         case UTIME_OMIT:
122                 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
123                         print_timespec_t(t);
124                 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
125                         break;
126
127                 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
128                         ? tprints_comment : tprints)(t->TIMESPEC_NSEC == UTIME_NOW
129                                 ? "UTIME_NOW" : "UTIME_OMIT");
130                 break;
131         default:
132                 print_timespec_t(t);
133                 tprints_comment(sprinttime_nsec(TIMESPEC_TO_SEC_NSEC(t)));
134                 break;
135         }
136 }
137
138 int
139 PRINT_TIMESPEC_UTIME_PAIR(struct tcb *const tcp, const kernel_ulong_t addr)
140 {
141         TIMESPEC_T t[2];
142
143         if (umove_or_printaddr(tcp, addr, &t))
144                 return -1;
145
146         tprints("[");
147         print_timespec_t_utime(&t[0]);
148         tprints(", ");
149         print_timespec_t_utime(&t[1]);
150         tprints("]");
151         return 0;
152 }
153 #endif /* PRINT_TIMESPEC_UTIME_PAIR */
154
155 #ifdef PRINT_ITIMERSPEC
156 int
157 PRINT_ITIMERSPEC(struct tcb *const tcp, const kernel_ulong_t addr)
158 {
159         TIMESPEC_T t[2];
160
161         if (umove_or_printaddr(tcp, addr, &t))
162                 return -1;
163
164         tprints("{it_interval=");
165         print_timespec_t(&t[0]);
166         tprints(", it_value=");
167         print_timespec_t(&t[1]);
168         tprints("}");
169         return 0;
170 }
171 #endif /* PRINT_ITIMERSPEC */