18
Advanced Debugging in Visual Studio Ingo Rammer ingo.rammer@thinktecture .com thinktecture

Ingo Rammer [email protected] thinktecture

Embed Size (px)

Citation preview

Page 1: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Advanced Debugging in Visual Studio

Ingo [email protected]

Page 2: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Ingo Rammer und

Kleine Beratungsfirma für Softwareentwickler

Unterstützung, Coaching und Beratung für Software-Architekten und -Entwickler

Architektur- und Code-ReviewsPrototypentwicklung und ArchitekturberatungCoaching und MentoringAnwendungsoptimierung und Troubleshooting

http://[email protected]

Page 3: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Erweiterte Breakpoints

Breakpoints„Just My Code“ (ToolsOptionsDebuggingGeneral)CTRL+B, Breakpoint Groups

TracepointsHitcountConditionsMakrosCommand Window

helpalias

Page 4: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Variablenfenster

Ausdrücke in Variablenfenstern (Watch, Locals, Autos)DebuggerDisplayDebuggerProxyDebuggerVisualizer

Page 5: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Interaktive Tests

Interaktive KlassenerstellungAufruf von statischen Methoden

Object WorkbenchAufruf von Instanzmethoden

Immediate Fenster

Page 6: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Neues in Visual Studio 2008

Debugging in den .NET Framework Quellcode

http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspxOder: Suche nach „debugging .net source code 2008“Grund warum ich es nicht zeige: benötigt Internet-Verbindung, Quellcode ist nicht gecached

Multithreaded Debugging

Page 7: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

DebuggerDisplay

public class Invoice{ [DebuggerDisplay("Items: {_items.Count} Sum: {Sum}")] private List<InvoiceItem> _items = new List<InvoiceItem>();

public double Sum { get { double sum = 0; foreach (InvoiceItem itm in _items) sum += (itm.Price * itm.Amount); return sum; } }}

• [DebuggerDisplay]-Attribut

Page 8: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

DebuggerVisualizer

[DebuggerVisualizer] AttributKlassen müssen [Serializable] sein

Visualizer erhält serialisierten KlonRückmeldung per objectService.ReplaceObject()

[DebuggerVisualizer(typeof(MyVisualizer))][Serializable]public class Invoice{ //...}

Page 9: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Debugger Visualizer

Für Applikationsklassen (Visualizer in gleicher Assembly wie Anwendung)

Referenz auf Microsoft.VisualStudio.DebuggerVisualizersAbleiten von DialogDebuggerVisualizer

protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider){ MyClass dt = (MyClass) objectProvider.GetObject();

Form1 frm = new Form1(); // ... show data ... windowService.ShowDialog(frm);}

Page 10: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Debugger Visualizer

Möglichkeit 2: Getrennte Visualizer in Klassenbibliothek

Visualizer kann global oder per User installiert werden:

Program Files\Microsoft Visual Studio 9\ Common7\Packages\Debugger\VisualizersMy Documents\Visual Studio 2008\Visualizers

[assembly: System.Diagnostics.DebuggerVisualizer(typeof(InvoiceVisualizer),typeof(VisualizerObjectSource),Target = typeof(Invoice),Description = "Invoice Visualizer")]

Page 11: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Obskures: Debugger Proxy

Stellvertreter-Klassen im DebuggerWerden automatisch statt den ursprünglichen Klassen angezeigt[assembly: DebuggerTypeProxy(typeof(InvoiceProxy), Target = typeof(Invoice))]

public class InvoiceProxy: Invoice{ private Invoice _val;

public InvoiceProxy(Invoice val) {

_val = val; } ...

Page 12: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Teil 2Vorbereiten für´s Debuggen

Page 13: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Vorbereitungen?

Wofür?Zeitersparnis während EntwicklungUnterstützung im Testbetrieb („Friendly Customers“)Fehlersuche im Echtbetrieb („Production Debugging“)

GrundsätzlichWährend der Entwicklungszeit bestimmen Sie die notwendige Zeit für späteres Debugging

Page 14: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Exceptions

Primärer Einsprungpunkt für späteres DebuggenDrei Grundregeln

Auslösen von nicht-essentiellen Exceptions vermeiden (TryParse, File.Exists)Was ist essentiell? Wenn die Methode nicht das macht (oder: machen kann), was der Name nahelegt, dann wird eine Exception geworfenExceptions werden nur dort gefangen, wo die Applikation etwas mit der Exception machen kann

Logging ist kein Grund für re-throws! (Callstack gibt‘s auch später noch)Wo muss gecatcht werden?

Threadprozeduren, Finalizer, eventuell Events

Page 15: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

MDbg

Kleiner, feiner DebuggerZwei Versionen:

MDbg SDK (hier nicht relevant)MDbg Sample (mit Sourcecode)

Bietet komplettes Debugger Objektmodell

Page 16: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Beispiel: Locks

Zu lange Locking-Dauer ist ein Problem für Skalierbarkeit von ApplikationenWas kann man im Debugger machen?

Page 17: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

Summary

Debugging von Applikationen mit Visual StudioErweiterte Breakpoints, Tracepoints, Makros[DebuggerDisplay], [DebuggerVisualizer], [DebuggerProxy]Neues in Visual Studio 2008

Vorbereitung von AnwendungenException-HandlingMDbgEigene Debugger mit dem Objektmodell von MDbg

Beispiel: Lock-Bearbeitung

Page 18: Ingo Rammer ingo.rammer@thinktecture.com thinktecture

© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after

the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.