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