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