OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/sampler.h" | 5 #include "src/sampler.h" |
6 | 6 |
7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 7 #if V8_OS_POSIX && !V8_OS_CYGWIN |
8 | 8 |
9 #define USE_SIGNALS | 9 #define USE_SIGNALS |
10 | 10 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | 166 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} |
167 ThreadId profiled_thread_id() { return profiled_thread_id_; } | 167 ThreadId profiled_thread_id() { return profiled_thread_id_; } |
168 | 168 |
169 protected: | 169 protected: |
170 ~PlatformDataCommon() {} | 170 ~PlatformDataCommon() {} |
171 | 171 |
172 private: | 172 private: |
173 ThreadId profiled_thread_id_; | 173 ThreadId profiled_thread_id_; |
174 }; | 174 }; |
175 | 175 |
176 | |
177 bool IsSamePage(byte* ptr1, byte* ptr2) { | |
178 const uint32_t kPageSize = 4096; | |
179 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); | |
180 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == | |
181 (reinterpret_cast<uintptr_t>(ptr2) & mask); | |
182 } | |
183 | |
184 | |
185 // Check if the code at specified address could potentially be a | |
186 // frame setup code. | |
187 bool IsNoFrameRegion(Address address) { | |
188 struct Pattern { | |
189 int bytes_count; | |
190 byte bytes[8]; | |
191 int offsets[4]; | |
192 }; | |
193 byte* pc = reinterpret_cast<byte*>(address); | |
194 static Pattern patterns[] = { | |
195 #if V8_HOST_ARCH_IA32 | |
196 // push %ebp | |
197 // mov %esp,%ebp | |
198 {3, {0x55, 0x89, 0xe5}, {0, 1, -1}}, | |
199 // pop %ebp | |
200 // ret N | |
201 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
202 // pop %ebp | |
203 // ret | |
204 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
205 #elif V8_HOST_ARCH_X64 | |
206 // pushq %rbp | |
207 // movq %rsp,%rbp | |
208 {4, {0x55, 0x48, 0x89, 0xe5}, {0, 1, -1}}, | |
209 // popq %rbp | |
210 // ret N | |
211 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
212 // popq %rbp | |
213 // ret | |
214 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
215 #endif | |
216 {0, {}, {}} | |
217 }; | |
218 for (Pattern* pattern = patterns; pattern->bytes_count; ++pattern) { | |
219 for (int* offset_ptr = pattern->offsets; *offset_ptr != -1; ++offset_ptr) { | |
220 int offset = *offset_ptr; | |
221 if (!offset || IsSamePage(pc, pc - offset)) { | |
222 MSAN_MEMORY_IS_INITIALIZED(pc - offset, pattern->bytes_count); | |
223 if (!memcmp(pc - offset, pattern->bytes, pattern->bytes_count)) | |
224 return true; | |
225 } else { | |
226 // It is not safe to examine bytes on another page as it might not be | |
227 // allocated thus causing a SEGFAULT. | |
228 // Check the pattern part that's on the same page and | |
229 // pessimistically assume it could be the entire pattern match. | |
230 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); | |
231 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) | |
232 return true; | |
233 } | |
234 } | |
235 } | |
236 return false; | |
237 } | |
238 | |
239 } // namespace | 176 } // namespace |
240 | 177 |
241 #if defined(USE_SIGNALS) | 178 #if defined(USE_SIGNALS) |
242 | 179 |
243 class Sampler::PlatformData : public PlatformDataCommon { | 180 class Sampler::PlatformData : public PlatformDataCommon { |
244 public: | 181 public: |
245 PlatformData() : vm_tid_(pthread_self()) {} | 182 PlatformData() : vm_tid_(pthread_self()) {} |
246 pthread_t vm_tid() const { return vm_tid_; } | 183 pthread_t vm_tid() const { return vm_tid_; } |
247 | 184 |
248 private: | 185 private: |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 timestamp = base::TimeTicks::HighResolutionNow(); | 585 timestamp = base::TimeTicks::HighResolutionNow(); |
649 pc = reinterpret_cast<Address>(regs.pc); | 586 pc = reinterpret_cast<Address>(regs.pc); |
650 state = isolate->current_vm_state(); | 587 state = isolate->current_vm_state(); |
651 | 588 |
652 // Avoid collecting traces while doing GC. | 589 // Avoid collecting traces while doing GC. |
653 if (state == GC) return; | 590 if (state == GC) return; |
654 | 591 |
655 Address js_entry_sp = isolate->js_entry_sp(); | 592 Address js_entry_sp = isolate->js_entry_sp(); |
656 if (js_entry_sp == 0) return; // Not executing JS now. | 593 if (js_entry_sp == 0) return; // Not executing JS now. |
657 | 594 |
658 if (pc && IsNoFrameRegion(pc)) { | |
659 pc = 0; | |
660 return; | |
661 } | |
662 | |
663 ExternalCallbackScope* scope = isolate->external_callback_scope(); | 595 ExternalCallbackScope* scope = isolate->external_callback_scope(); |
664 Address handler = Isolate::handler(isolate->thread_local_top()); | 596 Address handler = Isolate::handler(isolate->thread_local_top()); |
665 // If there is a handler on top of the external callback scope then | 597 // If there is a handler on top of the external callback scope then |
666 // we have already entrered JavaScript again and the external callback | 598 // we have already entrered JavaScript again and the external callback |
667 // is not the top function. | 599 // is not the top function. |
668 if (scope && scope->scope_address() < handler) { | 600 if (scope && scope->scope_address() < handler) { |
669 external_callback = scope->callback(); | 601 external_callback = scope->callback(); |
670 has_external_callback = true; | 602 has_external_callback = true; |
671 } else { | 603 } else { |
672 // Sample potential return address value for frameless invocation of | 604 // Sample potential return address value for frameless invocation of |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 SampleStack(state); | 768 SampleStack(state); |
837 } | 769 } |
838 ResumeThread(profiled_thread); | 770 ResumeThread(profiled_thread); |
839 } | 771 } |
840 | 772 |
841 #endif // USE_SIGNALS | 773 #endif // USE_SIGNALS |
842 | 774 |
843 | 775 |
844 } // namespace internal | 776 } // namespace internal |
845 } // namespace v8 | 777 } // namespace v8 |
OLD | NEW |