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