dump.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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, traceexitstub = jutil.tracemc, jutil.traceexitstub
  62. local tracesnap = jutil.tracesnap
  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 nexit > nexitsym then
  80. for i=nexitsym,nexit-1 do t[traceexitstub(i)] = tostring(i) end
  81. nexitsym = nexit
  82. end
  83. return t
  84. end
  85. local function dumpwrite(s)
  86. out:write(s)
  87. end
  88. -- Disassemble machine code.
  89. local function dump_mcode(tr)
  90. local info = traceinfo(tr)
  91. if not info then return end
  92. local mcode, addr, loop = tracemc(tr)
  93. if not mcode then return end
  94. if not discreate then
  95. discreate = require("jit.dis_"..jit.arch).create
  96. end
  97. out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")
  98. local ctx = discreate(mcode, addr, dumpwrite)
  99. ctx.hexdump = 0
  100. ctx.symtab = fillsymtab(info.nexit)
  101. if loop ~= 0 then
  102. symtab[addr+loop] = "LOOP"
  103. ctx:disass(0, loop)
  104. out:write("->LOOP:\n")
  105. ctx:disass(loop, #mcode-loop)
  106. symtab[addr+loop] = nil
  107. else
  108. ctx:disass(0, #mcode)
  109. end
  110. end
  111. ------------------------------------------------------------------------------
  112. local irtype_text = {
  113. [0] = "nil",
  114. "fal",
  115. "tru",
  116. "lud",
  117. "str",
  118. "ptr",
  119. "thr",
  120. "pro",
  121. "fun",
  122. "t09",
  123. "tab",
  124. "udt",
  125. "num",
  126. "int",
  127. "i8 ",
  128. "u8 ",
  129. "i16",
  130. "u16",
  131. }
  132. local colortype_ansi = {
  133. [0] = "%s",
  134. "%s",
  135. "%s",
  136. "\027[36m%s\027[m",
  137. "\027[32m%s\027[m",
  138. "%s",
  139. "\027[1m%s\027[m",
  140. "%s",
  141. "\027[1m%s\027[m",
  142. "%s",
  143. "\027[31m%s\027[m",
  144. "\027[36m%s\027[m",
  145. "\027[34m%s\027[m",
  146. "\027[35m%s\027[m",
  147. "\027[35m%s\027[m",
  148. "\027[35m%s\027[m",
  149. "\027[35m%s\027[m",
  150. "\027[35m%s\027[m",
  151. }
  152. local function colorize_text(s, t)
  153. return s
  154. end
  155. local function colorize_ansi(s, t)
  156. return format(colortype_ansi[t], s)
  157. end
  158. local irtype_ansi = setmetatable({},
  159. { __index = function(tab, t)
  160. local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end })
  161. local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }
  162. local function colorize_html(s, t)
  163. s = gsub(s, "[<>&]", html_escape)
  164. return format('<span class="irt_%s">%s</span>', irtype_text[t], s)
  165. end
  166. local irtype_html = setmetatable({},
  167. { __index = function(tab, t)
  168. local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end })
  169. local header_html = [[
  170. <style type="text/css">
  171. background { background: #ffffff; color: #000000; }
  172. pre.ljdump {
  173. font-size: 10pt;
  174. background: #f0f4ff;
  175. color: #000000;
  176. border: 1px solid #bfcfff;
  177. padding: 0.5em;
  178. margin-left: 2em;
  179. margin-right: 2em;
  180. }
  181. span.irt_str { color: #00a000; }
  182. span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }
  183. span.irt_tab { color: #c00000; }
  184. span.irt_udt, span.irt_lud { color: #00c0c0; }
  185. span.irt_num { color: #4040c0; }
  186. span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }
  187. </style>
  188. ]]
  189. local colorize, irtype
  190. -- Lookup table to convert some literals into names.
  191. local litname = {
  192. ["SLOAD "] = { [0] = "", "I", "R", "RI", "P", "PI", "PR", "PRI",
  193. "T", "IT", "RT", "RIT", "PT", "PIT", "PRT", "PRIT",
  194. "F", "IF", "RF", "RIF", "PF", "PIF", "PRF", "PRIF",
  195. "TF", "ITF", "RTF", "RITF", "PTF", "PITF", "PRTF", "PRITF",
  196. },
  197. ["XLOAD "] = { [0] = "", "R", "U", "RU", },
  198. ["TOINT "] = { [0] = "check", "index", "", },
  199. ["FLOAD "] = vmdef.irfield,
  200. ["FREF "] = vmdef.irfield,
  201. ["FPMATH"] = vmdef.irfpm,
  202. }
  203. local function ctlsub(c)
  204. if c == "\n" then return "\\n"
  205. elseif c == "\r" then return "\\r"
  206. elseif c == "\t" then return "\\t"
  207. elseif c == "\r" then return "\\r"
  208. else return format("\\%03d", byte(c))
  209. end
  210. end
  211. local function fmtfunc(func, pc)
  212. local fi = funcinfo(func, pc)
  213. if fi.loc then
  214. return fi.loc
  215. elseif fi.ffid then
  216. return vmdef.ffnames[fi.ffid]
  217. elseif fi.addr then
  218. return format("C:%x", fi.addr)
  219. else
  220. return "(?)"
  221. end
  222. end
  223. local function formatk(tr, idx)
  224. local k, t, slot = tracek(tr, idx)
  225. local tn = type(k)
  226. local s
  227. if tn == "number" then
  228. if k == 2^52+2^51 then
  229. s = "bias"
  230. else
  231. s = format("%+.14g", k)
  232. end
  233. elseif tn == "string" then
  234. s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))
  235. elseif tn == "function" then
  236. s = fmtfunc(k)
  237. elseif tn == "table" then
  238. s = format("{%p}", k)
  239. elseif tn == "userdata" then
  240. if t == 11 then
  241. s = format("userdata:%p", k)
  242. else
  243. s = format("[%p]", k)
  244. if s == "[0x00000000]" then s = "NULL" end
  245. end
  246. else
  247. s = tostring(k) -- For primitives.
  248. end
  249. s = colorize(format("%-4s", s), t)
  250. if slot then
  251. s = format("%s @%d", s, slot)
  252. end
  253. return s
  254. end
  255. local function printsnap(tr, snap)
  256. local n = 2
  257. for s=0,snap[1]-1 do
  258. local sn = snap[n]
  259. if shr(sn, 24) == s then
  260. n = n + 1
  261. local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS
  262. if ref < 0 then
  263. out:write(formatk(tr, ref))
  264. else
  265. local m, ot, op1, op2 = traceir(tr, ref)
  266. out:write(colorize(format("%04d", ref), band(ot, 15)))
  267. end
  268. out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME
  269. else
  270. out:write("---- ")
  271. end
  272. end
  273. out:write("]\n")
  274. end
  275. -- Dump snapshots (not interleaved with IR).
  276. local function dump_snap(tr)
  277. out:write("---- TRACE ", tr, " snapshots\n")
  278. for i=0,1000000000 do
  279. local snap = tracesnap(tr, i)
  280. if not snap then break end
  281. out:write(format("#%-3d %04d [ ", i, snap[0]))
  282. printsnap(tr, snap)
  283. end
  284. end
  285. -- NYI: should really get the register map from the disassembler.
  286. local reg_map = ({
  287. x86 = {
  288. [0] = "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  289. "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  290. },
  291. x64 = {
  292. [0] = "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  293. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  294. "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  295. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
  296. }
  297. })[jit.arch]
  298. -- Return a register name or stack slot for a rid/sp location.
  299. local function ridsp_name(ridsp)
  300. local rid = band(ridsp, 0xff)
  301. if ridsp > 255 then return format("[%x]", shr(ridsp, 8)*4) end
  302. if rid < 128 then return reg_map[rid] end
  303. return ""
  304. end
  305. -- Recursively gather CALL* args and dump them.
  306. local function dumpcallargs(tr, ins)
  307. if ins < 0 then
  308. out:write(formatk(tr, ins))
  309. else
  310. local m, ot, op1, op2 = traceir(tr, ins)
  311. local oidx = 6*shr(ot, 8)
  312. local op = sub(vmdef.irnames, oidx+1, oidx+6)
  313. if op == "CARG " then
  314. dumpcallargs(tr, op1)
  315. if op2 < 0 then
  316. out:write(" ", formatk(tr, op2))
  317. else
  318. out:write(" ", format("%04d", op2))
  319. end
  320. else
  321. out:write(format("%04d", ins))
  322. end
  323. end
  324. end
  325. -- Dump IR and interleaved snapshots.
  326. local function dump_ir(tr, dumpsnap, dumpreg)
  327. local info = traceinfo(tr)
  328. if not info then return end
  329. local nins = info.nins
  330. out:write("---- TRACE ", tr, " IR\n")
  331. local irnames = vmdef.irnames
  332. local snapref = 65536
  333. local snap, snapno
  334. if dumpsnap then
  335. snap = tracesnap(tr, 0)
  336. snapref = snap[0]
  337. snapno = 0
  338. end
  339. for ins=1,nins do
  340. if ins >= snapref then
  341. if dumpreg then
  342. out:write(format(".... SNAP #%-3d [ ", snapno))
  343. else
  344. out:write(format(".... SNAP #%-3d [ ", snapno))
  345. end
  346. printsnap(tr, snap)
  347. snapno = snapno + 1
  348. snap = tracesnap(tr, snapno)
  349. snapref = snap and snap[0] or 65536
  350. end
  351. local m, ot, op1, op2, ridsp = traceir(tr, ins)
  352. local oidx, t = 6*shr(ot, 8), band(ot, 31)
  353. local op = sub(irnames, oidx+1, oidx+6)
  354. if op == "LOOP " then
  355. if dumpreg then
  356. out:write(format("%04d ------------ LOOP ------------\n", ins))
  357. else
  358. out:write(format("%04d ------ LOOP ------------\n", ins))
  359. end
  360. elseif op ~= "NOP " and op ~= "CARG " and
  361. (dumpreg or op ~= "RENAME") then
  362. if dumpreg then
  363. out:write(format("%04d %-5s ", ins, ridsp_name(ridsp)))
  364. else
  365. out:write(format("%04d ", ins))
  366. end
  367. out:write(format("%s%s %s %s ",
  368. band(ot, 128) == 0 and " " or ">",
  369. band(ot, 64) == 0 and " " or "+",
  370. irtype[t], op))
  371. local m1 = band(m, 3)
  372. if sub(op, 1, 4) == "CALL" then
  373. out:write(format("%-10s (", vmdef.ircall[op2]))
  374. if op1 ~= -1 then dumpcallargs(tr, op1) end
  375. out:write(")")
  376. elseif m1 ~= 3 then -- op1 != IRMnone
  377. if op1 < 0 then
  378. out:write(formatk(tr, op1))
  379. else
  380. out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))
  381. end
  382. local m2 = band(m, 3*4)
  383. if m2 ~= 3*4 then -- op2 != IRMnone
  384. if m2 == 1*4 then -- op2 == IRMlit
  385. local litn = litname[op]
  386. if litn and litn[op2] then
  387. out:write(" ", litn[op2])
  388. elseif op == "UREFO " or op == "UREFC " then
  389. out:write(format(" #%-3d", shr(op2, 8)))
  390. else
  391. out:write(format(" #%-3d", op2))
  392. end
  393. elseif op2 < 0 then
  394. out:write(" ", formatk(tr, op2))
  395. else
  396. out:write(format(" %04d", op2))
  397. end
  398. end
  399. end
  400. out:write("\n")
  401. end
  402. end
  403. if snap then
  404. if dumpreg then
  405. out:write(format(".... SNAP #%-3d [ ", snapno))
  406. else
  407. out:write(format(".... SNAP #%-3d [ ", snapno))
  408. end
  409. printsnap(tr, snap)
  410. end
  411. end
  412. ------------------------------------------------------------------------------
  413. local recprefix = ""
  414. local recdepth = 0
  415. -- Format trace error message.
  416. local function fmterr(err, info)
  417. if type(err) == "number" then
  418. if type(info) == "function" then info = fmtfunc(info) end
  419. err = format(vmdef.traceerr[err], info)
  420. end
  421. return err
  422. end
  423. -- Dump trace states.
  424. local function dump_trace(what, tr, func, pc, otr, oex)
  425. if what == "stop" or (what == "abort" and dumpmode.a) then
  426. if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")
  427. elseif dumpmode.s then dump_snap(tr) end
  428. if dumpmode.m then dump_mcode(tr) end
  429. end
  430. if what == "start" then
  431. if dumpmode.H then out:write('<pre class="ljdump">\n') end
  432. out:write("---- TRACE ", tr, " ", what)
  433. if otr then out:write(" ", otr, "/", oex) end
  434. out:write(" ", fmtfunc(func, pc), "\n")
  435. recprefix = ""
  436. elseif what == "stop" or what == "abort" then
  437. out:write("---- TRACE ", tr, " ", what)
  438. recprefix = nil
  439. if what == "abort" then
  440. out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
  441. else
  442. local link = traceinfo(tr).link
  443. if link == tr then
  444. link = "loop"
  445. elseif link == 0 then
  446. link = "interpreter"
  447. end
  448. out:write(" -> ", link, "\n")
  449. end
  450. if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end
  451. else
  452. out:write("---- TRACE ", what, "\n\n")
  453. end
  454. out:flush()
  455. end
  456. -- Dump recorded bytecode.
  457. local function dump_record(tr, func, pc, depth, callee)
  458. if depth ~= recdepth then
  459. recdepth = depth
  460. recprefix = rep(" .", depth)
  461. end
  462. local line
  463. if pc >= 0 then
  464. line = bcline(func, pc, recprefix)
  465. if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end
  466. else
  467. line = "0000 "..recprefix.." FUNCC \n"
  468. callee = func
  469. end
  470. if pc <= 0 then
  471. out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n")
  472. else
  473. out:write(line)
  474. end
  475. if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
  476. out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
  477. end
  478. end
  479. ------------------------------------------------------------------------------
  480. -- Dump taken trace exits.
  481. local function dump_texit(tr, ex, ngpr, nfpr, ...)
  482. out:write("---- TRACE ", tr, " exit ", ex, "\n")
  483. if dumpmode.X then
  484. local regs = {...}
  485. if jit.arch == "x64" then
  486. for i=1,ngpr do
  487. out:write(format(" %016x", regs[i]))
  488. if i % 4 == 0 then out:write("\n") end
  489. end
  490. else
  491. for i=1,ngpr do
  492. out:write(format(" %08x", regs[i]))
  493. if i % 8 == 0 then out:write("\n") end
  494. end
  495. end
  496. for i=1,nfpr do
  497. out:write(format(" %+17.14g", regs[ngpr+i]))
  498. if i % 4 == 0 then out:write("\n") end
  499. end
  500. end
  501. end
  502. ------------------------------------------------------------------------------
  503. -- Detach dump handlers.
  504. local function dumpoff()
  505. if active then
  506. active = false
  507. jit.attach(dump_texit)
  508. jit.attach(dump_record)
  509. jit.attach(dump_trace)
  510. if out and out ~= stdout and out ~= stderr then out:close() end
  511. out = nil
  512. end
  513. end
  514. -- Open the output file and attach dump handlers.
  515. local function dumpon(opt, outfile)
  516. if active then dumpoff() end
  517. local colormode = os.getenv("COLORTERM") and "A" or "T"
  518. if opt then
  519. opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
  520. end
  521. local m = { t=true, b=true, i=true, m=true, }
  522. if opt and opt ~= "" then
  523. local o = sub(opt, 1, 1)
  524. if o ~= "+" and o ~= "-" then m = {} end
  525. for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
  526. end
  527. dumpmode = m
  528. if m.t or m.b or m.i or m.s or m.m then
  529. jit.attach(dump_trace, "trace")
  530. end
  531. if m.b then
  532. jit.attach(dump_record, "record")
  533. if not bcline then bcline = require("jit.bc").line end
  534. end
  535. if m.x or m.X then
  536. jit.attach(dump_texit, "texit")
  537. end
  538. if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
  539. if outfile then
  540. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  541. else
  542. out = stdout
  543. end
  544. m[colormode] = true
  545. if colormode == "A" then
  546. colorize = colorize_ansi
  547. irtype = irtype_ansi
  548. elseif colormode == "H" then
  549. colorize = colorize_html
  550. irtype = irtype_html
  551. out:write(header_html)
  552. else
  553. colorize = colorize_text
  554. irtype = irtype_text
  555. end
  556. active = true
  557. end
  558. -- Public module functions.
  559. module(...)
  560. on = dumpon
  561. off = dumpoff
  562. start = dumpon -- For -j command line option.