目录

Analyzing Exception Stack Traces

Introduction to Build Products

[h2]ArkTS Debug Product: sourceMaps.map

sourceMaps.map is a build product in release mode, stored in {ProjectPath}/{ModuleName}/build/{product}/cache/default/default@CompileArkTS/esmodule/release/sourceMaps.map.

[h2]C++ Debug Product: Debug .so Files

The .so files with debugging information are stored in {ProjectPath}/{ModuleName}/build/{product}/intermediates/libs.

For details, see Retaining Debugging Information with .so Files in Release Builds.

[h2]Code Obfuscation Product: nameCache.json

nameCache.json is an obfuscation mapping table, a build product in release mode, stored in {ProjectPath}/{ModuleName}/build/{product}/cache/default/default@CompileArkTS/esmodule/release/obfuscation.

C++ Stack Trace Analysis Principles

[h2]Build Option Differences

[h2]Retaining Debugging Information with .so Files in Release Builds

Typically, symbol tables and debugging information are stripped from .so files in release builds.

To retain the symbol table and debugging information in the .so file, configure the following under buildOption/externalNativeOptions in the build-profile.json5 file: "arguments": "-DCMAKE_BUILD_TYPE=RelWithDebInfo".

{
  "apiType": "stageMode",
  "buildOption": {
    "externalNativeOptions": {
      "path": "./src/main/cpp/CMakeLists.txt",
      "arguments": "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
      "cppFlags": "",
    }
  },
}

The compilation yields two .so outputs:

[h2]C++ Stack Trace Analysis Process

llvm-addr2line (stored in sdk/default/openharmony/native/llvm/bin under the DevEco Studio installation directory) is a tool that resolves function addresses to file names and line numbers.

Given an address in an executable file or an offset address in a relocatable object, the tool uses debugging information to find the associated file name and line number.

The table below lists common parameters.

Name

Description

-a

Displays the address in hexadecimal notation.

-C

Decodes symbolic names to user-level names.

-e

Sets the name of the executable file whose address needs to be converted.

-f

Displays the file name, line number, and function name.

-F

Displays the function name and file line number.

-j

Reads the offset of the specified part instead of the absolute address.

-p

Configures each address information to appear on a separate line.

Examples:

View the file name, line number, and function name.

llvm-addr2line -f -e File.so

Find the code location corresponding to a specific address.

llvm-addr2line 0x00000000004005e7 -e test -f -C -s

Sample code:

llvm-addr2line -e libapplication.so 00003714 -f -C

ASan stack trace analysis

ArkTS Stack Trace Analysis Principles

[h2]sourceMap Format

Figure 1 Source code
Figure 2 Build product

Actual code line mapping:

70->29

71->30

72->31

73->32

sourceMap structure

The sourceMaps.map file, as an individual module build product, is a merged file that contains the mapping relationships of all the files in that module. Each JSON has a key that serves as the primary key for the unique path of the build product. The running program's .abc file retains the corresponding key information. When an exception occurs during runtime and the code is attributed to this file, the output information is the key in the file. sources is the actual source code file information used for stack trace decompression; mappings is an encoded line and column number mapping table, with each file having an independent mapping relationship.

[h2]sourceMap Parsing Process

The following figure illustrates the structured parsing process for each key in the source map. Taking entry|har1|1.0.0|src/main/ets/pages/w.ts as an example, the fields are separated by the "|" delimiter and have the following meanings:

entry is the name of the module defined in the oh-package.json5 file, har1|1.0.0 is the name and version of the dependent HAR package defined in the oh-package.json5 file (or the module's own name and version if there is no dependent package), and src/main/ets/pages/w.ts is the path to the referenced source file.

Figure 3 Structured key processing in the sourceMap

De-obfuscation Analysis Principles

For details about code obfuscation configuration, see Code Obfuscation.

[h2]Introduction to the Code Obfuscation Product

The code obfuscation product is in effect an obfuscation mapping table: $ProjectPath\$ModuleName\build\$product\cache\default\default@CompileArkTS\esmodule\release\obfuscation\nameCache.json.

{
  "home/src/main/ets/homeability/HomeAbility.ets": {
    "IdentifierCache": {
      "#AbilityConstant": "AbilityConstant",
      "#hilog": "hilog",
      "#UIAbility": "UIAbility",
      "#Want": "Want",
      "#window": "window",
      "HomeAbility#onWindowStageCreate#__function": "i"
    },
    "MemberMethodCache": {
      "onCreate:10:16": "onCreate",
      "onDestroy:18:20": "onDestroy",
      "onWindowStageCreate:22:33": "onWindowStageCreate",
      "onWindowStageDestroy:35:38": "onWindowStageDestroy",
      "onForeground:40:43": "onForeground",
      "onBackground:45:48": "onBackground"
    },
    "obfName": "home/src/main/ets/homeability/HomeAbility.ets"
  },
  "compileSdkVersion": "5.0.0.25",
  "entryPackageInfo": "home|1.0.0",
  "PropertyCache": {
    "integratedHsp": "i",
    "asanClick": "j",
    "Index_Params": "m",
    "testNapi": "o",
    "Index": "t",
    "testObfuscation": "g2"
  }
}

[h2]Code Obfuscation Analysis Process

The exception stack is as follows:

Pid:58348
Uid:20020156
Reason:RangeError
Error name:RangeError
Error message:The number cannot be converted to a BigInt because it is not an integer
Stacktrace:
Cannot get SourceMap info, dump raw stack:
    at g2 (home|home|1.0.0|src/main/ets/pages/a.ts:6:6)
    at getVersion (home|home|1.0.0|src/main/ets/pages/a.ts:2:2)
    at anonymous (home|home|1.0.0|src/main/ets/pages/Index.ts:61:61)

  1. After transcoding via sourceMap, the stack trace is as follows:
    at g2 (home/src/main/ets/pages/tool.ts:7:27)
    at getVersion (home/src/main/ets/pages/tool.ts:2:30)
    at anonymous (home/src/main/ets/pages/Index.ets:23:40)

    a.ts is restored to tool.ts via sourceMap.

    "home|home|1.0.0|src/main/ets/pages/a.ts": {
        "version": 3,
        "file": "tool.ts",
        "sources": [
          "home/src/main/ets/pages/tool.ts"
        ],
        "names": [],
        "mappings": "AAAA,MAAM,CAAC,OAAO,UAAU,UAAU,IAAI,MAAM;IAC1C,IAAI,KAAM,IAAiB,CAAA;IAC3B,UAAW;AACb,CAAC;AAED,eAA2B,MAAM;IAC/B,IAAI,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;IACxB,OAAO,GAAG,CAAC;AACb,CAAC",
        "sourceRoot": "",
        "entry-package-info": "home|1.0.0"
      }
  2. Conduct function-level file name mapping.

    Check the obfuscation mapping table $ProjectPath\$ModuleName\build\$product\cache\default\default@CompileArkTS\esmodule\release\obfuscation\nameCache.json.

    "home/src/main/ets/pages/tool.ts": {
        "IdentifierCache": {
          "getVersion#res": "h2",
          "#testObfuscation:6:9": "g2"
        },
        "MemberMethodCache": {},
        "obfName": "home/src/main/ets/pages/a.ts"
      }

    The IdentifierCache and MemberMethodCache fields save the mapping relationship between the method names before and after obfuscation, in the following format: "source code method name:start line:end line":"obfuscated method name".

    The "source code method name" in the source code represents the parent-child relationship. Therefore, you can use the number sign (#) to retain the last name after mapping.

    The first stack has the obfuscated method name g2. If there are multiple g2 entries, you need to filter them by line number range. Therefore, use the above fields to restore this method name:

    1. Find the mapping table based on key(home/src/main/ets/pages/tool.ts).
    2. Find all entries in the above field where the obfuscated method name is g2. The entry found is:
      "#testObfuscation:6:9": "g2"
    3. Find the entry whose line number range includes the line number restored in step 1. The line number obtained in step 1 is 7, which is included in 6–9. Therefore, the corresponding source code method name is #testObfuscation, and the result after string processing is testObfuscation.
      at testObfuscation (home/src/main/ets/pages/tool.ts:7:27)
      at getVersion (home/src/main/ets/pages/tool.ts:2:30)
      at anonymous (home/src/main/ets/pages/Index.ets:23:40)