def emit_simple(event, time, actions, errcode, fields, info, lib):
    if lib:
        return
    if event in (EVENT_WARNING, EVENT_ERROR, EVENT_CRITICAL):
        print '[%s] (%d) <%s> ERR:%r %r %r' % (
            EVENT_NAMES[event], time, erepr(get_action_name(
                actions[0])), erepr(get_error_name(errcode)), fields, info)
    else:
        print '[%s] (%d) <%s> %r %r' % (EVENT_NAMES[event], time,
                                        erepr(get_action_name(
                                            actions[0])), fields, info)
Exemple #2
0
def emit_simple(event, time, actions, errcode, fields, info, lib):
    if lib:
        return
    if event in (EVENT_WARNING, EVENT_ERROR, EVENT_CRITICAL):
        print "[%s] (%d) <%s> ERR:%r %r %r" % (
            EVENT_NAMES[event],
            time,
            erepr(get_action_name(actions[0])),
            erepr(get_error_name(errcode)),
            fields,
            info,
        )
    else:
        print "[%s] (%d) <%s> %r %r" % (EVENT_NAMES[event], time, erepr(get_action_name(actions[0])), fields, info)
Exemple #3
0
def emit_human(event, time, actions, errcode, fields, info, lib):
    act = actions[0]
    if act == ACT_LOADDB:
        if event == log.EVENT_START:
            sys.stdout.write("Loading db...")
        elif event == log.EVENT_SUCCESS:
            sys.stdout.write(" DONE\n")
        elif event == log.EVENT_END:
            sys.stdout.write("\n")
    elif act == ACT_SCANFILE:
        if event == log.EVENT_START:
            sys.stdout.write("Scan file: %s\n" % erepr(fields['path']))
        elif event == log.EVENT_END:
            sys.stdout.write("\n")
    elif act == ACT_CALCOFFSET:
        if event == log.EVENT_SUCCESS:
            sys.stdout.write("Offset code: %s\n" % hex(info['offset']))
        elif event == log.EVENT_ERROR and errcode == ERR_WRONGFMT:
            sys.stdout.write("ERROR: Wrong file format")
    elif act == ACT_SEARCHSIGNS:
        if event == log.EVENT_SUCCESS:
            sys.stdout.write("Found signature %r at %s\n" % (info['name'], hex(info['pos'])))
        elif event == log.EVENT_ERROR and errcode == ERR_NOTFOUND:
            sys.stdout.write("Signature not found\n")
    sys.stdout.flush()
Exemple #4
0
def deep_tb(exc_type, exc_value, exc_tb):
    traceback = ['=== Traceback (most recent call last) ===']
    lvl = 0
    while exc_tb:
        path = os.path.abspath(exc_tb.tb_frame.f_code.co_filename)
        lineno = exc_tb.tb_lineno
        traceback.append('[%d]' % lvl)
        traceback.append('  File: %r' % path)
        traceback.append('  Function: %r' % exc_tb.tb_frame.f_code.co_name)
        with fd.File.open(path, fd.FO_READEX) as src:
            line = src.get_line(lineno - 1)
        traceback.append('  Line: (%d) %r' % (lineno, line.strip()))
        traceback.append('  Variables:')
        for token, where, val in linevars(line.strip(), exc_tb.tb_frame):
            if where is None:
                val = '<undefined>'
            else:
                val = '[%s] %r' % (erepr(getattr(type(val), '__name__', type(val))), val)
            traceback.append('    %r: %s' % (token, val))
        traceback.append('  Code:')
        for ist, lineno, label, op, arg in disassemble(exc_tb.tb_frame.f_code):
            prefix = '>> ' if ist == exc_tb.tb_lasti else '   '
            postfix = ' << %s' % exc_type.__name__ if ist == exc_tb.tb_lasti else ''
            if lineno == exc_tb.tb_lineno:
                if arg is NOVAL:
                    traceback.append('  %s%s%s' % (prefix, op, postfix))
                else:
                    traceback.append('  %s%s %r%s' % (prefix, op, arg, postfix))
        exc_tb = exc_tb.tb_next
        lvl += 1
    traceback.append('[ERROR]')
    traceback.append('%s: %r' % (exc_type.__name__, str(exc_value)))
    traceback.append('=========================================\n')
    return '\n'.join(traceback)
Exemple #5
0
def emit_human(event, time, actions, errcode, fields, info, lib):
    act = actions[0]
    if act == ACT_LOADDB:
        if event == log.EVENT_START:
            sys.stdout.write("Loading db...")
        elif event == log.EVENT_SUCCESS:
            sys.stdout.write(" DONE\n")
        elif event == log.EVENT_END:
            sys.stdout.write("\n")
    elif act == ACT_SCANFILE:
        if event == log.EVENT_START:
            sys.stdout.write("Scan file: %s\n" % erepr(fields['path']))
        elif event == log.EVENT_END:
            sys.stdout.write("\n")
    elif act == ACT_CALCOFFSET:
        if event == log.EVENT_SUCCESS:
            sys.stdout.write("Offset code: %s\n" % hex(info['offset']))
        elif event == log.EVENT_ERROR and errcode == ERR_WRONGFMT:
            sys.stdout.write("ERROR: Wrong file format")
    elif act == ACT_SEARCHSIGNS:
        if event == log.EVENT_SUCCESS:
            sys.stdout.write("Found signature %r at %s\n" %
                             (info['name'], hex(info['pos'])))
        elif event == log.EVENT_ERROR and errcode == ERR_NOTFOUND:
            sys.stdout.write("Signature not found\n")
    sys.stdout.flush()
def deep_tb(exc_type, exc_value, exc_tb):
    traceback = ['=== Traceback (most recent call last) ===']
    lvl = 0
    while exc_tb:
        path = os.path.abspath(exc_tb.tb_frame.f_code.co_filename)
        lineno = exc_tb.tb_lineno
        traceback.append('[%d]' % lvl)
        traceback.append('  File: %r' % path)
        traceback.append('  Function: %r' % exc_tb.tb_frame.f_code.co_name)
        with fd.File.open(path, fd.FO_READEX) as src:
            line = src.get_line(lineno - 1)
        traceback.append('  Line: (%d) %r' % (lineno, line.strip()))
        traceback.append('  Variables:')
        for token, where, val in linevars(line.strip(), exc_tb.tb_frame):
            if where is None:
                val = '<undefined>'
            else:
                val = '[%s] %r' % (erepr(
                    getattr(type(val), '__name__', type(val))), val)
            traceback.append('    %r: %s' % (token, val))
        traceback.append('  Code:')
        for ist, lineno, label, op, arg in disassemble(exc_tb.tb_frame.f_code):
            prefix = '>> ' if ist == exc_tb.tb_lasti else '   '
            postfix = ' << %s' % exc_type.__name__ if ist == exc_tb.tb_lasti else ''
            if lineno == exc_tb.tb_lineno:
                if arg is NOVAL:
                    traceback.append('  %s%s%s' % (prefix, op, postfix))
                else:
                    traceback.append('  %s%s %r%s' %
                                     (prefix, op, arg, postfix))
        exc_tb = exc_tb.tb_next
        lvl += 1
    traceback.append('[ERROR]')
    traceback.append('%s: %r' % (exc_type.__name__, str(exc_value)))
    traceback.append('=========================================\n')
    return '\n'.join(traceback)
from pysec.io import fd
from pysec import strings
from pysec.xsplit import xlines


BUFSIZE = 4096


if __name__ == '__main__':
    path = os.path.abspath(sys.argv[1])
    offset = 0 if len(sys.argv) < 3 else int(sys.argv[2])
    size = None if len(sys.argv) < 4 else int(sys.argv[3])
    with fd.File.open(path, fd.FO_READEX) as fb:
        if size is None:
            size = len(fb) - offset
        print "=== Repr visible characters ==="
        for chunk in fb.chunks(BUFSIZE, start=offset, stop=offset+size):
            sys.stdout.write(strings.erepr(chunk))
        print
        print "=== Repr visible characters and split lines ==="
        for line in xlines(fb, eol='\n', keep_eol=1, start=offset, stop=offset+size, find=alg.knp_first):
            sys.stdout.write(strings.erepr(line))
            sys.stdout.write('\n')
        print
        print "=== Only printable characters and split lines ==="
        for line in xlines(fb, eol='\n', keep_eol=0, start=offset, stop=offset+size, find=alg.knp_first):
            sys.stdout.write(strings.only_printable(line))
            sys.stdout.write('\n')
        print