Rename VERBOSE log level to TRACE
[babeltrace.git] / src / common / common.h
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 <errno.h>
28 #include <glib.h>
29 #include <inttypes.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include <babeltrace2/graph/self-message-iterator.h>
37 #include <babeltrace2/trace-ir/event-class-const.h>
38 #include <babeltrace2/trace-ir/field-class-const.h>
39 #include <babeltrace2/trace-ir/field-path-const.h>
40 #include <babeltrace2/graph/self-component.h>
41 #include <babeltrace2/graph/message-iterator-const.h>
42 #include <babeltrace2/logging.h>
43 #include <babeltrace2/value.h>
44
45 #include "common/assert.h"
46 #include "common/macros.h"
47 #include "common/safe.h"
48
49 #define BT_COMMON_COLOR_RESET "\033[0m"
50 #define BT_COMMON_COLOR_BOLD "\033[1m"
51 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
52 #define BT_COMMON_COLOR_FG_RED "\033[31m"
53 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
54 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
55 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
56 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
57 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
58 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
59 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
60 #define BT_COMMON_COLOR_BG_RED "\033[41m"
61 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
62 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
63 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
64 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
65 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
66 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
67
68 struct bt_common_lttng_live_url_parts {
69 GString *proto;
70 GString *hostname;
71 GString *target_hostname;
72 GString *session_name;
73
74 /* -1 means default port */
75 int port;
76 };
77
78 /*
79 * Checks if the current process has setuid or setgid access rights.
80 * Returns `true` if so.
81 */
82 BT_HIDDEN
83 bool bt_common_is_setuid_setgid(void);
84
85 /*
86 * Returns the system-wide plugin path, e.g.
87 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
88 */
89 BT_HIDDEN
90 const char *bt_common_get_system_plugin_path(void);
91
92 /*
93 * Returns the user plugin path, e.g.
94 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
95 * return value.
96 */
97 BT_HIDDEN
98 char *bt_common_get_home_plugin_path(int log_level);
99
100 /*
101 * Appends the list of directories in `paths` to the array `dirs`.
102 * `paths` is a list of directories separated by `:`. Returns 0 on
103 * success.
104 */
105 BT_HIDDEN
106 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
107
108 /*
109 * Returns `true` if terminal color codes are supported for this
110 * process.
111 */
112 BT_HIDDEN
113 bool bt_common_colors_supported(void);
114
115 BT_HIDDEN
116 const char *bt_common_color_reset(void);
117
118 BT_HIDDEN
119 const char *bt_common_color_bold(void);
120
121 BT_HIDDEN
122 const char *bt_common_color_fg_default(void);
123
124 BT_HIDDEN
125 const char *bt_common_color_fg_red(void);
126
127 BT_HIDDEN
128 const char *bt_common_color_fg_green(void);
129
130 BT_HIDDEN
131 const char *bt_common_color_fg_yellow(void);
132
133 BT_HIDDEN
134 const char *bt_common_color_fg_blue(void);
135
136 BT_HIDDEN
137 const char *bt_common_color_fg_magenta(void);
138
139 BT_HIDDEN
140 const char *bt_common_color_fg_cyan(void);
141
142 BT_HIDDEN
143 const char *bt_common_color_fg_light_gray(void);
144
145 BT_HIDDEN
146 const char *bt_common_color_bg_default(void);
147
148 BT_HIDDEN
149 const char *bt_common_color_bg_red(void);
150
151 BT_HIDDEN
152 const char *bt_common_color_bg_green(void);
153
154 BT_HIDDEN
155 const char *bt_common_color_bg_yellow(void);
156
157 BT_HIDDEN
158 const char *bt_common_color_bg_blue(void);
159
160 BT_HIDDEN
161 const char *bt_common_color_bg_magenta(void);
162
163 BT_HIDDEN
164 const char *bt_common_color_bg_cyan(void);
165
166 BT_HIDDEN
167 const char *bt_common_color_bg_light_gray(void);
168
169 /*
170 * Returns the substring from `input` to the first character found
171 * in the list of characters `end_chars`, unescaping any character
172 * found in `escapable_chars`, and sets `*end_pos` to the position of
173 * the end (from `input`). The caller owns the returned GString.
174 */
175 BT_HIDDEN
176 GString *bt_common_string_until(const char *input, const char *escapable_chars,
177 const char *end_chars, size_t *end_pos);
178
179 /*
180 * Returns the quoted version of `input` for a shell. If
181 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
182 * and suffix to the returned string; otherwise the caller should
183 * prepend and append them manually, although they are not always
184 * required. The caller owns the returned GString.
185 */
186 BT_HIDDEN
187 GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
188
189 /*
190 * Returns `true` if `input` is a string made only of printable
191 * characters.
192 */
193 BT_HIDDEN
194 bool bt_common_string_is_printable(const char *input);
195
196 /*
197 * Destroys the parts of an LTTng live URL as returned by
198 * bt_common_parse_lttng_live_url().
199 */
200 BT_HIDDEN
201 void bt_common_destroy_lttng_live_url_parts(
202 struct bt_common_lttng_live_url_parts *parts);
203
204 /*
205 * Parses the LTTng live URL `url` and returns its different parts.
206 * If there's an error, writes the error message into `*error_buf`
207 * up to `error_buf_size` bytes. You must destroy the returned value
208 * with bt_common_destroy_lttng_live_url_parts().
209 */
210 BT_HIDDEN
211 struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
212 const char *url, char *error_buf, size_t error_buf_size);
213
214 /*
215 * Normalizes (in place) a star globbing pattern to be used with
216 * bt_common_star_glob_match(). This function always succeeds.
217 */
218 BT_HIDDEN
219 void bt_common_normalize_star_glob_pattern(char *pattern);
220
221 /*
222 * Returns `true` if `candidate` (of size `candidate_len`) matches
223 * the star globbing pattern `pattern` (of size `pattern_len`).
224 */
225 BT_HIDDEN
226 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
227 const char *candidate, size_t candidate_len);
228
229 /*
230 * Normalizes the path `path`:
231 *
232 * * If it's a relative path, converts it to an absolute path using
233 * `wd` as the working directory (or the current working directory
234 * if `wd` is NULL).
235 * * Removes consecutive and trailing slashes.
236 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
237 * * Does NOT resolve symbolic links.
238 *
239 * The caller owns the returned GString.
240 */
241 BT_HIDDEN
242 GString *bt_common_normalize_path(const char *path, const char *wd);
243
244 typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
245 char **buf, size_t avail_size, const char **fmt, va_list *args);
246
247 /*
248 * This is a custom vsnprintf() which handles the standard conversion
249 * specifier as well as custom ones.
250 *
251 * `fmt` is a typical printf()-style format string, with the following
252 * limitations:
253 *
254 * * The `*` width specifier is not accepted.
255 * * The `*` precision specifier is not accepted.
256 * * The `j` and `t` length modifiers are not accepted.
257 * * The `n` format specifier is not accepted.
258 * * The format specifiers defined in <inttypes.h> are not accepted
259 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
260 * `PRIi64`.
261 *
262 * `intro` specifies which special character immediately following an
263 * introductory `%` character in `fmt` is used to indicate a custom
264 * conversion specifier. For example, if `intro` is '@', then any `%@`
265 * sequence in `fmt` is the beginning of a custom conversion specifier.
266 *
267 * When a custom conversion specifier is encountered in `fmt`,
268 * the function calls `handle_specifier`. This callback receives:
269 *
270 * `priv_data`:
271 * Custom, private data.
272 *
273 * `buf`:
274 * Address of the current buffer pointer. `*buf` is the position to
275 * append new data. The callback must update `*buf` when appending
276 * new data. The callback must ensure not to write passed the whole
277 * buffer passed to bt_common_custom_vsnprintf().
278 *
279 * `avail_size`:
280 * Number of bytes left in whole buffer from the `*buf` point.
281 *
282 * `fmt`:
283 * Address of the current format string pointer. `*fmt` points to
284 * the introductory `%` character, which is followed by the
285 * character `intro`. The callback must update `*fmt` so that it
286 * points after the whole custom conversion specifier.
287 *
288 * `args`:
289 * Variable argument list. Use va_arg() to get new arguments from
290 * this list and update it at the same time.
291 *
292 * Because this is an internal utility, this function and its callback
293 * do not return error codes: they abort when there's any error (bad
294 * format string, for example).
295 */
296 BT_HIDDEN
297 void bt_common_custom_vsnprintf(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, va_list *args);
301
302 /*
303 * Variadic form of bt_common_custom_vsnprintf().
304 */
305 BT_HIDDEN
306 void bt_common_custom_snprintf(char *buf, size_t buf_size,
307 char intro,
308 bt_common_handle_custom_specifier_func handle_specifier,
309 void *priv_data, const char *fmt, ...);
310
311 /*
312 * Returns the system page size.
313 */
314 BT_HIDDEN
315 size_t bt_common_get_page_size(int log_level);
316
317 /*
318 * Adds the digit separator `sep` as many times as needed to form groups
319 * of `digits_per_group` digits within `str`. `str` must have enough
320 * room to accomodate the new separators, that is:
321 *
322 * strlen(str) + (strlen(str) / digits_per_group) + 1
323 *
324 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
325 * `,`, `str` becomes `1,983,198,398,213`.
326 *
327 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
328 * must not be `\0`.
329 */
330 BT_HIDDEN
331 void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep);
332
333 /*
334 * Wraps read() function to handle EINTR and partial reads.
335 * On success, it returns `count` received as parameter. On error, it returns a
336 * value smaller than the requested `count`.
337 */
338 static inline
339 ssize_t bt_common_read(int fd, void *buf, size_t count, int log_level)
340 {
341 size_t i = 0;
342 ssize_t ret;
343
344 BT_ASSERT(buf);
345
346 /* Never return an overflow value. */
347 BT_ASSERT(count <= SSIZE_MAX);
348
349 do {
350 ret = read(fd, buf + i, count - i);
351 if (ret < 0) {
352 if (errno == EINTR) {
353 #ifdef BT_LOG_WRITE_CUR_LVL
354 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG, log_level,
355 BT_LOG_TAG,
356 "read() call interrupted; retrying...");
357 #endif
358 /* retry operation */
359 continue;
360 } else {
361 #ifdef BT_LOG_WRITE_ERRNO_CUR_LVL
362 BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR,
363 log_level, BT_LOG_TAG,
364 "Error while reading", ": fd=%d", fd);
365 #endif
366 goto end;
367 }
368 }
369 i += ret;
370 BT_ASSERT(i <= count);
371 } while (count - i > 0 && ret > 0);
372
373 end:
374 if (ret >= 0) {
375 if (i == 0) {
376 ret = -1;
377 } else {
378 ret = i;
379 }
380 }
381
382 return ret;
383 }
384
385 static inline
386 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
387 {
388 switch (class_type) {
389 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
390 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
391 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
392 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
393 case BT_FIELD_CLASS_TYPE_REAL:
394 return "BT_FIELD_CLASS_TYPE_REAL";
395 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
396 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
397 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
398 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
399 case BT_FIELD_CLASS_TYPE_STRING:
400 return "BT_FIELD_CLASS_TYPE_STRING";
401 case BT_FIELD_CLASS_TYPE_STRUCTURE:
402 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
403 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
404 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
405 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
406 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
407 case BT_FIELD_CLASS_TYPE_VARIANT:
408 return "BT_FIELD_CLASS_TYPE_VARIANT";
409 default:
410 return "(unknown)";
411 }
412 };
413
414 static inline
415 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
416 {
417 switch (base) {
418 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
419 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
420 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
421 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
422 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
423 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
424 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
425 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
426 default:
427 return "(unknown)";
428 }
429 }
430
431 static inline
432 const char *bt_common_scope_string(enum bt_scope scope)
433 {
434 switch (scope) {
435 case BT_SCOPE_PACKET_CONTEXT:
436 return "BT_SCOPE_PACKET_CONTEXT";
437 case BT_SCOPE_EVENT_COMMON_CONTEXT:
438 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
439 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
440 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
441 case BT_SCOPE_EVENT_PAYLOAD:
442 return "BT_SCOPE_EVENT_PAYLOAD";
443 default:
444 return "(unknown)";
445 }
446 }
447
448 static inline
449 const char *bt_common_event_class_log_level_string(
450 enum bt_event_class_log_level level)
451 {
452 switch (level) {
453 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
454 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
455 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
456 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
457 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
458 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
459 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
460 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
461 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
462 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
463 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
464 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
465 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
466 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
467 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
468 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
469 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
470 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
471 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
472 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
473 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
474 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
475 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
476 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
477 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
478 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
479 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
480 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
481 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
482 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
483 default:
484 return "(unknown)";
485 }
486 };
487
488 static inline
489 const char *bt_common_value_type_string(enum bt_value_type type)
490 {
491 switch (type) {
492 case BT_VALUE_TYPE_NULL:
493 return "BT_VALUE_TYPE_NULL";
494 case BT_VALUE_TYPE_BOOL:
495 return "BT_VALUE_TYPE_BOOL";
496 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
497 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
498 case BT_VALUE_TYPE_SIGNED_INTEGER:
499 return "BT_VALUE_TYPE_SIGNED_INTEGER";
500 case BT_VALUE_TYPE_REAL:
501 return "BT_VALUE_TYPE_REAL";
502 case BT_VALUE_TYPE_STRING:
503 return "BT_VALUE_TYPE_STRING";
504 case BT_VALUE_TYPE_ARRAY:
505 return "BT_VALUE_TYPE_ARRAY";
506 case BT_VALUE_TYPE_MAP:
507 return "BT_VALUE_TYPE_MAP";
508 default:
509 return "(unknown)";
510 }
511 };
512
513 static inline
514 GString *bt_common_field_path_string(struct bt_field_path *path)
515 {
516 GString *str = g_string_new(NULL);
517 uint64_t i;
518
519 BT_ASSERT(path);
520
521 if (!str) {
522 goto end;
523 }
524
525 g_string_append_printf(str, "[%s", bt_common_scope_string(
526 bt_field_path_get_root_scope(path)));
527
528 for (i = 0; i < bt_field_path_get_item_count(path); i++) {
529 const struct bt_field_path_item *fp_item =
530 bt_field_path_borrow_item_by_index_const(path, i);
531
532 switch (bt_field_path_item_get_type(fp_item)) {
533 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
534 g_string_append_printf(str, ", %" PRIu64,
535 bt_field_path_item_index_get_index(fp_item));
536 break;
537 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
538 g_string_append(str, ", <CUR>");
539 break;
540 default:
541 abort();
542 }
543 }
544
545 g_string_append(str, "]");
546
547 end:
548 return str;
549 }
550
551 static inline
552 const char *bt_common_self_message_iterator_status_string(
553 enum bt_self_message_iterator_status status)
554 {
555 switch (status) {
556 case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN:
557 return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
558 case BT_SELF_MESSAGE_ITERATOR_STATUS_END:
559 return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
560 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK:
561 return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
562 case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR:
563 return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
564 case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM:
565 return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
566 default:
567 return "(unknown)";
568 }
569 };
570
571 static inline
572 const char *bt_common_logging_level_string(
573 enum bt_logging_level level)
574 {
575 switch (level) {
576 case BT_LOGGING_LEVEL_TRACE:
577 return "BT_LOGGING_LEVEL_TRACE";
578 case BT_LOGGING_LEVEL_DEBUG:
579 return "BT_LOGGING_LEVEL_DEBUG";
580 case BT_LOGGING_LEVEL_INFO:
581 return "BT_LOGGING_LEVEL_INFO";
582 case BT_LOGGING_LEVEL_WARN:
583 return "BT_LOGGING_LEVEL_WARN";
584 case BT_LOGGING_LEVEL_ERROR:
585 return "BT_LOGGING_LEVEL_ERROR";
586 case BT_LOGGING_LEVEL_FATAL:
587 return "BT_LOGGING_LEVEL_FATAL";
588 case BT_LOGGING_LEVEL_NONE:
589 return "BT_LOGGING_LEVEL_NONE";
590 default:
591 return "(unknown)";
592 }
593 };
594
595 static inline
596 const char *bt_self_component_status_string(
597 enum bt_self_component_status status)
598 {
599 switch (status) {
600 case BT_SELF_COMPONENT_STATUS_OK:
601 return "BT_SELF_COMPONENT_STATUS_OK";
602 case BT_SELF_COMPONENT_STATUS_END:
603 return "BT_SELF_COMPONENT_STATUS_END";
604 case BT_SELF_COMPONENT_STATUS_AGAIN:
605 return "BT_SELF_COMPONENT_STATUS_AGAIN";
606 case BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION:
607 return "BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION";
608 case BT_SELF_COMPONENT_STATUS_ERROR:
609 return "BT_SELF_COMPONENT_STATUS_ERROR";
610 case BT_SELF_COMPONENT_STATUS_NOMEM:
611 return "BT_SELF_COMPONENT_STATUS_NOMEM";
612 default:
613 return "(unknown)";
614 }
615 }
616
617 static inline
618 const char *bt_message_iterator_status_string(
619 enum bt_message_iterator_status status)
620 {
621 switch (status) {
622 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
623 return "BT_MESSAGE_ITERATOR_STATUS_AGAIN";
624 case BT_MESSAGE_ITERATOR_STATUS_END:
625 return "BT_MESSAGE_ITERATOR_STATUS_END";
626 case BT_MESSAGE_ITERATOR_STATUS_OK:
627 return "BT_MESSAGE_ITERATOR_STATUS_OK";
628 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
629 return "BT_MESSAGE_ITERATOR_STATUS_ERROR";
630 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
631 return "BT_MESSAGE_ITERATOR_STATUS_NOMEM";
632 default:
633 return "(unknown)";
634 }
635 }
636
637 #define NS_PER_S_I INT64_C(1000000000)
638 #define NS_PER_S_U UINT64_C(1000000000)
639
640 static inline
641 int bt_common_clock_value_from_ns_from_origin(
642 int64_t cc_offset_seconds, uint64_t cc_offset_cycles,
643 uint64_t cc_freq, int64_t ns_from_origin,
644 uint64_t *raw_value)
645 {
646 int ret = 0;
647 int64_t offset_in_ns;
648 uint64_t value_in_ns;
649 uint64_t rem_value_in_ns;
650 uint64_t value_periods;
651 uint64_t value_period_cycles;
652 int64_t ns_to_add;
653
654 BT_ASSERT(raw_value);
655
656 /* Compute offset part of requested value, in nanoseconds */
657 if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) {
658 ret = -1;
659 goto end;
660 }
661
662 offset_in_ns = cc_offset_seconds * NS_PER_S_I;
663
664 if (cc_freq == NS_PER_S_U) {
665 ns_to_add = (int64_t) cc_offset_cycles;
666 } else {
667 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles,
668 NS_PER_S_I)) {
669 ret = -1;
670 goto end;
671 }
672
673 ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) /
674 (int64_t) cc_freq;
675 }
676
677 if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) {
678 ret = -1;
679 goto end;
680 }
681
682 offset_in_ns += ns_to_add;
683
684 /* Value part in nanoseconds */
685 if (ns_from_origin < offset_in_ns) {
686 ret = -1;
687 goto end;
688 }
689
690 value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns);
691
692 /* Number of whole clock periods in `value_in_ns` */
693 value_periods = value_in_ns / NS_PER_S_U;
694
695 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
696 rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U;
697
698 if (value_periods > UINT64_MAX / cc_freq) {
699 ret = -1;
700 goto end;
701 }
702
703 if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) {
704 ret = -1;
705 goto end;
706 }
707
708 value_period_cycles = value_periods * cc_freq;
709
710 if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) {
711 ret = -1;
712 goto end;
713 }
714
715 if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U,
716 value_period_cycles)) {
717 ret = -1;
718 goto end;
719 }
720
721 *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U +
722 value_period_cycles;
723
724 end:
725 return ret;
726 }
727
728 static inline
729 enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self(
730 enum bt_message_iterator_status status)
731 {
732 return (int) status;
733 }
734
735 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.04532 seconds and 4 git commands to generate.