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