13 Star 24 Fork 207

OpenHarmony / hiviewdfx_faultloggerd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

FaultLoggerd组件

简介

Faultloggerd部件是OpenHarmony中C/C++运行时崩溃临时日志的生成及管理模块。面向基于 Rust 开发的部件,Faultloggerd 提供了Rust Panic故障日志生成能力。系统开发者可以在预设的路径下找到故障日志,定位相关问题。

架构

架构

  • Native InnerKits 接口
    • SignalHandler:信号处理器,接收系统异常信号,触发抓取进程异常时的现场信息。
    • BackTrace:本地回栈库,提供进程内本地回栈能力。
    • DumpCatcher:堆栈信息抓取工具库,提供了抓取指定进程和线程的堆栈栈信息的能力。
    • FaultloggerdClient:崩溃临时日志管理客户端,接收申请文件描述符、堆栈导出等请求。
  • Rust 接口
    • PanicHandler:Rust PANIC故障处理器,封装faultloggerd回栈能力支持rust模块PANIC故障回栈。
    • Rustc Demangle:Rust 符号demangle库,支持Rust模块mangled符号解析。
  • Faultlogger Daemon 服务
    • FaultloggerdServer:核心服务处理模块,接收并处理客户端的请求。
    • FaultloggerdSecure:权限校验模块,对运行时崩溃日志生成和抓取提供权限管理和校验能力。
    • FaultloggerdConfig:崩溃临时日志管理模块。
    • FaultloggerdPipe:数据管道传输管理模块,提供数据传输管道申请和管理能力。
  • 工具
    • DumpCatcher Command Tool:提供命令行形式的主动抓栈工具,仅在Debug版本提供。
    • ProcessDump:进程信息抓取二进制工具,通过命令行方式提供抓取指定进程、线程堆栈信息的能力。
    • crasher:崩溃构造器,提供了崩溃构造和模拟能力。
    • Rust Panic Maker:Rust PANIC 故障构造器,提供了构造Rust模块的故障构造能力。

目前主要支持对以下C/C++运行时崩溃异常信号的处理:

信号值 信号 解释 触发原因
4 SIGILL 非法指令 执行了非法指令,通常是因为可执行文件本身出现错误,或者试图执行数据段,堆栈溢出时也有可能产生这个信号。
5 SIGTRAP 断点或陷阱异常 由断点指令或其它trap指令产生。
6 SIGABRT abort发出的信号 调用abort函数生成的信号。
7 SIGBUS 非法内存访问 非法地址,包括内存地址对齐(alignment)出错。比如访问一个四个字长的整数,但其地址不是4的倍数。它与SIGSEGV的区别在于后者是由于对合法存储地址的非法访问触发的(如访问不属于自己存储空间或只读存储空间)。
8 SIGFPE 浮点异常 在发生致命的算术运算错误时发出,不仅包括浮点运算错误,还包括溢出及除数为0等其它所有的算术的错误。
11 SIGSEGV 无效内存访问 试图访问未分配给自己的内存,或试图往没有写权限的内存地址写数据。
16 SIGSTKFLT 栈溢出 堆栈溢出。
31 SIGSYS 系统调用异常 非法的系统调用。

目录

faultloggerd/
├── OAT.xml
├── common                                 # 工具库和公共定义
├── docs                                   # 文档
├── example                                # 样例代码
├── frameworks                             # 主动抓栈实现
├── interfaces
│   ├── innerkits
│   │   ├── backtrace                      # 本地回栈库
│   │   ├── dump_catcher                   # 抓取调用栈基础库
│   │   ├── faultloggerd_client            # 崩溃临时日志管理服务客户端接口
│   │   └── signal_handler                 # 异常信号处理器
│   └── rust
│       ├── panic_handler                  # Rust Panic 处理器
│       ├── panic_report                   # Rust Panic 故障上报库
│       └── rustc_demangle                 # Rust demangle 库
├── services                               # faultloggerd 常驻服务
├── test
│   ├── funchook                           # hook 工具测试用例
│   ├── fuzztest                           # 模糊测试用例
│   ├── moduletest                         # 模块测试用例
│   ├── performancetest                    # 性能测试用例
│   ├── systemtest                         # 系统测试用例
│   └── unittest                           # 单元测试用例
└── tools
    ├── crasher_c                          # 崩溃构造器(C)
    ├── crasher_cpp                        # 崩溃构造器(C++)
    ├── dump_catcher                       # DumpCatcher 命令行工具
    ├── panic_maker                        # Rust Panic 故障构造器
    └── process_dump                       # 崩溃抓栈实现

使用说明

进程崩溃日志生成

目前已默认开启,进程因上述异常信号崩溃将会在设备 /data/log/faultlog/temp 目录下生成完整的崩溃日志,可基于该崩溃日志进行问题定位可分析。

崩溃日志介绍和和常见问题指南参看:faultloggerd FAQ

DumpCatcher 接口

DumpCatcher是提供给第三方模块使用的抓取调用栈基础库,其中包含了打印指定进程(或线程)的栈信息的接口函数。目前支持CPP调用栈和CPP-JS混合栈。

接口类名:DfxDumpCatcher

接口定义:

  • 默认:bool DumpCatch(int pid, int tid, std::string& msg);
  • 支持混合栈:bool DumpCatchMix(int pid, int tid, std::string& msg);
  • 支持输出到指定文件:bool DumpCatchFd(int pid, int tid, std::string& msg, int fd);
  • 支持批量抓栈:bool DumpCatchMultiPid(const std::vector<int> pidV, std::string& msg);

接口参数说明:

  • 接口返回值:
    • true:回栈成功,回栈信息存储在msg字符串对象中;
    • false:回栈失败。
  • 输入参数:
    • pid:希望回栈的进程号,如果需要回栈进程中的所有线程,则tid设定为0
    • tid:希望回栈的线程号;
    • fd:指定写入回栈信息的文件句柄;
  • 输出参数:
    • msg:如果回栈成功,则通过msg输出回栈后的信息。

注意:此接口需要调用者是管理员(system,root)用户,或者只抓取自己用户拥有的进程信息。

样例代码:

  • dump_catcher_demo.h
#ifndef DUMP_CATCHER_DEMO_H
#define DUMP_CATCHER_DEMO_H

#include <inttypes.h>

#define NOINLINE __attribute__((noinline))

#define GEN_TEST_FUNCTION(FuncNumA, FuncNumB)          \
    __attribute__((noinline)) int TestFunc##FuncNumA() \
    {                                                  \
        return TestFunc##FuncNumB();                   \
    }

// test functions for callstack depth test
int TestFunc0(void);
int TestFunc1(void);
int TestFunc2(void);
int TestFunc3(void);
int TestFunc4(void);
int TestFunc5(void);
int TestFunc6(void);
int TestFunc7(void);
int TestFunc8(void);
int TestFunc9(void);
int TestFunc10(void);

#endif // DUMP_CATCHER_DEMO_H
  • dump_catcher_demo.cpp
#include "dump_catcher_demo.h"

#include <iostream>
#include <string>
#include <unistd.h>
#include "dfx_dump_catcher.h"
using namespace std;

NOINLINE int TestFunc10(void)
{
    OHOS::HiviewDFX::DfxDumpCatcher dumplog;
    string msg = "";
    bool ret = dumplog.DumpCatch(getpid(), gettid(), msg);
    if (ret) {
        cout << msg << endl;
    }
    return 0;
}

// auto gen function
GEN_TEST_FUNCTION(0, 1)
GEN_TEST_FUNCTION(1, 2)
GEN_TEST_FUNCTION(2, 3)
GEN_TEST_FUNCTION(3, 4)
GEN_TEST_FUNCTION(4, 5)
GEN_TEST_FUNCTION(5, 6)
GEN_TEST_FUNCTION(6, 7)
GEN_TEST_FUNCTION(7, 8)
GEN_TEST_FUNCTION(8, 9)
GEN_TEST_FUNCTION(9, 10)

int main(int argc, char *argv[])
{
    TestFunc0();
    return 0;
}
  • BUILD.gn:
import("//base/hiviewdfx/faultloggerd/faultloggerd.gni")
import("//build/ohos.gni")

config("dumpcatcherdemo_config") {
  visibility = [ ":*" ]

  include_dirs = [
    ".",
    "//utils/native/base/include",
    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher/include/",
  ]
}

ohos_executable("dumpcatcherdemo") {
  sources = [ "dump_catcher_demo.cpp" ]

  configs = [ ":dumpcatcherdemo_config" ]

  deps = [
    "//base/hiviewdfx/faultloggerd/interfaces/innerkits/dump_catcher:libdfx_dumpcatcher",
    "//utils/native/base:utils",
  ]

  external_deps = [ "hilog:libhilog" ]

  install_enable = true
  part_name = "faultloggerd"
  subsystem_name = "hiviewdfx"
}
  • 执行结果:
# ./dumpcatcherdemo
#00 pc 0000000000000981(00000000004a8981) /data/test/dumpcatcherdemo
#01 pc 0000000000000a6d(00000000004a8a6d) /data/test/dumpcatcherdemo
#02 pc 0000000000000a63(00000000004a8a63) /data/test/dumpcatcherdemo
#03 pc 0000000000000a59(00000000004a8a59) /data/test/dumpcatcherdemo
#04 pc 0000000000000a4f(00000000004a8a4f) /data/test/dumpcatcherdemo
#05 pc 0000000000000a45(00000000004a8a45) /data/test/dumpcatcherdemo
#06 pc 0000000000000a3b(00000000004a8a3b) /data/test/dumpcatcherdemo
#07 pc 0000000000000a31(00000000004a8a31) /data/test/dumpcatcherdemo
#08 pc 0000000000000a27(00000000004a8a27) /data/test/dumpcatcherdemo
#09 pc 0000000000000a1d(00000000004a8a1d) /data/test/dumpcatcherdemo
#10 pc 0000000000000a13(00000000004a8a13) /data/test/dumpcatcherdemo
#11 pc 0000000000000a77(00000000004a8a77) /data/test/dumpcatcherdemo
#12 pc 00000000000c2b08(00000000b6fafb08) /system/lib/ld-musl-arm.so.1(__libc_start_main+116)
#13 pc 0000000000000938(00000000004a8938) /data/test/dumpcatcherdemo
#14 pc 00000000000008c4(00000000004a88c4) /data/test/dumpcatcherdemo

DumpCatcher 命令行工具

DumpCatcher 是指提供给用户的一个抓取调用栈命令行工具,由 DumpCatcher innerkits 接口封装实现,该工具通过 -p-t 参数指定进程和线程,以及 [-c -m -k] 可选参数指定抓栈的类型,命令执行后在命令行窗口打印指定的进程的线程栈信息。

工具名称:dumpcatcher

位置:/system/bin

参数说明:

  • -p [pid]:打印指定进程下面的所有线程栈信息;
  • -p [pid] -t [tid]:打印指定进程下面的指定线程信息。
  • [-c -m -k]:可选参数, 指定打印 -c(pp)C++调用栈、-m(ix)C++ JS混合调用栈、-k(ernel)调用栈类型。

返回打印说明:如果栈信息解析成功,则将信息显示到标准输出。

注意:使用此接口需要调用者是管理员(system,root)用户。

Rust Panic 故障处理器

TODO

处理流程

进程崩溃抓栈处理流程

进程崩溃抓栈处理流程

  1. 进程运行时异常崩溃后会收到来自 Kernel 发送的崩溃信号,由进程在启动加载的 SignalHandler 模块进行信号处理;
  2. 进程接收到崩溃信号后,保存当前进程上下文,fork 出子进程执行 ProcessDump 二进制进行抓栈;
  3. ProcessDumpFaultloggerd 申请文件句柄用于存储收集到的崩溃日志数据;
  4. ProcesDump 将完整崩溃日志数据写入到 /data/log/faultlog/temp 目录下进行临时存储;
  5. ProcessDump 收集完崩溃日志后,上报给 Hiview 进行后续处理;
  6. Hiview 接收到新增进程崩溃故障数据后,提取简易的崩溃日志存储到 /data/log/faultlog/faultlogger 目录下,并生成 HiSysevent 故障事件。

DumpCatcher 接口/命令行工具 主动抓栈处理流程

DumpCatcher主动抓栈处理流程图

  1. 进程A调用DumpCatcher库提供的系列接口(1B),或通过 DumpCatcher 命令行工具(1A),申请dump指定进程和线程的堆栈信息;
  2. 如果目前进程是当前进程,则直接调用 BackTrace Local 提供的能力进行本地回栈输出(2B);如果不是,则向 Faultloggerd 服务发送抓栈请求(2A);
  3. Faultloggerd 接收到抓栈请求,鉴通过权和管道申请等操作后,向目标进程发送 SIGDUMP(35) 信号触发主动抓栈(3);
  4. 目前进程接收到 SIGDUMP(35) 抓栈信号后,保存当前进程上下文,fork出子进程执行 ProcessDump 二进制进行抓栈,通过 Faultloggerd 申请到的管道返回调用栈数据(4)。

Rust Panic 故障日志收集流程

// TODO

相关仓

DFX子系统

hiviewdfx_hiview

hiviewdfx_hilog

hiviewdfx_hiappevent

hiviewdfx_hisysevent

hiviewdfx_faultloggerd

hiviewdfx_hilog_lite

hiviewdfx_hievent_lite

hiviewdfx_hiview_lite

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS

简介

Implementing C/C++ crash logging service | C/C++故障日志生成服务 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openharmony/hiviewdfx_faultloggerd.git
git@gitee.com:openharmony/hiviewdfx_faultloggerd.git
openharmony
hiviewdfx_faultloggerd
hiviewdfx_faultloggerd
master

搜索帮助