Repository navigation
Expand file tree
/
Copy pathmemory_tracer.lua
More file actions
68 lines (53 loc) · 1.98 KB
/
Copy pathmemory_tracer.lua
File metadata and controls
68 lines (53 loc) · 1.98 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
-- memory access tracer with values
-- monitors memory read and write operations with detailed logging
local read_count = 0
local write_count = 0
local tracer = {}
local function format_value(access)
if not access.value_known or access.value == nil then
return "<??>"
end
local hex = w1.util.format_hex(access.value, {prefix = true, width = access.size * 2})
return hex or tostring(access.value)
end
local function log_memory_access(access_type, count, pc, address, size, value, instruction)
local msg = string.format("%s #%d @ %s [%s] size=%d value=%s inst: %s",
access_type,
count,
w1.util.format_address(pc),
w1.util.format_address(address),
size,
value,
instruction)
w1.log.info(msg)
end
local function on_instruction(vm, gpr, fpr)
local memory_accesses = w1.mem.accesses(vm)
if #memory_accesses > 0 then
local pc = w1.reg.pc(gpr) or 0
local instruction = w1.inst.disasm(vm) or "<unknown>"
for _, access in ipairs(memory_accesses) do
local formatted_value = format_value(access)
if access.is_read then
read_count = read_count + 1
log_memory_access("read", read_count, pc, access.address, access.size, formatted_value, instruction)
end
if access.is_write then
write_count = write_count + 1
log_memory_access("write", write_count, pc, access.address, access.size, formatted_value, instruction)
end
end
end
return w1.enum.vm_action.CONTINUE
end
function tracer.init()
w1.on(w1.event.INSTRUCTION_POST, on_instruction)
end
function tracer.shutdown()
local total_operations = read_count + write_count
w1.log.info("memory operations summary:")
w1.log.info(string.format(" reads: %d", read_count))
w1.log.info(string.format(" writes: %d", write_count))
w1.log.info(string.format(" total: %d", total_operations))
end
return tracer