test
platform/mbed_mem_trace.c@2:4364577b5ad8, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:33:19 2020 -0500
- Revision:
- 2:4364577b5ad8
- Parent:
- 1:8a094db1347f
copied mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | * Copyright (c) 2006-2016 ARM Limited |
elijahsj | 1:8a094db1347f | 3 | * |
elijahsj | 1:8a094db1347f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elijahsj | 1:8a094db1347f | 5 | * you may not use this file except in compliance with the License. |
elijahsj | 1:8a094db1347f | 6 | * You may obtain a copy of the License at |
elijahsj | 1:8a094db1347f | 7 | * |
elijahsj | 1:8a094db1347f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elijahsj | 1:8a094db1347f | 9 | * |
elijahsj | 1:8a094db1347f | 10 | * Unless required by applicable law or agreed to in writing, software |
elijahsj | 1:8a094db1347f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elijahsj | 1:8a094db1347f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elijahsj | 1:8a094db1347f | 13 | * See the License for the specific language governing permissions and |
elijahsj | 1:8a094db1347f | 14 | * limitations under the License. |
elijahsj | 1:8a094db1347f | 15 | */ |
elijahsj | 1:8a094db1347f | 16 | |
elijahsj | 1:8a094db1347f | 17 | #include <stdlib.h> |
elijahsj | 1:8a094db1347f | 18 | #include <stdarg.h> |
elijahsj | 1:8a094db1347f | 19 | #include <stdio.h> |
elijahsj | 1:8a094db1347f | 20 | #include "platform/mbed_mem_trace.h" |
elijahsj | 1:8a094db1347f | 21 | #include "platform/mbed_critical.h" |
elijahsj | 1:8a094db1347f | 22 | |
elijahsj | 1:8a094db1347f | 23 | /****************************************************************************** |
elijahsj | 1:8a094db1347f | 24 | * Internal variables, functions and helpers |
elijahsj | 1:8a094db1347f | 25 | *****************************************************************************/ |
elijahsj | 1:8a094db1347f | 26 | |
elijahsj | 1:8a094db1347f | 27 | /* The callback function that will be called after a traced memory operations finishes. */ |
elijahsj | 1:8a094db1347f | 28 | static mbed_mem_trace_cb_t mem_trace_cb; |
elijahsj | 1:8a094db1347f | 29 | /* 'trave_level' guards "trace inside trace" situations (for example, the implementation |
elijahsj | 1:8a094db1347f | 30 | * of realloc() might call malloc() internally, and since malloc() is also traced, this could |
elijahsj | 1:8a094db1347f | 31 | * result in two calls to the callback function instead of one. */ |
elijahsj | 1:8a094db1347f | 32 | static uint8_t trace_level; |
elijahsj | 1:8a094db1347f | 33 | |
elijahsj | 1:8a094db1347f | 34 | /****************************************************************************** |
elijahsj | 1:8a094db1347f | 35 | * Public interface |
elijahsj | 1:8a094db1347f | 36 | *****************************************************************************/ |
elijahsj | 1:8a094db1347f | 37 | |
elijahsj | 1:8a094db1347f | 38 | void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) { |
elijahsj | 1:8a094db1347f | 39 | mem_trace_cb = cb; |
elijahsj | 1:8a094db1347f | 40 | } |
elijahsj | 1:8a094db1347f | 41 | |
elijahsj | 1:8a094db1347f | 42 | void *mbed_mem_trace_malloc(void *res, size_t size, void *caller) { |
elijahsj | 1:8a094db1347f | 43 | if (mem_trace_cb) { |
elijahsj | 1:8a094db1347f | 44 | if (core_util_atomic_incr_u8(&trace_level, 1) == 1) { |
elijahsj | 1:8a094db1347f | 45 | mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size); |
elijahsj | 1:8a094db1347f | 46 | } |
elijahsj | 1:8a094db1347f | 47 | core_util_atomic_decr_u8(&trace_level, 1); |
elijahsj | 1:8a094db1347f | 48 | } |
elijahsj | 1:8a094db1347f | 49 | return res; |
elijahsj | 1:8a094db1347f | 50 | } |
elijahsj | 1:8a094db1347f | 51 | |
elijahsj | 1:8a094db1347f | 52 | void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller) { |
elijahsj | 1:8a094db1347f | 53 | if (mem_trace_cb) { |
elijahsj | 1:8a094db1347f | 54 | if (core_util_atomic_incr_u8(&trace_level, 1) == 1) { |
elijahsj | 1:8a094db1347f | 55 | mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size); |
elijahsj | 1:8a094db1347f | 56 | } |
elijahsj | 1:8a094db1347f | 57 | core_util_atomic_decr_u8(&trace_level, 1); |
elijahsj | 1:8a094db1347f | 58 | } |
elijahsj | 1:8a094db1347f | 59 | return res; |
elijahsj | 1:8a094db1347f | 60 | } |
elijahsj | 1:8a094db1347f | 61 | |
elijahsj | 1:8a094db1347f | 62 | void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller) { |
elijahsj | 1:8a094db1347f | 63 | if (mem_trace_cb) { |
elijahsj | 1:8a094db1347f | 64 | if (core_util_atomic_incr_u8(&trace_level, 1) == 1) { |
elijahsj | 1:8a094db1347f | 65 | mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size); |
elijahsj | 1:8a094db1347f | 66 | } |
elijahsj | 1:8a094db1347f | 67 | core_util_atomic_decr_u8(&trace_level, 1); |
elijahsj | 1:8a094db1347f | 68 | } |
elijahsj | 1:8a094db1347f | 69 | return res; |
elijahsj | 1:8a094db1347f | 70 | } |
elijahsj | 1:8a094db1347f | 71 | |
elijahsj | 1:8a094db1347f | 72 | void mbed_mem_trace_free(void *ptr, void *caller) { |
elijahsj | 1:8a094db1347f | 73 | if (mem_trace_cb) { |
elijahsj | 1:8a094db1347f | 74 | if (core_util_atomic_incr_u8(&trace_level, 1) == 1) { |
elijahsj | 1:8a094db1347f | 75 | mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr); |
elijahsj | 1:8a094db1347f | 76 | } |
elijahsj | 1:8a094db1347f | 77 | core_util_atomic_decr_u8(&trace_level, 1); |
elijahsj | 1:8a094db1347f | 78 | } |
elijahsj | 1:8a094db1347f | 79 | } |
elijahsj | 1:8a094db1347f | 80 | |
elijahsj | 1:8a094db1347f | 81 | void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...) { |
elijahsj | 1:8a094db1347f | 82 | va_list va; |
elijahsj | 1:8a094db1347f | 83 | size_t temp_s1, temp_s2; |
elijahsj | 1:8a094db1347f | 84 | void *temp_ptr; |
elijahsj | 1:8a094db1347f | 85 | |
elijahsj | 1:8a094db1347f | 86 | va_start(va, caller); |
elijahsj | 1:8a094db1347f | 87 | switch(op) { |
elijahsj | 1:8a094db1347f | 88 | case MBED_MEM_TRACE_MALLOC: |
elijahsj | 1:8a094db1347f | 89 | temp_s1 = va_arg(va, size_t); |
elijahsj | 1:8a094db1347f | 90 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1); |
elijahsj | 1:8a094db1347f | 91 | break; |
elijahsj | 1:8a094db1347f | 92 | |
elijahsj | 1:8a094db1347f | 93 | case MBED_MEM_TRACE_REALLOC: |
elijahsj | 1:8a094db1347f | 94 | temp_ptr = va_arg(va, void*); |
elijahsj | 1:8a094db1347f | 95 | temp_s1 = va_arg(va, size_t); |
elijahsj | 1:8a094db1347f | 96 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1); |
elijahsj | 1:8a094db1347f | 97 | break; |
elijahsj | 1:8a094db1347f | 98 | |
elijahsj | 1:8a094db1347f | 99 | case MBED_MEM_TRACE_CALLOC: |
elijahsj | 1:8a094db1347f | 100 | temp_s1 = va_arg(va, size_t); |
elijahsj | 1:8a094db1347f | 101 | temp_s2 = va_arg(va, size_t); |
elijahsj | 1:8a094db1347f | 102 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2); |
elijahsj | 1:8a094db1347f | 103 | break; |
elijahsj | 1:8a094db1347f | 104 | |
elijahsj | 1:8a094db1347f | 105 | case MBED_MEM_TRACE_FREE: |
elijahsj | 1:8a094db1347f | 106 | temp_ptr = va_arg(va, void*); |
elijahsj | 1:8a094db1347f | 107 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr); |
elijahsj | 1:8a094db1347f | 108 | break; |
elijahsj | 1:8a094db1347f | 109 | |
elijahsj | 1:8a094db1347f | 110 | default: |
elijahsj | 1:8a094db1347f | 111 | printf("?\n"); |
elijahsj | 1:8a094db1347f | 112 | } |
elijahsj | 1:8a094db1347f | 113 | va_end(va); |
elijahsj | 1:8a094db1347f | 114 | } |
elijahsj | 1:8a094db1347f | 115 |