Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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