1

I have a function called analyze() which is like following:

def analyze():
    for stmt in irsb.statements:
        if isinstance(stmt, pyvex.IRStmt.WrTmp):
           wrtmp(stmt)
        if isinstance(stmt, pyvex.IRStmt.Store):
           address = stmt.addr
           address1 = '{}'.format(address)[1:]
           print address1
           data = stmt.data
           data1 = '{}'.format(data)[1:]
           tmp3 = store64(address1, int64(data1))
        if isinstance(stmt, pyvex.IRStmt.Put):
           expr = stmt.expressions[0]
           putoffset = stmt.offset
           data =  stmt.data
           data4 = '{}'.format(data)[1:]
           if (str(data).startswith("0x")):
             #const_1 = ir.Constant(int64, data4)
             tmp = put64(putoffset, ZERO_TAG)
           else:
             put64(putoffset, int64(data4))
             if isinstance(stmt.data, pyvex.IRExpr.Const):
                reg_name = irsb.arch.translate_register_name(stmt.offset, stmt.data.result_size(stmt.data.tag))
                print reg_name
        stmt.pp()

This code function gets following input and try to analyze it:

CODE = b"\xc1\xe0\x05"  
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())

When this input is in the same file in my code (lets call the whole as analyze.py) it works and python analyze.py will make me an output. However, I want to make a seperate file(call array.py), call analyze there and also put the inputs inside it and run python array.py to get the same result. I did the following for array.py:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze()

However, when I run the array.py, it stops me with error;

NameError: name 'CODE' is not defined

how can I resolve this problem? What is the solution?

Serjik
  • 10,543
  • 8
  • 61
  • 70
sarah123
  • 175
  • 1
  • 7

1 Answers1

2

A simple change in your function, add parameters:

def analyze(irsb):   # irsb here called parameter
  ...
  # The rest is the same

And then pass arguments when calling it:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb_as_arg = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze(irsb_as_arg)   # irsb_as_arg is an argument

I have just changed here irsb to irsb_as_arg to take attention, but it can be the same name

Serjik
  • 10,543
  • 8
  • 61
  • 70
  • Thank you it works. What if I want to import functions of analyze.py when analyze.py is in another directory? – sarah123 Sep 21 '17 at 16:49
  • Please refer to python absolute and relative import, that depends if you treated the whole as a module or direct script https://stackoverflow.com/questions/46327361/how-to-import-one-submodule-from-different-submodule/46328021#46328021 – Serjik Sep 21 '17 at 16:53