|
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
|
2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
|
3 |
|
4 """Control of and utilities for debugging.""" |
|
5 |
|
6 import contextlib |
|
7 import inspect |
|
8 import os |
|
9 import re |
|
10 import sys |
|
11 try: |
|
12 import _thread |
|
13 except ImportError: |
|
14 import thread as _thread |
|
15 |
|
16 from coverage.backward import StringIO |
|
17 from coverage.misc import isolate_module |
|
18 |
|
19 os = isolate_module(os) |
|
20 |
|
21 |
|
22 # When debugging, it can be helpful to force some options, especially when |
|
23 # debugging the configuration mechanisms you usually use to control debugging! |
|
24 # This is a list of forced debugging options. |
|
25 FORCED_DEBUG = [] |
|
26 |
|
27 # A hack for debugging testing in sub-processes. |
|
28 _TEST_NAME_FILE = "" # "/tmp/covtest.txt" |
|
29 |
|
30 |
|
31 class DebugControl(object): |
|
32 """Control and output for debugging.""" |
|
33 |
|
34 def __init__(self, options, output): |
|
35 """Configure the options and output file for debugging.""" |
|
36 self.options = list(options) + FORCED_DEBUG |
|
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 ) |
|
48 |
|
49 def __repr__(self): |
|
50 return "<DebugControl options=%r raw_output=%r>" % (self.options, self.raw_output) |
|
51 |
|
52 def should(self, option): |
|
53 """Decide whether to output debug information in category `option`.""" |
|
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 |
|
67 |
|
68 def write(self, msg): |
|
69 """Write a line of debug output. |
|
70 |
|
71 `msg` is the line to write. A newline will be appended. |
|
72 |
|
73 """ |
|
74 self.output.write(msg+"\n") |
|
75 if self.should('callers'): |
|
76 dump_stack_frames(out=self.output, skip=1) |
|
77 self.output.flush() |
|
78 |
|
79 |
|
80 class DebugControlString(DebugControl): |
|
81 """A `DebugControl` that writes to a StringIO, for testing.""" |
|
82 def __init__(self, options): |
|
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() |
|
88 |
|
89 |
|
90 def info_header(label): |
|
91 """Make a nice header string.""" |
|
92 return "--{0:-<60s}".format(" "+label+" ") |
|
93 |
|
94 |
|
95 def info_formatter(info): |
|
96 """Produce a sequence of formatted lines from info. |
|
97 |
|
98 `info` is a sequence of pairs (label, data). The produced lines are |
|
99 nicely formatted, ready to print. |
|
100 |
|
101 """ |
|
102 info = list(info) |
|
103 if not info: |
|
104 return |
|
105 label_len = max(len(l) for l, _d in info) |
|
106 for label, data in info: |
|
107 if data == []: |
|
108 data = "-none-" |
|
109 if isinstance(data, (list, set, tuple)): |
|
110 prefix = "%*s:" % (label_len, label) |
|
111 for e in data: |
|
112 yield "%*s %s" % (label_len+1, prefix, e) |
|
113 prefix = "" |
|
114 else: |
|
115 yield "%*s: %s" % (label_len, label, data) |
|
116 |
|
117 |
|
118 def write_formatted_info(writer, header, info): |
|
119 """Write a sequence of (label,data) pairs nicely.""" |
|
120 writer.write(info_header(header)) |
|
121 for line in info_formatter(info): |
|
122 writer.write(" %s" % line) |
|
123 |
|
124 |
|
125 def short_stack(limit=None, skip=0): |
|
126 """Return a string summarizing the call stack. |
|
127 |
|
128 The string is multi-line, with one line per stack frame. Each line shows |
|
129 the function name, the file name, and the line number: |
|
130 |
|
131 ... |
|
132 start_import_stop : /Users/ned/coverage/trunk/tests/coveragetest.py @95 |
|
133 import_local_file : /Users/ned/coverage/trunk/tests/coveragetest.py @81 |
|
134 import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 |
|
135 ... |
|
136 |
|
137 `limit` is the number of frames to include, defaulting to all of them. |
|
138 |
|
139 `skip` is the number of frames to skip, so that debugging functions can |
|
140 call this and not be included in the result. |
|
141 |
|
142 """ |
|
143 stack = inspect.stack()[limit:skip:-1] |
|
144 return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) |
|
145 |
|
146 |
|
147 def dump_stack_frames(limit=None, out=None, skip=0): |
|
148 """Print a summary of the stack to stdout, or someplace else.""" |
|
149 out = out or sys.stdout |
|
150 out.write(short_stack(limit=limit, skip=skip+1)) |
|
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) |