Add enable kernel channel support
authorDavid Goulet <david.goulet@polymtl.ca>
Wed, 29 Jun 2011 18:21:21 +0000 (14:21 -0400)
committerDavid Goulet <david.goulet@polymtl.ca>
Wed, 29 Jun 2011 18:37:43 +0000 (14:37 -0400)
Fix a null pointer dereference in the kernel session teardown.

Signed-off-by: David Goulet <david.goulet@polymtl.ca>
12 files changed:
liblttngctl/liblttngctl.c
liblttsessiondcomm/liblttsessiondcomm.c
liblttsessiondcomm/liblttsessiondcomm.h
ltt-sessiond/kernel-ctl.c
ltt-sessiond/kernel-ctl.h
ltt-sessiond/main.c
ltt-sessiond/trace.c
ltt-sessiond/trace.h
lttng/Makefile.am
lttng/cmd.h
lttng/commands/enable_channels.c [new file with mode: 0644]
lttng/lttng.c

index 7a480d2c711504c635d8200f39c971cfc33df4fb..7ddbc3099d102de20d83e87f0990c5ab65e830c5 100644 (file)
@@ -306,6 +306,7 @@ int lttng_kernel_disable_event(char *name, char *channel_name)
  */
 int lttng_kernel_enable_channel(char *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);
 }
 
        return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
 }
 
index 7dfdbb438d146da42a14683618e7236603e27efd..6f39388c78c70eb424c71058c3902eef7a2fbb1c 100644 (file)
@@ -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_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",
        [ 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",
index 8f7ce0317f68c28b99f654a5a43a53a623c5e028..df3cc6179d53042388ba14695d37289262b24fd8 100644 (file)
@@ -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_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 */
        LTTCOMM_KERN_ENABLE_FAIL,               /* Kernel enable event failed */
        LTTCOMM_KERN_DISABLE_FAIL,              /* Kernel disable event failed */
        LTTCOMM_KERN_META_FAIL,                 /* Kernel open metadata failed */
index 16b85bdb5bcb5b6793450b0f2794023541fa2466..bc92ffca918cb07b524aa8b9a6865308e455761b 100644 (file)
@@ -141,7 +141,6 @@ int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *chann
        }
 
        event->fd = ret;
        }
 
        event->fd = ret;
-       event->enabled = 1;
        /* Prevent fd duplication after execlp() */
        ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
        /* Prevent fd duplication after execlp() */
        ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
@@ -160,6 +159,31 @@ error:
        return -1;
 }
 
        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
  *
 /*
  *  kernel_enable_event
  *
@@ -181,7 +205,7 @@ int kernel_enable_event(struct ltt_kernel_event *event)
        return 0;
 
 error:
        return 0;
 
 error:
-       return -1;
+       return ret;
 }
 
 /*
 }
 
 /*
@@ -205,7 +229,7 @@ int kernel_disable_event(struct ltt_kernel_event *event)
        return 0;
 
 error:
        return 0;
 
 error:
-       return -1;
+       return ret;
 }
 
 /*
 }
 
 /*
index d36cd6d103fcea0b49dcea64e2ceb7985405a85a..82b680bd759908f73efad5a5fb66014ef847950b 100644 (file)
@@ -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_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);
 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);
index d9f7667b1df95497e8e76420cc35ab5f5bfb69ec..4fba6a2cf53a701054601e88df4478f35b658342 100644 (file)
@@ -941,6 +941,35 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                ret = LTTCOMM_OK;
                break;
        }
                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;
        case LTTNG_KERNEL_ENABLE_EVENT:
        {
                struct ltt_kernel_channel *chan;
index eb7d0e152497569634e5fbf094d2a8d7170e9cbe..1ab2857ce2a2328050d6fde0d8c70809d85ecbda 100644 (file)
@@ -139,6 +139,7 @@ struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *cha
 
        lkc->fd = 0;
        lkc->stream_count = 0;
 
        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);
        /* 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;
        /* Setting up a kernel event */
        lke->fd = 0;
        lke->event = attr;
+       lke->enabled = 1;
 
        return lke;
 
 
        return lke;
 
@@ -355,7 +357,9 @@ void trace_destroy_kernel_session(struct ltt_kernel_session *session)
                close(session->metadata_stream_fd);
        }
 
                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);
 
        cds_list_for_each_entry(channel, &session->channel_list.head, list) {
                trace_destroy_kernel_channel(channel);
index 427b05b8814be010bd72b2ce0caf15c753fa8f51..493a23636847a65fe3b1c06cedee5057ee1179a9 100644 (file)
@@ -52,6 +52,7 @@ struct ltt_kernel_event {
 /* Kernel channel */
 struct ltt_kernel_channel {
        int fd;
 /* Kernel channel */
 struct ltt_kernel_channel {
        int fd;
+       int enabled;
        char *pathname;
        unsigned int stream_count;
        struct lttng_channel *channel;
        char *pathname;
        unsigned int stream_count;
        struct lttng_channel *channel;
index a2afb9fad1242648b2bed58c129b5a3cc594ebeb..ef2ddc1e01a8488eca16c45bdc4b6e0830e841e4 100644 (file)
@@ -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 \
 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
 
 lttng_LDADD = \
                $(top_builddir)/liblttngctl/liblttngctl.la
index d932d2eb0fde28fc76112ee0d238f513fa631a43..7f54bafdafa1b70c63c505e6470bef4e64507f23 100644 (file)
@@ -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_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 */
 
 #endif /* _LTTNG_CMD_H */
diff --git a/lttng/commands/enable_channels.c b/lttng/commands/enable_channels.c
new file mode 100644 (file)
index 0000000..f8ad58d
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ *
+ * 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 <popt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#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;
+}
index 84d10eb7222b0e5c72f7db04e0d78cb5c6d275b8..e4578c70a3a60ad8eb8a2a2cdf510a5458e5e82b 100644 (file)
@@ -64,6 +64,7 @@ static struct cmd_struct commands[] =  {
        { "stop", cmd_stop},
        { "enable-event", cmd_enable_events},
        { "disable-event", cmd_disable_events},
        { "stop", cmd_stop},
        { "enable-event", cmd_enable_events},
        { "disable-event", cmd_disable_events},
+       { "enable-channel", cmd_enable_channels},
        { NULL, NULL}   /* Array closure */
 };
 
        { 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, "    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");
        fprintf(ofp, "    disable-event   Disable tracing event\n");
        fprintf(ofp, "    list            List possible tracing options\n");
        fprintf(ofp, "    start           Start tracing\n");
This page took 0.032139 seconds and 4 git commands to generate.