Sat, 08 Oct 2016 21:01:32 +0200
Give the next debugger command to the thread where we are stopped at the moment.
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

1  # * coding: utf8 * 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

2  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

3  # Copyright (c) 2014  2016 Detlev Offenbach <detlev@dieoffenbachs.de> 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

4  # 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

5  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

6  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

7  Module implementing an import hook patching thread modules to get debugged too. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

8  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

9  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

10  import os.path 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

11  import sys 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

12  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

13  if sys.version_info[0] == 2: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

14  import thread as _thread 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

15  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

16  import _thread 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

17  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

18  import threading 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

19  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

20  from DebugBase import DebugBase 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

21  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

22  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

23  class ThreadExtension(object): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

24  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

25  Class implementing the thread support for the debugger. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

26  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

27  Provides methods for intercepting thread creation, retriving the running 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

28  threads and their name and state. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

29  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

30  def __init__(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

31  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

32  Constructor 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

33  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

34  self.threadNumber = 1 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

35  self.enableImportHooks = True 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

36  self._original_start_new_thread = None 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

37  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

38  self.clientLock = threading.RLock() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

39  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

40  # dictionary of all threads running {id: DebugBase} 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

41  self.threads = {_thread.get_ident(): self} 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

42  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

43  # the "current" thread, basically for variables view 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

44  self.currentThread = self 
5221
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset

45  # the thread we are at a breakpoint continuing at next command 
960afd19c1b6
Give the next debugger command to the thread where we are stopped at the moment.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5209
diff
changeset

46  self.currentThreadExec = self 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

47  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

48  # special objects representing the main scripts thread and frame 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

49  self.mainThread = self 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

50  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

51  if sys.version_info[0] == 2: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

52  self.threadModName = 'thread' 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

53  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

54  self.threadModName = '_thread' 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

55  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

56  # reset already imported thread module to apply hooks at next import 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

57  del sys.modules[self.threadModName] 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

58  del sys.modules['threading'] 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

59  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

60  sys.meta_path.insert(0, self) 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

61  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

62  # provide a hook to perform a hard breakpoint 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

63  # Use it like this: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

64  # if hasattr(sys, 'breakpoint): sys.breakpoint() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

65  sys.breakpoint = self.set_trace 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

66  
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

67  def attachThread(self, target=None, args=None, kwargs={}, 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

68  mainThread=False): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

69  """ 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

70  Public method to setup a standard thread for DebugClient to debug. 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

71  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

72  If mainThread is True, then we are attaching to the already 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

73  started mainthread of the app and the rest of the args are ignored. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

74  
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

75  @param target the start function of the target thread (i.e. the user 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

76  code) 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

77  @param args arguments to pass to target 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

78  @param kwargs keyword arguments to pass to target 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

79  @param mainThread True, if we are attaching to the already 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

80  started mainthread of the app 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

81  @return identifier of the created thread 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

82  """ 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

83  if mainThread: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

84  ident = _thread.get_ident() 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

85  name = 'MainThread' 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

86  newThread = self.mainThread 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

87  newThread.isMainThread = True 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

88  if self.debugging: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

89  sys.setprofile(newThread.profile) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

90  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

91  else: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

92  newThread = DebugBase(self) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

93  ident = self._original_start_new_thread( 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

94  newThread.bootstrap, (target, args, kwargs)) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

95  name = 'Thread{0}'.format(self.threadNumber) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

96  self.threadNumber += 1 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

97  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

98  newThread.id = ident 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

99  newThread.name = name 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

100  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

101  self.threads[ident] = newThread 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

102  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

103  return ident 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

104  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

105  def threadTerminated(self, threadId): 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

106  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

107  Public method called when a DebugThread has exited. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

108  
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

109  @param threadId id of the DebugThread that has exited 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

110  @type int 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

111  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

112  self.lockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

113  try: 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

114  del self.threads[threadId] 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

115  except KeyError: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

116  pass 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

117  finally: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

118  self.unlockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

119  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

120  def lockClient(self, blocking=True): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

121  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

122  Public method to acquire the lock for this client. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

123  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

124  @param blocking flag to indicating a blocking lock 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

125  @type bool 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

126  @return flag indicating successful locking 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

127  @rtype bool 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

128  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

129  if blocking: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

130  self.clientLock.acquire() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

131  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

132  return self.clientLock.acquire(blocking) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

133  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

134  def unlockClient(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

135  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

136  Public method to release the lock for this client. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

137  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

138  try: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

139  self.clientLock.release() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

140  except AssertionError: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

141  pass 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

142  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

143  def setCurrentThread(self, id): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

144  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

145  Public method to set the current thread. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

146  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

147  @param id the id the current thread should be set to. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

148  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

149  try: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

150  self.lockClient() 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

151  if id is None: 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

152  self.currentThread = None 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

153  else: 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

154  self.currentThread = self.threads.get(id) 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

155  finally: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

156  self.unlockClient() 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

157  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

158  def dumpThreadList(self): 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

159  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

160  Public method to send the list of threads. 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

161  """ 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

162  threadList = [] 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

163  if len(self.threads) > 1: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

164  currentId = _thread.get_ident() 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

165  # update thread names set by user (threading.setName) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

166  threadNames = {t.ident: t.getName() for t in threading.enumerate()} 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

167  
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

168  for id, thd in self.threads.items(): 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

169  d = {"id": id} 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

170  try: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

171  d["name"] = threadNames.get(id, thd.name) 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

172  d["broken"] = thd.isBroken 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

173  except Exception: 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

174  d["name"] = 'UnknownThread' 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

175  d["broken"] = False 
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

176  
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

177  threadList.append(d) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

178  else: 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

179  currentId = 1 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

180  d = {"id": 1} 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

181  d["name"] = "MainThread" 
5208
aa8045780ce4
Make attachThread and dumpThreadList runnable again.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5207
diff
changeset

182  d["broken"] = self.isBroken 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

183  threadList.append(d) 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

184  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

185  self.sendJsonCommand("ResponseThreadList", { 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

186  "currentID": currentId, 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

187  "threadList": threadList, 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

188  }) 
5209
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

189  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

190  def find_module(self, fullname, path=None): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

191  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

192  Public method returning the module loader. 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

193  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

194  @param fullname name of the module to be loaded 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

195  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

196  @param path path to resolve the module name 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

197  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

198  @return module loader object 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

199  @rtype object 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

200  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

201  if fullname in sys.modules or not self.debugging: 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

202  return None 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

203  if fullname == self.threadModName and self.enableImportHooks: 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

204  # Disable hook to be able to import original module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

205  self.enableImportHooks = False 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

206  return self 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

207  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

208  return None 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

209  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

210  def load_module(self, fullname): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

211  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

212  Public method to load a module. 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

213  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

214  @param fullname name of the module to be loaded 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

215  @type str 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

216  @return reference to the loaded module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

217  @rtype module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

218  """ 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

219  module = __import__(fullname) 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

220  sys.modules[fullname] = module 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

221  if (fullname == self.threadModName and 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

222  self._original_start_new_thread is None): 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

223  # make thread hooks available to system 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

224  self._original_start_new_thread = module.start_new_thread 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

225  module.start_new_thread = self.attachThread 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

226  
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

227  self.enableImportHooks = True 
cd058aa6af37
Insert import hook to modify thread module to use our bootstrap.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
5208
diff
changeset

228  return module 
5207
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

229  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

230  
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

231  # 
7283629b02c0
Relocated some methods from Debug*Thread and the remaining modules into a new one.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset

232  # eflag: noqa = M702 