Skip to contentSkip to conversation

perf_counter : breaking parameter parsing when the command is reached

PATCH In review Erdem Aktas 2 participants 2 messages Jul 6, 2009 – Jul 10, 2009 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_counter : breaking parameter parsing when the command is reached

Once the perf parameter parser reaches a non-parameter word, that means the command is already found and the rest of the string is the parameter of this command so no need to parse more.

As an example, when we want to run
perf stat -- ls -al
it is obvious that the -al is the parameter of the ls command, so we should able to run this like
perf stat ls -al

tools/perf/util/parse-options.c +5 -0
index 1bf6719..4ad4962 100644
@@ -284,6 +284,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 				break;
 			ctx->out[ctx->cpidx++] = ctx->argv[0];
+			while (--(ctx->argc)) {
+				ctx->argv++;
+				ctx->out[ctx->cpidx++] = ctx->argv[0];
+			}
+			ctx->argc++;
 			continue;
 		}
#1 lore raw
Quoted 17 lines · Erdem Aktas

Once the perf parameter parser reaches a non-parameter word, that means the command is already found and the rest of the string is the parameter of this command so no need to parse more.

As an example, when we want to run
perf stat -- ls -al
it is obvious that the -al is the parameter of the ls command, so we should able to run this like
perf stat ls -al

tools/perf/util/parse-options.c +0 -0
index 1bf6719..4ad4962 100644

tools/perf/util/parse-options.c int parse_options_step(struct parse_opt_ctx_t *ctx, · line 291

Quoted 8 lines
tools/perf/util/parse-options.c +5 -0
@@ -284,6 +284,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 				break;
 			ctx->out[ctx->cpidx++] = ctx->argv[0];
+			while (--(ctx->argc)) {
+				ctx->argv++;
+				ctx->out[ctx->cpidx++] = ctx->argv[0];
+			}
+			ctx->argc++;

I think there might be a simpler solution: pass in PARSE_OPT_STOP_AT_NON_OPTION.

Then we'll trigger this existing code in parse_options_end():

        memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
        ctx->out[ctx->cpidx + ctx->argc] = NULL;
        return ctx->cpidx + ctx->argc;

which should solve the issue just as well, correct?

Ingo