292e3325d1df42f7d397cb326ce3627d59104d24
[platform/upstream/nodejs.git] / deps / v8 / src / profile-generator.cc
1 // Copyright 2012 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/v8.h"
6
7 #include "src/profile-generator-inl.h"
8
9 #include "src/compiler.h"
10 #include "src/debug.h"
11 #include "src/deoptimizer.h"
12 #include "src/global-handles.h"
13 #include "src/sampler.h"
14 #include "src/scopeinfo.h"
15 #include "src/unicode.h"
16
17 namespace v8 {
18 namespace internal {
19
20
21 bool StringsStorage::StringsMatch(void* key1, void* key2) {
22   return strcmp(reinterpret_cast<char*>(key1),
23                 reinterpret_cast<char*>(key2)) == 0;
24 }
25
26
27 StringsStorage::StringsStorage(Heap* heap)
28     : hash_seed_(heap->HashSeed()), names_(StringsMatch) {
29 }
30
31
32 StringsStorage::~StringsStorage() {
33   for (HashMap::Entry* p = names_.Start();
34        p != NULL;
35        p = names_.Next(p)) {
36     DeleteArray(reinterpret_cast<const char*>(p->value));
37   }
38 }
39
40
41 const char* StringsStorage::GetCopy(const char* src) {
42   int len = static_cast<int>(strlen(src));
43   HashMap::Entry* entry = GetEntry(src, len);
44   if (entry->value == NULL) {
45     Vector<char> dst = Vector<char>::New(len + 1);
46     StrNCpy(dst, src, len);
47     dst[len] = '\0';
48     entry->key = dst.start();
49     entry->value = entry->key;
50   }
51   return reinterpret_cast<const char*>(entry->value);
52 }
53
54
55 const char* StringsStorage::GetFormatted(const char* format, ...) {
56   va_list args;
57   va_start(args, format);
58   const char* result = GetVFormatted(format, args);
59   va_end(args);
60   return result;
61 }
62
63
64 const char* StringsStorage::AddOrDisposeString(char* str, int len) {
65   HashMap::Entry* entry = GetEntry(str, len);
66   if (entry->value == NULL) {
67     // New entry added.
68     entry->key = str;
69     entry->value = str;
70   } else {
71     DeleteArray(str);
72   }
73   return reinterpret_cast<const char*>(entry->value);
74 }
75
76
77 const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
78   Vector<char> str = Vector<char>::New(1024);
79   int len = VSNPrintF(str, format, args);
80   if (len == -1) {
81     DeleteArray(str.start());
82     return GetCopy(format);
83   }
84   return AddOrDisposeString(str.start(), len);
85 }
86
87
88 const char* StringsStorage::GetName(Name* name) {
89   if (name->IsString()) {
90     String* str = String::cast(name);
91     int length = Min(kMaxNameSize, str->length());
92     int actual_length = 0;
93     SmartArrayPointer<char> data =
94         str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
95                        &actual_length);
96     return AddOrDisposeString(data.Detach(), actual_length);
97   } else if (name->IsSymbol()) {
98     return "<symbol>";
99   }
100   return "";
101 }
102
103
104 const char* StringsStorage::GetName(int index) {
105   return GetFormatted("%d", index);
106 }
107
108
109 const char* StringsStorage::GetFunctionName(Name* name) {
110   return GetName(name);
111 }
112
113
114 const char* StringsStorage::GetFunctionName(const char* name) {
115   return GetCopy(name);
116 }
117
118
119 size_t StringsStorage::GetUsedMemorySize() const {
120   size_t size = sizeof(*this);
121   size += sizeof(HashMap::Entry) * names_.capacity();
122   for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
123     size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
124   }
125   return size;
126 }
127
128
129 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
130   uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
131   return names_.Lookup(const_cast<char*>(str), hash, true);
132 }
133
134
135 JITLineInfoTable::JITLineInfoTable() {}
136
137
138 JITLineInfoTable::~JITLineInfoTable() {}
139
140
141 void JITLineInfoTable::SetPosition(int pc_offset, int line) {
142   DCHECK(pc_offset >= 0);
143   DCHECK(line > 0);  // The 1-based number of the source line.
144   if (GetSourceLineNumber(pc_offset) != line) {
145     pc_offset_map_.insert(std::make_pair(pc_offset, line));
146   }
147 }
148
149
150 int JITLineInfoTable::GetSourceLineNumber(int pc_offset) const {
151   PcOffsetMap::const_iterator it = pc_offset_map_.lower_bound(pc_offset);
152   if (it == pc_offset_map_.end()) {
153     if (pc_offset_map_.empty()) return v8::CpuProfileNode::kNoLineNumberInfo;
154     return (--pc_offset_map_.end())->second;
155   }
156   return it->second;
157 }
158
159
160 const char* const CodeEntry::kEmptyNamePrefix = "";
161 const char* const CodeEntry::kEmptyResourceName = "";
162 const char* const CodeEntry::kEmptyBailoutReason = "";
163 const char* const CodeEntry::kNoDeoptReason = "";
164
165
166 CodeEntry::~CodeEntry() {
167   delete no_frame_ranges_;
168   delete line_info_;
169 }
170
171
172 uint32_t CodeEntry::GetCallUid() const {
173   uint32_t hash = ComputeIntegerHash(tag(), v8::internal::kZeroHashSeed);
174   if (shared_id_ != 0) {
175     hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
176                                v8::internal::kZeroHashSeed);
177   } else {
178     hash ^= ComputeIntegerHash(
179         static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
180         v8::internal::kZeroHashSeed);
181     hash ^= ComputeIntegerHash(
182         static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
183         v8::internal::kZeroHashSeed);
184     hash ^= ComputeIntegerHash(
185         static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
186         v8::internal::kZeroHashSeed);
187     hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
188   }
189   return hash;
190 }
191
192
193 bool CodeEntry::IsSameAs(CodeEntry* entry) const {
194   return this == entry ||
195          (tag() == entry->tag() && shared_id_ == entry->shared_id_ &&
196           (shared_id_ != 0 ||
197            (name_prefix_ == entry->name_prefix_ && name_ == entry->name_ &&
198             resource_name_ == entry->resource_name_ &&
199             line_number_ == entry->line_number_)));
200 }
201
202
203 void CodeEntry::SetBuiltinId(Builtins::Name id) {
204   bit_field_ = TagField::update(bit_field_, Logger::BUILTIN_TAG);
205   bit_field_ = BuiltinIdField::update(bit_field_, id);
206 }
207
208
209 int CodeEntry::GetSourceLine(int pc_offset) const {
210   if (line_info_ && !line_info_->empty()) {
211     return line_info_->GetSourceLineNumber(pc_offset);
212   }
213   return v8::CpuProfileNode::kNoLineNumberInfo;
214 }
215
216
217 void ProfileNode::CollectDeoptInfo(CodeEntry* entry) {
218   deopt_infos_.Add(DeoptInfo(entry->deopt_reason(), entry->deopt_location()));
219   entry->clear_deopt_info();
220 }
221
222
223 ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
224   HashMap::Entry* map_entry =
225       children_.Lookup(entry, CodeEntryHash(entry), false);
226   return map_entry != NULL ?
227       reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
228 }
229
230
231 ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
232   HashMap::Entry* map_entry =
233       children_.Lookup(entry, CodeEntryHash(entry), true);
234   ProfileNode* node = reinterpret_cast<ProfileNode*>(map_entry->value);
235   if (node == NULL) {
236     // New node added.
237     node = new ProfileNode(tree_, entry);
238     map_entry->value = node;
239     children_list_.Add(node);
240   }
241   return node;
242 }
243
244
245 void ProfileNode::IncrementLineTicks(int src_line) {
246   if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) return;
247   // Increment a hit counter of a certain source line.
248   // Add a new source line if not found.
249   HashMap::Entry* e =
250       line_ticks_.Lookup(reinterpret_cast<void*>(src_line), src_line, true);
251   DCHECK(e);
252   e->value = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(e->value) + 1);
253 }
254
255
256 bool ProfileNode::GetLineTicks(v8::CpuProfileNode::LineTick* entries,
257                                unsigned int length) const {
258   if (entries == NULL || length == 0) return false;
259
260   unsigned line_count = line_ticks_.occupancy();
261
262   if (line_count == 0) return true;
263   if (length < line_count) return false;
264
265   v8::CpuProfileNode::LineTick* entry = entries;
266
267   for (HashMap::Entry* p = line_ticks_.Start(); p != NULL;
268        p = line_ticks_.Next(p), entry++) {
269     entry->line =
270         static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->key));
271     entry->hit_count =
272         static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->value));
273   }
274
275   return true;
276 }
277
278
279 void ProfileNode::Print(int indent) {
280   base::OS::Print("%5u %*s %s%s %d #%d", self_ticks_, indent, "",
281                   entry_->name_prefix(), entry_->name(), entry_->script_id(),
282                   id());
283   if (entry_->resource_name()[0] != '\0')
284     base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
285   base::OS::Print("\n");
286   for (auto info : deopt_infos_) {
287     base::OS::Print("%*s deopted at %d with reason '%s'\n", indent + 10, "",
288                     info.deopt_location, info.deopt_reason);
289   }
290   const char* bailout_reason = entry_->bailout_reason();
291   if (bailout_reason != GetBailoutReason(BailoutReason::kNoReason) &&
292       bailout_reason != CodeEntry::kEmptyBailoutReason) {
293     base::OS::Print("%*s bailed out due to '%s'\n", indent + 10, "",
294                     bailout_reason);
295   }
296   for (HashMap::Entry* p = children_.Start();
297        p != NULL;
298        p = children_.Next(p)) {
299     reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
300   }
301 }
302
303
304 class DeleteNodesCallback {
305  public:
306   void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
307
308   void AfterAllChildrenTraversed(ProfileNode* node) {
309     delete node;
310   }
311
312   void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
313 };
314
315
316 ProfileTree::ProfileTree()
317     : root_entry_(Logger::FUNCTION_TAG, "(root)"),
318       next_node_id_(1),
319       root_(new ProfileNode(this, &root_entry_)) {
320 }
321
322
323 ProfileTree::~ProfileTree() {
324   DeleteNodesCallback cb;
325   TraverseDepthFirst(&cb);
326 }
327
328
329 ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path,
330                                          int src_line) {
331   ProfileNode* node = root_;
332   CodeEntry* last_entry = NULL;
333   for (CodeEntry** entry = path.start() + path.length() - 1;
334        entry != path.start() - 1;
335        --entry) {
336     if (*entry != NULL) {
337       node = node->FindOrAddChild(*entry);
338       last_entry = *entry;
339     }
340   }
341   if (last_entry && last_entry->has_deopt_info()) {
342     node->CollectDeoptInfo(last_entry);
343   }
344   node->IncrementSelfTicks();
345   if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
346     node->IncrementLineTicks(src_line);
347   }
348   return node;
349 }
350
351
352 struct NodesPair {
353   NodesPair(ProfileNode* src, ProfileNode* dst)
354       : src(src), dst(dst) { }
355   ProfileNode* src;
356   ProfileNode* dst;
357 };
358
359
360 class Position {
361  public:
362   explicit Position(ProfileNode* node)
363       : node(node), child_idx_(0) { }
364   INLINE(ProfileNode* current_child()) {
365     return node->children()->at(child_idx_);
366   }
367   INLINE(bool has_current_child()) {
368     return child_idx_ < node->children()->length();
369   }
370   INLINE(void next_child()) { ++child_idx_; }
371
372   ProfileNode* node;
373  private:
374   int child_idx_;
375 };
376
377
378 // Non-recursive implementation of a depth-first post-order tree traversal.
379 template <typename Callback>
380 void ProfileTree::TraverseDepthFirst(Callback* callback) {
381   List<Position> stack(10);
382   stack.Add(Position(root_));
383   while (stack.length() > 0) {
384     Position& current = stack.last();
385     if (current.has_current_child()) {
386       callback->BeforeTraversingChild(current.node, current.current_child());
387       stack.Add(Position(current.current_child()));
388     } else {
389       callback->AfterAllChildrenTraversed(current.node);
390       if (stack.length() > 1) {
391         Position& parent = stack[stack.length() - 2];
392         callback->AfterChildTraversed(parent.node, current.node);
393         parent.next_child();
394       }
395       // Remove child from the stack.
396       stack.RemoveLast();
397     }
398   }
399 }
400
401
402 CpuProfile::CpuProfile(const char* title, bool record_samples)
403     : title_(title),
404       record_samples_(record_samples),
405       start_time_(base::TimeTicks::HighResolutionNow()) {
406 }
407
408
409 void CpuProfile::AddPath(base::TimeTicks timestamp,
410                          const Vector<CodeEntry*>& path, int src_line) {
411   ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path, src_line);
412   if (record_samples_) {
413     timestamps_.Add(timestamp);
414     samples_.Add(top_frame_node);
415   }
416 }
417
418
419 void CpuProfile::CalculateTotalTicksAndSamplingRate() {
420   end_time_ = base::TimeTicks::HighResolutionNow();
421 }
422
423
424 void CpuProfile::Print() {
425   base::OS::Print("[Top down]:\n");
426   top_down_.Print();
427 }
428
429
430 CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
431 const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
432
433
434 void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
435   DeleteAllCoveredCode(addr, addr + size);
436   CodeTree::Locator locator;
437   tree_.Insert(addr, &locator);
438   locator.set_value(CodeEntryInfo(entry, size));
439 }
440
441
442 void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
443   List<Address> to_delete;
444   Address addr = end - 1;
445   while (addr >= start) {
446     CodeTree::Locator locator;
447     if (!tree_.FindGreatestLessThan(addr, &locator)) break;
448     Address start2 = locator.key(), end2 = start2 + locator.value().size;
449     if (start2 < end && start < end2) to_delete.Add(start2);
450     addr = start2 - 1;
451   }
452   for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
453 }
454
455
456 CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
457   CodeTree::Locator locator;
458   if (tree_.FindGreatestLessThan(addr, &locator)) {
459     // locator.key() <= addr. Need to check that addr is within entry.
460     const CodeEntryInfo& entry = locator.value();
461     if (addr < (locator.key() + entry.size)) {
462       if (start) {
463         *start = locator.key();
464       }
465       return entry.entry;
466     }
467   }
468   return NULL;
469 }
470
471
472 int CodeMap::GetSharedId(Address addr) {
473   CodeTree::Locator locator;
474   // For shared function entries, 'size' field is used to store their IDs.
475   if (tree_.Find(addr, &locator)) {
476     const CodeEntryInfo& entry = locator.value();
477     DCHECK(entry.entry == kSharedFunctionCodeEntry);
478     return entry.size;
479   } else {
480     tree_.Insert(addr, &locator);
481     int id = next_shared_id_++;
482     locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
483     return id;
484   }
485 }
486
487
488 void CodeMap::MoveCode(Address from, Address to) {
489   if (from == to) return;
490   CodeTree::Locator locator;
491   if (!tree_.Find(from, &locator)) return;
492   CodeEntryInfo entry = locator.value();
493   tree_.Remove(from);
494   AddCode(to, entry.entry, entry.size);
495 }
496
497
498 void CodeMap::CodeTreePrinter::Call(
499     const Address& key, const CodeMap::CodeEntryInfo& value) {
500   // For shared function entries, 'size' field is used to store their IDs.
501   if (value.entry == kSharedFunctionCodeEntry) {
502     base::OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
503   } else {
504     base::OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
505   }
506 }
507
508
509 void CodeMap::Print() {
510   CodeTreePrinter printer;
511   tree_.ForEach(&printer);
512 }
513
514
515 CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
516     : function_and_resource_names_(heap),
517       current_profiles_semaphore_(1) {
518 }
519
520
521 static void DeleteCodeEntry(CodeEntry** entry_ptr) {
522   delete *entry_ptr;
523 }
524
525
526 static void DeleteCpuProfile(CpuProfile** profile_ptr) {
527   delete *profile_ptr;
528 }
529
530
531 CpuProfilesCollection::~CpuProfilesCollection() {
532   finished_profiles_.Iterate(DeleteCpuProfile);
533   current_profiles_.Iterate(DeleteCpuProfile);
534   code_entries_.Iterate(DeleteCodeEntry);
535 }
536
537
538 bool CpuProfilesCollection::StartProfiling(const char* title,
539                                            bool record_samples) {
540   current_profiles_semaphore_.Wait();
541   if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
542     current_profiles_semaphore_.Signal();
543     return false;
544   }
545   for (int i = 0; i < current_profiles_.length(); ++i) {
546     if (strcmp(current_profiles_[i]->title(), title) == 0) {
547       // Ignore attempts to start profile with the same title...
548       current_profiles_semaphore_.Signal();
549       // ... though return true to force it collect a sample.
550       return true;
551     }
552   }
553   current_profiles_.Add(new CpuProfile(title, record_samples));
554   current_profiles_semaphore_.Signal();
555   return true;
556 }
557
558
559 CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
560   const int title_len = StrLength(title);
561   CpuProfile* profile = NULL;
562   current_profiles_semaphore_.Wait();
563   for (int i = current_profiles_.length() - 1; i >= 0; --i) {
564     if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
565       profile = current_profiles_.Remove(i);
566       break;
567     }
568   }
569   current_profiles_semaphore_.Signal();
570
571   if (profile == NULL) return NULL;
572   profile->CalculateTotalTicksAndSamplingRate();
573   finished_profiles_.Add(profile);
574   return profile;
575 }
576
577
578 bool CpuProfilesCollection::IsLastProfile(const char* title) {
579   // Called from VM thread, and only it can mutate the list,
580   // so no locking is needed here.
581   if (current_profiles_.length() != 1) return false;
582   return StrLength(title) == 0
583       || strcmp(current_profiles_[0]->title(), title) == 0;
584 }
585
586
587 void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
588   // Called from VM thread for a completed profile.
589   for (int i = 0; i < finished_profiles_.length(); i++) {
590     if (profile == finished_profiles_[i]) {
591       finished_profiles_.Remove(i);
592       return;
593     }
594   }
595   UNREACHABLE();
596 }
597
598
599 void CpuProfilesCollection::AddPathToCurrentProfiles(
600     base::TimeTicks timestamp, const Vector<CodeEntry*>& path, int src_line) {
601   // As starting / stopping profiles is rare relatively to this
602   // method, we don't bother minimizing the duration of lock holding,
603   // e.g. copying contents of the list to a local vector.
604   current_profiles_semaphore_.Wait();
605   for (int i = 0; i < current_profiles_.length(); ++i) {
606     current_profiles_[i]->AddPath(timestamp, path, src_line);
607   }
608   current_profiles_semaphore_.Signal();
609 }
610
611
612 CodeEntry* CpuProfilesCollection::NewCodeEntry(
613     Logger::LogEventsAndTags tag, const char* name, const char* name_prefix,
614     const char* resource_name, int line_number, int column_number,
615     JITLineInfoTable* line_info, Address instruction_start) {
616   CodeEntry* code_entry =
617       new CodeEntry(tag, name, name_prefix, resource_name, line_number,
618                     column_number, line_info, instruction_start);
619   code_entries_.Add(code_entry);
620   return code_entry;
621 }
622
623
624 const char* const ProfileGenerator::kProgramEntryName =
625     "(program)";
626 const char* const ProfileGenerator::kIdleEntryName =
627     "(idle)";
628 const char* const ProfileGenerator::kGarbageCollectorEntryName =
629     "(garbage collector)";
630 const char* const ProfileGenerator::kUnresolvedFunctionName =
631     "(unresolved function)";
632
633
634 ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
635     : profiles_(profiles),
636       program_entry_(
637           profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
638       idle_entry_(
639           profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
640       gc_entry_(
641           profiles->NewCodeEntry(Logger::BUILTIN_TAG,
642                                  kGarbageCollectorEntryName)),
643       unresolved_entry_(
644           profiles->NewCodeEntry(Logger::FUNCTION_TAG,
645                                  kUnresolvedFunctionName)) {
646 }
647
648
649 void ProfileGenerator::RecordTickSample(const TickSample& sample) {
650   // Allocate space for stack frames + pc + function + vm-state.
651   ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
652   // As actual number of decoded code entries may vary, initialize
653   // entries vector with NULL values.
654   CodeEntry** entry = entries.start();
655   memset(entry, 0, entries.length() * sizeof(*entry));
656
657   // The ProfileNode knows nothing about all versions of generated code for
658   // the same JS function. The line number information associated with
659   // the latest version of generated code is used to find a source line number
660   // for a JS function. Then, the detected source line is passed to
661   // ProfileNode to increase the tick count for this source line.
662   int src_line = v8::CpuProfileNode::kNoLineNumberInfo;
663   bool src_line_not_found = true;
664
665   if (sample.pc != NULL) {
666     if (sample.has_external_callback && sample.state == EXTERNAL &&
667         sample.top_frame_type == StackFrame::EXIT) {
668       // Don't use PC when in external callback code, as it can point
669       // inside callback's code, and we will erroneously report
670       // that a callback calls itself.
671       *entry++ = code_map_.FindEntry(sample.external_callback);
672     } else {
673       Address start;
674       CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
675       // If pc is in the function code before it set up stack frame or after the
676       // frame was destroyed SafeStackFrameIterator incorrectly thinks that
677       // ebp contains return address of the current function and skips caller's
678       // frame. Check for this case and just skip such samples.
679       if (pc_entry) {
680         List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
681         int pc_offset =
682             static_cast<int>(sample.pc - pc_entry->instruction_start());
683         if (ranges) {
684           for (int i = 0; i < ranges->length(); i++) {
685             OffsetRange& range = ranges->at(i);
686             if (range.from <= pc_offset && pc_offset < range.to) {
687               return;
688             }
689           }
690         }
691         src_line = pc_entry->GetSourceLine(pc_offset);
692         if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
693           src_line = pc_entry->line_number();
694         }
695         src_line_not_found = false;
696         *entry++ = pc_entry;
697
698         if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
699             pc_entry->builtin_id() == Builtins::kFunctionApply) {
700           // When current function is FunctionCall or FunctionApply builtin the
701           // top frame is either frame of the calling JS function or internal
702           // frame. In the latter case we know the caller for sure but in the
703           // former case we don't so we simply replace the frame with
704           // 'unresolved' entry.
705           if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
706             *entry++ = unresolved_entry_;
707           }
708         }
709       }
710     }
711
712     for (const Address* stack_pos = sample.stack,
713            *stack_end = stack_pos + sample.frames_count;
714          stack_pos != stack_end;
715          ++stack_pos) {
716       Address start = NULL;
717       *entry = code_map_.FindEntry(*stack_pos, &start);
718
719       // Skip unresolved frames (e.g. internal frame) and get source line of
720       // the first JS caller.
721       if (src_line_not_found && *entry) {
722         int pc_offset =
723             static_cast<int>(*stack_pos - (*entry)->instruction_start());
724         src_line = (*entry)->GetSourceLine(pc_offset);
725         if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
726           src_line = (*entry)->line_number();
727         }
728         src_line_not_found = false;
729       }
730
731       entry++;
732     }
733   }
734
735   if (FLAG_prof_browser_mode) {
736     bool no_symbolized_entries = true;
737     for (CodeEntry** e = entries.start(); e != entry; ++e) {
738       if (*e != NULL) {
739         no_symbolized_entries = false;
740         break;
741       }
742     }
743     // If no frames were symbolized, put the VM state entry in.
744     if (no_symbolized_entries) {
745       *entry++ = EntryForVMState(sample.state);
746     }
747   }
748
749   profiles_->AddPathToCurrentProfiles(sample.timestamp, entries, src_line);
750 }
751
752
753 CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
754   switch (tag) {
755     case GC:
756       return gc_entry_;
757     case JS:
758     case COMPILER:
759     // DOM events handlers are reported as OTHER / EXTERNAL entries.
760     // To avoid confusing people, let's put all these entries into
761     // one bucket.
762     case OTHER:
763     case EXTERNAL:
764       return program_entry_;
765     case IDLE:
766       return idle_entry_;
767     default: return NULL;
768   }
769 }
770
771 } }  // namespace v8::internal