PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/python/helpers/pydev/_pydevd_bundle/pydevd_frame.py

http://github.com/JetBrains/intellij-community
Python | 922 lines | 624 code | 119 blank | 179 comment | 254 complexity | 6f32c2bac5f306682cbf227e763e5981 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. import linecache
  2. import os.path
  3. import re
  4. import sys
  5. import traceback # @Reimport
  6. # IFDEF CYTHON
  7. # import dis
  8. # ENDIF
  9. from _pydev_bundle import pydev_log
  10. from _pydevd_bundle import pydevd_dont_trace
  11. from _pydevd_bundle import pydevd_vars
  12. from _pydevd_bundle.pydevd_breakpoints import get_exception_breakpoint
  13. from _pydevd_bundle.pydevd_comm_constants import (CMD_STEP_CAUGHT_EXCEPTION, CMD_STEP_RETURN, CMD_STEP_OVER, CMD_SET_BREAK,
  14. CMD_STEP_INTO, CMD_SMART_STEP_INTO, CMD_STEP_INTO_MY_CODE, CMD_STEP_INTO_COROUTINE)
  15. from _pydevd_bundle.pydevd_constants import STATE_SUSPEND, get_current_thread_id, STATE_RUN, dict_iter_values, IS_PY3K, \
  16. dict_keys, RETURN_VALUES_DICT, NO_FTRACE, IS_CPYTHON
  17. from _pydevd_bundle.pydevd_dont_trace_files import DONT_TRACE, PYDEV_FILE
  18. from _pydevd_bundle.pydevd_frame_utils import add_exception_to_frame, just_raised, remove_exception_from_frame, ignore_exception_trace
  19. from _pydevd_bundle.pydevd_bytecode_utils import find_last_call_name, find_last_func_call_order
  20. from _pydevd_bundle.pydevd_utils import get_clsname_for_code
  21. from pydevd_file_utils import get_abs_path_real_path_and_base_from_frame, is_real_file
  22. try:
  23. from inspect import CO_GENERATOR
  24. except:
  25. CO_GENERATOR = 0
  26. from _pydevd_bundle.pydevd_constants import IS_PY2
  27. try:
  28. from _pydevd_bundle.pydevd_signature import send_signature_call_trace, send_signature_return_trace
  29. except ImportError:
  30. def send_signature_call_trace(*args, **kwargs):
  31. pass
  32. basename = os.path.basename
  33. IGNORE_EXCEPTION_TAG = re.compile('[^#]*#.*@IgnoreException')
  34. DEBUG_START = ('pydevd.py', '_exec')
  35. DEBUG_START_PY3K = ('_pydev_execfile.py', 'execfile')
  36. TRACE_PROPERTY = 'pydevd_traceproperty.py'
  37. get_file_type = DONT_TRACE.get
  38. def handle_breakpoint_condition(py_db, info, breakpoint, new_frame):
  39. condition = breakpoint.condition
  40. try:
  41. if breakpoint.handle_hit_condition(new_frame):
  42. return True
  43. if condition is None:
  44. return False
  45. return eval(condition, new_frame.f_globals, new_frame.f_locals)
  46. except Exception as e:
  47. if IS_PY2:
  48. # Must be bytes on py2.
  49. if isinstance(condition, unicode):
  50. condition = condition.encode('utf-8')
  51. if not isinstance(e, py_db.skip_print_breakpoint_exception):
  52. sys.stderr.write('Error while evaluating expression: %s\n' % (condition,))
  53. etype, value, tb = sys.exc_info()
  54. traceback.print_exception(etype, value, tb.tb_next)
  55. if not isinstance(e, py_db.skip_suspend_on_breakpoint_exception):
  56. try:
  57. # add exception_type and stacktrace into thread additional info
  58. etype, value, tb = sys.exc_info()
  59. error = ''.join(traceback.format_exception_only(etype, value))
  60. stack = traceback.extract_stack(f=tb.tb_frame.f_back)
  61. # On self.set_suspend(thread, CMD_SET_BREAK) this info will be
  62. # sent to the client.
  63. info.conditional_breakpoint_exception = \
  64. ('Condition:\n' + condition + '\n\nError:\n' + error, stack)
  65. except:
  66. traceback.print_exc()
  67. return True
  68. return False
  69. finally:
  70. etype, value, tb = None, None, None
  71. def handle_breakpoint_expression(breakpoint, info, new_frame):
  72. try:
  73. try:
  74. val = eval(breakpoint.expression, new_frame.f_globals, new_frame.f_locals)
  75. except:
  76. val = sys.exc_info()[1]
  77. finally:
  78. if val is not None:
  79. info.pydev_message = str(val)
  80. #=======================================================================================================================
  81. # PyDBFrame
  82. #=======================================================================================================================
  83. # IFDEF CYTHON
  84. # cdef class PyDBFrame:
  85. # ELSE
  86. class PyDBFrame:
  87. '''This makes the tracing for a given frame, so, the trace_dispatch
  88. is used initially when we enter into a new context ('call') and then
  89. is reused for the entire context.
  90. '''
  91. # ENDIF
  92. # Note: class (and not instance) attributes.
  93. # Same thing in the main debugger but only considering the file contents, while the one in the main debugger
  94. # considers the user input (so, the actual result must be a join of both).
  95. filename_to_lines_where_exceptions_are_ignored = {}
  96. filename_to_stat_info = {}
  97. # IFDEF CYTHON
  98. # cdef tuple _args
  99. # cdef int should_skip
  100. # cdef int _bytecode_offset
  101. # cdef list _instructions
  102. # def __init__(self, tuple args):
  103. # self._args = args # In the cython version we don't need to pass the frame
  104. # self.should_skip = -1 # On cythonized version, put in instance.
  105. # self._bytecode_offset = 0
  106. # self._instructions = None
  107. # ELSE
  108. should_skip = -1 # Default value in class (put in instance on set).
  109. def __init__(self, args):
  110. # args = main_debugger, filename, base, info, t, frame
  111. # yeap, much faster than putting in self and then getting it from self later on
  112. self._args = args
  113. self._bytecode_offset = 0
  114. # ENDIF
  115. def set_suspend(self, *args, **kwargs):
  116. self._args[0].set_suspend(*args, **kwargs)
  117. def do_wait_suspend(self, *args, **kwargs):
  118. self._args[0].do_wait_suspend(*args, **kwargs)
  119. # IFDEF CYTHON
  120. # def trace_exception(self, frame, str event, arg):
  121. # cdef bint should_stop;
  122. # ELSE
  123. def trace_exception(self, frame, event, arg):
  124. # ENDIF
  125. if event == 'exception':
  126. should_stop, frame = self.should_stop_on_exception(frame, event, arg)
  127. if should_stop:
  128. self.handle_exception(frame, event, arg)
  129. return self.trace_dispatch
  130. return self.trace_exception
  131. def trace_return(self, frame, event, arg):
  132. if event == 'return':
  133. main_debugger, filename = self._args[0], self._args[1]
  134. send_signature_return_trace(main_debugger, frame, filename, arg)
  135. return self.trace_return
  136. # IFDEF CYTHON
  137. # def should_stop_on_exception(self, frame, str event, arg):
  138. # cdef PyDBAdditionalThreadInfo info;
  139. # cdef bint flag;
  140. # ELSE
  141. def should_stop_on_exception(self, frame, event, arg):
  142. # ENDIF
  143. # main_debugger, _filename, info, _thread = self._args
  144. main_debugger = self._args[0]
  145. info = self._args[2]
  146. should_stop = False
  147. # STATE_SUSPEND = 2
  148. if info.pydev_state != 2: # and breakpoint is not None:
  149. exception, value, trace = arg
  150. if trace is not None and hasattr(trace, 'tb_next'):
  151. # on jython trace is None on the first event and it may not have a tb_next.
  152. exception_breakpoint = get_exception_breakpoint(
  153. exception, main_debugger.break_on_caught_exceptions)
  154. is_real = is_real_file(frame.f_code.co_filename)
  155. if exception_breakpoint is not None:
  156. if exception_breakpoint.condition is not None:
  157. # Always add exception to frame (must remove later after we proceed).
  158. add_exception_to_frame(frame, (exception, value, trace))
  159. eval_result = handle_breakpoint_condition(main_debugger, info, exception_breakpoint, frame)
  160. remove_exception_from_frame(frame)
  161. if not eval_result:
  162. return False, frame
  163. if exception_breakpoint.ignore_libraries:
  164. if not main_debugger.is_exception_trace_in_project_scope(trace):
  165. return False, frame
  166. if ignore_exception_trace(trace):
  167. return False, frame
  168. was_just_raised = just_raised(trace)
  169. if was_just_raised:
  170. if main_debugger.skip_on_exceptions_thrown_in_same_context:
  171. # Option: Don't break if an exception is caught in the same function from which it is thrown
  172. return False, frame
  173. if exception_breakpoint.notify_on_first_raise_only:
  174. if main_debugger.skip_on_exceptions_thrown_in_same_context:
  175. # In this case we never stop if it was just raised, so, to know if it was the first we
  176. # need to check if we're in the 2nd method.
  177. if not was_just_raised and not just_raised(trace.tb_next):
  178. return False, frame # I.e.: we stop only when we're at the caller of a method that throws an exception
  179. else:
  180. if not was_just_raised and not main_debugger.is_top_level_trace_in_project_scope(trace):
  181. return False, frame # I.e.: we stop only when it was just raised
  182. # If it got here we should stop.
  183. should_stop = True
  184. try:
  185. info.pydev_message = exception_breakpoint.qname
  186. except:
  187. info.pydev_message = exception_breakpoint.qname.encode('utf-8')
  188. # Always add exception to frame (must remove later after we proceed).
  189. add_exception_to_frame(frame, (exception, value, trace))
  190. info.pydev_message = "python-%s" % info.pydev_message
  191. else:
  192. # No regular exception breakpoint, let's see if some plugin handles it.
  193. try:
  194. if main_debugger.plugin is not None:
  195. result = main_debugger.plugin.exception_break(main_debugger, self, frame, self._args, arg)
  196. if result:
  197. should_stop, frame = result
  198. except:
  199. should_stop = False
  200. if should_stop:
  201. if exception_breakpoint is not None and exception_breakpoint.expression is not None:
  202. handle_breakpoint_expression(exception_breakpoint, info, frame)
  203. return should_stop, frame
  204. def handle_exception(self, frame, event, arg):
  205. try:
  206. # We have 3 things in arg: exception type, description, traceback object
  207. trace_obj = arg[2]
  208. main_debugger = self._args[0]
  209. initial_trace_obj = trace_obj
  210. if trace_obj.tb_next is None and trace_obj.tb_frame is frame:
  211. # I.e.: tb_next should be only None in the context it was thrown (trace_obj.tb_frame is frame is just a double check).
  212. pass
  213. else:
  214. # Get the trace_obj from where the exception was raised...
  215. while trace_obj.tb_next is not None:
  216. trace_obj = trace_obj.tb_next
  217. if main_debugger.ignore_exceptions_thrown_in_lines_with_ignore_exception:
  218. for check_trace_obj in (initial_trace_obj, trace_obj):
  219. filename = get_abs_path_real_path_and_base_from_frame(check_trace_obj.tb_frame)[1]
  220. filename_to_lines_where_exceptions_are_ignored = self.filename_to_lines_where_exceptions_are_ignored
  221. lines_ignored = filename_to_lines_where_exceptions_are_ignored.get(filename)
  222. if lines_ignored is None:
  223. lines_ignored = filename_to_lines_where_exceptions_are_ignored[filename] = {}
  224. try:
  225. curr_stat = os.stat(filename)
  226. curr_stat = (curr_stat.st_size, curr_stat.st_mtime)
  227. except:
  228. curr_stat = None
  229. last_stat = self.filename_to_stat_info.get(filename)
  230. if last_stat != curr_stat:
  231. self.filename_to_stat_info[filename] = curr_stat
  232. lines_ignored.clear()
  233. try:
  234. linecache.checkcache(filename)
  235. except:
  236. # Jython 2.1
  237. linecache.checkcache()
  238. from_user_input = main_debugger.filename_to_lines_where_exceptions_are_ignored.get(filename)
  239. if from_user_input:
  240. merged = {}
  241. merged.update(lines_ignored)
  242. # Override what we have with the related entries that the user entered
  243. merged.update(from_user_input)
  244. else:
  245. merged = lines_ignored
  246. exc_lineno = check_trace_obj.tb_lineno
  247. # print ('lines ignored', lines_ignored)
  248. # print ('user input', from_user_input)
  249. # print ('merged', merged, 'curr', exc_lineno)
  250. if exc_lineno not in merged: # Note: check on merged but update lines_ignored.
  251. try:
  252. line = linecache.getline(filename, exc_lineno, check_trace_obj.tb_frame.f_globals)
  253. except:
  254. # Jython 2.1
  255. line = linecache.getline(filename, exc_lineno)
  256. if IGNORE_EXCEPTION_TAG.match(line) is not None:
  257. lines_ignored[exc_lineno] = 1
  258. return
  259. else:
  260. # Put in the cache saying not to ignore
  261. lines_ignored[exc_lineno] = 0
  262. else:
  263. # Ok, dict has it already cached, so, let's check it...
  264. if merged.get(exc_lineno, 0):
  265. return
  266. thread = self._args[3]
  267. try:
  268. frame_id_to_frame = {}
  269. frame_id_to_frame[id(frame)] = frame
  270. f = trace_obj.tb_frame
  271. while f is not None:
  272. frame_id_to_frame[id(f)] = f
  273. f = f.f_back
  274. f = None
  275. thread_id = get_current_thread_id(thread)
  276. pydevd_vars.add_additional_frame_by_id(thread_id, frame_id_to_frame)
  277. try:
  278. main_debugger.send_caught_exception_stack(thread, arg, id(frame))
  279. self.set_suspend(thread, CMD_STEP_CAUGHT_EXCEPTION)
  280. self.do_wait_suspend(thread, frame, event, arg)
  281. main_debugger.send_caught_exception_stack_proceeded(thread)
  282. finally:
  283. pydevd_vars.remove_additional_frame_by_id(thread_id)
  284. except KeyboardInterrupt as e:
  285. raise e
  286. except:
  287. traceback.print_exc()
  288. main_debugger.set_trace_for_frame_and_parents(frame)
  289. finally:
  290. # Make sure the user cannot see the '__exception__' we added after we leave the suspend state.
  291. remove_exception_from_frame(frame)
  292. # Clear some local variables...
  293. frame = None
  294. trace_obj = None
  295. initial_trace_obj = None
  296. check_trace_obj = None
  297. f = None
  298. frame_id_to_frame = None
  299. main_debugger = None
  300. thread = None
  301. def get_func_name(self, frame):
  302. code_obj = frame.f_code
  303. func_name = code_obj.co_name
  304. try:
  305. cls_name = get_clsname_for_code(code_obj, frame)
  306. if cls_name is not None:
  307. return "%s.%s" % (cls_name, func_name)
  308. else:
  309. return func_name
  310. except:
  311. traceback.print_exc()
  312. return func_name
  313. def manage_return_values(self, main_debugger, frame, event, arg):
  314. def get_func_name(frame):
  315. code_obj = frame.f_code
  316. func_name = code_obj.co_name
  317. try:
  318. cls_name = get_clsname_for_code(code_obj, frame)
  319. if cls_name is not None:
  320. return "%s.%s" % (cls_name, func_name)
  321. else:
  322. return func_name
  323. except:
  324. traceback.print_exc()
  325. return func_name
  326. try:
  327. if main_debugger.show_return_values:
  328. if event == "return" and hasattr(frame, "f_code") and hasattr(frame.f_code, "co_name"):
  329. if hasattr(frame, "f_back") and hasattr(frame.f_back, "f_locals"):
  330. if RETURN_VALUES_DICT not in dict_keys(frame.f_back.f_locals):
  331. frame.f_back.f_locals[RETURN_VALUES_DICT] = {}
  332. name = get_func_name(frame)
  333. frame.f_back.f_locals[RETURN_VALUES_DICT][name] = arg
  334. if main_debugger.remove_return_values_flag:
  335. # Showing return values was turned off, we should remove them from locals dict.
  336. # The values can be in the current frame or in the back one
  337. if RETURN_VALUES_DICT in dict_keys(frame.f_locals):
  338. frame.f_locals.pop(RETURN_VALUES_DICT)
  339. if hasattr(frame, "f_back") and hasattr(frame.f_back, "f_locals"):
  340. if RETURN_VALUES_DICT in dict_keys(frame.f_back.f_locals):
  341. frame.f_back.f_locals.pop(RETURN_VALUES_DICT)
  342. main_debugger.remove_return_values_flag = False
  343. except:
  344. main_debugger.remove_return_values_flag = False
  345. traceback.print_exc()
  346. def clear_run_state(self, info):
  347. info.pydev_step_stop = None
  348. info.pydev_step_cmd = -1
  349. info.pydev_state = STATE_RUN
  350. # IFDEF CYTHON
  351. # cpdef trace_dispatch(self, frame, str event, arg):
  352. # cdef str filename;
  353. # cdef bint is_exception_event;
  354. # cdef bint has_exception_breakpoints;
  355. # cdef bint can_skip;
  356. # cdef PyDBAdditionalThreadInfo info;
  357. # cdef int step_cmd;
  358. # cdef int line;
  359. # cdef bint is_line;
  360. # cdef bint is_call;
  361. # cdef bint is_return;
  362. # cdef bint should_stop;
  363. # cdef dict breakpoints_for_file;
  364. # cdef str curr_func_name;
  365. # cdef bint exist_result;
  366. # cdef dict frame_skips_cache;
  367. # cdef tuple frame_cache_key;
  368. # cdef tuple line_cache_key;
  369. # cdef int breakpoints_in_line_cache;
  370. # cdef int breakpoints_in_frame_cache;
  371. # cdef bint has_breakpoint_in_frame;
  372. # cdef bint need_trace_return;
  373. # ELSE
  374. def trace_dispatch(self, frame, event, arg):
  375. # ENDIF
  376. main_debugger, filename, info, thread, frame_skips_cache, frame_cache_key = self._args
  377. # print('frame trace_dispatch %s %s %s %s %s' % (frame.f_lineno, frame.f_code.co_name, frame.f_code.co_filename, event, info.pydev_step_cmd))
  378. # The thread can be already suspended by another function, e.g. built-in breakpoint hook.
  379. if info.is_tracing:
  380. return None
  381. try:
  382. info.is_tracing = True
  383. line = frame.f_lineno
  384. line_cache_key = (frame_cache_key, line)
  385. if main_debugger._finish_debugging_session:
  386. if event != 'call': frame.f_trace = NO_FTRACE
  387. return None
  388. # IFDEF CYTHON
  389. # if event == 'opcode':
  390. # instructions = self._get_instructions(frame)
  391. # for i, inst in enumerate(instructions):
  392. # if inst.offset == frame.f_lasti:
  393. # opname, arg, argval = inst.opname, inst.arg, str(inst.argval)
  394. # print('frame trace_dispatch %s %s %s %s %s %s %s %s' % (frame.f_lineno, frame.f_lasti, frame.f_code.co_name,
  395. # frame.f_code.co_filename, event, opname, arg, argval))
  396. # try:
  397. # self._bytecode_offset = instructions[i + 1].offset
  398. # except IndexError:
  399. # break
  400. # return self.trace_dispatch
  401. # ENDIF
  402. plugin_manager = main_debugger.plugin
  403. is_exception_event = event == 'exception'
  404. has_exception_breakpoints = main_debugger.break_on_caught_exceptions or main_debugger.has_plugin_exception_breaks
  405. if is_exception_event:
  406. if has_exception_breakpoints:
  407. should_stop, frame = self.should_stop_on_exception(frame, event, arg)
  408. if should_stop:
  409. self.handle_exception(frame, event, arg)
  410. # No need to reset frame.f_trace to keep the same trace function.
  411. return self.trace_dispatch
  412. is_line = False
  413. is_return = False
  414. is_call = False
  415. else:
  416. is_line = event == 'line'
  417. is_return = event == 'return'
  418. is_call = event == 'call'
  419. if not is_line and not is_return and not is_call:
  420. # Unexpected: just keep the same trace func.
  421. # No need to reset frame.f_trace to keep the same trace function.
  422. return self.trace_dispatch
  423. need_signature_trace_return = False
  424. if main_debugger.signature_factory is not None:
  425. if is_call:
  426. need_signature_trace_return = send_signature_call_trace(main_debugger, frame, filename)
  427. elif is_return:
  428. send_signature_return_trace(main_debugger, frame, filename, arg)
  429. stop_frame = info.pydev_step_stop
  430. step_cmd = info.pydev_step_cmd
  431. is_generator_or_coroutime = frame.f_code.co_flags & 0xa0 # 0xa0 == CO_GENERATOR = 0x20 | CO_COROUTINE = 0x80
  432. breakpoints_for_file = main_debugger.breakpoints.get(filename)
  433. if not is_exception_event:
  434. if is_generator_or_coroutime:
  435. if is_return:
  436. # Dealing with coroutines and generators:
  437. # When in a coroutine we change the perceived event to the debugger because
  438. # a call, StopIteration exception and return are usually just pausing/unpausing it.
  439. returns_cache_key = (frame_cache_key, 'returns')
  440. return_lines = frame_skips_cache.get(returns_cache_key)
  441. if return_lines is None:
  442. # Note: we're collecting the return lines by inspecting the bytecode as
  443. # there are multiple returns and multiple stop iterations when awaiting and
  444. # it doesn't give any clear indication when a coroutine or generator is
  445. # finishing or just pausing.
  446. return_lines = set()
  447. for x in main_debugger.collect_return_info(frame.f_code):
  448. # Note: cython does not support closures in cpdefs (so we can't use
  449. # a list comprehension).
  450. return_lines.add(x.return_line)
  451. frame_skips_cache[returns_cache_key] = return_lines
  452. if line not in return_lines:
  453. # Not really a return (coroutine/generator paused).
  454. return self.trace_dispatch
  455. elif is_call:
  456. # Don't stop when calling coroutines, we will on other event anyway if necessary.
  457. return self.trace_dispatch
  458. can_skip = False
  459. if info.pydev_state == 1: # STATE_RUN = 1
  460. # we can skip if:
  461. # - we have no stop marked
  462. # - we should make a step return/step over and we're not in the current frame
  463. # CMD_STEP_RETURN = 109, CMD_STEP_OVER = 108
  464. can_skip = (step_cmd == -1 and stop_frame is None) \
  465. or (step_cmd in (109, 108) and stop_frame is not frame)
  466. if can_skip:
  467. if plugin_manager is not None and main_debugger.has_plugin_line_breaks:
  468. can_skip = not plugin_manager.can_not_skip(main_debugger, self, frame, info)
  469. # CMD_STEP_OVER = 108
  470. if can_skip and main_debugger.show_return_values and info.pydev_step_cmd == 108 and frame.f_back is info.pydev_step_stop:
  471. # trace function for showing return values after step over
  472. can_skip = False
  473. # Let's check to see if we are in a function that has a breakpoint. If we don't have a breakpoint,
  474. # we will return nothing for the next trace
  475. # also, after we hit a breakpoint and go to some other debugging state, we have to force the set trace anyway,
  476. # so, that's why the additional checks are there.
  477. if not breakpoints_for_file:
  478. if can_skip:
  479. if has_exception_breakpoints:
  480. frame.f_trace = self.trace_exception
  481. return self.trace_exception
  482. else:
  483. if need_signature_trace_return:
  484. frame.f_trace = self.trace_return
  485. return self.trace_return
  486. else:
  487. if not is_call: frame.f_trace = NO_FTRACE
  488. return None
  489. else:
  490. # When cached, 0 means we don't have a breakpoint and 1 means we have.
  491. if can_skip:
  492. breakpoints_in_line_cache = frame_skips_cache.get(line_cache_key, -1)
  493. if breakpoints_in_line_cache == 0:
  494. # No need to reset frame.f_trace to keep the same trace function.
  495. return self.trace_dispatch
  496. breakpoints_in_frame_cache = frame_skips_cache.get(frame_cache_key, -1)
  497. if breakpoints_in_frame_cache != -1:
  498. # Gotten from cache.
  499. has_breakpoint_in_frame = breakpoints_in_frame_cache == 1
  500. else:
  501. has_breakpoint_in_frame = False
  502. # Checks the breakpoint to see if there is a context match in some function
  503. curr_func_name = frame.f_code.co_name
  504. # global context is set with an empty name
  505. if curr_func_name in ('?', '<module>', '<lambda>'):
  506. curr_func_name = ''
  507. for breakpoint in dict_iter_values(breakpoints_for_file): # jython does not support itervalues()
  508. # will match either global or some function
  509. if breakpoint.func_name in ('None', curr_func_name):
  510. has_breakpoint_in_frame = True
  511. break
  512. # Cache the value (1 or 0 or -1 for default because of cython).
  513. if has_breakpoint_in_frame:
  514. frame_skips_cache[frame_cache_key] = 1
  515. else:
  516. frame_skips_cache[frame_cache_key] = 0
  517. if can_skip and not has_breakpoint_in_frame:
  518. if has_exception_breakpoints:
  519. frame.f_trace = self.trace_exception
  520. return self.trace_exception
  521. else:
  522. if need_signature_trace_return:
  523. frame.f_trace = self.trace_return
  524. return self.trace_return
  525. else:
  526. if not is_call: frame.f_trace = NO_FTRACE
  527. return None
  528. # We may have hit a breakpoint or we are already in step mode. Either way, let's check what we should do in this frame
  529. # print('NOT skipped: %s %s %s %s' % (frame.f_lineno, frame.f_code.co_name, event, frame.__class__.__name__))
  530. try:
  531. flag = False
  532. # return is not taken into account for breakpoint hit because we'd have a double-hit in this case
  533. # (one for the line and the other for the return).
  534. stop_info = {}
  535. breakpoint = None
  536. exist_result = False
  537. stop = False
  538. bp_type = None
  539. smart_stop_frame = info.pydev_smart_step_context.smart_step_stop
  540. context_start_line = info.pydev_smart_step_context.start_line
  541. context_end_line = info.pydev_smart_step_context.end_line
  542. is_within_context = context_start_line <= line <= context_end_line
  543. if not is_return and info.pydev_state != STATE_SUSPEND and breakpoints_for_file is not None and line in breakpoints_for_file:
  544. breakpoint = breakpoints_for_file[line]
  545. new_frame = frame
  546. stop = True
  547. if step_cmd == CMD_STEP_OVER:
  548. if stop_frame is frame and (is_line or is_return):
  549. stop = False # we don't stop on breakpoint if we have to stop by step-over (it will be processed later)
  550. elif is_generator_or_coroutime and frame.f_back and frame.f_back is stop_frame:
  551. stop = False # we don't stop on breakpoint if stepping is active and we enter a `genexpr` or coroutine context
  552. elif step_cmd == CMD_SMART_STEP_INTO and (frame.f_back is smart_stop_frame and is_within_context):
  553. stop = False
  554. elif plugin_manager is not None and main_debugger.has_plugin_line_breaks:
  555. result = plugin_manager.get_breakpoint(main_debugger, self, frame, event, self._args)
  556. if result:
  557. exist_result = True
  558. flag, breakpoint, new_frame, bp_type = result
  559. if breakpoint:
  560. # ok, hit breakpoint, now, we have to discover if it is a conditional breakpoint
  561. # lets do the conditional stuff here
  562. if stop or exist_result:
  563. eval_result = False
  564. if breakpoint.has_condition:
  565. eval_result = handle_breakpoint_condition(main_debugger, info, breakpoint, new_frame)
  566. if breakpoint.expression is not None:
  567. handle_breakpoint_expression(breakpoint, info, new_frame)
  568. if breakpoint.is_logpoint and info.pydev_message is not None and len(info.pydev_message) > 0:
  569. cmd = main_debugger.cmd_factory.make_io_message(info.pydev_message + os.linesep, '1')
  570. main_debugger.writer.add_command(cmd)
  571. if breakpoint.has_condition and not eval_result:
  572. # No need to reset frame.f_trace to keep the same trace function.
  573. return self.trace_dispatch
  574. if is_call and frame.f_code.co_name in ('<module>', '<lambda>'):
  575. # If we find a call for a module, it means that the module is being imported/executed for the
  576. # first time. In this case we have to ignore this hit as it may later duplicated by a
  577. # line event at the same place (so, if there's a module with a print() in the first line
  578. # the user will hit that line twice, which is not what we want).
  579. #
  580. # As for lambda, as it only has a single statement, it's not interesting to trace
  581. # its call and later its line event as they're usually in the same line.
  582. # No need to reset frame.f_trace to keep the same trace function.
  583. return self.trace_dispatch
  584. else:
  585. # if the frame is traced after breakpoint stop,
  586. # but the file should be ignored while stepping because of filters
  587. if step_cmd != -1:
  588. if main_debugger.is_filter_enabled and main_debugger.is_ignored_by_filters(filename):
  589. # ignore files matching stepping filters
  590. # No need to reset frame.f_trace to keep the same trace function.
  591. return self.trace_dispatch
  592. if main_debugger.is_filter_libraries and not main_debugger.in_project_scope(filename):
  593. # ignore library files while stepping
  594. # No need to reset frame.f_trace to keep the same trace function.
  595. return self.trace_dispatch
  596. if main_debugger.show_return_values or main_debugger.remove_return_values_flag:
  597. self.manage_return_values(main_debugger, frame, event, arg)
  598. if stop:
  599. self.set_suspend(
  600. thread,
  601. CMD_SET_BREAK,
  602. suspend_other_threads=breakpoint and breakpoint.suspend_policy == "ALL",
  603. )
  604. elif flag and plugin_manager is not None:
  605. result = plugin_manager.suspend(main_debugger, thread, frame, bp_type)
  606. if result:
  607. frame = result
  608. # if thread has a suspend flag, we suspend with a busy wait
  609. if info.pydev_state == STATE_SUSPEND:
  610. self.do_wait_suspend(thread, frame, event, arg)
  611. # No need to reset frame.f_trace to keep the same trace function.
  612. return self.trace_dispatch
  613. else:
  614. if not breakpoint and is_line:
  615. # No stop from anyone and no breakpoint found in line (cache that).
  616. frame_skips_cache[line_cache_key] = 0
  617. except KeyboardInterrupt:
  618. self.clear_run_state(info)
  619. raise
  620. except:
  621. traceback.print_exc()
  622. raise
  623. # step handling. We stop when we hit the right frame
  624. try:
  625. should_skip = 0
  626. if pydevd_dont_trace.should_trace_hook is not None:
  627. if self.should_skip == -1:
  628. # I.e.: cache the result on self.should_skip (no need to evaluate the same frame multiple times).
  629. # Note that on a code reload, we won't re-evaluate this because in practice, the frame.f_code
  630. # Which will be handled by this frame is read-only, so, we can cache it safely.
  631. if not pydevd_dont_trace.should_trace_hook(frame, filename):
  632. # -1, 0, 1 to be Cython-friendly
  633. should_skip = self.should_skip = 1
  634. else:
  635. should_skip = self.should_skip = 0
  636. else:
  637. should_skip = self.should_skip
  638. plugin_stop = False
  639. if should_skip:
  640. stop = False
  641. elif step_cmd == CMD_SMART_STEP_INTO:
  642. stop = False
  643. if smart_stop_frame is frame:
  644. if not is_within_context or not IS_CPYTHON:
  645. # We don't stop on jumps in multiline statements, which the Python interpreter does in some cases,
  646. # if we they happen in smart step into context.
  647. info.pydev_func_name = '.invalid.' # Must match the type in cython
  648. stop = True # act as if we did a step into
  649. if is_line or is_exception_event:
  650. curr_func_name = frame.f_code.co_name
  651. # global context is set with an empty name
  652. if curr_func_name in ('?', '<module>') or curr_func_name is None:
  653. curr_func_name = ''
  654. if smart_stop_frame and smart_stop_frame is frame.f_back:
  655. if curr_func_name == info.pydev_func_name and not IS_CPYTHON:
  656. # for implementations other than CPython we don't perform any additional checks
  657. stop = True
  658. else:
  659. try:
  660. if curr_func_name != info.pydev_func_name and frame.f_back:
  661. # try to find function call name using bytecode analysis
  662. curr_func_name = find_last_call_name(frame.f_back)
  663. if curr_func_name == info.pydev_func_name:
  664. stop = find_last_func_call_order(frame.f_back, context_start_line) \
  665. == info.pydev_smart_step_context.call_order
  666. except:
  667. pydev_log.debug("Exception while handling smart step into in frame tracer, step into will be performed instead.")
  668. info.pydev_smart_step_context.reset()
  669. stop = True # act as if we did a step into
  670. # we have to check this case for situations when a user has tried to step into a native function or method,
  671. # e.g. `len()`, `list.append()`, etc and this was the only call in a return statement
  672. if smart_stop_frame is frame and is_return:
  673. stop = True
  674. elif step_cmd == CMD_STEP_INTO:
  675. stop = is_line or is_return
  676. if plugin_manager is not None:
  677. result = plugin_manager.cmd_step_into(main_debugger, frame, event, self._args, stop_info, stop)
  678. if result:
  679. stop, plugin_stop = result
  680. elif step_cmd == CMD_STEP_INTO_MY_CODE:
  681. if main_debugger.in_project_scope(frame.f_code.co_filename):
  682. stop = is_line
  683. elif step_cmd in (CMD_STEP_OVER, CMD_STEP_INTO_COROUTINE):
  684. stop = stop_frame is frame
  685. if stop:
  686. if is_line:
  687. # the only case we shouldn't stop on a line, is when we traversing though asynchronous framework machinery
  688. if step_cmd == CMD_STEP_INTO_COROUTINE:
  689. stop = main_debugger.in_project_scope(frame.f_code.co_filename)
  690. elif is_return:
  691. stop = frame.f_back and main_debugger.in_project_scope(frame.f_back.f_code.co_filename)
  692. if not stop:
  693. back = frame.f_back
  694. if back:
  695. info.pydev_step_stop = back
  696. if main_debugger.in_project_scope(frame.f_code.co_filename):
  697. # we are returning from the project scope, step over should always lead to the project scope
  698. if is_generator_or_coroutime and step_cmd == CMD_STEP_OVER:
  699. # setting ad hoc command to ensure we will skip line stops in an asynchronous framework
  700. info.pydev_step_cmd = CMD_STEP_INTO_COROUTINE
  701. else:
  702. # we were already outside the project scope because of step into or breakpoint, it's ok to stop
  703. # if we are not chopping a way through an asynchronous framework
  704. stop = not step_cmd == CMD_STEP_INTO_COROUTINE
  705. else:
  706. # if there's no back frame, we just stop as soon as possible
  707. info.pydev_step_cmd = CMD_STEP_INTO
  708. info.pydev_step_stop = None
  709. else:
  710. stop = False
  711. if CMD_STEP_OVER and plugin_manager is not None:
  712. result = plugin_manager.cmd_step_over(main_debugger, frame, event, self._args, stop_info, stop)
  713. if result:
  714. stop, plugin_stop = result
  715. elif step_cmd == CMD_STEP_RETURN:
  716. stop = is_return and stop_frame is frame
  717. else:
  718. stop = False
  719. if stop and step_cmd != -1 and is_return and IS_PY3K and hasattr(frame, "f_back"):
  720. f_code = getattr(frame.f_back, 'f_code', None)
  721. if f_code is not None:
  722. back_filename = os.path.basename(f_code.co_filename)
  723. file_type = get_file_type(back_filename)
  724. if file_type == PYDEV_FILE:
  725. stop = False
  726. if plugin_stop:
  727. stopped_on_plugin = plugin_manager.stop(main_debugger, frame, event, self._args, stop_info, arg, step_cmd)
  728. elif stop:
  729. if is_line:
  730. self.set_suspend(thread, step_cmd)
  731. self.do_wait_suspend(thread, frame, event, arg)
  732. else: # return event
  733. back = frame.f_back
  734. if back is not None:
  735. # When we get to the pydevd run function, the debugging has actually finished for the main thread
  736. # (note that it can still go on for other threads, but for this one, we just make it finish)
  737. # So, just setting it to None should be OK
  738. _, back_filename, base = get_abs_path_real_path_and_base_from_frame(back)
  739. if (base, back.f_code.co_name) in (DEBUG_START, DEBUG_START_PY3K):
  740. back = None
  741. elif base == TRACE_PROPERTY:
  742. # We dont want to trace the return event of pydevd_traceproperty (custom property for debugging)
  743. # if we're in a return, we want it to appear to the user in the previous frame!
  744. if not is_call: frame.f_trace = NO_FTRACE
  745. return None
  746. elif pydevd_dont_trace.should_trace_hook is not None:
  747. if not pydevd_dont_trace.should_trace_hook(back, back_filename):
  748. # In this case, we'll have to skip the previous one because it shouldn't be traced.
  749. # Also, we have to reset the tracing, because if the parent's parent (or some
  750. # other parent) has to be traced and it's not currently, we wouldn't stop where
  751. # we should anymore (so, a step in/over/return may not stop anywhere if no parent is traced).
  752. # Related test: _debugger_case17a.py
  753. main_debugger.set_trace_for_frame_and_parents(back)
  754. if not is_call: frame.f_trace = NO_FTRACE
  755. return None
  756. if back is not None:
  757. # if we're in a return, we want it to appear to the user in the previous frame!
  758. self.set_suspend(thread, step_cmd)
  759. self.do_wait_suspend(thread, back, event, arg)
  760. else:
  761. # in jython we may not have a back frame
  762. self.clear_run_state(info)
  763. except KeyboardInterrupt:
  764. self.clear_run_state(info)
  765. raise
  766. except:
  767. try:
  768. traceback.print_exc()
  769. info.pydev_step_cmd = -1
  770. except:
  771. if not is_call: frame.f_trace = NO_FTRACE
  772. return None
  773. # if we are quitting, let's stop the tracing
  774. if not main_debugger.quitting:
  775. # No need to reset frame.f_trace to keep the same trace function.
  776. return self.trace_dispatch
  777. else:
  778. if not is_call: frame.f_trace = NO_FTRACE
  779. return None
  780. finally:
  781. info.is_tracing = False
  782. # end trace_dispatch
  783. # IFDEF CYTHON
  784. # cdef _get_instructions(self, frame):
  785. # if self._instructions is None:
  786. # self._instructions = list(dis.get_instructions(frame.f_code))
  787. # return self._instructions
  788. # ENDIF