• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 // some module head files not self-included.
25 #include <csignal>
26 #include <unistd.h>
27 #include <sys/syscall.h>
28 namespace OHOS {
29 namespace HiviewDFX {
30 class DfxDumpCatcher {
31 public:
32     static constexpr size_t DEFAULT_MAX_FRAME_NUM = 256;
33 
34     DfxDumpCatcher();
35     DfxDumpCatcher(const DfxDumpCatcher&) = delete;
36     DfxDumpCatcher& operator=(const DfxDumpCatcher&) = delete;
37 
38     /**
39      * @brief Dump native stack by specify pid and tid
40      *
41      * @param pid  process id
42      * @param tid  thread id
43      * @param msg  message of native stack
44      * @param maxFrameNums the maximum number of frames to dump, if pid is not equal to caller pid then it is ignored
45      * @param isJson whether message of native stack is json formatted
46      * @return if succeed return true, otherwise return false
47     */
48     bool DumpCatch(int pid, int tid, std::string& msg, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM,
49                    bool isJson = false);
50 
51     /**
52      * @brief Dump native stack by specify pid and tid to file
53      *
54      * @param pid  process id
55      * @param tid  thread id
56      * @param fd  file descriptor
57      * @param maxFrameNums the maximum number of frames to dump,
58      *  if pid is not equal to caller pid then it does not support setting
59      * @return if succeed return true, otherwise return false
60     */
61     bool DumpCatchFd(int pid, int tid, std::string& msg, int fd, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM);
62 
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> &pids, std::string& msg);
72     /**
73      * @brief Dump stack of process with timeout
74      *
75      * @param pid  process id
76      * @param msg  message of stack
77      * @param timeout  Set the dump timeout time to be at least 1000ms
78      * @param isJson  whether message of stack is json formatted
79      * @return ret and reason.
80      *  ret: -1: dump catch failed 0:msg is normal stack 1:msg is kernel stack(not json format)
81      *  reason: if ret is 1, it contains normal stack fail reason.
82      *          if ret is -1, it contains normal stack fail reason and kernel stack fail reason.
83     */
84     std::pair<int, std::string> DumpCatchWithTimeout(int pid, std::string& msg, int timeout = 3000,
85         int tid = 0, bool isJson = false);
86 private:
87     class Impl;
88     std::shared_ptr<Impl> impl_;
89 };
90 } // namespace HiviewDFX
91 } // namespace OHOS
92 
93 #endif
94