@magic.line_cell_magic
    @magic_arguments()
    @argument('hook_address', type=hexint)
    @argument('handler_address', nargs='?', type=hexint_aligned, default=target_memory.hook_code)
    @argument('-q', '--quiet', action='store_true', help="Just install the hook, don't talk about it")
    @argument('-R', '--reset', action='store_true', help="Reset the ARM before starting")
    @argument('-c', '--console', action='store_true', help='Immediately launch into a %%console after installing')
    @argument('-b', '--bitbang',action='store_true', help='Use bitbang_console() to send output to the bitbang serial port')
    @argument('-f', '--console-file', type=str, default=None, metavar='FILE', help='Append console output to a text file')
    @argument('-d', '--delay', type=float, default=None, metavar='SEC', help='Add a delay loop to the default hook')
    @argument('-m', '--message', type=str, default=None, help='Message to log in the default hook')
    @argument('-r', '--replace', action='store_true', help='Replace the hooked instruction instead of relocating it')
    @argument('-s', '--sram', action='store_true', help='The target already has an SRAM mapping, use that instead of moving the overlay')
    @argument('--console-buffer', type=hexint_aligned, metavar='HEX', default=console_address, help='Specify a different address for the console_buffer_t data structure')
    def hook(self, line, cell=None):
        args = parse_argstring(self.hook, line)
        d = self.shell.user_ns['d']

        # Hook body is either the cell provided or a trivial default_hook()

        if not cell:
            # Default hook, including our command line as a trace message
            message = args.message or ('%%hook %x' % args.hook_address)
            cell = 'default_hook(regs, %s)' % json.dumps(message)
        elif args.message:
            raise UsageError('--message only applies when using the default hook')

        # Add-ons for any hook body

        if args.delay:
            cell = '%s;\n wait_ms(%d)' % (cell, args.delay * 1000)

        if args.bitbang:
            cell = '%s;\n bitbang_console();' % cell

        try:
            overlay_hook(d, args.hook_address, cell,
                defines = all_defines(),
                includes = all_includes(),
                handler_address = args.handler_address,
                replace_one_instruction = args.replace,
                reset = args.reset,
                verbose = not args.quiet,
                target_already_mapped = args.sram)
        except CodeError, e:
            raise UsageError(str(e))

        if args.console:
            console_mainloop(d, buffer=args.console_buffer, log_filename=args.console_file)
Inject a C++ hook into Thumb code executing from Flash memory.
dataset/ETHPy150Open scanlime/coastermelt/backdoor/shell_magics.py/ShellMagics.hook