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