Skip to content

Commit 7d09bb7

Browse files
Bogdan Marinescuc1728p9
authored andcommitted
Runtime dynamic memory tracing
This commit adds a dynamic memory tracer that calls a callback whenever one of the basic memory allocation functions (malloc, realloc, calloc, free) is called. The operation of the tracer is guarded by the 'MBED_MEM_TRACING_ENABLED` macro. Infinite recursion during tracing is guarded by using atomic increment/decrement primitives on the `trace_level` variable. Thanks to @c1728p9 and @Heky for their help and suggestions.
1 parent f147f6f commit 7d09bb7

File tree

5 files changed

+588
-201
lines changed

5 files changed

+588
-201
lines changed

hal/api/mbed_mem_trace.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 <stdint.h>
25+
#include <stddef.h>
26+
27+
/* Operation types for tracer */
28+
enum {
29+
MBED_MEM_TRACE_MALLOC,
30+
MBED_MEM_TRACE_REALLOC,
31+
MBED_MEM_TRACE_CALLOC,
32+
MBED_MEM_TRACE_FREE
33+
};
34+
35+
/* Prefix for the output of the default tracer */
36+
#define MBED_MEM_DEFAULT_TRACER_PREFIX "#"
37+
38+
/**
39+
* Type of the callback used by the memory tracer. This callback is called when a memory
40+
* allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
41+
* for that memory allocation function.
42+
*
43+
* @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
44+
* MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
45+
* @param res the result that the memory operation returned (NULL for 'free').
46+
* @param caller the caller of the memory operation. Note that the value of 'caller' might be
47+
* unreliable.
48+
*
49+
* The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
50+
* that triggered its call (see 'man malloc' for details):
51+
*
52+
* - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
53+
* - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
54+
* - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
55+
* - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
56+
*/
57+
typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...);
58+
59+
/**
60+
* Set the callback used by the memory tracer (use NULL for disable tracing).
61+
*
62+
* @param cb the callback to call on each memory operation.
63+
*/
64+
void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
65+
66+
/**
67+
* Trace a call to 'malloc'.
68+
* @param res the result of running 'malloc'.
69+
* @param size the 'size' argument given to 'malloc'.
70+
* @param caller the caller of the memory operation.
71+
* @return 'res' (the first argument).
72+
*/
73+
void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
74+
75+
/**
76+
* Trace a call to 'realloc'.
77+
* @param res the result of running 'realloc'.
78+
* @param ptr the 'ptr' argument given to 'realloc'.
79+
* @param size the 'size' argument given to 'realloc'.
80+
*
81+
* @return 'res' (the first argument).
82+
*/
83+
void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
84+
85+
/**
86+
* Trace a call to 'calloc'.
87+
* @param res the result of running 'calloc'.
88+
* @param nmemb the 'nmemb' argument given to 'calloc'.
89+
* @param size the 'size' argument given to 'calloc'.
90+
* @param caller the caller of the memory operation.
91+
* @Return 'res' (the first argument).
92+
*/
93+
void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
94+
95+
/**
96+
* Trace a call to 'free'.
97+
* @param ptr the 'ptr' argument given to 'free'.
98+
* @param caller the caller of the memory operation.
99+
*/
100+
void mbed_mem_trace_free(void *ptr, void *caller);
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_trace_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+

hal/api/toolchain.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@
240240
*/
241241
#define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]")
242242

243+
/** MBED_CALLER_ADDR()
244+
* Returns the caller of the current function.
245+
*
246+
* @note
247+
* This macro is only implemented for GCC and ARMCC.
248+
*
249+
* @code
250+
* #include "toolchain.h"
251+
*
252+
* printf("This function was called from %p", MBED_CALLER_ADDR());
253+
* @endcode
254+
*
255+
* @return Address of the calling function
256+
*/
257+
#ifndef MBED_CALLER_ADDR
258+
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
259+
#define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0))
260+
#elif defined(__CC_ARM)
261+
#define MBED_CALLER_ADDR() __builtin_return_address(0)
262+
#else
263+
#define MBED_CALLER_ADDR() (NULL)
264+
#endif
265+
#endif
243266

244267
// FILEHANDLE declaration
245268
#if defined(TOOLCHAIN_ARM)

0 commit comments

Comments
 (0)