[erlang-questions] Displaying the line number when error occured

Dmitrii 'Mamut' Dimandt dmitriid@REDACTED
Fri May 18 16:34:19 CEST 2007


June Kim wrote:
> Suppose foo.erl is:
>
> -module(foo).
> -export([bar/1]).
>
> bar(X)->
>     1+2+3,
>     5=X.
>
> ==============
>
> 8> foo:bar(5).
>
> =ERROR REPORT==== 18-May-2007::23:08:44 ===
> Error in process <0.61.0> with exit value:
> {{badmatch,5},[{foo,bar,1},{shell,exprs,6},{shell,eval_loop,3}]}
>
> ** exited: {{badmatch,5},
>             [{foo,bar,1},{shell,exprs,6},{shell,eval_loop,3}]} **
>
> The report isn't very helpful. Is there any way I can see the line
> number of the source code that the error occured?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>   
A while back somone posted this patch for shell.erl:

Index: shell.erl
===================================================================
RCS file: /hipe/otp/lib/stdlib/src/shell.erl,v
retrieving revision 1.8
diff -u -r1.8 shell.erl
--- shell.erl    24 Jan 2006 11:46:11 -0000    1.8
+++ shell.erl    11 Apr 2006 20:52:30 -0000
@@ -248,10 +248,77 @@
     {'EXIT', Pid, Res} ->
         {Res, Eval};
     {'EXIT', Eval, Reason} ->
-        io:fwrite("** exited: ~P **\n", [Reason, ?LINEMAX]),
+        report_exit(Reason),
         get_command1(Pid, start_eval(Bs, RT, Ds), Bs, RT, Ds)
     end.
 
+report_exit(Reason) ->
+    {Term,Trace} = analyze_exit(Reason),
+    io:fwrite("** exited: ~P **\n~s", [Term, ?LINEMAX, Trace]).
+
+analyze_exit({Term, Stack}=Reason) when is_list(Stack) ->
+    case is_stacktrace(Stack) of
+    true ->
+        {Term, format_stacktrace(Stack)};
+    false ->
+        {Reason, ""}
+    end;
+analyze_exit(Reason) ->
+    {Reason, ""}.
+
+is_stacktrace([]) ->
+    true;
+is_stacktrace([{M,F,A}|Fs]) when is_atom(M), is_atom(F), is_integer(A) ->
+    is_stacktrace(Fs);
+is_stacktrace([{M,F,As}|Fs]) when is_atom(M), is_atom(F), is_list(As) ->
+    is_stacktrace(Fs);
+is_stacktrace(_) ->
+    false.
+
+format_stacktrace(Stack) ->
+    format_stacktrace(Stack,"in function","in call from").
+
+format_stacktrace([{M,F,A}|Fs],Pre,Pre1) when is_integer(A) ->
+    [io_lib:fwrite("  ~s ~w:~w/~w\n", [Pre,M,F,A])
+     | format_stacktrace(Fs,Pre1,Pre1)];
+format_stacktrace([{M,F,As}|Fs],Pre,Pre1) when is_list(As) ->
+    A = length(As),
+    C = case is_op(M,F,A) of
+        true when A == 1 ->
+        [A1] = As,
+        io_lib:fwrite("~s ~s", [F,format_arg(A1)]);
+        true when A == 2 ->
+        [A1, A2] = As,
+        io_lib:fwrite("~s ~s ~s",
+                  [format_arg(A1),F,format_arg(A2)]);
+        false ->
+        io_lib:fwrite("~w(~s)", [F,format_arglist(As)])
+    end,
+    [io_lib:fwrite("  ~s ~w:~w/~w\n    called as ~s\n",
+           [Pre,M,F,A,C])
+     | format_stacktrace(Fs,Pre1,Pre1)];
+format_stacktrace([],_Pre,_Pre1) ->
+    "".
+
+format_arg(A) ->
+    io_lib:fwrite("~P",[A,?LINEMAX]).
+
+format_arglist([A]) ->
+    format_arg(A);
+format_arglist([A|As]) ->
+    [io_lib:fwrite("~P,",[A,?LINEMAX]) | format_arglist(As)];
+format_arglist([]) ->
+    "".
+
+is_op(erlang, F, A) ->
+    erl_internal:arith_op(F, A)
+    orelse erl_internal:bool_op(F, A)
+    orelse erl_internal:comp_op(F, A)
+    orelse erl_internal:list_op(F, A)
+    orelse erl_internal:send_op(F, A);
+is_op(_M, _F, _A) ->
+    false.
+
 prompt(N) ->
     case is_alive() of
     true -> {format,"(~s)~w> ",[node(),N]};
@@ -407,7 +474,7 @@
         Ev ! {shell_rep,self(),ok},
         shell_rep(Ev, Bs0, RT, Ds);
     {'EXIT',Ev,Reason} ->            % It has exited unnaturally
-        io:fwrite("** exited: ~P **\n", [Reason,?LINEMAX]),
+        report_exit(Reason),
         {{'EXIT',Reason},start_eval(Bs0, RT, Ds0), Bs0, Ds0};
     {'EXIT',_Id,interrupt} ->        % Someone interrupted us
         exit(Ev, kill),




I don't think it prints line numers, but it's supposed to be more readable
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070518/7ac7ce95/attachment.htm>


More information about the erlang-questions mailing list