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