Git init
[framework/appfw/aul-1.git] / launchpad_src / heap_dbg.h
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <malloc.h>
24 #include <stdio.h>
25 #include <signal.h>
26
27 #ifdef HEAPDGB_ACTIVATE
28
29 #define HOOK_RESET()    \
30 do {\
31         __malloc_hook = old_malloc_hook; \
32         __free_hook = old_free_hook;    \
33 } while (0);
34
35 #define HOOK_SET()      \
36 do {\
37         __malloc_hook = my_malloc;      \
38         __free_hook = my_free;          \
39 } while (0);
40
41 static void *(*old_malloc_hook) (size_t size, const void *caller);
42 static void *(*old_realloc_hook) (void *ptr, size_t size, const void *caller);
43 static void (*old_free_hook) (void *ptr, const void *caller);
44
45 static void *my_malloc(size_t size, const void *caller);
46 static void *my_realloc(void *ptr, size_t size, const void *caller);
47 static void my_free(void *ptr, const void *caller);
48
49 static void my_free(void *ptr, const void *caller)
50 {
51         void *callstack_addrs[20];
52         char **callstack_strings;
53         int i;
54
55         HOOK_RESET();
56
57         printf("%c[1;31m[FREE] %x %x", 27, ptr, caller);
58         printf("%c[0m\n", 27);
59         free(ptr);
60
61         HOOK_SET();
62
63 }
64
65 static void *my_malloc(size_t size, const void *caller)
66 {
67         void *ptr;
68
69         HOOK_RESET();
70
71         ptr = malloc(size);
72         printf("%c[1;31m[MALLOC] %x %x", 27, ptr, caller);
73         printf("%c[0m\n", 27);
74
75         HOOK_SET();
76
77         return ptr;
78 }
79
80 static void malloc_init(void)
81 {
82         old_malloc_hook = __malloc_hook;
83         old_free_hook = __free_hook;
84
85         HOOK_SET();
86 }
87
88 __attribute__ ((visibility("default")))
89 void (*__malloc_initialize_hook) (void) = malloc_init;
90
91 #else
92
93 #endif