From bc544c51d84bfd61f3dafc5048e86cea02641b43 Mon Sep 17 00:00:00 2001
From: Peter Andersson <peppe@erlang.org>
Date: Wed, 20 Jul 2016 14:12:24 +0200
Subject: Make sure exit in non-interpreted code doesn't crash the debugger

---
 lib/debugger/src/dbg_iserver.erl  | 40 +++++++++++++++++++++------------------
 lib/debugger/src/dbg_wx_trace.erl | 13 ++++++++-----
 lib/debugger/src/dbg_wx_view.erl  | 12 ++++++++----
 3 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/lib/debugger/src/dbg_iserver.erl b/lib/debugger/src/dbg_iserver.erl
index 3561454685..3e959e8e30 100644
--- a/lib/debugger/src/dbg_iserver.erl
+++ b/lib/debugger/src/dbg_iserver.erl
@@ -214,7 +214,6 @@ handle_call({new_process, Pid, Meta, Function}, _From, State) ->
 
 %% Code loading
 handle_call({load, Mod, Src, Bin}, _From, State) ->
-
     %% Create an ETS table for storing information about the module
     Db = State#state.db,
     ModDb = ets:new(Mod, [ordered_set, public]),
@@ -285,23 +284,28 @@ handle_call({contents, Mod, Pid}, _From, State) ->
     {reply, {ok, Bin}, State};
 handle_call({raw_contents, Mod, Pid}, _From, State) ->
     Db = State#state.db,
-    [{{Mod, refs}, ModDbs}] = ets:lookup(Db, {Mod, refs}),
-    ModDb = if
-		Pid =:= any -> hd(ModDbs);
-		true ->
-		    lists:foldl(fun(T, not_found) ->
-					[{T, Pids}] = ets:lookup(Db, T),
-					case lists:member(Pid, Pids) of
-					    true -> T;
-					    false -> not_found
-					end;
-				   (_T, T) -> T
-				end,
-				not_found,
-				ModDbs)
-	    end,
-    [{mod_raw, Bin}] = ets:lookup(ModDb, mod_raw),
-    {reply, {ok, Bin}, State};
+    case ets:lookup(Db, {Mod, refs}) of
+	[{{Mod, refs}, ModDbs}] ->
+	    ModDb = 
+		if
+		    Pid =:= any -> hd(ModDbs);
+		    true ->
+			lists:foldl(fun(T, not_found) ->
+					    [{T, Pids}] = ets:lookup(Db, T),
+					    case lists:member(Pid, Pids) of
+						true -> T;
+						false -> not_found
+					    end;
+				       (_T, T) -> T
+				    end,
+				    not_found,
+				    ModDbs)
+		end,
+	    [{mod_raw, Bin}] = ets:lookup(ModDb, mod_raw),
+	    {reply, {ok, Bin}, State};
+	[] ->					% code not interpreted
+	    {reply, not_found, State}
+    end;
 handle_call({is_interpreted, Mod, Name, Arity}, _From, State) ->
     Db = State#state.db,
     Reply = case ets:lookup(Db, {Mod, refs}) of
diff --git a/lib/debugger/src/dbg_wx_trace.erl b/lib/debugger/src/dbg_wx_trace.erl
index 6af19af33b..29c8e8cefb 100644
--- a/lib/debugger/src/dbg_wx_trace.erl
+++ b/lib/debugger/src/dbg_wx_trace.erl
@@ -818,11 +818,14 @@ gui_show_module(Win, Mod, Line, _Cm, Pid, How) ->
 
 gui_load_module(Win, Mod, _Pid) ->
     dbg_wx_trace_win:display(Win,{text, "Loading module..."}),
-    %% Contents = int:contents(Mod, Pid),
-    {ok, Contents} = dbg_iserver:call({raw_contents, Mod, any}),
-    Win2 = dbg_wx_trace_win:show_code(Win, Mod, Contents),
-    dbg_wx_trace_win:display(Win,{text, ""}),
-    Win2.
+    case dbg_iserver:call({raw_contents, Mod, any}) of
+	{ok, Contents} ->
+	    Win2 = dbg_wx_trace_win:show_code(Win, Mod, Contents),
+	    dbg_wx_trace_win:display(Win,{text, ""}),
+	    Win2;
+	not_found ->
+	    dbg_wx_trace_win:show_no_code(Win)
+    end.
 
 gui_update_bindings(Win,Meta) ->
     Bs = int:meta(Meta, bindings, nostack),
diff --git a/lib/debugger/src/dbg_wx_view.erl b/lib/debugger/src/dbg_wx_view.erl
index 91fc0d08cb..86d009238f 100644
--- a/lib/debugger/src/dbg_wx_view.erl
+++ b/lib/debugger/src/dbg_wx_view.erl
@@ -263,7 +263,11 @@ shortcut(_) -> false.
 
 gui_load_module(Win, Mod) ->
     dbg_wx_trace_win:display(Win,{text, "Loading module..."}),
-    {ok, Contents} = dbg_iserver:call({raw_contents, Mod, any}),
-    Win2 = dbg_wx_trace_win:show_code(Win, Mod, Contents),
-    dbg_wx_trace_win:display(Win,{text, ""}),
-    Win2.
+    case dbg_iserver:call({raw_contents, Mod, any}) of
+	{ok, Contents} ->
+	    Win2 = dbg_wx_trace_win:show_code(Win, Mod, Contents),
+	    dbg_wx_trace_win:display(Win,{text, ""}),
+	    Win2;
+	not_found ->
+	    dbg_wx_trace_win:show_no_code(Win)
+    end.
-- 
cgit v1.2.3