Name Date Size

..25-Oct-20244 KiB

.gitattributesH A D25-Oct-2024631

.gitignoreH A D25-Oct-202421

BUILD.gnH A D25-Oct-20241.4 KiB

bundle.jsonH A D25-Oct-20245.4 KiB

common/H25-Oct-20244 KiB

docs/H25-Oct-20244 KiB

example/H25-Oct-20244 KiB

faultloggerd.gniH A D25-Oct-20241.9 KiB

figures/H25-Oct-20244 KiB

frameworks/H25-Oct-20244 KiB

interfaces/H25-Oct-20244 KiB

LICENSEH A D25-Oct-20249.9 KiB

OAT.xmlH A D25-Oct-20248 KiB

README.mdH A D25-Oct-202413.6 KiB

README_zh.mdH A D25-Oct-202413.2 KiB

services/H25-Oct-20244 KiB

test/H25-Oct-20244 KiB

tools/H25-Oct-20244 KiB

README.md

1# FaultLoggerd
2
3-   Introduction
4-   Architecture
5-   Directory Structure
6-   How to Use
7    -   DumpCatcher
8    -   ProcessDump
9-   Service Process
10    -   DumpCatcher SDK Service Process
11    -   ProcessDump Service Process
12    -   Process Crash Service Process
13-   Repositories Involved
14
15## Introduction
16
17Faultloggerd is a component that generates and manages temporary C/C++ runtime crash logs in OpenHarmony. You can find crash logs in the preset directory to locate faults.
18
19## Architecture
20
21![Architecture](figures/faultloggerd-architecture.png)
22
23* SignalHandler: signal handler, which receives system exception signals and triggers the capture of process exception information.
24* DumpCatcher: stack information capture tool, which provides the function of capturing stack information of the specified process and thread.
25* FaultloggerdClient: temporary crash log management client, which receives requests for file descriptors and stack exporting.
26* ProcessDump: binary tool for capturing process information, which provides the function of capturing stack information of the specified process and thread in command line mode.
27* Crasher: crash constructor, which constructs and simulates crashes.
28* FaultloggerdServer: core service processing module, which receives and processes requests from FaultloggerdClient.
29* FaultloggerdSecure: permission verification module, which provides permission management and verification for runtime crash log generation and capture.
30* FaultloggerdConfig: temporary crash log management module.
31
32The following table describes the exception signals that can be processed.
33
34| Signal Value| Signal     | Description           | Cause                                                    |
35| ------ | --------- | --------------- | ------------------------------------------------------------ |
36| 4      | SIGILL    | Invalid instruction       | The executable file incurs an error or attempts to execute a data segment. This signal may also be generated in the case of stack overflow.|
37| 5      | SIGTRAP   | Breakpoint or trap  | A breakpoint instruction or any other trap instruction is executed.                              |
38| 6      | SIGABRT   | Abort signal| The abort function is called.                                   |
39| 7      | SIGBUS    | Illegal memory access   | Invalid address, including memory address alignment error, is identified. For example, accessing an integer of four digits at an address not divisible by four. The difference between SIGBUS and SIGSEGV is that SIGSEGV is triggered by unauthorized access to valid storage addresses (for example, access to storage space that is read only or does not belong to the process).|
40| 8      | SIGFPE    | Floating-point exception       | A fatal arithmetic error, for example, floating-point arithmetic error, division overflow, or division by 0, has occurred.|
41| 11     | SIGSEGV   | Invalid memory access   | The process attempts to access memory that is not allocated to itself, or write data to a memory address that does not have the write permission.|
42| 16     | SIGSTKFLT | Stack overflow         | The stack overflows.                                                  |
43| 31     | SIGSYS    | System call exception   | An invalid system call is initiated.                                            |
44
45## Directory Structure
46
47```txt
48faultloggerd/
49├── OAT.xml
50├── common                                 # Common definitions
51├── faultloggerd.gni
52├── interfaces                             # APIs
53│   └── innerkits
54│       ├── dump_catcher                   # Stack information capture tool
55│       ├── faultloggerd_client            # Temporary crash log management client
56│       └── signal_handler                 # Signal handler
57├── ohos.build
58├── services                               # FaultLoggerd services
59│   ├── BUILD.gn
60│   ├── config                             # Startup configuration
61│   ├── fault_logger_config.cpp            # Log file management
62│   ├── fault_logger_config.h              # Log file management
63│   ├── fault_logger_daemon.cpp            # Faultloggerd service implementation
64│   ├── fault_logger_daemon.h              # Faultloggerd service implementation
65│   ├── fault_logger_secure.cpp            # Permission management and verification
66│   ├── fault_logger_secure.h              # Permission management and verification
67│   ├── main.cpp
68│   └── test
69├── test                                   # Test resources
70│   ├── BUILD.gn
71│   ├── fuzztest                           # Fuzz test
72│   ├── performancetest                    # Performance test
73│   └── systemtest                         # System function test
74└── tools                                  # Tools
75    ├── crasher_c                          # Crash constructor (C)
76    ├── crasher_cpp                        # Crash constructor (C++)
77    └── process_dump                       # Process information capture tool
78```
79
80## How to Use
81
82### DumpCatcher
83
84DumpCatcher allows an application to capture call stack information. It provides the API for printing stack information of the specified process and thread.
85
86Interface class: `DfxDumpCatcher`
87
88Interface method: `bool DumpCatch(const int pid, const int tid, std::string& msg);`
89
90Parameters:
91
92* Return value:
93  * `true`: Dumping of stack information is successful. Related information is stored in the `msg` string object.
94  * `false`: Dumping of stack information has failed.
95* Input parameters:
96  * `pid`: ID of the process for dumping stack information. If all threads in the process need to be back traced, set `tid` to **0**.
97  * `tid`: ID of the thread for dumping stack information.
98* Output parameters:
99  * `msg`: dumping result.
100
101> Note: Only the administrator (**system** or **root**) is allowed to capture all process information. Common users can only capture information on their own processes. To capture call stack information of a process that does not belong to the current user group, ensure that you have permissions to read **/proc/pid/maps** and implement **ptrace** on the target process.
102
103Sample Code
104
105* dump_catcher_demo.h
106
107```c++
108#ifndef DUMP_CATCHER_DEMO_H
109#define DUMP_CATCHER_DEMO_H
110
111#include <inttypes.h>
112
113#define NOINLINE __attribute__((noinline))
114
115#define GEN_TEST_FUNCTION(FuncNumA, FuncNumB)          \
116    __attribute__((noinline)) int TestFunc##FuncNumA() \
117    {                                                  \
118        return TestFunc##FuncNumB();                   \
119    }
120
121// Test functions for callstack depth test
122int TestFunc0(void);
123int TestFunc1(void);
124int TestFunc2(void);
125int TestFunc3(void);
126int TestFunc4(void);
127int TestFunc5(void);
128int TestFunc6(void);
129int TestFunc7(void);
130int TestFunc8(void);
131int TestFunc9(void);
132int TestFunc10(void);
133
134#endif // DUMP_CATCHER_DEMO_H
135```
136
137  * dump_catcher_demo.cpp
138
139```c++
140#include "dump_catcher_dump.h"
141
142#include <iostream>
143#include <string>
144#include <unistd.h>
145#include "dfx_dump_catcher.h"
146using namespace std;
147
148NOINLINE int TestFunc10(void)
149{
150    OHOS::HiviewDFX::DfxDumpCatcher dumplog;
151    string msg = "";
152    bool ret = dumplog.DumpCatch(getpid(), gettid(), msg);
153    if (ret) {
154        cout << msg << endl;
155    }
156    return 0;
157}
158
159// auto gen function
160GEN_TEST_FUNCTION(0, 1)
161GEN_TEST_FUNCTION(1, 2)
162GEN_TEST_FUNCTION(2, 3)
163GEN_TEST_FUNCTION(3, 4)
164GEN_TEST_FUNCTION(4, 5)
165GEN_TEST_FUNCTION(5, 6)
166GEN_TEST_FUNCTION(6, 7)
167GEN_TEST_FUNCTION(7, 8)
168GEN_TEST_FUNCTION(8, 9)
169GEN_TEST_FUNCTION(9, 10)
170
171int main(int argc, char *argv[])
172{
173    TestFunc0();
174    return 0;
175}
176```
177
178* Sample **BUILD.gn** file:
179
180```gn
181import("//base/hiviewdfx/faultloggerd/faultloggerd.gni")
182import("//build/ohos.gni")
183
184config("dumpcatcherdemo_config") {
185  visibility = [ ":*" ]
186
187  include_dirs = [
188    ".",
189    "//utils/native/base/include",
190    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher/include/",
191  ]
192}
193
194ohos_executable("dumpcatcherdemo") {
195  sources = [ "dump_catcher_demo.cpp" ]
196
197  configs = [ ":dumpcatcherdemo_config" ]
198
199  deps = [
200    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher:libdfx_dumpcatcher",
201    "//utils/native/base:utils",
202  ]
203
204  external_deps = [ "hilog:libhilog" ]
205
206  install_enable = true
207  part_name = "faultloggerd"
208  subsystem_name = "hiviewdfx"
209}
210```
211
212* Execution result:
213
214```txt
215# ./dumpcatcherdemo
216#00 pc 0000000000000981(00000000004a8981) /data/test/dumpcatcherdemo
217#01 pc 0000000000000a6d(00000000004a8a6d) /data/test/dumpcatcherdemo
218#02 pc 0000000000000a63(00000000004a8a63) /data/test/dumpcatcherdemo
219#03 pc 0000000000000a59(00000000004a8a59) /data/test/dumpcatcherdemo
220#04 pc 0000000000000a4f(00000000004a8a4f) /data/test/dumpcatcherdemo
221#05 pc 0000000000000a45(00000000004a8a45) /data/test/dumpcatcherdemo
222#06 pc 0000000000000a3b(00000000004a8a3b) /data/test/dumpcatcherdemo
223#07 pc 0000000000000a31(00000000004a8a31) /data/test/dumpcatcherdemo
224#08 pc 0000000000000a27(00000000004a8a27) /data/test/dumpcatcherdemo
225#09 pc 0000000000000a1d(00000000004a8a1d) /data/test/dumpcatcherdemo
226#10 pc 0000000000000a13(00000000004a8a13) /data/test/dumpcatcherdemo
227#11 pc 0000000000000a77(00000000004a8a77) /data/test/dumpcatcherdemo
228#12 pc 00000000000c2b08(00000000b6fafb08) /system/lib/ld-musl-arm.so.1(__libc_start_main+116)
229#13 pc 0000000000000938(00000000004a8938) /data/test/dumpcatcherdemo
230#14 pc 00000000000008c4(00000000004a88c4) /data/test/dumpcatcherdemo
231```
232
233### ProcessDump
234
235ProcessDump is a command line tool provided for users to capture call stack information. This tool uses the `-p` and `-t` parameters to specify the target process and thread. After command execution is complete, it displays the thread stack information of the specified process in the command line window.
236
237Tool name: `processdump`
238
239Location: `/system/bin`
240
241Parameters:
242
243* `-p [pid]`: prints all thread stack information of the specified process.
244* `-p [pid] -t [tid]`: prints information about the specified thread of a process.
245
246Return result: Stack information parsed.
247
248> Note: Only the administrator (**system** or **root**) is allowed to use this API.
249
250Example: querying call stack information of the Hiview main thread
251
252```txt
253# ps -A | grep hiview
254  114 ?        00:00:00 hiview
255# processdump -p 114 -t 114
256Tid:114, Name:hiview
257#00 pc 0000000000089824(00000000b6f44824) /system/lib/ld-musl-arm.so.1(ioctl+68)
258#01 pc 000000000002a709(00000000b6c56709) /system/lib/libipc_core.z.so(_ZN4OHOS15BinderConnector11WriteBinderEmPv+16)
259#02 pc 000000000002ba75(00000000b6c57a75) /system/lib/libipc_core.z.so(_ZN4OHOS13BinderInvoker18TransactWithDriverEb+224)
260#03 pc 000000000002bb37(00000000b6c57b37) /system/lib/libipc_core.z.so(_ZN4OHOS13BinderInvoker13StartWorkLoopEv+22)
261#04 pc 000000000002c211(00000000b6c58211) /system/lib/libipc_core.z.so(_ZN4OHOS13BinderInvoker10JoinThreadEb+36)
262#05 pc 0000000000038d07(00000000004bcd07) /system/bin/hiview(_ZNSt3__h6vectorINS_9sub_matchINS_11__wrap_iterIPKcEEEENS_9allocatorIS6_EEE8__appendEj+596)
263#06 pc 0000000000028655(00000000004ac655) /system/bin/hiview
264#07 pc 00000000000c2b08(00000000b6f7db08) /system/lib/ld-musl-arm.so.1(__libc_start_main+116)
265#08 pc 00000000000285f4(00000000004ac5f4) /system/bin/hiview
266#09 pc 0000000000028580(00000000004ac580) /system/bin/hiview
267```
268
269## Service Process
270
271### DumpCatcher SDK Service Process
272
273![DumpCatcher service process](figures/dumpcatcher-process.png)
274
2751. Process A invokes the `DumpCatch()` API provided by `DumpCatcher` to request for dumping stack information of the specified process and thread.
2762. After receiving the dumping request from process A, `DumpCatcher` runs `ProcessDump` to obtain the stack information.
2773. `ProcessDump` requests permission verification from `Faultloggerd`.
2784. If the permission verification is successful, `Faultloggerd` returns a file descriptor to `ProcessDump`. `ProcessDump` then sends the dumping result to `DumpCatcher`.
279
280### ProcessDump Service Process
281
282![ProcessDump service process](figures/processdump-process.png)
283
2841. `Shell` runs the `processdump -p [pid] -t [tid]` command to request for dumping stack information of the specified process and thread.
2852. `ProcessDump` requests permission verification from `Faultloggerd`.
2863. If the permission verification is successful, `Faultloggerd` returns a file descriptor to `ProcessDump`. `ProcessDump` then writes the dumping result to the standard output.
287
288### Faultloggerd Service Process
289
290![Faultloggerd service process](figures/faultloggerd-process.png)
291
2921. Process B calls the `DFX_SignalHandler` function to detect crash exception signals from `SignalHandler`.
2932. When detecting a crash exception signal, `SignalHandler` forks a child process and runs `ProcessDump` to dump the stack information of the crashed process and thread.
2943. After reading the stack information, `ProcessDump` writes logs to the temporary storage directory in `Faultloggerd`.
2954. `Faultloggerd` calls `AddFaultLog()` to report the fault to `Hiview` for subsequent processing.
296
297
298## Repositories Involved
299
300[DFX Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/dfx.md)
301
302[hiviewdfx\_hiview](https://gitee.com/openharmony/hiviewdfx_hiview/blob/master/README.md)
303
304[hiviewdfx\_hilog](https://gitee.com/openharmony/hiviewdfx_hilog/blob/master/README.md)
305
306[hiviewdfx\_hiappevent](https://gitee.com/openharmony/hiviewdfx_hiappevent/blob/master/README.md)
307
308[hiviewdfx\_hisysevent](https://gitee.com/openharmony/hiviewdfx_hisysevent/blob/master/README.md)
309
310**hiviewdfx\_faultloggerd**
311
312[hiviewdfx\_hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README.md)
313
314[hiviewdfx\_hievent\_lite](https://gitee.com/openharmony/hiviewdfx_hievent_lite/blob/master/README.md)
315
316[hiviewdfx\_hiview\_lite](https://gitee.com/openharmony/hiviewdfx_hiview_lite/blob/master/README.md)
317

README_zh.md

1# FaultLoggerd组件
2
3## 简介
4
5Faultloggerd部件是OpenHarmony中C/C++运行时崩溃临时日志的生成及管理模块。面向基于 Rust 开发的部件,Faultloggerd 提供了Rust Panic故障日志生成能力。系统开发者可以在预设的路径下找到故障日志,定位相关问题。
6
7## 架构
8
9![架构](figures/faultloggerd-architecture.png)
10
11* Native InnerKits 接口
12  * SignalHandler:信号处理器,接收系统异常信号,触发抓取进程异常时的现场信息。
13  * BackTrace:本地回栈库,提供进程内本地回栈能力。
14  * DumpCatcher:堆栈信息抓取工具库,提供了抓取指定进程和线程的堆栈信息的能力。
15  * FaultloggerdClient:崩溃临时日志管理客户端,接收申请文件描述符、堆栈导出等请求。
16* Rust 接口
17  * PanicHandler:Rust PANIC故障处理器,封装faultloggerd回栈能力支持rust模块PANIC故障回栈。
18  * Rustc Demangle:Rust 符号demangle库,支持Rust模块mangled符号解析。
19* Faultlogger Daemon 服务
20  * FaultloggerdServer:核心服务处理模块,接收并处理客户端的请求。
21  * FaultloggerdSecure:权限校验模块,对运行时崩溃日志生成和抓取提供权限管理和校验能力。
22  * FaultloggerdConfig:崩溃临时日志管理模块。
23  * FaultloggerdPipe:数据管道传输管理模块,提供数据传输管道申请和管理能力。
24* 工具
25  * DumpCatcher Command Tool:提供命令行形式的主动抓栈工具,仅在Debug版本提供。
26  * ProcessDump:进程信息抓取二进制工具,通过命令行方式提供抓取指定进程、线程堆栈信息的能力。
27  * crasher:崩溃构造器,提供了崩溃构造和模拟能力。
28  * Rust Panic Maker:Rust PANIC 故障构造器,提供了构造Rust模块的故障构造能力。
29
30目前主要支持对以下C/C++运行时崩溃异常信号的处理:
31
32| 信号值 | 信号      | 解释            | 触发原因                                                     |
33| ------ | --------- | --------------- | ------------------------------------------------------------ |
34| 4      | SIGILL    | 非法指令        | 执行了非法指令,通常是因为可执行文件本身出现错误,或者试图执行数据段,堆栈溢出时也有可能产生这个信号。 |
35| 5      | SIGTRAP   | 断点或陷阱异常  | 由断点指令或其它trap指令产生。                               |
36| 6      | SIGABRT   | abort发出的信号 | 调用abort函数生成的信号。                                    |
37| 7      | SIGBUS    | 非法内存访问    | 非法地址,包括内存地址对齐(alignment)出错。比如访问一个四个字长的整数,但其地址不是4的倍数。它与SIGSEGV的区别在于后者是由于对合法存储地址的非法访问触发的(如访问不属于自己存储空间或只读存储空间)。 |
38| 8      | SIGFPE    | 浮点异常        | 在发生致命的算术运算错误时发出,不仅包括浮点运算错误,还包括溢出及除数为0等其它所有的算术的错误。 |
39| 11     | SIGSEGV   | 无效内存访问    | 试图访问未分配给自己的内存,或试图往没有写权限的内存地址写数据。 |
40| 16     | SIGSTKFLT | 栈溢出          | 堆栈溢出。                                                   |
41| 31     | SIGSYS    | 系统调用异常    | 非法的系统调用。                                             |
42
43## 目录
44
45```txt
46faultloggerd/
47├── OAT.xml
48├── common                                 # 工具库和公共定义
49├── docs                                   # 文档
50├── example                                # 样例代码
51├── frameworks                             # 主动抓栈实现
52├── interfaces
53│   ├── innerkits
54│   │   ├── backtrace                      # 本地回栈库
55│   │   ├── dump_catcher                   # 抓取调用栈基础库
56│   │   ├── faultloggerd_client            # 崩溃临时日志管理服务客户端接口
57│   │   └── signal_handler                 # 异常信号处理器
58│   └── rust
59│       ├── panic_handler                  # Rust Panic 处理器
60│       ├── panic_report                   # Rust Panic 故障上报库
61│       └── rustc_demangle                 # Rust demangle 库
62├── services                               # faultloggerd 常驻服务
63├── test
64│   ├── funchook                           # hook 工具测试用例
65│   ├── fuzztest                           # 模糊测试用例
66│   ├── moduletest                         # 模块测试用例
67│   ├── performancetest                    # 性能测试用例
68│   ├── systemtest                         # 系统测试用例
69│   └── unittest                           # 单元测试用例
70└── tools
71    ├── crasher_c                          # 崩溃构造器(C)
72    ├── crasher_cpp                        # 崩溃构造器(C++)
73    ├── dump_catcher                       # DumpCatcher 命令行工具
74    ├── panic_maker                        # Rust Panic 故障构造器
75    └── process_dump                       # 崩溃抓栈实现
76```
77
78## 使用说明
79
80### 进程崩溃日志生成
81
82目前已默认开启,进程因上述异常信号崩溃将会在设备 `/data/log/faultlog/temp` 目录下生成完整的崩溃日志,可基于该崩溃日志进行问题定位和分析。
83
84> 崩溃日志介绍和常见问题指南参见:[faultloggerd FAQ](docs/usage.md)
85
86### DumpCatcher 接口
87
88DumpCatcher是提供给第三方模块使用的抓取调用栈基础库,其中包含了打印指定进程(或线程)的栈信息的接口函数。目前支持CPP调用栈和CPP-JS混合栈。
89
90接口类名:`DfxDumpCatcher`
91
92接口定义:
93* 默认(支持混合栈):`bool DumpCatch(int pid, int tid, std::string& msg);`
94* 支持输出到指定文件:`bool DumpCatchFd(int pid, int tid, std::string& msg, int fd);`
95* 支持批量抓栈:`bool DumpCatchMultiPid(const std::vector<int> pidV, std::string& msg);`
96
97接口参数说明:
98* 接口返回值:
99  * `true`:回栈成功,回栈信息存储在`msg`字符串对象中;
100  * `false`:回栈失败。
101* 输入参数:
102  * `pid`:希望回栈的进程号,如果需要回栈进程中的所有线程,则`tid`设定为`0`;
103  * `tid`:希望回栈的线程号;
104  * `fd`:指定写入回栈信息的文件句柄;
105* 输出参数:
106  * `msg`:如果回栈成功,则通过`msg`输出回栈后的信息。
107
108> 注意:此接口需要调用者是管理员(system,root)用户,或者只抓取自己用户拥有的进程信息。
109
110样例代码:
111
112* dump_catcher_demo.h
113
114```c++
115#ifndef DUMP_CATCHER_DEMO_H
116#define DUMP_CATCHER_DEMO_H
117
118#include <inttypes.h>
119
120#define NOINLINE __attribute__((noinline))
121
122#define GEN_TEST_FUNCTION(FuncNumA, FuncNumB)          \
123    __attribute__((noinline)) int TestFunc##FuncNumA() \
124    {                                                  \
125        return TestFunc##FuncNumB();                   \
126    }
127
128// test functions for callstack depth test
129int TestFunc0(void);
130int TestFunc1(void);
131int TestFunc2(void);
132int TestFunc3(void);
133int TestFunc4(void);
134int TestFunc5(void);
135int TestFunc6(void);
136int TestFunc7(void);
137int TestFunc8(void);
138int TestFunc9(void);
139int TestFunc10(void);
140
141#endif // DUMP_CATCHER_DEMO_H
142```
143
144  * dump_catcher_demo.cpp
145
146```c++
147#include "dump_catcher_demo.h"
148
149#include <iostream>
150#include <string>
151#include <unistd.h>
152#include "dfx_dump_catcher.h"
153using namespace std;
154
155NOINLINE int TestFunc10(void)
156{
157    OHOS::HiviewDFX::DfxDumpCatcher dumplog;
158    string msg = "";
159    bool ret = dumplog.DumpCatch(getpid(), gettid(), msg);
160    if (ret) {
161        cout << msg << endl;
162    }
163    return 0;
164}
165
166// auto gen function
167GEN_TEST_FUNCTION(0, 1)
168GEN_TEST_FUNCTION(1, 2)
169GEN_TEST_FUNCTION(2, 3)
170GEN_TEST_FUNCTION(3, 4)
171GEN_TEST_FUNCTION(4, 5)
172GEN_TEST_FUNCTION(5, 6)
173GEN_TEST_FUNCTION(6, 7)
174GEN_TEST_FUNCTION(7, 8)
175GEN_TEST_FUNCTION(8, 9)
176GEN_TEST_FUNCTION(9, 10)
177
178int main(int argc, char *argv[])
179{
180    TestFunc0();
181    return 0;
182}
183```
184
185* BUILD.gn186
187```gn
188import("//base/hiviewdfx/faultloggerd/faultloggerd.gni")
189import("//build/ohos.gni")
190
191config("dumpcatcherdemo_config") {
192  visibility = [ ":*" ]
193
194  include_dirs = [
195    ".",
196    "//utils/native/base/include",
197    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher/include/",
198  ]
199}
200
201ohos_executable("dumpcatcherdemo") {
202  sources = [ "dump_catcher_demo.cpp" ]
203
204  configs = [ ":dumpcatcherdemo_config" ]
205
206  deps = [
207    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher:libdfx_dumpcatcher",
208    "//utils/native/base:utils",
209  ]
210
211  external_deps = [ "hilog:libhilog" ]
212
213  install_enable = true
214  part_name = "faultloggerd"
215  subsystem_name = "hiviewdfx"
216}
217```
218
219* 执行结果:
220
221```txt
222# ./dumpcatcherdemo
223#00 pc 0000000000000981(00000000004a8981) /data/test/dumpcatcherdemo
224#01 pc 0000000000000a6d(00000000004a8a6d) /data/test/dumpcatcherdemo
225#02 pc 0000000000000a63(00000000004a8a63) /data/test/dumpcatcherdemo
226#03 pc 0000000000000a59(00000000004a8a59) /data/test/dumpcatcherdemo
227#04 pc 0000000000000a4f(00000000004a8a4f) /data/test/dumpcatcherdemo
228#05 pc 0000000000000a45(00000000004a8a45) /data/test/dumpcatcherdemo
229#06 pc 0000000000000a3b(00000000004a8a3b) /data/test/dumpcatcherdemo
230#07 pc 0000000000000a31(00000000004a8a31) /data/test/dumpcatcherdemo
231#08 pc 0000000000000a27(00000000004a8a27) /data/test/dumpcatcherdemo
232#09 pc 0000000000000a1d(00000000004a8a1d) /data/test/dumpcatcherdemo
233#10 pc 0000000000000a13(00000000004a8a13) /data/test/dumpcatcherdemo
234#11 pc 0000000000000a77(00000000004a8a77) /data/test/dumpcatcherdemo
235#12 pc 00000000000c2b08(00000000b6fafb08) /system/lib/ld-musl-arm.so.1(__libc_start_main+116)
236#13 pc 0000000000000938(00000000004a8938) /data/test/dumpcatcherdemo
237#14 pc 00000000000008c4(00000000004a88c4) /data/test/dumpcatcherdemo
238```
239
240### DumpCatcher 命令行工具
241
242DumpCatcher 是指提供给用户的一个抓取调用栈命令行工具,由 DumpCatcher innerkits 接口封装实现,该工具通过 `-p`、`-t` 参数指定进程和线程,命令执行后在命令行窗口打印指定的进程的线程栈信息。
243
244工具名称:`dumpcatcher`
245
246位置:`/system/bin`
247
248参数说明:
249
250* `-p [pid]`:打印指定进程下面的所有线程栈信息;
251* `-p [pid] -t [tid]`:打印指定进程下面的指定线程信息。
252
253返回打印说明:如果栈信息解析成功,则将信息显示到标准输出。
254
255> 注意:使用此接口需要调用者是管理员(system,root)用户。
256
257### Rust Panic 故障处理器
258
259> TODO
260
261## 处理流程
262
263### 进程崩溃抓栈处理流程
264
265![进程崩溃抓栈处理流程](figures/crash-process.png)
266
2671. 进程运行时异常崩溃后会收到来自 `Kernel` 发送的崩溃信号,由进程在启动加载的 `SignalHandler` 模块进行信号处理;
2682. 进程接收到崩溃信号后,保存当前进程上下文,fork 出子进程执行 `ProcessDump` 二进制进行抓栈;
2693. `ProcessDump` 向 `Faultloggerd` 申请文件句柄用于存储收集到的崩溃日志数据;
2704. `ProcesDump` 将完整崩溃日志数据写入到 `/data/log/faultlog/temp` 目录下进行临时存储;
2715. `ProcessDump` 收集完崩溃日志后,上报给 `Hiview` 进行后续处理;
2724. `Hiview` 接收到新增进程崩溃故障数据后,提取简易的崩溃日志存储到 `/data/log/faultlog/faultlogger` 目录下,并生成 `HiSysevent` 故障事件。
273
274### DumpCatcher 接口/命令行工具 主动抓栈处理流程
275
276![DumpCatcher主动抓栈处理流程图](figures/dumpcatcher-process.png)
277
2781. 进程A调用`DumpCatcher`库提供的系列接口(1B),或通过 `DumpCatcher` 命令行工具(1A),申请dump指定进程和线程的堆栈信息;
2792. 如果目前进程是当前进程,则直接调用 `BackTrace Local` 提供的能力进行本地回栈输出(2B);如果不是,则向 `Faultloggerd` 服务发送抓栈请求(2A);
2803. `Faultloggerd` 接收到抓栈请求,通过鉴权和管道申请等操作后,向目标进程发送 `SIGDUMP(35)` 信号触发主动抓栈(3);
2814. 目前进程接收到 `SIGDUMP(35)` 抓栈信号后,保存当前进程上下文,fork出子进程执行 `ProcessDump` 二进制进行抓栈,通过 `Faultloggerd` 申请到的管道返回调用栈数据(4)。
282
283### Rust Panic 故障日志收集流程
284
285// TODO
286
287## 相关仓
288
289[DFX子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/DFX%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
290
291[hiviewdfx\_hiview](https://gitee.com/openharmony/hiviewdfx_hiview/blob/master/README_zh.md)
292
293[hiviewdfx\_hilog](https://gitee.com/openharmony/hiviewdfx_hilog/blob/master/README_zh.md)
294
295[hiviewdfx\_hiappevent](https://gitee.com/openharmony/hiviewdfx_hiappevent/blob/master/README_zh.md)
296
297[hiviewdfx\_hisysevent](https://gitee.com/openharmony/hiviewdfx_hisysevent/blob/master/README_zh.md)
298
299**hiviewdfx\_faultloggerd**
300
301[hiviewdfx\_hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README_zh.md)
302
303[hiviewdfx\_hievent\_lite](https://gitee.com/openharmony/hiviewdfx_hievent_lite/blob/master/README_zh.md)
304
305[hiviewdfx\_hiview\_lite](https://gitee.com/openharmony/hiviewdfx_hiview_lite/blob/master/README_zh.md)
306
307