如何在 OSX 上的 edge.js 中的 C# 堆栈跟踪 运行 中获取文件名和行号

How do I get filenames and line numbers in c# stack traces running in edge.js on OSX

这特别是关于在运行在 edge.js 下使用 c# 时获取堆栈跟踪中的行号。

鉴于此 C# 源文件 (test.cs)

using System;
using System.Threading.Tasks;

namespace test {
    public class Startup {
        public async Task<object> Invoke(dynamic input) {
            try {
                throw new Exception("some error");
            } catch(Exception e) {
                Console.WriteLine(e);
            }
            return null;
       }
    }
}

我正在使用以下命令构建 .dll(和 .dll.mdb):

mcs -debug -target:library -out:test.dll test.cs

和运行这个edge.js脚本:

var edge = require('edge');

var myfunc = edge.func({
    assemblyFile: __dirname + '/test.dll'
});

myfunc(true, function(err, result) { });

输出中的堆栈跟踪没有文件名或行号:

System.Exception: some error
  at test.Startup+<Invoke>c__async0.MoveNext () [0x00000] in <filename unknown>:0

有没有办法在堆栈跟踪中获取文件名和行号而不是 :0?

在命令行中,mono 必须 运行 使用 --debug 参数才能获取行号。如果是这种情况,那么这个问题可能会归结为 "How do i pass arguments to CLR from edge.js?"

版本:node.js:v0.10.39,mcs/mono:4.0.4.0,edge.js:4.0.0

[编辑] 我发现您可以通过设置环境变量 MONO_ENV_OPTIONS 将命令行参数从边缘获取到单声道。不幸的是,这不适用于 --debug.

研究了一下,edge似乎没有提供在嵌入式mono中激活调试的方法,你需要自己修改和构建edge来完成它。这是一个快速的过程,但是,一旦你弄明白了:

1. 通常为您的项目安装 edge:

npm install edge

我确实按照 building on OSX 的步骤操作,但这不是必需的。

2. 更改 node_modules/edge/src/mono/monoembedding.cpp 以包含 mono-debug.h 并在 MonoEmbedding::Initialize 中调用 mono_debug_init。该文件应如下所示:

#include <dlfcn.h>
#include <limits.h>
#include <libgen.h>
#include "edge.h"

#include "mono/metadata/mono-debug.h" //This is new
#include "mono/metadata/assembly.h"
#include "mono/metadata/mono-config.h"
#include "mono/jit/jit.h"


MonoAssembly* MonoEmbedding::assembly = NULL;

void MonoEmbedding::Initialize()
{
    ...

    //This is new. Must be called before 'mono_jit_init'
    mono_debug_init(MONO_DEBUG_FORMAT_MONO);
    mono_config_parse (NULL);
    mono_jit_init (fullPath);
    ...
}

3. 来自 node_modules/edge/,构建边使用:

node-gyp configure build

您可能会收到类似于以下内容的错误:

Error: Cannot find module 'nan'

在那种情况下 运行 npm install --save nan,应该可以解决它。

4.运行你再测一次。现在的输出是:

System.Exception: some error   
    at test.Startup+<Invoke>c__async0.MoveNext () 
   [0x00019] in /Developer/tests/so/test.cs:8