Fix errors and fully support matplotlib interactive mode in run and debug with the...
[idea/community.git] / python / helpers / pydev / pydevd.py
1 '''
2 Entry point module (keep at root):
3
4 This module starts the debugger.
5 '''
6 from __future__ import nested_scopes  # Jython 2.1 support
7
8 import atexit
9 import os
10 import sys
11 import traceback
12
13 from _pydevd_bundle.pydevd_constants import IS_JYTH_LESS25, IS_PY3K, IS_PY34_OLDER, get_thread_id, dict_keys, dict_pop, dict_contains, \
14     dict_iter_items, DebugInfoHolder, PYTHON_SUSPEND, STATE_SUSPEND, STATE_RUN, get_frame, xrange, \
15     clear_cached_thread_id
16 from _pydev_bundle import fix_getpass
17 from _pydev_bundle import pydev_imports, pydev_log
18 from _pydev_bundle._pydev_filesystem_encoding import getfilesystemencoding
19 from _pydev_bundle.pydev_is_thread_alive import is_thread_alive
20 from _pydev_imps._pydev_saved_modules import threading
21 from _pydev_imps._pydev_saved_modules import time
22 from _pydev_imps._pydev_saved_modules import thread
23 from _pydevd_bundle import pydevd_io, pydevd_vm_type, pydevd_tracing
24 from _pydevd_bundle import pydevd_utils
25 from _pydevd_bundle import pydevd_vars
26 from _pydevd_bundle.pydevd_additional_thread_info import PyDBAdditionalThreadInfo
27 from _pydevd_bundle.pydevd_breakpoints import ExceptionBreakpoint, update_exception_hook
28 from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \
29     CMD_STEP_RETURN, CMD_STEP_INTO_MY_CODE, CMD_THREAD_SUSPEND, CMD_RUN_TO_LINE, \
30     CMD_ADD_EXCEPTION_BREAK, CMD_SMART_STEP_INTO, InternalConsoleExec, NetCommandFactory, \
31     PyDBDaemonThread, _queue, ReaderThread, GetGlobalDebugger, get_global_debugger, \
32     set_global_debugger, WriterThread, pydevd_find_thread_by_id, pydevd_log, \
33     start_client, start_server, InternalGetBreakpointException, InternalSendCurrExceptionTrace, \
34     InternalSendCurrExceptionTraceProceeded
35 from _pydevd_bundle.pydevd_custom_frames import CustomFramesContainer, custom_frames_container_init
36 from _pydevd_bundle.pydevd_frame_utils import add_exception_to_frame
37 from _pydevd_bundle.pydevd_kill_all_pydevd_threads import kill_all_pydev_threads
38 from _pydevd_bundle.pydevd_trace_dispatch import trace_dispatch as _trace_dispatch
39 from _pydevd_bundle.pydevd_utils import save_main_module
40 from pydevd_concurrency_analyser.pydevd_concurrency_logger import ThreadingLogger, AsyncioLogger, send_message, cur_time
41 from pydevd_concurrency_analyser.pydevd_thread_wrappers import wrap_threads
42
43
44 __version_info__ = (0, 0, 5)
45 __version_info_str__ = []
46 for v in __version_info__:
47     __version_info_str__.append(str(v))
48
49 __version__ = '.'.join(__version_info_str__)
50
51 #IMPORTANT: pydevd_constants must be the 1st thing defined because it'll keep a reference to the original sys._getframe
52
53
54
55
56
57
58
59 SUPPORT_PLUGINS = not IS_JYTH_LESS25
60 PluginManager = None
61 if SUPPORT_PLUGINS:
62     from _pydevd_bundle.pydevd_plugin_utils import PluginManager
63
64
65 threadingEnumerate = threading.enumerate
66 threadingCurrentThread = threading.currentThread
67
68 try:
69     'dummy'.encode('utf-8') # Added because otherwise Jython 2.2.1 wasn't finding the encoding (if it wasn't loaded in the main thread).
70 except:
71     pass
72
73
74 connected = False
75 bufferStdOutToServer = False
76 bufferStdErrToServer = False
77 remote = False
78
79 file_system_encoding = getfilesystemencoding()
80
81
82 #=======================================================================================================================
83 # PyDBCommandThread
84 #=======================================================================================================================
85 class PyDBCommandThread(PyDBDaemonThread):
86
87     def __init__(self, py_db):
88         PyDBDaemonThread.__init__(self)
89         self._py_db_command_thread_event = py_db._py_db_command_thread_event
90         self.py_db = py_db
91         self.setName('pydevd.CommandThread')
92
93     def _on_run(self):
94         for i in xrange(1, 10):
95             time.sleep(0.5) #this one will only start later on (because otherwise we may not have any non-daemon threads
96             if self.killReceived:
97                 return
98
99         if self.pydev_do_not_trace:
100             self.py_db.SetTrace(None) # no debugging on this thread
101
102         try:
103             while not self.killReceived:
104                 try:
105                     self.py_db.process_internal_commands()
106                 except:
107                     pydevd_log(0, 'Finishing debug communication...(2)')
108                 self._py_db_command_thread_event.clear()
109                 self._py_db_command_thread_event.wait(0.5)
110         except:
111             pydev_log.debug(sys.exc_info()[0])
112
113             #only got this error in interpreter shutdown
114             #pydevd_log(0, 'Finishing debug communication...(3)')
115
116
117
118 #=======================================================================================================================
119 # CheckOutputThread
120 # Non-daemonic thread guaranties that all data is written even if program is finished
121 #=======================================================================================================================
122 class CheckOutputThread(PyDBDaemonThread):
123
124     def __init__(self, py_db):
125         PyDBDaemonThread.__init__(self)
126         self.py_db = py_db
127         self.setName('pydevd.CheckAliveThread')
128         self.daemon = False
129         py_db.output_checker = self
130
131     def _on_run(self):
132         if self.pydev_do_not_trace:
133
134             disable_tracing = True
135
136             if pydevd_vm_type.get_vm_type() == pydevd_vm_type.PydevdVmType.JYTHON and sys.hexversion <= 0x020201f0:
137                 # don't run untraced threads if we're in jython 2.2.1 or lower
138                 # jython bug: if we start a thread and another thread changes the tracing facility
139                 # it affects other threads (it's not set only for the thread but globally)
140                 # Bug: http://sourceforge.net/tracker/index.php?func=detail&aid=1870039&group_id=12867&atid=112867
141                 disable_tracing = False
142
143             if disable_tracing:
144                 pydevd_tracing.SetTrace(None)  # no debugging on this thread
145
146         while not self.killReceived:
147             time.sleep(0.3)
148             if not self.py_db.has_threads_alive() and self.py_db.writer.empty() \
149                     and not has_data_to_redirect():
150                 try:
151                     pydev_log.debug("No alive threads, finishing debug session")
152                     self.py_db.finish_debugging_session()
153                     kill_all_pydev_threads()
154                 except:
155                     traceback.print_exc()
156
157                 self.killReceived = True
158
159             self.py_db.check_output_redirect()
160
161
162     def do_kill_pydev_thread(self):
163         self.killReceived = True
164
165
166
167 #=======================================================================================================================
168 # PyDB
169 #=======================================================================================================================
170 class PyDB:
171     """ Main debugging class
172     Lots of stuff going on here:
173
174     PyDB starts two threads on startup that connect to remote debugger (RDB)
175     The threads continuously read & write commands to RDB.
176     PyDB communicates with these threads through command queues.
177        Every RDB command is processed by calling process_net_command.
178        Every PyDB net command is sent to the net by posting NetCommand to WriterThread queue
179
180        Some commands need to be executed on the right thread (suspend/resume & friends)
181        These are placed on the internal command queue.
182     """
183
184
185     def __init__(self):
186         set_global_debugger(self)
187         pydevd_tracing.replace_sys_set_trace_func()
188         self.reader = None
189         self.writer = None
190         self.output_checker = None
191         self.quitting = None
192         self.cmd_factory = NetCommandFactory()
193         self._cmd_queue = {}  # the hash of Queues. Key is thread id, value is thread
194
195         self.breakpoints = {}
196
197         self.file_to_id_to_line_breakpoint = {}
198         self.file_to_id_to_plugin_breakpoint = {}
199
200         # Note: breakpoints dict should not be mutated: a copy should be created
201         # and later it should be assigned back (to prevent concurrency issues).
202         self.break_on_uncaught_exceptions = {}
203         self.break_on_caught_exceptions = {}
204
205         self.ready_to_run = False
206         self._main_lock = thread.allocate_lock()
207         self._lock_running_thread_ids = thread.allocate_lock()
208         self._py_db_command_thread_event = threading.Event()
209         CustomFramesContainer._py_db_command_thread_event = self._py_db_command_thread_event
210         self._finish_debugging_session = False
211         self._termination_event_set = False
212         self.signature_factory = None
213         self.SetTrace = pydevd_tracing.SetTrace
214         self.break_on_exceptions_thrown_in_same_context = False
215         self.ignore_exceptions_thrown_in_lines_with_ignore_exception = True
216
217         # Suspend debugger even if breakpoint condition raises an exception
218         SUSPEND_ON_BREAKPOINT_EXCEPTION = True
219         self.suspend_on_breakpoint_exception = SUSPEND_ON_BREAKPOINT_EXCEPTION
220
221         # By default user can step into properties getter/setter/deleter methods
222         self.disable_property_trace = False
223         self.disable_property_getter_trace = False
224         self.disable_property_setter_trace = False
225         self.disable_property_deleter_trace = False
226
227         #this is a dict of thread ids pointing to thread ids. Whenever a command is passed to the java end that
228         #acknowledges that a thread was created, the thread id should be passed here -- and if at some time we do not
229         #find that thread alive anymore, we must remove it from this list and make the java side know that the thread
230         #was killed.
231         self._running_thread_ids = {}
232         self._set_breakpoints_with_id = False
233
234         # This attribute holds the file-> lines which have an @IgnoreException.
235         self.filename_to_lines_where_exceptions_are_ignored = {}
236
237         #working with plugins (lazily initialized)
238         self.plugin = None
239         self.has_plugin_line_breaks = False
240         self.has_plugin_exception_breaks = False
241         self.thread_analyser = None
242         self.asyncio_analyser = None
243
244         # matplotlib support in debugger and debug console
245         self.mpl_in_use = False
246         self.mpl_hooks_in_debug_console = False
247         self.mpl_modules_for_patching = {}
248
249         self._filename_to_not_in_scope = {}
250         self.first_breakpoint_reached = False
251         self.is_filter_enabled = pydevd_utils.is_filter_enabled()
252         self.is_filter_libraries = pydevd_utils.is_filter_libraries()
253         self.show_return_values = False
254         self.remove_return_values_flag = False
255
256     def get_plugin_lazy_init(self):
257         if self.plugin is None and SUPPORT_PLUGINS:
258             self.plugin = PluginManager(self)
259         return self.plugin
260
261     def not_in_scope(self, filename):
262         return pydevd_utils.not_in_project_roots(filename)
263
264     def is_ignored_by_filters(self, filename):
265         return pydevd_utils.is_ignored_by_filter(filename)
266
267     def first_appearance_in_scope(self, trace):
268         if trace is None or self.not_in_scope(trace.tb_frame.f_code.co_filename):
269             return False
270         else:
271             trace = trace.tb_next
272             while trace is not None:
273                 frame = trace.tb_frame
274                 if not self.not_in_scope(frame.f_code.co_filename):
275                     return False
276                 trace = trace.tb_next
277             return True
278
279     def has_threads_alive(self):
280         for t in threadingEnumerate():
281             if getattr(t, 'is_pydev_daemon_thread', False):
282                 #Important: Jython 2.5rc4 has a bug where a thread created with thread.start_new_thread won't be
283                 #set as a daemon thread, so, we also have to check for the 'is_pydev_daemon_thread' flag.
284                 #See: https://github.com/fabioz/PyDev.Debugger/issues/11
285                 continue
286
287             if isinstance(t, PyDBDaemonThread):
288                 pydev_log.error_once(
289                         'Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n')
290
291             if is_thread_alive(t):
292                 if not t.isDaemon() or hasattr(t, "__pydevd_main_thread"):
293                     return True
294
295         return False
296
297     def finish_debugging_session(self):
298         self._finish_debugging_session = True
299
300
301     def initialize_network(self, sock):
302         try:
303             sock.settimeout(None)  # infinite, no timeouts from now on - jython does not have it
304         except:
305             pass
306         self.writer = WriterThread(sock)
307         self.reader = ReaderThread(sock)
308         self.writer.start()
309         self.reader.start()
310
311         time.sleep(0.1)  # give threads time to start
312
313     def connect(self, host, port):
314         if host:
315             s = start_client(host, port)
316         else:
317             s = start_server(port)
318
319         self.initialize_network(s)
320
321
322     def get_internal_queue(self, thread_id):
323         """ returns internal command queue for a given thread.
324         if new queue is created, notify the RDB about it """
325         if thread_id.startswith('__frame__'):
326             thread_id = thread_id[thread_id.rfind('|') + 1:]
327         try:
328             return self._cmd_queue[thread_id]
329         except KeyError:
330             return self._cmd_queue.setdefault(thread_id, _queue.Queue()) #@UndefinedVariable
331
332
333     def post_internal_command(self, int_cmd, thread_id):
334         """ if thread_id is *, post to all """
335         if thread_id == "*":
336             threads = threadingEnumerate()
337             for t in threads:
338                 thread_id = get_thread_id(t)
339                 queue = self.get_internal_queue(thread_id)
340                 queue.put(int_cmd)
341
342         else:
343             queue = self.get_internal_queue(thread_id)
344             queue.put(int_cmd)
345
346     def check_output_redirect(self):
347         global bufferStdOutToServer
348         global bufferStdErrToServer
349
350         if bufferStdOutToServer:
351             init_stdout_redirect()
352             self.check_output(sys.stdoutBuf, 1) #@UndefinedVariable
353
354         if bufferStdErrToServer:
355             init_stderr_redirect()
356             self.check_output(sys.stderrBuf, 2) #@UndefinedVariable
357
358     def check_output(self, out, outCtx):
359         '''Checks the output to see if we have to send some buffered output to the debug server
360
361         @param out: sys.stdout or sys.stderr
362         @param outCtx: the context indicating: 1=stdout and 2=stderr (to know the colors to write it)
363         '''
364
365         try:
366             v = out.getvalue()
367
368             if v:
369                 self.cmd_factory.make_io_message(v, outCtx, self)
370         except:
371             traceback.print_exc()
372
373
374     def init_matplotlib_in_debug_console(self):
375         # import hook and patches for matplotlib support in debug console
376         from _pydev_bundle.pydev_import_hook import import_hook_manager
377         for module in dict_keys(self.mpl_modules_for_patching):
378             import_hook_manager.add_module_name(module, dict_pop(self.mpl_modules_for_patching, module))
379
380     def init_matplotlib_support(self):
381         # prepare debugger for integration with matplotlib GUI event loop
382         from pydev_ipython.matplotlibtools import activate_matplotlib, activate_pylab, activate_pyplot, do_enable_gui
383         # enable_gui_function in activate_matplotlib should be called in main thread. Unlike integrated console,
384         # in the debug console we have no interpreter instance with exec_queue, but we run this code in the main
385         # thread and can call it directly.
386         class _MatplotlibHelper:
387             _return_control_osc = False
388
389         def return_control():
390             # Some of the input hooks (e.g. Qt4Agg) check return control without doing
391             # a single operation, so we don't return True on every
392             # call when the debug hook is in place to allow the GUI to run
393             _MatplotlibHelper._return_control_osc = not _MatplotlibHelper._return_control_osc
394             return _MatplotlibHelper._return_control_osc
395
396         from pydev_ipython.inputhook import set_return_control_callback
397         set_return_control_callback(return_control)
398
399         self.mpl_modules_for_patching = {"matplotlib": lambda: activate_matplotlib(do_enable_gui),
400                                          "matplotlib.pyplot": activate_pyplot,
401                                          "pylab": activate_pylab }
402
403     def _activate_mpl_if_needed(self):
404         if len(self.mpl_modules_for_patching) > 0:
405             for module in dict_keys(self.mpl_modules_for_patching):
406                 if module in sys.modules:
407                     activate_function = dict_pop(self.mpl_modules_for_patching, module)
408                     activate_function()
409                     self.mpl_in_use = True
410
411     def _call_mpl_hook(self):
412         try:
413             from pydev_ipython.inputhook import get_inputhook
414             inputhook = get_inputhook()
415             if inputhook:
416                 inputhook()
417         except:
418             pass
419
420     def suspend_all_other_threads(self, thread_suspended_at_bp):
421         all_threads = threadingEnumerate()
422         for t in all_threads:
423             if getattr(t, 'is_pydev_daemon_thread', False):
424                 pass # I.e.: skip the DummyThreads created from pydev daemon threads
425             elif hasattr(t, 'pydev_do_not_trace'):
426                 pass  # skip some other threads, i.e. ipython history saving thread from debug console
427             else:
428                 if t is thread_suspended_at_bp:
429                     continue
430                 additional_info = None
431                 try:
432                     additional_info = t.additional_info
433                 except AttributeError:
434                     pass  # that's ok, no info currently set
435
436                 if additional_info is not None:
437                     for frame in additional_info.iter_frames(t):
438                         self.set_trace_for_frame_and_parents(frame, overwrite_prev_trace=True)
439                         del frame
440
441                     self.set_suspend(t, CMD_THREAD_SUSPEND)
442                 else:
443                     sys.stderr.write("Can't suspend thread: %s\n" % (t,))
444
445     def process_internal_commands(self):
446         '''This function processes internal commands
447         '''
448         self._main_lock.acquire()
449         try:
450
451             self.check_output_redirect()
452
453             curr_thread_id = get_thread_id(threadingCurrentThread())
454             program_threads_alive = {}
455             all_threads = threadingEnumerate()
456             program_threads_dead = []
457             self._lock_running_thread_ids.acquire()
458             try:
459                 for t in all_threads:
460                     if getattr(t, 'is_pydev_daemon_thread', False):
461                         pass # I.e.: skip the DummyThreads created from pydev daemon threads
462                     elif isinstance(t, PyDBDaemonThread):
463                         pydev_log.error_once('Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n')
464
465                     elif is_thread_alive(t):
466                         if not self._running_thread_ids:
467                             # Fix multiprocessing debug with breakpoints in both main and child processes
468                             # (https://youtrack.jetbrains.com/issue/PY-17092) When the new process is created, the main
469                             # thread in the new process already has the attribute 'pydevd_id', so the new thread doesn't
470                             # get new id with its process number and the debugger loses access to both threads.
471                             # Therefore we should update thread_id for every main thread in the new process.
472
473                             # TODO: Investigate: should we do this for all threads in threading.enumerate()?
474                             # (i.e.: if a fork happens on Linux, this seems likely).
475                             old_thread_id = get_thread_id(t)
476
477                             clear_cached_thread_id(t)
478                             clear_cached_thread_id(threadingCurrentThread())
479
480                             thread_id = get_thread_id(t)
481                             curr_thread_id = get_thread_id(threadingCurrentThread())
482                             if pydevd_vars.has_additional_frames_by_id(old_thread_id):
483                                 frames_by_id = pydevd_vars.get_additional_frames_by_id(old_thread_id)
484                                 pydevd_vars.add_additional_frame_by_id(thread_id, frames_by_id)
485                         else:
486                             thread_id = get_thread_id(t)
487                         program_threads_alive[thread_id] = t
488
489                         if not dict_contains(self._running_thread_ids, thread_id):
490                             if not hasattr(t, 'additional_info'):
491                                 # see http://sourceforge.net/tracker/index.php?func=detail&aid=1955428&group_id=85796&atid=577329
492                                 # Let's create the additional info right away!
493                                 t.additional_info = PyDBAdditionalThreadInfo()
494                             self._running_thread_ids[thread_id] = t
495                             self.writer.add_command(self.cmd_factory.make_thread_created_message(t))
496
497
498                         queue = self.get_internal_queue(thread_id)
499                         cmdsToReadd = []  # some commands must be processed by the thread itself... if that's the case,
500                         # we will re-add the commands to the queue after executing.
501                         try:
502                             while True:
503                                 int_cmd = queue.get(False)
504
505                                 if not self.mpl_hooks_in_debug_console and isinstance(int_cmd, InternalConsoleExec):
506                                     # add import hooks for matplotlib patches if only debug console was started
507                                     try:
508                                         self.init_matplotlib_in_debug_console()
509                                         self.mpl_in_use = True
510                                     except:
511                                         pydevd_log(2, "Matplotlib support in debug console failed", traceback.format_exc())
512                                     self.mpl_hooks_in_debug_console = True
513
514                                 if int_cmd.can_be_executed_by(curr_thread_id):
515                                     pydevd_log(2, "processing internal command ", str(int_cmd))
516                                     int_cmd.do_it(self)
517                                 else:
518                                     pydevd_log(2, "NOT processing internal command ", str(int_cmd))
519                                     cmdsToReadd.append(int_cmd)
520
521
522                         except _queue.Empty: #@UndefinedVariable
523                             for int_cmd in cmdsToReadd:
524                                 queue.put(int_cmd)
525                                 # this is how we exit
526
527
528                 thread_ids = list(self._running_thread_ids.keys())
529                 for tId in thread_ids:
530                     if not dict_contains(program_threads_alive, tId):
531                         program_threads_dead.append(tId)
532             finally:
533                 self._lock_running_thread_ids.release()
534
535             for tId in program_threads_dead:
536                 try:
537                     self._process_thread_not_alive(tId)
538                 except:
539                     sys.stderr.write('Error iterating through %s (%s) - %s\n' % (
540                         program_threads_alive, program_threads_alive.__class__, dir(program_threads_alive)))
541                     raise
542
543
544             if len(program_threads_alive) == 0:
545                 self.finish_debugging_session()
546                 for t in all_threads:
547                     if hasattr(t, 'do_kill_pydev_thread'):
548                         t.do_kill_pydev_thread()
549
550         finally:
551             self._main_lock.release()
552
553
554     def set_tracing_for_untraced_contexts(self, ignore_frame=None, overwrite_prev_trace=False):
555         # Enable the tracing for existing threads (because there may be frames being executed that
556         # are currently untraced).
557         threads = threadingEnumerate()
558         try:
559             for t in threads:
560                 if getattr(t, 'is_pydev_daemon_thread', False):
561                     continue
562
563                 # TODO: optimize so that we only actually add that tracing if it's in
564                 # the new breakpoint context.
565                 additional_info = None
566                 try:
567                     additional_info = t.additional_info
568                 except AttributeError:
569                     pass  # that's ok, no info currently set
570
571                 if additional_info is not None:
572                     for frame in additional_info.iter_frames(t):
573                         if frame is not ignore_frame:
574                             self.set_trace_for_frame_and_parents(frame, overwrite_prev_trace=overwrite_prev_trace)
575         finally:
576             frame = None
577             t = None
578             threads = None
579             additional_info = None
580
581
582     def consolidate_breakpoints(self, file, id_to_breakpoint, breakpoints):
583         break_dict = {}
584         for breakpoint_id, pybreakpoint in dict_iter_items(id_to_breakpoint):
585             break_dict[pybreakpoint.line] = pybreakpoint
586
587         breakpoints[file] = break_dict
588
589     def add_break_on_exception(
590             self,
591             exception,
592             notify_always,
593             notify_on_terminate,
594             notify_on_first_raise_only,
595             ignore_libraries=False
596     ):
597         try:
598             eb = ExceptionBreakpoint(
599                     exception,
600                     notify_always,
601                     notify_on_terminate,
602                     notify_on_first_raise_only,
603                     ignore_libraries
604             )
605         except ImportError:
606             pydev_log.error("Error unable to add break on exception for: %s (exception could not be imported)\n" % (exception,))
607             return None
608
609         if eb.notify_on_terminate:
610             cp = self.break_on_uncaught_exceptions.copy()
611             cp[exception] = eb
612             if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0:
613                 pydev_log.error("Exceptions to hook on terminate: %s\n" % (cp,))
614             self.break_on_uncaught_exceptions = cp
615
616         if eb.notify_always:
617             cp = self.break_on_caught_exceptions.copy()
618             cp[exception] = eb
619             if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0:
620                 pydev_log.error("Exceptions to hook always: %s\n" % (cp,))
621             self.break_on_caught_exceptions = cp
622
623         return eb
624
625     def update_after_exceptions_added(self, added):
626         updated_on_caught = False
627         updated_on_uncaught = False
628
629         for eb in added:
630             if not updated_on_uncaught and eb.notify_on_terminate:
631                 updated_on_uncaught = True
632                 update_exception_hook(self)
633
634             if not updated_on_caught and eb.notify_always:
635                 updated_on_caught = True
636                 self.set_tracing_for_untraced_contexts()
637
638
639     def _process_thread_not_alive(self, threadId):
640         """ if thread is not alive, cancel trace_dispatch processing """
641         self._lock_running_thread_ids.acquire()
642         try:
643             thread = self._running_thread_ids.pop(threadId, None)
644             if thread is None:
645                 return
646
647             wasNotified = thread.additional_info.pydev_notify_kill
648             if not wasNotified:
649                 thread.additional_info.pydev_notify_kill = True
650
651         finally:
652             self._lock_running_thread_ids.release()
653
654         cmd = self.cmd_factory.make_thread_killed_message(threadId)
655         self.writer.add_command(cmd)
656
657
658     def set_suspend(self, thread, stop_reason):
659         thread.additional_info.suspend_type = PYTHON_SUSPEND
660         thread.additional_info.pydev_state = STATE_SUSPEND
661         thread.stop_reason = stop_reason
662
663         # If conditional breakpoint raises any exception during evaluation send details to Java
664         if stop_reason == CMD_SET_BREAK and self.suspend_on_breakpoint_exception:
665             self._send_breakpoint_condition_exception(thread)
666
667
668     def _send_breakpoint_condition_exception(self, thread):
669         """If conditional breakpoint raises an exception during evaluation
670         send exception details to java
671         """
672         thread_id = get_thread_id(thread)
673         conditional_breakpoint_exception_tuple = thread.additional_info.conditional_breakpoint_exception
674         # conditional_breakpoint_exception_tuple - should contain 2 values (exception_type, stacktrace)
675         if conditional_breakpoint_exception_tuple and len(conditional_breakpoint_exception_tuple) == 2:
676             exc_type, stacktrace = conditional_breakpoint_exception_tuple
677             int_cmd = InternalGetBreakpointException(thread_id, exc_type, stacktrace)
678             # Reset the conditional_breakpoint_exception details to None
679             thread.additional_info.conditional_breakpoint_exception = None
680             self.post_internal_command(int_cmd, thread_id)
681
682
683     def send_caught_exception_stack(self, thread, arg, curr_frame_id):
684         """Sends details on the exception which was caught (and where we stopped) to the java side.
685
686         arg is: exception type, description, traceback object
687         """
688         thread_id = get_thread_id(thread)
689         int_cmd = InternalSendCurrExceptionTrace(thread_id, arg, curr_frame_id)
690         self.post_internal_command(int_cmd, thread_id)
691
692
693     def send_caught_exception_stack_proceeded(self, thread):
694         """Sends that some thread was resumed and is no longer showing an exception trace.
695         """
696         thread_id = get_thread_id(thread)
697         int_cmd = InternalSendCurrExceptionTraceProceeded(thread_id)
698         self.post_internal_command(int_cmd, thread_id)
699         self.process_internal_commands()
700
701
702     def send_process_created_message(self):
703         """Sends a message that a new process has been created.
704         """
705         cmd = self.cmd_factory.make_process_created_message()
706         self.writer.add_command(cmd)
707
708
709     def do_wait_suspend(self, thread, frame, event, arg): #@UnusedVariable
710         """ busy waits until the thread state changes to RUN
711         it expects thread's state as attributes of the thread.
712         Upon running, processes any outstanding Stepping commands.
713         """
714         self.process_internal_commands()
715
716         message = thread.additional_info.pydev_message
717
718         cmd = self.cmd_factory.make_thread_suspend_message(get_thread_id(thread), frame, thread.stop_reason, message)
719         self.writer.add_command(cmd)
720
721         CustomFramesContainer.custom_frames_lock.acquire()  # @UndefinedVariable
722         try:
723             from_this_thread = []
724
725             for frame_id, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames):
726                 if custom_frame.thread_id == thread.ident:
727                     # print >> sys.stderr, 'Frame created: ', frame_id
728                     self.writer.add_command(self.cmd_factory.make_custom_frame_created_message(frame_id, custom_frame.name))
729                     self.writer.add_command(self.cmd_factory.make_thread_suspend_message(frame_id, custom_frame.frame, CMD_THREAD_SUSPEND, ""))
730
731                 from_this_thread.append(frame_id)
732
733         finally:
734             CustomFramesContainer.custom_frames_lock.release()  # @UndefinedVariable
735
736         info = thread.additional_info
737
738         if info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session:
739             # before every stop check if matplotlib modules were imported inside script code
740             self._activate_mpl_if_needed()
741
742         while info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session:
743             if self.mpl_in_use:
744                 # call input hooks if only matplotlib is in use
745                 self._call_mpl_hook()
746
747             self.process_internal_commands()
748             time.sleep(0.01)
749
750         # process any stepping instructions
751         if info.pydev_step_cmd == CMD_STEP_INTO or info.pydev_step_cmd == CMD_STEP_INTO_MY_CODE:
752             info.pydev_step_stop = None
753             info.pydev_smart_step_stop = None
754
755         elif info.pydev_step_cmd == CMD_STEP_OVER:
756             info.pydev_step_stop = frame
757             info.pydev_smart_step_stop = None
758             self.set_trace_for_frame_and_parents(frame)
759
760         elif info.pydev_step_cmd == CMD_SMART_STEP_INTO:
761             self.set_trace_for_frame_and_parents(frame)
762             info.pydev_step_stop = None
763             info.pydev_smart_step_stop = frame
764
765         elif info.pydev_step_cmd == CMD_RUN_TO_LINE or info.pydev_step_cmd == CMD_SET_NEXT_STATEMENT :
766             self.set_trace_for_frame_and_parents(frame)
767
768             if event == 'line' or event == 'exception':
769                 #If we're already in the correct context, we have to stop it now, because we can act only on
770                 #line events -- if a return was the next statement it wouldn't work (so, we have this code
771                 #repeated at pydevd_frame).
772                 stop = False
773                 curr_func_name = frame.f_code.co_name
774
775                 #global context is set with an empty name
776                 if curr_func_name in ('?', '<module>'):
777                     curr_func_name = ''
778
779                 if curr_func_name == info.pydev_func_name:
780                     line = info.pydev_next_line
781                     if frame.f_lineno == line:
782                         stop = True
783                     else :
784                         if frame.f_trace is None:
785                             frame.f_trace = self.trace_dispatch
786                         frame.f_lineno = line
787                         frame.f_trace = None
788                         stop = True
789                 if stop:
790                     info.pydev_state = STATE_SUSPEND
791                     self.do_wait_suspend(thread, frame, event, arg)
792                     return
793
794
795         elif info.pydev_step_cmd == CMD_STEP_RETURN:
796             back_frame = frame.f_back
797             if back_frame is not None:
798                 # steps back to the same frame (in a return call it will stop in the 'back frame' for the user)
799                 info.pydev_step_stop = frame
800                 self.set_trace_for_frame_and_parents(frame)
801             else:
802                 # No back frame?!? -- this happens in jython when we have some frame created from an awt event
803                 # (the previous frame would be the awt event, but this doesn't make part of 'jython', only 'java')
804                 # so, if we're doing a step return in this situation, it's the same as just making it run
805                 info.pydev_step_stop = None
806                 info.pydev_step_cmd = -1
807                 info.pydev_state = STATE_RUN
808
809         del frame
810         cmd = self.cmd_factory.make_thread_run_message(get_thread_id(thread), info.pydev_step_cmd)
811         self.writer.add_command(cmd)
812
813         CustomFramesContainer.custom_frames_lock.acquire()  # @UndefinedVariable
814         try:
815             # The ones that remained on last_running must now be removed.
816             for frame_id in from_this_thread:
817                 # print >> sys.stderr, 'Removing created frame: ', frame_id
818                 self.writer.add_command(self.cmd_factory.make_thread_killed_message(frame_id))
819
820         finally:
821             CustomFramesContainer.custom_frames_lock.release()  # @UndefinedVariable
822
823     def handle_post_mortem_stop(self, thread, frame, frames_byid, exception):
824         pydev_log.debug("We are stopping in post-mortem\n")
825         thread_id = get_thread_id(thread)
826         pydevd_vars.add_additional_frame_by_id(thread_id, frames_byid)
827         try:
828             try:
829                 add_exception_to_frame(frame, exception)
830                 self.set_suspend(thread, CMD_ADD_EXCEPTION_BREAK)
831                 self.do_wait_suspend(thread, frame, 'exception', None)
832             except:
833                 pydev_log.error("We've got an error while stopping in post-mortem: %s\n"%sys.exc_info()[0])
834         finally:
835             pydevd_vars.remove_additional_frame_by_id(thread_id)
836
837
838     def set_trace_for_frame_and_parents(self, frame, also_add_to_passed_frame=True, overwrite_prev_trace=False, dispatch_func=None):
839         if dispatch_func is None:
840             dispatch_func = self.trace_dispatch
841
842         if also_add_to_passed_frame:
843             self.update_trace(frame, dispatch_func, overwrite_prev_trace)
844
845         frame = frame.f_back
846         while frame:
847             self.update_trace(frame, dispatch_func, overwrite_prev_trace)
848
849             frame = frame.f_back
850         del frame
851
852     def update_trace(self, frame, dispatch_func, overwrite_prev):
853         if frame.f_trace is None:
854             frame.f_trace = dispatch_func
855         else:
856             if overwrite_prev:
857                 frame.f_trace = dispatch_func
858             else:
859                 try:
860                     #If it's the trace_exception, go back to the frame trace dispatch!
861                     if frame.f_trace.im_func.__name__ == 'trace_exception':
862                         frame.f_trace = frame.f_trace.im_self.trace_dispatch
863                 except AttributeError:
864                     pass
865                 frame = frame.f_back
866         del frame
867
868     def prepare_to_run(self):
869         ''' Shared code to prepare debugging by installing traces and registering threads '''
870         self.patch_threads()
871         pydevd_tracing.SetTrace(self.trace_dispatch)
872
873
874         PyDBCommandThread(self).start()
875         if self.signature_factory is not None or self.thread_analyser is not None:
876             # we need all data to be sent to IDE even after program finishes
877             CheckOutputThread(self).start()
878
879
880     def patch_threads(self):
881         try:
882             # not available in jython!
883             import threading
884             threading.settrace(self.trace_dispatch)  # for all future threads
885         except:
886             pass
887
888         from _pydev_bundle.pydev_monkey import patch_thread_modules
889         patch_thread_modules()
890
891     def get_fullname(self, mod_name):
892         if IS_PY3K:
893             import pkgutil
894         else:
895             from _pydev_imps import _pydev_pkgutil_old as pkgutil
896         try:
897             loader = pkgutil.get_loader(mod_name)
898         except:
899             return None
900         if loader is not None:
901             for attr in ("get_filename", "_get_filename"):
902                 meth = getattr(loader, attr, None)
903                 if meth is not None:
904                     return meth(mod_name)
905         return None
906
907     def run(self, file, globals=None, locals=None, module=False, set_trace=True):
908         if module:
909             filename = self.get_fullname(file)
910             if filename is None:
911                 sys.stderr.write("No module named %s\n" % file)
912                 return
913             else:
914                 file = filename
915
916         if os.path.isdir(file):
917             new_target = os.path.join(file, '__main__.py')
918             if os.path.isfile(new_target):
919                 file = new_target
920
921         if globals is None:
922             m = save_main_module(file, 'pydevd')
923             globals = m.__dict__
924             try:
925                 globals['__builtins__'] = __builtins__
926             except NameError:
927                 pass  # Not there on Jython...
928
929         if locals is None:
930             locals = globals
931
932         if set_trace:
933             # Predefined (writable) attributes: __name__ is the module's name;
934             # __doc__ is the module's documentation string, or None if unavailable;
935             # __file__ is the pathname of the file from which the module was loaded,
936             # if it was loaded from a file. The __file__ attribute is not present for
937             # C modules that are statically linked into the interpreter; for extension modules
938             # loaded dynamically from a shared library, it is the pathname of the shared library file.
939
940
941             # I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in
942             # debug and run.
943             if m.__file__.startswith(sys.path[0]):
944                 # print >> sys.stderr, 'Deleting: ', sys.path[0]
945                 del sys.path[0]
946
947             # now, the local directory has to be added to the pythonpath
948             # sys.path.insert(0, os.getcwd())
949             # Changed: it's not the local directory, but the directory of the file launched
950             # The file being run ust be in the pythonpath (even if it was not before)
951             sys.path.insert(0, os.path.split(file)[0])
952
953             self.prepare_to_run()
954
955             while not self.ready_to_run:
956                 time.sleep(0.1)  # busy wait until we receive run command
957
958         if self.thread_analyser is not None:
959             wrap_threads()
960             t = threadingCurrentThread()
961             self.thread_analyser.set_start_time(cur_time())
962             send_message("threading_event", 0, t.getName(), get_thread_id(t), "thread", "start", file, 1, None, parent=get_thread_id(t))
963
964         if self.asyncio_analyser is not None:
965             # we don't have main thread in asyncio graph, so we should add a fake event
966             send_message("asyncio_event", 0, "Task", "Task", "thread", "stop", file, 1, frame=None, parent=None)
967
968         try:
969             self.init_matplotlib_support()
970         except:
971             sys.stderr.write("Matplotlib support in debugger failed\n")
972             traceback.print_exc()
973
974         pydev_imports.execfile(file, globals, locals)  # execute the script
975         return globals
976
977     def exiting(self):
978         sys.stdout.flush()
979         sys.stderr.flush()
980         self.check_output_redirect()
981         cmd = self.cmd_factory.make_exit_message()
982         self.writer.add_command(cmd)
983
984     def wait_for_commands(self, globals):
985         self._activate_mpl_if_needed()
986
987         thread = threading.currentThread()
988         from _pydevd_bundle import pydevd_frame_utils
989         frame = pydevd_frame_utils.Frame(None, -1, pydevd_frame_utils.FCode("Console",
990                                                                             os.path.abspath(os.path.dirname(__file__))), globals, globals)
991         thread_id = get_thread_id(thread)
992         from _pydevd_bundle import pydevd_vars
993         pydevd_vars.add_additional_frame_by_id(thread_id, {id(frame): frame})
994
995         cmd = self.cmd_factory.make_show_console_message(thread_id, frame)
996         self.writer.add_command(cmd)
997
998         while True:
999             if self.mpl_in_use:
1000                 # call input hooks if only matplotlib is in use
1001                 self._call_mpl_hook()
1002             self.process_internal_commands()
1003             time.sleep(0.01)
1004
1005     trace_dispatch = _trace_dispatch
1006
1007 def set_debug(setup):
1008     setup['DEBUG_RECORD_SOCKET_READS'] = True
1009     setup['DEBUG_TRACE_BREAKPOINTS'] = 1
1010     setup['DEBUG_TRACE_LEVEL'] = 3
1011
1012
1013 def enable_qt_support():
1014     from _pydev_bundle import pydev_monkey_qt
1015     pydev_monkey_qt.patch_qt()
1016
1017
1018 def process_command_line(argv):
1019     """ parses the arguments.
1020         removes our arguments from the command line """
1021     setup = {}
1022     setup['client'] = None
1023     setup['server'] = False
1024     setup['port'] = 0
1025     setup['file'] = ''
1026     setup['multiproc'] = False #Used by PyCharm (reuses connection: ssh tunneling)
1027     setup['multiprocess'] = False # Used by PyDev (creates new connection to ide)
1028     setup['save-signatures'] = False
1029     setup['save-threading'] = False
1030     setup['save-asyncio'] = False
1031     setup['qt-support'] = False
1032     setup['print-in-debugger-startup'] = False
1033     setup['cmd-line'] = False
1034     setup['module'] = False
1035     i = 0
1036     del argv[0]
1037     while (i < len(argv)):
1038         if argv[i] == '--port':
1039             del argv[i]
1040             setup['port'] = int(argv[i])
1041             del argv[i]
1042         elif argv[i] == '--vm_type':
1043             del argv[i]
1044             setup['vm_type'] = argv[i]
1045             del argv[i]
1046         elif argv[i] == '--client':
1047             del argv[i]
1048             setup['client'] = argv[i]
1049             del argv[i]
1050         elif argv[i] == '--server':
1051             del argv[i]
1052             setup['server'] = True
1053         elif argv[i] == '--file':
1054             del argv[i]
1055             setup['file'] = argv[i]
1056             i = len(argv) # pop out, file is our last argument
1057         elif argv[i] == '--DEBUG_RECORD_SOCKET_READS':
1058             del argv[i]
1059             setup['DEBUG_RECORD_SOCKET_READS'] = True
1060         elif argv[i] == '--DEBUG':
1061             del argv[i]
1062             set_debug(setup)
1063         elif argv[i] == '--multiproc':
1064             del argv[i]
1065             setup['multiproc'] = True
1066         elif argv[i] == '--multiprocess':
1067             del argv[i]
1068             setup['multiprocess'] = True
1069         elif argv[i] == '--save-signatures':
1070             del argv[i]
1071             setup['save-signatures'] = True
1072         elif argv[i] == '--save-threading':
1073             del argv[i]
1074             setup['save-threading'] = True
1075         elif argv[i] == '--save-asyncio':
1076             del argv[i]
1077             setup['save-asyncio'] = True
1078         elif argv[i] == '--qt-support':
1079             del argv[i]
1080             setup['qt-support'] = True
1081
1082         elif argv[i] == '--print-in-debugger-startup':
1083             del argv[i]
1084             setup['print-in-debugger-startup'] = True
1085         elif (argv[i] == '--cmd-line'):
1086             del argv[i]
1087             setup['cmd-line'] = True
1088         elif (argv[i] == '--module'):
1089             del argv[i]
1090             setup['module'] = True
1091         else:
1092             raise ValueError("unexpected option " + argv[i])
1093     return setup
1094
1095 def usage(doExit=0):
1096     sys.stdout.write('Usage:\n')
1097     sys.stdout.write('pydevd.py --port=N [(--client hostname) | --server] --file executable [file_options]\n')
1098     if doExit:
1099         sys.exit(0)
1100
1101
1102 def init_stdout_redirect():
1103     if not getattr(sys, 'stdoutBuf', None):
1104         sys.stdoutBuf = pydevd_io.IOBuf()
1105         sys.stdout_original = sys.stdout
1106         sys.stdout = pydevd_io.IORedirector(sys.stdout, sys.stdoutBuf) #@UndefinedVariable
1107
1108 def init_stderr_redirect():
1109     if not getattr(sys, 'stderrBuf', None):
1110         sys.stderrBuf = pydevd_io.IOBuf()
1111         sys.stderr_original = sys.stderr
1112         sys.stderr = pydevd_io.IORedirector(sys.stderr, sys.stderrBuf) #@UndefinedVariable
1113
1114
1115 def has_data_to_redirect():
1116     if getattr(sys, 'stdoutBuf', None):
1117         if not sys.stdoutBuf.empty():
1118             return True
1119     if getattr(sys, 'stderrBuf', None):
1120         if not sys.stderrBuf.empty():
1121             return True
1122
1123     return False
1124
1125 #=======================================================================================================================
1126 # settrace
1127 #=======================================================================================================================
1128 def settrace(
1129         host=None,
1130         stdoutToServer=False,
1131         stderrToServer=False,
1132         port=5678,
1133         suspend=True,
1134         trace_only_current_thread=False,
1135         overwrite_prev_trace=False,
1136         patch_multiprocessing=False,
1137 ):
1138     '''Sets the tracing function with the pydev debug function and initializes needed facilities.
1139
1140     @param host: the user may specify another host, if the debug server is not in the same machine (default is the local
1141         host)
1142
1143     @param stdoutToServer: when this is true, the stdout is passed to the debug server
1144
1145     @param stderrToServer: when this is true, the stderr is passed to the debug server
1146         so that they are printed in its console and not in this process console.
1147
1148     @param port: specifies which port to use for communicating with the server (note that the server must be started
1149         in the same port). @note: currently it's hard-coded at 5678 in the client
1150
1151     @param suspend: whether a breakpoint should be emulated as soon as this function is called.
1152
1153     @param trace_only_current_thread: determines if only the current thread will be traced or all current and future
1154         threads will also have the tracing enabled.
1155
1156     @param overwrite_prev_trace: if True we'll reset the frame.f_trace of frames which are already being traced
1157
1158     @param patch_multiprocessing: if True we'll patch the functions which create new processes so that launched
1159         processes are debugged.
1160     '''
1161     _set_trace_lock.acquire()
1162     try:
1163         _locked_settrace(
1164                 host,
1165                 stdoutToServer,
1166                 stderrToServer,
1167                 port,
1168                 suspend,
1169                 trace_only_current_thread,
1170                 overwrite_prev_trace,
1171                 patch_multiprocessing,
1172         )
1173     finally:
1174         _set_trace_lock.release()
1175
1176
1177
1178 _set_trace_lock = thread.allocate_lock()
1179
1180 def _locked_settrace(
1181         host,
1182         stdoutToServer,
1183         stderrToServer,
1184         port,
1185         suspend,
1186         trace_only_current_thread,
1187         overwrite_prev_trace,
1188         patch_multiprocessing,
1189 ):
1190     if patch_multiprocessing:
1191         try:
1192             from _pydev_bundle import pydev_monkey
1193         except:
1194             pass
1195         else:
1196             pydev_monkey.patch_new_process_functions()
1197
1198     global connected
1199     global bufferStdOutToServer
1200     global bufferStdErrToServer
1201
1202     if not connected :
1203         pydevd_vm_type.setup_type()
1204
1205         debugger = PyDB()
1206         debugger.connect(host, port)  # Note: connect can raise error.
1207
1208         # Mark connected only if it actually succeeded.
1209         connected = True
1210         bufferStdOutToServer = stdoutToServer
1211         bufferStdErrToServer = stderrToServer
1212
1213         if bufferStdOutToServer:
1214             init_stdout_redirect()
1215
1216         if bufferStdErrToServer:
1217             init_stderr_redirect()
1218
1219         patch_stdin(debugger)
1220         debugger.set_trace_for_frame_and_parents(get_frame(), False, overwrite_prev_trace=overwrite_prev_trace)
1221
1222
1223         CustomFramesContainer.custom_frames_lock.acquire()  # @UndefinedVariable
1224         try:
1225             for _frameId, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames):
1226                 debugger.set_trace_for_frame_and_parents(custom_frame.frame, False)
1227         finally:
1228             CustomFramesContainer.custom_frames_lock.release()  # @UndefinedVariable
1229
1230
1231         t = threadingCurrentThread()
1232         try:
1233             additional_info = t.additional_info
1234         except AttributeError:
1235             additional_info = PyDBAdditionalThreadInfo()
1236             t.additional_info = additional_info
1237
1238         while not debugger.ready_to_run:
1239             time.sleep(0.1)  # busy wait until we receive run command
1240
1241         # note that we do that through pydevd_tracing.SetTrace so that the tracing
1242         # is not warned to the user!
1243         pydevd_tracing.SetTrace(debugger.trace_dispatch)
1244
1245         if not trace_only_current_thread:
1246             # Trace future threads?
1247             debugger.patch_threads()
1248
1249             # As this is the first connection, also set tracing for any untraced threads
1250             debugger.set_tracing_for_untraced_contexts(ignore_frame=get_frame(), overwrite_prev_trace=overwrite_prev_trace)
1251
1252         # Stop the tracing as the last thing before the actual shutdown for a clean exit.
1253         atexit.register(stoptrace)
1254
1255         PyDBCommandThread(debugger).start()
1256         CheckOutputThread(debugger).start()
1257
1258         #Suspend as the last thing after all tracing is in place.
1259         if suspend:
1260             debugger.set_suspend(t, CMD_THREAD_SUSPEND)
1261
1262
1263     else:
1264         # ok, we're already in debug mode, with all set, so, let's just set the break
1265         debugger = get_global_debugger()
1266
1267         debugger.set_trace_for_frame_and_parents(get_frame(), False)
1268
1269         t = threadingCurrentThread()
1270         try:
1271             additional_info = t.additional_info
1272         except AttributeError:
1273             additional_info = PyDBAdditionalThreadInfo()
1274             t.additional_info = additional_info
1275
1276         pydevd_tracing.SetTrace(debugger.trace_dispatch)
1277
1278         if not trace_only_current_thread:
1279             # Trace future threads?
1280             debugger.patch_threads()
1281
1282
1283         if suspend:
1284             debugger.set_suspend(t, CMD_THREAD_SUSPEND)
1285
1286
1287 def stoptrace():
1288     global connected
1289     if connected:
1290         pydevd_tracing.restore_sys_set_trace_func()
1291         sys.settrace(None)
1292         try:
1293             #not available in jython!
1294             threading.settrace(None) # for all future threads
1295         except:
1296             pass
1297
1298         from _pydev_bundle.pydev_monkey import undo_patch_thread_modules
1299         undo_patch_thread_modules()
1300
1301         debugger = get_global_debugger()
1302
1303         if debugger:
1304
1305             debugger.set_trace_for_frame_and_parents(
1306                     get_frame(), also_add_to_passed_frame=True, overwrite_prev_trace=True, dispatch_func=lambda *args:None)
1307             debugger.exiting()
1308
1309             kill_all_pydev_threads()
1310
1311         connected = False
1312
1313 class Dispatcher(object):
1314     def __init__(self):
1315         self.port = None
1316
1317     def connect(self, host, port):
1318         self.host  = host
1319         self.port = port
1320         self.client = start_client(self.host, self.port)
1321         self.reader = DispatchReader(self)
1322         self.reader.pydev_do_not_trace = False #we run reader in the same thread so we don't want to loose tracing
1323         self.reader.run()
1324
1325     def close(self):
1326         try:
1327             self.reader.do_kill_pydev_thread()
1328         except :
1329             pass
1330
1331 class DispatchReader(ReaderThread):
1332     def __init__(self, dispatcher):
1333         self.dispatcher = dispatcher
1334         ReaderThread.__init__(self, self.dispatcher.client)
1335
1336     def _on_run(self):
1337         dummy_thread = threading.currentThread()
1338         dummy_thread.is_pydev_daemon_thread = False
1339         return ReaderThread._on_run(self)
1340
1341     def handle_except(self):
1342         ReaderThread.handle_except(self)
1343
1344     def process_command(self, cmd_id, seq, text):
1345         if cmd_id == 99:
1346             self.dispatcher.port = int(text)
1347             self.killReceived = True
1348
1349
1350 DISPATCH_APPROACH_NEW_CONNECTION = 1 # Used by PyDev
1351 DISPATCH_APPROACH_EXISTING_CONNECTION = 2 # Used by PyCharm
1352 DISPATCH_APPROACH = DISPATCH_APPROACH_NEW_CONNECTION
1353
1354 def dispatch():
1355     setup = SetupHolder.setup
1356     host = setup['client']
1357     port = setup['port']
1358     if DISPATCH_APPROACH == DISPATCH_APPROACH_EXISTING_CONNECTION:
1359         dispatcher = Dispatcher()
1360         try:
1361             dispatcher.connect(host, port)
1362             port = dispatcher.port
1363         finally:
1364             dispatcher.close()
1365     return host, port
1366
1367
1368 def settrace_forked():
1369     '''
1370     When creating a fork from a process in the debugger, we need to reset the whole debugger environment!
1371     '''
1372     host, port = dispatch()
1373
1374     from _pydevd_bundle import pydevd_tracing
1375     pydevd_tracing.restore_sys_set_trace_func()
1376
1377     if port is not None:
1378         global connected
1379         connected = False
1380
1381         custom_frames_container_init()
1382
1383         settrace(
1384                 host,
1385                 port=port,
1386                 suspend=False,
1387                 trace_only_current_thread=False,
1388                 overwrite_prev_trace=True,
1389                 patch_multiprocessing=True,
1390         )
1391
1392 #=======================================================================================================================
1393 # SetupHolder
1394 #=======================================================================================================================
1395 class SetupHolder:
1396
1397     setup = None
1398
1399
1400 def apply_debugger_options(setup_options):
1401     """
1402
1403     :type setup_options: dict[str, bool]
1404     """
1405     default_options = {'save-signatures': False, 'qt-support': False}
1406     default_options.update(setup_options)
1407     setup_options = default_options
1408
1409     debugger = GetGlobalDebugger()
1410     if setup_options['save-signatures']:
1411         if pydevd_vm_type.get_vm_type() == pydevd_vm_type.PydevdVmType.JYTHON:
1412             sys.stderr.write("Collecting run-time type information is not supported for Jython\n")
1413         else:
1414             # Only import it if we're going to use it!
1415             from _pydevd_bundle.pydevd_signature import SignatureFactory
1416             debugger.signature_factory = SignatureFactory()
1417
1418     if setup_options['qt-support']:
1419         enable_qt_support()
1420
1421
1422 def patch_stdin(debugger):
1423     from _pydev_bundle.pydev_console_utils import DebugConsoleStdIn
1424     orig_stdin = sys.stdin
1425     sys.stdin = DebugConsoleStdIn(debugger, orig_stdin)
1426
1427
1428 #=======================================================================================================================
1429 # main
1430 #=======================================================================================================================
1431 if __name__ == '__main__':
1432
1433     # parse the command line. --file is our last argument that is required
1434     try:
1435         sys.original_argv = sys.argv[:]
1436         setup = process_command_line(sys.argv)
1437         SetupHolder.setup = setup
1438     except ValueError:
1439         traceback.print_exc()
1440         usage(1)
1441
1442     if setup['print-in-debugger-startup']:
1443         try:
1444             pid = ' (pid: %s)' % os.getpid()
1445         except:
1446             pid = ''
1447         sys.stderr.write("pydev debugger: starting%s\n" % pid)
1448
1449     fix_getpass.fix_getpass()
1450
1451     pydev_log.debug("Executing file %s" % setup['file'])
1452     pydev_log.debug("arguments: %s"% str(sys.argv))
1453
1454
1455     pydevd_vm_type.setup_type(setup.get('vm_type', None))
1456
1457     if os.getenv('PYCHARM_DEBUG') == 'True' or os.getenv('PYDEV_DEBUG') == 'True':
1458         set_debug(setup)
1459
1460     DebugInfoHolder.DEBUG_RECORD_SOCKET_READS = setup.get('DEBUG_RECORD_SOCKET_READS', DebugInfoHolder.DEBUG_RECORD_SOCKET_READS)
1461     DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS = setup.get('DEBUG_TRACE_BREAKPOINTS', DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS)
1462     DebugInfoHolder.DEBUG_TRACE_LEVEL = setup.get('DEBUG_TRACE_LEVEL', DebugInfoHolder.DEBUG_TRACE_LEVEL)
1463
1464     port = setup['port']
1465     host = setup['client']
1466     f = setup['file']
1467     fix_app_engine_debug = False
1468
1469     debugger = PyDB()
1470
1471     try:
1472         from _pydev_bundle import pydev_monkey
1473     except:
1474         pass #Not usable on jython 2.1
1475     else:
1476         if setup['multiprocess']: # PyDev
1477             pydev_monkey.patch_new_process_functions()
1478
1479         elif setup['multiproc']: # PyCharm
1480             pydev_log.debug("Started in multiproc mode\n")
1481             # Note: we're not inside method, so, no need for 'global'
1482             DISPATCH_APPROACH = DISPATCH_APPROACH_EXISTING_CONNECTION
1483
1484             dispatcher = Dispatcher()
1485             try:
1486                 dispatcher.connect(host, port)
1487                 if dispatcher.port is not None:
1488                     port = dispatcher.port
1489                     pydev_log.debug("Received port %d\n" %port)
1490                     pydev_log.info("pydev debugger: process %d is connecting\n"% os.getpid())
1491
1492                     try:
1493                         pydev_monkey.patch_new_process_functions()
1494                     except:
1495                         pydev_log.error("Error patching process functions\n")
1496                         traceback.print_exc()
1497                 else:
1498                     pydev_log.error("pydev debugger: couldn't get port for new debug process\n")
1499             finally:
1500                 dispatcher.close()
1501         else:
1502             pydev_log.info("pydev debugger: starting\n")
1503
1504             try:
1505                 pydev_monkey.patch_new_process_functions_with_warning()
1506             except:
1507                 pydev_log.error("Error patching process functions\n")
1508                 traceback.print_exc()
1509
1510             # Only do this patching if we're not running with multiprocess turned on.
1511             if f.find('dev_appserver.py') != -1:
1512                 if os.path.basename(f).startswith('dev_appserver.py'):
1513                     appserver_dir = os.path.dirname(f)
1514                     version_file = os.path.join(appserver_dir, 'VERSION')
1515                     if os.path.exists(version_file):
1516                         try:
1517                             stream = open(version_file, 'r')
1518                             try:
1519                                 for line in stream.read().splitlines():
1520                                     line = line.strip()
1521                                     if line.startswith('release:'):
1522                                         line = line[8:].strip()
1523                                         version = line.replace('"', '')
1524                                         version = version.split('.')
1525                                         if int(version[0]) > 1:
1526                                             fix_app_engine_debug = True
1527
1528                                         elif int(version[0]) == 1:
1529                                             if int(version[1]) >= 7:
1530                                                 # Only fix from 1.7 onwards
1531                                                 fix_app_engine_debug = True
1532                                         break
1533                             finally:
1534                                 stream.close()
1535                         except:
1536                             traceback.print_exc()
1537
1538     try:
1539         # In the default run (i.e.: run directly on debug mode), we try to patch stackless as soon as possible
1540         # on a run where we have a remote debug, we may have to be more careful because patching stackless means
1541         # that if the user already had a stackless.set_schedule_callback installed, he'd loose it and would need
1542         # to call it again (because stackless provides no way of getting the last function which was registered
1543         # in set_schedule_callback).
1544         #
1545         # So, ideally, if there's an application using stackless and the application wants to use the remote debugger
1546         # and benefit from stackless debugging, the application itself must call:
1547         #
1548         # import pydevd_stackless
1549         # pydevd_stackless.patch_stackless()
1550         #
1551         # itself to be able to benefit from seeing the tasklets created before the remote debugger is attached.
1552         from _pydevd_bundle import pydevd_stackless
1553         pydevd_stackless.patch_stackless()
1554     except:
1555         # It's ok not having stackless there...
1556         try:
1557             sys.exc_clear()  # the exception information should be cleaned in Python 2
1558         except:
1559             pass
1560
1561     is_module = setup['module']
1562     patch_stdin(debugger)
1563
1564     if fix_app_engine_debug:
1565         sys.stderr.write("pydev debugger: google app engine integration enabled\n")
1566         curr_dir = os.path.dirname(__file__)
1567         app_engine_startup_file = os.path.join(curr_dir, 'pydev_app_engine_debug_startup.py')
1568
1569         sys.argv.insert(1, '--python_startup_script=' + app_engine_startup_file)
1570         import json
1571         setup['pydevd'] = __file__
1572         sys.argv.insert(2, '--python_startup_args=%s' % json.dumps(setup),)
1573         sys.argv.insert(3, '--automatic_restart=no')
1574         sys.argv.insert(4, '--max_module_instances=1')
1575
1576         # Run the dev_appserver
1577         debugger.run(setup['file'], None, None, is_module, set_trace=False)
1578     else:
1579         if setup['save-threading']:
1580             debugger.thread_analyser = ThreadingLogger()
1581         if setup['save-asyncio']:
1582             if IS_PY34_OLDER:
1583                 debugger.asyncio_analyser = AsyncioLogger()
1584
1585         apply_debugger_options(setup)
1586
1587         try:
1588             debugger.connect(host, port)
1589         except:
1590             sys.stderr.write("Could not connect to %s: %s\n" % (host, port))
1591             traceback.print_exc()
1592             sys.exit(1)
1593
1594         connected = True  # Mark that we're connected when started from inside ide.
1595
1596         globals = debugger.run(setup['file'], None, None, is_module)
1597
1598         if setup['cmd-line']:
1599             debugger.wait_for_commands(globals)
1600
1601