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