1 | n/a | """Debugger basics""" |
---|
2 | n/a | |
---|
3 | n/a | import fnmatch |
---|
4 | n/a | import sys |
---|
5 | n/a | import os |
---|
6 | n/a | from inspect import CO_GENERATOR |
---|
7 | n/a | |
---|
8 | n/a | __all__ = ["BdbQuit", "Bdb", "Breakpoint"] |
---|
9 | n/a | |
---|
10 | n/a | class BdbQuit(Exception): |
---|
11 | n/a | """Exception to give up completely.""" |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | class Bdb: |
---|
15 | n/a | """Generic Python debugger base class. |
---|
16 | n/a | |
---|
17 | n/a | This class takes care of details of the trace facility; |
---|
18 | n/a | a derived class should implement user interaction. |
---|
19 | n/a | The standard debugger class (pdb.Pdb) is an example. |
---|
20 | n/a | """ |
---|
21 | n/a | |
---|
22 | n/a | def __init__(self, skip=None): |
---|
23 | n/a | self.skip = set(skip) if skip else None |
---|
24 | n/a | self.breaks = {} |
---|
25 | n/a | self.fncache = {} |
---|
26 | n/a | self.frame_returning = None |
---|
27 | n/a | |
---|
28 | n/a | def canonic(self, filename): |
---|
29 | n/a | if filename == "<" + filename[1:-1] + ">": |
---|
30 | n/a | return filename |
---|
31 | n/a | canonic = self.fncache.get(filename) |
---|
32 | n/a | if not canonic: |
---|
33 | n/a | canonic = os.path.abspath(filename) |
---|
34 | n/a | canonic = os.path.normcase(canonic) |
---|
35 | n/a | self.fncache[filename] = canonic |
---|
36 | n/a | return canonic |
---|
37 | n/a | |
---|
38 | n/a | def reset(self): |
---|
39 | n/a | import linecache |
---|
40 | n/a | linecache.checkcache() |
---|
41 | n/a | self.botframe = None |
---|
42 | n/a | self._set_stopinfo(None, None) |
---|
43 | n/a | |
---|
44 | n/a | def trace_dispatch(self, frame, event, arg): |
---|
45 | n/a | if self.quitting: |
---|
46 | n/a | return # None |
---|
47 | n/a | if event == 'line': |
---|
48 | n/a | return self.dispatch_line(frame) |
---|
49 | n/a | if event == 'call': |
---|
50 | n/a | return self.dispatch_call(frame, arg) |
---|
51 | n/a | if event == 'return': |
---|
52 | n/a | return self.dispatch_return(frame, arg) |
---|
53 | n/a | if event == 'exception': |
---|
54 | n/a | return self.dispatch_exception(frame, arg) |
---|
55 | n/a | if event == 'c_call': |
---|
56 | n/a | return self.trace_dispatch |
---|
57 | n/a | if event == 'c_exception': |
---|
58 | n/a | return self.trace_dispatch |
---|
59 | n/a | if event == 'c_return': |
---|
60 | n/a | return self.trace_dispatch |
---|
61 | n/a | print('bdb.Bdb.dispatch: unknown debugging event:', repr(event)) |
---|
62 | n/a | return self.trace_dispatch |
---|
63 | n/a | |
---|
64 | n/a | def dispatch_line(self, frame): |
---|
65 | n/a | if self.stop_here(frame) or self.break_here(frame): |
---|
66 | n/a | self.user_line(frame) |
---|
67 | n/a | if self.quitting: raise BdbQuit |
---|
68 | n/a | return self.trace_dispatch |
---|
69 | n/a | |
---|
70 | n/a | def dispatch_call(self, frame, arg): |
---|
71 | n/a | # XXX 'arg' is no longer used |
---|
72 | n/a | if self.botframe is None: |
---|
73 | n/a | # First call of dispatch since reset() |
---|
74 | n/a | self.botframe = frame.f_back # (CT) Note that this may also be None! |
---|
75 | n/a | return self.trace_dispatch |
---|
76 | n/a | if not (self.stop_here(frame) or self.break_anywhere(frame)): |
---|
77 | n/a | # No need to trace this function |
---|
78 | n/a | return # None |
---|
79 | n/a | # Ignore call events in generator except when stepping. |
---|
80 | n/a | if self.stopframe and frame.f_code.co_flags & CO_GENERATOR: |
---|
81 | n/a | return self.trace_dispatch |
---|
82 | n/a | self.user_call(frame, arg) |
---|
83 | n/a | if self.quitting: raise BdbQuit |
---|
84 | n/a | return self.trace_dispatch |
---|
85 | n/a | |
---|
86 | n/a | def dispatch_return(self, frame, arg): |
---|
87 | n/a | if self.stop_here(frame) or frame == self.returnframe: |
---|
88 | n/a | # Ignore return events in generator except when stepping. |
---|
89 | n/a | if self.stopframe and frame.f_code.co_flags & CO_GENERATOR: |
---|
90 | n/a | return self.trace_dispatch |
---|
91 | n/a | try: |
---|
92 | n/a | self.frame_returning = frame |
---|
93 | n/a | self.user_return(frame, arg) |
---|
94 | n/a | finally: |
---|
95 | n/a | self.frame_returning = None |
---|
96 | n/a | if self.quitting: raise BdbQuit |
---|
97 | n/a | # The user issued a 'next' or 'until' command. |
---|
98 | n/a | if self.stopframe is frame and self.stoplineno != -1: |
---|
99 | n/a | self._set_stopinfo(None, None) |
---|
100 | n/a | return self.trace_dispatch |
---|
101 | n/a | |
---|
102 | n/a | def dispatch_exception(self, frame, arg): |
---|
103 | n/a | if self.stop_here(frame): |
---|
104 | n/a | # When stepping with next/until/return in a generator frame, skip |
---|
105 | n/a | # the internal StopIteration exception (with no traceback) |
---|
106 | n/a | # triggered by a subiterator run with the 'yield from' statement. |
---|
107 | n/a | if not (frame.f_code.co_flags & CO_GENERATOR |
---|
108 | n/a | and arg[0] is StopIteration and arg[2] is None): |
---|
109 | n/a | self.user_exception(frame, arg) |
---|
110 | n/a | if self.quitting: raise BdbQuit |
---|
111 | n/a | # Stop at the StopIteration or GeneratorExit exception when the user |
---|
112 | n/a | # has set stopframe in a generator by issuing a return command, or a |
---|
113 | n/a | # next/until command at the last statement in the generator before the |
---|
114 | n/a | # exception. |
---|
115 | n/a | elif (self.stopframe and frame is not self.stopframe |
---|
116 | n/a | and self.stopframe.f_code.co_flags & CO_GENERATOR |
---|
117 | n/a | and arg[0] in (StopIteration, GeneratorExit)): |
---|
118 | n/a | self.user_exception(frame, arg) |
---|
119 | n/a | if self.quitting: raise BdbQuit |
---|
120 | n/a | |
---|
121 | n/a | return self.trace_dispatch |
---|
122 | n/a | |
---|
123 | n/a | # Normally derived classes don't override the following |
---|
124 | n/a | # methods, but they may if they want to redefine the |
---|
125 | n/a | # definition of stopping and breakpoints. |
---|
126 | n/a | |
---|
127 | n/a | def is_skipped_module(self, module_name): |
---|
128 | n/a | for pattern in self.skip: |
---|
129 | n/a | if fnmatch.fnmatch(module_name, pattern): |
---|
130 | n/a | return True |
---|
131 | n/a | return False |
---|
132 | n/a | |
---|
133 | n/a | def stop_here(self, frame): |
---|
134 | n/a | # (CT) stopframe may now also be None, see dispatch_call. |
---|
135 | n/a | # (CT) the former test for None is therefore removed from here. |
---|
136 | n/a | if self.skip and \ |
---|
137 | n/a | self.is_skipped_module(frame.f_globals.get('__name__')): |
---|
138 | n/a | return False |
---|
139 | n/a | if frame is self.stopframe: |
---|
140 | n/a | if self.stoplineno == -1: |
---|
141 | n/a | return False |
---|
142 | n/a | return frame.f_lineno >= self.stoplineno |
---|
143 | n/a | if not self.stopframe: |
---|
144 | n/a | return True |
---|
145 | n/a | return False |
---|
146 | n/a | |
---|
147 | n/a | def break_here(self, frame): |
---|
148 | n/a | filename = self.canonic(frame.f_code.co_filename) |
---|
149 | n/a | if filename not in self.breaks: |
---|
150 | n/a | return False |
---|
151 | n/a | lineno = frame.f_lineno |
---|
152 | n/a | if lineno not in self.breaks[filename]: |
---|
153 | n/a | # The line itself has no breakpoint, but maybe the line is the |
---|
154 | n/a | # first line of a function with breakpoint set by function name. |
---|
155 | n/a | lineno = frame.f_code.co_firstlineno |
---|
156 | n/a | if lineno not in self.breaks[filename]: |
---|
157 | n/a | return False |
---|
158 | n/a | |
---|
159 | n/a | # flag says ok to delete temp. bp |
---|
160 | n/a | (bp, flag) = effective(filename, lineno, frame) |
---|
161 | n/a | if bp: |
---|
162 | n/a | self.currentbp = bp.number |
---|
163 | n/a | if (flag and bp.temporary): |
---|
164 | n/a | self.do_clear(str(bp.number)) |
---|
165 | n/a | return True |
---|
166 | n/a | else: |
---|
167 | n/a | return False |
---|
168 | n/a | |
---|
169 | n/a | def do_clear(self, arg): |
---|
170 | n/a | raise NotImplementedError("subclass of bdb must implement do_clear()") |
---|
171 | n/a | |
---|
172 | n/a | def break_anywhere(self, frame): |
---|
173 | n/a | return self.canonic(frame.f_code.co_filename) in self.breaks |
---|
174 | n/a | |
---|
175 | n/a | # Derived classes should override the user_* methods |
---|
176 | n/a | # to gain control. |
---|
177 | n/a | |
---|
178 | n/a | def user_call(self, frame, argument_list): |
---|
179 | n/a | """This method is called when there is the remote possibility |
---|
180 | n/a | that we ever need to stop in this function.""" |
---|
181 | n/a | pass |
---|
182 | n/a | |
---|
183 | n/a | def user_line(self, frame): |
---|
184 | n/a | """This method is called when we stop or break at this line.""" |
---|
185 | n/a | pass |
---|
186 | n/a | |
---|
187 | n/a | def user_return(self, frame, return_value): |
---|
188 | n/a | """This method is called when a return trap is set here.""" |
---|
189 | n/a | pass |
---|
190 | n/a | |
---|
191 | n/a | def user_exception(self, frame, exc_info): |
---|
192 | n/a | """This method is called if an exception occurs, |
---|
193 | n/a | but only if we are to stop at or just below this level.""" |
---|
194 | n/a | pass |
---|
195 | n/a | |
---|
196 | n/a | def _set_stopinfo(self, stopframe, returnframe, stoplineno=0): |
---|
197 | n/a | self.stopframe = stopframe |
---|
198 | n/a | self.returnframe = returnframe |
---|
199 | n/a | self.quitting = False |
---|
200 | n/a | # stoplineno >= 0 means: stop at line >= the stoplineno |
---|
201 | n/a | # stoplineno -1 means: don't stop at all |
---|
202 | n/a | self.stoplineno = stoplineno |
---|
203 | n/a | |
---|
204 | n/a | # Derived classes and clients can call the following methods |
---|
205 | n/a | # to affect the stepping state. |
---|
206 | n/a | |
---|
207 | n/a | def set_until(self, frame, lineno=None): |
---|
208 | n/a | """Stop when the line with the line no greater than the current one is |
---|
209 | n/a | reached or when returning from current frame""" |
---|
210 | n/a | # the name "until" is borrowed from gdb |
---|
211 | n/a | if lineno is None: |
---|
212 | n/a | lineno = frame.f_lineno + 1 |
---|
213 | n/a | self._set_stopinfo(frame, frame, lineno) |
---|
214 | n/a | |
---|
215 | n/a | def set_step(self): |
---|
216 | n/a | """Stop after one line of code.""" |
---|
217 | n/a | # Issue #13183: pdb skips frames after hitting a breakpoint and running |
---|
218 | n/a | # step commands. |
---|
219 | n/a | # Restore the trace function in the caller (that may not have been set |
---|
220 | n/a | # for performance reasons) when returning from the current frame. |
---|
221 | n/a | if self.frame_returning: |
---|
222 | n/a | caller_frame = self.frame_returning.f_back |
---|
223 | n/a | if caller_frame and not caller_frame.f_trace: |
---|
224 | n/a | caller_frame.f_trace = self.trace_dispatch |
---|
225 | n/a | self._set_stopinfo(None, None) |
---|
226 | n/a | |
---|
227 | n/a | def set_next(self, frame): |
---|
228 | n/a | """Stop on the next line in or below the given frame.""" |
---|
229 | n/a | self._set_stopinfo(frame, None) |
---|
230 | n/a | |
---|
231 | n/a | def set_return(self, frame): |
---|
232 | n/a | """Stop when returning from the given frame.""" |
---|
233 | n/a | if frame.f_code.co_flags & CO_GENERATOR: |
---|
234 | n/a | self._set_stopinfo(frame, None, -1) |
---|
235 | n/a | else: |
---|
236 | n/a | self._set_stopinfo(frame.f_back, frame) |
---|
237 | n/a | |
---|
238 | n/a | def set_trace(self, frame=None): |
---|
239 | n/a | """Start debugging from `frame`. |
---|
240 | n/a | |
---|
241 | n/a | If frame is not specified, debugging starts from caller's frame. |
---|
242 | n/a | """ |
---|
243 | n/a | if frame is None: |
---|
244 | n/a | frame = sys._getframe().f_back |
---|
245 | n/a | self.reset() |
---|
246 | n/a | while frame: |
---|
247 | n/a | frame.f_trace = self.trace_dispatch |
---|
248 | n/a | self.botframe = frame |
---|
249 | n/a | frame = frame.f_back |
---|
250 | n/a | self.set_step() |
---|
251 | n/a | sys.settrace(self.trace_dispatch) |
---|
252 | n/a | |
---|
253 | n/a | def set_continue(self): |
---|
254 | n/a | # Don't stop except at breakpoints or when finished |
---|
255 | n/a | self._set_stopinfo(self.botframe, None, -1) |
---|
256 | n/a | if not self.breaks: |
---|
257 | n/a | # no breakpoints; run without debugger overhead |
---|
258 | n/a | sys.settrace(None) |
---|
259 | n/a | frame = sys._getframe().f_back |
---|
260 | n/a | while frame and frame is not self.botframe: |
---|
261 | n/a | del frame.f_trace |
---|
262 | n/a | frame = frame.f_back |
---|
263 | n/a | |
---|
264 | n/a | def set_quit(self): |
---|
265 | n/a | self.stopframe = self.botframe |
---|
266 | n/a | self.returnframe = None |
---|
267 | n/a | self.quitting = True |
---|
268 | n/a | sys.settrace(None) |
---|
269 | n/a | |
---|
270 | n/a | # Derived classes and clients can call the following methods |
---|
271 | n/a | # to manipulate breakpoints. These methods return an |
---|
272 | n/a | # error message is something went wrong, None if all is well. |
---|
273 | n/a | # Set_break prints out the breakpoint line and file:lineno. |
---|
274 | n/a | # Call self.get_*break*() to see the breakpoints or better |
---|
275 | n/a | # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint(). |
---|
276 | n/a | |
---|
277 | n/a | def set_break(self, filename, lineno, temporary=False, cond=None, |
---|
278 | n/a | funcname=None): |
---|
279 | n/a | filename = self.canonic(filename) |
---|
280 | n/a | import linecache # Import as late as possible |
---|
281 | n/a | line = linecache.getline(filename, lineno) |
---|
282 | n/a | if not line: |
---|
283 | n/a | return 'Line %s:%d does not exist' % (filename, lineno) |
---|
284 | n/a | list = self.breaks.setdefault(filename, []) |
---|
285 | n/a | if lineno not in list: |
---|
286 | n/a | list.append(lineno) |
---|
287 | n/a | bp = Breakpoint(filename, lineno, temporary, cond, funcname) |
---|
288 | n/a | |
---|
289 | n/a | def _prune_breaks(self, filename, lineno): |
---|
290 | n/a | if (filename, lineno) not in Breakpoint.bplist: |
---|
291 | n/a | self.breaks[filename].remove(lineno) |
---|
292 | n/a | if not self.breaks[filename]: |
---|
293 | n/a | del self.breaks[filename] |
---|
294 | n/a | |
---|
295 | n/a | def clear_break(self, filename, lineno): |
---|
296 | n/a | filename = self.canonic(filename) |
---|
297 | n/a | if filename not in self.breaks: |
---|
298 | n/a | return 'There are no breakpoints in %s' % filename |
---|
299 | n/a | if lineno not in self.breaks[filename]: |
---|
300 | n/a | return 'There is no breakpoint at %s:%d' % (filename, lineno) |
---|
301 | n/a | # If there's only one bp in the list for that file,line |
---|
302 | n/a | # pair, then remove the breaks entry |
---|
303 | n/a | for bp in Breakpoint.bplist[filename, lineno][:]: |
---|
304 | n/a | bp.deleteMe() |
---|
305 | n/a | self._prune_breaks(filename, lineno) |
---|
306 | n/a | |
---|
307 | n/a | def clear_bpbynumber(self, arg): |
---|
308 | n/a | try: |
---|
309 | n/a | bp = self.get_bpbynumber(arg) |
---|
310 | n/a | except ValueError as err: |
---|
311 | n/a | return str(err) |
---|
312 | n/a | bp.deleteMe() |
---|
313 | n/a | self._prune_breaks(bp.file, bp.line) |
---|
314 | n/a | |
---|
315 | n/a | def clear_all_file_breaks(self, filename): |
---|
316 | n/a | filename = self.canonic(filename) |
---|
317 | n/a | if filename not in self.breaks: |
---|
318 | n/a | return 'There are no breakpoints in %s' % filename |
---|
319 | n/a | for line in self.breaks[filename]: |
---|
320 | n/a | blist = Breakpoint.bplist[filename, line] |
---|
321 | n/a | for bp in blist: |
---|
322 | n/a | bp.deleteMe() |
---|
323 | n/a | del self.breaks[filename] |
---|
324 | n/a | |
---|
325 | n/a | def clear_all_breaks(self): |
---|
326 | n/a | if not self.breaks: |
---|
327 | n/a | return 'There are no breakpoints' |
---|
328 | n/a | for bp in Breakpoint.bpbynumber: |
---|
329 | n/a | if bp: |
---|
330 | n/a | bp.deleteMe() |
---|
331 | n/a | self.breaks = {} |
---|
332 | n/a | |
---|
333 | n/a | def get_bpbynumber(self, arg): |
---|
334 | n/a | if not arg: |
---|
335 | n/a | raise ValueError('Breakpoint number expected') |
---|
336 | n/a | try: |
---|
337 | n/a | number = int(arg) |
---|
338 | n/a | except ValueError: |
---|
339 | n/a | raise ValueError('Non-numeric breakpoint number %s' % arg) |
---|
340 | n/a | try: |
---|
341 | n/a | bp = Breakpoint.bpbynumber[number] |
---|
342 | n/a | except IndexError: |
---|
343 | n/a | raise ValueError('Breakpoint number %d out of range' % number) |
---|
344 | n/a | if bp is None: |
---|
345 | n/a | raise ValueError('Breakpoint %d already deleted' % number) |
---|
346 | n/a | return bp |
---|
347 | n/a | |
---|
348 | n/a | def get_break(self, filename, lineno): |
---|
349 | n/a | filename = self.canonic(filename) |
---|
350 | n/a | return filename in self.breaks and \ |
---|
351 | n/a | lineno in self.breaks[filename] |
---|
352 | n/a | |
---|
353 | n/a | def get_breaks(self, filename, lineno): |
---|
354 | n/a | filename = self.canonic(filename) |
---|
355 | n/a | return filename in self.breaks and \ |
---|
356 | n/a | lineno in self.breaks[filename] and \ |
---|
357 | n/a | Breakpoint.bplist[filename, lineno] or [] |
---|
358 | n/a | |
---|
359 | n/a | def get_file_breaks(self, filename): |
---|
360 | n/a | filename = self.canonic(filename) |
---|
361 | n/a | if filename in self.breaks: |
---|
362 | n/a | return self.breaks[filename] |
---|
363 | n/a | else: |
---|
364 | n/a | return [] |
---|
365 | n/a | |
---|
366 | n/a | def get_all_breaks(self): |
---|
367 | n/a | return self.breaks |
---|
368 | n/a | |
---|
369 | n/a | # Derived classes and clients can call the following method |
---|
370 | n/a | # to get a data structure representing a stack trace. |
---|
371 | n/a | |
---|
372 | n/a | def get_stack(self, f, t): |
---|
373 | n/a | stack = [] |
---|
374 | n/a | if t and t.tb_frame is f: |
---|
375 | n/a | t = t.tb_next |
---|
376 | n/a | while f is not None: |
---|
377 | n/a | stack.append((f, f.f_lineno)) |
---|
378 | n/a | if f is self.botframe: |
---|
379 | n/a | break |
---|
380 | n/a | f = f.f_back |
---|
381 | n/a | stack.reverse() |
---|
382 | n/a | i = max(0, len(stack) - 1) |
---|
383 | n/a | while t is not None: |
---|
384 | n/a | stack.append((t.tb_frame, t.tb_lineno)) |
---|
385 | n/a | t = t.tb_next |
---|
386 | n/a | if f is None: |
---|
387 | n/a | i = max(0, len(stack) - 1) |
---|
388 | n/a | return stack, i |
---|
389 | n/a | |
---|
390 | n/a | def format_stack_entry(self, frame_lineno, lprefix=': '): |
---|
391 | n/a | import linecache, reprlib |
---|
392 | n/a | frame, lineno = frame_lineno |
---|
393 | n/a | filename = self.canonic(frame.f_code.co_filename) |
---|
394 | n/a | s = '%s(%r)' % (filename, lineno) |
---|
395 | n/a | if frame.f_code.co_name: |
---|
396 | n/a | s += frame.f_code.co_name |
---|
397 | n/a | else: |
---|
398 | n/a | s += "<lambda>" |
---|
399 | n/a | if '__args__' in frame.f_locals: |
---|
400 | n/a | args = frame.f_locals['__args__'] |
---|
401 | n/a | else: |
---|
402 | n/a | args = None |
---|
403 | n/a | if args: |
---|
404 | n/a | s += reprlib.repr(args) |
---|
405 | n/a | else: |
---|
406 | n/a | s += '()' |
---|
407 | n/a | if '__return__' in frame.f_locals: |
---|
408 | n/a | rv = frame.f_locals['__return__'] |
---|
409 | n/a | s += '->' |
---|
410 | n/a | s += reprlib.repr(rv) |
---|
411 | n/a | line = linecache.getline(filename, lineno, frame.f_globals) |
---|
412 | n/a | if line: |
---|
413 | n/a | s += lprefix + line.strip() |
---|
414 | n/a | return s |
---|
415 | n/a | |
---|
416 | n/a | # The following methods can be called by clients to use |
---|
417 | n/a | # a debugger to debug a statement or an expression. |
---|
418 | n/a | # Both can be given as a string, or a code object. |
---|
419 | n/a | |
---|
420 | n/a | def run(self, cmd, globals=None, locals=None): |
---|
421 | n/a | if globals is None: |
---|
422 | n/a | import __main__ |
---|
423 | n/a | globals = __main__.__dict__ |
---|
424 | n/a | if locals is None: |
---|
425 | n/a | locals = globals |
---|
426 | n/a | self.reset() |
---|
427 | n/a | if isinstance(cmd, str): |
---|
428 | n/a | cmd = compile(cmd, "<string>", "exec") |
---|
429 | n/a | sys.settrace(self.trace_dispatch) |
---|
430 | n/a | try: |
---|
431 | n/a | exec(cmd, globals, locals) |
---|
432 | n/a | except BdbQuit: |
---|
433 | n/a | pass |
---|
434 | n/a | finally: |
---|
435 | n/a | self.quitting = True |
---|
436 | n/a | sys.settrace(None) |
---|
437 | n/a | |
---|
438 | n/a | def runeval(self, expr, globals=None, locals=None): |
---|
439 | n/a | if globals is None: |
---|
440 | n/a | import __main__ |
---|
441 | n/a | globals = __main__.__dict__ |
---|
442 | n/a | if locals is None: |
---|
443 | n/a | locals = globals |
---|
444 | n/a | self.reset() |
---|
445 | n/a | sys.settrace(self.trace_dispatch) |
---|
446 | n/a | try: |
---|
447 | n/a | return eval(expr, globals, locals) |
---|
448 | n/a | except BdbQuit: |
---|
449 | n/a | pass |
---|
450 | n/a | finally: |
---|
451 | n/a | self.quitting = True |
---|
452 | n/a | sys.settrace(None) |
---|
453 | n/a | |
---|
454 | n/a | def runctx(self, cmd, globals, locals): |
---|
455 | n/a | # B/W compatibility |
---|
456 | n/a | self.run(cmd, globals, locals) |
---|
457 | n/a | |
---|
458 | n/a | # This method is more useful to debug a single function call. |
---|
459 | n/a | |
---|
460 | n/a | def runcall(self, func, *args, **kwds): |
---|
461 | n/a | self.reset() |
---|
462 | n/a | sys.settrace(self.trace_dispatch) |
---|
463 | n/a | res = None |
---|
464 | n/a | try: |
---|
465 | n/a | res = func(*args, **kwds) |
---|
466 | n/a | except BdbQuit: |
---|
467 | n/a | pass |
---|
468 | n/a | finally: |
---|
469 | n/a | self.quitting = True |
---|
470 | n/a | sys.settrace(None) |
---|
471 | n/a | return res |
---|
472 | n/a | |
---|
473 | n/a | |
---|
474 | n/a | def set_trace(): |
---|
475 | n/a | Bdb().set_trace() |
---|
476 | n/a | |
---|
477 | n/a | |
---|
478 | n/a | class Breakpoint: |
---|
479 | n/a | """Breakpoint class. |
---|
480 | n/a | |
---|
481 | n/a | Implements temporary breakpoints, ignore counts, disabling and |
---|
482 | n/a | (re)-enabling, and conditionals. |
---|
483 | n/a | |
---|
484 | n/a | Breakpoints are indexed by number through bpbynumber and by |
---|
485 | n/a | the file,line tuple using bplist. The former points to a |
---|
486 | n/a | single instance of class Breakpoint. The latter points to a |
---|
487 | n/a | list of such instances since there may be more than one |
---|
488 | n/a | breakpoint per line. |
---|
489 | n/a | |
---|
490 | n/a | """ |
---|
491 | n/a | |
---|
492 | n/a | # XXX Keeping state in the class is a mistake -- this means |
---|
493 | n/a | # you cannot have more than one active Bdb instance. |
---|
494 | n/a | |
---|
495 | n/a | next = 1 # Next bp to be assigned |
---|
496 | n/a | bplist = {} # indexed by (file, lineno) tuple |
---|
497 | n/a | bpbynumber = [None] # Each entry is None or an instance of Bpt |
---|
498 | n/a | # index 0 is unused, except for marking an |
---|
499 | n/a | # effective break .... see effective() |
---|
500 | n/a | |
---|
501 | n/a | def __init__(self, file, line, temporary=False, cond=None, funcname=None): |
---|
502 | n/a | self.funcname = funcname |
---|
503 | n/a | # Needed if funcname is not None. |
---|
504 | n/a | self.func_first_executable_line = None |
---|
505 | n/a | self.file = file # This better be in canonical form! |
---|
506 | n/a | self.line = line |
---|
507 | n/a | self.temporary = temporary |
---|
508 | n/a | self.cond = cond |
---|
509 | n/a | self.enabled = True |
---|
510 | n/a | self.ignore = 0 |
---|
511 | n/a | self.hits = 0 |
---|
512 | n/a | self.number = Breakpoint.next |
---|
513 | n/a | Breakpoint.next += 1 |
---|
514 | n/a | # Build the two lists |
---|
515 | n/a | self.bpbynumber.append(self) |
---|
516 | n/a | if (file, line) in self.bplist: |
---|
517 | n/a | self.bplist[file, line].append(self) |
---|
518 | n/a | else: |
---|
519 | n/a | self.bplist[file, line] = [self] |
---|
520 | n/a | |
---|
521 | n/a | def deleteMe(self): |
---|
522 | n/a | index = (self.file, self.line) |
---|
523 | n/a | self.bpbynumber[self.number] = None # No longer in list |
---|
524 | n/a | self.bplist[index].remove(self) |
---|
525 | n/a | if not self.bplist[index]: |
---|
526 | n/a | # No more bp for this f:l combo |
---|
527 | n/a | del self.bplist[index] |
---|
528 | n/a | |
---|
529 | n/a | def enable(self): |
---|
530 | n/a | self.enabled = True |
---|
531 | n/a | |
---|
532 | n/a | def disable(self): |
---|
533 | n/a | self.enabled = False |
---|
534 | n/a | |
---|
535 | n/a | def bpprint(self, out=None): |
---|
536 | n/a | if out is None: |
---|
537 | n/a | out = sys.stdout |
---|
538 | n/a | print(self.bpformat(), file=out) |
---|
539 | n/a | |
---|
540 | n/a | def bpformat(self): |
---|
541 | n/a | if self.temporary: |
---|
542 | n/a | disp = 'del ' |
---|
543 | n/a | else: |
---|
544 | n/a | disp = 'keep ' |
---|
545 | n/a | if self.enabled: |
---|
546 | n/a | disp = disp + 'yes ' |
---|
547 | n/a | else: |
---|
548 | n/a | disp = disp + 'no ' |
---|
549 | n/a | ret = '%-4dbreakpoint %s at %s:%d' % (self.number, disp, |
---|
550 | n/a | self.file, self.line) |
---|
551 | n/a | if self.cond: |
---|
552 | n/a | ret += '\n\tstop only if %s' % (self.cond,) |
---|
553 | n/a | if self.ignore: |
---|
554 | n/a | ret += '\n\tignore next %d hits' % (self.ignore,) |
---|
555 | n/a | if self.hits: |
---|
556 | n/a | if self.hits > 1: |
---|
557 | n/a | ss = 's' |
---|
558 | n/a | else: |
---|
559 | n/a | ss = '' |
---|
560 | n/a | ret += '\n\tbreakpoint already hit %d time%s' % (self.hits, ss) |
---|
561 | n/a | return ret |
---|
562 | n/a | |
---|
563 | n/a | def __str__(self): |
---|
564 | n/a | return 'breakpoint %s at %s:%s' % (self.number, self.file, self.line) |
---|
565 | n/a | |
---|
566 | n/a | # -----------end of Breakpoint class---------- |
---|
567 | n/a | |
---|
568 | n/a | def checkfuncname(b, frame): |
---|
569 | n/a | """Check whether we should break here because of `b.funcname`.""" |
---|
570 | n/a | if not b.funcname: |
---|
571 | n/a | # Breakpoint was set via line number. |
---|
572 | n/a | if b.line != frame.f_lineno: |
---|
573 | n/a | # Breakpoint was set at a line with a def statement and the function |
---|
574 | n/a | # defined is called: don't break. |
---|
575 | n/a | return False |
---|
576 | n/a | return True |
---|
577 | n/a | |
---|
578 | n/a | # Breakpoint set via function name. |
---|
579 | n/a | |
---|
580 | n/a | if frame.f_code.co_name != b.funcname: |
---|
581 | n/a | # It's not a function call, but rather execution of def statement. |
---|
582 | n/a | return False |
---|
583 | n/a | |
---|
584 | n/a | # We are in the right frame. |
---|
585 | n/a | if not b.func_first_executable_line: |
---|
586 | n/a | # The function is entered for the 1st time. |
---|
587 | n/a | b.func_first_executable_line = frame.f_lineno |
---|
588 | n/a | |
---|
589 | n/a | if b.func_first_executable_line != frame.f_lineno: |
---|
590 | n/a | # But we are not at the first line number: don't break. |
---|
591 | n/a | return False |
---|
592 | n/a | return True |
---|
593 | n/a | |
---|
594 | n/a | # Determines if there is an effective (active) breakpoint at this |
---|
595 | n/a | # line of code. Returns breakpoint number or 0 if none |
---|
596 | n/a | def effective(file, line, frame): |
---|
597 | n/a | """Determine which breakpoint for this file:line is to be acted upon. |
---|
598 | n/a | |
---|
599 | n/a | Called only if we know there is a bpt at this |
---|
600 | n/a | location. Returns breakpoint that was triggered and a flag |
---|
601 | n/a | that indicates if it is ok to delete a temporary bp. |
---|
602 | n/a | |
---|
603 | n/a | """ |
---|
604 | n/a | possibles = Breakpoint.bplist[file, line] |
---|
605 | n/a | for b in possibles: |
---|
606 | n/a | if not b.enabled: |
---|
607 | n/a | continue |
---|
608 | n/a | if not checkfuncname(b, frame): |
---|
609 | n/a | continue |
---|
610 | n/a | # Count every hit when bp is enabled |
---|
611 | n/a | b.hits += 1 |
---|
612 | n/a | if not b.cond: |
---|
613 | n/a | # If unconditional, and ignoring go on to next, else break |
---|
614 | n/a | if b.ignore > 0: |
---|
615 | n/a | b.ignore -= 1 |
---|
616 | n/a | continue |
---|
617 | n/a | else: |
---|
618 | n/a | # breakpoint and marker that it's ok to delete if temporary |
---|
619 | n/a | return (b, True) |
---|
620 | n/a | else: |
---|
621 | n/a | # Conditional bp. |
---|
622 | n/a | # Ignore count applies only to those bpt hits where the |
---|
623 | n/a | # condition evaluates to true. |
---|
624 | n/a | try: |
---|
625 | n/a | val = eval(b.cond, frame.f_globals, frame.f_locals) |
---|
626 | n/a | if val: |
---|
627 | n/a | if b.ignore > 0: |
---|
628 | n/a | b.ignore -= 1 |
---|
629 | n/a | # continue |
---|
630 | n/a | else: |
---|
631 | n/a | return (b, True) |
---|
632 | n/a | # else: |
---|
633 | n/a | # continue |
---|
634 | n/a | except: |
---|
635 | n/a | # if eval fails, most conservative thing is to stop on |
---|
636 | n/a | # breakpoint regardless of ignore count. Don't delete |
---|
637 | n/a | # temporary, as another hint to user. |
---|
638 | n/a | return (b, False) |
---|
639 | n/a | return (None, None) |
---|
640 | n/a | |
---|
641 | n/a | |
---|
642 | n/a | # -------------------- testing -------------------- |
---|
643 | n/a | |
---|
644 | n/a | class Tdb(Bdb): |
---|
645 | n/a | def user_call(self, frame, args): |
---|
646 | n/a | name = frame.f_code.co_name |
---|
647 | n/a | if not name: name = '???' |
---|
648 | n/a | print('+++ call', name, args) |
---|
649 | n/a | def user_line(self, frame): |
---|
650 | n/a | import linecache |
---|
651 | n/a | name = frame.f_code.co_name |
---|
652 | n/a | if not name: name = '???' |
---|
653 | n/a | fn = self.canonic(frame.f_code.co_filename) |
---|
654 | n/a | line = linecache.getline(fn, frame.f_lineno, frame.f_globals) |
---|
655 | n/a | print('+++', fn, frame.f_lineno, name, ':', line.strip()) |
---|
656 | n/a | def user_return(self, frame, retval): |
---|
657 | n/a | print('+++ return', retval) |
---|
658 | n/a | def user_exception(self, frame, exc_stuff): |
---|
659 | n/a | print('+++ exception', exc_stuff) |
---|
660 | n/a | self.set_continue() |
---|
661 | n/a | |
---|
662 | n/a | def foo(n): |
---|
663 | n/a | print('foo(', n, ')') |
---|
664 | n/a | x = bar(n*10) |
---|
665 | n/a | print('bar returned', x) |
---|
666 | n/a | |
---|
667 | n/a | def bar(a): |
---|
668 | n/a | print('bar(', a, ')') |
---|
669 | n/a | return a/2 |
---|
670 | n/a | |
---|
671 | n/a | def test(): |
---|
672 | n/a | t = Tdb() |
---|
673 | n/a | t.run('import bdb; bdb.foo(10)') |
---|