[lttng-dev] [PATCH lttng-tools] mi: support "add-context --list"

Jérémie Galarneau jeremie.galarneau at efficios.com
Tue Feb 27 18:07:16 EST 2018


On 27 February 2018 at 14:44, Jonathan Rajotte
<jonathan.rajotte-julien at efficios.com> wrote:
> The symbol element is the string passed/to be passed on the cli
> for the --type option.
>
> Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien at efficios.com>
> ---
>  src/bin/lttng/commands/add_context.c | 281 ++++++++++++++++++++++++-----------
>  src/common/mi-lttng-3.0.xsd          |  13 +-
>  src/common/mi-lttng.c                |   3 +
>  src/common/mi-lttng.h                |   3 +
>  tests/regression/tools/mi/test_mi    |  15 +-
>  tests/utils/utils.sh                 |   7 +
>  6 files changed, 228 insertions(+), 94 deletions(-)
>
> diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c
> index 209a9f4..7bb98f6 100644
> --- a/src/bin/lttng/commands/add_context.c
> +++ b/src/bin/lttng/commands/add_context.c
> @@ -482,20 +482,7 @@ struct ctx_type_list {
>         .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
>  };
>
> -/*
> - * Pretty print context type.
> - */
> -static void print_ctx_type(FILE *ofp)
> -{
> -       int i = 0;
>
> -       while (ctx_opts[i].symbol != NULL) {
> -               if (!ctx_opts[i].hide_help) {
> -                       fprintf(ofp, "%s\n", ctx_opts[i].symbol);
> -               }
> -               i++;
> -       }
> -}
>
>  /*
>   * Find context numerical value from string.
> @@ -535,6 +522,182 @@ enum lttng_domain_type get_domain(void)
>         }
>  }
>
> +static int mi_open()
> +{
> +       int ret;

Missing empty line.

> +       /* Mi check */

MI is an abbreviation, please use "MI" instead of "Mi".

> +       if (!lttng_opt_mi) {
> +               ret = 0;
> +               goto end;
> +       }
> +
> +       writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
> +       if (!writer) {
> +               ret = -LTTNG_ERR_NOMEM;
> +               goto end;
> +       }
> +
> +       /* Open command element */
> +       ret = mi_lttng_writer_command_open(writer,
> +                       mi_lttng_element_command_add_context);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +
> +       /* Open output element */
> +       ret = mi_lttng_writer_open_element(writer,
> +                       mi_lttng_element_command_output);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +end:
> +       return ret;
> +}
> +
> +static int mi_close(int success)
> +{
> +       int ret;

Missing empty line.

> +       /* Mi closing */

Mi -> MI.

> +       if (!lttng_opt_mi) {
> +               ret = 0;
> +               goto end;
> +       }
> +       /* Close  output element */
> +       ret = mi_lttng_writer_close_element(writer);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +
> +       /* Success ? */
> +       ret = mi_lttng_writer_write_element_bool(writer,
> +                       mi_lttng_element_command_success, success);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +
> +       /* Command element close */
> +       ret = mi_lttng_writer_command_close(writer);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +end:
> +       return ret;
> +}
> +
> +static void populate_context(struct lttng_event_context *context, const struct ctx_opts *opt)
> +{
> +       char *ptr;
> +
> +       context->ctx = (enum lttng_event_context_type) opt->ctx_type;
> +       switch (context->ctx) {
> +       case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
> +       case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
> +       case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
> +               context->u.perf_counter.type = opt->u.perf.type;
> +               context->u.perf_counter.config = opt->u.perf.config;
> +               strncpy(context->u.perf_counter.name, opt->symbol,
> +                               LTTNG_SYMBOL_NAME_LEN);
> +               context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
> +               /* Replace : and - by _ */
> +               while ((ptr = strchr(context->u.perf_counter.name, '-')) != NULL) {
> +                       *ptr = '_';
> +               }
> +               while ((ptr = strchr(context->u.perf_counter.name, ':')) != NULL) {
> +                       *ptr = '_';
> +               }
> +               break;
> +       case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
> +               context->u.app_ctx.provider_name =
> +                       opt->u.app_ctx.provider_name;
> +               context->u.app_ctx.ctx_name =
> +                       opt->u.app_ctx.ctx_name;
> +               break;
> +       default:
> +               break;
> +       }
> +}
> +
> +/*
> + * Pretty print context type.
> + */
> +static int print_ctx_type(void)
> +{
> +
> +       FILE *ofp = stdout;
> +       int i = 0;
> +       int ret;
> +       struct lttng_event_context context;
> +
> +       memset(&context, 0, sizeof(context));
> +
> +       ret = mi_open();
> +       if (ret) {
> +               ret = CMD_ERROR;
> +               goto end;
> +       }
> +
> +       if (lttng_opt_mi) {
> +               /* Open a contexts element */
> +               ret = mi_lttng_writer_open_element(writer, config_element_contexts);
> +               if (ret) {
> +                       ret = CMD_ERROR;
> +                       goto end;
> +               }
> +       }
> +
> +       while (ctx_opts[i].symbol != NULL) {
> +               if (!ctx_opts[i].hide_help) {
> +                       if (lttng_opt_mi) {
> +

Remove empty line.

> +                               populate_context(&context, &ctx_opts[i]);
> +                               ret = mi_lttng_context(writer, &context, 1);
> +                               if (ret) {
> +                                       ret = CMD_ERROR;
> +                                       goto end;
> +                               }
> +
> +                               ret = mi_lttng_writer_write_element_string(
> +                                               writer,
> +                                               mi_lttng_element_context_symbol,
> +                                               ctx_opts[i].symbol);
> +                               if (ret) {
> +                                       ret = CMD_ERROR;
> +                                       goto end;
> +                               }
> +
> +                               ret = mi_lttng_writer_close_element(writer);
> +                               if (ret) {
> +                                       ret = CMD_ERROR;
> +                                       goto end;
> +                               }
> +                       } else {
> +                               fprintf(ofp, "%s\n", ctx_opts[i].symbol);
> +                       }
> +               }
> +               i++;
> +       }
> +
> +       if (lttng_opt_mi) {
> +               /* Close contexts element */
> +               ret = mi_lttng_writer_close_element(writer);
> +               if (ret) {
> +                       goto end;
> +               }
> +       }
> +
> +       ret = mi_close(1);
> +       if (ret) {
> +               ret = CMD_ERROR;
> +       }
> +end:
> +       return ret;
> +}
> +
>  /*
>   * Add context to channel or event.
>   */
> @@ -544,7 +707,6 @@ static int add_context(char *session_name)
>         struct lttng_event_context context;
>         struct lttng_domain dom;
>         struct ctx_type *type;
> -       char *ptr;
>
>         memset(&context, 0, sizeof(context));
>         memset(&dom, 0, sizeof(dom));
> @@ -566,35 +728,10 @@ static int add_context(char *session_name)
>
>         /* Iterate over all the context types given */
>         cds_list_for_each_entry(type, &ctx_type_list.head, list) {
> -               context.ctx = (enum lttng_event_context_type) type->opt->ctx_type;
> -               switch (context.ctx) {
> -               case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
> -               case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
> -               case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
> -                       context.u.perf_counter.type = type->opt->u.perf.type;
> -                       context.u.perf_counter.config = type->opt->u.perf.config;
> -                       strncpy(context.u.perf_counter.name, type->opt->symbol,
> -                               LTTNG_SYMBOL_NAME_LEN);
> -                       context.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
> -                       /* Replace : and - by _ */
> -                       while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
> -                               *ptr = '_';
> -                       }
> -                       while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
> -                               *ptr = '_';
> -                       }
> -                       break;
> -               case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
> -                       context.u.app_ctx.provider_name =
> -                                       type->opt->u.app_ctx.provider_name;
> -                       context.u.app_ctx.ctx_name =
> -                                       type->opt->u.app_ctx.ctx_name;
> -                       break;
> -               default:
> -                       break;
> -               }
>                 DBG("Adding context...");
>
> +               populate_context(&context, type->opt);
> +
>                 if (lttng_opt_mi) {
>                         /* We leave context open the update the success of the command */
>                         ret = mi_lttng_context(writer, &context, 1);
> @@ -602,6 +739,14 @@ static int add_context(char *session_name)
>                                 ret = CMD_ERROR;
>                                 goto error;
>                         }
> +
> +                       ret = mi_lttng_writer_write_element_string(writer,
> +                                       mi_lttng_element_context_symbol,
> +                                       type->opt->symbol);
> +                       if (ret) {
> +                               ret = CMD_ERROR;
> +                               goto error;
> +                       }
>                 }
>
>                 ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
> @@ -913,7 +1058,7 @@ int cmd_add_context(int argc, const char **argv)
>                         SHOW_HELP();
>                         goto end;
>                 case OPT_LIST:
> -                       print_ctx_type(stdout);
> +                       ret = print_ctx_type();
>                         goto end;
>                 case OPT_TYPE:
>                 {
> @@ -967,29 +1112,9 @@ int cmd_add_context(int argc, const char **argv)
>                 session_name = opt_session_name;
>         }
>
> -       /* Mi check */
> -       if (lttng_opt_mi) {
> -               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
> -               if (!writer) {
> -                       ret = -LTTNG_ERR_NOMEM;
> -                       goto end;
> -               }
> -
> -               /* Open command element */
> -               ret = mi_lttng_writer_command_open(writer,
> -                               mi_lttng_element_command_add_context);
> -               if (ret) {
> -                       ret = CMD_ERROR;
> -                       goto end;
> -               }
> -
> -               /* Open output element */
> -               ret = mi_lttng_writer_open_element(writer,
> -                               mi_lttng_element_command_output);
> -               if (ret) {
> -                       ret = CMD_ERROR;
> -                       goto end;
> -               }
> +       ret = mi_open();
> +       if (ret) {
> +               goto end;
>         }
>
>         command_ret = add_context(session_name);
> @@ -997,29 +1122,9 @@ int cmd_add_context(int argc, const char **argv)
>                 success = 0;
>         }
>
> -       /* Mi closing */
> -       if (lttng_opt_mi) {
> -               /* Close  output element */
> -               ret = mi_lttng_writer_close_element(writer);
> -               if (ret) {
> -                       ret = CMD_ERROR;
> -                       goto end;
> -               }
> -
> -               /* Success ? */
> -               ret = mi_lttng_writer_write_element_bool(writer,
> -                               mi_lttng_element_command_success, success);
> -               if (ret) {
> -                       ret = CMD_ERROR;
> -                       goto end;
> -               }
> -
> -               /* Command element close */
> -               ret = mi_lttng_writer_command_close(writer);
> -               if (ret) {
> -                       ret = CMD_ERROR;
> -                       goto end;
> -               }
> +       ret = mi_close(success);
> +       if (ret) {
> +               goto end;
>         }
>
>  end:
> diff --git a/src/common/mi-lttng-3.0.xsd b/src/common/mi-lttng-3.0.xsd
> index ae00ee8..a2827a9 100644
> --- a/src/common/mi-lttng-3.0.xsd
> +++ b/src/common/mi-lttng-3.0.xsd
> @@ -480,11 +480,14 @@ THE SOFTWARE.
>
>         <!-- Maps to lttng_event_context -->
>         <xs:complexType name="context_type">
> -               <xs:choice>
> -                       <xs:element name="type" type="tns:context_type_type"/>
> -                       <xs:element name="perf" type="tns:perf_counter_context_type"/>
> -                       <xs:element name="app" type="tns:app_context_type"/>
> -               </xs:choice>
> +               <xs:sequence>
> +                       <xs:choice>
> +                               <xs:element name="type" type="tns:context_type_type"/>
> +                               <xs:element name="perf" type="tns:perf_counter_context_type"/>
> +                               <xs:element name="app" type="tns:app_context_type"/>
> +                       </xs:choice>
> +                       <xs:element name="symbol" type="xs:string" minOccurs="0" />
> +               </xs:sequence>
>         </xs:complexType>
>
>         <!-- Maps to an array of domain -->
> diff --git a/src/common/mi-lttng.c b/src/common/mi-lttng.c
> index f0244d9..4e80c0c 100644
> --- a/src/common/mi-lttng.c
> +++ b/src/common/mi-lttng.c
> @@ -179,6 +179,9 @@ const char * const mi_lttng_element_snapshots = "snapshots";
>  /* String related to track/untrack command */
>  const char * const mi_lttng_element_track_untrack_all_wildcard = "*";
>
> +/* String related to add-context command */
> +LTTNG_HIDDEN const char * const mi_lttng_element_context_symbol = "symbol";
> +
>  /* Deprecated symbols preserved for ABI compatibility. */
>  const char * const mi_lttng_context_type_perf_counter;
>  const char * const mi_lttng_context_type_perf_cpu_counter;
> diff --git a/src/common/mi-lttng.h b/src/common/mi-lttng.h
> index e7cf8af..cddaf02 100644
> --- a/src/common/mi-lttng.h
> +++ b/src/common/mi-lttng.h
> @@ -185,6 +185,9 @@ extern const char * const mi_lttng_element_snapshots;
>  /* String related to track/untrack command */
>  const char * const mi_lttng_element_track_untrack_all_wildcard;
>
> +/* String related to add-context command */
> +LTTNG_HIDDEN extern const char * const mi_lttng_element_context_symbol;
> +
>  /* Utility string function  */
>  const char *mi_lttng_loglevel_string(int value, enum lttng_domain_type domain);
>  const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value);
> diff --git a/tests/regression/tools/mi/test_mi b/tests/regression/tools/mi/test_mi
> index 0d3bd4f..3509da1 100755
> --- a/tests/regression/tools/mi/test_mi
> +++ b/tests/regression/tools/mi/test_mi
> @@ -59,7 +59,7 @@ DEVNULL=/dev/null 2>&1
>
>  DIR=$(readlink -f $TESTDIR)
>
> -NUM_TESTS=228
> +NUM_TESTS=230
>
>  source $TESTDIR/utils/utils.sh
>
> @@ -878,6 +878,18 @@ function test_track_untrack ()
>         destroy_lttng_sessions
>  }
>
> +function test_add_context_list()
> +{
> +       diag "Test context listing"
> +
> +       OUTPUT_FILE="list-context.xml"
> +       OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
> +       lttng_add_context_list
> +
> +       $XML_VALIDATE $OUTPUT_DEST
> +       ok $? "Mi test: context listing validation"
> +}
> +
>  start_lttng_sessiond $FOO_LOAD_DIR
>  TESTS=(
>         test_version
> @@ -894,6 +906,7 @@ TESTS=(
>         test_snapshot
>         test_track_untrack
>         test_list_session_long_path
> +       test_add_context_list
>  )
>
>
> diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh
> index ab04979..0c51ac3 100644
> --- a/tests/utils/utils.sh
> +++ b/tests/utils/utils.sh
> @@ -1318,6 +1318,13 @@ function lttng_untrack_kernel_all_ok()
>         ok $? "Lttng untrack all pid on the kernel domain"
>  }
>
> +function lttng_add_context_list()
> +{
> +       $TESTDIR/../src/bin/lttng/$LTTNG_BIN add-context --list 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
> +       ret=$?
> +       ok $ret "Context listing"
> +}
> +
>  function add_context_lttng()
>  {
>         local expected_to_fail="$1"
> --
> 2.7.4
>



-- 
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com


More information about the lttng-dev mailing list