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