How to debug auto-generated code? - c#

I am trying to see what goes on in the IAsyncStateMachine at runtime ,and i desperately need to see what variables it has and what it calls.I know you can see the code with the ILSpy ...but i need to debug it.
Is there any method?
I need to see what goes on inside the IAsyncStateMachine MoveNext method !
public sealed partial class MethodBuilder<T> : Errand.MethodBuilder {
public static new MethodBuilder<T> Create() => new MethodBuilder<T>();
public new void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine {
this.myStateMachine = stateMachine;
this.Task = new Task<T>(this);
stateMachine.MoveNext(); //i have to see the properties of stateMachine and inside this method !!!!!
}
public new void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine machine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine {
}
public void SetResult(T result) {
this.Task.isCompleted = true;
this.Task.result = result;
}
public new void SetStateMachine(IAsyncStateMachine stateMachine) => base.SetStateMachine(stateMachine);
public new void SetException(Exception ex) => base.SetException(ex);
}

From MSDN How to: Debug .NET Framework Source.
To enable .NET Framework source debugging
On the Tools menu, click Options.
In the Options dialog box, click the Debugging category.
In the General box, set Enable .NET Framework source stepping.
If you had Just My Code enabled, a warning dialog box tells you that
Just My Code is now disabled. Click OK.
If you did not have a symbol cache location set, another warning
dialog box tells you that a default symbol cache location is now
set. Click OK.
Under the Debugging category, click Symbols.
If you want to change the symbols cache location:
Open the Debugging node in the box on the left.
Under the Debugging node, click Symbols.
Edit the location in Cache symbols from symbol servers to this
directory or click Browse to choose a location.
If you want to download symbols immediately, click Load Symbols
using above locations.
This button is not available in design mode.
If you do not choose to download symbols now, symbols will be
downloaded automatically the next time that you start the debugging
your program.
Click OK to close the Options dialog box.

perhaps you can use debugger.launch
once the debugger launch visual studio will prompt for vs version selection
internal class Program
{
public static void Main(string[] args)
{
System.Diagnostics.Debugger.Launch();
Console.WriteLine("crap");
}
}

Related

C# DLL calls property "get" for no reason

I am creating a custom C# Windows Forms control library (a DLL) in Visual Studio 2019 (Professional). My control has a property that takes the following form (this property is aimed to be accessed by applications which use the DLL):
public double Hello
{
get { throw new ApplicationException("Hi!"); }
}
(In my efforts to find out why this is happening, I've simplified the property to just throw an exception and do nothing else.)
For some reason, if I run my User Control (in Debug mode), the exception is raised - even though nowhere else in this code calls that property! (The IDE confirms this - saying "0 references" above it). Why does the property "get" accessor seem to be called for no reason? The stack trace shows that the "get" was called by "[External code]"...
This should be pretty easy to reproduce if you have Visual Studio 2019: create a new "Windows Forms Control Library (.NET Framework)" project under C#, then right click on "UserControl1.cs" in the Solution Explorer and click "View Code", then just add in the above code to the class.
I have reproduced your problem. Based on my test, I find that winformscontrollibary will
load all the properties you set in the code, because it needs to load them into the
property bar of form.
Like the following, if you write the following code.
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
public double Hello
{
get { return 1.02; }
}
public int Number
{
get { return 1; }
}
}
You will see the correct property(Hello and Number) in the right of form.
Therefore, if you write the code throw new ApplicationException("Hi!"); in the get method , it will throw the exception.

VS2017 Error debugging Task of Tuple

I am unable to view the debug information when using a Task of Tuple. E.G. When a breakpoint his hit, I cannot view any variables on hover, in the local window, or in the watch window.
The repro is just to create a new WPF app, add System.ValueTuple, add this code to MainWindow.xaml.cs, and then set breakpoints at both lines with "return".
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var task1 = TaskWithLocalDebugInfo();
var task2 = TaskWithoutLocalDebugInfo();
}
private async Task<bool> TaskWithLocalDebugInfo()
{
var viewableInLocalWindowAndHover = true;
return viewableInLocalWindowAndHover;
}
private async Task<(bool, bool)> TaskWithoutLocalDebugInfo()
{
var notViewableInLocalWindowAndHover = true;
return (notViewableInLocalWindowAndHover, notViewableInLocalWindowAndHover);
}
}
}
Edit: If I add the un-viewable local variable to watch, I get: error CS8182: Predefined type 'ValueTuple`2' must be a struct.
It's a bug in the current version of Visual Studio 2017. It has been fixed and will be out in the next quarterly release.
See the GitHub issue and the comment from the MS employee saying it's fixed.
In the meantime, from the GitHub comment on Apr 13, 2017:
i can confirm that the bug repros with ValueTuple 4.3.0, but not with 4.3.0-preview1-24530-04.
You can install the "preview" version via the NuGet Package Manager/Manage NuGet Packages for Solution interface. Just select 4.3.0-preview1-24530-04 from the "Version:" dropdown and click "Install".
One hopes that, after the next update for Visual Studio, using the "preview" version of the package won't be necessary. As noted by the previously mentioned comment, it's not clear why using the "preview" version of the package avoids triggering the bug. But obviously it's preferable to be able to use the latest "stable" version of the package if one can; who knows what changes happened since the "preview" version that would lead to some other hard-to-diagnose bug, in the debugger or otherwise.
After seeing the error in watch window, I remembered that there is a new ValueTask in C# 7. So I added package system.threading.tasks.extensions and changed the broken method to use ValueTask instead of Task
private static async ValueTask<(bool, bool)> TaskWithoutLocalDebugInfo()
{
var notViewableInLocalWindowAndHover = true;
return (notViewableInLocalWindowAndHover, notViewableInLocalWindowAndHover);
}
I can now see the debug information.

StyleCop Analyzers rule SA1515 violation output only in Output Window but not in Error List Window

I have installed StyleCop.Analyzers nuget (it's version 1.0.0) and I have configured SA1515 (Single comments must be preceded by blank line) to error on violation.
The following code however
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int x = 2;
/// my comment
int y = 3;
}
}
}
does only show the violation in the Ouput Window as
ConsoleApplication6\Program.cs(8,7,8,9): error SA1515: Single-line comment must be preceded by blank linelists the error
but there are no squiggles and no entries in the Error Window. Changing the /// to // makes the error appear in the error list immediately.
Is there are way to debug this in order to find the buggy spot? I have opened the source code I got from GitHub in Visual Studio, set a break point on the one context.ReportDiagnostic call in that rule class, set to start another VS instance on debug start, opened the ConsoleApplication6 in there and never got a break point hit.

Can't step Into and debug the serviced component source code

I have developed a COM+ Component in C# be inheriting ServicedComponent.
Here is how it looks like:
[Transaction(TransactionOption.Required)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[EventTrackingEnabledAttribute(true)]
[JustInTimeActivation]
[ObjectPooling(Enabled = true, MinPoolSize = 10, MaxPoolSize = 30, CreationTimeout = 15000)]
[Synchronization]
class MyComponent: System.EnterpriseServices.ServicedComponent
{
[AutoComplete(true)]
public string getHello()
{//2nd breakpoint
ContextUtil.SetComplete();
return "HelloWorld";
}
}
I have another test project from which I call this component.
class Program
{
static void Main(string[] args)
{
MyComponent myComp = new MyComponent();
myComp.getHello();//1st Breakpoint
}
}
I am not able to reach 2nd Breakpoint. This was working before I switched to VS 2012. Strange thing is after switching to 2012 its no longer working in VS 2010 too.
I've already tried,
Attach to process
Unchecked "Enable Just My Code" in debug settings
Can someone please give direction from here?
UPDATE 1
From the links given by Mike, I tried symchk for my DLL in the same folder where DLL and PDB files were there. It fails with error saying PDB mismatched or not found. I don't know how to resolve this error.
You may be missing the .pdb file in your project.
Check this microsoft link out for an explanation: https://msdn.microsoft.com/en-us/library/yd4f8bd1(vs.71).aspx

Make VS jump to line x in editor

Im writing a policy plugin for VS which checks several issues with the code. If an issue occurs it will be displayed in the policy warnings tab. Now I want to jump to the line where the issue occurs in the editor when I double click it in the policy warning tab. How can I do that?
namespace PolicyPlugin
{
[Serializable]
public class MyPolicyPlugin : PolicyBase
{
//...
//called if the user clicks on a policy warning
public override void Activate(PolicyFailure failure)
{
// make jump to line x
}
}
}
Thanks!
You could try to get DTE automation object first:
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
or use alternative ways to get it.
An then execute standard command (that's what happens when you press CTRL+G in Visual Studio)
DTE.ExecuteCommand("Edit.Goto", "1234")
Note: I'm not sure about exact ExecuteCommand method signature. Also you can manipulate IDE the same way for other commands.

Categories