1 // Copyright (c) 2013 Intel Corporation. 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.
5 #ifndef XDK_CODE_MAP_H_
6 #define XDK_CODE_MAP_H_
8 // ----------------------------------------------------------------------------
10 // This file contains the FunctionSnapshot and related objects declarations
12 // The FunctionSnapshot object maintains a map of JIT compiled functions.
13 // It is modified on code events(CodeAdded, CodeMoved and CodeDeleted) from
14 // V8 built-in profiler.
16 // ----------------------------------------------------------------------------
18 #include "xdk-types.h"
29 v8engine::Address, // start address of code
32 // This class is used to record the JITted code position info for JIT
37 LineEntry(size_t offset, size_t line)
38 : pcOffset(offset), line(line) { }
40 size_t pcOffset; // PC offset from the begining of the code trace.
41 size_t line; // Can be either position returned from V8 assembler
42 // (which needs to be converted to src line) or src line
46 typedef std::list<LineEntry> Entries;
48 void setPosition(size_t offset, size_t line) {
49 addCodeLineEntry(LineEntry(offset, line));
52 inline size_t getSize() const { return m_lines.size(); }
53 const Entries* getEntries() const { return &m_lines; }
56 void addCodeLineEntry(const LineEntry& entry) { m_lines.push_back(entry); }
61 // This class describes the function reported with CodeAdded event.
64 explicit Function(v8engine::Address codeAddr, uint32_t codeLen,
65 const std::string& name, const std::string& type,
66 const LineMap* lineMap);
68 inline v8engine::Address getCodeAddress() const { return m_codeAddr; }
69 inline uint32_t getCodeLength() const { return m_codeLen; }
71 inline const std::string& getType() const { return m_type; }
72 inline const std::string& getName() const { return m_name; }
73 inline const std::string& getLogLine() const { return m_logLine; }
75 const LineMap& getLineMap() const { return m_lineMap; }
78 v8engine::Address m_codeAddr;
82 std::string m_logLine;
86 // This class describes the code range related to object of Function type.
87 // The start address and length are taken from CodeAdded event.
90 class Comparator : public std::binary_function<Range&, Range&, bool> {
92 bool operator()(const Range& l, const Range& r) const {
93 return (l.start() + l.length() <= r.start());
97 Range(v8engine::Address start, uint32_t length)
98 : m_start(start), m_length(length) { }
100 inline v8engine::Address start() const { return m_start; }
101 inline uint32_t length() const { return m_length; }
104 v8engine::Address m_start;
108 // This class maintains a map of JIT compiled functions.
109 // The content is changed on CodeAdded, CodeMoved and CodeDeleted events.
110 typedef std::map<Range, const Function, Range::Comparator> CodeMap;
112 class FunctionSnapshot {
114 explicit FunctionSnapshot() {}
115 virtual ~FunctionSnapshot() { m_impl.clear(); }
117 void insert(const Function& func);
118 void move(v8engine::Address from, v8engine::Address to);
119 void remove(v8engine::Address addr);
121 inline const CodeMap& entries() { return m_impl; }
124 FunctionSnapshot(const FunctionSnapshot&);
125 FunctionSnapshot& operator=(const FunctionSnapshot&);
127 void removeAll(const Range& range);
132 } } // namespace xdk::internal
134 #endif // XDK_CODE_MAP_H_