blob: 397676c252328803f9f25881fa21586a3b3146b1 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#ifndef GIT_COMPAT_UTIL_H
2#define GIT_COMPAT_UTIL_H
3
Ingo Molnar07800602009-04-20 15:00:56 +02004#define _ALL_SOURCE 1
Ingo Molnar07800602009-04-20 15:00:56 +02005#define _BSD_SOURCE 1
Chanho Park512fe362014-09-12 11:10:17 +09006/* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
7#define _DEFAULT_SOURCE 1
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -03008#define HAS_BOOL
Ingo Molnar07800602009-04-20 15:00:56 +02009
10#include <unistd.h>
11#include <stdio.h>
12#include <sys/stat.h>
Jason Baronf6bdafe2009-07-21 12:20:22 -040013#include <sys/statfs.h>
Ingo Molnar07800602009-04-20 15:00:56 +020014#include <fcntl.h>
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -030015#include <stdbool.h>
Ingo Molnar07800602009-04-20 15:00:56 +020016#include <stddef.h>
17#include <stdlib.h>
18#include <stdarg.h>
19#include <string.h>
Josh Poimboeuf1fe143c2015-12-07 22:21:42 -060020#include <term.h>
Ingo Molnar07800602009-04-20 15:00:56 +020021#include <limits.h>
22#include <sys/param.h>
23#include <sys/types.h>
24#include <dirent.h>
25#include <sys/time.h>
26#include <time.h>
27#include <signal.h>
28#include <fnmatch.h>
29#include <assert.h>
30#include <regex.h>
31#include <utime.h>
Ingo Molnar07800602009-04-20 15:00:56 +020032#include <sys/wait.h>
Arnaldo Carvalho de Meloa8fa4962014-09-15 15:54:34 -030033#include <poll.h>
Ingo Molnar07800602009-04-20 15:00:56 +020034#include <sys/socket.h>
35#include <sys/ioctl.h>
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030036#include <linux/kernel.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +020037#include <linux/types.h>
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -030038#include <sys/ttydefaults.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +020039#include <api/fs/tracing_path.h>
Joonsoo Kim756bbc82013-06-19 10:02:30 +090040#include <termios.h>
Adrian Hunter1dbfa9382013-12-09 15:18:39 +020041#include <linux/bitops.h>
Jiri Olsa9398c482014-08-11 10:50:02 +020042#include <termios.h>
Frederic Weisbecker1fe2c102009-08-12 10:19:53 +020043
Stephane Eranian45de34b2010-06-01 21:25:01 +020044extern char buildid_dir[];
Arnaldo Carvalho de Melo28902842009-11-23 17:51:08 -020045
Ingo Molnar07800602009-04-20 15:00:56 +020046#ifdef __GNUC__
47#define NORETURN __attribute__((__noreturn__))
48#else
49#define NORETURN
50#ifndef __attribute__
51#define __attribute__(x)
52#endif
53#endif
54
Namhyung Kimfc672972013-09-13 15:27:43 +090055#define PERF_GTK_DSO "libperf-gtk.so"
56
Ingo Molnar07800602009-04-20 15:00:56 +020057/* General helper functions */
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030058void usage(const char *err) NORETURN;
59void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
60int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
61void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
Ingo Molnar07800602009-04-20 15:00:56 +020062
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030063void set_warning_routine(void (*routine)(const char *err, va_list params));
Ingo Molnar07800602009-04-20 15:00:56 +020064
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030065int prefixcmp(const char *str, const char *prefix);
66void set_buildid_dir(const char *dir);
Ingo Molnar07800602009-04-20 15:00:56 +020067
Ingo Molnar07800602009-04-20 15:00:56 +020068#ifdef __GLIBC_PREREQ
69#if __GLIBC_PREREQ(2, 1)
70#define HAVE_STRCHRNUL
71#endif
72#endif
73
74#ifndef HAVE_STRCHRNUL
75#define strchrnul gitstrchrnul
76static inline char *gitstrchrnul(const char *s, int c)
77{
78 while (*s && *s != c)
79 s++;
80 return (char *)s;
81}
82#endif
83
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020084static inline void *zalloc(size_t size)
85{
86 return calloc(1, size);
87}
88
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030089#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
90
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030091struct strlist;
92
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020093int mkdir_p(char *path, mode_t mode);
Joe Stringer9a9c7332017-01-26 13:19:59 -080094int rm_rf(const char *path);
Masami Hiramatsue1ce7262016-04-26 18:02:42 +090095struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
96bool lsdir_no_dot_filter(const char *name, struct dirent *d);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020097int copyfile(const char *from, const char *to);
Adrian Hunter9a17d722013-10-14 13:43:41 +030098int copyfile_mode(const char *from, const char *to, mode_t mode);
Namhyung Kim9c9f5a22015-05-18 09:30:18 +090099int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200100
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300101unsigned long convert_unit(unsigned long value, char *unit);
Jiri Olsa727ebd52013-11-28 11:30:14 +0100102ssize_t readn(int fd, void *buf, size_t n);
Jiri Olsabc3a5022013-11-28 11:30:16 +0100103ssize_t writen(int fd, void *buf, size_t n);
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -0300104
Joerg Roedel1aed2672012-01-04 17:54:20 +0100105struct perf_event_attr;
106
107void event_attr_init(struct perf_event_attr *attr);
108
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300109size_t hex_width(u64 v);
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200110int hex2u64(const char *ptr, u64 *val);
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300111
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300112void dump_stack(void);
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300113void sighandler_dump_stack(int sig);
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300114
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300115extern unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -0400116extern int cacheline_size;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -0300117extern int sysctl_perf_event_max_stack;
118extern int sysctl_perf_event_max_contexts_per_stack;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300119
Jiri Olsa27050f52013-09-01 12:36:13 +0200120struct parse_tag {
121 char tag;
122 int mult;
123};
124
125unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
Namhyung Kimf048d542013-09-11 14:09:28 +0900126
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300127static inline int path__join(char *bf, size_t size,
128 const char *path1, const char *path2)
129{
130 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
131}
132
133static inline int path__join3(char *bf, size_t size,
134 const char *path1, const char *path2,
135 const char *path3)
136{
137 return scnprintf(bf, size, "%s%s%s%s%s",
138 path1, path1[0] ? "/" : "",
139 path2, path2[0] ? "/" : "", path3);
140}
141
Adrian Hunter1a472452013-12-11 14:36:23 +0200142int perf_event_paranoid(void);
Dongsheng Yange1a2b172013-12-06 17:25:51 -0500143
Adrian Hunter71db07b2013-12-11 14:36:32 +0200144void mem_bswap_64(void *src, int byte_size);
145void mem_bswap_32(void *src, int byte_size);
146
Dongsheng Yange1a2b172013-12-06 17:25:51 -0500147const char *get_filename_for_perf_kvm(void);
Jiri Olsa63914ac2014-08-01 17:46:54 +0200148bool find_process(const char *name);
Namhyung Kime92ce122014-10-31 16:51:38 +0900149
150#ifdef HAVE_ZLIB_SUPPORT
151int gzip_decompress_to_file(const char *input, int output_fd);
152#endif
153
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100154#ifdef HAVE_LZMA_SUPPORT
155int lzma_decompress_to_file(const char *input, int output_fd);
156#endif
157
Kan Liang076a30c2015-08-06 15:44:52 -0400158int get_stack_size(const char *str, unsigned long *_size);
159
Wang Nan07bc5c62015-11-06 13:55:35 +0000160int fetch_kernel_version(unsigned int *puint,
161 char *str, size_t str_sz);
Wang Nand3e0ce32015-11-06 13:58:09 +0000162#define KVER_VERSION(x) (((x) >> 16) & 0xff)
163#define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
164#define KVER_SUBLEVEL(x) ((x) & 0xff)
165#define KVER_FMT "%d.%d.%d"
166#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
Wang Nan07bc5c62015-11-06 13:55:35 +0000167
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900168const char *perf_tip(const char *dirpath);
Jiri Olsa40356722016-01-20 12:56:32 +0100169bool is_regular_file(const char *file);
Wang Nan37b20152016-01-25 09:56:13 +0000170int fetch_current_timestamp(char *buf, size_t sz);
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900171
Arnaldo Carvalho de Melo120010c2017-03-02 12:55:49 -0300172#ifndef HAVE_SCHED_GETCPU_SUPPORT
173int sched_getcpu(void);
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -0300174#endif
175
Namhyung Kim99620a52016-10-24 11:02:45 +0900176int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
177
Jiri Olsa98081432017-01-09 10:51:55 +0100178int unit_number__scnprintf(char *buf, size_t size, u64 n);
Jin Yaoa64489c2017-03-26 04:34:26 +0800179
Borislav Petkov13559152013-02-20 16:32:31 +0100180#endif /* GIT_COMPAT_UTIL_H */