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