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