-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_default.erl
211 lines (184 loc) · 6.39 KB
/
user_default.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-module(user_default).
-author('serge@hq.idt.net').
%% Compile this file and use this line in your ~/.erlang file (with
%% correct path, of course!) to where the user_default.beam file is stored.
%%
%% code:load_abs("/home/fritchie/erlang/user_default").
-export([help/0,dbgtc/1, dbgtc_follow/1, dbgon/1, dbgon/2,
dbgadd/1, dbgadd/2, dbgdel/1, dbgdel/2, dbgoff/0,
dbg_ip_trace/1,
l/0, mm/0, la/0]).
-export([my_tracer/0, my_dhandler/2, filt_state_from_term/1]).
-import(io, [format/1]).
help() ->
shell_default:help(),
format("** user extended commands **~n"),
format("dbgtc(File) -- use dbg:trace_client() to read data from File\n"),
format("dbgon(M) -- enable dbg tracer on all funs in module M\n"),
format("dbgon(M,Fun) -- enable dbg tracer for module M and function F\n"),
format("dbgon(M,File) -- enable dbg tracer for module M and log to File\n"),
format("dbgadd(M) -- enable call tracer for module M\n"),
format("dbgadd(M,F) -- enable call tracer for function M:F\n"),
format("dbgdel(M) -- disable call tracer for module M\n"),
format("dbgdel(M,F) -- disable call tracer for function M:F\n"),
format("dbgoff() -- disable dbg tracer (calls dbg:stop/0)\n"),
format("l() -- load all changed modules\n"),
format("la() -- load all modules\n"),
% format("nl() -- load all changed modules on all known nodes\n"),
format("mm() -- list modified modules\n"),
true.
dbgtc(File) ->
Fun = fun({trace,_,call,{M,F,A}}, _) ->
io:format("call: ~w:~w~w~n", [M,F,A]);
({trace,_,return_from,{M,F,A},R}, _) ->
io:format("retn: ~w:~w/~w -> ~w~n", [M,F,A,R]);
(A,B) ->
io:format("~w: ~w~n", [A,B])
end,
dbg:trace_client(file, File, {Fun, []}).
dbgtc_follow(File) ->
Fun = fun({trace,_,call,{M,F,A}}, _) ->
io:format("call: ~w:~w~w~n", [M,F,A]);
({trace,_,return_from,{M,F,A},R}, _) ->
io:format("retn: ~w:~w/~w -> ~w~n", [M,F,A,R]);
(A,B) ->
io:format("~w: ~w~n", [A,B])
end,
dbg:trace_client(follow_file, File, {Fun, []}).
dbgon(Module) ->
case dbg:tracer() of
{ok,_} ->
dbg:p(all,call),
dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
ok;
Else ->
Else
end.
dbgon(Module, Fun) when is_atom(Fun) ->
{ok,_} = dbg:tracer(),
dbg:p(all,call),
%%dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
dbg:tpl(Module, Fun, [{'_',[],[{return_trace},{exception_trace}]}]),
ok;
dbgon(Module, File) when is_list(File) ->
{ok,_} = dbg:tracer(port, dbg:trace_port(file, File)),
dbg:p(all, call),
dbg:tpl(Module, [{'_',[],[{return_trace},{exception_trace}]}]),
ok;
dbgon(Module, TcpPort) when is_integer(TcpPort) ->
io:format("Use this command on the node you're tracing (-remsh ...)\n"),
io:format("Use dbg:stop() on target node when done.\n"),
{ok,_} = dbg:tracer(port, dbg:trace_port(ip, TcpPort)),
dbg:p(all,call),
%%dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
dbg:tpl(Module, [{'_',[],[{return_trace},{exception_trace}]}]),
ok.
dbg_ip_trace(TcpPort) ->
io:format("Run on same machine as target but NOT -remsh target-node\n"),
io:format("Use dbg:stop() when done.\n"),
dbg:trace_client(ip, TcpPort).
dbgadd(Module) ->
%%dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
dbg:tpl(Module, [{'_',[],[{return_trace},{exception_trace}]}]),
ok.
dbgadd(Module, Fun) ->
%%dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
dbg:tpl(Module, Fun, [{'_',[],[{return_trace},{exception_trace}]}]),
ok.
dbgdel(Module) ->
dbg:ctpl(Module),
ok.
dbgdel(Module, Fun) ->
dbg:ctpl(Module, Fun),
ok.
dbgoff() ->
dbg:stop().
%% Reload modules that have been modified since last load. From Tobbe
%% Tornqvist, http://blog.tornkvist.org/, "Easy load of recompiled
%% code", which may in turn have come from Serge?
l() ->
[c:l(M) || M <- mm()].
mm() ->
modified_modules().
modified_modules() ->
[M || {M, _} <- code:all_loaded(),
module_modified(M) == true].
module_modified(Module) ->
case code:is_loaded(Module) of
{file, preloaded} ->
false;
{file, Path} ->
CompileOpts =
proplists:get_value(compile, Module:module_info()),
CompileTime = proplists:get_value(time, CompileOpts),
Src = proplists:get_value(source, CompileOpts),
module_modified(Path, CompileTime, Src);
_ ->
false
end.
module_modified(Path, PrevCompileTime, PrevSrc) ->
case find_module_file(Path) of
false ->
false;
ModPath ->
case beam_lib:chunks(ModPath, ["CInf"]) of
{ok, {_, [{_, CB}]}} ->
CompileOpts = binary_to_term(CB),
CompileTime = proplists:get_value(time,
CompileOpts),
Src = proplists:get_value(source, CompileOpts),
not (CompileTime == PrevCompileTime) and
(Src == PrevSrc);
_ ->
false
end
end.
find_module_file(Path) ->
case file:read_file_info(Path) of
{ok, _} ->
Path;
_ ->
%% may be the path was changed
case code:where_is_file(filename:basename(Path)) of
non_existing ->
false;
NewPath ->
NewPath
end
end.
%% Reload all modules, regardless of age.
la() ->
FiltZip = lists:filter(
fun({_Mod, Path}) when is_list(Path) ->
case string:str(Path, "/kernel-") +
string:str(Path, "/stdlib-") of
0 -> true;
_ -> false
end;
(_) -> false
end, code:all_loaded()),
{Ms, _} = lists:unzip(FiltZip),
lists:foldl(fun(M, Acc) ->
case shell_default:l(M) of
{error, _} -> Acc;
_ -> [M|Acc]
end
end, [], Ms).
my_tracer() ->
dbg:tracer(process, {fun my_dhandler/2, user}).
my_dhandler(TraceMsg, Acc) ->
dbg:dhandler(filt_state_from_term(TraceMsg), Acc).
filt_state_from_term(T) when is_tuple(T), element(1, T) == state ->
sTatE;
filt_state_from_term(T) when is_tuple(T), element(1, T) == chain_r ->
cHain_R;
filt_state_from_term(T) when is_tuple(T), element(1, T) == g_hash_r ->
g_Hash_R;
filt_state_from_term(T) when is_tuple(T), element(1, T) == hash_r ->
hAsh_R;
filt_state_from_term(T) when is_tuple(T) ->
list_to_tuple(filt_state_from_term(tuple_to_list(T)));
filt_state_from_term([H|T]) ->
[filt_state_from_term(H)|filt_state_from_term(T)];
filt_state_from_term(X) ->
X.