1 /*
2 * Copyright (c) 2022 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 <cstdint>
16 #include <cstdlib>
17 #include <chrono>
18 #include <functional>
19 #include <regex>
20 #include <sstream>
21 #include <thread>
22 #include <fstream>
23 #include <fcntl.h>
24 #include <securec.h>
25 #include <hilog/log.h>
26 #include <unistd.h>
27 #include "hilog_common.h"
28 #include "hilog_cmd.h"
29 #include "log_utils.h"
30
31 namespace {
32 constexpr uint32_t ONE_KB = (1UL << 10);
33 constexpr uint32_t ONE_MB = (1UL << 20);
34 constexpr uint32_t ONE_GB = (1UL << 30);
35 constexpr uint64_t ONE_TB = (1ULL << 40);
36 constexpr uint32_t DOMAIN_MIN = DOMAIN_APP_MIN;
37 constexpr uint32_t DOMAIN_MAX = DOMAIN_OS_MAX;
38 constexpr int CMDLINE_PATH_LEN = 32;
39 constexpr int CMDLINE_LEN = 128;
40 constexpr int STATUS_PATH_LEN = 32;
41 constexpr int STATUS_LEN = 1024;
42 const std::string SH_NAMES[] = { "sh", "/bin/sh", "/system/bin/sh", "/xbin/sh", "/system/xbin/sh"};
43 }
44
45 namespace OHOS {
46 namespace HiviewDFX {
47 using namespace std;
48 using namespace std::chrono;
49
50 // Buffer Size&Char Map
51 static const KVMap<char, uint64_t> g_SizeMap({
52 {'B', 1}, {'K', ONE_KB}, {'M', ONE_MB},
53 {'G', ONE_GB}, {'T', ONE_TB}
54 }, ' ', 0);
55
Size2Str(uint64_t size)56 string Size2Str(uint64_t size)
57 {
58 string str;
59 uint64_t unit = 1;
60 switch (size) {
61 case 0 ... ONE_KB - 1: unit = 1; break;
62 case ONE_KB ... ONE_MB - 1: unit = ONE_KB; break;
63 case ONE_MB ... ONE_GB - 1: unit = ONE_MB; break;
64 case ONE_GB ... ONE_TB - 1: unit = ONE_GB; break;
65 default: unit = ONE_TB; break;
66 }
67 float i = (static_cast<float>(size)) / unit;
68 constexpr int len = 16;
69 char buf[len] = { 0 };
70 int ret = snprintf_s(buf, len, len - 1, "%.1f", i);
71 if (ret <= 0) {
72 str = to_string(size);
73 } else {
74 str = buf;
75 }
76 return str + g_SizeMap.GetKey(unit);
77 }
78
Str2Size(const string & str)79 uint64_t Str2Size(const string& str)
80 {
81 std::regex reg("[0-9]+[BKMGT]?");
82 if (!std::regex_match(str, reg)) {
83 return 0;
84 }
85 uint64_t index = str.size() - 1;
86 uint64_t unit = g_SizeMap.GetValue(str[index]);
87
88 uint64_t value = stoull(str.substr(0, unit !=0 ? index : index + 1));
89 return value * (unit != 0 ? unit : 1);
90 }
91
92 // Error Codes&Strings Map
93 static const KVMap<int16_t, string> g_ErrorMsgs({
94 {RET_SUCCESS, "Success"},
95 {RET_FAIL, "Unknown failure reason"},
96 {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"
97 " or DEBUG/INFO/WARN/ERROR/FATAL"},
98 {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init/kmsg"},
99 {ERR_INVALID_RQST_CMD, "Invalid request cmd, please check sourcecode"},
100 {ERR_QUERY_TYPE_INVALID, "Can't query kmsg type logs combined with other types logs."},
101 {ERR_INVALID_DOMAIN_STR, "Invalid domain string"},
102 {ERR_LOG_PERSIST_FILE_SIZE_INVALID, "Invalid log persist file size, file size should be in range ["
103 + Size2Str(MIN_LOG_FILE_SIZE) + ", " + Size2Str(MAX_LOG_FILE_SIZE) + "]"},
104 {ERR_LOG_PERSIST_FILE_NAME_INVALID, "Invalid log persist file name, file name should not contain [\\/:*?\"<>|]"},
105 {ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP, "Invalid Log persist compression buffer"},
106 {ERR_LOG_PERSIST_FILE_PATH_INVALID, "Invalid persister file path or persister directory does not exist"},
107 {ERR_LOG_PERSIST_COMPRESS_INIT_FAIL, "Log persist compression initialization failed"},
108 {ERR_LOG_PERSIST_FILE_OPEN_FAIL, "Log persist open file failed"},
109 {ERR_LOG_PERSIST_JOBID_FAIL, "Log persist jobid not exist"},
110 {ERR_LOG_PERSIST_TASK_EXISTED, "Log persist task is existed"},
111 {ERR_DOMAIN_INVALID, ("Invalid domain, domain should be in range (" + Uint2HexStr(DOMAIN_MIN)
112 + ", " +Uint2HexStr(DOMAIN_MAX) +"]")},
113 {ERR_MSG_LEN_INVALID, "Invalid message length"},
114 {ERR_LOG_PERSIST_JOBID_INVALID, "Invalid jobid, jobid should be in range [" + to_string(JOB_ID_MIN)
115 + ", " + to_string(JOB_ID_MAX) + ")"},
116 {ERR_BUFF_SIZE_INVALID, ("Invalid buffer size, buffer size should be in range [" + Size2Str(MIN_BUFFER_SIZE)
117 + ", " + Size2Str(MAX_BUFFER_SIZE) + "]")},
118 {ERR_COMMAND_INVALID, "Mutlti commands can't be used in combination"},
119 {ERR_LOG_FILE_NUM_INVALID, "Invalid number of files"},
120 {ERR_NOT_NUMBER_STR, "Not a numeric string"},
121 {ERR_TOO_MANY_ARGUMENTS, "Too many arguments"},
122 {ERR_DUPLICATE_OPTION, "Too many duplicate options"},
123 {ERR_INVALID_ARGUMENT, "Invalid argument"},
124 {ERR_TOO_MANY_DOMAINS, "Max domain count is " + to_string(MAX_DOMAINS)},
125 {ERR_INVALID_SIZE_STR, "Invalid size string"},
126 {ERR_TOO_MANY_PIDS, "Max pid count is " + to_string(MAX_PIDS)},
127 {ERR_TOO_MANY_TAGS, "Max tag count is " + to_string(MAX_TAGS)},
128 {ERR_TAG_STR_TOO_LONG, ("Tag string too long, max length is " + to_string(MAX_TAG_LEN - 1))},
129 {ERR_REGEX_STR_TOO_LONG, ("Regular expression too long, max length is " + to_string(MAX_REGEX_STR_LEN - 1))},
130 {ERR_FILE_NAME_TOO_LONG, ("File name too long, max length is " + to_string(MAX_FILE_NAME_LEN))},
131 {ERR_SOCKET_CLIENT_INIT_FAIL, "Socket client init failed"},
132 {ERR_SOCKET_WRITE_MSG_HEADER_FAIL, "Socket rite message header failed"},
133 {ERR_SOCKET_WRITE_CMD_FAIL, "Socket write command failed"},
134 {ERR_SOCKET_RECEIVE_RSP, "Unable to receive message from socket"},
135 {ERR_PERSIST_TASK_EMPTY, "No running persist task, please check"},
136 {ERR_JOBID_NOT_EXSIST, "Persist task of this job id doesn't exist, please check"},
137 {ERR_TOO_MANY_JOBS, ("Too many jobs are running, max job count is:" + to_string(MAX_JOBS))},
138 {ERR_STATS_NOT_ENABLE, "Statistic feature is not enable, "
139 "please set param persist.sys.hilog.stats true to enable it, "
140 "further more, you can set persist.sys.hilog.stats.tag true to enable counting log by tags"},
141 {ERR_NO_RUNNING_TASK, "No running persistent task"},
142 {ERR_NO_PID_PERMISSION, "Permission denied, only shell and root can filter logs by pid"},
143 }, RET_FAIL, "Unknown error code");
144
ErrorCode2Str(int16_t errorCode)145 string ErrorCode2Str(int16_t errorCode)
146 {
147 return g_ErrorMsgs.GetValue((uint16_t)errorCode) + " [CODE: " + to_string(errorCode) + "]";
148 }
149
150 // Log Types&Strings Map
151 static const StringMap g_LogTypes({
152 {LOG_INIT, "init"}, {LOG_CORE, "core"}, {LOG_APP, "app"}, {LOG_KMSG, "kmsg"}
153 }, LOG_TYPE_MAX, "invalid");
154
LogType2Str(uint16_t logType)155 string LogType2Str(uint16_t logType)
156 {
157 return g_LogTypes.GetValue(logType);
158 }
159
Str2LogType(const string & str)160 uint16_t Str2LogType(const string& str)
161 {
162 return g_LogTypes.GetKey(str);
163 }
164
ComboLogType2Str(uint16_t shiftType)165 string ComboLogType2Str(uint16_t shiftType)
166 {
167 vector<uint16_t> types = g_LogTypes.GetAllKeys();
168 string str = "";
169 uint16_t typeAll = 0;
170
171 for (uint16_t t : types) {
172 typeAll |= (1 << t);
173 }
174 shiftType &= typeAll;
175 for (uint16_t t: types) {
176 if ((1 << t) & shiftType) {
177 shiftType &= (~(1 << t));
178 str += (LogType2Str(t) + (shiftType != 0 ? "," : ""));
179 }
180 if (shiftType == 0) {
181 break;
182 }
183 }
184 return str;
185 }
186
Str2ComboLogType(const string & str)187 uint16_t Str2ComboLogType(const string& str)
188 {
189 uint16_t logTypes = 0;
190 if (str == "") {
191 logTypes = (1 << LOG_CORE) | (1 << LOG_APP);
192 return logTypes;
193 }
194 vector<string> vec;
195 Split(str, vec);
196 for (auto& it : vec) {
197 if (it == "") {
198 continue;
199 }
200 uint16_t t = Str2LogType(it);
201 if (t == LOG_TYPE_MAX) {
202 return 0;
203 }
204 logTypes |= (1 << t);
205 }
206 return logTypes;
207 }
208
GetAllLogTypes()209 vector<uint16_t> GetAllLogTypes()
210 {
211 return g_LogTypes.GetAllKeys();
212 }
213
214 // Log Levels&Strings Map
215 static const StringMap g_LogLevels({
216 {LOG_DEBUG, "DEBUG"}, {LOG_INFO, "INFO"}, {LOG_WARN, "WARN"},
217 {LOG_ERROR, "ERROR"}, {LOG_FATAL, "FATAL"}, {LOG_LEVEL_MAX, "X"}
__anonaa6faf1d0202(const string& l1, const string& l2) 218 }, LOG_LEVEL_MIN, "INVALID", [](const string& l1, const string& l2) {
219 if (l1.length() == l2.length()) {
220 return std::equal(l1.begin(), l1.end(), l2.begin(), [](char a, char b) {
221 return std::tolower(a) == std::tolower(b);
222 });
223 } else {
224 return false;
225 }
226 });
227
LogLevel2Str(uint16_t logLevel)228 string LogLevel2Str(uint16_t logLevel)
229 {
230 return g_LogLevels.GetValue(logLevel);
231 }
232
Str2LogLevel(const string & str)233 uint16_t Str2LogLevel(const string& str)
234 {
235 return g_LogLevels.GetKey(str);
236 }
237
238 // Log Levels&Short Strings Map
239 static const StringMap g_ShortLogLevels({
240 {LOG_DEBUG, "D"}, {LOG_INFO, "I"}, {LOG_WARN, "W"},
241 {LOG_ERROR, "E"}, {LOG_FATAL, "F"}, {LOG_LEVEL_MAX, "X"}
__anonaa6faf1d0402(const string& l1, const string& l2) 242 }, LOG_LEVEL_MIN, "V", [](const string& l1, const string& l2) {
243 return (l1.length() == 1 && std::tolower(l1[0]) == std::tolower(l2[0]));
244 });
245
LogLevel2ShortStr(uint16_t logLevel)246 string LogLevel2ShortStr(uint16_t logLevel)
247 {
248 return g_ShortLogLevels.GetValue(logLevel);
249 }
250
ShortStr2LogLevel(const string & str)251 uint16_t ShortStr2LogLevel(const string& str)
252 {
253 return g_ShortLogLevels.GetKey(str);
254 }
255
PrettyStr2LogLevel(const string & str)256 uint16_t PrettyStr2LogLevel(const string& str)
257 {
258 uint16_t level = ShortStr2LogLevel(str);
259 if (level == static_cast<uint16_t>(LOG_LEVEL_MIN)) {
260 return Str2LogLevel(str);
261 }
262 return level;
263 }
264
ComboLogLevel2Str(uint16_t shiftLevel)265 string ComboLogLevel2Str(uint16_t shiftLevel)
266 {
267 vector<uint16_t> levels = g_ShortLogLevels.GetAllKeys();
268 string str = "";
269 uint16_t levelAll = 0;
270
271 for (uint16_t l : levels) {
272 levelAll |= (1 << l);
273 }
274 shiftLevel &= levelAll;
275 for (uint16_t l: levels) {
276 if ((1 << l) & shiftLevel) {
277 shiftLevel &= (~(1 << l));
278 str += (LogLevel2Str(l) + (shiftLevel != 0 ? "," : ""));
279 }
280 if (shiftLevel == 0) {
281 break;
282 }
283 }
284 return str;
285 }
286
Str2ComboLogLevel(const string & str)287 uint16_t Str2ComboLogLevel(const string& str)
288 {
289 uint16_t logLevels = 0;
290 if (str == "") {
291 logLevels = 0xFFFF;
292 return logLevels;
293 }
294 vector<string> vec;
295 Split(str, vec);
296 for (auto& it : vec) {
297 if (it == "") {
298 continue;
299 }
300 uint16_t t = PrettyStr2LogLevel(it);
301 if (t == LOG_LEVEL_MIN || t >= LOG_LEVEL_MAX) {
302 return 0;
303 }
304 logLevels |= (1 << t);
305 }
306 return logLevels;
307 }
308
Split(const std::string & src,std::vector<std::string> & dest,const std::string & separator)309 void Split(const std::string& src, std::vector<std::string>& dest, const std::string& separator)
310 {
311 std::string str = src;
312 std::string substring;
313 std::string::size_type start = 0;
314 std::string::size_type index;
315 dest.clear();
316 index = str.find_first_of(separator, start);
317 if (index == std::string::npos) {
318 dest.emplace_back(str);
319 return;
320 }
321 do {
322 substring = str.substr(start, index - start);
323 dest.emplace_back(substring);
324 start = index + separator.size();
325 index = str.find(separator, start);
326 } while (index != std::string::npos);
327 substring = str.substr(start);
328 if (substring != "") {
329 dest.emplace_back(substring);
330 }
331 }
332
GetBitsCount(uint64_t n)333 uint32_t GetBitsCount(uint64_t n)
334 {
335 uint32_t count = 0;
336 while (n != 0) {
337 ++count;
338 n = n & (n-1);
339 }
340 return count;
341 }
342
GetBitPos(uint64_t n)343 uint16_t GetBitPos(uint64_t n)
344 {
345 if (!(n && (!(n & (n-1))))) { // only accpet the number which is power of 2
346 return 0;
347 }
348
349 uint16_t i = 0;
350 while (n >> (i++)) {}
351 i--;
352 return i-1;
353 }
354
355 enum class Radix {
356 RADIX_DEC,
357 RADIX_HEX,
358 };
359 template<typename T>
Num2Str(T num,Radix radix)360 static string Num2Str(T num, Radix radix)
361 {
362 stringstream ss;
363 auto r = std::dec;
364 if (radix == Radix::RADIX_HEX) {
365 r = std::hex;
366 }
367 ss << r << num;
368 return ss.str();
369 }
370
371 template<typename T>
Str2Num(const string & str,T & num,Radix radix)372 static void Str2Num(const string& str, T& num, Radix radix)
373 {
374 T i = 0;
375 std::stringstream ss;
376 auto r = std::dec;
377 if (radix == Radix::RADIX_HEX) {
378 r = std::hex;
379 }
380 ss << r << str;
381 ss >> i;
382 num = i;
383 return;
384 }
385
Uint2DecStr(uint32_t i)386 string Uint2DecStr(uint32_t i)
387 {
388 return Num2Str(i, Radix::RADIX_DEC);
389 }
390
DecStr2Uint(const string & str)391 uint32_t DecStr2Uint(const string& str)
392 {
393 uint32_t i = 0;
394 Str2Num(str, i, Radix::RADIX_DEC);
395 return i;
396 }
397
Uint2HexStr(uint32_t i)398 string Uint2HexStr(uint32_t i)
399 {
400 return Num2Str(i, Radix::RADIX_HEX);
401 }
402
HexStr2Uint(const string & str)403 uint32_t HexStr2Uint(const string& str)
404 {
405 uint32_t i = 0;
406 Str2Num(str, i, Radix::RADIX_HEX);
407 return i;
408 }
409
410 #if !defined(__WINDOWS__) and !defined(__LINUX__)
GetProgName()411 string GetProgName()
412 {
413 #ifdef HILOG_USE_MUSL
414 return program_invocation_short_name;
415 #else
416 return getprogname();
417 #endif
418 }
419 #endif
420
GetNameByPid(uint32_t pid)421 string GetNameByPid(uint32_t pid)
422 {
423 char path[CMDLINE_PATH_LEN] = { 0 };
424 if (snprintf_s(path, CMDLINE_PATH_LEN, CMDLINE_PATH_LEN - 1, "/proc/%d/cmdline", pid) <= 0) {
425 return "";
426 }
427 char cmdline[CMDLINE_LEN] = { 0 };
428 int i = 0;
429 FILE *fp = fopen(path, "r");
430 if (fp == nullptr) {
431 return "";
432 }
433 while (i < (CMDLINE_LEN - 1)) {
434 char c = static_cast<char>(fgetc(fp));
435 // 0. don't need args of cmdline
436 // 1. ignore unvisible character
437 if (!isgraph(c)) {
438 break;
439 }
440 cmdline[i] = c;
441 i++;
442 }
443 (void)fclose(fp);
444 return cmdline;
445 }
446
GetPPidByPid(uint32_t pid)447 uint32_t GetPPidByPid(uint32_t pid)
448 {
449 uint32_t ppid = 0;
450 char path[STATUS_PATH_LEN] = { 0 };
451 if (snprintf_s(path, STATUS_PATH_LEN, STATUS_PATH_LEN - 1, "/proc/%u/status", pid) <= 0) {
452 return ppid;
453 }
454 FILE *fp = fopen(path, "r");
455 if (fp == nullptr) {
456 return ppid;
457 }
458 char buf[STATUS_LEN] = { 0 };
459 size_t ret = fread(buf, sizeof(char), STATUS_LEN - 1, fp);
460 (void)fclose(fp);
461 if (ret <= 0) {
462 return ppid;
463 } else {
464 buf[ret++] = '\0';
465 }
466 char *ppidLoc = strstr(buf, "PPid:");
467 if ((ppidLoc == nullptr) || (sscanf_s(ppidLoc, "PPid:%d", &ppid) == -1)) {
468 return ppid;
469 }
470 std::string ppidName = GetNameByPid(ppid);
471 // ppid fork the sh to execute hilog, sh is not wanted ppid
472 if (std::find(std::begin(SH_NAMES), std::end(SH_NAMES), ppidName) != std::end(SH_NAMES)) {
473 return GetPPidByPid(ppid);
474 }
475 return ppid;
476 }
477
GenerateHash(const char * p,size_t size)478 uint64_t GenerateHash(const char *p, size_t size)
479 {
480 static const uint64_t PRIME = 0x100000001B3ull;
481 static const uint64_t BASIS = 0xCBF29CE484222325ull;
482 uint64_t ret {BASIS};
483 unsigned long i = 0;
484 while (i < size) {
485 ret ^= *(p + i);
486 ret *= PRIME;
487 i++;
488 }
489 return ret;
490 }
491
PrintErrorno(int err)492 void PrintErrorno(int err)
493 {
494 constexpr int bufSize = 256;
495 char buf[bufSize] = { 0 };
496 #ifndef __WINDOWS__
497 (void)strerror_r(err, buf, bufSize);
498 #else
499 (void)strerror_s(buf, bufSize, err);
500 #endif
501 std::cerr << "Errno: " << err << ", " << buf << std::endl;
502 }
503
WaitingToDo(int max,const string & path,function<int (const string & path)> func)504 int WaitingToDo(int max, const string& path, function<int(const string &path)> func)
505 {
506 chrono::steady_clock::time_point start = chrono::steady_clock::now();
507 chrono::milliseconds wait(max);
508 while (true) {
509 if (func(path) != RET_FAIL) {
510 cout << "waiting for " << path << " successfully!" << endl;
511 return RET_SUCCESS;
512 }
513
514 std::this_thread::sleep_for(10ms);
515 if ((chrono::steady_clock::now() - start) > wait) {
516 cerr << "waiting for " << path << " failed!" << endl;
517 return RET_FAIL;
518 }
519 }
520 }
521 } // namespace HiviewDFX
522 } // namespace OHOS
523