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