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