]> granicus.if.org Git - strace/blob - tests/tests.h
tests: add file:line to perror_msg_and_fail/error_msg_and_fail output
[strace] / tests / tests.h
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2017 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 #ifndef STRACE_TESTS_H
30 #define STRACE_TESTS_H
31
32 # ifdef HAVE_CONFIG_H
33 #  include "config.h"
34 # endif
35
36 # include <sys/types.h>
37 # include "kernel_types.h"
38 # include "gcc_compat.h"
39 # include "macros.h"
40
41 /*
42  * The printf-like function to use in header files
43  * shared between strace and its tests.
44  */
45 #ifndef STRACE_PRINTF
46 # define STRACE_PRINTF printf
47 #endif
48
49 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
50 #ifndef VERBOSE
51 # define VERBOSE 0
52 #endif
53
54 #ifndef DEFAULT_STRLEN
55 /* Default maximum # of bytes printed in printstr et al. */
56 # define DEFAULT_STRLEN 32
57 #endif
58
59 /* Cached sysconf(_SC_PAGESIZE). */
60 size_t get_page_size(void);
61
62 /* The size of kernel's sigset_t. */
63 unsigned int get_sigset_size(void);
64
65 /* Print message and strerror(errno) to stderr, then exit(1). */
66 void perror_msg_and_fail(const char *, ...)
67         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
68 /* Print message to stderr, then exit(1). */
69 void error_msg_and_fail(const char *, ...)
70         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
71 /* Print message to stderr, then exit(77). */
72 void error_msg_and_skip(const char *, ...)
73         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
74 /* Print message and strerror(errno) to stderr, then exit(77). */
75 void perror_msg_and_skip(const char *, ...)
76         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
77
78 #ifndef perror_msg_and_fail
79 # define perror_msg_and_fail(fmt_, ...) \
80         perror_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
81 #endif
82 #ifndef perror_msg_and_fail
83 # define error_msg_and_fail(fmt_, ...) \
84         error_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
85 #endif
86
87 /* Stat the specified file and skip the test if the stat call failed. */
88 void skip_if_unavailable(const char *);
89
90 /*
91  * Allocate memory that ends on the page boundary.
92  * Pages allocated by this call are preceded by an unmapped page
93  * and followed also by an unmapped page.
94  */
95 void *tail_alloc(const size_t)
96         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
97 /* Allocate memory using tail_alloc, then memcpy. */
98 void *tail_memdup(const void *, const size_t)
99         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
100
101 /*
102  * Allocate an object of the specified type at the end
103  * of a mapped memory region.
104  * Assign its address to the specified constant pointer.
105  */
106 #define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr)        \
107         type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
108
109 /*
110  * Allocate an object of the specified type at the end
111  * of a mapped memory region.
112  * Assign its address to the specified variable pointer.
113  */
114 #define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr)          \
115         type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
116
117 /*
118  * Fill memory (pointed by ptr, having size bytes) with different bytes (with
119  * values starting with start and resetting every period) in order to catch
120  * sign, byte order and/or alignment errors.
121  */
122 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
123                     unsigned char period);
124 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
125 void fill_memory(void *ptr, size_t size);
126
127 /* Close stdin, move stdout to a non-standard descriptor, and print. */
128 void tprintf(const char *, ...)
129         ATTRIBUTE_FORMAT((printf, 1, 2));
130
131 /* Make a hexdump copy of C string */
132 const char *hexdump_strdup(const char *);
133
134 /* Make a hexdump copy of memory */
135 const char *hexdump_memdup(const char *, size_t);
136
137 /* Make a hexquoted copy of a string */
138 const char *hexquote_strndup(const char *, size_t);
139
140 /* Return inode number of socket descriptor. */
141 unsigned long inode_of_sockfd(int);
142
143 /* Print string in a quoted form. */
144 void print_quoted_string(const char *);
145
146 /*
147  * Print a NUL-terminated string `str' of length up to `size' - 1
148  * in a quoted form.
149  */
150 void print_quoted_cstring(const char *str, size_t size);
151
152 /* Print memory in a quoted form. */
153 void print_quoted_memory(const void *, size_t);
154
155 /* Print memory in a hexquoted form. */
156 void print_quoted_hex(const void *, size_t);
157
158 /* Print time_t and nanoseconds in symbolic format. */
159 void print_time_t_nsec(time_t, unsigned long long, int);
160
161 /* Print time_t and microseconds in symbolic format. */
162 void print_time_t_usec(time_t, unsigned long long, int);
163
164 /* Read an int from the file. */
165 int read_int_from_file(const char *, int *);
166
167 /* Check whether given uid matches kernel overflowuid. */
168 void check_overflowuid(const int);
169
170 /* Check whether given gid matches kernel overflowgid. */
171 void check_overflowgid(const int);
172
173 /* Translate errno to its name. */
174 const char *errno2name(void);
175
176 /* Translate signal number to its name. */
177 const char *signal2name(int);
178
179 /* Print return code and, in case return code is -1, errno information. */
180 const char *sprintrc(long rc);
181 /* sprintrc variant suitable for usage as part of grep pattern. */
182 const char *sprintrc_grep(long rc);
183
184 struct xlat;
185
186 /* Print flags in symbolic form according to xlat table. */
187 int printflags(const struct xlat *, const unsigned long long, const char *);
188
189 /* Print constant in symbolic form according to xlat table. */
190 int printxval(const struct xlat *, const unsigned long long, const char *);
191
192 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
193 int socketcall(const int nr, const int call,
194                long a1, long a2, long a3, long a4, long a5);
195
196 /* Wrappers for recvmmsg and sendmmsg syscalls. */
197 struct mmsghdr;
198 struct timespec;
199 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
200 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
201
202 /* Create a netlink socket. */
203 int create_nl_socket_ext(int proto, const char *name);
204 #define create_nl_socket(proto) create_nl_socket_ext((proto), #proto)
205
206 /* Create a pipe with maximized descriptor numbers. */
207 void pipe_maxfd(int pipefd[2]);
208
209 /* if_nametoindex("lo") */
210 unsigned int ifindex_lo(void);
211
212 #ifdef HAVE_IF_INDEXTONAME
213 # define IFINDEX_LO_STR "if_nametoindex(\"lo\")"
214 #else
215 # define IFINDEX_LO_STR "1"
216 #endif
217
218 #define F8ILL_KULONG_SUPPORTED  (sizeof(void *) < sizeof(kernel_ulong_t))
219 #define F8ILL_KULONG_MASK       ((kernel_ulong_t) 0xffffffff00000000ULL)
220
221 /*
222  * For 64-bit kernel_ulong_t and 32-bit pointer,
223  * return a kernel_ulong_t value by filling higher bits.
224  * For other architertures, return the original pointer.
225  */
226 static inline kernel_ulong_t
227 f8ill_ptr_to_kulong(const void *const ptr)
228 {
229         const unsigned long uptr = (unsigned long) ptr;
230         return F8ILL_KULONG_SUPPORTED
231                ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
232 }
233
234 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
235
236 /* Zero-extend a signed integer type to unsigned long long. */
237 #define zero_extend_signed_to_ull(v) \
238         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
239          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
240          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
241          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
242          (unsigned long long) (v))
243
244 /* Sign-extend an unsigned integer type to long long. */
245 #define sign_extend_unsigned_to_ll(v) \
246         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
247          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
248          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
249          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
250          (long long) (v))
251
252 # define SKIP_MAIN_UNDEFINED(arg) \
253         int main(void) { error_msg_and_skip("undefined: %s", arg); }
254
255 # if WORDS_BIGENDIAN
256 #  define LL_PAIR(HI, LO) (HI), (LO)
257 # else
258 #  define LL_PAIR(HI, LO) (LO), (HI)
259 # endif
260 # define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
261
262 # define _STR(_arg) #_arg
263 # define ARG_STR(_arg) (_arg), #_arg
264 # define ARG_ULL_STR(_arg) _arg##ULL, #_arg
265
266 /*
267  * Assign an object of type DEST_TYPE at address DEST_ADDR
268  * using memcpy to avoid potential unaligned access.
269  */
270 #define SET_STRUCT(DEST_TYPE, DEST_ADDR, ...)                                           \
271         do {                                                                            \
272                 DEST_TYPE dest_type_tmp_var = { __VA_ARGS__ };                          \
273                 memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var));       \
274         } while (0)
275
276 #define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
277
278 #endif /* !STRACE_TESTS_H */