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