Software /
code /
prosody
Annotate
tools/modtrace.lua @ 12629:4c1d3f817063
util.datetime: Add support for sub-second precision timestamps
Lua since 5.3 raises a fuss when time functions are handed a number with
a fractional part and the underlying C functions are all based on
integer seconds without support for more precision.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 14 Aug 2022 16:57:31 +0200 |
parent | 12590:5eaf77114fdb |
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 |
12590
5eaf77114fdb
compat: Use table.pack (there since Lua 5.2) over our util.table
Kim Alvefur <zash@zash.se>
parents:
12589
diff
changeset
|
11 local t_pack = table.pack; |
11195
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; |
12589
39ae08180c81
compat: Remove handling of Lua 5.1 location of 'unpack' function
Kim Alvefur <zash@zash.se>
parents:
11197
diff
changeset
|
13 local unpack = table.unpack; |
11195
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 |
11197
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
16 local serialize_cfg = { |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
17 preset = "oneline"; |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
18 freeze = true; |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
19 fatal = false; |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
20 fallback = function (v) return "<"..tostring(v)..">" end; |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
21 }; |
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
22 |
11195
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 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
|
27 return tostring(v); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
11197
50f182931bdd
tools.modtrace: Pass config to serialize()
Matthew Wild <mwild1@gmail.com>
parents:
11195
diff
changeset
|
29 return serialize(v, serialize_cfg); |
11195
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 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 local function stringify_params(...) |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local n = select("#", ...); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local r = {}; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 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
|
36 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
|
37 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 return table.concat(r, ", "); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 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
|
42 local r = {}; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return table.concat(r, ", "); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 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
|
50 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
|
51 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 else |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 f:write("\n"); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 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
|
66 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 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
|
70 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
|
71 return function (...) |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 else |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 f:write("\n"); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 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
|
80 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 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
|
84 if name == "__index" then |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 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
|
86 local original_method; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 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
|
88 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
|
89 else |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 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
|
93 return nil; |
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 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
|
96 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 return function (new_obj, ...) |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 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
|
100 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 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
|
104 local new_mt = {}; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 return new_mt; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 end |
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 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
|
117 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
|
118 return function (...) |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 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
|
120 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
|
121 local obj = ret[1]; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 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
|
127 else |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 f:write("\n"); |
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 if obj then |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 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
|
133 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 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
|
135 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 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
|
139 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
|
140 module = require(module); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 opts = opts or {}; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 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
|
144 local fake_module = setmetatable({}, { |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 __index = function (_, k) |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 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
|
147 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
|
148 else |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 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
|
150 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 }); |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 return fake_module; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 end |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 return { |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 wrap = trace; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 trace = trace; |
c4cb536b67b5
tools.modtrace: Library for tracing/debugging Lua module and method calls
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 } |