Skip to contentSkip to conversation

perf-script : improves option passing mechansim

PATCH In review Adrien BAK 3 participants 4 messages Mar 18, 2014 – Mar 20, 2014 perf events

lore
Share & tools
lore

Adrien BAK

author
#0 lore raw

PATCH 1/1 perf-script : improves option passing mechansim

This pull request fixes the following issues :
* when passing custom arguments to a script, if those arguments are not declared within perf, they prevent the execution of perf.
e.g.

perf script -i path/to/perf.data -s my_script -arg1 arg_value

perf will issue an error message and no processing will occur

* when passing custom arguments to a script, if those arguments are declared by perf, they are consumed and not passed to the script.
e.g.
perf script -i path/to/perf.data -s my_script -h

perf will display its own help message, instead of the expected help message from my_script.

These issues are addressed as follows :
* The parse option flag is changed from PARSE_OPT_STOP_AT_NON_OPTION to PARSE_OPT_KEEP_UNKNOWN
* A new option type is introduce OPT_CALLBACK_FINAL_OPTION, which effectively prevents the parsing of all options located after the script.

---

 tools/perf/builtin-script.c     | 8 ++++----
 tools/perf/util/parse-options.c | 4 ++++
 tools/perf/util/parse-options.h | 5 +++++
 3 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-script.c +4 -4
index 9e9c91f..3cd6a46 100644
@@ -1523,9 +1523,9 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
             "show latency attributes (irqs/preemption disabled, etc)"),
     OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
                list_available_scripts),
-    OPT_CALLBACK('s', "script", NULL, "name",
-             "script file name (lang:script name, script name, or *)",
-             parse_scriptname),
+    OPT_CALLBACK_FINAL_OPTION('s', "script", NULL, "name",
+                  "script file name (lang:script name, script name, or *)",
+                  parse_scriptname),
     OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
            "generate perf-script.xx script in specified language"),
     OPT_STRING('i', "input", &input_name, "file", "input file name"),
@@ -1578,7 +1578,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
     setup_scripting();
 
     argc = parse_options(argc, argv, options, script_usage,
-                 PARSE_OPT_STOP_AT_NON_OPTION);
+                 PARSE_OPT_KEEP_UNKNOWN);
 
     file.path = input_name;
 
tools/perf/util/parse-options.c +4 -0
index d22e3f8..77f566d 100644
@@ -112,6 +112,8 @@ static int get_value(struct parse_opt_ctx_t *p,
             return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
         if (get_arg(p, opt, flags, &arg))
             return -1;
+        if (opt->flags & PARSE_OPT_FINAL_OPTION)
+            return (*opt->callback)(opt, arg, 0)?(-1) : (-3);
         return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 
     case OPTION_INTEGER:
@@ -366,6 +368,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
                 return parse_options_usage(usagestr, options, arg + 1, 1);
             case -2:
                 goto unknown;
+            case -3:
+                return PARSE_OPT_DONE;
             default:
                 break;
             }
tools/perf/util/parse-options.h +1 -0
index cbf0149..b1fa6b6 100644
@@ -38,6 +38,7 @@ enum parse_opt_option_flags {
     PARSE_OPT_NONEG   = 4,
     PARSE_OPT_HIDDEN  = 8,
     PARSE_OPT_LASTARG_DEFAULT = 16,
+    PARSE_OPT_FINAL_OPTION = 32,
 };
 
 struct option;
@@ -123,6 +124,10 @@ struct option {
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), 

.value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }

 #define OPT_CALLBACK(s, l, v, a, h, f) \
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), 
.value = (v), (a), .help = (h), .callback = (f) }
+#define OPT_CALLBACK_FINAL_OPTION(s, l, v, a, h, f) \
+    { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
+    .value = (v), (a), .help = (h), .callback = (f),\
+    .flags = PARSE_OPT_FINAL_OPTION}
 #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), 

.value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \

#1 lore raw

Em Tue, Mar 18, 2014 at 05:09:33PM +0100, Adrien BAK escreveu:

Quoted 8 lines

This pull request fixes the following issues :
* when passing custom arguments to a script, if those arguments are not declared within perf, they prevent the execution of perf.
e.g.

perf script -i path/to/perf.data -s my_script -arg1 arg_value

perf will issue an error message and no processing will occur

I haven't tested this, but what comes to mind is the use of -- to separate what options should be processed by perf and which ones should be left to the script, is that what you're fixing?

- Arnaldo

Quoted 105 lines

* when passing custom arguments to a script, if those arguments are declared by perf, they are consumed and not passed to the script.
e.g.
perf script -i path/to/perf.data -s my_script -h

perf will display its own help message, instead of the expected help message from my_script.

These issues are addressed as follows :
* The parse option flag is changed from PARSE_OPT_STOP_AT_NON_OPTION to PARSE_OPT_KEEP_UNKNOWN
* A new option type is introduce OPT_CALLBACK_FINAL_OPTION, which effectively
prevents the parsing of all options located after the script.

---

 tools/perf/builtin-script.c     | 8 ++++----
 tools/perf/util/parse-options.c | 4 ++++
 tools/perf/util/parse-options.h | 5 +++++
 3 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-script.c +3 -3
index 9e9c91f..3cd6a46 100644
@@ -1523,9 +1523,9 @@ int cmd_script(int argc, const char **argv,
 const char *prefix __maybe_unused)
             "show latency attributes (irqs/preemption disabled, etc)"),
     OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
                list_available_scripts),
-    OPT_CALLBACK('s', "script", NULL, "name",
-             "script file name (lang:script name, script name, or *)",
-             parse_scriptname),
+    OPT_CALLBACK_FINAL_OPTION('s', "script", NULL, "name",
+                  "script file name (lang:script name, script name, or *)",
+                  parse_scriptname),
     OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
            "generate perf-script.xx script in specified language"),
     OPT_STRING('i', "input", &input_name, "file", "input file name"),
tools/perf/builtin-script.c +1 -1
@@ -1578,7 +1578,7 @@ int cmd_script(int argc, const char **argv,
 const char *prefix __maybe_unused)
     setup_scripting();
 
     argc = parse_options(argc, argv, options, script_usage,
-                 PARSE_OPT_STOP_AT_NON_OPTION);
+                 PARSE_OPT_KEEP_UNKNOWN);
 
     file.path = input_name;
tools/perf/util/parse-options.c +4 -0
index d22e3f8..77f566d 100644
@@ -112,6 +112,8 @@ static int get_value(struct parse_opt_ctx_t *p,
             return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
         if (get_arg(p, opt, flags, &arg))
             return -1;
+        if (opt->flags & PARSE_OPT_FINAL_OPTION)
+            return (*opt->callback)(opt, arg, 0)?(-1) : (-3);
         return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 
     case OPTION_INTEGER:
@@ -366,6 +368,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
                 return parse_options_usage(usagestr, options, arg + 1, 1);
             case -2:
                 goto unknown;
+            case -3:
+                return PARSE_OPT_DONE;
             default:
                 break;
             }
tools/perf/util/parse-options.h +1 -0
index cbf0149..b1fa6b6 100644
@@ -38,6 +38,7 @@ enum parse_opt_option_flags {
     PARSE_OPT_NONEG   = 4,
     PARSE_OPT_HIDDEN  = 8,
     PARSE_OPT_LASTARG_DEFAULT = 16,
+    PARSE_OPT_FINAL_OPTION = 32,
 };
 
 struct option;
@@ -123,6 +124,10 @@ struct option {
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }

 #define OPT_CALLBACK(s, l, v, a, h, f) \
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), (a), .help = (h), .callback = (f) }

tools/perf/util/parse-options.h +4 -0
+#define OPT_CALLBACK_FINAL_OPTION(s, l, v, a, h, f) \
+    { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
+    .value = (v), (a), .help = (h), .callback = (f),\
+    .flags = PARSE_OPT_FINAL_OPTION}
 #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \

author
#2 lore raw

Indeed. I wasn't aware of the '--' behaviour and the documentation doesn't seem to mention it. My patch essentially implements the same behaviour, except that the script name itself is used to separate the options. I guess there is not much need the proposed patch then.

Sorry for the wasted time.
Adrien

Quoted 119 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 18, 2014 at 05:09:33PM +0100, Adrien BAK escreveu:

Quoted 8 lines

This pull request fixes the following issues :
* when passing custom arguments to a script, if those arguments are not declared within perf, they prevent the execution of perf.
e.g.

perf script -i path/to/perf.data -s my_script -arg1 arg_value

perf will issue an error message and no processing will occur

I haven't tested this, but what comes to mind is the use of -- to separate what options should be processed by perf and which ones should be left to the script, is that what you're fixing?

- Arnaldo

Quoted 104 lines

* when passing custom arguments to a script, if those arguments are declared by perf, they are consumed and not passed to the script.
e.g.
perf script -i path/to/perf.data -s my_script -h

perf will display its own help message, instead of the expected help message from my_script.

These issues are addressed as follows :
* The parse option flag is changed from PARSE_OPT_STOP_AT_NON_OPTION to PARSE_OPT_KEEP_UNKNOWN
* A new option type is introduce OPT_CALLBACK_FINAL_OPTION, which effectively
prevents the parsing of all options located after the script.

---

  tools/perf/builtin-script.c     | 8 ++++----
  tools/perf/util/parse-options.c | 4 ++++
  tools/perf/util/parse-options.h | 5 +++++
  3 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-script.c +3 -3
index 9e9c91f..3cd6a46 100644
@@ -1523,9 +1523,9 @@ int cmd_script(int argc, const char **argv,
 const char *prefix __maybe_unused)
              "show latency attributes (irqs/preemption disabled, etc)"),
      OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
                 list_available_scripts),
-    OPT_CALLBACK('s', "script", NULL, "name",
-             "script file name (lang:script name, script name, or *)",
-             parse_scriptname),
+    OPT_CALLBACK_FINAL_OPTION('s', "script", NULL, "name",
+                  "script file name (lang:script name, script name, or *)",
+                  parse_scriptname),
      OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
             "generate perf-script.xx script in specified language"),
      OPT_STRING('i', "input", &input_name, "file", "input file name"),
tools/perf/builtin-script.c +1 -1
@@ -1578,7 +1578,7 @@ int cmd_script(int argc, const char **argv,
 const char *prefix __maybe_unused)
      setup_scripting();
 
      argc = parse_options(argc, argv, options, script_usage,
-                 PARSE_OPT_STOP_AT_NON_OPTION);
+                 PARSE_OPT_KEEP_UNKNOWN);
 
      file.path = input_name;
tools/perf/util/parse-options.c +4 -0
index d22e3f8..77f566d 100644
@@ -112,6 +112,8 @@ static int get_value(struct parse_opt_ctx_t *p,
              return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
          if (get_arg(p, opt, flags, &arg))
              return -1;
+        if (opt->flags & PARSE_OPT_FINAL_OPTION)
+            return (*opt->callback)(opt, arg, 0)?(-1) : (-3);
          return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 
      case OPTION_INTEGER:
@@ -366,6 +368,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
                  return parse_options_usage(usagestr, options, arg + 1, 1);
              case -2:
                  goto unknown;
+            case -3:
+                return PARSE_OPT_DONE;
              default:
                  break;
              }
tools/perf/util/parse-options.h +1 -0
index cbf0149..b1fa6b6 100644
@@ -38,6 +38,7 @@ enum parse_opt_option_flags {
      PARSE_OPT_NONEG   = 4,
      PARSE_OPT_HIDDEN  = 8,
      PARSE_OPT_LASTARG_DEFAULT = 16,
+    PARSE_OPT_FINAL_OPTION = 32,
  };
 
  struct option;
@@ -123,6 +124,10 @@ struct option {
      { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }

  #define OPT_CALLBACK(s, l, v, a, h, f) \
      { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), (a), .help = (h), .callback = (f) }

tools/perf/util/parse-options.h +4 -0
+#define OPT_CALLBACK_FINAL_OPTION(s, l, v, a, h, f) \
+    { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
+    .value = (v), (a), .help = (h), .callback = (f),\
+    .flags = PARSE_OPT_FINAL_OPTION}
  #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
      { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),

.value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \

#3 lore raw

Em Thu, Mar 20, 2014 at 02:52:30PM +0100, Adrien BAK escreveu:

Quoted 6 lines

Indeed. I wasn't aware of the '--' behaviour and the documentation doesn't seem to mention it. My patch essentially implements the same behaviour, except that the script name itself is used to separate the options. I guess there is not much need the proposed patch then.

Sorry for the wasted time.

No need to be sorry, I appreciate _you_ taking the time to help improve the tools, maybe you can send a patch to update the documentation instead, with examples where -- is used so that people don't fall into this trap again?

Thanks!

- Arnaldo

Quoted 33 lines

Adrien

Quoted 30 lines · Arnaldo Carvalho de Melo

Em Tue, Mar 18, 2014 at 05:09:33PM +0100, Adrien BAK escreveu:

Quoted 8 lines

This pull request fixes the following issues :
* when passing custom arguments to a script, if those arguments are not declared within perf, they prevent the execution of perf.
e.g.

perf script -i path/to/perf.data -s my_script -arg1 arg_value

perf will issue an error message and no processing will occur

I haven't tested this, but what comes to mind is the use of -- to separate what options should be processed by perf and which ones should be left to the script, is that what you're fixing?

- Arnaldo

Quoted 16 lines

* when passing custom arguments to a script, if those arguments are declared by perf, they are consumed and not passed to the script.
e.g.
perf script -i path/to/perf.data -s my_script -h

perf will display its own help message, instead of the expected help message from my_script.

These issues are addressed as follows :
* The parse option flag is changed from PARSE_OPT_STOP_AT_NON_OPTION to PARSE_OPT_KEEP_UNKNOWN
* A new option type is introduce OPT_CALLBACK_FINAL_OPTION, which effectively
prevents the parsing of all options located after the script.