Upstream version 6.34.113.0
[platform/framework/web/crosswalk.git] / src / v8 / src / third_party / xdk / xdk-code-map.cc
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.
4
5 #include "xdk-code-map.h"
6 #include <sstream>
7
8 namespace xdk {
9 namespace internal {
10
11 static std::string replaceAddress(const std::string& str,
12                                   v8engine::Address addr) {
13   // The input str: code-creation,LazyCompile,0,0x3851c4e0,200," native uri.js"
14   std::string first;
15   std::string end;
16
17   std::size_t found = str.find(',');
18   if (found != std::string::npos) {
19     found = str.find(',', found + 1);
20     if (found != std::string::npos) {
21       found = str.find(',', found + 1);
22       if (found != std::string::npos) {
23         first = str.substr(0, found);
24         found = str.find(',', found + 1);
25         if (found != std::string::npos) {
26           end = str.substr(found, str.size() - found);
27         }
28       }
29     }
30   }
31
32   if (!first.size() || !end.size()) return str;
33
34   std::stringstream ss;
35   ss << first << ','
36      << std::showbase << std::hex << static_cast<void*>(addr) << end;
37   return ss.str();
38 }
39
40
41 Function::Function(v8engine::Address codeAddr, uint32_t codeLen,
42                    const std::string& name, const std::string& type,
43                    const LineMap* lineMap)
44     : m_codeAddr(codeAddr), m_codeLen(codeLen), m_name(name), m_type(type) {
45   CHECK(codeAddr);
46   CHECK(codeLen);
47   // Can't be empty because it's came from CodeCreation(...) events
48   CHECK(!name.empty());
49   m_logLine = m_name;
50
51   if (lineMap && lineMap->getSize()) m_lineMap = *lineMap;
52 }
53
54
55 void FunctionSnapshot::removeAll(const Range& range) {
56   CodeMap::iterator low = m_impl.lower_bound(range);
57   CodeMap::iterator up = m_impl.upper_bound(range);
58   CodeMap::iterator::difference_type num = std::distance(low, up);
59
60   if (num) {
61     XDKLog("xdk: %d ranges were overlapped and removed\n", num);
62
63     CodeMap::iterator itr = low;
64     for (; itr != up; ++itr) {
65       XDKLog("xdk:  ovrl&removed addr=0x%x len=0x%x name=%s\n",
66              itr->first.start(), itr->first.length(),
67              itr->second.getLogLine().c_str());
68     }
69     m_impl.erase(low, up);
70   }
71 }
72
73
74 void FunctionSnapshot::insert(const Function& func) {
75   v8engine::Address codeAddr = func.getCodeAddress();
76   uint32_t codeLen = func.getCodeLength();
77   CHECK(codeAddr);
78   CHECK(codeLen);
79
80   Range range(codeAddr, codeLen);
81
82   removeAll(range);
83
84   std::pair<CodeMap::iterator, bool> res =
85     m_impl.insert(std::make_pair(range, func));
86   CHECK(res.second);
87
88   XDKLog("xdk: size=%d added addr=0x%x name=%s\n",
89          m_impl.size(), range.start(), func.getLogLine().c_str());
90 }
91
92
93 void FunctionSnapshot::remove(v8engine::Address codeAddr) {
94   if (!codeAddr) return;
95   CodeMap::iterator itr = m_impl.find(Range(codeAddr, 1));
96   if (itr != m_impl.end()) {
97     std::string name = itr->second.getLogLine();
98     uint32_t len = itr->first.length();
99     m_impl.erase(itr);
100     XDKLog("xdk: size=%d removed addr=0x%x name=%s\n",
101            m_impl.size(), codeAddr, len, name.c_str());
102   }
103 }
104
105
106 void FunctionSnapshot::move(v8engine::Address from, v8engine::Address to) {
107   if (!from || !to) return;
108   if (from == to) return;
109
110   CodeMap::iterator itr = m_impl.find(Range(from, 1));
111   if (itr == m_impl.end()) {
112     XDKLog("xdk: couldn't find a code to move from=0x%x to=0x%x\n", from, to);
113     return;
114   }
115   if (itr->first.start() != from) {
116     XDKLog("xdk: discarded move from=0x%x to=0x%x\n", from, to);
117     return;
118   }
119
120   uint32_t codeLen = itr->second.getCodeLength();
121   const LineMap& lines = itr->second.getLineMap();
122
123   // In case of CodeMoved we have to check that name contains the same code
124   // addr and code length as the input params and replace if they are different.
125   const std::string& orig = itr->second.getName();
126   std::string name = replaceAddress(orig, to);
127
128   const std::string& type = itr->second.getType();
129   Function toEntry(to, codeLen, name, type, &lines);
130
131   m_impl.erase(itr);
132
133   Range range(to, codeLen);
134   removeAll(range);
135
136   // Now ready to move
137
138   bool ok = m_impl.insert(std::make_pair(range, toEntry)).second;
139   CHECK(ok);
140
141   XDKLog("xdk: size=%d moved from=0x%x to=0x%x name=%s\n",
142          m_impl.size(), from, to, toEntry.getLogLine().c_str());
143 }
144
145 } }  // namespace xdk::internal