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