assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[babeltrace.git] / include / babeltrace / common-internal.h
CommitLineData
1670bffd
PP
1#ifndef BABELTRACE_COMMON_INTERNAL_H
2#define BABELTRACE_COMMON_INTERNAL_H
3
c55a9f58 4#include <stdbool.h>
3dca2276 5#include <babeltrace/assert-internal.h>
1670bffd 6#include <babeltrace/babeltrace-internal.h>
3dca2276
PP
7#include <babeltrace/ctf-ir/field-types.h>
8#include <babeltrace/ctf-ir/field-path.h>
9#include <babeltrace/ctf-ir/event-class.h>
85cd02cf 10#include <stdarg.h>
3dca2276 11#include <glib.h>
1670bffd 12
ad96d936
PP
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
db0f160a
PP
32struct 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
6eecdc5f
PP
42/*
43 * Checks if the current process has setuid or setgid access rights.
44 * Returns `true` if so.
45 */
1670bffd
PP
46BT_HIDDEN
47bool bt_common_is_setuid_setgid(void);
48
6eecdc5f
PP
49/*
50 * Returns the system-wide plugin path, e.g.
51 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
52 */
1670bffd
PP
53BT_HIDDEN
54const char *bt_common_get_system_plugin_path(void);
55
6eecdc5f
PP
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 */
1670bffd
PP
61BT_HIDDEN
62char *bt_common_get_home_plugin_path(void);
63
6eecdc5f
PP
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 */
1670bffd
PP
69BT_HIDDEN
70int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
71
6eecdc5f
PP
72/*
73 * Returns `true` if terminal color codes are supported for this
74 * process.
75 */
ad96d936
PP
76BT_HIDDEN
77bool bt_common_colors_supported(void);
78
290725f7
PP
79BT_HIDDEN
80const char *bt_common_color_reset(void);
81
82BT_HIDDEN
83const char *bt_common_color_bold(void);
84
85BT_HIDDEN
86const char *bt_common_color_fg_default(void);
87
88BT_HIDDEN
89const char *bt_common_color_fg_red(void);
90
91BT_HIDDEN
92const char *bt_common_color_fg_green(void);
93
94BT_HIDDEN
95const char *bt_common_color_fg_yellow(void);
96
97BT_HIDDEN
98const char *bt_common_color_fg_blue(void);
99
100BT_HIDDEN
101const char *bt_common_color_fg_magenta(void);
102
103BT_HIDDEN
104const char *bt_common_color_fg_cyan(void);
105
106BT_HIDDEN
107const char *bt_common_color_fg_light_gray(void);
108
109BT_HIDDEN
110const char *bt_common_color_bg_default(void);
111
112BT_HIDDEN
113const char *bt_common_color_bg_red(void);
114
115BT_HIDDEN
116const char *bt_common_color_bg_green(void);
117
118BT_HIDDEN
119const char *bt_common_color_bg_yellow(void);
120
121BT_HIDDEN
122const char *bt_common_color_bg_blue(void);
123
124BT_HIDDEN
125const char *bt_common_color_bg_magenta(void);
126
127BT_HIDDEN
128const char *bt_common_color_bg_cyan(void);
129
130BT_HIDDEN
131const char *bt_common_color_bg_light_gray(void);
132
6eecdc5f
PP
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 */
db0f160a
PP
139BT_HIDDEN
140GString *bt_common_string_until(const char *input, const char *escapable_chars,
141 const char *end_chars, size_t *end_pos);
142
6eecdc5f
PP
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 */
db0f160a 150BT_HIDDEN
36b405c6 151GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
db0f160a 152
6eecdc5f
PP
153/*
154 * Returns `true` if `input` is a string made only of printable
155 * characters.
156 */
db0f160a
PP
157BT_HIDDEN
158bool bt_common_string_is_printable(const char *input);
159
6eecdc5f
PP
160/*
161 * Destroys the parts of an LTTng live URL as returned by
162 * bt_common_parse_lttng_live_url().
163 */
db0f160a
PP
164BT_HIDDEN
165void bt_common_destroy_lttng_live_url_parts(
166 struct bt_common_lttng_live_url_parts *parts);
167
6eecdc5f
PP
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 */
db0f160a
PP
174BT_HIDDEN
175struct 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
6eecdc5f
PP
178/*
179 * Normalizes (in place) a star globbing pattern to be used with
180 * bt_common_star_glob_match(). This function always succeeds.
181 */
9009cc24
PP
182BT_HIDDEN
183void bt_common_normalize_star_glob_pattern(char *pattern);
184
6eecdc5f
PP
185/*
186 * Returns `true` if `candidate` (of size `candidate_len`) matches
187 * the star globbing pattern `pattern` (of size `pattern_len`).
188 */
9009cc24
PP
189BT_HIDDEN
190bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
191 const char *candidate, size_t candidate_len);
192
6eecdc5f
PP
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 */
e49a18d1
PP
205BT_HIDDEN
206GString *bt_common_normalize_path(const char *path, const char *wd);
207
85cd02cf
PP
208typedef 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 */
260BT_HIDDEN
261void 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 */
269BT_HIDDEN
270void 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
108e5a1e 275/*
85cd02cf 276 * Returns the system page size.
108e5a1e
MJ
277 */
278BT_HIDDEN
279size_t bt_common_get_page_size(void);
280
3dca2276
PP
281static inline
282const 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
308static inline
309const 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
329static inline
330const 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
346static inline
347const 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
367static inline
368const 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
392static inline
393const 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
436static inline
437GString *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
459end:
460 return str;
461}
462
1670bffd 463#endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.049459 seconds and 4 git commands to generate.