| 1 | #ifndef BABELTRACE_COMMON_INTERNAL_H |
| 2 | #define BABELTRACE_COMMON_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation |
| 6 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <stdbool.h> |
| 28 | #include <babeltrace/assert-internal.h> |
| 29 | #include <babeltrace/babeltrace-internal.h> |
| 30 | #include <babeltrace/trace-ir/field-class-const.h> |
| 31 | #include <babeltrace/trace-ir/field-path-const.h> |
| 32 | #include <babeltrace/trace-ir/event-class-const.h> |
| 33 | #include <babeltrace/graph/self-message-iterator.h> |
| 34 | #include <babeltrace/value.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <inttypes.h> |
| 37 | #include <stdint.h> |
| 38 | #include <glib.h> |
| 39 | |
| 40 | #define BT_COMMON_COLOR_RESET "\033[0m" |
| 41 | #define BT_COMMON_COLOR_BOLD "\033[1m" |
| 42 | #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m" |
| 43 | #define BT_COMMON_COLOR_FG_RED "\033[31m" |
| 44 | #define BT_COMMON_COLOR_FG_GREEN "\033[32m" |
| 45 | #define BT_COMMON_COLOR_FG_YELLOW "\033[33m" |
| 46 | #define BT_COMMON_COLOR_FG_BLUE "\033[34m" |
| 47 | #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m" |
| 48 | #define BT_COMMON_COLOR_FG_CYAN "\033[36m" |
| 49 | #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m" |
| 50 | #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m" |
| 51 | #define BT_COMMON_COLOR_BG_RED "\033[41m" |
| 52 | #define BT_COMMON_COLOR_BG_GREEN "\033[42m" |
| 53 | #define BT_COMMON_COLOR_BG_YELLOW "\033[43m" |
| 54 | #define BT_COMMON_COLOR_BG_BLUE "\033[44m" |
| 55 | #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m" |
| 56 | #define BT_COMMON_COLOR_BG_CYAN "\033[46m" |
| 57 | #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m" |
| 58 | |
| 59 | struct bt_common_lttng_live_url_parts { |
| 60 | GString *proto; |
| 61 | GString *hostname; |
| 62 | GString *target_hostname; |
| 63 | GString *session_name; |
| 64 | |
| 65 | /* -1 means default port */ |
| 66 | int port; |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * Checks if the current process has setuid or setgid access rights. |
| 71 | * Returns `true` if so. |
| 72 | */ |
| 73 | BT_HIDDEN |
| 74 | bool bt_common_is_setuid_setgid(void); |
| 75 | |
| 76 | /* |
| 77 | * Returns the system-wide plugin path, e.g. |
| 78 | * `/usr/lib/babeltrace/plugins`. Do not free the return value. |
| 79 | */ |
| 80 | BT_HIDDEN |
| 81 | const char *bt_common_get_system_plugin_path(void); |
| 82 | |
| 83 | /* |
| 84 | * Returns the user plugin path, e.g. |
| 85 | * `/home/user/.local/lib/babeltrace/plugins`. You need to free the |
| 86 | * return value. |
| 87 | */ |
| 88 | BT_HIDDEN |
| 89 | char *bt_common_get_home_plugin_path(void); |
| 90 | |
| 91 | /* |
| 92 | * Appends the list of directories in `paths` to the array `dirs`. |
| 93 | * `paths` is a list of directories separated by `:`. Returns 0 on |
| 94 | * success. |
| 95 | */ |
| 96 | BT_HIDDEN |
| 97 | int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs); |
| 98 | |
| 99 | /* |
| 100 | * Returns `true` if terminal color codes are supported for this |
| 101 | * process. |
| 102 | */ |
| 103 | BT_HIDDEN |
| 104 | bool bt_common_colors_supported(void); |
| 105 | |
| 106 | BT_HIDDEN |
| 107 | const char *bt_common_color_reset(void); |
| 108 | |
| 109 | BT_HIDDEN |
| 110 | const char *bt_common_color_bold(void); |
| 111 | |
| 112 | BT_HIDDEN |
| 113 | const char *bt_common_color_fg_default(void); |
| 114 | |
| 115 | BT_HIDDEN |
| 116 | const char *bt_common_color_fg_red(void); |
| 117 | |
| 118 | BT_HIDDEN |
| 119 | const char *bt_common_color_fg_green(void); |
| 120 | |
| 121 | BT_HIDDEN |
| 122 | const char *bt_common_color_fg_yellow(void); |
| 123 | |
| 124 | BT_HIDDEN |
| 125 | const char *bt_common_color_fg_blue(void); |
| 126 | |
| 127 | BT_HIDDEN |
| 128 | const char *bt_common_color_fg_magenta(void); |
| 129 | |
| 130 | BT_HIDDEN |
| 131 | const char *bt_common_color_fg_cyan(void); |
| 132 | |
| 133 | BT_HIDDEN |
| 134 | const char *bt_common_color_fg_light_gray(void); |
| 135 | |
| 136 | BT_HIDDEN |
| 137 | const char *bt_common_color_bg_default(void); |
| 138 | |
| 139 | BT_HIDDEN |
| 140 | const char *bt_common_color_bg_red(void); |
| 141 | |
| 142 | BT_HIDDEN |
| 143 | const char *bt_common_color_bg_green(void); |
| 144 | |
| 145 | BT_HIDDEN |
| 146 | const char *bt_common_color_bg_yellow(void); |
| 147 | |
| 148 | BT_HIDDEN |
| 149 | const char *bt_common_color_bg_blue(void); |
| 150 | |
| 151 | BT_HIDDEN |
| 152 | const char *bt_common_color_bg_magenta(void); |
| 153 | |
| 154 | BT_HIDDEN |
| 155 | const char *bt_common_color_bg_cyan(void); |
| 156 | |
| 157 | BT_HIDDEN |
| 158 | const char *bt_common_color_bg_light_gray(void); |
| 159 | |
| 160 | /* |
| 161 | * Returns the substring from `input` to the first character found |
| 162 | * in the list of characters `end_chars`, unescaping any character |
| 163 | * found in `escapable_chars`, and sets `*end_pos` to the position of |
| 164 | * the end (from `input`). The caller owns the returned GString. |
| 165 | */ |
| 166 | BT_HIDDEN |
| 167 | GString *bt_common_string_until(const char *input, const char *escapable_chars, |
| 168 | const char *end_chars, size_t *end_pos); |
| 169 | |
| 170 | /* |
| 171 | * Returns the quoted version of `input` for a shell. If |
| 172 | * `with_single_quotes` is `true`, prepends and appends the `'` prefix |
| 173 | * and suffix to the returned string; otherwise the caller should |
| 174 | * prepend and append them manually, although they are not always |
| 175 | * required. The caller owns the returned GString. |
| 176 | */ |
| 177 | BT_HIDDEN |
| 178 | GString *bt_common_shell_quote(const char *input, bool with_single_quotes); |
| 179 | |
| 180 | /* |
| 181 | * Returns `true` if `input` is a string made only of printable |
| 182 | * characters. |
| 183 | */ |
| 184 | BT_HIDDEN |
| 185 | bool bt_common_string_is_printable(const char *input); |
| 186 | |
| 187 | /* |
| 188 | * Destroys the parts of an LTTng live URL as returned by |
| 189 | * bt_common_parse_lttng_live_url(). |
| 190 | */ |
| 191 | BT_HIDDEN |
| 192 | void bt_common_destroy_lttng_live_url_parts( |
| 193 | struct bt_common_lttng_live_url_parts *parts); |
| 194 | |
| 195 | /* |
| 196 | * Parses the LTTng live URL `url` and returns its different parts. |
| 197 | * If there's an error, writes the error message into `*error_buf` |
| 198 | * up to `error_buf_size` bytes. You must destroy the returned value |
| 199 | * with bt_common_destroy_lttng_live_url_parts(). |
| 200 | */ |
| 201 | BT_HIDDEN |
| 202 | struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( |
| 203 | const char *url, char *error_buf, size_t error_buf_size); |
| 204 | |
| 205 | /* |
| 206 | * Normalizes (in place) a star globbing pattern to be used with |
| 207 | * bt_common_star_glob_match(). This function always succeeds. |
| 208 | */ |
| 209 | BT_HIDDEN |
| 210 | void bt_common_normalize_star_glob_pattern(char *pattern); |
| 211 | |
| 212 | /* |
| 213 | * Returns `true` if `candidate` (of size `candidate_len`) matches |
| 214 | * the star globbing pattern `pattern` (of size `pattern_len`). |
| 215 | */ |
| 216 | BT_HIDDEN |
| 217 | bool bt_common_star_glob_match(const char *pattern, size_t pattern_len, |
| 218 | const char *candidate, size_t candidate_len); |
| 219 | |
| 220 | /* |
| 221 | * Normalizes the path `path`: |
| 222 | * |
| 223 | * * If it's a relative path, converts it to an absolute path using |
| 224 | * `wd` as the working directory (or the current working directory |
| 225 | * if `wd` is NULL). |
| 226 | * * Removes consecutive and trailing slashes. |
| 227 | * * Resolves `..` and `.` in the path (both in `path` and in `wd`). |
| 228 | * * Does NOT resolve symbolic links. |
| 229 | * |
| 230 | * The caller owns the returned GString. |
| 231 | */ |
| 232 | BT_HIDDEN |
| 233 | GString *bt_common_normalize_path(const char *path, const char *wd); |
| 234 | |
| 235 | typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data, |
| 236 | char **buf, size_t avail_size, const char **fmt, va_list *args); |
| 237 | |
| 238 | /* |
| 239 | * This is a custom vsnprintf() which handles the standard conversion |
| 240 | * specifier as well as custom ones. |
| 241 | * |
| 242 | * `fmt` is a typical printf()-style format string, with the following |
| 243 | * limitations: |
| 244 | * |
| 245 | * * The `*` width specifier is not accepted. |
| 246 | * * The `*` precision specifier is not accepted. |
| 247 | * * The `j` and `t` length modifiers are not accepted. |
| 248 | * * The `n` format specifier is not accepted. |
| 249 | * * The format specifiers defined in <inttypes.h> are not accepted |
| 250 | * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and |
| 251 | * `PRIi64`. |
| 252 | * |
| 253 | * `intro` specifies which special character immediately following an |
| 254 | * introductory `%` character in `fmt` is used to indicate a custom |
| 255 | * conversion specifier. For example, if `intro` is '@', then any `%@` |
| 256 | * sequence in `fmt` is the beginning of a custom conversion specifier. |
| 257 | * |
| 258 | * When a custom conversion specifier is encountered in `fmt`, |
| 259 | * the function calls `handle_specifier`. This callback receives: |
| 260 | * |
| 261 | * `priv_data`: |
| 262 | * Custom, private data. |
| 263 | * |
| 264 | * `buf`: |
| 265 | * Address of the current buffer pointer. `*buf` is the position to |
| 266 | * append new data. The callback must update `*buf` when appending |
| 267 | * new data. The callback must ensure not to write passed the whole |
| 268 | * buffer passed to bt_common_custom_vsnprintf(). |
| 269 | * |
| 270 | * `avail_size`: |
| 271 | * Number of bytes left in whole buffer from the `*buf` point. |
| 272 | * |
| 273 | * `fmt`: |
| 274 | * Address of the current format string pointer. `*fmt` points to |
| 275 | * the introductory `%` character, which is followed by the |
| 276 | * character `intro`. The callback must update `*fmt` so that it |
| 277 | * points after the whole custom conversion specifier. |
| 278 | * |
| 279 | * `args`: |
| 280 | * Variable argument list. Use va_arg() to get new arguments from |
| 281 | * this list and update it at the same time. |
| 282 | * |
| 283 | * Because this is an internal utility, this function and its callback |
| 284 | * do not return error codes: they abort when there's any error (bad |
| 285 | * format string, for example). |
| 286 | */ |
| 287 | BT_HIDDEN |
| 288 | void bt_common_custom_vsnprintf(char *buf, size_t buf_size, |
| 289 | char intro, |
| 290 | bt_common_handle_custom_specifier_func handle_specifier, |
| 291 | void *priv_data, const char *fmt, va_list *args); |
| 292 | |
| 293 | /* |
| 294 | * Variadic form of bt_common_custom_vsnprintf(). |
| 295 | */ |
| 296 | BT_HIDDEN |
| 297 | void bt_common_custom_snprintf(char *buf, size_t buf_size, |
| 298 | char intro, |
| 299 | bt_common_handle_custom_specifier_func handle_specifier, |
| 300 | void *priv_data, const char *fmt, ...); |
| 301 | |
| 302 | /* |
| 303 | * Returns the system page size. |
| 304 | */ |
| 305 | BT_HIDDEN |
| 306 | size_t bt_common_get_page_size(void); |
| 307 | |
| 308 | static inline |
| 309 | const char *bt_common_field_class_type_string(enum bt_field_class_type class_type) |
| 310 | { |
| 311 | switch (class_type) { |
| 312 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 313 | return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER"; |
| 314 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 315 | return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER"; |
| 316 | case BT_FIELD_CLASS_TYPE_REAL: |
| 317 | return "BT_FIELD_CLASS_TYPE_REAL"; |
| 318 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 319 | return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION"; |
| 320 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 321 | return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION"; |
| 322 | case BT_FIELD_CLASS_TYPE_STRING: |
| 323 | return "BT_FIELD_CLASS_TYPE_STRING"; |
| 324 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 325 | return "BT_FIELD_CLASS_TYPE_STRUCTURE"; |
| 326 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 327 | return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY"; |
| 328 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 329 | return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY"; |
| 330 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 331 | return "BT_FIELD_CLASS_TYPE_VARIANT"; |
| 332 | default: |
| 333 | return "(unknown)"; |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | static inline |
| 338 | const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base) |
| 339 | { |
| 340 | switch (base) { |
| 341 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
| 342 | return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY"; |
| 343 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
| 344 | return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL"; |
| 345 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
| 346 | return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL"; |
| 347 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
| 348 | return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL"; |
| 349 | default: |
| 350 | return "(unknown)"; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static inline |
| 355 | const char *bt_common_scope_string(enum bt_scope scope) |
| 356 | { |
| 357 | switch (scope) { |
| 358 | case BT_SCOPE_PACKET_CONTEXT: |
| 359 | return "BT_SCOPE_PACKET_CONTEXT"; |
| 360 | case BT_SCOPE_EVENT_COMMON_CONTEXT: |
| 361 | return "BT_SCOPE_EVENT_COMMON_CONTEXT"; |
| 362 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 363 | return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT"; |
| 364 | case BT_SCOPE_EVENT_PAYLOAD: |
| 365 | return "BT_SCOPE_EVENT_PAYLOAD"; |
| 366 | default: |
| 367 | return "(unknown)"; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static inline |
| 372 | const char *bt_common_event_class_log_level_string( |
| 373 | enum bt_event_class_log_level level) |
| 374 | { |
| 375 | switch (level) { |
| 376 | case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY: |
| 377 | return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY"; |
| 378 | case BT_EVENT_CLASS_LOG_LEVEL_ALERT: |
| 379 | return "BT_EVENT_CLASS_LOG_LEVEL_ALERT"; |
| 380 | case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL: |
| 381 | return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL"; |
| 382 | case BT_EVENT_CLASS_LOG_LEVEL_ERROR: |
| 383 | return "BT_EVENT_CLASS_LOG_LEVEL_ERROR"; |
| 384 | case BT_EVENT_CLASS_LOG_LEVEL_WARNING: |
| 385 | return "BT_EVENT_CLASS_LOG_LEVEL_WARNING"; |
| 386 | case BT_EVENT_CLASS_LOG_LEVEL_NOTICE: |
| 387 | return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE"; |
| 388 | case BT_EVENT_CLASS_LOG_LEVEL_INFO: |
| 389 | return "BT_EVENT_CLASS_LOG_LEVEL_INFO"; |
| 390 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: |
| 391 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM"; |
| 392 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: |
| 393 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM"; |
| 394 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: |
| 395 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS"; |
| 396 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: |
| 397 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE"; |
| 398 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: |
| 399 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT"; |
| 400 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: |
| 401 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION"; |
| 402 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: |
| 403 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE"; |
| 404 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG: |
| 405 | return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG"; |
| 406 | default: |
| 407 | return "(unknown)"; |
| 408 | } |
| 409 | }; |
| 410 | |
| 411 | static inline |
| 412 | const char *bt_common_value_type_string(enum bt_value_type type) |
| 413 | { |
| 414 | switch (type) { |
| 415 | case BT_VALUE_TYPE_NULL: |
| 416 | return "BT_VALUE_TYPE_NULL"; |
| 417 | case BT_VALUE_TYPE_BOOL: |
| 418 | return "BT_VALUE_TYPE_BOOL"; |
| 419 | case BT_VALUE_TYPE_INTEGER: |
| 420 | return "BT_VALUE_TYPE_INTEGER"; |
| 421 | case BT_VALUE_TYPE_REAL: |
| 422 | return "BT_VALUE_TYPE_REAL"; |
| 423 | case BT_VALUE_TYPE_STRING: |
| 424 | return "BT_VALUE_TYPE_STRING"; |
| 425 | case BT_VALUE_TYPE_ARRAY: |
| 426 | return "BT_VALUE_TYPE_ARRAY"; |
| 427 | case BT_VALUE_TYPE_MAP: |
| 428 | return "BT_VALUE_TYPE_MAP"; |
| 429 | default: |
| 430 | return "(unknown)"; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | static inline |
| 435 | GString *bt_common_field_path_string(struct bt_field_path *path) |
| 436 | { |
| 437 | GString *str = g_string_new(NULL); |
| 438 | uint64_t i; |
| 439 | |
| 440 | BT_ASSERT(path); |
| 441 | |
| 442 | if (!str) { |
| 443 | goto end; |
| 444 | } |
| 445 | |
| 446 | g_string_append_printf(str, "[%s", bt_common_scope_string( |
| 447 | bt_field_path_get_root_scope(path))); |
| 448 | |
| 449 | for (i = 0; i < bt_field_path_get_index_count(path); i++) { |
| 450 | g_string_append_printf(str, ", %" PRIu64, |
| 451 | bt_field_path_get_index_by_index(path, i)); |
| 452 | } |
| 453 | |
| 454 | g_string_append(str, "]"); |
| 455 | |
| 456 | end: |
| 457 | return str; |
| 458 | } |
| 459 | |
| 460 | static inline |
| 461 | const char *bt_common_self_message_iterator_status_string( |
| 462 | enum bt_self_message_iterator_status status) |
| 463 | { |
| 464 | switch (status) { |
| 465 | case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 466 | return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN"; |
| 467 | case BT_SELF_MESSAGE_ITERATOR_STATUS_END: |
| 468 | return "BT_SELF_MESSAGE_ITERATOR_STATUS_END"; |
| 469 | case BT_SELF_MESSAGE_ITERATOR_STATUS_OK: |
| 470 | return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK"; |
| 471 | case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR: |
| 472 | return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR"; |
| 473 | case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 474 | return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM"; |
| 475 | default: |
| 476 | return "(unknown)"; |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | #define NS_PER_S_I INT64_C(1000000000) |
| 481 | #define NS_PER_S_U UINT64_C(1000000000) |
| 482 | |
| 483 | static inline |
| 484 | int bt_common_clock_value_from_ns_from_origin( |
| 485 | int64_t cc_offset_seconds, uint64_t cc_offset_cycles, |
| 486 | uint64_t cc_freq, int64_t ns_from_origin, |
| 487 | uint64_t *raw_value) |
| 488 | { |
| 489 | int ret = 0; |
| 490 | int64_t offset_in_ns; |
| 491 | uint64_t value_in_ns; |
| 492 | uint64_t rem_value_in_ns; |
| 493 | uint64_t value_periods; |
| 494 | uint64_t value_period_cycles; |
| 495 | int64_t ns_to_add; |
| 496 | |
| 497 | BT_ASSERT(raw_value); |
| 498 | |
| 499 | /* Compute offset part of requested value, in nanoseconds */ |
| 500 | if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) { |
| 501 | ret = -1; |
| 502 | goto end; |
| 503 | } |
| 504 | |
| 505 | offset_in_ns = cc_offset_seconds * NS_PER_S_I; |
| 506 | |
| 507 | if (cc_freq == NS_PER_S_U) { |
| 508 | ns_to_add = (int64_t) cc_offset_cycles; |
| 509 | } else { |
| 510 | if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles, |
| 511 | NS_PER_S_I)) { |
| 512 | ret = -1; |
| 513 | goto end; |
| 514 | } |
| 515 | |
| 516 | ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) / |
| 517 | (int64_t) cc_freq; |
| 518 | } |
| 519 | |
| 520 | if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) { |
| 521 | ret = -1; |
| 522 | goto end; |
| 523 | } |
| 524 | |
| 525 | offset_in_ns += ns_to_add; |
| 526 | |
| 527 | /* Value part in nanoseconds */ |
| 528 | if (ns_from_origin < offset_in_ns) { |
| 529 | ret = -1; |
| 530 | goto end; |
| 531 | } |
| 532 | |
| 533 | value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns); |
| 534 | |
| 535 | /* Number of whole clock periods in `value_in_ns` */ |
| 536 | value_periods = value_in_ns / NS_PER_S_U; |
| 537 | |
| 538 | /* Remaining nanoseconds in cycles + whole clock periods in cycles */ |
| 539 | rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U; |
| 540 | |
| 541 | if (value_periods > UINT64_MAX / cc_freq) { |
| 542 | ret = -1; |
| 543 | goto end; |
| 544 | } |
| 545 | |
| 546 | if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) { |
| 547 | ret = -1; |
| 548 | goto end; |
| 549 | } |
| 550 | |
| 551 | value_period_cycles = value_periods * cc_freq; |
| 552 | |
| 553 | if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) { |
| 554 | ret = -1; |
| 555 | goto end; |
| 556 | } |
| 557 | |
| 558 | if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U, |
| 559 | value_period_cycles)) { |
| 560 | ret = -1; |
| 561 | goto end; |
| 562 | } |
| 563 | |
| 564 | *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U + |
| 565 | value_period_cycles; |
| 566 | |
| 567 | end: |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | static inline |
| 572 | bt_self_message_iterator_status bt_message_iterator_status_to_self( |
| 573 | bt_message_iterator_status status) |
| 574 | { |
| 575 | return (bt_self_message_iterator_status) status; |
| 576 | } |
| 577 | #endif /* BABELTRACE_COMMON_INTERNAL_H */ |