dump.lua 20 KB

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