如何在VS代码中调试Erlang程序

wrrgggsh  于 2022-12-16  发布在  Erlang
关注(0)|答案(2)|浏览(198)

我现在正在学习Erlang,据我所知有一个工具叫rebar3,可以生成一个工程样板,好的,那么安装之后,我生成了一个空的工程,是这样的:

$ rebar3 new umbrella myproj

现在我打开VS代码,安装了Erlang extension,我添加了一个launch.json文件,正如文档中所说的:

{
  // 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"
    }
  ]
}

然后,添加另一个tasks.json文件:

{
  // 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"
    }
  ]
}

然后,我在myproj_app.erl文件中设置一个断点:

%%%-------------------------------------------------------------------
%% @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

我按F5键,调试器启动,但不会停止。

> 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

如果我加上一行:

"arguments": "-config dev -s sample"

对于launch.json文件,它会给我一个错误,并且不会启动。而且我很确定这些参数不是我必须传递给负责启动我的程序的任何东西的参数。

{
"
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

我如何配置VS代码进行调试?缺少什么?

klr1opcd

klr1opcd1#

我是Erlang新手,遇到了同样的问题......显然,启动调试会话并不会启动您的程序,而只是启动带有调试标志的elangshell,在这里您可以启动任何您感兴趣调试的module:function(args)。注意,调试控制台窗口有“〉”提示符,您可以在这里调用它。因此,在您的特定示例中,我将使用“myproj_app:start(1,2)”。其中,args并不重要,因为它们并不使用。一旦输入

z18hc3ub

z18hc3ub2#

OTP中有一个内置的调试器。只要在任何地方调用debugger:start(),一旦应用程序点击了debugger:start/0,调试器窗口就会弹出,允许您解释特定的模块并放置断点等。更多细节请阅读官方文档。此外,观察者(观察者:start())有时也可以帮助您调试,所以请记住这两个窗口。

相关问题