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