Annotate

tools/modtrace.lua @ 11195:c4cb536b67b5

tools.modtrace: Library for tracing/debugging Lua module and method calls
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Oct 2020 13:53:24 +0000
child 11197:50f182931bdd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11195
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Trace module calls and method calls on created objects
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 --
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Very rough and for debugging purposes only. It makes many
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- assumptions and there are many ways it could fail.
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Example use:
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- local dbuffer = require "tools.modtrace".trace("util.dbuffer");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 --
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local t_pack = require "util.table".pack;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local serialize = require "util.serialization".serialize;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local unpack = table.unpack or unpack; --luacheck: ignore 113
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local set = require "util.set";
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local function stringify_value(v)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if type(v) == "string" and #v > 20 then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return ("<string(%d)>"):format(#v);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 elseif type(v) == "function" then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return tostring(v);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 return serialize(v, "debug");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function stringify_params(...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local n = select("#", ...);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local r = {};
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 for i = 1, n do
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 table.insert(r, stringify_value((select(i, ...))));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return table.concat(r, ", ");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local function stringify_result(ret)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local r = {};
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 for i = 1, ret.n do
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 table.insert(r, stringify_value(ret[i]));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return table.concat(r, ", ");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function stringify_call(method_name, ...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return ("%s(%s)"):format(method_name, stringify_params(...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local function wrap_method(original_obj, original_method, method_name)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 method_name = ("<%s>:%s"):format(getmetatable(original_obj).__name or "object", method_name);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return function (new_obj_self, ...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local opts = new_obj_self._modtrace_opts;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local f = opts.output or io.stderr;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 f:write(stringify_call(method_name, ...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local ret = t_pack(original_method(original_obj, ...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if ret.n > 0 then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 f:write(" = ", stringify_result(ret), "\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 else
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 f:write("\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return unpack(ret, 1, ret.n);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local function wrap_function(original_function, function_name, opts)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local f = opts.output or io.stderr;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return function (...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 f:write(stringify_call(function_name, ...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local ret = t_pack(original_function(...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if ret.n > 0 then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 f:write(" = ", stringify_result(ret), "\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 else
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 f:write("\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return unpack(ret, 1, ret.n);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local function wrap_metamethod(name, method)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if name == "__index" then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return function (new_obj, k)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local original_method;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if type(method) == "table" then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 original_method = new_obj._modtrace_original_obj[k];
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 else
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 original_method = method(new_obj._modtrace_original_obj, k);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if original_method == nil then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return nil;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return wrap_method(new_obj._modtrace_original_obj, original_method, k);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return function (new_obj, ...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 return method(new_obj._modtrace_original_obj, ...);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local function wrap_mt(original_mt)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local new_mt = {};
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 for k, v in pairs(original_mt) do
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 new_mt[k] = wrap_metamethod(k, v);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 return new_mt;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local function wrap_obj(original_obj, opts)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local new_mt = wrap_mt(getmetatable(original_obj));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 return setmetatable({_modtrace_original_obj = original_obj, _modtrace_opts = opts}, new_mt);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local function wrap_new(original_new, function_name, opts)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 local f = opts.output or io.stderr;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 return function (...)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 f:write(stringify_call(function_name, ...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local ret = t_pack(original_new(...));
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local obj = ret[1];
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if ret.n == 1 and type(ret[1]) == "table" then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 f:write(" = <", getmetatable(ret[1]).__name or "object", ">", "\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 elseif ret.n > 0 then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 f:write(" = ", stringify_result(ret), "\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 else
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 f:write("\n");
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 if obj then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 ret[1] = wrap_obj(obj, opts);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return unpack(ret, 1, ret.n);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 local function trace(module, opts)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if type(module) == "string" then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 module = require(module);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 opts = opts or {};
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local new_methods = set.new(opts.new_methods or {"new"});
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 local fake_module = setmetatable({}, {
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 __index = function (_, k)
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if new_methods:contains(k) then
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 return wrap_new(module[k], k, opts);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 else
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return wrap_function(module[k], k, opts);
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 });
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 return fake_module;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 return {
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 wrap = trace;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 trace = trace;
c4cb536b67b5 tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 }