1

我想在“向应用程序中添加分析指令”教程中编写示例,所以我按照教程中的说明编写程序。但是当我运行它时,我得到了这个错误:

[Thread-3] 错误 heros.solver.CountingThreadPoolExecutor - 工作线程执行失败:字段已存在:gotoCount 类型

这个错误对应于

Scene.v().getMainClass().addField(gotoCounter);

我把我写的代码:

import soot.*;
import soot.jimple.*;
import soot.util.Chain;
import java.util.Iterator;
import java.util.Map;

public class GoToInstrumenter extends BodyTransformer {
private static GoToInstrumenter instance = new GoToInstrumenter();
private GoToInstrumenter() {}
public static GoToInstrumenter v() { return instance; }
protected void internalTransform(Body body, String phaseName, Map options) {
    if (!Scene.v().getMainClass().
            declaresMethod("void main(java.lang.String[])"))
        throw new RuntimeException("couldn't find main() in mainClass");
    SootField gotoCounter;
    SootClass javaIoPrintStream;
    boolean addedFieldToMainClassAndLoadedPrintStream=false;
    if (addedFieldToMainClassAndLoadedPrintStream)
        gotoCounter = Scene.v().getMainClass().getFieldByName("gotoCount");
    else
    {

        gotoCounter = new SootField("gotoCount", LongType.v(),
                Modifier.STATIC);
        Scene.v().getMainClass().addField(gotoCounter);

        Scene.v().loadClassAndSupport("java.io.PrintStream");
        javaIoPrintStream =Scene.v().getSootClass("java.io.PrintStream");

        addedFieldToMainClassAndLoadedPrintStream = true;
    }
    boolean isMainMethod = body.getMethod().getSubSignature()
            .equals("void main(java.lang.String[])");
    Local tmpLocal = Jimple.v().newLocal("tmp", LongType.v());
    body.getLocals().add(tmpLocal);
    Iterator stmtIt = body.getUnits().snapshotIterator();
    Chain units=body.getUnits();
    while (stmtIt.hasNext()) {
        Stmt s = (Stmt) stmtIt.next();
        if (s instanceof GotoStmt) {
            AssignStmt toAdd1 = Jimple.v().newAssignStmt(tmpLocal,
                    Jimple.v().newStaticFieldRef(gotoCounter.makeRef()));
            units.insertBefore(toAdd1, s);
       }

         }

      }
  }

请帮助我,我无法找出导致此问题的原因?

4

0 回答 0