4

We're hosting the script in out app. On exceptions/crashes, we'd like to see the line number in the stacktrace.

I can't find if there is a setting to include debug info when setting up the CSScript compiler?

Macke
  • 24,812
  • 7
  • 82
  • 118

1 Answers1

2

I believe you mean CS-Script (if not please correct me). I am not sure how you are calling it but I did find this command line documentation(it seems that the position in their help file is not reflected in their URL, you need to navigate to Overview -> Command-line interface). With .net the line number is included in the stack trace if you have a corresponding .pdb file and the line number will also be correct if there is no optimization done at compile time (this should not be a problem for CS-Script). In the documentation for cscs.exe there is a switch /dbg or /d. When you include this switch the corresponding .pdb file will be included with your .exe (or with the .dll if building a library). Once you have both files line numbers will now be available in the stack trace of any given exception that hits an operation in that assembly.

/dbg or /d

Forces compiler to include debug information.

Assume we have a file called Test.cs with some test code:

cscs.exe /e /dbg Test.cs

This will output 2 files:

  • Test.exe
  • Test.pdb

Here is the sample content of Test.cs, when you execute it you will see the line numbers.

using System;
namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new Exception("OH NO");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }
    }
}

For Executing inline

This should also be possible with the DebugBuild flag in the EvaluatorConfig section set to true. In my example below you will get everything expected BUt when using LoadCode it uses a temporary file name so the file name looks funny although the line number is correct. There are also LoadXXX commands for loading one or more files which will give a prettier stack trace as the file name is now known.

class Program
{
    static void Main(string[] args)
    {
        CSScript.EvaluatorConfig.DebugBuild = true;
        CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;

        Console.WriteLine("Debug on = " + CSScript.Evaluator.DebugBuild);

        dynamic script = CSScript.Evaluator
                     .LoadCode(@"using System;
                                 public class Script
                                 {
                                     public int Sum(int a, int b)
                                     {
                                         try{
                                         throw new Exception();}
                                         catch(Exception ex){
                                           Console.WriteLine(ex.StackTrace);
                                         }
                                         return a+b;
                                     }
                                 }");
        int result = script.Sum(1, 2);
        Console.WriteLine(result);
        Console.ReadLine();
    }
}

I did test this all to ensure that it does work. If you have problems just let me know.

Community
  • 1
  • 1
Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    I'm assuming they're not compiling the C# but running it as a script in their app using the scripting library. Unless there's methods on the CSScript object that enable debugging, it's unlikely that dynamically emitted assemblies would have line numbers (as you say, that functionality is pdb-based). – Zastai Mar 29 '16 at 17:53
  • @Zastai - I had not thought about that, thank you! I updated my answer, there is a flag to enable this (tested it just now with the code above) and seems to work. Either way you approach it a .net assembly will be created (which can be temporary) so the opportunity to create a .pdb should also be there (as long as supported by the API which this one done). – Igor Mar 29 '16 at 18:11
  • We're running it in our app. This looks ok, so bounty given. I missed that there was a new version with better settings for Debug, so thanks for that. However, I couldn't upgrade to the latest setting and test it out due to .. lots of errors. – Macke Apr 05 '16 at 06:47
  • 1
    It worked. I had to reference the Microsoft.CSharp assembly into CSScript evaluator, but once that was up it works nicely. Thanks again! :) – Macke Apr 05 '16 at 07:44