oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.57k stars 234 forks source link

Question about script debugging. #305

Closed mvainLive closed 1 year ago

mvainLive commented 1 year ago

Hi,

I have a compiled exe application that will load a script like this,

CSScript.Evaluator.LoadFile(scriptPath, _logger);

My script is a class that contains methods that can do some work. I pass in a ILogger instance as can be seen above.

The source code for the hosting application is not be available where the scripts will be created and tested.

What I would like to do is keep VSCode open with the script and when methods are called in the script VSCode can break at a break point and allow step through debugging.

Is this possible?

I do have the script running and doing what I want during my development process.

Is this possible?

This is the first time I have posted / asked a question on here so I hope my question is clear enough?

Martin

damian-666 commented 1 year ago

yes , but this is how we did it years ago, mabye ( ihope) there is a better way even with closed legacy code) . I will explain is how my lead programmer did it with a wpf script host , a separate pipe process to allow removal of the script -editor -inserted Break line..System.Diagnostics.Debugger.Break(), and using attached visual studio to step code in the script,

this is not too the point but sorry i dont have time to make it concise or fix all the typos. BUT it -might- contain what you may need to do. i also responded via email and it did not show here. im just really swamped with net6, way behind where i want to be now.

PROBLEM was once you hit the Break line in the script and area transferred to the debugger attached to its host, you can trace, inspect, but not edit and continue, so you cant remove it in the atttached debugger.. ( or couldn't) with an old AvalonEdit years ago . ( we do have the source to most of the script host but you won't need all of it to do this. if this sounds like what you want to do, read on. maybe edit and continue can remove the line now.. I don't know. the code was in a temp assembly and it would not allow it before with debugger attached. i didnt use Rosyln then and im starting to get it working, took out plugin namespaces, updating all to net6 but with come closed source wpf 4.8 pieces via windows compatibilty.

and im looking for a debugger that can step and inspect right in my script tool better without inserting/ removing a line Debug.Writelin( "xx" + xx.ToString() to the script , thast what it does now to inspect. But github has much better inspection controls, and debugging , actual step and inspect features are being used like rosyln pad, notepadd++ plugin,etc. so im open to new and better ways.. but I dont want to rewrite all my huge level editor.. BUT if you have tried and cant find a way , this might help:

anyways here WAS our problem.. if you put a Break line it will go to your debugger, we used vs2022 but vs code should be fine, that you can attach or launch the scripting host with.. ( with our without symbols) it will stop in your code (usually in a temp file)

when you press f9 in our script editor based on avalonia it places a this in the script this..

public static string DEBUGONCE_STRING = "if (Core.Game.Debugger.CanBreak) System.Diagnostics.Debugger.Break();";

The problem we got is, once you added the debug line , how do you stop hitting the BP? how do you erase that line of code? when you attached a debugger, you cant modify the temp assembly. so you cant remove the breakpoint and keep going.

so he launched a separate pipe apt, from the spawned wpf app, an exe process, just a little wpf exe.. what it doestis open a separate process and a connect named pipe to the ..to the script host . the little debugger.exe host is ont the buttom right of screen on top, it has a toggle button called Disable Breakpoints. so..s from the scripting host to a a named pipe that turned off /on the Core.Game.Debugger.CanBreak. it works...but

I dont like this. im updating old WPF legacy code to nett6.. i dont wanan go full avalonia but I would love to explore the rosyln pad, other pre-compilers, and such. more folding and complete avaloniaEdit feature like codecomplete, . I would rather step in and out an over, inspect, and not use attach vs 2022 as a debugger. if im doing alot of coding, or refactoring I dont expect to to it in my scripts i wll or change base classes. but even very limited scripting is now too hard...

Im not saying ths is still an issue , and stil updateing my WPF 4.8 exes and wpf controls and maybe docking stuff.. to net6.. but now pulling in compatible stuff for old docking so i could be in a lot of trouble.

im using using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.AvalonEdit.CodeCompletion; using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit; using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; using ICSharpCode.SharpDevelop.Dom.CSharp;

its old... i tried to update it it and now only old c# works. im still learning. but if you are beyond me and still cant get a way to trace, this pipe way can help. the vs 2022 preview, btw has great collection views and inspectors now. so if yhou have a massive screen or 2 4x screens maybe its ok enough. i dont use vscode b/c it is actualy slower now and i dev on windows., and some of the new features going in vs 2022 preview fast are great like code and feature search.. anways hope this help and maybe i need some direction too... ill be updating stufff all week as this guy donest work for me and i have to dig into it

Thanks for keeping cscript gong tho.. i reallhy need Net6 core on everything so i have barely any .48 refs.. the windowws compatibilty does drag some in, and i might need to remove all old stuff...my docking components use them, etc. im just trying to get back basic scripting for now tho...

if this is still a real issue.. witho no better work around ill send u the pipe code and all that. its simple..only works on windows it think tho. id love a better way id spend up to a month..if there iis.. i dont make big scripts tho.. its just little plugins for game characters, nice to tweak and tune while testing andrunning the game.. edit and continue is very limited , but inspect and step in, over , out, toggle BP, without 3 windows ( game, tool, debugger) + the little named pipe App, all on screen would be ideal.

damian-666 commented 1 year ago

btw (SORRY TLDR stuff here) i see were we using very old stuff.. NRefactory, etc. those were achived years ago.. i dont need VB conversion, and have tons of folding , highlighting, code code completion code that relies on this sort of precompiler. that only works on older c# code if at all now . Imsuppose im going to replace the NRefactory with Microsoft.Code Analysis, but if someone can point me to a WPF code text editor control that can work in a Netcore6 windows app ( or better, multiplatfrom Avalonia) that does all this.. and allows code stepping competion, preparsing, highlighting, inspecton... in wpf ( windows net.6 at least ) i mght be able to ditch a ton of old code completion/ folding/highlight, styling service code, to talks to my my text editor. I thnk I can eventually remove -all- netstandard 2 , legacy net 4.8 wpf pieces, but id prefer not to... I see quite a few out there...

heres where i think im headed: https://github.com/roslynpad/roslynpad

and something like this https://github.com/icsharpcode/AvalonEdit/blob/master/ICSharpCode.AvalonEdit.Sample/Window1.xaml

much of ths stuff has not been updated to net6.. dont know if i should wait and see ..

and something like this https://github.com/icsharpcode/AvalonEdit/blob/master/ICSharpCode.AvalonEdit.Sample/Window1.xaml

sorry m all over the place on this... Anyways, if you just just need the debug hack, maybe my suggestion will help .. on 4.8 windows wpf tool, , i can still script, debug/ simple plugins, but only old versions of c#. because of net 6 , netstandard 2.0 deprecation ( ts not -really- supported, theres issues) . 4.8 issues to net 6 lib issues, , i mght really have to go all the way... theres too many breaking changes between net48 and netcore 6 for wpf and weird compatibly modes, upgrade assistant put in.

oleg-shilo commented 1 year ago

Basically, there are two options:

  1. Do what @damian-666 suggested: insert a programmatic breakpoint System.Diagnostics.Debugger.Break(). If you want fine control and the ability to disable breakpoint you can use a different condition of a global scope variable. IE:
    public static string DEBUGONCE_STRING = "if (Environment.GetEnvironmentVariable("CSS_DISABLE_BREAKPOINT") == null && Core.Game.Debugger.CanBreak) System.Diagnostics.Debugger.Break();";

    And when you hit breakpoint first time you can disable it by setting the envar from the immediate window: image

  2. If you know your script file location you can open it in VS, put breakpoint on the required line and attach the debugger to the running script host process. That's it. After that you can enable/disable breakpoints the usual way.
mvainLive commented 1 year ago

Thanks for your help, I'll let you know how I get on.

damian-666 commented 1 year ago

"And when you hit breakpoint first time you can disable it by setting the envar from the immediate window:" thx, so mayb nevermind that pipe process stuff was descrbing to shut off the host static variable ..maybe Immedidate Window didnt exist when we started so he had to go that far around.. also a watch mght be able just change a global variable in your host ..but the prominent "Disable Breakponts" button on the pipe process we made, (stay on top window in bottom right of screen) gave you back control to your UI on your tool, then you could toggle F9 to remove the DEbug.Break lines from the script, , and it would remove the line w/ regex or cursor location and the text editor thing, withoutt hunting around too much.. depends how much source you have access to.

damian-666 commented 1 year ago

BTW we made a huge debugger / text editor based on AvaloniaEdit, that parses, folds, simple search and replace, f8 toggles a Debug.Writelin with selecgted text and Regex to put in the inspection, f9 toggled the break point aby adding that line.. So I'm looking for a newer sort of thing.. i see close to it... without the old NRefactory... buthavent found a code stepper i can just stick in to a wpf host and not sure one high level control will fits my purpose. with UI in it...

but , BTW even the old stuff still works ok if i use old c# and no linq.. its for tuning, level design, virtual robots, and tweakng not for serious development of data structures. thx CSScrpt ,we been using this for like 10 years. then the plugin code goes into a folder and then it gets built into the game package. But theres qute a bit of advanced tooling i gottta visit...

oleg-shilo commented 1 year ago

@damian-666, this is how the success story reads :) Glad you managed to produce such a comprehensive solution

damian-666 commented 1 year ago

well, thx for reading all my troubles. So i decided to stick to my old only WPF tool with its old docking and just target windows , it net6.windows now.

everything the script now at least basically works, folding, autocomplete.. tweaking code, inspection , showing errors , highlighting with latest AvaloniaEdit, inspection by adding a trace statement, debug like we discussed earlier, but its depending on an old NRefactory v 3.5, and for a 5000 line plug its needs the Parser which i commented out, so fully works using exceptions, parsing those, from the rosyln Compile the first class, to show and highlight errors, but its way too slow for a large plugin . i dont care if it leaks or never really unloads stuff..

whats missing is that I had to take out the Parser step as it doesn't do anything now, never returns an error , and is based on an old IRefactory v 3.5.. i dont need VB either.

so i do a complete script build each tick.. of my compile service.. way tooo slow ...

I think i need the old Parser back to give me autocomplete / errors, and highlighting in a usable way..

Foldings and AbstractfoldingStrategy is gone from the new NRefactory in your CSScipt ..The rest seems somewhat portable. I don't use or need folding it works but its too slow so almost never used. so i have a mix old NRefactory, commented out code, and new SCCript and zero Parsing/ precompiling.

**i see in one our your vaults you still use a similar ( somewhat) CSharp.NRefactory and it version 5.2 ICSharpCode.NRefactory.CSharp.dll And you have a Parse that mabye would give me some autocomplete intellisense that might be compatible with my UI code that works now.

So im wondering if I should reference your code, here

https://github.com/oleg-shilo/cs-script.npp/tree/master/src/CSScriptIntellisense/CSharpIntellisense**

**this says WIP???.

i see a sort of Parser precompiler based on a NRefactory thats similar... hat mabye all i need to transplant that and similar , and i could limit c# features in plugins if necessary.

using yours as a reference , sticking with the code and UI my staff cut out of somewhere and port it using your newer code**

or ... use Microsoft code analysis or something for a new parser? is there a easy path our of NRefactory as its achived last i checked? i don't mind using it for 2 years more.

i use plugins as sort of a python like front end to science code, for fluids or AI, or robot feedback controller, but tons of performance code ends up in there..but then it it works i can then move it to base classes , then optimize it, but i need it to at least be able to reflect on it...from the scripts.. i need the phython- like experience as you have always provided.

I m willing to pay for a consult if you could advise , or if i show my code and you advise from it ...I don't know if facing possible weeks of work or a few days.

im willing to spend a week or less on it and ditch VB, folding.. and other stuff mabye u dropped... perhaps give a PR back if yours if its your WIP is not in a working state..

Unless you point me another way ill start taking https://github.com/oleg-shilo/cs-script.npp/tree/master/src/CSScriptIntellisense/CSharpIntellisense and see if i can rid my old stuff , ditch VB and folding.

or TLDR (off ropix)
Or i could try and switch to another wpf module there are a few , nugets, but mabye another bag of trouble.

some other whole wpf/ script / debug package with UI and stuff... and try it these out in my windows net6 wpf app ( which doesn't target 4.8 and can't because it uses net6 dlls) but i think does link to 4.8 docking components) ... im afraid it will do too much and be slow... i dont need this work work outside of windows either.

as for alternatives seems to me , AvaloniaUI wpf or otehr debugger/ script / autocomplete wpf controls could be a nightmare i don't need.. even if they do have stepping and debug and inspection or formatting i dont want the bloat. if i want to refactor or restructure i use vsstudio and build it in there. but i do need it to be fast as least after the initial load, to reload. and so i need the Parser again to work...

i try to build Rosyln pad and others but they don't even build on net6-windows yet for wpf..... or try to build part of update RoslynPad to net6.. myself.. its not on net6,, links to 48 still and its was an open issue for over a year.. it looks like has too many features, issues..

thx if you have time to read this and offer suggestion or ill dig at ur newest Parserthats been touched a few months back, seems relevant still..

damian-666 commented 1 year ago

PROGRES!! i just trying dropped in your version of the IRefactory dll and all the old code ported over. now its fast enough and usable. the code completing doestn seem much better than before but i need to recheck it carefully.. some issues like new features like default values in parameters method( int A, int B =0) showing as parser errors, ( mabye i should ignore those particular errors in that ) . Should I find the source to NRefactory and build it for .net 6? Thanx for maintaing this even if you dont have time to read all this.. I'm not sure if this is the best way but at least im using much newer code , seems promising to be enough for my next release... I dont want to use NRefactory if its supplanted, but its fine,, or i might be missing some setup.. looking like i can just keep what i have thanks for maintaining it). i commented out the folding but maybe i can put it back and look at your code more..

oleg-shilo commented 1 year ago

I say, do not go NRefactory road. I gave up on it a long time ago.

All intellisense derivative products of CS-Script (including CS-Script.Npp) are now using Syntaxer.

I suggest you use it as a dependency service. It is a simple app that runs as a syntax provider service. It uses Roslyn to allow the following intellisense functionality:

It runs as a console app and listens to the socket for the intellisense requests. And responds with a simple to interpret data.

It was not properly released as an individual product (so no dedicated public documentation) but rather as an embedded part of IDE plugins. The cleanest integration is for the CS-SCript extension for VSCode. Though CS-Script.npp can also be used as an integration guide. In fact you can probably just reuse syntaxer.cs

damian-666 commented 1 year ago

thank you... i got home last night try to pull out another old COM dependency and realized its not good enough.. i will give it a try.. i loved that the old code just build without changes but i would spend some more days on it..

BTW slighly off topic but you might be curious im talking to a julia guy who is making a IL compiler for julia. its basically raison e'etre is similar as yours module, and why julia is embraced by science community. that you can script it like python, but also compile fast code.. however, not deploy it like netcore so easily. but he realises they cant compete with net 6, 7 innovation.. .. but with net u could now deploy the whole thing to anyware, even wiht the julia code. . hes basically giving to JUlia what netcore has, the deployability and interfacing wiht closed sources .. and u have given to .net what Python has. https://github.com/HyperSphereStudio/JULIAdotNET ... im just staying wiht c#.. ill follow ur advice because i need that live plugin script for the same reason people use python.. but i like to pack up an ship all my code too... hes working at a different level tho, at the IL level.. the julia IDEs already script fine.

On Wed, Sep 14, 2022 at 8:50 AM Oleg Shilo @.***> wrote:

I say, do not go NRefactory road. I gave up on it a long time ago.

All intellisense derivative products of CS-Script (including CS-Script.Npp) are now using Syntaxer https://github.com/oleg-shilo/syntaxer.core.

I suggest you use it as a dependency service. It is a simple app that runs as a syntax provider service. It uses Roslyn to allow the following intellisense functionality:

  • references
  • suggest_usings
  • resolve
  • completion
  • tooltip
  • signaturehelp
  • project
  • codemap
  • format

It runs as a console app and listens to the socket for the intellisense requests. And responds with a simple to interpret data.

It was not properly released as an individual product (so no dedicated public documentation) but rather as an embedded part of IDE plugins. The cleanest integration is for the CS-SCript extension for VSCode. Though CS-Script.npp can also be used as an integration guide. In fact you can probably just reuse syntaxer.cs https://github.com/oleg-shilo/cs-script.npp/blob/6900ac7fe8e5904316dfb81ee48cb8c042c2e213/src/CSScriptIntellisense/Syntaxer.cs

— Reply to this email directly, view it on GitHub https://github.com/oleg-shilo/cs-script/issues/305#issuecomment-1246884512, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD74XGP3LZCXA2VYLNMSKZ3V6HQ2ZANCNFSM565BNVRA . You are receiving this because you were mentioned.Message ID: @.***>

oleg-shilo commented 1 year ago

thanks for sharing that info on JULIAdotNET

damian-666 commented 1 year ago

thanks.for showing m where to look, seems lke it would work. i think i got all the advice i need to finish but it you are curious about my use case, read on:

those other code classes are also similar to what mh prio tech lead probably copied from your vault like10 years ago.. my updated IAvanlotEdit and highlighter is fed by a CodeSenseUpdate(); which gives back a IList , and i think the editor / highligher just wants a IEnumerable of ICompletionData so i might get lucky and transplant it... regardless its looking like it will work well enough even with what i got. i just went through and took out new c# syntax for now and its good enough. ill revisit autocomplete and folding later, but i dont really need it to be that perfect.

BTW this is just an anecdote of my use case of physics simulation code in a game, in case anyone is interested, i think ill have to get DomEvaluator to work to solve it. im sure ill figure out a way to do this and its amazing i can do all this with your ccsript: this really gives a great way to tweak simulations outside of devstudio in the visualizer. as ML people do with python.. but better as i can ship it all , even AOT compile it. since the plugins folder in Dev studio, compiles any new file in there now, its easier than ever. But, Edit and continue and the switching between VS studio 2022 and my visual output requires way to much of the screen and back and forth.. and it rarely even works... i have to rebuild everything. the the script usually lets me tweak code for 30 min before it starts to give issues. so its been very valuable .

rosyln seems best for me, after i stripped out namespaces, but it appends one root. like ccscip-root .. but i have some plugins that define classes that are based on a Parameterized: IPlugin type thing. the T is the type of params that go with the enity ECS that has the plugin, so the save/ load /param/ and param apply , is based in the base class and not repeated. that way i can script a separate parameter persistent class llke

[DataContract] WaveParam s{ [datamember] float [Datamember] Waveheight {get;set} float Period {get;set;}},

defined in the root namespace, in the plugin file, its very useful for tuning and keeping the tunings data for one instance separate of the class code, but along with the class in one file.

and the params are saved as a member with the Parent entity owner of the IPlugin such as an ECS entity like an instance of one body of water , as a blob or xml string with DataContractSerializer. the plugin class is in one separate class file ..wiht base classes precompiled and a templated interface , all works well. i also start wiht the most classes precompiled in an assemby and switch to the scripted version only if its changed...

then I use a class like WaveSim: Parameterized :Plugin, in the same root namespace, which loads and saves the tuning params in the file without special casting.. using the base setup and save code.. But its using data loaded either from the assembly compile version, or the tweaked plugin version that might replace it . so everhting was good .. But with roysln i get a type cast mismatch loading the params because now the WaveParams in the script are redefined, in another assembly, that is tempory and or has a differnt name with the root prepended. i get error says typeA was define in assemby.. temp\csscriptxxxxxxv2... but type B ( in the base class).. in my appdomain so mabye its not possible to expect this to work, w rosylin.. and i should factor the Params in another class but its nice to add members while scripting. mabye theres a clever way to serialize the params tho. if i scritp the base class i will get serialization issues later as well) .

anyways* im weighting the pros and cons of Dom vs roysyn*. i think if i get Dom to work even these big pluging scripts with serialized params can probably work, hopefully wihtout me adding namespaces.. or ill use your precompiler..... I might just introduce a switch so that i can choose rosylin for simple plugins and dom for ones that define types. or factor out the param classes but its nice to introduce new params member to persistence tuning data while im scripting along with the plugin class in one file.

thanks again for maintaining this ..

On Wed, Sep 14, 2022 at 4:23 PM Damian @.***> wrote:

thank you... i got home last night try to pull out another old COM dependency and realized its not good enough.. i will give it a try.. i loved that the old code just build without changes but i would spend some more days on it..

BTW slighly off topic but you might be curious im talking to a julia guy who is making a IL compiler for julia. its basically raison e'etre is similar as yours module, and why julia is embraced by science community. that you can script it like python, but also compile fast code.. however, not deploy it like netcore so easily. but he realises they cant compete with net 6, 7 innovation.. .. but with net u could now deploy the whole thing to anyware, even wiht the julia code. . hes basically giving to JUlia what netcore has, the deployability and interfacing wiht closed sources .. and u have given to .net what Python has. https://github.com/HyperSphereStudio/JULIAdotNET ... im just staying wiht c#.. ill follow ur advice because i need that live plugin script for the same reason people use python.. but i like to pack up an ship all my code too... hes working at a different level tho, at the IL level.. the julia IDEs already script fine.

On Wed, Sep 14, 2022 at 8:50 AM Oleg Shilo @.***> wrote:

I say, do not go NRefactory road. I gave up on it a long time ago.

All intellisense derivative products of CS-Script (including CS-Script.Npp) are now using Syntaxer https://github.com/oleg-shilo/syntaxer.core.

I suggest you use it as a dependency service. It is a simple app that runs as a syntax provider service. It uses Roslyn to allow the following intellisense functionality:

  • references
  • suggest_usings
  • resolve
  • completion
  • tooltip
  • signaturehelp
  • project
  • codemap
  • format

It runs as a console app and listens to the socket for the intellisense requests. And responds with a simple to interpret data.

It was not properly released as an individual product (so no dedicated public documentation) but rather as an embedded part of IDE plugins. The cleanest integration is for the CS-SCript extension for VSCode. Though CS-Script.npp can also be used as an integration guide. In fact you can probably just reuse syntaxer.cs https://github.com/oleg-shilo/cs-script.npp/blob/6900ac7fe8e5904316dfb81ee48cb8c042c2e213/src/CSScriptIntellisense/Syntaxer.cs

— Reply to this email directly, view it on GitHub https://github.com/oleg-shilo/cs-script/issues/305#issuecomment-1246884512, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD74XGP3LZCXA2VYLNMSKZ3V6HQ2ZANCNFSM565BNVRA . You are receiving this because you were mentioned.Message ID: @.***>

oleg-shilo commented 1 year ago

Not a problem.

Seems like you have quite some work ahead of you :)

damian-666 commented 1 year ago

its working now so i might just leave it workinhg as is.. slow and old c# syntax.. i just found some guys unmerged PR to magically made my Monogame code working again embedded in WPF.. once a codebase is 10 years old.. this going to .net 6 can makeor break a company but everyone wants that net 6.. its so damn fast. m probably lucky to spend only 6 weeks on it...i i had just 3 guys x 6 years of legacy code. cant imagine what big companies face. https://blog.ndepend.com/5x-lessons-learned-from-migrating-a-large-legacy-to-net-5-6/

damian-666 commented 1 year ago

sorry to comment on a closed topic this but i was thinking instead of a complex low level c++ complete stepping debugger like the samsung one, just a reflection based, code injection.. might be good enough.. one that doesnt require a debugger attached but allows some inspection via reflection and property sheets visualizers for sort of watches..... . if i just put a sort of code that calls into a logger, and then waits or sleeps forever on the current thread till i press resume, then it returns. , like a breakpoint i cant at least press continue and or remove the BP.. .. if i press step it woud moves that waithandle or sleep(forever ) call below the next statement. but the then i guess that meast rebuild script and be slow to step over .. not step into.....

got the idea from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information?redirectedfrom=MSDN

and the 3rd answer in

https://stackoverflow.com/questions/5695827/how-to-get-current-value-of-eip-in-managed-code/18066990#18066990

"With C# 5.0 there is a new, well-hidden feature that enables this."

Caller Info attributes

anways, being able to just break , and inspect, would be nice. Then use reflection or various methods to visualize the state of the class im in, or put a property page this this?... i mabye cant see local variables.. but pretty usefull...

// using System.Runtime.CompilerServices // using System.Diagnostics;

public void DoProcessing() { TraceMessage("Something happened."); }

public void TraceMessage(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) { Trace.WriteLine("message: " + message); Trace.WriteLine("member name: " + memberName); Trace.WriteLine("source file path: " + sourceFilePath); Trace.WriteLine("source line number: " + sourceLineNumber); }

// Sample Output: // message: Something happened. // member name: DoProcessing // source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs // source line number: 31

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information?redirectedfrom=MSDN

  **the samsung debgger in mentioned in the last comment is quite complicated and in c++...  so im a bit discourage to get into that...  i can make some inspectors,  and the meta data will stell me the caller, like a call stack and** 
damian-666 commented 1 year ago

yes and I will explain is how my lead programmer did it with a wpf script host , a seperate pipe process to allow removal of the Break line..System.Diagnostics.Debugger.Break(), and using visual studion to step code in the script, PROBLEM was once you hit the Break. you cant remove it in the atttached debugger.. ( or couldn't) with an old AvalonEdit years ago . ( we do have teh source to most of the script host but you won't need all of it to do this. if this sould like what you wnat to do, read on. maybe edit and continue can remove the line now.. i dont know. the code was in a temp assembly and it would not allow it before with debugger attached.

and im looking for a debugger that can step and inspect right in my script tool better without inserting/ removing a line Debug.Writelin( "xx" + xx.ToString() to the script , thast what it does now to inspect. But github has much better inspection controls, and debugging , actual step and inspect features are being used like rosyln pad, notepadd++ plugin,etc. so im open to new and better ways.. bugt dongt want rewrite all mhy huge level editor.. BUT if you have tried and cant find a way , this might help:

anyways here WAS our problem.. if you put a Break line it will go to your debugger, we used vs2022 but vs code should be fine, that you can attach or launch the scripting host with.. ( with our without symbols) it will stop in your code (usually in a temp file)

when you press f9 in our script editor based on avalonia it places a this in the script this..

public static string DEBUGONCE_STRING = "if (Core.Game.Debugger.CanBreak) System.Diagnostics.Debugger.Break();";

proble we got is, once you added the debug line , how do you stop hitting the BP? how do you erase that line of code? when you attached a debugger, you cant modify the temp assembly. so you cant remove the breakpoint and keep going.

so he launched a separate pipe apt, from the spawned wpf app, an exe process, just a little wpf exe.. what it doestis open a separate process and a connect named pipe to the ..to the script host . the little debugger.exe host is ont the buttom right of screen on top, it has a toggle button called Disable Breakpoints. so..s from the scripting host to a a named pipe that turned off /on the Core.Game.Debugger.CanBreak. it works...but

I dont like this. im updating old WPF legacy code to nett6.. i dont wanan go full avalonia but I would love to explore the rosyln pad, other precompilers, and such. more complete avalonai like codecomplete, . I stripped namedspaces and got CCsrpit to work again. with rosyln.. I would rather step and out, inspect, and not use attach vs 2022 as a debugger. if im doing alot of code i can build in my scripts and or changen base classes.

Im not saying ths is still an issue , and stil updateing my WPF 4.8 to net6.. and pulling in compabitibly stuf for old docking so im in a lot of troubles.

im using using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.AvalonEdit.CodeCompletion; using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit; using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; using ICSharpCode.SharpDevelop.Dom.CSharp;

its old... i tried to update it it and now only old c# works. im still learning. but if you are beyond meand still cant get a way to trace, this can help. the vs 2022 preview, btw has great collection views and inspectors now. so if yhou have a massive screen or 2 4x screens maybe its ok enough. i dont use vscode b/c it is actualy slower now and i dev on windows., and some of the new featuers going in fast are great like code and feature search.. anways hope this help and maybe i need some direction too... ill be updating stufff all week as this guy donest work for me and i have to dig into it

thants for keeping cscript gong tho.. i reallhy need Net6 core on everything so i have barelyh any .48 refs.. the windowws compatibilty does drag some in, and i might need to remove all old stuff...my docking components use them, etc. im just trying to get back basic scripting for now tho...

if this is still a real issue.. witho no better work around ill send u the pipe code and all that. its simple..only works on windows it think tho. id love a better way id spend up to a month..if there iis.. i dont make big scripts tho.. its just little plugins for game characters, nice to tweak and tune while testing andrunning the game.. edit and continue is very limited , but inspect and step in, over , out, toggle BP, without 3 windows ( game, tool, debugger) + the little named pipe App, all on screen would be ideal.

On Thu, Aug 18, 2022 at 6:36 AM Martin Vain @.***> wrote:

Hi,

I have a compiled exe application that will load a script like this,

CSScript.Evaluator.LoadFile(scriptPath, _logger);

My script is a class that contains methods that can do some work. I pass in a ILogger instance as can be seen above.

The source code for the hosting application is not be available where the scripts will be created and tested.

What I would like to do is keep VSCode open with the script and when methods are called in the script VSCode can break at a break point and allow step through debugging.

Is this possible?

I do have the script running and doing what I want during my development process.

Is this possible?

This is the first time I have posted / asked a question on here so I hope my question is clear enough?

Martin

— Reply to this email directly, view it on GitHub https://github.com/oleg-shilo/cs-script/issues/305, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD74XGPJT5KKRTYSKMNGNM3VZYU5NANCNFSM565BNVRA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

oleg-shilo commented 1 year ago

Sorry for the delayed response. Did now want to respond in a rush.

It is a very interesting topic. I spend endless nights getting debugger integration work for .NET, so I know very well the challenge you are facing.

I am not sure if you are aware or not but CS-Script plugin for Notepad++ comes with a fully functional debugger. True debugger. Though... it is a .NET Framework debugger, not .NET Core.

I took practically unknown, undocumented and incomplete source code of MDbg that MS shared, I reverse-engineered it, extracted the core functionality and integrated it with CS-Script as runtime and Notepad++ as IDE. I had all the traditional debugger features:

Sounds great, right? No.... It was a nightmare. The Mdbg itself is a managed app with about 10 assemblies with absolute zero documentation. It was created in the early days of .NET so it was full of nasty C++ antipatterns. While it was managed code the actual debugging API was COM based so reverse engineering was extremely difficult because Reflection did not go beyond IntPtr handles. It was a snapshot of the evolving codebase that had completely ignored property inspections. The whole first-class citizen type of runtime objects was not even covered.

I managed to get it all together. True. But I felt like Frodo Baggins after crossing Dark Forest. "I survived, but I will never go back." I felt like it was maybe only a dozen in the world people ever touched that code outside of MS. This is what Google search showed. They all had the same questions as I did. But I arguably succeeded the most.

In result, the solution was working but flaky and on top of it, the awkward Scintilla API (Notepad++) made User Experience almost substandard.

The last blow came when it became apparent that .NET Core is incompatible with Mdbg. So eventually I migrated Notepad++ plugin on .NET Core CS-Script and simply ditched the debugger completely. Instead, I simplified user experience for attaching debugger of your choice and switched focus to VSCde integration as it has ready to go nice CLR debugger. And this is what I recommend CS-Script users use.

For a light runtime object dumps you can use built-in <object>.print(); and it will spit JSON of the object. the same way as VS "Immediate Window" dows. For anything more comprehensive use a proper debugger. VSCode is a recommended one. It is the only available portable option.

Just going through some of your points...

[CallerMember*] agree it is an interesting technique. Combined with the DebugFrames available already it can be a simple, cheap and reliable call stack visualization functionality.

public static string DEBUGONCE_STRING - it is exactly what CS-Script does at the script start when you execute srcript with '//x' (css script.cs //x).

You can easily control if BP should fire again and again within the same process session by extending the BP condition by analysing the external hosting context. IE if the environment variable with the name bp|<file_path>|<line_number> is set, then fire System.Diagnostics.Debugger.Break();. If not, then pass through.

The only problem is that you cannot discover stack frame clr objects with Reflection, even though you can use Reflection to inspect objects themselves. This was a limitation of the CLR. Unless .NET Core somehow advanced in this area.

Nevertheless, I like the whole idea you are describing and I see the value in it. At least some :) But sadly there are a few show-stoppers there that make the whole proposal questionable.