diff options
author | Josh Poimboeuf <jpoimboe@redhat.com> | 2015-12-15 10:39:38 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-12-17 12:27:10 -0500 |
commit | 2f4ce5ec1d447beb42143a9653716a2ab025161e (patch) | |
tree | 97c7c5342f217383a7fefc579ad16f8da62bb6a2 /tools/perf | |
parent | 46113a54be53aea50a4f5926b87e86e2e66c4266 (diff) |
perf tools: Finalize subcmd independence
For the files that will be moved to the subcmd library, remove all their
perf-specific includes and duplicate any needed functionality.
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/6e12946f0f26ce4d543d34db68d9dae3c8551cb9.1450193761.git.jpoimboe@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r-- | tools/perf/util/exec_cmd.c | 64 | ||||
-rw-r--r-- | tools/perf/util/help.c | 47 | ||||
-rw-r--r-- | tools/perf/util/help.h | 4 | ||||
-rw-r--r-- | tools/perf/util/pager.c | 7 | ||||
-rw-r--r-- | tools/perf/util/parse-options.c | 73 | ||||
-rw-r--r-- | tools/perf/util/parse-options.h | 2 | ||||
-rw-r--r-- | tools/perf/util/run-command.c | 16 | ||||
-rw-r--r-- | tools/perf/util/run-command.h | 2 | ||||
-rw-r--r-- | tools/perf/util/sigchain.c | 3 | ||||
-rw-r--r-- | tools/perf/util/subcmd-util.h | 67 | ||||
-rw-r--r-- | tools/perf/util/util.h | 14 |
11 files changed, 237 insertions, 62 deletions
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c index 701111ac7699..e7f9ed7943e3 100644 --- a/tools/perf/util/exec_cmd.c +++ b/tools/perf/util/exec_cmd.c | |||
@@ -1,12 +1,17 @@ | |||
1 | #include "cache.h" | 1 | #include <linux/compiler.h> |
2 | #include "exec_cmd.h" | 2 | #include <linux/string.h> |
3 | #include "quote.h" | 3 | #include <sys/types.h> |
4 | #include "subcmd-config.h" | 4 | #include <sys/stat.h> |
5 | 5 | #include <unistd.h> | |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <stdlib.h> | ||
8 | #include <stdio.h> | ||
7 | #include "subcmd-util.h" | 9 | #include "subcmd-util.h" |
10 | #include "exec_cmd.h" | ||
11 | #include "subcmd-config.h" | ||
8 | 12 | ||
9 | #define MAX_ARGS 32 | 13 | #define MAX_ARGS 32 |
14 | #define PATH_MAX 4096 | ||
10 | 15 | ||
11 | static const char *argv_exec_path; | 16 | static const char *argv_exec_path; |
12 | static const char *argv0_path; | 17 | static const char *argv0_path; |
@@ -20,6 +25,49 @@ void exec_cmd_init(const char *exec_name, const char *prefix, | |||
20 | subcmd_config.exec_path_env = exec_path_env; | 25 | subcmd_config.exec_path_env = exec_path_env; |
21 | } | 26 | } |
22 | 27 | ||
28 | #define is_dir_sep(c) ((c) == '/') | ||
29 | |||
30 | static int is_absolute_path(const char *path) | ||
31 | { | ||
32 | return path[0] == '/'; | ||
33 | } | ||
34 | |||
35 | static const char *get_pwd_cwd(void) | ||
36 | { | ||
37 | static char cwd[PATH_MAX + 1]; | ||
38 | char *pwd; | ||
39 | struct stat cwd_stat, pwd_stat; | ||
40 | if (getcwd(cwd, PATH_MAX) == NULL) | ||
41 | return NULL; | ||
42 | pwd = getenv("PWD"); | ||
43 | if (pwd && strcmp(pwd, cwd)) { | ||
44 | stat(cwd, &cwd_stat); | ||
45 | if (!stat(pwd, &pwd_stat) && | ||
46 | pwd_stat.st_dev == cwd_stat.st_dev && | ||
47 | pwd_stat.st_ino == cwd_stat.st_ino) { | ||
48 | strlcpy(cwd, pwd, PATH_MAX); | ||
49 | } | ||
50 | } | ||
51 | return cwd; | ||
52 | } | ||
53 | |||
54 | static const char *make_nonrelative_path(const char *path) | ||
55 | { | ||
56 | static char buf[PATH_MAX + 1]; | ||
57 | |||
58 | if (is_absolute_path(path)) { | ||
59 | if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) | ||
60 | die("Too long path: %.*s", 60, path); | ||
61 | } else { | ||
62 | const char *cwd = get_pwd_cwd(); | ||
63 | if (!cwd) | ||
64 | die("Cannot determine the current working directory"); | ||
65 | if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX) | ||
66 | die("Too long path: %.*s", 60, path); | ||
67 | } | ||
68 | return buf; | ||
69 | } | ||
70 | |||
23 | char *system_path(const char *path) | 71 | char *system_path(const char *path) |
24 | { | 72 | { |
25 | char *buf = NULL; | 73 | char *buf = NULL; |
@@ -151,8 +199,10 @@ int execl_cmd(const char *cmd,...) | |||
151 | break; | 199 | break; |
152 | } | 200 | } |
153 | va_end(param); | 201 | va_end(param); |
154 | if (MAX_ARGS <= argc) | 202 | if (MAX_ARGS <= argc) { |
155 | return error("too many args to run %s", cmd); | 203 | fprintf(stderr, " Error: too many args to run %s\n", cmd); |
204 | return -1; | ||
205 | } | ||
156 | 206 | ||
157 | argv[argc] = NULL; | 207 | argv[argc] = NULL; |
158 | return execv_cmd(argv); | 208 | return execv_cmd(argv); |
diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c index 303a347ee234..8169480066c6 100644 --- a/tools/perf/util/help.c +++ b/tools/perf/util/help.c | |||
@@ -1,8 +1,15 @@ | |||
1 | #include "cache.h" | 1 | #include <stdio.h> |
2 | #include "../builtin.h" | 2 | #include <stdlib.h> |
3 | #include "exec_cmd.h" | 3 | #include <string.h> |
4 | #include "help.h" | 4 | #include <termios.h> |
5 | #include <sys/ioctl.h> | ||
6 | #include <sys/types.h> | ||
7 | #include <sys/stat.h> | ||
8 | #include <unistd.h> | ||
9 | #include <dirent.h> | ||
5 | #include "subcmd-util.h" | 10 | #include "subcmd-util.h" |
11 | #include "help.h" | ||
12 | #include "exec_cmd.h" | ||
6 | 13 | ||
7 | void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) | 14 | void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) |
8 | { | 15 | { |
@@ -70,6 +77,28 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) | |||
70 | cmds->cnt = cj; | 77 | cmds->cnt = cj; |
71 | } | 78 | } |
72 | 79 | ||
80 | static void get_term_dimensions(struct winsize *ws) | ||
81 | { | ||
82 | char *s = getenv("LINES"); | ||
83 | |||
84 | if (s != NULL) { | ||
85 | ws->ws_row = atoi(s); | ||
86 | s = getenv("COLUMNS"); | ||
87 | if (s != NULL) { | ||
88 | ws->ws_col = atoi(s); | ||
89 | if (ws->ws_row && ws->ws_col) | ||
90 | return; | ||
91 | } | ||
92 | } | ||
93 | #ifdef TIOCGWINSZ | ||
94 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && | ||
95 | ws->ws_row && ws->ws_col) | ||
96 | return; | ||
97 | #endif | ||
98 | ws->ws_row = 25; | ||
99 | ws->ws_col = 80; | ||
100 | } | ||
101 | |||
73 | static void pretty_print_string_list(struct cmdnames *cmds, int longest) | 102 | static void pretty_print_string_list(struct cmdnames *cmds, int longest) |
74 | { | 103 | { |
75 | int cols = 1, rows; | 104 | int cols = 1, rows; |
@@ -113,6 +142,14 @@ static int is_executable(const char *name) | |||
113 | return st.st_mode & S_IXUSR; | 142 | return st.st_mode & S_IXUSR; |
114 | } | 143 | } |
115 | 144 | ||
145 | static int has_extension(const char *filename, const char *ext) | ||
146 | { | ||
147 | size_t len = strlen(filename); | ||
148 | size_t extlen = strlen(ext); | ||
149 | |||
150 | return len > extlen && !memcmp(filename + len - extlen, ext, extlen); | ||
151 | } | ||
152 | |||
116 | static void list_commands_in_dir(struct cmdnames *cmds, | 153 | static void list_commands_in_dir(struct cmdnames *cmds, |
117 | const char *path, | 154 | const char *path, |
118 | const char *prefix) | 155 | const char *prefix) |
@@ -168,7 +205,7 @@ void load_command_list(const char *prefix, | |||
168 | char *paths, *path, *colon; | 205 | char *paths, *path, *colon; |
169 | path = paths = strdup(env_path); | 206 | path = paths = strdup(env_path); |
170 | while (1) { | 207 | while (1) { |
171 | if ((colon = strchr(path, PATH_SEP))) | 208 | if ((colon = strchr(path, ':'))) |
172 | *colon = 0; | 209 | *colon = 0; |
173 | if (!exec_path || strcmp(path, exec_path)) | 210 | if (!exec_path || strcmp(path, exec_path)) |
174 | list_commands_in_dir(other_cmds, path, prefix); | 211 | list_commands_in_dir(other_cmds, path, prefix); |
diff --git a/tools/perf/util/help.h b/tools/perf/util/help.h index 14851b0e44f5..096c8bc45cd7 100644 --- a/tools/perf/util/help.h +++ b/tools/perf/util/help.h | |||
@@ -1,12 +1,14 @@ | |||
1 | #ifndef __PERF_HELP_H | 1 | #ifndef __PERF_HELP_H |
2 | #define __PERF_HELP_H | 2 | #define __PERF_HELP_H |
3 | 3 | ||
4 | #include <sys/types.h> | ||
5 | |||
4 | struct cmdnames { | 6 | struct cmdnames { |
5 | size_t alloc; | 7 | size_t alloc; |
6 | size_t cnt; | 8 | size_t cnt; |
7 | struct cmdname { | 9 | struct cmdname { |
8 | size_t len; /* also used for similarity index in help.c */ | 10 | size_t len; /* also used for similarity index in help.c */ |
9 | char name[FLEX_ARRAY]; | 11 | char name[]; |
10 | } **names; | 12 | } **names; |
11 | }; | 13 | }; |
12 | 14 | ||
diff --git a/tools/perf/util/pager.c b/tools/perf/util/pager.c index d5ef62eaa413..d50f3b58606b 100644 --- a/tools/perf/util/pager.c +++ b/tools/perf/util/pager.c | |||
@@ -1,4 +1,9 @@ | |||
1 | #include "cache.h" | 1 | #include <sys/select.h> |
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | #include <string.h> | ||
5 | #include <signal.h> | ||
6 | #include "pager.h" | ||
2 | #include "run-command.h" | 7 | #include "run-command.h" |
3 | #include "sigchain.h" | 8 | #include "sigchain.h" |
4 | #include "subcmd-config.h" | 9 | #include "subcmd-config.h" |
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c index c1da2a53ed4e..981bb4481fd5 100644 --- a/tools/perf/util/parse-options.c +++ b/tools/perf/util/parse-options.c | |||
@@ -1,10 +1,14 @@ | |||
1 | #include "util.h" | 1 | #include <linux/compiler.h> |
2 | #include <linux/types.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <stdint.h> | ||
6 | #include <string.h> | ||
7 | #include <ctype.h> | ||
2 | #include "subcmd-util.h" | 8 | #include "subcmd-util.h" |
3 | #include "parse-options.h" | 9 | #include "parse-options.h" |
4 | #include "cache.h" | ||
5 | #include "header.h" | ||
6 | #include "subcmd-config.h" | 10 | #include "subcmd-config.h" |
7 | #include <linux/string.h> | 11 | #include "pager.h" |
8 | 12 | ||
9 | #define OPT_SHORT 1 | 13 | #define OPT_SHORT 1 |
10 | #define OPT_UNSET 2 | 14 | #define OPT_UNSET 2 |
@@ -14,20 +18,29 @@ char *error_buf; | |||
14 | static int opterror(const struct option *opt, const char *reason, int flags) | 18 | static int opterror(const struct option *opt, const char *reason, int flags) |
15 | { | 19 | { |
16 | if (flags & OPT_SHORT) | 20 | if (flags & OPT_SHORT) |
17 | return error("switch `%c' %s", opt->short_name, reason); | 21 | fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason); |
18 | if (flags & OPT_UNSET) | 22 | else if (flags & OPT_UNSET) |
19 | return error("option `no-%s' %s", opt->long_name, reason); | 23 | fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason); |
20 | return error("option `%s' %s", opt->long_name, reason); | 24 | else |
25 | fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason); | ||
26 | |||
27 | return -1; | ||
28 | } | ||
29 | |||
30 | static const char *skip_prefix(const char *str, const char *prefix) | ||
31 | { | ||
32 | size_t len = strlen(prefix); | ||
33 | return strncmp(str, prefix, len) ? NULL : str + len; | ||
21 | } | 34 | } |
22 | 35 | ||
23 | static void optwarning(const struct option *opt, const char *reason, int flags) | 36 | static void optwarning(const struct option *opt, const char *reason, int flags) |
24 | { | 37 | { |
25 | if (flags & OPT_SHORT) | 38 | if (flags & OPT_SHORT) |
26 | warning("switch `%c' %s", opt->short_name, reason); | 39 | fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason); |
27 | else if (flags & OPT_UNSET) | 40 | else if (flags & OPT_UNSET) |
28 | warning("option `no-%s' %s", opt->long_name, reason); | 41 | fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason); |
29 | else | 42 | else |
30 | warning("option `%s' %s", opt->long_name, reason); | 43 | fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason); |
31 | } | 44 | } |
32 | 45 | ||
33 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, | 46 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
@@ -71,11 +84,11 @@ static int get_value(struct parse_opt_ctx_t *p, | |||
71 | 84 | ||
72 | if (((flags & OPT_SHORT) && p->excl_opt->short_name) || | 85 | if (((flags & OPT_SHORT) && p->excl_opt->short_name) || |
73 | p->excl_opt->long_name == NULL) { | 86 | p->excl_opt->long_name == NULL) { |
74 | scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'", | 87 | snprintf(msg, sizeof(msg), "cannot be used with switch `%c'", |
75 | p->excl_opt->short_name); | 88 | p->excl_opt->short_name); |
76 | } else { | 89 | } else { |
77 | scnprintf(msg, sizeof(msg), "cannot be used with %s", | 90 | snprintf(msg, sizeof(msg), "cannot be used with %s", |
78 | p->excl_opt->long_name); | 91 | p->excl_opt->long_name); |
79 | } | 92 | } |
80 | opterror(opt, msg, flags); | 93 | opterror(opt, msg, flags); |
81 | return -3; | 94 | return -3; |
@@ -401,14 +414,16 @@ match: | |||
401 | return get_value(p, options, flags); | 414 | return get_value(p, options, flags); |
402 | } | 415 | } |
403 | 416 | ||
404 | if (ambiguous_option) | 417 | if (ambiguous_option) { |
405 | return error("Ambiguous option: %s " | 418 | fprintf(stderr, |
406 | "(could be --%s%s or --%s%s)", | 419 | " Error: Ambiguous option: %s (could be --%s%s or --%s%s)", |
407 | arg, | 420 | arg, |
408 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", | 421 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
409 | ambiguous_option->long_name, | 422 | ambiguous_option->long_name, |
410 | (abbrev_flags & OPT_UNSET) ? "no-" : "", | 423 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
411 | abbrev_option->long_name); | 424 | abbrev_option->long_name); |
425 | return -1; | ||
426 | } | ||
412 | if (abbrev_option) | 427 | if (abbrev_option) |
413 | return get_value(p, abbrev_option, abbrev_flags); | 428 | return get_value(p, abbrev_option, abbrev_flags); |
414 | return -2; | 429 | return -2; |
@@ -420,7 +435,7 @@ static void check_typos(const char *arg, const struct option *options) | |||
420 | return; | 435 | return; |
421 | 436 | ||
422 | if (!prefixcmp(arg, "no-")) { | 437 | if (!prefixcmp(arg, "no-")) { |
423 | error ("did you mean `--%s` (with two dashes ?)", arg); | 438 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); |
424 | exit(129); | 439 | exit(129); |
425 | } | 440 | } |
426 | 441 | ||
@@ -428,7 +443,7 @@ static void check_typos(const char *arg, const struct option *options) | |||
428 | if (!options->long_name) | 443 | if (!options->long_name) |
429 | continue; | 444 | continue; |
430 | if (!prefixcmp(options->long_name, arg)) { | 445 | if (!prefixcmp(options->long_name, arg)) { |
431 | error ("did you mean `--%s` (with two dashes ?)", arg); | 446 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); |
432 | exit(129); | 447 | exit(129); |
433 | } | 448 | } |
434 | } | 449 | } |
@@ -746,16 +761,18 @@ static int option__cmp(const void *va, const void *vb) | |||
746 | 761 | ||
747 | static struct option *options__order(const struct option *opts) | 762 | static struct option *options__order(const struct option *opts) |
748 | { | 763 | { |
749 | int nr_opts = 0; | 764 | int nr_opts = 0, len; |
750 | const struct option *o = opts; | 765 | const struct option *o = opts; |
751 | struct option *ordered; | 766 | struct option *ordered; |
752 | 767 | ||
753 | for (o = opts; o->type != OPTION_END; o++) | 768 | for (o = opts; o->type != OPTION_END; o++) |
754 | ++nr_opts; | 769 | ++nr_opts; |
755 | 770 | ||
756 | ordered = memdup(opts, sizeof(*o) * (nr_opts + 1)); | 771 | len = sizeof(*o) * (nr_opts + 1); |
757 | if (ordered == NULL) | 772 | ordered = malloc(len); |
773 | if (!ordered) | ||
758 | goto out; | 774 | goto out; |
775 | memcpy(ordered, opts, len); | ||
759 | 776 | ||
760 | qsort(ordered, nr_opts, sizeof(*o), option__cmp); | 777 | qsort(ordered, nr_opts, sizeof(*o), option__cmp); |
761 | out: | 778 | out: |
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h index d1544069c7c0..dec893f10477 100644 --- a/tools/perf/util/parse-options.h +++ b/tools/perf/util/parse-options.h | |||
@@ -1,8 +1,8 @@ | |||
1 | #ifndef __PERF_PARSE_OPTIONS_H | 1 | #ifndef __PERF_PARSE_OPTIONS_H |
2 | #define __PERF_PARSE_OPTIONS_H | 2 | #define __PERF_PARSE_OPTIONS_H |
3 | 3 | ||
4 | #include <linux/kernel.h> | ||
5 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | #include <stdint.h> | ||
6 | 6 | ||
7 | enum parse_opt_type { | 7 | enum parse_opt_type { |
8 | /* special types */ | 8 | /* special types */ |
diff --git a/tools/perf/util/run-command.c b/tools/perf/util/run-command.c index 910c0f6479f4..fed37d6ae070 100644 --- a/tools/perf/util/run-command.c +++ b/tools/perf/util/run-command.c | |||
@@ -1,7 +1,15 @@ | |||
1 | #include "cache.h" | 1 | #include <unistd.h> |
2 | #include <sys/types.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <string.h> | ||
6 | #include <errno.h> | ||
7 | #include <sys/wait.h> | ||
8 | #include "subcmd-util.h" | ||
2 | #include "run-command.h" | 9 | #include "run-command.h" |
3 | #include "exec_cmd.h" | 10 | #include "exec_cmd.h" |
4 | #include "debug.h" | 11 | |
12 | #define STRERR_BUFSIZE 128 | ||
5 | 13 | ||
6 | static inline void close_pair(int fd[2]) | 14 | static inline void close_pair(int fd[2]) |
7 | { | 15 | { |
@@ -164,8 +172,8 @@ static int wait_or_whine(pid_t pid) | |||
164 | if (waiting < 0) { | 172 | if (waiting < 0) { |
165 | if (errno == EINTR) | 173 | if (errno == EINTR) |
166 | continue; | 174 | continue; |
167 | error("waitpid failed (%s)", | 175 | fprintf(stderr, " Error: waitpid failed (%s)", |
168 | strerror_r(errno, sbuf, sizeof(sbuf))); | 176 | strerror_r(errno, sbuf, sizeof(sbuf))); |
169 | return -ERR_RUN_COMMAND_WAITPID; | 177 | return -ERR_RUN_COMMAND_WAITPID; |
170 | } | 178 | } |
171 | if (waiting != pid) | 179 | if (waiting != pid) |
diff --git a/tools/perf/util/run-command.h b/tools/perf/util/run-command.h index cf7d655ee2a3..4a55393a6547 100644 --- a/tools/perf/util/run-command.h +++ b/tools/perf/util/run-command.h | |||
@@ -1,6 +1,8 @@ | |||
1 | #ifndef __PERF_RUN_COMMAND_H | 1 | #ifndef __PERF_RUN_COMMAND_H |
2 | #define __PERF_RUN_COMMAND_H | 2 | #define __PERF_RUN_COMMAND_H |
3 | 3 | ||
4 | #include <unistd.h> | ||
5 | |||
4 | enum { | 6 | enum { |
5 | ERR_RUN_COMMAND_FORK = 10000, | 7 | ERR_RUN_COMMAND_FORK = 10000, |
6 | ERR_RUN_COMMAND_EXEC, | 8 | ERR_RUN_COMMAND_EXEC, |
diff --git a/tools/perf/util/sigchain.c b/tools/perf/util/sigchain.c index ba785e9b1841..3537c348a18e 100644 --- a/tools/perf/util/sigchain.c +++ b/tools/perf/util/sigchain.c | |||
@@ -1,5 +1,6 @@ | |||
1 | #include <signal.h> | ||
2 | #include "subcmd-util.h" | ||
1 | #include "sigchain.h" | 3 | #include "sigchain.h" |
2 | #include "cache.h" | ||
3 | 4 | ||
4 | #define SIGCHAIN_MAX_SIGNALS 32 | 5 | #define SIGCHAIN_MAX_SIGNALS 32 |
5 | 6 | ||
diff --git a/tools/perf/util/subcmd-util.h b/tools/perf/util/subcmd-util.h index 98fb9f9270eb..321aeb11a381 100644 --- a/tools/perf/util/subcmd-util.h +++ b/tools/perf/util/subcmd-util.h | |||
@@ -1,8 +1,66 @@ | |||
1 | #ifndef __PERF_SUBCMD_UTIL_H | 1 | #ifndef __PERF_SUBCMD_UTIL_H |
2 | #define __PERF_SUBCMD_UTIL_H | 2 | #define __PERF_SUBCMD_UTIL_H |
3 | 3 | ||
4 | #include <stdarg.h> | ||
5 | #include <stdlib.h> | ||
4 | #include <stdio.h> | 6 | #include <stdio.h> |
5 | 7 | ||
8 | #define NORETURN __attribute__((__noreturn__)) | ||
9 | |||
10 | static inline void report(const char *prefix, const char *err, va_list params) | ||
11 | { | ||
12 | char msg[1024]; | ||
13 | vsnprintf(msg, sizeof(msg), err, params); | ||
14 | fprintf(stderr, " %s%s\n", prefix, msg); | ||
15 | } | ||
16 | |||
17 | static NORETURN inline void die(const char *err, ...) | ||
18 | { | ||
19 | va_list params; | ||
20 | |||
21 | va_start(params, err); | ||
22 | report(" Fatal: ", err, params); | ||
23 | exit(128); | ||
24 | va_end(params); | ||
25 | } | ||
26 | |||
27 | #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) | ||
28 | |||
29 | #define alloc_nr(x) (((x)+16)*3/2) | ||
30 | |||
31 | /* | ||
32 | * Realloc the buffer pointed at by variable 'x' so that it can hold | ||
33 | * at least 'nr' entries; the number of entries currently allocated | ||
34 | * is 'alloc', using the standard growing factor alloc_nr() macro. | ||
35 | * | ||
36 | * DO NOT USE any expression with side-effect for 'x' or 'alloc'. | ||
37 | */ | ||
38 | #define ALLOC_GROW(x, nr, alloc) \ | ||
39 | do { \ | ||
40 | if ((nr) > alloc) { \ | ||
41 | if (alloc_nr(alloc) < (nr)) \ | ||
42 | alloc = (nr); \ | ||
43 | else \ | ||
44 | alloc = alloc_nr(alloc); \ | ||
45 | x = xrealloc((x), alloc * sizeof(*(x))); \ | ||
46 | } \ | ||
47 | } while(0) | ||
48 | |||
49 | static inline void *xrealloc(void *ptr, size_t size) | ||
50 | { | ||
51 | void *ret = realloc(ptr, size); | ||
52 | if (!ret && !size) | ||
53 | ret = realloc(ptr, 1); | ||
54 | if (!ret) { | ||
55 | ret = realloc(ptr, size); | ||
56 | if (!ret && !size) | ||
57 | ret = realloc(ptr, 1); | ||
58 | if (!ret) | ||
59 | die("Out of memory, realloc failed"); | ||
60 | } | ||
61 | return ret; | ||
62 | } | ||
63 | |||
6 | #define astrcatf(out, fmt, ...) \ | 64 | #define astrcatf(out, fmt, ...) \ |
7 | ({ \ | 65 | ({ \ |
8 | char *tmp = *(out); \ | 66 | char *tmp = *(out); \ |
@@ -21,4 +79,13 @@ static inline void astrcat(char **out, const char *add) | |||
21 | free(tmp); | 79 | free(tmp); |
22 | } | 80 | } |
23 | 81 | ||
82 | static inline int prefixcmp(const char *str, const char *prefix) | ||
83 | { | ||
84 | for (; ; str++, prefix++) | ||
85 | if (!*prefix) | ||
86 | return 0; | ||
87 | else if (*str != *prefix) | ||
88 | return (unsigned char)*prefix - (unsigned char)*str; | ||
89 | } | ||
90 | |||
24 | #endif /* __PERF_SUBCMD_UTIL_H */ | 91 | #endif /* __PERF_SUBCMD_UTIL_H */ |
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 150858f3b4f0..4b519c59bdc3 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
@@ -151,12 +151,6 @@ extern void set_warning_routine(void (*routine)(const char *err, va_list params) | |||
151 | extern int prefixcmp(const char *str, const char *prefix); | 151 | extern int prefixcmp(const char *str, const char *prefix); |
152 | extern void set_buildid_dir(const char *dir); | 152 | extern void set_buildid_dir(const char *dir); |
153 | 153 | ||
154 | static inline const char *skip_prefix(const char *str, const char *prefix) | ||
155 | { | ||
156 | size_t len = strlen(prefix); | ||
157 | return strncmp(str, prefix, len) ? NULL : str + len; | ||
158 | } | ||
159 | |||
160 | #ifdef __GLIBC_PREREQ | 154 | #ifdef __GLIBC_PREREQ |
161 | #if __GLIBC_PREREQ(2, 1) | 155 | #if __GLIBC_PREREQ(2, 1) |
162 | #define HAVE_STRCHRNUL | 156 | #define HAVE_STRCHRNUL |
@@ -187,14 +181,6 @@ static inline void *zalloc(size_t size) | |||
187 | 181 | ||
188 | #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) | 182 | #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
189 | 183 | ||
190 | static inline int has_extension(const char *filename, const char *ext) | ||
191 | { | ||
192 | size_t len = strlen(filename); | ||
193 | size_t extlen = strlen(ext); | ||
194 | |||
195 | return len > extlen && !memcmp(filename + len - extlen, ext, extlen); | ||
196 | } | ||
197 | |||
198 | /* Sane ctype - no locale, and works with signed chars */ | 184 | /* Sane ctype - no locale, and works with signed chars */ |
199 | #undef isascii | 185 | #undef isascii |
200 | #undef isspace | 186 | #undef isspace |