lib: make trace IR API const-correct
[babeltrace.git] / include / babeltrace / common-internal.h
1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
3
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
27 #include <stdbool.h>
28 #include <babeltrace/assert-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
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>
33 #include <babeltrace/values.h>
34 #include <stdarg.h>
35 #include <inttypes.h>
36 #include <stdint.h>
37 #include <glib.h>
38
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
58 struct 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
68 /*
69 * Checks if the current process has setuid or setgid access rights.
70 * Returns `true` if so.
71 */
72 BT_HIDDEN
73 bool bt_common_is_setuid_setgid(void);
74
75 /*
76 * Returns the system-wide plugin path, e.g.
77 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
78 */
79 BT_HIDDEN
80 const char *bt_common_get_system_plugin_path(void);
81
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 */
87 BT_HIDDEN
88 char *bt_common_get_home_plugin_path(void);
89
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 */
95 BT_HIDDEN
96 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
97
98 /*
99 * Returns `true` if terminal color codes are supported for this
100 * process.
101 */
102 BT_HIDDEN
103 bool bt_common_colors_supported(void);
104
105 BT_HIDDEN
106 const char *bt_common_color_reset(void);
107
108 BT_HIDDEN
109 const char *bt_common_color_bold(void);
110
111 BT_HIDDEN
112 const char *bt_common_color_fg_default(void);
113
114 BT_HIDDEN
115 const char *bt_common_color_fg_red(void);
116
117 BT_HIDDEN
118 const char *bt_common_color_fg_green(void);
119
120 BT_HIDDEN
121 const char *bt_common_color_fg_yellow(void);
122
123 BT_HIDDEN
124 const char *bt_common_color_fg_blue(void);
125
126 BT_HIDDEN
127 const char *bt_common_color_fg_magenta(void);
128
129 BT_HIDDEN
130 const char *bt_common_color_fg_cyan(void);
131
132 BT_HIDDEN
133 const char *bt_common_color_fg_light_gray(void);
134
135 BT_HIDDEN
136 const char *bt_common_color_bg_default(void);
137
138 BT_HIDDEN
139 const char *bt_common_color_bg_red(void);
140
141 BT_HIDDEN
142 const char *bt_common_color_bg_green(void);
143
144 BT_HIDDEN
145 const char *bt_common_color_bg_yellow(void);
146
147 BT_HIDDEN
148 const char *bt_common_color_bg_blue(void);
149
150 BT_HIDDEN
151 const char *bt_common_color_bg_magenta(void);
152
153 BT_HIDDEN
154 const char *bt_common_color_bg_cyan(void);
155
156 BT_HIDDEN
157 const char *bt_common_color_bg_light_gray(void);
158
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 */
165 BT_HIDDEN
166 GString *bt_common_string_until(const char *input, const char *escapable_chars,
167 const char *end_chars, size_t *end_pos);
168
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 */
176 BT_HIDDEN
177 GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
178
179 /*
180 * Returns `true` if `input` is a string made only of printable
181 * characters.
182 */
183 BT_HIDDEN
184 bool bt_common_string_is_printable(const char *input);
185
186 /*
187 * Destroys the parts of an LTTng live URL as returned by
188 * bt_common_parse_lttng_live_url().
189 */
190 BT_HIDDEN
191 void bt_common_destroy_lttng_live_url_parts(
192 struct bt_common_lttng_live_url_parts *parts);
193
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 */
200 BT_HIDDEN
201 struct 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
204 /*
205 * Normalizes (in place) a star globbing pattern to be used with
206 * bt_common_star_glob_match(). This function always succeeds.
207 */
208 BT_HIDDEN
209 void bt_common_normalize_star_glob_pattern(char *pattern);
210
211 /*
212 * Returns `true` if `candidate` (of size `candidate_len`) matches
213 * the star globbing pattern `pattern` (of size `pattern_len`).
214 */
215 BT_HIDDEN
216 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
217 const char *candidate, size_t candidate_len);
218
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 */
231 BT_HIDDEN
232 GString *bt_common_normalize_path(const char *path, const char *wd);
233
234 typedef 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 */
286 BT_HIDDEN
287 void 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 */
295 BT_HIDDEN
296 void 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
301 /*
302 * Returns the system page size.
303 */
304 BT_HIDDEN
305 size_t bt_common_get_page_size(void);
306
307 static inline
308 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
309 {
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";
331 default:
332 return "(unknown)";
333 }
334 };
335
336 static inline
337 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
338 {
339 switch (base) {
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";
348 default:
349 return "(unknown)";
350 }
351 }
352
353 static inline
354 const char *bt_common_scope_string(enum bt_scope scope)
355 {
356 switch (scope) {
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";
367 case BT_SCOPE_EVENT_PAYLOAD:
368 return "BT_SCOPE_EVENT_PAYLOAD";
369 default:
370 return "(unknown)";
371 }
372 }
373
374 static inline
375 const char *bt_common_event_class_log_level_string(
376 enum bt_event_class_log_level level)
377 {
378 switch (level) {
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
414 static inline
415 const 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
437 static inline
438 GString *bt_field_path_string(struct bt_field_path *path)
439 {
440 GString *str = g_string_new(NULL);
441 uint64_t i;
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++) {
453 g_string_append_printf(str, ", %" PRIu64,
454 bt_field_path_get_index_by_index(path, i));
455 }
456
457 g_string_append(str, "]");
458
459 end:
460 return str;
461 }
462
463 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.038223 seconds and 4 git commands to generate.