1

This is specifically about getting line numbers in stack traces when running c# under edge.js.

Given this C# source file (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;
       }
    }
}

I'm building a .dll (and .dll.mdb) with the command:

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

And running with this edge.js script:

var edge = require('edge');

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

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

The stack trace in the output has no filename or line numbers:

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

Is there a way to get filenames and line numbers in the stack trace instead of <filename unknown>:0 ?

From the command line, mono must be run using the --debug argument in order to get line numbers. If that's the case here, then this question may boil down to "How do i pass arguments to CLR from edge.js?"

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

[Edit] I found you can get command line arguments into mono from edge by setting the environment variable MONO_ENV_OPTIONS. Unfortunately this doesn't work with --debug.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • Possible duplicate of [How can I include line numbers in a stack trace without a pdb?](http://stackoverflow.com/questions/1328836/how-can-i-include-line-numbers-in-a-stack-trace-without-a-pdb) –  Dec 11 '15 at 23:53
  • Not a duplicate. This is specifically about c# code running under edge.js. – Ted Bigham Dec 11 '15 at 23:54
  • Debugging information is in the .mdb file generated by mcs. When running from the command line, filenames and line numbers are present. This is *specifically* about running under edge.js. – Ted Bigham Dec 12 '15 at 00:27

1 Answers1

1

After researching this, seems like edge does not provide a way to activate debugging in the embedded mono, you need to modify and build edge yourself to accomplish it. It is a quick process, though, once you figure it out:

1. Install edge for your project normally with:

npm install edge

I did follow the steps for building on OSX, but it's not necessary.

2. Change node_modules/edge/src/mono/monoembedding.cpp to include mono-debug.h and call mono_debug_init within MonoEmbedding::Initialize. The file should look something like this:

#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. From node_modules/edge/, build edge using:

node-gyp configure build

You might get an error similar to:

Error: Cannot find module 'nan'

In that case run npm install --save nan, that should fix it.

4. Run your test again. Output is now:

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