X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fenable_events.c;h=46a08d88c89b7a78de567a253d9e876d28829a7c;hp=556538daddd87444019e7013933803599c48c50c;hb=da3d7d0e160ca3c7d78002a8e7ba9ee66e42a540;hpb=b2064f542c84041bf0b991d7d83f9e85db8adc19 diff --git a/src/bin/lttng/commands/enable_events.c b/src/bin/lttng/commands/enable_events.c index 556538dad..46a08d88c 100644 --- a/src/bin/lttng/commands/enable_events.c +++ b/src/bin/lttng/commands/enable_events.c @@ -27,9 +27,17 @@ #include #include -#include "../command.h" #include +/* Mi dependancy */ +#include + +#include "../command.h" + +#if (LTTNG_SYMBOL_NAME_LEN == 256) +#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" +#endif + static char *opt_event_list; static int opt_event_type; static const char *opt_loglevel; @@ -67,6 +75,7 @@ enum { }; static struct lttng_handle *handle; +static struct mi_writer *writer; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ @@ -135,7 +144,8 @@ static void usage(FILE *ofp) fprintf(ofp, " --syscall System call event\n"); fprintf(ofp, "\n"); fprintf(ofp, " --loglevel name\n"); - fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n"); + fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel.\n"); + fprintf(ofp, " For JUL domain, see the table below for the range values.\n"); fprintf(ofp, " --loglevel-only name\n"); fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n"); fprintf(ofp, "\n"); @@ -225,6 +235,7 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt) { int ret; char s_hex[19]; +#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */ char name[LTTNG_SYMBOL_NAME_LEN]; if (opt == NULL) { @@ -233,7 +244,8 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt) } /* Check for symbol+offset */ - ret = sscanf(opt, "%[^'+']+%s", name, s_hex); + ret = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API + "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex); if (ret == 2) { strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; @@ -251,7 +263,8 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt) /* Check for symbol */ if (isalpha(name[0])) { - ret = sscanf(opt, "%s", name); + ret = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s", + name); if (ret == 1) { strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; @@ -264,7 +277,7 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt) } /* Check for address */ - ret = sscanf(opt, "%s", s_hex); + ret = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex); if (ret > 0) { if (*s_hex == '\0') { ERR("Invalid probe address %s", s_hex); @@ -391,6 +404,40 @@ const char *print_raw_channel_name(const char *name) return name ? : ""; } +/* + * Mi print exlcusion list + */ +static +int mi_print_exclusion(int count, char **names) +{ + int i, ret; + + assert(writer); + + if (count == 0) { + ret = 0; + goto end; + } + ret = mi_lttng_writer_open_element(writer, config_element_exclusions); + if (ret) { + goto end; + } + + for (i = 0; i < count; i++) { + ret = mi_lttng_writer_write_element_string(writer, + config_element_exclusion, names[i]); + if (ret) { + goto end; + } + } + + /* Close exclusions element */ + ret = mi_lttng_writer_close_element(writer); + +end: + return ret; +} + /* * Return allocated string for pretty-printing exclusion names. */ @@ -421,6 +468,7 @@ char *print_exclusions(int count, char **names) strcat(ret, ","); } } + return ret; } @@ -514,10 +562,12 @@ end: } /* * Enabling event using the lttng API. + * Note: in case of error only the last error code will be return. */ static int enable_events(char *session_name) { - int ret = CMD_SUCCESS, warn = 0; + int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; + int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1; char *event_name, *channel_name = NULL; struct lttng_event ev; struct lttng_domain dom; @@ -570,6 +620,16 @@ static int enable_events(char *session_name) goto error; } + /* Prepare Mi */ + if (lttng_opt_mi) { + /* Open a events element */ + ret = mi_lttng_writer_open_element(writer, config_element_events); + if (ret) { + ret = CMD_ERROR; + goto error; + } + } + if (opt_enable_all) { /* Default setup for enable all */ if (opt_kernel) { @@ -609,6 +669,7 @@ static int enable_events(char *session_name) if (ret == CMD_ERROR) { goto error; } + ev.exclusion = 1; } if (!opt_filter) { ret = lttng_enable_event_with_exclusions(handle, @@ -620,6 +681,7 @@ static int enable_events(char *session_name) case LTTNG_ERR_KERN_EVENT_EXIST: WARN("Kernel events already enabled (channel %s, session %s)", print_channel_name(channel_name), session_name); + warn = 1; break; default: ERR("Events: %s (channel %s, session %s)", @@ -628,6 +690,7 @@ static int enable_events(char *session_name) ? print_raw_channel_name(channel_name) : print_channel_name(channel_name), session_name); + error = 1; break; } goto end; @@ -684,30 +747,81 @@ static int enable_events(char *session_name) goto error; } } + if (opt_filter) { - ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, + command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, opt_filter, exclusion_count, exclusion_list); - if (ret < 0) { - switch (-ret) { + if (command_ret < 0) { + switch (-command_ret) { case LTTNG_ERR_FILTER_EXIST: WARN("Filter on all events is already enabled" " (channel %s, session %s)", print_channel_name(channel_name), session_name); + warn = 1; break; default: ERR("All events: %s (channel %s, session %s, filter \'%s\')", - lttng_strerror(ret), - ret == -LTTNG_ERR_NEED_CHANNEL_NAME + lttng_strerror(command_ret), + command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ? print_raw_channel_name(channel_name) : print_channel_name(channel_name), session_name, opt_filter); + error = 1; break; } - goto error; + error_holder = command_ret; } else { + ev.filter = 1; MSG("Filter '%s' successfully set", opt_filter); } } + + if (lttng_opt_mi) { + /* The wildcard * is used for kernel and ust domain to + * represent ALL. We copy * in event name to force the wildcard use + * for kernel domain + * + * Note: this is strictly for semantic and printing while in + * machine interface mode. + */ + strcpy(ev.name, "*"); + + /* If we reach here the events are enabled */ + if (!error && !warn) { + ev.enabled = 1; + } else { + ev.enabled = 0; + success = 0; + } + ret = mi_lttng_event(writer, &ev, 1); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* print exclusion */ + ret = mi_print_exclusion(exclusion_count, exclusion_list); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* Success ? */ + ret = mi_lttng_writer_write_element_bool(writer, + mi_lttng_element_command_success, success); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* Close event element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto error; + } + } + goto end; } @@ -794,6 +908,7 @@ static int enable_events(char *session_name) } if (opt_exclude) { + ev.exclusion = 1; if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) { ERR("Exclusion option can only be used with tracepoint events"); ret = CMD_ERROR; @@ -857,35 +972,44 @@ static int enable_events(char *session_name) if (!opt_filter) { char *exclusion_string; - ret = lttng_enable_event_with_exclusions(handle, + command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, NULL, exclusion_count, exclusion_list); exclusion_string = print_exclusions(exclusion_count, exclusion_list); - if (ret < 0) { + if (command_ret < 0) { /* Turn ret to positive value to handle the positive error code */ - switch (-ret) { + switch (-command_ret) { case LTTNG_ERR_KERN_EVENT_EXIST: WARN("Kernel event %s%s already enabled (channel %s, session %s)", event_name, exclusion_string, print_channel_name(channel_name), session_name); + warn = 1; break; default: ERR("Event %s%s: %s (channel %s, session %s)", event_name, exclusion_string, - lttng_strerror(ret), - ret == -LTTNG_ERR_NEED_CHANNEL_NAME + lttng_strerror(command_ret), + command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ? print_raw_channel_name(channel_name) : print_channel_name(channel_name), session_name); + error = 1; break; } - warn = 1; + error_holder = command_ret; } else { - MSG("%s event %s%s created in channel %s", - get_domain_str(dom.type), event_name, - exclusion_string, - print_channel_name(channel_name)); + /* So we don't print the default channel name for JUL. */ + if (dom.type == LTTNG_DOMAIN_JUL) { + MSG("%s event %s%s enabled.", + get_domain_str(dom.type), event_name, + exclusion_string); + } else { + MSG("%s event %s%s created in channel %s", + get_domain_str(dom.type), event_name, + exclusion_string, + print_channel_name(channel_name)); + } } free(exclusion_string); } @@ -893,31 +1017,36 @@ static int enable_events(char *session_name) if (opt_filter) { char *exclusion_string; - ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, + /* Filter present */ + ev.filter = 1; + + command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name, opt_filter, exclusion_count, exclusion_list); exclusion_string = print_exclusions(exclusion_count, exclusion_list); - if (ret < 0) { - switch (-ret) { + if (command_ret < 0) { + switch (-command_ret) { case LTTNG_ERR_FILTER_EXIST: WARN("Filter on event %s%s is already enabled" " (channel %s, session %s)", event_name, exclusion_string, print_channel_name(channel_name), session_name); + warn = 1; break; default: ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name, exclusion_string, - lttng_strerror(ret), - ret == -LTTNG_ERR_NEED_CHANNEL_NAME + lttng_strerror(command_ret), + command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ? print_raw_channel_name(channel_name) : print_channel_name(channel_name), session_name, opt_filter); + error = 1; break; } - free(exclusion_string); - goto error; + error_holder = command_ret; + } else { MSG("Event %s%s: Filter '%s' successfully set", event_name, exclusion_string, @@ -926,15 +1055,66 @@ static int enable_events(char *session_name) free(exclusion_string); } + if (lttng_opt_mi) { + if (command_ret) { + success = 0; + ev.enabled = 0; + } else { + ev.enabled = 1; + } + + ret = mi_lttng_event(writer, &ev, 1); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* print exclusion */ + ret = mi_print_exclusion(exclusion_count, exclusion_list); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* Success ? */ + ret = mi_lttng_writer_write_element_bool(writer, + mi_lttng_element_command_success, success); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + /* Close event element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto end; + } + } + /* Next event */ event_name = strtok(NULL, ","); + /* Reset warn, error and success */ + success = 1; } end: + /* Close Mi */ + if (lttng_opt_mi) { + /* Close events element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto error; + } + } error: if (warn) { ret = CMD_WARNING; } + if (error) { + ret = CMD_ERROR; + } lttng_destroy_handle(handle); if (exclusion_list != NULL) { @@ -944,6 +1124,11 @@ error: free(exclusion_list); } + /* Overwrite ret with error_holder if there was an actual error with + * enabling an event. + */ + ret = error_holder ? error_holder : ret; + return ret; } @@ -952,7 +1137,7 @@ error: */ int cmd_enable_events(int argc, const char **argv) { - int opt, ret = CMD_SUCCESS; + int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; static poptContext pc; char *session_name = NULL; int event_type = -1; @@ -1019,6 +1204,31 @@ int cmd_enable_events(int argc, const char **argv) } } + /* 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_enable_event); + 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; + } + } + opt_event_list = (char*) poptGetArg(pc); if (opt_event_list == NULL && opt_enable_all == 0) { ERR("Missing event name(s).\n"); @@ -1030,20 +1240,59 @@ int cmd_enable_events(int argc, const char **argv) if (!opt_session_name) { session_name = get_session_name(); if (session_name == NULL) { - ret = CMD_ERROR; - goto end; + command_ret = CMD_ERROR; + success = 0; + goto mi_closing; } } else { session_name = opt_session_name; } - ret = enable_events(session_name); + command_ret = enable_events(session_name); + if (command_ret) { + success = 0; + goto mi_closing; + } + +mi_closing: + /* Mi closing */ + if (lttng_opt_mi) { + /* Close output element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + 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: + /* Mi clean-up */ + if (writer && mi_lttng_writer_destroy(writer)) { + /* Preserve original error code */ + ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; + } + if (opt_session_name == NULL) { free(session_name); } + /* Overwrite ret if an error occurred in enable_events */ + ret = command_ret ? command_ret : ret; + poptFreeContext(pc); return ret; }