<gh_stars>1-10
#
# Copyright (C) [2020-2021] Futurewei Technologies, Inc. All rights reserved.
#
# OpenArkCompiler is licensed underthe Mulan Permissive Software License v2.
# You can use this software according to the terms and conditions of the MulanPSL - 2.0.
# You may obtain a copy of MulanPSL - 2.0 at:
#
#   https://opensource.org/licenses/MulanPSL-2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the MulanPSL - 2.0 for more details.
#

# ---------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
#
# # To use this in the embedded python interpreter using "lldb" just
# import it with the full path using the "command script import"
# command
#   (lldb) command script import /path/to/cmdtemplate.py
# ---------------------------------------------------------------------

from __future__ import print_function

import inspect
import lldb
import optparse
import shlex
import sys
import m_config_lldb


class MNextiCommand:
    program = 'mnexti'

    @classmethod
    def register_lldb_command(cls, debugger, module_name):
        parser = cls.create_options()
        cls.__doc__ = parser.format_help()
        # Add any commands contained in this module to LLDB
        command = 'command script add -c %s.%s %s' % (module_name,
                                                      cls.__name__,
                                                      cls.program)
        debugger.HandleCommand(command)
        if m_config_lldb.debug:
            print('The "{0}" command has been installed, type "help {0}" or "{0} '
                  '--help" for detailed help.'.format(cls.program))
        command = 'command alias mni mnexti'
        debugger.HandleCommand(command)

    @classmethod
    def create_options(cls):

        usage = "usage: %prog [options]"
        description = ('Basically:  mnexti: Steps one Maple instruction, but proceeds through subroutine calls')

        """Steps one Maple instruction, but proceed through subroutine calls
        mnexti: Steps one Maple instruction, but proceed through subroutine calls
        """

        # Pass add_help_option = False, since this keeps the command in line
        #  with lldb commands, and we wire up "help command" to work by
        # providing the long & short help methods below.
        parser = optparse.OptionParser(
            description=description,
            prog=cls.program,
            usage=usage,
            add_help_option=False)

        parser.add_option(
            '-s',
            '--show',
            action='store_true',
            dest='show_next',
            help=': show next Maple instruction before steping',
            default=False)

        parser.add_option(
            '-h',
            '--help',
            action='store_true',
            dest='nexti_help',
            help=description,
            default=False)

        return parser

    def get_short_help(self):
        return "Steps one Maple instruction, but proceeds through subroutine calls"

    def get_long_help(self):
        return self.help_string

    def __init__(self, debugger, unused):
        self.parser = self.create_options()
        self.help_string = self.parser.format_help()

    def __call__(self, debugger, command, exe_ctx, result):
        # Use the Shell Lexer to properly parse up command options just like a
        # shell would
        command_args = shlex.split(command)

        try:
            (options, args) = self.parser.parse_args(command_args)
            print(options)
            print(args)
            if options.mnexti_help:
                print ("mnexti: Steps one Maple instruction, but proceeds through subroutine calls")
            else:
                print("Executing command")

        except Exception:
            # if you don't handle exceptions, passing an incorrect argument to
            # the OptionParser will cause LLDB to exit (courtesy of OptParse
            # dealing with argument errors by throwing SystemExit)
            result.SetError("option parsing failed")
            print("-"*60)
            print("Exception caught in mnexti code:")
            traceback.print_stack(file=sys.stdout)
            print("-"*60)
            traceback.print_exc(file=sys.stdout)
            print("-"*60)
            return
        # not returning anything is akin to returning success

class MNextCommand:
    program = 'mnext'

    @classmethod
    def register_lldb_command(cls, debugger, module_name):
        parser = cls.create_options()
        cls.__doc__ = parser.format_help()
        # Add any commands contained in this module to LLDB
        command = 'command script add -c %s.%s %s' % (module_name,
                                                      cls.__name__,
                                                      cls.program)
        debugger.HandleCommand(command)
        if m_config_lldb.debug:
            print('The "{0}" command has been installed, type "help {0}" or "{0} '
                  '--help" for detailed help.'.format(cls.program))
        command = 'command alias mn mnext'
        debugger.HandleCommand(command)

    @classmethod
    def create_options(cls):

        usage = "usage: %prog [options]"
        description = ('Basically:  mnext: Steps program, proceeding through subroutine calls of Maple application')

        """Steps program, proceeding through subroutine calls of Maple application
        Steps program, proceeding through subroutine calls of Maple application
        """

        # Pass add_help_option = False, since this keeps the command in line
        #  with lldb commands, and we wire up "help command" to work by
        # providing the long & short help methods below.
        parser = optparse.OptionParser(
            description=description,
            prog=cls.program,
            usage=usage,
            add_help_option=False)

        parser.add_option(
            '-h',
            '--help',
            action='store_true',
            dest='next_help',
            help=description,
            default=False)

        return parser

    def get_short_help(self):
        return "Steps program, proceeding through subroutine calls of Maple application"

    def get_long_help(self):
        return self.help_string

    def __init__(self, debugger, unused):
        self.parser = self.create_options()
        self.help_string = self.parser.format_help()

    def __call__(self, debugger, command, exe_ctx, result):
        # Use the Shell Lexer to properly parse up command options just like a
        # shell would
        command_args = shlex.split(command)

        try:
            (options, args) = self.parser.parse_args(command_args)
            print(options)
            print(args)
            if options.mnext_help:
                print ("mnext: Step program, proceeding through subroutine calls of Maple application")
            else:
                print("Executing command")

        except Exception:
            # if you don't handle exceptions, passing an incorrect argument to
            # the OptionParser will cause LLDB to exit (courtesy of OptParse
            # dealing with argument errors by throwing SystemExit)
            result.SetError("option parsing failed")
            print("-"*60)
            print("Exception caught in mnext code:")
            traceback.print_stack(file=sys.stdout)
            print("-"*60)
            traceback.print_exc(file=sys.stdout)
            print("-"*60)
            return
        # not returning anything is akin to returning success


def __lldb_init_module(debugger, dict):
    # Register all classes that have a register_lldb_command method
    for _name, cls in inspect.getmembers(sys.modules[__name__]):
        if inspect.isclass(cls) and callable(getattr(cls,
                                                     "register_lldb_command",
                                                     None)):
            cls.register_lldb_command(debugger, __name__)



#
# Copyright (C) [2020-2021] Futurewei Technologies, Inc. All rights reserved.
#
# OpenArkCompiler is licensed underthe Mulan Permissive Software License v2.
# You can use this software according to the terms and conditions of the MulanPSL - 2.0.
# You may obtain a copy of MulanPSL - 2.0 at:
#
#   https://opensource.org/licenses/MulanPSL-2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the MulanPSL - 2.0 for more details.
#

# ---------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
#
# # To use this in the embedded python interpreter using "lldb" just
# import it with the full path using the "command script import"
# command
#   (lldb) command script import /path/to/cmdtemplate.py
# ---------------------------------------------------------------------

from __future__ import print_function

import inspect
import lldb
import optparse
import shlex
import sys
import m_config_lldb


class MNextiCommand:
    program = 'mnexti'

    @classmethod
    def register_lldb_command(cls, debugger, module_name):
        parser = cls.create_options()
        cls.__doc__ = parser.format_help()
        # Add any commands contained in this module to LLDB
        command = 'command script add -c %s.%s %s' % (module_name,
                                                      cls.__name__,
                                                      cls.program)
        debugger.HandleCommand(command)
        if m_config_lldb.debug:
            print('The "{0}" command has been installed, type "help {0}" or "{0} '
                  '--help" for detailed help.'.format(cls.program))
        command = 'command alias mni mnexti'
        debugger.HandleCommand(command)

    @classmethod
    def create_options(cls):

        usage = "usage: %prog [options]"
        description = ('Basically:  mnexti: Steps one Maple instruction, but proceeds through subroutine calls')

        """Steps one Maple instruction, but proceed through subroutine calls
        mnexti: Steps one Maple instruction, but proceed through subroutine calls
        """

        # Pass add_help_option = False, since this keeps the command in line
        #  with lldb commands, and we wire up "help command" to work by
        # providing the long & short help methods below.
        parser = optparse.OptionParser(
            description=description,
            prog=cls.program,
            usage=usage,
            add_help_option=False)

        parser.add_option(
            '-s',
            '--show',
            action='store_true',
            dest='show_next',
            help=': show next Maple instruction before steping',
            default=False)

        parser.add_option(
            '-h',
            '--help',
            action='store_true',
            dest='nexti_help',
            help=description,
            default=False)

        return parser

    def get_short_help(self):
        return "Steps one Maple instruction, but proceeds through subroutine calls"

    def get_long_help(self):
        return self.help_string

    def __init__(self, debugger, unused):
        self.parser = self.create_options()
        self.help_string = self.parser.format_help()

    def __call__(self, debugger, command, exe_ctx, result):
        # Use the Shell Lexer to properly parse up command options just like a
        # shell would
        command_args = shlex.split(command)

        try:
            (options, args) = self.parser.parse_args(command_args)
            print(options)
            print(args)
            if options.mnexti_help:
                print ("mnexti: Steps one Maple instruction, but proceeds through subroutine calls")
            else:
                print("Executing command")

        except Exception:
            # if you don't handle exceptions, passing an incorrect argument to
            # the OptionParser will cause LLDB to exit (courtesy of OptParse
            # dealing with argument errors by throwing SystemExit)
            result.SetError("option parsing failed")
            print("-"*60)
            print("Exception caught in mnexti code:")
            traceback.print_stack(file=sys.stdout)
            print("-"*60)
            traceback.print_exc(file=sys.stdout)
            print("-"*60)
            return
        # not returning anything is akin to returning success

class MNextCommand:
    program = 'mnext'

    @classmethod
    def register_lldb_command(cls, debugger, module_name):
        parser = cls.create_options()
        cls.__doc__ = parser.format_help()
        # Add any commands contained in this module to LLDB
        command = 'command script add -c %s.%s %s' % (module_name,
                                                      cls.__name__,
                                                      cls.program)
        debugger.HandleCommand(command)
        if m_config_lldb.debug:
            print('The "{0}" command has been installed, type "help {0}" or "{0} '
                  '--help" for detailed help.'.format(cls.program))
        command = 'command alias mn mnext'
        debugger.HandleCommand(command)

    @classmethod
    def create_options(cls):

        usage = "usage: %prog [options]"
        description = ('Basically:  mnext: Steps program, proceeding through subroutine calls of Maple application')

        """Steps program, proceeding through subroutine calls of Maple application
        Steps program, proceeding through subroutine calls of Maple application
        """

        # Pass add_help_option = False, since this keeps the command in line
        #  with lldb commands, and we wire up "help command" to work by
        # providing the long & short help methods below.
        parser = optparse.OptionParser(
            description=description,
            prog=cls.program,
            usage=usage,
            add_help_option=False)

        parser.add_option(
            '-h',
            '--help',
            action='store_true',
            dest='next_help',
            help=description,
            default=False)

        return parser

    def get_short_help(self):
        return "Steps program, proceeding through subroutine calls of Maple application"

    def get_long_help(self):
        return self.help_string

    def __init__(self, debugger, unused):
        self.parser = self.create_options()
        self.help_string = self.parser.format_help()

    def __call__(self, debugger, command, exe_ctx, result):
        # Use the Shell Lexer to properly parse up command options just like a
        # shell would
        command_args = shlex.split(command)

        try:
            (options, args) = self.parser.parse_args(command_args)
            print(options)
            print(args)
            if options.mnext_help:
                print ("mnext: Step program, proceeding through subroutine calls of Maple application")
            else:
                print("Executing command")

        except Exception:
            # if you don't handle exceptions, passing an incorrect argument to
            # the OptionParser will cause LLDB to exit (courtesy of OptParse
            # dealing with argument errors by throwing SystemExit)
            result.SetError("option parsing failed")
            print("-"*60)
            print("Exception caught in mnext code:")
            traceback.print_stack(file=sys.stdout)
            print("-"*60)
            traceback.print_exc(file=sys.stdout)
            print("-"*60)
            return
        # not returning anything is akin to returning success


def __lldb_init_module(debugger, dict):
    # Register all classes that have a register_lldb_command method
    for _name, cls in inspect.getmembers(sys.modules[__name__]):
        if inspect.isclass(cls) and callable(getattr(cls,
                                                     "register_lldb_command",
                                                     None)):
            cls.register_lldb_command(debugger, __name__)
# # Copyright (C) [2020-2021] Futurewei Technologies, Inc. All rights reserved. # # OpenArkCompiler is licensed underthe Mulan Permissive Software License v2. # You can use this software according to the terms and conditions of the MulanPSL - 2.0. # You may obtain a copy of MulanPSL - 2.0 at: # #   https://opensource.org/licenses/MulanPSL-2.0 # # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR # FIT FOR A PARTICULAR PURPOSE. # See the MulanPSL - 2.0 for more details. # # --------------------------------------------------------------------- # Be sure to add the python path that points to the LLDB shared library. # # # To use this in the embedded python interpreter using "lldb" just # import it with the full path using the "command script import" # command #   (lldb) command script import /path/to/cmdtemplate.py # --------------------------------------------------------------------- # Add any commands contained in this module to LLDB Steps one Maple instruction, but proceed through subroutine calls         mnexti: Steps one Maple instruction, but proceed through subroutine calls # Pass add_help_option = False, since this keeps the command in line #  with lldb commands, and we wire up "help command" to work by # providing the long & short help methods below. # Use the Shell Lexer to properly parse up command options just like a # shell would # if you don't handle exceptions, passing an incorrect argument to # the OptionParser will cause LLDB to exit (courtesy of OptParse # dealing with argument errors by throwing SystemExit) # not returning anything is akin to returning success # Add any commands contained in this module to LLDB Steps program, proceeding through subroutine calls of Maple application         Steps program, proceeding through subroutine calls of Maple application # Pass add_help_option = False, since this keeps the command in line #  with lldb commands, and we wire up "help command" to work by # providing the long & short help methods below. # Use the Shell Lexer to properly parse up command options just like a # shell would # if you don't handle exceptions, passing an incorrect argument to # the OptionParser will cause LLDB to exit (courtesy of OptParse # dealing with argument errors by throwing SystemExit) # not returning anything is akin to returning success # Register all classes that have a register_lldb_command method