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