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