Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / config / session-config.c
index c7f471a1c2162c3c40d9d8a35ce1d0d348a5a0f0..416f9046ebb702cb4ac0fb3f87633293ecb378b7 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "lttng/tracker.h"
 #define _LGPL_SOURCE
-#include <assert.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -154,6 +153,13 @@ LTTNG_HIDDEN const char * const config_element_process_attr_gid_value = "gid";
 LTTNG_HIDDEN const char * const config_element_process_attr_vgid_value = "vgid";
 LTTNG_HIDDEN const char * const config_element_process_attr_tracker_type = "process_attr_tracker_type";
 
+/* Used for support of legacy tracker serialization (< 2.12). */
+LTTNG_HIDDEN const char * const config_element_trackers_legacy = "trackers";
+LTTNG_HIDDEN const char * const config_element_pid_tracker_legacy = "pid_tracker";
+LTTNG_HIDDEN const char * const config_element_tracker_targets_legacy = "targets";
+LTTNG_HIDDEN const char * const config_element_tracker_pid_target_legacy = "pid_target";
+LTTNG_HIDDEN const char * const config_element_tracker_pid_legacy = "pid";
+
 LTTNG_HIDDEN const char * const config_element_rotation_schedules = "rotation_schedules";
 LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic = "periodic";
 LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic_time_us = "time_us";
@@ -216,6 +222,7 @@ LTTNG_HIDDEN const char * const config_event_context_ipc_ns = "IPC_NS";
 LTTNG_HIDDEN const char * const config_event_context_mnt_ns = "MNT_NS";
 LTTNG_HIDDEN const char * const config_event_context_net_ns = "NET_NS";
 LTTNG_HIDDEN const char * const config_event_context_pid_ns = "PID_NS";
+LTTNG_HIDDEN const char * const config_event_context_time_ns = "TIME_NS";
 LTTNG_HIDDEN const char * const config_event_context_user_ns = "USER_NS";
 LTTNG_HIDDEN const char * const config_event_context_uts_ns = "UTS_NS";
 LTTNG_HIDDEN const char * const config_event_context_uid = "UID";
@@ -252,7 +259,7 @@ static int config_entry_handler_filter(struct handler_filter_args *args,
        int ret = 0;
        struct config_entry entry = { section, name, value };
 
-       assert(args);
+       LTTNG_ASSERT(args);
 
        if (!section || !name || !value) {
                ret = -EIO;
@@ -402,7 +409,7 @@ static xmlChar *encode_string(const char *in_str)
        xmlCharEncodingHandlerPtr handler;
        int out_len, ret, in_len;
 
-       assert(in_str);
+       LTTNG_ASSERT(in_str);
 
        handler = xmlFindCharEncodingHandler(config_xml_encoding);
        if (!handler) {
@@ -631,6 +638,32 @@ int config_writer_write_element_bool(struct config_writer *writer,
                value ? config_xml_true : config_xml_false);
 }
 
+LTTNG_HIDDEN
+int config_writer_write_element_double(struct config_writer *writer,
+               const char *element_name,
+               double value)
+{
+       int ret;
+       xmlChar *encoded_element_name;
+
+       if (!writer || !writer->writer || !element_name || !element_name[0]) {
+               ret = -1;
+               goto end;
+       }
+
+       encoded_element_name = encode_string(element_name);
+       if (!encoded_element_name) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = xmlTextWriterWriteFormatElement(
+                       writer->writer, encoded_element_name, "%f", value);
+       xmlFree(encoded_element_name);
+end:
+       return ret >= 0 ? 0 : ret;
+}
+
 LTTNG_HIDDEN
 int config_writer_write_element_string(struct config_writer *writer,
                const char *element_name, const char *value)
@@ -704,7 +737,7 @@ void fini_session_config_validation_ctx(
 }
 
 static
-char *get_session_config_xsd_path()
+char *get_session_config_xsd_path(void)
 {
        char *xsd_path;
        const char *base_path = lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
@@ -835,12 +868,14 @@ int parse_bool(xmlChar *str, int *val)
                goto end;
        }
 
-       if (!strcmp((const char *) str, config_xml_true)) {
+       if (!strcmp((const char *) str, config_xml_true) ||
+                       !strcmp((const char *) str, "1")) {
                *val = 1;
-       } else if (!strcmp((const char *) str, config_xml_false)) {
+       } else if (!strcmp((const char *) str, config_xml_false) ||
+                       !strcmp((const char *) str, "0")) {
                *val = 0;
        } else {
-               WARN("Invalid boolean value encoutered (%s).",
+               WARN("Invalid boolean value encountered (%s).",
                        (const char *) str);
                ret = -1;
        }
@@ -1087,6 +1122,9 @@ int get_context_type(xmlChar *context_type)
        } else if (!strcmp((char *) context_type,
                config_event_context_pid_ns)) {
                ret = LTTNG_EVENT_CONTEXT_PID_NS;
+       } else if (!strcmp((char *) context_type,
+               config_event_context_time_ns)) {
+               ret = LTTNG_EVENT_CONTEXT_TIME_NS;
        } else if (!strcmp((char *) context_type,
                config_event_context_user_ns)) {
                ret = LTTNG_EVENT_CONTEXT_USER_NS;
@@ -1219,7 +1257,7 @@ int process_consumer_output(xmlNodePtr consumer_output_node,
        int ret;
        xmlNodePtr node;
 
-       assert(output);
+       LTTNG_ASSERT(output);
 
        for (node = xmlFirstElementChild(consumer_output_node); node;
                        node = xmlNextElementSibling(node)) {
@@ -1285,7 +1323,7 @@ int create_session_net_output(const char *name, const char *control_uri,
        struct lttng_handle *handle;
        const char *uri = NULL;
 
-       assert(name);
+       LTTNG_ASSERT(name);
 
        handle = lttng_create_handle(name, NULL);
        if (!handle) {
@@ -1314,7 +1352,7 @@ int create_snapshot_session(const char *session_name, xmlNodePtr output_node,
        xmlNodePtr snapshot_output_list_node;
        xmlNodePtr snapshot_output_node;
 
-       assert(session_name);
+       LTTNG_ASSERT(session_name);
 
        ret = lttng_create_session_snapshot(session_name, NULL);
        if (ret) {
@@ -1467,7 +1505,7 @@ int create_session(const char *name,
        const char *data_uri = NULL;
        const char *path = NULL;
 
-       assert(name);
+       LTTNG_ASSERT(name);
 
        if (output_node) {
                consumer_output_node = xmlFirstElementChild(output_node);
@@ -1733,8 +1771,8 @@ int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
 {
        int ret;
 
-       assert(probe_attribute_node);
-       assert(attr);
+       LTTNG_ASSERT(probe_attribute_node);
+       LTTNG_ASSERT(attr);
 
        if (!strcmp((const char *) probe_attribute_node->name,
                config_element_address)) {
@@ -1817,9 +1855,9 @@ int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
        unsigned long exclusion_count = 0;
        char *filter_expression = NULL;
 
-       assert(event_node);
-       assert(handle);
-       assert(channel_name);
+       LTTNG_ASSERT(event_node);
+       LTTNG_ASSERT(handle);
+       LTTNG_ASSERT(channel_name);
 
        event = lttng_event_create();
        if (!event) {
@@ -1844,7 +1882,7 @@ int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
                event->loglevel = LTTNG_LOGLEVEL_DEBUG;
                break;
        default:
-               assert(0);
+               abort();
        }
 
        for (node = xmlFirstElementChild(event_node); node;
@@ -2149,9 +2187,9 @@ int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
        struct lttng_event event;
        xmlNodePtr node;
 
-       assert(events_node);
-       assert(handle);
-       assert(channel_name);
+       LTTNG_ASSERT(events_node);
+       LTTNG_ASSERT(handle);
+       LTTNG_ASSERT(channel_name);
 
        for (node = xmlFirstElementChild(events_node); node;
                node = xmlNextElementSibling(node)) {
@@ -2193,10 +2231,10 @@ int process_channel_attr_node(xmlNodePtr attr_node,
 {
        int ret;
 
-       assert(attr_node);
-       assert(channel);
-       assert(contexts_node);
-       assert(events_node);
+       LTTNG_ASSERT(attr_node);
+       LTTNG_ASSERT(channel);
+       LTTNG_ASSERT(contexts_node);
+       LTTNG_ASSERT(events_node);
 
        if (!strcmp((const char *) attr_node->name, config_element_name)) {
                xmlChar *content;
@@ -2499,8 +2537,8 @@ int process_context_node(xmlNodePtr context_node,
        struct lttng_event_context context;
        xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
 
-       assert(handle);
-       assert(channel_name);
+       LTTNG_ASSERT(handle);
+       LTTNG_ASSERT(channel_name);
 
        if (!context_child_node) {
                ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
@@ -2725,12 +2763,143 @@ static int get_tracker_elements(enum lttng_process_attr process_attr,
        return ret;
 }
 
+static int process_legacy_pid_tracker_node(
+               xmlNodePtr trackers_node, struct lttng_handle *handle)
+{
+       int ret = 0, child_count;
+       xmlNodePtr targets_node = NULL;
+       xmlNodePtr node;
+       const char *element_id_tracker;
+       const char *element_target_id;
+       const char *element_id;
+       const char *element_id_alias;
+       const char *element_name;
+       enum lttng_error_code tracker_handle_ret_code;
+       struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
+       enum lttng_process_attr_tracker_handle_status status;
+       const enum lttng_process_attr process_attr =
+                       handle->domain.type == LTTNG_DOMAIN_UST ?
+                                       LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID :
+                                       LTTNG_PROCESS_ATTR_PROCESS_ID;
+
+       LTTNG_ASSERT(handle);
+
+       tracker_handle_ret_code = lttng_session_get_tracker_handle(
+                       handle->session_name, handle->domain.type,
+                       process_attr,
+                       &tracker_handle);
+       if (tracker_handle_ret_code != LTTNG_OK) {
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ret = get_tracker_elements(process_attr, &element_id_tracker,
+                       &element_target_id, &element_id, &element_id_alias,
+                       &element_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Get the targets node */
+       for (node = xmlFirstElementChild(trackers_node); node;
+                       node = xmlNextElementSibling(node)) {
+               if (!strcmp((const char *) node->name,
+                               config_element_tracker_targets_legacy)) {
+                       targets_node = node;
+                       break;
+               }
+       }       
+
+       if (!targets_node) {
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /* Go through all id target node */
+       child_count = xmlChildElementCount(targets_node);
+       status = lttng_process_attr_tracker_handle_set_tracking_policy(
+                       tracker_handle,
+                       child_count == 0 ? LTTNG_TRACKING_POLICY_EXCLUDE_ALL :
+                                          LTTNG_TRACKING_POLICY_INCLUDE_SET);
+       if (status != LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK) {
+               ret = LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       /* Add all tracked values. */
+       for (node = xmlFirstElementChild(targets_node); node;
+                       node = xmlNextElementSibling(node)) {
+               xmlNodePtr pid_target_node = node;
+
+               /* get pid_target node and track it */
+               for (node = xmlFirstElementChild(pid_target_node); node;
+                               node = xmlNextElementSibling(node)) {
+                       if (!strcmp((const char *) node->name,
+                                           config_element_tracker_pid_legacy)) {
+                               int64_t id;
+                               xmlChar *content = xmlNodeGetContent(node);
+
+                               if (!content) {
+                                       ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
+                                       goto end;
+                               }
+
+                               ret = parse_int(content, &id);
+                               free(content);
+                               if (ret) {
+                                       ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
+                                       goto end;
+                               }
+
+                               switch (process_attr) {
+                               case LTTNG_PROCESS_ATTR_PROCESS_ID:
+                                       status = lttng_process_attr_process_id_tracker_handle_add_pid(
+                                                       tracker_handle,
+                                                       (pid_t) id);
+                                       break;
+                               case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
+                                       status = lttng_process_attr_virtual_process_id_tracker_handle_add_pid(
+                                                       tracker_handle,
+                                                       (pid_t) id);
+                                       break;
+                               default:
+                                       ret = LTTNG_ERR_INVALID;
+                                       goto end;
+                               }
+                       }
+                       switch (status) {
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK:
+                               continue;
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID:
+                               ret = LTTNG_ERR_INVALID;
+                               break;
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS:
+                               ret = LTTNG_ERR_PROCESS_ATTR_EXISTS;
+                               break;
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING:
+                               ret = LTTNG_ERR_PROCESS_ATTR_MISSING;
+                               break;
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR:
+                       case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR:
+                       default:
+                               ret = LTTNG_ERR_UNK;
+                               goto end;
+                       }
+               }
+               node = pid_target_node;
+       }
+
+end:
+       lttng_process_attr_tracker_handle_destroy(tracker_handle);
+       return ret;
+ }
+
 static int process_id_tracker_node(xmlNodePtr id_tracker_node,
                struct lttng_handle *handle,
                enum lttng_process_attr process_attr)
 {
        int ret = 0, child_count;
-       xmlNodePtr targets_node = NULL;
+       xmlNodePtr values_node = NULL;
        xmlNodePtr node;
        const char *element_id_tracker;
        const char *element_target_id;
@@ -2741,8 +2910,8 @@ static int process_id_tracker_node(xmlNodePtr id_tracker_node,
        struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
        enum lttng_process_attr_tracker_handle_status status;
 
-       assert(handle);
-       assert(id_tracker_node);
+       LTTNG_ASSERT(handle);
+       LTTNG_ASSERT(id_tracker_node);
 
        tracker_handle_ret_code = lttng_session_get_tracker_handle(
                        handle->session_name, handle->domain.type, process_attr,
@@ -2759,23 +2928,23 @@ static int process_id_tracker_node(xmlNodePtr id_tracker_node,
                goto end;
        }
 
-       /* get the targets node */
+       /* get the values node */
        for (node = xmlFirstElementChild(id_tracker_node); node;
                        node = xmlNextElementSibling(node)) {
                if (!strcmp((const char *) node->name,
                                config_element_process_attr_values)) {
-                       targets_node = node;
+                       values_node = node;
                        break;
                }
        }
 
-       if (!targets_node) {
+       if (!values_node) {
                ret = LTTNG_ERR_INVALID;
                goto end;
        }
 
        /* Go through all id target node */
-       child_count = xmlChildElementCount(targets_node);
+       child_count = xmlChildElementCount(values_node);
        status = lttng_process_attr_tracker_handle_set_tracking_policy(
                        tracker_handle,
                        child_count == 0 ? LTTNG_TRACKING_POLICY_EXCLUDE_ALL :
@@ -2786,7 +2955,7 @@ static int process_id_tracker_node(xmlNodePtr id_tracker_node,
        }
 
        /* Add all tracked values. */
-       for (node = xmlFirstElementChild(targets_node); node;
+       for (node = xmlFirstElementChild(values_node); node;
                        node = xmlNextElementSibling(node)) {
                xmlNodePtr id_target_node = node;
 
@@ -2929,7 +3098,7 @@ int process_domain_node(xmlNodePtr domain_node, const char *session_name)
        xmlNodePtr vgid_tracker_node = NULL;
        xmlNodePtr node;
 
-       assert(session_name);
+       LTTNG_ASSERT(session_name);
 
        ret = init_domain(domain_node, &domain);
        if (ret) {
@@ -3027,7 +3196,16 @@ int process_domain_node(xmlNodePtr domain_node, const char *session_name)
        for (node = xmlFirstElementChild(domain_node); node;
                        node = xmlNextElementSibling(node)) {
                if (!strcmp((const char *) node->name,
-                               config_element_process_attr_trackers)) {
+                                   config_element_process_attr_trackers) ||
+                               !strcmp((const char *) node->name,
+                                               config_element_trackers_legacy)) {
+                       if (trackers_node) {
+                               ERR("Only one instance of `%s` or `%s` is allowed in a session configuration",
+                                               config_element_process_attr_trackers,
+                                               config_element_trackers_legacy);
+                               ret = -1;
+                               goto end;
+                       }
                        trackers_node = node;
                        break;
                }
@@ -3093,6 +3271,13 @@ int process_domain_node(xmlNodePtr domain_node, const char *session_name)
                                goto end;
                        }
                }
+               if (!strcmp((const char *) node->name,
+                                   config_element_pid_tracker_legacy)) {
+                       ret = process_legacy_pid_tracker_node(node, handle);
+                       if (ret) {
+                               goto end;
+                       }
+               }
        }
 
 end:
@@ -3549,7 +3734,7 @@ static int validate_file_read_creds(const char *path)
 {
        int ret;
 
-       assert(path);
+       LTTNG_ASSERT(path);
 
        /* Can we read the file. */
        ret = access(path, R_OK);
@@ -3576,8 +3761,8 @@ int load_session_from_file(const char *path, const char *session_name,
        xmlNodePtr sessions_node;
        xmlNodePtr session_node;
 
-       assert(path);
-       assert(validation_ctx);
+       LTTNG_ASSERT(path);
+       LTTNG_ASSERT(validation_ctx);
 
        ret = validate_file_read_creds(path);
        if (ret != 1) {
@@ -3613,10 +3798,24 @@ int load_session_from_file(const char *path, const char *session_name,
                        xmlNextElementSibling(session_node)) {
                ret = process_session_node(session_node,
                        session_name, overwrite, overrides);
-               if (session_name && ret == 0) {
-                       /* Target session found and loaded */
-                       session_found = 1;
-                       break;
+               if (!session_name && ret) {
+                       /* Loading error occurred. */
+                       goto end;
+               } else if (session_name) {
+                       if (ret == 0) {
+                               /* Target session found and loaded */
+                               session_found = 1;
+                               break;
+                       } else if (ret == -LTTNG_ERR_NO_SESSION) {
+                               /*
+                                * Ignore this error, we are looking for a
+                                * specific session.
+                                */
+                               ret = 0;
+                       } else {
+                               /* Loading error occurred. */
+                               goto end;
+                       }
                }
        }
 end:
@@ -3624,9 +3823,6 @@ end:
        if (!ret) {
                ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
        }
-       if (ret == -LTTNG_ERR_NO_SESSION) {
-               ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
-       }
        return ret;
 }
 
@@ -3640,8 +3836,8 @@ int load_session_from_path(const char *path, const char *session_name,
        struct lttng_dynamic_buffer file_path;
        size_t path_len;
 
-       assert(path);
-       assert(validation_ctx);
+       LTTNG_ASSERT(path);
+       LTTNG_ASSERT(validation_ctx);
        path_len = strlen(path);
        lttng_dynamic_buffer_init(&file_path);
        if (path_len >= LTTNG_PATH_MAX) {
@@ -3802,7 +3998,7 @@ static int validate_path_creds(const char *path)
        int ret, uid = getuid();
        struct stat buf;
 
-       assert(path);
+       LTTNG_ASSERT(path);
 
        if (uid == 0) {
                goto valid;
@@ -3848,16 +4044,17 @@ int config_load_session(const char *path, const char *session_name,
                /* Try home path */
                home_path = utils_get_home_dir();
                if (home_path) {
-                       char path[PATH_MAX];
+                       char path_buf[PATH_MAX];
 
                        /*
                         * Try user session configuration path. Ignore error here so we can
                         * continue loading the system wide sessions.
                         */
                        if (autoload) {
-                               ret = snprintf(path, sizeof(path),
-                                               DEFAULT_SESSION_HOME_CONFIGPATH "/"
-                                               DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
+                               ret = snprintf(path_buf, sizeof(path_buf),
+                                               DEFAULT_SESSION_HOME_CONFIGPATH
+                                               "/" DEFAULT_SESSION_CONFIG_AUTOLOAD,
+                                               home_path);
                                if (ret < 0) {
                                        PERROR("snprintf session autoload home config path");
                                        ret = -LTTNG_ERR_INVALID;
@@ -3869,19 +4066,20 @@ int config_load_session(const char *path, const char *session_name,
                                 * avoid any user session daemon to try to load kernel sessions
                                 * automatically and failing all the times.
                                 */
-                               ret = validate_path_creds(path);
+                               ret = validate_path_creds(path_buf);
                                if (ret) {
-                                       path_ptr = path;
+                                       path_ptr = path_buf;
                                }
                        } else {
-                               ret = snprintf(path, sizeof(path),
-                                               DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
+                               ret = snprintf(path_buf, sizeof(path_buf),
+                                               DEFAULT_SESSION_HOME_CONFIGPATH,
+                                               home_path);
                                if (ret < 0) {
                                        PERROR("snprintf session home config path");
                                        ret = -LTTNG_ERR_INVALID;
                                        goto end;
                                }
-                               path_ptr = path;
+                               path_ptr = path_buf;
                        }
                        if (path_ptr) {
                                ret = load_session_from_path(path_ptr, session_name,
This page took 0.031528 seconds and 4 git commands to generate.