dump.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT compiler dump module.
  3. --
  4. -- Copyright (C) 2005-2010 Mike Pall. All rights reserved.
  5. -- Released under the MIT/X license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. --
  8. -- This module can be used to debug the JIT compiler itself. It dumps the
  9. -- code representations and structures used in various compiler stages.
  10. --
  11. -- Example usage:
  12. --
  13. -- luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)"
  14. -- luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R
  15. -- luajit -jdump=is myapp.lua | less -R
  16. -- luajit -jdump=-b myapp.lua
  17. -- luajit -jdump=+aH,myapp.html myapp.lua
  18. -- luajit -jdump=ixT,myapp.dump myapp.lua
  19. --
  20. -- The first argument specifies the dump mode. The second argument gives
  21. -- the output file name. Default output is to stdout, unless the environment
  22. -- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the
  23. -- module is started.
  24. --
  25. -- Different features can be turned on or off with the dump mode. If the
  26. -- mode starts with a '+', the following features are added to the default
  27. -- set of features; a '-' removes them. Otherwise the features are replaced.
  28. --
  29. -- The following dump features are available (* marks the default):
  30. --
  31. -- * t Print a line for each started, ended or aborted trace (see also -jv).
  32. -- * b Dump the traced bytecode.
  33. -- * i Dump the IR (intermediate representation).
  34. -- r Augment the IR with register/stack slots.
  35. -- s Dump the snapshot map.
  36. -- * m Dump the generated machine code.
  37. -- x Print each taken trace exit.
  38. -- X Print each taken trace exit and the contents of all registers.
  39. --
  40. -- The output format can be set with the following characters:
  41. --
  42. -- T Plain text output.
  43. -- A ANSI-colored text output
  44. -- H Colorized HTML + CSS output.
  45. --
  46. -- The default output format is plain text. It's set to ANSI-colored text
  47. -- if the COLORTERM variable is set. Note: this is independent of any output
  48. -- redirection, which is actually considered a feature.
  49. --
  50. -- You probably want to use less -R to enjoy viewing ANSI-colored text from
  51. -- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R"
  52. --
  53. ------------------------------------------------------------------------------
  54. -- Cache some library functions and objects.
  55. local jit = require("jit")
  56. assert(jit.version_num == 20000, "LuaJIT core/library version mismatch")
  57. local jutil = require("jit.util")
  58. local vmdef = require("jit.vmdef")
  59. local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc
  60. local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek
  61. local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap
  62. local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr
  63. local bit = require("bit")
  64. local band, shl, shr = bit.band, bit.lshift, bit.rshift
  65. local sub, gsub, format = string.sub, string.gsub, string.format
  66. local byte, char, rep = string.byte, string.char, string.rep
  67. local type, tostring = type, tostring
  68. local stdout, stderr = io.stdout, io.stderr
  69. -- Load other modules on-demand.
  70. local bcline, discreate
  71. -- Active flag, output file handle and dump mode.
  72. local active, out, dumpmode
  73. ------------------------------------------------------------------------------
  74. local symtab = {}
  75. local nexitsym = 0
  76. -- Fill symbol table with trace exit addresses.
  77. local function fillsymtab(nexit)
  78. local t = symtab
  79. if nexitsym == 0 then
  80. local ircall = vmdef.ircall
  81. for i=0,#ircall do t[ircalladdr(i)] = ircall[i] end
  82. end
  83. if nexit > nexitsym then
  84. for i=nexitsym,nexit-1 do t[traceexitstub(i)] = tostring(i) end
  85. nexitsym = nexit
  86. end
  87. return t
  88. end
  89. local function dumpwrite(s)
  90. out:write(s)
  91. end
  92. -- Disassemble machine code.
  93. local function dump_mcode(tr)
  94. local info = traceinfo(tr)
  95. if not info then return end
  96. local mcode, addr, loop = tracemc(tr)
  97. if not mcode then return end
  98. if not discreate then
  99. discreate = require("jit.dis_"..jit.arch).create
  100. end
  101. out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")
  102. local ctx = discreate(mcode, addr, dumpwrite)
  103. ctx.hexdump = 0
  104. ctx.symtab = fillsymtab(info.nexit)
  105. if loop ~= 0 then
  106. symtab[addr+loop] = "LOOP"
  107. ctx:disass(0, loop)
  108. out:write("->LOOP:\n")
  109. ctx:disass(loop, #mcode-loop)
  110. symtab[addr+loop] = nil
  111. else
  112. ctx:disass(0, #mcode)
  113. end
  114. end
  115. ------------------------------------------------------------------------------
  116. local irtype_text = {
  117. [0] = "nil",
  118. "fal",
  119. "tru",
  120. "lud",
  121. "str",
  122. "ptr",
  123. "thr",
  124. "pro",
  125. "fun",
  126. "t09",
  127. "tab",
  128. "udt",
  129. "num",
  130. "int",
  131. "i8 ",
  132. "u8 ",
  133. "i16",
  134. "u16",
  135. }
  136. local colortype_ansi = {
  137. [0] = "%s",
  138. "%s",
  139. "%s",
  140. "\027[36m%s\027[m",
  141. "\027[32m%s\027[m",
  142. "%s",
  143. "\027[1m%s\027[m",
  144. "%s",
  145. "\027[1m%s\027[m",
  146. "%s",
  147. "\027[31m%s\027[m",
  148. "\027[36m%s\027[m",
  149. "\027[34m%s\027[m",
  150. "\027[35m%s\027[m",
  151. "\027[35m%s\027[m",
  152. "\027[35m%s\027[m",
  153. "\027[35m%s\027[m",
  154. "\027[35m%s\027[m",
  155. }
  156. local function colorize_text(s, t)
  157. return s
  158. end
  159. local function colorize_ansi(s, t)
  160. return format(colortype_ansi[t], s)
  161. end
  162. local irtype_ansi = setmetatable({},
  163. { __index = function(tab, t)
  164. local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end })
  165. local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }
  166. local function colorize_html(s, t)
  167. s = gsub(s, "[<>&]", html_escape)
  168. return format('<span class="irt_%s">%s</span>', irtype_text[t], s)
  169. end
  170. local irtype_html = setmetatable({},
  171. { __index = function(tab, t)
  172. local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end })
  173. local header_html = [[
  174. <style type="text/css">
  175. background { background: #ffffff; color: #000000; }
  176. pre.ljdump {
  177. font-size: 10pt;
  178. background: #f0f4ff;
  179. color: #000000;
  180. border: 1px solid #bfcfff;
  181. padding: 0.5em;
  182. margin-left: 2em;
  183. margin-right: 2em;
  184. }
  185. span.irt_str { color: #00a000; }
  186. span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }
  187. span.irt_tab { color: #c00000; }
  188. span.irt_udt, span.irt_lud { color: #00c0c0; }
  189. span.irt_num { color: #4040c0; }
  190. span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }
  191. </style>
  192. ]]
  193. local colorize, irtype
  194. -- Lookup table to convert some literals into names.
  195. local litname = {
  196. ["SLOAD "] = setmetatable({}, { __index = function(t, mode)
  197. local s = ""
  198. if band(mode, 1) ~= 0 then s = s.."P" end
  199. if band(mode, 2) ~= 0 then s = s.."F" end
  200. if band(mode, 4) ~= 0 then s = s.."T" end
  201. if band(mode, 8) ~= 0 then s = s.."C" end
  202. if band(mode, 16) ~= 0 then s = s.."R" end
  203. if band(mode, 32) ~= 0 then s = s.."I" end
  204. t[mode] = s
  205. return s
  206. end}),
  207. ["XLOAD "] = { [0] = "", "R", "U", "RU", },
  208. ["TOINT "] = { [0] = "check", "index", "", },
  209. ["FLOAD "] = vmdef.irfield,
  210. ["FREF "] = vmdef.irfield,
  211. ["FPMATH"] = vmdef.irfpm,
  212. }
  213. local function ctlsub(c)
  214. if c == "\n" then return "\\n"
  215. elseif c == "\r" then return "\\r"
  216. elseif c == "\t" then return "\\t"
  217. elseif c == "\r" then return "\\r"
  218. else return format("\\%03d", byte(c))
  219. end
  220. end
  221. local function fmtfunc(func, pc)
  222. local fi = funcinfo(func, pc)
  223. if fi.loc then
  224. return fi.loc
  225. elseif fi.ffid then
  226. return vmdef.ffnames[fi.ffid]
  227. elseif fi.addr then
  228. return format("C:%x", fi.addr)
  229. else
  230. return "(?)"
  231. end
  232. end
  233. local function formatk(tr, idx)
  234. local k, t, slot = tracek(tr, idx)
  235. local tn = type(k)
  236. local s
  237. if tn == "number" then
  238. if k == 2^52+2^51 then
  239. s = "bias"
  240. else
  241. s = format("%+.14g", k)
  242. end
  243. elseif tn == "string" then
  244. s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))
  245. elseif tn == "function" then
  246. s = fmtfunc(k)
  247. elseif tn == "table" then
  248. s = format("{%p}", k)
  249. elseif tn == "userdata" then
  250. if t == 11 then
  251. s = format("userdata:%p", k)
  252. else
  253. s = format("[%p]", k)
  254. if s == "[0x00000000]" then s = "NULL" end
  255. end
  256. else
  257. s = tostring(k) -- For primitives.
  258. end
  259. s = colorize(format("%-4s", s), t)
  260. if slot then
  261. s = format("%s @%d", s, slot)
  262. end
  263. return s
  264. end
  265. local function printsnap(tr, snap)
  266. local n = 2
  267. for s=0,snap[1]-1 do
  268. local sn = snap[n]
  269. if shr(sn, 24) == s then
  270. n = n + 1
  271. local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS
  272. if ref < 0 then
  273. out:write(formatk(tr, ref))
  274. else
  275. local m, ot, op1, op2 = traceir(tr, ref)
  276. out:write(colorize(format("%04d", ref), band(ot, 15)))
  277. end
  278. out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
  279. else
  280. out:write("---- ")
  281. end
  282. end
  283. out:write("]\n")
  284. end
  285. -- Dump snapshots (not interleaved with IR).
  286. local function dump_snap(tr)
  287. out:write("---- TRACE ", tr, " snapshots\n")
  288. for i=0,1000000000 do
  289. local snap = tracesnap(tr, i)
  290. if not snap then break end
  291. out:write(format("#%-3d %04d [ ", i, snap[0]))
  292. printsnap(tr, snap)
  293. end
  294. end
  295. -- NYI: should really get the register map from the disassembler.
  296. local reg_map = ({
  297. x86 = {
  298. [0] = "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  299. "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  300. },
  301. x64 = {
  302. [0] = "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  303. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  304. "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  305. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
  306. }
  307. })[jit.arch]
  308. -- Return a register name or stack slot for a rid/sp location.
  309. local function ridsp_name(ridsp)
  310. local rid = band(ridsp, 0xff)
  311. if ridsp > 255 then return format("[%x]", shr(ridsp, 8)*4) end
  312. if rid < 128 then return reg_map[rid] end
  313. return ""
  314. end
  315. -- Recursively gather CALL* args and dump them.
  316. local function dumpcallargs(tr, ins)
  317. if ins < 0 then
  318. out:write(formatk(tr, ins))
  319. else
  320. local m, ot, op1, op2 = traceir(tr, ins)
  321. local oidx = 6*shr(ot, 8)
  322. local op = sub(vmdef.irnames, oidx+1, oidx+6)
  323. if op == "CARG " then
  324. dumpcallargs(tr, op1)
  325. if op2 < 0 then
  326. out:write(" ", formatk(tr, op2))
  327. else
  328. out:write(" ", format("%04d", op2))
  329. end
  330. else
  331. out:write(format("%04d", ins))
  332. end
  333. end
  334. end
  335. -- Dump IR and interleaved snapshots.
  336. local function dump_ir(tr, dumpsnap, dumpreg)
  337. local info = traceinfo(tr)
  338. if not info then return end
  339. local nins = info.nins
  340. out:write("---- TRACE ", tr, " IR\n")
  341. local irnames = vmdef.irnames
  342. local snapref = 65536
  343. local snap, snapno
  344. if dumpsnap then
  345. snap = tracesnap(tr, 0)
  346. snapref = snap[0]
  347. snapno = 0
  348. end
  349. for ins=1,nins do
  350. if ins >= snapref then
  351. if dumpreg then
  352. out:write(format(".... SNAP #%-3d [ ", snapno))
  353. else
  354. out:write(format(".... SNAP #%-3d [ ", snapno))
  355. end
  356. printsnap(tr, snap)
  357. snapno = snapno + 1
  358. snap = tracesnap(tr, snapno)
  359. snapref = snap and snap[0] or 65536
  360. end
  361. local m, ot, op1, op2, ridsp = traceir(tr, ins)
  362. local oidx, t = 6*shr(ot, 8), band(ot, 31)
  363. local op = sub(irnames, oidx+1, oidx+6)
  364. if op == "LOOP " then
  365. if dumpreg then
  366. out:write(format("%04d ------------ LOOP ------------\n", ins))
  367. else
  368. out:write(format("%04d ------ LOOP ------------\n", ins))
  369. end
  370. elseif op ~= "NOP " and op ~= "CARG " and
  371. (dumpreg or op ~= "RENAME") then
  372. if dumpreg then
  373. out:write(format("%04d %-5s ", ins, ridsp_name(ridsp)))
  374. else
  375. out:write(format("%04d ", ins))
  376. end
  377. out:write(format("%s%s %s %s ",
  378. band(ot, 128) == 0 and " " or ">",
  379. band(ot, 64) == 0 and " " or "+",
  380. irtype[t], op))
  381. local m1 = band(m, 3)
  382. if sub(op, 1, 4) == "CALL" then
  383. out:write(format("%-10s (", vmdef.ircall[op2]))
  384. if op1 ~= -1 then dumpcallargs(tr, op1) end
  385. out:write(")")
  386. elseif m1 ~= 3 then -- op1 != IRMnone
  387. if op1 < 0 then
  388. out:write(formatk(tr, op1))
  389. else
  390. out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))
  391. end
  392. local m2 = band(m, 3*4)
  393. if m2 ~= 3*4 then -- op2 != IRMnone
  394. if m2 == 1*4 then -- op2 == IRMlit
  395. local litn = litname[op]
  396. if litn and litn[op2] then
  397. out:write(" ", litn[op2])
  398. elseif op == "UREFO " or op == "UREFC " then
  399. out:write(format(" #%-3d", shr(op2, 8)))
  400. else
  401. out:write(format(" #%-3d", op2))
  402. end
  403. elseif op2 < 0 then
  404. out:write(" ", formatk(tr, op2))
  405. else
  406. out:write(format(" %04d", op2))
  407. end
  408. end
  409. end
  410. out:write("\n")
  411. end
  412. end
  413. if snap then
  414. if dumpreg then
  415. out:write(format(".... SNAP #%-3d [ ", snapno))
  416. else
  417. out:write(format(".... SNAP #%-3d [ ", snapno))
  418. end
  419. printsnap(tr, snap)
  420. end
  421. end
  422. ------------------------------------------------------------------------------
  423. local recprefix = ""
  424. local recdepth = 0
  425. -- Format trace error message.
  426. local function fmterr(err, info)
  427. if type(err) == "number" then
  428. if type(info) == "function" then info = fmtfunc(info) end
  429. err = format(vmdef.traceerr[err], info)
  430. end
  431. return err
  432. end
  433. -- Dump trace states.
  434. local function dump_trace(what, tr, func, pc, otr, oex)
  435. if what == "stop" or (what == "abort" and dumpmode.a) then
  436. if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")
  437. elseif dumpmode.s then dump_snap(tr) end
  438. if dumpmode.m then dump_mcode(tr) end
  439. end
  440. if what == "start" then
  441. if dumpmode.H then out:write('<pre class="ljdump">\n') end
  442. out:write("---- TRACE ", tr, " ", what)
  443. if otr then out:write(" ", otr, "/", oex) end
  444. out:write(" ", fmtfunc(func, pc), "\n")
  445. recprefix = ""
  446. elseif what == "stop" or what == "abort" then
  447. out:write("---- TRACE ", tr, " ", what)
  448. recprefix = nil
  449. if what == "abort" then
  450. out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
  451. else
  452. local link = traceinfo(tr).link
  453. if link == tr then
  454. link = "loop"
  455. elseif link == 0 then
  456. link = "interpreter"
  457. end
  458. out:write(" -> ", link, "\n")
  459. end
  460. if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end
  461. else
  462. out:write("---- TRACE ", what, "\n\n")
  463. end
  464. out:flush()
  465. end
  466. -- Dump recorded bytecode.
  467. local function dump_record(tr, func, pc, depth, callee)
  468. if depth ~= recdepth then
  469. recdepth = depth
  470. recprefix = rep(" .", depth)
  471. end
  472. local line
  473. if pc >= 0 then
  474. line = bcline(func, pc, recprefix)
  475. if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end
  476. else
  477. line = "0000 "..recprefix.." FUNCC \n"
  478. callee = func
  479. end
  480. if pc <= 0 then
  481. out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n")
  482. else
  483. out:write(line)
  484. end
  485. if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
  486. out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
  487. end
  488. end
  489. ------------------------------------------------------------------------------
  490. -- Dump taken trace exits.
  491. local function dump_texit(tr, ex, ngpr, nfpr, ...)
  492. out:write("---- TRACE ", tr, " exit ", ex, "\n")
  493. if dumpmode.X then
  494. local regs = {...}
  495. if jit.arch == "x64" then
  496. for i=1,ngpr do
  497. out:write(format(" %016x", regs[i]))
  498. if i % 4 == 0 then out:write("\n") end
  499. end
  500. else
  501. for i=1,ngpr do
  502. out:write(format(" %08x", regs[i]))
  503. if i % 8 == 0 then out:write("\n") end
  504. end
  505. end
  506. for i=1,nfpr do
  507. out:write(format(" %+17.14g", regs[ngpr+i]))
  508. if i % 4 == 0 then out:write("\n") end
  509. end
  510. end
  511. end
  512. ------------------------------------------------------------------------------
  513. -- Detach dump handlers.
  514. local function dumpoff()
  515. if active then
  516. active = false
  517. jit.attach(dump_texit)
  518. jit.attach(dump_record)
  519. jit.attach(dump_trace)
  520. if out and out ~= stdout and out ~= stderr then out:close() end
  521. out = nil
  522. end
  523. end
  524. -- Open the output file and attach dump handlers.
  525. local function dumpon(opt, outfile)
  526. if active then dumpoff() end
  527. local colormode = os.getenv("COLORTERM") and "A" or "T"
  528. if opt then
  529. opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
  530. end
  531. local m = { t=true, b=true, i=true, m=true, }
  532. if opt and opt ~= "" then
  533. local o = sub(opt, 1, 1)
  534. if o ~= "+" and o ~= "-" then m = {} end
  535. for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
  536. end
  537. dumpmode = m
  538. if m.t or m.b or m.i or m.s or m.m then
  539. jit.attach(dump_trace, "trace")
  540. end
  541. if m.b then
  542. jit.attach(dump_record, "record")
  543. if not bcline then bcline = require("jit.bc").line end
  544. end
  545. if m.x or m.X then
  546. jit.attach(dump_texit, "texit")
  547. end
  548. if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
  549. if outfile then
  550. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  551. else
  552. out = stdout
  553. end
  554. m[colormode] = true
  555. if colormode == "A" then
  556. colorize = colorize_ansi
  557. irtype = irtype_ansi
  558. elseif colormode == "H" then
  559. colorize = colorize_html
  560. irtype = irtype_html
  561. out:write(header_html)
  562. else
  563. colorize = colorize_text
  564. irtype = irtype_text
  565. end
  566. active = true
  567. end
  568. -- Public module functions.
  569. module(...)
  570. on = dumpon
  571. off = dumpoff
  572. start = dumpon -- For -j command line option.