util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef GIT_COMPAT_UTIL_H
  2. #define GIT_COMPAT_UTIL_H
  3. #define _ALL_SOURCE 1
  4. #define _BSD_SOURCE 1
  5. /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
  6. #define _DEFAULT_SOURCE 1
  7. #define HAS_BOOL
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <sys/stat.h>
  11. #include <sys/statfs.h>
  12. #include <fcntl.h>
  13. #include <stdbool.h>
  14. #include <stddef.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <string.h>
  18. #include <limits.h>
  19. #include <sys/param.h>
  20. #include <sys/types.h>
  21. #include <assert.h>
  22. #include <sys/wait.h>
  23. #include <poll.h>
  24. #include <sys/socket.h>
  25. #include <sys/ioctl.h>
  26. #include <linux/kernel.h>
  27. #include <linux/types.h>
  28. extern char buildid_dir[];
  29. #ifdef __GNUC__
  30. #define NORETURN __attribute__((__noreturn__))
  31. #else
  32. #define NORETURN
  33. #ifndef __attribute__
  34. #define __attribute__(x)
  35. #endif
  36. #endif
  37. #define PERF_GTK_DSO "libperf-gtk.so"
  38. /* General helper functions */
  39. void usage(const char *err) NORETURN;
  40. void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  41. int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  42. void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  43. void set_warning_routine(void (*routine)(const char *err, va_list params));
  44. int prefixcmp(const char *str, const char *prefix);
  45. void set_buildid_dir(const char *dir);
  46. static inline void *zalloc(size_t size)
  47. {
  48. return calloc(1, size);
  49. }
  50. #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  51. struct dirent;
  52. struct strlist;
  53. int mkdir_p(char *path, mode_t mode);
  54. int rm_rf(const char *path);
  55. struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
  56. bool lsdir_no_dot_filter(const char *name, struct dirent *d);
  57. int copyfile(const char *from, const char *to);
  58. int copyfile_mode(const char *from, const char *to, mode_t mode);
  59. int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
  60. ssize_t readn(int fd, void *buf, size_t n);
  61. ssize_t writen(int fd, void *buf, size_t n);
  62. struct perf_event_attr;
  63. void event_attr_init(struct perf_event_attr *attr);
  64. size_t hex_width(u64 v);
  65. int hex2u64(const char *ptr, u64 *val);
  66. void dump_stack(void);
  67. void sighandler_dump_stack(int sig);
  68. extern unsigned int page_size;
  69. extern int cacheline_size;
  70. extern int sysctl_perf_event_max_stack;
  71. extern int sysctl_perf_event_max_contexts_per_stack;
  72. struct parse_tag {
  73. char tag;
  74. int mult;
  75. };
  76. unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
  77. int perf_event_paranoid(void);
  78. void mem_bswap_64(void *src, int byte_size);
  79. void mem_bswap_32(void *src, int byte_size);
  80. bool find_process(const char *name);
  81. #ifdef HAVE_ZLIB_SUPPORT
  82. int gzip_decompress_to_file(const char *input, int output_fd);
  83. #endif
  84. #ifdef HAVE_LZMA_SUPPORT
  85. int lzma_decompress_to_file(const char *input, int output_fd);
  86. #endif
  87. int get_stack_size(const char *str, unsigned long *_size);
  88. int fetch_kernel_version(unsigned int *puint,
  89. char *str, size_t str_sz);
  90. #define KVER_VERSION(x) (((x) >> 16) & 0xff)
  91. #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
  92. #define KVER_SUBLEVEL(x) ((x) & 0xff)
  93. #define KVER_FMT "%d.%d.%d"
  94. #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
  95. const char *perf_tip(const char *dirpath);
  96. #ifndef HAVE_SCHED_GETCPU_SUPPORT
  97. int sched_getcpu(void);
  98. #endif
  99. #endif /* GIT_COMPAT_UTIL_H */