<div dir="ltr"><div>Merged in master with some small changes.</div><div><br></div><div>Thanks!</div><div>Jérémie<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 28 February 2018 at 16:06, Jonathan Rajotte <span dir="ltr"><<a href="mailto:jonathan.rajotte-julien@efficios.com" target="_blank">jonathan.rajotte-julien@efficios.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The symbol element is the string passed/to be passed on the cli<br>
for the --type option.<br>
<br>
Signed-off-by: Jonathan Rajotte <<a href="mailto:jonathan.rajotte-julien@efficios.com">jonathan.rajotte-julien@<wbr>efficios.com</a>><br>
---<br>
v2:<br>
    Mi -> MI (2x)<br>
    Missing empty line after variable declaration at top of function (2x)<br>
    mi_open() -> mi_open(void)<br>
    Check error on fileno()<br>
    mi_close argument is now an enum cmd_err_code<br>
<br>
---<br>
 src/bin/lttng/commands/add_<wbr>context.c | 294 ++++++++++++++++++++++++------<wbr>-----<br>
 src/common/mi-lttng-3.0.xsd          |  13 +-<br>
 src/common/mi-lttng.c                |   3 +<br>
 src/common/mi-lttng.h                |   3 +<br>
 tests/regression/tools/mi/<wbr>test_mi    |  15 +-<br>
 tests/utils/utils.sh                 |   7 +<br>
 6 files changed, 236 insertions(+), 99 deletions(-)<br>
<br>
diff --git a/src/bin/lttng/commands/add_<wbr>context.c b/src/bin/lttng/commands/add_<wbr>context.c<br>
index 209a9f4..95c834c 100644<br>
--- a/src/bin/lttng/commands/add_<wbr>context.c<br>
+++ b/src/bin/lttng/commands/add_<wbr>context.c<br>
@@ -482,20 +482,7 @@ struct ctx_type_list {<br>
        .head = CDS_LIST_HEAD_INIT(ctx_type_<wbr>list.head),<br>
 };<br>
<br>
-/*<br>
- * Pretty print context type.<br>
- */<br>
-static void print_ctx_type(FILE *ofp)<br>
-{<br>
-       int i = 0;<br>
<br>
-       while (ctx_opts[i].symbol != NULL) {<br>
-               if (!ctx_opts[i].hide_help) {<br>
-                       fprintf(ofp, "%s\n", ctx_opts[i].symbol);<br>
-               }<br>
-               i++;<br>
-       }<br>
-}<br>
<br>
 /*<br>
  * Find context numerical value from string.<br>
@@ -535,6 +522,190 @@ enum lttng_domain_type get_domain(void)<br>
        }<br>
 }<br>
<br>
+static int mi_open(void)<br>
+{<br>
+       int ret;<br>
+<br>
+       /* MI check */<br>
+       if (!lttng_opt_mi) {<br>
+               ret = 0;<br>
+               goto end;<br>
+       }<br>
+<br>
+       ret = fileno(stdout);<br>
+       if (ret < 0) {<br>
+               PERROR("fileno on stdout");<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+<br>
+       writer = mi_lttng_writer_create(ret, lttng_opt_mi);<br>
+       if (!writer) {<br>
+               ret = -LTTNG_ERR_NOMEM;<br>
+               goto end;<br>
+       }<br>
+<br>
+       /* Open command element */<br>
+       ret = mi_lttng_writer_command_open(<wbr>writer,<br>
+                       mi_lttng_element_command_add_<wbr>context);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+<br>
+       /* Open output element */<br>
+       ret = mi_lttng_writer_open_element(<wbr>writer,<br>
+                       mi_lttng_element_command_<wbr>output);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+end:<br>
+       return ret;<br>
+}<br>
+<br>
+static int mi_close(enum cmd_error_code success)<br>
+{<br>
+       int ret;<br>
+<br>
+       /* MI closing */<br>
+       if (!lttng_opt_mi) {<br>
+               ret = 0;<br>
+               goto end;<br>
+       }<br>
+       /* Close  output element */<br>
+       ret = mi_lttng_writer_close_element(<wbr>writer);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+<br>
+       /* Success ? */<br>
+       ret = mi_lttng_writer_write_element_<wbr>bool(writer,<br>
+                       mi_lttng_element_command_<wbr>success, !success);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+<br>
+       /* Command element close */<br>
+       ret = mi_lttng_writer_command_close(<wbr>writer);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+end:<br>
+       return ret;<br>
+}<br>
+<br>
+static void populate_context(struct lttng_event_context *context, const struct ctx_opts *opt)<br>
+{<br>
+       char *ptr;<br>
+<br>
+       context->ctx = (enum lttng_event_context_type) opt->ctx_type;<br>
+       switch (context->ctx) {<br>
+       case LTTNG_EVENT_CONTEXT_PERF_<wbr>COUNTER:<br>
+       case LTTNG_EVENT_CONTEXT_PERF_CPU_<wbr>COUNTER:<br>
+       case LTTNG_EVENT_CONTEXT_PERF_<wbr>THREAD_COUNTER:<br>
+               context->u.perf_counter.type = opt->u.perf.type;<br>
+               context->u.perf_counter.config = opt->u.perf.config;<br>
+               strncpy(context-><a href="http://u.perf_counter.name" rel="noreferrer" target="_blank">u.perf_<wbr>counter.name</a>, opt->symbol,<br>
+                               LTTNG_SYMBOL_NAME_LEN);<br>
+               context-><a href="http://u.perf_counter.name" rel="noreferrer" target="_blank">u.perf_counter.name</a>[<wbr>LTTNG_SYMBOL_NAME_LEN - 1] = '\0';<br>
+               /* Replace : and - by _ */<br>
+               while ((ptr = strchr(context-><a href="http://u.perf_counter.name" rel="noreferrer" target="_blank">u.perf_<wbr>counter.name</a>, '-')) != NULL) {<br>
+                       *ptr = '_';<br>
+               }<br>
+               while ((ptr = strchr(context-><a href="http://u.perf_counter.name" rel="noreferrer" target="_blank">u.perf_<wbr>counter.name</a>, ':')) != NULL) {<br>
+                       *ptr = '_';<br>
+               }<br>
+               break;<br>
+       case LTTNG_EVENT_CONTEXT_APP_<wbr>CONTEXT:<br>
+               context->u.app_ctx.provider_<wbr>name =<br>
+                       opt->u.app_ctx.provider_name;<br>
+               context->u.app_ctx.ctx_name =<br>
+                       opt->u.app_ctx.ctx_name;<br>
+               break;<br>
+       default:<br>
+               break;<br>
+       }<br>
+}<br>
+<br>
+/*<br>
+ * Pretty print context type.<br>
+ */<br>
+static int print_ctx_type(void)<br>
+{<br>
+<br>
+       FILE *ofp = stdout;<br>
+       int i = 0;<br>
+       int ret;<br>
+       struct lttng_event_context context;<br>
+<br>
+       memset(&context, 0, sizeof(context));<br>
+<br>
+       ret = mi_open();<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+               goto end;<br>
+       }<br>
+<br>
+       if (lttng_opt_mi) {<br>
+               /* Open a contexts element */<br>
+               ret = mi_lttng_writer_open_element(<wbr>writer, config_element_contexts);<br>
+               if (ret) {<br>
+                       ret = CMD_ERROR;<br>
+                       goto end;<br>
+               }<br>
+       }<br>
+<br>
+       while (ctx_opts[i].symbol != NULL) {<br>
+               if (!ctx_opts[i].hide_help) {<br>
+                       if (lttng_opt_mi) {<br>
+                               populate_context(&context, &ctx_opts[i]);<br>
+                               ret = mi_lttng_context(writer, &context, 1);<br>
+                               if (ret) {<br>
+                                       ret = CMD_ERROR;<br>
+                                       goto end;<br>
+                               }<br>
+<br>
+                               ret = mi_lttng_writer_write_element_<wbr>string(<br>
+                                               writer,<br>
+                                               mi_lttng_element_context_<wbr>symbol,<br>
+                                               ctx_opts[i].symbol);<br>
+                               if (ret) {<br>
+                                       ret = CMD_ERROR;<br>
+                                       goto end;<br>
+                               }<br>
+<br>
+                               ret = mi_lttng_writer_close_element(<wbr>writer);<br>
+                               if (ret) {<br>
+                                       ret = CMD_ERROR;<br>
+                                       goto end;<br>
+                               }<br>
+                       } else {<br>
+                               fprintf(ofp, "%s\n", ctx_opts[i].symbol);<br>
+                       }<br>
+               }<br>
+               i++;<br>
+       }<br>
+<br>
+       if (lttng_opt_mi) {<br>
+               /* Close contexts element */<br>
+               ret = mi_lttng_writer_close_element(<wbr>writer);<br>
+               if (ret) {<br>
+                       goto end;<br>
+               }<br>
+       }<br>
+<br>
+end:<br>
+       ret = mi_close(ret);<br>
+       if (ret) {<br>
+               ret = CMD_ERROR;<br>
+       }<br>
+       return ret;<br>
+}<br>
+<br>
 /*<br>
  * Add context to channel or event.<br>
  */<br>
@@ -544,7 +715,6 @@ static int add_context(char *session_name)<br>
        struct lttng_event_context context;<br>
        struct lttng_domain dom;<br>
        struct ctx_type *type;<br>
-       char *ptr;<br>
<br>
        memset(&context, 0, sizeof(context));<br>
        memset(&dom, 0, sizeof(dom));<br>
@@ -566,35 +736,10 @@ static int add_context(char *session_name)<br>
<br>
        /* Iterate over all the context types given */<br>
        cds_list_for_each_entry(type, &ctx_type_list.head, list) {<br>
-               context.ctx = (enum lttng_event_context_type) type->opt->ctx_type;<br>
-               switch (context.ctx) {<br>
-               case LTTNG_EVENT_CONTEXT_PERF_<wbr>COUNTER:<br>
-               case LTTNG_EVENT_CONTEXT_PERF_CPU_<wbr>COUNTER:<br>
-               case LTTNG_EVENT_CONTEXT_PERF_<wbr>THREAD_COUNTER:<br>
-                       context.u.perf_counter.type = type->opt->u.perf.type;<br>
-                       context.u.perf_counter.config = type->opt->u.perf.config;<br>
-                       strncpy(<a href="http://context.u.perf_counter.name" rel="noreferrer" target="_blank">context.u.perf_<wbr>counter.name</a>, type->opt->symbol,<br>
-                               LTTNG_SYMBOL_NAME_LEN);<br>
-                       <a href="http://context.u.perf_counter.name" rel="noreferrer" target="_blank">context.u.perf_counter.name</a>[<wbr>LTTNG_SYMBOL_NAME_LEN - 1] = '\0';<br>
-                       /* Replace : and - by _ */<br>
-                       while ((ptr = strchr(<a href="http://context.u.perf_counter.name" rel="noreferrer" target="_blank">context.u.perf_counter.<wbr>name</a>, '-')) != NULL) {<br>
-                               *ptr = '_';<br>
-                       }<br>
-                       while ((ptr = strchr(<a href="http://context.u.perf_counter.name" rel="noreferrer" target="_blank">context.u.perf_counter.<wbr>name</a>, ':')) != NULL) {<br>
-                               *ptr = '_';<br>
-                       }<br>
-                       break;<br>
-               case LTTNG_EVENT_CONTEXT_APP_<wbr>CONTEXT:<br>
-                       context.u.app_ctx.provider_<wbr>name =<br>
-                                       type->opt->u.app_ctx.provider_<wbr>name;<br>
-                       context.u.app_ctx.ctx_name =<br>
-                                       type->opt->u.app_ctx.ctx_name;<br>
-                       break;<br>
-               default:<br>
-                       break;<br>
-               }<br>
                DBG("Adding context...");<br>
<br>
+               populate_context(&context, type->opt);<br>
+<br>
                if (lttng_opt_mi) {<br>
                        /* We leave context open the update the success of the command */<br>
                        ret = mi_lttng_context(writer, &context, 1);<br>
@@ -602,6 +747,14 @@ static int add_context(char *session_name)<br>
                                ret = CMD_ERROR;<br>
                                goto error;<br>
                        }<br>
+<br>
+                       ret = mi_lttng_writer_write_element_<wbr>string(writer,<br>
+                                       mi_lttng_element_context_<wbr>symbol,<br>
+                                       type->opt->symbol);<br>
+                       if (ret) {<br>
+                               ret = CMD_ERROR;<br>
+                               goto error;<br>
+                       }<br>
                }<br>
<br>
                ret = lttng_add_context(handle, &context, NULL, opt_channel_name);<br>
@@ -894,7 +1047,6 @@ not_found:<br>
 int cmd_add_context(int argc, const char **argv)<br>
 {<br>
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;<br>
-       int success = 1;<br>
        static poptContext pc;<br>
        struct ctx_type *type, *tmptype;<br>
        char *session_name = NULL;<br>
@@ -913,7 +1065,7 @@ int cmd_add_context(int argc, const char **argv)<br>
                        SHOW_HELP();<br>
                        goto end;<br>
                case OPT_LIST:<br>
-                       print_ctx_type(stdout);<br>
+                       ret = print_ctx_type();<br>
                        goto end;<br>
                case OPT_TYPE:<br>
                {<br>
@@ -967,59 +1119,15 @@ int cmd_add_context(int argc, const char **argv)<br>
                session_name = opt_session_name;<br>
        }<br>
<br>
-       /* Mi check */<br>
-       if (lttng_opt_mi) {<br>
-               writer = mi_lttng_writer_create(fileno(<wbr>stdout), lttng_opt_mi);<br>
-               if (!writer) {<br>
-                       ret = -LTTNG_ERR_NOMEM;<br>
-                       goto end;<br>
-               }<br>
-<br>
-               /* Open command element */<br>
-               ret = mi_lttng_writer_command_open(<wbr>writer,<br>
-                               mi_lttng_element_command_add_<wbr>context);<br>
-               if (ret) {<br>
-                       ret = CMD_ERROR;<br>
-                       goto end;<br>
-               }<br>
-<br>
-               /* Open output element */<br>
-               ret = mi_lttng_writer_open_element(<wbr>writer,<br>
-                               mi_lttng_element_command_<wbr>output);<br>
-               if (ret) {<br>
-                       ret = CMD_ERROR;<br>
-                       goto end;<br>
-               }<br>
+       ret = mi_open();<br>
+       if (ret) {<br>
+               goto end;<br>
        }<br>
<br>
        command_ret = add_context(session_name);<br>
-       if (command_ret) {<br>
-               success = 0;<br>
-       }<br>
-<br>
-       /* Mi closing */<br>
-       if (lttng_opt_mi) {<br>
-               /* Close  output element */<br>
-               ret = mi_lttng_writer_close_element(<wbr>writer);<br>
-               if (ret) {<br>
-                       ret = CMD_ERROR;<br>
-                       goto end;<br>
-               }<br>
-<br>
-               /* Success ? */<br>
-               ret = mi_lttng_writer_write_element_<wbr>bool(writer,<br>
-                               mi_lttng_element_command_<wbr>success, success);<br>
-               if (ret) {<br>
-                       ret = CMD_ERROR;<br>
-                       goto end;<br>
-               }<br>
-<br>
-               /* Command element close */<br>
-               ret = mi_lttng_writer_command_close(<wbr>writer);<br>
-               if (ret) {<br>
-                       ret = CMD_ERROR;<br>
-                       goto end;<br>
-               }<br>
+       ret = mi_close(command_ret);<br>
+       if (ret) {<br>
+               goto end;<br>
        }<br>
<br>
 end:<br>
diff --git a/src/common/mi-lttng-3.0.xsd b/src/common/mi-lttng-3.0.xsd<br>
index ae00ee8..a2827a9 100644<br>
--- a/src/common/mi-lttng-3.0.xsd<br>
+++ b/src/common/mi-lttng-3.0.xsd<br>
@@ -480,11 +480,14 @@ THE SOFTWARE.<br>
<br>
        <!-- Maps to lttng_event_context --><br>
        <xs:complexType name="context_type"><br>
-               <xs:choice><br>
-                       <xs:element name="type" type="tns:context_type_type"/><br>
-                       <xs:element name="perf" type="tns:perf_counter_<wbr>context_type"/><br>
-                       <xs:element name="app" type="tns:app_context_type"/><br>
-               </xs:choice><br>
+               <xs:sequence><br>
+                       <xs:choice><br>
+                               <xs:element name="type" type="tns:context_type_type"/><br>
+                               <xs:element name="perf" type="tns:perf_counter_<wbr>context_type"/><br>
+                               <xs:element name="app" type="tns:app_context_type"/><br>
+                       </xs:choice><br>
+                       <xs:element name="symbol" type="xs:string" minOccurs="0" /><br>
+               </xs:sequence><br>
        </xs:complexType><br>
<br>
        <!-- Maps to an array of domain --><br>
diff --git a/src/common/mi-lttng.c b/src/common/mi-lttng.c<br>
index f0244d9..4e80c0c 100644<br>
--- a/src/common/mi-lttng.c<br>
+++ b/src/common/mi-lttng.c<br>
@@ -179,6 +179,9 @@ const char * const mi_lttng_element_snapshots = "snapshots";<br>
 /* String related to track/untrack command */<br>
 const char * const mi_lttng_element_track_<wbr>untrack_all_wildcard = "*";<br>
<br>
+/* String related to add-context command */<br>
+LTTNG_HIDDEN const char * const mi_lttng_element_context_<wbr>symbol = "symbol";<br>
+<br>
 /* Deprecated symbols preserved for ABI compatibility. */<br>
 const char * const mi_lttng_context_type_perf_<wbr>counter;<br>
 const char * const mi_lttng_context_type_perf_<wbr>cpu_counter;<br>
diff --git a/src/common/mi-lttng.h b/src/common/mi-lttng.h<br>
index e7cf8af..cddaf02 100644<br>
--- a/src/common/mi-lttng.h<br>
+++ b/src/common/mi-lttng.h<br>
@@ -185,6 +185,9 @@ extern const char * const mi_lttng_element_snapshots;<br>
 /* String related to track/untrack command */<br>
 const char * const mi_lttng_element_track_<wbr>untrack_all_wildcard;<br>
<br>
+/* String related to add-context command */<br>
+LTTNG_HIDDEN extern const char * const mi_lttng_element_context_<wbr>symbol;<br>
+<br>
 /* Utility string function  */<br>
 const char *mi_lttng_loglevel_string(int value, enum lttng_domain_type domain);<br>
 const char *mi_lttng_logleveltype_string(<wbr>enum lttng_loglevel_type value);<br>
diff --git a/tests/regression/tools/mi/<wbr>test_mi b/tests/regression/tools/mi/<wbr>test_mi<br>
index 0d3bd4f..3509da1 100755<br>
--- a/tests/regression/tools/mi/<wbr>test_mi<br>
+++ b/tests/regression/tools/mi/<wbr>test_mi<br>
@@ -59,7 +59,7 @@ DEVNULL=/dev/null 2>&1<br>
<br>
 DIR=$(readlink -f $TESTDIR)<br>
<br>
-NUM_TESTS=228<br>
+NUM_TESTS=230<br>
<br>
 source $TESTDIR/utils/utils.sh<br>
<br>
@@ -878,6 +878,18 @@ function test_track_untrack ()<br>
        destroy_lttng_sessions<br>
 }<br>
<br>
+function test_add_context_list()<br>
+{<br>
+       diag "Test context listing"<br>
+<br>
+       OUTPUT_FILE="list-context.xml"<br>
+       OUTPUT_DEST=$OUTPUT_DIR/$<wbr>OUTPUT_FILE<br>
+       lttng_add_context_list<br>
+<br>
+       $XML_VALIDATE $OUTPUT_DEST<br>
+       ok $? "Mi test: context listing validation"<br>
+}<br>
+<br>
 start_lttng_sessiond $FOO_LOAD_DIR<br>
 TESTS=(<br>
        test_version<br>
@@ -894,6 +906,7 @@ TESTS=(<br>
        test_snapshot<br>
        test_track_untrack<br>
        test_list_session_long_path<br>
+       test_add_context_list<br>
 )<br>
<br>
<br>
diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh<br>
index ab04979..0c51ac3 100644<br>
--- a/tests/utils/utils.sh<br>
+++ b/tests/utils/utils.sh<br>
@@ -1318,6 +1318,13 @@ function lttng_untrack_kernel_all_ok()<br>
        ok $? "Lttng untrack all pid on the kernel domain"<br>
 }<br>
<br>
+function lttng_add_context_list()<br>
+{<br>
+       $TESTDIR/../src/bin/lttng/$<wbr>LTTNG_BIN add-context --list 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST<br>
+       ret=$?<br>
+       ok $ret "Context listing"<br>
+}<br>
+<br>
 function add_context_lttng()<br>
 {<br>
        local expected_to_fail="$1"<br>
<span class="HOEnZb"><font color="#888888">-- <br>
2.7.4<br>
<br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Jérémie Galarneau<br>EfficiOS Inc.<br><a href="http://www.efficios.com" target="_blank">http://www.efficios.com</a></div>
</div>