Mercurial > prosody-hg
annotate tools/modtrace.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parents | 5eaf77114fdb |
| children |
| 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 } |
