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