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