aboutsummaryrefslogtreecommitdiffstats
path: root/lib/observer/src/observer_html_lib.erl
blob: 0c4e32af49dcb0b4cbf4ce76f46e12de04b080a6 (plain) (blame)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2003-2018. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%
-module(observer_html_lib).

%% 
%% This module implements the HTML generation for the crashdump
%% viewer. No logic or states are kept by this module.
%%

-export([plain_page/1,
	 expandable_term/3,
	 warning/1]).

-include("crashdump_viewer.hrl").
-include("observer_defs.hrl").

%%%-----------------------------------------------------------------
%%% Display the given information as is, no heading
%%% Empty body if no info exists.
warning(Info) ->
    header(body(warning_body(Info))).

warning_body(Info) ->
    [warn(Info)].

%%%-----------------------------------------------------------------
%%% Display the given information as is, no heading
%%% Empty body if no info exists.
plain_page(Info) ->
    header(body(plain_body(Info))).

plain_body(Info) ->
    [pre(href_proc_port(lists:flatten(Info)))].

%%%-----------------------------------------------------------------
%%% Expanded memory
expandable_term(Heading,Expanded,Tab) ->
    header(Heading,body(expandable_term_body(Heading,Expanded,Tab))).
    
expandable_term_body(Heading,[],_Tab) ->
    [case Heading of
	 "MsgQueue" -> "No messages were found";
	 "Message Queue" -> "No messages were found";
	 "StackDump"  -> "No stack dump was found";
	 "Dictionary" -> "No dictionary was found";
	 "ProcState"  -> "Information could not be retrieved,"
			     " system messages may not be handled by this process.";
         "SaslLog"    -> "No log entry was found"
     end];
expandable_term_body(Heading,Expanded,Tab) ->
    Attr = "BORDER=0 CELLPADDING=0 CELLSPACING=1 WIDTH=100%",
    [case Heading of
	 "MsgQueue" ->
	     table(Attr,
		   [tr(
		      [th("WIDTH=70%","Message"),
		       th("WIDTH=30%","SeqTraceToken")]) |
		    element(1, lists:mapfoldl(fun(Msg, Even) ->
						      {msgq_table(Tab, Msg, Even),
						       not Even}
					      end,
					      true, Expanded))]);
	 "Message Queue" ->
	     table(Attr,
		   [tr(
		      [th("WIDTH=10%","Id"),
		       th("WIDTH=90%","Message")]) |
		    element(1, lists:mapfoldl(fun(Msg, {Even,N}) ->
						      {msgq_table(Tab, Msg, N, Even),
						       {not Even, N+1}}
					      end,
					      {true,1}, Expanded))]);
	 "StackDump" ->
	     table(Attr,
		   [tr(
		      [th("WIDTH=20%","Label"),
		       th("WIDTH=80%","Term")]) |
		    element(1, lists:mapfoldl(fun(Entry, Even) ->
						      {stackdump_table(Tab, Entry, Even),
						       not Even}
					      end, true, Expanded))]);
	 "ProcState" ->
	     table(Attr,
		   [tr(
		      [th("WIDTH=20%","Label"),
		       th("WIDTH=80%","Information")]) |
		    element(1, lists:mapfoldl(fun(Entry, Even) ->
						      {proc_state(Tab, Entry,Even),
						       not Even}
					      end, true, Expanded))]);         
     "SaslLog"  ->
             table(Attr,
                   [tr("BGCOLOR=white",[td("ALIGN=left", pre(href_proc_port(Expanded)))])]) ;
	 _ ->
	     table(Attr,
		   [tr(
		      [th("WIDTH=30%","Key"),
		       th("WIDTH=70%","Value")]) |
		    element(1, lists:mapfoldl(fun(Entry, Even) ->
						      {dict_table(Tab, Entry,Even),
						       not Even}
					      end, true, Expanded))])
     end].

msgq_table(Tab,{Msg0,Token0}, Even) ->
    Token = case Token0 of
		[] -> "";
		_ -> io_lib:fwrite("~w",[Token0])
	    end,
    Msg = all_or_expand(Tab,Msg0),
    tr(color(Even),[td(pre(Msg)), td(Token)]).

msgq_table(Tab,Msg0, Id, Even) ->
    Msg = all_or_expand(Tab,Msg0),
    tr(color(Even),[td(integer_to_list(Id)), td(pre(Msg))]).

stackdump_table(Tab,{Label0,Term0},Even) ->
    Label = io_lib:format("~w",[Label0]),
    Term = all_or_expand(Tab,Term0),
    tr(color(Even), [td("VALIGN=center",pre(Label)), td(pre(Term))]).

dict_table(Tab,{Key0,Value0}, Even) ->
    Key = all_or_expand(Tab,Key0),
    Value = all_or_expand(Tab,Value0),
    tr(color(Even), [td("VALIGN=center",pre(Key)), td(pre(Value))]).

proc_state(Tab,{Key0,Value0}, Even) ->
    Key = lists:flatten(io_lib:format("~ts",[Key0])),
    Value = all_or_expand(Tab,Value0),
    tr(color(Even), [td("VALIGN=center",Key), td(pre(Value))]).

all_or_expand(Tab,Term) ->
    Preview = io_lib:format("~tP",[Term,8]),
    Check = io_lib:format("~tP",[Term,100]),
    Exp = Preview=/=Check,
    all_or_expand(Tab,Term,Preview,Exp).
all_or_expand(_Tab,Term,Str,false)
  when not is_binary(Term) ->
    href_proc_port(lists:flatten(Str));
all_or_expand(Tab,Term,Preview,true)
  when not is_binary(Term) ->
    Key = {Key1,Key2,Key3} = {erlang:unique_integer([positive]),1,2},
    ets:insert(Tab,{Key,Term}),
    [href_proc_port(lists:flatten(Preview), false), $\n,
     href("TARGET=\"expanded\"",
	  ["#Term?key1="++integer_to_list(Key1)++
	       "&key2="++integer_to_list(Key2)++
	       "&key3="++integer_to_list(Key3)],
	  "Click to expand above term")];
all_or_expand(Tab,Bin,_PreviewStr,_Expand)
  when is_binary(Bin) ->
    OBSBin = observer_lib:make_obsbin(Bin,Tab),
    Term = io_lib:format("~tp", [OBSBin]),
    href_proc_port(lists:flatten(Term), true).

color(true) -> io_lib:format("BGCOLOR=\"#~2.16.0B~2.16.0B~2.16.0B\"", tuple_to_list(?BG_EVEN));
color(false) -> io_lib:format("BGCOLOR=\"#~2.16.0B~2.16.0B~2.16.0B\"", tuple_to_list(?BG_ODD)).

%%%-----------------------------------------------------------------
%%% Internal library
start_html() ->
    "<HTML>\n".
stop_html() ->
    "</HTML>".
start_html_body() ->
    "<BODY BGCOLOR=\"#FFFFFF\">\n".
stop_html_body() ->
    "</BODY>\n".

header(Body) ->
    header("","",Body).
header(Title,Body) ->
    header(Title,"",Body).
header(Title,JavaScript,Body) ->
    [%only_http_header(),
     html_header(Title,JavaScript,Body)].

html_header(Title,JavaScript,Body) ->    
    [start_html(),
     only_html_header(Title,JavaScript),
     Body,
     stop_html()].

only_html_header(Title,JavaScript) ->
    ["<HEAD>\n",
     "<TITLE>", Title,  "</TITLE>\n",
     JavaScript,
     "</HEAD>\n"].

body(Text) ->
    [start_html_body(),
     Text,
     stop_html_body()].

start_table(Args) ->
    ["<TABLE ", Args, ">\n"].
stop_table() ->
    "</TABLE>\n".

table(Args,Text) ->
    [start_table(Args), Text, stop_table()].
tr(Text) ->
    ["<TR>\n", Text, "\n</TR>\n"].
tr(Args,Text) ->
    ["<TR ", Args, ">\n", Text, "\n</TR>\n"].
th(Args,Text) ->
    ["<TH ", Args, ">\n", Text, "\n</TH>\n"].
td(Text) ->
    ["<TD>", Text, "</TD>"].
td(Args,Text) ->
    ["<TD ", Args, ">", Text, "</TD>"].

start_pre() ->
    "<PRE>".
stop_pre() ->
    "</PRE>".
pre(Text) ->
    [start_pre(),Text,stop_pre()].
href(Link,Text) ->
    ["<A HREF=\"",Link,"\">",Text,"</A>"].
href(Args,Link,Text) ->
    ["<A HREF=\"",Link,"\" ",Args,">",Text,"</A>"].
font(Args,Text) ->
    ["<FONT ",Args,">\n",Text,"\n</FONT>\n"].
p(Text) ->
    ["<P>",Text,"</P>\n"].
br() ->
    "<BR>\n".


%% In all the following, "<" is changed to "&lt;" and ">" is changed to "&gt;"
href_proc_port(Text) ->
    href_proc_port(Text,true).
href_proc_port(Text,LinkToBin) ->
    href_proc_port(Text,[],LinkToBin).
href_proc_port("#Ref<"++T,Acc,LTB) ->
    %% No links to refs
    href_proc_port(T,["#Ref&lt;"|Acc],LTB);
href_proc_port("#Fun<"++T,Acc,LTB) ->
    %% No links to funs
    href_proc_port(T,["#Fun&lt;"|Acc],LTB);
href_proc_port("#Port<"++T,Acc,LTB) ->
    {Port0,Rest} = split($>,T),
    Port = "#Port&lt;"++Port0 ++ "&gt;",
    href_proc_port(Rest,[href(Port,Port)|Acc],LTB);
href_proc_port("<<"++T,Acc,LTB) ->
    %% No links to binaries
    href_proc_port(T,["&lt;&lt;"|Acc],LTB);
href_proc_port("<"++([C|_]=T),Acc,LTB) when $0 =< C, C =< $9 ->
    %% Pid
    {Pid0,Rest} = split($>,T),
    Pid = "&lt;" ++ Pid0 ++ "&gt",
    href_proc_port(Rest,[href(Pid,Pid)|Acc],LTB);
href_proc_port("['#CDVBin'"++T,Acc,LTB) ->
    %% Binary written by crashdump_viewer:parse_heap_term(...)
    href_proc_bin(cdv, T, Acc, LTB);
href_proc_port("['#OBSBin'"++T,Acc,LTB) ->
    %% Binary written by crashdump_viewer:parse_heap_term(...)
    href_proc_bin(obs, T, Acc, LTB);
href_proc_port("['#CDVPort'"++T,Acc,LTB) ->
    %% Port written by crashdump_viewer:parse_term(...)
    {Port0,Rest} = split($],T),
    PortStr=
	case string:lexemes(Port0,",.|") of
	    [X,Y] ->
		Port = "#Port&lt;"++X++"."++Y++"&gt;",
		href(Port,Port);
	Ns ->
		"#Port&lt;" ++ lists:join($.,Ns) ++"...&gt;"
    end,
    href_proc_port(Rest,[PortStr|Acc],LTB);
href_proc_port("['#CDVPid'"++T,Acc,LTB) ->
    %% Pid written by crashdump_viewer:parse_term(...)
    {Pid0,Rest} = split($],T),
    PidStr =
	case string:lexemes(Pid0,",.|") of
	    [X,Y,Z] ->
		Pid = "&lt;"++X++"."++Y++"."++Z++"&gt;",
		href(Pid,Pid);
	    Ns ->
		"&lt;" ++ lists:join($.,Ns) ++ "...&gt;"
	end,
    href_proc_port(Rest,[PidStr|Acc],LTB);
href_proc_port("'#CDVIncompleteHeap'"++T,Acc,LTB)->
    %% The heap is incomplete! Written by crashdump_viewer:deref_pts(...)
    IH = lists:reverse(
	   lists:flatten(
	     "<FONT COLOR=\"#FF0000\">...(Incomplete Heap)</FONT>")),
    href_proc_port(T,IH++Acc,LTB);
href_proc_port("'#CDVTruncatedBinary'"++T,Acc,LTB)->
    %% A binary which is truncated! Written by 
    %% crashdump_viewer:parse_heap_term(...)
    IH = lists:reverse(
	   lists:flatten(
	     "<FONT COLOR=\"#FF0000\">&lt;&lt;...(Truncated Binary)&gt;&gt;"
	     "</FONT>")),
    href_proc_port(T,IH++Acc,LTB);
href_proc_port("'#CDVNonexistingBinary'"++T,Acc,LTB)->
    %% A binary which could not be found in the dump! Written by 
    %% crashdump_viewer:parse_heap_term(...)
    IH = lists:reverse(
	   lists:flatten(
	     "<FONT COLOR=\"#FF0000\">&lt;&lt;...(Nonexisting Binary)&gt;&gt;"
	     "</FONT>")),
    href_proc_port(T,IH++Acc,LTB);
href_proc_port("<"++T,Acc,LTB) ->
    href_proc_port(T,["&lt;"|Acc],LTB);
href_proc_port(">"++T,Acc,LTB) ->
    href_proc_port(T,["&gt;"|Acc],LTB);
href_proc_port([H|T],Acc,LTB) ->
    href_proc_port(T,[H|Acc],LTB);
href_proc_port([],Acc,_) ->
    lists:reverse(Acc).

href_proc_bin(From, T, Acc, LTB) ->
    {OffsetSizePos,Rest} = split($],T),
    BinStr =
	case string:lexemes(OffsetSizePos,",.| \n") of
	    [Offset,SizeStr,Pos] when From =:= cdv ->
                Size = list_to_integer(SizeStr),
                PreviewSize = min(Size,10),
		Id = {list_to_integer(Offset),PreviewSize,list_to_integer(Pos)},
                case crashdump_viewer:expand_binary(Id) of
                    {ok, '#CDVTruncatedBinary'} ->
                          lists:flatten(
                            "<FONT COLOR=\"#FF0000\">"
                            "&lt;&lt;...(Truncated Binary)&gt;&gt;"
                            "</FONT>");
                    {ok, PreviewBin} ->
                        PreviewStr = preview_string(Size, PreviewBin),
                        if LTB ->
                                href("TARGET=\"expanded\"",
                                     ["#Binary?offset="++Offset++
                                          "&size="++SizeStr++
                                          "&pos="++Pos],
                                     PreviewStr);
                           true ->
                                PreviewStr
                        end
                end;
	    [PreviewIntStr,PreviewBitSizeStr,SizeStr,Md5] when From =:= obs ->
		Size = list_to_integer(SizeStr),
                PreviewInt = list_to_integer(PreviewIntStr),
                PreviewBitSize = list_to_integer(PreviewBitSizeStr),
		PreviewStr = preview_string(Size,<<PreviewInt:PreviewBitSize>>),
		if LTB ->
			href("TARGET=\"expanded\"",
			     ["#OBSBinary?key1="++PreviewIntStr++
				  "&key2="++SizeStr++
				  "&key3="++Md5],
			     PreviewStr);
		   true ->
			PreviewStr
		end;
	    _ ->
		"&lt;&lt; ... &gt;&gt;"
	end,
    href_proc_port(Rest,[BinStr|Acc],LTB).

preview_string(Size, PreviewBin) when Size > 10 ->
    ["&lt;&lt;",
     remove_lgt(io_lib:format("~tp",[PreviewBin])),
     "...(",
     observer_lib:to_str({bytes,Size}),
     ")",
     "&gt;&gt"];
preview_string(_, PreviewBin) ->
    ["&lt;&lt;",
     remove_lgt(io_lib:format("~tp",[PreviewBin])),
     "&gt;&gt"].

remove_lgt(Deep) ->
    remove_lgt_1(lists:flatten(Deep)).

remove_lgt_1([$<,$<|Rest]) ->
    [$>,$>|BinStr] = lists:reverse(Rest),
    replace_lgt(lists:reverse(BinStr));
remove_lgt_1(TruncBin) ->
    TruncBin.

replace_lgt([$<|R]) ->
    ["&lt;"|replace_lgt(R)];
replace_lgt([$>|R]) ->
    ["&gt;"|replace_lgt(R)];
replace_lgt([L=[_|_]|R]) ->
    [replace_lgt(L)|replace_lgt(R)];
replace_lgt([A|R]) ->
    [A|replace_lgt(R)];
replace_lgt([]) -> [].

split(Char,Str) ->
    split(Char,Str,[]).
split(Char,[Char|Str],Acc) -> % match Char
    {lists:reverse(Acc),Str};
split(Char,[H|T],Acc) ->
    split(Char,T,[H|Acc]).

warn([]) ->
    [];
warn(Warning) ->
    font("COLOR=\"#FF0000\"",p([Warning,br(),br()])).