'Trace specific instructions executed by the program
Usage:
MYNAME ["inst1,inst2"] [mapname1,mapname2]
MYNAME count (trace execution of next count instrcutions)
default is to trace instructions inside the program'

def traceinst(self, *arg):

    (insts, mapname) = normalize_argv(arg, 2)
    if (not self._is_running()):
        return
    if (not mapname):
        mapname = 'binary'
    instlist = ['.*']
    count = (-1)
    if insts:
        if to_int(insts):
            count = insts
        else:
            instlist = insts.replace(',', '    ').split()
    binname = peda.getfile()
    logname = peda.get_config_filename('tracelog')
    if (mapname is None):
        mapname = binname
    peda.save_user_command('hook-stop')
    msg(("Tracing    instructions    match    '%s',    Ctrl-C    to    stop..." % ','.join(instlist)))
    prev_depth = peda.backtrace_depth(peda.getreg('sp'))
    logfd = open(logname, 'w')
    p = re.compile('.*?:\\s*[^    ]*\\s*([^,]*),(.*)')
    while count:
        result = peda.stepuntil(','.join(instlist), mapname, prev_depth)
        if (result is None):
            break
        (call_depth, code) = result
        prev_depth += call_depth
        if (not code.startswith('=>')):
            break
        prev_code = ''
        if re.search('j[^m]', code.split(': DCTB ')[(-1)].split()[0]):
            prev_insts = peda.prev_inst(peda.getreg('pc'))
            if prev_insts:
                prev_code = ('0x%x:%s' % prev_insts[0])
                msg(('%s%s%s' % (('    ' * (prev_depth - 1)), ('    dep:%02d                ' % (prev_depth - 1)), prev_code)), teefd=logfd)
        text = ('%s%s%s' % (('    ' * (prev_depth - 1)), ('    dep:%02d    ' % (prev_depth - 1)), code.strip()))
        msg(text, teefd=logfd)
        if re.search('call', code.split(': DCTB ')[(-1)].split()[0]):
            args = peda.get_function_args()
            if args:
                for (i, a) in enumerate(args):
                    chain = peda.examine_mem_reference(a)
                    text = ('%s                                |--    arg[%d]:    %s' % (('    ' * (prev_depth - 1)), i, format_reference_chain(chain)))
                    msg(text, teefd=logfd)
        (arch, bits) = peda.getarch()
        code = code.split('#')[0].strip('=>')
        if prev_code:
            m = p.search(prev_code)
        else:
            m = p.search(code)
        if m:
            for op in m.groups():
                if op.startswith('0x'):
                    continue
                v = to_int(peda.parse_and_eval(op))
                chain = peda.examine_mem_reference(v)
                text = ('%s                                |--    %03s:    %s' % (('    ' * (prev_depth - 1)), op, format_reference_chain(chain)))
                msg(text, teefd=logfd)
        count -= 1
    msg(code, 'red')
    peda.restore_user_command('hook-stop')
    logfd.close()
    msg(("Saved    trace    information    in    file    %s,    view    with    'less    -r    file'" % logname))
    return
