1 /*
2 * Copyright (C) 2021 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 #include "log_catcher_utils.h"
16
17 #include <map>
18 #include <memory>
19 #include <sstream>
20 #include <string>
21
22 #include "dfx_dump_catcher.h"
23
24 #include "common_utils.h"
25 #include "file_util.h"
26 #include "logger.h"
27 #include "string_util.h"
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace LogCatcherUtils {
31 static std::map<int, std::shared_ptr<std::pair<bool, std::string>>> dumpMap;
32 static std::mutex dumpMutex;
33 static std::condition_variable getSync;
34
GetDump(int pid,std::string & msg)35 bool GetDump(int pid, std::string& msg)
36 {
37 std::unique_lock lock(dumpMutex);
38 auto it = dumpMap.find(pid);
39 if (it == dumpMap.end()) {
40 dumpMap[pid] = std::make_shared<std::pair<bool, std::string>>(
41 std::pair<bool, std::string>(false, ""));
42 return false;
43 }
44 std::shared_ptr<std::pair<bool, std::string>> tmp = it->second;
45 if (!tmp->first) {
46 getSync.wait_for(lock, std::chrono::seconds(10), // 10: dump stack timeout
47 [pid]() -> bool {
48 return (dumpMap.find(pid) == dumpMap.end());
49 });
50 if (!tmp->first) {
51 return false;
52 }
53 }
54 msg = tmp->second;
55 return true;
56 }
57
FinshDump(int pid,const std::string & msg)58 void FinshDump(int pid, const std::string& msg)
59 {
60 std::lock_guard lock(dumpMutex);
61 auto it = dumpMap.find(pid);
62 if (it == dumpMap.end()) {
63 return;
64 }
65 std::shared_ptr<std::pair<bool, std::string>> tmp = it->second;
66 tmp->first = true;
67 tmp->second = msg;
68 dumpMap.erase(pid);
69 getSync.notify_all();
70 }
71
DumpStacktrace(int fd,int pid)72 int DumpStacktrace(int fd, int pid)
73 {
74 if (fd < 0) {
75 return -1;
76 }
77 std::string msg = "";
78 if (!GetDump(pid, msg)) {
79 DfxDumpCatcher dumplog;
80 std::string ret;
81 if (!dumplog.DumpCatch(pid, 0, ret)) {
82 msg = "Failed to dump stacktrace for " + std::to_string(pid) + "\n" + ret;
83 } else {
84 msg = ret;
85 }
86 FinshDump(pid, "\n-repeat-\n" + msg);
87 }
88
89 if (msg == "") {
90 msg = "dumpCatch return empty stack!!!!";
91 }
92 FileUtil::SaveStringToFd(fd, msg);
93
94 return 0;
95 }
96 }
97 } // namespace HiviewDFX
98 } // namespace OHOS
99