From: Francis Deslauriers Date: Thu, 28 Jun 2018 19:43:50 +0000 (-0400) Subject: trace_kernel_create_event() now returns an error code X-Git-Tag: v2.11.0-rc1~79 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=71a3bb01e288ad6e611be0501a4444375c4124a7;p=lttng-tools.git trace_kernel_create_event() now returns an error code 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 Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng-sessiond/event.c b/src/bin/lttng-sessiond/event.c index ecb368fd5..a67941f09 100644 --- a/src/bin/lttng-sessiond/event.c +++ b/src/bin/lttng-sessiond/event.c @@ -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) { diff --git a/src/bin/lttng-sessiond/kernel.c b/src/bin/lttng-sessiond/kernel.c index fc817b2db..8a4652aec 100644 --- a/src/bin/lttng-sessiond/kernel.c +++ b/src/bin/lttng-sessiond/kernel.c @@ -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; diff --git a/src/bin/lttng-sessiond/trace-kernel.c b/src/bin/lttng-sessiond/trace-kernel.c index ef5abbab4..f7bb98197 100644 --- a/src/bin/lttng-sessiond/trace-kernel.c +++ b/src/bin/lttng-sessiond/trace-kernel.c @@ -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; } /* diff --git a/src/bin/lttng-sessiond/trace-kernel.h b/src/bin/lttng-sessiond/trace-kernel.h index 3210d4b33..64316d8b7 100644 --- a/src/bin/lttng-sessiond/trace-kernel.h +++ b/src/bin/lttng-sessiond/trace-kernel.h @@ -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); diff --git a/tests/unit/test_kernel_data.c b/tests/unit/test_kernel_data.c index ff1b75df4..412e4004f 100644 --- a/tests/unit/test_kernel_data.c +++ b/tests/unit/test_kernel_data.c @@ -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");