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