From ab329b5294942b24d66802151d0a47a3e23af031 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 5 Jul 2021 13:55:25 +0800 Subject: [PATCH] Support Pid query and exclusion query Signed-off-by: Wu Shangwei <2826256824@qq.com> --- frameworks/native/include/hilog_common.h | 1 + frameworks/native/include/hilogtool_msg.h | 4 +++ services/hilogd/include/log_reader.h | 4 +++ services/hilogd/log_buffer.cpp | 19 +++++++++- services/hilogd/log_querier.cpp | 8 +++++ services/hilogtool/include/hilogtool.h | 4 +++ services/hilogtool/log_controller.cpp | 19 ++++++++-- services/hilogtool/main.cpp | 42 ++++++++++++++++++++--- 8 files changed, 93 insertions(+), 8 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index e9371c8..4cb6906 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -32,6 +32,7 @@ #define MAX_TAG_LEN 32 /* log tag size, include '\0' */ #define MAX_DOMAINS 5 #define MAX_TAGS 10 +#define MAX_PIDS 5 #define RET_SUCCESS 0 #define RET_FAIL (-1) #define IS_ONE(number, n) ((number>>n)&0x01) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 86a1f26..6ff89ec 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -101,17 +101,21 @@ typedef struct { typedef struct { MessageHeader header; + uint8_t nPid; + uint8_t nNoPid; uint8_t nDomain; uint8_t nNoDomain; uint8_t nTag; uint8_t nNoTag; uint8_t levels; uint16_t types; + uint32_t pids[MAX_PIDS]; uint32_t domains[MAX_DOMAINS]; char tags[MAX_TAGS][MAX_TAG_LEN]; uint16_t logCount; uint8_t noLevels; uint16_t noTypes; + uint32_t noPids[MAX_PIDS]; uint32_t noDomains[MAX_DOMAINS]; char noTags[MAX_TAGS][MAX_TAG_LEN]; } LogQueryRequest; diff --git a/services/hilogd/include/log_reader.h b/services/hilogd/include/log_reader.h index 85f36b1..acdfe5f 100644 --- a/services/hilogd/include/log_reader.h +++ b/services/hilogd/include/log_reader.h @@ -33,16 +33,20 @@ class HilogBuffer; #define TYPE_PERSISTER 2 using QueryCondition = struct QueryCondition { + uint8_t nPid = 0; + uint8_t nNoPid = 0; uint8_t nDomain = 0; uint8_t nNoDomain = 0; uint8_t nTag = 0; uint8_t nNoTag = 0; uint16_t levels = 0; uint16_t types = 0; + uint32_t pids[MAX_PIDS]; uint32_t domains[MAX_DOMAINS]; std::string tags[MAX_TAGS]; uint8_t noLevels = 0; uint16_t noTypes = 0; + uint32_t noPids[MAX_PIDS]; uint32_t noDomains[MAX_DOMAINS]; std::string noTags[MAX_TAGS]; }; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7b0180a..7b63f57 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -341,6 +341,16 @@ bool HilogBuffer::ConditionMatch(std::shared_ptr reader) return false; int ret = 0; + if (reader->queryCondition.nPid > 0) { + for (int i = 0; i < reader->queryCondition.nPid; i++) { + if (reader->readPos->pid == reader->queryCondition.pids[i]) { + ret = 1; + break; + } + } + if (ret == 0) return false; + ret = 0; + } if (reader->queryCondition.nDomain > 0) { for (int i = 0; i < reader->queryCondition.nDomain; i++) { uint32_t domains = reader->queryCondition.domains[i]; @@ -361,8 +371,15 @@ bool HilogBuffer::ConditionMatch(std::shared_ptr reader) } } if (ret == 0) return false; + ret = 0; + } + + // exclusion + if (reader->queryCondition.nNoPid > 0) { + for (int i = 0; i < reader->queryCondition.nNoPid; i++) { + if (reader->readPos->pid == reader->queryCondition.noPids[i]) return false; + } } - // domain exclusion if (reader->queryCondition.nNoDomain != 0) { for (int i = 0; i < reader->queryCondition.nNoDomain; i++) { uint32_t noDomains = reader->queryCondition.noDomains[i]; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e7126c3..70251ce 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -411,18 +411,26 @@ void SetCondition(std::shared_ptr logReader, const LogQueryRequest& q { logReader->queryCondition.levels = qRstMsg.levels; logReader->queryCondition.types = qRstMsg.types; + logReader->queryCondition.nPid = qRstMsg.nPid; logReader->queryCondition.nDomain = qRstMsg.nDomain; logReader->queryCondition.nTag = qRstMsg.nTag; logReader->queryCondition.noLevels = qRstMsg.noLevels; logReader->queryCondition.noTypes = qRstMsg.noTypes; + logReader->queryCondition.nNoPid = qRstMsg.nNoPid; logReader->queryCondition.nNoDomain = qRstMsg.nNoDomain; logReader->queryCondition.nNoTag = qRstMsg.nNoTag; + for (int i = 0; i < qRstMsg.nPid; i++) { + logReader->queryCondition.pids[i] = qRstMsg.pids[i]; + } for (int i = 0; i < qRstMsg.nDomain; i++) { logReader->queryCondition.domains[i] = qRstMsg.domains[i]; } for (int i = 0; i < qRstMsg.nTag; i++) { logReader->queryCondition.tags[i] = qRstMsg.tags[i]; } + for (int i = 0; i < qRstMsg.nNoPid; i++) { + logReader->queryCondition.noPids[i] = qRstMsg.noPids[i]; + } for (int i = 0; i < qRstMsg.nNoDomain; i++) { logReader->queryCondition.noDomains[i] = qRstMsg.noDomains[i]; } diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index a13cfdb..b9138c4 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -19,6 +19,8 @@ namespace OHOS { namespace HiviewDFX { typedef struct { uint16_t noBlockMode; + uint8_t nPid; + uint8_t nNoPid; uint8_t nDomain; uint8_t nNoDomain; uint8_t nTag; @@ -29,10 +31,12 @@ typedef struct { uint16_t tailLines; std::string domain; // domain recv std::string tag; // tag recv + uint32_t pids[MAX_PIDS]; std::string domains[MAX_DOMAINS]; // domains send std::string tags[MAX_TAGS]; // tags send uint16_t noTypes; uint16_t noLevels; + uint32_t noPids[MAX_PIDS]; std::string noDomains[MAX_DOMAINS]; std::string noTags[MAX_TAGS]; std::string regexArgs; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 1fb61fd..74af4a1 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -151,17 +151,32 @@ void LogQueryRequestOp(SeqPacketSocketClient& controller, const HilogArgs* conte memset_s(&logQueryRequest, sizeof(LogQueryRequest), 0, sizeof(LogQueryRequest)); logQueryRequest.levels = context->levels; logQueryRequest.types = context->types; + logQueryRequest.nPid = context->nPid; logQueryRequest.nDomain = context->nDomain; logQueryRequest.nTag = context->nTag; + for (int i = 0; i < context->nPid; i++) { + logQueryRequest.pids[i] = context->pids[i]; + } for (int i = 0; i < context->nDomain; i++) { - std::istringstream(context->domains[i]) >> std::hex >> logQueryRequest.domains[i]; + logQueryRequest.domains[i] = stoul(context->domains[i]); } for (int i = 0; i < context->nTag; i++) { - std::istringstream(context->tags[i]) >> std::hex >> logQueryRequest.tags[i]; + if (context->tags[i].length() >= MAX_TAG_LEN) { + strncpy_s(logQueryRequest.tags[i], MAX_TAG_LEN, + context->tags[i].c_str(), MAX_TAG_LEN - 1); + } else { + strncpy_s(logQueryRequest.tags[i], context->tags[i].length() + 1, + context->tags[i].c_str(), context->tags[i].length()); + } } logQueryRequest.noLevels = context->noLevels; logQueryRequest.noTypes = context->noTypes; + logQueryRequest.nNoPid = context->nNoPid; logQueryRequest.nNoDomain = context->nNoDomain; + logQueryRequest.nNoTag = context->nNoTag; + for (int i = 0; i < context->nNoPid; i++) { + logQueryRequest.noPids[i] = context->noPids[i]; + } for (int i = 0; i < context->nNoDomain; i++) { std::istringstream(context->noDomains[i]) >> std::hex >> logQueryRequest.noDomains[i]; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 0b2edf1..b2095c6 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -65,9 +65,9 @@ static void Helper() " Reads and prints logs of the specific type,\n" " which is -t app (application logs) by default.\n" " -D , --domain=\n" - " specify the domain.\n" + " specify the domain, no more than %d.\n" " -T , --Tag=\n" - " specify the tag.\n" + " specify the tag, no more than %d.\n" " -c , --compress=\n" " specify compress type of log files.\n" " 0 plain text, 1 stream compression 2 compress per file.\n" @@ -75,7 +75,7 @@ static void Helper() " -z , --tail= show n lines log on tail.\n" " -G , --buffer-size=\n" " set hilogd buffer size, use -t to specify log type.\n" - " -P specify pid .\n" + " -P specify pid, no more than %d.\n" " -e , --regex=\n" " show the logs which match the regular expression,\n" " is a regular expression.\n" @@ -107,7 +107,8 @@ static void Helper() " \n Types, levels, domains, tags support exclusion query.\n" " Exclusion query can be done with parameters starting with \"^\" and delimiter \",\".\n" " Example: \"-t ^core,app\" excludes logs with types core and app.\n" - " Could be used along with other parameters.\n" + " Could be used along with other parameters.\n", + MAX_DOMAINS, MAX_TAGS, MAX_PIDS ); } @@ -165,6 +166,7 @@ int HilogEntry(int argc, char* argv[]) int optIndex = 0; int indexLevel = 0; int indexType = 0; + int indexPid = 0; int indexDomain = 0; int indexTag = 0; bool noLogOption = false; @@ -409,7 +411,37 @@ int HilogEntry(int argc, char* argv[]) noLogOption = true; break; case 'P': - context.pidArgs = optarg; + indexPid = optind - 1; + while (indexPid < argc) { + if ((context.nPid >= MAX_PIDS) || (context.nNoPid >= MAX_PIDS)) { + break; + } + std::string pids(argv[indexPid]); + indexPid++; + if (!strstr(pids.c_str(), "-")) { + if (pids.front() == '^') { + vector v(sregex_token_iterator(pids.begin() + 1, pids.end(), delimiter, -1), + sregex_token_iterator()); + for (auto s: v) { + context.noPids[context.nNoPid++] = stoul(s); + } + } else { + vector v(sregex_token_iterator(pids.begin(), pids.end(), delimiter, -1), + sregex_token_iterator()); + for (auto s: v) { + context.pids[context.nPid++] = stoul(s); + context.pidArgs += s + " "; + } + } + } else { + break; + } + } + if (context.nPid != 0 && context.nNoPid != 0) { + std::cout << "Query condition on both pid and excluded pid is undefined." << std::endl; + std::cout << "Please remove pid or excluded pid condition, and try again." << std::endl; + exit(RET_FAIL); + } break; case 'm': context.algorithmArgs = optarg; -- Gitee