-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_harness_debug.cpp
More file actions
334 lines (262 loc) · 12 KB
/
test_harness_debug.cpp
File metadata and controls
334 lines (262 loc) · 12 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
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
/*
*
* Copyright (C) 2021-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include <thread>
#include "test_harness/test_harness.hpp"
#include <level_zero/ze_api.h>
#include "utils/utils.hpp"
#include "test_harness/zet_intel_gpu_debug.h"
namespace lzt = level_zero_tests;
namespace level_zero_tests {
std::map<zet_debug_event_type_t, std::string> debuggerEventTypeString = {
{ZET_DEBUG_EVENT_TYPE_INVALID, "ZET_DEBUG_EVENT_TYPE_INVALID"},
{ZET_DEBUG_EVENT_TYPE_DETACHED, "ZET_DEBUG_EVENT_TYPE_DETACHED"},
{ZET_DEBUG_EVENT_TYPE_PROCESS_ENTRY, "ZET_DEBUG_EVENT_TYPE_PROCESS_ENTRY"},
{ZET_DEBUG_EVENT_TYPE_PROCESS_EXIT, "ZET_DEBUG_EVENT_TYPE_PROCESS_EXIT"},
{ZET_DEBUG_EVENT_TYPE_MODULE_LOAD, "ZET_DEBUG_EVENT_TYPE_MODULE_LOAD"},
{ZET_DEBUG_EVENT_TYPE_MODULE_UNLOAD, "ZET_DEBUG_EVENT_TYPE_MODULE_UNLOAD"},
{ZET_DEBUG_EVENT_TYPE_THREAD_STOPPED,
"ZET_DEBUG_EVENT_TYPE_THREAD_STOPPED"},
{ZET_DEBUG_EVENT_TYPE_THREAD_UNAVAILABLE,
"ZET_DEBUG_EVENT_TYPE_THREAD_UNAVAILABLE"},
{ZET_DEBUG_EVENT_TYPE_PAGE_FAULT, "ZET_DEBUG_EVENT_TYPE_PAGE_FAULT"},
{ZET_DEBUG_EVENT_TYPE_FORCE_UINT32, "ZET_DEBUG_EVENT_TYPE_FORCE_UINT32 "}};
zet_device_debug_properties_t get_debug_properties(ze_device_handle_t device) {
zet_device_debug_properties_t properties = {};
properties.stype = ZET_STRUCTURE_TYPE_DEVICE_DEBUG_PROPERTIES;
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDeviceGetDebugProperties(device, &properties));
return properties;
}
std::unordered_map<zet_debug_session_handle_t, bool> sessionsAttachStatus;
zet_debug_session_handle_t debug_attach(const ze_device_handle_t &device,
const zet_debug_config_t &debug_config,
uint32_t timeout) {
zet_debug_session_handle_t debug_session = {};
ze_result_t result;
std::chrono::high_resolution_clock::time_point startTime =
std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point loopTime =
std::chrono::high_resolution_clock::now();
do {
result = zetDebugAttach(device, &debug_config, &debug_session);
if (result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE ||
result == ZE_RESULT_ERROR_DEVICE_LOST) {
break;
}
if (result != ZE_RESULT_SUCCESS) {
LOG_DEBUG << "Attach failed, sleeping and retrying...";
std::this_thread::sleep_for(std::chrono::milliseconds(350));
loopTime = std::chrono::high_resolution_clock::now();
}
} while (result != ZE_RESULT_SUCCESS &&
(std::chrono::duration_cast<std::chrono::duration<double>>(loopTime -
startTime)
.count() < timeout));
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
if (ZE_RESULT_SUCCESS == result) {
sessionsAttachStatus[debug_session] = true;
}
return debug_session;
}
void debug_detach(const zet_debug_session_handle_t &debug_session) {
ze_result_t result = zetDebugDetach(debug_session);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
if (ZE_RESULT_SUCCESS == result) {
sessionsAttachStatus[debug_session] = false;
}
}
zet_debug_event_t
debug_read_event(const zet_debug_session_handle_t &debug_session) {
zet_debug_event_t debug_event = {};
auto timeout = std::numeric_limits<uint64_t>::max();
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugReadEvent(debug_session, timeout, &debug_event));
return debug_event;
}
ze_result_t debug_read_event(const zet_debug_session_handle_t &debug_session,
zet_debug_event_t &debugEvent, uint64_t timeout,
bool allowTimeout) {
zet_debug_event_t event = {};
ze_result_t result = zetDebugReadEvent(debug_session, timeout, &event);
// Expect that timeout expired if not successful
if (allowTimeout && ZE_RESULT_SUCCESS != result) {
LOG_INFO << "[Debugger] zetDebugReadEvent timed out";
EXPECT_EQ(ZE_RESULT_NOT_READY, result);
} else {
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
}
debugEvent = event;
return result;
}
void debug_ack_event(const zet_debug_session_handle_t &debug_session,
const zet_debug_event_t *debug_event) {
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugAcknowledgeEvent(debug_session, debug_event));
}
void debug_interrupt(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugInterrupt(debug_session, device_thread));
}
void debug_resume(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugResume(debug_session, device_thread));
}
void printRegSetProperties(zet_debug_regset_properties_t regSet) {
LOG_DEBUG << "[Debugger] Reg Set Type: " << regSet.type;
LOG_DEBUG << "[Debugger] Reg Set Version: " << regSet.version;
LOG_DEBUG << "[Debugger] Reg Set GeneralFlags: " << regSet.generalFlags;
LOG_DEBUG << "[Debugger] Reg Set deviceFlags: " << regSet.deviceFlags;
LOG_DEBUG << "[Debugger] Reg Set count: " << regSet.count;
LOG_DEBUG << "[Debugger] Reg Set bit size: " << regSet.bitSize;
LOG_DEBUG << "[Debugger] Reg Set byte size: " << regSet.byteSize;
return;
}
bool get_register_set_props(ze_device_handle_t device,
zet_debug_regset_type_intel_gpu_t type,
zet_debug_regset_properties_t ®) {
uint32_t nRegSets = 0;
zetDebugGetRegisterSetProperties(device, &nRegSets, nullptr);
zet_debug_regset_properties_t *pRegSets =
new zet_debug_regset_properties_t[nRegSets];
for (int i = 0; i < nRegSets; i++) {
pRegSets[i] = {ZET_STRUCTURE_TYPE_DEBUG_REGSET_PROPERTIES, nullptr};
}
zetDebugGetRegisterSetProperties(device, &nRegSets, pRegSets);
bool found = false;
for (int i = 0; i < nRegSets; i++) {
if (pRegSets[i].type == type) {
printRegSetProperties(pRegSets[i]);
reg = pRegSets[i];
found = true;
break;
}
}
delete[] pRegSets;
return found;
}
void clear_exceptions(const ze_device_handle_t &device,
const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread) {
size_t reg_size_in_bytes = 0;
zet_debug_regset_properties_t cr_reg_prop;
ASSERT_TRUE(get_register_set_props(device, ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU,
cr_reg_prop));
reg_size_in_bytes = cr_reg_prop.count * cr_reg_prop.byteSize;
uint8_t *cr_values = new uint8_t[reg_size_in_bytes];
uint32_t *uint32_values = (uint32_t *)cr_values;
ASSERT_EQ(zetDebugReadRegisters(debug_session, device_thread,
ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0,
cr_reg_prop.count, cr_values),
ZE_RESULT_SUCCESS);
uint32_values[1] &= ~((1 << 26) | (1 << 30));
ASSERT_EQ(zetDebugWriteRegisters(debug_session, device_thread,
ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0,
cr_reg_prop.count, cr_values),
ZE_RESULT_SUCCESS);
}
void debug_read_memory(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread,
const zet_debug_memory_space_desc_t &desc, size_t size,
void *buffer) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugReadMemory(debug_session, device_thread,
&desc, size, buffer));
}
void debug_write_memory(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread,
const zet_debug_memory_space_desc_t &desc, size_t size,
const void *buffer) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugWriteMemory(debug_session, device_thread,
&desc, size, buffer));
}
uint32_t get_register_set_properties_count(const ze_device_handle_t &device) {
uint32_t count = 0;
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugGetRegisterSetProperties(device, &count, nullptr));
return count;
}
std::vector<zet_debug_regset_properties_t>
get_register_set_properties(const ze_device_handle_t &device) {
auto count = get_register_set_properties_count(device);
std::vector<zet_debug_regset_properties_t> properties(
count, {ZET_STRUCTURE_TYPE_DEBUG_REGSET_PROPERTIES});
auto device_initial = device;
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugGetRegisterSetProperties(
device, &count, properties.data()));
LOG_DEBUG << "[Debugger] Found " << count << " regsets";
EXPECT_EQ(device, device_initial);
EXPECT_NE(properties.data(), nullptr);
return properties;
}
std::vector<zet_debug_regset_properties_t> get_thread_register_set_properties(
const zet_debug_session_handle_t &debug_session,
const ze_device_handle_t &device, const ze_device_thread_t &thread) {
uint32_t count = 0;
auto device_initial = device;
EXPECT_EQ(ZE_RESULT_SUCCESS, zetDebugGetThreadRegisterSetProperties(
debug_session, thread, &count, nullptr));
std::vector<zet_debug_regset_properties_t> properties(
count, {ZET_STRUCTURE_TYPE_DEBUG_REGSET_PROPERTIES});
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugGetThreadRegisterSetProperties(debug_session, thread,
&count, properties.data()));
EXPECT_EQ(device, device_initial);
return properties;
}
void debug_read_registers(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread,
const uint32_t type, const uint32_t start,
const uint32_t count, void *buffer) {
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugReadRegisters(debug_session, device_thread, type, start,
count, buffer));
}
void debug_write_registers(const zet_debug_session_handle_t &debug_session,
const ze_device_thread_t &device_thread,
const uint32_t type, const uint32_t start,
const uint32_t count, void *buffer) {
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetDebugWriteRegisters(debug_session, device_thread, type, start,
count, buffer));
}
std::vector<uint8_t> get_debug_info(const zet_module_handle_t &module_handle) {
size_t size = 0;
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetModuleGetDebugInfo(module_handle,
ZET_MODULE_DEBUG_INFO_FORMAT_ELF_DWARF, &size,
nullptr));
std::vector<uint8_t> debug_info(size);
EXPECT_EQ(ZE_RESULT_SUCCESS,
zetModuleGetDebugInfo(module_handle,
ZET_MODULE_DEBUG_INFO_FORMAT_ELF_DWARF, &size,
debug_info.data()));
return debug_info;
}
bool is_heapless_mode(ze_device_thread_t stopped_thread,
ze_device_handle_t &device_handle,
zet_debug_session_handle_t debug_session) {
uint8_t *mode_values = nullptr;
bool result = false;
std::vector<zet_debug_regset_properties_t> regset_properties =
lzt::get_register_set_properties(device_handle);
for (auto ®ister_set : regset_properties) {
if (register_set.type == ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU) {
auto reg_size_in_bytes = register_set.count * register_set.byteSize;
mode_values = new uint8_t[reg_size_in_bytes];
EXPECT_EQ(
zetDebugReadRegisters(debug_session, stopped_thread,
ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU, 0,
register_set.count, mode_values),
ZE_RESULT_SUCCESS);
uint32_t *uint32_t_values = (uint32_t *)mode_values;
LOG_DEBUG << "[Debugger] mode value: %u " << uint32_t_values[0];
result = (uint32_t_values[0] & ZET_DEBUG_MODE_FLAG_HEAPLESS);
}
}
return result;
}
} // namespace level_zero_tests