lttng-ctl: Add error query interface
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index 38df4cb7b11c1234c5b33f8b92d1731ea541585f..7debaed6b813d412404db52c277622cbd1be7154 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
+#include <common/bytecode/bytecode.h>
 #include <common/common.h>
 #include <common/compat/errno.h>
 #include <common/compat/string.h>
 #include <common/defaults.h>
-#include <common/dynamic-buffer.h>
 #include <common/dynamic-array.h>
-#include <common/payload.h>
+#include <common/dynamic-buffer.h>
 #include <common/payload-view.h>
+#include <common/payload.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/tracker.h>
 #include <common/unix.h>
 #include <lttng/channel-internal.h>
 #include <lttng/destruction-handle.h>
 #include <lttng/endpoint.h>
+#include <lttng/error-query-internal.h>
 #include <lttng/event-internal.h>
 #include <lttng/health-internal.h>
+#include <lttng/lttng-error.h>
 #include <lttng/lttng.h>
 #include <lttng/session-descriptor-internal.h>
 #include <lttng/session-internal.h>
 #include <lttng/trigger/trigger-internal.h>
 #include <lttng/userspace-probe-internal.h>
-#include <lttng/lttng-error.h>
 
+#include "lttng-ctl-helper.h"
 #include <common/filter/filter-ast.h>
 #include <common/filter/filter-parser.h>
-#include <common/filter/filter-bytecode.h>
 #include <common/filter/memstream.h>
-#include "lttng-ctl-helper.h"
 
 #define COPY_DOMAIN_PACKED(dst, src)                           \
 do {                                                           \
@@ -116,7 +117,8 @@ static int send_session_msg(struct lttcomm_session_msg *lsm)
                goto end;
        }
 
-       DBG("LSM cmd type : %d", lsm->cmd_type);
+       DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm->cmd_type),
+                       lsm->cmd_type);
 
        ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
                        sizeof(struct lttcomm_session_msg));
@@ -652,7 +654,12 @@ int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
 
        /* Check error code if OK */
        if (llm.ret_code != LTTNG_OK) {
-               ret = -llm.ret_code;
+               if (llm.ret_code < LTTNG_OK || llm.ret_code >= LTTNG_ERR_NR) {
+                       /* Invalid error code received. */
+                       ret = -LTTNG_ERR_UNK;
+               } else {
+                       ret = -llm.ret_code;
+               }
                goto end;
        }
 
@@ -1264,15 +1271,15 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
 
                assert(fd_count == 0 || fd_count == 1);
                if (fd_count == 1) {
-                       struct fd_handle *handle =
+                       struct fd_handle *h =
                                        lttng_payload_view_pop_fd_handle(&view);
 
-                       if (!handle) {
+                       if (!h) {
                                goto mem_error;
                        }
 
-                       fd_to_send = fd_handle_get_fd(handle);
-                       fd_handle_put(handle);
+                       fd_to_send = fd_handle_get_fd(h);
+                       fd_handle_put(h);
                }
 
                ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
@@ -2064,7 +2071,7 @@ int lttng_destroy_session_no_wait(const char *session_name)
        enum lttng_error_code ret_code;
 
        ret_code = lttng_destroy_session_ext(session_name, NULL);
-       return ret_code == LTTNG_OK ? ret_code : -ret_code;
+       return ret_code == LTTNG_OK ? 0 : -ret_code;
 }
 
 /*
@@ -3091,7 +3098,6 @@ int lttng_register_trigger(struct lttng_trigger *trigger)
                .gid = LTTNG_OPTIONAL_INIT_UNSET,
        };
 
-
        lttng_payload_init(&message);
        lttng_payload_init(&reply);
 
@@ -3141,18 +3147,18 @@ int lttng_register_trigger(struct lttng_trigger *trigger)
                goto end;
        }
 
-       /*
-        * This is needed to populate the trigger object size for the command
-        * header.
-       */
-       message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
-
        ret = lttng_trigger_serialize(trigger, &message);
        if (ret < 0) {
                ret = -LTTNG_ERR_UNK;
                goto end;
        }
 
+       /*
+        * This is needed to populate the trigger object size for the command
+        * header.
+        */
+       message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
+
        message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
 
        {
@@ -3195,13 +3201,92 @@ end:
        return ret;
 }
 
-int lttng_unregister_trigger(struct lttng_trigger *trigger)
+enum lttng_error_code lttng_error_query_execute(
+               const struct lttng_error_query *query,
+               const struct lttng_endpoint *endpoint,
+               struct lttng_error_query_results **results)
+{
+       int ret;
+       enum lttng_error_code ret_code;
+       struct lttcomm_session_msg lsm = {
+               .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
+       };
+       struct lttng_payload message;
+       struct lttng_payload reply;
+       struct lttcomm_session_msg *message_lsm;
+
+       lttng_payload_init(&message);
+       lttng_payload_init(&reply);
+
+       if (!query || !results) {
+               ret_code = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (endpoint != lttng_session_daemon_command_endpoint) {
+               ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
+               goto end;
+       }
+
+       ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
+       if (ret) {
+               ret_code = LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       ret = lttng_error_query_serialize(query, &message);
+       if (ret) {
+               ret_code = LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
+       message_lsm->u.error_query.length =
+                       (uint32_t) message.buffer.size - sizeof(lsm);
+
+       {
+               struct lttng_payload_view message_view =
+                               lttng_payload_view_from_payload(
+                                               &message, 0, -1);
+
+               message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
+                               &message_view);
+               ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
+               if (ret < 0) {
+                       ret_code = -ret;
+                       goto end;
+               }
+       }
+
+       {
+               ssize_t reply_create_ret;
+               struct lttng_payload_view reply_view =
+                               lttng_payload_view_from_payload(
+                                               &reply, 0, reply.buffer.size);
+
+               reply_create_ret = lttng_error_query_results_create_from_payload(
+                               &reply_view, results);
+               if (reply_create_ret < 0) {
+                       ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+                       goto end;
+               }
+       }
+
+       ret_code = LTTNG_OK;
+end:
+       lttng_payload_reset(&message);
+       lttng_payload_reset(&reply);
+       return ret_code;
+}
+
+int lttng_unregister_trigger(const struct lttng_trigger *trigger)
 {
        int ret;
        struct lttcomm_session_msg lsm;
        struct lttcomm_session_msg *message_lsm;
        struct lttng_payload message;
        struct lttng_payload reply;
+       struct lttng_trigger *copy = NULL;
        const struct lttng_credentials user_creds = {
                .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
                .gid = LTTNG_OPTIONAL_INIT_UNSET,
@@ -3215,9 +3300,15 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger)
                goto end;
        }
 
-       if (!trigger->creds.uid.is_set) {
-               /* Use the client's credentials as the trigger credentials. */
-               lttng_trigger_set_credentials(trigger, &user_creds);
+       copy = lttng_trigger_copy(trigger);
+       if (!copy) {
+               ret = -LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       if (!copy->creds.uid.is_set) {
+               /* Use the client credentials as the trigger credentials */
+               lttng_trigger_set_credentials(copy, &user_creds);
        } else {
                /*
                 * Validate that either the current trigger credentials and the
@@ -3230,8 +3321,7 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger)
                 * "safety" checks.
                 */
                const struct lttng_credentials *trigger_creds =
-                               lttng_trigger_get_credentials(trigger);
-
+                               lttng_trigger_get_credentials(copy);
                if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
                        if (lttng_credentials_get_uid(&user_creds) != 0) {
                                ret = -LTTNG_ERR_EPERM;
@@ -3240,7 +3330,7 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger)
                }
        }
 
-       if (!lttng_trigger_validate(trigger)) {
+       if (!lttng_trigger_validate(copy)) {
                ret = -LTTNG_ERR_INVALID_TRIGGER;
                goto end;
        }
@@ -3260,7 +3350,7 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger)
        */
        message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
 
-       ret = lttng_trigger_serialize(trigger, &message);
+       ret = lttng_trigger_serialize(copy, &message);
        if (ret < 0) {
                ret = -LTTNG_ERR_UNK;
                goto end;
@@ -3288,6 +3378,7 @@ int lttng_unregister_trigger(struct lttng_trigger *trigger)
 
        ret = 0;
 end:
+       lttng_trigger_destroy(copy);
        lttng_payload_reset(&message);
        lttng_payload_reset(&reply);
        return ret;
This page took 0.027474 seconds and 4 git commands to generate.