aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/LuaJIT-1.1.7/jit/trace.lua
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/LuaJIT-1.1.7/jit/trace.lua')
-rw-r--r--libraries/LuaJIT-1.1.7/jit/trace.lua111
1 files changed, 111 insertions, 0 deletions
diff --git a/libraries/LuaJIT-1.1.7/jit/trace.lua b/libraries/LuaJIT-1.1.7/jit/trace.lua
new file mode 100644
index 0000000..42367c6
--- /dev/null
+++ b/libraries/LuaJIT-1.1.7/jit/trace.lua
@@ -0,0 +1,111 @@
1----------------------------------------------------------------------------
2-- LuaJIT compiler tracing module.
3--
4-- Copyright (C) 2005-2011 Mike Pall. All rights reserved.
5-- Released under the MIT/X license. See luajit.h for full copyright notice.
6----------------------------------------------------------------------------
7-- Activate this module to trace the progress of the JIT compiler.
8--
9-- Try: luajit -j trace -e 'print "foo"'
10-- luajit -j trace=foo.trace foo.lua
11--
12-- Default output is to stderr. To redirect output to a file,
13-- pass a filename as an argument or set the environment variable
14-- "LUAJIT_TRACEFILE".
15-- Note: the file is overwritten each time you run luajit.
16------------------------------------------------------------------------------
17
18-- Priority for compiler pipeline. Must run after backend (negative)
19-- and should be odd to catch compiler errors, too.
20local PRIORITY = -99
21
22-- Cache some library functions and objects.
23local jit = require("jit")
24assert(jit.version_num == 10107, "LuaJIT core/library version mismatch")
25local jutil = require("jit.util")
26local type, tostring, sub, format = type, tostring, string.sub, string.format
27local getinfo, justats = debug.getinfo, jutil.stats
28local stdout, stderr = io.stdout, io.stderr
29
30-- Turn compilation off for the whole module. LuaJIT would do that anyway.
31jit.off(true, true)
32
33-- Active flag and output file handle.
34local active, out
35
36-- Generate range string from table: pc[-pc] [,...]
37local function rangestring(t)
38 local s = ""
39 for i,range in ipairs(t) do
40 if i ~= 1 then s = s.."," end
41 local pc = range % 65536
42 range = (range - pc) / 65536
43 s = s..pc
44 if range ~= 0 then s = s..(-(pc+range)) end
45 end
46 return s
47end
48
49-- Trace handler for compiler pipeline.
50local function h_trace(st, status)
51 local o = out or stderr
52 local func = st.func
53 if type(func) ~= "function" then return end
54 local info = getinfo(func, "S")
55 local src, line = info.source, info.linedefined or 0
56 if src then
57 if sub(src, 1, 1) == "@" or sub(src, 1, 2) == "=(" then
58 src = sub(src, 2)
59 else
60 src = "**"..string.gsub(sub(src, 1, 40), "%c", " ").."**"
61 end
62 else
63 src = "?"
64 end
65 local aux = st.deopt and " DEOPT="..rangestring(st.deopt) or ""
66 if status == nil then
67 local stats = justats(func)
68 if not stats then return end
69 o:write(format("[LuaJIT: OK %4d %6d %s:%d%s]\n",
70 stats.bytecodes, stats.mcodesize or -1, src, line, aux))
71 return
72 else
73 local stname = jit.util.status[status] or status
74 local pc, err = st.dasm_pc, st.dasm_err
75 if type(pc) == "number" and type(err) == "number" then
76 local op = jutil.bytecode(func, pc) or "END"
77 o:write(format("[LuaJIT: %s %s@%d %08x %s:%d%s]\n",
78 stname, op, pc, err, src, line, aux))
79 else
80 o:write(format("[LuaJIT: %s %s:%d%s]\n", stname, src, line, aux))
81 end
82 end
83end
84
85-- Detach trace handler from compiler pipeline.
86local function traceoff()
87 if active then
88 active = false
89 jit.attach(h_trace)
90 if out and out ~= stdout then out:close() end
91 out = nil
92 end
93end
94
95-- Open the output file and attach trace handler to compiler pipeline.
96local function traceon(filename)
97 if active then traceoff() end
98 local outfile = filename or os.getenv("LUAJIT_TRACEFILE")
99 out = outfile and (outfile == "-" and stdout or assert(io.open(outfile, "w")))
100 jit.attach(h_trace, PRIORITY)
101 active = true
102end
103
104
105-- Public module functions.
106module(...)
107
108on = traceon
109off = traceoff
110start = traceon -- For -j command line option.
111