4
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 1996-2011. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
21
21
%% Utilities to use from shell.
23
%% Avoid warning for local function error/2 clashing with autoimported BIF.
24
-compile({no_auto_import,[error/2]}).
23
25
-export([help/0,lc/1,c/1,c/2,nc/1,nc/2, nl/1,l/1,i/0,i/1,ni/0,
25
27
lc_batch/0, lc_batch/1,
31
33
-export([display_info/1]).
32
34
-export([appcall/4]).
34
-import(lists, [reverse/1,flatten/1,sublist/3,sort/1,keysearch/3,keysort/2,
36
-import(lists, [reverse/1,flatten/1,sublist/3,sort/1,keysort/2,
35
37
concat/1,max/1,min/1,foreach/2,foldl/3,flatmap/2]).
36
38
-import(io, [format/1, format/2]).
40
%%-----------------------------------------------------------------------
39
format("bt(Pid) -- stack backtrace for a process\n"
40
"c(File) -- compile and load code in <File>\n"
41
"cd(Dir) -- change working directory\n"
42
"flush() -- flush any messages sent to the shell\n"
43
"help() -- help info\n"
44
"i() -- information about the system\n"
45
"ni() -- information about the networked system\n"
46
"i(X,Y,Z) -- information about pid <X,Y,Z>\n"
47
"l(Module) -- load or reload module\n"
48
"lc([File]) -- compile a list of Erlang modules\n"
49
"ls() -- list files in the current directory\n"
50
"ls(Dir) -- list files in directory <Dir>\n"
51
"m() -- which modules are loaded\n"
52
"m(Mod) -- information about module <Mod>\n"
53
"memory() -- memory allocation information\n"
54
"memory(T) -- memory allocation information of type <T>\n"
55
"nc(File) -- compile and load code in <File> on all nodes\n"
56
"nl(Module) -- load module on all nodes\n"
57
"pid(X,Y,Z) -- convert X,Y,Z to a Pid\n"
58
"pwd() -- print working directory\n"
59
"q() -- quit - shorthand for init:stop()\n"
60
"regs() -- information about registered processes\n"
61
"nregs() -- information about all registered processes\n"
62
"xm(M) -- cross reference check a module\n"
63
"y(File) -- generate a Yecc parser\n").
45
io:put_chars(<<"bt(Pid) -- stack backtrace for a process\n"
46
"c(File) -- compile and load code in <File>\n"
47
"cd(Dir) -- change working directory\n"
48
"flush() -- flush any messages sent to the shell\n"
49
"help() -- help info\n"
50
"i() -- information about the system\n"
51
"ni() -- information about the networked system\n"
52
"i(X,Y,Z) -- information about pid <X,Y,Z>\n"
53
"l(Module) -- load or reload module\n"
54
"lc([File]) -- compile a list of Erlang modules\n"
55
"ls() -- list files in the current directory\n"
56
"ls(Dir) -- list files in directory <Dir>\n"
57
"m() -- which modules are loaded\n"
58
"m(Mod) -- information about module <Mod>\n"
59
"memory() -- memory allocation information\n"
60
"memory(T) -- memory allocation information of type <T>\n"
61
"nc(File) -- compile and load code in <File> on all nodes\n"
62
"nl(Module) -- load module on all nodes\n"
63
"pid(X,Y,Z) -- convert X,Y,Z to a Pid\n"
64
"pwd() -- print working directory\n"
65
"q() -- quit - shorthand for init:stop()\n"
66
"regs() -- information about registered processes\n"
67
"nregs() -- information about all registered processes\n"
68
"xm(M) -- cross reference check a module\n"
69
"y(File) -- generate a Yecc parser\n">>).
66
72
%% Compile a file/module.
74
-spec c(file:name()) -> {'ok', module()} | 'error'.
68
76
c(File) -> c(File, []).
78
-spec c(file:name(), [compile:option()]) -> {'ok', module()} | 'error'.
70
80
c(File, Opts0) when is_list(Opts0) ->
71
81
Opts = [report_errors,report_warnings|Opts0],
72
82
case compile:file(File, Opts) of
118
130
%%% loaded from some other place than current directory.
119
131
%%% Now, loading from other than current directory is supposed to work.
120
132
%%% so this function does nothing special.
121
check_load({error, R}, _) -> {error, R};
122
check_load(_, X) -> {ok, X}.
133
check_load({error, _R} = Error, _) -> Error;
134
check_load(_, Mod) -> {ok, Mod}.
124
136
%% Compile a list of modules
125
137
%% enables the nice unix shell cmd
208
-spec nc(file:name()) -> {'ok', module()} | 'error'.
194
210
nc(File) -> nc(File, []).
212
-spec nc(file:name(), [compile:option()] | compile:option()) ->
213
{'ok', module} | 'error'.
196
215
nc(File, Opts0) when is_list(Opts0) ->
197
216
Opts = Opts0 ++ [report_errors, report_warnings],
198
217
case compile:file(File, Opts) of
200
Fname = concat([File, code:objfile_extension()]),
220
Obj = filename:basename(File, ".erl") ++ code:objfile_extension(),
221
Fname = filename:join(Dir, Obj),
201
222
case file:read_file(Fname) of
203
224
rpc:eval_everywhere(code,load_binary,[Mod,Fname,Bin]),
215
236
%% Reload module Mod from file of same name
237
-spec l(module()) -> code:load_ret().
219
241
code:load_file(Mod).
221
243
%% Network version of l/1
244
%%-spec nl(module()) ->
223
246
case code:get_object_code(Mod) of
224
247
{_Module, Bin, Fname} ->
225
rpc:eval_everywhere(code,load_binary,[Mod,Fname,Bin]);
248
rpc:eval_everywhere(code, load_binary, [Mod, Fname, Bin]);
230
255
i() -> i(processes()).
231
259
ni() -> i(all_procs()).
261
-spec i([pid()]) -> 'ok'.
234
264
i(Ps, length(Ps)).
266
-spec i([pid()], non_neg_integer()) -> 'ok'.
236
268
i(Ps, N) when N =< 100 ->
237
269
iformat("Pid", "Initial Call", "Heap", "Reds",
364
393
fetch(Key, Info) ->
365
case keysearch(Key, 1, Info) of
366
{value, {_, Val}} -> Val;
394
case lists:keyfind(Key, 1, Info) of
399
-spec pid(non_neg_integer(), non_neg_integer(), non_neg_integer()) -> pid().
371
402
list_to_pid("<" ++ integer_to_list(X) ++ "." ++
372
403
integer_to_list(Y) ++ "." ++
373
404
integer_to_list(Z) ++ ">").
375
i(X,Y,Z) -> pinfo(pid(X,Y,Z)).
406
-spec i(non_neg_integer(), non_neg_integer(), non_neg_integer()) ->
409
i(X, Y, Z) -> pinfo(pid(X, Y, Z)).
411
-spec q() -> no_return().
416
-spec bt(pid()) -> 'ok' | 'undefined'.
381
419
case catch erlang:process_display(Pid, backtrace) of
437
477
%% Short and nice form of module info
479
-spec m(module()) -> 'ok'.
441
482
L = M:module_info(),
442
{value,{exports,E}} = keysearch(exports, 1, L),
483
{exports,E} = lists:keyfind(exports, 1, L),
443
484
Time = get_compile_time(L),
444
485
COpts = get_compile_options(L),
445
486
format("Module ~w compiled: ",[M]), print_time(Time),
661
memory() -> erlang:memory().
732
-spec memory() -> [{atom(), non_neg_integer()}].
734
memory() -> erlang:memory().
736
-spec memory(atom()) -> non_neg_integer()
737
; ([atom()]) -> [{atom(), non_neg_integer()}].
662
739
memory(TypeSpec) -> erlang:memory(TypeSpec).
665
742
%% Cross Reference Check
744
%%-spec xm(module() | file:filename()) -> xref:m/1 return
669
746
appcall(tools, xref, m, [M]).
751
%%-spec y(file:name()) -> yecc:file/2 return
675
752
y(File) -> y(File, []).
754
%%-spec y(file:name(), [yecc:option()]) -> yecc:file/2 return
678
appcall(parsetools, yecc, file, [File,Opts]).
756
appcall(parsetools, yecc, file, [File, Opts]).