configure: add '-Wundef' to warning flags
[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);
48b7cdc2 1006 ASSERT_RCU_READ_LOCKED();
f20baf8e 1007
412d7227
SM
1008 lttng_ht_lookup(the_agent_apps_ht_by_sock,
1009 (void *) ((unsigned long) sock), &iter);
f20baf8e
DG
1010 node = lttng_ht_iter_get_node_ulong(&iter);
1011 if (node == NULL) {
1012 goto error;
1013 }
022d91ba 1014 app = caa_container_of(node, struct agent_app, node);
f20baf8e 1015
022d91ba 1016 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
f20baf8e
DG
1017 return app;
1018
1019error:
022d91ba 1020 DBG3("Agent app NOT found by sock %d.", sock);
f20baf8e
DG
1021 return NULL;
1022}
1023
1024/*
022d91ba 1025 * Add agent application object to the global hash table.
f20baf8e 1026 */
022d91ba 1027void agent_add_app(struct agent_app *app)
f20baf8e 1028{
a0377dfe 1029 LTTNG_ASSERT(app);
f20baf8e 1030
022d91ba 1031 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
412d7227 1032 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock, &app->node);
f20baf8e
DG
1033}
1034
f20baf8e 1035/*
022d91ba 1036 * Delete agent application from the global hash table.
d558f236
JG
1037 *
1038 * rcu_read_lock() must be held by the caller.
f20baf8e 1039 */
022d91ba 1040void agent_delete_app(struct agent_app *app)
f20baf8e
DG
1041{
1042 int ret;
1043 struct lttng_ht_iter iter;
1044
a0377dfe 1045 LTTNG_ASSERT(app);
48b7cdc2 1046 ASSERT_RCU_READ_LOCKED();
f20baf8e 1047
022d91ba 1048 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
f20baf8e
DG
1049
1050 iter.iter.node = &app->node.node;
412d7227 1051 ret = lttng_ht_del(the_agent_apps_ht_by_sock, &iter);
a0377dfe 1052 LTTNG_ASSERT(!ret);
f20baf8e
DG
1053}
1054
1055/*
e785906c 1056 * Destroy an agent application object by detaching it from its corresponding
022d91ba 1057 * UST app if one is connected by closing the socket. Finally, perform a
428de77a 1058 * delayed memory reclaim.
f20baf8e 1059 */
022d91ba 1060void agent_destroy_app(struct agent_app *app)
f20baf8e 1061{
a0377dfe 1062 LTTNG_ASSERT(app);
f20baf8e
DG
1063
1064 if (app->sock) {
1065 app->sock->ops->close(app->sock);
1066 lttcomm_destroy_sock(app->sock);
1067 }
1068
022d91ba 1069 call_rcu(&app->node.head, destroy_app_agent_rcu);
f20baf8e
DG
1070}
1071
0475c50c 1072/*
022d91ba 1073 * Initialize an already allocated agent object.
0475c50c
DG
1074 *
1075 * Return 0 on success or else a negative errno value.
1076 */
022d91ba 1077int agent_init(struct agent *agt)
0475c50c
DG
1078{
1079 int ret;
1080
a0377dfe 1081 LTTNG_ASSERT(agt);
0475c50c 1082
022d91ba
DG
1083 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1084 if (!agt->events) {
0475c50c
DG
1085 ret = -ENOMEM;
1086 goto error;
1087 }
fefd409b 1088 lttng_ht_node_init_u64(&agt->node, agt->domain);
0475c50c 1089
bdf64013 1090 CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
0475c50c
DG
1091 return 0;
1092
1093error:
1094 return ret;
1095}
1096
fefd409b
DG
1097/*
1098 * Add agent object to the given hash table.
1099 */
1100void agent_add(struct agent *agt, struct lttng_ht *ht)
1101{
a0377dfe
FD
1102 LTTNG_ASSERT(agt);
1103 LTTNG_ASSERT(ht);
fefd409b
DG
1104
1105 DBG3("Agent adding from domain %d", agt->domain);
1106
fefd409b 1107 lttng_ht_add_unique_u64(ht, &agt->node);
fefd409b
DG
1108}
1109
1110/*
1111 * Create an agent object for the given domain.
1112 *
1113 * Return the allocated agent or NULL on error.
1114 */
1115struct agent *agent_create(enum lttng_domain_type domain)
1116{
1117 int ret;
1118 struct agent *agt;
1119
7966af57 1120 agt = (agent *) zmalloc(sizeof(struct agent));
fefd409b
DG
1121 if (!agt) {
1122 goto error;
1123 }
1124 agt->domain = domain;
1125
1126 ret = agent_init(agt);
1127 if (ret < 0) {
1128 free(agt);
988ae332 1129 agt = NULL;
fefd409b
DG
1130 goto error;
1131 }
1132
1133error:
1134 return agt;
1135}
1136
0475c50c 1137/*
51755dc8
JG
1138 * Create a newly allocated agent event data structure.
1139 * Ownership of filter_expression is taken.
0475c50c
DG
1140 *
1141 * Return a new object else NULL on error.
1142 */
022d91ba 1143struct agent_event *agent_create_event(const char *name,
a9319624 1144 enum lttng_loglevel_type loglevel_type, int loglevel_value,
2b00d462 1145 struct lttng_bytecode *filter, char *filter_expression)
0475c50c 1146{
51755dc8 1147 struct agent_event *event = NULL;
0475c50c 1148
6b10b3b0
AM
1149 DBG3("Agent create new event with name %s, loglevel type %d, \
1150 loglevel value %d and filter %s",
40111ba1
JG
1151 name, loglevel_type, loglevel_value,
1152 filter_expression ? filter_expression : "NULL");
0475c50c 1153
51755dc8
JG
1154 if (!name) {
1155 ERR("Failed to create agent event; no name provided.");
0475c50c
DG
1156 goto error;
1157 }
1158
7966af57 1159 event = (agent_event *) zmalloc(sizeof(*event));
51755dc8
JG
1160 if (!event) {
1161 goto error;
0475c50c
DG
1162 }
1163
51755dc8
JG
1164 strncpy(event->name, name, sizeof(event->name));
1165 event->name[sizeof(event->name) - 1] = '\0';
1166 lttng_ht_node_init_str(&event->node, event->name);
be6a6276 1167
2106efa0 1168 event->loglevel_value = loglevel_value;
51755dc8
JG
1169 event->loglevel_type = loglevel_type;
1170 event->filter = filter;
1171 event->filter_expression = filter_expression;
0475c50c
DG
1172error:
1173 return event;
1174}
1175
1176/*
022d91ba 1177 * Unique add of a agent event to an agent object.
0475c50c 1178 */
022d91ba 1179void agent_add_event(struct agent_event *event, struct agent *agt)
0475c50c 1180{
a0377dfe
FD
1181 LTTNG_ASSERT(event);
1182 LTTNG_ASSERT(agt);
1183 LTTNG_ASSERT(agt->events);
0475c50c 1184
022d91ba 1185 DBG3("Agent adding event %s", event->name);
022d91ba 1186 add_unique_agent_event(agt->events, event);
022d91ba 1187 agt->being_used = 1;
0475c50c
DG
1188}
1189
bdf64013
JG
1190/*
1191 * Unique add of a agent context to an agent object.
1192 */
df4f5a87 1193int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt)
bdf64013
JG
1194{
1195 int ret = LTTNG_OK;
1196 struct agent_app_ctx *agent_ctx = NULL;
1197
a0377dfe
FD
1198 LTTNG_ASSERT(ctx);
1199 LTTNG_ASSERT(agt);
1200 LTTNG_ASSERT(agt->events);
1201 LTTNG_ASSERT(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
bdf64013
JG
1202
1203 agent_ctx = create_app_ctx(ctx);
1204 if (!agent_ctx) {
1205 ret = LTTNG_ERR_NOMEM;
1206 goto end;
1207 }
1208
1209 DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name,
1210 ctx->u.app_ctx.ctx_name);
1211 cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
1212end:
1213 return ret;
1214}
1215
0475c50c 1216/*
e261a6cc 1217 * Find multiple agent events sharing the given name.
4a4ab2c3 1218 *
e261a6cc
PP
1219 * RCU read side lock MUST be acquired. It must be held for the
1220 * duration of the iteration.
4a4ab2c3 1221 *
e261a6cc 1222 * Sets the given iterator.
4a4ab2c3 1223 */
e261a6cc
PP
1224void agent_find_events_by_name(const char *name, struct agent *agt,
1225 struct lttng_ht_iter* iter)
4a4ab2c3 1226{
4a4ab2c3 1227 struct lttng_ht *ht;
022d91ba 1228 struct agent_ht_key key;
4a4ab2c3 1229
a0377dfe
FD
1230 LTTNG_ASSERT(name);
1231 LTTNG_ASSERT(agt);
1232 LTTNG_ASSERT(agt->events);
1233 LTTNG_ASSERT(iter);
48b7cdc2 1234 ASSERT_RCU_READ_LOCKED();
4a4ab2c3 1235
022d91ba 1236 ht = agt->events;
4a4ab2c3
DG
1237 key.name = name;
1238
1239 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
e261a6cc
PP
1240 ht_match_event_by_name, &key, &iter->iter);
1241}
4a4ab2c3 1242
44760c20
JR
1243/*
1244 * Find the agent event matching a trigger.
1245 *
1246 * RCU read side lock MUST be acquired. It must be held for as long as
1247 * the returned agent_event is used.
1248 *
1249 * Return object if found else NULL.
1250 */
1251struct agent_event *agent_find_event_by_trigger(
1252 const struct lttng_trigger *trigger, struct agent *agt)
1253{
1254 enum lttng_condition_status c_status;
1255 enum lttng_event_rule_status er_status;
1256 enum lttng_domain_type domain;
1257 const struct lttng_condition *condition;
1258 const struct lttng_event_rule *rule;
1259 const char *name;
1260 const char *filter_expression;
85b05318 1261 const struct lttng_log_level_rule *log_level_rule;
44760c20
JR
1262 /* Unused when loglevel_type is 'ALL'. */
1263 int loglevel_value = 0;
1264 enum lttng_loglevel_type loglevel_type;
695f7044
JR
1265 event_rule_logging_get_name_pattern logging_get_name_pattern;
1266 event_rule_logging_get_log_level_rule logging_get_log_level_rule;
44760c20 1267
a0377dfe
FD
1268 LTTNG_ASSERT(agt);
1269 LTTNG_ASSERT(agt->events);
48b7cdc2 1270 ASSERT_RCU_READ_LOCKED();
44760c20
JR
1271
1272 condition = lttng_trigger_get_const_condition(trigger);
1273
a0377dfe 1274 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
8dbb86b8 1275 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
44760c20 1276
8dbb86b8
JR
1277 c_status = lttng_condition_event_rule_matches_get_rule(
1278 condition, &rule);
a0377dfe 1279 LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK);
44760c20 1280
695f7044
JR
1281 switch (lttng_event_rule_get_type(rule)) {
1282 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1283 logging_get_name_pattern =
1284 lttng_event_rule_jul_logging_get_name_pattern;
1285 logging_get_log_level_rule =
1286 lttng_event_rule_jul_logging_get_log_level_rule;
1287 break;
1288 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1289 logging_get_name_pattern =
1290 lttng_event_rule_log4j_logging_get_name_pattern;
1291 logging_get_log_level_rule =
1292 lttng_event_rule_log4j_logging_get_log_level_rule;
1293 break;
1294 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1295 logging_get_name_pattern =
1296 lttng_event_rule_python_logging_get_name_pattern;
1297 logging_get_log_level_rule =
1298 lttng_event_rule_python_logging_get_log_level_rule;
1299 break;
1300 default:
1301 abort();
1302 break;
1303 }
44760c20
JR
1304
1305 domain = lttng_event_rule_get_domain_type(rule);
a0377dfe 1306 LTTNG_ASSERT(domain == LTTNG_DOMAIN_JUL || domain == LTTNG_DOMAIN_LOG4J ||
44760c20
JR
1307 domain == LTTNG_DOMAIN_PYTHON);
1308
8bc73626 1309 /* Get the event's pattern name ('name' in the legacy terminology). */
695f7044 1310 er_status = logging_get_name_pattern(rule, &name);
a0377dfe 1311 LTTNG_ASSERT(er_status == LTTNG_EVENT_RULE_STATUS_OK);
44760c20
JR
1312
1313 /* Get the internal filter expression. */
1314 filter_expression = lttng_event_rule_get_filter(rule);
1315
85b05318 1316 /* Map log_level_rule to loglevel value. */
695f7044 1317 er_status = logging_get_log_level_rule(rule, &log_level_rule);
85b05318
JR
1318 if (er_status == LTTNG_EVENT_RULE_STATUS_UNSET) {
1319 loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1320 loglevel_value = 0;
1321 } else if (er_status == LTTNG_EVENT_RULE_STATUS_OK) {
1322 lttng_log_level_rule_to_loglevel(log_level_rule, &loglevel_type, &loglevel_value);
1323 } else {
1324 abort();
44760c20
JR
1325 }
1326
1327 return agent_find_event(name, loglevel_type, loglevel_value,
1328 filter_expression, agt);
1329}
1330
e261a6cc
PP
1331/*
1332 * Get the next agent event duplicate by name. This should be called
1333 * after a call to agent_find_events_by_name() to iterate on events.
1334 *
1335 * The RCU read lock must be held during the iteration and for as long
1336 * as the object the iterator points to remains in use.
1337 */
1338void agent_event_next_duplicate(const char *name,
1339 struct agent *agt, struct lttng_ht_iter* iter)
1340{
1341 struct agent_ht_key key;
4a4ab2c3 1342
48b7cdc2
FD
1343 ASSERT_RCU_READ_LOCKED();
1344
e261a6cc
PP
1345 key.name = name;
1346
1347 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
1348 &key, &iter->iter);
4a4ab2c3
DG
1349}
1350
1351/*
6b10b3b0 1352 * Find a agent event in the given agent using name, loglevel and filter.
0475c50c 1353 *
1e17eae2
JG
1354 * RCU read side lock MUST be acquired. It must be kept for as long as
1355 * the returned agent_event is used.
0475c50c
DG
1356 *
1357 * Return object if found else NULL.
1358 */
a9319624 1359struct agent_event *agent_find_event(const char *name,
44760c20
JR
1360 enum lttng_loglevel_type loglevel_type,
1361 int loglevel_value,
1362 const char *filter_expression,
1363 struct agent *agt)
0475c50c
DG
1364{
1365 struct lttng_ht_node_str *node;
1366 struct lttng_ht_iter iter;
4a4ab2c3 1367 struct lttng_ht *ht;
022d91ba 1368 struct agent_ht_key key;
0475c50c 1369
a0377dfe
FD
1370 LTTNG_ASSERT(name);
1371 LTTNG_ASSERT(agt);
1372 LTTNG_ASSERT(agt->events);
48b7cdc2 1373 ASSERT_RCU_READ_LOCKED();
0475c50c 1374
022d91ba 1375 ht = agt->events;
4a4ab2c3 1376 key.name = name;
2106efa0 1377 key.loglevel_value = loglevel_value;
a9319624 1378 key.loglevel_type = loglevel_type;
6b10b3b0 1379 key.filter_expression = filter_expression;
4a4ab2c3
DG
1380
1381 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1382 ht_match_event, &key, &iter.iter);
0475c50c
DG
1383 node = lttng_ht_iter_get_node_str(&iter);
1384 if (node == NULL) {
1385 goto error;
1386 }
1387
022d91ba
DG
1388 DBG3("Agent event found %s.", name);
1389 return caa_container_of(node, struct agent_event, node);
0475c50c
DG
1390
1391error:
a51e817b 1392 DBG3("Agent event NOT found %s.", name);
0475c50c
DG
1393 return NULL;
1394}
1395
0475c50c 1396/*
022d91ba
DG
1397 * Free given agent event. This event must not be globally visible at this
1398 * point (only expected to be used on failure just after event creation). After
1399 * this call, the pointer is not usable anymore.
0475c50c 1400 */
022d91ba 1401void agent_destroy_event(struct agent_event *event)
0475c50c 1402{
a0377dfe 1403 LTTNG_ASSERT(event);
0475c50c 1404
971da06a 1405 free(event->filter);
8404118c 1406 free(event->filter_expression);
51755dc8 1407 free(event->exclusion);
0475c50c
DG
1408 free(event);
1409}
1410
bdf64013
JG
1411static
1412void destroy_app_ctx_rcu(struct rcu_head *head)
1413{
1414 struct agent_app_ctx *ctx =
1415 caa_container_of(head, struct agent_app_ctx, rcu_node);
1416
1417 destroy_app_ctx(ctx);
1418}
1419
0475c50c 1420/*
35ed21a5 1421 * Destroy an agent completely.
0475c50c 1422 */
022d91ba 1423void agent_destroy(struct agent *agt)
0475c50c
DG
1424{
1425 struct lttng_ht_node_str *node;
1426 struct lttng_ht_iter iter;
bdf64013 1427 struct agent_app_ctx *ctx;
0475c50c 1428
a0377dfe 1429 LTTNG_ASSERT(agt);
0475c50c 1430
022d91ba 1431 DBG3("Agent destroy");
0475c50c 1432
0475c50c 1433 rcu_read_lock();
022d91ba 1434 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
0475c50c 1435 int ret;
022d91ba 1436 struct agent_event *event;
29c0fd4d
DG
1437
1438 /*
bdf64013
JG
1439 * When destroying an event, we have to try to disable it on the
1440 * agent side so the event stops generating data. The return
1441 * value is not important since we have to continue anyway
1442 * destroying the object.
29c0fd4d 1443 */
022d91ba
DG
1444 event = caa_container_of(node, struct agent_event, node);
1445 (void) agent_disable_event(event, agt->domain);
0475c50c 1446
022d91ba 1447 ret = lttng_ht_del(agt->events, &iter);
a0377dfe 1448 LTTNG_ASSERT(!ret);
022d91ba 1449 call_rcu(&node->head, destroy_event_agent_rcu);
0475c50c 1450 }
0475c50c 1451
bdf64013
JG
1452 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1453 (void) disable_context(ctx, agt->domain);
1454 cds_list_del(&ctx->list_node);
1455 call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
1456 }
1457 rcu_read_unlock();
3c339053 1458 lttng_ht_destroy(agt->events);
35ed21a5 1459 free(agt);
f20baf8e
DG
1460}
1461
1462/*
6a4e4039 1463 * Allocate agent_apps_ht_by_sock.
f20baf8e 1464 */
6a4e4039 1465int agent_app_ht_alloc(void)
f20baf8e 1466{
412d7227
SM
1467 the_agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1468 return the_agent_apps_ht_by_sock ? 0 : -1;
6a4e4039
JG
1469}
1470
1471/*
1472 * Destroy a agent application by socket.
1473 */
1474void agent_destroy_app_by_sock(int sock)
1475{
1476 struct agent_app *app;
1477
a0377dfe 1478 LTTNG_ASSERT(sock >= 0);
6a4e4039
JG
1479
1480 /*
1481 * Not finding an application is a very important error that should NEVER
1482 * happen. The hash table deletion is ONLY done through this call when the
1483 * main sessiond thread is torn down.
1484 */
1485 rcu_read_lock();
1486 app = agent_find_app_by_sock(sock);
a0377dfe 1487 LTTNG_ASSERT(app);
6a4e4039
JG
1488
1489 /* RCU read side lock is assumed to be held by this function. */
1490 agent_delete_app(app);
1491
1492 /* The application is freed in a RCU call but the socket is closed here. */
1493 agent_destroy_app(app);
1494 rcu_read_unlock();
1495}
1496
1497/*
1498 * Clean-up the agent app hash table and destroy it.
1499 */
1500void agent_app_ht_clean(void)
1501{
1502 struct lttng_ht_node_ulong *node;
1503 struct lttng_ht_iter iter;
1504
412d7227 1505 if (!the_agent_apps_ht_by_sock) {
a433283e
JG
1506 return;
1507 }
6a4e4039 1508 rcu_read_lock();
412d7227
SM
1509 cds_lfht_for_each_entry(
1510 the_agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
6a4e4039
JG
1511 struct agent_app *app;
1512
1513 app = caa_container_of(node, struct agent_app, node);
1514 agent_destroy_app_by_sock(app->sock->fd);
1515 }
1516 rcu_read_unlock();
1517
412d7227 1518 lttng_ht_destroy(the_agent_apps_ht_by_sock);
f20baf8e
DG
1519}
1520
1521/*
022d91ba 1522 * Update a agent application (given socket) using the given agent.
f20baf8e
DG
1523 *
1524 * Note that this function is most likely to be used with a tracing session
1525 * thus the caller should make sure to hold the appropriate lock(s).
1526 */
733c9165 1527void agent_update(const struct agent *agt, const struct agent_app *app)
f20baf8e
DG
1528{
1529 int ret;
022d91ba 1530 struct agent_event *event;
f20baf8e 1531 struct lttng_ht_iter iter;
bdf64013 1532 struct agent_app_ctx *ctx;
f20baf8e 1533
a0377dfe
FD
1534 LTTNG_ASSERT(agt);
1535 LTTNG_ASSERT(app);
f20baf8e 1536
733c9165 1537 DBG("Agent updating app: pid = %ld", (long) app->pid);
f20baf8e
DG
1538
1539 rcu_read_lock();
bdf64013
JG
1540 /*
1541 * We are in the registration path thus if the application is gone,
1542 * there is a serious code flow error.
1543 */
733c9165 1544
022d91ba 1545 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
f20baf8e 1546 /* Skip event if disabled. */
44760c20 1547 if (!AGENT_EVENT_IS_ENABLED(event)) {
f20baf8e
DG
1548 continue;
1549 }
1550
f20baf8e
DG
1551 ret = enable_event(app, event);
1552 if (ret != LTTNG_OK) {
022d91ba 1553 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
f20baf8e
DG
1554 event->name, app->pid, app->sock->fd);
1555 /* Let's try the others here and don't assume the app is dead. */
1556 continue;
1557 }
1558 }
bdf64013
JG
1559
1560 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1561 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
1562 if (ret != LTTNG_OK) {
1563 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1564 ctx->provider_name, ctx->ctx_name,
1565 app->pid, app->sock->fd);
1566 continue;
1567 }
1568 }
1569
f20baf8e 1570 rcu_read_unlock();
0475c50c 1571}
44760c20
JR
1572
1573/*
1574 * Allocate the per-event notifier domain agent hash table. It is lazily
1575 * populated as domains are used.
1576 */
1577int agent_by_event_notifier_domain_ht_create(void)
1578{
412d7227
SM
1579 the_trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
1580 return the_trigger_agents_ht_by_domain ? 0 : -1;
44760c20
JR
1581}
1582
1583/*
1584 * Clean-up the per-event notifier domain agent hash table and destroy it.
1585 */
1586void agent_by_event_notifier_domain_ht_destroy(void)
1587{
1588 struct lttng_ht_node_u64 *node;
1589 struct lttng_ht_iter iter;
1590
412d7227 1591 if (!the_trigger_agents_ht_by_domain) {
44760c20
JR
1592 return;
1593 }
1594
1595 rcu_read_lock();
412d7227
SM
1596 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain->ht,
1597 &iter.iter, node, node) {
44760c20
JR
1598 struct agent *agent =
1599 caa_container_of(node, struct agent, node);
1600 const int ret = lttng_ht_del(
412d7227 1601 the_trigger_agents_ht_by_domain, &iter);
44760c20 1602
a0377dfe 1603 LTTNG_ASSERT(ret == 0);
44760c20
JR
1604 agent_destroy(agent);
1605 }
1606
1607 rcu_read_unlock();
412d7227 1608 lttng_ht_destroy(the_trigger_agents_ht_by_domain);
44760c20
JR
1609}
1610
1611struct agent *agent_find_by_event_notifier_domain(
1612 enum lttng_domain_type domain_type)
1613{
1614 struct agent *agt = NULL;
1615 struct lttng_ht_node_u64 *node;
1616 struct lttng_ht_iter iter;
1617 const uint64_t key = (uint64_t) domain_type;
1618
a0377dfe 1619 LTTNG_ASSERT(the_trigger_agents_ht_by_domain);
44760c20
JR
1620
1621 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1622 lttng_domain_type_str(domain_type));
1623
412d7227 1624 lttng_ht_lookup(the_trigger_agents_ht_by_domain, &key, &iter);
44760c20
JR
1625 node = lttng_ht_iter_get_node_u64(&iter);
1626 if (!node) {
1627 goto end;
1628 }
1629
1630 agt = caa_container_of(node, struct agent, node);
1631
1632end:
1633 return agt;
1634}
This page took 0.147953 seconds and 4 git commands to generate.