mbed source development branch
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
mbed_mem_trace.h
00001 00002 /** \addtogroup platform */ 00003 /** @{*/ 00004 /* mbed Microcontroller Library 00005 * Copyright (c) 2006-2016 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 #ifndef __MBED_MEM_TRACE_H__ 00021 #define __MBED_MEM_TRACE_H__ 00022 00023 #ifdef __cplusplus 00024 extern "C" { 00025 #endif 00026 00027 #include <stdint.h> 00028 #include <stddef.h> 00029 00030 /* Operation types for tracer */ 00031 enum { 00032 MBED_MEM_TRACE_MALLOC, 00033 MBED_MEM_TRACE_REALLOC, 00034 MBED_MEM_TRACE_CALLOC, 00035 MBED_MEM_TRACE_FREE 00036 }; 00037 00038 /* Prefix for the output of the default tracer */ 00039 #define MBED_MEM_DEFAULT_TRACER_PREFIX "#" 00040 00041 /** 00042 * Type of the callback used by the memory tracer. This callback is called when a memory 00043 * allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled 00044 * for that memory allocation function. 00045 * 00046 * @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC, 00047 * MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE). 00048 * @param res the result that the memory operation returned (NULL for 'free'). 00049 * @param caller the caller of the memory operation. Note that the value of 'caller' might be 00050 * unreliable. 00051 * 00052 * The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations 00053 * that triggered its call (see 'man malloc' for details): 00054 * 00055 * - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size). 00056 * - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size). 00057 * - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size). 00058 * - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr). 00059 */ 00060 typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...); 00061 00062 /** 00063 * Set the callback used by the memory tracer (use NULL for disable tracing). 00064 * 00065 * @param cb the callback to call on each memory operation. 00066 */ 00067 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb); 00068 00069 /** 00070 * Trace a call to 'malloc'. 00071 * @param res the result of running 'malloc'. 00072 * @param size the 'size' argument given to 'malloc'. 00073 * @param caller the caller of the memory operation. 00074 * @return 'res' (the first argument). 00075 */ 00076 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller); 00077 00078 /** 00079 * Trace a call to 'realloc'. 00080 * @param res the result of running 'realloc'. 00081 * @param ptr the 'ptr' argument given to 'realloc'. 00082 * @param size the 'size' argument given to 'realloc'. 00083 * 00084 * @return 'res' (the first argument). 00085 */ 00086 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller); 00087 00088 /** 00089 * Trace a call to 'calloc'. 00090 * @param res the result of running 'calloc'. 00091 * @param nmemb the 'nmemb' argument given to 'calloc'. 00092 * @param size the 'size' argument given to 'calloc'. 00093 * @param caller the caller of the memory operation. 00094 * @Return 'res' (the first argument). 00095 */ 00096 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller); 00097 00098 /** 00099 * Trace a call to 'free'. 00100 * @param ptr the 'ptr' argument given to 'free'. 00101 * @param caller the caller of the memory operation. 00102 */ 00103 void mbed_mem_trace_free(void *ptr, void *caller); 00104 00105 /** 00106 * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used 00107 * as the second argument of 'mbed_mem_trace_setup'. 00108 * 00109 * The default callback outputs trace data using 'printf', in a format that's 00110 * easily parsable by an external tool. For each memory operation, the callback 00111 * outputs a line that begins with '#<op>:<0xresult>;<0xcaller>-': 00112 * 00113 * - 'op' identifies the memory operation ('m' for 'malloc', 'r' for 'realloc', 00114 * 'c' for 'calloc' and 'f' for 'free'). 00115 * - 'result' (base 16) is the result of the memor operation. This is always NULL 00116 * for 'free', since 'free' doesn't return anything. 00117 * -'caller' (base 16) is the caller of the memory operation. Note that the value 00118 * of 'caller' might be unreliable. 00119 * 00120 * The rest of the output depends on the operation being traced: 00121 * 00122 * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'. 00123 * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'. 00124 * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'. 00125 * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'. 00126 * 00127 * Examples: 00128 * 00129 * - '#m:0x20003240;0x600d-50' encodes a 'malloc' that returned 0x20003240, was called 00130 * by the instruction at 0x600D with a the 'size' argument equal to 50. 00131 * - '#f:0x0;0x602f-0x20003240' encodes a 'free' that was called by the instruction at 00132 * 0x602f with the 'ptr' argument equal to 0x20003240. 00133 */ 00134 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...); 00135 00136 #ifdef __cplusplus 00137 } 00138 #endif 00139 00140 #endif// #ifndef __MBED_MEM_TRACE_H__ 00141 00142 00143 /** @}*/
Generated on Tue Jul 12 2022 19:34:53 by 1.7.2