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