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