From 5cb0dc3d392327245aaf2a0c79c2aead49ae2d02 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 21 Oct 2022 12:37:45 -0600 Subject: Fix Python console not working if UI plugins are not available for any reason --- python/scriptingprovider.py | 138 +++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 66 deletions(-) (limited to 'python') diff --git a/python/scriptingprovider.py b/python/scriptingprovider.py index ffc1033d..8cee8035 100644 --- a/python/scriptingprovider.py +++ b/python/scriptingprovider.py @@ -829,77 +829,83 @@ from binaryninja import * self.locals["current_sections"] = None self.locals["current_comment"] = None + ui_locals_valid = False if binaryninja.core_ui_enabled(): - from binaryninjaui import UIContext - from binaryninja import Variable, FunctionGraphType - - context = UIContext.activeContext() - action_handler = None - view_frame = None - view = None - if context is not None: - action_handler = context.getCurrentActionHandler() - view_frame = context.getCurrentViewFrame() - view = context.getCurrentView() - - view_location = view_frame.getViewLocation() if view_frame is not None else None - action_context = None - if view is not None: - action_context = view.actionContext() - elif action_handler is not None: - action_context = action_handler.actionContext() - - token_state = None - token = None - var = None - if action_context is not None: - token_state = action_context.token - token = token_state.token if token_state.valid else None - var = token_state.localVar if token_state.localVarValid else None - if var and self.active_func: - var = Variable.from_core_variable(self.active_func, var) - - if view_location is not None and view_location.isValid(): - self.active_il_index = view_location.getInstrIndex() - ilType = view_location.getILViewType() - if ilType == FunctionGraphType.LowLevelILFunctionGraph: - self.active_il_function = self.locals["current_llil"] - elif ilType == FunctionGraphType.MediumLevelILFunctionGraph: - self.active_il_function = self.locals["current_mlil"] - elif ilType == FunctionGraphType.HighLevelILFunctionGraph: - self.active_il_function = self.locals["current_hlil"] - else: - self.active_il_function = None - - self.locals["current_il_index"] = self.active_il_index - self.locals["current_il_function"] = self.active_il_function - if self.active_il_function: - try: - self.locals["current_il_instruction"] = self.active_il_function[self.active_il_index] - except: + try: + from binaryninjaui import UIContext + from binaryninja import Variable, FunctionGraphType + + context = UIContext.activeContext() + action_handler = None + view_frame = None + view = None + if context is not None: + action_handler = context.getCurrentActionHandler() + view_frame = context.getCurrentViewFrame() + view = context.getCurrentView() + + view_location = view_frame.getViewLocation() if view_frame is not None else None + action_context = None + if view is not None: + action_context = view.actionContext() + elif action_handler is not None: + action_context = action_handler.actionContext() + + token_state = None + token = None + var = None + if action_context is not None: + token_state = action_context.token + token = token_state.token if token_state.valid else None + var = token_state.localVar if token_state.localVarValid else None + if var and self.active_func: + var = Variable.from_core_variable(self.active_func, var) + + if view_location is not None and view_location.isValid(): + self.active_il_index = view_location.getInstrIndex() + ilType = view_location.getILViewType() + if ilType == FunctionGraphType.LowLevelILFunctionGraph: + self.active_il_function = self.locals["current_llil"] + elif ilType == FunctionGraphType.MediumLevelILFunctionGraph: + self.active_il_function = self.locals["current_mlil"] + elif ilType == FunctionGraphType.HighLevelILFunctionGraph: + self.active_il_function = self.locals["current_hlil"] + else: + self.active_il_function = None + + self.locals["current_il_index"] = self.active_il_index + self.locals["current_il_function"] = self.active_il_function + if self.active_il_function: + try: + self.locals["current_il_instruction"] = self.active_il_function[self.active_il_index] + except: + self.locals["current_il_instruction"] = None + + if self.locals["current_il_instruction"]: + self.locals["current_il_basic_block"] = self.locals["current_il_instruction"].il_basic_block + else: self.locals["current_il_instruction"] = None - - if self.locals["current_il_instruction"]: - self.locals["current_il_basic_block"] = self.locals["current_il_instruction"].il_basic_block + self.locals["current_il_basic_block"] = None else: + self.locals["current_il_index"] = None + self.locals["current_il_function"] = None self.locals["current_il_instruction"] = None self.locals["current_il_basic_block"] = None - else: - self.locals["current_il_index"] = None - self.locals["current_il_function"] = None - self.locals["current_il_instruction"] = None - self.locals["current_il_basic_block"] = None - self.active_il_function = None - - self.locals["current_ui_context"] = context - self.locals["current_ui_view_frame"] = view_frame - self.locals["current_ui_view"] = view - self.locals["current_ui_action_handler"] = action_handler - self.locals["current_ui_view_location"] = view_location - self.locals["current_ui_action_context"] = action_context - self.locals["current_token"] = token - self.locals["current_variable"] = var - else: + self.active_il_function = None + + self.locals["current_ui_context"] = context + self.locals["current_ui_view_frame"] = view_frame + self.locals["current_ui_view"] = view + self.locals["current_ui_action_handler"] = action_handler + self.locals["current_ui_view_location"] = view_location + self.locals["current_ui_action_context"] = action_context + self.locals["current_token"] = token + self.locals["current_variable"] = var + ui_locals_valid = True + except ImportError: + pass + + if not ui_locals_valid: self.locals["current_ui_context"] = None self.locals["current_ui_view_frame"] = None self.locals["current_ui_view"] = None -- cgit v1.3.1