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