blob: f311e20bdeb69d1da6815a99745da7face22d89a [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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
33namespace v8 { namespace internal {
34
35static const int kMentionedObjectCacheMaxSize = 256;
36static List<HeapObject*, PreallocatedStorage>* debug_object_cache = NULL;
37static Object* current_security_token = NULL;
38
39
40char* HeapStringAllocator::allocate(unsigned bytes) {
41 space_ = NewArray<char>(bytes);
42 return space_;
43}
44
45
46NoAllocationStringAllocator::NoAllocationStringAllocator(unsigned bytes) {
47 size_ = bytes;
48 space_ = NewArray<char>(bytes);
49}
50
51
kasper.lundaf4734f2008-07-28 12:50:18 +000052NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory,
53 unsigned size) {
54 size_ = size;
55 space_ = memory;
56}
57
58
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059bool StringStream::Put(char c) {
60 if (space() == 0) return false;
61 if (length_ >= capacity_ - 1) {
62 unsigned new_capacity = capacity_;
63 char* new_buffer = allocator_->grow(&new_capacity);
64 if (new_capacity > capacity_) {
65 capacity_ = new_capacity;
66 buffer_ = new_buffer;
67 } else {
68 // Indicate truncation with dots.
69 memset(cursor(), '.', space());
70 length_ = capacity_;
71 buffer_[length_ - 2] = '\n';
72 buffer_[length_ - 1] = '\0';
73 return false;
74 }
75 }
76 buffer_[length_] = c;
77 buffer_[length_ + 1] = '\0';
78 length_++;
79 return true;
80}
81
82
83// A control character is one that configures a format element. For
84// instance, in %.5s, .5 are control characters.
85static bool IsControlChar(char c) {
86 switch (c) {
87 case '0': case '1': case '2': case '3': case '4': case '5':
88 case '6': case '7': case '8': case '9': case '.': case '-':
89 return true;
90 default:
91 return false;
92 }
93}
94
95
ager@chromium.orga74f0da2008-12-03 16:05:52 +000096void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 // If we already ran out of space then return immediately.
98 if (space() == 0)
99 return;
100 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 }
155 case 'i': case 'd': case 'u': case 'x': case 'c': case 'p': 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 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 default:
170 UNREACHABLE();
171 break;
172 }
173 }
174
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000175 // Verify that the buffer is 0-terminated
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 ASSERT(buffer_[length_] == '\0');
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177}
178
179
180void StringStream::PrintObject(Object* o) {
181 o->ShortPrint(this);
182 if (o->IsString()) {
183 if (String::cast(o)->length() <= String::kMaxMediumStringSize) {
184 return;
185 }
186 } else if (o->IsNumber() || o->IsOddball()) {
187 return;
188 }
189 if (o->IsHeapObject()) {
190 for (int i = 0; i < debug_object_cache->length(); i++) {
191 if ((*debug_object_cache)[i] == o) {
192 Add("#%d#", i);
193 return;
194 }
195 }
196 if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) {
197 Add("#%d#", debug_object_cache->length());
198 debug_object_cache->Add(HeapObject::cast(o));
199 } else {
200 Add("@%p", o);
201 }
202 }
203}
204
205
206void StringStream::Add(const char* format) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000207 Add(CStrVector(format));
208}
209
210
211void StringStream::Add(Vector<const char> format) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 Add(format, Vector<FmtElm>::empty());
213}
214
215
216void StringStream::Add(const char* format, FmtElm arg0) {
217 const char argc = 1;
218 FmtElm argv[argc] = { arg0 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000219 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220}
221
222
223void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) {
224 const char argc = 2;
225 FmtElm argv[argc] = { arg0, arg1 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000226 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227}
228
229
230void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
231 FmtElm arg2) {
232 const char argc = 3;
233 FmtElm argv[argc] = { arg0, arg1, arg2 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000234 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
235}
236
237
238void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
239 FmtElm arg2, FmtElm arg3) {
240 const char argc = 4;
241 FmtElm argv[argc] = { arg0, arg1, arg2, arg3 };
242 Add(CStrVector(format), Vector<FmtElm>(argv, argc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243}
244
245
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246SmartPointer<const char> StringStream::ToCString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 char* str = NewArray<char>(length_ + 1);
248 memcpy(str, buffer_, length_);
249 str[length_] = '\0';
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000250 return SmartPointer<const char>(str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251}
252
253
254void StringStream::Log() {
255 LOG(StringEvent("StackDump", buffer_));
256}
257
258
259void StringStream::OutputToStdOut() {
260 // Dump the output to stdout, but make sure to break it up into
261 // manageable chunks to avoid losing parts of the output in the OS
262 // printing code. This is a problem on Windows in particular; see
263 // the VPrint() function implementations in platform-win32.cc.
264 unsigned position = 0;
265 for (unsigned next; (next = position + 2048) < length_; position = next) {
266 char save = buffer_[next];
267 buffer_[next] = '\0';
268 internal::PrintF("%s", &buffer_[position]);
269 buffer_[next] = save;
270 }
271 internal::PrintF("%s", &buffer_[position]);
272}
273
274
275Handle<String> StringStream::ToString() {
276 return Factory::NewStringFromUtf8(Vector<const char>(buffer_, length_));
277}
278
279
280void StringStream::ClearMentionedObjectCache() {
281 current_security_token = NULL;
282 if (debug_object_cache == NULL) {
283 debug_object_cache = new List<HeapObject*, PreallocatedStorage>(0);
284 }
285 debug_object_cache->Clear();
286}
287
288
289#ifdef DEBUG
290bool StringStream::IsMentionedObjectCacheClear() {
291 return (debug_object_cache->length() == 0);
292}
293#endif
294
295
296bool StringStream::Put(String* str) {
297 return Put(str, 0, str->length());
298}
299
300
301bool StringStream::Put(String* str, int start, int end) {
302 StringInputBuffer name_buffer(str);
303 name_buffer.Seek(start);
304 for (int i = start; i < end && name_buffer.has_more(); i++) {
305 int c = name_buffer.GetNext();
306 if (c >= 127 || c < 32) {
307 c = '?';
308 }
309 if (!Put(c)) {
310 return false; // Output was truncated.
311 }
312 }
313 return true;
314}
315
316
317void StringStream::PrintName(Object* name) {
318 if (name->IsString()) {
319 String* str = String::cast(name);
320 if (str->length() > 0) {
321 Put(str);
322 } else {
323 Add("/* anonymous */");
324 }
325 } else {
326 Add("%o", name);
327 }
328}
329
330
331void StringStream::PrintUsingMap(JSObject* js_object) {
332 Map* map = js_object->map();
333 if (!Heap::Contains(map) ||
334 !map->IsHeapObject() ||
335 !map->IsMap()) {
336 Add("<Invalid map>\n");
337 return;
338 }
339 for (DescriptorReader r(map->instance_descriptors()); !r.eos(); r.advance()) {
340 switch (r.type()) {
341 case FIELD: {
342 Object* key = r.GetKey();
343 if (key->IsString() || key->IsNumber()) {
344 int len = 3;
345 if (key->IsString()) {
346 len = String::cast(key)->length();
347 }
348 for (; len < 18; len++)
349 Put(' ');
350 if (key->IsString()) {
351 Put(String::cast(key));
352 } else {
353 key->ShortPrint();
354 }
355 Add(": ");
ager@chromium.org7c537e22008-10-16 08:43:32 +0000356 Object* value = js_object->FastPropertyAt(r.GetFieldIndex());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 Add("%o\n", value);
358 }
359 }
360 break;
361 default:
362 break;
363 }
364 }
365}
366
367
368void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) {
369 for (unsigned int i = 0; i < 10 && i < limit; i++) {
370 Object* element = array->get(i);
371 if (element != Heap::the_hole_value()) {
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
383void 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
405void StringStream::PrintMentionedObjectCache() {
406 Add("==== Key ============================================\n\n");
407 for (int i = 0; i < debug_object_cache->length(); i++) {
408 HeapObject* printee = (*debug_object_cache)[i];
409 Add(" #%d# %p: ", i, printee);
410 printee->ShortPrint(this);
411 Add("\n");
412 if (printee->IsJSObject()) {
413 if (printee->IsJSValue()) {
414 Add(" value(): %o\n", JSValue::cast(printee)->value());
415 }
416 PrintUsingMap(JSObject::cast(printee));
417 if (printee->IsJSArray()) {
418 JSArray* array = JSArray::cast(printee);
419 if (array->HasFastElements()) {
420 unsigned int limit = FixedArray::cast(array->elements())->length();
421 unsigned int length =
422 static_cast<uint32_t>(JSArray::cast(array)->length()->Number());
423 if (length < limit) limit = length;
424 PrintFixedArray(FixedArray::cast(array->elements()), limit);
425 }
426 }
427 } else if (printee->IsByteArray()) {
428 PrintByteArray(ByteArray::cast(printee));
429 } else if (printee->IsFixedArray()) {
430 unsigned int limit = FixedArray::cast(printee)->length();
431 PrintFixedArray(FixedArray::cast(printee), limit);
432 }
433 }
434}
435
436
437void StringStream::PrintSecurityTokenIfChanged(Object* f) {
438 if (!f->IsHeapObject() || !Heap::Contains(HeapObject::cast(f))) {
439 return;
440 }
441 Map* map = HeapObject::cast(f)->map();
442 if (!map->IsHeapObject() ||
443 !Heap::Contains(map) ||
444 !map->IsMap() ||
445 !f->IsJSFunction()) {
446 return;
447 }
448
449 JSFunction* fun = JSFunction::cast(f);
450 Object* perhaps_context = fun->unchecked_context();
451 if (perhaps_context->IsHeapObject() &&
452 Heap::Contains(HeapObject::cast(perhaps_context)) &&
453 perhaps_context->IsContext()) {
454 Context* context = fun->context();
455 if (!Heap::Contains(context)) {
456 Add("(Function context is outside heap)\n");
457 return;
458 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000459 Object* token = context->global_context()->security_token();
460 if (token != current_security_token) {
461 Add("Security context: %o\n", token);
462 current_security_token = token;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 }
464 } else {
465 Add("(Function context is corrupt)\n");
466 }
467}
468
469
470void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) {
471 if (f->IsHeapObject() &&
472 Heap::Contains(HeapObject::cast(f)) &&
473 Heap::Contains(HeapObject::cast(f)->map()) &&
474 HeapObject::cast(f)->map()->IsMap()) {
475 if (f->IsJSFunction()) {
476 JSFunction* fun = JSFunction::cast(f);
477 // Common case: on-stack function present and resolved.
478 PrintPrototype(fun, receiver);
479 *code = fun->code();
480 } else if (f->IsSymbol()) {
481 // Unresolved and megamorphic calls: Instead of the function
482 // we have the function name on the stack.
483 PrintName(f);
484 Add("/* unresolved */ ");
485 } else {
486 // Unless this is the frame of a built-in function, we should always have
487 // the callee function or name on the stack. If we don't, we have a
488 // problem or a change of the stack frame layout.
489 Add("%o", f);
490 Add("/* warning: no JSFunction object or function name found */ ");
491 }
492 /* } else if (is_trampoline()) {
493 Print("trampoline ");
494 */
495 } else {
496 if (!f->IsHeapObject()) {
497 Add("/* warning: 'function' was not a heap object */ ");
498 return;
499 }
500 if (!Heap::Contains(HeapObject::cast(f))) {
501 Add("/* warning: 'function' was not on the heap */ ");
502 return;
503 }
504 if (!Heap::Contains(HeapObject::cast(f)->map())) {
505 Add("/* warning: function's map was not on the heap */ ");
506 return;
507 }
508 if (!HeapObject::cast(f)->map()->IsMap()) {
509 Add("/* warning: function's map was not a valid map */ ");
510 return;
511 }
512 Add("/* warning: Invalid JSFunction object found */ ");
513 }
514}
515
516
517void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) {
518 Object* name = fun->shared()->name();
519 bool print_name = false;
520 for (Object* p = receiver; p != Heap::null_value(); p = p->GetPrototype()) {
521 if (p->IsJSObject()) {
522 Object* key = JSObject::cast(p)->SlowReverseLookup(fun);
523 if (key != Heap::undefined_value()) {
524 if (!name->IsString() ||
525 !key->IsString() ||
526 !String::cast(name)->Equals(String::cast(key))) {
527 print_name = true;
528 }
529 if (name->IsString() && String::cast(name)->length() == 0) {
530 print_name = false;
531 }
532 name = key;
533 }
534 } else {
535 print_name = true;
536 }
537 }
538 PrintName(name);
539 // Also known as - if the name in the function doesn't match the name under
540 // which it was looked up.
541 if (print_name) {
542 Add("(aka ");
543 PrintName(fun->shared()->name());
544 Put(')');
545 }
546}
547
548
549char* HeapStringAllocator::grow(unsigned* bytes) {
550 unsigned new_bytes = *bytes * 2;
551 // Check for overflow.
552 if (new_bytes <= *bytes) {
553 return space_;
554 }
555 char* new_space = NewArray<char>(new_bytes);
556 if (new_space == NULL) {
557 return space_;
558 }
559 memcpy(new_space, space_, *bytes);
560 *bytes = new_bytes;
561 DeleteArray(space_);
562 space_ = new_space;
563 return new_space;
564}
565
566
567char* NoAllocationStringAllocator::grow(unsigned* bytes) {
568 unsigned new_bytes = *bytes * 2;
569 if (new_bytes > size_) {
570 new_bytes = size_;
571 }
572 *bytes = new_bytes;
573 return space_;
574}
575
576
577} } // namespace v8::internal