/
Dumper.lua
152 lines (141 loc) · 5.21 KB
/
Dumper.lua
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
-- Mainly all LuaDbg code =P
local function Dump(file)
local _print = print
local indent = 1
local last = true
local function print(...) -- MWAHAHA HAX !!
local function patch(...)
local t = { ... }
local r = { }
for k, v in pairs(t) do r[k] = tostring(v) end
return unpack(r)
end
_print(("\t"):rep(indent) .. table.concat({patch(...)}, ""))
end
local function getSpec(chunk, instr, a, b, c)
local rules = instr.OpcodeParams
if rules[b] == 1 then
if a < chunk.Locals.Count then
if chunk.Locals[a].StartPC < c and chunk.Locals[a].EndPC >= c then
return "[" .. chunk.Locals[a].Name .."]"
elseif chunk.Locals[a].SPC == c then
return "new [" .. chunk.Locals[a].Name .."]"
end
end
elseif rules[b] == 2 then
local k = chunk.Constants[a]
if k.Type == "String" then
return "(\'" ..k.Value .."\')"
elseif k.Type == "nil" then
return "(nil)"
else
return "("..tostring(k.Value)..")";
end
elseif rules[b] == 3 then
if a >= 256 then
local k= chunk.Constants[a-256]
if k.Type == "String" then
return "(\'" ..k.Value .."\')"
elseif k.Type == "nil" then
return "(nil)"
else
return "("..tostring(k.Value)..")"
end
else
if a < chunk.Locals.Count then
if chunk.Locals[a].StartPC <= c and chunk.Locals[a].EndPC >= c then
return "[" .. chunk.Locals[a].Name .."]"
end
end
end
elseif rules[b] == 4 then
return "<"..(chunk.Upvalues[a] and chunk.Upvalues[a].Name or "Unknown Upvalue")..">"
elseif rules[b] == 5 then
return "to [" .. c + a + 1 .. "]"
end
return a
end
local function dumpInstruction(chunk, instr, i)
if instr.OpcodeType == "ABC" then
local a, b, c = instr.A, instr.B, instr.C
local _a = getSpec(chunk, instr, a, 1, i)
local _b = getSpec(chunk, instr, b, 2, i)
local _c = getSpec(chunk, instr, c, 3, i)
print("[", i, "] (Line ", instr.LineNumber or 0, ")\tOpcode: ", instr.Opcode, "\t",
a, "\t" ,b, "\t", c, "\t; ",_a, "\t ", _b, "\t ", _c)
elseif instr.OpcodeType == "ABx" then
local a, bx = instr.A, instr.Bx
local _a = getSpec(chunk, instr, a, 1, i)
local _bx = getSpec(chunk, instr, bx, 2, i)
print("[", i, "] (Line ", instr.LineNumber or 0, ")\tOpcode: ", instr.Opcode, "\t", a, "\t" ,bx,
"\t<nil>\t; ", _a, "\t ", _bx)
elseif instr.OpcodeType == "AsBx" then
local a, sbx = instr.A, instr.sBx
local _a, _sbx = getSpec(chunk, instr, a, 1, i), getSpec(chunk, instr, sbx, 2, i);
print("[", i, "] (Line ", instr.LineNumber or 0, ")\tOpcode: ", instr.Opcode, "\t", a, "\t", sbx, "\t<nil>\t; ", _a, "\t", _sbx)
end
end
local function dumpFunc(f)
print("[Function ", (f.Name and (f.Name == "" and "<Unnamed>" or f.Name) or "<Unnamed>"), "]")
indent = indent + 1
print("Lines: ", f.FirstLine, " - ", f.LastLine)
print("Upvalue count: ", f.UpvalueCount)
print("Argument count: ", f.ArgumentCount)
print("Vararg flag: ", f.Vararg)
print("MaxStackSize: ", f.MaxStackSize)
print("[Instructions, Count = ", f.Instructions.Count, "]")
indent = indent + 1
for i = 1, f.Instructions.Count do
dumpInstruction(f, f.Instructions[i - 1], i)
end
indent = indent - 1
print("[Constants, Count = ", f.Constants.Count, "]")
indent = indent + 1
for i = 1, f.Constants.Count do
local constant = f.Constants[i - 1]
if constant.Type == "Nil" then
print("[Constant 'nil', Type = Nil]")
else
print("[Constant '", constant.Value, "', Type = ", constant.Type, "]")
end
end
indent = indent - 1
print("[Locals, Count = ", f.Locals.Count, "]")
indent = indent + 1
for i = 1, f.Locals.Count do
local l = f.Locals[i - 1]
print("[Local '", l.Name, "', StartPC = ", l.StartPC, " EndPC = ", l.EndPC, "]")
end
indent = indent - 1
print("[Upvalues, Count = ", f.Upvalues.Count, "]")
indent = indent + 1
for i = 1, f.Upvalues.Count do
print("[Upvalue '", f.Upvalues[i - 1].Name, "']")
end
indent = indent - 1
print("[Prototypes, Count: ", f.Protos.Count, "]")
indent = indent + 1
for i = 1, f.Protos.Count do
dumpFunc(f.Protos[i - 1])
end
indent = indent - 2
end
print("[File Header]")
if file.Identifier == "\027Lua" then
print("Identifier: ", "<ESC>Lua")
else
print("Identifier (Invalid): ", file.Identifier)
end
print("Version: ", ("0x%02X"):format(file.Version))
print("Format: ", file.Format)
print("Little Endian: ", tostring(not file.BigEndian))
print("Integer Size: ", file.IntegerSize)
print("String Size: ", file.SizeT)
print("Instruction Size: ", file.InstructionSize)
print("Number Size: ", file.NumberSize)
print("Number Type: ", file.IsFloatingPoint and "Floating Point" or "Integer")
print("")
print("[Main function]")
dumpFunc(file.Main)
end
return Dump