Kev Mann / mbed-dev-OS5_10_4
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_mem_trace.h Source File

mbed_mem_trace.h

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