<commit_before>'''
A unified context menu builder for all address context clicks.
'''
from PyQt4 import QtGui,QtCore

import envi
import envi.bits as e_bits
import envi.qt.memcanvas as e_q_memcanvas
import vivisect.analysis.generic.codeblocks as vagc

from vqt.common import *
from envi.threads import *
from vivisect.const import *

def cmpoffset(x,y):
    return cmp(x[0], y[0])

@firethread
def printEmuState(vw, fva, va):
    vw.vprint('Running emulator to: 0x%.8x' % (va,))
    emu = vw.getEmulator()

    stack = emu.getStackCounter()
    emu.runFunction(fva, stopva=va, maxhit=2)
    dstack = emu.getStackCounter() - stack

    regs = emu.getRegisters()
    rnames = regs.keys()
    rnames.sort()

    vw.vprint("Showing Register/Magic State At: 0x%.8x" % va)
    vw.vprint('Stack Delta: %d' % dstack)

    # FIXME: this may not be as flexible as it could be, as we don't necessarily *have* to have a location... though we certainly should.
    loc = vw.getLocation(va)
    if loc == None:
        vw.vprint("ARG! can't find location info for 0x%x" % va)
        return

    lva, lsz, ltype, tinfo = loc
    op = vw.parseOpcode(va, arch=tinfo)
    vw.canvas.addVaText("0x%.8x: " % va, va)
    op.render(vw.canvas)
    vw.canvas.addText("\n")
    for i in xrange(len(op.opers)):
        o = op.opers[i]
        o.render(vw.canvas, op, i)
        oaddr = o.getOperAddr(op, emu)
        if oaddr != None:
            vw.canvas.addText(' [ 0x%x ] ' % oaddr)

        vw.canvas.addText(" = ")
        oval = o.getOperValue(op, emu)
        taint = emu.getVivTaint(oval)
        base = "0x%.8x (%d)" % (oval,oval)
        if taint != None:
            base += '%s + %d' % ( emu.reprVivTaint(taint), oval - taint[0])

        vw.vprint(base)

def buildContextMenu(vw, va=None, expr=None, menu=None, parent=None, nav=None):
    '''
    Return (optionally construct) a menu to use for handling a context click
    at the given virtual address or envi expression.

    Arguments:
    va      - virtual address of the context click
    expr    - expression to parse instead of va
    menu    - existing menu to add items to
    parent  - Qt parent
    nav     - the "local" EnviNavMixin instance
    '''
    if va == None:
        try:
            va = vw.parseExpression(expr)
        except Exception, e:
            sys.excepthook(*sys.exc_info())

    if expr == None:
        expr = '0x%.8x' % va
<commit_msg>residue from a separate delta.<commit_after>'''
A unified context menu builder for all address context clicks.
'''
from PyQt4 import QtGui,QtCore

import envi
import envi.bits as e_bits
import envi.qt.memcanvas as e_q_memcanvas
import vivisect.analysis.generic.codeblocks as vagc

from vqt.common import *
from envi.threads import *
from vivisect.const import *

def cmpoffset(x,y):
    return cmp(x[0], y[0])

@firethread
def printEmuState(vw, fva, va):
    vw.vprint('Running emulator to: 0x%.8x' % (va,))
    emu = vw.getEmulator()

    stack = emu.getStackCounter()
    emu.runFunction(fva, stopva=va, maxhit=2)
    dstack = emu.getStackCounter() - stack

    regs = emu.getRegisters()
    rnames = regs.keys()
    rnames.sort()

    vw.vprint("Showing Register/Magic State At: 0x%.8x" % va)
    vw.vprint('Stack Delta: %d' % dstack)

    # FIXME: this may not be as flexible as it could be, as we don't necessarily *have* to have a location... though we certainly should.
    loc = vw.getLocation(va)
    if loc == None:
        vw.vprint("ARG! can't find location info for 0x%x" % va)
        return

    lva, lsz, ltype, tinfo = loc
    op = vw.parseOpcode(va, arch=tinfo)
    vw.canvas.addVaText("0x%.8x: " % va, va)
    op.render(vw.canvas)
    vw.canvas.addText("\n")
    for i in xrange(len(op.opers)):
        o = op.opers[i]
        o.render(vw.canvas, op, i)
        oaddr = o.getOperAddr(op, emu)
        if oaddr != None:
            vw.canvas.addText(' [ 0x%x ] ' % oaddr)

        vw.canvas.addText(" = ")
        oval = o.getOperValue(op, emu)
        taint = emu.getVivTaint(oval)
        base = "0x%.8x (%d)" % (oval,oval)
        if taint != None:
            base += '%s + %d' % ( emu.reprVivTaint(taint), oval - taint[0])

        vw.vprint(base)

def buildContextMenu(vw, va=None, expr=None, menu=None, parent=None, nav=None):
    '''
    Return (optionally construct) a menu to use for handling a context click
    at the given virtual address or envi expression.

    Arguments:
    va      - virtual address of the context click
    expr    - expression to parse instead of va
    menu    - existing menu to add items to
    parent  - Qt parent
    nav     - the "local" EnviNavMixin instance
    '''
    if va == None:
        va = vw.parseExpression(expr)

    if expr == None:
        expr = '0x%.8x' % va

{'fraction_non_alphanumeric': 0.12449968401095429, 'fraction_numerical': 0.007162418369496524, 'mean_word_length': 2.567242674680691, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2722533', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1679, 'n_words': 479}