whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "factory.h" |
| 31 | #include "string-stream.h" |
| 32 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 33 | #include "allocation-inl.h" |
| 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
| 38 | static const int kMentionedObjectCacheMaxSize = 256; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
| 40 | char* HeapStringAllocator::allocate(unsigned bytes) { |
| 41 | space_ = NewArray<char>(bytes); |
| 42 | return space_; |
| 43 | } |
| 44 | |
| 45 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 46 | NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory, |
| 47 | unsigned size) { |
| 48 | size_ = size; |
| 49 | space_ = memory; |
| 50 | } |
| 51 | |
| 52 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | bool StringStream::Put(char c) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 54 | if (full()) return false; |
| 55 | ASSERT(length_ < capacity_); |
| 56 | // Since the trailing '\0' is not accounted for in length_ fullness is |
| 57 | // indicated by a difference of 1 between length_ and capacity_. Thus when |
| 58 | // reaching a difference of 2 we need to grow the buffer. |
| 59 | if (length_ == capacity_ - 2) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | unsigned new_capacity = capacity_; |
| 61 | char* new_buffer = allocator_->grow(&new_capacity); |
| 62 | if (new_capacity > capacity_) { |
| 63 | capacity_ = new_capacity; |
| 64 | buffer_ = new_buffer; |
| 65 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 66 | // Reached the end of the available buffer. |
| 67 | ASSERT(capacity_ >= 5); |
| 68 | length_ = capacity_ - 1; // Indicate fullness of the stream. |
| 69 | buffer_[length_ - 4] = '.'; |
| 70 | buffer_[length_ - 3] = '.'; |
| 71 | buffer_[length_ - 2] = '.'; |
| 72 | buffer_[length_ - 1] = '\n'; |
| 73 | buffer_[length_] = '\0'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | buffer_[length_] = c; |
| 78 | buffer_[length_ + 1] = '\0'; |
| 79 | length_++; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | // A control character is one that configures a format element. For |
| 85 | // instance, in %.5s, .5 are control characters. |
| 86 | static bool IsControlChar(char c) { |
| 87 | switch (c) { |
| 88 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 89 | case '6': case '7': case '8': case '9': case '.': case '-': |
| 90 | return true; |
| 91 | default: |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 97 | void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | // If we already ran out of space then return immediately. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 99 | if (full()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | int offset = 0; |
| 101 | int elm = 0; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 102 | while (offset < format.length()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | if (format[offset] != '%' || elm == elms.length()) { |
| 104 | Put(format[offset]); |
| 105 | offset++; |
| 106 | continue; |
| 107 | } |
| 108 | // Read this formatting directive into a temporary buffer |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 109 | EmbeddedVector<char, 24> temp; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | int format_length = 0; |
| 111 | // Skip over the whole control character sequence until the |
| 112 | // format element type |
| 113 | temp[format_length++] = format[offset++]; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 114 | while (offset < format.length() && IsControlChar(format[offset])) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | temp[format_length++] = format[offset++]; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 116 | if (offset >= format.length()) |
| 117 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 118 | char type = format[offset]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | temp[format_length++] = type; |
| 120 | temp[format_length] = '\0'; |
| 121 | offset++; |
| 122 | FmtElm current = elms[elm++]; |
| 123 | switch (type) { |
| 124 | case 's': { |
| 125 | ASSERT_EQ(FmtElm::C_STR, current.type_); |
| 126 | const char* value = current.data_.u_c_str_; |
| 127 | Add(value); |
| 128 | break; |
| 129 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 130 | case 'w': { |
| 131 | ASSERT_EQ(FmtElm::LC_STR, current.type_); |
| 132 | Vector<const uc16> value = *current.data_.u_lc_str_; |
| 133 | for (int i = 0; i < value.length(); i++) |
| 134 | Put(static_cast<char>(value[i])); |
| 135 | break; |
| 136 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | case 'o': { |
| 138 | ASSERT_EQ(FmtElm::OBJ, current.type_); |
| 139 | Object* obj = current.data_.u_obj_; |
| 140 | PrintObject(obj); |
| 141 | break; |
| 142 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 143 | case 'k': { |
| 144 | ASSERT_EQ(FmtElm::INT, current.type_); |
| 145 | int value = current.data_.u_int_; |
| 146 | if (0x20 <= value && value <= 0x7F) { |
| 147 | Put(value); |
| 148 | } else if (value <= 0xff) { |
| 149 | Add("\\x%02x", value); |
| 150 | } else { |
| 151 | Add("\\u%04x", value); |
| 152 | } |
| 153 | break; |
| 154 | } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 155 | case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | int value = current.data_.u_int_; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 157 | EmbeddedVector<char, 24> formatted; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 158 | int length = OS::SNPrintF(formatted, temp.start(), value); |
| 159 | Add(Vector<const char>(formatted.start(), length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 162 | case 'f': case 'g': case 'G': case 'e': case 'E': { |
| 163 | double value = current.data_.u_double_; |
| 164 | EmbeddedVector<char, 28> formatted; |
| 165 | OS::SNPrintF(formatted, temp.start(), value); |
| 166 | Add(formatted.start()); |
| 167 | break; |
| 168 | } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 169 | case 'p': { |
| 170 | void* value = current.data_.u_pointer_; |
| 171 | EmbeddedVector<char, 20> formatted; |
| 172 | OS::SNPrintF(formatted, temp.start(), value); |
| 173 | Add(formatted.start()); |
| 174 | break; |
| 175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | default: |
| 177 | UNREACHABLE(); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 182 | // Verify that the buffer is 0-terminated |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | ASSERT(buffer_[length_] == '\0'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | void StringStream::PrintObject(Object* o) { |
| 188 | o->ShortPrint(this); |
| 189 | if (o->IsString()) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 190 | if (String::cast(o)->length() <= String::kMaxShortPrintLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | } else if (o->IsNumber() || o->IsOddball()) { |
| 194 | return; |
| 195 | } |
| 196 | if (o->IsHeapObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 197 | DebugObjectCache* debug_object_cache = Isolate::Current()-> |
| 198 | string_stream_debug_object_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 200 | if ((*debug_object_cache)[i] == o) { |
| 201 | Add("#%d#", i); |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) { |
| 206 | Add("#%d#", debug_object_cache->length()); |
| 207 | debug_object_cache->Add(HeapObject::cast(o)); |
| 208 | } else { |
| 209 | Add("@%p", o); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void StringStream::Add(const char* format) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 216 | Add(CStrVector(format)); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void StringStream::Add(Vector<const char> format) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | Add(format, Vector<FmtElm>::empty()); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void StringStream::Add(const char* format, FmtElm arg0) { |
| 226 | const char argc = 1; |
| 227 | FmtElm argv[argc] = { arg0 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 228 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) { |
| 233 | const char argc = 2; |
| 234 | FmtElm argv[argc] = { arg0, arg1 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 235 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 240 | FmtElm arg2) { |
| 241 | const char argc = 3; |
| 242 | FmtElm argv[argc] = { arg0, arg1, arg2 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 243 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 248 | FmtElm arg2, FmtElm arg3) { |
| 249 | const char argc = 4; |
| 250 | FmtElm argv[argc] = { arg0, arg1, arg2, arg3 }; |
| 251 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 255 | SmartPointer<const char> StringStream::ToCString() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | char* str = NewArray<char>(length_ + 1); |
| 257 | memcpy(str, buffer_, length_); |
| 258 | str[length_] = '\0'; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 259 | return SmartPointer<const char>(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | void StringStream::Log() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 264 | LOG(ISOLATE, StringEvent("StackDump", buffer_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 268 | void StringStream::OutputToFile(FILE* out) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | // Dump the output to stdout, but make sure to break it up into |
| 270 | // manageable chunks to avoid losing parts of the output in the OS |
| 271 | // printing code. This is a problem on Windows in particular; see |
| 272 | // the VPrint() function implementations in platform-win32.cc. |
| 273 | unsigned position = 0; |
| 274 | for (unsigned next; (next = position + 2048) < length_; position = next) { |
| 275 | char save = buffer_[next]; |
| 276 | buffer_[next] = '\0'; |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 277 | internal::PrintF(out, "%s", &buffer_[position]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | buffer_[next] = save; |
| 279 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 280 | internal::PrintF(out, "%s", &buffer_[position]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | Handle<String> StringStream::ToString() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 285 | return FACTORY->NewStringFromUtf8(Vector<const char>(buffer_, length_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | void StringStream::ClearMentionedObjectCache() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 290 | Isolate* isolate = Isolate::Current(); |
| 291 | isolate->set_string_stream_current_security_token(NULL); |
| 292 | if (isolate->string_stream_debug_object_cache() == NULL) { |
| 293 | isolate->set_string_stream_debug_object_cache( |
| 294 | new List<HeapObject*, PreallocatedStorage>(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 296 | isolate->string_stream_debug_object_cache()->Clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
| 300 | #ifdef DEBUG |
| 301 | bool StringStream::IsMentionedObjectCacheClear() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 302 | return ( |
| 303 | Isolate::Current()->string_stream_debug_object_cache()->length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | } |
| 305 | #endif |
| 306 | |
| 307 | |
| 308 | bool StringStream::Put(String* str) { |
| 309 | return Put(str, 0, str->length()); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | bool StringStream::Put(String* str, int start, int end) { |
| 314 | StringInputBuffer name_buffer(str); |
| 315 | name_buffer.Seek(start); |
| 316 | for (int i = start; i < end && name_buffer.has_more(); i++) { |
| 317 | int c = name_buffer.GetNext(); |
| 318 | if (c >= 127 || c < 32) { |
| 319 | c = '?'; |
| 320 | } |
| 321 | if (!Put(c)) { |
| 322 | return false; // Output was truncated. |
| 323 | } |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | void StringStream::PrintName(Object* name) { |
| 330 | if (name->IsString()) { |
| 331 | String* str = String::cast(name); |
| 332 | if (str->length() > 0) { |
| 333 | Put(str); |
| 334 | } else { |
| 335 | Add("/* anonymous */"); |
| 336 | } |
| 337 | } else { |
| 338 | Add("%o", name); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | void StringStream::PrintUsingMap(JSObject* js_object) { |
| 344 | Map* map = js_object->map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 345 | if (!HEAP->Contains(map) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | !map->IsHeapObject() || |
| 347 | !map->IsMap()) { |
| 348 | Add("<Invalid map>\n"); |
| 349 | return; |
| 350 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 351 | DescriptorArray* descs = map->instance_descriptors(); |
| 352 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 353 | switch (descs->GetType(i)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | case FIELD: { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 355 | Object* key = descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | if (key->IsString() || key->IsNumber()) { |
| 357 | int len = 3; |
| 358 | if (key->IsString()) { |
| 359 | len = String::cast(key)->length(); |
| 360 | } |
| 361 | for (; len < 18; len++) |
| 362 | Put(' '); |
| 363 | if (key->IsString()) { |
| 364 | Put(String::cast(key)); |
| 365 | } else { |
| 366 | key->ShortPrint(); |
| 367 | } |
| 368 | Add(": "); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 369 | Object* value = js_object->FastPropertyAt(descs->GetFieldIndex(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | Add("%o\n", value); |
| 371 | } |
| 372 | } |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | |
| 381 | void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 382 | Heap* heap = HEAP; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 384 | Object* element = array->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 385 | if (element != heap->the_hole_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | for (int len = 1; len < 18; len++) |
| 387 | Put(' '); |
| 388 | Add("%d: %o\n", i, array->get(i)); |
| 389 | } |
| 390 | } |
| 391 | if (limit >= 10) { |
| 392 | Add(" ...\n"); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | void StringStream::PrintByteArray(ByteArray* byte_array) { |
| 398 | unsigned int limit = byte_array->length(); |
| 399 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 400 | byte b = byte_array->get(i); |
| 401 | Add(" %d: %3d 0x%02x", i, b, b); |
| 402 | if (b >= ' ' && b <= '~') { |
| 403 | Add(" '%c'", b); |
| 404 | } else if (b == '\n') { |
| 405 | Add(" '\n'"); |
| 406 | } else if (b == '\r') { |
| 407 | Add(" '\r'"); |
| 408 | } else if (b >= 1 && b <= 26) { |
| 409 | Add(" ^%c", b + 'A' - 1); |
| 410 | } |
| 411 | Add("\n"); |
| 412 | } |
| 413 | if (limit >= 10) { |
| 414 | Add(" ...\n"); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
| 419 | void StringStream::PrintMentionedObjectCache() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 420 | DebugObjectCache* debug_object_cache = |
| 421 | Isolate::Current()->string_stream_debug_object_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 422 | Add("==== Key ============================================\n\n"); |
| 423 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 424 | HeapObject* printee = (*debug_object_cache)[i]; |
| 425 | Add(" #%d# %p: ", i, printee); |
| 426 | printee->ShortPrint(this); |
| 427 | Add("\n"); |
| 428 | if (printee->IsJSObject()) { |
| 429 | if (printee->IsJSValue()) { |
| 430 | Add(" value(): %o\n", JSValue::cast(printee)->value()); |
| 431 | } |
| 432 | PrintUsingMap(JSObject::cast(printee)); |
| 433 | if (printee->IsJSArray()) { |
| 434 | JSArray* array = JSArray::cast(printee); |
| 435 | if (array->HasFastElements()) { |
| 436 | unsigned int limit = FixedArray::cast(array->elements())->length(); |
| 437 | unsigned int length = |
| 438 | static_cast<uint32_t>(JSArray::cast(array)->length()->Number()); |
| 439 | if (length < limit) limit = length; |
| 440 | PrintFixedArray(FixedArray::cast(array->elements()), limit); |
| 441 | } |
| 442 | } |
| 443 | } else if (printee->IsByteArray()) { |
| 444 | PrintByteArray(ByteArray::cast(printee)); |
| 445 | } else if (printee->IsFixedArray()) { |
| 446 | unsigned int limit = FixedArray::cast(printee)->length(); |
| 447 | PrintFixedArray(FixedArray::cast(printee), limit); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void StringStream::PrintSecurityTokenIfChanged(Object* f) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 454 | Isolate* isolate = Isolate::Current(); |
| 455 | Heap* heap = isolate->heap(); |
| 456 | if (!f->IsHeapObject() || !heap->Contains(HeapObject::cast(f))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | Map* map = HeapObject::cast(f)->map(); |
| 460 | if (!map->IsHeapObject() || |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 461 | !heap->Contains(map) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | !map->IsMap() || |
| 463 | !f->IsJSFunction()) { |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | JSFunction* fun = JSFunction::cast(f); |
| 468 | Object* perhaps_context = fun->unchecked_context(); |
| 469 | if (perhaps_context->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 470 | heap->Contains(HeapObject::cast(perhaps_context)) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 471 | perhaps_context->IsContext()) { |
| 472 | Context* context = fun->context(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 473 | if (!heap->Contains(context)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | Add("(Function context is outside heap)\n"); |
| 475 | return; |
| 476 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 477 | Object* token = context->global_context()->security_token(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 478 | if (token != isolate->string_stream_current_security_token()) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 479 | Add("Security context: %o\n", token); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 480 | isolate->set_string_stream_current_security_token(token); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | } |
| 482 | } else { |
| 483 | Add("(Function context is corrupt)\n"); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
| 488 | void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) { |
| 489 | if (f->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 490 | HEAP->Contains(HeapObject::cast(f)) && |
| 491 | HEAP->Contains(HeapObject::cast(f)->map()) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 492 | HeapObject::cast(f)->map()->IsMap()) { |
| 493 | if (f->IsJSFunction()) { |
| 494 | JSFunction* fun = JSFunction::cast(f); |
| 495 | // Common case: on-stack function present and resolved. |
| 496 | PrintPrototype(fun, receiver); |
| 497 | *code = fun->code(); |
| 498 | } else if (f->IsSymbol()) { |
| 499 | // Unresolved and megamorphic calls: Instead of the function |
| 500 | // we have the function name on the stack. |
| 501 | PrintName(f); |
| 502 | Add("/* unresolved */ "); |
| 503 | } else { |
| 504 | // Unless this is the frame of a built-in function, we should always have |
| 505 | // the callee function or name on the stack. If we don't, we have a |
| 506 | // problem or a change of the stack frame layout. |
| 507 | Add("%o", f); |
| 508 | Add("/* warning: no JSFunction object or function name found */ "); |
| 509 | } |
| 510 | /* } else if (is_trampoline()) { |
| 511 | Print("trampoline "); |
| 512 | */ |
| 513 | } else { |
| 514 | if (!f->IsHeapObject()) { |
| 515 | Add("/* warning: 'function' was not a heap object */ "); |
| 516 | return; |
| 517 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 518 | if (!HEAP->Contains(HeapObject::cast(f))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | Add("/* warning: 'function' was not on the heap */ "); |
| 520 | return; |
| 521 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 522 | if (!HEAP->Contains(HeapObject::cast(f)->map())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | Add("/* warning: function's map was not on the heap */ "); |
| 524 | return; |
| 525 | } |
| 526 | if (!HeapObject::cast(f)->map()->IsMap()) { |
| 527 | Add("/* warning: function's map was not a valid map */ "); |
| 528 | return; |
| 529 | } |
| 530 | Add("/* warning: Invalid JSFunction object found */ "); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
| 535 | void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) { |
| 536 | Object* name = fun->shared()->name(); |
| 537 | bool print_name = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 538 | Heap* heap = HEAP; |
| 539 | for (Object* p = receiver; p != heap->null_value(); p = p->GetPrototype()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | if (p->IsJSObject()) { |
| 541 | Object* key = JSObject::cast(p)->SlowReverseLookup(fun); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 542 | if (key != heap->undefined_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | if (!name->IsString() || |
| 544 | !key->IsString() || |
| 545 | !String::cast(name)->Equals(String::cast(key))) { |
| 546 | print_name = true; |
| 547 | } |
| 548 | if (name->IsString() && String::cast(name)->length() == 0) { |
| 549 | print_name = false; |
| 550 | } |
| 551 | name = key; |
| 552 | } |
| 553 | } else { |
| 554 | print_name = true; |
| 555 | } |
| 556 | } |
| 557 | PrintName(name); |
| 558 | // Also known as - if the name in the function doesn't match the name under |
| 559 | // which it was looked up. |
| 560 | if (print_name) { |
| 561 | Add("(aka "); |
| 562 | PrintName(fun->shared()->name()); |
| 563 | Put(')'); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | |
| 568 | char* HeapStringAllocator::grow(unsigned* bytes) { |
| 569 | unsigned new_bytes = *bytes * 2; |
| 570 | // Check for overflow. |
| 571 | if (new_bytes <= *bytes) { |
| 572 | return space_; |
| 573 | } |
| 574 | char* new_space = NewArray<char>(new_bytes); |
| 575 | if (new_space == NULL) { |
| 576 | return space_; |
| 577 | } |
| 578 | memcpy(new_space, space_, *bytes); |
| 579 | *bytes = new_bytes; |
| 580 | DeleteArray(space_); |
| 581 | space_ = new_space; |
| 582 | return new_space; |
| 583 | } |
| 584 | |
| 585 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 586 | // Only grow once to the maximum allowable size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | char* NoAllocationStringAllocator::grow(unsigned* bytes) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 588 | ASSERT(size_ >= *bytes); |
| 589 | *bytes = size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | return space_; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | } } // namespace v8::internal |