svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1 | // Copyright 2012 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()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 197 | HeapObject* ho = HeapObject::cast(o); |
| 198 | DebugObjectCache* debug_object_cache = ho->GetIsolate()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 199 | string_stream_debug_object_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 201 | if ((*debug_object_cache)[i] == o) { |
| 202 | Add("#%d#", i); |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) { |
| 207 | Add("#%d#", debug_object_cache->length()); |
| 208 | debug_object_cache->Add(HeapObject::cast(o)); |
| 209 | } else { |
| 210 | Add("@%p", o); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void StringStream::Add(const char* format) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 217 | Add(CStrVector(format)); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | void StringStream::Add(Vector<const char> format) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | Add(format, Vector<FmtElm>::empty()); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void StringStream::Add(const char* format, FmtElm arg0) { |
| 227 | const char argc = 1; |
| 228 | FmtElm argv[argc] = { arg0 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 229 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
| 233 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) { |
| 234 | const char argc = 2; |
| 235 | FmtElm argv[argc] = { arg0, arg1 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 236 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 241 | FmtElm arg2) { |
| 242 | const char argc = 3; |
| 243 | FmtElm argv[argc] = { arg0, arg1, arg2 }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 244 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 249 | FmtElm arg2, FmtElm arg3) { |
| 250 | const char argc = 4; |
| 251 | FmtElm argv[argc] = { arg0, arg1, arg2, arg3 }; |
| 252 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 256 | void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1, |
| 257 | FmtElm arg2, FmtElm arg3, FmtElm arg4) { |
| 258 | const char argc = 5; |
| 259 | FmtElm argv[argc] = { arg0, arg1, arg2, arg3, arg4 }; |
| 260 | Add(CStrVector(format), Vector<FmtElm>(argv, argc)); |
| 261 | } |
| 262 | |
| 263 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 264 | SmartArrayPointer<const char> StringStream::ToCString() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | char* str = NewArray<char>(length_ + 1); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 266 | OS::MemCopy(str, buffer_, length_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | str[length_] = '\0'; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 268 | return SmartArrayPointer<const char>(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 272 | void StringStream::Log(Isolate* isolate) { |
| 273 | LOG(isolate, StringEvent("StackDump", buffer_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 277 | void StringStream::OutputToFile(FILE* out) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | // Dump the output to stdout, but make sure to break it up into |
| 279 | // manageable chunks to avoid losing parts of the output in the OS |
| 280 | // printing code. This is a problem on Windows in particular; see |
| 281 | // the VPrint() function implementations in platform-win32.cc. |
| 282 | unsigned position = 0; |
| 283 | for (unsigned next; (next = position + 2048) < length_; position = next) { |
| 284 | char save = buffer_[next]; |
| 285 | buffer_[next] = '\0'; |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 286 | internal::PrintF(out, "%s", &buffer_[position]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | buffer_[next] = save; |
| 288 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 289 | internal::PrintF(out, "%s", &buffer_[position]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 293 | Handle<String> StringStream::ToString(Isolate* isolate) { |
| 294 | return isolate->factory()->NewStringFromUtf8( |
| 295 | Vector<const char>(buffer_, length_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 299 | void StringStream::ClearMentionedObjectCache(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 300 | isolate->set_string_stream_current_security_token(NULL); |
| 301 | if (isolate->string_stream_debug_object_cache() == NULL) { |
| 302 | isolate->set_string_stream_debug_object_cache( |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 303 | new List<HeapObject*, PreallocatedStorageAllocationPolicy>(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 305 | isolate->string_stream_debug_object_cache()->Clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | #ifdef DEBUG |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 310 | bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) { |
| 311 | return isolate->string_stream_debug_object_cache()->length() == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | } |
| 313 | #endif |
| 314 | |
| 315 | |
| 316 | bool StringStream::Put(String* str) { |
| 317 | return Put(str, 0, str->length()); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | bool StringStream::Put(String* str, int start, int end) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 322 | ConsStringIteratorOp op; |
| 323 | StringCharacterStream stream(str, &op, start); |
| 324 | for (int i = start; i < end && stream.HasMore(); i++) { |
| 325 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | if (c >= 127 || c < 32) { |
| 327 | c = '?'; |
| 328 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 329 | if (!Put(static_cast<char>(c))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | return false; // Output was truncated. |
| 331 | } |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void StringStream::PrintName(Object* name) { |
| 338 | if (name->IsString()) { |
| 339 | String* str = String::cast(name); |
| 340 | if (str->length() > 0) { |
| 341 | Put(str); |
| 342 | } else { |
| 343 | Add("/* anonymous */"); |
| 344 | } |
| 345 | } else { |
| 346 | Add("%o", name); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | |
| 351 | void StringStream::PrintUsingMap(JSObject* js_object) { |
| 352 | Map* map = js_object->map(); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 353 | if (!js_object->GetHeap()->Contains(map) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | !map->IsHeapObject() || |
| 355 | !map->IsMap()) { |
| 356 | Add("<Invalid map>\n"); |
| 357 | return; |
| 358 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 359 | int real_size = map->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 360 | DescriptorArray* descs = map->instance_descriptors(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 361 | for (int i = 0; i < real_size; i++) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 362 | PropertyDetails details = descs->GetDetails(i); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 363 | if (details.type() == FIELD) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 364 | Object* key = descs->GetKey(i); |
| 365 | if (key->IsString() || key->IsNumber()) { |
| 366 | int len = 3; |
| 367 | if (key->IsString()) { |
| 368 | len = String::cast(key)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 370 | for (; len < 18; len++) |
| 371 | Put(' '); |
| 372 | if (key->IsString()) { |
| 373 | Put(String::cast(key)); |
| 374 | } else { |
| 375 | key->ShortPrint(); |
| 376 | } |
| 377 | Add(": "); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 378 | Object* value = js_object->RawFastPropertyAt(descs->GetFieldIndex(i)); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 379 | Add("%o\n", value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 387 | Heap* heap = array->GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 388 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 389 | Object* element = array->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 390 | if (element != heap->the_hole_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | for (int len = 1; len < 18; len++) |
| 392 | Put(' '); |
| 393 | Add("%d: %o\n", i, array->get(i)); |
| 394 | } |
| 395 | } |
| 396 | if (limit >= 10) { |
| 397 | Add(" ...\n"); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void StringStream::PrintByteArray(ByteArray* byte_array) { |
| 403 | unsigned int limit = byte_array->length(); |
| 404 | for (unsigned int i = 0; i < 10 && i < limit; i++) { |
| 405 | byte b = byte_array->get(i); |
| 406 | Add(" %d: %3d 0x%02x", i, b, b); |
| 407 | if (b >= ' ' && b <= '~') { |
| 408 | Add(" '%c'", b); |
| 409 | } else if (b == '\n') { |
| 410 | Add(" '\n'"); |
| 411 | } else if (b == '\r') { |
| 412 | Add(" '\r'"); |
| 413 | } else if (b >= 1 && b <= 26) { |
| 414 | Add(" ^%c", b + 'A' - 1); |
| 415 | } |
| 416 | Add("\n"); |
| 417 | } |
| 418 | if (limit >= 10) { |
| 419 | Add(" ...\n"); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 424 | void StringStream::PrintMentionedObjectCache(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 425 | DebugObjectCache* debug_object_cache = |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 426 | isolate->string_stream_debug_object_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 427 | Add("==== Key ============================================\n\n"); |
| 428 | for (int i = 0; i < debug_object_cache->length(); i++) { |
| 429 | HeapObject* printee = (*debug_object_cache)[i]; |
| 430 | Add(" #%d# %p: ", i, printee); |
| 431 | printee->ShortPrint(this); |
| 432 | Add("\n"); |
| 433 | if (printee->IsJSObject()) { |
| 434 | if (printee->IsJSValue()) { |
| 435 | Add(" value(): %o\n", JSValue::cast(printee)->value()); |
| 436 | } |
| 437 | PrintUsingMap(JSObject::cast(printee)); |
| 438 | if (printee->IsJSArray()) { |
| 439 | JSArray* array = JSArray::cast(printee); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 440 | if (array->HasFastObjectElements()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | unsigned int limit = FixedArray::cast(array->elements())->length(); |
| 442 | unsigned int length = |
| 443 | static_cast<uint32_t>(JSArray::cast(array)->length()->Number()); |
| 444 | if (length < limit) limit = length; |
| 445 | PrintFixedArray(FixedArray::cast(array->elements()), limit); |
| 446 | } |
| 447 | } |
| 448 | } else if (printee->IsByteArray()) { |
| 449 | PrintByteArray(ByteArray::cast(printee)); |
| 450 | } else if (printee->IsFixedArray()) { |
| 451 | unsigned int limit = FixedArray::cast(printee)->length(); |
| 452 | PrintFixedArray(FixedArray::cast(printee), limit); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | void StringStream::PrintSecurityTokenIfChanged(Object* f) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 459 | if (!f->IsHeapObject()) return; |
| 460 | HeapObject* obj = HeapObject::cast(f); |
| 461 | Isolate* isolate = obj->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 462 | Heap* heap = isolate->heap(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 463 | if (!heap->Contains(obj)) return; |
| 464 | Map* map = obj->map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | if (!map->IsHeapObject() || |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 466 | !heap->Contains(map) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | !map->IsMap() || |
| 468 | !f->IsJSFunction()) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | JSFunction* fun = JSFunction::cast(f); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 473 | Object* perhaps_context = fun->context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | if (perhaps_context->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 475 | heap->Contains(HeapObject::cast(perhaps_context)) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 476 | perhaps_context->IsContext()) { |
| 477 | Context* context = fun->context(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 478 | if (!heap->Contains(context)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | Add("(Function context is outside heap)\n"); |
| 480 | return; |
| 481 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 482 | Object* token = context->native_context()->security_token(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 483 | if (token != isolate->string_stream_current_security_token()) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 484 | Add("Security context: %o\n", token); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 485 | isolate->set_string_stream_current_security_token(token); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | } |
| 487 | } else { |
| 488 | Add("(Function context is corrupt)\n"); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 494 | if (!f->IsHeapObject()) { |
| 495 | Add("/* warning: 'function' was not a heap object */ "); |
| 496 | return; |
| 497 | } |
| 498 | Heap* heap = HeapObject::cast(f)->GetHeap(); |
| 499 | if (!heap->Contains(HeapObject::cast(f))) { |
| 500 | Add("/* warning: 'function' was not on the heap */ "); |
| 501 | return; |
| 502 | } |
| 503 | if (!heap->Contains(HeapObject::cast(f)->map())) { |
| 504 | Add("/* warning: function's map was not on the heap */ "); |
| 505 | return; |
| 506 | } |
| 507 | if (!HeapObject::cast(f)->map()->IsMap()) { |
| 508 | Add("/* warning: function's map was not a valid map */ "); |
| 509 | return; |
| 510 | } |
| 511 | if (f->IsJSFunction()) { |
| 512 | JSFunction* fun = JSFunction::cast(f); |
| 513 | // Common case: on-stack function present and resolved. |
| 514 | PrintPrototype(fun, receiver); |
| 515 | *code = fun->code(); |
| 516 | } else if (f->IsInternalizedString()) { |
| 517 | // Unresolved and megamorphic calls: Instead of the function |
| 518 | // we have the function name on the stack. |
| 519 | PrintName(f); |
| 520 | Add("/* unresolved */ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | } else { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 522 | // Unless this is the frame of a built-in function, we should always have |
| 523 | // the callee function or name on the stack. If we don't, we have a |
| 524 | // problem or a change of the stack frame layout. |
| 525 | Add("%o", f); |
| 526 | Add("/* warning: no JSFunction object or function name found */ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) { |
| 532 | Object* name = fun->shared()->name(); |
| 533 | bool print_name = false; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 534 | Isolate* isolate = fun->GetIsolate(); |
| 535 | for (Object* p = receiver; |
| 536 | p != isolate->heap()->null_value(); |
| 537 | p = p->GetPrototype(isolate)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 538 | if (p->IsJSObject()) { |
| 539 | Object* key = JSObject::cast(p)->SlowReverseLookup(fun); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 540 | if (key != isolate->heap()->undefined_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | if (!name->IsString() || |
| 542 | !key->IsString() || |
| 543 | !String::cast(name)->Equals(String::cast(key))) { |
| 544 | print_name = true; |
| 545 | } |
| 546 | if (name->IsString() && String::cast(name)->length() == 0) { |
| 547 | print_name = false; |
| 548 | } |
| 549 | name = key; |
| 550 | } |
| 551 | } else { |
| 552 | print_name = true; |
| 553 | } |
| 554 | } |
| 555 | PrintName(name); |
| 556 | // Also known as - if the name in the function doesn't match the name under |
| 557 | // which it was looked up. |
| 558 | if (print_name) { |
| 559 | Add("(aka "); |
| 560 | PrintName(fun->shared()->name()); |
| 561 | Put(')'); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | char* HeapStringAllocator::grow(unsigned* bytes) { |
| 567 | unsigned new_bytes = *bytes * 2; |
| 568 | // Check for overflow. |
| 569 | if (new_bytes <= *bytes) { |
| 570 | return space_; |
| 571 | } |
| 572 | char* new_space = NewArray<char>(new_bytes); |
| 573 | if (new_space == NULL) { |
| 574 | return space_; |
| 575 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 576 | OS::MemCopy(new_space, space_, *bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | *bytes = new_bytes; |
| 578 | DeleteArray(space_); |
| 579 | space_ = new_space; |
| 580 | return new_space; |
| 581 | } |
| 582 | |
| 583 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 584 | // Only grow once to the maximum allowable size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | char* NoAllocationStringAllocator::grow(unsigned* bytes) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 586 | ASSERT(size_ >= *bytes); |
| 587 | *bytes = size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | return space_; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | } } // namespace v8::internal |