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