Rtos API example

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 /* Operation types for tracer */
00032 enum {
00033     MBED_MEM_TRACE_MALLOC,
00034     MBED_MEM_TRACE_REALLOC,
00035     MBED_MEM_TRACE_CALLOC,
00036     MBED_MEM_TRACE_FREE
00037 };
00038 
00039 /**
00040  * \defgroup platform_mem_trace mem_trace functions
00041  * @{
00042  */
00043 
00044 /* Prefix for the output of the default tracer */
00045 #define MBED_MEM_DEFAULT_TRACER_PREFIX  "#"
00046 
00047 /**
00048  * Type of the callback used by the memory tracer. This callback is called when a memory
00049  * allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
00050  * for that memory allocation function.
00051  *
00052  * @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
00053  *           MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
00054  * @param res the result that the memory operation returned (NULL for 'free').
00055  * @param caller the caller of the memory operation. Note that the value of 'caller' might be
00056  *               unreliable.
00057  *
00058  * The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
00059  * that triggered its call (see 'man malloc' for details):
00060  *
00061  * - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
00062  * - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
00063  * - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
00064  * - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
00065  */
00066 typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...);
00067 
00068 /**
00069  * Set the callback used by the memory tracer (use NULL for disable tracing).
00070  *
00071  * @param cb the callback to call on each memory operation.
00072  */
00073 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
00074 
00075 /**
00076  * Trace a call to 'malloc'.
00077  * @param res the result of running 'malloc'.
00078  * @param size the 'size' argument given to 'malloc'.
00079  * @param caller the caller of the memory operation.
00080  * @return 'res' (the first argument).
00081  */
00082 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
00083 
00084 /**
00085  * Trace a call to 'realloc'.
00086  * @param res the result of running 'realloc'.
00087  * @param ptr the 'ptr' argument given to 'realloc'.
00088  * @param size the 'size' argument given to 'realloc'.
00089  * @param caller the caller of the memory operation.
00090  * @return 'res' (the first argument).
00091  */
00092 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
00093 
00094 /**
00095  * Trace a call to 'calloc'.
00096  * @param res the result of running 'calloc'.
00097  * @param num the 'nmemb' argument given to 'calloc'.
00098  * @param size the 'size' argument given to 'calloc'.
00099  * @param caller the caller of the memory operation.
00100  * @return 'res' (the first argument).
00101  */
00102 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
00103 
00104 /**
00105  * Trace a call to 'free'.
00106  * @param ptr the 'ptr' argument given to 'free'.
00107  * @param caller the caller of the memory operation.
00108  */
00109 void mbed_mem_trace_free(void *ptr, void *caller);
00110 
00111 /**
00112  * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
00113  * as the second argument of 'mbed_mem_trace_setup'.
00114  *
00115  * The default callback outputs trace data using 'printf', in a format that's
00116  * easily parsable by an external tool. For each memory operation, the callback
00117  * outputs a line that begins with "#<op>:<0xresult>;<0xcaller>-":
00118  *
00119  * @param op        identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
00120  *                  'c' for 'calloc' and 'f' for 'free').
00121  * @param res       (base 16) is the result of the memor operation. This is always NULL
00122  *                  for 'free', since 'free' doesn't return anything.
00123  * @param caller    (base 16) is the caller of the memory operation. Note that the value
00124  *                  of 'caller' might be unreliable.
00125  *
00126  * The rest of the output depends on the operation being traced:
00127  *
00128  * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
00129  * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
00130  * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
00131  * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
00132  *
00133  * Examples:
00134  *
00135  * - "#m:0x20003240;0x600d-50" encodes a 'malloc' that returned 0x20003240, was called
00136  *   by the instruction at 0x600D with a the 'size' argument equal to 50.
00137  * - "#f:0x0;0x602f-0x20003240" encodes a 'free' that was called by the instruction at
00138  *   0x602f with the 'ptr' argument equal to 0x20003240.
00139  */
00140 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
00141 
00142 /** @}*/
00143 
00144 #ifdef __cplusplus
00145 }
00146 #endif
00147 
00148 #endif// #ifndef __MBED_MEM_TRACE_H__
00149 
00150 
00151 /** @}*/