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