dump.lua 20 KB

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