X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fagent.cpp;h=31f1634bd70b70a681b6479433e0eb743fe72492;hb=HEAD;hp=08785235623d5d312d14b95960c1efaaac003f68;hpb=64803277bbdbe0a943360d918298a48157d9da55;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/agent.cpp b/src/bin/lttng-sessiond/agent.cpp index 087852356..210ec9b0c 100644 --- a/src/bin/lttng-sessiond/agent.cpp +++ b/src/bin/lttng-sessiond/agent.cpp @@ -7,38 +7,38 @@ */ #define _LGPL_SOURCE -#include -#include +#include "agent.hpp" +#include "common/error.hpp" +#include "ust-app.hpp" +#include "utils.hpp" -#include +#include +#include +#include +#include + +#include +#include +#include #include +#include #include #include #include -#include -#include -#include #include -#include -#include - -#include - -#include "agent.hpp" -#include "ust-app.hpp" -#include "utils.hpp" -#include "common/error.hpp" +#include +#include -typedef enum lttng_event_rule_status (*event_rule_logging_get_name_pattern)( - const struct lttng_event_rule *rule, const char **pattern); -typedef enum lttng_event_rule_status (*event_rule_logging_get_log_level_rule)( - const struct lttng_event_rule *rule, - const struct lttng_log_level_rule **log_level_rule); +using event_rule_logging_get_name_pattern = + enum lttng_event_rule_status (*)(const struct lttng_event_rule *, const char **); +using event_rule_logging_get_log_level_rule = enum lttng_event_rule_status (*)( + const struct lttng_event_rule *, const struct lttng_log_level_rule **); /* * Agent application context representation. */ +namespace { struct agent_app_ctx { char *provider_name; char *ctx_name; @@ -49,12 +49,12 @@ struct agent_app_ctx { /* For call_rcu teardown. */ struct rcu_head rcu_node; }; +} /* namespace */ /* * Human readable agent return code. */ -static -const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code) +static const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code) { switch (code) { case AGENT_RET_CODE_SUCCESS: @@ -68,8 +68,7 @@ const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code) } }; -static -void log_reply_code(uint32_t in_reply_ret_code) +static void log_reply_code(uint32_t in_reply_ret_code) { int level = PRINT_DBG3; /* @@ -79,22 +78,21 @@ void log_reply_code(uint32_t in_reply_ret_code) */ uint32_t reply_ret_code = in_reply_ret_code; - if (reply_ret_code < AGENT_RET_CODE_SUCCESS || - reply_ret_code >= AGENT_RET_CODE_NR) { + 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 ")", - lttcomm_agent_ret_code_str((lttcomm_agent_ret_code) reply_ret_code), - in_reply_ret_code); + LOG(level, + "Agent replied with retcode: %s (%" PRIu32 ")", + lttcomm_agent_ret_code_str((lttcomm_agent_ret_code) reply_ret_code), + in_reply_ret_code); } /* * Match function for the events hash table lookup by name. */ -static int ht_match_event_by_name(struct cds_lfht_node *node, - const void *_key) +static int ht_match_event_by_name(struct cds_lfht_node *node, const void *_key) { struct agent_event *event; const struct agent_ht_key *key; @@ -122,8 +120,7 @@ no_match: * Match function for the events hash table lookup by name, log level and * filter expression. */ -static int ht_match_event(struct cds_lfht_node *node, - const void *_key) +static int ht_match_event(struct cds_lfht_node *node, const void *_key) { struct agent_event *event; const struct agent_ht_key *key; @@ -144,8 +141,10 @@ static int ht_match_event(struct cds_lfht_node *node, /* 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); + event->loglevel_value, + key->loglevel_type, + key->loglevel_value, + LTTNG_EVENT_LOGLEVEL_ALL); if (!ll_match) { goto no_match; @@ -158,8 +157,9 @@ static int ht_match_event(struct cds_lfht_node *node, } if (event->filter_expression) { - if (strncmp(event->filter_expression, key->filter_expression, - strlen(event->filter_expression)) != 0) { + if (strncmp(event->filter_expression, + key->filter_expression, + strlen(event->filter_expression)) != 0) { goto no_match; } } @@ -173,8 +173,7 @@ no_match: /* * Add unique agent event based on the event name and loglevel. */ -static void add_unique_agent_event(struct lttng_ht *ht, - struct agent_event *event) +static void add_unique_agent_event(struct lttng_ht *ht, struct agent_event *event) { struct cds_lfht_node *node_ptr; struct agent_ht_key key; @@ -189,8 +188,10 @@ static void add_unique_agent_event(struct lttng_ht *ht, key.filter_expression = event->filter_expression; node_ptr = cds_lfht_add_unique(ht->ht, - ht->hash_fct(event->node.key, lttng_ht_seed), - ht_match_event, &key, &event->node.node); + ht->hash_fct(event->node.key, lttng_ht_seed), + ht_match_event, + &key, + &event->node.node); LTTNG_ASSERT(node_ptr == &event->node.node); } @@ -199,10 +200,8 @@ static void add_unique_agent_event(struct lttng_ht *ht, */ static void destroy_event_agent_rcu(struct rcu_head *head) { - struct lttng_ht_node_str *node = - caa_container_of(head, struct lttng_ht_node_str, head); - struct agent_event *event = - caa_container_of(node, struct agent_event, node); + struct lttng_ht_node_str *node = lttng::utils::container_of(head, <tng_ht_node_str::head); + struct agent_event *event = lttng::utils::container_of(node, &agent_event::node); agent_destroy_event(event); } @@ -213,9 +212,8 @@ static void destroy_event_agent_rcu(struct rcu_head *head) static void destroy_app_agent_rcu(struct rcu_head *head) { struct lttng_ht_node_ulong *node = - caa_container_of(head, struct lttng_ht_node_ulong, head); - struct agent_app *app = - caa_container_of(node, struct agent_app, node); + lttng::utils::container_of(head, <tng_ht_node_ulong::head); + struct agent_app *app = lttng::utils::container_of(node, &agent_app::node); free(app); } @@ -226,8 +224,8 @@ static void destroy_app_agent_rcu(struct rcu_head *head) * * Return 0 on success or else a negative errno message of sendmsg() op. */ -static int send_header(struct lttcomm_sock *sock, uint64_t data_size, - uint32_t cmd, uint32_t cmd_version) +static int +send_header(struct lttcomm_sock *sock, uint64_t data_size, uint32_t cmd, uint32_t cmd_version) { int ret; ssize_t size; @@ -257,8 +255,7 @@ error: * * Return 0 on success or else a negative errno value of sendmsg() op. */ -static int send_payload(struct lttcomm_sock *sock, const void *data, - size_t size) +static int send_payload(struct lttcomm_sock *sock, const void *data, size_t size) { int ret; ssize_t len; @@ -315,16 +312,15 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events) 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 lttng_event *tmp_events = nullptr; + struct lttcomm_agent_list_reply *reply = nullptr; struct lttcomm_agent_list_reply_hdr reply_hdr; LTTNG_ASSERT(app); LTTNG_ASSERT(app->sock); LTTNG_ASSERT(events); - DBG2("Agent listing events for app pid: %d and socket %d", app->pid, - app->sock->fd); + DBG2("Agent listing events for app pid: %d and socket %d", app->pid, app->sock->fd); ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0); if (ret < 0) { @@ -369,8 +365,9 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events) for (i = 0; i < nb_event; i++) { offset += len; - if (lttng_strncpy(tmp_events[i].name, reply->payload + offset, - sizeof(tmp_events[i].name))) { + if (lttng_strncpy(tmp_events[i].name, + reply->payload + offset, + sizeof(tmp_events[i].name))) { ret = LTTNG_ERR_INVALID; goto error; } @@ -390,7 +387,6 @@ error: free(reply); free(tmp_events); return -ret; - } /* @@ -413,8 +409,10 @@ static int enable_event(const struct agent_app *app, struct agent_event *event) LTTNG_ASSERT(app->sock); LTTNG_ASSERT(event); - DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name, - app->pid, app->sock->fd); + DBG2("Agent enabling event %s for app pid: %d and socket %d", + event->name, + app->pid, + app->sock->fd); /* * Calculate the payload's size, which is the fixed-size struct followed @@ -449,8 +447,9 @@ static int enable_event(const struct agent_app *app, struct agent_event *event) memcpy(bytes_to_send, &msg, sizeof(msg)); if (filter_expression_length > 0) { - memcpy(bytes_to_send + sizeof(msg), event->filter_expression, - filter_expression_length); + memcpy(bytes_to_send + sizeof(msg), + event->filter_expression, + filter_expression_length); } ret = send_payload(app->sock, bytes_to_send, data_size); @@ -488,8 +487,7 @@ error: /* * Send Pascal-style string. Size is sent as a 32-bit big endian integer. */ -static -int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len) +static int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len) { int ret; uint32_t len_be; @@ -515,7 +513,8 @@ end: * Return LTTNG_OK on success or else a LTTNG_ERR* code. */ static int app_context_op(const struct agent_app *app, - const struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd) + const struct agent_app_ctx *ctx, + enum lttcomm_agent_command cmd) { int ret; uint32_t reply_ret_code; @@ -525,13 +524,14 @@ static int app_context_op(const struct agent_app *app, LTTNG_ASSERT(app); LTTNG_ASSERT(app->sock); LTTNG_ASSERT(ctx); - LTTNG_ASSERT(cmd == AGENT_CMD_APP_CTX_ENABLE || - cmd == AGENT_CMD_APP_CTX_DISABLE); + LTTNG_ASSERT(cmd == AGENT_CMD_APP_CTX_ENABLE || cmd == AGENT_CMD_APP_CTX_DISABLE); DBG2("Agent %s application %s:%s for app pid: %d and socket %d", - cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling", - ctx->provider_name, ctx->ctx_name, - app->pid, app->sock->fd); + cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling", + ctx->provider_name, + ctx->ctx_name, + app->pid, + app->sock->fd); /* * Calculate the payload's size, which consists of the size (u32, BE) @@ -541,29 +541,26 @@ static int app_context_op(const struct agent_app *app, */ app_ctx_provider_name_len = strlen(ctx->provider_name) + 1; app_ctx_name_len = strlen(ctx->ctx_name) + 1; - data_size = sizeof(uint32_t) + app_ctx_provider_name_len + - sizeof(uint32_t) + app_ctx_name_len; + data_size = + sizeof(uint32_t) + app_ctx_provider_name_len + sizeof(uint32_t) + app_ctx_name_len; ret = send_header(app->sock, data_size, cmd, 0); if (ret < 0) { goto error_io; } - if (app_ctx_provider_name_len > UINT32_MAX || - app_ctx_name_len > UINT32_MAX) { + if (app_ctx_provider_name_len > UINT32_MAX || app_ctx_name_len > UINT32_MAX) { ERR("Application context name > MAX_UINT32"); ret = LTTNG_ERR_INVALID; goto error; } - ret = send_pstring(app->sock, ctx->provider_name, - (uint32_t) app_ctx_provider_name_len); + ret = send_pstring(app->sock, ctx->provider_name, (uint32_t) app_ctx_provider_name_len); if (ret < 0) { goto error_io; } - ret = send_pstring(app->sock, ctx->ctx_name, - (uint32_t) app_ctx_name_len); + ret = send_pstring(app->sock, ctx->ctx_name, (uint32_t) app_ctx_name_len); if (ret < 0) { goto error_io; } @@ -609,8 +606,10 @@ static int disable_event(struct agent_app *app, struct agent_event *event) LTTNG_ASSERT(app->sock); LTTNG_ASSERT(event); - DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name, - app->pid, app->sock->fd); + DBG2("Agent disabling event %s for app pid: %d and socket %d", + event->name, + app->pid, + app->sock->fd); data_size = sizeof(msg); memset(&msg, 0, sizeof(msg)); @@ -676,8 +675,7 @@ 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, - enum lttng_domain_type domain) +int agent_enable_event(struct agent_event *event, enum lttng_domain_type domain) { int ret; struct agent_app *app; @@ -685,18 +683,20 @@ int agent_enable_event(struct agent_event *event, LTTNG_ASSERT(event); - rcu_read_lock(); + { + lttng::urcu::read_lock_guard read_lock; - cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app, - node.node) { - if (app->domain != domain) { - continue; - } + cds_lfht_for_each_entry ( + the_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) { - goto error; + /* Enable event on agent application through TCP socket. */ + ret = enable_event(app, event); + if (ret != LTTNG_OK) { + goto error; + } } } @@ -704,22 +704,19 @@ int agent_enable_event(struct agent_event *event, ret = LTTNG_OK; error: - rcu_read_unlock(); return ret; } -static -void destroy_app_ctx(struct agent_app_ctx *ctx) +static void destroy_app_ctx(struct agent_app_ctx *ctx) { free(ctx->provider_name); free(ctx->ctx_name); free(ctx); } -static -struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx) +static struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx) { - struct agent_app_ctx *agent_ctx = NULL; + struct agent_app_ctx *agent_ctx = nullptr; if (!ctx) { goto end; @@ -735,7 +732,7 @@ struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx) agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name); if (!agent_ctx->provider_name || !agent_ctx->ctx_name) { destroy_app_ctx(agent_ctx); - agent_ctx = NULL; + agent_ctx = nullptr; } end: return agent_ctx; @@ -747,8 +744,7 @@ end: * * Return LTTNG_OK on success or else a LTTNG_ERR* code. */ -int agent_enable_context(const struct lttng_event_context *ctx, - enum lttng_domain_type domain) +int agent_enable_context(const struct lttng_event_context *ctx, enum lttng_domain_type domain) { int ret; struct agent_app *app; @@ -760,34 +756,35 @@ int agent_enable_context(const struct lttng_event_context *ctx, goto error; } - rcu_read_lock(); + { + lttng::urcu::read_lock_guard read_lock; - cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app, - node.node) { - struct agent_app_ctx *agent_ctx; + cds_lfht_for_each_entry ( + the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) { + struct agent_app_ctx *agent_ctx; - if (app->domain != domain) { - continue; - } + if (app->domain != domain) { + continue; + } - agent_ctx = create_app_ctx(ctx); - if (!agent_ctx) { - ret = LTTNG_ERR_NOMEM; - goto error_unlock; - } + agent_ctx = create_app_ctx(ctx); + if (!agent_ctx) { + ret = LTTNG_ERR_NOMEM; + goto error_unlock; + } - /* Enable event on agent application through TCP socket. */ - ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE); - destroy_app_ctx(agent_ctx); - if (ret != LTTNG_OK) { - goto error_unlock; + /* Enable event on agent application through TCP socket. */ + ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE); + destroy_app_ctx(agent_ctx); + if (ret != LTTNG_OK) { + goto error_unlock; + } } } ret = LTTNG_OK; error_unlock: - rcu_read_unlock(); error: return ret; } @@ -798,8 +795,7 @@ error: * * Return LTTNG_OK on success or else a LTTNG_ERR* code. */ -int agent_disable_event(struct agent_event *event, - enum lttng_domain_type domain) +int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain) { int ret = LTTNG_OK; struct agent_app *app; @@ -820,18 +816,20 @@ int agent_disable_event(struct agent_event *event, goto end; } - rcu_read_lock(); + { + lttng::urcu::read_lock_guard read_lock; - cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app, - node.node) { - if (app->domain != domain) { - continue; - } + cds_lfht_for_each_entry ( + the_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) { - goto error; + /* Enable event on agent application through TCP socket. */ + ret = disable_event(app, event); + if (ret != LTTNG_OK) { + goto error; + } } } @@ -839,7 +837,6 @@ int agent_disable_event(struct agent_event *event, LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event)); error: - rcu_read_unlock(); end: return ret; } @@ -850,31 +847,31 @@ end: * * Return LTTNG_OK on success or else a LTTNG_ERR* code. */ -static int disable_context(struct agent_app_ctx *ctx, - enum lttng_domain_type domain) +static int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain) { int ret = LTTNG_OK; struct agent_app *app; struct lttng_ht_iter iter; LTTNG_ASSERT(ctx); + DBG2("Disabling agent application context %s:%s", ctx->provider_name, ctx->ctx_name); - rcu_read_lock(); - DBG2("Disabling agent application context %s:%s", - ctx->provider_name, ctx->ctx_name); - cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app, - node.node) { - if (app->domain != domain) { - continue; - } + { + lttng::urcu::read_lock_guard read_lock; - ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE); - if (ret != LTTNG_OK) { - goto end; + cds_lfht_for_each_entry ( + the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) { + if (app->domain != domain) { + continue; + } + + ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE); + if (ret != LTTNG_OK) { + goto end; + } } } end: - rcu_read_unlock(); return ret; } @@ -884,13 +881,12 @@ end: * * Return the number of events or else a negative value. */ -int agent_list_events(struct lttng_event **events, - enum lttng_domain_type domain) +int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain) { int ret; size_t nbmem, count = 0; struct agent_app *app; - struct lttng_event *tmp_events = NULL; + struct lttng_event *tmp_events = nullptr; struct lttng_ht_iter iter; LTTNG_ASSERT(events); @@ -905,58 +901,60 @@ int agent_list_events(struct lttng_event **events, goto error; } - rcu_read_lock(); - cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app, - node.node) { - ssize_t nb_ev; - struct lttng_event *agent_events; + { + lttng::urcu::read_lock_guard read_lock; - /* Skip domain not asked by the list. */ - if (app->domain != domain) { - continue; - } + cds_lfht_for_each_entry ( + the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) { + ssize_t nb_ev; + struct lttng_event *agent_events; - nb_ev = list_events(app, &agent_events); - if (nb_ev < 0) { - ret = nb_ev; - goto error_unlock; - } + /* Skip domain not asked by the list. */ + if (app->domain != domain) { + continue; + } - if (count + nb_ev > nbmem) { - /* In case the realloc fails, we free the memory */ - struct lttng_event *new_tmp_events; - size_t new_nbmem; - - new_nbmem = std::max(count + nb_ev, nbmem << 1); - DBG2("Reallocating agent event list from %zu to %zu entries", - nbmem, new_nbmem); - new_tmp_events = (lttng_event *) realloc(tmp_events, - new_nbmem * sizeof(*new_tmp_events)); - if (!new_tmp_events) { - PERROR("realloc agent events"); - ret = -ENOMEM; - free(agent_events); - goto error_unlock; + nb_ev = list_events(app, &agent_events); + if (nb_ev < 0) { + ret = nb_ev; + goto error; + } + + if (count + nb_ev > nbmem) { + /* In case the realloc fails, we free the memory */ + struct lttng_event *new_tmp_events; + size_t new_nbmem; + + new_nbmem = std::max(count + nb_ev, nbmem << 1); + DBG2("Reallocating agent event list from %zu to %zu entries", + nbmem, + new_nbmem); + new_tmp_events = (lttng_event *) realloc( + tmp_events, new_nbmem * sizeof(*new_tmp_events)); + if (!new_tmp_events) { + PERROR("realloc agent events"); + ret = -ENOMEM; + free(agent_events); + goto error; + } + + /* Zero the new memory */ + memset(new_tmp_events + nbmem, + 0, + (new_nbmem - nbmem) * sizeof(*new_tmp_events)); + nbmem = new_nbmem; + tmp_events = new_tmp_events; } - /* Zero the new memory */ - memset(new_tmp_events + nbmem, 0, - (new_nbmem - nbmem) * sizeof(*new_tmp_events)); - nbmem = new_nbmem; - tmp_events = new_tmp_events; + memcpy(tmp_events + count, agent_events, nb_ev * sizeof(*tmp_events)); + free(agent_events); + count += nb_ev; } - memcpy(tmp_events + count, agent_events, - nb_ev * sizeof(*tmp_events)); - free(agent_events); - count += nb_ev; } - rcu_read_unlock(); ret = count; *events = tmp_events; return ret; -error_unlock: - rcu_read_unlock(); error: free(tmp_events); return ret; @@ -967,8 +965,8 @@ error: * * Return newly allocated object or else NULL on error. */ -struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain, - 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; @@ -1005,20 +1003,19 @@ struct agent_app *agent_find_app_by_sock(int sock) LTTNG_ASSERT(sock >= 0); ASSERT_RCU_READ_LOCKED(); - lttng_ht_lookup(the_agent_apps_ht_by_sock, - (void *) ((unsigned long) sock), &iter); + lttng_ht_lookup(the_agent_apps_ht_by_sock, (void *) ((unsigned long) sock), &iter); node = lttng_ht_iter_get_node_ulong(&iter); - if (node == NULL) { + if (node == nullptr) { goto error; } - app = caa_container_of(node, struct agent_app, node); + app = lttng::utils::container_of(node, &agent_app::node); DBG3("Agent app pid %d found by sock %d.", app->pid, sock); return app; error: DBG3("Agent app NOT found by sock %d.", sock); - return NULL; + return nullptr; } /* @@ -1126,7 +1123,7 @@ struct agent *agent_create(enum lttng_domain_type domain) ret = agent_init(agt); if (ret < 0) { free(agt); - agt = NULL; + agt = nullptr; goto error; } @@ -1141,15 +1138,19 @@ error: * Return a new object else NULL on error. */ struct agent_event *agent_create_event(const char *name, - enum lttng_loglevel_type loglevel_type, int loglevel_value, - struct lttng_bytecode *filter, char *filter_expression) + enum lttng_loglevel_type loglevel_type, + int loglevel_value, + struct lttng_bytecode *filter, + char *filter_expression) { - struct agent_event *event = NULL; + struct agent_event *event = nullptr; DBG3("Agent create new event with name %s, loglevel type %d, \ loglevel value %d and filter %s", - name, loglevel_type, loglevel_value, - filter_expression ? filter_expression : "NULL"); + name, + loglevel_type, + loglevel_value, + filter_expression ? filter_expression : "NULL"); if (!name) { ERR("Failed to create agent event; no name provided."); @@ -1193,7 +1194,7 @@ void agent_add_event(struct agent_event *event, struct agent *agt) int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt) { int ret = LTTNG_OK; - struct agent_app_ctx *agent_ctx = NULL; + struct agent_app_ctx *agent_ctx = nullptr; LTTNG_ASSERT(ctx); LTTNG_ASSERT(agt); @@ -1206,8 +1207,7 @@ int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt) goto end; } - DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name, - ctx->u.app_ctx.ctx_name); + DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name, ctx->u.app_ctx.ctx_name); cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list); end: return ret; @@ -1221,8 +1221,7 @@ end: * * Sets the given iterator. */ -void agent_find_events_by_name(const char *name, struct agent *agt, - struct lttng_ht_iter* iter) +void agent_find_events_by_name(const char *name, struct agent *agt, struct lttng_ht_iter *iter) { struct lttng_ht *ht; struct agent_ht_key key; @@ -1236,8 +1235,11 @@ void agent_find_events_by_name(const char *name, struct agent *agt, 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); + cds_lfht_lookup(ht->ht, + ht->hash_fct((void *) name, lttng_ht_seed), + ht_match_event_by_name, + &key, + &iter->iter); } /* @@ -1248,8 +1250,8 @@ void agent_find_events_by_name(const char *name, struct agent *agt, * * Return object if found else NULL. */ -struct agent_event *agent_find_event_by_trigger( - const struct lttng_trigger *trigger, struct agent *agt) +struct agent_event *agent_find_event_by_trigger(const struct lttng_trigger *trigger, + struct agent *agt) { enum lttng_condition_status c_status; enum lttng_event_rule_status er_status; @@ -1272,30 +1274,23 @@ struct agent_event *agent_find_event_by_trigger( condition = lttng_trigger_get_const_condition(trigger); LTTNG_ASSERT(lttng_condition_get_type(condition) == - LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); + LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); - c_status = lttng_condition_event_rule_matches_get_rule( - condition, &rule); + c_status = lttng_condition_event_rule_matches_get_rule(condition, &rule); LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK); switch (lttng_event_rule_get_type(rule)) { case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: - logging_get_name_pattern = - lttng_event_rule_jul_logging_get_name_pattern; - logging_get_log_level_rule = - lttng_event_rule_jul_logging_get_log_level_rule; + logging_get_name_pattern = lttng_event_rule_jul_logging_get_name_pattern; + logging_get_log_level_rule = lttng_event_rule_jul_logging_get_log_level_rule; break; case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: - logging_get_name_pattern = - lttng_event_rule_log4j_logging_get_name_pattern; - logging_get_log_level_rule = - lttng_event_rule_log4j_logging_get_log_level_rule; + logging_get_name_pattern = lttng_event_rule_log4j_logging_get_name_pattern; + logging_get_log_level_rule = lttng_event_rule_log4j_logging_get_log_level_rule; break; case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: - logging_get_name_pattern = - lttng_event_rule_python_logging_get_name_pattern; - logging_get_log_level_rule = - lttng_event_rule_python_logging_get_log_level_rule; + logging_get_name_pattern = lttng_event_rule_python_logging_get_name_pattern; + logging_get_log_level_rule = lttng_event_rule_python_logging_get_log_level_rule; break; default: abort(); @@ -1304,7 +1299,7 @@ struct agent_event *agent_find_event_by_trigger( domain = lttng_event_rule_get_domain_type(rule); LTTNG_ASSERT(domain == LTTNG_DOMAIN_JUL || domain == LTTNG_DOMAIN_LOG4J || - domain == LTTNG_DOMAIN_PYTHON); + domain == LTTNG_DOMAIN_PYTHON); /* Get the event's pattern name ('name' in the legacy terminology). */ er_status = logging_get_name_pattern(rule, &name); @@ -1324,8 +1319,7 @@ struct agent_event *agent_find_event_by_trigger( abort(); } - return agent_find_event(name, loglevel_type, loglevel_value, - filter_expression, agt); + return agent_find_event(name, loglevel_type, loglevel_value, filter_expression, agt); } /* @@ -1335,8 +1329,7 @@ struct agent_event *agent_find_event_by_trigger( * 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) +void agent_event_next_duplicate(const char *name, struct agent *agt, struct lttng_ht_iter *iter) { struct agent_ht_key key; @@ -1344,8 +1337,7 @@ void agent_event_next_duplicate(const char *name, key.name = name; - cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name, - &key, &iter->iter); + cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name, &key, &iter->iter); } /* @@ -1357,10 +1349,10 @@ void agent_event_next_duplicate(const char *name, * Return object if found else NULL. */ struct agent_event *agent_find_event(const char *name, - enum lttng_loglevel_type loglevel_type, - int loglevel_value, - const char *filter_expression, - struct agent *agt) + enum lttng_loglevel_type loglevel_type, + int loglevel_value, + const char *filter_expression, + struct agent *agt) { struct lttng_ht_node_str *node; struct lttng_ht_iter iter; @@ -1378,19 +1370,22 @@ struct agent_event *agent_find_event(const char *name, key.loglevel_type = loglevel_type; key.filter_expression = filter_expression; - cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), - ht_match_event, &key, &iter.iter); + cds_lfht_lookup(ht->ht, + ht->hash_fct((void *) name, lttng_ht_seed), + ht_match_event, + &key, + &iter.iter); node = lttng_ht_iter_get_node_str(&iter); - if (node == NULL) { + if (node == nullptr) { goto error; } DBG3("Agent event found %s.", name); - return caa_container_of(node, struct agent_event, node); + return lttng::utils::container_of(node, &agent_event::node); error: DBG3("Agent event NOT found %s.", name); - return NULL; + return nullptr; } /* @@ -1408,11 +1403,9 @@ void agent_destroy_event(struct agent_event *event) free(event); } -static -void destroy_app_ctx_rcu(struct rcu_head *head) +static void destroy_app_ctx_rcu(struct rcu_head *head) { - struct agent_app_ctx *ctx = - caa_container_of(head, struct agent_app_ctx, rcu_node); + struct agent_app_ctx *ctx = lttng::utils::container_of(head, &agent_app_ctx::rcu_node); destroy_app_ctx(ctx); } @@ -1430,31 +1423,35 @@ void agent_destroy(struct agent *agt) DBG3("Agent destroy"); - rcu_read_lock(); - cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) { - int ret; - struct agent_event *event; - - /* - * When destroying an event, we have to try to disable it on the - * agent side so the event stops generating data. The return - * value is not important since we have to continue anyway - * destroying the object. - */ - event = caa_container_of(node, struct agent_event, node); - (void) agent_disable_event(event, agt->domain); + { + lttng::urcu::read_lock_guard read_lock; + + cds_lfht_for_each_entry (agt->events->ht, &iter.iter, node, node) { + int ret; + struct agent_event *event; + + /* + * When destroying an event, we have to try to disable it on the + * agent side so the event stops generating data. The return + * value is not important since we have to continue anyway + * destroying the object. + */ + event = lttng::utils::container_of(node, &agent_event::node); + (void) agent_disable_event(event, agt->domain); + + ret = lttng_ht_del(agt->events, &iter); + LTTNG_ASSERT(!ret); + call_rcu(&node->head, destroy_event_agent_rcu); + } - ret = lttng_ht_del(agt->events, &iter); - LTTNG_ASSERT(!ret); - call_rcu(&node->head, destroy_event_agent_rcu); + cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) + { + (void) disable_context(ctx, agt->domain); + cds_list_del(&ctx->list_node); + call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu); + } } - cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) { - (void) disable_context(ctx, agt->domain); - cds_list_del(&ctx->list_node); - call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu); - } - rcu_read_unlock(); lttng_ht_destroy(agt->events); free(agt); } @@ -1462,7 +1459,7 @@ void agent_destroy(struct agent *agt) /* * Allocate agent_apps_ht_by_sock. */ -int agent_app_ht_alloc(void) +int agent_app_ht_alloc() { the_agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); return the_agent_apps_ht_by_sock ? 0 : -1; @@ -1482,7 +1479,7 @@ void agent_destroy_app_by_sock(int sock) * happen. The hash table deletion is ONLY done through this call when the * main sessiond thread is torn down. */ - rcu_read_lock(); + lttng::urcu::read_lock_guard read_lock; app = agent_find_app_by_sock(sock); LTTNG_ASSERT(app); @@ -1491,13 +1488,12 @@ void agent_destroy_app_by_sock(int sock) /* 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) +void agent_app_ht_clean() { struct lttng_ht_node_ulong *node; struct lttng_ht_iter iter; @@ -1505,15 +1501,17 @@ void agent_app_ht_clean(void) if (!the_agent_apps_ht_by_sock) { return; } - rcu_read_lock(); - cds_lfht_for_each_entry( - the_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); + { + lttng::urcu::read_lock_guard read_lock; + + cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, node, node) { + struct agent_app *app; + + app = lttng::utils::container_of(node, &agent_app::node); + agent_destroy_app_by_sock(app->sock->fd); + } } - rcu_read_unlock(); lttng_ht_destroy(the_agent_apps_ht_by_sock); } @@ -1536,45 +1534,50 @@ void agent_update(const struct agent *agt, const struct agent_app *app) DBG("Agent updating app: pid = %ld", (long) app->pid); - rcu_read_lock(); /* * We are in the registration path thus if the application is gone, * there is a serious code flow error. */ + { + lttng::urcu::read_lock_guard read_lock; - cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { - /* Skip event if disabled. */ - if (!AGENT_EVENT_IS_ENABLED(event)) { - continue; - } + cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) { + /* Skip event if disabled. */ + if (!AGENT_EVENT_IS_ENABLED(event)) { + continue; + } - ret = enable_event(app, event); - if (ret != LTTNG_OK) { - DBG2("Agent update unable to enable event %s on app pid: %d sock %d", - event->name, app->pid, app->sock->fd); - /* Let's try the others here and don't assume the app is dead. */ - continue; + ret = enable_event(app, event); + if (ret != LTTNG_OK) { + DBG2("Agent update unable to enable event %s on app pid: %d sock %d", + event->name, + app->pid, + app->sock->fd); + /* Let's try the others here and don't assume the app is dead. */ + continue; + } } - } - cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) { - ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE); - if (ret != LTTNG_OK) { - DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d", - ctx->provider_name, ctx->ctx_name, - app->pid, app->sock->fd); - continue; + cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) + { + ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE); + if (ret != LTTNG_OK) { + DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d", + ctx->provider_name, + ctx->ctx_name, + app->pid, + app->sock->fd); + continue; + } } } - - rcu_read_unlock(); } /* * Allocate the per-event notifier domain agent hash table. It is lazily * populated as domains are used. */ -int agent_by_event_notifier_domain_ht_create(void) +int agent_by_event_notifier_domain_ht_create() { the_trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64); return the_trigger_agents_ht_by_domain ? 0 : -1; @@ -1583,7 +1586,7 @@ int agent_by_event_notifier_domain_ht_create(void) /* * Clean-up the per-event notifier domain agent hash table and destroy it. */ -void agent_by_event_notifier_domain_ht_destroy(void) +void agent_by_event_notifier_domain_ht_destroy() { struct lttng_ht_node_u64 *node; struct lttng_ht_iter iter; @@ -1592,26 +1595,25 @@ void agent_by_event_notifier_domain_ht_destroy(void) return; } - rcu_read_lock(); - cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain->ht, - &iter.iter, node, node) { - struct agent *agent = - caa_container_of(node, struct agent, node); - const int ret = lttng_ht_del( - the_trigger_agents_ht_by_domain, &iter); + { + lttng::urcu::read_lock_guard read_lock; - LTTNG_ASSERT(ret == 0); - agent_destroy(agent); + cds_lfht_for_each_entry ( + the_trigger_agents_ht_by_domain->ht, &iter.iter, node, node) { + struct agent *agent = lttng::utils::container_of(node, &agent::node); + const int ret = lttng_ht_del(the_trigger_agents_ht_by_domain, &iter); + + LTTNG_ASSERT(ret == 0); + agent_destroy(agent); + } } - rcu_read_unlock(); lttng_ht_destroy(the_trigger_agents_ht_by_domain); } -struct agent *agent_find_by_event_notifier_domain( - enum lttng_domain_type domain_type) +struct agent *agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type) { - struct agent *agt = NULL; + struct agent *agt = nullptr; struct lttng_ht_node_u64 *node; struct lttng_ht_iter iter; const uint64_t key = (uint64_t) domain_type; @@ -1619,7 +1621,7 @@ struct agent *agent_find_by_event_notifier_domain( LTTNG_ASSERT(the_trigger_agents_ht_by_domain); DBG3("Per-event notifier domain agent lookup for domain '%s'", - lttng_domain_type_str(domain_type)); + lttng_domain_type_str(domain_type)); lttng_ht_lookup(the_trigger_agents_ht_by_domain, &key, &iter); node = lttng_ht_iter_get_node_u64(&iter); @@ -1627,7 +1629,7 @@ struct agent *agent_find_by_event_notifier_domain( goto end; } - agt = caa_container_of(node, struct agent, node); + agt = lttng::utils::container_of(node, &agent::node); end: return agt;