Soot 学习笔记 8:More on profiling

本文介绍了如何利用Soot对class文件进行inspect和profile。通过一个具体的Task——统计TestInvoke.java中InvokeStatic指令的运行次数,展示了创建助手类MyCounter以及设计包装类InvokeStaticInstrumenter来插入profiling指令的过程。在MainDriver.java中注册新的phase并运行插桩器,实现对程序的动态分析。
摘要由CSDN通过智能技术生成
本文参考 Using Soot to instrument a class file

目标

  1. 怎样用 soot  对一个 class 文件 inspect
  2. 怎样用为 class 文件插桩的方法对程序 profile

用例子说明

Task:为下面这个 TestInvoke.java 程序统计 InvokeStatic 指令的运行次数。

class TestInvoke {
  private static int calls=0;
  public static void main(String[] args) {
 
    for (int i=0; i<10; i++) {
      foo();
    }
 
    System.out.println("I made "+calls+" static calls");
  }
 
  private static void foo(){
    calls++;
    bar();
  }
 
  private static void bar(){
    calls++;
  }
}

为了记录计数器,我写了一个助手类 MyCounter 如下

/* The counter class */
public class MyCounter {
  /* the counter, initialize to zero */
  private static int c = 0;
 
  /**
   * increases the counter by <pre>howmany</pre>
   * @param howmany, the increment of the counter.
   */
  public static synchronized void increase(int howmany) {
    c += howmany;
  }
 
  /**
   * reports the counter content.
   */
  public static synchronized void report() {
    System.err.println("counter : " + c);
  }
}

现在我新建一个包装类来为 Soot 添加一个 phase。目的是插入 profiling 的指令,然后调用 soot.Main.main()。这个driver 类的 main method 添加了一个命名为 “jtp.instrumenter” 的 transformation phase 到 soot 的 “jtp” pack。当 MainDriversoot.Main.main 发起调用时,Soot 能从 PackManager 中得知一个新的 phase 被注册了,并且它的 internalTransform 方法会被 soot 调用。以下是 MainDriver.java:

      /* Usage: java MainDriver [soot-options] appClass
       */
    
      /* import necessary soot packages */
      import soot.*;
    
      public class MainDriver {
        public static void main(String[] args) {
    
          /* check the arguments */
          if (args.length == 0) {
            System.err.println("Usage: java MainDriver [options] classname");
            System.exit(0);
          }
    
          /* add a phase to trans
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值