Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / agent.cpp
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include "agent.hpp"
11 #include "common/error.hpp"
12 #include "ust-app.hpp"
13 #include "utils.hpp"
14
15 #include <common/common.hpp>
16 #include <common/compat/endian.hpp>
17 #include <common/sessiond-comm/agent.hpp>
18 #include <common/urcu.hpp>
19
20 #include <lttng/condition/condition.h>
21 #include <lttng/condition/event-rule-matches.h>
22 #include <lttng/domain-internal.hpp>
23 #include <lttng/event-rule/event-rule-internal.hpp>
24 #include <lttng/event-rule/event-rule.h>
25 #include <lttng/event-rule/jul-logging.h>
26 #include <lttng/event-rule/log4j-logging.h>
27 #include <lttng/event-rule/log4j2-logging.h>
28 #include <lttng/event-rule/python-logging.h>
29 #include <lttng/log-level-rule-internal.hpp>
30
31 #include <urcu/rculist.h>
32 #include <urcu/uatomic.h>
33
34 using event_rule_logging_get_name_pattern =
35 enum lttng_event_rule_status (*)(const struct lttng_event_rule *, const char **);
36 using event_rule_logging_get_log_level_rule = enum lttng_event_rule_status (*)(
37 const struct lttng_event_rule *, const struct lttng_log_level_rule **);
38
39 /*
40 * Agent application context representation.
41 */
42 namespace {
43 struct agent_app_ctx {
44 char *provider_name;
45 char *ctx_name;
46
47 /* agent_app_ctx are part of the agent app_ctx_list. */
48 struct cds_list_head list_node;
49
50 /* For call_rcu teardown. */
51 struct rcu_head rcu_node;
52 };
53 } /* namespace */
54
55 /*
56 * Human readable agent return code.
57 */
58 static const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code)
59 {
60 switch (code) {
61 case AGENT_RET_CODE_SUCCESS:
62 return "Success";
63 case AGENT_RET_CODE_INVALID:
64 return "Invalid command";
65 case AGENT_RET_CODE_UNKNOWN_NAME:
66 return "Unknown logger name";
67 default:
68 return "Unknown code";
69 }
70 };
71
72 static void log_reply_code(uint32_t in_reply_ret_code)
73 {
74 int level = PRINT_DBG3;
75 /*
76 * reply_ret_code and in_reply_ret_code are kept separate to have a
77 * sanitized value (used to retrieve the human readable string) and the
78 * original value which is logged as-is.
79 */
80 uint32_t reply_ret_code = in_reply_ret_code;
81
82 if (reply_ret_code < AGENT_RET_CODE_SUCCESS || reply_ret_code >= AGENT_RET_CODE_NR) {
83 reply_ret_code = AGENT_RET_CODE_NR;
84 level = PRINT_ERR;
85 }
86
87 LOG(level,
88 "Agent replied with retcode: %s (%" PRIu32 ")",
89 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code) reply_ret_code),
90 in_reply_ret_code);
91 }
92
93 /*
94 * Match function for the events hash table lookup by name.
95 */
96 static int ht_match_event_by_name(struct cds_lfht_node *node, const void *_key)
97 {
98 LTTNG_ASSERT(node);
99 LTTNG_ASSERT(_key);
100
101 auto *event = lttng_ht_node_container_of(node, &agent_event::node);
102 const auto *key = static_cast<const agent_ht_key *>(_key);
103
104 /* Match 1 elements of the key: name. */
105
106 /* Event name */
107 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
108 goto no_match;
109 }
110 /* Match. */
111 return 1;
112
113 no_match:
114 return 0;
115 }
116
117 /*
118 * Match function for the events hash table lookup by name, log level and
119 * filter expression.
120 */
121 static int ht_match_event(struct cds_lfht_node *node, const void *_key)
122 {
123 LTTNG_ASSERT(node);
124 LTTNG_ASSERT(_key);
125
126 const auto *event = lttng_ht_node_container_of(node, &agent_event::node);
127 const auto *key = (agent_ht_key *) _key;
128 bool ll_match;
129
130 /* Match 2 elements of the key: name and loglevel. */
131
132 /* Event name */
133 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
134 goto no_match;
135 }
136
137 /* Event loglevel value and type. */
138 ll_match = loglevels_match(event->loglevel_type,
139 event->loglevel_value,
140 key->loglevel_type,
141 key->loglevel_value,
142 LTTNG_EVENT_LOGLEVEL_ALL);
143
144 if (!ll_match) {
145 goto no_match;
146 }
147
148 /* Filter expression */
149 if (!!event->filter_expression != !!key->filter_expression) {
150 /* One has a filter expression, the other does not */
151 goto no_match;
152 }
153
154 if (event->filter_expression) {
155 if (strncmp(event->filter_expression,
156 key->filter_expression,
157 strlen(event->filter_expression)) != 0) {
158 goto no_match;
159 }
160 }
161
162 return 1;
163
164 no_match:
165 return 0;
166 }
167
168 /*
169 * Add unique agent event based on the event name and loglevel.
170 */
171 static void add_unique_agent_event(struct lttng_ht *ht, struct agent_event *event)
172 {
173 struct cds_lfht_node *node_ptr;
174 struct agent_ht_key key;
175
176 LTTNG_ASSERT(ht);
177 LTTNG_ASSERT(ht->ht);
178 LTTNG_ASSERT(event);
179
180 key.name = event->name;
181 key.loglevel_value = event->loglevel_value;
182 key.loglevel_type = event->loglevel_type;
183 key.filter_expression = event->filter_expression;
184
185 node_ptr = cds_lfht_add_unique(ht->ht,
186 ht->hash_fct(event->node.key, lttng_ht_seed),
187 ht_match_event,
188 &key,
189 &event->node.node);
190 LTTNG_ASSERT(node_ptr == &event->node.node);
191 }
192
193 /*
194 * URCU delayed agent event reclaim.
195 */
196 static void destroy_event_agent_rcu(struct rcu_head *head)
197 {
198 struct lttng_ht_node_str *node = lttng::utils::container_of(head, &lttng_ht_node_str::head);
199 struct agent_event *event = lttng::utils::container_of(node, &agent_event::node);
200
201 agent_destroy_event(event);
202 }
203
204 /*
205 * URCU delayed agent app reclaim.
206 */
207 static void destroy_app_agent_rcu(struct rcu_head *head)
208 {
209 struct lttng_ht_node_ulong *node =
210 lttng::utils::container_of(head, &lttng_ht_node_ulong::head);
211 struct agent_app *app = lttng::utils::container_of(node, &agent_app::node);
212
213 free(app);
214 }
215
216 /*
217 * Communication with the agent. Send the message header to the given socket in
218 * big endian.
219 *
220 * Return 0 on success or else a negative errno message of sendmsg() op.
221 */
222 static int
223 send_header(struct lttcomm_sock *sock, uint64_t data_size, uint32_t cmd, uint32_t cmd_version)
224 {
225 int ret;
226 ssize_t size;
227 struct lttcomm_agent_hdr msg;
228
229 LTTNG_ASSERT(sock);
230
231 memset(&msg, 0, sizeof(msg));
232 msg.data_size = htobe64(data_size);
233 msg.cmd = htobe32(cmd);
234 msg.cmd_version = htobe32(cmd_version);
235
236 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
237 if (size < sizeof(msg)) {
238 ret = -errno;
239 goto error;
240 }
241 ret = 0;
242
243 error:
244 return ret;
245 }
246
247 /*
248 * Communication call with the agent. Send the payload to the given socket. The
249 * header MUST be sent prior to this call.
250 *
251 * Return 0 on success or else a negative errno value of sendmsg() op.
252 */
253 static int send_payload(struct lttcomm_sock *sock, const void *data, size_t size)
254 {
255 int ret;
256 ssize_t len;
257
258 LTTNG_ASSERT(sock);
259 LTTNG_ASSERT(data);
260
261 len = sock->ops->sendmsg(sock, data, size, 0);
262 if (len < size) {
263 ret = -errno;
264 goto error;
265 }
266 ret = 0;
267
268 error:
269 return ret;
270 }
271
272 /*
273 * Communication call with the agent. Receive reply from the agent using the
274 * given socket.
275 *
276 * Return 0 on success or else a negative errno value from recvmsg() op.
277 */
278 static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
279 {
280 int ret;
281 ssize_t len;
282
283 LTTNG_ASSERT(sock);
284 LTTNG_ASSERT(buf);
285
286 len = sock->ops->recvmsg(sock, buf, size, 0);
287 if (len < size) {
288 ret = -errno;
289 goto error;
290 }
291 ret = 0;
292
293 error:
294 return ret;
295 }
296
297 /*
298 * Internal event listing for a given app. Populate events.
299 *
300 * Return number of element in the list or else a negative LTTNG_ERR* code.
301 * On success, the caller is responsible for freeing the memory
302 * allocated for "events".
303 */
304 static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
305 {
306 int ret, i, len = 0, offset = 0;
307 uint32_t nb_event;
308 size_t data_size;
309 uint32_t reply_ret_code;
310 struct lttng_event *tmp_events = nullptr;
311 struct lttcomm_agent_list_reply *reply = nullptr;
312 struct lttcomm_agent_list_reply_hdr reply_hdr;
313
314 LTTNG_ASSERT(app);
315 LTTNG_ASSERT(app->sock);
316 LTTNG_ASSERT(events);
317
318 DBG2("Agent listing events for app pid: %d and socket %d", app->pid, app->sock->fd);
319
320 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
321 if (ret < 0) {
322 goto error_io;
323 }
324
325 /* Get list header so we know how much we'll receive. */
326 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
327 if (ret < 0) {
328 goto error_io;
329 }
330
331 reply_ret_code = be32toh(reply_hdr.ret_code);
332 log_reply_code(reply_ret_code);
333 switch (reply_ret_code) {
334 case AGENT_RET_CODE_SUCCESS:
335 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
336 break;
337 default:
338 ret = LTTNG_ERR_UNK;
339 goto error;
340 }
341
342 reply = zmalloc<lttcomm_agent_list_reply>(data_size);
343 if (!reply) {
344 ret = LTTNG_ERR_NOMEM;
345 goto error;
346 }
347
348 /* Get the list with the appropriate data size. */
349 ret = recv_reply(app->sock, reply, data_size);
350 if (ret < 0) {
351 goto error_io;
352 }
353
354 nb_event = be32toh(reply->nb_event);
355 tmp_events = calloc<lttng_event>(nb_event);
356 if (!tmp_events) {
357 ret = LTTNG_ERR_NOMEM;
358 goto error;
359 }
360
361 for (i = 0; i < nb_event; i++) {
362 offset += len;
363 if (lttng_strncpy(tmp_events[i].name,
364 reply->payload + offset,
365 sizeof(tmp_events[i].name))) {
366 ret = LTTNG_ERR_INVALID;
367 goto error;
368 }
369 tmp_events[i].pid = app->pid;
370 tmp_events[i].enabled = -1;
371 len = strlen(reply->payload + offset) + 1;
372 }
373
374 *events = tmp_events;
375
376 free(reply);
377 return nb_event;
378
379 error_io:
380 ret = LTTNG_ERR_UST_LIST_FAIL;
381 error:
382 free(reply);
383 free(tmp_events);
384 return -ret;
385 }
386
387 /*
388 * Internal enable agent event on a agent application. This function
389 * communicates with the agent to enable a given event.
390 *
391 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
392 */
393 static int enable_event(const struct agent_app *app, struct agent_event *event)
394 {
395 int ret;
396 char *bytes_to_send;
397 uint64_t data_size;
398 size_t filter_expression_length;
399 uint32_t reply_ret_code;
400 struct lttcomm_agent_enable_event msg;
401 struct lttcomm_agent_generic_reply reply;
402
403 LTTNG_ASSERT(app);
404 LTTNG_ASSERT(app->sock);
405 LTTNG_ASSERT(event);
406
407 DBG2("Agent enabling event %s for app pid: %d and socket %d",
408 event->name,
409 app->pid,
410 app->sock->fd);
411
412 /*
413 * Calculate the payload's size, which is the fixed-size struct followed
414 * by the variable-length filter expression (+1 for the ending \0).
415 */
416 if (!event->filter_expression) {
417 filter_expression_length = 0;
418 } else {
419 filter_expression_length = strlen(event->filter_expression) + 1;
420 }
421 data_size = sizeof(msg) + filter_expression_length;
422
423 memset(&msg, 0, sizeof(msg));
424 msg.loglevel_value = htobe32(event->loglevel_value);
425 msg.loglevel_type = htobe32(event->loglevel_type);
426 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
427 ret = LTTNG_ERR_INVALID;
428 goto error;
429 }
430 msg.filter_expression_length = htobe32(filter_expression_length);
431
432 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
433 if (ret < 0) {
434 goto error_io;
435 }
436
437 bytes_to_send = calloc<char>(data_size);
438 if (!bytes_to_send) {
439 ret = LTTNG_ERR_NOMEM;
440 goto error;
441 }
442
443 memcpy(bytes_to_send, &msg, sizeof(msg));
444 if (filter_expression_length > 0) {
445 memcpy(bytes_to_send + sizeof(msg),
446 event->filter_expression,
447 filter_expression_length);
448 }
449
450 ret = send_payload(app->sock, bytes_to_send, data_size);
451 free(bytes_to_send);
452 if (ret < 0) {
453 goto error_io;
454 }
455
456 ret = recv_reply(app->sock, &reply, sizeof(reply));
457 if (ret < 0) {
458 goto error_io;
459 }
460
461 reply_ret_code = be32toh(reply.ret_code);
462 log_reply_code(reply_ret_code);
463 switch (reply_ret_code) {
464 case AGENT_RET_CODE_SUCCESS:
465 break;
466 case AGENT_RET_CODE_UNKNOWN_NAME:
467 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
468 goto error;
469 default:
470 ret = LTTNG_ERR_UNK;
471 goto error;
472 }
473
474 return LTTNG_OK;
475
476 error_io:
477 ret = LTTNG_ERR_UST_ENABLE_FAIL;
478 error:
479 return ret;
480 }
481
482 /*
483 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
484 */
485 static int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
486 {
487 int ret;
488 uint32_t len_be;
489
490 len_be = htobe32(len);
491 ret = send_payload(sock, &len_be, sizeof(len_be));
492 if (ret) {
493 goto end;
494 }
495
496 ret = send_payload(sock, str, len);
497 if (ret) {
498 goto end;
499 }
500 end:
501 return ret;
502 }
503
504 /*
505 * Internal enable application context on an agent application. This function
506 * communicates with the agent to enable a given application context.
507 *
508 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
509 */
510 static int app_context_op(const struct agent_app *app,
511 const struct agent_app_ctx *ctx,
512 enum lttcomm_agent_command cmd)
513 {
514 int ret;
515 uint32_t reply_ret_code;
516 struct lttcomm_agent_generic_reply reply;
517 size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
518
519 LTTNG_ASSERT(app);
520 LTTNG_ASSERT(app->sock);
521 LTTNG_ASSERT(ctx);
522 LTTNG_ASSERT(cmd == AGENT_CMD_APP_CTX_ENABLE || cmd == AGENT_CMD_APP_CTX_DISABLE);
523
524 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
525 cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
526 ctx->provider_name,
527 ctx->ctx_name,
528 app->pid,
529 app->sock->fd);
530
531 /*
532 * Calculate the payload's size, which consists of the size (u32, BE)
533 * of the provider name, the NULL-terminated provider name string, the
534 * size (u32, BE) of the context name, followed by the NULL-terminated
535 * context name string.
536 */
537 app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
538 app_ctx_name_len = strlen(ctx->ctx_name) + 1;
539 data_size =
540 sizeof(uint32_t) + app_ctx_provider_name_len + sizeof(uint32_t) + app_ctx_name_len;
541
542 ret = send_header(app->sock, data_size, cmd, 0);
543 if (ret < 0) {
544 goto error_io;
545 }
546
547 if (app_ctx_provider_name_len > UINT32_MAX || app_ctx_name_len > UINT32_MAX) {
548 ERR("Application context name > MAX_UINT32");
549 ret = LTTNG_ERR_INVALID;
550 goto error;
551 }
552
553 ret = send_pstring(app->sock, ctx->provider_name, (uint32_t) app_ctx_provider_name_len);
554 if (ret < 0) {
555 goto error_io;
556 }
557
558 ret = send_pstring(app->sock, ctx->ctx_name, (uint32_t) app_ctx_name_len);
559 if (ret < 0) {
560 goto error_io;
561 }
562
563 ret = recv_reply(app->sock, &reply, sizeof(reply));
564 if (ret < 0) {
565 goto error_io;
566 }
567
568 reply_ret_code = be32toh(reply.ret_code);
569 log_reply_code(reply_ret_code);
570 switch (reply_ret_code) {
571 case AGENT_RET_CODE_SUCCESS:
572 break;
573 default:
574 ret = LTTNG_ERR_UNK;
575 goto error;
576 }
577
578 return LTTNG_OK;
579
580 error_io:
581 ret = LTTNG_ERR_UST_ENABLE_FAIL;
582 error:
583 return ret;
584 }
585
586 /*
587 * Internal disable agent event call on a agent application. This function
588 * communicates with the agent to disable a given event.
589 *
590 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
591 */
592 static int disable_event(struct agent_app *app, struct agent_event *event)
593 {
594 int ret;
595 uint64_t data_size;
596 uint32_t reply_ret_code;
597 struct lttcomm_agent_disable_event msg;
598 struct lttcomm_agent_generic_reply reply;
599
600 LTTNG_ASSERT(app);
601 LTTNG_ASSERT(app->sock);
602 LTTNG_ASSERT(event);
603
604 DBG2("Agent disabling event %s for app pid: %d and socket %d",
605 event->name,
606 app->pid,
607 app->sock->fd);
608
609 data_size = sizeof(msg);
610 memset(&msg, 0, sizeof(msg));
611 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
612 ret = LTTNG_ERR_INVALID;
613 goto error;
614 }
615
616 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
617 if (ret < 0) {
618 goto error_io;
619 }
620
621 ret = send_payload(app->sock, &msg, sizeof(msg));
622 if (ret < 0) {
623 goto error_io;
624 }
625
626 ret = recv_reply(app->sock, &reply, sizeof(reply));
627 if (ret < 0) {
628 goto error_io;
629 }
630
631 reply_ret_code = be32toh(reply.ret_code);
632 log_reply_code(reply_ret_code);
633 switch (reply_ret_code) {
634 case AGENT_RET_CODE_SUCCESS:
635 break;
636 case AGENT_RET_CODE_UNKNOWN_NAME:
637 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
638 goto error;
639 default:
640 ret = LTTNG_ERR_UNK;
641 goto error;
642 }
643
644 return LTTNG_OK;
645
646 error_io:
647 ret = LTTNG_ERR_UST_DISABLE_FAIL;
648 error:
649 return ret;
650 }
651
652 /*
653 * Send back the registration DONE command to a given agent application.
654 *
655 * Return 0 on success or else a negative value.
656 */
657 int agent_send_registration_done(struct agent_app *app)
658 {
659 LTTNG_ASSERT(app);
660 LTTNG_ASSERT(app->sock);
661
662 DBG("Agent sending registration done to app socket %d", app->sock->fd);
663
664 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
665 }
666
667 /*
668 * Enable agent event on every agent applications registered with the session
669 * daemon.
670 *
671 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
672 */
673 int agent_enable_event(struct agent_event *event, enum lttng_domain_type domain)
674 {
675 int ret;
676
677 LTTNG_ASSERT(event);
678
679 for (auto *app : lttng::urcu::
680 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
681 *the_agent_apps_ht_by_sock->ht)) {
682 if (app->domain != domain) {
683 continue;
684 }
685
686 /* Enable event on agent application through TCP socket. */
687 ret = enable_event(app, event);
688 if (ret != LTTNG_OK) {
689 goto error;
690 }
691 }
692
693 event->enabled_count++;
694 ret = LTTNG_OK;
695
696 error:
697 return ret;
698 }
699
700 static void destroy_app_ctx(struct agent_app_ctx *ctx)
701 {
702 free(ctx->provider_name);
703 free(ctx->ctx_name);
704 free(ctx);
705 }
706
707 static struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx)
708 {
709 struct agent_app_ctx *agent_ctx = nullptr;
710
711 if (!ctx) {
712 goto end;
713 }
714
715 LTTNG_ASSERT(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
716 agent_ctx = zmalloc<agent_app_ctx>();
717 if (!agent_ctx) {
718 goto end;
719 }
720
721 agent_ctx->provider_name = strdup(ctx->u.app_ctx.provider_name);
722 agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name);
723 if (!agent_ctx->provider_name || !agent_ctx->ctx_name) {
724 destroy_app_ctx(agent_ctx);
725 agent_ctx = nullptr;
726 }
727 end:
728 return agent_ctx;
729 }
730
731 /*
732 * Enable agent context on every agent applications registered with the session
733 * daemon.
734 *
735 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
736 */
737 int agent_enable_context(const struct lttng_event_context *ctx, enum lttng_domain_type domain)
738 {
739 int ret;
740
741 LTTNG_ASSERT(ctx);
742 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
743 ret = LTTNG_ERR_INVALID;
744 goto error;
745 }
746
747 for (auto *app : lttng::urcu::
748 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
749 *the_agent_apps_ht_by_sock->ht)) {
750 struct agent_app_ctx *agent_ctx;
751
752 if (app->domain != domain) {
753 continue;
754 }
755
756 agent_ctx = create_app_ctx(ctx);
757 if (!agent_ctx) {
758 ret = LTTNG_ERR_NOMEM;
759 goto error_unlock;
760 }
761
762 /* Enable event on agent application through TCP socket. */
763 ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE);
764 destroy_app_ctx(agent_ctx);
765 if (ret != LTTNG_OK) {
766 goto error_unlock;
767 }
768 }
769
770 ret = LTTNG_OK;
771
772 error_unlock:
773 error:
774 return ret;
775 }
776
777 /*
778 * Disable agent event on every agent application registered with the session
779 * daemon.
780 *
781 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
782 */
783 int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain)
784 {
785 int ret = LTTNG_OK;
786
787 LTTNG_ASSERT(event);
788 if (!AGENT_EVENT_IS_ENABLED(event)) {
789 goto end;
790 }
791
792 if (--event->enabled_count != 0) {
793 /*
794 * Agent event still enabled. Disable the agent event only when
795 * all "users" have disabled it (event notifiers, event rules,
796 * etc.).
797 */
798 ret = LTTNG_OK;
799 goto end;
800 }
801
802 for (auto *app : lttng::urcu::
803 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
804 *the_agent_apps_ht_by_sock->ht)) {
805 if (app->domain != domain) {
806 continue;
807 }
808
809 /* Enable event on agent application through TCP socket. */
810 ret = disable_event(app, event);
811 if (ret != LTTNG_OK) {
812 goto error;
813 }
814 }
815
816 /* event->enabled_count is now 0. */
817 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event));
818
819 error:
820 end:
821 return ret;
822 }
823
824 /*
825 * Disable agent context on every agent application registered with the session
826 * daemon.
827 *
828 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
829 */
830 static int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain)
831 {
832 int ret = LTTNG_OK;
833
834 LTTNG_ASSERT(ctx);
835 DBG2("Disabling agent application context %s:%s", ctx->provider_name, ctx->ctx_name);
836
837 for (auto *app : lttng::urcu::
838 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
839 *the_agent_apps_ht_by_sock->ht)) {
840 if (app->domain != domain) {
841 continue;
842 }
843
844 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE);
845 if (ret != LTTNG_OK) {
846 goto end;
847 }
848 }
849 end:
850 return ret;
851 }
852
853 /*
854 * Ask every agent for the list of possible event. Events is allocated with the
855 * events of every agent application.
856 *
857 * Return the number of events or else a negative value.
858 */
859 int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain)
860 {
861 int ret;
862 size_t nbmem, count = 0;
863 struct lttng_event *tmp_events = nullptr;
864
865 LTTNG_ASSERT(events);
866
867 DBG2("Agent listing events for domain %d", domain);
868
869 nbmem = UST_APP_EVENT_LIST_SIZE;
870 tmp_events = calloc<lttng_event>(nbmem);
871 if (!tmp_events) {
872 PERROR("zmalloc agent list events");
873 ret = -ENOMEM;
874 goto error;
875 }
876
877 for (auto *app : lttng::urcu::
878 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
879 *the_agent_apps_ht_by_sock->ht)) {
880 ssize_t nb_ev;
881 struct lttng_event *agent_events;
882
883 /* Skip domain not asked by the list. */
884 if (app->domain != domain) {
885 continue;
886 }
887
888 nb_ev = list_events(app, &agent_events);
889 if (nb_ev < 0) {
890 ret = nb_ev;
891 goto error;
892 }
893
894 if (count + nb_ev > nbmem) {
895 /* In case the realloc fails, we free the memory */
896 struct lttng_event *new_tmp_events;
897 size_t new_nbmem;
898
899 new_nbmem = std::max(count + nb_ev, nbmem << 1);
900 DBG2("Reallocating agent event list from %zu to %zu entries",
901 nbmem,
902 new_nbmem);
903 new_tmp_events = (lttng_event *) realloc(
904 tmp_events, new_nbmem * sizeof(*new_tmp_events));
905 if (!new_tmp_events) {
906 PERROR("realloc agent events");
907 ret = -ENOMEM;
908 free(agent_events);
909 goto error;
910 }
911
912 /* Zero the new memory */
913 memset(new_tmp_events + nbmem,
914 0,
915 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
916 nbmem = new_nbmem;
917 tmp_events = new_tmp_events;
918 }
919 memcpy(tmp_events + count, agent_events, nb_ev * sizeof(*tmp_events));
920 free(agent_events);
921 count += nb_ev;
922 }
923
924 ret = count;
925 *events = tmp_events;
926 return ret;
927
928 error:
929 free(tmp_events);
930 return ret;
931 }
932
933 /*
934 * Create a agent app object using the given PID.
935 *
936 * Return newly allocated object or else NULL on error.
937 */
938 struct agent_app *
939 agent_create_app(pid_t pid, enum lttng_domain_type domain, struct lttcomm_sock *sock)
940 {
941 struct agent_app *app;
942
943 LTTNG_ASSERT(sock);
944
945 app = zmalloc<agent_app>();
946 if (!app) {
947 PERROR("Failed to allocate agent application instance");
948 goto error;
949 }
950
951 app->pid = pid;
952 app->domain = domain;
953 app->sock = sock;
954 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
955
956 error:
957 return app;
958 }
959
960 /*
961 * Lookup agent app by socket in the global hash table.
962 *
963 * RCU read side lock MUST be acquired.
964 *
965 * Return object if found else NULL.
966 */
967 struct agent_app *agent_find_app_by_sock(int sock)
968 {
969 struct lttng_ht_node_ulong *node;
970 struct lttng_ht_iter iter;
971 struct agent_app *app;
972
973 LTTNG_ASSERT(sock >= 0);
974 ASSERT_RCU_READ_LOCKED();
975
976 lttng_ht_lookup(the_agent_apps_ht_by_sock, (void *) ((unsigned long) sock), &iter);
977 node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter);
978 if (node == nullptr) {
979 goto error;
980 }
981 app = lttng::utils::container_of(node, &agent_app::node);
982
983 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
984 return app;
985
986 error:
987 DBG3("Agent app NOT found by sock %d.", sock);
988 return nullptr;
989 }
990
991 /*
992 * Add agent application object to the global hash table.
993 */
994 void agent_add_app(struct agent_app *app)
995 {
996 LTTNG_ASSERT(app);
997
998 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
999 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock, &app->node);
1000 }
1001
1002 /*
1003 * Delete agent application from the global hash table.
1004 *
1005 * rcu_read_lock() must be held by the caller.
1006 */
1007 void agent_delete_app(struct agent_app *app)
1008 {
1009 int ret;
1010 struct lttng_ht_iter iter;
1011
1012 LTTNG_ASSERT(app);
1013 ASSERT_RCU_READ_LOCKED();
1014
1015 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
1016
1017 iter.iter.node = &app->node.node;
1018 ret = lttng_ht_del(the_agent_apps_ht_by_sock, &iter);
1019 LTTNG_ASSERT(!ret);
1020 }
1021
1022 /*
1023 * Destroy an agent application object by detaching it from its corresponding
1024 * UST app if one is connected by closing the socket. Finally, perform a
1025 * delayed memory reclaim.
1026 */
1027 void agent_destroy_app(struct agent_app *app)
1028 {
1029 LTTNG_ASSERT(app);
1030
1031 if (app->sock) {
1032 app->sock->ops->close(app->sock);
1033 lttcomm_destroy_sock(app->sock);
1034 }
1035
1036 call_rcu(&app->node.head, destroy_app_agent_rcu);
1037 }
1038
1039 /*
1040 * Initialize an already allocated agent object.
1041 *
1042 * Return 0 on success or else a negative errno value.
1043 */
1044 int agent_init(struct agent *agt)
1045 {
1046 int ret;
1047
1048 LTTNG_ASSERT(agt);
1049
1050 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1051 if (!agt->events) {
1052 ret = -ENOMEM;
1053 goto error;
1054 }
1055 lttng_ht_node_init_u64(&agt->node, agt->domain);
1056
1057 CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
1058 return 0;
1059
1060 error:
1061 return ret;
1062 }
1063
1064 /*
1065 * Add agent object to the given hash table.
1066 */
1067 void agent_add(struct agent *agt, struct lttng_ht *ht)
1068 {
1069 LTTNG_ASSERT(agt);
1070 LTTNG_ASSERT(ht);
1071
1072 DBG3("Agent adding from domain %d", agt->domain);
1073
1074 lttng_ht_add_unique_u64(ht, &agt->node);
1075 }
1076
1077 /*
1078 * Create an agent object for the given domain.
1079 *
1080 * Return the allocated agent or NULL on error.
1081 */
1082 struct agent *agent_create(enum lttng_domain_type domain)
1083 {
1084 int ret;
1085 struct agent *agt;
1086
1087 agt = zmalloc<agent>();
1088 if (!agt) {
1089 goto error;
1090 }
1091 agt->domain = domain;
1092
1093 ret = agent_init(agt);
1094 if (ret < 0) {
1095 free(agt);
1096 agt = nullptr;
1097 goto error;
1098 }
1099
1100 error:
1101 return agt;
1102 }
1103
1104 /*
1105 * Create a newly allocated agent event data structure.
1106 * Ownership of filter_expression is taken.
1107 *
1108 * Return a new object else NULL on error.
1109 */
1110 struct agent_event *agent_create_event(const char *name,
1111 enum lttng_loglevel_type loglevel_type,
1112 int loglevel_value,
1113 struct lttng_bytecode *filter,
1114 char *filter_expression)
1115 {
1116 struct agent_event *event = nullptr;
1117
1118 DBG3("Agent create new event with name %s, loglevel type %d, \
1119 loglevel value %d and filter %s",
1120 name,
1121 loglevel_type,
1122 loglevel_value,
1123 filter_expression ? filter_expression : "NULL");
1124
1125 if (!name) {
1126 ERR("Failed to create agent event; no name provided.");
1127 goto error;
1128 }
1129
1130 event = zmalloc<agent_event>();
1131 if (!event) {
1132 goto error;
1133 }
1134
1135 strncpy(event->name, name, sizeof(event->name));
1136 event->name[sizeof(event->name) - 1] = '\0';
1137 lttng_ht_node_init_str(&event->node, event->name);
1138
1139 event->loglevel_value = loglevel_value;
1140 event->loglevel_type = loglevel_type;
1141 event->filter = filter;
1142 event->filter_expression = filter_expression;
1143 error:
1144 return event;
1145 }
1146
1147 /*
1148 * Unique add of a agent event to an agent object.
1149 */
1150 void agent_add_event(struct agent_event *event, struct agent *agt)
1151 {
1152 LTTNG_ASSERT(event);
1153 LTTNG_ASSERT(agt);
1154 LTTNG_ASSERT(agt->events);
1155
1156 DBG3("Agent adding event %s", event->name);
1157 add_unique_agent_event(agt->events, event);
1158 agt->being_used = 1;
1159 }
1160
1161 /*
1162 * Unique add of a agent context to an agent object.
1163 */
1164 int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt)
1165 {
1166 int ret = LTTNG_OK;
1167 struct agent_app_ctx *agent_ctx = nullptr;
1168
1169 LTTNG_ASSERT(ctx);
1170 LTTNG_ASSERT(agt);
1171 LTTNG_ASSERT(agt->events);
1172 LTTNG_ASSERT(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1173
1174 agent_ctx = create_app_ctx(ctx);
1175 if (!agent_ctx) {
1176 ret = LTTNG_ERR_NOMEM;
1177 goto end;
1178 }
1179
1180 DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name, ctx->u.app_ctx.ctx_name);
1181 cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
1182 end:
1183 return ret;
1184 }
1185
1186 /*
1187 * Find multiple agent events sharing the given name.
1188 *
1189 * RCU read side lock MUST be acquired. It must be held for the
1190 * duration of the iteration.
1191 *
1192 * Sets the given iterator.
1193 */
1194 void agent_find_events_by_name(const char *name, struct agent *agt, struct lttng_ht_iter *iter)
1195 {
1196 struct lttng_ht *ht;
1197 struct agent_ht_key key;
1198
1199 LTTNG_ASSERT(name);
1200 LTTNG_ASSERT(agt);
1201 LTTNG_ASSERT(agt->events);
1202 LTTNG_ASSERT(iter);
1203 ASSERT_RCU_READ_LOCKED();
1204
1205 ht = agt->events;
1206 key.name = name;
1207
1208 cds_lfht_lookup(ht->ht,
1209 ht->hash_fct((void *) name, lttng_ht_seed),
1210 ht_match_event_by_name,
1211 &key,
1212 &iter->iter);
1213 }
1214
1215 /*
1216 * Find the agent event matching a trigger.
1217 *
1218 * RCU read side lock MUST be acquired. It must be held for as long as
1219 * the returned agent_event is used.
1220 *
1221 * Return object if found else NULL.
1222 */
1223 struct agent_event *agent_find_event_by_trigger(const struct lttng_trigger *trigger,
1224 struct agent *agt)
1225 {
1226 enum lttng_condition_status c_status;
1227 enum lttng_event_rule_status er_status;
1228 enum lttng_domain_type domain;
1229 const struct lttng_condition *condition;
1230 const struct lttng_event_rule *rule;
1231 const char *name;
1232 const char *filter_expression;
1233 const struct lttng_log_level_rule *log_level_rule;
1234 /* Unused when loglevel_type is 'ALL'. */
1235 int loglevel_value = 0;
1236 enum lttng_loglevel_type loglevel_type;
1237 event_rule_logging_get_name_pattern logging_get_name_pattern;
1238 event_rule_logging_get_log_level_rule logging_get_log_level_rule;
1239
1240 LTTNG_ASSERT(agt);
1241 LTTNG_ASSERT(agt->events);
1242 ASSERT_RCU_READ_LOCKED();
1243
1244 condition = lttng_trigger_get_const_condition(trigger);
1245
1246 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
1247 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
1248
1249 c_status = lttng_condition_event_rule_matches_get_rule(condition, &rule);
1250 LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK);
1251
1252 switch (lttng_event_rule_get_type(rule)) {
1253 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1254 logging_get_name_pattern = lttng_event_rule_jul_logging_get_name_pattern;
1255 logging_get_log_level_rule = lttng_event_rule_jul_logging_get_log_level_rule;
1256 break;
1257 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1258 logging_get_name_pattern = lttng_event_rule_log4j_logging_get_name_pattern;
1259 logging_get_log_level_rule = lttng_event_rule_log4j_logging_get_log_level_rule;
1260 break;
1261 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING:
1262 logging_get_name_pattern = lttng_event_rule_log4j2_logging_get_name_pattern;
1263 logging_get_log_level_rule = lttng_event_rule_log4j2_logging_get_log_level_rule;
1264 break;
1265 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1266 logging_get_name_pattern = lttng_event_rule_python_logging_get_name_pattern;
1267 logging_get_log_level_rule = lttng_event_rule_python_logging_get_log_level_rule;
1268 break;
1269 default:
1270 abort();
1271 break;
1272 }
1273
1274 domain = lttng_event_rule_get_domain_type(rule);
1275 LTTNG_ASSERT(domain == LTTNG_DOMAIN_JUL || domain == LTTNG_DOMAIN_LOG4J ||
1276 domain == LTTNG_DOMAIN_LOG4J2 || domain == LTTNG_DOMAIN_PYTHON);
1277
1278 /* Get the event's pattern name ('name' in the legacy terminology). */
1279 er_status = logging_get_name_pattern(rule, &name);
1280 LTTNG_ASSERT(er_status == LTTNG_EVENT_RULE_STATUS_OK);
1281
1282 /* Get the internal filter expression. */
1283 filter_expression = lttng_event_rule_get_filter(rule);
1284
1285 /* Map log_level_rule to loglevel value. */
1286 er_status = logging_get_log_level_rule(rule, &log_level_rule);
1287 if (er_status == LTTNG_EVENT_RULE_STATUS_UNSET) {
1288 loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1289 loglevel_value = 0;
1290 } else if (er_status == LTTNG_EVENT_RULE_STATUS_OK) {
1291 lttng_log_level_rule_to_loglevel(log_level_rule, &loglevel_type, &loglevel_value);
1292 } else {
1293 abort();
1294 }
1295
1296 return agent_find_event(name, loglevel_type, loglevel_value, filter_expression, agt);
1297 }
1298
1299 /*
1300 * Get the next agent event duplicate by name. This should be called
1301 * after a call to agent_find_events_by_name() to iterate on events.
1302 *
1303 * The RCU read lock must be held during the iteration and for as long
1304 * as the object the iterator points to remains in use.
1305 */
1306 void agent_event_next_duplicate(const char *name, struct agent *agt, struct lttng_ht_iter *iter)
1307 {
1308 struct agent_ht_key key;
1309
1310 ASSERT_RCU_READ_LOCKED();
1311
1312 key.name = name;
1313
1314 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name, &key, &iter->iter);
1315 }
1316
1317 /*
1318 * Find a agent event in the given agent using name, loglevel and filter.
1319 *
1320 * RCU read side lock MUST be acquired. It must be kept for as long as
1321 * the returned agent_event is used.
1322 *
1323 * Return object if found else NULL.
1324 */
1325 struct agent_event *agent_find_event(const char *name,
1326 enum lttng_loglevel_type loglevel_type,
1327 int loglevel_value,
1328 const char *filter_expression,
1329 struct agent *agt)
1330 {
1331 struct lttng_ht_node_str *node;
1332 struct lttng_ht_iter iter;
1333 struct lttng_ht *ht;
1334 struct agent_ht_key key;
1335
1336 LTTNG_ASSERT(name);
1337 LTTNG_ASSERT(agt);
1338 LTTNG_ASSERT(agt->events);
1339 ASSERT_RCU_READ_LOCKED();
1340
1341 ht = agt->events;
1342 key.name = name;
1343 key.loglevel_value = loglevel_value;
1344 key.loglevel_type = loglevel_type;
1345 key.filter_expression = filter_expression;
1346
1347 cds_lfht_lookup(ht->ht,
1348 ht->hash_fct((void *) name, lttng_ht_seed),
1349 ht_match_event,
1350 &key,
1351 &iter.iter);
1352 node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter);
1353 if (node == nullptr) {
1354 goto error;
1355 }
1356
1357 DBG3("Agent event found %s.", name);
1358 return lttng::utils::container_of(node, &agent_event::node);
1359
1360 error:
1361 DBG3("Agent event NOT found %s.", name);
1362 return nullptr;
1363 }
1364
1365 /*
1366 * Free given agent event. This event must not be globally visible at this
1367 * point (only expected to be used on failure just after event creation). After
1368 * this call, the pointer is not usable anymore.
1369 */
1370 void agent_destroy_event(struct agent_event *event)
1371 {
1372 LTTNG_ASSERT(event);
1373
1374 free(event->filter);
1375 free(event->filter_expression);
1376 free(event->exclusion);
1377 free(event);
1378 }
1379
1380 static void destroy_app_ctx_rcu(struct rcu_head *head)
1381 {
1382 struct agent_app_ctx *ctx = lttng::utils::container_of(head, &agent_app_ctx::rcu_node);
1383
1384 destroy_app_ctx(ctx);
1385 }
1386
1387 /*
1388 * Destroy an agent completely.
1389 */
1390 void agent_destroy(struct agent *agt)
1391 {
1392 LTTNG_ASSERT(agt);
1393
1394 DBG3("Agent destroy");
1395
1396 for (auto *event :
1397 lttng::urcu::lfht_iteration_adapter<agent_event,
1398 decltype(agent_event::node),
1399 &agent_event::node>(*agt->events->ht)) {
1400 int ret;
1401
1402 /*
1403 * When destroying an event, we have to try to disable it on the
1404 * agent side so the event stops generating data. The return
1405 * value is not important since we have to continue anyway
1406 * destroying the object.
1407 */
1408 (void) agent_disable_event(event, agt->domain);
1409
1410 ret = cds_lfht_del(agt->events->ht, &event->node.node);
1411 LTTNG_ASSERT(!ret);
1412 call_rcu(&event->node.head, destroy_event_agent_rcu);
1413 }
1414
1415 agent_app_ctx *ctx;
1416 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node)
1417 {
1418 (void) disable_context(ctx, agt->domain);
1419 cds_list_del(&ctx->list_node);
1420 call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
1421 }
1422
1423 lttng_ht_destroy(agt->events);
1424 free(agt);
1425 }
1426
1427 /*
1428 * Allocate agent_apps_ht_by_sock.
1429 */
1430 int agent_app_ht_alloc()
1431 {
1432 the_agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1433 return the_agent_apps_ht_by_sock ? 0 : -1;
1434 }
1435
1436 /*
1437 * Destroy a agent application by socket.
1438 */
1439 void agent_destroy_app_by_sock(int sock)
1440 {
1441 struct agent_app *app;
1442
1443 LTTNG_ASSERT(sock >= 0);
1444
1445 /*
1446 * Not finding an application is a very important error that should NEVER
1447 * happen. The hash table deletion is ONLY done through this call when the
1448 * main sessiond thread is torn down.
1449 */
1450 const lttng::urcu::read_lock_guard read_lock;
1451 app = agent_find_app_by_sock(sock);
1452 LTTNG_ASSERT(app);
1453
1454 /* RCU read side lock is assumed to be held by this function. */
1455 agent_delete_app(app);
1456
1457 /* The application is freed in a RCU call but the socket is closed here. */
1458 agent_destroy_app(app);
1459 }
1460
1461 /*
1462 * Clean-up the agent app hash table and destroy it.
1463 */
1464 void agent_app_ht_clean()
1465 {
1466 if (!the_agent_apps_ht_by_sock) {
1467 return;
1468 }
1469
1470 for (auto *app : lttng::urcu::
1471 lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
1472 *the_agent_apps_ht_by_sock->ht)) {
1473 agent_destroy_app_by_sock(app->sock->fd);
1474 }
1475
1476 lttng_ht_destroy(the_agent_apps_ht_by_sock);
1477 }
1478
1479 /*
1480 * Update a agent application (given socket) using the given agent.
1481 *
1482 * Note that this function is most likely to be used with a tracing session
1483 * thus the caller should make sure to hold the appropriate lock(s).
1484 */
1485 void agent_update(const struct agent *agt, const struct agent_app *app)
1486 {
1487 int ret;
1488 struct agent_app_ctx *ctx;
1489
1490 LTTNG_ASSERT(agt);
1491 LTTNG_ASSERT(app);
1492
1493 DBG("Agent updating app: pid = %ld", (long) app->pid);
1494
1495 /*
1496 * We are in the registration path thus if the application is gone,
1497 * there is a serious code flow error.
1498 */
1499 for (auto *event :
1500 lttng::urcu::lfht_iteration_adapter<agent_event,
1501 decltype(agent_event::node),
1502 &agent_event::node>(*agt->events->ht)) {
1503 /* Skip event if disabled. */
1504 if (!AGENT_EVENT_IS_ENABLED(event)) {
1505 continue;
1506 }
1507
1508 ret = enable_event(app, event);
1509 if (ret != LTTNG_OK) {
1510 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1511 event->name,
1512 app->pid,
1513 app->sock->fd);
1514 /* Let's try the others here and don't assume the app is dead. */
1515 continue;
1516 }
1517 }
1518
1519 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node)
1520 {
1521 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
1522 if (ret != LTTNG_OK) {
1523 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1524 ctx->provider_name,
1525 ctx->ctx_name,
1526 app->pid,
1527 app->sock->fd);
1528 continue;
1529 }
1530 }
1531 }
1532
1533 /*
1534 * Allocate the per-event notifier domain agent hash table. It is lazily
1535 * populated as domains are used.
1536 */
1537 int agent_by_event_notifier_domain_ht_create()
1538 {
1539 the_trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
1540 return the_trigger_agents_ht_by_domain ? 0 : -1;
1541 }
1542
1543 /*
1544 * Clean-up the per-event notifier domain agent hash table and destroy it.
1545 */
1546 void agent_by_event_notifier_domain_ht_destroy()
1547 {
1548 if (!the_trigger_agents_ht_by_domain) {
1549 return;
1550 }
1551
1552 for (struct agent *agent :
1553 lttng::urcu::lfht_iteration_adapter<struct agent, decltype(agent::node), &agent::node>(
1554 *the_trigger_agents_ht_by_domain->ht)) {
1555 const auto ret =
1556 cds_lfht_del(the_trigger_agents_ht_by_domain->ht, &agent->node.node);
1557
1558 LTTNG_ASSERT(ret == 0);
1559 agent_destroy(agent);
1560 }
1561
1562 lttng_ht_destroy(the_trigger_agents_ht_by_domain);
1563 }
1564
1565 struct agent *agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type)
1566 {
1567 struct agent *agt = nullptr;
1568 struct lttng_ht_node_u64 *node;
1569 struct lttng_ht_iter iter;
1570 const uint64_t key = (uint64_t) domain_type;
1571
1572 LTTNG_ASSERT(the_trigger_agents_ht_by_domain);
1573
1574 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1575 lttng_domain_type_str(domain_type));
1576
1577 lttng_ht_lookup(the_trigger_agents_ht_by_domain, &key, &iter);
1578 node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
1579 if (!node) {
1580 goto end;
1581 }
1582
1583 agt = lttng::utils::container_of(node, &agent::node);
1584
1585 end:
1586 return agt;
1587 }
This page took 0.103359 seconds and 5 git commands to generate.