diff options
author | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
commit | 9621add2918cc4943e6693b74ae85d51dd264fcf (patch) | |
tree | fff1edf2c69d7a08a0e12885eecc9b96ed847a6a /LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua | |
parent | LuaSL_test's window doesn't need to be so huge. (diff) | |
download | SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.zip SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.gz SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.bz2 SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.xz |
We don't need the testlua directory any more.
Diffstat (limited to 'LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua')
-rw-r--r-- | LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua | 254 |
1 files changed, 0 insertions, 254 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua b/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua deleted file mode 100644 index 5c60e17..0000000 --- a/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/tools/call_graph.lua +++ /dev/null | |||
@@ -1,254 +0,0 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | call_graph.lua | ||
4 | Call graph generator. | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- Notes: | ||
17 | -- * the call tracer wraps function calls in tables to do its work | ||
18 | -- * not very elegant as the namespace of the table/module is affected | ||
19 | -- * tracing using the debugger is probably much more powerful... | ||
20 | -- * use of braces {} allows editors to match braces in the output | ||
21 | -- and do folding, if such facilities are available; for example, the | ||
22 | -- output looks better if Lua syntax highlighting is used on SciTE | ||
23 | ----------------------------------------------------------------------]] | ||
24 | |||
25 | ------------------------------------------------------------------------ | ||
26 | -- options | ||
27 | ------------------------------------------------------------------------ | ||
28 | |||
29 | local SHOW_EXPDESC = true -- show expdesc struct data | ||
30 | |||
31 | ------------------------------------------------------------------------ | ||
32 | -- load and initialize modules | ||
33 | ------------------------------------------------------------------------ | ||
34 | require("../lzio.lua") | ||
35 | require("../llex.lua") | ||
36 | require("../lopcodes.lua") | ||
37 | require("../ldump.lua") | ||
38 | require("../lcode.lua") | ||
39 | require("../lparser.lua") | ||
40 | |||
41 | function lua_assert(test) | ||
42 | if not test then error("assertion failed!") end | ||
43 | end | ||
44 | luaX:init() | ||
45 | local LuaState = {} | ||
46 | |||
47 | ------------------------------------------------------------------------ | ||
48 | -- call graph generator | ||
49 | -- * (1) logging functions, (2) the wrapper initializer itself | ||
50 | ------------------------------------------------------------------------ | ||
51 | |||
52 | llog = {} | ||
53 | |||
54 | ------------------------------------------------------------------------ | ||
55 | -- initialize log file; the usual mode is append; can use stdout/stderr | ||
56 | ------------------------------------------------------------------------ | ||
57 | function llog:init(filename) | ||
58 | if filename == "stdout" then self.h = io.stdout | ||
59 | elseif filename == "stderr" then self.h = io.stderr | ||
60 | else | ||
61 | self.h = io.open(filename, "ab") | ||
62 | if not self.h then | ||
63 | error("can't open log file "..filename.."for writing") | ||
64 | end | ||
65 | end | ||
66 | self.h:write("\n-- start of log --\n\n") | ||
67 | end | ||
68 | |||
69 | ------------------------------------------------------------------------ | ||
70 | -- cleanly closes log file | ||
71 | ------------------------------------------------------------------------ | ||
72 | function llog:exit() | ||
73 | self.h:write("\n-- end of log --\n\n") | ||
74 | if self.h ~= io.stdout and self.h ~= io.stderr then | ||
75 | self.h:close() | ||
76 | end | ||
77 | end | ||
78 | |||
79 | ------------------------------------------------------------------------ | ||
80 | -- logs a message at a particular call depth | ||
81 | ------------------------------------------------------------------------ | ||
82 | function llog:msg(msg, level) | ||
83 | if level then msg = string.rep(" ", level)..msg end | ||
84 | self.h:write(msg) | ||
85 | self.h:flush() | ||
86 | end | ||
87 | |||
88 | ------------------------------------------------------------------------ | ||
89 | -- set up wrapper functions to do tracing on a per-module basis | ||
90 | ------------------------------------------------------------------------ | ||
91 | function llog:calltrace(parms) | ||
92 | ------------------------------------------------------------------ | ||
93 | -- process parameters | ||
94 | ------------------------------------------------------------------ | ||
95 | local module = parms.module | ||
96 | local modulename = parms.modulename | ||
97 | if type(module) ~= "table" then | ||
98 | error("module table parameter required") | ||
99 | elseif not modulename then | ||
100 | error("module name parameter required") | ||
101 | end | ||
102 | ------------------------------------------------------------------ | ||
103 | -- use either allow or deny list | ||
104 | ------------------------------------------------------------------ | ||
105 | local allow = parms.allow or {} | ||
106 | local deny = parms.deny or {} | ||
107 | if table.getn(allow) > 0 and table.getn(deny) > 0 then | ||
108 | error("can't apply both allow and deny lists at the same time") | ||
109 | end | ||
110 | ------------------------------------------------------------------ | ||
111 | -- select functions to wrap | ||
112 | ------------------------------------------------------------------ | ||
113 | local flist = {} | ||
114 | for i, f in pairs(module) do | ||
115 | local wrapthis | ||
116 | if table.getn(allow) > 0 then -- allow some only | ||
117 | wrapthis = false | ||
118 | for j, v in ipairs(allow) do | ||
119 | if i == v then wrapthis = true; break end | ||
120 | end | ||
121 | elseif table.getn(deny) > 0 then -- deny some only | ||
122 | wrapthis = true | ||
123 | for j, v in ipairs(deny) do | ||
124 | if i == v then wrapthis = false; break end | ||
125 | end | ||
126 | else -- default include | ||
127 | wrapthis = true | ||
128 | end | ||
129 | if wrapthis then flist[i] = f end | ||
130 | end | ||
131 | ------------------------------------------------------------------ | ||
132 | -- wrapped function(s) in a module for tracing | ||
133 | ------------------------------------------------------------------ | ||
134 | llog.level = 0 -- nesting level | ||
135 | for i, f in pairs(flist) do | ||
136 | local ModuleName = modulename..":" | ||
137 | local OldName, OldFunc = i, f | ||
138 | if type(OldFunc) == "function" then | ||
139 | local NewName = "__"..OldName | ||
140 | while module[NewName] ~= nil do -- avoid collisions | ||
141 | NewName = "_"..NewName | ||
142 | end | ||
143 | module[NewName] = OldFunc | ||
144 | module[OldName] = | ||
145 | ---------------------------------------------------------- | ||
146 | -- wrapper function for a module's function | ||
147 | -- old function XYZ is renamed __XYZ | ||
148 | ---------------------------------------------------------- | ||
149 | function(self, ...) | ||
150 | local parms = " (" | ||
151 | local exps = {} | ||
152 | -- look for expdesc structs, identify FuncState structs too | ||
153 | local function checkexpdesc(v) | ||
154 | local typ = type(v) | ||
155 | if typ == "table" then | ||
156 | if v.code then return "func" | ||
157 | elseif v.L then return "ls" | ||
158 | elseif v.seminfo then return "token" | ||
159 | elseif v.k then | ||
160 | table.insert(exps, v) | ||
161 | return "exp"..table.getn(exps) | ||
162 | end | ||
163 | end | ||
164 | return typ | ||
165 | end | ||
166 | -- format parameters for printing | ||
167 | for i,v in ipairs(arg) do | ||
168 | if type(v) == "number" then parms = parms..v.."," | ||
169 | elseif type(v) == "string" then parms = parms.."'"..v.."'," | ||
170 | elseif type(v) == "boolean" then parms = parms..tostring(v).."," | ||
171 | elseif SHOW_EXPDESC then parms = parms..checkexpdesc(v).."," | ||
172 | else parms = parms..type(v).."," | ||
173 | end | ||
174 | end | ||
175 | if table.getn(arg) > 0 then -- chop last comma | ||
176 | parms = string.sub(parms, 1, -2) | ||
177 | end | ||
178 | -- up level | ||
179 | llog:msg(ModuleName..OldName..parms..") {\n", llog.level) | ||
180 | llog.level = llog.level + 1 | ||
181 | -- display contents of expdesc | ||
182 | if SHOW_EXPDESC and table.getn(exps) > 0 then | ||
183 | for i,v in ipairs(exps) do | ||
184 | parms = "k:'"..v.k.."'," | ||
185 | if v.info then parms = parms.."info:"..v.info.."," end | ||
186 | if v.aux then parms = parms.."aux:"..v.aux.."," end | ||
187 | if v.t then parms = parms.."t:"..v.t.."," end | ||
188 | if v.f then parms = parms.."f:"..v.f.."," end | ||
189 | parms = string.sub(parms, 1, -2) | ||
190 | llog:msg("exp"..i.."("..parms..")\n", llog.level) | ||
191 | end | ||
192 | end | ||
193 | -- original function called here... | ||
194 | local retval = {self[NewName](self, unpack(arg))} | ||
195 | -- format return values | ||
196 | local rets = " = " | ||
197 | for i,v in ipairs(retval) do | ||
198 | if type(v) == "number" then rets = rets..v.."," | ||
199 | elseif type(v) == "string" then rets = rets.."'"..v.."'," | ||
200 | elseif type(v) == "boolean" then rets = rets..tostring(v).."," | ||
201 | else rets = rets..type(v).."," | ||
202 | end | ||
203 | end | ||
204 | if table.getn(retval) > 0 then -- chop last comma | ||
205 | rets = string.sub(rets, 1, -2) | ||
206 | else | ||
207 | rets = "" | ||
208 | end | ||
209 | -- down level | ||
210 | llog.level = llog.level - 1 | ||
211 | llog:msg("} "..ModuleName..OldName..rets.."\n", llog.level) | ||
212 | return unpack(retval) | ||
213 | end--function | ||
214 | ---------------------------------------------------------- | ||
215 | --print("patched "..OldName) | ||
216 | end--if | ||
217 | end--for | ||
218 | end | ||
219 | |||
220 | ------------------------------------------------------------------------ | ||
221 | -- testing here | ||
222 | -- * allow/deny works a bit like a somewhat similar Apache syntax | ||
223 | -- * e.g. to show only function 'lex' and 'save' -> allow={"lex","save",} | ||
224 | -- to not show function 'save_and_next' -> deny={"save_and_next",} | ||
225 | -- * you can't do both allow and deny at the same time | ||
226 | ------------------------------------------------------------------------ | ||
227 | |||
228 | -- select the file or stream to output to | ||
229 | --llog:init("calls.log") | ||
230 | llog:init("stdout") | ||
231 | |||
232 | -- select modules to trace | ||
233 | llog:calltrace{module=luaX, modulename="luaX", allow={"lex"} } | ||
234 | -- here we trace only the main lex() function, to avoid showing | ||
235 | -- too many lexer calls; we want to focus on luaY and luaK | ||
236 | llog:calltrace{module=luaY, modulename="luaY", deny={"growvector"} } | ||
237 | -- growvector() is just a limit checker in Yueliang, so drop it | ||
238 | -- to simplify the output log | ||
239 | llog:calltrace{module=luaK, modulename="luaK"} | ||
240 | --llog:calltrace{module=luaU, modulename="luaU"} | ||
241 | |||
242 | -- select input stream | ||
243 | local zio = luaZ:init(luaZ:make_getS("local a = 1"), nil, "=string") | ||
244 | --local zio = luaZ:init(luaZ:make_getF("sample.lua"), nil, "@sample.lua") | ||
245 | |||
246 | -- compile the source | ||
247 | local Func = luaY:parser(LuaState, zio, nil) | ||
248 | |||
249 | -- write binary chunk | ||
250 | local Writer, Buff = luaU:make_setF("call_graph.out") | ||
251 | luaU:dump(LuaState, Func, Writer, Buff) | ||
252 | |||
253 | llog:exit() | ||
254 | --end | ||