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