aboutsummaryrefslogtreecommitdiffstats
path: root/lib/debugger/src/dbg_ui_interpret.erl
blob: 079eaeb634753498ea96a2c184a41804a80cc309 (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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
-module(dbg_ui_interpret).

-include_lib("kernel/include/file.hrl").

%% External exports
-export([start/4]).

%% Internal exports
-export([init/6]).

-record(state, {gs,      % term() Graphics system id
		win,     % term() Interpret dialog window data
		monitor, % pid() Monitor pid
		mode     % local | global
	       }).

%%====================================================================
%% External exports
%%====================================================================

%%--------------------------------------------------------------------
%% start(GS, Pos, Dir, Mode)
%%   GS  = Graphics system id
%%   Dir = string()
%%   Pos = {X,Y}
%%   Mode = local | global
%%--------------------------------------------------------------------
start(GS, Pos, Dir, Mode) ->
    Title = "Interpret Dialog",
    case dbg_ui_winman:is_started(Title) of
	true -> ignore;
	false ->
	    spawn(?MODULE, init, [self(), GS, Pos, Title, Dir, Mode])
    end.

%%====================================================================
%% Internal exports
%%====================================================================

init(Monitor, GS, Pos, Title, Dir, Mode) ->
    Filter = filename:join(Dir, "*.erl"),
    Extra = fun(File) ->
		    case int:interpretable(File) of
			true ->
			    ModS = filename:basename(File, ".erl"),
			    Mod = list_to_atom(ModS),
			    case int:file(Mod) of
				File -> {true, tag};
				_ -> true % {error,not_loaded} | File2
			    end;
			_Error -> {true,disable}
		    end
	    end,
			
    %% Create interpret dialog window
    Win = dbg_ui_filedialog_win:create_win(GS, Title, Pos, multiselect,
					   Filter, Extra),
    Window = dbg_ui_filedialog_win:get_window(Win),
    dbg_ui_winman:insert(Title, Window),

    State = #state{gs=GS, win=Win, monitor=Monitor, mode=Mode},
    loop(State).


%%====================================================================
%% Main loop and message handling
%%====================================================================

loop(State) ->
    receive

	%% From the GUI
	GuiEvent when is_tuple(GuiEvent), element(1, GuiEvent)==gs ->
	    Cmd = dbg_ui_filedialog_win:handle_event(GuiEvent,
						    State#state.win),
	    State2 = gui_cmd(Cmd, State),
	    loop(State2);

	%% From the dbg_ui_winman process (Debugger window manager)
	{dbg_ui_winman, update_windows_menu, _Data} ->
	    loop(State);
	{dbg_ui_winman, destroy} ->
	    exit(normal)
    end.

gui_cmd(ignore, State) ->
    State;
gui_cmd({stopped, Dir}, State) ->	
    State#state.monitor ! {dbg_ui_interpret, Dir},
    exit(normal);
gui_cmd({win, Win}, State) ->
    State#state{win=Win};
gui_cmd({select, File}, State) ->
    Res = case State#state.mode of
	      local -> int:i(File);
	      global -> int:ni(File)
	  end,

    case Res of
	%% Interpretation succeeded, tag the file name
	{module, _Mod} ->
	    dbg_ui_filedialog_win:tag(State#state.win, File);

	%% Interpretation failed
	error ->
	    Error = format_error(int:interpretable(File)),
	    Msg = ["Error when interpreting:", File, Error],
	    Window = dbg_ui_filedialog_win:get_window(State#state.win),
	    tool_utils:notify(Window, Msg)
    end,
    State;
gui_cmd({multiselect, Dir, FileNames}, State) ->
    interpret_all(State, Dir, FileNames),
    State.

interpret_all(State, Dir, [File0|Files]) ->
    File = filename:join(Dir, File0),
    Res = case State#state.mode of
	      local -> int:i(File);
	      global -> int:ni(File)
	  end,
    case Res of
	{module, _Mod} ->
	    dbg_ui_filedialog_win:tag(State#state.win, File),
	    interpret_all(State, Dir, Files);
	error ->
	    Window = dbg_ui_filedialog_win:get_window(State#state.win),
	    Error = format_error(int:interpretable(File)),
	    Msg = ["Error when interpreting:", File, Error,
		   "OK to continue?"],
	    case tool_utils:confirm(Window, Msg) of
		ok -> interpret_all(State, Dir, Files);
		cancel -> true
	    end
    end;
interpret_all(_State, _Dir, []) ->
    true.

format_error({error,no_beam}) -> "No BEAM file";
format_error({error,no_debug_info}) -> "No debug_info in BEAM file";
format_error({error,badarg}) -> "File does not exist";
format_error({error,{app,App}}) ->
    "Cannot interpret "++atom_to_list(App)++" modules".