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