mbed_mem_trace.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /** \addtogroup platform */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2006-2016 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef __MBED_MEM_TRACE_H__
  19. #define __MBED_MEM_TRACE_H__
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include <stdint.h>
  24. #include <stddef.h>
  25. /* Operation types for tracer */
  26. enum {
  27. MBED_MEM_TRACE_MALLOC,
  28. MBED_MEM_TRACE_REALLOC,
  29. MBED_MEM_TRACE_CALLOC,
  30. MBED_MEM_TRACE_FREE
  31. };
  32. /**
  33. * \defgroup platform_mem_trace mem_trace functions
  34. * @{
  35. */
  36. /* Prefix for the output of the default tracer */
  37. #define MBED_MEM_DEFAULT_TRACER_PREFIX "#"
  38. /**
  39. * Type of the callback used by the memory tracer. This callback is called when a memory
  40. * allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
  41. * for that memory allocation function.
  42. *
  43. * @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
  44. * MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
  45. * @param res the result that the memory operation returned (NULL for 'free').
  46. * @param caller the caller of the memory operation. Note that the value of 'caller' might be
  47. * unreliable.
  48. *
  49. * The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
  50. * that triggered its call (see 'man malloc' for details):
  51. *
  52. * - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
  53. * - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
  54. * - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
  55. * - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
  56. */
  57. typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void *caller, ...);
  58. /**
  59. * Set the callback used by the memory tracer (use NULL for disable tracing).
  60. *
  61. * @param cb the callback to call on each memory operation.
  62. */
  63. void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
  64. /**
  65. * Trace lock.
  66. * @note Locking prevent recursive tracing of malloc/free inside relloc/calloc
  67. */
  68. void mbed_mem_trace_lock();
  69. /**
  70. * Trace unlock.
  71. */
  72. void mbed_mem_trace_unlock();
  73. /**
  74. * Trace a call to 'malloc'.
  75. * @param res the result of running 'malloc'.
  76. * @param size the 'size' argument given to 'malloc'.
  77. * @param caller the caller of the memory operation.
  78. * @return 'res' (the first argument).
  79. */
  80. void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
  81. /**
  82. * Trace a call to 'realloc'.
  83. * @param res the result of running 'realloc'.
  84. * @param ptr the 'ptr' argument given to 'realloc'.
  85. * @param size the 'size' argument given to 'realloc'.
  86. * @param caller the caller of the memory operation.
  87. * @return 'res' (the first argument).
  88. */
  89. void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
  90. /**
  91. * Trace a call to 'calloc'.
  92. * @param res the result of running 'calloc'.
  93. * @param num the 'nmemb' argument given to 'calloc'.
  94. * @param size the 'size' argument given to 'calloc'.
  95. * @param caller the caller of the memory operation.
  96. * @return 'res' (the first argument).
  97. */
  98. void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
  99. /**
  100. * Trace a call to 'free'.
  101. * @param ptr the 'ptr' argument given to 'free'.
  102. * @param caller the caller of the memory operation.
  103. */
  104. void mbed_mem_trace_free(void *ptr, void *caller);
  105. /**
  106. * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
  107. * as the second argument of 'mbed_mem_trace_setup'.
  108. *
  109. * The default callback outputs trace data using 'printf', in a format that's
  110. * easily parsable by an external tool. For each memory operation, the callback
  111. * outputs a line that begins with "#<op>:<0xresult>;<0xcaller>-":
  112. *
  113. * @param op identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
  114. * 'c' for 'calloc' and 'f' for 'free').
  115. * @param res (base 16) is the result of the memor operation. This is always NULL
  116. * for 'free', since 'free' doesn't return anything.
  117. * @param caller (base 16) is the caller of the memory operation. Note that the value
  118. * of 'caller' might be unreliable.
  119. *
  120. * The rest of the output depends on the operation being traced:
  121. *
  122. * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
  123. * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
  124. * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
  125. * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
  126. *
  127. * Examples:
  128. *
  129. * - "#m:0x20003240;0x600d-50" encodes a 'malloc' that returned 0x20003240, was called
  130. * by the instruction at 0x600D with a the 'size' argument equal to 50.
  131. * - "#f:0x0;0x602f-0x20003240" encodes a 'free' that was called by the instruction at
  132. * 0x602f with the 'ptr' argument equal to 0x20003240.
  133. */
  134. void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
  135. /** @}*/
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #endif// #ifndef __MBED_MEM_TRACE_H__
  140. /** @}*/