PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/perf/util/util.h

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