kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 | // |
| 3 | // Tests of profiler-related functions from log.h |
| 4 | |
| 5 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include "v8.h" |
| 10 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11 | #include "codegen.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 12 | #include "log.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13 | #include "top.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 14 | #include "cctest.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 15 | #include "disassembler.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame^] | 16 | #include "register-allocator-inl.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 17 | |
| 18 | using v8::Function; |
| 19 | using v8::Local; |
| 20 | using v8::Object; |
| 21 | using v8::Script; |
| 22 | using v8::String; |
| 23 | using v8::Value; |
| 24 | |
| 25 | using v8::internal::byte; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 26 | using v8::internal::Address; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 27 | using v8::internal::Handle; |
| 28 | using v8::internal::JSFunction; |
| 29 | using v8::internal::StackTracer; |
| 30 | using v8::internal::TickSample; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 31 | using v8::internal::Top; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 32 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 33 | namespace i = v8::internal; |
| 34 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 35 | |
| 36 | static v8::Persistent<v8::Context> env; |
| 37 | |
| 38 | |
| 39 | static struct { |
| 40 | StackTracer* tracer; |
| 41 | TickSample* sample; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 42 | } trace_env = { NULL, NULL }; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | static void InitTraceEnv(StackTracer* tracer, TickSample* sample) { |
| 46 | trace_env.tracer = tracer; |
| 47 | trace_env.sample = sample; |
| 48 | } |
| 49 | |
| 50 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 51 | static void DoTrace(Address fp) { |
| 52 | trace_env.sample->fp = reinterpret_cast<uintptr_t>(fp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 53 | // sp is only used to define stack high bound |
| 54 | trace_env.sample->sp = |
| 55 | reinterpret_cast<unsigned int>(trace_env.sample) - 10240; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 56 | trace_env.tracer->Trace(trace_env.sample); |
| 57 | } |
| 58 | |
| 59 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 60 | // Hide c_entry_fp to emulate situation when sampling is done while |
| 61 | // pure JS code is being executed |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 62 | static void DoTraceHideCEntryFPAddress(Address fp) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 63 | v8::internal::Address saved_c_frame_fp = *(Top::c_entry_fp_address()); |
| 64 | CHECK(saved_c_frame_fp); |
| 65 | *(Top::c_entry_fp_address()) = 0; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 66 | DoTrace(fp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 67 | *(Top::c_entry_fp_address()) = saved_c_frame_fp; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 71 | static void CheckRetAddrIsInFunction(const char* func_name, |
| 72 | Address ret_addr, |
| 73 | Address func_start_addr, |
| 74 | unsigned int func_len) { |
| 75 | printf("CheckRetAddrIsInFunction \"%s\": %p %p %p\n", |
| 76 | func_name, func_start_addr, ret_addr, func_start_addr + func_len); |
| 77 | CHECK_GE(ret_addr, func_start_addr); |
| 78 | CHECK_GE(func_start_addr + func_len, ret_addr); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void CheckRetAddrIsInJSFunction(const char* func_name, |
| 83 | Address ret_addr, |
| 84 | Handle<JSFunction> func) { |
| 85 | v8::internal::Code* func_code = func->code(); |
| 86 | CheckRetAddrIsInFunction( |
| 87 | func_name, ret_addr, |
| 88 | func_code->instruction_start(), |
| 89 | func_code->ExecutableSize()); |
| 90 | } |
| 91 | |
| 92 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 93 | // --- T r a c e E x t e n s i o n --- |
| 94 | |
| 95 | class TraceExtension : public v8::Extension { |
| 96 | public: |
| 97 | TraceExtension() : v8::Extension("v8/trace", kSource) { } |
| 98 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 99 | v8::Handle<String> name); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 100 | static v8::Handle<v8::Value> Trace(const v8::Arguments& args); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 101 | static v8::Handle<v8::Value> JSTrace(const v8::Arguments& args); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 102 | private: |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 103 | static Address GetFP(const v8::Arguments& args); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 104 | static const char* kSource; |
| 105 | }; |
| 106 | |
| 107 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 108 | const char* TraceExtension::kSource = |
| 109 | "native function trace();" |
| 110 | "native function js_trace();"; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | v8::Handle<v8::FunctionTemplate> TraceExtension::GetNativeFunction( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 114 | v8::Handle<String> name) { |
| 115 | if (name->Equals(String::New("trace"))) { |
| 116 | return v8::FunctionTemplate::New(TraceExtension::Trace); |
| 117 | } else if (name->Equals(String::New("js_trace"))) { |
| 118 | return v8::FunctionTemplate::New(TraceExtension::JSTrace); |
| 119 | } else { |
| 120 | CHECK(false); |
| 121 | return v8::Handle<v8::FunctionTemplate>(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 126 | Address TraceExtension::GetFP(const v8::Arguments& args) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 127 | CHECK_EQ(1, args.Length()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 128 | Address fp = reinterpret_cast<Address>(args[0]->Int32Value() << 2); |
| 129 | printf("Trace: %p\n", fp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 130 | return fp; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | v8::Handle<v8::Value> TraceExtension::Trace(const v8::Arguments& args) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 135 | DoTrace(GetFP(args)); |
| 136 | return v8::Undefined(); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | v8::Handle<v8::Value> TraceExtension::JSTrace(const v8::Arguments& args) { |
| 141 | DoTraceHideCEntryFPAddress(GetFP(args)); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 142 | return v8::Undefined(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static TraceExtension kTraceExtension; |
| 147 | v8::DeclareExtension kTraceExtensionDeclaration(&kTraceExtension); |
| 148 | |
| 149 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 150 | static void InitializeVM() { |
| 151 | if (env.IsEmpty()) { |
| 152 | v8::HandleScope scope; |
| 153 | const char* extensions[] = { "v8/trace" }; |
| 154 | v8::ExtensionConfiguration config(1, extensions); |
| 155 | env = v8::Context::New(&config); |
| 156 | } |
| 157 | v8::HandleScope scope; |
| 158 | env->Enter(); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static Handle<JSFunction> CompileFunction(const char* source) { |
| 163 | return v8::Utils::OpenHandle(*Script::Compile(String::New(source))); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static void CompileRun(const char* source) { |
| 168 | Script::Compile(String::New(source))->Run(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static Local<Value> GetGlobalProperty(const char* name) { |
| 173 | return env->Global()->Get(String::New(name)); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static Handle<JSFunction> GetGlobalJSFunction(const char* name) { |
| 178 | Handle<JSFunction> js_func(JSFunction::cast( |
| 179 | *(v8::Utils::OpenHandle( |
| 180 | *GetGlobalProperty(name))))); |
| 181 | return js_func; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static void CheckRetAddrIsInJSFunction(const char* func_name, |
| 186 | Address ret_addr) { |
| 187 | CheckRetAddrIsInJSFunction(func_name, ret_addr, |
| 188 | GetGlobalJSFunction(func_name)); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static void SetGlobalProperty(const char* name, Local<Value> value) { |
| 193 | env->Global()->Set(String::New(name), value); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static Handle<v8::internal::String> NewString(const char* s) { |
| 198 | return i::Factory::NewStringFromAscii(i::CStrVector(s)); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | namespace v8 { namespace internal { |
| 203 | |
| 204 | class CodeGeneratorPatcher { |
| 205 | public: |
| 206 | CodeGeneratorPatcher() { |
| 207 | CodeGenerator::InlineRuntimeLUT genGetFramePointer = |
| 208 | {&CodeGenerator::GenerateGetFramePointer, "_GetFramePointer"}; |
| 209 | // _FastCharCodeAt is not used in our tests. |
| 210 | bool result = CodeGenerator::PatchInlineRuntimeEntry( |
| 211 | NewString("_FastCharCodeAt"), |
| 212 | genGetFramePointer, &oldInlineEntry); |
| 213 | CHECK(result); |
| 214 | } |
| 215 | |
| 216 | ~CodeGeneratorPatcher() { |
| 217 | CHECK(CodeGenerator::PatchInlineRuntimeEntry( |
| 218 | NewString("_GetFramePointer"), |
| 219 | oldInlineEntry, NULL)); |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | CodeGenerator::InlineRuntimeLUT oldInlineEntry; |
| 224 | }; |
| 225 | |
| 226 | } } // namespace v8::internal |
| 227 | |
| 228 | |
| 229 | // Creates a global function named 'func_name' that calls the tracing |
| 230 | // function 'trace_func_name' with an actual EBP register value, |
| 231 | // shifted right to be presented as Smi. |
| 232 | static void CreateTraceCallerFunction(const char* func_name, |
| 233 | const char* trace_func_name) { |
| 234 | i::EmbeddedVector<char, 256> trace_call_buf; |
| 235 | i::OS::SNPrintF(trace_call_buf, "%s(%%_GetFramePointer());", trace_func_name); |
| 236 | |
| 237 | // Compile the script. |
| 238 | i::CodeGeneratorPatcher patcher; |
| 239 | bool allow_natives_syntax = i::FLAG_allow_natives_syntax; |
| 240 | i::FLAG_allow_natives_syntax = true; |
| 241 | Handle<JSFunction> func = CompileFunction(trace_call_buf.start()); |
| 242 | CHECK(!func.is_null()); |
| 243 | i::FLAG_allow_natives_syntax = allow_natives_syntax; |
| 244 | |
| 245 | #ifdef DEBUG |
| 246 | v8::internal::Code* func_code = func->code(); |
| 247 | CHECK(func_code->IsCode()); |
| 248 | func_code->Print(); |
| 249 | #endif |
| 250 | |
| 251 | SetGlobalProperty(func_name, v8::ToApi<Value>(func)); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | TEST(CFromJSStackTrace) { |
| 256 | TickSample sample; |
| 257 | StackTracer tracer(reinterpret_cast<uintptr_t>(&sample)); |
| 258 | InitTraceEnv(&tracer, &sample); |
| 259 | |
| 260 | InitializeVM(); |
| 261 | v8::HandleScope scope; |
| 262 | CreateTraceCallerFunction("JSFuncDoTrace", "trace"); |
| 263 | CompileRun( |
| 264 | "function JSTrace() {" |
| 265 | " JSFuncDoTrace();" |
| 266 | "};\n" |
| 267 | "JSTrace();"); |
| 268 | CHECK_GT(sample.frames_count, 1); |
| 269 | // Stack sampling will start from the first JS function, i.e. "JSFuncDoTrace" |
| 270 | CheckRetAddrIsInJSFunction("JSFuncDoTrace", |
| 271 | sample.stack[0]); |
| 272 | CheckRetAddrIsInJSFunction("JSTrace", |
| 273 | sample.stack[1]); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | TEST(PureJSStackTrace) { |
| 278 | TickSample sample; |
| 279 | StackTracer tracer(reinterpret_cast<uintptr_t>(&sample)); |
| 280 | InitTraceEnv(&tracer, &sample); |
| 281 | |
| 282 | InitializeVM(); |
| 283 | v8::HandleScope scope; |
| 284 | CreateTraceCallerFunction("JSFuncDoTrace", "js_trace"); |
| 285 | CompileRun( |
| 286 | "function JSTrace() {" |
| 287 | " JSFuncDoTrace();" |
| 288 | "};\n" |
| 289 | "function OuterJSTrace() {" |
| 290 | " JSTrace();" |
| 291 | "};\n" |
| 292 | "OuterJSTrace();"); |
| 293 | CHECK_GT(sample.frames_count, 1); |
| 294 | // Stack sampling will start from the caller of JSFuncDoTrace, i.e. "JSTrace" |
| 295 | CheckRetAddrIsInJSFunction("JSTrace", |
| 296 | sample.stack[0]); |
| 297 | CheckRetAddrIsInJSFunction("OuterJSTrace", |
| 298 | sample.stack[1]); |
| 299 | } |
| 300 | |
| 301 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 302 | static void CFuncDoTrace() { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 303 | Address fp; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 304 | #ifdef __GNUC__ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 305 | fp = reinterpret_cast<Address>(__builtin_frame_address(0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 306 | #elif defined _MSC_VER |
| 307 | __asm mov [fp], ebp // NOLINT |
| 308 | #endif |
| 309 | DoTrace(fp); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | static int CFunc(int depth) { |
| 314 | if (depth <= 0) { |
| 315 | CFuncDoTrace(); |
| 316 | return 0; |
| 317 | } else { |
| 318 | return CFunc(depth - 1) + 1; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | |
| 323 | TEST(PureCStackTrace) { |
| 324 | TickSample sample; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 325 | StackTracer tracer(reinterpret_cast<uintptr_t>(&sample)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 326 | InitTraceEnv(&tracer, &sample); |
| 327 | // Check that sampler doesn't crash |
| 328 | CHECK_EQ(10, CFunc(10)); |
| 329 | } |
| 330 | |
| 331 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 332 | #endif // ENABLE_LOGGING_AND_PROFILING |