forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.cpp
More file actions
283 lines (247 loc) · 7.68 KB
/
Copy pathdebug.cpp
File metadata and controls
283 lines (247 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/debug/debug.h"
#include "hphp/runtime/vm/jit/mcgen.h"
#include "hphp/util/configs/eval.h"
#include "hphp/util/current-executable.h"
#include "hphp/util/roar.h"
#include <stdio.h>
#include <string.h>
#include <folly/debugging/symbolizer/Symbolizer.h>
#include <folly/portability/Unistd.h>
#include <folly/Demangle.h>
using namespace HPHP::jit;
namespace HPHP {
namespace Debug {
namespace { DebugInfo* s_info; }
void* DebugInfo::pidMapOverlayStart;
void* DebugInfo::pidMapOverlayEnd;
void initDebugInfo() {
s_info = new DebugInfo();
}
void destroyDebugInfo() {
if (s_info) {
delete s_info;
s_info = nullptr;
}
}
DebugInfo* DebugInfo::Get() {
return s_info;
}
DebugInfo::DebugInfo() {
m_perfMapName = folly::sformat("/tmp/perf-{}.map", getpid());
if (Cfg::Eval::PerfPidMap) {
m_perfMapEnabled = true;
// When running with ROAR, ROAR owns the pid-map file, and HHVM writes to it
// indirectly through ROAR.
if (!use_roar) {
m_perfMap = fopen(m_perfMapName.c_str(), "w");
}
}
if (Cfg::Eval::PerfJitDump) {
initPerfJitDump();
}
m_dataMapName = folly::sformat("/tmp/perf-data-{}.map", getpid());
if (Cfg::Eval::PerfDataMap) {
m_dataMap = fopen(m_dataMapName.c_str(), "w");
}
generatePidMapOverlay();
}
DebugInfo::~DebugInfo() {
if (m_perfMap) {
fclose(m_perfMap);
if (!Cfg::Eval::KeepPerfPidMap) {
unlink(m_perfMapName.c_str());
}
}
if (m_perfJitDump) {
closePerfJitDump();
if (!Cfg::Eval::KeepPerfPidMap) {
unlink(m_perfJitDumpName.c_str());
}
}
if (m_dataMap) {
fclose(m_dataMap);
if (!Cfg::Eval::KeepPerfPidMap) {
unlink(m_dataMapName.c_str());
}
}
}
void DebugInfo::generatePidMapOverlay() {
if (!m_perfMapEnabled || !pidMapOverlayStart) return;
struct SymInfo {
const char* name;
uintptr_t addr;
size_t size;
};
std::vector<SymInfo> sorted;
auto self = current_executable_path();
using folly::symbolizer::ElfFile;
ElfFile file;
using ElfSym = ElfW(Sym);
using ElfShdr = ElfW(Shdr);
if (file.openNoThrow(self.c_str()) != ElfFile::kSuccess) return;
file.iterateSectionsWithType(SHT_SYMTAB, [&] (const ElfShdr& section) {
auto strings = file.getSectionByIndex(section.sh_link);
if (!strings) return false;
file.iterateSymbolsWithType(section, STT_FUNC, [&] (const ElfSym& sym) {
if (sym.st_shndx == SHN_UNDEF) return false;
if (!sym.st_name) return false;
if (sym.st_value >= uintptr_t(pidMapOverlayStart) &&
sym.st_value <= uintptr_t(pidMapOverlayEnd)) {
sorted.push_back(SymInfo {
file.getString(*strings, sym.st_name),
sym.st_value,
sym.st_size});
}
return false;
});
return false;
});
std::sort(
sorted.begin(), sorted.end(),
[](const SymInfo& a, const SymInfo& b) {
if (a.addr != b.addr) return a.addr < b.addr;
if (a.size != b.size) return a.size > b.size;
return
strncmp("_ZN4HPHP", a.name, 8) &&
!strncmp("_ZN4HPHP", b.name, 8);
}
);
for (size_t i = 0; i < sorted.size(); i++) {
auto const& sym = sorted[i];
auto demangled = folly::demangle(sym.name);
unsigned size;
if (i + 1 < sorted.size()) {
auto const& s2 = sorted[i + 1];
size = s2.addr - sym.addr;
} else {
size = uintptr_t(pidMapOverlayEnd) - sym.addr;
}
if (!size) continue;
writeToPidMap(uint64_t(sym.addr), size, demangled.c_str());
}
return;
}
void DebugInfo::recordStub(TCRange range, const std::string& name) {
if (range.isAcold()) {
m_acoldDwarfInfo.addTracelet(range, name, nullptr, -1, false);
} else {
m_aDwarfInfo.addTracelet(range, name, nullptr, -1, false);
}
}
extern "C" __attribute__((weak))
int __roar_api_write_pid_map(uint64_t address, uint64_t size, const char* name);
void DebugInfo::writeToPidMap(uint64_t start, uint64_t size, const char* name) {
if (use_roar) {
__roar_api_write_pid_map(start, size, name);
} else {
fprintf(m_perfMap, "%lx %lx %s\n", start, size, name);
fflush(m_perfMap);
}
}
void DebugInfo::recordPerfMap(TCRange range, SrcKey sk, std::string name) {
if (!m_perfMapEnabled) return;
if (Cfg::Eval::ProfileBC) return;
if (name.empty()) {
name = lookupFunction(sk.func(), sk.prologue(),
Cfg::Eval::PerfPidMapIncludeFilePath);
}
auto const start = reinterpret_cast<uintptr_t>(range.begin());
writeToPidMap(start, range.size(), name.c_str());
//Dump the object code into the specified file
if (m_perfJitDump) {
perfJitDumpTrace(range.begin(), range.size(), name.c_str());
}
}
void DebugInfo::recordBCInstr(TCRange range, uint32_t op) {
static const char* opcodeName[] = {
#define O(name, imm, push, pop, flags) \
#name,
OPCODES
#undef O
};
static const char* highOpcodeName[] = {
"OpHighStart",
#define O(name) \
#name,
HIGH_OPCODES
#undef O
};
if (Cfg::Eval::ProfileBC) {
if (!m_perfMapEnabled) return;
const char* name;
if (op < Op_count) {
name = opcodeName[op];
} else {
name = highOpcodeName[op - OpHighStart];
}
auto const start = reinterpret_cast<uintptr_t>(range.begin());
writeToPidMap(start, range.size(), name);
}
}
void DebugInfo::recordTracelet(TCRange range, SrcKey sk) {
if (range.isAcold()) {
m_acoldDwarfInfo.addTracelet(range, std::nullopt, sk.func(), sk.lineNumber(),
sk.prologue());
} else {
m_aDwarfInfo.addTracelet(range, std::nullopt, sk.func(), sk.lineNumber(),
sk.prologue());
}
}
void DebugInfo::recordDataMap(const void* from, const void* to,
const std::string& desc) {
if (!mcgen::initialized()) return;
if (auto dataMap = Get()->m_dataMap) {
fprintf(dataMap, "%" PRIxPTR " %" PRIx64 " %s\n",
uintptr_t(from),
uint64_t((char*)to - (char*)from),
desc.c_str());
fflush(dataMap);
}
}
void DebugInfo::debugSync() {
m_aDwarfInfo.syncChunks();
m_acoldDwarfInfo.syncChunks();
}
std::string lookupFunction(const Func* f,
bool inPrologue,
bool pseudoWithFileName) {
// TODO: mangle the namespace and name?
std::string fname("PHP::");
if (pseudoWithFileName) {
fname += f->unit()->origFilepath()->data();
fname += "::";
}
if (!strcmp(f->name()->data(), "")) {
// TODO: should no longer happen
fname += "__pseudoMain";
return fname;
}
if (f->isClosureBody()) {
fname += f->baseCls()->name()->toCppString();
fname = fname.substr(0, fname.find(';'));
fname += "::__invoke";
} else {
fname += f->fullName()->data();
}
if (inPrologue) {
fname += "$prologue";
}
return fname;
}
}
}