Add the filter expression to the enable_event agent protocol message
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
CommitLineData
0475c50c
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
0475c50c 19#include <assert.h>
f20baf8e 20#include <urcu/uatomic.h>
0475c50c
DG
21
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"
23c2bd47
JG
30#include "error.h"
31
32#define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
33
34/*
35 * Human readable agent return code.
36 */
37static const char *error_string_array[] = {
38 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS) ] = "Success",
39 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID) ] = "Invalid command",
40 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME) ] = "Unknown logger name",
41
42 /* Last element */
43 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR) ] = "Unknown code",
44};
45
46static
47void log_reply_code(uint32_t in_reply_ret_code)
48{
49 int level = PRINT_DBG3;
50 /*
51 * reply_ret_code and in_reply_ret_code are kept separate to have a
52 * sanitized value (used to retrieve the human readable string) and the
53 * original value which is logged as-is.
54 */
55 uint32_t reply_ret_code = in_reply_ret_code;
56
57 if (reply_ret_code < AGENT_RET_CODE_SUCCESS ||
58 reply_ret_code >= AGENT_RET_CODE_NR) {
59 reply_ret_code = AGENT_RET_CODE_NR;
60 level = PRINT_ERR;
61 }
62
63 LOG(level, "Agent replied with retcode: %s (%"PRIu32")",
64 error_string_array[AGENT_RET_CODE_INDEX(
65 reply_ret_code)],
66 in_reply_ret_code);
67}
0475c50c 68
4a4ab2c3
DG
69/*
70 * Match function for the events hash table lookup by name.
71 */
72static int ht_match_event_by_name(struct cds_lfht_node *node,
73 const void *_key)
74{
022d91ba
DG
75 struct agent_event *event;
76 const struct agent_ht_key *key;
4a4ab2c3
DG
77
78 assert(node);
79 assert(_key);
80
022d91ba 81 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
82 key = _key;
83
84 /* Match 1 elements of the key: name. */
85
86 /* Event name */
87 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
88 goto no_match;
89 }
90 /* Match. */
91 return 1;
92
93no_match:
94 return 0;
95}
96
97/*
98 * Match function for the events hash table lookup by name and loglevel.
99 */
100static int ht_match_event(struct cds_lfht_node *node,
101 const void *_key)
102{
022d91ba
DG
103 struct agent_event *event;
104 const struct agent_ht_key *key;
19a97244 105 int ll_match;
4a4ab2c3
DG
106
107 assert(node);
108 assert(_key);
109
022d91ba 110 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
111 key = _key;
112
113 /* Match 2 elements of the key: name and loglevel. */
114
115 /* Event name */
116 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
117 goto no_match;
118 }
119
a9319624 120 /* Event loglevel value and type. */
19a97244
PP
121 ll_match = loglevels_match(event->loglevel_type,
122 event->loglevel_value, key->loglevel_type,
123 key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
124
125 if (!ll_match) {
4a4ab2c3
DG
126 goto no_match;
127 }
a9319624 128
4a4ab2c3
DG
129 return 1;
130
131no_match:
132 return 0;
133}
134
135/*
022d91ba 136 * Add unique agent event based on the event name and loglevel.
4a4ab2c3 137 */
022d91ba
DG
138static void add_unique_agent_event(struct lttng_ht *ht,
139 struct agent_event *event)
4a4ab2c3
DG
140{
141 struct cds_lfht_node *node_ptr;
022d91ba 142 struct agent_ht_key key;
4a4ab2c3
DG
143
144 assert(ht);
145 assert(ht->ht);
146 assert(event);
147
148 key.name = event->name;
2106efa0 149 key.loglevel_value = event->loglevel_value;
a9319624 150 key.loglevel_type = event->loglevel_type;
4a4ab2c3
DG
151
152 node_ptr = cds_lfht_add_unique(ht->ht,
153 ht->hash_fct(event->node.key, lttng_ht_seed),
154 ht_match_event, &key, &event->node.node);
155 assert(node_ptr == &event->node.node);
156}
157
0475c50c 158/*
022d91ba 159 * URCU delayed agent event reclaim.
0475c50c 160 */
022d91ba 161static void destroy_event_agent_rcu(struct rcu_head *head)
0475c50c
DG
162{
163 struct lttng_ht_node_str *node =
164 caa_container_of(head, struct lttng_ht_node_str, head);
022d91ba
DG
165 struct agent_event *event =
166 caa_container_of(node, struct agent_event, node);
0475c50c 167
992febc7 168 agent_destroy_event(event);
0475c50c
DG
169}
170
f20baf8e 171/*
022d91ba 172 * URCU delayed agent app reclaim.
f20baf8e 173 */
022d91ba 174static void destroy_app_agent_rcu(struct rcu_head *head)
f20baf8e
DG
175{
176 struct lttng_ht_node_ulong *node =
177 caa_container_of(head, struct lttng_ht_node_ulong, head);
022d91ba
DG
178 struct agent_app *app =
179 caa_container_of(node, struct agent_app, node);
f20baf8e
DG
180
181 free(app);
182}
183
184/*
022d91ba
DG
185 * Communication with the agent. Send the message header to the given socket in
186 * big endian.
f20baf8e
DG
187 *
188 * Return 0 on success or else a negative errno message of sendmsg() op.
189 */
190static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
191 uint32_t cmd, uint32_t cmd_version)
192{
193 int ret;
194 ssize_t size;
022d91ba 195 struct lttcomm_agent_hdr msg;
f20baf8e
DG
196
197 assert(sock);
198
53efb85a 199 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
200 msg.data_size = htobe64(data_size);
201 msg.cmd = htobe32(cmd);
202 msg.cmd_version = htobe32(cmd_version);
203
204 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
205 if (size < sizeof(msg)) {
206 ret = -errno;
207 goto error;
208 }
209 ret = 0;
210
211error:
212 return ret;
213}
214
215/*
022d91ba
DG
216 * Communication call with the agent. Send the payload to the given socket. The
217 * header MUST be sent prior to this call.
f20baf8e
DG
218 *
219 * Return 0 on success or else a negative errno value of sendmsg() op.
220 */
221static int send_payload(struct lttcomm_sock *sock, void *data,
222 size_t size)
223{
224 int ret;
225 ssize_t len;
226
227 assert(sock);
228 assert(data);
229
230 len = sock->ops->sendmsg(sock, data, size, 0);
231 if (len < size) {
232 ret = -errno;
233 goto error;
234 }
235 ret = 0;
236
237error:
238 return ret;
239}
240
241/*
022d91ba
DG
242 * Communication call with the agent. Receive reply from the agent using the
243 * given socket.
f20baf8e
DG
244 *
245 * Return 0 on success or else a negative errno value from recvmsg() op.
246 */
247static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
248{
249 int ret;
250 ssize_t len;
251
252 assert(sock);
253 assert(buf);
254
255 len = sock->ops->recvmsg(sock, buf, size, 0);
256 if (len < size) {
257 ret = -errno;
258 goto error;
259 }
260 ret = 0;
261
262error:
263 return ret;
264}
265
3c6a091f 266/*
428de77a 267 * Internal event listing for a given app. Populate events.
3c6a091f
DG
268 *
269 * Return number of element in the list or else a negative LTTNG_ERR* code.
428de77a
MD
270 * On success, the caller is responsible for freeing the memory
271 * allocated for "events".
3c6a091f 272 */
022d91ba 273static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
3c6a091f
DG
274{
275 int ret, i, len = 0, offset = 0;
276 uint32_t nb_event;
277 size_t data_size;
d84af1a4 278 uint32_t reply_ret_code;
3c6a091f 279 struct lttng_event *tmp_events = NULL;
022d91ba
DG
280 struct lttcomm_agent_list_reply *reply = NULL;
281 struct lttcomm_agent_list_reply_hdr reply_hdr;
3c6a091f
DG
282
283 assert(app);
284 assert(app->sock);
285 assert(events);
286
022d91ba 287 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
3c6a091f
DG
288 app->sock->fd);
289
022d91ba 290 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
3c6a091f
DG
291 if (ret < 0) {
292 goto error_io;
293 }
294
295 /* Get list header so we know how much we'll receive. */
296 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
297 if (ret < 0) {
298 goto error_io;
299 }
300
d84af1a4
JG
301 reply_ret_code = be32toh(reply_hdr.ret_code);
302 log_reply_code(reply_ret_code);
303 switch (reply_ret_code) {
022d91ba 304 case AGENT_RET_CODE_SUCCESS:
3c6a091f
DG
305 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
306 break;
307 default:
3bd9aaeb 308 ret = LTTNG_ERR_UNK;
3c6a091f
DG
309 goto error;
310 }
311
312 reply = zmalloc(data_size);
313 if (!reply) {
314 ret = LTTNG_ERR_NOMEM;
315 goto error;
316 }
317
318 /* Get the list with the appropriate data size. */
319 ret = recv_reply(app->sock, reply, data_size);
320 if (ret < 0) {
321 goto error_io;
322 }
323
324 nb_event = be32toh(reply->nb_event);
325 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
326 if (!tmp_events) {
327 ret = LTTNG_ERR_NOMEM;
328 goto error;
329 }
330
331 for (i = 0; i < nb_event; i++) {
332 offset += len;
333 strncpy(tmp_events[i].name, reply->payload + offset,
334 sizeof(tmp_events[i].name));
335 tmp_events[i].pid = app->pid;
336 tmp_events[i].enabled = -1;
337 len = strlen(reply->payload + offset) + 1;
338 }
339
340 *events = tmp_events;
341
342 free(reply);
343 return nb_event;
344
345error_io:
346 ret = LTTNG_ERR_UST_LIST_FAIL;
347error:
348 free(reply);
349 free(tmp_events);
350 return -ret;
351
352}
353
f20baf8e 354/*
022d91ba
DG
355 * Internal enable agent event on a agent application. This function
356 * communicates with the agent to enable a given event.
f20baf8e
DG
357 *
358 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
359 */
022d91ba 360static int enable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
361{
362 int ret;
a0ba721c 363 char *bytes_to_send;
f20baf8e 364 uint64_t data_size;
a0ba721c 365 size_t filter_expression_length;
f4f9d4db 366 uint32_t reply_ret_code;
022d91ba
DG
367 struct lttcomm_agent_enable msg;
368 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
369
370 assert(app);
371 assert(app->sock);
372 assert(event);
373
022d91ba 374 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
375 app->pid, app->sock->fd);
376
a0ba721c
AM
377 /*
378 * Calculate the payload's size, which is the fixed-size struct followed
379 * by the variable-length filter expression (+1 for the ending \0).
380 */
381 if (!event->filter_expression) {
382 filter_expression_length = 0;
383 } else {
384 filter_expression_length = strlen(event->filter_expression) + 1;
385 }
386 data_size = sizeof(msg) + filter_expression_length;
f20baf8e 387
022d91ba 388 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
f20baf8e
DG
389 if (ret < 0) {
390 goto error_io;
391 }
392
53efb85a 393 memset(&msg, 0, sizeof(msg));
2106efa0 394 msg.loglevel_value = event->loglevel_value;
b2064f54 395 msg.loglevel_type = event->loglevel_type;
f20baf8e 396 strncpy(msg.name, event->name, sizeof(msg.name));
a0ba721c
AM
397 msg.filter_expression_length = filter_expression_length;
398
399 bytes_to_send = zmalloc(data_size);
400 if (!bytes_to_send) {
401 ret = LTTNG_ERR_NOMEM;
402 goto error;
403 }
404
405 memcpy(bytes_to_send, &msg, sizeof(msg));
406 if (filter_expression_length > 0) {
407 memcpy(bytes_to_send + sizeof(msg), event->filter_expression,
408 filter_expression_length);
409 }
410
411 ret = send_payload(app->sock, bytes_to_send, data_size);
412 free(bytes_to_send);
f20baf8e
DG
413 if (ret < 0) {
414 goto error_io;
415 }
416
417 ret = recv_reply(app->sock, &reply, sizeof(reply));
418 if (ret < 0) {
419 goto error_io;
420 }
421
f4f9d4db
JG
422 reply_ret_code = be32toh(reply.ret_code);
423 log_reply_code(reply_ret_code);
424 switch (reply_ret_code) {
022d91ba 425 case AGENT_RET_CODE_SUCCESS:
f20baf8e 426 break;
022d91ba 427 case AGENT_RET_CODE_UNKNOWN_NAME:
f20baf8e
DG
428 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
429 goto error;
430 default:
3bd9aaeb 431 ret = LTTNG_ERR_UNK;
f20baf8e
DG
432 goto error;
433 }
434
435 return LTTNG_OK;
436
437error_io:
438 ret = LTTNG_ERR_UST_ENABLE_FAIL;
439error:
440 return ret;
441}
442
443/*
022d91ba
DG
444 * Internal disable agent event call on a agent application. This function
445 * communicates with the agent to disable a given event.
f20baf8e
DG
446 *
447 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
448 */
022d91ba 449static int disable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
450{
451 int ret;
452 uint64_t data_size;
517992f8 453 uint32_t reply_ret_code;
022d91ba
DG
454 struct lttcomm_agent_disable msg;
455 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
456
457 assert(app);
458 assert(app->sock);
459 assert(event);
460
022d91ba 461 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
462 app->pid, app->sock->fd);
463
464 data_size = sizeof(msg);
465
022d91ba 466 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
f20baf8e
DG
467 if (ret < 0) {
468 goto error_io;
469 }
470
53efb85a 471 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
472 strncpy(msg.name, event->name, sizeof(msg.name));
473 ret = send_payload(app->sock, &msg, sizeof(msg));
474 if (ret < 0) {
475 goto error_io;
476 }
477
478 ret = recv_reply(app->sock, &reply, sizeof(reply));
479 if (ret < 0) {
480 goto error_io;
481 }
482
517992f8
JG
483 reply_ret_code = be32toh(reply.ret_code);
484 log_reply_code(reply_ret_code);
485 switch (reply_ret_code) {
022d91ba
DG
486 case AGENT_RET_CODE_SUCCESS:
487 break;
488 case AGENT_RET_CODE_UNKNOWN_NAME:
489 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
490 goto error;
491 default:
3bd9aaeb 492 ret = LTTNG_ERR_UNK;
022d91ba 493 goto error;
f20baf8e
DG
494 }
495
496 return LTTNG_OK;
497
498error_io:
499 ret = LTTNG_ERR_UST_DISABLE_FAIL;
500error:
501 return ret;
502}
503
1b500e7a 504/*
022d91ba 505 * Send back the registration DONE command to a given agent application.
1b500e7a
DG
506 *
507 * Return 0 on success or else a negative value.
508 */
022d91ba 509int agent_send_registration_done(struct agent_app *app)
1b500e7a
DG
510{
511 assert(app);
512 assert(app->sock);
513
022d91ba 514 DBG("Agent sending registration done to app socket %d", app->sock->fd);
1b500e7a 515
b26d1f5c 516 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
1b500e7a
DG
517}
518
f20baf8e 519/*
022d91ba 520 * Enable agent event on every agent applications registered with the session
f20baf8e
DG
521 * daemon.
522 *
523 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
524 */
fefd409b
DG
525int agent_enable_event(struct agent_event *event,
526 enum lttng_domain_type domain)
f20baf8e
DG
527{
528 int ret;
022d91ba 529 struct agent_app *app;
f20baf8e
DG
530 struct lttng_ht_iter iter;
531
532 assert(event);
533
534 rcu_read_lock();
535
022d91ba 536 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 537 node.node) {
fefd409b
DG
538 if (app->domain != domain) {
539 continue;
540 }
541
022d91ba 542 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
543 ret = enable_event(app, event);
544 if (ret != LTTNG_OK) {
545 goto error;
546 }
f20baf8e
DG
547 }
548
3c6a091f 549 event->enabled = 1;
f20baf8e
DG
550 ret = LTTNG_OK;
551
552error:
553 rcu_read_unlock();
554 return ret;
555}
556
557/*
022d91ba 558 * Disable agent event on every agent applications registered with the session
f20baf8e
DG
559 * daemon.
560 *
561 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
562 */
fefd409b
DG
563int agent_disable_event(struct agent_event *event,
564 enum lttng_domain_type domain)
f20baf8e 565{
f1bc0129 566 int ret = LTTNG_OK;
022d91ba 567 struct agent_app *app;
f20baf8e
DG
568 struct lttng_ht_iter iter;
569
570 assert(event);
f1bc0129
JG
571 if (!event->enabled) {
572 goto end;
573 }
f20baf8e
DG
574
575 rcu_read_lock();
576
022d91ba 577 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 578 node.node) {
fefd409b
DG
579 if (app->domain != domain) {
580 continue;
581 }
582
022d91ba 583 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
584 ret = disable_event(app, event);
585 if (ret != LTTNG_OK) {
586 goto error;
587 }
f20baf8e
DG
588 }
589
3c6a091f 590 event->enabled = 0;
f20baf8e
DG
591
592error:
593 rcu_read_unlock();
f1bc0129 594end:
f20baf8e
DG
595 return ret;
596}
597
598/*
022d91ba
DG
599 * Ask every agent for the list of possible event. Events is allocated with the
600 * events of every agent application.
f20baf8e
DG
601 *
602 * Return the number of events or else a negative value.
603 */
f60140a1
DG
604int agent_list_events(struct lttng_event **events,
605 enum lttng_domain_type domain)
f20baf8e
DG
606{
607 int ret;
608 size_t nbmem, count = 0;
022d91ba 609 struct agent_app *app;
aae6255e 610 struct lttng_event *tmp_events = NULL;
f20baf8e
DG
611 struct lttng_ht_iter iter;
612
613 assert(events);
614
0e115563
DG
615 DBG2("Agent listing events for domain %d", domain);
616
f20baf8e
DG
617 nbmem = UST_APP_EVENT_LIST_SIZE;
618 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
619 if (!tmp_events) {
022d91ba 620 PERROR("zmalloc agent list events");
f20baf8e
DG
621 ret = -ENOMEM;
622 goto error;
623 }
624
625 rcu_read_lock();
022d91ba 626 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e
DG
627 node.node) {
628 ssize_t nb_ev;
022d91ba 629 struct lttng_event *agent_events;
f20baf8e 630
f60140a1
DG
631 /* Skip domain not asked by the list. */
632 if (app->domain != domain) {
633 continue;
634 }
635
022d91ba 636 nb_ev = list_events(app, &agent_events);
f20baf8e
DG
637 if (nb_ev < 0) {
638 ret = nb_ev;
428de77a 639 goto error_unlock;
f20baf8e
DG
640 }
641
53efb85a 642 if (count + nb_ev > nbmem) {
f20baf8e 643 /* In case the realloc fails, we free the memory */
53efb85a
MD
644 struct lttng_event *new_tmp_events;
645 size_t new_nbmem;
646
647 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
022d91ba 648 DBG2("Reallocating agent event list from %zu to %zu entries",
53efb85a
MD
649 nbmem, new_nbmem);
650 new_tmp_events = realloc(tmp_events,
651 new_nbmem * sizeof(*new_tmp_events));
652 if (!new_tmp_events) {
022d91ba 653 PERROR("realloc agent events");
f20baf8e 654 ret = -ENOMEM;
022d91ba 655 free(agent_events);
428de77a 656 goto error_unlock;
f20baf8e 657 }
53efb85a
MD
658 /* Zero the new memory */
659 memset(new_tmp_events + nbmem, 0,
660 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
661 nbmem = new_nbmem;
662 tmp_events = new_tmp_events;
f20baf8e 663 }
022d91ba 664 memcpy(tmp_events + count, agent_events,
53efb85a 665 nb_ev * sizeof(*tmp_events));
022d91ba 666 free(agent_events);
f20baf8e
DG
667 count += nb_ev;
668 }
669 rcu_read_unlock();
670
671 ret = count;
672 *events = tmp_events;
aae6255e 673 return ret;
f20baf8e 674
428de77a
MD
675error_unlock:
676 rcu_read_unlock();
f20baf8e 677error:
aae6255e 678 free(tmp_events);
f20baf8e
DG
679 return ret;
680}
681
682/*
022d91ba 683 * Create a agent app object using the given PID.
f20baf8e
DG
684 *
685 * Return newly allocated object or else NULL on error.
686 */
fefd409b
DG
687struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
688 struct lttcomm_sock *sock)
f20baf8e 689{
022d91ba 690 struct agent_app *app;
f20baf8e
DG
691
692 assert(sock);
693
694 app = zmalloc(sizeof(*app));
695 if (!app) {
022d91ba 696 PERROR("zmalloc agent create");
f20baf8e
DG
697 goto error;
698 }
699
700 app->pid = pid;
fefd409b 701 app->domain = domain;
f20baf8e 702 app->sock = sock;
f20baf8e
DG
703 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
704
705error:
706 return app;
707}
708
709/*
022d91ba 710 * Lookup agent app by socket in the global hash table.
f20baf8e
DG
711 *
712 * RCU read side lock MUST be acquired.
713 *
714 * Return object if found else NULL.
715 */
022d91ba 716struct agent_app *agent_find_app_by_sock(int sock)
f20baf8e
DG
717{
718 struct lttng_ht_node_ulong *node;
719 struct lttng_ht_iter iter;
022d91ba 720 struct agent_app *app;
f20baf8e
DG
721
722 assert(sock >= 0);
723
022d91ba 724 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
f20baf8e
DG
725 node = lttng_ht_iter_get_node_ulong(&iter);
726 if (node == NULL) {
727 goto error;
728 }
022d91ba 729 app = caa_container_of(node, struct agent_app, node);
f20baf8e 730
022d91ba 731 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
f20baf8e
DG
732 return app;
733
734error:
022d91ba 735 DBG3("Agent app NOT found by sock %d.", sock);
f20baf8e
DG
736 return NULL;
737}
738
739/*
022d91ba 740 * Add agent application object to the global hash table.
f20baf8e 741 */
022d91ba 742void agent_add_app(struct agent_app *app)
f20baf8e
DG
743{
744 assert(app);
745
022d91ba 746 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
022d91ba 747 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
f20baf8e
DG
748}
749
f20baf8e 750/*
022d91ba 751 * Delete agent application from the global hash table.
d558f236
JG
752 *
753 * rcu_read_lock() must be held by the caller.
f20baf8e 754 */
022d91ba 755void agent_delete_app(struct agent_app *app)
f20baf8e
DG
756{
757 int ret;
758 struct lttng_ht_iter iter;
759
760 assert(app);
761
022d91ba 762 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
f20baf8e
DG
763
764 iter.iter.node = &app->node.node;
022d91ba 765 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
f20baf8e
DG
766 assert(!ret);
767}
768
769/*
e785906c 770 * Destroy an agent application object by detaching it from its corresponding
022d91ba 771 * UST app if one is connected by closing the socket. Finally, perform a
428de77a 772 * delayed memory reclaim.
f20baf8e 773 */
022d91ba 774void agent_destroy_app(struct agent_app *app)
f20baf8e
DG
775{
776 assert(app);
777
778 if (app->sock) {
779 app->sock->ops->close(app->sock);
780 lttcomm_destroy_sock(app->sock);
781 }
782
022d91ba 783 call_rcu(&app->node.head, destroy_app_agent_rcu);
f20baf8e
DG
784}
785
0475c50c 786/*
022d91ba 787 * Initialize an already allocated agent object.
0475c50c
DG
788 *
789 * Return 0 on success or else a negative errno value.
790 */
022d91ba 791int agent_init(struct agent *agt)
0475c50c
DG
792{
793 int ret;
794
022d91ba 795 assert(agt);
0475c50c 796
022d91ba
DG
797 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
798 if (!agt->events) {
0475c50c
DG
799 ret = -ENOMEM;
800 goto error;
801 }
fefd409b 802 lttng_ht_node_init_u64(&agt->node, agt->domain);
0475c50c
DG
803
804 return 0;
805
806error:
807 return ret;
808}
809
fefd409b
DG
810/*
811 * Add agent object to the given hash table.
812 */
813void agent_add(struct agent *agt, struct lttng_ht *ht)
814{
815 assert(agt);
816 assert(ht);
817
818 DBG3("Agent adding from domain %d", agt->domain);
819
fefd409b 820 lttng_ht_add_unique_u64(ht, &agt->node);
fefd409b
DG
821}
822
823/*
824 * Create an agent object for the given domain.
825 *
826 * Return the allocated agent or NULL on error.
827 */
828struct agent *agent_create(enum lttng_domain_type domain)
829{
830 int ret;
831 struct agent *agt;
832
3b5f70d4 833 agt = zmalloc(sizeof(struct agent));
fefd409b
DG
834 if (!agt) {
835 goto error;
836 }
837 agt->domain = domain;
838
839 ret = agent_init(agt);
840 if (ret < 0) {
841 free(agt);
988ae332 842 agt = NULL;
fefd409b
DG
843 goto error;
844 }
845
846error:
847 return agt;
848}
849
0475c50c 850/*
51755dc8
JG
851 * Create a newly allocated agent event data structure.
852 * Ownership of filter_expression is taken.
0475c50c
DG
853 *
854 * Return a new object else NULL on error.
855 */
022d91ba 856struct agent_event *agent_create_event(const char *name,
a9319624 857 enum lttng_loglevel_type loglevel_type, int loglevel_value,
51755dc8 858 struct lttng_filter_bytecode *filter, char *filter_expression)
0475c50c 859{
51755dc8 860 struct agent_event *event = NULL;
0475c50c 861
a9319624
PP
862 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
863 name, loglevel_type, loglevel_value);
0475c50c 864
51755dc8
JG
865 if (!name) {
866 ERR("Failed to create agent event; no name provided.");
0475c50c
DG
867 goto error;
868 }
869
51755dc8
JG
870 event = zmalloc(sizeof(*event));
871 if (!event) {
872 goto error;
0475c50c
DG
873 }
874
51755dc8
JG
875 strncpy(event->name, name, sizeof(event->name));
876 event->name[sizeof(event->name) - 1] = '\0';
877 lttng_ht_node_init_str(&event->node, event->name);
be6a6276 878
2106efa0 879 event->loglevel_value = loglevel_value;
51755dc8
JG
880 event->loglevel_type = loglevel_type;
881 event->filter = filter;
882 event->filter_expression = filter_expression;
0475c50c
DG
883error:
884 return event;
885}
886
887/*
022d91ba 888 * Unique add of a agent event to an agent object.
0475c50c 889 */
022d91ba 890void agent_add_event(struct agent_event *event, struct agent *agt)
0475c50c
DG
891{
892 assert(event);
022d91ba
DG
893 assert(agt);
894 assert(agt->events);
0475c50c 895
022d91ba 896 DBG3("Agent adding event %s", event->name);
022d91ba 897 add_unique_agent_event(agt->events, event);
022d91ba 898 agt->being_used = 1;
0475c50c
DG
899}
900
901/*
e261a6cc 902 * Find multiple agent events sharing the given name.
4a4ab2c3 903 *
e261a6cc
PP
904 * RCU read side lock MUST be acquired. It must be held for the
905 * duration of the iteration.
4a4ab2c3 906 *
e261a6cc 907 * Sets the given iterator.
4a4ab2c3 908 */
e261a6cc
PP
909void agent_find_events_by_name(const char *name, struct agent *agt,
910 struct lttng_ht_iter* iter)
4a4ab2c3 911{
4a4ab2c3 912 struct lttng_ht *ht;
022d91ba 913 struct agent_ht_key key;
4a4ab2c3
DG
914
915 assert(name);
022d91ba
DG
916 assert(agt);
917 assert(agt->events);
e261a6cc 918 assert(iter);
4a4ab2c3 919
022d91ba 920 ht = agt->events;
4a4ab2c3
DG
921 key.name = name;
922
923 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
e261a6cc
PP
924 ht_match_event_by_name, &key, &iter->iter);
925}
4a4ab2c3 926
e261a6cc
PP
927/*
928 * Get the next agent event duplicate by name. This should be called
929 * after a call to agent_find_events_by_name() to iterate on events.
930 *
931 * The RCU read lock must be held during the iteration and for as long
932 * as the object the iterator points to remains in use.
933 */
934void agent_event_next_duplicate(const char *name,
935 struct agent *agt, struct lttng_ht_iter* iter)
936{
937 struct agent_ht_key key;
4a4ab2c3 938
e261a6cc
PP
939 key.name = name;
940
941 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
942 &key, &iter->iter);
4a4ab2c3
DG
943}
944
945/*
022d91ba 946 * Find a agent event in the given agent using name and loglevel.
0475c50c 947 *
1e17eae2
JG
948 * RCU read side lock MUST be acquired. It must be kept for as long as
949 * the returned agent_event is used.
0475c50c
DG
950 *
951 * Return object if found else NULL.
952 */
a9319624
PP
953struct agent_event *agent_find_event(const char *name,
954 enum lttng_loglevel_type loglevel_type, int loglevel_value,
022d91ba 955 struct agent *agt)
0475c50c
DG
956{
957 struct lttng_ht_node_str *node;
958 struct lttng_ht_iter iter;
4a4ab2c3 959 struct lttng_ht *ht;
022d91ba 960 struct agent_ht_key key;
0475c50c
DG
961
962 assert(name);
022d91ba
DG
963 assert(agt);
964 assert(agt->events);
0475c50c 965
022d91ba 966 ht = agt->events;
4a4ab2c3 967 key.name = name;
2106efa0 968 key.loglevel_value = loglevel_value;
a9319624 969 key.loglevel_type = loglevel_type;
4a4ab2c3
DG
970
971 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
972 ht_match_event, &key, &iter.iter);
0475c50c
DG
973 node = lttng_ht_iter_get_node_str(&iter);
974 if (node == NULL) {
975 goto error;
976 }
977
022d91ba
DG
978 DBG3("Agent event found %s.", name);
979 return caa_container_of(node, struct agent_event, node);
0475c50c
DG
980
981error:
a51e817b 982 DBG3("Agent event NOT found %s.", name);
0475c50c
DG
983 return NULL;
984}
985
0475c50c 986/*
022d91ba
DG
987 * Free given agent event. This event must not be globally visible at this
988 * point (only expected to be used on failure just after event creation). After
989 * this call, the pointer is not usable anymore.
0475c50c 990 */
022d91ba 991void agent_destroy_event(struct agent_event *event)
0475c50c
DG
992{
993 assert(event);
994
971da06a 995 free(event->filter);
8404118c 996 free(event->filter_expression);
51755dc8 997 free(event->exclusion);
0475c50c
DG
998 free(event);
999}
1000
1001/*
35ed21a5 1002 * Destroy an agent completely.
0475c50c 1003 */
022d91ba 1004void agent_destroy(struct agent *agt)
0475c50c
DG
1005{
1006 struct lttng_ht_node_str *node;
1007 struct lttng_ht_iter iter;
1008
022d91ba 1009 assert(agt);
0475c50c 1010
022d91ba 1011 DBG3("Agent destroy");
0475c50c 1012
0475c50c 1013 rcu_read_lock();
022d91ba 1014 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
0475c50c 1015 int ret;
022d91ba 1016 struct agent_event *event;
29c0fd4d
DG
1017
1018 /*
1019 * When destroying an event, we have to try to disable it on the agent
1020 * side so the event stops generating data. The return value is not
1021 * important since we have to continue anyway destroying the object.
1022 */
022d91ba
DG
1023 event = caa_container_of(node, struct agent_event, node);
1024 (void) agent_disable_event(event, agt->domain);
0475c50c 1025
022d91ba 1026 ret = lttng_ht_del(agt->events, &iter);
0475c50c 1027 assert(!ret);
022d91ba 1028 call_rcu(&node->head, destroy_event_agent_rcu);
0475c50c
DG
1029 }
1030 rcu_read_unlock();
1031
f8be902b 1032 ht_cleanup_push(agt->events);
35ed21a5 1033 free(agt);
f20baf8e
DG
1034}
1035
1036/*
6a4e4039 1037 * Allocate agent_apps_ht_by_sock.
f20baf8e 1038 */
6a4e4039 1039int agent_app_ht_alloc(void)
f20baf8e 1040{
6a4e4039
JG
1041 int ret = 0;
1042
022d91ba
DG
1043 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1044 if (!agent_apps_ht_by_sock) {
6a4e4039 1045 ret = -1;
f20baf8e
DG
1046 }
1047
6a4e4039
JG
1048 return ret;
1049}
1050
1051/*
1052 * Destroy a agent application by socket.
1053 */
1054void agent_destroy_app_by_sock(int sock)
1055{
1056 struct agent_app *app;
1057
1058 assert(sock >= 0);
1059
1060 /*
1061 * Not finding an application is a very important error that should NEVER
1062 * happen. The hash table deletion is ONLY done through this call when the
1063 * main sessiond thread is torn down.
1064 */
1065 rcu_read_lock();
1066 app = agent_find_app_by_sock(sock);
1067 assert(app);
1068
1069 /* RCU read side lock is assumed to be held by this function. */
1070 agent_delete_app(app);
1071
1072 /* The application is freed in a RCU call but the socket is closed here. */
1073 agent_destroy_app(app);
1074 rcu_read_unlock();
1075}
1076
1077/*
1078 * Clean-up the agent app hash table and destroy it.
1079 */
1080void agent_app_ht_clean(void)
1081{
1082 struct lttng_ht_node_ulong *node;
1083 struct lttng_ht_iter iter;
1084
a433283e
JG
1085 if (!agent_apps_ht_by_sock) {
1086 return;
1087 }
6a4e4039
JG
1088 rcu_read_lock();
1089 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1090 struct agent_app *app;
1091
1092 app = caa_container_of(node, struct agent_app, node);
1093 agent_destroy_app_by_sock(app->sock->fd);
1094 }
1095 rcu_read_unlock();
1096
1097 lttng_ht_destroy(agent_apps_ht_by_sock);
f20baf8e
DG
1098}
1099
1100/*
022d91ba 1101 * Update a agent application (given socket) using the given agent.
f20baf8e
DG
1102 *
1103 * Note that this function is most likely to be used with a tracing session
1104 * thus the caller should make sure to hold the appropriate lock(s).
1105 */
022d91ba 1106void agent_update(struct agent *agt, int sock)
f20baf8e
DG
1107{
1108 int ret;
022d91ba
DG
1109 struct agent_app *app;
1110 struct agent_event *event;
f20baf8e
DG
1111 struct lttng_ht_iter iter;
1112
022d91ba 1113 assert(agt);
f20baf8e
DG
1114 assert(sock >= 0);
1115
022d91ba 1116 DBG("Agent updating app socket %d", sock);
f20baf8e
DG
1117
1118 rcu_read_lock();
022d91ba 1119 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
f20baf8e
DG
1120 /* Skip event if disabled. */
1121 if (!event->enabled) {
1122 continue;
1123 }
1124
022d91ba 1125 app = agent_find_app_by_sock(sock);
f20baf8e
DG
1126 /*
1127 * We are in the registration path thus if the application is gone,
1128 * there is a serious code flow error.
1129 */
1130 assert(app);
1131
1132 ret = enable_event(app, event);
1133 if (ret != LTTNG_OK) {
022d91ba 1134 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
f20baf8e
DG
1135 event->name, app->pid, app->sock->fd);
1136 /* Let's try the others here and don't assume the app is dead. */
1137 continue;
1138 }
1139 }
1140 rcu_read_unlock();
0475c50c 1141}
This page took 0.086004 seconds and 4 git commands to generate.