DebugClients/Python/DebugThread.py

changeset 5141
bc64243b7672
parent 5126
d28b92dabc2b
parent 5140
01484c0afbc6
child 5144
1ab536d25072
equal deleted inserted replaced
5126:d28b92dabc2b 5141:bc64243b7672
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the debug thread.
8 """
9
10 import bdb
11 import sys
12
13 from DebugBase import DebugBase
14
15
16 class DebugThread(DebugBase):
17 """
18 Class implementing a debug thread.
19
20 It represents a thread in the python interpreter that we are tracing.
21
22 Provides simple wrapper methods around bdb for the 'owning' client to
23 call to step etc.
24 """
25 def __init__(self, dbgClient, targ=None, args=None, kwargs=None,
26 mainThread=0):
27 """
28 Constructor
29
30 @param dbgClient the owning client
31 @param targ the target method in the run thread
32 @param args arguments to be passed to the thread
33 @param kwargs arguments to be passed to the thread
34 @param mainThread 0 if this thread is not the mainscripts thread
35 """
36 DebugBase.__init__(self, dbgClient)
37
38 self._target = targ
39 self._args = args
40 self._kwargs = kwargs
41 self._mainThread = mainThread
42 # thread running tracks execution state of client code
43 # it will always be 0 for main thread as that is tracked
44 # by DebugClientThreads and Bdb...
45 self._threadRunning = 0
46
47 self.__ident = None # id of this thread.
48 self.__name = ""
49 self.tracePython = False
50
51 def set_ident(self, id):
52 """
53 Public method to set the id for this thread.
54
55 @param id id for this thread (int)
56 """
57 self.__ident = id
58
59 def get_ident(self):
60 """
61 Public method to return the id of this thread.
62
63 @return the id of this thread (int)
64 """
65 return self.__ident
66
67 def get_name(self):
68 """
69 Public method to return the name of this thread.
70
71 @return name of this thread (string)
72 """
73 return self.__name
74
75 def traceThread(self):
76 """
77 Public method to setup tracing for this thread.
78 """
79 self.set_trace()
80 if not self._mainThread:
81 self.set_continue(0)
82
83 def bootstrap(self):
84 """
85 Public method to bootstrap the thread.
86
87 It wraps the call to the user function to enable tracing
88 before hand.
89 """
90 try:
91 try:
92 self._threadRunning = 1
93 self.traceThread()
94 self._target(*self._args, **self._kwargs)
95 except bdb.BdbQuit:
96 pass
97 finally:
98 self._threadRunning = 0
99 self.quitting = 1
100 self._dbgClient.threadTerminated(self)
101 sys.settrace(None)
102 sys.setprofile(None)
103
104 def trace_dispatch(self, frame, event, arg):
105 """
106 Public method wrapping the trace_dispatch of bdb.py.
107
108 It wraps the call to dispatch tracing into
109 bdb to make sure we have locked the client to prevent multiple
110 threads from entering the client event loop.
111
112 @param frame The current stack frame.
113 @param event The trace event (string)
114 @param arg The arguments
115 @return local trace function
116 """
117 try:
118 self._dbgClient.lockClient()
119 # if this thread came out of a lock, and we are quitting
120 # and we are still running, then get rid of tracing for this thread
121 if self.quitting and self._threadRunning:
122 sys.settrace(None)
123 sys.setprofile(None)
124 import threading
125 self.__name = threading.currentThread().getName()
126 retval = DebugBase.trace_dispatch(self, frame, event, arg)
127 finally:
128 self._dbgClient.unlockClient()
129
130 return retval
131
132 #
133 # eflag: FileType = Python2
134 # eflag: noqa = M601, M702

eric ide

mercurial