erlang 用 IntelliJ IDEA 工具进行调试

Requirements

My system is a Windows 8.1 (6.3 build 9600).

Download IntelliJ IDEA from JetBrains (I use the Community Edition, currently ideaIC-14.0.3.exe).

My installed erlang is:

?
1
Erlang/OTP 17 [erts-6.2] [64-bit] [smp:8:8] [async-threads:10]

Start IDEA, and go the plugin page (click "Configure"). Install the very nice Erlang plugin (currently ver 0.5.9 (2014-11-18)).

Image

Install rebar somewhere on your system (clone the repository and run bootstrap.bat with the erlang binaries in your PATH).

Configure the IDE

In the IDE's settings window, go to "Other Settings / Erlang External Tools" and input the location of your rebar binaries.

Image

In "Build, Execution, Deployment/Erlan Compiler", check "Compile project with rebar".

Image

Create a new project, and select Erlang. Leave "Additional Libraries and Frameworks" empty and click Next.

Click on "Configure" and input the location of your Erlang SDK (exemple: "C:\Program Files\erl6.2").

If you did well, the SDK is recognized correctly.

Image

Debugging a simple file

Let's create a simple Erlang code and try to debug it.

Add a file, input some code, and click "Build / Make".

If your code is incorrect, the list of warnings and errors appears, and you can double click on the items to go directly to the source of error (like in any good IDE).

Image

If your code is correct, the project is compiled.

A simple default debug configuration is created. You can put a breakpoint in your code and debug it.

ImageImage

So far, so good.

Debugging standard OTP applications

Now let's try to debug something more serious, like an OTP application.

?
1
2
3
4
5
rebarcreate-app appid=testerlangide
==> testerlangide (create-app)
Writing src/testerlangide.app.src
Writing src/testerlangide_app.erl
Writing src/testerlangide_sup.erl

Let's create a gen_server which prints something every 5 seconds

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
 
-define(DELAY_BETWEEN_LOOKUPS, 5000).
 
-record(state, {timer}).
 
init([]) ->
   Timer = erlang:send_after(?DELAY_BETWEEN_LOOKUPS, self(), timer_ticked),
   {ok, #state{timer=Timer}}.
 
...
 
handle_info(timer_ticked, #state{timer=OldTimer}=State) ->
   erlang:cancel_timer(OldTimer),
   io:format("Tick!~n"),
   Timer = erlang:send_after(?DELAY_BETWEEN_LOOKUPS, self(), timer_ticked),
   {noreply, State#state{timer=Timer}};
 
...

I've tried debugging the application directly, by having the debugger directly start the application (application:start(testerlangide)), but it didn't work well:

  • When "Stop Erlang interpreter automatically after execution" is ticked, the Erlang node is stopped directly after the app is started, which is obviously not good.
  • When "Stop Erlang interpreter automatically after execution" is not ticked, the breakpoints in the gen_server do not work, because the node is considered by the IDE to be stopped. Thus you can't stop it, which is also not good.

What I ended up doing was the creation of a "proxy" file, that starts the app, and does nothing until the node is stopped. That way, the breakpoints in the gen_servers work, and the node can be stopped by clicking on the red square button in the IDE.

?
1
2
3
4
5
6
7
8
9
10
-export([debug/0]).
 
loop_sleep() ->
   timer:sleep(5000),
   loop_sleep().
 
debug() ->
   %% Start your dependencies here
   application:start(testerlangide),
   loop_sleep().

ImageImage

Debugging a remote node

Using Intellij on Windows works fine, but many Erlang libraries don't. In my opinion, Erlang is much more pleasant on UNIX systems. Still, that shouldn't prevent us from debugging.

Fortunately, the author of the plugin included a way to debug remote nodes. A local node will be spawned on the IDE's computer, connect to the target node, and fire up the debugger.

Image

If your remote node is using long names, please be sure that your intellij-erlang include this commit and this commit. Otherwise, your local node won't be started with a correct name and debugging will fail.

Image

You can even connect to an already running system, debug a few lines, and let it live again normally. That's perfect to debug hard live problems.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值