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