dump.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT compiler dump module.
  3. --
  4. -- Copyright (C) 2005-2012 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. --
  40. -- The output format can be set with the following characters:
  41. --
  42. -- T Plain text output.
  43. -- A ANSI-colored text output
  44. -- H Colorized HTML + CSS output.
  45. --
  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.
  49. --
  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"
  52. --
  53. ------------------------------------------------------------------------------
  54. -- Cache some library functions and objects.
  55. local jit = require("jit")
  56. assert(jit.version_num == 20000, "LuaJIT core/library version mismatch")
  57. local jutil = require("jit.util")
  58. local vmdef = require("jit.vmdef")
  59. local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc
  60. local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek
  61. local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap
  62. local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr
  63. local bit = require("bit")
  64. local band, shl, shr = bit.band, bit.lshift, bit.rshift
  65. local sub, gsub, format = string.sub, string.gsub, string.format
  66. local byte, char, rep = string.byte, string.char, string.rep
  67. local type, tostring = type, tostring
  68. local stdout, stderr = io.stdout, io.stderr
  69. -- Load other modules on-demand.
  70. local bcline, disass
  71. -- Active flag, output file handle and dump mode.
  72. local active, out, dumpmode
  73. ------------------------------------------------------------------------------
  74. local symtabmt = { __index = false }
  75. local symtab = {}
  76. local nexitsym = 0
  77. -- Fill nested symbol table with per-trace exit stub addresses.
  78. local function fillsymtab_tr(tr, nexit)
  79. local t = {}
  80. symtabmt.__index = t
  81. if jit.arch == "mips" or jit.arch == "mipsel" then
  82. t[traceexitstub(tr, 0)] = "exit"
  83. return
  84. end
  85. for i=0,nexit-1 do
  86. local addr = traceexitstub(tr, i)
  87. t[addr] = tostring(i)
  88. end
  89. local addr = traceexitstub(tr, nexit)
  90. if addr then t[addr] = "stack_check" end
  91. end
  92. -- Fill symbol table with trace exit stub addresses.
  93. local function fillsymtab(tr, nexit)
  94. local t = symtab
  95. if nexitsym == 0 then
  96. local ircall = vmdef.ircall
  97. for i=0,#ircall do
  98. local addr = ircalladdr(i)
  99. if addr ~= 0 then t[addr] = ircall[i] end
  100. end
  101. end
  102. if nexitsym == 1000000 then -- Per-trace exit stubs.
  103. fillsymtab_tr(tr, nexit)
  104. elseif nexit > nexitsym then -- Shared exit stubs.
  105. for i=nexitsym,nexit-1 do
  106. local addr = traceexitstub(i)
  107. if addr == nil then -- Fall back to per-trace exit stubs.
  108. fillsymtab_tr(tr, nexit)
  109. setmetatable(symtab, symtabmt)
  110. nexit = 1000000
  111. break
  112. end
  113. t[addr] = tostring(i)
  114. end
  115. nexitsym = nexit
  116. end
  117. return t
  118. end
  119. local function dumpwrite(s)
  120. out:write(s)
  121. end
  122. -- Disassemble machine code.
  123. local function dump_mcode(tr)
  124. local info = traceinfo(tr)
  125. if not info then return end
  126. local mcode, addr, loop = tracemc(tr)
  127. if not mcode then return end
  128. if not disass then disass = require("jit.dis_"..jit.arch) end
  129. out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")
  130. local ctx = disass.create(mcode, addr, dumpwrite)
  131. ctx.hexdump = 0
  132. ctx.symtab = fillsymtab(tr, info.nexit)
  133. if loop ~= 0 then
  134. symtab[addr+loop] = "LOOP"
  135. ctx:disass(0, loop)
  136. out:write("->LOOP:\n")
  137. ctx:disass(loop, #mcode-loop)
  138. symtab[addr+loop] = nil
  139. else
  140. ctx:disass(0, #mcode)
  141. end
  142. end
  143. ------------------------------------------------------------------------------
  144. local irtype_text = {
  145. [0] = "nil",
  146. "fal",
  147. "tru",
  148. "lud",
  149. "str",
  150. "p32",
  151. "thr",
  152. "pro",
  153. "fun",
  154. "p64",
  155. "cdt",
  156. "tab",
  157. "udt",
  158. "flt",
  159. "num",
  160. "i8 ",
  161. "u8 ",
  162. "i16",
  163. "u16",
  164. "int",
  165. "u32",
  166. "i64",
  167. "u64",
  168. "sfp",
  169. }
  170. local colortype_ansi = {
  171. [0] = "%s",
  172. "%s",
  173. "%s",
  174. "\027[36m%s\027[m",
  175. "\027[32m%s\027[m",
  176. "%s",
  177. "\027[1m%s\027[m",
  178. "%s",
  179. "\027[1m%s\027[m",
  180. "%s",
  181. "\027[33m%s\027[m",
  182. "\027[31m%s\027[m",
  183. "\027[36m%s\027[m",
  184. "\027[34m%s\027[m",
  185. "\027[34m%s\027[m",
  186. "\027[35m%s\027[m",
  187. "\027[35m%s\027[m",
  188. "\027[35m%s\027[m",
  189. "\027[35m%s\027[m",
  190. "\027[35m%s\027[m",
  191. "\027[35m%s\027[m",
  192. "\027[35m%s\027[m",
  193. "\027[35m%s\027[m",
  194. "\027[35m%s\027[m",
  195. }
  196. local function colorize_text(s, t)
  197. return s
  198. end
  199. local function colorize_ansi(s, t)
  200. return format(colortype_ansi[t], s)
  201. end
  202. local irtype_ansi = setmetatable({},
  203. { __index = function(tab, t)
  204. local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end })
  205. local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }
  206. local function colorize_html(s, t)
  207. s = gsub(s, "[<>&]", html_escape)
  208. return format('<span class="irt_%s">%s</span>', irtype_text[t], s)
  209. end
  210. local irtype_html = setmetatable({},
  211. { __index = function(tab, t)
  212. local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end })
  213. local header_html = [[
  214. <style type="text/css">
  215. background { background: #ffffff; color: #000000; }
  216. pre.ljdump {
  217. font-size: 10pt;
  218. background: #f0f4ff;
  219. color: #000000;
  220. border: 1px solid #bfcfff;
  221. padding: 0.5em;
  222. margin-left: 2em;
  223. margin-right: 2em;
  224. }
  225. span.irt_str { color: #00a000; }
  226. span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }
  227. span.irt_tab { color: #c00000; }
  228. span.irt_udt, span.irt_lud { color: #00c0c0; }
  229. span.irt_num { color: #4040c0; }
  230. span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }
  231. </style>
  232. ]]
  233. local colorize, irtype
  234. -- Lookup tables to convert some literals into names.
  235. local litname = {
  236. ["SLOAD "] = setmetatable({}, { __index = function(t, mode)
  237. local s = ""
  238. if band(mode, 1) ~= 0 then s = s.."P" end
  239. if band(mode, 2) ~= 0 then s = s.."F" end
  240. if band(mode, 4) ~= 0 then s = s.."T" end
  241. if band(mode, 8) ~= 0 then s = s.."C" end
  242. if band(mode, 16) ~= 0 then s = s.."R" end
  243. if band(mode, 32) ~= 0 then s = s.."I" end
  244. t[mode] = s
  245. return s
  246. end}),
  247. ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", },
  248. ["CONV "] = setmetatable({}, { __index = function(t, mode)
  249. local s = irtype[band(mode, 31)]
  250. s = irtype[band(shr(mode, 5), 31)].."."..s
  251. if band(mode, 0x400) ~= 0 then s = s.." trunc"
  252. elseif band(mode, 0x800) ~= 0 then s = s.." sext" end
  253. local c = shr(mode, 14)
  254. if c == 2 then s = s.." index" elseif c == 3 then s = s.." check" end
  255. t[mode] = s
  256. return s
  257. end}),
  258. ["FLOAD "] = vmdef.irfield,
  259. ["FREF "] = vmdef.irfield,
  260. ["FPMATH"] = vmdef.irfpm,
  261. }
  262. local function ctlsub(c)
  263. if c == "\n" then return "\\n"
  264. elseif c == "\r" then return "\\r"
  265. elseif c == "\t" then return "\\t"
  266. elseif c == "\r" then return "\\r"
  267. else return format("\\%03d", byte(c))
  268. end
  269. end
  270. local function fmtfunc(func, pc)
  271. local fi = funcinfo(func, pc)
  272. if fi.loc then
  273. return fi.loc
  274. elseif fi.ffid then
  275. return vmdef.ffnames[fi.ffid]
  276. elseif fi.addr then
  277. return format("C:%x", fi.addr)
  278. else
  279. return "(?)"
  280. end
  281. end
  282. local function formatk(tr, idx)
  283. local k, t, slot = tracek(tr, idx)
  284. local tn = type(k)
  285. local s
  286. if tn == "number" then
  287. if k == 2^52+2^51 then
  288. s = "bias"
  289. else
  290. s = format("%+.14g", k)
  291. end
  292. elseif tn == "string" then
  293. s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))
  294. elseif tn == "function" then
  295. s = fmtfunc(k)
  296. elseif tn == "table" then
  297. s = format("{%p}", k)
  298. elseif tn == "userdata" then
  299. if t == 12 then
  300. s = format("userdata:%p", k)
  301. else
  302. s = format("[%p]", k)
  303. if s == "[0x00000000]" then s = "NULL" end
  304. end
  305. elseif t == 21 then -- int64_t
  306. s = sub(tostring(k), 1, -3)
  307. if sub(s, 1, 1) ~= "-" then s = "+"..s end
  308. else
  309. s = tostring(k) -- For primitives.
  310. end
  311. s = colorize(format("%-4s", s), t)
  312. if slot then
  313. s = format("%s @%d", s, slot)
  314. end
  315. return s
  316. end
  317. local function printsnap(tr, snap)
  318. local n = 2
  319. for s=0,snap[1]-1 do
  320. local sn = snap[n]
  321. if shr(sn, 24) == s then
  322. n = n + 1
  323. local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS
  324. if ref < 0 then
  325. out:write(formatk(tr, ref))
  326. elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM
  327. out:write(colorize(format("%04d/%04d", ref, ref+1), 14))
  328. else
  329. local m, ot, op1, op2 = traceir(tr, ref)
  330. out:write(colorize(format("%04d", ref), band(ot, 31)))
  331. end
  332. out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
  333. else
  334. out:write("---- ")
  335. end
  336. end
  337. out:write("]\n")
  338. end
  339. -- Dump snapshots (not interleaved with IR).
  340. local function dump_snap(tr)
  341. out:write("---- TRACE ", tr, " snapshots\n")
  342. for i=0,1000000000 do
  343. local snap = tracesnap(tr, i)
  344. if not snap then break end
  345. out:write(format("#%-3d %04d [ ", i, snap[0]))
  346. printsnap(tr, snap)
  347. end
  348. end
  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
  355. return ""
  356. end
  357. -- Dump CALL* function ref and return optional ctype.
  358. local function dumpcallfunc(tr, ins)
  359. local ctype
  360. if ins > 0 then
  361. local m, ot, op1, op2 = traceir(tr, ins)
  362. if band(ot, 31) == 0 then -- nil type means CARG(func, ctype).
  363. ins = op1
  364. ctype = formatk(tr, op2)
  365. end
  366. end
  367. if ins < 0 then
  368. out:write(format("[0x%x](", tonumber((tracek(tr, ins)))))
  369. else
  370. out:write(format("%04d (", ins))
  371. end
  372. return ctype
  373. end
  374. -- Recursively gather CALL* args and dump them.
  375. local function dumpcallargs(tr, ins)
  376. if ins < 0 then
  377. out:write(formatk(tr, ins))
  378. else
  379. local m, ot, op1, op2 = traceir(tr, ins)
  380. local oidx = 6*shr(ot, 8)
  381. local op = sub(vmdef.irnames, oidx+1, oidx+6)
  382. if op == "CARG " then
  383. dumpcallargs(tr, op1)
  384. if op2 < 0 then
  385. out:write(" ", formatk(tr, op2))
  386. else
  387. out:write(" ", format("%04d", op2))
  388. end
  389. else
  390. out:write(format("%04d", ins))
  391. end
  392. end
  393. end
  394. -- Dump IR and interleaved snapshots.
  395. local function dump_ir(tr, dumpsnap, dumpreg)
  396. local info = traceinfo(tr)
  397. if not info then return end
  398. local nins = info.nins
  399. out:write("---- TRACE ", tr, " IR\n")
  400. local irnames = vmdef.irnames
  401. local snapref = 65536
  402. local snap, snapno
  403. if dumpsnap then
  404. snap = tracesnap(tr, 0)
  405. snapref = snap[0]
  406. snapno = 0
  407. end
  408. for ins=1,nins do
  409. if ins >= snapref then
  410. if dumpreg then
  411. out:write(format(".... SNAP #%-3d [ ", snapno))
  412. else
  413. out:write(format(".... SNAP #%-3d [ ", snapno))
  414. end
  415. printsnap(tr, snap)
  416. snapno = snapno + 1
  417. snap = tracesnap(tr, snapno)
  418. snapref = snap and snap[0] or 65536
  419. end
  420. local m, ot, op1, op2, ridsp = traceir(tr, ins)
  421. local oidx, t = 6*shr(ot, 8), band(ot, 31)
  422. local op = sub(irnames, oidx+1, oidx+6)
  423. if op == "LOOP " then
  424. if dumpreg then
  425. out:write(format("%04d ------------ LOOP ------------\n", ins))
  426. else
  427. out:write(format("%04d ------ LOOP ------------\n", ins))
  428. end
  429. elseif op ~= "NOP " and op ~= "CARG " and
  430. (dumpreg or op ~= "RENAME") then
  431. if dumpreg then
  432. out:write(format("%04d %-5s ", ins, ridsp_name(ridsp)))
  433. else
  434. out:write(format("%04d ", ins))
  435. end
  436. out:write(format("%s%s %s %s ",
  437. band(ot, 128) == 0 and " " or ">",
  438. band(ot, 64) == 0 and " " or "+",
  439. irtype[t], op))
  440. local m1, m2 = band(m, 3), band(m, 3*4)
  441. if sub(op, 1, 4) == "CALL" then
  442. local ctype
  443. if m2 == 1*4 then -- op2 == IRMlit
  444. out:write(format("%-10s (", vmdef.ircall[op2]))
  445. else
  446. ctype = dumpcallfunc(tr, op2)
  447. end
  448. if op1 ~= -1 then dumpcallargs(tr, op1) end
  449. out:write(")")
  450. if ctype then out:write(" ctype ", ctype) end
  451. elseif op == "CNEW " and op2 == -1 then
  452. out:write(formatk(tr, op1))
  453. elseif m1 ~= 3 then -- op1 != IRMnone
  454. if op1 < 0 then
  455. out:write(formatk(tr, op1))
  456. else
  457. out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))
  458. end
  459. if m2 ~= 3*4 then -- op2 != IRMnone
  460. if m2 == 1*4 then -- op2 == IRMlit
  461. local litn = litname[op]
  462. if litn and litn[op2] then
  463. out:write(" ", litn[op2])
  464. elseif op == "UREFO " or op == "UREFC " then
  465. out:write(format(" #%-3d", shr(op2, 8)))
  466. else
  467. out:write(format(" #%-3d", op2))
  468. end
  469. elseif op2 < 0 then
  470. out:write(" ", formatk(tr, op2))
  471. else
  472. out:write(format(" %04d", op2))
  473. end
  474. end
  475. end
  476. out:write("\n")
  477. end
  478. end
  479. if snap then
  480. if dumpreg then
  481. out:write(format(".... SNAP #%-3d [ ", snapno))
  482. else
  483. out:write(format(".... SNAP #%-3d [ ", snapno))
  484. end
  485. printsnap(tr, snap)
  486. end
  487. end
  488. ------------------------------------------------------------------------------
  489. local recprefix = ""
  490. local recdepth = 0
  491. -- Format trace error message.
  492. local function fmterr(err, info)
  493. if type(err) == "number" then
  494. if type(info) == "function" then info = fmtfunc(info) end
  495. err = format(vmdef.traceerr[err], info)
  496. end
  497. return err
  498. end
  499. -- Dump trace states.
  500. local function dump_trace(what, tr, func, pc, otr, oex)
  501. if what == "stop" or (what == "abort" and dumpmode.a) then
  502. if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")
  503. elseif dumpmode.s then dump_snap(tr) end
  504. if dumpmode.m then dump_mcode(tr) end
  505. end
  506. if what == "start" then
  507. if dumpmode.H then out:write('<pre class="ljdump">\n') end
  508. out:write("---- TRACE ", tr, " ", what)
  509. if otr then out:write(" ", otr, "/", oex) end
  510. out:write(" ", fmtfunc(func, pc), "\n")
  511. recprefix = ""
  512. elseif what == "stop" or what == "abort" then
  513. out:write("---- TRACE ", tr, " ", what)
  514. recprefix = nil
  515. if what == "abort" then
  516. out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
  517. else
  518. local info = traceinfo(tr)
  519. local link, ltype = info.link, info.linktype
  520. if link == tr or link == 0 then
  521. out:write(" -> ", ltype, "\n")
  522. elseif ltype == "root" then
  523. out:write(" -> ", link, "\n")
  524. else
  525. out:write(" -> ", link, " ", ltype, "\n")
  526. end
  527. end
  528. if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end
  529. else
  530. out:write("---- TRACE ", what, "\n\n")
  531. end
  532. out:flush()
  533. end
  534. -- Dump recorded bytecode.
  535. local function dump_record(tr, func, pc, depth, callee)
  536. if depth ~= recdepth then
  537. recdepth = depth
  538. recprefix = rep(" .", depth)
  539. end
  540. local line
  541. if pc >= 0 then
  542. line = bcline(func, pc, recprefix)
  543. if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end
  544. else
  545. line = "0000 "..recprefix.." FUNCC \n"
  546. callee = func
  547. end
  548. if pc <= 0 then
  549. out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n")
  550. else
  551. out:write(line)
  552. end
  553. if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
  554. out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
  555. end
  556. end
  557. ------------------------------------------------------------------------------
  558. -- Dump taken trace exits.
  559. local function dump_texit(tr, ex, ngpr, nfpr, ...)
  560. out:write("---- TRACE ", tr, " exit ", ex, "\n")
  561. if dumpmode.X then
  562. local regs = {...}
  563. if jit.arch == "x64" then
  564. for i=1,ngpr do
  565. out:write(format(" %016x", regs[i]))
  566. if i % 4 == 0 then out:write("\n") end
  567. end
  568. else
  569. for i=1,ngpr do
  570. out:write(format(" %08x", regs[i]))
  571. if i % 8 == 0 then out:write("\n") end
  572. end
  573. end
  574. if jit.arch == "mips" or jit.arch == "mipsel" then
  575. for i=1,nfpr,2 do
  576. out:write(format(" %+17.14g", regs[ngpr+i]))
  577. if i % 8 == 7 then out:write("\n") end
  578. end
  579. else
  580. for i=1,nfpr do
  581. out:write(format(" %+17.14g", regs[ngpr+i]))
  582. if i % 4 == 0 then out:write("\n") end
  583. end
  584. end
  585. end
  586. end
  587. ------------------------------------------------------------------------------
  588. -- Detach dump handlers.
  589. local function dumpoff()
  590. if active then
  591. active = false
  592. jit.attach(dump_texit)
  593. jit.attach(dump_record)
  594. jit.attach(dump_trace)
  595. if out and out ~= stdout and out ~= stderr then out:close() end
  596. out = nil
  597. end
  598. end
  599. -- Open the output file and attach dump handlers.
  600. local function dumpon(opt, outfile)
  601. if active then dumpoff() end
  602. local colormode = os.getenv("COLORTERM") and "A" or "T"
  603. if opt then
  604. opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
  605. end
  606. local m = { t=true, b=true, i=true, m=true, }
  607. if opt and opt ~= "" then
  608. local o = sub(opt, 1, 1)
  609. if o ~= "+" and o ~= "-" then m = {} end
  610. for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
  611. end
  612. dumpmode = m
  613. if m.t or m.b or m.i or m.s or m.m then
  614. jit.attach(dump_trace, "trace")
  615. end
  616. if m.b then
  617. jit.attach(dump_record, "record")
  618. if not bcline then bcline = require("jit.bc").line end
  619. end
  620. if m.x or m.X then
  621. jit.attach(dump_texit, "texit")
  622. end
  623. if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
  624. if outfile then
  625. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  626. else
  627. out = stdout
  628. end
  629. m[colormode] = true
  630. if colormode == "A" then
  631. colorize = colorize_ansi
  632. irtype = irtype_ansi
  633. elseif colormode == "H" then
  634. colorize = colorize_html
  635. irtype = irtype_html
  636. out:write(header_html)
  637. else
  638. colorize = colorize_text
  639. irtype = irtype_text
  640. end
  641. active = true
  642. end
  643. -- Public module functions.
  644. module(...)
  645. on = dumpon
  646. off = dumpoff
  647. start = dumpon -- For -j command line option.