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