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