dump.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT compiler dump module.
  3. --
  4. -- Copyright (C) 2005-2017 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, shr, tohex = bit.band, bit.rshift, bit.tohex
  66. local sub, gsub, format = string.sub, string.gsub, string.format
  67. local byte, rep = string.byte, 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:sub(1, 4) == "mips" 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)
  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, sn)
  290. local k, t, slot = tracek(tr, idx)
  291. local tn = type(k)
  292. local s
  293. if tn == "number" then
  294. if band(sn or 0, 0x30000) ~= 0 then
  295. s = band(sn, 0x20000) ~= 0 and "contpc" or "ftsz"
  296. elseif k == 2^52+2^51 then
  297. s = "bias"
  298. else
  299. s = format(0 < k and k < 0x1p-1026 and "%+a" or "%+.14g", k)
  300. end
  301. elseif tn == "string" then
  302. s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))
  303. elseif tn == "function" then
  304. s = fmtfunc(k)
  305. elseif tn == "table" then
  306. s = format("{%p}", k)
  307. elseif tn == "userdata" then
  308. if t == 12 then
  309. s = format("userdata:%p", k)
  310. else
  311. s = format("[%p]", k)
  312. if s == "[NULL]" then s = "NULL" end
  313. end
  314. elseif t == 21 then -- int64_t
  315. s = sub(tostring(k), 1, -3)
  316. if sub(s, 1, 1) ~= "-" then s = "+"..s end
  317. elseif sn == 0x1057fff then -- SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL)
  318. return "----" -- Special case for LJ_FR2 slot 1.
  319. else
  320. s = tostring(k) -- For primitives.
  321. end
  322. s = colorize(format("%-4s", s), t)
  323. if slot then
  324. s = format("%s @%d", s, slot)
  325. end
  326. return s
  327. end
  328. local function printsnap(tr, snap)
  329. local n = 2
  330. for s=0,snap[1]-1 do
  331. local sn = snap[n]
  332. if shr(sn, 24) == s then
  333. n = n + 1
  334. local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS
  335. if ref < 0 then
  336. out:write(formatk(tr, ref, sn))
  337. elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM
  338. out:write(colorize(format("%04d/%04d", ref, ref+1), 14))
  339. else
  340. local m, ot, op1, op2 = traceir(tr, ref)
  341. out:write(colorize(format("%04d", ref), band(ot, 31)))
  342. end
  343. out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
  344. else
  345. out:write("---- ")
  346. end
  347. end
  348. out:write("]\n")
  349. end
  350. -- Dump snapshots (not interleaved with IR).
  351. local function dump_snap(tr)
  352. out:write("---- TRACE ", tr, " snapshots\n")
  353. for i=0,1000000000 do
  354. local snap = tracesnap(tr, i)
  355. if not snap then break end
  356. out:write(format("#%-3d %04d [ ", i, snap[0]))
  357. printsnap(tr, snap)
  358. end
  359. end
  360. -- Return a register name or stack slot for a rid/sp location.
  361. local function ridsp_name(ridsp, ins)
  362. if not disass then disass = require("jit.dis_"..jit.arch) end
  363. local rid, slot = band(ridsp, 0xff), shr(ridsp, 8)
  364. if rid == 253 or rid == 254 then
  365. return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot)
  366. end
  367. if ridsp > 255 then return format("[%x]", slot*4) end
  368. if rid < 128 then return disass.regname(rid) end
  369. return ""
  370. end
  371. -- Dump CALL* function ref and return optional ctype.
  372. local function dumpcallfunc(tr, ins)
  373. local ctype
  374. if ins > 0 then
  375. local m, ot, op1, op2 = traceir(tr, ins)
  376. if band(ot, 31) == 0 then -- nil type means CARG(func, ctype).
  377. ins = op1
  378. ctype = formatk(tr, op2)
  379. end
  380. end
  381. if ins < 0 then
  382. out:write(format("[0x%x](", tonumber((tracek(tr, ins)))))
  383. else
  384. out:write(format("%04d (", ins))
  385. end
  386. return ctype
  387. end
  388. -- Recursively gather CALL* args and dump them.
  389. local function dumpcallargs(tr, ins)
  390. if ins < 0 then
  391. out:write(formatk(tr, ins))
  392. else
  393. local m, ot, op1, op2 = traceir(tr, ins)
  394. local oidx = 6*shr(ot, 8)
  395. local op = sub(vmdef.irnames, oidx+1, oidx+6)
  396. if op == "CARG " then
  397. dumpcallargs(tr, op1)
  398. if op2 < 0 then
  399. out:write(" ", formatk(tr, op2))
  400. else
  401. out:write(" ", format("%04d", op2))
  402. end
  403. else
  404. out:write(format("%04d", ins))
  405. end
  406. end
  407. end
  408. -- Dump IR and interleaved snapshots.
  409. local function dump_ir(tr, dumpsnap, dumpreg)
  410. local info = traceinfo(tr)
  411. if not info then return end
  412. local nins = info.nins
  413. out:write("---- TRACE ", tr, " IR\n")
  414. local irnames = vmdef.irnames
  415. local snapref = 65536
  416. local snap, snapno
  417. if dumpsnap then
  418. snap = tracesnap(tr, 0)
  419. snapref = snap[0]
  420. snapno = 0
  421. end
  422. for ins=1,nins do
  423. if ins >= snapref then
  424. if dumpreg then
  425. out:write(format(".... SNAP #%-3d [ ", snapno))
  426. else
  427. out:write(format(".... SNAP #%-3d [ ", snapno))
  428. end
  429. printsnap(tr, snap)
  430. snapno = snapno + 1
  431. snap = tracesnap(tr, snapno)
  432. snapref = snap and snap[0] or 65536
  433. end
  434. local m, ot, op1, op2, ridsp = traceir(tr, ins)
  435. local oidx, t = 6*shr(ot, 8), band(ot, 31)
  436. local op = sub(irnames, oidx+1, oidx+6)
  437. if op == "LOOP " then
  438. if dumpreg then
  439. out:write(format("%04d ------------ LOOP ------------\n", ins))
  440. else
  441. out:write(format("%04d ------ LOOP ------------\n", ins))
  442. end
  443. elseif op ~= "NOP " and op ~= "CARG " and
  444. (dumpreg or op ~= "RENAME") then
  445. local rid = band(ridsp, 255)
  446. if dumpreg then
  447. out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins)))
  448. else
  449. out:write(format("%04d ", ins))
  450. end
  451. out:write(format("%s%s %s %s ",
  452. (rid == 254 or rid == 253) and "}" or
  453. (band(ot, 128) == 0 and " " or ">"),
  454. band(ot, 64) == 0 and " " or "+",
  455. irtype[t], op))
  456. local m1, m2 = band(m, 3), band(m, 3*4)
  457. if sub(op, 1, 4) == "CALL" then
  458. local ctype
  459. if m2 == 1*4 then -- op2 == IRMlit
  460. out:write(format("%-10s (", vmdef.ircall[op2]))
  461. else
  462. ctype = dumpcallfunc(tr, op2)
  463. end
  464. if op1 ~= -1 then dumpcallargs(tr, op1) end
  465. out:write(")")
  466. if ctype then out:write(" ctype ", ctype) end
  467. elseif op == "CNEW " and op2 == -1 then
  468. out:write(formatk(tr, op1))
  469. elseif m1 ~= 3 then -- op1 != IRMnone
  470. if op1 < 0 then
  471. out:write(formatk(tr, op1))
  472. else
  473. out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))
  474. end
  475. if m2 ~= 3*4 then -- op2 != IRMnone
  476. if m2 == 1*4 then -- op2 == IRMlit
  477. local litn = litname[op]
  478. if litn and litn[op2] then
  479. out:write(" ", litn[op2])
  480. elseif op == "UREFO " or op == "UREFC " then
  481. out:write(format(" #%-3d", shr(op2, 8)))
  482. else
  483. out:write(format(" #%-3d", op2))
  484. end
  485. elseif op2 < 0 then
  486. out:write(" ", formatk(tr, op2))
  487. else
  488. out:write(format(" %04d", op2))
  489. end
  490. end
  491. end
  492. out:write("\n")
  493. end
  494. end
  495. if snap then
  496. if dumpreg then
  497. out:write(format(".... SNAP #%-3d [ ", snapno))
  498. else
  499. out:write(format(".... SNAP #%-3d [ ", snapno))
  500. end
  501. printsnap(tr, snap)
  502. end
  503. end
  504. ------------------------------------------------------------------------------
  505. local recprefix = ""
  506. local recdepth = 0
  507. -- Format trace error message.
  508. local function fmterr(err, info)
  509. if type(err) == "number" then
  510. if type(info) == "function" then info = fmtfunc(info) end
  511. err = format(vmdef.traceerr[err], info)
  512. end
  513. return err
  514. end
  515. -- Dump trace states.
  516. local function dump_trace(what, tr, func, pc, otr, oex)
  517. if what == "stop" or (what == "abort" and dumpmode.a) then
  518. if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")
  519. elseif dumpmode.s then dump_snap(tr) end
  520. if dumpmode.m then dump_mcode(tr) end
  521. end
  522. if what == "start" then
  523. if dumpmode.H then out:write('<pre class="ljdump">\n') end
  524. out:write("---- TRACE ", tr, " ", what)
  525. if otr then out:write(" ", otr, "/", oex == -1 and "stitch" or oex) end
  526. out:write(" ", fmtfunc(func, pc), "\n")
  527. elseif what == "stop" or what == "abort" then
  528. out:write("---- TRACE ", tr, " ", what)
  529. if what == "abort" then
  530. out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
  531. else
  532. local info = traceinfo(tr)
  533. local link, ltype = info.link, info.linktype
  534. if link == tr or link == 0 then
  535. out:write(" -> ", ltype, "\n")
  536. elseif ltype == "root" then
  537. out:write(" -> ", link, "\n")
  538. else
  539. out:write(" -> ", link, " ", ltype, "\n")
  540. end
  541. end
  542. if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end
  543. else
  544. if what == "flush" then symtab, nexitsym = {}, 0 end
  545. out:write("---- TRACE ", what, "\n\n")
  546. end
  547. out:flush()
  548. end
  549. -- Dump recorded bytecode.
  550. local function dump_record(tr, func, pc, depth, callee)
  551. if depth ~= recdepth then
  552. recdepth = depth
  553. recprefix = rep(" .", depth)
  554. end
  555. local line
  556. if pc >= 0 then
  557. line = bcline(func, pc, recprefix)
  558. if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end
  559. else
  560. line = "0000 "..recprefix.." FUNCC \n"
  561. callee = func
  562. end
  563. if pc <= 0 then
  564. out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n")
  565. else
  566. out:write(line)
  567. end
  568. if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
  569. out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
  570. end
  571. end
  572. ------------------------------------------------------------------------------
  573. -- Dump taken trace exits.
  574. local function dump_texit(tr, ex, ngpr, nfpr, ...)
  575. out:write("---- TRACE ", tr, " exit ", ex, "\n")
  576. if dumpmode.X then
  577. local regs = {...}
  578. if jit.arch == "x64" then
  579. for i=1,ngpr do
  580. out:write(format(" %016x", regs[i]))
  581. if i % 4 == 0 then out:write("\n") end
  582. end
  583. else
  584. for i=1,ngpr do
  585. out:write(" ", tohex(regs[i]))
  586. if i % 8 == 0 then out:write("\n") end
  587. end
  588. end
  589. if jit.arch == "mips" or jit.arch == "mipsel" then
  590. for i=1,nfpr,2 do
  591. out:write(format(" %+17.14g", regs[ngpr+i]))
  592. if i % 8 == 7 then out:write("\n") end
  593. end
  594. else
  595. for i=1,nfpr do
  596. out:write(format(" %+17.14g", regs[ngpr+i]))
  597. if i % 4 == 0 then out:write("\n") end
  598. end
  599. end
  600. end
  601. end
  602. ------------------------------------------------------------------------------
  603. -- Detach dump handlers.
  604. local function dumpoff()
  605. if active then
  606. active = false
  607. jit.attach(dump_texit)
  608. jit.attach(dump_record)
  609. jit.attach(dump_trace)
  610. if out and out ~= stdout and out ~= stderr then out:close() end
  611. out = nil
  612. end
  613. end
  614. -- Open the output file and attach dump handlers.
  615. local function dumpon(opt, outfile)
  616. if active then dumpoff() end
  617. local term = os.getenv("TERM")
  618. local colormode = (term and term:match("color") or os.getenv("COLORTERM")) and "A" or "T"
  619. if opt then
  620. opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
  621. end
  622. local m = { t=true, b=true, i=true, m=true, }
  623. if opt and opt ~= "" then
  624. local o = sub(opt, 1, 1)
  625. if o ~= "+" and o ~= "-" then m = {} end
  626. for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
  627. end
  628. dumpmode = m
  629. if m.t or m.b or m.i or m.s or m.m then
  630. jit.attach(dump_trace, "trace")
  631. end
  632. if m.b then
  633. jit.attach(dump_record, "record")
  634. if not bcline then bcline = require("jit.bc").line end
  635. end
  636. if m.x or m.X then
  637. jit.attach(dump_texit, "texit")
  638. end
  639. if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
  640. if outfile then
  641. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  642. else
  643. out = stdout
  644. end
  645. m[colormode] = true
  646. if colormode == "A" then
  647. colorize = colorize_ansi
  648. irtype = irtype_ansi
  649. elseif colormode == "H" then
  650. colorize = colorize_html
  651. irtype = irtype_html
  652. out:write(header_html)
  653. else
  654. colorize = colorize_text
  655. irtype = irtype_text
  656. end
  657. active = true
  658. end
  659. -- Public module functions.
  660. return {
  661. on = dumpon,
  662. off = dumpoff,
  663. start = dumpon -- For -j command line option.
  664. }