mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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