Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/string-stream.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/handles-inl.h" |
| 8 | #include "src/prototype.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
| 13 | static const int kMentionedObjectCacheMaxSize = 256; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | |
| 15 | char* HeapStringAllocator::allocate(unsigned bytes) { |
| 16 | space_ = NewArray<char>(bytes); |
| 17 | return space_; |
| 18 | } |
| 19 | |
| 20 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | bool StringStream::Put(char c) { |
| 22 | if (full()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 23 | DCHECK(length_ < capacity_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 24 | // Since the trailing '\0' is not accounted for in length_ fullness is |
| 25 | // indicated by a difference of 1 between length_ and capacity_. Thus when |
| 26 | // reaching a difference of 2 we need to grow the buffer. |
| 27 | if (length_ == capacity_ - 2) { |
| 28 | unsigned new_capacity = capacity_; |
| 29 | char* new_buffer = allocator_->grow(&new_capacity); |
| 30 | if (new_capacity > capacity_) { |
| 31 | capacity_ = new_capacity; |
| 32 | buffer_ = new_buffer; |
| 33 | } else { |
| 34 | // Reached the end of the available buffer. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | DCHECK(capacity_ >= 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | length_ = capacity_ - 1; // Indicate fullness of the stream. |
| 37 | buffer_[length_ - 4] = '.'; |
| 38 | buffer_[length_ - 3] = '.'; |
| 39 | buffer_[length_ - 2] = '.'; |
| 40 | buffer_[length_ - 1] = '\n'; |
| 41 | buffer_[length_] = '\0'; |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | buffer_[length_] = c; |
| 46 | buffer_[length_ + 1] = '\0'; |
| 47 | length_++; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | // A control character is one that configures a format element. For |
| 53 | // instance, in %.5s, .5 are control characters. |
| 54 | static bool IsControlChar(char c) { |
| 55 | switch (c) { |
| 56 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 57 | case '6': case '7': case '8': case '9': case '.': case '-': |
| 58 | return true; |
| 59 | default: |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) { |
| 66 | // If we already ran out of space then return immediately. |
| 67 | if (full()) return; |
| 68 | int offset = 0; |
| 69 | int elm = 0; |
| 70 | while (offset < format.length()) { |
| 71 | if (format[offset] != '%' || elm == elms.length()) { |
| 72 | Put(format[offset]); |
| 73 | offset++; |
| 74 | continue; |
| 75 | } |
| 76 | // Read this formatting directive into a temporary buffer |
| 77 | EmbeddedVector<char, 24> temp; |
| 78 | int format_length = 0; |
| 79 | // Skip over the whole control character sequence until the |
| 80 | // format element type |
| 81 | temp[format_length++] = format[offset++]; |
| 82 | while (offset < format.length() && IsControlChar(format[offset])) |
| 83 | temp[format_length++] = format[offset++]; |
| 84 | if (offset >= format.length()) |
| 85 | return; |
| 86 | char type = format[offset]; |
| 87 | temp[format_length++] = type; |
| 88 | temp[format_length] = '\0'; |
| 89 | offset++; |
| 90 | FmtElm current = elms[elm++]; |
| 91 | switch (type) { |
| 92 | case 's': { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | DCHECK_EQ(FmtElm::C_STR, current.type_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | const char* value = current.data_.u_c_str_; |
| 95 | Add(value); |
| 96 | break; |
| 97 | } |
| 98 | case 'w': { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | DCHECK_EQ(FmtElm::LC_STR, current.type_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | Vector<const uc16> value = *current.data_.u_lc_str_; |
| 101 | for (int i = 0; i < value.length(); i++) |
| 102 | Put(static_cast<char>(value[i])); |
| 103 | break; |
| 104 | } |
| 105 | case 'o': { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | DCHECK_EQ(FmtElm::OBJ, current.type_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | Object* obj = current.data_.u_obj_; |
| 108 | PrintObject(obj); |
| 109 | break; |
| 110 | } |
| 111 | case 'k': { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | DCHECK_EQ(FmtElm::INT, current.type_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | int value = current.data_.u_int_; |
| 114 | if (0x20 <= value && value <= 0x7F) { |
| 115 | Put(value); |
| 116 | } else if (value <= 0xff) { |
| 117 | Add("\\x%02x", value); |
| 118 | } else { |
| 119 | Add("\\u%04x", value); |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': { |
| 124 | int value = current.data_.u_int_; |
| 125 | EmbeddedVector<char, 24> formatted; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | int length = SNPrintF(formatted, temp.start(), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | Add(Vector<const char>(formatted.start(), length)); |
| 128 | break; |
| 129 | } |
| 130 | case 'f': case 'g': case 'G': case 'e': case 'E': { |
| 131 | double value = current.data_.u_double_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | int inf = std::isinf(value); |
| 133 | if (inf == -1) { |
| 134 | Add("-inf"); |
| 135 | } else if (inf == 1) { |
| 136 | Add("inf"); |
| 137 | } else if (std::isnan(value)) { |
| 138 | Add("nan"); |
| 139 | } else { |
| 140 | EmbeddedVector<char, 28> formatted; |
| 141 | SNPrintF(formatted, temp.start(), value); |
| 142 | Add(formatted.start()); |
| 143 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | case 'p': { |
| 147 | void* value = current.data_.u_pointer_; |
| 148 | EmbeddedVector<char, 20> formatted; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | SNPrintF(formatted, temp.start(), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | Add(formatted.start()); |
| 151 | break; |
| 152 | } |
| 153 | default: |
| 154 | UNREACHABLE(); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Verify that the buffer is 0-terminated |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 160 | DCHECK(buffer_[length_] == '\0'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | void StringStream::PrintObject(Object* o) { |
| 165 | o->ShortPrint(this); |
| 166 | if (o->IsString()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 167 | if (String::cast(o)->length() <= String::kMaxShortPrintLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | } else if (o->IsNumber() || o->IsOddball()) { |
| 171 | return; |
| 172 | } |
| 173 | if (o->IsHeapObject()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 174 | HeapObject* ho = HeapObject::cast(o); |
| 175 | DebugObjectCache* debug_object_cache = ho->GetIsolate()-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 176 | string_stream_debug_object_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 178 | if ((*debug_object_cache)[i] == o) { |
| 179 | Add("#%d#", i); |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) { |
| 184 | Add("#%d#", debug_object_cache->length()); |
| 185 | debug_object_cache->Add(HeapObject::cast(o)); |
| 186 | } else { |
| 187 | Add("@%p", o); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void StringStream::Add(const char* format) { |
| 194 | Add(CStrVector(format)); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void StringStream::Add(Vector<const char> format) { |
| 199 | Add(format, Vector<FmtElm>::empty()); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void StringStream::Add(const char* format, FmtElm arg0) { |
| 204 | const char argc = 1; |
| 205 | FmtElm argv[argc] = { arg0 }; |
| 206 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) { |
| 211 | const char argc = 2; |
| 212 | FmtElm argv[argc] = { arg0, arg1 }; |
| 213 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 218 | FmtElm arg2) { |
| 219 | const char argc = 3; |
| 220 | FmtElm argv[argc] = { arg0, arg1, arg2 }; |
| 221 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 226 | FmtElm arg2, FmtElm arg3) { |
| 227 | const char argc = 4; |
| 228 | FmtElm argv[argc] = { arg0, arg1, arg2, arg3 }; |
| 229 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 230 | } |
| 231 | |
| 232 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 234 | FmtElm arg2, FmtElm arg3, FmtElm arg4) { |
| 235 | const char argc = 5; |
| 236 | FmtElm argv[argc] = { arg0, arg1, arg2, arg3, arg4 }; |
| 237 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 238 | } |
| 239 | |
| 240 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 241 | SmartArrayPointer<const char> StringStream::ToCString() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | char* str = NewArray<char>(length_ + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | MemCopy(str, buffer_, length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | str[length_] = '\0'; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 245 | return SmartArrayPointer<const char>(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | void StringStream::Log(Isolate* isolate) { |
| 250 | LOG(isolate, StringEvent("StackDump", buffer_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 254 | void StringStream::OutputToFile(FILE* out) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | // Dump the output to stdout, but make sure to break it up into |
| 256 | // manageable chunks to avoid losing parts of the output in the OS |
| 257 | // printing code. This is a problem on Windows in particular; see |
| 258 | // the VPrint() function implementations in platform-win32.cc. |
| 259 | unsigned position = 0; |
| 260 | for (unsigned next; (next = position + 2048) < length_; position = next) { |
| 261 | char save = buffer_[next]; |
| 262 | buffer_[next] = '\0'; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 263 | internal::PrintF(out, "%s", &buffer_[position]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | buffer_[next] = save; |
| 265 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 266 | internal::PrintF(out, "%s", &buffer_[position]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 270 | Handle<String> StringStream::ToString(Isolate* isolate) { |
| 271 | return isolate->factory()->NewStringFromUtf8( |
| 272 | Vector<const char>(buffer_, length_)).ToHandleChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 276 | void StringStream::ClearMentionedObjectCache(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 277 | isolate->set_string_stream_current_security_token(NULL); |
| 278 | if (isolate->string_stream_debug_object_cache() == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 279 | isolate->set_string_stream_debug_object_cache(new DebugObjectCache(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 281 | isolate->string_stream_debug_object_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 286 | bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) { |
| 287 | return isolate->string_stream_debug_object_cache()->length() == 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | } |
| 289 | #endif |
| 290 | |
| 291 | |
| 292 | bool StringStream::Put(String* str) { |
| 293 | return Put(str, 0, str->length()); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | bool StringStream::Put(String* str, int start, int end) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 298 | StringCharacterStream stream(str, start); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | for (int i = start; i < end && stream.HasMore(); i++) { |
| 300 | uint16_t c = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | if (c >= 127 || c < 32) { |
| 302 | c = '?'; |
| 303 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 304 | if (!Put(static_cast<char>(c))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | return false; // Output was truncated. |
| 306 | } |
| 307 | } |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | void StringStream::PrintName(Object* name) { |
| 313 | if (name->IsString()) { |
| 314 | String* str = String::cast(name); |
| 315 | if (str->length() > 0) { |
| 316 | Put(str); |
| 317 | } else { |
| 318 | Add("/* anonymous */"); |
| 319 | } |
| 320 | } else { |
| 321 | Add("%o", name); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | void StringStream::PrintUsingMap(JSObject* js_object) { |
| 327 | Map* map = js_object->map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 328 | if (!js_object->GetHeap()->Contains(map) || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | !map->IsHeapObject() || |
| 330 | !map->IsMap()) { |
| 331 | Add("<Invalid map>\n"); |
| 332 | return; |
| 333 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 334 | int real_size = map->NumberOfOwnDescriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | DescriptorArray* descs = map->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 336 | for (int i = 0; i < real_size; i++) { |
| 337 | PropertyDetails details = descs->GetDetails(i); |
| 338 | if (details.type() == FIELD) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 339 | Object* key = descs->GetKey(i); |
| 340 | if (key->IsString() || key->IsNumber()) { |
| 341 | int len = 3; |
| 342 | if (key->IsString()) { |
| 343 | len = String::cast(key)->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 345 | for (; len < 18; len++) |
| 346 | Put(' '); |
| 347 | if (key->IsString()) { |
| 348 | Put(String::cast(key)); |
| 349 | } else { |
| 350 | key->ShortPrint(); |
| 351 | } |
| 352 | Add(": "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 353 | FieldIndex index = FieldIndex::ForDescriptor(map, i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 354 | if (js_object->IsUnboxedDoubleField(index)) { |
| 355 | double value = js_object->RawFastDoublePropertyAt(index); |
| 356 | Add("<unboxed double> %.16g\n", FmtElm(value)); |
| 357 | } else { |
| 358 | Object* value = js_object->RawFastPropertyAt(index); |
| 359 | Add("%o\n", value); |
| 360 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
| 367 | void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 368 | Heap* heap = array->GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 370 | Object* element = array->get(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 371 | if (element != heap->the_hole_value()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | for (int len = 1; len < 18; len++) |
| 373 | Put(' '); |
| 374 | Add("%d: %o\n", i, array->get(i)); |
| 375 | } |
| 376 | } |
| 377 | if (limit >= 10) { |
| 378 | Add(" ...\n"); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | void StringStream::PrintByteArray(ByteArray* byte_array) { |
| 384 | unsigned int limit = byte_array->length(); |
| 385 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 386 | byte b = byte_array->get(i); |
| 387 | Add(" %d: %3d 0x%02x", i, b, b); |
| 388 | if (b >= ' ' && b <= '~') { |
| 389 | Add(" '%c'", b); |
| 390 | } else if (b == '\n') { |
| 391 | Add(" '\n'"); |
| 392 | } else if (b == '\r') { |
| 393 | Add(" '\r'"); |
| 394 | } else if (b >= 1 && b <= 26) { |
| 395 | Add(" ^%c", b + 'A' - 1); |
| 396 | } |
| 397 | Add("\n"); |
| 398 | } |
| 399 | if (limit >= 10) { |
| 400 | Add(" ...\n"); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 405 | void StringStream::PrintMentionedObjectCache(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 406 | DebugObjectCache* debug_object_cache = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | isolate->string_stream_debug_object_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 408 | Add("==== Key ============================================\n\n"); |
| 409 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 410 | HeapObject* printee = (*debug_object_cache)[i]; |
| 411 | Add(" #%d# %p: ", i, printee); |
| 412 | printee->ShortPrint(this); |
| 413 | Add("\n"); |
| 414 | if (printee->IsJSObject()) { |
| 415 | if (printee->IsJSValue()) { |
| 416 | Add(" value(): %o\n", JSValue::cast(printee)->value()); |
| 417 | } |
| 418 | PrintUsingMap(JSObject::cast(printee)); |
| 419 | if (printee->IsJSArray()) { |
| 420 | JSArray* array = JSArray::cast(printee); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | if (array->HasFastObjectElements()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | unsigned int limit = FixedArray::cast(array->elements())->length(); |
| 423 | unsigned int length = |
| 424 | static_cast<uint32_t>(JSArray::cast(array)->length()->Number()); |
| 425 | if (length < limit) limit = length; |
| 426 | PrintFixedArray(FixedArray::cast(array->elements()), limit); |
| 427 | } |
| 428 | } |
| 429 | } else if (printee->IsByteArray()) { |
| 430 | PrintByteArray(ByteArray::cast(printee)); |
| 431 | } else if (printee->IsFixedArray()) { |
| 432 | unsigned int limit = FixedArray::cast(printee)->length(); |
| 433 | PrintFixedArray(FixedArray::cast(printee), limit); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | |
| 439 | void StringStream::PrintSecurityTokenIfChanged(Object* f) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 440 | if (!f->IsHeapObject()) return; |
| 441 | HeapObject* obj = HeapObject::cast(f); |
| 442 | Isolate* isolate = obj->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 443 | Heap* heap = isolate->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 444 | if (!heap->Contains(obj)) return; |
| 445 | Map* map = obj->map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | if (!map->IsHeapObject() || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 447 | !heap->Contains(map) || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | !map->IsMap() || |
| 449 | !f->IsJSFunction()) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | JSFunction* fun = JSFunction::cast(f); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 454 | Object* perhaps_context = fun->context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 455 | if (perhaps_context->IsHeapObject() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 456 | heap->Contains(HeapObject::cast(perhaps_context)) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 457 | perhaps_context->IsContext()) { |
| 458 | Context* context = fun->context(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 459 | if (!heap->Contains(context)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | Add("(Function context is outside heap)\n"); |
| 461 | return; |
| 462 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 463 | Object* token = context->native_context()->security_token(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 464 | if (token != isolate->string_stream_current_security_token()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | Add("Security context: %o\n", token); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 466 | isolate->set_string_stream_current_security_token(token); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | } |
| 468 | } else { |
| 469 | Add("(Function context is corrupt)\n"); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 475 | if (!f->IsHeapObject()) { |
| 476 | Add("/* warning: 'function' was not a heap object */ "); |
| 477 | return; |
| 478 | } |
| 479 | Heap* heap = HeapObject::cast(f)->GetHeap(); |
| 480 | if (!heap->Contains(HeapObject::cast(f))) { |
| 481 | Add("/* warning: 'function' was not on the heap */ "); |
| 482 | return; |
| 483 | } |
| 484 | if (!heap->Contains(HeapObject::cast(f)->map())) { |
| 485 | Add("/* warning: function's map was not on the heap */ "); |
| 486 | return; |
| 487 | } |
| 488 | if (!HeapObject::cast(f)->map()->IsMap()) { |
| 489 | Add("/* warning: function's map was not a valid map */ "); |
| 490 | return; |
| 491 | } |
| 492 | if (f->IsJSFunction()) { |
| 493 | JSFunction* fun = JSFunction::cast(f); |
| 494 | // Common case: on-stack function present and resolved. |
| 495 | PrintPrototype(fun, receiver); |
| 496 | *code = fun->code(); |
| 497 | } else if (f->IsInternalizedString()) { |
| 498 | // Unresolved and megamorphic calls: Instead of the function |
| 499 | // we have the function name on the stack. |
| 500 | PrintName(f); |
| 501 | Add("/* unresolved */ "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 503 | // Unless this is the frame of a built-in function, we should always have |
| 504 | // the callee function or name on the stack. If we don't, we have a |
| 505 | // problem or a change of the stack frame layout. |
| 506 | Add("%o", f); |
| 507 | Add("/* warning: no JSFunction object or function name found */ "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | |
| 512 | void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) { |
| 513 | Object* name = fun->shared()->name(); |
| 514 | bool print_name = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 515 | Isolate* isolate = fun->GetIsolate(); |
| 516 | for (PrototypeIterator iter(isolate, receiver, |
| 517 | PrototypeIterator::START_AT_RECEIVER); |
| 518 | !iter.IsAtEnd(); iter.Advance()) { |
| 519 | if (iter.GetCurrent()->IsJSObject()) { |
| 520 | Object* key = JSObject::cast(iter.GetCurrent())->SlowReverseLookup(fun); |
| 521 | if (key != isolate->heap()->undefined_value()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | if (!name->IsString() || |
| 523 | !key->IsString() || |
| 524 | !String::cast(name)->Equals(String::cast(key))) { |
| 525 | print_name = true; |
| 526 | } |
| 527 | if (name->IsString() && String::cast(name)->length() == 0) { |
| 528 | print_name = false; |
| 529 | } |
| 530 | name = key; |
| 531 | } |
| 532 | } else { |
| 533 | print_name = true; |
| 534 | } |
| 535 | } |
| 536 | PrintName(name); |
| 537 | // Also known as - if the name in the function doesn't match the name under |
| 538 | // which it was looked up. |
| 539 | if (print_name) { |
| 540 | Add("(aka "); |
| 541 | PrintName(fun->shared()->name()); |
| 542 | Put(')'); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | |
| 547 | char* HeapStringAllocator::grow(unsigned* bytes) { |
| 548 | unsigned new_bytes = *bytes * 2; |
| 549 | // Check for overflow. |
| 550 | if (new_bytes <= *bytes) { |
| 551 | return space_; |
| 552 | } |
| 553 | char* new_space = NewArray<char>(new_bytes); |
| 554 | if (new_space == NULL) { |
| 555 | return space_; |
| 556 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | MemCopy(new_space, space_, *bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | *bytes = new_bytes; |
| 559 | DeleteArray(space_); |
| 560 | space_ = new_space; |
| 561 | return new_space; |
| 562 | } |
| 563 | |
| 564 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 565 | } } // namespace v8::internal |