OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string.h> |
| 6 |
| 7 #include "include/libplatform/v8-tracing.h" |
| 8 |
| 9 namespace v8 { |
| 10 |
| 11 class Isolate; |
| 12 |
| 13 namespace platform { |
| 14 namespace tracing { |
| 15 |
| 16 // String options that can be used to initialize TraceOptions. |
| 17 const char kRecordUntilFull[] = "record-until-full"; |
| 18 const char kRecordContinuously[] = "record-continuously"; |
| 19 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; |
| 20 |
| 21 const char kRecordModeParam[] = "record_mode"; |
| 22 const char kEnableSamplingParam[] = "enable_sampling"; |
| 23 const char kEnableSystraceParam[] = "enable_systrace"; |
| 24 const char kEnableArgumentFilterParam[] = "enable_argument_filter"; |
| 25 const char kIncludedCategoriesParam[] = "included_categories"; |
| 26 const char kExcludedCategoriesParam[] = "excluded_categories"; |
| 27 |
| 28 class TraceConfigParser { |
| 29 public: |
| 30 static void FillTraceConfig(v8::Isolate* isolate, TraceConfig* trace_config, |
| 31 const char* json_str) { |
| 32 HandleScope outer_scope(isolate); |
| 33 Local<Context> context = Context::New(isolate); |
| 34 Context::Scope context_scope(context); |
| 35 HandleScope inner_scope(isolate); |
| 36 |
| 37 Local<String> source = |
| 38 String::NewFromUtf8(isolate, json_str, NewStringType::kNormal) |
| 39 .ToLocalChecked(); |
| 40 Local<Value> result = JSON::Parse(context, source).ToLocalChecked(); |
| 41 Local<v8::Object> trace_config_object = Local<v8::Object>::Cast(result); |
| 42 |
| 43 trace_config->record_mode_ = |
| 44 GetTraceRecordMode(isolate, context, trace_config_object); |
| 45 trace_config->enable_sampling_ = |
| 46 GetBoolean(isolate, context, trace_config_object, kEnableSamplingParam); |
| 47 trace_config->enable_systrace_ = |
| 48 GetBoolean(isolate, context, trace_config_object, kEnableSystraceParam); |
| 49 trace_config->enable_argument_filter_ = GetBoolean( |
| 50 isolate, context, trace_config_object, kEnableArgumentFilterParam); |
| 51 UpdateCategoriesList(isolate, context, trace_config_object, |
| 52 kIncludedCategoriesParam, |
| 53 trace_config->included_categories_); |
| 54 UpdateCategoriesList(isolate, context, trace_config_object, |
| 55 kExcludedCategoriesParam, |
| 56 trace_config->excluded_categories_); |
| 57 } |
| 58 |
| 59 private: |
| 60 static bool GetBoolean(v8::Isolate* isolate, Local<Context> context, |
| 61 Local<v8::Object> object, const char* property) { |
| 62 Local<Value> value = GetValue(isolate, context, object, property); |
| 63 if (value->IsNumber()) { |
| 64 Local<Boolean> v8_boolean = value->ToBoolean(context).ToLocalChecked(); |
| 65 return v8_boolean->Value(); |
| 66 } |
| 67 return false; |
| 68 } |
| 69 |
| 70 static int UpdateCategoriesList(v8::Isolate* isolate, Local<Context> context, |
| 71 Local<v8::Object> object, |
| 72 const char* property, |
| 73 TraceConfig::StringList& categories_list) { |
| 74 Local<Value> value = GetValue(isolate, context, object, property); |
| 75 if (value->IsArray()) { |
| 76 Local<Array> v8_array = Local<Array>::Cast(value); |
| 77 for (int i = 0, length = v8_array->Length(); i < length; ++i) { |
| 78 Local<Value> v = v8_array->Get(context, i) |
| 79 .ToLocalChecked() |
| 80 ->ToString(context) |
| 81 .ToLocalChecked(); |
| 82 String::Utf8Value str(v->ToString(context).ToLocalChecked()); |
| 83 categories_list.push_back(std::string(*str)); |
| 84 } |
| 85 return v8_array->Length(); |
| 86 } |
| 87 return 0; |
| 88 } |
| 89 |
| 90 static TraceRecordMode GetTraceRecordMode(v8::Isolate* isolate, |
| 91 Local<Context> context, |
| 92 Local<v8::Object> object) { |
| 93 Local<Value> value = GetValue(isolate, context, object, kRecordModeParam); |
| 94 if (value->IsString()) { |
| 95 Local<String> v8_string = value->ToString(context).ToLocalChecked(); |
| 96 String::Utf8Value str(v8_string); |
| 97 if (strcmp(kRecordUntilFull, *str) == 0) { |
| 98 return TraceRecordMode::RECORD_UNTIL_FULL; |
| 99 } else if (strcmp(kRecordContinuously, *str) == 0) { |
| 100 return TraceRecordMode::RECORD_CONTINUOUSLY; |
| 101 } else if (strcmp(kRecordAsMuchAsPossible, *str) == 0) { |
| 102 return TraceRecordMode::RECORD_AS_MUCH_AS_POSSIBLE; |
| 103 } |
| 104 } |
| 105 return TraceRecordMode::RECORD_UNTIL_FULL; |
| 106 } |
| 107 |
| 108 static Local<Value> GetValue(v8::Isolate* isolate, Local<Context> context, |
| 109 Local<v8::Object> object, const char* property) { |
| 110 Local<String> v8_str = |
| 111 String::NewFromUtf8(isolate, property, NewStringType::kNormal) |
| 112 .ToLocalChecked(); |
| 113 return object->Get(context, v8_str).ToLocalChecked(); |
| 114 } |
| 115 }; |
| 116 |
| 117 TraceConfig* TraceConfig::CreateTraceConfigFromJSON(v8::Isolate* isolate, |
| 118 const char* json_str) { |
| 119 TraceConfig* trace_config = new TraceConfig(); |
| 120 TraceConfigParser::FillTraceConfig(isolate, trace_config, json_str); |
| 121 return trace_config; |
| 122 } |
| 123 |
| 124 TraceConfig* TraceConfig::CreateDefaultTraceConfig() { |
| 125 TraceConfig* trace_config = new TraceConfig(); |
| 126 trace_config->included_categories_.push_back("v8"); |
| 127 return trace_config; |
| 128 } |
| 129 |
| 130 bool TraceConfig::IsCategoryGroupEnabled(const char* category_group) const { |
| 131 for (auto included_category : included_categories_) { |
| 132 if (strcmp(included_category.data(), category_group) == 0) return true; |
| 133 } |
| 134 return false; |
| 135 } |
| 136 |
| 137 } // namespace tracing |
| 138 } // namespace platform |
| 139 } // namespace v8 |
OLD | NEW |