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