chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
 
using System;
using System.Reflection;
 
namespace OpenTap
{
    /// <summary> Debugger plugin interface for attaching custom debuggers. </summary>
    public interface IDebugger
    {
        /// <summary> Invoked when programs starts. </summary>
        void AttachDebugger();
    }
 
    /// <summary> Class for managing debuggers. Set the OPENTAP_DEBUGGER_ASSEMBLY environment variable to make it possible to attach debuggers. 
    /// The environment variable should contain paths to assemblies with IDebugger implementations. </summary>
    /// <remarks>
    /// To use this feature with Visual Studio, first install the Visual Studio SDK:
    ///    C:\git\opentap\bin\Debug\tap package install "Visual Studio SDK"
    /// Then set the environment variable to the location of the debugger dll in that package:
    ///    $env:OPENTAP_DEBUGGER_ASSEMBLY="C:\git\opentap\bin\Debug\Packages\Visual Studio SDK\OpenTap.VSSdk.Debugger.dll"
    /// </remarks>
    public static class DebuggerAttacher
    {
        const string DebuggerAssemblyEnv = "OPENTAP_DEBUGGER_ASSEMBLY";
 
        /// <summary> Attaches the debugger. </summary>
        public static void TryAttach()
        {
            var assembly = Environment.GetEnvironmentVariable(DebuggerAssemblyEnv);
            if (string.IsNullOrEmpty(assembly))
                return;
 
            Console.WriteLine("Attaching debugger " + assembly);
            try
            {
                var bytes = System.IO.File.ReadAllBytes(assembly);
                var asm = Assembly.Load(bytes);
                var types = asm.GetExportedTypes();
                foreach (Type t in types)
                {
                    // This is done to avoid loading plugins in the tap.exe application - we cannot reference the IDebugger interface directly.
                    if (t.GetInterface("OpenTap.IDebugger") != null)
                    {
                        object dbg = Activator.CreateInstance(t);
                        var m = t.GetMethod("AttachDebugger");
                        m.Invoke(dbg, Array.Empty<object>());
                    }
                }
            }
            catch(TargetInvocationException tie)
            {
                Console.WriteLine("Exception when running IDebugger.AttachDebugger: {0}", tie.InnerException.Message);
            }
            catch(Exception e)
            {
                Console.WriteLine("Caught exception {0}", e.Message);
            }
        }
    }
}