takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_mem_trace.cpp Source File

mbed_mem_trace.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <stdlib.h>
00018 #include <stdarg.h>
00019 #include <stdio.h>
00020 #include "platform/mbed_mem_trace.h"
00021 #include "platform/mbed_critical.h"
00022 #include "platform/SingletonPtr.h"
00023 #include "platform/PlatformMutex.h"
00024 
00025 /******************************************************************************
00026  * Internal variables, functions and helpers
00027  *****************************************************************************/
00028 
00029 /* The callback function that will be called after a traced memory operations finishes. */
00030 static mbed_mem_trace_cb_t mem_trace_cb;
00031 /* 'trace_lock_count' guards "trace inside trace" situations (for example, the implementation
00032  * of realloc() might call malloc() internally, and since malloc() is also traced, this could
00033  * result in two calls to the callback function instead of one. */
00034 static uint8_t trace_lock_count;
00035 static SingletonPtr<PlatformMutex>  mem_trace_mutex;
00036 
00037 #define TRACE_FIRST_LOCK() (trace_lock_count < 2)
00038 
00039 
00040 /******************************************************************************
00041  * Public interface
00042  *****************************************************************************/
00043 
00044 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb)
00045 {
00046     mem_trace_cb = cb;
00047 }
00048 
00049 void mbed_mem_trace_lock()
00050 {
00051     mem_trace_mutex->lock();
00052     trace_lock_count++;
00053 }
00054 
00055 void mbed_mem_trace_unlock()
00056 {
00057     trace_lock_count--;
00058     mem_trace_mutex->unlock();
00059 }
00060 
00061 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller)
00062 {
00063     if (mem_trace_cb) {
00064         if (TRACE_FIRST_LOCK()) {
00065             mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size);
00066         }
00067     }
00068     return res;
00069 }
00070 
00071 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller)
00072 {
00073     if (mem_trace_cb) {
00074         if (TRACE_FIRST_LOCK()) {
00075             mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size);
00076         }
00077     }
00078     return res;
00079 }
00080 
00081 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller)
00082 {
00083     if (mem_trace_cb) {
00084         if (TRACE_FIRST_LOCK()) {
00085             mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size);
00086         }
00087     }
00088     return res;
00089 }
00090 
00091 void mbed_mem_trace_free(void *ptr, void *caller)
00092 {
00093     if (mem_trace_cb) {
00094         if (TRACE_FIRST_LOCK()) {
00095             mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr);
00096         }
00097     }
00098 }
00099 
00100 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...)
00101 {
00102     va_list va;
00103     size_t temp_s1, temp_s2;
00104     void *temp_ptr;
00105 
00106     va_start(va, caller);
00107     switch (op) {
00108         case MBED_MEM_TRACE_MALLOC:
00109             temp_s1 = va_arg(va, size_t);
00110             printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1);
00111             break;
00112 
00113         case MBED_MEM_TRACE_REALLOC:
00114             temp_ptr = va_arg(va, void *);
00115             temp_s1 = va_arg(va, size_t);
00116             printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1);
00117             break;
00118 
00119         case MBED_MEM_TRACE_CALLOC:
00120             temp_s1 = va_arg(va, size_t);
00121             temp_s2 = va_arg(va, size_t);
00122             printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2);
00123             break;
00124 
00125         case MBED_MEM_TRACE_FREE:
00126             temp_ptr = va_arg(va, void *);
00127             printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr);
00128             break;
00129 
00130         default:
00131             printf("?\n");
00132     }
00133     va_end(va);
00134 }
00135