• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
47 using Priority = enum {
48     PV0 = 0,
49     PV1,
50     PV2,
51     PV3,
52     PV4,
53     PV5,
54     PV6
55 };
56 
ParseHeader(std::string & str,uint16_t * level,uint64_t * timestamp)57 static void ParseHeader(std::string& str, uint16_t* level, uint64_t* timestamp)
58 {
59     static const std::string pattern = "(\\d+),(\\d+),(\\d+),(\\S);";
60     static const std::regex express(pattern);
61     std::match_results<std::string::iterator> res;
62     if (std::regex_search(str.begin(), str.end(), res, express)) {
63         *level = strtoul(res[1].str().c_str(), nullptr, DEC);
64         *timestamp = strtoumax(res[3].str().c_str(), nullptr, DEC);
65         str.erase(res.position(), res.length());
66     }
67 }
68 
69 // Parse pid if exists
ParsePid(std::string & str)70 static uint32_t ParsePid(std::string& str)
71 {
72     static const std::string pattern = "\\[pid=(\\d+)\\]";
73     static const std::regex express(pattern);
74     std::match_results<std::string::iterator> res;
75     if (std::regex_search(str.begin(), str.end(), res, express)) {
76         uint32_t ret = strtoumax(res[1].str().c_str(), nullptr, DEC);
77         str.erase(res.position(), res.length());
78         return ret;
79     }
80     return 0;
81 }
82 
ParseTag(std::string & str)83 static std::string ParseTag(std::string& str)
84 {
85     static const std::string pattern = "\\[.*?\\]";
86     static const std::regex express(pattern);
87     std::match_results<std::string::iterator> res;
88     if (std::regex_search(str.begin(), str.end(), res, express)) {
89         std::string ret = res[0].str();
90         str.erase(res.position(), res.length());
91         return ret;
92     }
93     return {};
94 }
95 
96 // Log levels are different in syslog.h and hilog log_c.h
KmsgLevelMap(uint16_t prio)97 static uint16_t KmsgLevelMap(uint16_t prio)
98 {
99     uint16_t level;
100     switch (prio) {
101         case Priority::PV0:
102         case Priority::PV1:
103         case Priority::PV2:
104             level = LOG_FATAL;
105             break;
106         case Priority::PV3:
107             level = LOG_ERROR;
108             break;
109         case Priority::PV4:
110         case Priority::PV5:
111             level = LOG_WARN;
112             break;
113         case Priority::PV6:
114             level = LOG_INFO;
115             break;
116         default:
117             level = LOG_DEBUG;
118             break;
119     }
120     return level;
121 }
122 
TimepointToTimespec(time_point<system_clock,nanoseconds> tp)123 static constexpr timespec TimepointToTimespec(time_point<system_clock, nanoseconds> tp)
124 {
125     auto secs = time_point_cast<seconds>(tp);
126     auto nsecs = time_point_cast<nanoseconds>(tp) - time_point_cast<nanoseconds>(secs);
127     return timespec{secs.time_since_epoch().count(), nsecs.count()};
128 }
129 
130 // Kmsg has microseconds from system boot. Now get the time of system boot.
BootTime()131 KmsgParser::BootTp KmsgParser::BootTime()
132 {
133     struct timespec t_uptime;
134     clock_gettime(CLOCK_BOOTTIME, &t_uptime);
135     auto uptime = seconds{t_uptime.tv_sec} + nanoseconds{t_uptime.tv_nsec};
136     auto current = system_clock::now();
137     auto boottime = current - uptime;
138     return boottime;
139 }
ParseKmsg(const std::vector<char> & kmsgBuffer)140 std::optional<HilogMsgWrapper> KmsgParser::ParseKmsg(const std::vector<char>& kmsgBuffer)
141 {
142     std::string kmsgStr(kmsgBuffer.data());
143     std::vector<char> mtag(MAX_TAG_LEN, '\0');
144     uint16_t mLevel = 0;
145     uint64_t timestamp = 0;
146     ParseHeader(kmsgStr, &mLevel, &timestamp);
147     // Parses pid if exists. Pid in kmsg content is like: [pid=xxx,...]
148     uint32_t mpid = ParsePid(kmsgStr);
149     // If there are some other content wrapped in square brackets "[]", parse it as tag
150     // Otherwise, use default tag  "kmsg"
151     size_t tagLen = 0;
152     std::string tagStr = ParseTag(kmsgStr);
153     if (!tagStr.empty()) {
154         tagLen = tagStr.size();
155         if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, tagStr.c_str(), tagStr.size()) != 0) {
156             return {};
157         }
158     } else {
159         constexpr auto defaultTag = "kmsg"sv;
160         tagLen = defaultTag.size();
161         if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, defaultTag.data(), defaultTag.size()) != 0) {
162             return {};
163         }
164     }
165     // Now build HilogMsg and insert it into buffer
166     auto len = kmsgStr.size() + 1;
167     auto msgLen = sizeof(HilogMsg) + tagLen + len + 1;
168     HilogMsgWrapper msgWrap((std::vector<char>(msgLen, '\0')));
169     HilogMsg& msg = msgWrap.GetHilogMsg();
170     msg.len = msgLen;
171     msg.tag_len = tagLen + 1;
172     msg.type = LOG_KMSG;
173     msg.domain = 0xD002600;
174     msg.level = KmsgLevelMap(mLevel);
175     time_point<system_clock, nanoseconds> logtime = BootTime() + microseconds{timestamp};
176     struct timespec logts = TimepointToTimespec(logtime);
177     msg.tv_sec = static_cast<uint32_t>(logts.tv_sec);
178     msg.tv_nsec = static_cast<uint32_t>(logts.tv_nsec);
179     msg.pid = mpid;
180     msg.tid = mpid;
181     if (strncpy_s(msg.tag, tagLen + 1, mtag.data(), tagLen) != 0) {
182         return {};
183     }
184     if (strncpy_s(CONTENT_PTR((&msg)), MAX_LOG_LEN, kmsgStr.c_str(), len) != 0) {
185         return {};
186     }
187     return msgWrap;
188 }
189 } // namespace HiviewDFX
190 } // namespace OHOS
191