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