-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathze_debug_info_collector.h
426 lines (351 loc) · 14.9 KB
/
ze_debug_info_collector.h
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_SAMPLES_ZE_DEBUG_INFO_ZE_DEBUG_INFO_COLLECTOR_H_
#define PTI_SAMPLES_ZE_DEBUG_INFO_ZE_DEBUG_INFO_COLLECTOR_H_
#include <level_zero/layers/zel_tracing_api.h>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "elf_parser.h"
#include "gen_binary_decoder.h"
#include "utils.h"
#include "ze_utils.h"
struct SourceLine {
uint32_t number;
std::string text;
};
struct SourceFileInfo {
uint32_t file_id;
std::string file_name;
std::vector<SourceLine> source_line_list;
};
struct KernelDebugInfo {
std::vector<Instruction> instruction_list;
std::vector<SourceMapping> line_info_list;
std::unordered_map<uint32_t, SourceFileInfo> source_info_list;
};
using KernelDebugInfoMap = std::map<std::string, KernelDebugInfo>;
class ZeDebugInfoCollector {
public: // User Interface
static ZeDebugInfoCollector* Create() {
ZeDebugInfoCollector* collector = new ZeDebugInfoCollector();
PTI_ASSERT(collector != nullptr);
ze_result_t status = ZE_RESULT_SUCCESS;
zel_tracer_desc_t tracer_desc = {ZEL_STRUCTURE_TYPE_TRACER_EXP_DESC, nullptr, collector};
zel_tracer_handle_t tracer = nullptr;
status = zelTracerCreate(&tracer_desc, &tracer);
if (status != ZE_RESULT_SUCCESS) {
std::cerr << "[WARNING] Unable to create Level Zero tracer" << std::endl;
delete collector;
return nullptr;
}
collector->EnableTracing(tracer);
return collector;
}
static void InstructionCallback(int32_t offset, void* data) {}
static void PrintKernelDebugInfo(std::string kernel_name,
const KernelDebugInfo& kernel_debug_info,
decltype(InstructionCallback)* callback = InstructionCallback,
void* callback_data = nullptr) {
PTI_ASSERT(!kernel_name.empty());
std::cerr << "===== Kernel: " << kernel_name << " =====" << std::endl;
const std::vector<Instruction>& instruction_list = kernel_debug_info.instruction_list;
PTI_ASSERT(instruction_list.size() > 0);
uint64_t last_instruction_address = instruction_list.back().offset;
const std::vector<SourceMapping>& line_info = kernel_debug_info.line_info_list;
PTI_ASSERT(line_info.size() > 0);
const auto& source_info_list = kernel_debug_info.source_info_list;
PTI_ASSERT(source_info_list.size() > 0);
// Print instructions with no corresponding file
std::cerr << "=== File: Unknown ===" << std::endl;
for (auto& instruction : instruction_list) {
bool found = false;
for (size_t l = 0; l < line_info.size(); ++l) {
uint64_t start_address = line_info[l].address;
uint64_t end_address =
(l + 1 < line_info.size()) ? line_info[l + 1].address : last_instruction_address;
if (instruction.offset >= start_address && instruction.offset < end_address) {
found = true;
break;
}
}
if (!found) {
std::cerr << "\t\t["
<< "0x" << std::setw(5) << std::setfill('0') << std::hex << std::uppercase
<< instruction.offset << "] " << instruction.text;
callback(instruction.offset, callback_data);
std::cerr << std::endl;
}
}
// Print info per file
for (auto& source_info : source_info_list) {
std::cerr << "=== File: " << source_info.second.file_name.c_str() << " ===" << std::endl;
const std::vector<SourceLine> line_list = source_info.second.source_line_list;
PTI_ASSERT(line_list.size() > 0);
// Print instructions with no corresponding source line
for (size_t l = 0; l < line_info.size(); ++l) {
if (line_info[l].line == 0) {
uint64_t start_address = line_info[l].address;
uint64_t end_address =
(l + 1 < line_info.size()) ? line_info[l + 1].address : last_instruction_address;
for (auto instruction : instruction_list) {
if (instruction.offset >= start_address && instruction.offset < end_address &&
source_info.second.file_id == line_info[l].file_id) {
std::cerr << "\t\t["
<< "0x" << std::setw(5) << std::setfill('0') << std::hex << std::uppercase
<< instruction.offset << "] " << instruction.text;
callback(instruction.offset, callback_data);
std::cerr << std::endl;
}
}
}
}
// Print instructions for corresponding source line
for (const auto& line : line_list) {
std::cerr << "[" << std::setw(5) << std::setfill(' ') << std::dec << line.number << "] "
<< line.text << std::endl;
for (size_t l = 0; l < line_info.size(); ++l) {
if (line_info[l].line == line.number) {
uint64_t start_address = line_info[l].address;
uint64_t end_address =
(l + 1 < line_info.size()) ? line_info[l + 1].address : last_instruction_address;
for (auto instruction : instruction_list) {
if (instruction.offset >= start_address && instruction.offset < end_address &&
source_info.second.file_id == line_info[l].file_id) {
std::cerr << "\t\t["
<< "0x" << std::setw(5) << std::setfill('0') << std::hex << std::uppercase
<< instruction.offset << "] " << instruction.text;
callback(instruction.offset, callback_data);
std::cerr << std::endl;
}
}
}
}
}
}
std::cerr << std::endl;
}
~ZeDebugInfoCollector() {
if (tracer_ != nullptr) {
ze_result_t status = zelTracerDestroy(tracer_);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
}
void DisableTracing() {
PTI_ASSERT(tracer_ != nullptr);
ze_result_t status = ZE_RESULT_SUCCESS;
status = zelTracerSetEnabled(tracer_, false);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
const KernelDebugInfoMap& GetKernelDebugInfoMap() const { return kernel_debug_info_map_; }
private: // Implementation Details
ZeDebugInfoCollector() {}
void EnableTracing(zel_tracer_handle_t tracer) {
PTI_ASSERT(tracer != nullptr);
tracer_ = tracer;
zet_core_callbacks_t epilogue_callbacks{};
epilogue_callbacks.Kernel.pfnCreateCb = OnExitKernelCreate;
ze_result_t status = ZE_RESULT_SUCCESS;
status = zelTracerSetEpilogues(tracer_, &epilogue_callbacks);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
status = zelTracerSetEnabled(tracer_, true);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
void AddKernel(std::string name, const std::vector<Instruction>& instruction_list,
const std::vector<SourceMapping>& line_info_list,
const std::unordered_map<uint32_t, SourceFileInfo>& source_info_list) {
PTI_ASSERT(!name.empty());
PTI_ASSERT(instruction_list.size() > 0);
PTI_ASSERT(line_info_list.size() > 0);
PTI_ASSERT(source_info_list.size() > 0);
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(kernel_debug_info_map_.count(name) == 0);
kernel_debug_info_map_[name] = {instruction_list, line_info_list, source_info_list};
}
static std::vector<SourceLine> ReadSourceFile(const std::string& file_path) {
std::string abs_path = file_path;
if (abs_path[0] == '.') {
abs_path = utils::GetExecutablePath() + abs_path;
}
std::ifstream file(abs_path);
if (!file.is_open()) {
return std::vector<SourceLine>();
}
std::vector<SourceLine> line_list;
uint32_t number = 1;
std::string text;
while (std::getline(file, text)) {
line_list.push_back({number, text});
++number;
}
return line_list;
}
private: // Callbacks
static void OnExitKernelCreate(ze_kernel_create_params_t* params, ze_result_t result,
void* global_user_data, void** instance_user_data) {
if (result != ZE_RESULT_SUCCESS) {
return;
}
ze_result_t status = ZE_RESULT_SUCCESS;
ze_module_handle_t module = *(params->phModule);
PTI_ASSERT(module != nullptr);
const ze_kernel_desc_t* desc = *(params->pdesc);
PTI_ASSERT(desc != nullptr);
const char* kernel_name = desc->pKernelName;
PTI_ASSERT(kernel_name != nullptr);
size_t debug_info_size = 0;
status = zetModuleGetDebugInfo(module, ZET_MODULE_DEBUG_INFO_FORMAT_ELF_DWARF, &debug_info_size,
nullptr);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (debug_info_size == 0) {
std::cerr << "[WARNING] Unable to find kernel symbols" << std::endl;
return;
}
std::vector<uint8_t> debug_info(debug_info_size);
status = zetModuleGetDebugInfo(module, ZET_MODULE_DEBUG_INFO_FORMAT_ELF_DWARF, &debug_info_size,
debug_info.data());
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
pti_result res;
elf_parser_handle_t parserHandle = nullptr;
res = ptiElfParserCreate(debug_info.data(), static_cast<uint32_t>(debug_info.size()),
&parserHandle);
if (res != PTI_SUCCESS || parserHandle == nullptr) {
std::cerr << "[WARNING] : Cannot create elf parser" << std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
bool is_valid = false;
res = ptiElfParserIsValid(parserHandle, &is_valid);
if (res != PTI_SUCCESS || !is_valid) {
std::cerr << "[WARNING] : Constructed Elf parser is not valid" << std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
uint32_t kernel_num = 0;
res = ptiElfParserGetKernelNames(parserHandle, 0, nullptr, &kernel_num);
if (res != PTI_SUCCESS) {
std::cerr << "Error: Failed to get kernel names" << std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
if (kernel_num == 0) {
std::cerr << "[WARNING] : No kernels found" << std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
std::vector<const char*> kernel_names(kernel_num);
res = ptiElfParserGetKernelNames(parserHandle, kernel_num, kernel_names.data(), nullptr);
if (res != PTI_SUCCESS) {
std::cerr << "Error: Failed to get kernel names" << std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
for (uint32_t kernel_idx = 0; kernel_idx < kernel_names.size(); kernel_idx++) {
if (kernel_name != std::string(kernel_names[kernel_idx])) {
continue;
}
uint32_t binary_size = 0;
const uint8_t* binary = nullptr;
uint64_t kernel_address = 0;
res = ptiElfParserGetBinaryPtr(parserHandle, kernel_idx, &binary, &binary_size,
&kernel_address);
if (res != PTI_SUCCESS || binary_size == 0) {
std::cerr << "[WARNING] : Unable to get GEN binary for kernel: " << kernel_name
<< std::endl;
continue;
}
uint32_t gfx_core = 0;
res = ptiElfParserGetGfxCore(parserHandle, &gfx_core);
if (res != PTI_SUCCESS || gfx_core == 0) {
std::cerr << "[WARNING] : Unable to get GEN binary version for kernel: " << kernel_name
<< std::endl;
continue;
}
GenBinaryDecoder decoder(binary, binary_size, GenBinaryDecoder::GfxCoreToIgaGen(gfx_core));
if (!decoder.IsValid()) {
std::cerr << "[WARNING] : Unable to create decoder for kernel: " << kernel_name
<< std::endl;
continue;
}
std::vector<Instruction> instruction_list = decoder.Disassemble();
if (instruction_list.size() == 0) {
std::cerr << "[WARNING] : Unable to decode kernel binary for kernel: " << kernel_name
<< std::endl;
continue;
}
/// Apply base addr to all instructions
for (auto& instruction : instruction_list) {
instruction.offset += kernel_address;
}
uint32_t mapping_num = 0;
res = ptiElfParserGetSourceMapping(parserHandle, kernel_idx, 0, nullptr, &mapping_num);
if (res != PTI_SUCCESS) {
std::cerr << "[WARNING] : Failed to get source mapping for kernel ID: " << kernel_idx
<< std::endl;
continue;
}
std::vector<SourceMapping> line_info_list(mapping_num);
res = ptiElfParserGetSourceMapping(parserHandle, kernel_idx, mapping_num,
line_info_list.data(), nullptr);
if (res != PTI_SUCCESS) {
std::cerr << "[WARNING] : No source mapping found for kernel ID: " << kernel_idx
<< std::endl;
continue;
}
std::unordered_map<uint32_t, SourceFileInfo> source_info_list;
for (const auto& line : line_info_list) {
if (source_info_list.find(line.file_id) != source_info_list.end()) {
continue;
}
std::filesystem::path fullpath =
std::filesystem::path(std::string(line.file_path)) / line.file_name;
std::vector<SourceLine> line_list = ReadSourceFile(fullpath);
if (line_list.size() == 0) {
std::cerr << "[WARNING] : Unable to find target source file for kernel: '" << kernel_name
<< "' : " << std::string(line.file_path) + line.file_name << std::endl;
continue;
}
PTI_ASSERT(line.file_id < (std::numeric_limits<uint32_t>::max)());
source_info_list[line.file_id] = {static_cast<uint32_t>(line.file_id), line.file_name,
line_list};
}
if (source_info_list.size() == 0) {
std::cerr << "[WARNING] : Unable to find kernel source files for kernel: " << kernel_name
<< std::endl;
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
return;
}
ZeDebugInfoCollector* collector = reinterpret_cast<ZeDebugInfoCollector*>(global_user_data);
PTI_ASSERT(collector != nullptr);
collector->AddKernel(kernel_name, instruction_list, line_info_list, source_info_list);
break;
}
res = ptiElfParserDestroy(&parserHandle);
PTI_ASSERT(res == PTI_SUCCESS);
PTI_ASSERT(parserHandle == nullptr);
}
private:
zel_tracer_handle_t tracer_ = nullptr;
std::mutex lock_;
KernelDebugInfoMap kernel_debug_info_map_;
};
#endif // PTI_SAMPLES_ZE_DEBUG_INFO_ZE_DEBUG_INFO_COLLECTOR_H_