1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef DFX_COREDUMP_SERVICE_H 16 #define DFX_COREDUMP_SERVICE_H 17 #if defined(__aarch64__) 18 19 #include "dfx_coredump_common.h" 20 #include "dfx_coredump_writer.h" 21 #include "dfx_dump_request.h" 22 #include "dfx_regs.h" 23 #include "elapsed_time.h" 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 class CoreDumpService { 28 public: 29 CoreDumpService() = default; 30 CoreDumpService(int32_t targetPid, int32_t targetTid, std::shared_ptr<DfxRegs> keyRegs); 31 CoreDumpService(const CoreDumpService&) = delete; 32 CoreDumpService &operator=(const CoreDumpService&) = delete; 33 ~CoreDumpService(); 34 bool StartCoreDump(); 35 bool FinishCoreDump(); 36 bool WriteSegmentHeader(); 37 bool WriteNoteSegment(); 38 bool WriteLoadSegment(); 39 bool WriteSectionHeader(); 40 void SetVmPid(int32_t vmPid); 41 void StartFirstStageDump(const ProcessDumpRequest& request); 42 void StartSecondStageDump(int32_t vmPid, const ProcessDumpRequest& request); 43 CoreDumpThread GetCoreDumpThread(); 44 std::string GetBundleNameItem(); 45 static bool IsHwasanCoredumpEnabled(); 46 static bool IsCoredumpSignal(const ProcessDumpRequest& request); 47 static bool IsCoredumpAllowed(const ProcessDumpRequest& request); 48 bool IsDoCoredump(); 49 bool GetKeyThreadData(const ProcessDumpRequest& request); 50 void SetCoreDumpServiceData(int32_t targetPid, int32_t targetTid, std::shared_ptr<DfxRegs> keyRegs); 51 private: 52 void DeInit(); 53 void ObtainDumpRegion(std::string &line, DumpMemoryRegions ®ion); 54 void ElfHeaderFill(Elf64_Ehdr &eh, uint16_t ePhnum); 55 bool CreateFile(); 56 bool MmapForFd(); 57 bool VerifyTrustlist(); 58 int CreateFileForCoreDump(); 59 static bool AdjustFileSize(int fd, uint64_t fileSize); 60 static bool UnlinkFile(const std::string &logPath); 61 uint64_t GetCoreFileSize(pid_t pid); 62 std::string GetCoredumpFileName(); 63 std::string GetCoredumpFilePath(); 64 private: 65 enum class WriteStatus { 66 START_STAGE, 67 WRITE_SEGMENT_HEADER_STAGE, 68 WRITE_NOTE_SEGMENT_STAGE, 69 WRITE_LOAD_SEGMENT_STAGE, 70 WRITE_SECTION_HEADER_STAGE, 71 STOP_STAGE, 72 DONE_STAGE, 73 }; 74 int fd_ {-1}; 75 uint64_t coreFileSize_ {0}; 76 uint64_t realCoreFileSize_ {0}; 77 Elf64_Half ePhnum_ {0}; 78 std::vector<DumpMemoryRegions> maps_; 79 std::string bundleName_; 80 char *mappedMemory_ {nullptr}; 81 char *currentPointer_ {nullptr}; 82 WriteStatus status_ = WriteStatus::START_STAGE; 83 CoreDumpThread coreDumpThread_; 84 ElapsedTime counter_; 85 std::shared_ptr<DfxRegs> keyRegs_; 86 bool isHwasanHap_ {false}; 87 CoreDumpKeyThreadData coreDumpKeyThreadData_; 88 }; 89 } // namespace HiviewDFX 90 } // namespace OHOS 91 #endif 92 #endif