/
user_default.erl
152 lines (128 loc) · 3.89 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
%% -*- erlang-indent-level: 2 -*-
%%% Created : 7 Apr 2008 by Mats Cronqvist <masse@kreditor.se>
%% @doc
%% @end
-module('user_default').
-author('Mats Cronqvist').
-export([ineti/0,
sig/1,sig/2,
ports/0,
export_all/1,
tab/0,
long/1,flat/1,dump/1,
e/2,
kill/1,
pi/1,pi/2,
os/1,
callstack/1,
bt/1,
pid/1,
lm/0]).
%% recompiles M with export_all without access to the source.
export_all(M) ->
case code:which(M) of
non_existing -> no_such_module;
F ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(F,[abstract_code]),
{ok,_,B} = compile:forms(AC,[export_all]),
code:soft_purge(M),
code:load_binary(M,F,B)
end.
lm() ->
MD5File =
fun(F) ->
case beam_lib:md5(F) of
{ok,{_,<<I:128>>}} -> [I];
_ -> undefined
end
end,
MD5Loaded =
fun(M) ->
proplists:get_value(vsn,M:module_info(attributes))
end,
Load =
fun(M,"") ->
{cannot_load,M};
(M,F) ->
code:purge(M),
{module,M} = code:load_abs(filename:rootname(F,".beam")),
M
end,
[Load(M,F) || {M,F} <- code:all_loaded(), MD5Loaded(M) =/= MD5File(F)].
sig(M,F) ->
[{F,lists:usort([[siga(A) || A <- As]
|| {clause,_,As,_,_} <- C])}
|| {function,_,G,_,C} <- forms(M),F==G].
siga({var,_,V}) -> V;
siga({cons,_,_,_}) -> list;
siga({nil,_}) -> list;
siga({match,_,A1,A2}) -> {siga(A1),siga(A2)};
siga(X) -> X.
sig(M) ->
[{F,A}||{function,_,F,A,_}<-forms(M)].
forms(M) ->
{ok,{M,[{"Abst",B}]}} = beam_lib:chunks(code:which(M),["Abst"]),
{_,Forms} = binary_to_term(B),
Forms.
tab() ->
N=node(),
io:setopts([{expand_fun,fun(B)->rpc:call(N,edlin_expand,expand,[B]) end}]).
dump(Term)->
File = filename:join([os:getenv("HOME"),"erlang.dump"]),
{ok,FD}=file:open(File,[write]),
try wr(FD,"~p.~n",Term),File
after file:close(FD)
end.
flat(L) -> wr("~s~n",lists:flatten(L)).
long(X) -> wr(X).
e(N,T) when is_list(T) -> lists:nth(N,T);
e(N,T) when is_tuple(T) -> element(N,T).
kill(P) -> exit(pid(P),kill).
pi(P) -> process_info(pid(P)).
pi(P,Item) -> process_info(pid(P),Item).
os(Cmd) ->
lists:foreach(fun(X)->wr("~s~n",X)end,string:tokens(os:cmd(Cmd),"\n")).
wr(E) -> wr("~p.~n",E).
wr(F,E) -> wr(user,F,E).
wr(FD,F,E) -> io:fwrite(FD,F,[E]).
callstack(P) ->
[string:strip(e(2,string:tokens(L,"(+)"))) || L<- bt(P), $0 =:= hd(L)].
bt(P) ->
string:tokens(binary_to_list(e(2,process_info(pid(P),backtrace))),"\n").
pid(Pid) when is_list(Pid) -> list_to_pid(Pid);
pid(Pid) when is_pid(Pid) -> Pid;
pid(Atom) when is_atom(Atom) -> whereis(Atom);
pid({0,I2,I3}) when is_integer(I2) -> c:pid(0,I2,I3);
pid(I2) when is_integer(I2) -> pid({0,I2,0}).
ineti() ->
lists:foreach(fun ineti/1,ports()).
ineti(P) ->
{_Fam,Type} = proplists:get_value(type,P),
[Status|_] = proplists:get_value(status,P),
{LIP,LPort} = proplists:get_value(local,P),
Sent = proplists:get_value(sent,P),
Recvd = proplists:get_value(received,P),
{RIP,RPort} =
case proplists:get_value(remote,P) of
enotconn -> {"*","*"};
{Rip,Rp} -> {inet_parse:ntoa(Rip),integer_to_list(Rp)}
end,
io:fwrite("~14s:~-5w ~14s:~-5s ~w ~w ~w ~w~n",
[inet_parse:ntoa(LIP),LPort,RIP,RPort,Type,Status,Sent,Recvd]).
ports() ->
[port_info(P)++PI ||
{P,PI}<-[{P,erlang:port_info(P)}||P<-erlang:ports()],
lists:sublist(proplists:get_value(name,PI),4,100)=="_inet"].
port_info(P) ->
{ok,Type} = prim_inet:gettype(P),
{ok,Status} = prim_inet:getstatus(P),
{ok,[{_,Sent}]} = prim_inet:getstat(P,[send_oct]),
{ok,[{_,Recvd}]} = prim_inet:getstat(P,[recv_oct]),
{ok,Local} = prim_inet:sockname(P),
Remote = case prim_inet:peername(P) of
{ok,R} -> R;
{error,R} -> R
end,
[{type,Type},{status,Status},
{sent,Sent},{received,Recvd},
{local,Local},{remote,Remote}].