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