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 #include "xdk-code-map.h"
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"
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);
32 if (!first.size() || !end.size()) return str;
36 << std::showbase << std::hex << static_cast<void*>(addr) << end;
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) {
47 // Can't be empty because it's came from CodeCreation(...) events
51 if (lineMap && lineMap->getSize()) m_lineMap = *lineMap;
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);
61 XDKLog("xdk: %d ranges were overlapped and removed\n", num);
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());
69 m_impl.erase(low, up);
74 void FunctionSnapshot::insert(const Function& func) {
75 v8engine::Address codeAddr = func.getCodeAddress();
76 uint32_t codeLen = func.getCodeLength();
80 Range range(codeAddr, codeLen);
84 std::pair<CodeMap::iterator, bool> res =
85 m_impl.insert(std::make_pair(range, func));
88 XDKLog("xdk: size=%d added addr=0x%x name=%s\n",
89 m_impl.size(), range.start(), func.getLogLine().c_str());
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();
100 XDKLog("xdk: size=%d removed addr=0x%x name=%s\n",
101 m_impl.size(), codeAddr, len, name.c_str());
106 void FunctionSnapshot::move(v8engine::Address from, v8engine::Address to) {
107 if (!from || !to) return;
108 if (from == to) return;
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);
115 if (itr->first.start() != from) {
116 XDKLog("xdk: discarded move from=0x%x to=0x%x\n", from, to);
120 uint32_t codeLen = itr->second.getCodeLength();
121 const LineMap& lines = itr->second.getLineMap();
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);
128 const std::string& type = itr->second.getType();
129 Function toEntry(to, codeLen, name, type, &lines);
133 Range range(to, codeLen);
138 bool ok = m_impl.insert(std::make_pair(range, toEntry)).second;
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());
145 } } // namespace xdk::internal