Giter VIP home page Giter VIP logo

leaktracer's Introduction

LeakTracer for Android NDK

Original Repository

  • README
  • License (library): LGPLv2.1+
  • License (manual and tools): GPLv2+

Porting to Android

  • use NDK API _Unwind_Backtrace to catch more accurate backtrace
  • automatically caculate symbols offset by retreiving module base ahead (easier for analyze)
  • use NDK toolchains to convert leak address to line in source code

Usage

Makefile

Use this library as static link. Source compilation or prebuilt as static library.

Android.mk

  • source compilation
LOCAL_C_INCLUDES += $(LOCAL_PATH)/libleaktracer/include/
LOCAL_SRC_FILES += $(LOCAL_PATH)/libleaktracer/src/AllocationHandlers.cpp \
                   $(LOCAL_PATH)/libleaktracer/src/MemoryTrace.cpp
  • prebuild
LOCAL_C_INCLUDES := $(LOCAL_PATH)/libleaktracer/include/
LOCAL_SRC_FILES := $(LOCAL_PATH)/libleaktracer/src/AllocationHandlers.cpp \
                   $(LOCAL_PATH)/libleaktracer/src/MemoryTrace.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY)

Application.mk

change to debug compilation

APP_OPTIM := debug

Native code

#include "MemoryTrace.hpp"

void somePlaceToStart()
{
	// starts monitoring memory allocations in all threads
	leaktracer::MemoryTrace::GetInstance().startMonitoringAllThreads();
	// or starts monitoring memory allocations in current thread
	// leaktracer::MemoryTrace::GetInstance().startMonitoringThisThread();
}

void somePlaceToStop()
{
	leaktracer::MemoryTrace::GetInstance().stopAllMonitoring();
	// just make sure you have the very permission (android.permission.WRITE_EXTERNAL_STORAGE)
	// to save leak log on SDCard
	leaktracer::MemoryTrace::GetInstance().writeLeaksToFile("/mnt/sdcard/test.leak");
}

Parsing leak file

  • add NDK_ROOT to your environment specifing Android NDK location root (used by leak-analyze-addr2line script)

  • parsing leak log on ternimal:

adb pull /mnt/sdcard/test.leak /tmp/
./helpers/leak-analyze-addr2line ~/proj.android/native/obj/local/armeabi/libtest.so /tmp/test.leak
  • if everything's fine, you should see output like this:
28 bytes lost in 1 blocks (one of them allocated at 25841.648999), from following call stack:
    ~/proj.android/native/./../../src/leaktracer/include/MemoryTrace.hpp:364
    ~/proj.android/native/./../../src/leaktracer/include/MemoryTrace.hpp:414
    ~/proj.android/native/./../../src/leaktracer/src/AllocationHandlers.cpp:29
    /Volumes/Android/buildbot/out_dirs/aosp-ndk-r11-release/build/tmp/build-42939/build-gnustl/static-armeabithumb-4.9/include/bits/basic_string.h:204
    ~/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/basic_string.tcc:138
    /Volumes/Android/buildbot/out_dirs/aosp-ndk-r11-release/build/tmp/build-42939/build-gnustl/static-armeabithumb-4.9/include/bits/basic_string.h:275
    ~/xlab/framework/platform/android/jni/xxJNIReflection.cpp:68 (discriminator 9)
    ~/xlab/framework/platform/android/jni/xxJNIReflection.cpp:49

23 bytes lost in 1 blocks (one of them allocated at 25841.654000), from following call stack:
    ~/proj.android/native/./../../src/leaktracer/include/MemoryTrace.hpp:364
    ~/proj.android/native/./../../src/leaktracer/include/MemoryTrace.hpp:414
    ~/proj.android/native/./../../src/leaktracer/src/AllocationHandlers.cpp:29
    /Volumes/Android/buildbot/out_dirs/aosp-ndk-r11-release/build/tmp/build-42939/build-gnustl/static-armeabithumb-4.9/include/bits/basic_string.h:204
    ~/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/basic_string.tcc:138
    /Volumes/Android/buildbot/out_dirs/aosp-ndk-r11-release/build/tmp/build-42939/build-gnustl/static-armeabithumb-4.9/include/bits/basic_string.h:275
    ~/proj.android/native/./../../src/android/VPNController-android.cpp:87 (discriminator 12)
    ~/proj.android/native/./../../src/XXControlCenter.cpp:296

Notice

  • leak-analyze-addr2line script use NDK toolchain v4.9 by default. feel free to modify $toolchains_version definition
  • leak-analyze-addr2line script handle object file with fully debug symbols (normally located at obj/local/ after each ndk-build)

leaktracer's People

Contributors

fredericgermain avatar hzeller avatar nmel 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.