util.h 7.9 KB

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