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