trace_kernel_create_event() now returns an error code
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 28 Jun 2018 19:43:50 +0000 (15:43 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 24 Aug 2018 19:58:07 +0000 (15:58 -0400)
The created kernel event is returned via an out parameter. The error
code specifies the reason of a potential failure of the creation of the
event.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-sessiond/event.c
src/bin/lttng-sessiond/kernel.c
src/bin/lttng-sessiond/trace-kernel.c
src/bin/lttng-sessiond/trace-kernel.h
tests/unit/test_kernel_data.c

index ecb368fd51ae5fd7971f7aa23d220918a07f9d2f..a67941f09757dc3a06d65eb30e7c9afc490834bd 100644 (file)
@@ -119,23 +119,11 @@ int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
        kevent = trace_kernel_find_event(event->name, kchan,
                        event->type, filter);
        if (kevent == NULL) {
-               ret = kernel_create_event(event, kchan,
-                       filter_expression, filter);
+               ret = kernel_create_event(event, kchan, filter_expression, filter);
                /* We have passed ownership */
                filter_expression = NULL;
                filter = NULL;
-               if (ret < 0) {
-                       switch (-ret) {
-                       case EEXIST:
-                               ret = LTTNG_ERR_KERN_EVENT_EXIST;
-                               break;
-                       case ENOSYS:
-                               ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
-                               break;
-                       default:
-                               ret = LTTNG_ERR_KERN_ENABLE_FAIL;
-                               break;
-                       }
+               if (ret) {
                        goto end;
                }
        } else if (kevent->enabled == 0) {
index fc817b2db304187675a38e1c5d8904658fe670a5..8a4652aecf4f548658f716d63aa0a71b0e1e3661 100644 (file)
@@ -201,60 +201,73 @@ int kernel_create_event(struct lttng_event *ev,
                char *filter_expression,
                struct lttng_filter_bytecode *filter)
 {
-       int ret;
+       int err, fd;
+       enum lttng_error_code ret;
        struct ltt_kernel_event *event;
 
        assert(ev);
        assert(channel);
 
        /* We pass ownership of filter_expression and filter */
-       event = trace_kernel_create_event(ev, filter_expression,
-                       filter);
-       if (event == NULL) {
-               ret = -1;
+       ret = trace_kernel_create_event(ev, filter_expression,
+                       filter, &event);
+       if (ret != LTTNG_OK) {
                goto error;
        }
 
-       ret = kernctl_create_event(channel->fd, event->event);
-       if (ret < 0) {
-               switch (-ret) {
+       fd = kernctl_create_event(channel->fd, event->event);
+       if (fd < 0) {
+               switch (-fd) {
                case EEXIST:
+                       ret = LTTNG_ERR_KERN_EVENT_EXIST;
                        break;
                case ENOSYS:
                        WARN("Event type not implemented");
+                       ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
                        break;
                case ENOENT:
                        WARN("Event %s not found!", ev->name);
+                       ret = LTTNG_ERR_KERN_ENABLE_FAIL;
                        break;
                default:
+                       ret = LTTNG_ERR_KERN_ENABLE_FAIL;
                        PERROR("create event ioctl");
                }
                goto free_event;
        }
 
        event->type = ev->type;
-       event->fd = ret;
+       event->fd = fd;
        /* Prevent fd duplication after execlp() */
-       ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
-       if (ret < 0) {
+       err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
+       if (err < 0) {
                PERROR("fcntl session fd");
        }
 
        if (filter) {
-               ret = kernctl_filter(event->fd, filter);
-               if (ret) {
+               err = kernctl_filter(event->fd, filter);
+               if (err < 0) {
+                       switch (-err) {
+                       case ENOMEM:
+                               ret = LTTNG_ERR_FILTER_NOMEM;
+                               break;
+                       default:
+                               ret = LTTNG_ERR_FILTER_INVAL;
+                               break;
+                       }
                        goto filter_error;
                }
        }
 
-       ret = kernctl_enable(event->fd);
-       if (ret < 0) {
-               switch (-ret) {
+       err = kernctl_enable(event->fd);
+       if (err < 0) {
+               switch (-err) {
                case EEXIST:
                        ret = LTTNG_ERR_KERN_EVENT_EXIST;
                        break;
                default:
                        PERROR("enable kernel event");
+                       ret = LTTNG_ERR_KERN_ENABLE_FAIL;
                        break;
                }
                goto enable_error;
index ef5abbab4c3b20fe8d39165012e27b756234a43e..f7bb9819746e31265ddd45dbc6a5e3e1eefb9a36 100644 (file)
@@ -292,18 +292,22 @@ error:
  *
  * Return pointer to structure or NULL.
  */
-struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
-               char *filter_expression, struct lttng_filter_bytecode *filter)
+enum lttng_error_code trace_kernel_create_event(
+               struct lttng_event *ev, char *filter_expression,
+               struct lttng_filter_bytecode *filter,
+               struct ltt_kernel_event **kernel_event)
 {
-       struct ltt_kernel_event *lke;
+       enum lttng_error_code ret;
        struct lttng_kernel_event *attr;
+       struct ltt_kernel_event *local_kernel_event;
 
        assert(ev);
 
-       lke = zmalloc(sizeof(struct ltt_kernel_event));
+       local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
        attr = zmalloc(sizeof(struct lttng_kernel_event));
-       if (lke == NULL || attr == NULL) {
+       if (local_kernel_event == NULL || attr == NULL) {
                PERROR("kernel event zmalloc");
+               ret = LTTNG_ERR_NOMEM;
                goto error;
        }
 
@@ -341,6 +345,7 @@ struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
                break;
        default:
                ERR("Unknown kernel instrumentation type (%d)", ev->type);
+               ret = LTTNG_ERR_INVALID;
                goto error;
        }
 
@@ -349,20 +354,22 @@ struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
        attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
 
        /* Setting up a kernel event */
-       lke->fd = -1;
-       lke->event = attr;
-       lke->enabled = 1;
-       lke->filter_expression = filter_expression;
-       lke->filter = filter;
+       local_kernel_event->fd = -1;
+       local_kernel_event->event = attr;
+       local_kernel_event->enabled = 1;
+       local_kernel_event->filter_expression = filter_expression;
+       local_kernel_event->filter = filter;
 
-       return lke;
+       *kernel_event = local_kernel_event;
+
+       return LTTNG_OK;
 
 error:
        free(filter_expression);
        free(filter);
-       free(lke);
+       free(local_kernel_event);
        free(attr);
-       return NULL;
+       return ret;
 }
 
 /*
index 3210d4b33bd58418f221257b9d39542479226ed3..64316d8b7b4ae9c26ddb9de388880565f686c9ff 100644 (file)
@@ -140,8 +140,9 @@ struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
 struct ltt_kernel_session *trace_kernel_create_session(void);
 struct ltt_kernel_channel *trace_kernel_create_channel(
                struct lttng_channel *chan);
-struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
-               char *filter_expression, struct lttng_filter_bytecode *filter);
+enum lttng_error_code trace_kernel_create_event(struct lttng_event *ev,
+               char *filter_expression, struct lttng_filter_bytecode *filter,
+               struct ltt_kernel_event **kernel_event);
 struct ltt_kernel_metadata *trace_kernel_create_metadata(void);
 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
                unsigned int count);
index ff1b75df4f3025f934398b6b9218dd4d34fda6cc..412e4004f9d09fcea583d2a79a5dba773f974bf7 100644 (file)
@@ -143,6 +143,7 @@ static void test_create_kernel_channel(void)
 
 static void test_create_kernel_event(void)
 {
+       enum lttng_error_code ret;
        struct ltt_kernel_event *event;
        struct lttng_event ev;
 
@@ -153,8 +154,8 @@ static void test_create_kernel_event(void)
        ev.type = LTTNG_EVENT_TRACEPOINT;
        ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
 
-       event = trace_kernel_create_event(&ev, NULL, NULL);
-       ok(event != NULL, "Create kernel event");
+       ret = trace_kernel_create_event(&ev, NULL, NULL, &event);
+       ok(ret == LTTNG_OK, "Create kernel event");
 
        if (!event) {
                skip(1, "Event is null");
This page took 0.031154 seconds and 4 git commands to generate.