1 ----------------------------------------------------------------------------
2 -- LuaJIT compiler dump module.
4 -- Copyright (C) 2005-2011 Mike Pall. All rights reserved.
5 -- Released under the MIT license. See Copyright Notice in luajit.h
6 ----------------------------------------------------------------------------
8 -- This module can be used to debug the JIT compiler itself. It dumps the
9 -- code representations and structures used in various compiler stages.
13 -- luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)"
14 -- luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R
15 -- luajit -jdump=is myapp.lua | less -R
16 -- luajit -jdump=-b myapp.lua
17 -- luajit -jdump=+aH,myapp.html myapp.lua
18 -- luajit -jdump=ixT,myapp.dump myapp.lua
20 -- The first argument specifies the dump mode. The second argument gives
21 -- the output file name. Default output is to stdout, unless the environment
22 -- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the
25 -- Different features can be turned on or off with the dump mode. If the
26 -- mode starts with a '+', the following features are added to the default
27 -- set of features; a '-' removes them. Otherwise the features are replaced.
29 -- The following dump features are available (* marks the default):
31 -- * t Print a line for each started, ended or aborted trace (see also -jv).
32 -- * b Dump the traced bytecode.
33 -- * i Dump the IR (intermediate representation).
34 -- r Augment the IR with register/stack slots.
35 -- s Dump the snapshot map.
36 -- * m Dump the generated machine code.
37 -- x Print each taken trace exit.
38 -- X Print each taken trace exit and the contents of all registers.
40 -- The output format can be set with the following characters:
42 -- T Plain text output.
43 -- A ANSI-colored text output
44 -- H Colorized HTML + CSS output.
46 -- The default output format is plain text. It's set to ANSI-colored text
47 -- if the COLORTERM variable is set. Note: this is independent of any output
48 -- redirection, which is actually considered a feature.
50 -- You probably want to use less -R to enjoy viewing ANSI-colored text from
51 -- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R"
53 ------------------------------------------------------------------------------
55 -- Cache some library functions and objects.
56 local jit
= require("jit")
57 assert(jit
.version_num
== 20000, "LuaJIT core/library version mismatch")
58 local jutil
= require("jit.util")
59 local vmdef
= require("jit.vmdef")
60 local funcinfo
, funcbc
= jutil
.funcinfo
, jutil
.funcbc
61 local traceinfo
, traceir
, tracek
= jutil
.traceinfo
, jutil
.traceir
, jutil
.tracek
62 local tracemc
, tracesnap
= jutil
.tracemc
, jutil
.tracesnap
63 local traceexitstub
, ircalladdr
= jutil
.traceexitstub
, jutil
.ircalladdr
64 local bit
= require("bit")
65 local band
, shl
, shr
= bit
.band
, bit
.lshift
, bit
.rshift
66 local sub
, gsub, format = string.sub
, string.gsub, string.format
67 local byte
, char
, rep
= string.byte
, string.char
, string.rep
68 local type, tostring = type, tostring
69 local stdout
, stderr
= io
.stdout
, io
.stderr
71 -- Load other modules on-demand.
74 -- Active flag, output file handle and dump mode.
75 local active
, out
, dumpmode
77 ------------------------------------------------------------------------------
82 -- Fill symbol table with trace exit addresses.
83 local function fillsymtab(nexit
)
86 local ircall
= vmdef
.ircall
87 for i
=0,#ircall
do t
[ircalladdr(i
)] = ircall
[i
] end
89 if nexit
> nexitsym
then
90 for i
=nexitsym
,nexit
-1 do
91 local addr
= traceexitstub(i
)
92 if addr
== nil then nexit
= 1000000; break end
100 local function dumpwrite(s
)
104 -- Disassemble machine code.
105 local function dump_mcode(tr
)
106 local info
= traceinfo(tr
)
107 if not info
then return end
108 local mcode
, addr
, loop
= tracemc(tr
)
109 if not mcode
then return end
110 if not disass
then disass
= require("jit.dis_"..jit
.arch
) end
111 out
:write("---- TRACE ", tr
, " mcode ", #mcode
, "\n")
112 local ctx
= disass
.create(mcode
, addr
, dumpwrite
)
114 ctx
.symtab
= fillsymtab(info
.nexit
)
116 symtab
[addr
+loop
] = "LOOP"
118 out
:write("->LOOP:\n")
119 ctx
:disass(loop
, #mcode
-loop
)
120 symtab
[addr
+loop
] = nil
122 ctx
:disass(0, #mcode
)
126 ------------------------------------------------------------------------------
128 local irtype_text
= {
155 local colortype_ansi
= {
182 local function colorize_text(s
, t
)
186 local function colorize_ansi(s
, t
)
187 return format(colortype_ansi
[t
], s
)
190 local irtype_ansi
= setmetatable({},
191 { __index
= function(tab
, t
)
192 local s
= colorize_ansi(irtype_text
[t
], t
); tab
[t
] = s
; return s
; end })
194 local html_escape
= { ["<"] = "<", [">"] = ">", ["&"] = "&", }
196 local function colorize_html(s
, t
)
197 s
= gsub(s
, "[<>&]", html_escape
)
198 return format('<span class="irt_%s">%s</span>', irtype_text
[t
], s
)
201 local irtype_html
= setmetatable({},
202 { __index
= function(tab
, t
)
203 local s
= colorize_html(irtype_text
[t
], t
); tab
[t
] = s
; return s
; end })
205 local header_html
= [[
206 <style type="text/css">
207 background { background: #ffffff; color: #000000; }
212 border: 1px solid #bfcfff;
217 span.irt_str { color: #00a000; }
218 span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }
219 span.irt_tab { color: #c00000; }
220 span.irt_udt, span.irt_lud { color: #00c0c0; }
221 span.irt_num { color: #4040c0; }
222 span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }
226 local colorize
, irtype
228 -- Lookup tables to convert some literals into names.
230 ["SLOAD "] = setmetatable({}, { __index
= function(t
, mode
)
232 if band(mode
, 1) ~= 0 then s
= s
.."P" end
233 if band(mode
, 2) ~= 0 then s
= s
.."F" end
234 if band(mode
, 4) ~= 0 then s
= s
.."T" end
235 if band(mode
, 8) ~= 0 then s
= s
.."C" end
236 if band(mode
, 16) ~= 0 then s
= s
.."R" end
237 if band(mode
, 32) ~= 0 then s
= s
.."I" end
241 ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", },
242 ["CONV "] = setmetatable({}, { __index
= function(t
, mode
)
243 local s
= irtype
[band(mode
, 31)]
244 s
= irtype
[band(shr(mode
, 5), 31)].."."..s
245 if band(mode
, 0x400) ~= 0 then s
= s
.." trunc"
246 elseif band(mode
, 0x800) ~= 0 then s
= s
.." sext" end
247 local c
= shr(mode
, 14)
248 if c
== 2 then s
= s
.." index" elseif c
== 3 then s
= s
.." check" end
252 ["FLOAD "] = vmdef
.irfield
,
253 ["FREF "] = vmdef
.irfield
,
254 ["FPMATH"] = vmdef
.irfpm
,
257 local function ctlsub(c
)
258 if c
== "\n" then return "\\n"
259 elseif c
== "\r" then return "\\r"
260 elseif c
== "\t" then return "\\t"
261 elseif c
== "\r" then return "\\r"
262 else return format("\\%03d", byte(c
))
266 local function fmtfunc(func
, pc
)
267 local fi
= funcinfo(func
, pc
)
271 return vmdef
.ffnames
[fi
.ffid
]
273 return format("C:%x", fi
.addr
)
279 local function formatk(tr
, idx
)
280 local k
, t
, slot
= tracek(tr
, idx
)
283 if tn
== "number" then
284 if k
== 2^
52+2^
51 then
287 s
= format("%+.14g", k
)
289 elseif tn
== "string" then
290 s
= format(#k
> 20 and '"%.20s"~' or '"%s"', gsub(k
, "%c", ctlsub
))
291 elseif tn
== "function" then
293 elseif tn
== "table" then
294 s
= format("{%p}", k
)
295 elseif tn
== "userdata" then
297 s
= format("userdata:%p", k
)
299 s
= format("[%p]", k
)
300 if s
== "[0x00000000]" then s
= "NULL" end
302 elseif t
== 21 then -- int64_t
303 s
= sub(tostring(k
), 1, -3)
304 if sub(s
, 1, 1) ~= "-" then s
= "+"..s
end
306 s
= tostring(k
) -- For primitives.
308 s
= colorize(format("%-4s", s
), t
)
310 s
= format("%s @%d", s
, slot
)
315 local function printsnap(tr
, snap
)
319 if shr(sn
, 24) == s
then
321 local ref
= band(sn
, 0xffff) - 0x8000 -- REF_BIAS
323 out
:write(formatk(tr
, ref
))
324 elseif band(sn
, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM
325 out
:write(colorize(format("%04d/%04d", ref
, ref
+1), 14))
327 local m
, ot
, op1
, op2
= traceir(tr
, ref
)
328 out
:write(colorize(format("%04d", ref
), band(ot
, 31)))
330 out
:write(band(sn
, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
338 -- Dump snapshots (not interleaved with IR).
339 local function dump_snap(tr
)
340 out
:write("---- TRACE ", tr
, " snapshots\n")
341 for i
=0,1000000000 do
342 local snap
= tracesnap(tr
, i
)
343 if not snap
then break end
344 out
:write(format("#%-3d %04d [ ", i
, snap
[0]))
349 -- Return a register name or stack slot for a rid/sp location.
350 local function ridsp_name(ridsp
)
351 if not disass
then disass
= require("jit.dis_"..jit
.arch
) end
352 local rid
= band(ridsp
, 0xff)
353 if ridsp
> 255 then return format("[%x]", shr(ridsp
, 8)*4) end
354 if rid
< 128 then return disass
.regname(rid
) end
358 -- Recursively gather CALL* args and dump them.
359 local function dumpcallargs(tr
, ins
)
361 out
:write(formatk(tr
, ins
))
363 local m
, ot
, op1
, op2
= traceir(tr
, ins
)
364 local oidx
= 6*shr(ot
, 8)
365 local op
= sub(vmdef
.irnames
, oidx
+1, oidx
+6)
366 if op
== "CARG " then
367 dumpcallargs(tr
, op1
)
369 out
:write(" ", formatk(tr
, op2
))
371 out
:write(" ", format("%04d", op2
))
374 out
:write(format("%04d", ins
))
379 -- Dump IR and interleaved snapshots.
380 local function dump_ir(tr
, dumpsnap
, dumpreg
)
381 local info
= traceinfo(tr
)
382 if not info
then return end
383 local nins
= info
.nins
384 out
:write("---- TRACE ", tr
, " IR\n")
385 local irnames
= vmdef
.irnames
386 local snapref
= 65536
389 snap
= tracesnap(tr
, 0)
394 if ins
>= snapref
then
396 out
:write(format(".... SNAP #%-3d [ ", snapno
))
398 out
:write(format(".... SNAP #%-3d [ ", snapno
))
402 snap
= tracesnap(tr
, snapno
)
403 snapref
= snap
and snap
[0] or 65536
405 local m
, ot
, op1
, op2
, ridsp
= traceir(tr
, ins
)
406 local oidx
, t
= 6*shr(ot
, 8), band(ot
, 31)
407 local op
= sub(irnames
, oidx
+1, oidx
+6)
408 if op
== "LOOP " then
410 out
:write(format("%04d ------------ LOOP ------------\n", ins
))
412 out
:write(format("%04d ------ LOOP ------------\n", ins
))
414 elseif op
~= "NOP " and op
~= "CARG " and
415 (dumpreg
or op
~= "RENAME") then
417 out
:write(format("%04d %-5s ", ins
, ridsp_name(ridsp
)))
419 out
:write(format("%04d ", ins
))
421 out
:write(format("%s%s %s %s ",
422 band(ot
, 128) == 0 and " " or ">",
423 band(ot
, 64) == 0 and " " or "+",
425 local m1
, m2
= band(m
, 3), band(m
, 3*4)
426 if sub(op
, 1, 4) == "CALL" then
427 if m2
== 1*4 then -- op2 == IRMlit
428 out
:write(format("%-10s (", vmdef
.ircall
[op2
]))
430 out
:write(format("[0x%x](", tonumber((tracek(tr
, op2
)))))
432 out
:write(format("%04d (", op2
))
434 if op1
~= -1 then dumpcallargs(tr
, op1
) end
436 elseif op
== "CNEW " and op2
== -1 then
437 out
:write(formatk(tr
, op1
))
438 elseif m1
~= 3 then -- op1 != IRMnone
440 out
:write(formatk(tr
, op1
))
442 out
:write(format(m1
== 0 and "%04d" or "#%-3d", op1
))
444 if m2
~= 3*4 then -- op2 != IRMnone
445 if m2
== 1*4 then -- op2 == IRMlit
446 local litn
= litname
[op
]
447 if litn
and litn
[op2
] then
448 out
:write(" ", litn
[op2
])
449 elseif op
== "UREFO " or op
== "UREFC " then
450 out
:write(format(" #%-3d", shr(op2
, 8)))
452 out
:write(format(" #%-3d", op2
))
455 out
:write(" ", formatk(tr
, op2
))
457 out
:write(format(" %04d", op2
))
466 out
:write(format(".... SNAP #%-3d [ ", snapno
))
468 out
:write(format(".... SNAP #%-3d [ ", snapno
))
474 ------------------------------------------------------------------------------
479 -- Format trace error message.
480 local function fmterr(err
, info
)
481 if type(err
) == "number" then
482 if type(info
) == "function" then info
= fmtfunc(info
) end
483 err
= format(vmdef
.traceerr
[err
], info
)
488 -- Dump trace states.
489 local function dump_trace(what
, tr
, func
, pc
, otr
, oex
)
490 if what
== "stop" or (what
== "abort" and dumpmode
.a
) then
491 if dumpmode
.i
then dump_ir(tr
, dumpmode
.s
, dumpmode
.r
and what
== "stop")
492 elseif dumpmode
.s
then dump_snap(tr
) end
493 if dumpmode
.m
then dump_mcode(tr
) end
495 if what
== "start" then
496 if dumpmode
.H
then out
:write('<pre class="ljdump">\n') end
497 out
:write("---- TRACE ", tr
, " ", what
)
498 if otr
then out
:write(" ", otr
, "/", oex
) end
499 out
:write(" ", fmtfunc(func
, pc
), "\n")
501 elseif what
== "stop" or what
== "abort" then
502 out
:write("---- TRACE ", tr
, " ", what
)
504 if what
== "abort" then
505 out
:write(" ", fmtfunc(func
, pc
), " -- ", fmterr(otr
, oex
), "\n")
507 local info
= traceinfo(tr
)
508 local link
, ltype
= info
.link
, info
.linktype
509 if link
== tr
or link
== 0 then
510 out
:write(" -> ", ltype
, "\n")
511 elseif ltype
== "root" then
512 out
:write(" -> ", link
, "\n")
514 out
:write(" -> ", link
, " ", ltype
, "\n")
517 if dumpmode
.H
then out
:write("</pre>\n\n") else out
:write("\n") end
519 out
:write("---- TRACE ", what
, "\n\n")
524 -- Dump recorded bytecode.
525 local function dump_record(tr
, func
, pc
, depth
, callee
)
526 if depth
~= recdepth
then
528 recprefix
= rep(" .", depth
)
532 line
= bcline(func
, pc
, recprefix
)
533 if dumpmode
.H
then line
= gsub(line
, "[<>&]", html_escape
) end
535 line
= "0000 "..recprefix
.." FUNCC \n"
539 out
:write(sub(line
, 1, -2), " ; ", fmtfunc(func
), "\n")
543 if pc
>= 0 and band(funcbc(func
, pc
), 0xff) < 16 then -- ORDER BC
544 out
:write(bcline(func
, pc
+1, recprefix
)) -- Write JMP for cond.
548 ------------------------------------------------------------------------------
550 -- Dump taken trace exits.
551 local function dump_texit(tr
, ex
, ngpr
, nfpr
, ...)
552 out
:write("---- TRACE ", tr
, " exit ", ex
, "\n")
555 if jit
.arch
== "x64" then
557 out
:write(format(" %016x", regs
[i
]))
558 if i
% 4 == 0 then out
:write("\n") end
562 out
:write(format(" %08x", regs
[i
]))
563 if i
% 8 == 0 then out
:write("\n") end
567 out
:write(format(" %+17.14g", regs
[ngpr
+i
]))
568 if i
% 4 == 0 then out
:write("\n") end
573 ------------------------------------------------------------------------------
575 -- Detach dump handlers.
576 local function dumpoff()
579 jit
.attach(dump_texit
)
580 jit
.attach(dump_record
)
581 jit
.attach(dump_trace
)
582 if out
and out
~= stdout
and out
~= stderr
then out
:close() end
587 -- Open the output file and attach dump handlers.
588 local function dumpon(opt
, outfile
)
589 if active
then dumpoff() end
591 local colormode
= os
.getenv("COLORTERM") and "A" or "T"
593 opt
= gsub(opt
, "[TAH]", function(mode
) colormode
= mode
; return ""; end)
596 local m
= { t
=true, b
=true, i
=true, m
=true, }
597 if opt
and opt
~= "" then
598 local o
= sub(opt
, 1, 1)
599 if o
~= "+" and o
~= "-" then m
= {} end
600 for i
=1,#opt
do m
[sub(opt
, i
, i
)] = (o
~= "-") end
604 if m
.t
or m
.b
or m
.i
or m
.s
or m
.m
then
605 jit
.attach(dump_trace
, "trace")
608 jit
.attach(dump_record
, "record")
609 if not bcline
then bcline
= require("jit.bc").line
end
612 jit
.attach(dump_texit
, "texit")
615 if not outfile
then outfile
= os
.getenv("LUAJIT_DUMPFILE") end
617 out
= outfile
== "-" and stdout
or assert(io
.open(outfile
, "w"))
623 if colormode
== "A" then
624 colorize
= colorize_ansi
626 elseif colormode
== "H" then
627 colorize
= colorize_html
629 out
:write(header_html
)
631 colorize
= colorize_text
638 -- Public module functions.
643 start
= dumpon
-- For -j command line option.