util.h 8.9 KB

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