]> git.cworth.org Git - apitrace/blob - wrappers/trace.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / wrappers / trace.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010-2011 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #ifdef ANDROID
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <sys/system_properties.h>
37 #endif
38
39 #include "os.hpp"
40 #include "os_string.hpp"
41 #include "trace.hpp"
42
43
44 namespace trace {
45
46
47 #ifdef ANDROID
48
49 static bool
50 isZygoteProcess(void)
51 {
52     os::String proc_name;
53
54     proc_name = os::getProcessName();
55     proc_name.trimDirectory();
56
57     return strcmp(proc_name, "app_process") == 0;
58 }
59
60 static os::String
61 getZygoteProcessName(void)
62 {
63     os::String path;
64     size_t size = PATH_MAX;
65     char *buf = path.buf(size);
66     ssize_t len;
67
68     int fd = open("/proc/self/cmdline", O_RDONLY);
69
70     assert(fd >= 0);
71     len = read(fd, buf, size - 1);
72     close(fd);
73     path.truncate(len);
74
75     return path;
76 }
77
78 bool
79 isTracingEnabled(void)
80 {
81     static pid_t cached_pid;
82     static bool enabled;
83     pid_t pid;
84
85     pid = getpid();
86     if (cached_pid == pid)
87         return enabled;
88     cached_pid = pid;
89
90     if (!isZygoteProcess()) {
91         os::log("apitrace[%d]: enabled for standalone %s",
92                 pid, (const char *)os::getProcessName());
93         enabled = true;
94         return true;
95     }
96
97     char target_proc_name[PROP_VALUE_MAX] = "";
98     os::String proc_name;
99
100     proc_name = getZygoteProcessName();
101     proc_name.trimDirectory();
102
103     __system_property_get("debug.apitrace.procname", target_proc_name);
104     enabled = !strcmp(target_proc_name, proc_name);
105     os::log("apitrace[%d]: %s for %s",
106             pid, enabled ? "enabled" : "disabled", (const char *)proc_name);
107
108     return enabled;
109 }
110
111 #endif /* ANDROID */
112
113
114 } /* namespace trace */
115