From a3adfb05f9a9d0bb3f49b6696a0c233d6e9f6626 Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Fri, 10 Sep 2010 10:49:49 +0200 Subject: [PATCH] Add functions and command line for listing trace_events v3 Changes from version 2: Fix a comment. Changes from version 1: Fix silly things per Davids comments, realise that we are looping over pids and therefore need to set all pointers to NULL and all counters to zero each time. --- include/ust/ustcmd.h | 6 ++++ libust/tracectl.c | 41 +++++++++++++++++++++-- libustcmd/ustcmd.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ ustctl/ustctl.c | 25 +++++++++++++- 4 files changed, 146 insertions(+), 3 deletions(-) diff --git a/include/ust/ustcmd.h b/include/ust/ustcmd.h index 60f5018..986ae61 100644 --- a/include/ust/ustcmd.h +++ b/include/ust/ustcmd.h @@ -43,6 +43,10 @@ struct marker_status { char *fs; /* Format string (end of marker_status array if NULL) */ }; +struct trace_event_status { + char *name; +}; + extern pid_t *ustcmd_get_online_pids(void); extern int ustcmd_set_marker_state(const char *, int, pid_t); extern int ustcmd_set_subbuf_size(const char *, pid_t); @@ -59,6 +63,8 @@ extern int ustcmd_free_cmsf(struct marker_status *); extern unsigned int ustcmd_count_nl(const char *); extern int ustcmd_send_cmd(const char *, pid_t, char **); extern int ustcmd_get_cmsf(struct marker_status **, pid_t); +extern int ustcmd_free_tes(struct trace_event_status *); +extern int ustcmd_get_tes(struct trace_event_status **, pid_t); extern int ustcmd_set_sock_path(const char *, pid_t); extern int ustcmd_get_sock_path(char **, pid_t); extern int ustcmd_force_switch(pid_t); diff --git a/libust/tracectl.c b/libust/tracectl.c index e64b26f..f1b644c 100644 --- a/libust/tracectl.c +++ b/libust/tracectl.c @@ -35,6 +35,7 @@ #include #include +#include #include #include "tracer.h" #include "usterr.h" @@ -127,6 +128,21 @@ static void print_markers(FILE *fp) unlock_markers(); } +static void print_trace_events(FILE *fp) +{ + struct trace_event_iter iter; + + lock_trace_events(); + trace_event_iter_reset(&iter); + trace_event_iter_start(&iter); + + while(iter.trace_event) { + fprintf(fp, "trace_event: %s\n", iter.trace_event->name); + trace_event_iter_next(&iter); + } + unlock_trace_events(); +} + static int init_socket(void); /* Ask the daemon to collect a trace called trace_name and being @@ -867,8 +883,29 @@ int process_client_cmd(char *recvbuf, struct ustcomm_source *src) result = ustcomm_send_reply(&ustcomm_app.server, ptr, src); free(ptr); - } - else if(!strcmp(recvbuf, "start")) { + } else if (!strcmp(recvbuf, "print_trace_events")) { + print_trace_events(stderr); + + } else if(!strcmp(recvbuf, "list_trace_events")) { + char *ptr; + size_t size; + FILE *fp; + + fp = open_memstream(&ptr, &size); + if (fp == NULL) { + ERR("opening memstream failed"); + return -1; + } + print_trace_events(fp); + fclose(fp); + + result = ustcomm_send_reply(&ustcomm_app.server, ptr, src); + if (result < 0) { + ERR("list_trace_events failed"); + return -1; + } + free(ptr); + } else if(!strcmp(recvbuf, "start")) { /* start is an operation that setups the trace, allocates it and starts it */ result = ltt_trace_setup(trace_name); if(result < 0) { diff --git a/libustcmd/ustcmd.c b/libustcmd/ustcmd.c index f0a6ae0..4248072 100644 --- a/libustcmd/ustcmd.c +++ b/libustcmd/ustcmd.c @@ -438,6 +438,83 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid) return 0; } + +/** + * Frees a TES array. + * + * @param tes TES array to free + * @return 0 if successful, or error USTCMD_ERR_ARG + */ +int ustcmd_free_tes(struct trace_event_status *tes) +{ + if (tes == NULL) { + return USTCMD_ERR_ARG; + } + + unsigned int i = 0; + while (tes[i].name != NULL) { + free(tes[i].name); + ++i; + } + free(tes); + + return 0; +} + +/** + * Gets trace_events string for a given PID. + * + * @param tes Pointer to TES array to be filled (callee allocates, caller + * frees with `ustcmd_free_tes') + * @param pid Targeted PID + * @return 0 if successful, or -1 on error + */ +int ustcmd_get_tes(struct trace_event_status **tes, + const pid_t pid) +{ + char *big_str = NULL; + int result; + struct trace_event_status *tmp_tes = NULL; + unsigned int i = 0, tes_ind = 0; + + if (tes == NULL) { + return -1; + } + + result = ustcmd_send_cmd("list_trace_events", pid, &big_str); + if (result != 1) { + ERR("error while getting trace_event list"); + return -1; + } + + tmp_tes = (struct trace_event_status *) + zmalloc(sizeof(struct trace_event_status) * + (ustcmd_count_nl(big_str) + 1)); + if (tmp_tes == NULL) { + ERR("Failed to allocate TES array"); + return -1; + } + + /* Parse received reply string (format: "[name]"): */ + while (big_str[i] != '\0') { + char state; + + sscanf(big_str + i, "trace_event: %a[^\n]", + &tmp_tes[tes_ind].name); + while (big_str[i] != '\n') { + ++i; /* Go to next '\n' */ + } + ++i; /* Skip current pointed '\n' */ + ++tes_ind; + } + tmp_tes[tes_ind].name = NULL; + + *tes = tmp_tes; + + free(big_str); + return 0; +} + /** * Set socket path * diff --git a/ustctl/ustctl.c b/ustctl/ustctl.c index 35bea7a..bf149d1 100644 --- a/ustctl/ustctl.c +++ b/ustctl/ustctl.c @@ -32,6 +32,7 @@ enum command { STOP_TRACE, DESTROY_TRACE, LIST_MARKERS, + LIST_TRACE_EVENTS, ENABLE_MARKER, DISABLE_MARKER, GET_ONLINE_PIDS, @@ -73,6 +74,7 @@ Commands:\n\ --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ + --list-trace-events\t\t\tList the trace-events of the process\n\ --force-switch\t\t\tForce a subbuffer switch\n\ \ "); @@ -94,6 +96,7 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts) { "stop-trace", 0, 0, STOP_TRACE }, { "destroy-trace", 0, 0, DESTROY_TRACE }, { "list-markers", 0, 0, LIST_MARKERS }, + { "list-trace-events", 0, 0, LIST_TRACE_EVENTS}, { "enable-marker", 1, 0, ENABLE_MARKER }, { "disable-marker", 1, 0, DISABLE_MARKER }, { "help", 0, 0, 'h' }, @@ -216,6 +219,8 @@ int main(int argc, char *argv[]) pidit = opts.pids; struct marker_status *cmsf = NULL; + struct trace_event_status *tes = NULL; + unsigned int i = 0; while(*pidit != -1) { switch (opts.cmd) { @@ -262,7 +267,7 @@ int main(int argc, char *argv[]) retval = EXIT_FAILURE; break; } - unsigned int i = 0; + i = 0; while (cmsf[i].channel != NULL) { printf("{PID: %u, channel/marker: %s/%s, " "state: %u, fmt: %s}\n", @@ -276,6 +281,24 @@ int main(int argc, char *argv[]) ustcmd_free_cmsf(cmsf); break; + case LIST_TRACE_EVENTS: + tes = NULL; + if (ustcmd_get_tes(&tes, *pidit)) { + ERR("error while trying to list " + "trace_events for PID %u\n", + (unsigned int) *pidit); + break; + } + i = 0; + while (tes[i].name != NULL) { + printf("{PID: %u, trace_event: %s}\n", + (unsigned int) *pidit, + tes[i].name); + ++i; + } + ustcmd_free_tes(tes); + + break; case ENABLE_MARKER: if (opts.regex) { if (ustcmd_set_marker_state(opts.regex, 1, *pidit)) { -- 2.34.1