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