1 /* 2 * Copyright (c) 2021-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_DUMPCATCH_H 17 #define DFX_DUMPCATCH_H 18 19 #include <cinttypes> 20 #include <cstring> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <unistd.h> 25 #include <vector> 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 class DfxDumpCatcher { 30 public: DfxDumpCatcher()31 DfxDumpCatcher() {} ~DfxDumpCatcher()32 ~DfxDumpCatcher() {} 33 34 /** 35 * @brief Dump native stack by specify pid and tid 36 * 37 * @param pid process id 38 * @param tid thread id 39 * @param msg message of native stack 40 * @return if succeed return true, otherwise return false 41 */ 42 bool DumpCatch(int pid, int tid, std::string& msg); 43 44 /** 45 * @brief Dump native and js mixed-stack by specify pid and tid 46 * 47 * @param pid process id 48 * @param tid thread id 49 * @param msg message of native and js mixed-stack 50 * @return if succeed return true, otherwise return false 51 */ 52 bool DumpCatchMix(int pid, int tid, std::string& msg); 53 54 /** 55 * @brief Dump native stack by specify pid and tid to file 56 * 57 * @param pid process id 58 * @param tid thread id 59 * @param fd file descriptor 60 * @return if succeed return true, otherwise return false 61 */ 62 bool DumpCatchFd(int pid, int tid, std::string& msg, int fd); 63 /** 64 * @brief Dump native stack by multi-pid 65 * 66 * @param pid process id 67 * @param tid thread id 68 * @param msg message of native stack 69 * @return if succeed return true, otherwise return false 70 */ 71 bool DumpCatchMultiPid(const std::vector<int> pidV, std::string& msg); 72 73 private: 74 bool DoDumpCurrTid(const size_t skipFrameNum, std::string& msg); 75 bool DoDumpLocalTid(const int tid, std::string& msg); 76 bool DoDumpLocalPid(int pid, std::string& msg); 77 bool DoDumpLocalLocked(int pid, int tid, std::string& msg); 78 bool DoDumpRemoteLocked(int pid, int tid, std::string& msg); 79 bool DoDumpCatchRemote(const int type, int pid, int tid, std::string& msg); 80 int DoDumpRemotePid(const int type, int pid, std::string& msg); 81 int DoDumpRemotePoll(int bufFd, int resFd, int timeout, std::string& msg); 82 bool DoReadBuf(int fd, std::string& msg); 83 bool DoReadRes(int fd, bool &ret, std::string& msg); 84 85 private: 86 std::mutex mutex_; 87 }; 88 } // namespace HiviewDFX 89 } // namespace OHOS 90 91 #endif 92