Port: Remove _GNU_SOURCE, defined in config.h
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
index cf8264a307b7029f479b779dd846221212922567..35f0e578a0da725863f1b07be9d385a577371ea2 100644 (file)
@@ -15,7 +15,7 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <assert.h>
 #include <urcu/uatomic.h>
 
 #include "agent.h"
 #include "ust-app.h"
 #include "utils.h"
+#include "error.h"
+
+#define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
+
+/*
+ * Human readable agent return code.
+ */
+static const char *error_string_array[] = {
+       [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS) ] = "Success",
+       [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID) ] = "Invalid command",
+       [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME) ] = "Unknown logger name",
+
+       /* Last element */
+       [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR) ] = "Unknown code",
+};
+
+static
+void log_reply_code(uint32_t in_reply_ret_code)
+{
+       int level = PRINT_DBG3;
+       /*
+        * reply_ret_code and in_reply_ret_code are kept separate to have a
+        * sanitized value (used to retrieve the human readable string) and the
+        * original value which is logged as-is.
+        */
+       uint32_t reply_ret_code = in_reply_ret_code;
+
+       if (reply_ret_code < AGENT_RET_CODE_SUCCESS ||
+                       reply_ret_code >= AGENT_RET_CODE_NR) {
+               reply_ret_code = AGENT_RET_CODE_NR;
+               level = PRINT_ERR;
+       }
+
+       LOG(level, "Agent replied with retcode: %s (%"PRIu32")",
+                       error_string_array[AGENT_RET_CODE_INDEX(
+                       reply_ret_code)],
+                       in_reply_ret_code);
+}
 
 /*
  * Match function for the events hash table lookup by name.
@@ -64,6 +102,7 @@ static int ht_match_event(struct cds_lfht_node *node,
 {
        struct agent_event *event;
        const struct agent_ht_key *key;
+       int ll_match;
 
        assert(node);
        assert(_key);
@@ -78,14 +117,15 @@ static int ht_match_event(struct cds_lfht_node *node,
                goto no_match;
        }
 
-       if (event->loglevel != key->loglevel) {
-               if (event->loglevel_type == LTTNG_EVENT_LOGLEVEL_ALL &&
-                               key->loglevel == 0 && event->loglevel == -1) {
-                       goto match;
-               }
+       /* Event loglevel value and type. */
+       ll_match = loglevels_match(event->loglevel_type,
+               event->loglevel_value, key->loglevel_type,
+               key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
+
+       if (!ll_match) {
                goto no_match;
        }
-match:
+
        return 1;
 
 no_match:
@@ -106,7 +146,8 @@ static void add_unique_agent_event(struct lttng_ht *ht,
        assert(event);
 
        key.name = event->name;
-       key.loglevel = event->loglevel;
+       key.loglevel_value = event->loglevel_value;
+       key.loglevel_type = event->loglevel_type;
 
        node_ptr = cds_lfht_add_unique(ht->ht,
                        ht->hash_fct(event->node.key, lttng_ht_seed),
@@ -124,7 +165,7 @@ static void destroy_event_agent_rcu(struct rcu_head *head)
        struct agent_event *event =
                caa_container_of(node, struct agent_event, node);
 
-       free(event);
+       agent_destroy_event(event);
 }
 
 /*
@@ -234,6 +275,7 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
        int ret, i, len = 0, offset = 0;
        uint32_t nb_event;
        size_t data_size;
+       uint32_t reply_ret_code;
        struct lttng_event *tmp_events = NULL;
        struct lttcomm_agent_list_reply *reply = NULL;
        struct lttcomm_agent_list_reply_hdr reply_hdr;
@@ -256,14 +298,14 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
                goto error_io;
        }
 
-       switch (be32toh(reply_hdr.ret_code)) {
+       reply_ret_code = be32toh(reply_hdr.ret_code);
+       log_reply_code(reply_ret_code);
+       switch (reply_ret_code) {
        case AGENT_RET_CODE_SUCCESS:
                data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
                break;
        default:
-               ERR("Agent returned an unknown code: %" PRIu32,
-                               be32toh(reply_hdr.ret_code));
-               ret = LTTNG_ERR_FATAL;
+               ret = LTTNG_ERR_UNK;
                goto error;
        }
 
@@ -319,6 +361,7 @@ static int enable_event(struct agent_app *app, struct agent_event *event)
 {
        int ret;
        uint64_t data_size;
+       uint32_t reply_ret_code;
        struct lttcomm_agent_enable msg;
        struct lttcomm_agent_generic_reply reply;
 
@@ -337,7 +380,7 @@ static int enable_event(struct agent_app *app, struct agent_event *event)
        }
 
        memset(&msg, 0, sizeof(msg));
-       msg.loglevel = event->loglevel;
+       msg.loglevel_value = event->loglevel_value;
        msg.loglevel_type = event->loglevel_type;
        strncpy(msg.name, event->name, sizeof(msg.name));
        ret = send_payload(app->sock, &msg, sizeof(msg));
@@ -350,16 +393,16 @@ static int enable_event(struct agent_app *app, struct agent_event *event)
                goto error_io;
        }
 
-       switch (be32toh(reply.ret_code)) {
+       reply_ret_code = be32toh(reply.ret_code);
+       log_reply_code(reply_ret_code);
+       switch (reply_ret_code) {
        case AGENT_RET_CODE_SUCCESS:
                break;
        case AGENT_RET_CODE_UNKNOWN_NAME:
                ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
                goto error;
        default:
-               ERR("Agent returned an unknown code: %" PRIu32,
-                               be32toh(reply.ret_code));
-               ret = LTTNG_ERR_FATAL;
+               ret = LTTNG_ERR_UNK;
                goto error;
        }
 
@@ -381,6 +424,7 @@ static int disable_event(struct agent_app *app, struct agent_event *event)
 {
        int ret;
        uint64_t data_size;
+       uint32_t reply_ret_code;
        struct lttcomm_agent_disable msg;
        struct lttcomm_agent_generic_reply reply;
 
@@ -410,16 +454,16 @@ static int disable_event(struct agent_app *app, struct agent_event *event)
                goto error_io;
        }
 
-       switch (be32toh(reply.ret_code)) {
+       reply_ret_code = be32toh(reply.ret_code);
+       log_reply_code(reply_ret_code);
+       switch (reply_ret_code) {
        case AGENT_RET_CODE_SUCCESS:
                break;
        case AGENT_RET_CODE_UNKNOWN_NAME:
                ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
                goto error;
        default:
-               ERR("Agent returned an unknown code: %" PRIu32,
-                               be32toh(reply.ret_code));
-               ret = LTTNG_ERR_FATAL;
+               ret = LTTNG_ERR_UNK;
                goto error;
        }
 
@@ -452,7 +496,8 @@ int agent_send_registration_done(struct agent_app *app)
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR* code.
  */
-int agent_enable_event(struct agent_event *event)
+int agent_enable_event(struct agent_event *event,
+               enum lttng_domain_type domain)
 {
        int ret;
        struct agent_app *app;
@@ -464,6 +509,10 @@ int agent_enable_event(struct agent_event *event)
 
        cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
                        node.node) {
+               if (app->domain != domain) {
+                       continue;
+               }
+
                /* Enable event on agent application through TCP socket. */
                ret = enable_event(app, event);
                if (ret != LTTNG_OK) {
@@ -485,18 +534,26 @@ error:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR* code.
  */
-int agent_disable_event(struct agent_event *event)
+int agent_disable_event(struct agent_event *event,
+               enum lttng_domain_type domain)
 {
-       int ret;
+       int ret = LTTNG_OK;
        struct agent_app *app;
        struct lttng_ht_iter iter;
 
        assert(event);
+       if (!event->enabled) {
+               goto end;
+       }
 
        rcu_read_lock();
 
        cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
                        node.node) {
+               if (app->domain != domain) {
+                       continue;
+               }
+
                /* Enable event on agent application through TCP socket. */
                ret = disable_event(app, event);
                if (ret != LTTNG_OK) {
@@ -505,10 +562,10 @@ int agent_disable_event(struct agent_event *event)
        }
 
        event->enabled = 0;
-       ret = LTTNG_OK;
 
 error:
        rcu_read_unlock();
+end:
        return ret;
 }
 
@@ -518,7 +575,8 @@ error:
  *
  * Return the number of events or else a negative value.
  */
-int agent_list_events(struct lttng_event **events)
+int agent_list_events(struct lttng_event **events,
+               enum lttng_domain_type domain)
 {
        int ret;
        size_t nbmem, count = 0;
@@ -528,6 +586,8 @@ int agent_list_events(struct lttng_event **events)
 
        assert(events);
 
+       DBG2("Agent listing events for domain %d", domain);
+
        nbmem = UST_APP_EVENT_LIST_SIZE;
        tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
        if (!tmp_events) {
@@ -542,6 +602,11 @@ int agent_list_events(struct lttng_event **events)
                ssize_t nb_ev;
                struct lttng_event *agent_events;
 
+               /* Skip domain not asked by the list. */
+               if (app->domain != domain) {
+                       continue;
+               }
+
                nb_ev = list_events(app, &agent_events);
                if (nb_ev < 0) {
                        ret = nb_ev;
@@ -593,7 +658,8 @@ error:
  *
  * Return newly allocated object or else NULL on error.
  */
-struct agent_app *agent_create_app(pid_t pid, struct lttcomm_sock *sock)
+struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
+               struct lttcomm_sock *sock)
 {
        struct agent_app *app;
 
@@ -606,6 +672,7 @@ struct agent_app *agent_create_app(pid_t pid, struct lttcomm_sock *sock)
        }
 
        app->pid = pid;
+       app->domain = domain;
        app->sock = sock;
        lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
 
@@ -651,14 +718,13 @@ void agent_add_app(struct agent_app *app)
        assert(app);
 
        DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
-
-       rcu_read_lock();
        lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
-       rcu_read_unlock();
 }
 
 /*
  * Delete agent application from the global hash table.
+ *
+ * rcu_read_lock() must be held by the caller.
  */
 void agent_delete_app(struct agent_app *app)
 {
@@ -670,14 +736,12 @@ void agent_delete_app(struct agent_app *app)
        DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
 
        iter.iter.node = &app->node.node;
-       rcu_read_lock();
        ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
-       rcu_read_unlock();
        assert(!ret);
 }
 
 /*
- * Destroy a agent application object by detaching it from its corresponding
+ * Destroy an agent application object by detaching it from its corresponding
  * UST app if one is connected by closing the socket. Finally, perform a
  * delayed memory reclaim.
  */
@@ -709,6 +773,7 @@ int agent_init(struct agent *agt)
                ret = -ENOMEM;
                goto error;
        }
+       lttng_ht_node_init_u64(&agt->node, agt->domain);
 
        return 0;
 
@@ -717,33 +782,78 @@ error:
 }
 
 /*
- * Create a newly allocated agent event data structure. If name is valid, it's
- * copied into the created event.
+ * Add agent object to the given hash table.
+ */
+void agent_add(struct agent *agt, struct lttng_ht *ht)
+{
+       assert(agt);
+       assert(ht);
+
+       DBG3("Agent adding from domain %d", agt->domain);
+
+       lttng_ht_add_unique_u64(ht, &agt->node);
+}
+
+/*
+ * Create an agent object for the given domain.
+ *
+ * Return the allocated agent or NULL on error.
+ */
+struct agent *agent_create(enum lttng_domain_type domain)
+{
+       int ret;
+       struct agent *agt;
+
+       agt = zmalloc(sizeof(struct agent));
+       if (!agt) {
+               goto error;
+       }
+       agt->domain = domain;
+
+       ret = agent_init(agt);
+       if (ret < 0) {
+               free(agt);
+               agt = NULL;
+               goto error;
+       }
+
+error:
+       return agt;
+}
+
+/*
+ * Create a newly allocated agent event data structure.
+ * Ownership of filter_expression is taken.
  *
  * Return a new object else NULL on error.
  */
 struct agent_event *agent_create_event(const char *name,
-               struct lttng_filter_bytecode *filter)
+               enum lttng_loglevel_type loglevel_type, int loglevel_value,
+               struct lttng_filter_bytecode *filter, char *filter_expression)
 {
-       struct agent_event *event;
+       struct agent_event *event = NULL;
+
+       DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
+               name, loglevel_type, loglevel_value);
 
-       DBG3("Agent create new event with name %s", name);
+       if (!name) {
+               ERR("Failed to create agent event; no name provided.");
+               goto error;
+       }
 
        event = zmalloc(sizeof(*event));
        if (!event) {
                goto error;
        }
 
-       if (name) {
-               strncpy(event->name, name, sizeof(event->name));
-               event->name[sizeof(event->name) - 1] = '\0';
-               lttng_ht_node_init_str(&event->node, event->name);
-       }
-
-       if (filter) {
-               event->filter = filter;
-       }
+       strncpy(event->name, name, sizeof(event->name));
+       event->name[sizeof(event->name) - 1] = '\0';
+       lttng_ht_node_init_str(&event->node, event->name);
 
+       event->loglevel_value = loglevel_value;
+       event->loglevel_type = loglevel_type;
+       event->filter = filter;
+       event->filter_expression = filter_expression;
 error:
        return event;
 }
@@ -758,58 +868,64 @@ void agent_add_event(struct agent_event *event, struct agent *agt)
        assert(agt->events);
 
        DBG3("Agent adding event %s", event->name);
-
-       rcu_read_lock();
        add_unique_agent_event(agt->events, event);
-       rcu_read_unlock();
        agt->being_used = 1;
 }
 
 /*
- * Find a agent event in the given agent using name.
+ * Find multiple agent events sharing the given name.
  *
- * RCU read side lock MUST be acquired.
+ * RCU read side lock MUST be acquired. It must be held for the
+ * duration of the iteration.
  *
- * Return object if found else NULL.
+ * Sets the given iterator.
  */
-struct agent_event *agent_find_event_by_name(const char *name,
-               struct agent *agt)
+void agent_find_events_by_name(const char *name, struct agent *agt,
+               struct lttng_ht_iter* iter)
 {
-       struct lttng_ht_node_str *node;
-       struct lttng_ht_iter iter;
        struct lttng_ht *ht;
        struct agent_ht_key key;
 
        assert(name);
        assert(agt);
        assert(agt->events);
+       assert(iter);
 
        ht = agt->events;
        key.name = name;
 
        cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
-                       ht_match_event_by_name, &key, &iter.iter);
-       node = lttng_ht_iter_get_node_str(&iter);
-       if (node == NULL) {
-               goto error;
-       }
+                       ht_match_event_by_name, &key, &iter->iter);
+}
 
-       DBG3("Agent event found %s by name.", name);
-       return caa_container_of(node, struct agent_event, node);
+/*
+ * Get the next agent event duplicate by name. This should be called
+ * after a call to agent_find_events_by_name() to iterate on events.
+ *
+ * The RCU read lock must be held during the iteration and for as long
+ * as the object the iterator points to remains in use.
+ */
+void agent_event_next_duplicate(const char *name,
+               struct agent *agt, struct lttng_ht_iter* iter)
+{
+       struct agent_ht_key key;
 
-error:
-       DBG3("Agent NOT found by name %s.", name);
-       return NULL;
+       key.name = name;
+
+       cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
+               &key, &iter->iter);
 }
 
 /*
  * Find a agent event in the given agent using name and loglevel.
  *
- * RCU read side lock MUST be acquired.
+ * RCU read side lock MUST be acquired. It must be kept for as long as
+ * the returned agent_event is used.
  *
  * Return object if found else NULL.
  */
-struct agent_event *agent_find_event(const char *name, int loglevel,
+struct agent_event *agent_find_event(const char *name,
+               enum lttng_loglevel_type loglevel_type, int loglevel_value,
                struct agent *agt)
 {
        struct lttng_ht_node_str *node;
@@ -823,7 +939,8 @@ struct agent_event *agent_find_event(const char *name, int loglevel,
 
        ht = agt->events;
        key.name = name;
-       key.loglevel = loglevel;
+       key.loglevel_value = loglevel_value;
+       key.loglevel_type = loglevel_type;
 
        cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
                        ht_match_event, &key, &iter.iter);
@@ -836,7 +953,7 @@ struct agent_event *agent_find_event(const char *name, int loglevel,
        return caa_container_of(node, struct agent_event, node);
 
 error:
-       DBG3("Agent NOT found %s.", name);
+       DBG3("Agent event NOT found %s.", name);
        return NULL;
 }
 
@@ -849,12 +966,14 @@ void agent_destroy_event(struct agent_event *event)
 {
        assert(event);
 
+       free(event->filter);
+       free(event->filter_expression);
+       free(event->exclusion);
        free(event);
 }
 
 /*
- * Destroy an agent completely. Note that the given pointer is NOT freed
- * thus a reference to static or stack data can be passed to this function.
+ * Destroy an agent completely.
  */
 void agent_destroy(struct agent *agt)
 {
@@ -865,14 +984,6 @@ void agent_destroy(struct agent *agt)
 
        DBG3("Agent destroy");
 
-       /*
-        * Just ignore if no events hash table exists. This is possible if for
-        * instance an agent object was allocated but not initialized.
-        */
-       if (!agt->events) {
-               return;
-       }
-
        rcu_read_lock();
        cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
                int ret;
@@ -892,20 +1003,72 @@ void agent_destroy(struct agent *agt)
        }
        rcu_read_unlock();
 
-       lttng_ht_destroy(agt->events);
+       ht_cleanup_push(agt->events);
+       free(agt);
 }
 
 /*
- * Initialize agent subsystem.
+ * Allocate agent_apps_ht_by_sock.
  */
-int agent_setup(void)
+int agent_app_ht_alloc(void)
 {
+       int ret = 0;
+
        agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
        if (!agent_apps_ht_by_sock) {
-               return -1;
+               ret = -1;
        }
 
-       return 0;
+       return ret;
+}
+
+/*
+ * Destroy a agent application by socket.
+ */
+void agent_destroy_app_by_sock(int sock)
+{
+       struct agent_app *app;
+
+       assert(sock >= 0);
+
+       /*
+        * Not finding an application is a very important error that should NEVER
+        * happen. The hash table deletion is ONLY done through this call when the
+        * main sessiond thread is torn down.
+        */
+       rcu_read_lock();
+       app = agent_find_app_by_sock(sock);
+       assert(app);
+
+       /* RCU read side lock is assumed to be held by this function. */
+       agent_delete_app(app);
+
+       /* The application is freed in a RCU call but the socket is closed here. */
+       agent_destroy_app(app);
+       rcu_read_unlock();
+}
+
+/*
+ * Clean-up the agent app hash table and destroy it.
+ */
+void agent_app_ht_clean(void)
+{
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+
+       if (!agent_apps_ht_by_sock) {
+               return;
+       }
+       rcu_read_lock();
+       cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
+               struct agent_app *app;
+
+               app = caa_container_of(node, struct agent_app, node);
+               agent_destroy_app_by_sock(app->sock->fd);
+       }
+       rcu_read_unlock();
+
+       lttng_ht_destroy(agent_apps_ht_by_sock);
 }
 
 /*
This page took 0.030788 seconds and 5 git commands to generate.