btrace

July 7, 2025 · View on GitHub

README 中文版

btrace for Android

btrace for iOS

Major Update

We are proud to announce the launch of btrace 3.0, introducing an industry-first high-performance synchronized sampling-based tracing solution. Additionally, the new version now provides comprehensive iOS tracing capabilities.

btrace for Android

Integration

Add dependencies in app/build.gradle file:

dependencies {
    if (enable_btrace == 'true') {
        implementation 'com.bytedance.btrace:rhea-inhouse:3.0.0'
    } else {
        implementation 'com.bytedance.btrace:rhea-inhouse-noop:3.0.0'
    }
}

Add enable_btrace switch in the gradle.properties file:

# Turn on this switch when you want to build app that support tracing.
enable_btrace=false

Add initialization code in attachBaseContext() method of your Application:


public class MyApp extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // When rhea-inhouse-noop is used, RheaTrace3.init() has empty implementation.
        RheaTrace3.init(base);
    }
}

Usage

To use btrace 3.0, follow these instructions:

  1. Make sure that your computer has integrated adb and Java and Python3 environment.
  2. Connect your phone to your computer and make sure it can recognized by adb devices.
  3. Install the APK that integrates btrace 3.0 on your phone.
  4. Download the latest script from "Script Management" below to your computer.
  5. In the directory where the computer script is located, execute the following command:
java -jar rhea-trace-shell.jar -a ${your_package_name} -t 10 -o output.pb -r sched
  1. Open the generated trace file in https://ui.perfetto.dev/ for detailed analysis.

Script Management

VersionRelease DatejarRelease Notes
3.0.02025-06-10rhea-trace-shell-3.0.0.jar3.0 first release

Parameters Description

Required Parameters
ParameterDefault ValueDescription
-a $applicationNameN/ASpecifies the package name of your app
Optional Parameters
ParameterDefault ValueDescription
-o $outputPath${applicationName}_yyyy_MM_dd_HH_mm_ss.pbSpecifies the path where the trace artifact is saved. By default, the value is autogenerated based on the tracing app package name and current timestamp.
-t $timeInSecond5Specifies the duration of the tracing, in seconds.
Note that: On MacOS, interactive tracing mode will be activated if you don't specifying the tracing duration. On windows, tracing dration must be specified, because we don't support interactive tracing mode on Windows currently.
-m $mappingPathSpecifies the mapping file path for the abofuscated app.
Note that: it's not the methodMapping file that was used in btrace 2.0, but the mapping file generated by proguard. There is no methodMapping file in btrace 3.0.
-mode $modeDecided at runtime.btrace currently support two kinds of modes:
  1. perfetto: The default mode for systems 8.1 and above, allowing collecting system atrace and ftrace using perfetto service besides the app trace.
  2. simple: The default mode for systems below 8.1, allowin collecting app trace only.
-maxAppTraceBufferSize $size200000Specifies the maximum count of stacktraces that our buffer allows to save, previously saved stacktraces will be overwritten if the maximum limit is met.
-sampleInterval $ns1000000Specifies the minimum sampling backtracing interval in nanoseconds.
-waitTraceTimeout20Specifies the timeout seconds for waiting for tracing data writing to complete and being pulled to the PC.
-s $serialSpecifies the device connected by adb.
-rAutomatically restarts the app to tracing the start up stage.
--listDisplays a list of supported atrace categories for the device.

Known Issues

ProblemsAdvices
1We currently only support devices running Android 8.0 or higher.Please use devices with Android 8.0 or higher.
2Java object allocation monitoring is not yet adapted for devices with Android 15 and above.If you need to inspect object memory allocation information or require more detailed tracing, please use devices with Android versions below 15.
3Devices that do not support Perfetto (mostly systems before 8.1) cannot collect system information such as CPU scheduling.Try with -mode simple.
432-bit devices or applications cannot collect tracing data.Please install and use 64-bit applications on 64-bit devices.

btrace for iOS

Record trace data offline without Instruments to help find performance issue of your app.

Installation

Clone source code, and add the following lines to your Podfile:

pod 'BTrace', :subspecs => ['Core', 'Debug'], :path => 'xxx/btrace-iOS'
pod 'fishhook', :git => 'https://github.com/facebook/fishhook.git', :branch => 'main'

Install command line tool:

# using homebrew
brew install libusbmuxd
brew install poetry

# install from the BTraceTool directory
poetry install

Usage

Activate the virtual environment before executing commands.

# activate from the BTraceTool directory
poetry shell
# or 
poetry env activate

Record

Note that if '-l' is not specified, app must have been launched before recording.

python3 -m btrace record [-h] [-i DEVICE_ID] [-b BUNDLE_ID] [-o OUTPUT] [-t TIME_LIMIT] [-d DSYM_PATH] [-m] [-l] [-s]
Options
OptionDescription
-h, --helpShow help
-i DEVICE_ID, --device_id DEVICE_IDDevice id. If not specified:
· If only one device is connected to Mac, it will be chosen
· If multiple devices are connected to Mac, prompt the user to make a selection
-b BUNDLE_ID --bundle_id BUNDLE_IDBundle id
-o OUTPUT --output OUTPUTOutput path. If not specified, data will be saved to '~/Desktop/btrace'
-t TIME_LIMIT --time_limit TIME_LIMITLimit recording time, default 3600s
-d DSYM_PATH --dsym_path DSYM_PATHDsym file path, or app path built by Xcode in debug mode. If specified,flamegraph will be displayed automatically after the recording ends
-m --main_thread_onlyIf given, only record main thread trace data
-l --launchIf given, app will be launched/relaunched, and start recording on app launch
-s --sys_symbolIf given, symbols in the system libraries will be parsed
Examples
python3 -m btrace record -i xxx -b xxx -d /xxxDebug-iphoneos/xxx.app
python3 -m btrace record -i xxx -b xxx -d /xxxDebug-iphoneos/xxx.dSYM

Stop

ctrl + c

Parse

When should the 'parse' command used?

  • '-d' option is not specified in the 'record' command.
  • reopen the parsed data
python3 -m btrace parse [-h] [-d DSYM_PATH] [-f] [-s] file_path
Options
OptionDescription
-h, --helpShow help
-d DSYM_PATH, --dsym_path DSYM_PATHDsym file path, or app path built by Xcode in debug mode
-s, --sys_symbolIf given, symbols in the system libraries will be parsed
-f, --forceIf given, force re-parsing trace data
Examples
btrace parse -d /xxx.dSYM xxx.sqlite
btrace parse -d /xxx.app xxx.sqlite

Technology Principle

If you are interested in the internal details of btrace 3.0, you can refer to the document: btrace 3.0 Internal Principle in Detail! .

Feedback

We've made a lot of improvements in btrace 3.0, including better error prompts. There might still be some cases where the prompts aren't accurate enough or the messages aren't clear. If you run into any issues, just give us a shout in the Lark group below, and we'll do our best to help you out. We always appreciate any other feedback or suggestions you might have too, thanks you!

Contributes

Contributing Guide

License

Apache License