'How to debug an Erlang program in VS Code
I'm learning Erlang now. As far as I know, there's a tool called rebar3 that can generate a project boilerplate. Ok, so after installing it, I generate an empty project like this:
$ rebar3 new umbrella myproj
Sweet. Now I open VS Code with the Erlang extension installed. I add a launch.json file as it is said in the documentation:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch erlang",
"type": "erlang",
"request": "launch",
"cwd": "${workspaceRoot}",
"preLaunchTask": "rebar3 compile"
}
]
}
Then, I add another tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "rebar3 compile",
"type": "shell",
"command": "rebar3 compile",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$erlang"
}
]
}
Then, I set a breakpoint, here in the myproj_app.erl file:
%%%-------------------------------------------------------------------
%% @doc myproj public API
%% @end
%%%-------------------------------------------------------------------
-module(myproj_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
io:format("Hello, world!~n"), %<-- BREAKPOINT
myproj_sup:start_link().
stop(_State) ->
io:format("Hello, world!~n"),
ok.
%% internal functions
I press F5, the debugger starts but it never stops. Here's the output:
> Executing task: rebar3 compile <
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling myproj
Terminal will be reused by tasks, press any key to close it.
Compiling arguments file "/tmp/bp_2863283.erl"
Compile result: sucess
Module bp_2863283 loaded
If I add the line:
"arguments": "-config dev -s sample"
To the launch.json file, it gives me an error and doesn't start. And I'm pretty sure those aren't the arguments I have to pass to whatever is in charge of launching my program. Here is the output:
{
"
c
o
u
l
d
n
o
t
s
t
a
r
t
kernel pid",application_controller,"error in config file \"./dev.config\" (none): configuration file not found"}
c
o
u
l
d
n
o
t
s
t
a
r
t
k
e
r
n
e
l
p
id (application_controller) (error in config file "./dev.config" (none): configuration file not found)
Crash dump is being written to: erl_crash.dump...
d
o
n
e
erl exit code:1
erl exit with code 1
How do I configure VS Code to debug? What is lacking?
Solution 1:[1]
I am new to Erlang and hit the same issue... Evidently, starting a debug session does not start your program, but only start elang shell with debug flags, where you can than start any module:function(args) you are interesting to debug. Note that the debug console window has ">" prompt where you can invoke it. So in your specific example, I'd be using "myproj_app:start(1, 2)." where the args does not really matter as they are not used. Once entered
Solution 2:[2]
There is a built in debugger in OTP. Simply call debugger:start() wherever you want and once the app hits the debugger:start/0 the debugger window will pop up, allowing you to interpret specific modules and put breakpoint etc. For more details read the official docs. Also, observer (observer:start()) can also help you some times while debugging, so have both of them in mind.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2825562 |
| Solution 2 | markomin |
