Sync with the latest one
[platform/framework/web/heap-monitor.git] / detector / detector.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24
25 #include <heap-monitor.h>
26
27 int errno;
28 static char execpath[4096];
29
30 int destructor(void) __attribute__((destructor));
31 int constructor(void) __attribute__((constructor));
32
33 void (*__malloc_initialize_hook)(void) = heap_monitor_init;
34
35 int constructor(void)
36 {
37         int fd;
38
39         snprintf(execpath, sizeof(execpath), "/proc/%d/cmdline", getpid());
40
41         fd = open(execpath, O_RDONLY);
42         if (fd >= 0) {
43                 int len;
44                 len = read(fd, execpath, sizeof(execpath));
45                 close(fd);
46
47                 printf("execpath: [%s]\n", execpath);
48
49                 heap_monitor_add_target("");
50                 heap_monitor_start();
51         } else {
52                 printf("open: %s\n", strerror(errno));
53         }
54
55         return 0;
56 }
57
58 int destructor(void)
59 {
60         if (strlen(execpath)) {
61                 int usage;
62
63                 heap_monitor_stop();
64                 usage = heap_monitor_target_usage("");
65                 heap_monitor_del_target("");
66
67                 printf("execpath: [%s]\n", execpath);
68                 printf("Leak: %d\n", usage);
69         }
70         return 0;
71 }
72
73 /* End of a file */