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