• 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 #include "dump_catcher.h"
17 
18 #include "dfx_define.h"
19 #include "dfx_dump_catcher.h"
20 #include "dfx_log.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
GetInstance()24 DumpCatcher &DumpCatcher::GetInstance()
25 {
26     static DumpCatcher ins;
27     return ins;
28 }
29 
Dump(int32_t pid,int32_t tid,int timeout) const30 void DumpCatcher::Dump(int32_t pid, int32_t tid, int timeout) const
31 {
32     DfxDumpCatcher dfxDump;
33     std::string msg = "";
34     auto dumpResult = dfxDump.DumpCatchWithTimeout(pid, msg, timeout, tid);
35     std::string toFind = "Result:";
36     size_t startPos = msg.find(toFind);
37     if (startPos != std::string::npos) {
38         size_t endPos = msg.find("\n", startPos);
39         if (endPos != std::string::npos) {
40             msg.erase(startPos, endPos - startPos + 1);
41         }
42     }
43     if (dumpResult.first == -1) {
44         printf("Result:dump failed.\n");
45     } else if (dumpResult.first == 0) {
46         printf("Result:dump normal stack success.\n");
47     } else if (dumpResult.first == 1) {
48         printf("Result:dump kernel stack success.\n");
49     }
50     printf("%s", dumpResult.second.c_str());
51     printf("%s\n", msg.c_str());
52 }
53 } // namespace HiviewDFX
54 } // namespace OHOS
55