Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Josh Poimboeuf and committed by
Arnaldo Carvalho de Melo
2f4ce5ec 46113a54

+237 -62
+57 -7
tools/perf/util/exec_cmd.c
··· 1 - #include "cache.h" 1 + #include <linux/compiler.h> 2 + #include <linux/string.h> 3 + #include <sys/types.h> 4 + #include <sys/stat.h> 5 + #include <unistd.h> 6 + #include <string.h> 7 + #include <stdlib.h> 8 + #include <stdio.h> 9 + #include "subcmd-util.h" 2 10 #include "exec_cmd.h" 3 - #include "quote.h" 4 11 #include "subcmd-config.h" 5 12 6 - #include <string.h> 7 - #include "subcmd-util.h" 8 - 9 13 #define MAX_ARGS 32 14 + #define PATH_MAX 4096 10 15 11 16 static const char *argv_exec_path; 12 17 static const char *argv0_path; ··· 23 18 subcmd_config.prefix = prefix; 24 19 subcmd_config.exec_path = exec_path; 25 20 subcmd_config.exec_path_env = exec_path_env; 21 + } 22 + 23 + #define is_dir_sep(c) ((c) == '/') 24 + 25 + static int is_absolute_path(const char *path) 26 + { 27 + return path[0] == '/'; 28 + } 29 + 30 + static const char *get_pwd_cwd(void) 31 + { 32 + static char cwd[PATH_MAX + 1]; 33 + char *pwd; 34 + struct stat cwd_stat, pwd_stat; 35 + if (getcwd(cwd, PATH_MAX) == NULL) 36 + return NULL; 37 + pwd = getenv("PWD"); 38 + if (pwd && strcmp(pwd, cwd)) { 39 + stat(cwd, &cwd_stat); 40 + if (!stat(pwd, &pwd_stat) && 41 + pwd_stat.st_dev == cwd_stat.st_dev && 42 + pwd_stat.st_ino == cwd_stat.st_ino) { 43 + strlcpy(cwd, pwd, PATH_MAX); 44 + } 45 + } 46 + return cwd; 47 + } 48 + 49 + static const char *make_nonrelative_path(const char *path) 50 + { 51 + static char buf[PATH_MAX + 1]; 52 + 53 + if (is_absolute_path(path)) { 54 + if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 55 + die("Too long path: %.*s", 60, path); 56 + } else { 57 + const char *cwd = get_pwd_cwd(); 58 + if (!cwd) 59 + die("Cannot determine the current working directory"); 60 + if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX) 61 + die("Too long path: %.*s", 60, path); 62 + } 63 + return buf; 26 64 } 27 65 28 66 char *system_path(const char *path) ··· 199 151 break; 200 152 } 201 153 va_end(param); 202 - if (MAX_ARGS <= argc) 203 - return error("too many args to run %s", cmd); 154 + if (MAX_ARGS <= argc) { 155 + fprintf(stderr, " Error: too many args to run %s\n", cmd); 156 + return -1; 157 + } 204 158 205 159 argv[argc] = NULL; 206 160 return execv_cmd(argv);
+42 -5
tools/perf/util/help.c
··· 1 - #include "cache.h" 2 - #include "../builtin.h" 3 - #include "exec_cmd.h" 4 - #include "help.h" 1 + #include <stdio.h> 2 + #include <stdlib.h> 3 + #include <string.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 10 #include "subcmd-util.h" 11 + #include "help.h" 12 + #include "exec_cmd.h" 6 13 7 14 void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) 8 15 { ··· 77 70 cmds->cnt = cj; 78 71 } 79 72 73 + static void get_term_dimensions(struct winsize *ws) 74 + { 75 + char *s = getenv("LINES"); 76 + 77 + if (s != NULL) { 78 + ws->ws_row = atoi(s); 79 + s = getenv("COLUMNS"); 80 + if (s != NULL) { 81 + ws->ws_col = atoi(s); 82 + if (ws->ws_row && ws->ws_col) 83 + return; 84 + } 85 + } 86 + #ifdef TIOCGWINSZ 87 + if (ioctl(1, TIOCGWINSZ, ws) == 0 && 88 + ws->ws_row && ws->ws_col) 89 + return; 90 + #endif 91 + ws->ws_row = 25; 92 + ws->ws_col = 80; 93 + } 94 + 80 95 static void pretty_print_string_list(struct cmdnames *cmds, int longest) 81 96 { 82 97 int cols = 1, rows; ··· 140 111 return 0; 141 112 142 113 return st.st_mode & S_IXUSR; 114 + } 115 + 116 + static int has_extension(const char *filename, const char *ext) 117 + { 118 + size_t len = strlen(filename); 119 + size_t extlen = strlen(ext); 120 + 121 + return len > extlen && !memcmp(filename + len - extlen, ext, extlen); 143 122 } 144 123 145 124 static void list_commands_in_dir(struct cmdnames *cmds, ··· 205 168 char *paths, *path, *colon; 206 169 path = paths = strdup(env_path); 207 170 while (1) { 208 - if ((colon = strchr(path, PATH_SEP))) 171 + if ((colon = strchr(path, ':'))) 209 172 *colon = 0; 210 173 if (!exec_path || strcmp(path, exec_path)) 211 174 list_commands_in_dir(other_cmds, path, prefix);
+3 -1
tools/perf/util/help.h
··· 1 1 #ifndef __PERF_HELP_H 2 2 #define __PERF_HELP_H 3 3 4 + #include <sys/types.h> 5 + 4 6 struct cmdnames { 5 7 size_t alloc; 6 8 size_t cnt; 7 9 struct cmdname { 8 10 size_t len; /* also used for similarity index in help.c */ 9 - char name[FLEX_ARRAY]; 11 + char name[]; 10 12 } **names; 11 13 }; 12 14
+6 -1
tools/perf/util/pager.c
··· 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 7 #include "run-command.h" 3 8 #include "sigchain.h" 4 9 #include "subcmd-config.h"
+45 -28
tools/perf/util/parse-options.c
··· 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 8 #include "subcmd-util.h" 3 9 #include "parse-options.h" 4 - #include "cache.h" 5 - #include "header.h" 6 10 #include "subcmd-config.h" 7 - #include <linux/string.h> 11 + #include "pager.h" 8 12 9 13 #define OPT_SHORT 1 10 14 #define OPT_UNSET 2 ··· 18 14 static int opterror(const struct option *opt, const char *reason, int flags) 19 15 { 20 16 if (flags & OPT_SHORT) 21 - return error("switch `%c' %s", opt->short_name, reason); 22 - if (flags & OPT_UNSET) 23 - return error("option `no-%s' %s", opt->long_name, reason); 24 - return error("option `%s' %s", opt->long_name, reason); 17 + fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason); 18 + else if (flags & OPT_UNSET) 19 + fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason); 20 + else 21 + fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason); 22 + 23 + return -1; 24 + } 25 + 26 + static const char *skip_prefix(const char *str, const char *prefix) 27 + { 28 + size_t len = strlen(prefix); 29 + return strncmp(str, prefix, len) ? NULL : str + len; 25 30 } 26 31 27 32 static void optwarning(const struct option *opt, const char *reason, int flags) 28 33 { 29 34 if (flags & OPT_SHORT) 30 - warning("switch `%c' %s", opt->short_name, reason); 35 + fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason); 31 36 else if (flags & OPT_UNSET) 32 - warning("option `no-%s' %s", opt->long_name, reason); 37 + fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason); 33 38 else 34 - warning("option `%s' %s", opt->long_name, reason); 39 + fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason); 35 40 } 36 41 37 42 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, ··· 84 71 85 72 if (((flags & OPT_SHORT) && p->excl_opt->short_name) || 86 73 p->excl_opt->long_name == NULL) { 87 - scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'", 88 - p->excl_opt->short_name); 74 + snprintf(msg, sizeof(msg), "cannot be used with switch `%c'", 75 + p->excl_opt->short_name); 89 76 } else { 90 - scnprintf(msg, sizeof(msg), "cannot be used with %s", 91 - p->excl_opt->long_name); 77 + snprintf(msg, sizeof(msg), "cannot be used with %s", 78 + p->excl_opt->long_name); 92 79 } 93 80 opterror(opt, msg, flags); 94 81 return -3; ··· 414 401 return get_value(p, options, flags); 415 402 } 416 403 417 - if (ambiguous_option) 418 - return error("Ambiguous option: %s " 419 - "(could be --%s%s or --%s%s)", 420 - arg, 421 - (ambiguous_flags & OPT_UNSET) ? "no-" : "", 422 - ambiguous_option->long_name, 423 - (abbrev_flags & OPT_UNSET) ? "no-" : "", 424 - abbrev_option->long_name); 404 + if (ambiguous_option) { 405 + fprintf(stderr, 406 + " Error: Ambiguous option: %s (could be --%s%s or --%s%s)", 407 + arg, 408 + (ambiguous_flags & OPT_UNSET) ? "no-" : "", 409 + ambiguous_option->long_name, 410 + (abbrev_flags & OPT_UNSET) ? "no-" : "", 411 + abbrev_option->long_name); 412 + return -1; 413 + } 425 414 if (abbrev_option) 426 415 return get_value(p, abbrev_option, abbrev_flags); 427 416 return -2; ··· 435 420 return; 436 421 437 422 if (!prefixcmp(arg, "no-")) { 438 - error ("did you mean `--%s` (with two dashes ?)", arg); 423 + fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); 439 424 exit(129); 440 425 } 441 426 ··· 443 428 if (!options->long_name) 444 429 continue; 445 430 if (!prefixcmp(options->long_name, arg)) { 446 - error ("did you mean `--%s` (with two dashes ?)", arg); 431 + fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); 447 432 exit(129); 448 433 } 449 434 } ··· 761 746 762 747 static struct option *options__order(const struct option *opts) 763 748 { 764 - int nr_opts = 0; 749 + int nr_opts = 0, len; 765 750 const struct option *o = opts; 766 751 struct option *ordered; 767 752 768 753 for (o = opts; o->type != OPTION_END; o++) 769 754 ++nr_opts; 770 755 771 - ordered = memdup(opts, sizeof(*o) * (nr_opts + 1)); 772 - if (ordered == NULL) 756 + len = sizeof(*o) * (nr_opts + 1); 757 + ordered = malloc(len); 758 + if (!ordered) 773 759 goto out; 760 + memcpy(ordered, opts, len); 774 761 775 762 qsort(ordered, nr_opts, sizeof(*o), option__cmp); 776 763 out:
+1 -1
tools/perf/util/parse-options.h
··· 1 1 #ifndef __PERF_PARSE_OPTIONS_H 2 2 #define __PERF_PARSE_OPTIONS_H 3 3 4 - #include <linux/kernel.h> 5 4 #include <stdbool.h> 5 + #include <stdint.h> 6 6 7 7 enum parse_opt_type { 8 8 /* special types */
+12 -4
tools/perf/util/run-command.c
··· 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 9 #include "run-command.h" 3 10 #include "exec_cmd.h" 4 - #include "debug.h" 11 + 12 + #define STRERR_BUFSIZE 128 5 13 6 14 static inline void close_pair(int fd[2]) 7 15 { ··· 172 164 if (waiting < 0) { 173 165 if (errno == EINTR) 174 166 continue; 175 - error("waitpid failed (%s)", 176 - strerror_r(errno, sbuf, sizeof(sbuf))); 167 + fprintf(stderr, " Error: waitpid failed (%s)", 168 + strerror_r(errno, sbuf, sizeof(sbuf))); 177 169 return -ERR_RUN_COMMAND_WAITPID; 178 170 } 179 171 if (waiting != pid)
+2
tools/perf/util/run-command.h
··· 1 1 #ifndef __PERF_RUN_COMMAND_H 2 2 #define __PERF_RUN_COMMAND_H 3 3 4 + #include <unistd.h> 5 + 4 6 enum { 5 7 ERR_RUN_COMMAND_FORK = 10000, 6 8 ERR_RUN_COMMAND_EXEC,
+2 -1
tools/perf/util/sigchain.c
··· 1 + #include <signal.h> 2 + #include "subcmd-util.h" 1 3 #include "sigchain.h" 2 - #include "cache.h" 3 4 4 5 #define SIGCHAIN_MAX_SIGNALS 32 5 6
+67
tools/perf/util/subcmd-util.h
··· 1 1 #ifndef __PERF_SUBCMD_UTIL_H 2 2 #define __PERF_SUBCMD_UTIL_H 3 3 4 + #include <stdarg.h> 5 + #include <stdlib.h> 4 6 #include <stdio.h> 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 + } 5 63 6 64 #define astrcatf(out, fmt, ...) \ 7 65 ({ \ ··· 77 19 die("asprintf failed"); 78 20 79 21 free(tmp); 22 + } 23 + 24 + static inline int prefixcmp(const char *str, const char *prefix) 25 + { 26 + for (; ; str++, prefix++) 27 + if (!*prefix) 28 + return 0; 29 + else if (*str != *prefix) 30 + return (unsigned char)*prefix - (unsigned char)*str; 80 31 } 81 32 82 33 #endif /* __PERF_SUBCMD_UTIL_H */
-14
tools/perf/util/util.h
··· 151 151 extern int prefixcmp(const char *str, const char *prefix); 152 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 154 #ifdef __GLIBC_PREREQ 161 155 #if __GLIBC_PREREQ(2, 1) 162 156 #define HAVE_STRCHRNUL ··· 180 186 } 181 187 182 188 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) 183 - 184 - static inline int has_extension(const char *filename, const char *ext) 185 - { 186 - size_t len = strlen(filename); 187 - size_t extlen = strlen(ext); 188 - 189 - return len > extlen && !memcmp(filename + len - extlen, ext, extlen); 190 - } 191 189 192 190 /* Sane ctype - no locale, and works with signed chars */ 193 191 #undef isascii