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