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