• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 skipFrames skip frames number
59      * @param releaseThread flag for whether to release thread after completing frames catching
60      * @return if succeed return true, otherwise return false
61     */
62     bool CatchFrame(int tid, std::vector<DfxFrame>& frames, int skipFrames = 0, bool releaseThread = false);
63 
64     /**
65      * @brief catch thread backtrace frame by thread id
66      *
67      * @param mapFrames  namespace tid and backtrace frames(output parameter)
68      * @param releaseThread flag for whether to release thread after completing frames catching
69      * @return if succeed return true, otherwise return false
70     */
71     bool CatchFrame(std::map<int, std::vector<DfxFrame>>& mapFrames, bool releaseThread = false);
72 
73     /**
74      * @brief release the resource of frame catcher
75      *
76     */
77     void DestroyFrameCatcher();
78 
79 private:
80     bool CatchFrameCurrTid(std::vector<DfxFrame>& frames, int skipFrames = 0, bool releaseThread = false);
81     bool CatchFrameLocalTid(int tid, std::vector<DfxFrame>& frames, int skipFrames = 0, bool releaseThread = false);
82 
83 private:
84     std::mutex mutex_;
85     int32_t pid_;
86     struct ProcInfo procInfo_;
87 };
88 } // namespace HiviewDFX
89 } // namespace OHOS
90 #endif
91