2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2016-2018 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef STRACE_TESTS_H
30 #define STRACE_TESTS_H
37 #include <sys/types.h>
38 #include "kernel_types.h"
39 #include "gcc_compat.h"
43 * The printf-like function to use in header files
44 * shared between strace and its tests.
47 # define STRACE_PRINTF printf
50 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
55 #ifndef DEFAULT_STRLEN
56 /* Default maximum # of bytes printed in printstr et al. */
57 # define DEFAULT_STRLEN 32
60 /* Cached sysconf(_SC_PAGESIZE). */
61 size_t get_page_size(void);
63 /* The size of kernel's sigset_t. */
64 unsigned int get_sigset_size(void);
66 /* Print message and strerror(errno) to stderr, then exit(1). */
67 void perror_msg_and_fail(const char *, ...)
68 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
69 /* Print message to stderr, then exit(1). */
70 void error_msg_and_fail(const char *, ...)
71 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
72 /* Print message to stderr, then exit(77). */
73 void error_msg_and_skip(const char *, ...)
74 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
75 /* Print message and strerror(errno) to stderr, then exit(77). */
76 void perror_msg_and_skip(const char *, ...)
77 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
79 #ifndef perror_msg_and_fail
80 # define perror_msg_and_fail(fmt_, ...) \
81 perror_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
83 #ifndef perror_msg_and_fail
84 # define error_msg_and_fail(fmt_, ...) \
85 error_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
88 /* Stat the specified file and skip the test if the stat call failed. */
89 void skip_if_unavailable(const char *);
92 * Allocate memory that ends on the page boundary.
93 * Pages allocated by this call are preceded by an unmapped page
94 * and followed also by an unmapped page.
96 void *tail_alloc(const size_t)
97 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
98 /* Allocate memory using tail_alloc, then memcpy. */
99 void *tail_memdup(const void *, const size_t)
100 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
103 * Allocate an object of the specified type at the end
104 * of a mapped memory region.
105 * Assign its address to the specified constant pointer.
107 #define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr) \
108 type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
111 * Allocate an object of the specified type at the end
112 * of a mapped memory region.
113 * Assign its address to the specified variable pointer.
115 #define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr) \
116 type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
119 * Fill memory (pointed by ptr, having size bytes) with different bytes (with
120 * values starting with start and resetting every period) in order to catch
121 * sign, byte order and/or alignment errors.
123 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
124 unsigned char period);
125 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
126 void fill_memory(void *ptr, size_t size);
128 /* Close stdin, move stdout to a non-standard descriptor, and print. */
129 void tprintf(const char *, ...)
130 ATTRIBUTE_FORMAT((printf, 1, 2));
132 /* Make a hexdump copy of C string */
133 const char *hexdump_strdup(const char *);
135 /* Make a hexdump copy of memory */
136 const char *hexdump_memdup(const char *, size_t);
138 /* Make a hexquoted copy of a string */
139 const char *hexquote_strndup(const char *, size_t);
141 /* Return inode number of socket descriptor. */
142 unsigned long inode_of_sockfd(int);
144 /* Print string in a quoted form with optional escape characters. */
145 void print_quoted_string_ex(const char *, bool quote, const char *escape_str);
147 /* Print string in a quoted form. */
148 void print_quoted_string(const char *);
151 * Print a NUL-terminated string `str' of length up to `size' - 1
154 void print_quoted_cstring(const char *str, size_t size);
156 /* Print memory in a quoted form with optional escape characters. */
157 void print_quoted_memory_ex(const void *, size_t, bool quote,
158 const char *escape_chars);
160 /* Print memory in a quoted form. */
161 void print_quoted_memory(const void *, size_t);
163 /* Print memory in a hexquoted form. */
164 void print_quoted_hex(const void *, size_t);
166 /* Print time_t and nanoseconds in symbolic format. */
167 void print_time_t_nsec(time_t, unsigned long long, int);
169 /* Print time_t and microseconds in symbolic format. */
170 void print_time_t_usec(time_t, unsigned long long, int);
172 /* Read an int from the file. */
173 int read_int_from_file(const char *, int *);
175 /* Check whether given uid matches kernel overflowuid. */
176 void check_overflowuid(const int);
178 /* Check whether given gid matches kernel overflowgid. */
179 void check_overflowgid(const int);
181 /* Translate errno to its name. */
182 const char *errno2name(void);
184 /* Translate signal number to its name. */
185 const char *signal2name(int);
187 /* Print return code and, in case return code is -1, errno information. */
188 const char *sprintrc(long rc);
189 /* sprintrc variant suitable for usage as part of grep pattern. */
190 const char *sprintrc_grep(long rc);
194 /* Print flags in symbolic form according to xlat table. */
195 int printflags(const struct xlat *, const unsigned long long, const char *);
197 /* Print constant in symbolic form according to xlat table. */
198 int printxval(const struct xlat *, const unsigned long long, const char *);
200 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
201 int socketcall(const int nr, const int call,
202 long a1, long a2, long a3, long a4, long a5);
204 /* Wrappers for recvmmsg and sendmmsg syscalls. */
207 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
208 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
210 /* Create a netlink socket. */
211 int create_nl_socket_ext(int proto, const char *name);
212 #define create_nl_socket(proto) create_nl_socket_ext((proto), #proto)
214 /* Create a pipe with maximized descriptor numbers. */
215 void pipe_maxfd(int pipefd[2]);
217 /* if_nametoindex("lo") */
218 unsigned int ifindex_lo(void);
220 #ifdef HAVE_IF_INDEXTONAME
221 # define IFINDEX_LO_STR "if_nametoindex(\"lo\")"
223 # define IFINDEX_LO_STR "1"
226 #define F8ILL_KULONG_SUPPORTED (sizeof(void *) < sizeof(kernel_ulong_t))
227 #define F8ILL_KULONG_MASK ((kernel_ulong_t) 0xffffffff00000000ULL)
230 * For 64-bit kernel_ulong_t and 32-bit pointer,
231 * return a kernel_ulong_t value by filling higher bits.
232 * For other architertures, return the original pointer.
234 static inline kernel_ulong_t
235 f8ill_ptr_to_kulong(const void *const ptr)
237 const unsigned long uptr = (unsigned long) ptr;
238 return F8ILL_KULONG_SUPPORTED
239 ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
242 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
244 /* Zero-extend a signed integer type to unsigned long long. */
245 #define zero_extend_signed_to_ull(v) \
246 (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
247 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
248 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
249 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
250 (unsigned long long) (v))
252 /* Sign-extend an unsigned integer type to long long. */
253 #define sign_extend_unsigned_to_ll(v) \
254 (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
255 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
256 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
257 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
260 #define SKIP_MAIN_UNDEFINED(arg) \
261 int main(void) { error_msg_and_skip("undefined: %s", arg); }
264 # define LL_PAIR(HI, LO) (HI), (LO)
266 # define LL_PAIR(HI, LO) (LO), (HI)
268 #define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
270 #define _STR(_arg) #_arg
271 #define ARG_STR(_arg) (_arg), #_arg
272 #define ARG_ULL_STR(_arg) _arg##ULL, #_arg
275 * Assign an object of type DEST_TYPE at address DEST_ADDR
276 * using memcpy to avoid potential unaligned access.
278 #define SET_STRUCT(DEST_TYPE, DEST_ADDR, ...) \
280 DEST_TYPE dest_type_tmp_var = { __VA_ARGS__ }; \
281 memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var)); \
284 #define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
286 #endif /* !STRACE_TESTS_H */