xref: /linux/tools/perf/util/util.h (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
3 
4 #ifndef FLEX_ARRAY
5 /*
6  * See if our compiler is known to support flexible array members.
7  */
8 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
9 # define FLEX_ARRAY /* empty */
10 #elif defined(__GNUC__)
11 # if (__GNUC__ >= 3)
12 #  define FLEX_ARRAY /* empty */
13 # else
14 #  define FLEX_ARRAY 0 /* older GNU extension */
15 # endif
16 #endif
17 
18 /*
19  * Otherwise, default to safer but a bit wasteful traditional style
20  */
21 #ifndef FLEX_ARRAY
22 # define FLEX_ARRAY 1
23 #endif
24 #endif
25 
26 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
27 
28 #ifdef __GNUC__
29 #define TYPEOF(x) (__typeof__(x))
30 #else
31 #define TYPEOF(x)
32 #endif
33 
34 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
35 #define HAS_MULTI_BITS(i)  ((i) & ((i) - 1))  /* checks if an integer has more than 1 bit set */
36 
37 /* Approximation of the length of the decimal representation of this type. */
38 #define decimal_length(x)	((int)(sizeof(x) * 2.56 + 0.5) + 1)
39 
40 #define _ALL_SOURCE 1
41 #define _BSD_SOURCE 1
42 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
43 #define _DEFAULT_SOURCE 1
44 #define HAS_BOOL
45 
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <sys/stat.h>
49 #include <sys/statfs.h>
50 #include <fcntl.h>
51 #include <stdbool.h>
52 #include <stddef.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <string.h>
56 #include <errno.h>
57 #include <limits.h>
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <dirent.h>
61 #include <sys/time.h>
62 #include <time.h>
63 #include <signal.h>
64 #include <fnmatch.h>
65 #include <assert.h>
66 #include <regex.h>
67 #include <utime.h>
68 #include <sys/wait.h>
69 #include <poll.h>
70 #include <sys/socket.h>
71 #include <sys/ioctl.h>
72 #include <inttypes.h>
73 #include <linux/kernel.h>
74 #include <linux/magic.h>
75 #include <linux/types.h>
76 #include <sys/ttydefaults.h>
77 #include <api/fs/debugfs.h>
78 #include <api/fs/tracefs.h>
79 #include <termios.h>
80 #include <linux/bitops.h>
81 #include <termios.h>
82 
83 extern const char *graph_line;
84 extern const char *graph_dotted_line;
85 extern char buildid_dir[];
86 extern char tracing_path[];
87 extern char tracing_events_path[];
88 extern void perf_debugfs_set_path(const char *mountpoint);
89 const char *perf_debugfs_mount(const char *mountpoint);
90 char *get_tracing_file(const char *name);
91 void put_tracing_file(char *file);
92 
93 /* On most systems <limits.h> would have given us this, but
94  * not on some systems (e.g. GNU/Hurd).
95  */
96 #ifndef PATH_MAX
97 #define PATH_MAX 4096
98 #endif
99 
100 #ifndef PRIuMAX
101 #define PRIuMAX "llu"
102 #endif
103 
104 #ifndef PRIu32
105 #define PRIu32 "u"
106 #endif
107 
108 #ifndef PRIx32
109 #define PRIx32 "x"
110 #endif
111 
112 #ifndef PATH_SEP
113 #define PATH_SEP ':'
114 #endif
115 
116 #ifndef STRIP_EXTENSION
117 #define STRIP_EXTENSION ""
118 #endif
119 
120 #ifndef has_dos_drive_prefix
121 #define has_dos_drive_prefix(path) 0
122 #endif
123 
124 #ifndef is_dir_sep
125 #define is_dir_sep(c) ((c) == '/')
126 #endif
127 
128 #ifdef __GNUC__
129 #define NORETURN __attribute__((__noreturn__))
130 #else
131 #define NORETURN
132 #ifndef __attribute__
133 #define __attribute__(x)
134 #endif
135 #endif
136 
137 #define PERF_GTK_DSO  "libperf-gtk.so"
138 
139 /* General helper functions */
140 extern void usage(const char *err) NORETURN;
141 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
142 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
143 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
144 
145 #include "../../../include/linux/stringify.h"
146 
147 #define DIE_IF(cnd)	\
148 	do { if (cnd)	\
149 		die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
150 		    __stringify(cnd) "\n");				\
151 	} while (0)
152 
153 
154 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
155 
156 extern int prefixcmp(const char *str, const char *prefix);
157 extern void set_buildid_dir(const char *dir);
158 
159 static inline const char *skip_prefix(const char *str, const char *prefix)
160 {
161 	size_t len = strlen(prefix);
162 	return strncmp(str, prefix, len) ? NULL : str + len;
163 }
164 
165 #ifdef __GLIBC_PREREQ
166 #if __GLIBC_PREREQ(2, 1)
167 #define HAVE_STRCHRNUL
168 #endif
169 #endif
170 
171 #ifndef HAVE_STRCHRNUL
172 #define strchrnul gitstrchrnul
173 static inline char *gitstrchrnul(const char *s, int c)
174 {
175 	while (*s && *s != c)
176 		s++;
177 	return (char *)s;
178 }
179 #endif
180 
181 /*
182  * Wrappers:
183  */
184 extern char *xstrdup(const char *str);
185 extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
186 
187 
188 static inline void *zalloc(size_t size)
189 {
190 	return calloc(1, size);
191 }
192 
193 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
194 
195 static inline int has_extension(const char *filename, const char *ext)
196 {
197 	size_t len = strlen(filename);
198 	size_t extlen = strlen(ext);
199 
200 	return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
201 }
202 
203 /* Sane ctype - no locale, and works with signed chars */
204 #undef isascii
205 #undef isspace
206 #undef isdigit
207 #undef isxdigit
208 #undef isalpha
209 #undef isprint
210 #undef isalnum
211 #undef islower
212 #undef isupper
213 #undef tolower
214 #undef toupper
215 
216 #ifndef NSEC_PER_MSEC
217 #define NSEC_PER_MSEC	1000000L
218 #endif
219 
220 int parse_nsec_time(const char *str, u64 *ptime);
221 
222 extern unsigned char sane_ctype[256];
223 #define GIT_SPACE		0x01
224 #define GIT_DIGIT		0x02
225 #define GIT_ALPHA		0x04
226 #define GIT_GLOB_SPECIAL	0x08
227 #define GIT_REGEX_SPECIAL	0x10
228 #define GIT_PRINT_EXTRA		0x20
229 #define GIT_PRINT		0x3E
230 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
231 #define isascii(x) (((x) & ~0x7f) == 0)
232 #define isspace(x) sane_istest(x,GIT_SPACE)
233 #define isdigit(x) sane_istest(x,GIT_DIGIT)
234 #define isxdigit(x)	\
235 	(sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
236 #define isalpha(x) sane_istest(x,GIT_ALPHA)
237 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
238 #define isprint(x) sane_istest(x,GIT_PRINT)
239 #define islower(x) (sane_istest(x,GIT_ALPHA) && (x & 0x20))
240 #define isupper(x) (sane_istest(x,GIT_ALPHA) && !(x & 0x20))
241 #define tolower(x) sane_case((unsigned char)(x), 0x20)
242 #define toupper(x) sane_case((unsigned char)(x), 0)
243 
244 static inline int sane_case(int x, int high)
245 {
246 	if (sane_istest(x, GIT_ALPHA))
247 		x = (x & ~0x20) | high;
248 	return x;
249 }
250 
251 int mkdir_p(char *path, mode_t mode);
252 int rm_rf(char *path);
253 int copyfile(const char *from, const char *to);
254 int copyfile_mode(const char *from, const char *to, mode_t mode);
255 int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
256 
257 s64 perf_atoll(const char *str);
258 char **argv_split(const char *str, int *argcp);
259 void argv_free(char **argv);
260 bool strglobmatch(const char *str, const char *pat);
261 bool strlazymatch(const char *str, const char *pat);
262 static inline bool strisglob(const char *str)
263 {
264 	return strpbrk(str, "*?[") != NULL;
265 }
266 int strtailcmp(const char *s1, const char *s2);
267 char *strxfrchar(char *s, char from, char to);
268 unsigned long convert_unit(unsigned long value, char *unit);
269 ssize_t readn(int fd, void *buf, size_t n);
270 ssize_t writen(int fd, void *buf, size_t n);
271 
272 struct perf_event_attr;
273 
274 void event_attr_init(struct perf_event_attr *attr);
275 
276 #define _STR(x) #x
277 #define STR(x) _STR(x)
278 
279 size_t hex_width(u64 v);
280 int hex2u64(const char *ptr, u64 *val);
281 
282 char *ltrim(char *s);
283 char *rtrim(char *s);
284 
285 void dump_stack(void);
286 void sighandler_dump_stack(int sig);
287 
288 extern unsigned int page_size;
289 extern int cacheline_size;
290 
291 void get_term_dimensions(struct winsize *ws);
292 void set_term_quiet_input(struct termios *old);
293 
294 struct parse_tag {
295 	char tag;
296 	int mult;
297 };
298 
299 unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
300 
301 #define SRCLINE_UNKNOWN  ((char *) "??:0")
302 
303 static inline int path__join(char *bf, size_t size,
304 			     const char *path1, const char *path2)
305 {
306 	return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
307 }
308 
309 static inline int path__join3(char *bf, size_t size,
310 			      const char *path1, const char *path2,
311 			      const char *path3)
312 {
313 	return scnprintf(bf, size, "%s%s%s%s%s",
314 			 path1, path1[0] ? "/" : "",
315 			 path2, path2[0] ? "/" : "", path3);
316 }
317 
318 struct dso;
319 struct symbol;
320 
321 extern bool srcline_full_filename;
322 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
323 		  bool show_sym);
324 void free_srcline(char *srcline);
325 
326 int filename__read_str(const char *filename, char **buf, size_t *sizep);
327 int perf_event_paranoid(void);
328 
329 void mem_bswap_64(void *src, int byte_size);
330 void mem_bswap_32(void *src, int byte_size);
331 
332 const char *get_filename_for_perf_kvm(void);
333 bool find_process(const char *name);
334 
335 #ifdef HAVE_ZLIB_SUPPORT
336 int gzip_decompress_to_file(const char *input, int output_fd);
337 #endif
338 
339 #ifdef HAVE_LZMA_SUPPORT
340 int lzma_decompress_to_file(const char *input, int output_fd);
341 #endif
342 
343 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
344 
345 static inline char *asprintf_expr_in_ints(const char *var, size_t nints, int *ints)
346 {
347 	return asprintf_expr_inout_ints(var, true, nints, ints);
348 }
349 
350 static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int *ints)
351 {
352 	return asprintf_expr_inout_ints(var, false, nints, ints);
353 }
354 
355 int get_stack_size(const char *str, unsigned long *_size);
356 
357 #endif /* GIT_COMPAT_UTIL_H */
358