<commit_before>%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2016. 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(ssh_dbg).

-export([messages/0,
	 messages/1,
	 messages/2,
	 stop/0
	]).

-export([shrink_bin/1,
	 wr_record/3]).

-include("ssh.hrl").
-include("ssh_transport.hrl").
-include("ssh_connect.hrl").
-include("ssh_auth.hrl").

-record(data, {
	  writer,
	  acc = []}).
%%%================================================================
messages() ->
    messages(fun(String,_D) -> io:format(String) end).

messages(Write) when is_function(Write,2) ->
    messages(Write, fun(X) -> X end).

messages(Write, MangleArg) when is_function(Write,2),
				is_function(MangleArg,1) ->
    catch dbg:start(),
    setup_tracer(Write, MangleArg),
    dbg:p(new,c),
    dbg_ssh_messages().

dbg_ssh_messages() ->
    dbg:tp(ssh_message,encode,1, x),
    dbg:tp(ssh_message,decode,1, x),
    dbg:tpl(ssh_transport,select_algorithm,3, x).

%%%----------------------------------------------------------------
stop() ->
    dbg:stop().

%%%================================================================
msg_formater({trace,Pid,call,{ssh_message,encode,[Msg]}}, D) ->
    fmt("~nSEND ~p ~s~n", [Pid,wr_record(shrink_bin(Msg))], D);
msg_formater({trace,_Pid,return_from,{ssh_message,encode,1},_Res}, D) -> 
    D;
	
msg_formater({trace,_Pid,call,{ssh_message,decode,_}}, D) ->
    D;
msg_formater({trace,Pid,return_from,{ssh_message,decode,1},Msg}, D) -> 
    fmt("~n~p RECV ~s~n", [Pid,wr_record(shrink_bin(Msg))], D);
	
msg_formater({trace,_Pid,call,{ssh_transport,select_algorithm,_}}, D) ->
    D;
msg_formater({trace,Pid,return_from,{ssh_transport,select_algorithm,3},{ok,Alg}}, D) ->
    fmt("~n~p ALGORITHMS~n~s~n", [Pid, wr_record(Alg)], D);


msg_formater({trace,Pid,send,{tcp,Sock,Bytes},Pid}, D) ->
    fmt("~n~p TCP SEND on ~p~n ~p~n", [Pid,Sock, shrink_bin(Bytes)], D);

msg_formater({trace,Pid,send,{tcp,Sock,Bytes},Dest}, D) ->
    fmt("~n~p TCP SEND from ~p TO ~p~n ~p~n", [Pid,Sock,Dest, shrink_bin(Bytes)], D);

msg_formater({trace,Pid,send,ErlangMsg,Dest}, D) ->
    fmt("~n~p ERL MSG SEND TO ~p~n ~p~n", [Pid,Dest, shrink_bin(ErlangMsg)], D);


msg_formater({trace,Pid,'receive',{tcp,Sock,Bytes}}, D) ->
    fmt("~n~p TCP RECEIVE on ~p~n ~p~n", [Pid,Sock,shrink_bin(Bytes)], D);

msg_formater({trace,Pid,'receive',ErlangMsg}, D) ->
    fmt("~n~p ERL MSG RECEIVE~n ~p~n", [Pid,shrink_bin(ErlangMsg)], D);


msg_formater(M, D) ->
    fmt("~nDBG ~n~p~n", [shrink_bin(M)], D).

%% msg_formater(_, D) -> 
%%     D.


fmt(Fmt, Args,  D=#data{writer=Write,acc=Acc}) ->
    D#data{acc = Write(io_lib:format(Fmt, Args), Acc)}.

%%%----------------------------------------------------------------
setup_tracer(Write, MangleArg) ->
    Handler = fun(Arg, D) ->
		      msg_formater(MangleArg(Arg), D)
	      end,
    InitialData = #data{writer = Write},
    {ok,_} = dbg:tracer(process, {Handler, InitialData}),
    ok.

%%%----------------------------------------------------------------
shrink_bin(B) when is_binary(B), size(B)>100 -> {'*** SHRINKED BIN',
						 size(B),
						 element(1,split_binary(B,20)),
						 '...',
						 element(2,split_binary(B,size(B)-20))
						};
shrink_bin(L) when is_list(L) -> lists:map(fun shrink_bin/1, L);
shrink_bin(T) when is_tuple(T) -> list_to_tuple(shrink_bin(tuple_to_list(T)));
shrink_bin(X) -> X.

%%%----------------------------------------------------------------
-define(wr_record(N,BlackList), wr_record(R=#N{}) -> wr_record(R, record_info(fields,N), BlackList)).

-define(wr_record(N), ?wr_record(N, [])).


?wr_record(alg);

?wr_record(ssh_msg_disconnect);
?wr_record(ssh_msg_ignore);
?wr_record(ssh_msg_unimplemented);
?wr_record(ssh_msg_debug);<commit_msg>ssh: ssh_dbg now reports HELLO msgs and timestamps<commit_after>%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2016. 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(ssh_dbg).

-export([messages/0,
	 messages/1,
	 messages/2,
	 stop/0
	]).

-export([shrink_bin/1,
	 wr_record/3]).

-include("ssh.hrl").
-include("ssh_transport.hrl").
-include("ssh_connect.hrl").
-include("ssh_auth.hrl").

-record(data, {
	  writer,
	  acc = []}).
%%%================================================================
messages() ->
    messages(fun(String,_D) -> io:format(String) end).

messages(Write) when is_function(Write,2) ->
    messages(Write, fun(X) -> X end).

messages(Write, MangleArg) when is_function(Write,2),
				is_function(MangleArg,1) ->
    catch dbg:start(),
    setup_tracer(Write, MangleArg),
    dbg:p(new,[c,timestamp]),
    dbg_ssh_messages().

dbg_ssh_messages() ->
    dbg:tp(ssh_message,encode,1, x),
    dbg:tp(ssh_message,decode,1, x),
    dbg:tpl(ssh_transport,select_algorithm,3, x),
    dbg:tp(ssh_transport,hello_version_msg,1, x),
    dbg:tp(ssh_transport,handle_hello_version,1, x).
   
%%%----------------------------------------------------------------
stop() ->
    dbg:stop().

%%%================================================================
msg_formater({trace_ts,Pid,call,{ssh_message,encode,[Msg]},TS}, D) ->
    fmt("~n~s SEND ~p ~s~n", [ts(TS),Pid,wr_record(shrink_bin(Msg))], D);
msg_formater({trace_ts,_Pid,return_from,{ssh_message,encode,1},_Res,_TS}, D) -> 
    D;
	
msg_formater({trace_ts,_Pid,call,{ssh_message,decode,_},_TS}, D) ->
    D;
msg_formater({trace_ts,Pid,return_from,{ssh_message,decode,1},Msg,TS}, D) -> 
    fmt("~n~s ~p RECV ~s~n", [ts(TS),Pid,wr_record(shrink_bin(Msg))], D);
	
msg_formater({trace_ts,_Pid,call,{ssh_transport,select_algorithm,_},_TS}, D) ->
    D;
msg_formater({trace_ts,Pid,return_from,{ssh_transport,select_algorithm,3},{ok,Alg},TS}, D) ->
    fmt("~n~s ~p ALGORITHMS~n~s~n", [ts(TS),Pid, wr_record(Alg)], D);

msg_formater({trace_ts,_Pid,call,{ssh_transport,hello_version_msg,_},_TS}, D) ->
    D;
msg_formater({trace_ts,Pid,return_from,{ssh_transport,hello_version_msg,1},Hello,TS}, D) -> 
    fmt("~n~s ~p TCP SEND HELLO~n  ~p~n", [ts(TS),Pid,lists:flatten(Hello)], D);

msg_formater({trace_ts,Pid,call,{ssh_transport,handle_hello_version,[Hello]},TS}, D) ->
    fmt("~n~s ~p RECV HELLO~n  ~p~n", [ts(TS),Pid,lists:flatten(Hello)], D);
msg_formater({trace_ts,_Pid,return_from,{ssh_transport,handle_hello_version,1},_,_TS}, D) -> 
    D;

msg_formater({trace_ts,Pid,send,{tcp,Sock,Bytes},Pid,TS}, D) ->
    fmt("~n~s ~p TCP SEND on ~p~n ~p~n", [ts(TS),Pid,Sock, shrink_bin(Bytes)], D);

msg_formater({trace_ts,Pid,send,{tcp,Sock,Bytes},Dest,TS}, D) ->
    fmt("~n~s ~p TCP SEND from ~p TO ~p~n ~p~n", [ts(TS),Pid,Sock,Dest, shrink_bin(Bytes)], D);

msg_formater({trace_ts,Pid,send,ErlangMsg,Dest,TS}, D) ->
    fmt("~n~s ~p ERL MSG SEND TO ~p~n ~p~n", [ts(TS),Pid,Dest, shrink_bin(ErlangMsg)], D);


msg_formater({trace_ts,Pid,'receive',{tcp,Sock,Bytes},TS}, D) ->
    fmt("~n~s ~p TCP RECEIVE on ~p~n ~p~n", [ts(TS),Pid,Sock,shrink_bin(Bytes)], D);

msg_formater({trace_ts,Pid,'receive',ErlangMsg,TS}, D) ->
    fmt("~n~s ~p ERL MSG RECEIVE~n ~p~n", [ts(TS),Pid,shrink_bin(ErlangMsg)], D);


msg_formater(M, D) ->
    fmt("~nDBG ~n~p~n", [shrink_bin(M)], D).

%% msg_formater(_, D) -> 
%%     D.


fmt(Fmt, Args,  D=#data{writer=Write,acc=Acc}) ->
    D#data{acc = Write(io_lib:format(Fmt, Args), Acc)}.

ts({_,_,Usec}=Now) ->
    {_Date,{HH,MM,SS}} = calendar:now_to_local_time(Now),
    io_lib:format("~.2.0w:~.2.0w:~.2.0w.~.6.0w",[HH,MM,SS,Usec]);
ts(_) ->
    "-".
%%%----------------------------------------------------------------
setup_tracer(Write, MangleArg) ->
    Handler = fun(Arg, D) ->
		      msg_formater(MangleArg(Arg), D)
	      end,
    InitialData = #data{writer = Write},
    {ok,_} = dbg:tracer(process, {Handler, InitialData}),
    ok.

%%%----------------------------------------------------------------
shrink_bin(B) when is_binary(B), size(B)>256 -> {'*** SHRINKED BIN',
						 size(B),
						 element(1,split_binary(B,64)),
						 '...',
						 element(2,split_binary(B,size(B)-64))
						};
shrink_bin(L) when is_list(L) -> lists:map(fun shrink_bin/1, L);
shrink_bin(T) when is_tuple(T) -> list_to_tuple(shrink_bin(tuple_to_list(T)));
shrink_bin(X) -> X.

%%%----------------------------------------------------------------
-define(wr_record(N,BlackList), wr_record(R=#N{}) -> wr_record(R, record_info(fields,N), BlackList)).

-define(wr_record(N), ?wr_record(N, [])).


?wr_record(alg);

?wr_record(ssh_msg_disconnect);
?wr_record(ssh_msg_ignore);
?wr_record(ssh_msg_unimplemented);
?wr_record(ssh_msg_debug);
{'fraction_non_alphanumeric': 0.26677099634846113, 'fraction_numerical': 0.008972352634324466, 'mean_word_length': 3.913377754997437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7022716', 'n_tokens_mistral': 3862, 'n_tokens_neox': 3563, 'n_words': 614}