tracing_format_common.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2019 Intel corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <sys/cbprintf.h>
  8. #include <tracing_buffer.h>
  9. #include <tracing_format_common.h>
  10. static int str_put(int c, void *ctx)
  11. {
  12. tracing_ctx_t *str_ctx = (tracing_ctx_t *)ctx;
  13. if (str_ctx->status == 0) {
  14. uint8_t *buf;
  15. uint32_t claimed_size;
  16. claimed_size = tracing_buffer_put_claim(&buf, 1);
  17. if (claimed_size) {
  18. *buf = (uint8_t)c;
  19. str_ctx->length++;
  20. } else {
  21. str_ctx->status = -1;
  22. }
  23. }
  24. return 0;
  25. }
  26. bool tracing_format_string_put(const char *str, va_list args)
  27. {
  28. tracing_ctx_t str_ctx = {0};
  29. (void)cbvprintf(str_put, (void *)&str_ctx, str, args);
  30. if (str_ctx.status == 0) {
  31. tracing_buffer_put_finish(str_ctx.length);
  32. return true;
  33. }
  34. tracing_buffer_put_finish(0);
  35. return false;
  36. }
  37. bool tracing_format_raw_data_put(uint8_t *data, uint32_t size)
  38. {
  39. uint32_t space = tracing_buffer_space_get();
  40. if (space >= size) {
  41. tracing_buffer_put(data, size);
  42. return true;
  43. }
  44. return false;
  45. }
  46. bool tracing_format_data_put(tracing_data_t *tracing_data_array, uint32_t count)
  47. {
  48. uint32_t total_size = 0U;
  49. for (uint32_t i = 0; i < count; i++) {
  50. tracing_data_t *tracing_data =
  51. tracing_data_array + i;
  52. uint8_t *data = tracing_data->data, *buf;
  53. uint32_t length = tracing_data->length, claimed_size;
  54. do {
  55. claimed_size = tracing_buffer_put_claim(&buf, length);
  56. memcpy(buf, data, claimed_size);
  57. total_size += claimed_size;
  58. length -= claimed_size;
  59. data += claimed_size;
  60. } while (length && claimed_size);
  61. if (length && claimed_size == 0) {
  62. tracing_buffer_put_finish(0);
  63. return false;
  64. }
  65. }
  66. tracing_buffer_put_finish(total_size);
  67. return true;
  68. }