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