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