File size: 7,322 Bytes
10865e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import os
import string
import sys

import pythoncom
import win32api
from win32com.axdebug import (
    adb,
    axdebug,
    codecontainer,
    contexts,
    documents,
    expressions,
    gateways,
)
from win32com.axdebug.util import _wrap, _wrap_remove, trace
from win32com.axscript import axscript

currentDebugger = None


class ModuleTreeNode:
    """Helper class for building a module tree"""

    def __init__(self, module):
        modName = module.__name__
        self.moduleName = modName
        self.module = module
        self.realNode = None
        self.cont = codecontainer.SourceModuleContainer(module)

    def __repr__(self):
        return "<ModuleTreeNode wrapping %s>" % (self.module)

    def Attach(self, parentRealNode):
        self.realNode.Attach(parentRealNode)

    def Close(self):
        self.module = None
        self.cont = None
        self.realNode = None


def BuildModule(module, built_nodes, rootNode, create_node_fn, create_node_args):
    if module:
        keep = module.__name__
        keep = keep and (built_nodes.get(module) is None)
        if keep and hasattr(module, "__file__"):
            keep = string.lower(os.path.splitext(module.__file__)[1]) not in [
                ".pyd",
                ".dll",
            ]
    #               keep = keep and module.__name__=='__main__'
    if module and keep:
        #        print "keeping", module.__name__
        node = ModuleTreeNode(module)
        built_nodes[module] = node
        realNode = create_node_fn(*(node,) + create_node_args)
        node.realNode = realNode

        # Split into parent nodes.
        parts = string.split(module.__name__, ".")
        if parts[-1][:8] == "__init__":
            parts = parts[:-1]
        parent = string.join(parts[:-1], ".")
        parentNode = rootNode
        if parent:
            parentModule = sys.modules[parent]
            BuildModule(
                parentModule, built_nodes, rootNode, create_node_fn, create_node_args
            )
            if parentModule in built_nodes:
                parentNode = built_nodes[parentModule].realNode
        node.Attach(parentNode)


def RefreshAllModules(builtItems, rootNode, create_node, create_node_args):
    for module in list(sys.modules.values()):
        BuildModule(module, builtItems, rootNode, create_node, create_node_args)


# realNode = pdm.CreateDebugDocumentHelper(None) # DebugDocumentHelper node?
# app.CreateApplicationNode() # doc provider node.


class CodeContainerProvider(documents.CodeContainerProvider):
    def __init__(self, axdebugger):
        self.axdebugger = axdebugger
        documents.CodeContainerProvider.__init__(self)
        self.currentNumModules = len(sys.modules)
        self.nodes = {}
        self.axdebugger.RefreshAllModules(self.nodes, self)

    def FromFileName(self, fname):
        ### It appears we cant add modules during a debug session!
        #               if self.currentNumModules != len(sys.modules):
        #                       self.axdebugger.RefreshAllModules(self.nodes, self)
        #                       self.currentNumModules = len(sys.modules)
        #               for key in self.ccsAndNodes.keys():
        #                       print "File:", key
        return documents.CodeContainerProvider.FromFileName(self, fname)

    def Close(self):
        documents.CodeContainerProvider.Close(self)
        self.axdebugger = None
        print("Closing %d nodes" % (len(self.nodes)))
        for node in self.nodes.values():
            node.Close()
        self.nodes = {}


class OriginalInterfaceMaker:
    def MakeInterfaces(self, pdm):
        app = self.pdm.CreateApplication()
        self.cookie = pdm.AddApplication(app)
        root = app.GetRootNode()
        return app, root

    def CloseInterfaces(self, pdm):
        pdm.RemoveApplication(self.cookie)


class SimpleHostStyleInterfaceMaker:
    def MakeInterfaces(self, pdm):
        app = pdm.GetDefaultApplication()
        root = app.GetRootNode()
        return app, root

    def CloseInterfaces(self, pdm):
        pass


class AXDebugger:
    def __init__(self, interfaceMaker=None, processName=None):
        if processName is None:
            processName = "Python Process"
        if interfaceMaker is None:
            interfaceMaker = SimpleHostStyleInterfaceMaker()

        self.pydebugger = adb.Debugger()

        self.pdm = pythoncom.CoCreateInstance(
            axdebug.CLSID_ProcessDebugManager,
            None,
            pythoncom.CLSCTX_ALL,
            axdebug.IID_IProcessDebugManager,
        )

        self.app, self.root = interfaceMaker.MakeInterfaces(self.pdm)
        self.app.SetName(processName)
        self.interfaceMaker = interfaceMaker

        expressionProvider = _wrap(
            expressions.ProvideExpressionContexts(),
            axdebug.IID_IProvideExpressionContexts,
        )
        self.expressionCookie = self.app.AddGlobalExpressionContextProvider(
            expressionProvider
        )

        contProvider = CodeContainerProvider(self)
        self.pydebugger.AttachApp(self.app, contProvider)

    def Break(self):
        # Get the frame we start debugging from - this is the frame 1 level up
        try:
            1 + ""
        except:
            frame = sys.exc_info()[2].tb_frame.f_back

        # Get/create the debugger, and tell it to break.
        self.app.StartDebugSession()
        #               self.app.CauseBreak()

        self.pydebugger.SetupAXDebugging(None, frame)
        self.pydebugger.set_trace()

    def Close(self):
        self.pydebugger.ResetAXDebugging()
        self.interfaceMaker.CloseInterfaces(self.pdm)
        self.pydebugger.CloseApp()
        self.app.RemoveGlobalExpressionContextProvider(self.expressionCookie)
        self.expressionCookie = None

        self.pdm = None
        self.app = None
        self.pydebugger = None
        self.root = None

    def RefreshAllModules(self, nodes, containerProvider):
        RefreshAllModules(
            nodes, self.root, self.CreateApplicationNode, (containerProvider,)
        )

    def CreateApplicationNode(self, node, containerProvider):
        realNode = self.app.CreateApplicationNode()

        document = documents.DebugDocumentText(node.cont)
        document = _wrap(document, axdebug.IID_IDebugDocument)

        node.cont.debugDocument = document

        provider = documents.DebugDocumentProvider(document)
        provider = _wrap(provider, axdebug.IID_IDebugDocumentProvider)
        realNode.SetDocumentProvider(provider)

        containerProvider.AddCodeContainer(node.cont, realNode)
        return realNode


def _GetCurrentDebugger():
    global currentDebugger
    if currentDebugger is None:
        currentDebugger = AXDebugger()
    return currentDebugger


def Break():
    _GetCurrentDebugger().Break()


brk = Break
set_trace = Break


def dosomethingelse():
    a = 2
    b = "Hi there"


def dosomething():
    a = 1
    b = 2
    dosomethingelse()


def test():
    Break()
    input("Waiting...")
    dosomething()
    print("Done")


if __name__ == "__main__":
    print("About to test the debugging interfaces!")
    test()
    print(
        " %d/%d com objects still alive"
        % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
    )