Git Product home page Git Product logo

chapter06's Introduction

Chapter06

该项目展示了使用 PLTHook 技术来获取 Atrace 的日志,可以学习到systrace的一些底层机制

运行环境

AndroidStudio3.2 NDK16~19 支持 x86 armeabi-v7a

说明

运行项目后点击开启 Atrace 日志,然后就可以在Logcat 日志中查看到捕获的日志,类似如下:

 ========= B|5667|Lock contention on task queue lock (owner tid: 5667)
 ========= B|5667|JIT compiling int android.view.MotionEvent.getAction()
 ========= E
 ========= B|5667|Compiling
 ========= B|5667|ScopedCodeCacheWrite
 ========= B|5667|mprotect all
 ========= E
 ========= E
 ========= B|5667|mprotect code
 ========= B|5667|ScopedCodeCacheWrite
 ========= B|5667|mprotect all
 ========= B|5667|mprotect code
 ========= B|5667|TrimMaps
 ========= B|5667|void art::ArenaPool::TrimMaps()

可以通过B|事件和E|事件是成对出现的,这样就可以获得每个事件使用的时间。

关键实现

hookLoadedLibs

使用plt hook libc的 write 相关方法,其中第一个参数意思是准备Hook的library名字。

这里我们用的是profilo中提供的plt hook库。

write_hook

这里为了提升性能,只hook atrace_marker 文件对应的fd

ssize_t write_hook(int fd, const void *buf, size_t count) {
    if (should_log_systrace(fd, count)) {
        log_systrace(buf, count);
        return count;
    }
    return CALL_PREV(write_hook, fd, buf, count);
}

这里我们可能要理解一下ftrace和atrace的机制,可以参考以下参考文章:

  1. 使用 ftrace
  2. ftrace Documentation
  3. ftrace 简介
  4. atrace.cpp

installSystraceSnooper

通过dlsym调用libcutils.so的方法,直接打开atrace。 这里需要适配Android版本小于18的情况

有人可能好奇类似"_ZN7android6Tracer12sEnabledTagsE"这样的名字是怎么样找出来的,我们可以参考这篇文章

c++filt -n _ZN7android6Tracer12sEnabledTagsE

android::Tracer::sEnabledTags

也可以直接objdump对应的so得到。

Thanks

Profilo

chapter06's People

Contributors

dodola avatar shwenzhang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.