src/jit/dump.lua - luajit-2.0-src

Functions defined

Source code

  1. ----------------------------------------------------------------------------
  2. -- LuaJIT compiler dump module.
  3. --
  4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.
  5. -- Released under the MIT license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. --
  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.
  10. --
  11. -- Example usage:
  12. --
  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
  19. --
  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
  23. -- module is started.
  24. --
  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.
  28. --
  29. -- The following dump features are available (* marks the default):
  30. --
  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.
  39. --    a  Print the IR of aborted traces, too.
  40. --
  41. -- The output format can be set with the following characters:
  42. --
  43. --    T  Plain text output.
  44. --    A  ANSI-colored text output
  45. --    H  Colorized HTML + CSS output.
  46. --
  47. -- The default output format is plain text. It's set to ANSI-colored text
  48. -- if the COLORTERM variable is set. Note: this is independent of any output
  49. -- redirection, which is actually considered a feature.
  50. --
  51. -- You probably want to use less -R to enjoy viewing ANSI-colored text from
  52. -- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R"
  53. --
  54. ------------------------------------------------------------------------------

  55. -- Cache some library functions and objects.
  56. local jit = require("jit")
  57. assert(jit.version_num == 20100, "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, tohex = bit.band, bit.lshift, bit.rshift, bit.tohex
  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

  70. -- Load other modules on-demand.
  71. local bcline, disass

  72. -- Active flag, output file handle and dump mode.
  73. local active, out, dumpmode

  74. ------------------------------------------------------------------------------

  75. local symtabmt = { __index = false }
  76. local symtab = {}
  77. local nexitsym = 0

  78. -- Fill nested symbol table with per-trace exit stub addresses.
  79. local function fillsymtab_tr(tr, nexit)
  80.   local t = {}
  81.   symtabmt.__index = t
  82.   if jit.arch == "mips" or jit.arch == "mipsel" then
  83.     t[traceexitstub(tr, 0)] = "exit"
  84.     return
  85.   end
  86.   for i=0,nexit-1 do
  87.     local addr = traceexitstub(tr, i)
  88.     if addr < 0 then addr = addr + 2^32 end
  89.     t[addr] = tostring(i)
  90.   end
  91.   local addr = traceexitstub(tr, nexit)
  92.   if addr then t[addr] = "stack_check" end
  93. end

  94. -- Fill symbol table with trace exit stub addresses.
  95. local function fillsymtab(tr, nexit)
  96.   local t = symtab
  97.   if nexitsym == 0 then
  98.     local ircall = vmdef.ircall
  99.     for i=0,#ircall do
  100.       local addr = ircalladdr(i)
  101.       if addr ~= 0 then
  102.         if addr < 0 then addr = addr + 2^32 end
  103.         t[addr] = ircall[i]
  104.       end
  105.     end
  106.   end
  107.   if nexitsym == 1000000 then -- Per-trace exit stubs.
  108.     fillsymtab_tr(tr, nexit)
  109.   elseif nexit > nexitsym then -- Shared exit stubs.
  110.     for i=nexitsym,nexit-1 do
  111.       local addr = traceexitstub(i)
  112.       if addr == nil then -- Fall back to per-trace exit stubs.
  113.         fillsymtab_tr(tr, nexit)
  114.         setmetatable(symtab, symtabmt)
  115.         nexit = 1000000
  116.         break
  117.       end
  118.       if addr < 0 then addr = addr + 2^32 end
  119.       t[addr] = tostring(i)
  120.     end
  121.     nexitsym = nexit
  122.   end
  123.   return t
  124. end

  125. local function dumpwrite(s)
  126.   out:write(s)
  127. end

  128. -- Disassemble machine code.
  129. local function dump_mcode(tr)
  130.   local info = traceinfo(tr)
  131.   if not info then return end
  132.   local mcode, addr, loop = tracemc(tr)
  133.   if not mcode then return end
  134.   if not disass then disass = require("jit.dis_"..jit.arch) end
  135.   if addr < 0 then addr = addr + 2^32 end
  136.   out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")
  137.   local ctx = disass.create(mcode, addr, dumpwrite)
  138.   ctx.hexdump = 0
  139.   ctx.symtab = fillsymtab(tr, info.nexit)
  140.   if loop ~= 0 then
  141.     symtab[addr+loop] = "LOOP"
  142.     ctx:disass(0, loop)
  143.     out:write("->LOOP:\n")
  144.     ctx:disass(loop, #mcode-loop)
  145.     symtab[addr+loop] = nil
  146.   else
  147.     ctx:disass(0, #mcode)
  148.   end
  149. end

  150. ------------------------------------------------------------------------------

  151. local irtype_text = {
  152.   [0] = "nil",
  153.   "fal",
  154.   "tru",
  155.   "lud",
  156.   "str",
  157.   "p32",
  158.   "thr",
  159.   "pro",
  160.   "fun",
  161.   "p64",
  162.   "cdt",
  163.   "tab",
  164.   "udt",
  165.   "flt",
  166.   "num",
  167.   "i8 ",
  168.   "u8 ",
  169.   "i16",
  170.   "u16",
  171.   "int",
  172.   "u32",
  173.   "i64",
  174.   "u64",
  175.   "sfp",
  176. }

  177. local colortype_ansi = {
  178.   [0] = "%s",
  179.   "%s",
  180.   "%s",
  181.   "\027[36m%s\027[m",
  182.   "\027[32m%s\027[m",
  183.   "%s",
  184.   "\027[1m%s\027[m",
  185.   "%s",
  186.   "\027[1m%s\027[m",
  187.   "%s",
  188.   "\027[33m%s\027[m",
  189.   "\027[31m%s\027[m",
  190.   "\027[36m%s\027[m",
  191.   "\027[34m%s\027[m",
  192.   "\027[34m%s\027[m",
  193.   "\027[35m%s\027[m",
  194.   "\027[35m%s\027[m",
  195.   "\027[35m%s\027[m",
  196.   "\027[35m%s\027[m",
  197.   "\027[35m%s\027[m",
  198.   "\027[35m%s\027[m",
  199.   "\027[35m%s\027[m",
  200.   "\027[35m%s\027[m",
  201.   "\027[35m%s\027[m",
  202. }

  203. local function colorize_text(s, t)
  204.   return s
  205. end

  206. local function colorize_ansi(s, t)
  207.   return format(colortype_ansi[t], s)
  208. end

  209. local irtype_ansi = setmetatable({},
  210.   { __index = function(tab, t)
  211.       local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end })

  212. local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }

  213. local function colorize_html(s, t)
  214.   s = gsub(s, "[<>&]", html_escape)
  215.   return format('<span class="irt_%s">%s</span>', irtype_text[t], s)
  216. end

  217. local irtype_html = setmetatable({},
  218.   { __index = function(tab, t)
  219.       local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end })

  220. local header_html = [[
  221. <style type="text/css">
  222. background { background: #ffffff; color: #000000; }
  223. pre.ljdump {
  224. font-size: 10pt;
  225. background: #f0f4ff;
  226. color: #000000;
  227. border: 1px solid #bfcfff;
  228. padding: 0.5em;
  229. margin-left: 2em;
  230. margin-right: 2em;
  231. }
  232. span.irt_str { color: #00a000; }
  233. span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }
  234. span.irt_tab { color: #c00000; }
  235. span.irt_udt, span.irt_lud { color: #00c0c0; }
  236. span.irt_num { color: #4040c0; }
  237. span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }
  238. </style>
  239. ]]

  240. local colorize, irtype

  241. -- Lookup tables to convert some literals into names.
  242. local litname = {
  243.   ["SLOAD "] = setmetatable({}, { __index = function(t, mode)
  244.     local s = ""
  245.     if band(mode, 1) ~= 0 then s = s.."P" end
  246.     if band(mode, 2) ~= 0 then s = s.."F" end
  247.     if band(mode, 4) ~= 0 then s = s.."T" end
  248.     if band(mode, 8) ~= 0 then s = s.."C" end
  249.     if band(mode, 16) ~= 0 then s = s.."R" end
  250.     if band(mode, 32) ~= 0 then s = s.."I" end
  251.     t[mode] = s
  252.     return s
  253.   end}),
  254.   ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", },
  255.   ["CONV  "] = setmetatable({}, { __index = function(t, mode)
  256.     local s = irtype[band(mode, 31)]
  257.     s = irtype[band(shr(mode, 5), 31)].."."..s
  258.     if band(mode, 0x800) ~= 0 then s = s.." sext" end
  259.     local c = shr(mode, 14)
  260.     if c == 2 then s = s.." index" elseif c == 3 then s = s.." check" end
  261.     t[mode] = s
  262.     return s
  263.   end}),
  264.   ["FLOAD "] = vmdef.irfield,
  265.   ["FREF  "] = vmdef.irfield,
  266.   ["FPMATH"] = vmdef.irfpm,
  267.   ["BUFHDR"] = { [0] = "RESET", "APPEND" },
  268.   ["TOSTR "] = { [0] = "INT", "NUM", "CHAR" },
  269. }

  270. local function ctlsub(c)
  271.   if c == "\n" then return "\\n"
  272.   elseif c == "\r" then return "\\r"
  273.   elseif c == "\t" then return "\\t"
  274.   else return format("\\%03d", byte(c))
  275.   end
  276. end

  277. local function fmtfunc(func, pc)
  278.   local fi = funcinfo(func, pc)
  279.   if fi.loc then
  280.     return fi.loc
  281.   elseif fi.ffid then
  282.     return vmdef.ffnames[fi.ffid]
  283.   elseif fi.addr then
  284.     return format("C:%x", fi.addr)
  285.   else
  286.     return "(?)"
  287.   end
  288. end

  289. local function formatk(tr, idx)
  290.   local k, t, slot = tracek(tr, idx)
  291.   local tn = type(k)
  292.   local s
  293.   if tn == "number" then
  294.     if k == 2^52+2^51 then
  295.       s = "bias"
  296.     else
  297.       s = format("%+.14g", k)
  298.     end
  299.   elseif tn == "string" then
  300.     s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))
  301.   elseif tn == "function" then
  302.     s = fmtfunc(k)
  303.   elseif tn == "table" then
  304.     s = format("{%p}", k)
  305.   elseif tn == "userdata" then
  306.     if t == 12 then
  307.       s = format("userdata:%p", k)
  308.     else
  309.       s = format("[%p]", k)
  310.       if s == "[0x00000000]" then s = "NULL" end
  311.     end
  312.   elseif t == 21 then -- int64_t
  313.     s = sub(tostring(k), 1, -3)
  314.     if sub(s, 1, 1) ~= "-" then s = "+"..s end
  315.   else
  316.     s = tostring(k) -- For primitives.
  317.   end
  318.   s = colorize(format("%-4s", s), t)
  319.   if slot then
  320.     s = format("%s @%d", s, slot)
  321.   end
  322.   return s
  323. end

  324. local function printsnap(tr, snap)
  325.   local n = 2
  326.   for s=0,snap[1]-1 do
  327.     local sn = snap[n]
  328.     if shr(sn, 24) == s then
  329.       n = n + 1
  330.       local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS
  331.       if ref < 0 then
  332.         out:write(formatk(tr, ref))
  333.       elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM
  334.         out:write(colorize(format("%04d/%04d", ref, ref+1), 14))
  335.       else
  336.         local m, ot, op1, op2 = traceir(tr, ref)
  337.         out:write(colorize(format("%04d", ref), band(ot, 31)))
  338.       end
  339.       out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
  340.     else
  341.       out:write("---- ")
  342.     end
  343.   end
  344.   out:write("]\n")
  345. end

  346. -- Dump snapshots (not interleaved with IR).
  347. local function dump_snap(tr)
  348.   out:write("---- TRACE ", tr, " snapshots\n")
  349.   for i=0,1000000000 do
  350.     local snap = tracesnap(tr, i)
  351.     if not snap then break end
  352.     out:write(format("#%-3d %04d [ ", i, snap[0]))
  353.     printsnap(tr, snap)
  354.   end
  355. end

  356. -- Return a register name or stack slot for a rid/sp location.
  357. local function ridsp_name(ridsp, ins)
  358.   if not disass then disass = require("jit.dis_"..jit.arch) end
  359.   local rid, slot = band(ridsp, 0xff), shr(ridsp, 8)
  360.   if rid == 253 or rid == 254 then
  361.     return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot)
  362.   end
  363.   if ridsp > 255 then return format("[%x]", slot*4) end
  364.   if rid < 128 then return disass.regname(rid) end
  365.   return ""
  366. end

  367. -- Dump CALL* function ref and return optional ctype.
  368. local function dumpcallfunc(tr, ins)
  369.   local ctype
  370.   if ins > 0 then
  371.     local m, ot, op1, op2 = traceir(tr, ins)
  372.     if band(ot, 31) == 0 then -- nil type means CARG(func, ctype).
  373.       ins = op1
  374.       ctype = formatk(tr, op2)
  375.     end
  376.   end
  377.   if ins < 0 then
  378.     out:write(format("[0x%x](", tonumber((tracek(tr, ins)))))
  379.   else
  380.     out:write(format("%04d (", ins))
  381.   end
  382.   return ctype
  383. end

  384. -- Recursively gather CALL* args and dump them.
  385. local function dumpcallargs(tr, ins)
  386.   if ins < 0 then
  387.     out:write(formatk(tr, ins))
  388.   else
  389.     local m, ot, op1, op2 = traceir(tr, ins)
  390.     local oidx = 6*shr(ot, 8)
  391.     local op = sub(vmdef.irnames, oidx+1, oidx+6)
  392.     if op == "CARG  " then
  393.       dumpcallargs(tr, op1)
  394.       if op2 < 0 then
  395.         out:write(" ", formatk(tr, op2))
  396.       else
  397.         out:write(" ", format("%04d", op2))
  398.       end
  399.     else
  400.       out:write(format("%04d", ins))
  401.     end
  402.   end
  403. end

  404. -- Dump IR and interleaved snapshots.
  405. local function dump_ir(tr, dumpsnap, dumpreg)
  406.   local info = traceinfo(tr)
  407.   if not info then return end
  408.   local nins = info.nins
  409.   out:write("---- TRACE ", tr, " IR\n")
  410.   local irnames = vmdef.irnames
  411.   local snapref = 65536
  412.   local snap, snapno
  413.   if dumpsnap then
  414.     snap = tracesnap(tr, 0)
  415.     snapref = snap[0]
  416.     snapno = 0
  417.   end
  418.   for ins=1,nins do
  419.     if ins >= snapref then
  420.       if dumpreg then
  421.         out:write(format("....              SNAP   #%-3d [ ", snapno))
  422.       else
  423.         out:write(format("....        SNAP   #%-3d [ ", snapno))
  424.       end
  425.       printsnap(tr, snap)
  426.       snapno = snapno + 1
  427.       snap = tracesnap(tr, snapno)
  428.       snapref = snap and snap[0] or 65536
  429.     end
  430.     local m, ot, op1, op2, ridsp = traceir(tr, ins)
  431.     local oidx, t = 6*shr(ot, 8), band(ot, 31)
  432.     local op = sub(irnames, oidx+1, oidx+6)
  433.     if op == "LOOP  " then
  434.       if dumpreg then
  435.         out:write(format("%04d ------------ LOOP ------------\n", ins))
  436.       else
  437.         out:write(format("%04d ------ LOOP ------------\n", ins))
  438.       end
  439.     elseif op ~= "NOP   " and op ~= "CARG  " and
  440.            (dumpreg or op ~= "RENAME") then
  441.       local rid = band(ridsp, 255)
  442.       if dumpreg then
  443.         out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins)))
  444.       else
  445.         out:write(format("%04d ", ins))
  446.       end
  447.       out:write(format("%s%s %s %s ",
  448.                        (rid == 254 or rid == 253) and "}" or
  449.                        (band(ot, 128) == 0 and " " or ">"),
  450.                        band(ot, 64) == 0 and " " or "+",
  451.                        irtype[t], op))
  452.       local m1, m2 = band(m, 3), band(m, 3*4)
  453.       if sub(op, 1, 4) == "CALL" then
  454.         local ctype
  455.         if m2 == 1*4 then -- op2 == IRMlit
  456.           out:write(format("%-10s  (", vmdef.ircall[op2]))
  457.         else
  458.           ctype = dumpcallfunc(tr, op2)
  459.         end
  460.         if op1 ~= -1 then dumpcallargs(tr, op1) end
  461.         out:write(")")
  462.         if ctype then out:write(" ctype ", ctype) end
  463.       elseif op == "CNEW  " and op2 == -1 then
  464.         out:write(formatk(tr, op1))
  465.       elseif m1 ~= 3 then -- op1 != IRMnone
  466.         if op1 < 0 then
  467.           out:write(formatk(tr, op1))
  468.         else
  469.           out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))
  470.         end
  471.         if m2 ~= 3*4 then -- op2 != IRMnone
  472.           if m2 == 1*4 then -- op2 == IRMlit
  473.             local litn = litname[op]
  474.             if litn and litn[op2] then
  475.               out:write("  ", litn[op2])
  476.             elseif op == "UREFO " or op == "UREFC " then
  477.               out:write(format("  #%-3d", shr(op2, 8)))
  478.             else
  479.               out:write(format("  #%-3d", op2))
  480.             end
  481.           elseif op2 < 0 then
  482.             out:write("  ", formatk(tr, op2))
  483.           else
  484.             out:write(format("  %04d", op2))
  485.           end
  486.         end
  487.       end
  488.       out:write("\n")
  489.     end
  490.   end
  491.   if snap then
  492.     if dumpreg then
  493.       out:write(format("....              SNAP   #%-3d [ ", snapno))
  494.     else
  495.       out:write(format("....        SNAP   #%-3d [ ", snapno))
  496.     end
  497.     printsnap(tr, snap)
  498.   end
  499. end

  500. ------------------------------------------------------------------------------

  501. local recprefix = ""
  502. local recdepth = 0

  503. -- Format trace error message.
  504. local function fmterr(err, info)
  505.   if type(err) == "number" then
  506.     if type(info) == "function" then info = fmtfunc(info) end
  507.     err = format(vmdef.traceerr[err], info)
  508.   end
  509.   return err
  510. end

  511. -- Dump trace states.
  512. local function dump_trace(what, tr, func, pc, otr, oex)
  513.   if what == "stop" or (what == "abort" and dumpmode.a) then
  514.     if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")
  515.     elseif dumpmode.s then dump_snap(tr) end
  516.     if dumpmode.m then dump_mcode(tr) end
  517.   end
  518.   if what == "start" then
  519.     if dumpmode.H then out:write('<pre class="ljdump">\n') end
  520.     out:write("---- TRACE ", tr, " ", what)
  521.     if otr then out:write(" ", otr, "/", oex) end
  522.     out:write(" ", fmtfunc(func, pc), "\n")
  523.   elseif what == "stop" or what == "abort" then
  524.     out:write("---- TRACE ", tr, " ", what)
  525.     if what == "abort" then
  526.       out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
  527.     else
  528.       local info = traceinfo(tr)
  529.       local link, ltype = info.link, info.linktype
  530.       if link == tr or link == 0 then
  531.         out:write(" -> ", ltype, "\n")
  532.       elseif ltype == "root" then
  533.         out:write(" -> ", link, "\n")
  534.       else
  535.         out:write(" -> ", link, " ", ltype, "\n")
  536.       end
  537.     end
  538.     if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end
  539.   else
  540.     out:write("---- TRACE ", what, "\n\n")
  541.   end
  542.   out:flush()
  543. end

  544. -- Dump recorded bytecode.
  545. local function dump_record(tr, func, pc, depth, callee)
  546.   if depth ~= recdepth then
  547.     recdepth = depth
  548.     recprefix = rep(" .", depth)
  549.   end
  550.   local line
  551.   if pc >= 0 then
  552.     line = bcline(func, pc, recprefix)
  553.     if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end
  554.   else
  555.     line = "0000 "..recprefix.." FUNCC      \n"
  556.     callee = func
  557.   end
  558.   if pc <= 0 then
  559.     out:write(sub(line, 1, -2), "         ; ", fmtfunc(func), "\n")
  560.   else
  561.     out:write(line)
  562.   end
  563.   if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
  564.     out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
  565.   end
  566. end

  567. ------------------------------------------------------------------------------

  568. -- Dump taken trace exits.
  569. local function dump_texit(tr, ex, ngpr, nfpr, ...)
  570.   out:write("---- TRACE ", tr, " exit ", ex, "\n")
  571.   if dumpmode.X then
  572.     local regs = {...}
  573.     if jit.arch == "x64" then
  574.       for i=1,ngpr do
  575.         out:write(format(" %016x", regs[i]))
  576.         if i % 4 == 0 then out:write("\n") end
  577.       end
  578.     else
  579.       for i=1,ngpr do
  580.         out:write(" ", tohex(regs[i]))
  581.         if i % 8 == 0 then out:write("\n") end
  582.       end
  583.     end
  584.     if jit.arch == "mips" or jit.arch == "mipsel" then
  585.       for i=1,nfpr,2 do
  586.         out:write(format(" %+17.14g", regs[ngpr+i]))
  587.         if i % 8 == 7 then out:write("\n") end
  588.       end
  589.     else
  590.       for i=1,nfpr do
  591.         out:write(format(" %+17.14g", regs[ngpr+i]))
  592.         if i % 4 == 0 then out:write("\n") end
  593.       end
  594.     end
  595.   end
  596. end

  597. ------------------------------------------------------------------------------

  598. -- Detach dump handlers.
  599. local function dumpoff()
  600.   if active then
  601.     active = false
  602.     jit.attach(dump_texit)
  603.     jit.attach(dump_record)
  604.     jit.attach(dump_trace)
  605.     if out and out ~= stdout and out ~= stderr then out:close() end
  606.     out = nil
  607.   end
  608. end

  609. -- Open the output file and attach dump handlers.
  610. local function dumpon(opt, outfile)
  611.   if active then dumpoff() end

  612.   local colormode = os.getenv("COLORTERM") and "A" or "T"
  613.   if opt then
  614.     opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
  615.   end

  616.   local m = { t=true, b=true, i=true, m=true, }
  617.   if opt and opt ~= "" then
  618.     local o = sub(opt, 1, 1)
  619.     if o ~= "+" and o ~= "-" then m = {} end
  620.     for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
  621.   end
  622.   dumpmode = m

  623.   if m.t or m.b or m.i or m.s or m.m then
  624.     jit.attach(dump_trace, "trace")
  625.   end
  626.   if m.b then
  627.     jit.attach(dump_record, "record")
  628.     if not bcline then bcline = require("jit.bc").line end
  629.   end
  630.   if m.x or m.X then
  631.     jit.attach(dump_texit, "texit")
  632.   end

  633.   if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
  634.   if outfile then
  635.     out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  636.   else
  637.     out = stdout
  638.   end

  639.   m[colormode] = true
  640.   if colormode == "A" then
  641.     colorize = colorize_ansi
  642.     irtype = irtype_ansi
  643.   elseif colormode == "H" then
  644.     colorize = colorize_html
  645.     irtype = irtype_html
  646.     out:write(header_html)
  647.   else
  648.     colorize = colorize_text
  649.     irtype = irtype_text
  650.   end

  651.   active = true
  652. end

  653. -- Public module functions.
  654. return {
  655.   on = dumpon,
  656.   off = dumpoff,
  657.   start = dumpon -- For -j command line option.
  658. }