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