util.h 8.9 KB

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