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_BACKTRACE_LOCAL_H 17 #define DFX_BACKTRACE_LOCAL_H 18 19 #include <cinttypes> 20 #include <string> 21 #include <vector> 22 23 #include "dfx_define.h" 24 #include "dfx_frame.h" 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 29 /** 30 * @brief Get a thread of backtrace frames by specify tid 31 * 32 * @param frames backtrace frames(output parameter) 33 * @param tid the id of thread 34 * @param skipFrameNum the number of frames to skip 35 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 36 * @param maxFrameNums the maximum number of frames to backtrace 37 * @return if succeed return true, otherwise return false 38 */ 39 bool GetBacktraceFramesByTid(std::vector<DfxFrame>& frames, int32_t tid, size_t skipFrameNum, bool fast, 40 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 41 42 /** 43 * @brief Get a thread of backtrace string by specify tid 44 * 45 * @param out backtrace string(output parameter) 46 * @param tid the id of thread 47 * @param skipFrameNum the number of frames to skip 48 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 49 * @param maxFrameNums the maximum number of frames to backtrace 50 * @return if succeed return true, otherwise return false 51 */ 52 bool GetBacktraceStringByTid(std::string& out, int32_t tid, size_t skipFrameNum, bool fast, 53 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 54 55 #ifndef is_ohos_lite 56 /** 57 * @brief Get a thread of backtrace json by specify tid 58 * 59 * @param out backtrace string(output parameter) 60 * @param tid the id of thread 61 * @param skipFrameNum the number of frames to skip 62 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 63 * @param maxFrameNums the maximum number of frames to backtrace 64 * @return if succeed return true, otherwise return false 65 */ 66 bool GetBacktraceJsonByTid(std::string& out, int32_t tid, size_t skipFrameNum, bool fast, 67 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 68 #endif 69 70 /** 71 * @brief Print backtrace information to fd 72 * 73 * @param fd file descriptor 74 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 75 * @param maxFrameNums the maximum number of frames to backtrace 76 * @return if succeed return true, otherwise return false 77 */ 78 bool PrintBacktrace(int32_t fd = -1, bool fast = false, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 79 80 /** 81 * @brief Get backtrace string of the current process 82 * 83 * @param out backtrace string(output parameter) 84 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 85 * @param maxFrameNums the maximum number of frames to backtrace 86 * @return if succeed return true, otherwise return false 87 */ 88 bool GetBacktrace(std::string& out, bool fast = false, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 89 90 /** 91 * @brief Get backtrace string of the current process 92 * 93 * @param out backtrace string(output parameter) 94 * @param skipFrameNum the number of frames to skip 95 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 96 * @param maxFrameNums the maximum number of frames to backtrace 97 * @return if succeed return true, otherwise return false 98 */ 99 bool GetBacktrace(std::string& out, size_t skipFrameNum, bool fast = false, 100 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM, bool isJson = false); 101 102 /** 103 * @brief Get formatted stacktrace string of current process 104 * 105 * This API is used to get formatted stacktrace string of current process 106 * 107 * @param maxFrameNums the maximum number of frames to backtrace 108 * @param isJson whether message of native stack is json formatted 109 * @return formatted stacktrace string 110 */ 111 std::string GetProcessStacktrace(size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM, bool isJson = false); 112 113 extern "C" { 114 /** 115 * @brief Print backtrace information to fd 116 * 117 * @param fd file descriptor 118 * @param maxFrameNums the maximum number of frames to backtrace 119 * @return if succeed return true, otherwise return false 120 */ 121 bool PrintTrace(int32_t fd = -1, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 122 123 /** 124 * @brief Get backtrace of the current process 125 * 126 * @param skipFrameNum the number of frames to skip 127 * @param maxFrameNums the maximum number of frames to backtrace 128 * @return backtrace of the current process 129 */ 130 const char* GetTrace(size_t skipFrameNum = 0, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 131 } 132 133 } // namespace HiviewDFX 134 } // namespace OHOS 135 #endif 136