Kir*_*ill 29 debugging vim emacs erlang
我有一些Ruby和Java背景,我习惯于在错误日志中有确切的行数.
因此,如果编译代码中存在错误,我将在控制台输出中看到导致异常的行数.
就像在这个Ruby示例中一样:
my_ruby_code.rb:13:in `/': divided by 0 (ZeroDivisionError)
from my_ruby_code.rb:13
Run Code Online (Sandbox Code Playgroud)
它简单而快速 - 我只需转到第13行并修复错误.
相反,Erlang只是说:
** exception error: no match of right hand side value [xxxx]
in function my_module:my_fun/1
in call from my_module:other_fun/2
Run Code Online (Sandbox Code Playgroud)
没有行号可供查看.
如果我有两行像
X = Param1,
Y = Param2,
Run Code Online (Sandbox Code Playgroud)
在'my_fun'中,怎么能理解问题出在哪一行?
另外,我试图从Vim切换到Emacs + Elang模式,但到目前为止我唯一的奖励就是能够在Emacs(Ck`)中循环编译错误.
因此,编写代码和寻找简单的逻辑错误(如"右手边不匹配")的过程似乎有点麻烦.
我试图在代码中添加许多"io:format"行,但这是需要时间的额外工作.
我也尝试过使用distel,但只需要10步就可以打开调试器一次.
问题:
Ada*_*erg 35
调试Erlang代码有时会很棘手,尤其是处理badmatch
错误.一般来说,要保留两个好的指导方针是:
function_clause
错误等的好处,这将提供更多信息)话虽这么说,使用调试器通常需要快速找到错误的底部.我建议使用命令行调试器,dbg
而不是图形调试器debugger
(当你知道如何使用它时,它会更快,而你不必从Erlang shell上传到GUI).
给定您提供的示例表达式,通常情况下您不仅要将变量分配给其他变量(在Erlang中绝对不需要):
run(X, Y) ->
X = something(whatever),
Y = other:thing(more_data),
Run Code Online (Sandbox Code Playgroud)
badmatch
使用命令行调试器可以在此处调试错误:
1> dbg:tracer(). % Start the CLI debugger
{ok,<0.55.0>}
2> dbg:p(all, c). % Trace all processes, only calls
{ok,[{matched,nonode@nohost,29}]}
3> dbg:tpl(my_module, something, x). % tpl = trace local functions as well
{ok,[{matched,nonode@nohost,1},{saved,x}]}
4> dbg:tp(other, do, x). % tp = trace exported functions
{ok,[{matched,nonode@nohost,1},{saved,x}]}
5> dbg:tp(my_module, run, x). % x means print exceptions
{ok,[{matched,nonode@nohost,1},{saved,x}]} % (and normal return values)
Run Code Online (Sandbox Code Playgroud)
{matched,_,1}
在返回值中查找...如果这将0
代替1
(或更多)意味着没有函数匹配模式.dbg
可以在此处找到该模块的完整文档.
鉴于这两个something/1
和other:do/1
总是返回OK,下面会发生:
6> my_module:run(ok, ok).
(<0.72.0>) call my_module:run(ok,ok)
(<0.72.0>) call my_module:something(whatever)
(<0.72.0>) returned from my_module:something/1 -> ok
(<0.72.0>) call other:thing(more_data)
(<0.72.0>) returned from other:thing/1 -> ok
(<0.72.0>) returned from my_module:run/2 -> ok
ok
Run Code Online (Sandbox Code Playgroud)
在这里,我们可以看到整个调用过程,以及给出的返回值.如果我们用一些我们知道会失败的东西来称呼它:
7> my_module:run(error, error).
** exception error: no match of right hand side value ok
(<0.72.0>) call my_module:run(error,error)
(<0.72.0>) call my_module:something(whatever)
(<0.72.0>) returned from my_module:something/1 -> ok
(<0.72.0>) exception_from {my_module,run,2} {error,{badmatch,ok}}
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到我们得到了一个badmatch
异常,something/1
被调用,但从来没有other:do/1
这样我们可以推断出坏匹配发生在那个调用之前.
熟练掌握命令行调试器可以节省大量时间,调试简单(但棘手!)badmatch
错误或更复杂的事情.
希望当Erlang R15在例外情况下出现行号时,这一切都会变得更容易!
Rob*_*rop 19
您可以使用Erlang调试器逐步执行代码并查看哪条线路出现故障.
从erl
,启动调试器:
debugger:start().
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用UI或使用带有ii的控制台选择要在解释模式下(调试所需)的模块:
ii(my_module).
Run Code Online (Sandbox Code Playgroud)
在UI或控制台中再次添加断点:
ib(my_module, my_func, func_arity).
Run Code Online (Sandbox Code Playgroud)
另外,在Erlang R15中,我们最终会有堆栈跟踪中的行号!
如果您将 erlang 安装替换为最近的安装,您将拥有行号,它们是从版本 15 开始添加的。
如果您的操作系统上还没有新版本,您可以从源代码构建或尝试在此处获取打包版本:http : //www.erlang-solutions.com/section/132/download-erlang-otp
归档时间: |
|
查看次数: |
13532 次 |
最近记录: |