1 /* 2 * Copyright (c) 2022-2024 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 25 namespace OHOS { 26 namespace HiviewDFX { 27 /** 28 * @brief Get a thread of backtrace string by specify tid 29 * 30 * @param out backtrace string(output parameter) 31 * @param tid the id of thread. 32 * notice: the value can not equal to tid of the caller thread, 33 * if you want to get stack of the caller thread, 34 * please use interface of "GetBacktrace" or "PrintBacktrace". 35 * @param skipFrameNum the number of frames to skip 36 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 37 * @param maxFrameNums the maximum number of frames to backtrace 38 * @param enableKernelStack if set to true, when failed to get user stack, try to get kernel stack. 39 * @return if succeed return true, otherwise return false 40 * @warning If enableKernelStack set to true, this interface requires that the caller process 41 * has ioctl system call permission, otherwise it may cause the calling process to crash. 42 */ 43 bool GetBacktraceStringByTid(std::string& out, int32_t tid, size_t skipFrameNum, bool fast, 44 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM, bool enableKernelStack = true); 45 46 /** 47 * @brief Get a thread of backtrace string by specify tid enable mix 48 * 49 * @param out backtrace string(output parameter) 50 * @param tid the id of thread 51 * @param skipFrameNum the number of frames to skip 52 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 53 * @param maxFrameNums the maximum number of frames to backtrace 54 * @param enableKernelStack if set to true, when failed to get user stack, try to get kernel stack. 55 * @return if succeed return true, otherwise return false 56 * @warning If enableKernelStack set to true, this interface requires that the caller process 57 * has ioctl system call permission, otherwise it may cause the calling process to crash. 58 */ 59 bool GetBacktraceStringByTidWithMix(std::string& out, int32_t tid, size_t skipFrameNum, bool fast, 60 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM, bool enableKernelStack = true); 61 62 /** 63 * @brief Print backtrace information to fd 64 * 65 * @param fd file descriptor 66 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 67 * @param maxFrameNums the maximum number of frames to backtrace 68 * @return if succeed return true, otherwise return false 69 */ 70 bool PrintBacktrace(int32_t fd = -1, bool fast = false, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 71 72 /** 73 * @brief Get backtrace string of the current thread 74 * 75 * @param out backtrace string(output parameter) 76 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 77 * @param maxFrameNums the maximum number of frames to backtrace 78 * @return if succeed return true, otherwise return false. 79 */ 80 bool GetBacktrace(std::string& out, bool fast = false, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 81 82 /** 83 * @brief Get backtrace string of the current thread 84 * 85 * @param out backtrace string(output parameter) 86 * @param skipFrameNum the number of frames to skip 87 * @param fast flag for using fp backtrace(true) or dwarf backtrace(false) 88 * @param maxFrameNums the maximum number of frames to backtrace 89 * @return if succeed return true, otherwise return false 90 */ 91 bool GetBacktrace(std::string& out, size_t skipFrameNum, bool fast = false, 92 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 93 94 /** 95 * @brief Get formatted stacktrace string of current process 96 * 97 * This API is used to get formatted stacktrace string of current process 98 * 99 * @param maxFrameNums the maximum number of frames to backtrace 100 * @param enableKernelStack if set to true, when failed to get user stack, try to get kernel stack. 101 * @return formatted stacktrace string 102 * @warning If enableKernelStack set to true, this interface requires that the caller process 103 * has ioctl system call permission, otherwise it may cause the calling process to crash. 104 */ 105 std::string GetProcessStacktrace(size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM, bool enableKernelStack = true); 106 107 extern "C" { 108 /** 109 * @brief Print backtrace information to fd 110 * 111 * @param fd file descriptor 112 * @param maxFrameNums the maximum number of frames to backtrace 113 * @return if succeed return true, otherwise return false 114 */ 115 bool PrintTrace(int32_t fd = -1, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 116 117 /** 118 * @brief Get backtrace of the current process 119 * 120 * @param skipFrameNum the number of frames to skip 121 * @param maxFrameNums the maximum number of frames to backtrace 122 * @return backtrace of the current process 123 */ 124 const char* GetTrace(size_t skipFrameNum = 0, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 125 } 126 } // namespace HiviewDFX 127 } // namespace OHOS 128 #endif 129