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