OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "MemoryInstrumentation.h" | |
33 | |
34 #include <wtf/MemoryObjectInfo.h> | |
35 #include <string.h> | |
36 | |
37 #if DEBUG_POINTER_INSTRUMENTATION | |
38 #include <stdio.h> | |
39 #include <wtf/Assertions.h> | |
40 #endif | |
41 | |
42 namespace WTF { | |
43 | |
44 MemoryInstrumentation::MemoryInstrumentation(MemoryInstrumentationClient* client
) | |
45 : m_client(client) | |
46 { | |
47 } | |
48 | |
49 MemoryInstrumentation::~MemoryInstrumentation() | |
50 { | |
51 } | |
52 | |
53 void MemoryInstrumentation::reportEdge(const void* target, const char* name, Mem
berType memberType) | |
54 { | |
55 m_client->reportEdge(target, name, memberType); | |
56 } | |
57 | |
58 MemoryObjectType MemoryInstrumentation::getObjectType(MemoryObjectInfo* objectIn
fo) | |
59 { | |
60 return objectInfo->objectType(); | |
61 } | |
62 | |
63 void MemoryInstrumentation::reportLinkToBuffer(const void* buffer, MemoryObjectT
ype ownerObjectType, size_t size, const char* className, const char* edgeName) | |
64 { | |
65 MemoryObjectInfo memoryObjectInfo(this, ownerObjectType, 0); | |
66 memoryObjectInfo.reportObjectInfo(buffer, ownerObjectType, size); | |
67 memoryObjectInfo.setClassName(className); | |
68 m_client->reportLeaf(memoryObjectInfo, edgeName); | |
69 } | |
70 | |
71 MemoryInstrumentation::WrapperBase::WrapperBase(MemoryObjectType objectType, con
st void* pointer) | |
72 : m_pointer(pointer) | |
73 , m_ownerObjectType(objectType) | |
74 { | |
75 #if DEBUG_POINTER_INSTRUMENTATION | |
76 m_callStackSize = s_maxCallStackSize; | |
77 WTFGetBacktrace(m_callStack, &m_callStackSize); | |
78 #endif | |
79 } | |
80 | |
81 void MemoryInstrumentation::WrapperBase::process(MemoryInstrumentation* memoryIn
strumentation) | |
82 { | |
83 processPointer(memoryInstrumentation, false); | |
84 } | |
85 | |
86 void MemoryInstrumentation::WrapperBase::processPointer(MemoryInstrumentation* m
emoryInstrumentation, bool isRoot) | |
87 { | |
88 MemoryObjectInfo memoryObjectInfo(memoryInstrumentation, m_ownerObjectType,
m_pointer); | |
89 if (isRoot) | |
90 memoryObjectInfo.markAsRoot(); | |
91 callReportMemoryUsage(&memoryObjectInfo); | |
92 | |
93 const void* realAddress = memoryObjectInfo.reportedPointer(); | |
94 ASSERT(realAddress); | |
95 | |
96 if (memoryObjectInfo.firstVisit()) { | |
97 memoryInstrumentation->countObjectSize(realAddress, memoryObjectInfo.obj
ectType(), memoryObjectInfo.objectSize()); | |
98 memoryInstrumentation->m_client->reportNode(memoryObjectInfo); | |
99 } | |
100 | |
101 if (realAddress != m_pointer) | |
102 memoryInstrumentation->m_client->reportBaseAddress(m_pointer, realAddres
s); | |
103 | |
104 if (memoryObjectInfo.firstVisit() | |
105 && !memoryObjectInfo.customAllocation() | |
106 && !memoryInstrumentation->checkCountedObject(realAddress)) { | |
107 #if DEBUG_POINTER_INSTRUMENTATION | |
108 fputs("Unknown object counted:\n", stderr); | |
109 WTFPrintBacktrace(m_callStack, m_callStackSize); | |
110 #endif | |
111 } | |
112 } | |
113 | |
114 void MemoryInstrumentation::WrapperBase::processRootObjectRef(MemoryInstrumentat
ion* memoryInstrumentation) | |
115 { | |
116 MemoryObjectInfo memoryObjectInfo(memoryInstrumentation, m_ownerObjectType,
m_pointer); | |
117 memoryObjectInfo.markAsRoot(); | |
118 callReportMemoryUsage(&memoryObjectInfo); | |
119 | |
120 ASSERT(m_pointer == memoryObjectInfo.reportedPointer()); | |
121 memoryInstrumentation->m_client->reportNode(memoryObjectInfo); | |
122 } | |
123 | |
124 #if COMPILER(MSVC) | |
125 static const char* className(const char* functionName, char* buffer, const int m
axLength) | |
126 { | |
127 // MSVC generates names like this: 'WTF::FN<class WebCore::SharedBuffer>::fn
' | |
128 static const char prefix[] = "WTF::FN<"; | |
129 ASSERT(!strncmp(functionName, prefix, sizeof(prefix) - 1)); | |
130 const char* begin = strchr(functionName, ' '); | |
131 if (!begin) { // Fallback. | |
132 strncpy(buffer, functionName, maxLength); | |
133 buffer[maxLength - 1] = 0; | |
134 return buffer; | |
135 } | |
136 const char* end = strrchr(begin, '>'); | |
137 ASSERT(end); | |
138 int length = end - begin; | |
139 length = length < maxLength ? length : maxLength - 1; | |
140 memcpy(buffer, begin, length); | |
141 buffer[length] = 0; | |
142 return buffer; | |
143 } | |
144 #else | |
145 static const char* className(const char* functionName, char* buffer, const int m
axLength) | |
146 { | |
147 #if COMPILER(CLANG) | |
148 static const char prefix[] = "[T ="; | |
149 #elif COMPILER(GCC) | |
150 static const char prefix[] = "[with T ="; | |
151 #else | |
152 static const char prefix[] = "T ="; | |
153 #endif | |
154 const char* begin = strstr(functionName, prefix); | |
155 if (!begin) { // Fallback. | |
156 strncpy(buffer, functionName, maxLength); | |
157 buffer[maxLength - 1] = 0; | |
158 return buffer; | |
159 } | |
160 begin += sizeof(prefix); | |
161 const char* end = strchr(begin, ']'); | |
162 ASSERT(end); | |
163 int length = end - begin; | |
164 length = length < maxLength ? length : maxLength - 1; | |
165 memcpy(buffer, begin, length); | |
166 buffer[length] = 0; | |
167 return buffer; | |
168 } | |
169 #endif | |
170 | |
171 void MemoryClassInfo::callReportObjectInfo(MemoryObjectInfo* memoryObjectInfo, c
onst void* objectAddress, const char* stringWithClassName, MemoryObjectType obje
ctType, size_t actualSize) | |
172 { | |
173 memoryObjectInfo->reportObjectInfo(objectAddress, objectType, actualSize); | |
174 char buffer[256]; | |
175 memoryObjectInfo->setClassName(className(stringWithClassName, buffer, sizeof
(buffer))); | |
176 } | |
177 | |
178 void MemoryClassInfo::init(const void* objectAddress, const char* stringWithClas
sName, MemoryObjectType objectType, size_t actualSize) | |
179 { | |
180 callReportObjectInfo(m_memoryObjectInfo, objectAddress, stringWithClassName,
objectType, actualSize); | |
181 m_memoryInstrumentation = m_memoryObjectInfo->memoryInstrumentation(); | |
182 m_objectType = m_memoryObjectInfo->objectType(); | |
183 m_skipMembers = !m_memoryObjectInfo->firstVisit(); | |
184 } | |
185 | |
186 void MemoryClassInfo::addRawBuffer(const void* buffer, size_t size, const char*
className, const char* edgeName) | |
187 { | |
188 if (!m_skipMembers) | |
189 m_memoryInstrumentation->addRawBuffer(buffer, m_objectType, size, classN
ame, edgeName); | |
190 } | |
191 | |
192 void MemoryClassInfo::addPrivateBuffer(size_t size, MemoryObjectType ownerObject
Type, const char* className, const char* edgeName) | |
193 { | |
194 if (!size) | |
195 return; | |
196 if (m_skipMembers) | |
197 return; | |
198 if (!ownerObjectType) | |
199 ownerObjectType = m_objectType; | |
200 m_memoryInstrumentation->countObjectSize(0, ownerObjectType, size); | |
201 m_memoryInstrumentation->reportLinkToBuffer(0, ownerObjectType, size, classN
ame, edgeName); | |
202 } | |
203 | |
204 void MemoryClassInfo::setCustomAllocation(bool customAllocation) | |
205 { | |
206 m_memoryObjectInfo->setCustomAllocation(customAllocation); | |
207 } | |
208 | |
209 } // namespace WTF | |
OLD | NEW |