OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 85 } |
86 | 86 |
87 | 87 |
88 // --- T r a c e E x t e n s i o n --- | 88 // --- T r a c e E x t e n s i o n --- |
89 | 89 |
90 class TraceExtension : public v8::Extension { | 90 class TraceExtension : public v8::Extension { |
91 public: | 91 public: |
92 TraceExtension() : v8::Extension("v8/trace", kSource) { } | 92 TraceExtension() : v8::Extension("v8/trace", kSource) { } |
93 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 93 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
94 v8::Handle<String> name); | 94 v8::Handle<String> name); |
95 static v8::Handle<v8::Value> Trace(const v8::Arguments& args); | 95 static void Trace(const v8::FunctionCallbackInfo<v8::Value>& args); |
96 static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args); | 96 static void JSTrace(const v8::FunctionCallbackInfo<v8::Value>& args); |
97 static v8::Handle<v8::Value> JSEntrySP(const v8::Arguments& args); | 97 static void JSEntrySP(const v8::FunctionCallbackInfo<v8::Value>& args); |
98 static v8::Handle<v8::Value> JSEntrySPLevel2(const v8::Arguments& args); | 98 static void JSEntrySPLevel2(const v8::FunctionCallbackInfo<v8::Value>& args); |
99 private: | 99 private: |
100 static Address GetFP(const v8::Arguments& args); | 100 static Address GetFP(const v8::FunctionCallbackInfo<v8::Value>& args); |
101 static const char* kSource; | 101 static const char* kSource; |
102 }; | 102 }; |
103 | 103 |
104 | 104 |
105 const char* TraceExtension::kSource = | 105 const char* TraceExtension::kSource = |
106 "native function trace();" | 106 "native function trace();" |
107 "native function js_trace();" | 107 "native function js_trace();" |
108 "native function js_entry_sp();" | 108 "native function js_entry_sp();" |
109 "native function js_entry_sp_level2();"; | 109 "native function js_entry_sp_level2();"; |
110 | 110 |
111 v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction( | 111 v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction( |
112 v8::Handle<String> name) { | 112 v8::Handle<String> name) { |
113 if (name->Equals(String::New("trace"))) { | 113 if (name->Equals(String::New("trace"))) { |
114 return v8::FunctionTemplate::New(TraceExtension::Trace); | 114 return v8::FunctionTemplate::New(TraceExtension::Trace); |
115 } else if (name->Equals(String::New("js_trace"))) { | 115 } else if (name->Equals(String::New("js_trace"))) { |
116 return v8::FunctionTemplate::New(TraceExtension::JSTrace); | 116 return v8::FunctionTemplate::New(TraceExtension::JSTrace); |
117 } else if (name->Equals(String::New("js_entry_sp"))) { | 117 } else if (name->Equals(String::New("js_entry_sp"))) { |
118 return v8::FunctionTemplate::New(TraceExtension::JSEntrySP); | 118 return v8::FunctionTemplate::New(TraceExtension::JSEntrySP); |
119 } else if (name->Equals(String::New("js_entry_sp_level2"))) { | 119 } else if (name->Equals(String::New("js_entry_sp_level2"))) { |
120 return v8::FunctionTemplate::New(TraceExtension::JSEntrySPLevel2); | 120 return v8::FunctionTemplate::New(TraceExtension::JSEntrySPLevel2); |
121 } else { | 121 } else { |
122 CHECK(false); | 122 CHECK(false); |
123 return v8::Handle<v8::FunctionTemplate>(); | 123 return v8::Handle<v8::FunctionTemplate>(); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 | 127 |
128 Address TraceExtension::GetFP(const v8::Arguments& args) { | 128 Address TraceExtension::GetFP(const v8::FunctionCallbackInfo<v8::Value>& args) { |
129 // Convert frame pointer from encoding as smis in the arguments to a pointer. | 129 // Convert frame pointer from encoding as smis in the arguments to a pointer. |
130 CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform. | 130 CHECK_EQ(2, args.Length()); // Ignore second argument on 32-bit platform. |
131 #if defined(V8_HOST_ARCH_32_BIT) | 131 #if defined(V8_HOST_ARCH_32_BIT) |
132 Address fp = *reinterpret_cast<Address*>(*args[0]); | 132 Address fp = *reinterpret_cast<Address*>(*args[0]); |
133 #elif defined(V8_HOST_ARCH_64_BIT) | 133 #elif defined(V8_HOST_ARCH_64_BIT) |
134 int64_t low_bits = *reinterpret_cast<uint64_t*>(*args[0]) >> 32; | 134 int64_t low_bits = *reinterpret_cast<uint64_t*>(*args[0]) >> 32; |
135 int64_t high_bits = *reinterpret_cast<uint64_t*>(*args[1]); | 135 int64_t high_bits = *reinterpret_cast<uint64_t*>(*args[1]); |
136 Address fp = reinterpret_cast<Address>(high_bits | low_bits); | 136 Address fp = reinterpret_cast<Address>(high_bits | low_bits); |
137 #else | 137 #else |
138 #error Host architecture is neither 32-bit nor 64-bit. | 138 #error Host architecture is neither 32-bit nor 64-bit. |
139 #endif | 139 #endif |
140 printf("Trace: %p\n", fp); | 140 printf("Trace: %p\n", fp); |
141 return fp; | 141 return fp; |
142 } | 142 } |
143 | 143 |
144 | 144 |
145 v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) { | 145 void TraceExtension::Trace(const v8::FunctionCallbackInfo<v8::Value>& args) { |
146 DoTrace(GetFP(args)); | 146 DoTrace(GetFP(args)); |
147 return v8::Undefined(); | |
148 } | 147 } |
149 | 148 |
150 | 149 |
151 v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) { | 150 void TraceExtension::JSTrace(const v8::FunctionCallbackInfo<v8::Value>& args) { |
152 DoTraceHideCEntryFPAddress(GetFP(args)); | 151 DoTraceHideCEntryFPAddress(GetFP(args)); |
153 return v8::Undefined(); | |
154 } | 152 } |
155 | 153 |
156 | 154 |
157 static Address GetJsEntrySp() { | 155 static Address GetJsEntrySp() { |
158 CHECK_NE(NULL, i::Isolate::Current()->thread_local_top()); | 156 CHECK_NE(NULL, i::Isolate::Current()->thread_local_top()); |
159 return Isolate::js_entry_sp(i::Isolate::Current()->thread_local_top()); | 157 return Isolate::js_entry_sp(i::Isolate::Current()->thread_local_top()); |
160 } | 158 } |
161 | 159 |
162 | 160 |
163 v8::Handle<v8::Value> TraceExtension::JSEntrySP(const v8::Arguments& args) { | 161 void TraceExtension::JSEntrySP( |
| 162 const v8::FunctionCallbackInfo<v8::Value>& args) { |
164 CHECK_NE(0, GetJsEntrySp()); | 163 CHECK_NE(0, GetJsEntrySp()); |
165 return v8::Undefined(); | |
166 } | 164 } |
167 | 165 |
168 | 166 |
169 v8::Handle<v8::Value> TraceExtension::JSEntrySPLevel2( | 167 void TraceExtension::JSEntrySPLevel2( |
170 const v8::Arguments& args) { | 168 const v8::FunctionCallbackInfo<v8::Value>& args) { |
171 v8::HandleScope scope(args.GetIsolate()); | 169 v8::HandleScope scope(args.GetIsolate()); |
172 const Address js_entry_sp = GetJsEntrySp(); | 170 const Address js_entry_sp = GetJsEntrySp(); |
173 CHECK_NE(0, js_entry_sp); | 171 CHECK_NE(0, js_entry_sp); |
174 CompileRun("js_entry_sp();"); | 172 CompileRun("js_entry_sp();"); |
175 CHECK_EQ(js_entry_sp, GetJsEntrySp()); | 173 CHECK_EQ(js_entry_sp, GetJsEntrySp()); |
176 return v8::Undefined(); | |
177 } | 174 } |
178 | 175 |
179 | 176 |
180 static TraceExtension kTraceExtension; | 177 static TraceExtension kTraceExtension; |
181 v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension); | 178 v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension); |
182 | 179 |
183 | 180 |
184 static bool IsAddressWithinFuncCode(JSFunction* function, Address addr) { | 181 static bool IsAddressWithinFuncCode(JSFunction* function, Address addr) { |
185 i::Code* code = function->code(); | 182 i::Code* code = function->code(); |
186 return code->contains(addr); | 183 return code->contains(addr); |
187 } | 184 } |
188 | 185 |
189 static bool IsAddressWithinFuncCode(const char* func_name, Address addr) { | 186 static bool IsAddressWithinFuncCode(const char* func_name, Address addr) { |
190 v8::Local<v8::Value> func = CcTest::env()->Global()->Get(v8_str(func_name)); | 187 v8::Local<v8::Value> func = CcTest::env()->Global()->Get(v8_str(func_name)); |
191 CHECK(func->IsFunction()); | 188 CHECK(func->IsFunction()); |
192 JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func)); | 189 JSFunction* js_func = JSFunction::cast(*v8::Utils::OpenHandle(*func)); |
193 return IsAddressWithinFuncCode(js_func, addr); | 190 return IsAddressWithinFuncCode(js_func, addr); |
194 } | 191 } |
195 | 192 |
196 | 193 |
197 // This C++ function is called as a constructor, to grab the frame pointer | 194 // This C++ function is called as a constructor, to grab the frame pointer |
198 // from the calling function. When this function runs, the stack contains | 195 // from the calling function. When this function runs, the stack contains |
199 // a C_Entry frame and a Construct frame above the calling function's frame. | 196 // a C_Entry frame and a Construct frame above the calling function's frame. |
200 static v8::Handle<Value> construct_call(const v8::Arguments& args) { | 197 static void construct_call(const v8::FunctionCallbackInfo<v8::Value>& args) { |
201 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 198 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
202 i::StackFrameIterator frame_iterator(isolate); | 199 i::StackFrameIterator frame_iterator(isolate); |
203 CHECK(frame_iterator.frame()->is_exit()); | 200 CHECK(frame_iterator.frame()->is_exit()); |
204 frame_iterator.Advance(); | 201 frame_iterator.Advance(); |
205 CHECK(frame_iterator.frame()->is_construct()); | 202 CHECK(frame_iterator.frame()->is_construct()); |
206 frame_iterator.Advance(); | 203 frame_iterator.Advance(); |
207 i::StackFrame* calling_frame = frame_iterator.frame(); | 204 i::StackFrame* calling_frame = frame_iterator.frame(); |
208 CHECK(calling_frame->is_java_script()); | 205 CHECK(calling_frame->is_java_script()); |
209 | 206 |
210 #if defined(V8_HOST_ARCH_32_BIT) | 207 #if defined(V8_HOST_ARCH_32_BIT) |
211 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp()); | 208 int32_t low_bits = reinterpret_cast<int32_t>(calling_frame->fp()); |
212 args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1)); | 209 args.This()->Set(v8_str("low_bits"), v8_num(low_bits >> 1)); |
213 #elif defined(V8_HOST_ARCH_64_BIT) | 210 #elif defined(V8_HOST_ARCH_64_BIT) |
214 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp()); | 211 uint64_t fp = reinterpret_cast<uint64_t>(calling_frame->fp()); |
215 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff); | 212 int32_t low_bits = static_cast<int32_t>(fp & 0xffffffff); |
216 int32_t high_bits = static_cast<int32_t>(fp >> 32); | 213 int32_t high_bits = static_cast<int32_t>(fp >> 32); |
217 args.This()->Set(v8_str("low_bits"), v8_num(low_bits)); | 214 args.This()->Set(v8_str("low_bits"), v8_num(low_bits)); |
218 args.This()->Set(v8_str("high_bits"), v8_num(high_bits)); | 215 args.This()->Set(v8_str("high_bits"), v8_num(high_bits)); |
219 #else | 216 #else |
220 #error Host architecture is neither 32-bit nor 64-bit. | 217 #error Host architecture is neither 32-bit nor 64-bit. |
221 #endif | 218 #endif |
222 return args.This(); | 219 args.GetReturnValue().Set(args.This()); |
223 } | 220 } |
224 | 221 |
225 | 222 |
226 // Use the API to create a JSFunction object that calls the above C++ function. | 223 // Use the API to create a JSFunction object that calls the above C++ function. |
227 void CreateFramePointerGrabberConstructor(const char* constructor_name) { | 224 void CreateFramePointerGrabberConstructor(const char* constructor_name) { |
228 Local<v8::FunctionTemplate> constructor_template = | 225 Local<v8::FunctionTemplate> constructor_template = |
229 v8::FunctionTemplate::New(construct_call); | 226 v8::FunctionTemplate::New(construct_call); |
230 constructor_template->SetClassName(v8_str("FPGrabber")); | 227 constructor_template->SetClassName(v8_str("FPGrabber")); |
231 Local<Function> fun = constructor_template->GetFunction(); | 228 Local<Function> fun = constructor_template->GetFunction(); |
232 CcTest::env()->Global()->Set(v8_str(constructor_name), fun); | 229 CcTest::env()->Global()->Set(v8_str(constructor_name), fun); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 CcTest::InitializeVM(TRACE_EXTENSION); | 385 CcTest::InitializeVM(TRACE_EXTENSION); |
389 v8::HandleScope scope(CcTest::isolate()); | 386 v8::HandleScope scope(CcTest::isolate()); |
390 CHECK_EQ(0, GetJsEntrySp()); | 387 CHECK_EQ(0, GetJsEntrySp()); |
391 CompileRun("a = 1; b = a + 1;"); | 388 CompileRun("a = 1; b = a + 1;"); |
392 CHECK_EQ(0, GetJsEntrySp()); | 389 CHECK_EQ(0, GetJsEntrySp()); |
393 CompileRun("js_entry_sp();"); | 390 CompileRun("js_entry_sp();"); |
394 CHECK_EQ(0, GetJsEntrySp()); | 391 CHECK_EQ(0, GetJsEntrySp()); |
395 CompileRun("js_entry_sp_level2();"); | 392 CompileRun("js_entry_sp_level2();"); |
396 CHECK_EQ(0, GetJsEntrySp()); | 393 CHECK_EQ(0, GetJsEntrySp()); |
397 } | 394 } |
OLD | NEW |