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
16 #include "kmsg_parser.h"
17 #include "hilog/log.h"
18
19 #include <cstdlib>
20 #include <cinttypes>
21 #include <iostream>
22 #include <string>
23 #include <thread>
24 #include <chrono>
25 #include <fstream>
26 #include <regex>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <string_view>
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 using namespace std::chrono;
36 using namespace std::literals;
37
38 constexpr int DEC = 10;
39
40 // Avoid name collision between sys/syslog.h and our log_c.h
41 #undef LOG_FATAL
42 #undef LOG_ERR
43 #undef LOG_WARN
44 #undef LOG_INFO
45 #undef LOG_DEBUG
46
ParseHeader(std::string & str,uint16_t * level,uint64_t * timestamp)47 static void ParseHeader(std::string& str, uint16_t* level, uint64_t* timestamp)
48 {
49 static const std::string pattern = "(\\d+),(\\d+),(\\d+),(\\S);";
50 static const std::regex express(pattern);
51 std::match_results<std::string::iterator> res;
52 if (std::regex_search(str.begin(), str.end(), res, express)) {
53 *level = strtoul(res[1].str().c_str(), nullptr, DEC);
54 *timestamp = strtoumax(res[3].str().c_str(), nullptr, DEC);
55 str.erase(res.position(), res.length());
56 }
57 }
58
59 // Parse pid if exists
ParsePid(std::string & str)60 static uint32_t ParsePid(std::string& str)
61 {
62 static const std::string pattern = "\\[pid=(\\d+)\\]";
63 static const std::regex express(pattern);
64 std::match_results<std::string::iterator> res;
65 if (std::regex_search(str.begin(), str.end(), res, express)) {
66 uint32_t ret = strtoumax(res[1].str().c_str(), nullptr, DEC);
67 str.erase(res.position(), res.length());
68 return ret;
69 }
70 return 0;
71 }
72
ParseTag(std::string & str)73 static std::string ParseTag(std::string& str)
74 {
75 static const std::string pattern = "\\[.*?\\]";
76 static const std::regex express(pattern);
77 std::match_results<std::string::iterator> res;
78 if (std::regex_search(str.begin(), str.end(), res, express)) {
79 std::string ret = res[0].str();
80 str.erase(res.position(), res.length());
81 return ret;
82 }
83 return {};
84 }
85
86 // Log levels are different in syslog.h and hilog log_c.h
KmsgLevelMap(uint16_t prio)87 static uint16_t KmsgLevelMap(uint16_t prio)
88 {
89 uint16_t level;
90 switch (prio) {
91 case 0:
92 case 1:
93 case 2:
94 level = LOG_FATAL;
95 break;
96 case 3:
97 level = LOG_ERROR;
98 break;
99 case 4:
100 case 5:
101 level = LOG_WARN;
102 break;
103 case 6:
104 level = LOG_INFO;
105 break;
106 default:
107 level = LOG_DEBUG;
108 break;
109 }
110 return level;
111 }
112
TimepointToTimespec(time_point<system_clock,nanoseconds> tp)113 static constexpr timespec TimepointToTimespec(time_point<system_clock, nanoseconds> tp)
114 {
115 auto secs = time_point_cast<seconds>(tp);
116 auto nsecs = time_point_cast<nanoseconds>(tp) - time_point_cast<nanoseconds>(secs);
117 return timespec{secs.time_since_epoch().count(), nsecs.count()};
118 }
119
120 // Kmsg has microseconds from system boot. Now get the time of system boot.
BootTime()121 KmsgParser::BootTp KmsgParser::BootTime()
122 {
123 struct timespec t_uptime;
124 clock_gettime(CLOCK_BOOTTIME, &t_uptime);
125 auto uptime = seconds{t_uptime.tv_sec} + nanoseconds{t_uptime.tv_nsec};
126 auto current = system_clock::now();
127 auto boottime = current - uptime;
128 return boottime;
129 }
ParseKmsg(const std::vector<char> & kmsgBuffer)130 std::optional<HilogMsgWrapper> KmsgParser::ParseKmsg(const std::vector<char>& kmsgBuffer)
131 {
132 std::string kmsgStr(kmsgBuffer.data());
133 std::vector<char> mtag(MAX_TAG_LEN, '\0');
134 uint16_t mLevel = 0;
135 uint64_t timestamp = 0;
136 ParseHeader(kmsgStr, &mLevel, ×tamp);
137 // Parses pid if exists. Pid in kmsg content is like: [pid=xxx,...]
138 uint32_t mpid = ParsePid(kmsgStr);
139 // If there are some other content wrapped in square brackets "[]", parse it as tag
140 // Otherwise, use default tag "kmsg"
141 size_t tagLen = 0;
142 std::string tagStr = ParseTag(kmsgStr);
143 if (!tagStr.empty()) {
144 tagLen = tagStr.size();
145 if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, tagStr.c_str(), tagStr.size()) != 0) {
146 return {};
147 }
148 } else {
149 constexpr auto defaultTag = "kmsg"sv;
150 tagLen = defaultTag.size();
151 if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, defaultTag.data(), defaultTag.size()) != 0) {
152 return {};
153 }
154 }
155 // Now build HilogMsg and insert it into buffer
156 auto len = kmsgStr.size() + 1;
157 auto msgLen = sizeof(HilogMsg) + tagLen + len + 1;
158 HilogMsgWrapper msgWrap((std::vector<char>(msgLen, '\0')));
159 HilogMsg& msg = msgWrap.GetHilogMsg();
160 msg.len = msgLen;
161 msg.tag_len = tagLen + 1;
162 msg.type = LOG_KMSG;
163 msg.domain = 0xdfffffff;
164 msg.level = KmsgLevelMap(mLevel);
165 time_point<system_clock, nanoseconds> logtime = BootTime() + microseconds{timestamp};
166 struct timespec logts = TimepointToTimespec(logtime);
167 msg.tv_sec = static_cast<uint32_t>(logts.tv_sec);
168 msg.tv_nsec = static_cast<uint32_t>(logts.tv_nsec);
169 msg.pid = mpid;
170 msg.tid = mpid;
171 if (strncpy_s(msg.tag, tagLen + 1, mtag.data(), tagLen) != 0) {
172 return {};
173 }
174 if (strncpy_s(CONTENT_PTR((&msg)), MAX_LOG_LEN, kmsgStr.c_str(), len) != 0) {
175 return {};
176 }
177 return msgWrap;
178 }
179 } // namespace HiviewDFX
180 } // namespace OHOS
181
182