lib: assign a unique ID to each pre/postcond. and report it on failure
[babeltrace.git] / src / common / common.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #ifndef BABELTRACE_COMMON_INTERNAL_H
9 #define BABELTRACE_COMMON_INTERNAL_H
10
11 #include <errno.h>
12 #include <glib.h>
13 #include <inttypes.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21
22 #include <babeltrace2/babeltrace.h>
23
24 #define __BT_IN_BABELTRACE_H
25 #include <babeltrace2/func-status.h>
26
27 #include "common/assert.h"
28 #include "common/macros.h"
29 #include "common/safe.h"
30
31 #define BT_COMMON_COLOR_RESET "\033[0m"
32 #define BT_COMMON_COLOR_BOLD "\033[1m"
33 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
34 #define BT_COMMON_COLOR_FG_RED "\033[31m"
35 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
36 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
37 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
38 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
39 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
40 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
41 #define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m"
42 #define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m"
43 #define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m"
44 #define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m"
45 #define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m"
46 #define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m"
47 #define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m"
48 #define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m"
49 #define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m"
50 #define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m"
51 #define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m"
52 #define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m"
53 #define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m"
54 #define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m"
55 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
56 #define BT_COMMON_COLOR_BG_RED "\033[41m"
57 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
58 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
59 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
60 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
61 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
62 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
63
64 enum bt_common_color_when {
65 BT_COMMON_COLOR_WHEN_AUTO,
66 BT_COMMON_COLOR_WHEN_ALWAYS,
67 BT_COMMON_COLOR_WHEN_NEVER,
68 };
69
70 struct bt_common_color_codes {
71 const char *reset;
72 const char *bold;
73 const char *fg_default;
74 const char *fg_red;
75 const char *fg_green;
76 const char *fg_yellow;
77 const char *fg_blue;
78 const char *fg_magenta;
79 const char *fg_cyan;
80 const char *fg_light_gray;
81 const char *fg_bright_red;
82 const char *fg_bright_green;
83 const char *fg_bright_yellow;
84 const char *fg_bright_blue;
85 const char *fg_bright_magenta;
86 const char *fg_bright_cyan;
87 const char *fg_bright_light_gray;
88 const char *bg_default;
89 const char *bg_red;
90 const char *bg_green;
91 const char *bg_yellow;
92 const char *bg_blue;
93 const char *bg_magenta;
94 const char *bg_cyan;
95 const char *bg_light_gray;
96 };
97
98 struct bt_common_lttng_live_url_parts {
99 GString *proto;
100 GString *hostname;
101 GString *target_hostname;
102 GString *session_name;
103
104 /* -1 means default port */
105 int port;
106 };
107
108 /*
109 * Checks if the current process has setuid or setgid access rights.
110 * Returns `true` if so.
111 */
112 BT_HIDDEN
113 bool bt_common_is_setuid_setgid(void);
114
115 /*
116 * Returns the system-wide plugin path, e.g.
117 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
118 */
119 BT_HIDDEN
120 const char *bt_common_get_system_plugin_path(void);
121
122 /*
123 * Returns the user plugin path, e.g.
124 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
125 * return value.
126 */
127 BT_HIDDEN
128 char *bt_common_get_home_plugin_path(int log_level);
129
130 /*
131 * Appends the list of directories in `paths` to the array `dirs`.
132 * `paths` is a list of directories separated by `:`. Returns 0 on
133 * success.
134 */
135 BT_HIDDEN
136 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
137
138 /*
139 * Returns `true` if terminal color codes are supported for this
140 * process.
141 */
142 BT_HIDDEN
143 bool bt_common_colors_supported(void);
144
145 BT_HIDDEN
146 const char *bt_common_color_reset(void);
147
148 BT_HIDDEN
149 const char *bt_common_color_bold(void);
150
151 BT_HIDDEN
152 const char *bt_common_color_fg_default(void);
153
154 BT_HIDDEN
155 const char *bt_common_color_fg_red(void);
156
157 BT_HIDDEN
158 const char *bt_common_color_fg_green(void);
159
160 BT_HIDDEN
161 const char *bt_common_color_fg_yellow(void);
162
163 BT_HIDDEN
164 const char *bt_common_color_fg_blue(void);
165
166 BT_HIDDEN
167 const char *bt_common_color_fg_magenta(void);
168
169 BT_HIDDEN
170 const char *bt_common_color_fg_cyan(void);
171
172 BT_HIDDEN
173 const char *bt_common_color_fg_light_gray(void);
174
175 BT_HIDDEN
176 const char *bt_common_color_fg_bright_red(void);
177
178 BT_HIDDEN
179 const char *bt_common_color_fg_bright_green(void);
180
181 BT_HIDDEN
182 const char *bt_common_color_fg_bright_yellow(void);
183
184 BT_HIDDEN
185 const char *bt_common_color_fg_bright_blue(void);
186
187 BT_HIDDEN
188 const char *bt_common_color_fg_bright_magenta(void);
189
190 BT_HIDDEN
191 const char *bt_common_color_fg_bright_cyan(void);
192
193 BT_HIDDEN
194 const char *bt_common_color_fg_bright_light_gray(void);
195
196 BT_HIDDEN
197 const char *bt_common_color_bg_default(void);
198
199 BT_HIDDEN
200 const char *bt_common_color_bg_red(void);
201
202 BT_HIDDEN
203 const char *bt_common_color_bg_green(void);
204
205 BT_HIDDEN
206 const char *bt_common_color_bg_yellow(void);
207
208 BT_HIDDEN
209 const char *bt_common_color_bg_blue(void);
210
211 BT_HIDDEN
212 const char *bt_common_color_bg_magenta(void);
213
214 BT_HIDDEN
215 const char *bt_common_color_bg_cyan(void);
216
217 BT_HIDDEN
218 const char *bt_common_color_bg_light_gray(void);
219
220 BT_HIDDEN
221 void bt_common_color_get_codes(struct bt_common_color_codes *codes,
222 enum bt_common_color_when use_colors);
223
224 /*
225 * Returns the substring from `input` to the first character found
226 * in the list of characters `end_chars`, unescaping any character
227 * found in `escapable_chars`, and sets `*end_pos` to the position of
228 * the end (from `input`). The caller owns the returned GString.
229 */
230 BT_HIDDEN
231 GString *bt_common_string_until(const char *input, const char *escapable_chars,
232 const char *end_chars, size_t *end_pos);
233
234 /*
235 * Returns the quoted version of `input` for a shell. If
236 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
237 * and suffix to the returned string; otherwise the caller should
238 * prepend and append them manually, although they are not always
239 * required. The caller owns the returned GString.
240 */
241 BT_HIDDEN
242 GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
243
244 /*
245 * Returns `true` if `input` is a string made only of printable
246 * characters.
247 */
248 BT_HIDDEN
249 bool bt_common_string_is_printable(const char *input);
250
251 /*
252 * Destroys the parts of an LTTng live URL as returned by
253 * bt_common_parse_lttng_live_url().
254 */
255 BT_HIDDEN
256 void bt_common_destroy_lttng_live_url_parts(
257 struct bt_common_lttng_live_url_parts *parts);
258
259 /*
260 * Parses the LTTng live URL `url` and returns its different parts.
261 * If there's an error, writes the error message into `*error_buf`
262 * up to `error_buf_size` bytes. You must destroy the returned value
263 * with bt_common_destroy_lttng_live_url_parts().
264 */
265 BT_HIDDEN
266 struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
267 const char *url, char *error_buf, size_t error_buf_size);
268
269 /*
270 * Normalizes (in place) a star globbing pattern to be used with
271 * bt_common_star_glob_match(). This function always succeeds.
272 */
273 BT_HIDDEN
274 void bt_common_normalize_star_glob_pattern(char *pattern);
275
276 /*
277 * Returns `true` if `candidate` (of size `candidate_len`) matches
278 * the star globbing pattern `pattern` (of size `pattern_len`).
279 */
280 BT_HIDDEN
281 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
282 const char *candidate, size_t candidate_len);
283
284 /*
285 * Normalizes the path `path`:
286 *
287 * * If it's a relative path, converts it to an absolute path using
288 * `wd` as the working directory (or the current working directory
289 * if `wd` is NULL).
290 * * Removes consecutive and trailing slashes.
291 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
292 * * Does NOT resolve symbolic links.
293 *
294 * The caller owns the returned GString.
295 */
296 BT_HIDDEN
297 GString *bt_common_normalize_path(const char *path, const char *wd);
298
299 typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
300 char **buf, size_t avail_size, const char **fmt, va_list *args);
301
302 /*
303 * This is a custom vsnprintf() which handles the standard conversion
304 * specifier as well as custom ones.
305 *
306 * `fmt` is a typical printf()-style format string, with the following
307 * limitations:
308 *
309 * * The `*` width specifier is not accepted.
310 * * The `*` precision specifier is not accepted.
311 * * The `j` and `t` length modifiers are not accepted.
312 * * The `n` format specifier is not accepted.
313 * * The format specifiers defined in <inttypes.h> are not accepted
314 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
315 * `PRIi64`.
316 *
317 * `intro` specifies which special character immediately following an
318 * introductory `%` character in `fmt` is used to indicate a custom
319 * conversion specifier. For example, if `intro` is '@', then any `%@`
320 * sequence in `fmt` is the beginning of a custom conversion specifier.
321 *
322 * When a custom conversion specifier is encountered in `fmt`,
323 * the function calls `handle_specifier`. This callback receives:
324 *
325 * `priv_data`:
326 * Custom, private data.
327 *
328 * `buf`:
329 * Address of the current buffer pointer. `*buf` is the position to
330 * append new data. The callback must update `*buf` when appending
331 * new data. The callback must ensure not to write passed the whole
332 * buffer passed to bt_common_custom_vsnprintf().
333 *
334 * `avail_size`:
335 * Number of bytes left in whole buffer from the `*buf` point.
336 *
337 * `fmt`:
338 * Address of the current format string pointer. `*fmt` points to
339 * the introductory `%` character, which is followed by the
340 * character `intro`. The callback must update `*fmt` so that it
341 * points after the whole custom conversion specifier.
342 *
343 * `args`:
344 * Variable argument list. Use va_arg() to get new arguments from
345 * this list and update it at the same time.
346 *
347 * Because this is an internal utility, this function and its callback
348 * do not return error codes: they abort when there's any error (bad
349 * format string, for example).
350 */
351 BT_HIDDEN
352 void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
353 char intro,
354 bt_common_handle_custom_specifier_func handle_specifier,
355 void *priv_data, const char *fmt, va_list *args);
356
357 /*
358 * Variadic form of bt_common_custom_vsnprintf().
359 */
360 BT_HIDDEN
361 void bt_common_custom_snprintf(char *buf, size_t buf_size,
362 char intro,
363 bt_common_handle_custom_specifier_func handle_specifier,
364 void *priv_data, const char *fmt, ...);
365
366 /*
367 * Returns the system page size.
368 */
369 BT_HIDDEN
370 size_t bt_common_get_page_size(int log_level);
371
372 /*
373 * Adds the digit separator `sep` as many times as needed to form groups
374 * of `digits_per_group` digits within `str`. `str` must have enough
375 * room to accomodate the new separators, that is:
376 *
377 * strlen(str) + (strlen(str) / digits_per_group) + 1
378 *
379 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
380 * `,`, `str` becomes `1,983,198,398,213`.
381 *
382 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
383 * must not be `\0`.
384 */
385 BT_HIDDEN
386 void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep);
387
388 /*
389 * This is similar to what the command `fold --spaces` does: it wraps
390 * the input lines of `str`, breaking at spaces, and indenting each line
391 * with `indent` spaces so that each line fits the total length
392 * `total_length`.
393 *
394 * If an original line in `str` contains a word which is >= the content
395 * length (`total_length - indent`), then the corresponding folded line
396 * is also larger than the content length. In other words, breaking at
397 * spaces is a best effort, but it might not be possible.
398 *
399 * The returned string, on success, is owned by the caller.
400 */
401 BT_HIDDEN
402 GString *bt_common_fold(const char *str, unsigned int total_length,
403 unsigned int indent);
404
405 /*
406 * Writes the terminal's width to `*width`, its height to `*height`,
407 * and returns 0 on success, or returns -1 on error.
408 */
409 BT_HIDDEN
410 int bt_common_get_term_size(unsigned int *width, unsigned int *height);
411
412 /*
413 * Appends the textual content of `fp` to `str`, starting from its
414 * current position to the end of the file.
415 *
416 * This function does NOT rewind `fp` once it's done or on error.
417 */
418 BT_HIDDEN
419 int bt_common_append_file_content_to_g_string(GString *str, FILE *fp);
420
421 BT_HIDDEN
422 void bt_common_abort(void) __attribute__((noreturn));
423
424 /*
425 * Wraps read() function to handle EINTR and partial reads.
426 * On success, it returns `count` received as parameter. On error, it returns a
427 * value smaller than the requested `count`.
428 */
429 static inline
430 ssize_t bt_common_read(int fd, void *buf, size_t count, int log_level)
431 {
432 size_t i = 0;
433 ssize_t ret;
434
435 BT_ASSERT_DBG(buf);
436
437 /* Never return an overflow value. */
438 BT_ASSERT_DBG(count <= SSIZE_MAX);
439
440 do {
441 ret = read(fd, buf + i, count - i);
442 if (ret < 0) {
443 if (errno == EINTR) {
444 #ifdef BT_LOG_WRITE_CUR_LVL
445 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG, log_level,
446 BT_LOG_TAG,
447 "read() call interrupted; retrying...");
448 #endif
449 /* retry operation */
450 continue;
451 } else {
452 #ifdef BT_LOG_WRITE_ERRNO_CUR_LVL
453 BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR,
454 log_level, BT_LOG_TAG,
455 "Error while reading", ": fd=%d", fd);
456 #endif
457 goto end;
458 }
459 }
460 i += ret;
461 BT_ASSERT_DBG(i <= count);
462 } while (count - i > 0 && ret > 0);
463
464 end:
465 if (ret >= 0) {
466 if (i == 0) {
467 ret = -1;
468 } else {
469 ret = i;
470 }
471 }
472
473 return ret;
474 }
475
476 static inline
477 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
478 {
479 switch (class_type) {
480 case BT_FIELD_CLASS_TYPE_BOOL:
481 return "BOOL";
482 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
483 return "BIT_ARRAY";
484 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
485 return "UNSIGNED_INTEGER";
486 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
487 return "SIGNED_INTEGER";
488 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
489 return "UNSIGNED_ENUMERATION";
490 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
491 return "SIGNED_ENUMERATION";
492 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
493 return "SINGLE_PRECISION_REAL";
494 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
495 return "DOUBLE_PRECISION_REAL";
496 case BT_FIELD_CLASS_TYPE_STRING:
497 return "STRING";
498 case BT_FIELD_CLASS_TYPE_STRUCTURE:
499 return "STRUCTURE";
500 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
501 return "STATIC_ARRAY";
502 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
503 return "DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD";
504 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
505 return "DYNAMIC_ARRAY_WITH_LENGTH_FIELD";
506 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
507 return "OPTION_WITHOUT_SELECTOR_FIELD";
508 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
509 return "OPTION_WITH_BOOL_SELECTOR_FIELD";
510 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
511 return "OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
512 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
513 return "OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
514 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
515 return "VARIANT_WITHOUT_SELECTOR_FIELD";
516 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
517 return "VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
518 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
519 return "VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
520 default:
521 return "(unknown)";
522 }
523 };
524
525 static inline
526 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
527 {
528 switch (base) {
529 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
530 return "BINARY";
531 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
532 return "OCTAL";
533 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
534 return "DECIMAL";
535 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
536 return "HEXADECIMAL";
537 default:
538 return "(unknown)";
539 }
540 }
541
542 static inline
543 const char *bt_common_scope_string(enum bt_field_path_scope scope)
544 {
545 switch (scope) {
546 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT:
547 return "PACKET_CONTEXT";
548 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT:
549 return "EVENT_COMMON_CONTEXT";
550 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT:
551 return "EVENT_SPECIFIC_CONTEXT";
552 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD:
553 return "EVENT_PAYLOAD";
554 default:
555 return "(unknown)";
556 }
557 }
558
559 static inline
560 const char *bt_common_event_class_log_level_string(
561 enum bt_event_class_log_level level)
562 {
563 switch (level) {
564 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
565 return "EMERGENCY";
566 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
567 return "ALERT";
568 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
569 return "CRITICAL";
570 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
571 return "ERROR";
572 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
573 return "WARNING";
574 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
575 return "NOTICE";
576 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
577 return "INFO";
578 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
579 return "DEBUG_SYSTEM";
580 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
581 return "DEBUG_PROGRAM";
582 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
583 return "DEBUG_PROCESS";
584 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
585 return "DEBUG_MODULE";
586 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
587 return "DEBUG_UNIT";
588 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
589 return "DEBUG_FUNCTION";
590 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
591 return "DEBUG_LINE";
592 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
593 return "DEBUG";
594 default:
595 return "(unknown)";
596 }
597 };
598
599 static inline
600 const char *bt_common_value_type_string(enum bt_value_type type)
601 {
602 switch (type) {
603 case BT_VALUE_TYPE_NULL:
604 return "NULL";
605 case BT_VALUE_TYPE_BOOL:
606 return "BOOL";
607 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
608 return "UNSIGNED_INTEGER";
609 case BT_VALUE_TYPE_SIGNED_INTEGER:
610 return "SIGNED_INTEGER";
611 case BT_VALUE_TYPE_REAL:
612 return "REAL";
613 case BT_VALUE_TYPE_STRING:
614 return "STRING";
615 case BT_VALUE_TYPE_ARRAY:
616 return "ARRAY";
617 case BT_VALUE_TYPE_MAP:
618 return "MAP";
619 default:
620 return "(unknown)";
621 }
622 };
623
624 static inline
625 GString *bt_common_field_path_string(struct bt_field_path *path)
626 {
627 GString *str = g_string_new(NULL);
628 uint64_t i;
629
630 BT_ASSERT_DBG(path);
631
632 if (!str) {
633 goto end;
634 }
635
636 g_string_append_printf(str, "[%s", bt_common_scope_string(
637 bt_field_path_get_root_scope(path)));
638
639 for (i = 0; i < bt_field_path_get_item_count(path); i++) {
640 const struct bt_field_path_item *fp_item =
641 bt_field_path_borrow_item_by_index_const(path, i);
642
643 switch (bt_field_path_item_get_type(fp_item)) {
644 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
645 g_string_append_printf(str, ", %" PRIu64,
646 bt_field_path_item_index_get_index(fp_item));
647 break;
648 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
649 g_string_append(str, ", <CUR>");
650 break;
651 default:
652 bt_common_abort();
653 }
654 }
655
656 g_string_append(str, "]");
657
658 end:
659 return str;
660 }
661
662 static inline
663 const char *bt_common_logging_level_string(
664 enum bt_logging_level level)
665 {
666 switch (level) {
667 case BT_LOGGING_LEVEL_TRACE:
668 return "TRACE";
669 case BT_LOGGING_LEVEL_DEBUG:
670 return "DEBUG";
671 case BT_LOGGING_LEVEL_INFO:
672 return "INFO";
673 case BT_LOGGING_LEVEL_WARNING:
674 return "WARNING";
675 case BT_LOGGING_LEVEL_ERROR:
676 return "ERROR";
677 case BT_LOGGING_LEVEL_FATAL:
678 return "FATAL";
679 case BT_LOGGING_LEVEL_NONE:
680 return "NONE";
681 default:
682 return "(unknown)";
683 }
684 };
685
686 static inline
687 const char *bt_common_func_status_string(int status)
688 {
689 switch (status) {
690 case __BT_FUNC_STATUS_OVERFLOW_ERROR:
691 return "OVERFLOW";
692 case __BT_FUNC_STATUS_UNKNOWN_OBJECT:
693 return "UNKNOWN_OBJECT";
694 case __BT_FUNC_STATUS_MEMORY_ERROR:
695 return "MEMORY_ERROR";
696 case __BT_FUNC_STATUS_USER_ERROR:
697 return "USER_ERROR";
698 case __BT_FUNC_STATUS_ERROR:
699 return "ERROR";
700 case __BT_FUNC_STATUS_OK:
701 return "OK";
702 case __BT_FUNC_STATUS_END:
703 return "END";
704 case __BT_FUNC_STATUS_NOT_FOUND:
705 return "NOT_FOUND";
706 case __BT_FUNC_STATUS_AGAIN:
707 return "AGAIN";
708 case __BT_FUNC_STATUS_INTERRUPTED:
709 return "INTERRUPTED";
710 default:
711 return "(unknown)";
712 }
713 }
714
715 #define NS_PER_S_I INT64_C(1000000000)
716 #define NS_PER_S_U UINT64_C(1000000000)
717
718 static inline
719 int bt_common_clock_value_from_ns_from_origin(
720 int64_t cc_offset_seconds, uint64_t cc_offset_cycles,
721 uint64_t cc_freq, int64_t ns_from_origin,
722 uint64_t *raw_value)
723 {
724 int ret = 0;
725 int64_t offset_in_ns;
726 uint64_t value_in_ns;
727 uint64_t rem_value_in_ns;
728 uint64_t value_periods;
729 uint64_t value_period_cycles;
730 int64_t ns_to_add;
731
732 BT_ASSERT_DBG(raw_value);
733
734 /* Compute offset part of requested value, in nanoseconds */
735 if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) {
736 ret = -1;
737 goto end;
738 }
739
740 offset_in_ns = cc_offset_seconds * NS_PER_S_I;
741
742 if (cc_freq == NS_PER_S_U) {
743 ns_to_add = (int64_t) cc_offset_cycles;
744 } else {
745 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles,
746 NS_PER_S_I)) {
747 ret = -1;
748 goto end;
749 }
750
751 ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) /
752 (int64_t) cc_freq;
753 }
754
755 if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) {
756 ret = -1;
757 goto end;
758 }
759
760 offset_in_ns += ns_to_add;
761
762 /* Value part in nanoseconds */
763 if (ns_from_origin < offset_in_ns) {
764 ret = -1;
765 goto end;
766 }
767
768 value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns);
769
770 /* Number of whole clock periods in `value_in_ns` */
771 value_periods = value_in_ns / NS_PER_S_U;
772
773 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
774 rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U;
775
776 if (value_periods > UINT64_MAX / cc_freq) {
777 ret = -1;
778 goto end;
779 }
780
781 if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) {
782 ret = -1;
783 goto end;
784 }
785
786 value_period_cycles = value_periods * cc_freq;
787
788 if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) {
789 ret = -1;
790 goto end;
791 }
792
793 if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U,
794 value_period_cycles)) {
795 ret = -1;
796 goto end;
797 }
798
799 *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U +
800 value_period_cycles;
801
802 end:
803 return ret;
804 }
805
806 /*
807 * bt_g_string_append_printf cannot be inlined because it expects a
808 * variadic argument list.
809 */
810 BT_HIDDEN __BT_ATTR_FORMAT_PRINTF(2, 3)
811 int bt_common_g_string_append_printf(GString *str, const char *fmt, ...);
812
813 static inline
814 void bt_common_g_string_append(GString *str, const char *s)
815 {
816 gsize len, allocated_len, s_len;
817
818 /* str->len excludes \0. */
819 len = str->len;
820 /* Exclude \0. */
821 allocated_len = str->allocated_len - 1;
822 s_len = strlen(s);
823 if (G_UNLIKELY(allocated_len < len + s_len)) {
824 /* Resize. */
825 g_string_set_size(str, len + s_len);
826 } else {
827 str->len = len + s_len;
828 }
829 memcpy(str->str + len, s, s_len + 1);
830 }
831
832 static inline
833 void bt_common_g_string_append_c(GString *str, char c)
834 {
835 gsize len, allocated_len, s_len;
836
837 /* str->len excludes \0. */
838 len = str->len;
839 /* Exclude \0. */
840 allocated_len = str->allocated_len - 1;
841 s_len = 1;
842 if (G_UNLIKELY(allocated_len < len + s_len)) {
843 /* Resize. */
844 g_string_set_size(str, len + s_len);
845 } else {
846 str->len = len + s_len;
847 }
848 str->str[len] = c;
849 str->str[len + 1] = '\0';
850 }
851
852 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.067231 seconds and 4 git commands to generate.