1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2016-2018 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "defs.h"
30
31 #include DEF_MPERS_TYPE(timespec_t)
32
33 typedef struct timespec timespec_t;
34
35 #include MPERS_DEFS
36
37 #include "xstring.h"
38
39 #ifndef UTIME_NOW
40 # define UTIME_NOW ((1l << 30) - 1l)
41 #endif
42 #ifndef UTIME_OMIT
43 # define UTIME_OMIT ((1l << 30) - 2l)
44 #endif
45
46 static const char timespec_fmt[] = "{tv_sec=%lld, tv_nsec=%llu}";
47
48 static void
print_timespec_t(const timespec_t * t)49 print_timespec_t(const timespec_t *t)
50 {
51 tprintf(timespec_fmt, (long long) t->tv_sec,
52 zero_extend_signed_to_ull(t->tv_nsec));
53 }
54
55 static void
print_timespec_t_utime(const timespec_t * t)56 print_timespec_t_utime(const timespec_t *t)
57 {
58 switch (t->tv_nsec) {
59 case UTIME_NOW:
60 case UTIME_OMIT:
61 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
62 print_timespec_t(t);
63 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
64 break;
65
66 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
67 ? tprints_comment : tprints)(t->tv_nsec == UTIME_NOW
68 ? "UTIME_NOW" : "UTIME_OMIT");
69 break;
70 default:
71 print_timespec_t(t);
72 tprints_comment(sprinttime_nsec(t->tv_sec,
73 zero_extend_signed_to_ull(t->tv_nsec)));
74 break;
75 }
76 }
77
MPERS_PRINTER_DECL(bool,print_struct_timespec_data_size,const void * arg,const size_t size)78 MPERS_PRINTER_DECL(bool, print_struct_timespec_data_size,
79 const void *arg, const size_t size)
80 {
81 if (size < sizeof(timespec_t)) {
82 tprints("?");
83 return false;
84 }
85
86 print_timespec_t(arg);
87 return true;
88 }
89
MPERS_PRINTER_DECL(bool,print_struct_timespec_array_data_size,const void * arg,const unsigned int nmemb,const size_t size)90 MPERS_PRINTER_DECL(bool, print_struct_timespec_array_data_size,
91 const void *arg, const unsigned int nmemb,
92 const size_t size)
93 {
94 const timespec_t *ts = arg;
95 unsigned int i;
96
97 if (nmemb > size / sizeof(timespec_t)) {
98 tprints("?");
99 return false;
100 }
101
102 tprints("[");
103
104 for (i = 0; i < nmemb; i++) {
105 if (i)
106 tprints(", ");
107 print_timespec_t(&ts[i]);
108 }
109
110 tprints("]");
111 return true;
112 }
113
MPERS_PRINTER_DECL(void,print_timespec,struct tcb * const tcp,const kernel_ulong_t addr)114 MPERS_PRINTER_DECL(void, print_timespec,
115 struct tcb *const tcp, const kernel_ulong_t addr)
116 {
117 timespec_t t;
118
119 if (umove_or_printaddr(tcp, addr, &t))
120 return;
121
122 print_timespec_t(&t);
123 }
124
MPERS_PRINTER_DECL(const char *,sprint_timespec,struct tcb * const tcp,const kernel_ulong_t addr)125 MPERS_PRINTER_DECL(const char *, sprint_timespec,
126 struct tcb *const tcp, const kernel_ulong_t addr)
127 {
128 timespec_t t;
129 static char buf[sizeof(timespec_fmt) + 3 * sizeof(t)];
130
131 if (!addr) {
132 strcpy(buf, "NULL");
133 } else if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
134 umove(tcp, addr, &t)) {
135 xsprintf(buf, "%#" PRI_klx, addr);
136 } else {
137 xsprintf(buf, timespec_fmt,
138 (long long) t.tv_sec,
139 zero_extend_signed_to_ull(t.tv_nsec));
140 }
141
142 return buf;
143 }
144
MPERS_PRINTER_DECL(void,print_timespec_utime_pair,struct tcb * const tcp,const kernel_ulong_t addr)145 MPERS_PRINTER_DECL(void, print_timespec_utime_pair,
146 struct tcb *const tcp, const kernel_ulong_t addr)
147 {
148 timespec_t t[2];
149
150 if (umove_or_printaddr(tcp, addr, &t))
151 return;
152
153 tprints("[");
154 print_timespec_t_utime(&t[0]);
155 tprints(", ");
156 print_timespec_t_utime(&t[1]);
157 tprints("]");
158 }
159
MPERS_PRINTER_DECL(void,print_itimerspec,struct tcb * const tcp,const kernel_ulong_t addr)160 MPERS_PRINTER_DECL(void, print_itimerspec,
161 struct tcb *const tcp, const kernel_ulong_t addr)
162 {
163 timespec_t t[2];
164
165 if (umove_or_printaddr(tcp, addr, &t))
166 return;
167
168 tprints("{it_interval=");
169 print_timespec_t(&t[0]);
170 tprints(", it_value=");
171 print_timespec_t(&t[1]);
172 tprints("}");
173 }
174