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