lib: make trace IR API const-correct
[babeltrace.git] / include / babeltrace / common-internal.h
CommitLineData
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
c55a9f58 27#include <stdbool.h>
3dca2276 28#include <babeltrace/assert-internal.h>
1670bffd 29#include <babeltrace/babeltrace-internal.h>
40f4ba76
PP
30#include <babeltrace/trace-ir/field-classes-const.h>
31#include <babeltrace/trace-ir/field-path-const.h>
32#include <babeltrace/trace-ir/event-class-const.h>
da91b29a 33#include <babeltrace/values.h>
85cd02cf 34#include <stdarg.h>
44c440bc
PP
35#include <inttypes.h>
36#include <stdint.h>
3dca2276 37#include <glib.h>
1670bffd 38
ad96d936
PP
39#define BT_COMMON_COLOR_RESET "\033[0m"
40#define BT_COMMON_COLOR_BOLD "\033[1m"
41#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
42#define BT_COMMON_COLOR_FG_RED "\033[31m"
43#define BT_COMMON_COLOR_FG_GREEN "\033[32m"
44#define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
45#define BT_COMMON_COLOR_FG_BLUE "\033[34m"
46#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
47#define BT_COMMON_COLOR_FG_CYAN "\033[36m"
48#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
49#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
50#define BT_COMMON_COLOR_BG_RED "\033[41m"
51#define BT_COMMON_COLOR_BG_GREEN "\033[42m"
52#define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
53#define BT_COMMON_COLOR_BG_BLUE "\033[44m"
54#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
55#define BT_COMMON_COLOR_BG_CYAN "\033[46m"
56#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
57
db0f160a
PP
58struct bt_common_lttng_live_url_parts {
59 GString *proto;
60 GString *hostname;
61 GString *target_hostname;
62 GString *session_name;
63
64 /* -1 means default port */
65 int port;
66};
67
6eecdc5f
PP
68/*
69 * Checks if the current process has setuid or setgid access rights.
70 * Returns `true` if so.
71 */
1670bffd
PP
72BT_HIDDEN
73bool bt_common_is_setuid_setgid(void);
74
6eecdc5f
PP
75/*
76 * Returns the system-wide plugin path, e.g.
77 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
78 */
1670bffd
PP
79BT_HIDDEN
80const char *bt_common_get_system_plugin_path(void);
81
6eecdc5f
PP
82/*
83 * Returns the user plugin path, e.g.
84 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
85 * return value.
86 */
1670bffd
PP
87BT_HIDDEN
88char *bt_common_get_home_plugin_path(void);
89
6eecdc5f
PP
90/*
91 * Appends the list of directories in `paths` to the array `dirs`.
92 * `paths` is a list of directories separated by `:`. Returns 0 on
93 * success.
94 */
1670bffd
PP
95BT_HIDDEN
96int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
97
6eecdc5f
PP
98/*
99 * Returns `true` if terminal color codes are supported for this
100 * process.
101 */
ad96d936
PP
102BT_HIDDEN
103bool bt_common_colors_supported(void);
104
290725f7
PP
105BT_HIDDEN
106const char *bt_common_color_reset(void);
107
108BT_HIDDEN
109const char *bt_common_color_bold(void);
110
111BT_HIDDEN
112const char *bt_common_color_fg_default(void);
113
114BT_HIDDEN
115const char *bt_common_color_fg_red(void);
116
117BT_HIDDEN
118const char *bt_common_color_fg_green(void);
119
120BT_HIDDEN
121const char *bt_common_color_fg_yellow(void);
122
123BT_HIDDEN
124const char *bt_common_color_fg_blue(void);
125
126BT_HIDDEN
127const char *bt_common_color_fg_magenta(void);
128
129BT_HIDDEN
130const char *bt_common_color_fg_cyan(void);
131
132BT_HIDDEN
133const char *bt_common_color_fg_light_gray(void);
134
135BT_HIDDEN
136const char *bt_common_color_bg_default(void);
137
138BT_HIDDEN
139const char *bt_common_color_bg_red(void);
140
141BT_HIDDEN
142const char *bt_common_color_bg_green(void);
143
144BT_HIDDEN
145const char *bt_common_color_bg_yellow(void);
146
147BT_HIDDEN
148const char *bt_common_color_bg_blue(void);
149
150BT_HIDDEN
151const char *bt_common_color_bg_magenta(void);
152
153BT_HIDDEN
154const char *bt_common_color_bg_cyan(void);
155
156BT_HIDDEN
157const char *bt_common_color_bg_light_gray(void);
158
6eecdc5f
PP
159/*
160 * Returns the substring from `input` to the first character found
161 * in the list of characters `end_chars`, unescaping any character
162 * found in `escapable_chars`, and sets `*end_pos` to the position of
163 * the end (from `input`). The caller owns the returned GString.
164 */
db0f160a
PP
165BT_HIDDEN
166GString *bt_common_string_until(const char *input, const char *escapable_chars,
167 const char *end_chars, size_t *end_pos);
168
6eecdc5f
PP
169/*
170 * Returns the quoted version of `input` for a shell. If
171 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
172 * and suffix to the returned string; otherwise the caller should
173 * prepend and append them manually, although they are not always
174 * required. The caller owns the returned GString.
175 */
db0f160a 176BT_HIDDEN
36b405c6 177GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
db0f160a 178
6eecdc5f
PP
179/*
180 * Returns `true` if `input` is a string made only of printable
181 * characters.
182 */
db0f160a
PP
183BT_HIDDEN
184bool bt_common_string_is_printable(const char *input);
185
6eecdc5f
PP
186/*
187 * Destroys the parts of an LTTng live URL as returned by
188 * bt_common_parse_lttng_live_url().
189 */
db0f160a
PP
190BT_HIDDEN
191void bt_common_destroy_lttng_live_url_parts(
192 struct bt_common_lttng_live_url_parts *parts);
193
6eecdc5f
PP
194/*
195 * Parses the LTTng live URL `url` and returns its different parts.
196 * If there's an error, writes the error message into `*error_buf`
197 * up to `error_buf_size` bytes. You must destroy the returned value
198 * with bt_common_destroy_lttng_live_url_parts().
199 */
db0f160a
PP
200BT_HIDDEN
201struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
202 const char *url, char *error_buf, size_t error_buf_size);
203
6eecdc5f
PP
204/*
205 * Normalizes (in place) a star globbing pattern to be used with
206 * bt_common_star_glob_match(). This function always succeeds.
207 */
9009cc24
PP
208BT_HIDDEN
209void bt_common_normalize_star_glob_pattern(char *pattern);
210
6eecdc5f
PP
211/*
212 * Returns `true` if `candidate` (of size `candidate_len`) matches
213 * the star globbing pattern `pattern` (of size `pattern_len`).
214 */
9009cc24
PP
215BT_HIDDEN
216bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
217 const char *candidate, size_t candidate_len);
218
6eecdc5f
PP
219/*
220 * Normalizes the path `path`:
221 *
222 * * If it's a relative path, converts it to an absolute path using
223 * `wd` as the working directory (or the current working directory
224 * if `wd` is NULL).
225 * * Removes consecutive and trailing slashes.
226 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
227 * * Does NOT resolve symbolic links.
228 *
229 * The caller owns the returned GString.
230 */
e49a18d1
PP
231BT_HIDDEN
232GString *bt_common_normalize_path(const char *path, const char *wd);
233
85cd02cf
PP
234typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
235 char **buf, size_t avail_size, const char **fmt, va_list *args);
236
237/*
238 * This is a custom vsnprintf() which handles the standard conversion
239 * specifier as well as custom ones.
240 *
241 * `fmt` is a typical printf()-style format string, with the following
242 * limitations:
243 *
244 * * The `*` width specifier is not accepted.
245 * * The `*` precision specifier is not accepted.
246 * * The `j` and `t` length modifiers are not accepted.
247 * * The `n` format specifier is not accepted.
248 * * The format specifiers defined in <inttypes.h> are not accepted
249 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
250 * `PRIi64`.
251 *
252 * `intro` specifies which special character immediately following an
253 * introductory `%` character in `fmt` is used to indicate a custom
254 * conversion specifier. For example, if `intro` is '@', then any `%@`
255 * sequence in `fmt` is the beginning of a custom conversion specifier.
256 *
257 * When a custom conversion specifier is encountered in `fmt`,
258 * the function calls `handle_specifier`. This callback receives:
259 *
260 * `priv_data`:
261 * Custom, private data.
262 *
263 * `buf`:
264 * Address of the current buffer pointer. `*buf` is the position to
265 * append new data. The callback must update `*buf` when appending
266 * new data. The callback must ensure not to write passed the whole
267 * buffer passed to bt_common_custom_vsnprintf().
268 *
269 * `avail_size`:
270 * Number of bytes left in whole buffer from the `*buf` point.
271 *
272 * `fmt`:
273 * Address of the current format string pointer. `*fmt` points to
274 * the introductory `%` character, which is followed by the
275 * character `intro`. The callback must update `*fmt` so that it
276 * points after the whole custom conversion specifier.
277 *
278 * `args`:
279 * Variable argument list. Use va_arg() to get new arguments from
280 * this list and update it at the same time.
281 *
282 * Because this is an internal utility, this function and its callback
283 * do not return error codes: they abort when there's any error (bad
284 * format string, for example).
285 */
286BT_HIDDEN
287void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
288 char intro,
289 bt_common_handle_custom_specifier_func handle_specifier,
290 void *priv_data, const char *fmt, va_list *args);
291
292/*
293 * Variadic form of bt_common_custom_vsnprintf().
294 */
295BT_HIDDEN
296void bt_common_custom_snprintf(char *buf, size_t buf_size,
297 char intro,
298 bt_common_handle_custom_specifier_func handle_specifier,
299 void *priv_data, const char *fmt, ...);
300
108e5a1e 301/*
85cd02cf 302 * Returns the system page size.
108e5a1e
MJ
303 */
304BT_HIDDEN
305size_t bt_common_get_page_size(void);
306
3dca2276 307static inline
864cad70 308const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
3dca2276 309{
864cad70
PP
310 switch (class_type) {
311 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
312 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
313 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
314 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
315 case BT_FIELD_CLASS_TYPE_REAL:
316 return "BT_FIELD_CLASS_TYPE_REAL";
317 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
318 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
319 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
320 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
321 case BT_FIELD_CLASS_TYPE_STRING:
322 return "BT_FIELD_CLASS_TYPE_STRING";
323 case BT_FIELD_CLASS_TYPE_STRUCTURE:
324 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
325 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
326 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
327 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
328 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
329 case BT_FIELD_CLASS_TYPE_VARIANT:
330 return "BT_FIELD_CLASS_TYPE_VARIANT";
3dca2276
PP
331 default:
332 return "(unknown)";
333 }
334};
335
336static inline
5cd6d0e5 337const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
3dca2276
PP
338{
339 switch (base) {
5cd6d0e5
PP
340 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
341 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
342 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
343 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
344 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
345 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
346 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
347 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
3dca2276
PP
348 default:
349 return "(unknown)";
350 }
351}
352
353static inline
354const char *bt_common_scope_string(enum bt_scope scope)
355{
356 switch (scope) {
44c440bc
PP
357 case BT_SCOPE_PACKET_HEADER:
358 return "BT_SCOPE_PACKET_HEADER";
359 case BT_SCOPE_PACKET_CONTEXT:
360 return "BT_SCOPE_PACKET_CONTEXT";
361 case BT_SCOPE_EVENT_HEADER:
362 return "BT_SCOPE_EVENT_HEADER";
363 case BT_SCOPE_EVENT_COMMON_CONTEXT:
364 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
365 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
366 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
3dca2276
PP
367 case BT_SCOPE_EVENT_PAYLOAD:
368 return "BT_SCOPE_EVENT_PAYLOAD";
3dca2276
PP
369 default:
370 return "(unknown)";
371 }
372}
373
374static inline
375const char *bt_common_event_class_log_level_string(
376 enum bt_event_class_log_level level)
377{
378 switch (level) {
3dca2276
PP
379 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
380 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
381 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
382 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
383 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
384 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
385 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
386 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
387 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
388 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
389 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
390 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
391 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
392 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
393 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
394 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
395 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
396 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
397 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
398 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
399 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
400 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
401 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
402 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
403 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
404 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
405 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
406 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
407 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
408 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
409 default:
410 return "(unknown)";
411 }
412};
413
da91b29a
PP
414static inline
415const char *bt_common_value_type_string(enum bt_value_type type)
416{
417 switch (type) {
418 case BT_VALUE_TYPE_NULL:
419 return "BT_VALUE_TYPE_NULL";
420 case BT_VALUE_TYPE_BOOL:
421 return "BT_VALUE_TYPE_BOOL";
422 case BT_VALUE_TYPE_INTEGER:
423 return "BT_VALUE_TYPE_INTEGER";
424 case BT_VALUE_TYPE_REAL:
425 return "BT_VALUE_TYPE_REAL";
426 case BT_VALUE_TYPE_STRING:
427 return "BT_VALUE_TYPE_STRING";
428 case BT_VALUE_TYPE_ARRAY:
429 return "BT_VALUE_TYPE_ARRAY";
430 case BT_VALUE_TYPE_MAP:
431 return "BT_VALUE_TYPE_MAP";
432 default:
433 return "(unknown)";
434 }
435};
436
3dca2276
PP
437static inline
438GString *bt_field_path_string(struct bt_field_path *path)
439{
440 GString *str = g_string_new(NULL);
44c440bc 441 uint64_t i;
3dca2276
PP
442
443 BT_ASSERT(path);
444
445 if (!str) {
446 goto end;
447 }
448
449 g_string_append_printf(str, "[%s", bt_common_scope_string(
450 bt_field_path_get_root_scope(path)));
451
452 for (i = 0; i < bt_field_path_get_index_count(path); i++) {
44c440bc
PP
453 g_string_append_printf(str, ", %" PRIu64,
454 bt_field_path_get_index_by_index(path, i));
3dca2276
PP
455 }
456
457 g_string_append(str, "]");
458
459end:
460 return str;
461}
462
1670bffd 463#endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.05357 seconds and 4 git commands to generate.