]> granicus.if.org Git - strace/blob - print_timeval.c
configure: use AC_MSG_ERROR and AC_MSG_FAILURE consistently
[strace] / print_timeval.c
1 /*
2  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "defs.h"
29
30 #include DEF_MPERS_TYPE(timeval_t)
31
32 typedef struct timeval timeval_t;
33
34 #include MPERS_DEFS
35
36 #include "xstring.h"
37
38 static const char timeval_fmt[]  = "{tv_sec=%lld, tv_usec=%llu}";
39
40 static void
41 print_timeval_t(const timeval_t *t)
42 {
43         tprintf(timeval_fmt, (long long) t->tv_sec,
44                 zero_extend_signed_to_ull(t->tv_usec));
45 }
46
47 static void
48 print_timeval_t_utime(const timeval_t *t)
49 {
50         print_timeval_t(t);
51         tprints_comment(sprinttime_usec(t->tv_sec,
52                 zero_extend_signed_to_ull(t->tv_usec)));
53 }
54
55 MPERS_PRINTER_DECL(void, print_struct_timeval, const void *arg)
56 {
57         print_timeval_t(arg);
58 }
59
60 MPERS_PRINTER_DECL(bool, print_struct_timeval_data_size,
61                    const void *arg, const size_t size)
62 {
63         if (size < sizeof(timeval_t)) {
64                 tprints("?");
65                 return false;
66         }
67
68         print_timeval_t(arg);
69         return true;
70 }
71
72 MPERS_PRINTER_DECL(void, print_timeval,
73                    struct tcb *const tcp, const kernel_ulong_t addr)
74 {
75         timeval_t t;
76
77         if (umove_or_printaddr(tcp, addr, &t))
78                 return;
79
80         print_timeval_t(&t);
81 }
82
83 MPERS_PRINTER_DECL(void, print_timeval_utimes,
84                    struct tcb *const tcp, const kernel_ulong_t addr)
85 {
86         timeval_t t[2];
87
88         if (umove_or_printaddr(tcp, addr, &t))
89                 return;
90
91         tprints("[");
92         print_timeval_t_utime(&t[0]);
93         tprints(", ");
94         print_timeval_t_utime(&t[1]);
95         tprints("]");
96 }
97
98 MPERS_PRINTER_DECL(const char *, sprint_timeval,
99                    struct tcb *const tcp, const kernel_ulong_t addr)
100 {
101         timeval_t t;
102         static char buf[sizeof(timeval_fmt) + 3 * sizeof(t)];
103
104         if (!addr) {
105                 strcpy(buf, "NULL");
106         } else if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
107                    umove(tcp, addr, &t)) {
108                 xsprintf(buf, "%#" PRI_klx, addr);
109         } else {
110                 xsprintf(buf, timeval_fmt,
111                          (long long) t.tv_sec,
112                          zero_extend_signed_to_ull(t.tv_usec));
113         }
114
115         return buf;
116 }
117
118 MPERS_PRINTER_DECL(void, print_itimerval,
119                    struct tcb *const tcp, const kernel_ulong_t addr)
120 {
121         timeval_t t[2];
122
123         if (umove_or_printaddr(tcp, addr, &t))
124                 return;
125
126         tprints("{it_interval=");
127         print_timeval_t(&t[0]);
128         tprints(", it_value=");
129         print_timeval_t(&t[1]);
130         tprints("}");
131 }
132
133 #ifdef ALPHA
134
135 void
136 print_timeval32_t(const timeval32_t *t)
137 {
138         tprintf(timeval_fmt, (long long) t->tv_sec,
139                 zero_extend_signed_to_ull(t->tv_usec));
140 }
141
142 static void
143 print_timeval32_t_utime(const timeval32_t *t)
144 {
145         print_timeval32_t(t);
146         tprints_comment(sprinttime_usec(t->tv_sec,
147                 zero_extend_signed_to_ull(t->tv_usec)));
148 }
149
150 void
151 print_timeval32(struct tcb *const tcp, const kernel_ulong_t addr)
152 {
153         timeval32_t t;
154
155         if (umove_or_printaddr(tcp, addr, &t))
156                 return;
157
158         print_timeval32_t(&t);
159 }
160
161 void
162 print_timeval32_utimes(struct tcb *const tcp, const kernel_ulong_t addr)
163 {
164         timeval32_t t[2];
165
166         if (umove_or_printaddr(tcp, addr, &t))
167                 return;
168
169         tprints("[");
170         print_timeval32_t_utime(&t[0]);
171         tprints(", ");
172         print_timeval32_t_utime(&t[1]);
173         tprints("]");
174 }
175
176 void
177 print_itimerval32(struct tcb *const tcp, const kernel_ulong_t addr)
178 {
179         timeval32_t t[2];
180
181         if (umove_or_printaddr(tcp, addr, &t))
182                 return;
183
184         tprints("{it_interval=");
185         print_timeval32_t(&t[0]);
186         tprints(", it_value=");
187         print_timeval32_t(&t[1]);
188         tprints("}");
189 }
190
191 const char *
192 sprint_timeval32(struct tcb *const tcp, const kernel_ulong_t addr)
193 {
194         timeval32_t t;
195         static char buf[sizeof(timeval_fmt) + 3 * sizeof(t)];
196
197         if (!addr) {
198                 strcpy(buf, "NULL");
199         } else if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
200                    umove(tcp, addr, &t)) {
201                 xsprintf(buf, "%#" PRI_klx, addr);
202         } else {
203                 xsprintf(buf, timeval_fmt,
204                          (long long) t.tv_sec,
205                          zero_extend_signed_to_ull(t.tv_usec));
206         }
207
208         return buf;
209 }
210
211 #endif /* ALPHA */