Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_mem_trace.h Source File

mbed_mem_trace.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef __MBED_MEM_TRACE_H__
00019 #define __MBED_MEM_TRACE_H__
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 #include <stdint.h>
00026 #include <stddef.h>
00027 
00028 /** \addtogroup platform-public-api */
00029 /** @{*/
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  * Disable the memory trace output by disabling the callback function
00079  */
00080 void mbed_mem_trace_disable();
00081 
00082 /**
00083  * Re-enable the memory trace output with the cb in use when disable was called
00084  */
00085 void mbed_mem_trace_enable();
00086 
00087 /**
00088  * Trace lock.
00089  * @note Locking prevent recursive tracing of malloc/free inside realloc/calloc
00090  */
00091 void mbed_mem_trace_lock();
00092 
00093 /**
00094  * Trace unlock.
00095  */
00096 void mbed_mem_trace_unlock();
00097 
00098 /**
00099  * Trace a call to 'malloc'.
00100  * @param res the result of running 'malloc'.
00101  * @param size the 'size' argument given to 'malloc'.
00102  * @param caller the caller of the memory operation.
00103  * @return 'res' (the first argument).
00104  */
00105 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
00106 
00107 /**
00108  * Trace a call to 'realloc'.
00109  * @param res the result of running 'realloc'.
00110  * @param ptr the 'ptr' argument given to 'realloc'.
00111  * @param size the 'size' argument given to 'realloc'.
00112  * @param caller the caller of the memory operation.
00113  * @return 'res' (the first argument).
00114  */
00115 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
00116 
00117 /**
00118  * Trace a call to 'calloc'.
00119  * @param res the result of running 'calloc'.
00120  * @param num the 'nmemb' argument given to 'calloc'.
00121  * @param size the 'size' argument given to 'calloc'.
00122  * @param caller the caller of the memory operation.
00123  * @return 'res' (the first argument).
00124  */
00125 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
00126 
00127 /**
00128  * Trace a call to 'free'.
00129  * @param ptr the 'ptr' argument given to 'free'.
00130  * @param caller the caller of the memory operation.
00131  */
00132 void mbed_mem_trace_free(void *ptr, void *caller);
00133 
00134 /**
00135  * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
00136  * as the second argument of 'mbed_mem_trace_setup'.
00137  *
00138  * The default callback outputs trace data using 'printf', in a format that's
00139  * easily parsable by an external tool. For each memory operation, the callback
00140  * outputs a line that begins with "#<op>:<0xresult>;<0xcaller>-":
00141  *
00142  * @param op        identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
00143  *                  'c' for 'calloc' and 'f' for 'free').
00144  * @param res       (base 16) is the result of the memory operation. This is always NULL
00145  *                  for 'free', since 'free' doesn't return anything.
00146  * @param caller    (base 16) is the caller of the memory operation. Note that the value
00147  *                  of 'caller' might be unreliable.
00148  *
00149  * The rest of the output depends on the operation being traced:
00150  *
00151  * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
00152  * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
00153  * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
00154  * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
00155  *
00156  * Examples:
00157  *
00158  * - "#m:0x20003240;0x600d-50" encodes a 'malloc' that returned 0x20003240, was called
00159  *   by the instruction at 0x600D with a the 'size' argument equal to 50.
00160  * - "#f:0x0;0x602f-0x20003240" encodes a 'free' that was called by the instruction at
00161  *   0x602f with the 'ptr' argument equal to 0x20003240.
00162  */
00163 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
00164 
00165 /** @}*/
00166 
00167 #ifdef __cplusplus
00168 }
00169 #endif
00170 
00171 #endif// #ifndef __MBED_MEM_TRACE_H__
00172 
00173 
00174 /** @}*/