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