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