mbed_mem_trace.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2016 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include "platform/mbed_mem_trace.h"
  20. #include "platform/mbed_critical.h"
  21. #include "platform/SingletonPtr.h"
  22. #include "platform/PlatformMutex.h"
  23. /******************************************************************************
  24. * Internal variables, functions and helpers
  25. *****************************************************************************/
  26. /* The callback function that will be called after a traced memory operations finishes. */
  27. static mbed_mem_trace_cb_t mem_trace_cb;
  28. /* 'trace_lock_count' guards "trace inside trace" situations (for example, the implementation
  29. * of realloc() might call malloc() internally, and since malloc() is also traced, this could
  30. * result in two calls to the callback function instead of one. */
  31. static uint8_t trace_lock_count;
  32. static SingletonPtr<PlatformMutex> mem_trace_mutex;
  33. #define TRACE_FIRST_LOCK() (trace_lock_count < 2)
  34. /******************************************************************************
  35. * Public interface
  36. *****************************************************************************/
  37. void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb)
  38. {
  39. mem_trace_cb = cb;
  40. }
  41. void mbed_mem_trace_lock()
  42. {
  43. mem_trace_mutex->lock();
  44. trace_lock_count++;
  45. }
  46. void mbed_mem_trace_unlock()
  47. {
  48. trace_lock_count--;
  49. mem_trace_mutex->unlock();
  50. }
  51. void *mbed_mem_trace_malloc(void *res, size_t size, void *caller)
  52. {
  53. if (mem_trace_cb) {
  54. if (TRACE_FIRST_LOCK()) {
  55. mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size);
  56. }
  57. }
  58. return res;
  59. }
  60. void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller)
  61. {
  62. if (mem_trace_cb) {
  63. if (TRACE_FIRST_LOCK()) {
  64. mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size);
  65. }
  66. }
  67. return res;
  68. }
  69. void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller)
  70. {
  71. if (mem_trace_cb) {
  72. if (TRACE_FIRST_LOCK()) {
  73. mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size);
  74. }
  75. }
  76. return res;
  77. }
  78. void mbed_mem_trace_free(void *ptr, void *caller)
  79. {
  80. if (mem_trace_cb) {
  81. if (TRACE_FIRST_LOCK()) {
  82. mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr);
  83. }
  84. }
  85. }
  86. void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...)
  87. {
  88. va_list va;
  89. size_t temp_s1, temp_s2;
  90. void *temp_ptr;
  91. va_start(va, caller);
  92. switch (op) {
  93. case MBED_MEM_TRACE_MALLOC:
  94. temp_s1 = va_arg(va, size_t);
  95. printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1);
  96. break;
  97. case MBED_MEM_TRACE_REALLOC:
  98. temp_ptr = va_arg(va, void *);
  99. temp_s1 = va_arg(va, size_t);
  100. printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1);
  101. break;
  102. case MBED_MEM_TRACE_CALLOC:
  103. temp_s1 = va_arg(va, size_t);
  104. temp_s2 = va_arg(va, size_t);
  105. printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2);
  106. break;
  107. case MBED_MEM_TRACE_FREE:
  108. temp_ptr = va_arg(va, void *);
  109. printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr);
  110. break;
  111. default:
  112. printf("?\n");
  113. }
  114. va_end(va);
  115. }