From d36b858358a8ef4e00de843379d670925f9c23b6 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Wed, 29 Jun 2011 14:21:21 -0400 Subject: [PATCH] Add enable kernel channel support Fix a null pointer dereference in the kernel session teardown. Signed-off-by: David Goulet --- liblttngctl/liblttngctl.c | 1 + liblttsessiondcomm/liblttsessiondcomm.c | 1 + liblttsessiondcomm/liblttsessiondcomm.h | 1 + ltt-sessiond/kernel-ctl.c | 30 ++++- ltt-sessiond/kernel-ctl.h | 1 + ltt-sessiond/main.c | 29 +++++ ltt-sessiond/trace.c | 6 +- ltt-sessiond/trace.h | 1 + lttng/Makefile.am | 3 +- lttng/cmd.h | 1 + lttng/commands/enable_channels.c | 158 ++++++++++++++++++++++++ lttng/lttng.c | 2 + 12 files changed, 229 insertions(+), 5 deletions(-) create mode 100644 lttng/commands/enable_channels.c diff --git a/liblttngctl/liblttngctl.c b/liblttngctl/liblttngctl.c index 7a480d2c7..7ddbc3099 100644 --- a/liblttngctl/liblttngctl.c +++ b/liblttngctl/liblttngctl.c @@ -306,6 +306,7 @@ int lttng_kernel_disable_event(char *name, char *channel_name) */ int lttng_kernel_enable_channel(char *name) { + strncpy(lsm.u.enable.channel_name, name, NAME_MAX); return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL); } diff --git a/liblttsessiondcomm/liblttsessiondcomm.c b/liblttsessiondcomm/liblttsessiondcomm.c index 7dfdbb438..6f39388c7 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.c +++ b/liblttsessiondcomm/liblttsessiondcomm.c @@ -52,6 +52,7 @@ static const char *lttcomm_readable_code[] = { [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", + [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", diff --git a/liblttsessiondcomm/liblttsessiondcomm.h b/liblttsessiondcomm/liblttsessiondcomm.h index 8f7ce0317..df3cc6179 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.h +++ b/liblttsessiondcomm/liblttsessiondcomm.h @@ -87,6 +87,7 @@ enum lttcomm_return_code { LTTCOMM_KERN_SESS_FAIL, /* Kernel create session failed */ LTTCOMM_KERN_CHAN_FAIL, /* Kernel create channel failed */ LTTCOMM_KERN_CHAN_NOT_FOUND, /* Kernel channel not found */ + LTTCOMM_KERN_CHAN_ENABLE_FAIL, /* Kernel enable channel failed */ LTTCOMM_KERN_ENABLE_FAIL, /* Kernel enable event failed */ LTTCOMM_KERN_DISABLE_FAIL, /* Kernel disable event failed */ LTTCOMM_KERN_META_FAIL, /* Kernel open metadata failed */ diff --git a/ltt-sessiond/kernel-ctl.c b/ltt-sessiond/kernel-ctl.c index 16b85bdb5..bc92ffca9 100644 --- a/ltt-sessiond/kernel-ctl.c +++ b/ltt-sessiond/kernel-ctl.c @@ -141,7 +141,6 @@ int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *chann } event->fd = ret; - event->enabled = 1; /* Prevent fd duplication after execlp() */ ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { @@ -160,6 +159,31 @@ error: return -1; } +/* + * kernel_enable_channel + * + * Enable a kernel channel. + */ +int kernel_enable_channel(struct ltt_kernel_channel *chan) +{ + int ret; + + ret = kernctl_enable(chan->fd); + if (ret < 0) { + perror("enable chan ioctl"); + ret = errno; + goto error; + } + + chan->enabled = 1; + DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); + + return 0; + +error: + return ret; +} + /* * kernel_enable_event * @@ -181,7 +205,7 @@ int kernel_enable_event(struct ltt_kernel_event *event) return 0; error: - return -1; + return ret; } /* @@ -205,7 +229,7 @@ int kernel_disable_event(struct ltt_kernel_event *event) return 0; error: - return -1; + return ret; } /* diff --git a/ltt-sessiond/kernel-ctl.h b/ltt-sessiond/kernel-ctl.h index d36cd6d10..82b680bd7 100644 --- a/ltt-sessiond/kernel-ctl.h +++ b/ltt-sessiond/kernel-ctl.h @@ -35,6 +35,7 @@ int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_chann int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel); int kernel_disable_event(struct ltt_kernel_event *event); int kernel_enable_event(struct ltt_kernel_event *event); +int kernel_enable_channel(struct ltt_kernel_channel *chan); int kernel_open_metadata(struct ltt_kernel_session *session); int kernel_open_metadata_stream(struct ltt_kernel_session *session); int kernel_open_channel_stream(struct ltt_kernel_channel *channel); diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index d9f7667b1..4fba6a2cf 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -941,6 +941,35 @@ static int process_client_msg(struct command_ctx *cmd_ctx) ret = LTTCOMM_OK; break; } + case LTTNG_KERNEL_ENABLE_CHANNEL: + { + struct ltt_kernel_channel *chan; + + /* Setup lttng message with no payload */ + ret = setup_lttng_msg(cmd_ctx, 0); + if (ret < 0) { + goto setup_error; + } + + chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, + cmd_ctx->session->kernel_session); + if (chan == NULL) { + ret = LTTCOMM_KERN_CHAN_NOT_FOUND; + goto error; + } else if (chan->enabled == 0) { + ret = kernel_enable_channel(chan); + if (ret < 0) { + if (ret != EEXIST) { + ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; + } + goto error; + } + } + + kernel_wait_quiescent(kernel_tracer_fd); + ret = LTTCOMM_OK; + break; + } case LTTNG_KERNEL_ENABLE_EVENT: { struct ltt_kernel_channel *chan; diff --git a/ltt-sessiond/trace.c b/ltt-sessiond/trace.c index eb7d0e152..1ab2857ce 100644 --- a/ltt-sessiond/trace.c +++ b/ltt-sessiond/trace.c @@ -139,6 +139,7 @@ struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *cha lkc->fd = 0; lkc->stream_count = 0; + lkc->enabled = 1; /* Init linked list */ CDS_INIT_LIST_HEAD(&lkc->events_list.head); CDS_INIT_LIST_HEAD(&lkc->stream_list.head); @@ -201,6 +202,7 @@ struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev) /* Setting up a kernel event */ lke->fd = 0; lke->event = attr; + lke->enabled = 1; return lke; @@ -355,7 +357,9 @@ void trace_destroy_kernel_session(struct ltt_kernel_session *session) close(session->metadata_stream_fd); } - trace_destroy_kernel_metadata(session->metadata); + if (session->metadata != NULL) { + trace_destroy_kernel_metadata(session->metadata); + } cds_list_for_each_entry(channel, &session->channel_list.head, list) { trace_destroy_kernel_channel(channel); diff --git a/ltt-sessiond/trace.h b/ltt-sessiond/trace.h index 427b05b88..493a23636 100644 --- a/ltt-sessiond/trace.h +++ b/ltt-sessiond/trace.h @@ -52,6 +52,7 @@ struct ltt_kernel_event { /* Kernel channel */ struct ltt_kernel_channel { int fd; + int enabled; char *pathname; unsigned int stream_count; struct lttng_channel *channel; diff --git a/lttng/Makefile.am b/lttng/Makefile.am index a2afb9fad..ef2ddc1e0 100644 --- a/lttng/Makefile.am +++ b/lttng/Makefile.am @@ -5,7 +5,8 @@ bin_PROGRAMS = lttng lttng_SOURCES = conf.c commands/start.c commands/add_channel.c \ commands/list.c commands/create.c commands/destroy.c \ commands/stop.c commands/enable_events.c \ - commands/disable_events.c utils.c lttng.c + commands/disable_events.c commands/enable_channels.c \ + utils.c lttng.c lttng_LDADD = \ $(top_builddir)/liblttngctl/liblttngctl.la diff --git a/lttng/cmd.h b/lttng/cmd.h index d932d2eb0..7f54bafda 100644 --- a/lttng/cmd.h +++ b/lttng/cmd.h @@ -45,5 +45,6 @@ extern int cmd_start(int argc, const char **argv); extern int cmd_stop(int argc, const char **argv); extern int cmd_enable_events(int argc, const char **argv); extern int cmd_disable_events(int argc, const char **argv); +extern int cmd_enable_channels(int argc, const char **argv); #endif /* _LTTNG_CMD_H */ diff --git a/lttng/commands/enable_channels.c b/lttng/commands/enable_channels.c new file mode 100644 index 000000000..f8ad58d54 --- /dev/null +++ b/lttng/commands/enable_channels.c @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "cmd.h" +#include "conf.h" +#include "utils.h" + +static char *opt_channels; +static char *opt_kernel; +static int opt_pid_all; +static int opt_userspace; +static pid_t opt_pid; + +enum { + OPT_HELP = 1, + OPT_USERSPACE, +}; + +static struct poptOption long_options[] = { + /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ + {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, + {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, + {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0}, + {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0} +}; + +/* + * usage + */ +static void usage(FILE *ofp) +{ + fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options]\n"); + fprintf(ofp, "\n"); + fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); + fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n"); + fprintf(ofp, " --all If -u, apply on all traceable apps\n"); + fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n"); + fprintf(ofp, "\n"); +} + +/* + * enable_channels + * + * Enabling channel using the lttng API. + */ +static int enable_channels(void) +{ + int ret = CMD_SUCCESS; + char *channel_name; + + if (set_session_name() < 0) { + ret = CMD_ERROR; + goto error; + } + + /* Strip event list */ + channel_name = strtok(opt_channels, ","); + while (channel_name != NULL) { + /* Kernel tracer action */ + if (opt_kernel) { + DBG("Enabling kernel channel %s", channel_name); + ret = lttng_kernel_enable_channel(channel_name); + if (ret < 0) { + goto error; + } else { + MSG("Kernel channel enabled %s", channel_name); + } + } else if (opt_userspace) { /* User-space tracer action */ + /* + * TODO: Waiting on lttng UST 2.0 + */ + if (opt_pid_all) { + } else if (opt_pid != 0) { + } + ret = CMD_NOT_IMPLEMENTED; + goto error; + } else { + ERR("Please specify a tracer (kernel or user-space)"); + goto error; + } + + /* Next event */ + channel_name = strtok(NULL, ","); + } + +error: + return ret; +} + +/* + * cmd_enable_channels + * + * Enable channel to trace session + */ +int cmd_enable_channels(int argc, const char **argv) +{ + int opt, ret; + static poptContext pc; + + pc = poptGetContext(NULL, argc, argv, long_options, 0); + poptReadDefaultConfig(pc, 0); + + while ((opt = poptGetNextOpt(pc)) != -1) { + switch (opt) { + case OPT_HELP: + usage(stderr); + ret = CMD_SUCCESS; + goto end; + case OPT_USERSPACE: + opt_userspace = 1; + break; + default: + usage(stderr); + ret = CMD_UNDEFINED; + goto end; + } + } + + opt_channels = (char*) poptGetArg(pc); + if (opt_channels == NULL) { + ERR("Missing channel name(s).\n"); + usage(stderr); + ret = CMD_SUCCESS; + goto end; + } + + ret = enable_channels(); + +end: + return ret; +} diff --git a/lttng/lttng.c b/lttng/lttng.c index 84d10eb72..e4578c70a 100644 --- a/lttng/lttng.c +++ b/lttng/lttng.c @@ -64,6 +64,7 @@ static struct cmd_struct commands[] = { { "stop", cmd_stop}, { "enable-event", cmd_enable_events}, { "disable-event", cmd_disable_events}, + { "enable-channel", cmd_enable_channels}, { NULL, NULL} /* Array closure */ }; @@ -85,6 +86,7 @@ static void usage(FILE *ofp) fprintf(ofp, " create Create tracing session\n"); fprintf(ofp, " destroy Teardown tracing session\n"); fprintf(ofp, " enable-event Enable tracing event\n"); + fprintf(ofp, " enable-channel Enable tracing channel\n"); fprintf(ofp, " disable-event Disable tracing event\n"); fprintf(ofp, " list List possible tracing options\n"); fprintf(ofp, " start Start tracing\n"); -- 2.34.1