1 /* 2 * Copyright (c) 2021 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_UNWIND_LOCAL_H 16 #define DFX_UNWIND_LOCAL_H 17 #include <atomic> 18 #include <chrono> 19 #include <cinttypes> 20 #include <condition_variable> 21 #include <csignal> 22 #include <libunwind.h> 23 #include <mutex> 24 #include "dfx_define.h" 25 #include "dfx_dump_request.h" 26 #include "dfx_frame.h" 27 #include "dfx_logger.h" 28 #include "dfx_maps.h" 29 #include "dfx_process.h" 30 #include "dfx_symbols_cache.h" 31 #include "dfx_thread.h" 32 #include "dfx_util.h" 33 #include "nocopyable.h" 34 35 namespace OHOS { 36 namespace HiviewDFX { 37 class DfxUnwindLocal { 38 public: 39 static DfxUnwindLocal &GetInstance(); 40 ~DfxUnwindLocal() = default; 41 42 bool Init(); 43 void Destroy(); 44 bool HasInit(); 45 46 bool ExecLocalDumpUnwind(size_t skipFramNum); 47 bool ExecLocalDumpUnwindByWait(); 48 std::string CollectUnwindResult(int32_t tid); 49 void CollectUnwindFrames(std::vector<std::shared_ptr<DfxFrame>>& frames); 50 bool SendAndWaitRequest(int32_t tid); 51 52 private: 53 DfxUnwindLocal(); 54 DISALLOW_COPY_AND_MOVE(DfxUnwindLocal); 55 56 void InstallLocalDumper(int sig); 57 void UninstallLocalDumper(int sig); 58 59 bool SendLocalDumpRequest(int32_t tid); 60 bool WaitLocalDumpRequest(); 61 62 void ResolveFrameInfo(size_t index, DfxFrame& frame); 63 64 static void LocalDumpering(int sig, siginfo_t *si, void *context); 65 void LocalDumper(int sig, siginfo_t *si, void *context); 66 67 bool ExecLocalDumpUnwinding(unw_context_t *ctx, size_t skipFramNum); 68 69 private: 70 unw_addr_space_t as_; 71 unw_context_t context_; 72 std::vector<DfxFrame> frames_; 73 uint32_t curIndex_ = 0; 74 std::unique_ptr<DfxSymbolsCache> cache_; 75 sigset_t mask_; 76 struct sigaction oldSigaction_; 77 std::condition_variable localDumperCV_; 78 std::mutex localDumperMutex_; 79 std::atomic<bool> insideSignalHandler_; 80 std::atomic<int> initTimes_; 81 bool isInited_ = false; 82 }; 83 } // namespace HiviewDFX 84 } // namespace OHOS 85 #endif // DFX_UNWIND_LOCAL_H 86