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