blob: 45b675fa8ba3bdc14df2517460af318c6308339c [file] [log] [blame]
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.org030d38e2011-07-13 13:23:34 +000033#include "allocation-inl.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38static const int kMentionedObjectCacheMaxSize = 256;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
40char* HeapStringAllocator::allocate(unsigned bytes) {
41 space_ = NewArray<char>(bytes);
42 return space_;
43}
44
45
kasper.lundaf4734f2008-07-28 12:50:18 +000046NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory,
47 unsigned size) {
48 size_ = size;
49 space_ = memory;
50}
51
52
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053bool StringStream::Put(char c) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +000054 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.hansen43d26ec2008-07-03 15:10:15 +000060 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.org65dad4b2009-04-23 08:48:43 +000066 // 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.hansen43d26ec2008-07-03 15:10:15 +000074 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.
86static 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.orga74f0da2008-12-03 16:05:52 +000097void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 // If we already ran out of space then return immediately.
ager@chromium.org65dad4b2009-04-23 08:48:43 +000099 if (full()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 int offset = 0;
101 int elm = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000102 while (offset < format.length()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 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.orgb9123622008-09-17 14:05:56 +0000109 EmbeddedVector<char, 24> temp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 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.orga74f0da2008-12-03 16:05:52 +0000114 while (offset < format.length() && IsControlChar(format[offset]))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 temp[format_length++] = format[offset++];
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000116 if (offset >= format.length())
117 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 char type = format[offset];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 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.orga74f0da2008-12-03 16:05:52 +0000130 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.hansen43d26ec2008-07-03 15:10:15 +0000137 case 'o': {
138 ASSERT_EQ(FmtElm::OBJ, current.type_);
139 Object* obj = current.data_.u_obj_;
140 PrintObject(obj);
141 break;
142 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000143 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.orgb9d7da12009-08-05 08:38:10 +0000155 case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 int value = current.data_.u_int_;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000157 EmbeddedVector<char, 24> formatted;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000158 int length = OS::SNPrintF(formatted, temp.start(), value);
159 Add(Vector<const char>(formatted.start(), length));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 break;
161 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000162 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.orgb9d7da12009-08-05 08:38:10 +0000169 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.hansen43d26ec2008-07-03 15:10:15 +0000176 default:
177 UNREACHABLE();
178 break;
179 }
180 }
181
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000182 // Verify that the buffer is 0-terminated
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 ASSERT(buffer_[length_] == '\0');
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184}
185
186
187void StringStream::PrintObject(Object* o) {
188 o->ShortPrint(this);
189 if (o->IsString()) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000190 if (String::cast(o)->length() <= String::kMaxShortPrintLength) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 return;
192 }
193 } else if (o->IsNumber() || o->IsOddball()) {
194 return;
195 }
196 if (o->IsHeapObject()) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000197 HeapObject* ho = HeapObject::cast(o);
198 DebugObjectCache* debug_object_cache = ho->GetIsolate()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 string_stream_debug_object_cache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 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
216void StringStream::Add(const char* format) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000217 Add(CStrVector(format));
218}
219
220
221void StringStream::Add(Vector<const char> format) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 Add(format, Vector<FmtElm>::empty());
223}
224
225
226void StringStream::Add(const char* format, FmtElm arg0) {
227 const char argc = 1;
228 FmtElm argv[argc] = { arg0 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000229 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230}
231
232
233void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) {
234 const char argc = 2;
235 FmtElm argv[argc] = { arg0, arg1 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000236 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237}
238
239
240void 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.orga74f0da2008-12-03 16:05:52 +0000244 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
245}
246
247
248void 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.hansen43d26ec2008-07-03 15:10:15 +0000253}
254
255
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000256void 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.org83e16822011-09-13 08:21:47 +0000264SmartArrayPointer<const char> StringStream::ToCString() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 char* str = NewArray<char>(length_ + 1);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000266 OS::MemCopy(str, buffer_, length_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 str[length_] = '\0';
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000268 return SmartArrayPointer<const char>(str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269}
270
271
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000272void StringStream::Log(Isolate* isolate) {
273 LOG(isolate, StringEvent("StackDump", buffer_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274}
275
276
whesse@chromium.org023421e2010-12-21 12:19:12 +0000277void StringStream::OutputToFile(FILE* out) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 // 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.org023421e2010-12-21 12:19:12 +0000286 internal::PrintF(out, "%s", &buffer_[position]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 buffer_[next] = save;
288 }
whesse@chromium.org023421e2010-12-21 12:19:12 +0000289 internal::PrintF(out, "%s", &buffer_[position]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290}
291
292
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000293Handle<String> StringStream::ToString(Isolate* isolate) {
294 return isolate->factory()->NewStringFromUtf8(
295 Vector<const char>(buffer_, length_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296}
297
298
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000299void StringStream::ClearMentionedObjectCache(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 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.org400388e2012-06-06 09:29:22 +0000303 new List<HeapObject*, PreallocatedStorageAllocationPolicy>(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 isolate->string_stream_debug_object_cache()->Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306}
307
308
309#ifdef DEBUG
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000310bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) {
311 return isolate->string_stream_debug_object_cache()->length() == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312}
313#endif
314
315
316bool StringStream::Put(String* str) {
317 return Put(str, 0, str->length());
318}
319
320
321bool StringStream::Put(String* str, int start, int end) {
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000322 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.hansen43d26ec2008-07-03 15:10:15 +0000326 if (c >= 127 || c < 32) {
327 c = '?';
328 }
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000329 if (!Put(static_cast<char>(c))) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 return false; // Output was truncated.
331 }
332 }
333 return true;
334}
335
336
337void 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
351void StringStream::PrintUsingMap(JSObject* js_object) {
352 Map* map = js_object->map();
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000353 if (!js_object->GetHeap()->Contains(map) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 !map->IsHeapObject() ||
355 !map->IsMap()) {
356 Add("<Invalid map>\n");
357 return;
358 }
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000359 int real_size = map->NumberOfOwnDescriptors();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000360 DescriptorArray* descs = map->instance_descriptors();
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000361 for (int i = 0; i < real_size; i++) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000362 PropertyDetails details = descs->GetDetails(i);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000363 if (details.type() == FIELD) {
danno@chromium.orgc612e022011-11-10 11:38:15 +0000364 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.hansen43d26ec2008-07-03 15:10:15 +0000369 }
danno@chromium.orgc612e022011-11-10 11:38:15 +0000370 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.org57ff8812013-05-10 08:16:55 +0000378 Object* value = js_object->RawFastPropertyAt(descs->GetFieldIndex(i));
danno@chromium.orgc612e022011-11-10 11:38:15 +0000379 Add("%o\n", value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 }
382 }
383}
384
385
386void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) {
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000387 Heap* heap = array->GetHeap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 for (unsigned int i = 0; i < 10 && i < limit; i++) {
389 Object* element = array->get(i);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390 if (element != heap->the_hole_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 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
402void 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.org3d00d0a2013-09-04 13:57:32 +0000424void StringStream::PrintMentionedObjectCache(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 DebugObjectCache* debug_object_cache =
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000426 isolate->string_stream_debug_object_cache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 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.org830d30c2012-05-29 13:20:14 +0000440 if (array->HasFastObjectElements()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 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
458void StringStream::PrintSecurityTokenIfChanged(Object* f) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000459 if (!f->IsHeapObject()) return;
460 HeapObject* obj = HeapObject::cast(f);
461 Isolate* isolate = obj->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 Heap* heap = isolate->heap();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000463 if (!heap->Contains(obj)) return;
464 Map* map = obj->map();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465 if (!map->IsHeapObject() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 !heap->Contains(map) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 !map->IsMap() ||
468 !f->IsJSFunction()) {
469 return;
470 }
471
472 JSFunction* fun = JSFunction::cast(f);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000473 Object* perhaps_context = fun->context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 if (perhaps_context->IsHeapObject() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000475 heap->Contains(HeapObject::cast(perhaps_context)) &&
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 perhaps_context->IsContext()) {
477 Context* context = fun->context();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000478 if (!heap->Contains(context)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 Add("(Function context is outside heap)\n");
480 return;
481 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000482 Object* token = context->native_context()->security_token();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000483 if (token != isolate->string_stream_current_security_token()) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000484 Add("Security context: %o\n", token);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000485 isolate->set_string_stream_current_security_token(token);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 }
487 } else {
488 Add("(Function context is corrupt)\n");
489 }
490}
491
492
493void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) {
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000494 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.hansen43d26ec2008-07-03 15:10:15 +0000521 } else {
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000522 // 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.hansen43d26ec2008-07-03 15:10:15 +0000527 }
528}
529
530
531void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) {
532 Object* name = fun->shared()->name();
533 bool print_name = false;
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000534 Isolate* isolate = fun->GetIsolate();
535 for (Object* p = receiver;
536 p != isolate->heap()->null_value();
537 p = p->GetPrototype(isolate)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 if (p->IsJSObject()) {
539 Object* key = JSObject::cast(p)->SlowReverseLookup(fun);
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000540 if (key != isolate->heap()->undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 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
566char* 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.orge27d6172013-04-17 11:51:44 +0000576 OS::MemCopy(new_space, space_, *bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 *bytes = new_bytes;
578 DeleteArray(space_);
579 space_ = new_space;
580 return new_space;
581}
582
583
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000584// Only grow once to the maximum allowable size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585char* NoAllocationStringAllocator::grow(unsigned* bytes) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000586 ASSERT(size_ >= *bytes);
587 *bytes = size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 return space_;
589}
590
591
592} } // namespace v8::internal