• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/string-stream.h"
6 
7 #include <memory>
8 
9 #include "src/handles-inl.h"
10 #include "src/log.h"
11 #include "src/objects-inl.h"
12 #include "src/objects/js-array-inl.h"
13 #include "src/prototype.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 static const int kMentionedObjectCacheMaxSize = 256;
19 
allocate(unsigned bytes)20 char* HeapStringAllocator::allocate(unsigned bytes) {
21   space_ = NewArray<char>(bytes);
22   return space_;
23 }
24 
25 
allocate(unsigned bytes)26 char* FixedStringAllocator::allocate(unsigned bytes) {
27   CHECK_LE(bytes, length_);
28   return buffer_;
29 }
30 
31 
grow(unsigned * old)32 char* FixedStringAllocator::grow(unsigned* old) {
33   *old = length_;
34   return buffer_;
35 }
36 
37 
Put(char c)38 bool StringStream::Put(char c) {
39   if (full()) return false;
40   DCHECK(length_ < capacity_);
41   // Since the trailing '\0' is not accounted for in length_ fullness is
42   // indicated by a difference of 1 between length_ and capacity_. Thus when
43   // reaching a difference of 2 we need to grow the buffer.
44   if (length_ == capacity_ - 2) {
45     unsigned new_capacity = capacity_;
46     char* new_buffer = allocator_->grow(&new_capacity);
47     if (new_capacity > capacity_) {
48       capacity_ = new_capacity;
49       buffer_ = new_buffer;
50     } else {
51       // Reached the end of the available buffer.
52       DCHECK_GE(capacity_, 5);
53       length_ = capacity_ - 1;  // Indicate fullness of the stream.
54       buffer_[length_ - 4] = '.';
55       buffer_[length_ - 3] = '.';
56       buffer_[length_ - 2] = '.';
57       buffer_[length_ - 1] = '\n';
58       buffer_[length_] = '\0';
59       return false;
60     }
61   }
62   buffer_[length_] = c;
63   buffer_[length_ + 1] = '\0';
64   length_++;
65   return true;
66 }
67 
68 
69 // A control character is one that configures a format element.  For
70 // instance, in %.5s, .5 are control characters.
IsControlChar(char c)71 static bool IsControlChar(char c) {
72   switch (c) {
73   case '0': case '1': case '2': case '3': case '4': case '5':
74   case '6': case '7': case '8': case '9': case '.': case '-':
75     return true;
76   default:
77     return false;
78   }
79 }
80 
81 
Add(Vector<const char> format,Vector<FmtElm> elms)82 void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) {
83   // If we already ran out of space then return immediately.
84   if (full()) return;
85   int offset = 0;
86   int elm = 0;
87   while (offset < format.length()) {
88     if (format[offset] != '%' || elm == elms.length()) {
89       Put(format[offset]);
90       offset++;
91       continue;
92     }
93     // Read this formatting directive into a temporary buffer
94     EmbeddedVector<char, 24> temp;
95     int format_length = 0;
96     // Skip over the whole control character sequence until the
97     // format element type
98     temp[format_length++] = format[offset++];
99     while (offset < format.length() && IsControlChar(format[offset]))
100       temp[format_length++] = format[offset++];
101     if (offset >= format.length())
102       return;
103     char type = format[offset];
104     temp[format_length++] = type;
105     temp[format_length] = '\0';
106     offset++;
107     FmtElm current = elms[elm++];
108     switch (type) {
109     case 's': {
110       DCHECK_EQ(FmtElm::C_STR, current.type_);
111       const char* value = current.data_.u_c_str_;
112       Add(value);
113       break;
114     }
115     case 'w': {
116       DCHECK_EQ(FmtElm::LC_STR, current.type_);
117       Vector<const uc16> value = *current.data_.u_lc_str_;
118       for (int i = 0; i < value.length(); i++)
119         Put(static_cast<char>(value[i]));
120       break;
121     }
122     case 'o': {
123       DCHECK_EQ(FmtElm::OBJ, current.type_);
124       Object* obj = current.data_.u_obj_;
125       PrintObject(obj);
126       break;
127     }
128     case 'k': {
129       DCHECK_EQ(FmtElm::INT, current.type_);
130       int value = current.data_.u_int_;
131       if (0x20 <= value && value <= 0x7F) {
132         Put(value);
133       } else if (value <= 0xFF) {
134         Add("\\x%02x", value);
135       } else {
136         Add("\\u%04x", value);
137       }
138       break;
139     }
140     case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': {
141       int value = current.data_.u_int_;
142       EmbeddedVector<char, 24> formatted;
143       int length = SNPrintF(formatted, temp.start(), value);
144       Add(Vector<const char>(formatted.start(), length));
145       break;
146     }
147     case 'f': case 'g': case 'G': case 'e': case 'E': {
148       double value = current.data_.u_double_;
149       int inf = std::isinf(value);
150       if (inf == -1) {
151         Add("-inf");
152       } else if (inf == 1) {
153         Add("inf");
154       } else if (std::isnan(value)) {
155         Add("nan");
156       } else {
157         EmbeddedVector<char, 28> formatted;
158         SNPrintF(formatted, temp.start(), value);
159         Add(formatted.start());
160       }
161       break;
162     }
163     case 'p': {
164       void* value = current.data_.u_pointer_;
165       EmbeddedVector<char, 20> formatted;
166       SNPrintF(formatted, temp.start(), value);
167       Add(formatted.start());
168       break;
169     }
170     default:
171       UNREACHABLE();
172       break;
173     }
174   }
175 
176   // Verify that the buffer is 0-terminated
177   DCHECK_EQ(buffer_[length_], '\0');
178 }
179 
180 
PrintObject(Object * o)181 void StringStream::PrintObject(Object* o) {
182   o->ShortPrint(this);
183   if (o->IsString()) {
184     if (String::cast(o)->length() <= String::kMaxShortPrintLength) {
185       return;
186     }
187   } else if (o->IsNumber() || o->IsOddball()) {
188     return;
189   }
190   if (o->IsHeapObject() && object_print_mode_ == kPrintObjectVerbose) {
191     // TODO(delphick): Consider whether we can get the isolate without using
192     // TLS.
193     DebugObjectCache* debug_object_cache =
194         Isolate::Current()->string_stream_debug_object_cache();
195     for (size_t i = 0; i < debug_object_cache->size(); i++) {
196       if ((*debug_object_cache)[i] == o) {
197         Add("#%d#", static_cast<int>(i));
198         return;
199       }
200     }
201     if (debug_object_cache->size() < kMentionedObjectCacheMaxSize) {
202       Add("#%d#", static_cast<int>(debug_object_cache->size()));
203       debug_object_cache->push_back(HeapObject::cast(o));
204     } else {
205       Add("@%p", o);
206     }
207   }
208 }
209 
210 
ToCString() const211 std::unique_ptr<char[]> StringStream::ToCString() const {
212   char* str = NewArray<char>(length_ + 1);
213   MemCopy(str, buffer_, length_);
214   str[length_] = '\0';
215   return std::unique_ptr<char[]>(str);
216 }
217 
218 
Log(Isolate * isolate)219 void StringStream::Log(Isolate* isolate) {
220   LOG(isolate, StringEvent("StackDump", buffer_));
221 }
222 
223 
OutputToFile(FILE * out)224 void StringStream::OutputToFile(FILE* out) {
225   // Dump the output to stdout, but make sure to break it up into
226   // manageable chunks to avoid losing parts of the output in the OS
227   // printing code. This is a problem on Windows in particular; see
228   // the VPrint() function implementations in platform-win32.cc.
229   unsigned position = 0;
230   for (unsigned next; (next = position + 2048) < length_; position = next) {
231     char save = buffer_[next];
232     buffer_[next] = '\0';
233     internal::PrintF(out, "%s", &buffer_[position]);
234     buffer_[next] = save;
235   }
236   internal::PrintF(out, "%s", &buffer_[position]);
237 }
238 
239 
ToString(Isolate * isolate)240 Handle<String> StringStream::ToString(Isolate* isolate) {
241   return isolate->factory()->NewStringFromUtf8(
242       Vector<const char>(buffer_, length_)).ToHandleChecked();
243 }
244 
245 
ClearMentionedObjectCache(Isolate * isolate)246 void StringStream::ClearMentionedObjectCache(Isolate* isolate) {
247   isolate->set_string_stream_current_security_token(nullptr);
248   if (isolate->string_stream_debug_object_cache() == nullptr) {
249     isolate->set_string_stream_debug_object_cache(new DebugObjectCache());
250   }
251   isolate->string_stream_debug_object_cache()->clear();
252 }
253 
254 
255 #ifdef DEBUG
IsMentionedObjectCacheClear(Isolate * isolate)256 bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) {
257   return object_print_mode_ == kPrintObjectConcise ||
258          isolate->string_stream_debug_object_cache()->size() == 0;
259 }
260 #endif
261 
262 
Put(String * str)263 bool StringStream::Put(String* str) {
264   return Put(str, 0, str->length());
265 }
266 
267 
Put(String * str,int start,int end)268 bool StringStream::Put(String* str, int start, int end) {
269   StringCharacterStream stream(str, start);
270   for (int i = start; i < end && stream.HasMore(); i++) {
271     uint16_t c = stream.GetNext();
272     if (c >= 127 || c < 32) {
273       c = '?';
274     }
275     if (!Put(static_cast<char>(c))) {
276       return false;  // Output was truncated.
277     }
278   }
279   return true;
280 }
281 
282 
PrintName(Object * name)283 void StringStream::PrintName(Object* name) {
284   if (name->IsString()) {
285     String* str = String::cast(name);
286     if (str->length() > 0) {
287       Put(str);
288     } else {
289       Add("/* anonymous */");
290     }
291   } else {
292     Add("%o", name);
293   }
294 }
295 
296 
PrintUsingMap(JSObject * js_object)297 void StringStream::PrintUsingMap(JSObject* js_object) {
298   Map* map = js_object->map();
299   int real_size = map->NumberOfOwnDescriptors();
300   DescriptorArray* descs = map->instance_descriptors();
301   for (int i = 0; i < real_size; i++) {
302     PropertyDetails details = descs->GetDetails(i);
303     if (details.location() == kField) {
304       DCHECK_EQ(kData, details.kind());
305       Object* key = descs->GetKey(i);
306       if (key->IsString() || key->IsNumber()) {
307         int len = 3;
308         if (key->IsString()) {
309           len = String::cast(key)->length();
310         }
311         for (; len < 18; len++)
312           Put(' ');
313         if (key->IsString()) {
314           Put(String::cast(key));
315         } else {
316           key->ShortPrint();
317         }
318         Add(": ");
319         FieldIndex index = FieldIndex::ForDescriptor(map, i);
320         if (js_object->IsUnboxedDoubleField(index)) {
321           double value = js_object->RawFastDoublePropertyAt(index);
322           Add("<unboxed double> %.16g\n", FmtElm(value));
323         } else {
324           Object* value = js_object->RawFastPropertyAt(index);
325           Add("%o\n", value);
326         }
327       }
328     }
329   }
330 }
331 
332 
PrintFixedArray(FixedArray * array,unsigned int limit)333 void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) {
334   ReadOnlyRoots roots = array->GetReadOnlyRoots();
335   for (unsigned int i = 0; i < 10 && i < limit; i++) {
336     Object* element = array->get(i);
337     if (element->IsTheHole(roots)) continue;
338     for (int len = 1; len < 18; len++) {
339       Put(' ');
340     }
341     Add("%d: %o\n", i, array->get(i));
342   }
343   if (limit >= 10) {
344     Add("                  ...\n");
345   }
346 }
347 
348 
PrintByteArray(ByteArray * byte_array)349 void StringStream::PrintByteArray(ByteArray* byte_array) {
350   unsigned int limit = byte_array->length();
351   for (unsigned int i = 0; i < 10 && i < limit; i++) {
352     byte b = byte_array->get(i);
353     Add("             %d: %3d 0x%02x", i, b, b);
354     if (b >= ' ' && b <= '~') {
355       Add(" '%c'", b);
356     } else if (b == '\n') {
357       Add(" '\n'");
358     } else if (b == '\r') {
359       Add(" '\r'");
360     } else if (b >= 1 && b <= 26) {
361       Add(" ^%c", b + 'A' - 1);
362     }
363     Add("\n");
364   }
365   if (limit >= 10) {
366     Add("                  ...\n");
367   }
368 }
369 
370 
PrintMentionedObjectCache(Isolate * isolate)371 void StringStream::PrintMentionedObjectCache(Isolate* isolate) {
372   if (object_print_mode_ == kPrintObjectConcise) return;
373   DebugObjectCache* debug_object_cache =
374       isolate->string_stream_debug_object_cache();
375   Add("==== Key         ============================================\n\n");
376   for (size_t i = 0; i < debug_object_cache->size(); i++) {
377     HeapObject* printee = (*debug_object_cache)[i];
378     Add(" #%d# %p: ", static_cast<int>(i), printee);
379     printee->ShortPrint(this);
380     Add("\n");
381     if (printee->IsJSObject()) {
382       if (printee->IsJSValue()) {
383         Add("           value(): %o\n", JSValue::cast(printee)->value());
384       }
385       PrintUsingMap(JSObject::cast(printee));
386       if (printee->IsJSArray()) {
387         JSArray* array = JSArray::cast(printee);
388         if (array->HasObjectElements()) {
389           unsigned int limit = FixedArray::cast(array->elements())->length();
390           unsigned int length =
391             static_cast<uint32_t>(JSArray::cast(array)->length()->Number());
392           if (length < limit) limit = length;
393           PrintFixedArray(FixedArray::cast(array->elements()), limit);
394         }
395       }
396     } else if (printee->IsByteArray()) {
397       PrintByteArray(ByteArray::cast(printee));
398     } else if (printee->IsFixedArray()) {
399       unsigned int limit = FixedArray::cast(printee)->length();
400       PrintFixedArray(FixedArray::cast(printee), limit);
401     }
402   }
403 }
404 
PrintSecurityTokenIfChanged(JSFunction * fun)405 void StringStream::PrintSecurityTokenIfChanged(JSFunction* fun) {
406   Context* context = fun->context();
407   Object* token = context->native_context()->security_token();
408   Isolate* isolate = fun->GetIsolate();
409   if (token != isolate->string_stream_current_security_token()) {
410     Add("Security context: %o\n", token);
411     isolate->set_string_stream_current_security_token(token);
412   }
413 }
414 
PrintFunction(JSFunction * fun,Object * receiver,Code ** code)415 void StringStream::PrintFunction(JSFunction* fun, Object* receiver,
416                                  Code** code) {
417   PrintPrototype(fun, receiver);
418   *code = fun->code();
419 }
420 
421 
PrintPrototype(JSFunction * fun,Object * receiver)422 void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) {
423   Object* name = fun->shared()->Name();
424   bool print_name = false;
425   Isolate* isolate = fun->GetIsolate();
426   if (receiver->IsNullOrUndefined(isolate) || receiver->IsTheHole(isolate) ||
427       receiver->IsJSProxy()) {
428     print_name = true;
429   } else if (isolate->context() != nullptr) {
430     if (!receiver->IsJSObject()) {
431       receiver = receiver->GetPrototypeChainRootMap(isolate)->prototype();
432     }
433 
434     for (PrototypeIterator iter(isolate, JSObject::cast(receiver),
435                                 kStartAtReceiver);
436          !iter.IsAtEnd(); iter.Advance()) {
437       if (iter.GetCurrent()->IsJSProxy()) break;
438       Object* key = iter.GetCurrent<JSObject>()->SlowReverseLookup(fun);
439       if (!key->IsUndefined(isolate)) {
440         if (!name->IsString() ||
441             !key->IsString() ||
442             !String::cast(name)->Equals(String::cast(key))) {
443           print_name = true;
444         }
445         if (name->IsString() && String::cast(name)->length() == 0) {
446           print_name = false;
447         }
448         name = key;
449         break;
450       }
451     }
452   }
453   PrintName(name);
454   // Also known as - if the name in the function doesn't match the name under
455   // which it was looked up.
456   if (print_name) {
457     Add("(aka ");
458     PrintName(fun->shared()->Name());
459     Put(')');
460   }
461 }
462 
463 
grow(unsigned * bytes)464 char* HeapStringAllocator::grow(unsigned* bytes) {
465   unsigned new_bytes = *bytes * 2;
466   // Check for overflow.
467   if (new_bytes <= *bytes) {
468     return space_;
469   }
470   char* new_space = NewArray<char>(new_bytes);
471   if (new_space == nullptr) {
472     return space_;
473   }
474   MemCopy(new_space, space_, *bytes);
475   *bytes = new_bytes;
476   DeleteArray(space_);
477   space_ = new_space;
478   return new_space;
479 }
480 
481 
482 }  // namespace internal
483 }  // namespace v8
484