Esempio n. 1
0
    def greenlet_events(event, args):
        if event in ('switch', 'throw'):
            py_db = get_global_debugger()
            origin, target = args

            if not origin.dead and origin.gr_frame is not None:
                frame_custom_thread_id = _saved_greenlets_to_custom_frame_thread_id.get(
                    origin)
                if frame_custom_thread_id is None:
                    _saved_greenlets_to_custom_frame_thread_id[
                        origin] = add_custom_frame(
                            origin.gr_frame, _get_paused_name(py_db, origin),
                            thread_get_ident())
                else:
                    update_custom_frame(frame_custom_thread_id,
                                        origin.gr_frame,
                                        _get_paused_name(py_db, origin),
                                        thread_get_ident())
            else:
                frame_custom_thread_id = _saved_greenlets_to_custom_frame_thread_id.pop(
                    origin, None)
                if frame_custom_thread_id is not None:
                    remove_custom_frame(frame_custom_thread_id)

            # This one will be resumed, so, remove custom frame from it.
            frame_custom_thread_id = _saved_greenlets_to_custom_frame_thread_id.pop(
                target, None)
            if frame_custom_thread_id is not None:
                remove_custom_frame(frame_custom_thread_id)

        # The tracing needs to be reapplied for each greenlet as gevent
        # clears the tracing set through sys.settrace for each greenlet.
        pydevd_tracing.reapply_settrace()
Esempio n. 2
0
def break_into_debugger():
    """If a remote debugger is attached, pauses execution of all threads,
    and breaks into the debugger with current thread as active.
    """

    ptvsd.log.info('break_into_debugger()')

    if not is_attached():
        ptvsd.log.info('break_into_debugger() ignored - debugger not attached')
        return

    # Get the first frame in the stack that's not an internal frame.
    global_debugger = get_global_debugger()
    stop_at_frame = sys._getframe().f_back
    while stop_at_frame is not None and global_debugger.get_file_type(
            get_abs_path_real_path_and_base_from_frame(stop_at_frame)) == global_debugger.PYDEV_FILE:
        stop_at_frame = stop_at_frame.f_back

    _pydevd_settrace(
        suspend=True,
        trace_only_current_thread=True,
        patch_multiprocessing=False,
        stop_at_frame=stop_at_frame,
    )
    stop_at_frame = None
Esempio n. 3
0
def break_into_debugger():
    """If a remote debugger is attached, pauses execution of all threads,
    and breaks into the debugger with current thread as active.
    """

    ptvsd.log.info('break_into_debugger()')

    if not is_attached():
        ptvsd.log.info('break_into_debugger() ignored - debugger not attached')
        return

    # Get the first frame in the stack that's not an internal frame.
    global_debugger = get_global_debugger()
    stop_at_frame = sys._getframe().f_back
    while stop_at_frame is not None and global_debugger.get_file_type(
            get_abs_path_real_path_and_base_from_frame(
                stop_at_frame)) == global_debugger.PYDEV_FILE:
        stop_at_frame = stop_at_frame.f_back

    _pydevd_settrace(
        suspend=True,
        trace_only_current_thread=True,
        patch_multiprocessing=False,
        stop_at_frame=stop_at_frame,
    )
    stop_at_frame = None
Esempio n. 4
0
    def __call__(self):
        # We monkey-patch the thread creation so that this function is called in the new thread. At this point
        # we notify of its creation and start tracing it.
        global_debugger = get_global_debugger()

        thread_id = None
        if global_debugger is not None:
            # Note: if this is a thread from threading.py, we're too early in the boostrap process (because we mocked
            # the start_new_thread internal machinery and thread._bootstrap has not finished), so, the code below needs
            # to make sure that we use the current thread bound to the original function and not use
            # threading.currentThread() unless we're sure it's a dummy thread.
            t = getattr(self.original_func, '__self__', getattr(self.original_func, 'im_self', None))
            if not isinstance(t, threading.Thread):
                # This is not a threading.Thread but a Dummy thread (so, get it as a dummy thread using
                # currentThread).
                t = threading.currentThread()
                
            if not getattr(t, 'is_pydev_daemon_thread', False):
                thread_id = get_thread_id(t)
                global_debugger.notify_thread_created(thread_id, t)
                _on_set_trace_for_new_thread(global_debugger)
            
            if getattr(global_debugger, 'thread_analyser', None) is not None:
                try:
                    from pydevd_concurrency_analyser.pydevd_concurrency_logger import log_new_thread
                    log_new_thread(global_debugger, t)
                except:
                    sys.stderr.write("Failed to detect new thread for visualization")
        try:
            ret = self.original_func(*self.args, **self.kwargs)
        finally:
            if thread_id is not None:
                global_debugger.notify_thread_not_alive(thread_id)
        
        return ret
Esempio n. 5
0
    async def debug_start(call: Optional[ServiceCall] = None) -> None:
        """Start the debugger."""
        _LOGGER.info(f"Activating PyCharm Remote Debugger for {host}:{port}")

        debugger = get_global_debugger()
        if debugger:
            _LOGGER.warning(f"Found running PyDB instance, stopping it now")
            _LOGGER.debug(f"connected={connected}")
            stoptrace()

            while connected:
                await asyncio.sleep(0.1)

        # DebugInfoHolder.DEBUG_RECORD_SOCKET_READS = True
        # DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS = 3
        # DebugInfoHolder.DEBUG_TRACE_LEVEL = 3

        try:
            pydevd_pycharm.settrace(
                host=host,
                port=port,
                stdoutToServer=False,
                stderrToServer=False,
                suspend=wait,
                trace_only_current_thread=False,
                patch_multiprocessing=False,
            )
        except (ConnectionRefusedError, OSError, socket.gaierror) as e:
            _LOGGER.warning(
                "Failed to connect Remote Debugger with PyCharm IDE")
Esempio n. 6
0
    def make_thread_stack_str(self, frame, frame_id_to_lineno=None):
        '''
        :param frame_id_to_lineno:
            If available, the line number for the frame will be gotten from this dict,
            otherwise frame.f_lineno will be used (needed for unhandled exceptions as
            the place where we report may be different from the place where it's raised).
        '''
        if frame_id_to_lineno is None:
            frame_id_to_lineno = {}
        make_valid_xml_value = pydevd_xml.make_valid_xml_value
        cmd_text_list = []
        append = cmd_text_list.append

        curr_frame = frame
        frame = None  # Clear frame reference
        try:
            py_db = get_global_debugger()
            for frame_id, frame, method_name, _original_filename, filename_in_utf8, lineno in self._iter_visible_frames_info(
                    py_db, curr_frame, frame_id_to_lineno):

                # print("file is ", filename_in_utf8)
                # print("line is ", lineno)

                # Note: variables are all gotten 'on-demand'.
                append('<frame id="%s" name="%s" ' %
                       (frame_id, make_valid_xml_value(method_name)))
                append('file="%s" line="%s">' %
                       (quote(make_valid_xml_value(filename_in_utf8),
                              '/>_= \t'), lineno))
                append("</frame>")
        except:
            traceback.print_exc()

        curr_frame = None  # Clear frame reference
        return ''.join(cmd_text_list)
    def make_thread_stack_str(self, frame, frame_id_to_lineno=None):
        '''
        :param frame_id_to_lineno:
            If available, the line number for the frame will be gotten from this dict,
            otherwise frame.f_lineno will be used (needed for unhandled exceptions as
            the place where we report may be different from the place where it's raised).
        '''
        if frame_id_to_lineno is None:
            frame_id_to_lineno = {}
        make_valid_xml_value = pydevd_xml.make_valid_xml_value
        cmd_text_list = []
        append = cmd_text_list.append

        curr_frame = frame
        frame = None  # Clear frame reference
        try:
            py_db = get_global_debugger()
            for frame_id, frame, method_name, filename_in_utf8, lineno in self._iter_visible_frames_info(
                    py_db, curr_frame, frame_id_to_lineno
                ):

                # print("file is ", filename_in_utf8)
                # print("line is ", lineno)

                # Note: variables are all gotten 'on-demand'.
                append('<frame id="%s" name="%s" ' % (frame_id , make_valid_xml_value(method_name)))
                append('file="%s" line="%s">' % (quote(make_valid_xml_value(filename_in_utf8), '/>_= \t'), lineno))
                append("</frame>")
        except:
            traceback.print_exc()

        curr_frame = None  # Clear frame reference
        return ''.join(cmd_text_list)
Esempio n. 8
0
    def __call__(self):
        # We monkey-patch the thread creation so that this function is called in the new thread. At this point
        # we notify of its creation and start tracing it.
        global_debugger = get_global_debugger()

        thread_id = None
        if global_debugger is not None:
            # Note: if this is a thread from threading.py, we're too early in the boostrap process (because we mocked
            # the start_new_thread internal machinery and thread._bootstrap has not finished), so, the code below needs
            # to make sure that we use the current thread bound to the original function and not use
            # threading.currentThread() unless we're sure it's a dummy thread.
            t = getattr(self.original_func, '__self__', getattr(self.original_func, 'im_self', None))
            if not isinstance(t, threading.Thread):
                # This is not a threading.Thread but a Dummy thread (so, get it as a dummy thread using
                # currentThread).
                t = threading.currentThread()

            if not getattr(t, 'is_pydev_daemon_thread', False):
                thread_id = get_current_thread_id(t)
                global_debugger.notify_thread_created(thread_id, t)
                _on_set_trace_for_new_thread(global_debugger)

            if getattr(global_debugger, 'thread_analyser', None) is not None:
                try:
                    from pydevd_concurrency_analyser.pydevd_concurrency_logger import log_new_thread
                    log_new_thread(global_debugger, t)
                except:
                    sys.stderr.write("Failed to detect new thread for visualization")
        try:
            ret = self.original_func(*self.args, **self.kwargs)
        finally:
            if thread_id is not None:
                global_debugger.notify_thread_not_alive(thread_id)

        return ret
Esempio n. 9
0
def break_into_debugger():
    """If a remote debugger is attached, pauses execution of all threads,
    and breaks into the debugger with current thread as active.
    """

    ptvsd.log.info('break_into_debugger()')

    if not is_attached():
        ptvsd.log.info('break_into_debugger() ignored - debugger not attached')
        return

    # Get the first frame in the stack that's not an internal frame.
    global_debugger = get_global_debugger()
    stop_at_frame = sys._getframe().f_back
    while stop_at_frame is not None and global_debugger.get_file_type(
            stop_at_frame) == global_debugger.PYDEV_FILE:
        stop_at_frame = stop_at_frame.f_back

    # pydevd.settrace() only enables debugging of the current
    # thread and all future threads.  PyDevd is not enabled for
    # existing threads (other than the current one).  Consequently,
    # pydevd.settrace() must be called ASAP in the current thread.
    # See issue #509.
    #
    # This is tricky, however, because settrace() will block until
    # it receives a CMD_RUN message.  You can't just call it in a
    # thread to avoid blocking; doing so would prevent the current
    # thread from being debugged.
    pydevd.settrace(
        suspend=True,
        trace_only_current_thread=True,
        patch_multiprocessing=False,
        stop_at_frame=stop_at_frame,
    )
    stop_at_frame = None
Esempio n. 10
0
def send_process_will_be_substituted():
    """Sends a message that a new process is going to be created.
    When `PyDB` works in server mode this method also waits for the
    response from IDE to be sure that IDE received this message.
    """
    from _pydevd_bundle.pydevd_comm import get_global_debugger
    debugger = get_global_debugger()
    if debugger is not None:
        debugger.send_process_will_be_substituted()
 def __send_input_requested_message(self, is_started):
     try:
         py_db = self._py_db
         if py_db is None:
             py_db = get_global_debugger()
         cmd = py_db.cmd_factory.make_input_requested_message(is_started)
         py_db.writer.add_command(cmd)
     except Exception:
         pydev_log.exception()
Esempio n. 12
0
def trace_this_thread(should_trace):
    ensure_logging()
    log.debug("trace_this_thread({0!r})", should_trace)

    pydb = get_global_debugger()
    if should_trace:
        pydb.enable_tracing()
    else:
        pydb.disable_tracing()
Esempio n. 13
0
File: api.py Progetto: int19h/ptvsd
def wait_for_attach():
    log.debug("wait_for_attach()")
    dbg = get_global_debugger()
    if dbg is None:
        raise RuntimeError("wait_for_attach() called before enable_attach()")

    cancel_event = threading.Event()
    ptvsd.wait_for_attach.cancel = wait_for_attach.cancel = cancel_event.set
    pydevd._wait_for_attach(cancel=cancel_event)
Esempio n. 14
0
 def _report_slow(self, compute_msg, *args):
     old = self._curr_time
     new = self._curr_time = time.time()
     diff = new - old
     if diff >= self.min_diff:
         py_db = get_global_debugger()
         if py_db is not None:
             msg = compute_msg(diff, *args)
             py_db.writer.add_command(
                 py_db.cmd_factory.make_warning_message(msg))
Esempio n. 15
0
def tracing(should_trace):
    pydb = get_global_debugger()

    try:
        was_tracing = _tls.is_tracing
    except AttributeError:
        was_tracing = pydb is not None

    if should_trace is None:
        return was_tracing

    # It is possible that IDE attaches after tracing is changed, but before it is
    # restored. In this case, we don't really want to restore the original value,
    # because it will effectively disable tracing for the just-attached IDE. Doing
    # the check outside the function below makes it so that if the original change
    # was a no-op because IDE wasn't attached, restore will be no-op as well, even
    # if IDE has attached by then.

    tid = threading.current_thread().ident
    if pydb is None:
        log.info(
            "ptvsd.tracing() ignored on thread {0} - debugger not attached",
            tid)

        def enable_or_disable(_):
            # Always fetch the fresh value, in case it changes before we restore.
            _tls.is_tracing = get_global_debugger() is not None

    else:

        def enable_or_disable(enable):
            if enable:
                log.info("Enabling tracing on thread {0}", tid)
                pydb.enable_tracing()
            else:
                log.info("Disabling tracing on thread {0}", tid)
                pydb.disable_tracing()
            _tls.is_tracing = enable

    # Context managers don't do anything unless used in a with-statement - that is,
    # even the code up to yield won't run. But we want callers to be able to omit
    # with-statement for this function, if they don't want to restore. So, we apply
    # the change directly out here in the non-generator context, so that it happens
    # immediately - and then return a context manager that is solely for the purpose
    # of restoring the original value, which the caller can use or discard.

    @contextlib.contextmanager
    def restore_tracing():
        try:
            yield
        finally:
            enable_or_disable(was_tracing)

    enable_or_disable(should_trace)
    return restore_tracing()
    def make_thread_stack_str(self, frame, frame_id_to_lineno=None):
        '''
        :param frame_id_to_lineno:
            If available, the line number for the frame will be gotten from this dict,
            otherwise frame.f_lineno will be used (needed for unhandled exceptions as
            the place where we report may be different from the place where it's raised).
        '''
        if frame_id_to_lineno is None:
            frame_id_to_lineno = {}
        make_valid_xml_value = pydevd_xml.make_valid_xml_value
        cmd_text_list = []
        append = cmd_text_list.append

        curr_frame = frame
        frame = None  # Clear frame reference
        try:
            py_db = get_global_debugger()
            while curr_frame:
                frame_id = id(curr_frame)

                if curr_frame.f_code is None:
                    break  # Iron Python sometimes does not have it!

                method_name = curr_frame.f_code.co_name  # method name (if in method) or ? if global
                if method_name is None:
                    break  # Iron Python sometimes does not have it!

                abs_path_real_path_and_base = get_abs_path_real_path_and_base_from_frame(curr_frame)
                if py_db.get_file_type(abs_path_real_path_and_base) == py_db.PYDEV_FILE:
                    # Skip pydevd files.
                    curr_frame = curr_frame.f_back
                    continue

                filename_in_utf8 = pydevd_file_utils.norm_file_to_client(abs_path_real_path_and_base[0])
                if not filesystem_encoding_is_utf8 and hasattr(filename_in_utf8, "decode"):
                    # filename_in_utf8 is a byte string encoded using the file system encoding
                    # convert it to utf8
                    filename_in_utf8 = filename_in_utf8.decode(file_system_encoding).encode("utf-8")

                # print("file is ", filename_in_utf8)

                lineno = frame_id_to_lineno.get(frame_id, curr_frame.f_lineno)
                # print("line is ", lineno)

                # Note: variables are all gotten 'on-demand'.
                append('<frame id="%s" name="%s" ' % (frame_id , make_valid_xml_value(method_name)))
                append('file="%s" line="%s">' % (quote(make_valid_xml_value(filename_in_utf8), '/>_= \t'), lineno))
                append("</frame>")
                curr_frame = curr_frame.f_back
        except:
            traceback.print_exc()

        curr_frame = None  # Clear frame reference
        return ''.join(cmd_text_list)
Esempio n. 17
0
def wait_for_attach():
    log.info("wait_for_attach()")
    dbg = get_global_debugger()
    if not bool(dbg):
        msg = "wait_for_attach() called before enable_attach()."
        log.info(msg)
        raise AssertionError(msg)

    cancel_event = threading.Event()
    ptvsd.wait_for_attach.cancel = wait_for_attach.cancel = cancel_event.set
    pydevd._wait_for_attach(cancel=cancel_event)
Esempio n. 18
0
    def __call__(self):
        # We monkey-patch the thread creation so that this function is called in the new thread. At this point
        # we notify of its creation and start tracing it.
        global_debugger = get_global_debugger()

        thread_id = None
        if global_debugger is not None:
            # Note: if this is a thread from threading.py, we're too early in the boostrap process (because we mocked
            # the start_new_thread internal machinery and thread._bootstrap has not finished), so, the code below needs
            # to make sure that we use the current thread bound to the original function and not use
            # threading.currentThread() unless we're sure it's a dummy thread.
            t = getattr(self.original_func, '__self__',
                        getattr(self.original_func, 'im_self', None))
            if not isinstance(t, threading.Thread):
                # This is not a threading.Thread but a Dummy thread (so, get it as a dummy thread using
                # currentThread).
                t = threading.currentThread()

            if not getattr(t, 'is_pydev_daemon_thread', False):
                thread_id = get_current_thread_id(t)
                global_debugger.notify_thread_created(thread_id, t)
                _on_set_trace_for_new_thread(global_debugger)

            if getattr(global_debugger, 'thread_analyser', None) is not None:
                try:
                    from pydevd_concurrency_analyser.pydevd_concurrency_logger import log_new_thread
                    log_new_thread(global_debugger, t)
                except:
                    sys.stderr.write(
                        "Failed to detect new thread for visualization")
        try:
            try:
                ret = self.original_func(*self.args, **self.kwargs)
            except:
                # If threads die with the debugger alive, it's possible that we
                # have exceptions during teardown (Python goes through each module
                # and sets their attributes to None). In this situation, don't
                # report spurious exceptions because of that.
                if sys is None or pydevd_tracing is None:
                    return
        finally:
            if sys is None or pydevd_tracing is None:
                return
            else:
                if thread_id is not None and global_debugger is not None:
                    global_debugger.notify_thread_not_alive(thread_id)
                frame = sys._getframe()
                while frame is not None:
                    if frame.f_trace is not None:
                        frame.f_trace = NO_FTRACE
                    frame = frame.f_back
                pydevd_tracing.SetTrace(None)

        return ret
Esempio n. 19
0
def wait_for_client():
    ensure_logging()
    log.debug("wait_for_client()")

    pydb = get_global_debugger()
    if pydb is None:
        raise RuntimeError("listen() or connect() must be called first")

    cancel_event = threading.Event()
    debugpy.wait_for_client.cancel = wait_for_client.cancel = cancel_event.set
    pydevd._wait_for_attach(cancel=cancel_event)
Esempio n. 20
0
    def make_thread_stack_str(self, frame, frame_to_lineno=None):
        '''
        :param frame_to_lineno:
            If available, the line number for the frame will be gotten from this dict,
            otherwise frame.f_lineno will be used (needed for unhandled exceptions as
            the place where we report may be different from the place where it's raised).
        '''
        if frame_to_lineno is None:
            frame_to_lineno = {}
        make_valid_xml_value = pydevd_xml.make_valid_xml_value
        cmd_text_list = []
        append = cmd_text_list.append

        curr_frame = frame
        frame = None  # Clear frame reference
        try:
            py_db = get_global_debugger()
            while curr_frame:
                my_id = id(curr_frame)

                if curr_frame.f_code is None:
                    break  # Iron Python sometimes does not have it!

                method_name = curr_frame.f_code.co_name  # method name (if in method) or ? if global
                if method_name is None:
                    break  # Iron Python sometimes does not have it!

                abs_path_real_path_and_base = get_abs_path_real_path_and_base_from_frame(curr_frame)
                if py_db.get_file_type(abs_path_real_path_and_base) == py_db.PYDEV_FILE:
                    # Skip pydevd files.
                    curr_frame = curr_frame.f_back
                    continue

                filename_in_utf8 = pydevd_file_utils.norm_file_to_client(abs_path_real_path_and_base[0])
                if not filesystem_encoding_is_utf8 and hasattr(filename_in_utf8, "decode"):
                    # filename_in_utf8 is a byte string encoded using the file system encoding
                    # convert it to utf8
                    filename_in_utf8 = filename_in_utf8.decode(file_system_encoding).encode("utf-8")

                # print("file is ", filename_in_utf8)

                lineno = frame_to_lineno.get(curr_frame, curr_frame.f_lineno)
                # print("line is ", lineno)

                # Note: variables are all gotten 'on-demand'.
                append('<frame id="%s" name="%s" ' % (my_id , make_valid_xml_value(method_name)))
                append('file="%s" line="%s">' % (quote(make_valid_xml_value(filename_in_utf8), '/>_= \t'), lineno))
                append("</frame>")
                curr_frame = curr_frame.f_back
        except:
            traceback.print_exc()

        curr_frame = None  # Clear frame reference
        return ''.join(cmd_text_list)
Esempio n. 21
0
def write_err(*args):
    py_db = get_global_debugger()
    if py_db is not None:
        new_lst = []
        for a in args:
            new_lst.append(str(a))

        msg = ' '.join(new_lst)
        s = 'code reload: %s\n' % (msg,)
        cmd = py_db.cmd_factory.make_io_message(s, 2)
        if py_db.writer is not None:
            py_db.writer.add_command(cmd)
Esempio n. 22
0
def _fallback_excepthook(exctype, value, tb):
    pydev_log.debug("Handling the uncaught exception in the fallback exception hook")
    try:
        debugger = get_global_debugger()
        if debugger and debugger.break_on_uncaught_exceptions:
            thread = threading.currentThread()
            additional_info = getattr(thread, 'additional_info', None)
            if not thread or additional_info is None:
                return
            debugger.disable_tracing()
            stop_on_unhandled_exception(debugger, thread, additional_info, (exctype, value, tb))
    finally:
        if sys.excepthook != dummy_excepthook:
            original_excepthook(exctype, value, tb)
        sys.exit(1)
Esempio n. 23
0
    def new_warn_multiproc(*args):
        import os

        warn_multiproc()

        result = getattr(os, original_name)(*args)

        if original_name == _ORIGINAL_PREFIX + 'fork':
            pid = result
            # If automatic attaching to new processes is disabled, it is important to stop tracing in the child process. The reason is the
            # "forked" instance of the debugger can potentially hit a breakpoint, which results in the process hanging.
            if pid == 0:
                debugger = get_global_debugger()
                if debugger:
                    debugger.stoptrace()
            return pid
        else:
            return result
Esempio n. 24
0
def breakpoint():
    ensure_logging()
    if not is_client_connected():
        log.info("breakpoint() ignored - debugger not attached")
        return
    log.debug("breakpoint()")

    # Get the first frame in the stack that's not an internal frame.
    pydb = get_global_debugger()
    stop_at_frame = sys._getframe().f_back
    while (stop_at_frame is not None
           and pydb.get_file_type(stop_at_frame) == pydb.PYDEV_FILE):
        stop_at_frame = stop_at_frame.f_back

    _settrace(
        suspend=True,
        trace_only_current_thread=True,
        patch_multiprocessing=False,
        stop_at_frame=stop_at_frame,
    )
    stop_at_frame = None
    def loadFullValue(self, seq, scope_attrs):
        """
        Evaluate full value for async Console variables in a separate thread and send results to IDE side
        :param seq: id of command
        :param scope_attrs: a sequence of variables with their attributes separated by NEXT_VALUE_SEPARATOR
        (i.e.: obj\tattr1\tattr2NEXT_VALUE_SEPARATORobj2\attr1\tattr2)
        :return:
        """
        frame_variables = self.get_namespace()
        var_objects = []
        vars = scope_attrs.split(NEXT_VALUE_SEPARATOR)
        for var_attrs in vars:
            if '\t' in var_attrs:
                name, attrs = var_attrs.split('\t', 1)

            else:
                name = var_attrs
                attrs = None
            if name in frame_variables:
                var_object = pydevd_vars.resolve_var_object(
                    frame_variables[name], attrs)
                var_objects.append((var_object, name))
            else:
                var_object = pydevd_vars.eval_in_context(
                    name, frame_variables, frame_variables)
                var_objects.append((var_object, name))

        from _pydevd_bundle.pydevd_comm import GetValueAsyncThreadConsole
        py_db = getattr(self, 'debugger', None)

        if py_db is None:
            py_db = get_global_debugger()

        if py_db is None:
            from pydevd import PyDB
            py_db = PyDB()

        t = GetValueAsyncThreadConsole(py_db, self.get_server(), seq,
                                       var_objects)
        t.start()
Esempio n. 26
0
def break_into_debugger():
    log.debug("break_into_debugger()")

    if not is_attached():
        log.info("break_into_debugger() ignored - debugger not attached")
        return

    # Get the first frame in the stack that's not an internal frame.
    global_debugger = get_global_debugger()
    stop_at_frame = sys._getframe().f_back
    while (stop_at_frame is not None
           and global_debugger.get_file_type(stop_at_frame)
           == global_debugger.PYDEV_FILE):
        stop_at_frame = stop_at_frame.f_back

    _settrace(
        suspend=True,
        trace_only_current_thread=True,
        patch_multiprocessing=False,
        stop_at_frame=stop_at_frame,
    )
    stop_at_frame = None
Esempio n. 27
0
def _clear_caches_related_to_scope_changes():
    # Clear related caches.
    _FILENAME_TO_IN_SCOPE_CACHE.clear()
    debugger = get_global_debugger()
    if debugger is not None:
        debugger.clear_skip_caches()
Esempio n. 28
0
File: api.py Progetto: int19h/ptvsd
 def enable_or_disable(_):
     # Always fetch the fresh value, in case it changes before we restore.
     _tls.is_tracing = get_global_debugger() is not None
Esempio n. 29
0
def send_process_created_message():
    debugger = get_global_debugger()
    if debugger is not None:
        debugger.send_process_created_message()
Esempio n. 30
0
def _clear_caches_related_to_scope_changes():
    # Clear related caches.
    _FILENAME_TO_IN_SCOPE_CACHE.clear()
    debugger = get_global_debugger()
    if debugger is not None:
        debugger.clear_skip_caches()
Esempio n. 31
0
def tracing(should_trace=None):
    """Enables or disables tracing on this thread. When called without an
    argument, returns the current tracing state.

    When tracing is disabled, breakpoints will not be hit, but code executes
    significantly faster.

    If debugger is not attached, this function has no effect.

    This function can also be used in a with-statement to automatically save
    and then restore the previous tracing setting::

        with ptvsd.tracing(False):
            # Tracing disabled
            ...
            # Tracing restored

    Parameters
    ----------
    should_trace : bool, optional
        Whether to enable or disable tracing.
    """

    pydb = pydevd_constants.get_global_debugger()

    try:
        was_tracing = _tls.is_tracing
    except AttributeError:
        was_tracing = pydb is not None

    if should_trace is None:
        return was_tracing

    # It is possible that IDE attaches after tracing is changed, but before it is
    # restored. In this case, we don't really want to restore the original value,
    # because it will effectively disable tracing for the just-attached IDE. Doing
    # the check outside the function below makes it so that if the original change
    # was a no-op because IDE wasn't attached, restore will be no-op as well, even
    # if IDE has attached by then.

    tid = threading.current_thread().ident
    if pydb is None:
        log.info(
            "ptvsd.tracing() ignored on thread {0} - debugger not attached",
            tid)

        def enable_or_disable(_):
            # Always fetch the fresh value, in case it changes before we restore.
            _tls.is_tracing = pydevd_constants.get_global_debugger(
            ) is not None
    else:

        def enable_or_disable(enable):
            if enable:
                log.info("Enabling tracing on thread {0}", tid)
                pydb.enable_tracing()
            else:
                log.info("Disabling tracing on thread {0}", tid)
                pydb.disable_tracing()
            _tls.is_tracing = enable

    # Context managers don't do anything unless used in a with-statement - that is,
    # even the code up to yield won't run. But we want callers to be able to omit
    # with-statement for this function, if they don't want to restore. So, we apply
    # the change directly out here in the non-generator context, so that it happens
    # immediately - and then return a context manager that is solely for the purpose
    # of restoring the original value, which the caller can use or discard.

    @contextlib.contextmanager
    def restore_tracing():
        try:
            yield
        finally:
            enable_or_disable(was_tracing)

    enable_or_disable(should_trace)
    return restore_tracing()
Esempio n. 32
0
 def get_pydb(self):
     # Note: separate method for mocking on tests.
     return get_global_debugger()
Esempio n. 33
0
def is_in_unittests_debugging_mode():
    debugger = get_global_debugger()
    if debugger:
        return debugger.stop_on_failed_tests
Esempio n. 34
0
def send_process_created_message():
    debugger = get_global_debugger()
    if debugger is not None:
        debugger.send_process_created_message()
Esempio n. 35
0
def send_process_created_message():
    py_db = get_global_debugger()
    if py_db is not None:
        py_db.send_process_created_message()
Esempio n. 36
0
- all thread tracing functions are reset on fork so the original debugger instance does not leak through them

.. note:: Meaningful only when frame evaluation is enabled.

"""
import os
import sys

from _pydevd_bundle.pydevd_constants import get_global_debugger
from _pydevd_frame_eval.pydevd_frame_eval_cython_wrapper import get_thread_info_py


def _get_thread_trace_func_debugger():
    thread_info = get_thread_info_py()
    # noinspection PyProtectedMember
    return thread_info.thread_trace_func._args[0]


if __name__ == '__main__':
    original_debugger = get_global_debugger()
    pid = os.fork()  # break here
    if pid == 0:
        debugger_in_fork = get_global_debugger()
        assert original_debugger is not debugger_in_fork, "The debugger in the forked process is a copy of the original debugger."
        # noinspection PyProtectedMember,PyUnresolvedReferences,PyUnresolvedReferences
        frame = sys._getframe()
        assert frame.f_trace is not None, "The trace function expected to be set, but it is `None`."
        assert original_debugger is not _get_thread_trace_func_debugger(), "The thread trace function uses the debugger from the parent " \
                                                                           "process. "
        print('TEST SUCEEDED')