1 /* 2 * Copyright (c) 2022-2023 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 16 #ifndef DFX_CATCH_FRAME_H 17 #define DFX_CATCH_FRAME_H 18 19 #include <cinttypes> 20 #include <cstring> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 #include <unistd.h> 26 #include <vector> 27 #include "procinfo.h" 28 #include "dfx_frame.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 33 class DfxCatchFrameLocal { 34 public: 35 DfxCatchFrameLocal(); 36 explicit DfxCatchFrameLocal(int32_t pid); 37 ~DfxCatchFrameLocal(); 38 39 /** 40 * @brief initialize catch frame 41 * 42 * @return if succeed return true, otherwise return false 43 */ 44 bool InitFrameCatcher(); 45 46 /** 47 * @brief release thread by thread id 48 * 49 * @param tid thread id 50 * @return if succeed return true, otherwise return false 51 */ 52 bool ReleaseThread(int tid); 53 54 /** 55 * @brief catch thread backtrace frame by thread id 56 * 57 * @param frames backtrace frames(output parameter) 58 * @param releaseThread flag for whether to release thread after completing frames catching 59 * @return if succeed return true, otherwise return false 60 */ 61 bool CatchFrame(int tid, std::vector<DfxFrame>& frames, bool releaseThread = false); 62 63 /** 64 * @brief catch thread backtrace frame by thread id 65 * 66 * @param mapFrames namespace tid and backtrace frames(output parameter) 67 * @param releaseThread flag for whether to release thread after completing frames catching 68 * @return if succeed return true, otherwise return false 69 */ 70 bool CatchFrame(std::map<int, std::vector<DfxFrame>>& mapFrames, bool releaseThread = false); 71 72 /** 73 * @brief release the resource of frame catcher 74 * 75 */ 76 void DestroyFrameCatcher(); 77 78 private: 79 bool CatchFrameCurrTid(std::vector<DfxFrame>& frames, bool releaseThread = false); 80 bool CatchFrameLocalTid(int tid, std::vector<DfxFrame>& frames, bool releaseThread = false); 81 82 private: 83 std::mutex mutex_; 84 int32_t pid_; 85 struct ProcInfo procInfo_; 86 }; 87 } // namespace HiviewDFX 88 } // namespace OHOS 89 #endif 90