dump.lua 19 KB

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