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