Skip to content

Commit

Permalink
perf tools: Finalize subcmd independence
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Josh Poimboeuf authored and Arnaldo Carvalho de Melo committed Dec 17, 2015
1 parent 46113a5 commit 2f4ce5e
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 62 deletions.
64 changes: 57 additions & 7 deletions tools/perf/util/exec_cmd.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "cache.h"
#include "exec_cmd.h"
#include "quote.h"
#include "subcmd-config.h"

#include <linux/compiler.h>
#include <linux/string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "subcmd-util.h"
#include "exec_cmd.h"
#include "subcmd-config.h"

#define MAX_ARGS 32
#define PATH_MAX 4096

static const char *argv_exec_path;
static const char *argv0_path;
Expand All @@ -20,6 +25,49 @@ void exec_cmd_init(const char *exec_name, const char *prefix,
subcmd_config.exec_path_env = exec_path_env;
}

#define is_dir_sep(c) ((c) == '/')

static int is_absolute_path(const char *path)
{
return path[0] == '/';
}

static const char *get_pwd_cwd(void)
{
static char cwd[PATH_MAX + 1];
char *pwd;
struct stat cwd_stat, pwd_stat;
if (getcwd(cwd, PATH_MAX) == NULL)
return NULL;
pwd = getenv("PWD");
if (pwd && strcmp(pwd, cwd)) {
stat(cwd, &cwd_stat);
if (!stat(pwd, &pwd_stat) &&
pwd_stat.st_dev == cwd_stat.st_dev &&
pwd_stat.st_ino == cwd_stat.st_ino) {
strlcpy(cwd, pwd, PATH_MAX);
}
}
return cwd;
}

static const char *make_nonrelative_path(const char *path)
{
static char buf[PATH_MAX + 1];

if (is_absolute_path(path)) {
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
die("Too long path: %.*s", 60, path);
} else {
const char *cwd = get_pwd_cwd();
if (!cwd)
die("Cannot determine the current working directory");
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
die("Too long path: %.*s", 60, path);
}
return buf;
}

char *system_path(const char *path)
{
char *buf = NULL;
Expand Down Expand Up @@ -151,8 +199,10 @@ int execl_cmd(const char *cmd,...)
break;
}
va_end(param);
if (MAX_ARGS <= argc)
return error("too many args to run %s", cmd);
if (MAX_ARGS <= argc) {
fprintf(stderr, " Error: too many args to run %s\n", cmd);
return -1;
}

argv[argc] = NULL;
return execv_cmd(argv);
Expand Down
47 changes: 42 additions & 5 deletions tools/perf/util/help.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include "cache.h"
#include "../builtin.h"
#include "exec_cmd.h"
#include "help.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "subcmd-util.h"
#include "help.h"
#include "exec_cmd.h"

void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
{
Expand Down Expand Up @@ -70,6 +77,28 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
cmds->cnt = cj;
}

static void get_term_dimensions(struct winsize *ws)
{
char *s = getenv("LINES");

if (s != NULL) {
ws->ws_row = atoi(s);
s = getenv("COLUMNS");
if (s != NULL) {
ws->ws_col = atoi(s);
if (ws->ws_row && ws->ws_col)
return;
}
}
#ifdef TIOCGWINSZ
if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
ws->ws_row && ws->ws_col)
return;
#endif
ws->ws_row = 25;
ws->ws_col = 80;
}

static void pretty_print_string_list(struct cmdnames *cmds, int longest)
{
int cols = 1, rows;
Expand Down Expand Up @@ -113,6 +142,14 @@ static int is_executable(const char *name)
return st.st_mode & S_IXUSR;
}

static int has_extension(const char *filename, const char *ext)
{
size_t len = strlen(filename);
size_t extlen = strlen(ext);

return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
}

static void list_commands_in_dir(struct cmdnames *cmds,
const char *path,
const char *prefix)
Expand Down Expand Up @@ -168,7 +205,7 @@ void load_command_list(const char *prefix,
char *paths, *path, *colon;
path = paths = strdup(env_path);
while (1) {
if ((colon = strchr(path, PATH_SEP)))
if ((colon = strchr(path, ':')))
*colon = 0;
if (!exec_path || strcmp(path, exec_path))
list_commands_in_dir(other_cmds, path, prefix);
Expand Down
4 changes: 3 additions & 1 deletion tools/perf/util/help.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef __PERF_HELP_H
#define __PERF_HELP_H

#include <sys/types.h>

struct cmdnames {
size_t alloc;
size_t cnt;
struct cmdname {
size_t len; /* also used for similarity index in help.c */
char name[FLEX_ARRAY];
char name[];
} **names;
};

Expand Down
7 changes: 6 additions & 1 deletion tools/perf/util/pager.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "cache.h"
#include <sys/select.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include "pager.h"
#include "run-command.h"
#include "sigchain.h"
#include "subcmd-config.h"
Expand Down
73 changes: 45 additions & 28 deletions tools/perf/util/parse-options.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "util.h"
#include <linux/compiler.h>
#include <linux/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "subcmd-util.h"
#include "parse-options.h"
#include "cache.h"
#include "header.h"
#include "subcmd-config.h"
#include <linux/string.h>
#include "pager.h"

#define OPT_SHORT 1
#define OPT_UNSET 2
Expand All @@ -14,20 +18,29 @@ char *error_buf;
static int opterror(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
return error("switch `%c' %s", opt->short_name, reason);
if (flags & OPT_UNSET)
return error("option `no-%s' %s", opt->long_name, reason);
return error("option `%s' %s", opt->long_name, reason);
fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
else if (flags & OPT_UNSET)
fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
else
fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);

return -1;
}

static const char *skip_prefix(const char *str, const char *prefix)
{
size_t len = strlen(prefix);
return strncmp(str, prefix, len) ? NULL : str + len;
}

static void optwarning(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
warning("switch `%c' %s", opt->short_name, reason);
fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
else if (flags & OPT_UNSET)
warning("option `no-%s' %s", opt->long_name, reason);
fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
else
warning("option `%s' %s", opt->long_name, reason);
fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
}

static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
Expand Down Expand Up @@ -71,11 +84,11 @@ static int get_value(struct parse_opt_ctx_t *p,

if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
p->excl_opt->long_name == NULL) {
scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
p->excl_opt->short_name);
snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
p->excl_opt->short_name);
} else {
scnprintf(msg, sizeof(msg), "cannot be used with %s",
p->excl_opt->long_name);
snprintf(msg, sizeof(msg), "cannot be used with %s",
p->excl_opt->long_name);
}
opterror(opt, msg, flags);
return -3;
Expand Down Expand Up @@ -401,14 +414,16 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
return get_value(p, options, flags);
}

if (ambiguous_option)
return error("Ambiguous option: %s "
"(could be --%s%s or --%s%s)",
arg,
(ambiguous_flags & OPT_UNSET) ? "no-" : "",
ambiguous_option->long_name,
(abbrev_flags & OPT_UNSET) ? "no-" : "",
abbrev_option->long_name);
if (ambiguous_option) {
fprintf(stderr,
" Error: Ambiguous option: %s (could be --%s%s or --%s%s)",
arg,
(ambiguous_flags & OPT_UNSET) ? "no-" : "",
ambiguous_option->long_name,
(abbrev_flags & OPT_UNSET) ? "no-" : "",
abbrev_option->long_name);
return -1;
}
if (abbrev_option)
return get_value(p, abbrev_option, abbrev_flags);
return -2;
Expand All @@ -420,15 +435,15 @@ static void check_typos(const char *arg, const struct option *options)
return;

if (!prefixcmp(arg, "no-")) {
error ("did you mean `--%s` (with two dashes ?)", arg);
fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
exit(129);
}

for (; options->type != OPTION_END; options++) {
if (!options->long_name)
continue;
if (!prefixcmp(options->long_name, arg)) {
error ("did you mean `--%s` (with two dashes ?)", arg);
fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
exit(129);
}
}
Expand Down Expand Up @@ -746,16 +761,18 @@ static int option__cmp(const void *va, const void *vb)

static struct option *options__order(const struct option *opts)
{
int nr_opts = 0;
int nr_opts = 0, len;
const struct option *o = opts;
struct option *ordered;

for (o = opts; o->type != OPTION_END; o++)
++nr_opts;

ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
if (ordered == NULL)
len = sizeof(*o) * (nr_opts + 1);
ordered = malloc(len);
if (!ordered)
goto out;
memcpy(ordered, opts, len);

qsort(ordered, nr_opts, sizeof(*o), option__cmp);
out:
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/parse-options.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef __PERF_PARSE_OPTIONS_H
#define __PERF_PARSE_OPTIONS_H

#include <linux/kernel.h>
#include <stdbool.h>
#include <stdint.h>

enum parse_opt_type {
/* special types */
Expand Down
16 changes: 12 additions & 4 deletions tools/perf/util/run-command.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include "cache.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include "subcmd-util.h"
#include "run-command.h"
#include "exec_cmd.h"
#include "debug.h"

#define STRERR_BUFSIZE 128

static inline void close_pair(int fd[2])
{
Expand Down Expand Up @@ -164,8 +172,8 @@ static int wait_or_whine(pid_t pid)
if (waiting < 0) {
if (errno == EINTR)
continue;
error("waitpid failed (%s)",
strerror_r(errno, sbuf, sizeof(sbuf)));
fprintf(stderr, " Error: waitpid failed (%s)",
strerror_r(errno, sbuf, sizeof(sbuf)));
return -ERR_RUN_COMMAND_WAITPID;
}
if (waiting != pid)
Expand Down
2 changes: 2 additions & 0 deletions tools/perf/util/run-command.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __PERF_RUN_COMMAND_H
#define __PERF_RUN_COMMAND_H

#include <unistd.h>

enum {
ERR_RUN_COMMAND_FORK = 10000,
ERR_RUN_COMMAND_EXEC,
Expand Down
3 changes: 2 additions & 1 deletion tools/perf/util/sigchain.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <signal.h>
#include "subcmd-util.h"
#include "sigchain.h"
#include "cache.h"

#define SIGCHAIN_MAX_SIGNALS 32

Expand Down
Loading

0 comments on commit 2f4ce5e

Please sign in to comment.