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