Skip to content

Commit 901421a

Browse files
jpoimboeacmel
authored andcommitted
perf tools: Remove subcmd dependencies on strbuf
Introduce and use new astrcat() and astrcatf() functions which replace the strbuf functionality for subcmd. For now they duplicate strbuf's die-on-allocation-error policy. Signed-off-by: Josh Poimboeuf <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/957d207e1254406fa11fc2e405e75a7e405aad8f.1450193761.git.jpoimboe@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 096d355 commit 901421a

File tree

4 files changed

+66
-41
lines changed

4 files changed

+66
-41
lines changed

tools/perf/util/exec_cmd.c

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "subcmd-config.h"
55

66
#include <string.h>
7+
#include "subcmd-util.h"
78

89
#define MAX_ARGS 32
910

@@ -21,14 +22,14 @@ void exec_cmd_init(const char *exec_name, const char *prefix,
2122

2223
char *system_path(const char *path)
2324
{
24-
struct strbuf d = STRBUF_INIT;
25+
char *buf = NULL;
2526

2627
if (is_absolute_path(path))
2728
return strdup(path);
2829

29-
strbuf_addf(&d, "%s/%s", subcmd_config.prefix, path);
30-
path = strbuf_detach(&d, NULL);
31-
return (char *)path;
30+
astrcatf(&buf, "%s/%s", subcmd_config.prefix, path);
31+
32+
return buf;
3233
}
3334

3435
const char *perf_extract_argv0_path(const char *argv0)
@@ -75,36 +76,36 @@ char *perf_exec_path(void)
7576
return system_path(subcmd_config.exec_path);
7677
}
7778

78-
static void add_path(struct strbuf *out, const char *path)
79+
static void add_path(char **out, const char *path)
7980
{
8081
if (path && *path) {
8182
if (is_absolute_path(path))
82-
strbuf_addstr(out, path);
83+
astrcat(out, path);
8384
else
84-
strbuf_addstr(out, make_nonrelative_path(path));
85+
astrcat(out, make_nonrelative_path(path));
8586

86-
strbuf_addch(out, PATH_SEP);
87+
astrcat(out, ":");
8788
}
8889
}
8990

9091
void setup_path(void)
9192
{
9293
const char *old_path = getenv("PATH");
93-
struct strbuf new_path = STRBUF_INIT;
94+
char *new_path = NULL;
9495
char *tmp = perf_exec_path();
9596

9697
add_path(&new_path, tmp);
9798
add_path(&new_path, argv0_path);
9899
free(tmp);
99100

100101
if (old_path)
101-
strbuf_addstr(&new_path, old_path);
102+
astrcat(&new_path, old_path);
102103
else
103-
strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
104+
astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin");
104105

105-
setenv("PATH", new_path.buf, 1);
106+
setenv("PATH", new_path, 1);
106107

107-
strbuf_release(&new_path);
108+
free(new_path);
108109
}
109110

110111
static const char **prepare_perf_cmd(const char **argv)

tools/perf/util/help.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../builtin.h"
33
#include "exec_cmd.h"
44
#include "help.h"
5+
#include "subcmd-util.h"
56

67
void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
78
{
@@ -119,27 +120,24 @@ static void list_commands_in_dir(struct cmdnames *cmds,
119120
int prefix_len;
120121
DIR *dir = opendir(path);
121122
struct dirent *de;
122-
struct strbuf buf = STRBUF_INIT;
123-
int len;
123+
char *buf = NULL;
124124

125125
if (!dir)
126126
return;
127127
if (!prefix)
128128
prefix = "perf-";
129129
prefix_len = strlen(prefix);
130130

131-
strbuf_addf(&buf, "%s/", path);
132-
len = buf.len;
131+
astrcatf(&buf, "%s/", path);
133132

134133
while ((de = readdir(dir)) != NULL) {
135134
int entlen;
136135

137136
if (prefixcmp(de->d_name, prefix))
138137
continue;
139138

140-
strbuf_setlen(&buf, len);
141-
strbuf_addstr(&buf, de->d_name);
142-
if (!is_executable(buf.buf))
139+
astrcat(&buf, de->d_name);
140+
if (!is_executable(buf))
143141
continue;
144142

145143
entlen = strlen(de->d_name) - prefix_len;
@@ -149,7 +147,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
149147
add_cmdname(cmds, de->d_name + prefix_len, entlen);
150148
}
151149
closedir(dir);
152-
strbuf_release(&buf);
150+
free(buf);
153151
}
154152

155153
void load_command_list(const char *prefix,

tools/perf/util/parse-options.c

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "util.h"
2+
#include "subcmd-util.h"
23
#include "parse-options.h"
34
#include "cache.h"
45
#include "header.h"
@@ -8,7 +9,7 @@
89
#define OPT_SHORT 1
910
#define OPT_UNSET 2
1011

11-
static struct strbuf error_buf = STRBUF_INIT;
12+
char *error_buf;
1213

1314
static int opterror(const struct option *opt, const char *reason, int flags)
1415
{
@@ -576,19 +577,18 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
576577

577578
/* build usage string if it's not provided */
578579
if (subcommands && !usagestr[0]) {
579-
struct strbuf buf = STRBUF_INIT;
580+
char *buf = NULL;
581+
582+
astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
580583

581-
strbuf_addf(&buf, "%s %s [<options>] {",
582-
subcmd_config.exec_name, argv[0]);
583584
for (int i = 0; subcommands[i]; i++) {
584585
if (i)
585-
strbuf_addstr(&buf, "|");
586-
strbuf_addstr(&buf, subcommands[i]);
586+
astrcat(&buf, "|");
587+
astrcat(&buf, subcommands[i]);
587588
}
588-
strbuf_addstr(&buf, "}");
589+
astrcat(&buf, "}");
589590

590-
usagestr[0] = strdup(buf.buf);
591-
strbuf_release(&buf);
591+
usagestr[0] = buf;
592592
}
593593

594594
parse_options_start(&ctx, argc, argv, flags);
@@ -613,13 +613,11 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
613613
putchar('\n');
614614
exit(130);
615615
default: /* PARSE_OPT_UNKNOWN */
616-
if (ctx.argv[0][1] == '-') {
617-
strbuf_addf(&error_buf, "unknown option `%s'",
618-
ctx.argv[0] + 2);
619-
} else {
620-
strbuf_addf(&error_buf, "unknown switch `%c'",
621-
*ctx.opt);
622-
}
616+
if (ctx.argv[0][1] == '-')
617+
astrcatf(&error_buf, "unknown option `%s'",
618+
ctx.argv[0] + 2);
619+
else
620+
astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
623621
usage_with_options(usagestr, options);
624622
}
625623

@@ -806,9 +804,9 @@ static int usage_with_options_internal(const char * const *usagestr,
806804

807805
setup_pager();
808806

809-
if (strbuf_avail(&error_buf)) {
810-
fprintf(stderr, " Error: %s\n", error_buf.buf);
811-
strbuf_release(&error_buf);
807+
if (error_buf) {
808+
fprintf(stderr, " Error: %s\n", error_buf);
809+
zfree(&error_buf);
812810
}
813811

814812
fprintf(stderr, "\n Usage: %s\n", *usagestr++);
@@ -852,11 +850,15 @@ void usage_with_options_msg(const char * const *usagestr,
852850
const struct option *opts, const char *fmt, ...)
853851
{
854852
va_list ap;
853+
char *tmp = error_buf;
855854

856855
va_start(ap, fmt);
857-
strbuf_addv(&error_buf, fmt, ap);
856+
if (vasprintf(&error_buf, fmt, ap) == -1)
857+
die("vasprintf failed");
858858
va_end(ap);
859859

860+
free(tmp);
861+
860862
usage_with_options_internal(usagestr, opts, 0, NULL);
861863
exit(129);
862864
}

tools/perf/util/subcmd-util.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef __PERF_SUBCMD_UTIL_H
2+
#define __PERF_SUBCMD_UTIL_H
3+
4+
#include <stdio.h>
5+
6+
#define astrcatf(out, fmt, ...) \
7+
({ \
8+
char *tmp = *(out); \
9+
if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \
10+
die("asprintf failed"); \
11+
free(tmp); \
12+
})
13+
14+
static inline void astrcat(char **out, const char *add)
15+
{
16+
char *tmp = *out;
17+
18+
if (asprintf(out, "%s%s", tmp ?: "", add) == -1)
19+
die("asprintf failed");
20+
21+
free(tmp);
22+
}
23+
24+
#endif /* __PERF_SUBCMD_UTIL_H */

0 commit comments

Comments
 (0)