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