Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | #ifndef GIT_COMPAT_UTIL_H |
| 2 | #define GIT_COMPAT_UTIL_H |
| 3 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 4 | #define _ALL_SOURCE 1 |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 5 | #define _BSD_SOURCE 1 |
Chanho Park | 512fe36 | 2014-09-12 11:10:17 +0900 | [diff] [blame] | 6 | /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ |
| 7 | #define _DEFAULT_SOURCE 1 |
Arnaldo Carvalho de Melo | e206d55 | 2010-04-03 10:19:26 -0300 | [diff] [blame] | 8 | #define HAS_BOOL |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 9 | |
| 10 | #include <unistd.h> |
| 11 | #include <stdio.h> |
| 12 | #include <sys/stat.h> |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 13 | #include <sys/statfs.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 14 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | e206d55 | 2010-04-03 10:19:26 -0300 | [diff] [blame] | 15 | #include <stdbool.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 16 | #include <stddef.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <string.h> |
Josh Poimboeuf | 1fe143c | 2015-12-07 22:21:42 -0600 | [diff] [blame] | 20 | #include <term.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 21 | #include <limits.h> |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <dirent.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <time.h> |
| 27 | #include <signal.h> |
| 28 | #include <fnmatch.h> |
| 29 | #include <assert.h> |
| 30 | #include <regex.h> |
| 31 | #include <utime.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 32 | #include <sys/wait.h> |
Arnaldo Carvalho de Melo | a8fa496 | 2014-09-15 15:54:34 -0300 | [diff] [blame] | 33 | #include <poll.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 34 | #include <sys/socket.h> |
| 35 | #include <sys/ioctl.h> |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 36 | #include <linux/kernel.h> |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 37 | #include <linux/types.h> |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 38 | #include <sys/ttydefaults.h> |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 39 | #include <api/fs/tracing_path.h> |
Joonsoo Kim | 756bbc8 | 2013-06-19 10:02:30 +0900 | [diff] [blame] | 40 | #include <termios.h> |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 41 | #include <linux/bitops.h> |
Jiri Olsa | 9398c48 | 2014-08-11 10:50:02 +0200 | [diff] [blame] | 42 | #include <termios.h> |
Frederic Weisbecker | 1fe2c10 | 2009-08-12 10:19:53 +0200 | [diff] [blame] | 43 | |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 44 | extern char buildid_dir[]; |
Arnaldo Carvalho de Melo | 2890284 | 2009-11-23 17:51:08 -0200 | [diff] [blame] | 45 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 46 | #ifdef __GNUC__ |
| 47 | #define NORETURN __attribute__((__noreturn__)) |
| 48 | #else |
| 49 | #define NORETURN |
| 50 | #ifndef __attribute__ |
| 51 | #define __attribute__(x) |
| 52 | #endif |
| 53 | #endif |
| 54 | |
Namhyung Kim | fc67297 | 2013-09-13 15:27:43 +0900 | [diff] [blame] | 55 | #define PERF_GTK_DSO "libperf-gtk.so" |
| 56 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 57 | /* General helper functions */ |
Arnaldo Carvalho de Melo | 3938bad | 2016-03-23 15:06:35 -0300 | [diff] [blame] | 58 | void usage(const char *err) NORETURN; |
| 59 | void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); |
| 60 | int error(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 61 | void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 62 | |
Arnaldo Carvalho de Melo | 3938bad | 2016-03-23 15:06:35 -0300 | [diff] [blame] | 63 | void set_warning_routine(void (*routine)(const char *err, va_list params)); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 64 | |
Arnaldo Carvalho de Melo | 3938bad | 2016-03-23 15:06:35 -0300 | [diff] [blame] | 65 | int prefixcmp(const char *str, const char *prefix); |
| 66 | void set_buildid_dir(const char *dir); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 67 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 68 | #ifdef __GLIBC_PREREQ |
| 69 | #if __GLIBC_PREREQ(2, 1) |
| 70 | #define HAVE_STRCHRNUL |
| 71 | #endif |
| 72 | #endif |
| 73 | |
| 74 | #ifndef HAVE_STRCHRNUL |
| 75 | #define strchrnul gitstrchrnul |
| 76 | static inline char *gitstrchrnul(const char *s, int c) |
| 77 | { |
| 78 | while (*s && *s != c) |
| 79 | s++; |
| 80 | return (char *)s; |
| 81 | } |
| 82 | #endif |
| 83 | |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 84 | static inline void *zalloc(size_t size) |
| 85 | { |
| 86 | return calloc(1, size); |
| 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 89 | #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 90 | |
Arnaldo Carvalho de Melo | 8ec20b1 | 2017-04-18 10:57:25 -0300 | [diff] [blame^] | 91 | struct strlist; |
| 92 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 93 | int mkdir_p(char *path, mode_t mode); |
Joe Stringer | 9a9c733 | 2017-01-26 13:19:59 -0800 | [diff] [blame] | 94 | int rm_rf(const char *path); |
Masami Hiramatsu | e1ce726 | 2016-04-26 18:02:42 +0900 | [diff] [blame] | 95 | struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *)); |
| 96 | bool lsdir_no_dot_filter(const char *name, struct dirent *d); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 97 | int copyfile(const char *from, const char *to); |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 98 | int copyfile_mode(const char *from, const char *to, mode_t mode); |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 99 | int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 101 | unsigned long convert_unit(unsigned long value, char *unit); |
Jiri Olsa | 727ebd5 | 2013-11-28 11:30:14 +0100 | [diff] [blame] | 102 | ssize_t readn(int fd, void *buf, size_t n); |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 103 | ssize_t writen(int fd, void *buf, size_t n); |
Arnaldo Carvalho de Melo | e206d55 | 2010-04-03 10:19:26 -0300 | [diff] [blame] | 104 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 105 | struct perf_event_attr; |
| 106 | |
| 107 | void event_attr_init(struct perf_event_attr *attr); |
| 108 | |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 109 | size_t hex_width(u64 v); |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 110 | int hex2u64(const char *ptr, u64 *val); |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 112 | void dump_stack(void); |
Arnaldo Carvalho de Melo | 07c1a0d | 2015-02-24 15:34:23 -0300 | [diff] [blame] | 113 | void sighandler_dump_stack(int sig); |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 114 | |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 115 | extern unsigned int page_size; |
Don Zickus | 2b1b710 | 2014-05-30 16:10:05 -0400 | [diff] [blame] | 116 | extern int cacheline_size; |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 117 | extern int sysctl_perf_event_max_stack; |
| 118 | extern int sysctl_perf_event_max_contexts_per_stack; |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 119 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 120 | struct parse_tag { |
| 121 | char tag; |
| 122 | int mult; |
| 123 | }; |
| 124 | |
| 125 | unsigned long parse_tag_value(const char *str, struct parse_tag *tags); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 126 | |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 127 | static inline int path__join(char *bf, size_t size, |
| 128 | const char *path1, const char *path2) |
| 129 | { |
| 130 | return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2); |
| 131 | } |
| 132 | |
| 133 | static inline int path__join3(char *bf, size_t size, |
| 134 | const char *path1, const char *path2, |
| 135 | const char *path3) |
| 136 | { |
| 137 | return scnprintf(bf, size, "%s%s%s%s%s", |
| 138 | path1, path1[0] ? "/" : "", |
| 139 | path2, path2[0] ? "/" : "", path3); |
| 140 | } |
| 141 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 142 | int perf_event_paranoid(void); |
Dongsheng Yang | e1a2b17 | 2013-12-06 17:25:51 -0500 | [diff] [blame] | 143 | |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 144 | void mem_bswap_64(void *src, int byte_size); |
| 145 | void mem_bswap_32(void *src, int byte_size); |
| 146 | |
Dongsheng Yang | e1a2b17 | 2013-12-06 17:25:51 -0500 | [diff] [blame] | 147 | const char *get_filename_for_perf_kvm(void); |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 148 | bool find_process(const char *name); |
Namhyung Kim | e92ce12 | 2014-10-31 16:51:38 +0900 | [diff] [blame] | 149 | |
| 150 | #ifdef HAVE_ZLIB_SUPPORT |
| 151 | int gzip_decompress_to_file(const char *input, int output_fd); |
| 152 | #endif |
| 153 | |
Jiri Olsa | 80a32e5b | 2015-01-29 13:29:39 +0100 | [diff] [blame] | 154 | #ifdef HAVE_LZMA_SUPPORT |
| 155 | int lzma_decompress_to_file(const char *input, int output_fd); |
| 156 | #endif |
| 157 | |
Kan Liang | 076a30c | 2015-08-06 15:44:52 -0400 | [diff] [blame] | 158 | int get_stack_size(const char *str, unsigned long *_size); |
| 159 | |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 160 | int fetch_kernel_version(unsigned int *puint, |
| 161 | char *str, size_t str_sz); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 162 | #define KVER_VERSION(x) (((x) >> 16) & 0xff) |
| 163 | #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff) |
| 164 | #define KVER_SUBLEVEL(x) ((x) & 0xff) |
| 165 | #define KVER_FMT "%d.%d.%d" |
| 166 | #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x) |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 167 | |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 168 | const char *perf_tip(const char *dirpath); |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 169 | bool is_regular_file(const char *file); |
Wang Nan | 37b2015 | 2016-01-25 09:56:13 +0000 | [diff] [blame] | 170 | int fetch_current_timestamp(char *buf, size_t sz); |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 171 | |
Arnaldo Carvalho de Melo | 120010c | 2017-03-02 12:55:49 -0300 | [diff] [blame] | 172 | #ifndef HAVE_SCHED_GETCPU_SUPPORT |
| 173 | int sched_getcpu(void); |
Arnaldo Carvalho de Melo | c7007e9 | 2016-07-12 10:29:31 -0300 | [diff] [blame] | 174 | #endif |
| 175 | |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 176 | int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz); |
| 177 | |
Jiri Olsa | 9808143 | 2017-01-09 10:51:55 +0100 | [diff] [blame] | 178 | int unit_number__scnprintf(char *buf, size_t size, u64 n); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 179 | |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 180 | #endif /* GIT_COMPAT_UTIL_H */ |