1 /* 2 * Copyright (c) 2022 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 UNWIND_LOCAL_THREAD_H 16 #define UNWIND_LOCAL_THREAD_H 17 18 #include <cinttypes> 19 #include <memory> 20 #include <vector> 21 22 #include <libunwind.h> 23 24 #include "dfx_frame.h" 25 #include "dfx_symbols.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 constexpr int32_t BACKTRACE_CURRENT_THREAD = -1; 30 31 class BacktraceLocalThread { 32 public: 33 explicit BacktraceLocalThread(int32_t tid); 34 ~BacktraceLocalThread(); 35 36 bool Unwind(unw_addr_space_t as, std::shared_ptr<DfxSymbols> symbol, size_t skipFrameNum, 37 bool fast = false, bool releaseThread = true); 38 void ReleaseThread(); 39 40 const std::vector<DfxFrame>& GetFrames() const; 41 std::string GetFormattedStr(bool withThreadName = false, bool isJson = false); 42 void SetMaxFrameNums(size_t maxFrameNums); 43 private: 44 bool UnwindCurrentThread(unw_addr_space_t as, std::shared_ptr<DfxSymbols> symbol, 45 size_t skipFrameNum, bool fast = false); 46 47 private: 48 int32_t tid_; 49 size_t maxFrameNums_; 50 std::vector<DfxFrame> frames_; 51 }; 52 } // namespace HiviewDFX 53 } // namespace OHOS 54 #endif // UNWIND_LOCAL_THREAD_H 55