/
yaws_debug.erl
237 lines (194 loc) · 5.38 KB
/
yaws_debug.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
%%%----------------------------------------------------------------------
%%% File : yaws_debug.erl
%%% Author : Claes Wikstrom <klacke@hyber.org>
%%% Purpose :
%%% Created : 7 Feb 2002 by Claes Wikstrom <klacke@hyber.org>
%%%----------------------------------------------------------------------
-module(yaws_debug).
-author('klacke@hyber.org').
-compile(export_all).
-include_lib("yaws/include/yaws.hrl").
-include_lib("yaws/include/yaws_api.hrl").
-include("yaws_debug.hrl").
typecheck([{record, Rec, X} | Tail], File, Line) when atom(X),
element(1, Rec) == X ->
typecheck(Tail, File, Line);
typecheck([{int, Int} |Tail], File, Line) when integer(Int) ->
typecheck(Tail, File, Line);
typecheck([Err|_], File, Line) ->
io:format(user, "TC ERROR ~s:~w:~n~p",
[File, Line, Err]),
exit(tcerr);
typecheck([], _,_) ->
ok.
%% returns {record, RecName, [Field1, Val1} .....]
format_record(Record, Name, Fields) ->
case tuple_to_list(Record) of
[Name | Rest] ->
{record, Name, format_record(Rest, Fields)};
_X ->
?Debug("Bad record ~p is not ~p~n", [_X, Name]),
"badrecord"
end.
format_record([], []) ->
[];
format_record([Val|Vals], [F|Fs]) ->
[{F, ?F("~p", [nobin(Val)])} |
format_record(Vals, Fs)].
assert(equal,X,Y,_) when X==Y ->
ok;
assert(neq,X,Y,_) when X/=Y ->
ok;
assert(integer,X,_,_) when integer(X) ->
ok;
assert(list,X,_,_) when list(X) ->
ok;
assert({list,length,equal},X,Y,_) when list(X), length(X)==Y ->
ok;
assert(greater,X,Y,_) when integer(X), integer(Y), X>Y ->
ok;
assert(min,X,Y,_) when integer(X), integer(Y), X>=Y ->
ok;
assert(lesser,X,Y,_) when integer(X), integer(Y), X<Y ->
ok;
assert(max,X,Y,_) when integer(X), integer(Y), X=<Y ->
ok;
assert(interval,X,{Min,Max},_) when integer(X), integer(Min),
integer(Max),
X>=Min, Max>=X ->
ok;
assert('fun', Fun, _, Failure) ->
case catch Fun() of
true -> ok;
_Other -> fail(Failure)
end;
assert(in,X,L,Failure) when list(L) ->
case lists:member(X,L) of
true -> ok;
_ -> fail(Failure)
end;
assert(_,_,_,Failure) ->
fail(Failure).
fail({assert,File,Line,Message}) ->
io:format(user, "Assertion FAILED ~p:~p, pid ~w exiting: ~p~n",
[File, Line, self(), Message]),
exit(assertion_failed);
fail({alert,File,Line,Message}) ->
io:format(user, "Assert WARNING ~p:~p, pid ~w: ~p~n",
[File, Line, self(), Message]),
ok;
fail({{debug,Fstr}, File,Line,Fmt, Args}) ->
Str = lists:flatten(
io_lib:format("~s <~p> ~s:~p, pid ~w: ~n",
[Fstr, node(), filename:basename(File),
Line, self()])),
case io:format(user, Str ++ Fmt ++ "~n", Args) of
ok -> ok;
_ -> io:format(user, "ERROR ~p:~p: Pid ~w: (bad format)~n~p,~p~n",
[File, Line, self(), Fmt, Args]),
ok
end;
fail({format, File,Line,Fmt,Args}) ->
case io:format(user, Fmt,Args) of
ok -> ok;
_ ->
io:format(user, "ERROR ~p:~p: Pid ~w: (bad format)~n~p,~p~n",
[File, Line, self(), Fmt, Args]),
ok
end.
format(GC, F, A) when ?gc_has_debug(GC) ->
io:format(F, A);
format(_,_,_) ->
ok.
derror(GC, F, A) when ?gc_has_debug(GC) ->
error_logger:error_msg(F, A);
derror(_,_,_) ->
ok.
dinfo(GC, F, A) when ?gc_has_debug(GC) ->
error_logger:info_msg(F, A);
dinfo(_,_,_) ->
ok.
mktags() ->
tags:dirs(["."]),
init:stop().
xref([Dir]) ->
io:format("~p~n", [xref:d(Dir)]),
init:stop().
pids() ->
lists:zf(
fun(P) ->
case process_info(P) of
L when list(L) ->
{value, {_, {M1, _,_}}} =
lists:keysearch(current_function, 1, L),
{value, {_, {M2, _,_}}} =
lists:keysearch(initial_call, 1, L),
S1= atom_to_list(M1),
S2 = atom_to_list(M2),
case {S1, S2} of
{"yaws" ++ _, _} ->
{true, P};
{_, "yaws"++_} ->
{true, P};
_ ->
false
end;
_ ->
false
end
end,
processes()).
eprof() ->
eprof:start(),
eprof:profile(pids()),
io:format("Ok run some traffic \n", []).
%% not used
check_headers(L) ->
Hs = string:tokens(lists:flatten(L), "\r\n"),
io:format("XX ~p~n", [Hs]),
lists:foreach(
fun(H) ->
case lists:reverse(H) of
[_,_,$\r|_] ->
error_logger:error_msg("Bad header ~p, it contains"
" '\\r' or '\\n' at end ",
[lists:flatten(H)]),
exit(normal);
[_,_,$\n|_] ->
error_logger:error_msg("Bad header ~p, it contains"
" '\\r' or '\\n' at end ",
[lists:flatten(H)]),
exit(normal);
_ ->
ok
end
end, Hs).
check_headers([$\r, $\n |Tail], Last) when Tail /= [] ->
case lists:member(hd(Tail), [$\r, $\n]) of
true ->
error_logger:error_msg("Bad header ~p, it contains"
" '\\r' or '\\n' at end ", [lists:reverse(Last)]),
exit(normal);
_ ->
check_headers(Tail, [])
end;
check_headers([H|T], Last) ->
check_headers(T, [H|Last]);
check_headers([], _) ->
ok.
nobin(X) ->
case catch xnobin(X) of
{'EXIT', Reason} ->
io:format("~p~n~p~n", [X, Reason]),
exit(Reason);
Res ->
Res
end.
xnobin(B) when binary(B) ->
lists:flatten(io_lib:format("#Bin(~w)", [size(B)]));
xnobin(L) when list(L) ->
lists:map(fun(X) -> xnobin(X) end, L);
xnobin(T) when tuple(T) ->
list_to_tuple(xnobin(tuple_to_list(T)));
xnobin(X) ->
X.