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