| 1 | /* |
| 2 | * event-notifier-notification.c |
| 3 | * |
| 4 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; only |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #define _LGPL_SOURCE |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <errno.h> |
| 25 | #include <lttng/ust-events.h> |
| 26 | #include <lttng/ust-endian.h> |
| 27 | #include <usterr-signal-safe.h> |
| 28 | |
| 29 | #include "../libmsgpack/msgpack.h" |
| 30 | #include "lttng-bytecode.h" |
| 31 | #include "share.h" |
| 32 | |
| 33 | /* |
| 34 | * We want this write to be atomic AND non-blocking, meaning that we |
| 35 | * want to write either everything OR nothing. |
| 36 | * According to `pipe(7)`, writes that are less than `PIPE_BUF` bytes must be |
| 37 | * atomic, so we bound the capture buffer size to the `PIPE_BUF` minus the size |
| 38 | * of the notification struct we are sending alongside the capture buffer. |
| 39 | */ |
| 40 | #define CAPTURE_BUFFER_SIZE \ |
| 41 | (PIPE_BUF - sizeof(struct lttng_ust_event_notifier_notification) - 1) |
| 42 | |
| 43 | struct lttng_event_notifier_notification { |
| 44 | int notification_fd; |
| 45 | uint64_t event_notifier_token; |
| 46 | uint8_t capture_buf[CAPTURE_BUFFER_SIZE]; |
| 47 | struct lttng_msgpack_writer writer; |
| 48 | bool has_captures; |
| 49 | }; |
| 50 | |
| 51 | static |
| 52 | void capture_enum(struct lttng_msgpack_writer *writer, |
| 53 | struct lttng_interpreter_output *output) |
| 54 | { |
| 55 | lttng_msgpack_begin_map(writer, 2); |
| 56 | lttng_msgpack_write_str(writer, "type"); |
| 57 | lttng_msgpack_write_str(writer, "enum"); |
| 58 | |
| 59 | lttng_msgpack_write_str(writer, "value"); |
| 60 | |
| 61 | switch (output->type) { |
| 62 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: |
| 63 | lttng_msgpack_write_signed_integer(writer, output->u.s); |
| 64 | break; |
| 65 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: |
| 66 | lttng_msgpack_write_signed_integer(writer, output->u.u); |
| 67 | break; |
| 68 | default: |
| 69 | abort(); |
| 70 | } |
| 71 | |
| 72 | lttng_msgpack_end_map(writer); |
| 73 | } |
| 74 | |
| 75 | static |
| 76 | int64_t capture_sequence_element_signed(uint8_t *ptr, |
| 77 | const struct lttng_integer_type *type) |
| 78 | { |
| 79 | int64_t value; |
| 80 | unsigned int size = type->size; |
| 81 | bool byte_order_reversed = type->reverse_byte_order; |
| 82 | |
| 83 | switch (size) { |
| 84 | case 8: |
| 85 | value = *ptr; |
| 86 | break; |
| 87 | case 16: |
| 88 | { |
| 89 | int16_t tmp; |
| 90 | tmp = *(int16_t *) ptr; |
| 91 | if (byte_order_reversed) |
| 92 | tmp = bswap_16(tmp); |
| 93 | |
| 94 | value = tmp; |
| 95 | break; |
| 96 | } |
| 97 | case 32: |
| 98 | { |
| 99 | int32_t tmp; |
| 100 | tmp = *(int32_t *) ptr; |
| 101 | if (byte_order_reversed) |
| 102 | tmp = bswap_32(tmp); |
| 103 | |
| 104 | value = tmp; |
| 105 | break; |
| 106 | } |
| 107 | case 64: |
| 108 | { |
| 109 | int64_t tmp; |
| 110 | tmp = *(int64_t *) ptr; |
| 111 | if (byte_order_reversed) |
| 112 | tmp = bswap_64(tmp); |
| 113 | |
| 114 | value = tmp; |
| 115 | break; |
| 116 | } |
| 117 | default: |
| 118 | abort(); |
| 119 | } |
| 120 | |
| 121 | return value; |
| 122 | } |
| 123 | |
| 124 | static |
| 125 | uint64_t capture_sequence_element_unsigned(uint8_t *ptr, |
| 126 | const struct lttng_integer_type *type) |
| 127 | { |
| 128 | uint64_t value; |
| 129 | unsigned int size = type->size; |
| 130 | bool byte_order_reversed = type->reverse_byte_order; |
| 131 | |
| 132 | switch (size) { |
| 133 | case 8: |
| 134 | value = *ptr; |
| 135 | break; |
| 136 | case 16: |
| 137 | { |
| 138 | uint16_t tmp; |
| 139 | tmp = *(uint16_t *) ptr; |
| 140 | if (byte_order_reversed) |
| 141 | tmp = bswap_16(tmp); |
| 142 | |
| 143 | value = tmp; |
| 144 | break; |
| 145 | } |
| 146 | case 32: |
| 147 | { |
| 148 | uint32_t tmp; |
| 149 | tmp = *(uint32_t *) ptr; |
| 150 | if (byte_order_reversed) |
| 151 | tmp = bswap_32(tmp); |
| 152 | |
| 153 | value = tmp; |
| 154 | break; |
| 155 | } |
| 156 | case 64: |
| 157 | { |
| 158 | uint64_t tmp; |
| 159 | tmp = *(uint64_t *) ptr; |
| 160 | if (byte_order_reversed) |
| 161 | tmp = bswap_64(tmp); |
| 162 | |
| 163 | value = tmp; |
| 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | abort(); |
| 168 | } |
| 169 | |
| 170 | return value; |
| 171 | } |
| 172 | |
| 173 | static |
| 174 | void capture_sequence(struct lttng_msgpack_writer *writer, |
| 175 | struct lttng_interpreter_output *output) |
| 176 | { |
| 177 | const struct lttng_integer_type *integer_type; |
| 178 | const struct lttng_type *nested_type; |
| 179 | uint8_t *ptr; |
| 180 | bool signedness; |
| 181 | int i; |
| 182 | |
| 183 | lttng_msgpack_begin_array(writer, output->u.sequence.nr_elem); |
| 184 | |
| 185 | ptr = (uint8_t *) output->u.sequence.ptr; |
| 186 | nested_type = output->u.sequence.nested_type; |
| 187 | switch (nested_type->atype) { |
| 188 | case atype_integer: |
| 189 | integer_type = &nested_type->u.integer; |
| 190 | break; |
| 191 | case atype_enum: |
| 192 | /* Treat enumeration as an integer. */ |
| 193 | integer_type = &nested_type->u.enum_nestable.container_type->u.integer; |
| 194 | break; |
| 195 | default: |
| 196 | /* Capture of array of non-integer are not supported. */ |
| 197 | abort(); |
| 198 | } |
| 199 | signedness = integer_type->signedness; |
| 200 | for (i = 0; i < output->u.sequence.nr_elem; i++) { |
| 201 | if (signedness) { |
| 202 | lttng_msgpack_write_signed_integer(writer, |
| 203 | capture_sequence_element_signed(ptr, integer_type)); |
| 204 | } else { |
| 205 | lttng_msgpack_write_unsigned_integer(writer, |
| 206 | capture_sequence_element_unsigned(ptr, integer_type)); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * We assume that alignment is smaller or equal to the size. |
| 211 | * This currently holds true but if it changes in the future, |
| 212 | * we will want to change the pointer arithmetics below to |
| 213 | * take into account that the next element might be further |
| 214 | * away. |
| 215 | */ |
| 216 | assert(integer_type->alignment <= integer_type->size); |
| 217 | |
| 218 | /* Size is in number of bits. */ |
| 219 | ptr += (integer_type->size / CHAR_BIT) ; |
| 220 | } |
| 221 | |
| 222 | lttng_msgpack_end_array(writer); |
| 223 | } |
| 224 | |
| 225 | static |
| 226 | void notification_init(struct lttng_event_notifier_notification *notif, |
| 227 | struct lttng_event_notifier *event_notifier) |
| 228 | { |
| 229 | struct lttng_msgpack_writer *writer = ¬if->writer; |
| 230 | |
| 231 | notif->event_notifier_token = event_notifier->user_token; |
| 232 | notif->notification_fd = event_notifier->group->notification_fd; |
| 233 | notif->has_captures = false; |
| 234 | |
| 235 | if (event_notifier->num_captures > 0) { |
| 236 | lttng_msgpack_writer_init(writer, notif->capture_buf, |
| 237 | CAPTURE_BUFFER_SIZE); |
| 238 | |
| 239 | lttng_msgpack_begin_array(writer, event_notifier->num_captures); |
| 240 | notif->has_captures = true; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static |
| 245 | void notification_append_capture( |
| 246 | struct lttng_event_notifier_notification *notif, |
| 247 | struct lttng_interpreter_output *output) |
| 248 | { |
| 249 | struct lttng_msgpack_writer *writer = ¬if->writer; |
| 250 | |
| 251 | switch (output->type) { |
| 252 | case LTTNG_INTERPRETER_TYPE_S64: |
| 253 | lttng_msgpack_write_signed_integer(writer, output->u.s); |
| 254 | break; |
| 255 | case LTTNG_INTERPRETER_TYPE_U64: |
| 256 | lttng_msgpack_write_unsigned_integer(writer, output->u.u); |
| 257 | break; |
| 258 | case LTTNG_INTERPRETER_TYPE_DOUBLE: |
| 259 | lttng_msgpack_write_double(writer, output->u.d); |
| 260 | break; |
| 261 | case LTTNG_INTERPRETER_TYPE_STRING: |
| 262 | lttng_msgpack_write_str(writer, output->u.str.str); |
| 263 | break; |
| 264 | case LTTNG_INTERPRETER_TYPE_SEQUENCE: |
| 265 | capture_sequence(writer, output); |
| 266 | break; |
| 267 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: |
| 268 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: |
| 269 | capture_enum(writer, output); |
| 270 | break; |
| 271 | default: |
| 272 | abort(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static |
| 277 | void notification_append_empty_capture( |
| 278 | struct lttng_event_notifier_notification *notif) |
| 279 | { |
| 280 | lttng_msgpack_write_nil(¬if->writer); |
| 281 | } |
| 282 | |
| 283 | static void record_error(struct lttng_event_notifier *event_notifier) |
| 284 | { |
| 285 | struct lttng_event_notifier_group *event_notifier_group = |
| 286 | event_notifier->group; |
| 287 | struct lttng_counter *error_counter; |
| 288 | size_t dimension_index[1]; |
| 289 | int ret; |
| 290 | |
| 291 | error_counter = CMM_LOAD_SHARED(event_notifier_group->error_counter); |
| 292 | /* |
| 293 | * load-acquire paired with store-release orders creation of the |
| 294 | * error counter and setting error_counter_len before the |
| 295 | * error_counter is used. |
| 296 | * Currently a full memory barrier is used, which could be |
| 297 | * turned into acquire-release barriers. |
| 298 | */ |
| 299 | cmm_smp_mb(); |
| 300 | /* This group may not have an error counter attached to it. */ |
| 301 | if (!error_counter) |
| 302 | return; |
| 303 | |
| 304 | dimension_index[0] = event_notifier->error_counter_index; |
| 305 | ret = event_notifier_group->error_counter->ops->counter_add( |
| 306 | error_counter->counter, dimension_index, 1); |
| 307 | if (ret) |
| 308 | WARN_ON_ONCE(1); |
| 309 | } |
| 310 | |
| 311 | static |
| 312 | void notification_send(struct lttng_event_notifier_notification *notif, |
| 313 | struct lttng_event_notifier *event_notifier) |
| 314 | { |
| 315 | ssize_t ret; |
| 316 | size_t content_len; |
| 317 | int iovec_count = 1; |
| 318 | struct lttng_ust_event_notifier_notification ust_notif = {0}; |
| 319 | struct iovec iov[2]; |
| 320 | |
| 321 | assert(notif); |
| 322 | |
| 323 | ust_notif.token = event_notifier->user_token; |
| 324 | |
| 325 | /* |
| 326 | * Prepare sending the notification from multiple buffers using an |
| 327 | * array of `struct iovec`. The first buffer of the vector is |
| 328 | * notification structure itself and is always present. |
| 329 | */ |
| 330 | iov[0].iov_base = &ust_notif; |
| 331 | iov[0].iov_len = sizeof(ust_notif); |
| 332 | |
| 333 | if (notif->has_captures) { |
| 334 | /* |
| 335 | * If captures were requested, the second buffer of the array |
| 336 | * is the capture buffer. |
| 337 | */ |
| 338 | assert(notif->writer.buffer); |
| 339 | content_len = notif->writer.write_pos - notif->writer.buffer; |
| 340 | |
| 341 | assert(content_len > 0 && content_len <= CAPTURE_BUFFER_SIZE); |
| 342 | |
| 343 | iov[1].iov_base = notif->capture_buf; |
| 344 | iov[1].iov_len = content_len; |
| 345 | |
| 346 | iovec_count++; |
| 347 | } else { |
| 348 | content_len = 0; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Update the capture buffer size so that receiver of the buffer will |
| 353 | * know how much to expect. |
| 354 | */ |
| 355 | ust_notif.capture_buf_size = content_len; |
| 356 | |
| 357 | /* Send all the buffers. */ |
| 358 | ret = patient_writev(notif->notification_fd, iov, iovec_count); |
| 359 | if (ret == -1) { |
| 360 | if (errno == EAGAIN) { |
| 361 | record_error(event_notifier); |
| 362 | DBG("Cannot send event_notifier notification without blocking: %s", |
| 363 | strerror(errno)); |
| 364 | } else { |
| 365 | DBG("Error to sending event notifier notification: %s", |
| 366 | strerror(errno)); |
| 367 | abort(); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void lttng_event_notifier_notification_send( |
| 373 | struct lttng_event_notifier *event_notifier, |
| 374 | const char *stack_data) |
| 375 | { |
| 376 | /* |
| 377 | * This function is called from the probe, we must do dynamic |
| 378 | * allocation in this context. |
| 379 | */ |
| 380 | struct lttng_event_notifier_notification notif = {0}; |
| 381 | |
| 382 | notification_init(¬if, event_notifier); |
| 383 | |
| 384 | if (caa_unlikely(!cds_list_empty(&event_notifier->capture_bytecode_runtime_head))) { |
| 385 | struct lttng_bytecode_runtime *capture_bc_runtime; |
| 386 | |
| 387 | /* |
| 388 | * Iterate over all the capture bytecodes. If the interpreter |
| 389 | * functions returns successfully, append the value of the |
| 390 | * `output` parameter to the capture buffer. If the interpreter |
| 391 | * fails, append an empty capture to the buffer. |
| 392 | */ |
| 393 | cds_list_for_each_entry(capture_bc_runtime, |
| 394 | &event_notifier->capture_bytecode_runtime_head, node) { |
| 395 | struct lttng_interpreter_output output; |
| 396 | |
| 397 | if (capture_bc_runtime->interpreter_funcs.capture(capture_bc_runtime, |
| 398 | stack_data, &output) & LTTNG_INTERPRETER_RECORD_FLAG) |
| 399 | notification_append_capture(¬if, &output); |
| 400 | else |
| 401 | notification_append_empty_capture(¬if); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Send the notification (including the capture buffer) to the |
| 407 | * sessiond. |
| 408 | */ |
| 409 | notification_send(¬if, event_notifier); |
| 410 | } |