1 / 45

Debugging Production SharePoint Applications

Debugging Production SharePoint Applications. Wouter van Vugt. Agenda. How .NET and the CLR work Optimizing code and the effect on debuggers Debugging third party code. How .NET and the CLR work. .NET Applications. Written in any language Executed in a runtime called CLR

lyndon
Télécharger la présentation

Debugging Production SharePoint Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging Production SharePoint Applications Wouter van Vugt

  2. Agenda • How .NET and the CLR work • Optimizing code and the effect on debuggers • Debugging third party code

  3. How .NET and the CLR work

  4. .NET Applications • Written in any language • Executed in a runtime called CLR • Shield code from hardware specifics • Java: One language, multi OS • .NET: Many languages, one OS (and Mono) • Allows various types of preprocessing of code

  5. Common Language Runtime

  6. Type Safety • Type Safety • A pointer of a specific type can never point to an object which is not of that type! • Checked at compile AND runtime! • Knowing the actual type enables • Reading its data  Quite useful in debugging  • Calling its functions Car c = new Person() ‘Pointer type’ Pointer Object type

  7. Demo Try and Circumvent type safety

  8. Creating .NET Applications C#, C++, VB.NET, F#.... (40+) Module Assembly Compile Link PDB PDB

  9. Executing code • Assemblies contain mostly IL code • IL is the ‘Machine code’ for .NET • Compiled to native instructions at runtime through the JIT Compiler • Enough information remains to decompile back to source! Language richness compared Machine Code C# IL

  10. Executing code with JIT Compilation Console.WriteLine("Hi"); Console.WriteLine("There"); Console metadata static void WriteLine() JIT Compiler Address static void WriteLine(string) JIT Compiler Address Native Address Native CPU Instructions • JIT Compiler • Lookup IL for method • Compile IL to native • Modify metadata to point to compiled code • Jump to compiled code

  11. Executing code with JIT Compilation Console.WriteLine("Hi"); Console.WriteLine("There"); Console metadata static void WriteLine() JIT Compiler Address static void WriteLine(string) Native Address Native CPU Instructions • JIT Compiler • Lookup IL for method • Compile IL to native • Modify metadata to point to compiled code • Jump to compiled code

  12. Code optimization • Compile-time • Constant value folding • Remove Branch to next instruction and NOP instructions used to break on control flow, {, }, EndIf etc • Overflow checking • … • During JIT • Eliminate local variables • Range check elimination • Method inlining • Tail call optimization • Common subexpression elimination • Dead code elimination • Loop unrolling • …

  13. Running non optimized code • Breakpoints • Edit and continue

  14. C# Compiler – Variable Generation

  15. C# Compiler – Variable Generation Debug Release

  16. C# Compiler – Inserting NOP

  17. C# Compiler – Inserting NOP Debug Release

  18. C# Compiler – Branching

  19. C# Compiler – Branching Debug Release

  20. Demo Compile time optimizations

  21. Just In Time Compilation • Transformation of MSIL to native x86 / x64 / ARM etc • Can optimize code • Mathematically ensured correctness • Takes a small performance hit during startup

  22. NGEN.exe • JIT Compilation incurs a runtime cost • NGEN pre-compiles MSIL assemblies to machine code • Runs as a service in the background • Also used by the “.NET Runtime Optimization Service”

  23. JIT - Local variable elimination Before User user = GetUser(3); PrintUser(user); PrintUser(GetUser(3)); After

  24. JIT – Range check elimination Before static int[] _array = new int[10]; static volatile int _i; static void Main(string[] args) { for (int i = 0; i < _array.Length; ++i) _i += _array[i]; } static int[] _array = new int[10]; static volatile int _i; static void Main(string[] args) { int[] localRef = _array; for (int i = 0; i < localRef.Length; ++i) _i += localRef[i]; } After

  25. JIT – Method Inlining Before public class Program { public static int And(int first, int second) { return first & second; } static voidMain(string[] args) { int i = And(5, 4); } } public class Program { static void Main(string[] args) { int i = 5 & 4; } } After

  26. JIT – Tail Call Optimization • staticvoid Main(string[] args) • { • TestTailCallOptimization(); • } • publicstaticvoidTestTailCallOptimization() • { • string s = "Test"; • TailCall1(s); • } • staticvoid TailCall1(string s) • { • Console.WriteLine(s); • TailCall2(s); • } • staticvoid TailCall2(string s) • { • Console.WriteLine(s + s); • }

  27. Loading non-precompiled assemblies • Native NGEN images are hard to debug • Remove the NGEN image • Or, prevent NGEN image from loading C:\> NGEN uninstall MyAssembly C:\> SET COMPLUS_ZAPDISABLE=1

  28. Preventing JIT optimization • Creating debug builds • JIT prevented through compile time attributes • INI file MyAssembly.ini • In Visual Studio, on module load after attach [assembly: Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.Default)] [.NET FrameworkDebuggingControl]GenerateTrackingInfo=1AllowOptimize=0

  29. Demo Debugging optimized code

  30. Debugging code • PDB files are as essential as code! • Breakpoints can do way more than break • Tracing is cheaper than debugging • IntelliTrace can run in production

  31. PDB Files • Contain vital debugging information • No PDB? Severely limited debugging experience! • Minimal investment: Symbol server • Managed PDB files contain • Source file names / line numbers • Local variable names • Alternate streams  Used by TFS

  32. Symbol Servers • Central repository for storing debug symbols • File system based technology • Integrated into Team Foundation and Visual Studio

  33. Locating debug symbols • PDB files are located • Same directory as module • Hardcoded path in module • Symbol server cache • Symbol server MyAssembly.dll MyAssembly.pdb Equal GUID GUID

  34. Dumpbin.exe • Use to peek inside PE files

  35. Prevent loading all PDBs

  36. Visual Studio Source Server support • Allows Visual Studio to download relevant source files automatically from a repository.

  37. Configuring Symbol Locations

  38. PDB alternate streams • Used by TFS to store server path to sources

  39. Demo Symbol servers, Source Servers and source indexing

  40. Working with break points

  41. Setting breakpoints on automatic properties • Use ‘Break at function’ and enter get_name

  42. Setting breakpoints on automatic properties • Inspect callstack / locals window

  43. Debugging third party code • Red Gate .NET Reflector PRO • Decompile assemblies into source code • Generate PDB file from assembly + source • Enables debugging third party code

  44. Enabling remote debugging • Run Remote Debugging x86 or x64 • Give permissions • Connect

  45. Demo Debugging SharEPoint

More Related