24 class DebugControl(object): |
31 class DebugControl(object): |
25 """Control and output for debugging.""" |
32 """Control and output for debugging.""" |
26 |
33 |
27 def __init__(self, options, output): |
34 def __init__(self, options, output): |
28 """Configure the options and output file for debugging.""" |
35 """Configure the options and output file for debugging.""" |
29 self.options = options |
36 self.options = list(options) + FORCED_DEBUG |
30 self.output = output |
37 self.raw_output = output |
|
38 self.suppress_callers = False |
|
39 |
|
40 filters = [] |
|
41 if self.should('pid'): |
|
42 filters.append(add_pid_and_tid) |
|
43 self.output = DebugOutputFile( |
|
44 self.raw_output, |
|
45 show_process=self.should('process'), |
|
46 filters=filters, |
|
47 ) |
31 |
48 |
32 def __repr__(self): |
49 def __repr__(self): |
33 return "<DebugControl options=%r output=%r>" % (self.options, self.output) |
50 return "<DebugControl options=%r raw_output=%r>" % (self.options, self.raw_output) |
34 |
51 |
35 def should(self, option): |
52 def should(self, option): |
36 """Decide whether to output debug information in category `option`.""" |
53 """Decide whether to output debug information in category `option`.""" |
37 return (option in self.options or option in FORCED_DEBUG) |
54 if option == "callers" and self.suppress_callers: |
|
55 return False |
|
56 return (option in self.options) |
|
57 |
|
58 @contextlib.contextmanager |
|
59 def without_callers(self): |
|
60 """A context manager to prevent call stacks from being logged.""" |
|
61 old = self.suppress_callers |
|
62 self.suppress_callers = True |
|
63 try: |
|
64 yield |
|
65 finally: |
|
66 self.suppress_callers = old |
38 |
67 |
39 def write(self, msg): |
68 def write(self, msg): |
40 """Write a line of debug output.""" |
69 """Write a line of debug output. |
41 if self.should('pid'): |
70 |
42 msg = "pid %5d: %s" % (os.getpid(), msg) |
71 `msg` is the line to write. A newline will be appended. |
|
72 |
|
73 """ |
43 self.output.write(msg+"\n") |
74 self.output.write(msg+"\n") |
44 if self.should('callers'): |
75 if self.should('callers'): |
45 dump_stack_frames(out=self.output) |
76 dump_stack_frames(out=self.output, skip=1) |
46 self.output.flush() |
77 self.output.flush() |
47 |
78 |
48 def write_formatted_info(self, header, info): |
79 |
49 """Write a sequence of (label,data) pairs nicely.""" |
80 class DebugControlString(DebugControl): |
50 self.write(info_header(header)) |
81 """A `DebugControl` that writes to a StringIO, for testing.""" |
51 for line in info_formatter(info): |
82 def __init__(self, options): |
52 self.write(" %s" % line) |
83 super(DebugControlString, self).__init__(options, StringIO()) |
|
84 |
|
85 def get_output(self): |
|
86 """Get the output text from the `DebugControl`.""" |
|
87 return self.raw_output.getvalue() |
53 |
88 |
54 |
89 |
55 def info_header(label): |
90 def info_header(label): |
56 """Make a nice header string.""" |
91 """Make a nice header string.""" |
57 return "--{0:-<60s}".format(" "+label+" ") |
92 return "--{0:-<60s}".format(" "+label+" ") |
92 import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 |
134 import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 |
93 ... |
135 ... |
94 |
136 |
95 `limit` is the number of frames to include, defaulting to all of them. |
137 `limit` is the number of frames to include, defaulting to all of them. |
96 |
138 |
97 """ |
139 `skip` is the number of frames to skip, so that debugging functions can |
98 stack = inspect.stack()[limit:0:-1] |
140 call this and not be included in the result. |
|
141 |
|
142 """ |
|
143 stack = inspect.stack()[limit:skip:-1] |
99 return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) |
144 return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) |
100 |
145 |
101 |
146 |
102 def dump_stack_frames(limit=None, out=None): # pragma: debugging |
147 def dump_stack_frames(limit=None, out=None, skip=0): |
103 """Print a summary of the stack to stdout, or some place else.""" |
148 """Print a summary of the stack to stdout, or someplace else.""" |
104 out = out or sys.stdout |
149 out = out or sys.stdout |
105 out.write(short_stack(limit=limit)) |
150 out.write(short_stack(limit=limit, skip=skip+1)) |
106 out.write("\n") |
151 out.write("\n") |
|
152 |
|
153 |
|
154 def short_id(id64): |
|
155 """Given a 64-bit id, make a shorter 16-bit one.""" |
|
156 id16 = 0 |
|
157 for offset in range(0, 64, 16): |
|
158 id16 ^= id64 >> offset |
|
159 return id16 & 0xFFFF |
|
160 |
|
161 |
|
162 def add_pid_and_tid(text): |
|
163 """A filter to add pid and tid to debug messages.""" |
|
164 # Thread ids are useful, but too long. Make a shorter one. |
|
165 tid = "{0:04x}".format(short_id(_thread.get_ident())) |
|
166 text = "{0:5d}.{1}: {2}".format(os.getpid(), tid, text) |
|
167 return text |
|
168 |
|
169 |
|
170 def filter_text(text, filters): |
|
171 """Run `text` through a series of filters. |
|
172 |
|
173 `filters` is a list of functions. Each takes a string and returns a |
|
174 string. Each is run in turn. |
|
175 |
|
176 Returns: the final string that results after all of the filters have |
|
177 run. |
|
178 |
|
179 """ |
|
180 clean_text = text.rstrip() |
|
181 ending = text[len(clean_text):] |
|
182 text = clean_text |
|
183 for fn in filters: |
|
184 lines = [] |
|
185 for line in text.splitlines(): |
|
186 lines.extend(fn(line).splitlines()) |
|
187 text = "\n".join(lines) |
|
188 return text + ending |
|
189 |
|
190 |
|
191 class CwdTracker(object): # pragma: debugging |
|
192 """A class to add cwd info to debug messages.""" |
|
193 def __init__(self): |
|
194 self.cwd = None |
|
195 |
|
196 def filter(self, text): |
|
197 """Add a cwd message for each new cwd.""" |
|
198 cwd = os.getcwd() |
|
199 if cwd != self.cwd: |
|
200 text = "cwd is now {0!r}\n".format(cwd) + text |
|
201 self.cwd = cwd |
|
202 return text |
|
203 |
|
204 |
|
205 class DebugOutputFile(object): # pragma: debugging |
|
206 """A file-like object that includes pid and cwd information.""" |
|
207 def __init__(self, outfile, show_process, filters): |
|
208 self.outfile = outfile |
|
209 self.show_process = show_process |
|
210 self.filters = list(filters) |
|
211 |
|
212 if self.show_process: |
|
213 self.filters.append(CwdTracker().filter) |
|
214 cmd = " ".join(getattr(sys, 'argv', ['???'])) |
|
215 self.write("New process: executable: %s\n" % (sys.executable,)) |
|
216 self.write("New process: cmd: %s\n" % (cmd,)) |
|
217 if hasattr(os, 'getppid'): |
|
218 self.write("New process: parent pid: %s\n" % (os.getppid(),)) |
|
219 |
|
220 SYS_MOD_NAME = '$coverage.debug.DebugOutputFile.the_one' |
|
221 |
|
222 @classmethod |
|
223 def the_one(cls, fileobj=None, show_process=True, filters=()): |
|
224 """Get the process-wide singleton DebugOutputFile. |
|
225 |
|
226 If it doesn't exist yet, then create it as a wrapper around the file |
|
227 object `fileobj`. `show_process` controls whether the debug file adds |
|
228 process-level information. |
|
229 |
|
230 """ |
|
231 # Because of the way igor.py deletes and re-imports modules, |
|
232 # this class can be defined more than once. But we really want |
|
233 # a process-wide singleton. So stash it in sys.modules instead of |
|
234 # on a class attribute. Yes, this is aggressively gross. |
|
235 the_one = sys.modules.get(cls.SYS_MOD_NAME) |
|
236 if the_one is None: |
|
237 assert fileobj is not None |
|
238 sys.modules[cls.SYS_MOD_NAME] = the_one = cls(fileobj, show_process, filters) |
|
239 return the_one |
|
240 |
|
241 def write(self, text): |
|
242 """Just like file.write, but filter through all our filters.""" |
|
243 self.outfile.write(filter_text(text, self.filters)) |
|
244 self.outfile.flush() |
|
245 |
|
246 def flush(self): |
|
247 """Flush our file.""" |
|
248 self.outfile.flush() |
|
249 |
|
250 |
|
251 def log(msg, stack=False): # pragma: debugging |
|
252 """Write a log message as forcefully as possible.""" |
|
253 out = DebugOutputFile.the_one() |
|
254 out.write(msg+"\n") |
|
255 if stack: |
|
256 dump_stack_frames(out=out, skip=1) |
|
257 |
|
258 |
|
259 def filter_aspectlib_frames(text): # pragma: debugging |
|
260 """Aspectlib prints stack traces, but includes its own frames. Scrub those out.""" |
|
261 # <<< aspectlib/__init__.py:257:function_wrapper < igor.py:143:run_tests < ... |
|
262 text = re.sub(r"(?<= )aspectlib/[^.]+\.py:\d+:\w+ < ", "", text) |
|
263 return text |
|
264 |
|
265 |
|
266 def enable_aspectlib_maybe(): # pragma: debugging |
|
267 """For debugging, we can use aspectlib to trace execution. |
|
268 |
|
269 Define COVERAGE_ASPECTLIB to enable and configure aspectlib to trace |
|
270 execution:: |
|
271 |
|
272 $ export COVERAGE_LOG=covaspect.txt |
|
273 $ export COVERAGE_ASPECTLIB=coverage.Coverage:coverage.data.CoverageData |
|
274 $ coverage run blah.py ... |
|
275 |
|
276 This will trace all the public methods on Coverage and CoverageData, |
|
277 writing the information to covaspect.txt. |
|
278 |
|
279 """ |
|
280 aspects = os.environ.get("COVERAGE_ASPECTLIB", "") |
|
281 if not aspects: |
|
282 return |
|
283 |
|
284 import aspectlib # pylint: disable=import-error |
|
285 import aspectlib.debug # pylint: disable=import-error |
|
286 |
|
287 filename = os.environ.get("COVERAGE_LOG", "/tmp/covlog.txt") |
|
288 filters = [add_pid_and_tid, filter_aspectlib_frames] |
|
289 aspects_file = DebugOutputFile.the_one(open(filename, "a"), show_process=True, filters=filters) |
|
290 aspect_log = aspectlib.debug.log( |
|
291 print_to=aspects_file, attributes=['id'], stacktrace=30, use_logging=False |
|
292 ) |
|
293 public_methods = re.compile(r'^(__init__|[a-zA-Z].*)$') |
|
294 for aspect in aspects.split(':'): |
|
295 aspectlib.weave(aspect, aspect_log, methods=public_methods) |