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