util.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  24. #ifdef __GNUC__
  25. #define TYPEOF(x) (__typeof__(x))
  26. #else
  27. #define TYPEOF(x)
  28. #endif
  29. #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
  30. #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
  31. /* Approximation of the length of the decimal representation of this type. */
  32. #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  33. #define _ALL_SOURCE 1
  34. #define _BSD_SOURCE 1
  35. /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
  36. #define _DEFAULT_SOURCE 1
  37. #define HAS_BOOL
  38. #include <unistd.h>
  39. #include <stdio.h>
  40. #include <sys/stat.h>
  41. #include <sys/statfs.h>
  42. #include <fcntl.h>
  43. #include <stdbool.h>
  44. #include <stddef.h>
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <string.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/magic.h>
  67. #include <linux/types.h>
  68. #include <sys/ttydefaults.h>
  69. #include <api/fs/debugfs.h>
  70. #include <api/fs/tracefs.h>
  71. #include <termios.h>
  72. #include <linux/bitops.h>
  73. #include <termios.h>
  74. extern const char *graph_line;
  75. extern const char *graph_dotted_line;
  76. extern char buildid_dir[];
  77. extern char tracing_path[];
  78. extern char tracing_events_path[];
  79. extern void perf_debugfs_set_path(const char *mountpoint);
  80. const char *perf_debugfs_mount(const char *mountpoint);
  81. char *get_tracing_file(const char *name);
  82. void put_tracing_file(char *file);
  83. /* On most systems <limits.h> would have given us this, but
  84. * not on some systems (e.g. GNU/Hurd).
  85. */
  86. #ifndef PATH_MAX
  87. #define PATH_MAX 4096
  88. #endif
  89. #ifndef PRIuMAX
  90. #define PRIuMAX "llu"
  91. #endif
  92. #ifndef PRIu32
  93. #define PRIu32 "u"
  94. #endif
  95. #ifndef PRIx32
  96. #define PRIx32 "x"
  97. #endif
  98. #ifndef PATH_SEP
  99. #define PATH_SEP ':'
  100. #endif
  101. #ifndef STRIP_EXTENSION
  102. #define STRIP_EXTENSION ""
  103. #endif
  104. #ifndef has_dos_drive_prefix
  105. #define has_dos_drive_prefix(path) 0
  106. #endif
  107. #ifndef is_dir_sep
  108. #define is_dir_sep(c) ((c) == '/')
  109. #endif
  110. #ifdef __GNUC__
  111. #define NORETURN __attribute__((__noreturn__))
  112. #else
  113. #define NORETURN
  114. #ifndef __attribute__
  115. #define __attribute__(x)
  116. #endif
  117. #endif
  118. #define PERF_GTK_DSO "libperf-gtk.so"
  119. /* General helper functions */
  120. extern void usage(const char *err) NORETURN;
  121. extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  122. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  123. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  124. #include "../../../include/linux/stringify.h"
  125. #define DIE_IF(cnd) \
  126. do { if (cnd) \
  127. die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
  128. __stringify(cnd) "\n"); \
  129. } while (0)
  130. extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  131. extern int prefixcmp(const char *str, const char *prefix);
  132. extern void set_buildid_dir(const char *dir);
  133. static inline const char *skip_prefix(const char *str, const char *prefix)
  134. {
  135. size_t len = strlen(prefix);
  136. return strncmp(str, prefix, len) ? NULL : str + len;
  137. }
  138. #ifdef __GLIBC_PREREQ
  139. #if __GLIBC_PREREQ(2, 1)
  140. #define HAVE_STRCHRNUL
  141. #endif
  142. #endif
  143. #ifndef HAVE_STRCHRNUL
  144. #define strchrnul gitstrchrnul
  145. static inline char *gitstrchrnul(const char *s, int c)
  146. {
  147. while (*s && *s != c)
  148. s++;
  149. return (char *)s;
  150. }
  151. #endif
  152. /*
  153. * Wrappers:
  154. */
  155. extern char *xstrdup(const char *str);
  156. extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
  157. static inline void *zalloc(size_t size)
  158. {
  159. return calloc(1, size);
  160. }
  161. #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  162. static inline int has_extension(const char *filename, const char *ext)
  163. {
  164. size_t len = strlen(filename);
  165. size_t extlen = strlen(ext);
  166. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  167. }
  168. /* Sane ctype - no locale, and works with signed chars */
  169. #undef isascii
  170. #undef isspace
  171. #undef isdigit
  172. #undef isxdigit
  173. #undef isalpha
  174. #undef isprint
  175. #undef isalnum
  176. #undef islower
  177. #undef isupper
  178. #undef tolower
  179. #undef toupper
  180. #ifndef NSEC_PER_MSEC
  181. #define NSEC_PER_MSEC 1000000L
  182. #endif
  183. int parse_nsec_time(const char *str, u64 *ptime);
  184. extern unsigned char sane_ctype[256];
  185. #define GIT_SPACE 0x01
  186. #define GIT_DIGIT 0x02
  187. #define GIT_ALPHA 0x04
  188. #define GIT_GLOB_SPECIAL 0x08
  189. #define GIT_REGEX_SPECIAL 0x10
  190. #define GIT_PRINT_EXTRA 0x20
  191. #define GIT_PRINT 0x3E
  192. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  193. #define isascii(x) (((x) & ~0x7f) == 0)
  194. #define isspace(x) sane_istest(x,GIT_SPACE)
  195. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  196. #define isxdigit(x) \
  197. (sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
  198. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  199. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  200. #define isprint(x) sane_istest(x,GIT_PRINT)
  201. #define islower(x) (sane_istest(x,GIT_ALPHA) && (x & 0x20))
  202. #define isupper(x) (sane_istest(x,GIT_ALPHA) && !(x & 0x20))
  203. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  204. #define toupper(x) sane_case((unsigned char)(x), 0)
  205. static inline int sane_case(int x, int high)
  206. {
  207. if (sane_istest(x, GIT_ALPHA))
  208. x = (x & ~0x20) | high;
  209. return x;
  210. }
  211. int mkdir_p(char *path, mode_t mode);
  212. int rm_rf(char *path);
  213. int copyfile(const char *from, const char *to);
  214. int copyfile_mode(const char *from, const char *to, mode_t mode);
  215. int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
  216. s64 perf_atoll(const char *str);
  217. char **argv_split(const char *str, int *argcp);
  218. void argv_free(char **argv);
  219. bool strglobmatch(const char *str, const char *pat);
  220. bool strlazymatch(const char *str, const char *pat);
  221. static inline bool strisglob(const char *str)
  222. {
  223. return strpbrk(str, "*?[") != NULL;
  224. }
  225. int strtailcmp(const char *s1, const char *s2);
  226. char *strxfrchar(char *s, char from, char to);
  227. unsigned long convert_unit(unsigned long value, char *unit);
  228. ssize_t readn(int fd, void *buf, size_t n);
  229. ssize_t writen(int fd, void *buf, size_t n);
  230. struct perf_event_attr;
  231. void event_attr_init(struct perf_event_attr *attr);
  232. #define _STR(x) #x
  233. #define STR(x) _STR(x)
  234. size_t hex_width(u64 v);
  235. int hex2u64(const char *ptr, u64 *val);
  236. char *ltrim(char *s);
  237. char *rtrim(char *s);
  238. void dump_stack(void);
  239. void sighandler_dump_stack(int sig);
  240. extern unsigned int page_size;
  241. extern int cacheline_size;
  242. void get_term_dimensions(struct winsize *ws);
  243. void set_term_quiet_input(struct termios *old);
  244. struct parse_tag {
  245. char tag;
  246. int mult;
  247. };
  248. unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
  249. #define SRCLINE_UNKNOWN ((char *) "??:0")
  250. static inline int path__join(char *bf, size_t size,
  251. const char *path1, const char *path2)
  252. {
  253. return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
  254. }
  255. static inline int path__join3(char *bf, size_t size,
  256. const char *path1, const char *path2,
  257. const char *path3)
  258. {
  259. return scnprintf(bf, size, "%s%s%s%s%s",
  260. path1, path1[0] ? "/" : "",
  261. path2, path2[0] ? "/" : "", path3);
  262. }
  263. struct dso;
  264. struct symbol;
  265. extern bool srcline_full_filename;
  266. char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  267. bool show_sym);
  268. void free_srcline(char *srcline);
  269. int filename__read_str(const char *filename, char **buf, size_t *sizep);
  270. int perf_event_paranoid(void);
  271. void mem_bswap_64(void *src, int byte_size);
  272. void mem_bswap_32(void *src, int byte_size);
  273. const char *get_filename_for_perf_kvm(void);
  274. bool find_process(const char *name);
  275. #ifdef HAVE_ZLIB_SUPPORT
  276. int gzip_decompress_to_file(const char *input, int output_fd);
  277. #endif
  278. #ifdef HAVE_LZMA_SUPPORT
  279. int lzma_decompress_to_file(const char *input, int output_fd);
  280. #endif
  281. char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
  282. static inline char *asprintf_expr_in_ints(const char *var, size_t nints, int *ints)
  283. {
  284. return asprintf_expr_inout_ints(var, true, nints, ints);
  285. }
  286. static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int *ints)
  287. {
  288. return asprintf_expr_inout_ints(var, false, nints, ints);
  289. }
  290. int get_stack_size(const char *str, unsigned long *_size);
  291. #endif /* GIT_COMPAT_UTIL_H */