Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1
switches 0:0e018d759a2a 2 /** \addtogroup platform */
switches 0:0e018d759a2a 3 /** @{*/
switches 0:0e018d759a2a 4 /* mbed Microcontroller Library
switches 0:0e018d759a2a 5 * Copyright (c) 2006-2016 ARM Limited
switches 0:0e018d759a2a 6 *
switches 0:0e018d759a2a 7 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 8 * you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 9 * You may obtain a copy of the License at
switches 0:0e018d759a2a 10 *
switches 0:0e018d759a2a 11 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 12 *
switches 0:0e018d759a2a 13 * Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 14 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 16 * See the License for the specific language governing permissions and
switches 0:0e018d759a2a 17 * limitations under the License.
switches 0:0e018d759a2a 18 */
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20 #ifndef __MBED_MEM_TRACE_H__
switches 0:0e018d759a2a 21 #define __MBED_MEM_TRACE_H__
switches 0:0e018d759a2a 22
switches 0:0e018d759a2a 23 #ifdef __cplusplus
switches 0:0e018d759a2a 24 extern "C" {
switches 0:0e018d759a2a 25 #endif
switches 0:0e018d759a2a 26
switches 0:0e018d759a2a 27 #include <stdint.h>
switches 0:0e018d759a2a 28 #include <stddef.h>
switches 0:0e018d759a2a 29
switches 0:0e018d759a2a 30 /* Operation types for tracer */
switches 0:0e018d759a2a 31 enum {
switches 0:0e018d759a2a 32 MBED_MEM_TRACE_MALLOC,
switches 0:0e018d759a2a 33 MBED_MEM_TRACE_REALLOC,
switches 0:0e018d759a2a 34 MBED_MEM_TRACE_CALLOC,
switches 0:0e018d759a2a 35 MBED_MEM_TRACE_FREE
switches 0:0e018d759a2a 36 };
switches 0:0e018d759a2a 37
switches 0:0e018d759a2a 38 /* Prefix for the output of the default tracer */
switches 0:0e018d759a2a 39 #define MBED_MEM_DEFAULT_TRACER_PREFIX "#"
switches 0:0e018d759a2a 40
switches 0:0e018d759a2a 41 /**
switches 0:0e018d759a2a 42 * Type of the callback used by the memory tracer. This callback is called when a memory
switches 0:0e018d759a2a 43 * allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
switches 0:0e018d759a2a 44 * for that memory allocation function.
switches 0:0e018d759a2a 45 *
switches 0:0e018d759a2a 46 * @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
switches 0:0e018d759a2a 47 * MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
switches 0:0e018d759a2a 48 * @param res the result that the memory operation returned (NULL for 'free').
switches 0:0e018d759a2a 49 * @param caller the caller of the memory operation. Note that the value of 'caller' might be
switches 0:0e018d759a2a 50 * unreliable.
switches 0:0e018d759a2a 51 *
switches 0:0e018d759a2a 52 * The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
switches 0:0e018d759a2a 53 * that triggered its call (see 'man malloc' for details):
switches 0:0e018d759a2a 54 *
switches 0:0e018d759a2a 55 * - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
switches 0:0e018d759a2a 56 * - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
switches 0:0e018d759a2a 57 * - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
switches 0:0e018d759a2a 58 * - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
switches 0:0e018d759a2a 59 */
switches 0:0e018d759a2a 60 typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...);
switches 0:0e018d759a2a 61
switches 0:0e018d759a2a 62 /**
switches 0:0e018d759a2a 63 * Set the callback used by the memory tracer (use NULL for disable tracing).
switches 0:0e018d759a2a 64 *
switches 0:0e018d759a2a 65 * @param cb the callback to call on each memory operation.
switches 0:0e018d759a2a 66 */
switches 0:0e018d759a2a 67 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
switches 0:0e018d759a2a 68
switches 0:0e018d759a2a 69 /**
switches 0:0e018d759a2a 70 * Trace a call to 'malloc'.
switches 0:0e018d759a2a 71 * @param res the result of running 'malloc'.
switches 0:0e018d759a2a 72 * @param size the 'size' argument given to 'malloc'.
switches 0:0e018d759a2a 73 * @param caller the caller of the memory operation.
switches 0:0e018d759a2a 74 * @return 'res' (the first argument).
switches 0:0e018d759a2a 75 */
switches 0:0e018d759a2a 76 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
switches 0:0e018d759a2a 77
switches 0:0e018d759a2a 78 /**
switches 0:0e018d759a2a 79 * Trace a call to 'realloc'.
switches 0:0e018d759a2a 80 * @param res the result of running 'realloc'.
switches 0:0e018d759a2a 81 * @param ptr the 'ptr' argument given to 'realloc'.
switches 0:0e018d759a2a 82 * @param size the 'size' argument given to 'realloc'.
switches 0:0e018d759a2a 83 *
switches 0:0e018d759a2a 84 * @return 'res' (the first argument).
switches 0:0e018d759a2a 85 */
switches 0:0e018d759a2a 86 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
switches 0:0e018d759a2a 87
switches 0:0e018d759a2a 88 /**
switches 0:0e018d759a2a 89 * Trace a call to 'calloc'.
switches 0:0e018d759a2a 90 * @param res the result of running 'calloc'.
switches 0:0e018d759a2a 91 * @param nmemb the 'nmemb' argument given to 'calloc'.
switches 0:0e018d759a2a 92 * @param size the 'size' argument given to 'calloc'.
switches 0:0e018d759a2a 93 * @param caller the caller of the memory operation.
switches 0:0e018d759a2a 94 * @Return 'res' (the first argument).
switches 0:0e018d759a2a 95 */
switches 0:0e018d759a2a 96 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
switches 0:0e018d759a2a 97
switches 0:0e018d759a2a 98 /**
switches 0:0e018d759a2a 99 * Trace a call to 'free'.
switches 0:0e018d759a2a 100 * @param ptr the 'ptr' argument given to 'free'.
switches 0:0e018d759a2a 101 * @param caller the caller of the memory operation.
switches 0:0e018d759a2a 102 */
switches 0:0e018d759a2a 103 void mbed_mem_trace_free(void *ptr, void *caller);
switches 0:0e018d759a2a 104
switches 0:0e018d759a2a 105 /**
switches 0:0e018d759a2a 106 * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
switches 0:0e018d759a2a 107 * as the second argument of 'mbed_mem_trace_setup'.
switches 0:0e018d759a2a 108 *
switches 0:0e018d759a2a 109 * The default callback outputs trace data using 'printf', in a format that's
switches 0:0e018d759a2a 110 * easily parsable by an external tool. For each memory operation, the callback
switches 0:0e018d759a2a 111 * outputs a line that begins with '#<op>:<0xresult>;<0xcaller>-':
switches 0:0e018d759a2a 112 *
switches 0:0e018d759a2a 113 * - 'op' identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
switches 0:0e018d759a2a 114 * 'c' for 'calloc' and 'f' for 'free').
switches 0:0e018d759a2a 115 * - 'result' (base 16) is the result of the memor operation. This is always NULL
switches 0:0e018d759a2a 116 * for 'free', since 'free' doesn't return anything.
switches 0:0e018d759a2a 117 * -'caller' (base 16) is the caller of the memory operation. Note that the value
switches 0:0e018d759a2a 118 * of 'caller' might be unreliable.
switches 0:0e018d759a2a 119 *
switches 0:0e018d759a2a 120 * The rest of the output depends on the operation being traced:
switches 0:0e018d759a2a 121 *
switches 0:0e018d759a2a 122 * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
switches 0:0e018d759a2a 123 * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
switches 0:0e018d759a2a 124 * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
switches 0:0e018d759a2a 125 * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
switches 0:0e018d759a2a 126 *
switches 0:0e018d759a2a 127 * Examples:
switches 0:0e018d759a2a 128 *
switches 0:0e018d759a2a 129 * - '#m:0x20003240;0x600d-50' encodes a 'malloc' that returned 0x20003240, was called
switches 0:0e018d759a2a 130 * by the instruction at 0x600D with a the 'size' argument equal to 50.
switches 0:0e018d759a2a 131 * - '#f:0x0;0x602f-0x20003240' encodes a 'free' that was called by the instruction at
switches 0:0e018d759a2a 132 * 0x602f with the 'ptr' argument equal to 0x20003240.
switches 0:0e018d759a2a 133 */
switches 0:0e018d759a2a 134 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
switches 0:0e018d759a2a 135
switches 0:0e018d759a2a 136 #ifdef __cplusplus
switches 0:0e018d759a2a 137 }
switches 0:0e018d759a2a 138 #endif
switches 0:0e018d759a2a 139
switches 0:0e018d759a2a 140 #endif// #ifndef __MBED_MEM_TRACE_H__
switches 0:0e018d759a2a 141
switches 0:0e018d759a2a 142
switches 0:0e018d759a2a 143 /** @}*/