Cleanup: remove private babeltrace.h
[babeltrace.git] / src / common / common.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 <errno.h>
28 #include <glib.h>
29 #include <inttypes.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include <babeltrace2/graph/self-message-iterator.h>
37 #include <babeltrace2/trace-ir/event-class-const.h>
38 #include <babeltrace2/trace-ir/field-class-const.h>
39 #include <babeltrace2/trace-ir/field-path-const.h>
40 #include <babeltrace2/value.h>
41
42 #include "common/assert.h"
43 #include "common/macros.h"
44 #include "common/safe.h"
45
46 #define BT_COMMON_COLOR_RESET "\033[0m"
47 #define BT_COMMON_COLOR_BOLD "\033[1m"
48 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
49 #define BT_COMMON_COLOR_FG_RED "\033[31m"
50 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
51 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
52 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
53 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
54 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
55 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
56 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
57 #define BT_COMMON_COLOR_BG_RED "\033[41m"
58 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
59 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
60 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
61 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
62 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
63 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
64
65 struct bt_common_lttng_live_url_parts {
66 GString *proto;
67 GString *hostname;
68 GString *target_hostname;
69 GString *session_name;
70
71 /* -1 means default port */
72 int port;
73 };
74
75 /*
76 * Checks if the current process has setuid or setgid access rights.
77 * Returns `true` if so.
78 */
79 BT_HIDDEN
80 bool bt_common_is_setuid_setgid(void);
81
82 /*
83 * Returns the system-wide plugin path, e.g.
84 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
85 */
86 BT_HIDDEN
87 const char *bt_common_get_system_plugin_path(void);
88
89 /*
90 * Returns the user plugin path, e.g.
91 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
92 * return value.
93 */
94 BT_HIDDEN
95 char *bt_common_get_home_plugin_path(void);
96
97 /*
98 * Appends the list of directories in `paths` to the array `dirs`.
99 * `paths` is a list of directories separated by `:`. Returns 0 on
100 * success.
101 */
102 BT_HIDDEN
103 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
104
105 /*
106 * Returns `true` if terminal color codes are supported for this
107 * process.
108 */
109 BT_HIDDEN
110 bool bt_common_colors_supported(void);
111
112 BT_HIDDEN
113 const char *bt_common_color_reset(void);
114
115 BT_HIDDEN
116 const char *bt_common_color_bold(void);
117
118 BT_HIDDEN
119 const char *bt_common_color_fg_default(void);
120
121 BT_HIDDEN
122 const char *bt_common_color_fg_red(void);
123
124 BT_HIDDEN
125 const char *bt_common_color_fg_green(void);
126
127 BT_HIDDEN
128 const char *bt_common_color_fg_yellow(void);
129
130 BT_HIDDEN
131 const char *bt_common_color_fg_blue(void);
132
133 BT_HIDDEN
134 const char *bt_common_color_fg_magenta(void);
135
136 BT_HIDDEN
137 const char *bt_common_color_fg_cyan(void);
138
139 BT_HIDDEN
140 const char *bt_common_color_fg_light_gray(void);
141
142 BT_HIDDEN
143 const char *bt_common_color_bg_default(void);
144
145 BT_HIDDEN
146 const char *bt_common_color_bg_red(void);
147
148 BT_HIDDEN
149 const char *bt_common_color_bg_green(void);
150
151 BT_HIDDEN
152 const char *bt_common_color_bg_yellow(void);
153
154 BT_HIDDEN
155 const char *bt_common_color_bg_blue(void);
156
157 BT_HIDDEN
158 const char *bt_common_color_bg_magenta(void);
159
160 BT_HIDDEN
161 const char *bt_common_color_bg_cyan(void);
162
163 BT_HIDDEN
164 const char *bt_common_color_bg_light_gray(void);
165
166 /*
167 * Returns the substring from `input` to the first character found
168 * in the list of characters `end_chars`, unescaping any character
169 * found in `escapable_chars`, and sets `*end_pos` to the position of
170 * the end (from `input`). The caller owns the returned GString.
171 */
172 BT_HIDDEN
173 GString *bt_common_string_until(const char *input, const char *escapable_chars,
174 const char *end_chars, size_t *end_pos);
175
176 /*
177 * Returns the quoted version of `input` for a shell. If
178 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
179 * and suffix to the returned string; otherwise the caller should
180 * prepend and append them manually, although they are not always
181 * required. The caller owns the returned GString.
182 */
183 BT_HIDDEN
184 GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
185
186 /*
187 * Returns `true` if `input` is a string made only of printable
188 * characters.
189 */
190 BT_HIDDEN
191 bool bt_common_string_is_printable(const char *input);
192
193 /*
194 * Destroys the parts of an LTTng live URL as returned by
195 * bt_common_parse_lttng_live_url().
196 */
197 BT_HIDDEN
198 void bt_common_destroy_lttng_live_url_parts(
199 struct bt_common_lttng_live_url_parts *parts);
200
201 /*
202 * Parses the LTTng live URL `url` and returns its different parts.
203 * If there's an error, writes the error message into `*error_buf`
204 * up to `error_buf_size` bytes. You must destroy the returned value
205 * with bt_common_destroy_lttng_live_url_parts().
206 */
207 BT_HIDDEN
208 struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
209 const char *url, char *error_buf, size_t error_buf_size);
210
211 /*
212 * Normalizes (in place) a star globbing pattern to be used with
213 * bt_common_star_glob_match(). This function always succeeds.
214 */
215 BT_HIDDEN
216 void bt_common_normalize_star_glob_pattern(char *pattern);
217
218 /*
219 * Returns `true` if `candidate` (of size `candidate_len`) matches
220 * the star globbing pattern `pattern` (of size `pattern_len`).
221 */
222 BT_HIDDEN
223 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
224 const char *candidate, size_t candidate_len);
225
226 /*
227 * Normalizes the path `path`:
228 *
229 * * If it's a relative path, converts it to an absolute path using
230 * `wd` as the working directory (or the current working directory
231 * if `wd` is NULL).
232 * * Removes consecutive and trailing slashes.
233 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
234 * * Does NOT resolve symbolic links.
235 *
236 * The caller owns the returned GString.
237 */
238 BT_HIDDEN
239 GString *bt_common_normalize_path(const char *path, const char *wd);
240
241 typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
242 char **buf, size_t avail_size, const char **fmt, va_list *args);
243
244 /*
245 * This is a custom vsnprintf() which handles the standard conversion
246 * specifier as well as custom ones.
247 *
248 * `fmt` is a typical printf()-style format string, with the following
249 * limitations:
250 *
251 * * The `*` width specifier is not accepted.
252 * * The `*` precision specifier is not accepted.
253 * * The `j` and `t` length modifiers are not accepted.
254 * * The `n` format specifier is not accepted.
255 * * The format specifiers defined in <inttypes.h> are not accepted
256 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
257 * `PRIi64`.
258 *
259 * `intro` specifies which special character immediately following an
260 * introductory `%` character in `fmt` is used to indicate a custom
261 * conversion specifier. For example, if `intro` is '@', then any `%@`
262 * sequence in `fmt` is the beginning of a custom conversion specifier.
263 *
264 * When a custom conversion specifier is encountered in `fmt`,
265 * the function calls `handle_specifier`. This callback receives:
266 *
267 * `priv_data`:
268 * Custom, private data.
269 *
270 * `buf`:
271 * Address of the current buffer pointer. `*buf` is the position to
272 * append new data. The callback must update `*buf` when appending
273 * new data. The callback must ensure not to write passed the whole
274 * buffer passed to bt_common_custom_vsnprintf().
275 *
276 * `avail_size`:
277 * Number of bytes left in whole buffer from the `*buf` point.
278 *
279 * `fmt`:
280 * Address of the current format string pointer. `*fmt` points to
281 * the introductory `%` character, which is followed by the
282 * character `intro`. The callback must update `*fmt` so that it
283 * points after the whole custom conversion specifier.
284 *
285 * `args`:
286 * Variable argument list. Use va_arg() to get new arguments from
287 * this list and update it at the same time.
288 *
289 * Because this is an internal utility, this function and its callback
290 * do not return error codes: they abort when there's any error (bad
291 * format string, for example).
292 */
293 BT_HIDDEN
294 void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
295 char intro,
296 bt_common_handle_custom_specifier_func handle_specifier,
297 void *priv_data, const char *fmt, va_list *args);
298
299 /*
300 * Variadic form of bt_common_custom_vsnprintf().
301 */
302 BT_HIDDEN
303 void bt_common_custom_snprintf(char *buf, size_t buf_size,
304 char intro,
305 bt_common_handle_custom_specifier_func handle_specifier,
306 void *priv_data, const char *fmt, ...);
307
308 /*
309 * Returns the system page size.
310 */
311 BT_HIDDEN
312 size_t bt_common_get_page_size(void);
313
314 /*
315 * Wraps read() function to handle EINTR and partial reads.
316 * On success, it returns `count` received as parameter. On error, it returns a
317 * value smaller than the requested `count`.
318 */
319 static inline
320 ssize_t bt_common_read(int fd, void *buf, size_t count)
321 {
322 size_t i = 0;
323 ssize_t ret;
324
325 BT_ASSERT(buf);
326
327 /* Never return an overflow value. */
328 BT_ASSERT(count <= SSIZE_MAX);
329
330 do {
331 ret = read(fd, buf + i, count - i);
332 if (ret < 0) {
333 if (errno == EINTR) {
334 #ifdef BT_LOGD_STR
335 BT_LOGD_STR("read() call interrupted. Retrying...");
336 #endif
337 /* retry operation */
338 continue;
339 } else {
340 #ifdef BT_LOGE_ERRNO
341 BT_LOGE_ERRNO("Error while reading", ": fd=%d",
342 fd);
343 #endif
344 goto end;
345 }
346 }
347 i += ret;
348 BT_ASSERT(i <= count);
349 } while (count - i > 0 && ret > 0);
350
351 end:
352 if (ret >= 0) {
353 if (i == 0) {
354 ret = -1;
355 } else {
356 ret = i;
357 }
358 }
359
360 return ret;
361 }
362
363 static inline
364 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
365 {
366 switch (class_type) {
367 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
368 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
369 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
370 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
371 case BT_FIELD_CLASS_TYPE_REAL:
372 return "BT_FIELD_CLASS_TYPE_REAL";
373 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
374 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
375 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
376 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
377 case BT_FIELD_CLASS_TYPE_STRING:
378 return "BT_FIELD_CLASS_TYPE_STRING";
379 case BT_FIELD_CLASS_TYPE_STRUCTURE:
380 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
381 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
382 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
383 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
384 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
385 case BT_FIELD_CLASS_TYPE_VARIANT:
386 return "BT_FIELD_CLASS_TYPE_VARIANT";
387 default:
388 return "(unknown)";
389 }
390 };
391
392 static inline
393 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
394 {
395 switch (base) {
396 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
397 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
398 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
399 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
400 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
401 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
402 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
403 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
404 default:
405 return "(unknown)";
406 }
407 }
408
409 static inline
410 const char *bt_common_scope_string(enum bt_scope scope)
411 {
412 switch (scope) {
413 case BT_SCOPE_PACKET_CONTEXT:
414 return "BT_SCOPE_PACKET_CONTEXT";
415 case BT_SCOPE_EVENT_COMMON_CONTEXT:
416 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
417 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
418 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
419 case BT_SCOPE_EVENT_PAYLOAD:
420 return "BT_SCOPE_EVENT_PAYLOAD";
421 default:
422 return "(unknown)";
423 }
424 }
425
426 static inline
427 const char *bt_common_event_class_log_level_string(
428 enum bt_event_class_log_level level)
429 {
430 switch (level) {
431 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
432 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
433 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
434 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
435 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
436 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
437 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
438 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
439 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
440 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
441 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
442 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
443 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
444 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
445 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
446 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
447 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
448 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
449 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
450 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
451 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
452 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
453 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
454 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
455 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
456 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
457 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
458 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
459 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
460 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
461 default:
462 return "(unknown)";
463 }
464 };
465
466 static inline
467 const char *bt_common_value_type_string(enum bt_value_type type)
468 {
469 switch (type) {
470 case BT_VALUE_TYPE_NULL:
471 return "BT_VALUE_TYPE_NULL";
472 case BT_VALUE_TYPE_BOOL:
473 return "BT_VALUE_TYPE_BOOL";
474 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
475 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
476 case BT_VALUE_TYPE_SIGNED_INTEGER:
477 return "BT_VALUE_TYPE_SIGNED_INTEGER";
478 case BT_VALUE_TYPE_REAL:
479 return "BT_VALUE_TYPE_REAL";
480 case BT_VALUE_TYPE_STRING:
481 return "BT_VALUE_TYPE_STRING";
482 case BT_VALUE_TYPE_ARRAY:
483 return "BT_VALUE_TYPE_ARRAY";
484 case BT_VALUE_TYPE_MAP:
485 return "BT_VALUE_TYPE_MAP";
486 default:
487 return "(unknown)";
488 }
489 };
490
491 static inline
492 GString *bt_common_field_path_string(struct bt_field_path *path)
493 {
494 GString *str = g_string_new(NULL);
495 uint64_t i;
496
497 BT_ASSERT(path);
498
499 if (!str) {
500 goto end;
501 }
502
503 g_string_append_printf(str, "[%s", bt_common_scope_string(
504 bt_field_path_get_root_scope(path)));
505
506 for (i = 0; i < bt_field_path_get_item_count(path); i++) {
507 const struct bt_field_path_item *fp_item =
508 bt_field_path_borrow_item_by_index_const(path, i);
509
510 switch (bt_field_path_item_get_type(fp_item)) {
511 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
512 g_string_append_printf(str, ", %" PRIu64,
513 bt_field_path_item_index_get_index(fp_item));
514 break;
515 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
516 g_string_append(str, ", <CUR>");
517 break;
518 default:
519 abort();
520 }
521 }
522
523 g_string_append(str, "]");
524
525 end:
526 return str;
527 }
528
529 static inline
530 const char *bt_common_self_message_iterator_status_string(
531 enum bt_self_message_iterator_status status)
532 {
533 switch (status) {
534 case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN:
535 return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
536 case BT_SELF_MESSAGE_ITERATOR_STATUS_END:
537 return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
538 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK:
539 return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
540 case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR:
541 return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
542 case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM:
543 return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
544 default:
545 return "(unknown)";
546 }
547 };
548
549 #define NS_PER_S_I INT64_C(1000000000)
550 #define NS_PER_S_U UINT64_C(1000000000)
551
552 static inline
553 int bt_common_clock_value_from_ns_from_origin(
554 int64_t cc_offset_seconds, uint64_t cc_offset_cycles,
555 uint64_t cc_freq, int64_t ns_from_origin,
556 uint64_t *raw_value)
557 {
558 int ret = 0;
559 int64_t offset_in_ns;
560 uint64_t value_in_ns;
561 uint64_t rem_value_in_ns;
562 uint64_t value_periods;
563 uint64_t value_period_cycles;
564 int64_t ns_to_add;
565
566 BT_ASSERT(raw_value);
567
568 /* Compute offset part of requested value, in nanoseconds */
569 if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) {
570 ret = -1;
571 goto end;
572 }
573
574 offset_in_ns = cc_offset_seconds * NS_PER_S_I;
575
576 if (cc_freq == NS_PER_S_U) {
577 ns_to_add = (int64_t) cc_offset_cycles;
578 } else {
579 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles,
580 NS_PER_S_I)) {
581 ret = -1;
582 goto end;
583 }
584
585 ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) /
586 (int64_t) cc_freq;
587 }
588
589 if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) {
590 ret = -1;
591 goto end;
592 }
593
594 offset_in_ns += ns_to_add;
595
596 /* Value part in nanoseconds */
597 if (ns_from_origin < offset_in_ns) {
598 ret = -1;
599 goto end;
600 }
601
602 value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns);
603
604 /* Number of whole clock periods in `value_in_ns` */
605 value_periods = value_in_ns / NS_PER_S_U;
606
607 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
608 rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U;
609
610 if (value_periods > UINT64_MAX / cc_freq) {
611 ret = -1;
612 goto end;
613 }
614
615 if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) {
616 ret = -1;
617 goto end;
618 }
619
620 value_period_cycles = value_periods * cc_freq;
621
622 if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) {
623 ret = -1;
624 goto end;
625 }
626
627 if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U,
628 value_period_cycles)) {
629 ret = -1;
630 goto end;
631 }
632
633 *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U +
634 value_period_cycles;
635
636 end:
637 return ret;
638 }
639
640 static inline
641 enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self(
642 enum bt_message_iterator_status status)
643 {
644 return (int) status;
645 }
646 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.041591 seconds and 4 git commands to generate.