I have some Ruby and Java background and I'm accustomed to having exact numbers of lines in the error logs.
So, if there is an error in the compiled code, I will see the number of line which caused the exception in the console output.
Like in this Ruby example:
my_ruby_code.rb:13:in `/': divided by 0 (ZeroDivisionError)
from my_ruby_code.rb:13
It's simple and fast - I just go to the line number 13 and fix the error.
On the contrary, Erlang just says something like:
** 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
There are no line numbers to look at.
And if I have two lines like
X = Param1,
Y = Param2,
in 'my_fun', how can understand in which line the problem lies?
Additionally, I have tried to switch to Emacs+Elang-mode from Vim, but the only bonus I've got so far is the ability to cycle through compilation errors inside Emacs (C-k `).
So, the process of writing code and seeking for simple logical errors like 'no match of right hand side' seems to be a bit cumbersome.
I have tried to add a lot of "io:format" lines in the code, but it is additional work which takes time.
I have also tried to use distel , but it requires 10 steps to just open a debugger once.
Questions:
- What is the most straight and simple way to debug Erlang code?
- Does Emacs' erlang-mode has something superior in terms of Erlang development comparing to Vim?
- What development 'write-compile-debug' cycle do you prefer? Do you leave Emacs to compile and run the code in the terminal? How do you search for errors in your Erlang code?
4条答案
按热度按时间relj7zay1#
Debugging Erlang code can be tricky at times, especially dealing with
badmatch
errors. In general, two good guidelines to keep are:function_clause
errors etc which are way more informative)That being said, using the debuggers are usually required to quickly get to the bottom of errors. I recommend to use the command line debugger,
dbg
, instead of the graphical one,debugger
(it's way faster when you know how to use it, and you don't have to context switch from the Erlang shell to a GUI).Given the sample expression you provided, the case is often that you have more than just variables being assigned to other variables (which is absolutely unnecessary in Erlang):
Debugging a
badmatch
error here is aided by using the command line debugger:Look for
{matched,_,1}
in the return value... if this would have been0
instead of1
(or more) that would have meant that no functions matched the pattern. Full documentation for thedbg
module can be found here .Given that both
something/1
andother:do/1
always returns ok, the following could happen:Here we can see the whole call procedure, and what return values were given. If we call it with something we know will fail:
Here we can see that we got a
badmatch
exception,something/1
was called, but neverother:do/1
so we can deduce that the badmatch happened before that call.Getting proficient with the command line debugger will save you a lot of time, whether you debug simple (but tricky!)
badmatch
errors or something much more complex.lyfkaqu12#
您可以使用Erlang debugger逐步执行程式码,并查看哪一行失败。
从
erl
启动调试器,方法如下:然后,您可以使用UI或使用控制台(带有ii:
再次在UI或控制台中添加断点:
同样,在Erlang R15中,我们最终会在堆栈跟踪中得到行号!
iq0todco3#
如果你用一个最新的erlang安装替换你的erlang安装,你会有行号,它们是从版本15开始添加的。
如果您的操作系统上还没有新版本,您可以从源代码构建或尝试从以下位置获取打包版本:http://www.erlang-solutions.com/section/132/download-erlang-otp
rpppsulh4#
您可以在文件的编译时使用“debug_info”和“debugger”
有关如何在Erlang中调试的更多详细信息,您可以通过视频-https://vimeo.com/32724400的链接进行访问