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