StateWindow.py

  1   # StateWindow.py
  2   # Display the threads, locals, and app domains of the processes being debugged
  3  
  4  import sys
  5  sys.LoadAssemblyByName("System.Drawing")
  6  sys.LoadAssemblyByName("System.Windows.Forms")
  7      
  8  from System.Drawing import *
  9  from System.Windows.Forms import *
10  
11   # Display a tree with threads, locals, and app domains of the processes being debugged
12  def StateWindow():    
13       # create the parent tree
14      tree = TreeView(Dock=DockStyle.Fill)
15      CreateProcessNodes(tree, Shell.Debugger.Processes)
16  
17       # expand out the active thread, process, and frame
18      activeFont = Font(tree.Font, FontStyle.Bold)
19      for processNode in tree.Nodes:
20          if processNode.Tag == Shell.Debugger.Processes.Active:
21              processNode.NodeFont = activeFont
22              processNode.Expand()
23  
24              processNode.Nodes[1].Expand()
25              for threadNode in processNode.Nodes[1].Nodes:
26                  if threadNode.Tag == Shell.Debugger.Processes.Active.Threads.Active:
27                      threadNode.NodeFont = activeFont
28                      threadNode.Expand()
29  
30                      for frameNode in threadNode.Nodes:
31                          if frameNode.Tag == Shell.Debugger.Processes.Active.Threads.Active.CurrentFrame:
32                              frameNode.NodeFont = activeFont
33                              frameNode.Expand()
34                              
35                              break
36                      break
37              break
38          
39       # display all the stacks
40      window = Form(Text="State Window", Width=500, Height=350)
41      window.Controls.Add(tree)
42      window.ShowDialog()
43  
44  def CreateAppDomainNodes(parent, appDomains):
45      for appDomain in appDomains:
46          appDomainNode = TreeNode("%d. %s" % (appDomain.CorAppDomain.Id, appDomain.CorAppDomain.Name))
47          appDomainNode.Tag = appDomain
48          parent.Nodes.Add(appDomainNode)
49              
50          CreateAssemblyNodes(appDomainNode, appDomain.CorAppDomain.Assemblies)
51  
52  def CreateAssemblyNodes(parent, assemblies):
53      for assembly in assemblies:
54          assemblyNode = TreeNode(assembly.Name)
55          assemblyNode.Tag = assembly
56          
57          if assembly.IsFullyTrusted:
58              assemblyNode.Text = "%s (FullTrust)" % assemblyNode.Text
59  
60          parent.Nodes.Add(assemblyNode)
61  
62  def CreateFrameNodes(parent, frames):
63      i = 0
64      for frame in frames:
65          frameNode = TreeNode("%d. %s" % (i, frame))
66          frameNode.Tag = frame
67          
68           # create a child node for the locals
69          for local in frame.Function.GetActiveLocalVars(frame):
70              frameNode.Nodes.Add(CreateValueNode(local))
71                  
72          parent.Nodes.Add(frameNode)
73          i = i + 1
74  
75  def CreateProcessNodes(parent, processes):
76       # each process gets a top level node
77      for process in processes:
78          processNode = TreeNode(process.Name)
79          processNode.Tag = process
80          
81           # create a node for each AppDomain in the process
82          appDomainRoot = TreeNode("AppDomains")
83          processNode.Nodes.Add(appDomainRoot)
84          CreateAppDomainNodes(appDomainRoot, process.AppDomains)
85  
86           # create a node for each thread
87          threadRoot = TreeNode("Threads")
88          processNode.Nodes.Add(threadRoot)
89          CreateThreadNodes(threadRoot, process.Threads)
90  
91          parent.Nodes.Add(processNode)
92          
93  def CreateThreadNodes(parent, threads):
94       # create a node for each thread in the process
95      for thread in threads:
96          threadNode = TreeNode("Thread %d : %d" % (thread.Number, thread.Id))
97          threadNode.Tag = thread
98          
99           # if this thread has an active exception, create a special node for it
100          if not thread.CurrentException.IsNull:
101              threadNode.Text = "%s (%s: %s)" % (threadNode.Text, thread.CurrentException.TypeName, thread.CurrentException.GetField("_message").GetStringValue(False))
102              threadNode.Nodes.Add(CreateValueNode(thread.CurrentException, "$exception", True))
103              
104          CreateFrameNodes(threadNode, thread.Frames)                
105          parent.Nodes.Add(threadNode)
106  
107  def CreateValueNode(value, defaultName = "", forceDefaultName = False, maxExpand = 2):
108      if forceDefaultName or value.Name == None:
109          name = defaultName
110      else:
111          name = value.Name
112  
113      valueNode = TreeNode("%s : %s : %s" % (name, value.TypeName, value.GetStringValue(False)))
114      valueNode.Tag = value
115      
116      if 0 < maxExpand:
117          if value.IsComplexType:
118              for field in value.GetFields():
119                  valueNode.Nodes.Add(CreateValueNode(field, field.Name, False, maxExpand - 1))
120          elif value.IsArrayType:
121              i = 0
122              for item in value.GetArrayItems():
123                  valueNode.Nodes.Add(CreateValueNode(item, "%s[%d]" % (name, i), True, maxExpand - 1))
124                  i = i + 1
125              
126      return valueNode