From b466fa7b370da5d3c4975f13c595b975486075cd Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 1 Sep 2021 20:56:18 +0800 Subject: [PATCH 01/31] LogCollector: Reads kmsg into HilogBuffer, setting type=init Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I591ecbee9e87f9d2e8dec92ace829970f69594ee --- services/hilogd/include/log_collector.h | 6 +++- services/hilogd/log_collector.cpp | 43 ++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 3109a7d..364f0a0 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -35,7 +35,11 @@ public: ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; + ssize_t ReadOneKmsg(); + void ReadAllKmsg(); + int kmsgCtl = 0; + char kmsgBuffer[BUFSIZ]; }; } // namespace HiviewDFX } // namespace OHOS -#endif \ No newline at end of file +#endif diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index ab73cec..0a5e0e9 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -21,11 +21,49 @@ #include #include #include - +#include +#include +#include +#include namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; + +ssize_t LogCollector::ReadOneKmsg() +{ + ssize_t size; + char kmsgTag[] = "kmsg"; + int tagLen = sizeof(kmsgTag); + do { + size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); + } while (size < 0 && errno == EPIPE); + if (size > 0) { + int len = strlen(kmsgBuffer); + int msgLen = sizeof(HilogMsg) + tagLen + 1 + len + 1; + HilogMsg *msg = (HilogMsg*)malloc(msgLen); + msg->len = msgLen; + msg->tag_len = 5; + msg->type = LOG_INIT; + msg->level = LOG_DEBUG; + strncpy_s(msg->tag, 5, kmsgTag, 5); + strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); + InsertLogToBuffer(*msg); + free(msg); + } + return size; +} + +void LogCollector::ReadAllKmsg() +{ + ssize_t sz = ReadOneKmsg(); + + while (sz > 0) { + memset(kmsgBuffer, '\0', BUFSIZ); + sz = ReadOneKmsg(); + } +} + int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) { string dropLog = to_string(ret) + " line(s) dropped!"; @@ -87,6 +125,9 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; + kmsgCtl = open("/dev/kmsg", O_RDONLY | O_NONBLOCK); +// lseek(kmsgCtl, 0, SEEK_DATA); + ReadAllKmsg(); } void LogCollector::operator()() -- Gitee From 53aa496c8b0c7243e2b20fd2809b4825ecf9eecf Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Tue, 31 Aug 2021 06:42:34 +0000 Subject: [PATCH 02/31] codex issue fix Signed-off-by: yaomanhai --- adapter/properties.cpp | 3 +-- adapter/socket_server_adapter.cpp | 4 ++-- .../native/include/seq_packet_socket_client.h | 2 +- services/hilogd/include/log_compress.h | 4 ++-- services/hilogd/log_collector.cpp | 6 +++--- services/hilogd/log_compress.cpp | 1 + services/hilogd/log_persister.cpp | 20 +++++++++++++------ services/hilogd/log_querier.cpp | 16 +++++++-------- 8 files changed, 32 insertions(+), 24 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 202f09f..939b0ea 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -87,8 +87,7 @@ void PropertySet(const string &key, const char* value) string GetProgName() { - return nullptr; - /* use HOS interface */ + return program_invocation_short_name; /* use HOS interface */ } string GetPropertyName(uint32_t propType) diff --git a/adapter/socket_server_adapter.cpp b/adapter/socket_server_adapter.cpp index 675676c..65dec87 100644 --- a/adapter/socket_server_adapter.cpp +++ b/adapter/socket_server_adapter.cpp @@ -49,13 +49,13 @@ static std::string GetFileNameByFd(const int fd) static void GetSocketFds(std::vector &fds) { - struct dirent *dp; + struct dirent *dp = nullptr; DIR *dir = opendir(OHOS_PROC_FD_DIR); if (!dir) { return; } - while ((dp = readdir(dir)) != NULL) { + while ((dp = readdir(dir)) != nullptr) { int fd = -1; if (dp->d_name[0] == '.') { continue; diff --git a/frameworks/native/include/seq_packet_socket_client.h b/frameworks/native/include/seq_packet_socket_client.h index d3fd6e5..90fdf01 100644 --- a/frameworks/native/include/seq_packet_socket_client.h +++ b/frameworks/native/include/seq_packet_socket_client.h @@ -28,7 +28,7 @@ enum SeqPacketSocketResult { class SeqPacketSocketClient : public SocketClient { public: - SeqPacketSocketClient(std::string serverPath, uint32_t socketOption) : SocketClient(serverPath, SOCK_SEQPACKET) + SeqPacketSocketClient(std::string serverPath, int socketOption) : SocketClient(serverPath, SOCK_SEQPACKET) { int socketType = SOCK_SEQPACKET | (socketOption & allowOption); SetType(socketType); diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index dcb2afd..0a32f76 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -39,8 +39,8 @@ public: virtual ~LogCompress() = default; virtual int Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &compressBuffer) = 0; unsigned char *zdata = nullptr; - char buffIn[CHUNK]; - char buffOut[CHUNK]; + char buffIn[CHUNK] = {0}; + char buffOut[CHUNK] = {0}; }; class NoneCompress : public LogCompress { diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 0a5e0e9..1fdf0ec 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -68,7 +68,7 @@ int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) { string dropLog = to_string(ret) + " line(s) dropped!"; char tag[] = "LOGLIMITD"; - int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; + int len = sizeof(HilogMsg) + sizeof(tag) + dropLog.size() + 1; HilogMsg *dropMsg = (HilogMsg*)malloc(len); if (dropMsg != nullptr) { dropMsg->len = len; @@ -81,9 +81,9 @@ int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) dropMsg->pid = msg->pid; dropMsg->tid = msg->tid; dropMsg->domain = msg->domain; - if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { + if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag))) { } - if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, + if (memcpy_s(dropMsg->tag + sizeof(tag), len - sizeof(HilogMsg) - sizeof(tag), dropLog.c_str(), dropLog.size() + 1)) { } InsertLogToBuffer(*dropMsg); diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 7b35f78..6a997af 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -52,6 +52,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.opaque = Z_NULL; if (deflateInit2(&cStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { delete zdata; + zdata = nullptr; return -1; } do { diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 58af512..8e5be8b 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -106,12 +106,13 @@ int LogPersister::Init() } bool hit = false; const lock_guard lock(g_listMutex); - for (auto it = logPersisters.begin(); it != logPersisters.end(); ++it) + for (auto it = logPersisters.begin(); it != logPersisters.end(); ++it) { if ((*it)->getPath() == path || (*it)->Identify(id)) { std::cout << path << std::endl; hit = true; break; } + } if (hit) { return ERR_LOG_PERSIST_FILE_PATH_INVALID; } @@ -122,9 +123,6 @@ int LogPersister::Init() fd = fopen(mmapPath.c_str(), "r+"); } else { fd = fopen(mmapPath.c_str(), "w+"); - ftruncate(fileno(fd), sizeof(LogPersisterBuffer)); - fflush(fd); - fsync(fileno(fd)); } if (fd == nullptr) { @@ -133,6 +131,12 @@ int LogPersister::Init() #endif return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } + + if (!restore) { + ftruncate(fileno(fd), sizeof(LogPersisterBuffer)); + fflush(fd); + fsync(fileno(fd)); + } buffer = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, MAP_SHARED, fileno(fd), 0); fclose(fd); @@ -186,8 +190,12 @@ int GenPersistLogHeader(HilogData *data, list& persistList) int offset = data->tag_len; char *dataCopy = (char*)calloc(data->len, sizeof(char)); - memcpy_s(dataCopy, offset, data->tag, offset); - memcpy_s(dataCopy + offset, data->len - offset, data->content, data->len - offset); + if (dataCopy == nullptr) { + return 0; + } + if (memcpy_s(dataCopy, data->len, data->tag, data->len)) { + return 0; + } showBuffer.data = dataCopy; char *dataBegin = dataCopy + offset; char *dataPos = dataCopy + offset; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 1add41b..a53da67 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -146,10 +146,10 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } else if (pLogPersistStartMsg->fileSize < MAX_PERSISTER_BUFFER_SIZE) { cout << "Persist log file size less than min size" << std::endl; pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_SIZE_INVALID; - } else if (IsValidFileName(string(pLogPersistStartMsg->filePath)) == false){ + } else if (IsValidFileName(string(pLogPersistStartMsg->filePath)) == false) { cout << "FileName is not valid!" << endl; pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_NAME_INVALID; - } else { + } else { logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + "hilog") : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); if (strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()) != 0) { @@ -430,22 +430,22 @@ void SetCondition(std::shared_ptr logReader, const LogQueryRequest& q logReader->queryCondition.nNoPid = qRstMsg.nNoPid; logReader->queryCondition.nNoDomain = qRstMsg.nNoDomain; logReader->queryCondition.nNoTag = qRstMsg.nNoTag; - for (int i = 0; i < qRstMsg.nPid; i++) { + for (int i = 0; (i < qRstMsg.nPid) && (i < MAX_PIDS); i++) { logReader->queryCondition.pids[i] = qRstMsg.pids[i]; } - for (int i = 0; i < qRstMsg.nDomain; i++) { + for (int i = 0; (i < qRstMsg.nDomain) && (i < MAX_DOMAINS); i++) { logReader->queryCondition.domains[i] = qRstMsg.domains[i]; } - for (int i = 0; i < qRstMsg.nTag; i++) { + for (int i = 0; (i < qRstMsg.nTag) && (i < MAX_TAGS); i++) { logReader->queryCondition.tags[i] = qRstMsg.tags[i]; } - for (int i = 0; i < qRstMsg.nNoPid; i++) { + for (int i = 0; (i < qRstMsg.nNoPid) && (i < MAX_PIDS); i++) { logReader->queryCondition.noPids[i] = qRstMsg.noPids[i]; } - for (int i = 0; i < qRstMsg.nNoDomain; i++) { + for (int i = 0; (i < qRstMsg.nNoDomain) && (i < MAX_DOMAINS); i++) { logReader->queryCondition.noDomains[i] = qRstMsg.noDomains[i]; } - for (int i = 0; i < qRstMsg.nNoTag; i++) { + for (int i = 0; (i < qRstMsg.nNoTag) && (i < MAX_TAGS); i++) { logReader->queryCondition.noTags[i] = qRstMsg.noTags[i]; } } -- Gitee From c31e7bbeda569f4999c946d1d53b486c304bca62 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Tue, 31 Aug 2021 12:56:46 +0000 Subject: [PATCH 03/31] fix codex issue Signed-off-by: yaomanhai --- services/hilogd/log_persister.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 8e5be8b..4831bfa 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -194,6 +194,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) return 0; } if (memcpy_s(dataCopy, data->len, data->tag, data->len)) { + free(dataCopy); return 0; } showBuffer.data = dataCopy; -- Gitee From 886e047bf9f470d6333612b566cfbbb5daef9d5f Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 1 Sep 2021 22:40:48 +0800 Subject: [PATCH 04/31] Blocking read from kmsg; However breaks LogCollector Notfiy Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Icfb99aa996a3764d2bf49b8c7c0e41d9e290f0f5 --- services/hilogd/include/log_collector.h | 6 +-- services/hilogd/log_collector.cpp | 37 -------------- services/hilogd/main.cpp | 67 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 41 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 364f0a0..4e1b542 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -35,10 +35,8 @@ public: ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; - ssize_t ReadOneKmsg(); - void ReadAllKmsg(); - int kmsgCtl = 0; - char kmsgBuffer[BUFSIZ]; +// ssize_t ReadOneKmsg(); +// void ReadAllKmsg(); }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 1fdf0ec..f3a3f50 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -30,40 +30,6 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; -ssize_t LogCollector::ReadOneKmsg() -{ - ssize_t size; - char kmsgTag[] = "kmsg"; - int tagLen = sizeof(kmsgTag); - do { - size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); - } while (size < 0 && errno == EPIPE); - if (size > 0) { - int len = strlen(kmsgBuffer); - int msgLen = sizeof(HilogMsg) + tagLen + 1 + len + 1; - HilogMsg *msg = (HilogMsg*)malloc(msgLen); - msg->len = msgLen; - msg->tag_len = 5; - msg->type = LOG_INIT; - msg->level = LOG_DEBUG; - strncpy_s(msg->tag, 5, kmsgTag, 5); - strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); - InsertLogToBuffer(*msg); - free(msg); - } - return size; -} - -void LogCollector::ReadAllKmsg() -{ - ssize_t sz = ReadOneKmsg(); - - while (sz > 0) { - memset(kmsgBuffer, '\0', BUFSIZ); - sz = ReadOneKmsg(); - } -} - int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) { string dropLog = to_string(ret) + " line(s) dropped!"; @@ -125,9 +91,6 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; - kmsgCtl = open("/dev/kmsg", O_RDONLY | O_NONBLOCK); -// lseek(kmsgCtl, 0, SEEK_DATA); - ReadAllKmsg(); } void LogCollector::operator()() diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index df060f2..f07ea9c 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "cmd_executor.h" #include "log_querier.h" @@ -38,6 +41,65 @@ using namespace std; static int g_fd = -1; #endif +int kmsgCtl = open("/dev/kmsg", O_RDONLY); +char kmsgBuffer[BUFSIZ]; + +size_t InsertLogToBuffer(const HilogMsg& msg, HilogBuffer* hilogBuffer) +{ + size_t result = hilogBuffer->Insert(msg); + if (result <= 0) { + return result; + } + hilogBuffer->logReaderListMutex.lock_shared(); + auto it = hilogBuffer->logReaderList.begin(); + while (it != hilogBuffer->logReaderList.end()) { + if ((*it).lock()->GetType() != TYPE_CONTROL) { + (*it).lock()->NotifyForNewData(); + } + ++it; + } + hilogBuffer->logReaderListMutex.unlock_shared(); + return result; +} + +ssize_t ReadOneKmsg(HilogBuffer* hilogBuffer) +{ + ssize_t size = -1; + char kmsgTag[] = "kmsg"; + int tagLen = sizeof(kmsgTag); + do { + size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); + } while (size < 0 && errno == EPIPE); + if (size > 0) { + int len = strlen(kmsgBuffer) + 1; + int msgLen = sizeof(HilogMsg) + tagLen + len + 1; + HilogMsg *msg = (HilogMsg*)malloc(msgLen); + msg->len = msgLen; + msg->tag_len = 5; + msg->type = LOG_INIT; + msg->level = LOG_DEBUG; + strncpy_s(msg->tag, 5, kmsgTag, 5); + strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); + InsertLogToBuffer(*msg, hilogBuffer); + free(msg); + } + return size; +} + +void ReadAllKmsg(HilogBuffer* hilogBuffer) +{ + ssize_t sz = ReadOneKmsg(hilogBuffer); + + std::thread th([sz,hilogBuffer]() { + int ret = sz; + while (ret > 0) { + memset(kmsgBuffer, '\0', BUFSIZ); + ret = ReadOneKmsg(hilogBuffer); + } + }); + th.detach(); +} + static void SigHandler(int sig) { if (sig == SIGINT) { @@ -89,6 +151,11 @@ int HilogdEntry(int argc, char* argv[]) }); startupCheckThread.detach(); + std::thread KmsgThread([&hilogBuffer]() { + ReadAllKmsg(&hilogBuffer); + }); + KmsgThread.detach(); + CmdExecutor cmdExecutor(&hilogBuffer); cmdExecutor.StartCmdExecutorThread(); -- Gitee From 89b4c3eab009cd3a7cf558d202728d523fe08c03 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 1 Sep 2021 23:16:53 +0800 Subject: [PATCH 05/31] Fix kmsg block problem by setting its time to zero Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Iacd55fcceac4b3a7b9b3ce6e8755abecc4a97db5 --- services/hilogd/include/log_collector.h | 6 ++- services/hilogd/log_collector.cpp | 40 +++++++++++++++ services/hilogd/main.cpp | 66 ------------------------- 3 files changed, 44 insertions(+), 68 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 4e1b542..3a8edf9 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -35,8 +35,10 @@ public: ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; -// ssize_t ReadOneKmsg(); -// void ReadAllKmsg(); + ssize_t ReadOneKmsg(); + void ReadAllKmsg(); + int kmsgCtl = -1; + char kmsgBuffer[BUFSIZ]; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index f3a3f50..4c8093d 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -30,6 +30,41 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; +ssize_t LogCollector::ReadOneKmsg() +{ + ssize_t size = -1; + char kmsgTag[] = "kmsg"; + int tagLen = sizeof(kmsgTag); + do { + size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); + } while (size < 0 && errno == EPIPE); + if (size > 0) { + int len = strlen(kmsgBuffer) + 1; + int msgLen = sizeof(HilogMsg) + tagLen + len + 1; + HilogMsg *msg = (HilogMsg*)malloc(msgLen); + msg->len = msgLen; + msg->tag_len = 5; + msg->type = LOG_INIT; + msg->level = LOG_DEBUG; + msg->tv_sec = 0; + msg->tv_nsec = 0; + strncpy_s(msg->tag, 5, kmsgTag, 5); + strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); + InsertLogToBuffer(*msg); + free(msg); + } + return size; +} + +void LogCollector::ReadAllKmsg() +{ + ssize_t sz = ReadOneKmsg(); + while (sz > 0) { + memset(kmsgBuffer, '\0', BUFSIZ); + sz = ReadOneKmsg(); + } +} + int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) { string dropLog = to_string(ret) + " line(s) dropped!"; @@ -91,6 +126,11 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; + kmsgCtl = open("/dev/kmsg", O_RDONLY); + std::thread KmsgThread([this]() { + ReadAllKmsg(); + }); + KmsgThread.detach(); } void LogCollector::operator()() diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index f07ea9c..c0398e3 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -17,9 +17,6 @@ #include #include #include -#include -#include -#include #include "cmd_executor.h" #include "log_querier.h" @@ -41,64 +38,6 @@ using namespace std; static int g_fd = -1; #endif -int kmsgCtl = open("/dev/kmsg", O_RDONLY); -char kmsgBuffer[BUFSIZ]; - -size_t InsertLogToBuffer(const HilogMsg& msg, HilogBuffer* hilogBuffer) -{ - size_t result = hilogBuffer->Insert(msg); - if (result <= 0) { - return result; - } - hilogBuffer->logReaderListMutex.lock_shared(); - auto it = hilogBuffer->logReaderList.begin(); - while (it != hilogBuffer->logReaderList.end()) { - if ((*it).lock()->GetType() != TYPE_CONTROL) { - (*it).lock()->NotifyForNewData(); - } - ++it; - } - hilogBuffer->logReaderListMutex.unlock_shared(); - return result; -} - -ssize_t ReadOneKmsg(HilogBuffer* hilogBuffer) -{ - ssize_t size = -1; - char kmsgTag[] = "kmsg"; - int tagLen = sizeof(kmsgTag); - do { - size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); - } while (size < 0 && errno == EPIPE); - if (size > 0) { - int len = strlen(kmsgBuffer) + 1; - int msgLen = sizeof(HilogMsg) + tagLen + len + 1; - HilogMsg *msg = (HilogMsg*)malloc(msgLen); - msg->len = msgLen; - msg->tag_len = 5; - msg->type = LOG_INIT; - msg->level = LOG_DEBUG; - strncpy_s(msg->tag, 5, kmsgTag, 5); - strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); - InsertLogToBuffer(*msg, hilogBuffer); - free(msg); - } - return size; -} - -void ReadAllKmsg(HilogBuffer* hilogBuffer) -{ - ssize_t sz = ReadOneKmsg(hilogBuffer); - - std::thread th([sz,hilogBuffer]() { - int ret = sz; - while (ret > 0) { - memset(kmsgBuffer, '\0', BUFSIZ); - ret = ReadOneKmsg(hilogBuffer); - } - }); - th.detach(); -} static void SigHandler(int sig) { @@ -151,11 +90,6 @@ int HilogdEntry(int argc, char* argv[]) }); startupCheckThread.detach(); - std::thread KmsgThread([&hilogBuffer]() { - ReadAllKmsg(&hilogBuffer); - }); - KmsgThread.detach(); - CmdExecutor cmdExecutor(&hilogBuffer); cmdExecutor.StartCmdExecutorThread(); -- Gitee From 2d85744bc6ed59d55e03f998eeb8445dd69d8283 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 2 Sep 2021 10:32:54 +0000 Subject: [PATCH 06/31] hilog -t kmsg is okay Signed-off-by: aajwy <13051180828@163.com> --- .../native/innerkits/include/hilog/log_c.h | 30 +++++++++---------- services/hilogd/log_collector.cpp | 4 +-- services/hilogd/log_querier.cpp | 2 +- services/hilogtool/main.cpp | 4 ++- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/interfaces/native/innerkits/include/hilog/log_c.h b/interfaces/native/innerkits/include/hilog/log_c.h index 5622eaa..9def3fd 100644 --- a/interfaces/native/innerkits/include/hilog/log_c.h +++ b/interfaces/native/innerkits/include/hilog/log_c.h @@ -1,17 +1,17 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef HIVIEWDFX_HILOG_C_H #define HIVIEWDFX_HILOG_C_H @@ -37,8 +37,8 @@ extern "C" { typedef enum { LOG_TYPE_MIN = 0, LOG_APP = 0, - // Log to kmsg, only used by init phase. LOG_INIT = 1, + LOG_KMSG = 2, // Used by core service, framework. LOG_CORE = 3, LOG_TYPE_MAX diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 4c8093d..31b439a 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -44,7 +44,7 @@ ssize_t LogCollector::ReadOneKmsg() HilogMsg *msg = (HilogMsg*)malloc(msgLen); msg->len = msgLen; msg->tag_len = 5; - msg->type = LOG_INIT; + msg->type = LOG_KMSG; msg->level = LOG_DEBUG; msg->tv_sec = 0; msg->tv_nsec = 0; @@ -126,7 +126,7 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; - kmsgCtl = open("/dev/kmsg", O_RDONLY); + kmsgCtl = open("/proc/kmsg", O_RDONLY); std::thread KmsgThread([this]() { ReadAllKmsg(); }); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index a53da67..97abdcf 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -44,7 +44,7 @@ using namespace std; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = HILOG_FILE_DIR; constexpr int DEFAULT_LOG_LEVEL = 1< Date: Thu, 2 Sep 2021 10:49:45 +0000 Subject: [PATCH 07/31] delete unused code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/include/log_controller.h | 1 - 1 file changed, 1 deletion(-) diff --git a/services/hilogtool/include/log_controller.h b/services/hilogtool/include/log_controller.h index 2030eac..aba30be 100644 --- a/services/hilogtool/include/log_controller.h +++ b/services/hilogtool/include/log_controller.h @@ -30,7 +30,6 @@ constexpr int RECV_BUF_LEN = MAX_LOG_LEN * 2; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen); int MultiQuerySplit(const std::string& src, const char& delim, std::vector& vec); -inline void PrintBuffer(void* pBuff, unsigned int nLen); void NextRequestOp(SeqPacketSocketClient& controller, uint16_t sendId); void LogQueryRequestOp(SeqPacketSocketClient& controller, const HilogArgs* context); void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uint32_t bufLen, -- Gitee From 9ae2f6030a26620369a6cf7093a28778551a5b2d Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Thu, 9 Sep 2021 21:17:17 +0800 Subject: [PATCH 08/31] LogCollector: parse priority and timestamp of kmsg Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/log_collector.h | 4 +-- services/hilogd/log_collector.cpp | 39 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 3a8edf9..f49dff9 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -32,13 +32,13 @@ public: #else static int onDataRecv(ucred cred, char *data, unsigned int dataLen); #endif - ~LogCollector() = default; + ~LogCollector(); private: static HilogBuffer* hilogBuffer; ssize_t ReadOneKmsg(); void ReadAllKmsg(); int kmsgCtl = -1; - char kmsgBuffer[BUFSIZ]; + char* kmsgBuffer; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 31b439a..347570a 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -25,31 +25,49 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; +uint16_t KmsgLevelMap(uint16_t prio) +{ + if (prio <= 2) return LOG_FATAL; + if (prio == 3) return LOG_ERR; + if (prio <= 5) return LOG_WARN; + if (prio == 6) return LOG_INFO; + return LOG_DEBUG; +} + ssize_t LogCollector::ReadOneKmsg() { ssize_t size = -1; char kmsgTag[] = "kmsg"; + char* end = NULL; int tagLen = sizeof(kmsgTag); do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); if (size > 0) { + uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, 10); + strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present + uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), &end, 10); + strsep(&kmsgBuffer, ";"); int len = strlen(kmsgBuffer) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); msg->len = msgLen; - msg->tag_len = 5; + msg->tag_len = tagLen; msg->type = LOG_KMSG; - msg->level = LOG_DEBUG; - msg->tv_sec = 0; - msg->tv_nsec = 0; - strncpy_s(msg->tag, 5, kmsgTag, 5); - strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); + msg->domain = 0xd000000; + msg->level = KmsgLevelMap(mLevel); + msg->tv_sec = timestamp / 1000000; + msg->tv_nsec = timestamp % 1000000; + msg->pid = 0; + msg->tid = 0; + strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); + strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); InsertLogToBuffer(*msg); free(msg); } @@ -126,13 +144,20 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; - kmsgCtl = open("/proc/kmsg", O_RDONLY); + kmsgBuffer = (char*)calloc(BUFSIZ, sizeof(char)); + kmsgCtl = open("/dev/kmsg", O_RDONLY); std::thread KmsgThread([this]() { ReadAllKmsg(); }); KmsgThread.detach(); } +LogCollector::~LogCollector() +{ + close(kmsgCtl); + free(kmsgBuffer); +} + void LogCollector::operator()() { HilogInputSocketServer server(onDataRecv); -- Gitee From 4af6b7633ecc5975740153f86e0ec587e2bd8600 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 14 Sep 2021 04:16:38 +0000 Subject: [PATCH 09/31] kmsg&&rm unused .h Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_collector.h | 4 +-- services/hilogd/log_buffer.cpp | 2 -- services/hilogd/log_collector.cpp | 41 ++++++++++++++++++++----- services/hilogtool/log_controller.cpp | 1 - services/hilogtool/log_display.cpp | 1 - services/hilogtool/main.cpp | 1 - 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 3a8edf9..f49dff9 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -32,13 +32,13 @@ public: #else static int onDataRecv(ucred cred, char *data, unsigned int dataLen); #endif - ~LogCollector() = default; + ~LogCollector(); private: static HilogBuffer* hilogBuffer; ssize_t ReadOneKmsg(); void ReadAllKmsg(); int kmsgCtl = -1; - char kmsgBuffer[BUFSIZ]; + char* kmsgBuffer; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7a4e683..e4d4ee0 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -15,14 +15,12 @@ #include "log_buffer.h" -#include #include #include #include #include #include #include -#include #include "hilog_common.h" #include "flow_control_init.h" diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 31b439a..342a981 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -25,31 +25,49 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; +uint16_t KmsgLevelMap(uint16_t prio) +{ + if (prio <= 2) return LOG_FATAL; + if (prio == 3) return LOG_ERR; + if (prio <= 5) return LOG_WARN; + if (prio == 6) return LOG_INFO; + return LOG_DEBUG; +} + ssize_t LogCollector::ReadOneKmsg() { ssize_t size = -1; char kmsgTag[] = "kmsg"; - int tagLen = sizeof(kmsgTag); + char* end = NULL; do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); if (size > 0) { + uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, 10); + strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present + uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), &end, 10); + strsep(&kmsgBuffer, ";"); int len = strlen(kmsgBuffer) + 1; + int tagLen = sizeof(kmsgTag); int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); msg->len = msgLen; - msg->tag_len = 5; + msg->tag_len = tagLen; msg->type = LOG_KMSG; - msg->level = LOG_DEBUG; - msg->tv_sec = 0; - msg->tv_nsec = 0; - strncpy_s(msg->tag, 5, kmsgTag, 5); - strncpy_s(CONTENT_PTR((msg)), BUFSIZ, kmsgBuffer, len); + msg->domain = 0xd000000; + msg->level = KmsgLevelMap(mLevel); + msg->tv_sec = timestamp / 1000000; + msg->tv_nsec = timestamp * 1000; + msg->pid = 0; + msg->tid = 0; + strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); + strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); InsertLogToBuffer(*msg); free(msg); } @@ -126,13 +144,20 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; - kmsgCtl = open("/proc/kmsg", O_RDONLY); + kmsgBuffer = (char*)calloc(BUFSIZ, sizeof(char)); + kmsgCtl = open("/dev/kmsg", O_RDONLY); std::thread KmsgThread([this]() { ReadAllKmsg(); }); KmsgThread.detach(); } +LogCollector::~LogCollector() +{ + close(kmsgCtl); + free(kmsgBuffer); +} + void LogCollector::operator()() { HilogInputSocketServer server(onDataRecv); diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 17f1e81..4b170f9 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 1b19a39..e22465e 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index f4762f5..5b1180b 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include -- Gitee From 64b92943b8718c6e756bc11c7dff0f6c2641beae Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 15 Sep 2021 11:58:30 +0800 Subject: [PATCH 10/31] hilogd: Kmsg as a seperate class; Kmsg in seperate buffer to avoid race condition Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/BUILD.gn | 1 + services/hilogd/include/klog.h | 38 +++++++++ services/hilogd/include/log_buffer.h | 1 + services/hilogd/include/log_collector.h | 6 +- services/hilogd/klog.cpp | 106 ++++++++++++++++++++++++ services/hilogd/log_buffer.cpp | 24 +++--- services/hilogd/log_collector.cpp | 67 +-------------- services/hilogd/main.cpp | 10 ++- services/hilogtool/main.cpp | 2 +- 9 files changed, 170 insertions(+), 85 deletions(-) create mode 100644 services/hilogd/include/klog.h create mode 100644 services/hilogd/klog.cpp diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index e241cd5..7670a53 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -24,6 +24,7 @@ ohos_executable("hilogd") { "cmd_executor.cpp", "flow_control_init.cpp", "log_buffer.cpp", + "klog.cpp", "log_collector.cpp", "log_compress.cpp", "log_persister.cpp", diff --git a/services/hilogd/include/klog.h b/services/hilogd/include/klog.h new file mode 100644 index 0000000..f001cd4 --- /dev/null +++ b/services/hilogd/include/klog.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef KLOG_H +#define KLOG_H + +#include "log_collector.h" + +namespace OHOS { +namespace HiviewDFX { +class KLog { +public: + KLog(const LogCollector& collector) : collector(collector) {}; + KLog(const LogCollector&&) = delete; + ~KLog(); + ssize_t ReadOneKmsg(); + int ReadAllKmsg(); + void Start(); +private: + int kmsgCtl = -1; + char* kmsgBuffer; + const LogCollector& collector; +}; +} // namespace HiviewDFX +} // namespace OHOS +#endif + diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index a0ad2dd..5dd12f9 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -53,6 +53,7 @@ private: size_t size; size_t sizeByType[LOG_TYPE_MAX]; std::list hilogDataList; + std::list hilogKlogList; std::shared_mutex hilogBufferMutex; std::map cacheLenByDomain; std::map printLenByDomain; diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index f49dff9..1eaa729 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -32,13 +32,9 @@ public: #else static int onDataRecv(ucred cred, char *data, unsigned int dataLen); #endif - ~LogCollector(); + ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; - ssize_t ReadOneKmsg(); - void ReadAllKmsg(); - int kmsgCtl = -1; - char* kmsgBuffer; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/klog.cpp b/services/hilogd/klog.cpp new file mode 100644 index 0000000..cdc37fb --- /dev/null +++ b/services/hilogd/klog.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "klog.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace OHOS { +namespace HiviewDFX { +using namespace std; + +uint16_t KmsgLevelMap(uint16_t prio) +{ + if (prio <= 2) return LOG_FATAL; + if (prio == 3) return LOG_ERR; + if (prio <= 5) return LOG_WARN; + if (prio == 6) return LOG_INFO; + return LOG_DEBUG; +} + +ssize_t KLog::ReadOneKmsg() +{ + ssize_t size = -1; + char kmsgTag[] = "kmsg"; + char* end = NULL; + int tagLen = sizeof(kmsgTag); + do { + size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); + } while (size < 0 && errno == EPIPE); + if (size > 0) { + uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, 10); + strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present + uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), &end, 10); + strsep(&kmsgBuffer, ";"); + int len = strlen(kmsgBuffer) + 1; + int msgLen = sizeof(HilogMsg) + tagLen + len + 1; + HilogMsg *msg = (HilogMsg*)malloc(msgLen); + msg->len = msgLen; + msg->tag_len = tagLen; + msg->type = LOG_KMSG; + msg->domain = 0xd000000; + msg->level = KmsgLevelMap(mLevel); + msg->tv_sec = timestamp / 1000000; + msg->tv_nsec = (timestamp % 1000000) * 1000; + msg->pid = 0; + msg->tid = 0; + strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); + strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); + collector.InsertLogToBuffer(*msg); + free(msg); + } + return size; +} + +int KLog::ReadAllKmsg() +{ + kmsgBuffer = (char*)calloc(BUFSIZ, sizeof(char)); + kmsgCtl = open("/dev/kmsg", O_RDONLY); + if (kmsgCtl < 0) { + std::cout << "Cannot open kmsg! Err=" << strerror(errno) << std::endl; + return -1; + } + ssize_t sz = ReadOneKmsg(); + while (sz > 0) { + memset(kmsgBuffer, '\0', BUFSIZ); + sz = ReadOneKmsg(); + } + return 1; +} + +void KLog::Start() +{ + std::thread KmsgThread([this]() { + ReadAllKmsg(); + }); + KmsgThread.join(); +} + +KLog::~KLog() +{ + close(kmsgCtl); + free(kmsgBuffer); +} +} // namespace HiviewDFX +} // namespace OHOS + diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7a4e683..548d404 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -60,13 +60,14 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) return 0; } + std::list& list = (unlikely(msg.type == LOG_KMSG)) ? hilogKlogList: hilogDataList; // Delete old entries when full if (eleSize + sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { hilogBufferMutex.lock(); // Drop 5% of maximum log when full - std::list::iterator it = hilogDataList.begin(); + std::list::iterator it = list.begin(); while (sizeByType[msg.type] > g_maxBufferSizeByType[msg.type] * (1 - DROP_RATIO) && - it != hilogDataList.end()) { + it != list.end()) { if ((*it).type != msg.type) { // Only remove old logs of the same type ++it; continue; @@ -84,7 +85,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size_t cLen = it->len - it->tag_len; size -= cLen; sizeByType[(*it).type] -= cLen; - it = hilogDataList.erase(it); + it = list.erase(it); } // Re-confirm if enough elements has been removed @@ -95,13 +96,13 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) } // Insert new log into HilogBuffer - std::list::reverse_iterator rit = hilogDataList.rbegin(); + std::list::reverse_iterator rit = list.rbegin(); if (msg.tv_sec >= (rit->tv_sec)) { - hilogDataList.emplace_back(msg); + list.emplace_back(msg); } else { // Find the place with right timestamp ++rit; - for (; rit != hilogDataList.rend() && msg.tv_sec < rit->tv_sec; ++rit) { + for (; rit != list.rend() && msg.tv_sec < rit->tv_sec; ++rit) { logReaderListMutex.lock_shared(); for (auto &itr :logReaderList) { if (itr.lock()->readPos == std::prev(rit.base())) { @@ -110,7 +111,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) } logReaderListMutex.unlock_shared(); } - hilogDataList.emplace(rit.base(), msg); + list.emplace(rit.base(), msg); } // Update current size of HilogBuffer size += eleSize; @@ -127,15 +128,16 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) bool HilogBuffer::Query(std::shared_ptr reader) { + std::list& list = (unlikely((reader->queryCondition.types) == 0b01 << LOG_KMSG)) ? hilogKlogList : hilogDataList; hilogBufferMutex.lock_shared(); if (reader->GetReload()) { - reader->readPos = hilogDataList.begin(); - reader->lastPos = hilogDataList.begin(); + reader->readPos = list.begin(); + reader->lastPos = list.begin(); reader->SetReload(false); } if (reader->isNotified) { - if (reader->readPos == hilogDataList.end()) { + if (reader->readPos == list.end()) { reader->readPos = std::next(reader->lastPos); } } @@ -154,7 +156,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) hilogBufferMutex.unlock_shared(); return true; } - while (reader->readPos != hilogDataList.end()) { + while (reader->readPos != list.end()) { reader->lastPos = reader->readPos; if (ConditionMatch(reader)) { reader->SetSendId(SENDIDA); diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 347570a..9b267e4 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -14,6 +14,7 @@ */ #include "log_collector.h" +#include "klog.h" #include "flow_control_init.h" #include @@ -21,8 +22,6 @@ #include #include #include -#include -#include #include #include #include @@ -31,58 +30,6 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; -uint16_t KmsgLevelMap(uint16_t prio) -{ - if (prio <= 2) return LOG_FATAL; - if (prio == 3) return LOG_ERR; - if (prio <= 5) return LOG_WARN; - if (prio == 6) return LOG_INFO; - return LOG_DEBUG; -} - -ssize_t LogCollector::ReadOneKmsg() -{ - ssize_t size = -1; - char kmsgTag[] = "kmsg"; - char* end = NULL; - int tagLen = sizeof(kmsgTag); - do { - size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); - } while (size < 0 && errno == EPIPE); - if (size > 0) { - uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, 10); - strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present - uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), &end, 10); - strsep(&kmsgBuffer, ";"); - int len = strlen(kmsgBuffer) + 1; - int msgLen = sizeof(HilogMsg) + tagLen + len + 1; - HilogMsg *msg = (HilogMsg*)malloc(msgLen); - msg->len = msgLen; - msg->tag_len = tagLen; - msg->type = LOG_KMSG; - msg->domain = 0xd000000; - msg->level = KmsgLevelMap(mLevel); - msg->tv_sec = timestamp / 1000000; - msg->tv_nsec = timestamp % 1000000; - msg->pid = 0; - msg->tid = 0; - strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); - strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); - InsertLogToBuffer(*msg); - free(msg); - } - return size; -} - -void LogCollector::ReadAllKmsg() -{ - ssize_t sz = ReadOneKmsg(); - while (sz > 0) { - memset(kmsgBuffer, '\0', BUFSIZ); - sz = ReadOneKmsg(); - } -} - int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) { string dropLog = to_string(ret) + " line(s) dropped!"; @@ -144,18 +91,6 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) LogCollector::LogCollector(HilogBuffer* buffer) { hilogBuffer = buffer; - kmsgBuffer = (char*)calloc(BUFSIZ, sizeof(char)); - kmsgCtl = open("/dev/kmsg", O_RDONLY); - std::thread KmsgThread([this]() { - ReadAllKmsg(); - }); - KmsgThread.detach(); -} - -LogCollector::~LogCollector() -{ - close(kmsgCtl); - free(kmsgBuffer); } void LogCollector::operator()() diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index c0398e3..17455db 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -12,9 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#define DEBUG 1 #include #include #include +#include #include #include @@ -22,6 +24,7 @@ #include "log_querier.h" #include "hilog_input_socket_server.h" #include "log_collector.h" +#include "klog.h" #include "flow_control_init.h" #ifdef DEBUG @@ -84,11 +87,14 @@ int HilogdEntry(int argc, char* argv[]) server.RunServingThread(); } - std::thread startupCheckThread([&hilogBuffer]() { + KLog klog(logCollector); + auto klogThread = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); + std::cout << "Started Klog thread!" << std::endl; + + auto startupCheckThread = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); logQuerier->RestorePersistJobs(hilogBuffer); }); - startupCheckThread.detach(); CmdExecutor cmdExecutor(&hilogBuffer); cmdExecutor.StartCmdExecutorThread(); diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index bc53bef..f733ebf 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -35,7 +35,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -constexpr int DEFAULT_LOG_TYPE = 1< Date: Wed, 15 Sep 2021 12:19:42 +0800 Subject: [PATCH 11/31] Fix compilation error Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/log_collector.h | 2 +- services/hilogd/log_collector.cpp | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 05e7884..1eaa729 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -32,7 +32,7 @@ public: #else static int onDataRecv(ucred cred, char *data, unsigned int dataLen); #endif - ~LogCollector(); + ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; }; diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 244ba8c..9b267e4 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -93,12 +93,6 @@ LogCollector::LogCollector(HilogBuffer* buffer) hilogBuffer = buffer; } -LogCollector::~LogCollector() -{ - close(kmsgCtl); - free(kmsgBuffer); -} - void LogCollector::operator()() { HilogInputSocketServer server(onDataRecv); -- Gitee From ecb60b2aa966428fa9e17efcf809677433106d97 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 15 Sep 2021 15:25:03 +0800 Subject: [PATCH 12/31] Fix incorrect log level of kmsg Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/klog.h | 2 +- services/hilogd/klog.cpp | 27 ++++++++++++++++++--------- services/hilogd/log_buffer.cpp | 5 +++-- services/hilogd/log_collector.cpp | 2 +- services/hilogd/main.cpp | 14 ++++++-------- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/services/hilogd/include/klog.h b/services/hilogd/include/klog.h index f001cd4..d80fb76 100644 --- a/services/hilogd/include/klog.h +++ b/services/hilogd/include/klog.h @@ -29,7 +29,7 @@ public: void Start(); private: int kmsgCtl = -1; - char* kmsgBuffer; + char *kmsgBuffer; const LogCollector& collector; }; } // namespace HiviewDFX diff --git a/services/hilogd/klog.cpp b/services/hilogd/klog.cpp index cdc37fb..465bf52 100644 --- a/services/hilogd/klog.cpp +++ b/services/hilogd/klog.cpp @@ -14,6 +14,7 @@ */ #include "klog.h" +#include "hilog/log.h" #include #include @@ -28,11 +29,20 @@ namespace OHOS { namespace HiviewDFX { using namespace std; +#define CUBIC_CUBIC 1000000 +#define CUBIC 1000 +#define DEC 10 +// Avoid name collision between sys/syslog.h and our log_c.h +#undef LOG_FATAL +#undef LOG_ERR +#undef LOG_WARN +#undef LOG_INFO +#undef LOG_DEBUG uint16_t KmsgLevelMap(uint16_t prio) { if (prio <= 2) return LOG_FATAL; - if (prio == 3) return LOG_ERR; + if (prio == 3) return LOG_ERROR; if (prio <= 5) return LOG_WARN; if (prio == 6) return LOG_INFO; return LOG_DEBUG; @@ -42,16 +52,15 @@ ssize_t KLog::ReadOneKmsg() { ssize_t size = -1; char kmsgTag[] = "kmsg"; - char* end = NULL; int tagLen = sizeof(kmsgTag); do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); if (size > 0) { - uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, 10); + uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, DEC); strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present - uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), &end, 10); - strsep(&kmsgBuffer, ";"); + uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), NULL, DEC); + strsep(&kmsgBuffer, ";"); // Kmsg flag is not needed at present int len = strlen(kmsgBuffer) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); @@ -60,8 +69,8 @@ ssize_t KLog::ReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xd000000; msg->level = KmsgLevelMap(mLevel); - msg->tv_sec = timestamp / 1000000; - msg->tv_nsec = (timestamp % 1000000) * 1000; + msg->tv_sec = timestamp / CUBIC_CUBIC; + msg->tv_nsec = (timestamp % CUBIC_CUBIC) * CUBIC; msg->pid = 0; msg->tid = 0; strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); @@ -82,7 +91,7 @@ int KLog::ReadAllKmsg() } ssize_t sz = ReadOneKmsg(); while (sz > 0) { - memset(kmsgBuffer, '\0', BUFSIZ); + memset_s(kmsgBuffer, BUFSIZ, '\0', BUFSIZ); sz = ReadOneKmsg(); } return 1; @@ -93,7 +102,7 @@ void KLog::Start() std::thread KmsgThread([this]() { ReadAllKmsg(); }); - KmsgThread.join(); + KmsgThread.join(); } KLog::~KLog() diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 4e26c46..1ffba79 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -58,7 +58,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) return 0; } - std::list& list = (unlikely(msg.type == LOG_KMSG)) ? hilogKlogList: hilogDataList; + std::list& list = (unlikely(msg.type == LOG_KMSG)) ? hilogKlogList : hilogDataList; // Delete old entries when full if (eleSize + sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { hilogBufferMutex.lock(); @@ -126,7 +126,8 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) bool HilogBuffer::Query(std::shared_ptr reader) { - std::list& list = (unlikely((reader->queryCondition.types) == 0b01 << LOG_KMSG)) ? hilogKlogList : hilogDataList; + uint16_t qTypes = reader->queryCondition.types; + std::list& list = (unlikely(qTypes == (0b01 << LOG_KMSG))) ? hilogKlogList : hilogDataList; hilogBufferMutex.lock_shared(); if (reader->GetReload()) { reader->readPos = list.begin(); diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 9b267e4..09d4737 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -19,12 +19,12 @@ #include #include +#include #include #include #include #include #include -#include namespace OHOS { namespace HiviewDFX { using namespace std; diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index b434d89..deab5b7 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -88,17 +88,15 @@ int HilogdEntry(int argc, char* argv[]) } KLog klog(logCollector); - auto klogThread = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); - std::cout << "Started Klog thread!" << std::endl; - - auto startupCheckThread = std::async(std::launch::async, [&hilogBuffer]() { + auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); + auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); logQuerier->RestorePersistJobs(hilogBuffer); }); - - CmdExecutor cmdExecutor(&hilogBuffer); - cmdExecutor.StartCmdExecutorThread(); - + auto cmdExecutorTask = std::async(std::launch::async, [&hilogBuffer]() { + CmdExecutor cmdExecutor(&hilogBuffer); + cmdExecutor.StartCmdExecutorThread(); + }); return 0; } } // namespace HiviewDFX -- Gitee From bee1408b91e04940215bf944a1190aa1f0a2bce2 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 15 Sep 2021 16:22:29 +0800 Subject: [PATCH 13/31] Fix compilation error Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/klog.cpp | 8 ++++---- services/hilogd/main.cpp | 6 +++--- services/hilogtool/log_controller.cpp | 2 +- services/hilogtool/main.cpp | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/services/hilogd/klog.cpp b/services/hilogd/klog.cpp index 465bf52..2af35f2 100644 --- a/services/hilogd/klog.cpp +++ b/services/hilogd/klog.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -25,13 +26,12 @@ #include #include #include -#include namespace OHOS { namespace HiviewDFX { using namespace std; -#define CUBIC_CUBIC 1000000 -#define CUBIC 1000 -#define DEC 10 +constexpr int CUBIC_CUBIC = 1000000; +constexpr int CUBIC = 1000; +constexpr int DEC = 10; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index deab5b7..10f9573 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -54,7 +54,7 @@ static void SigHandler(int sig) } } -int HilogdEntry(int argc, char* argv[]) +int HilogdEntry() { HilogBuffer hilogBuffer; @@ -102,8 +102,8 @@ int HilogdEntry(int argc, char* argv[]) } // namespace HiviewDFX } // namespace OHOS -int main(int argc, char* argv[]) +int main() { - OHOS::HiviewDFX::HilogdEntry(argc, argv); + OHOS::HiviewDFX::HilogdEntry(); return 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 4b170f9..f810c54 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -16,11 +16,11 @@ #include "log_controller.h" #include +#include #include #include #include #include -#include #include #include "hilog/log.h" #include "hilog_common.h" diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 5b1180b..be53430 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -16,11 +16,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include -- Gitee From f4ad71010e9200ed660ab77a02cb59cf4b9a0b13 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Wed, 15 Sep 2021 16:22:29 +0800 Subject: [PATCH 14/31] Fix compilation error Signed-off-by: Wu Shangwei <2826256824@qq.com> --- .../native/innerkits/include/hilog/log_c.h | 2 +- services/hilogd/BUILD.gn | 2 +- .../hilogd/include/{klog.h => log_kmsg.h} | 4 +-- services/hilogd/log_collector.cpp | 2 +- services/hilogd/{klog.cpp => log_kmsg.cpp} | 36 +++++++++++++------ services/hilogd/main.cpp | 8 ++--- services/hilogtool/log_controller.cpp | 2 +- services/hilogtool/main.cpp | 2 +- 8 files changed, 36 insertions(+), 22 deletions(-) rename services/hilogd/include/{klog.h => log_kmsg.h} (96%) rename services/hilogd/{klog.cpp => log_kmsg.cpp} (83%) diff --git a/interfaces/native/innerkits/include/hilog/log_c.h b/interfaces/native/innerkits/include/hilog/log_c.h index 9def3fd..a32b3ed 100644 --- a/interfaces/native/innerkits/include/hilog/log_c.h +++ b/interfaces/native/innerkits/include/hilog/log_c.h @@ -38,9 +38,9 @@ typedef enum { LOG_TYPE_MIN = 0, LOG_APP = 0, LOG_INIT = 1, - LOG_KMSG = 2, // Used by core service, framework. LOG_CORE = 3, + LOG_KMSG = 4, LOG_TYPE_MAX } LogType; diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 7670a53..6c6736a 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -24,7 +24,7 @@ ohos_executable("hilogd") { "cmd_executor.cpp", "flow_control_init.cpp", "log_buffer.cpp", - "klog.cpp", + "log_kmsg.cpp", "log_collector.cpp", "log_compress.cpp", "log_persister.cpp", diff --git a/services/hilogd/include/klog.h b/services/hilogd/include/log_kmsg.h similarity index 96% rename from services/hilogd/include/klog.h rename to services/hilogd/include/log_kmsg.h index d80fb76..f8dd143 100644 --- a/services/hilogd/include/klog.h +++ b/services/hilogd/include/log_kmsg.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef KLOG_H -#define KLOG_H +#ifndef LOG_KMSG_H +#define LOG_KMSG_H #include "log_collector.h" diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 09d4737..4bfcfd2 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -14,7 +14,7 @@ */ #include "log_collector.h" -#include "klog.h" +#include "log_kmsg.h" #include "flow_control_init.h" #include diff --git a/services/hilogd/klog.cpp b/services/hilogd/log_kmsg.cpp similarity index 83% rename from services/hilogd/klog.cpp rename to services/hilogd/log_kmsg.cpp index 465bf52..b31e3c8 100644 --- a/services/hilogd/klog.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -13,11 +13,12 @@ * limitations under the License. */ -#include "klog.h" +#include "log_kmsg.h" #include "hilog/log.h" #include #include +#include #include #include #include @@ -25,13 +26,12 @@ #include #include #include -#include namespace OHOS { namespace HiviewDFX { using namespace std; -#define CUBIC_CUBIC 1000000 -#define CUBIC 1000 -#define DEC 10 +constexpr int CUBIC_CUBIC = 1000000; +constexpr int CUBIC = 1000; +constexpr int DEC = 10; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR @@ -41,11 +41,25 @@ using namespace std; uint16_t KmsgLevelMap(uint16_t prio) { - if (prio <= 2) return LOG_FATAL; - if (prio == 3) return LOG_ERROR; - if (prio <= 5) return LOG_WARN; - if (prio == 6) return LOG_INFO; - return LOG_DEBUG; + uint16_t level; + switch (prio) { + case 0:case 1:case 2: + level = LOG_FATAL; + break; + case 3: + level = LOG_ERROR; + break; + case 4:case 5: + level = LOG_WARN; + break; + case 6: + level = LOG_INFO; + break; + default: + level = LOG_DEBUG; + break; + } + return level; } ssize_t KLog::ReadOneKmsg() @@ -67,7 +81,7 @@ ssize_t KLog::ReadOneKmsg() msg->len = msgLen; msg->tag_len = tagLen; msg->type = LOG_KMSG; - msg->domain = 0xd000000; + msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); msg->tv_sec = timestamp / CUBIC_CUBIC; msg->tv_nsec = (timestamp % CUBIC_CUBIC) * CUBIC; diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index deab5b7..ad2d8ea 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -24,7 +24,7 @@ #include "log_querier.h" #include "hilog_input_socket_server.h" #include "log_collector.h" -#include "klog.h" +#include "log_kmsg.h" #include "flow_control_init.h" #ifdef DEBUG @@ -54,7 +54,7 @@ static void SigHandler(int sig) } } -int HilogdEntry(int argc, char* argv[]) +int HilogdEntry() { HilogBuffer hilogBuffer; @@ -102,8 +102,8 @@ int HilogdEntry(int argc, char* argv[]) } // namespace HiviewDFX } // namespace OHOS -int main(int argc, char* argv[]) +int main() { - OHOS::HiviewDFX::HilogdEntry(argc, argv); + OHOS::HiviewDFX::HilogdEntry(); return 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 4b170f9..f810c54 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -16,11 +16,11 @@ #include "log_controller.h" #include +#include #include #include #include #include -#include #include #include "hilog/log.h" #include "hilog_common.h" diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 5b1180b..be53430 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -16,11 +16,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include -- Gitee From cfad09ca43089bcd754a5549e5e7e14c43ddf2fe Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 23 Sep 2021 10:07:20 +0000 Subject: [PATCH 15/31] add BootTime Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 5 ++- services/hilogd/include/log_kmsg.h | 1 + services/hilogd/log_kmsg.cpp | 39 +++++++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index e6e365f..135055c 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -44,7 +44,10 @@ #define ONE_MB (1UL<<20) #define ONE_GB (1UL<<30) #define ONE_TB (1ULL<<40) - +static const long long NS = 1000000000LL; +static const long long US = 1000000LL; +static const long long NS2US = 1000LL; +static const long long NS2MS = 1000000LL; const uint32_t MAX_BUFFER_SIZE = 1UL<<30; const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; const int MSG_MAX_LEN = 2048; diff --git a/services/hilogd/include/log_kmsg.h b/services/hilogd/include/log_kmsg.h index f8dd143..3d38799 100644 --- a/services/hilogd/include/log_kmsg.h +++ b/services/hilogd/include/log_kmsg.h @@ -27,6 +27,7 @@ public: ssize_t ReadOneKmsg(); int ReadAllKmsg(); void Start(); + void BootTime(); private: int kmsgCtl = -1; char *kmsgBuffer; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index b31e3c8..ec2a68d 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -24,14 +24,15 @@ #include #include #include +#include #include #include namespace OHOS { namespace HiviewDFX { using namespace std; -constexpr int CUBIC_CUBIC = 1000000; -constexpr int CUBIC = 1000; + constexpr int DEC = 10; +static struct timeval bootTime; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR @@ -39,6 +40,12 @@ constexpr int DEC = 10; #undef LOG_INFO #undef LOG_DEBUG +static uint64_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) +{ + return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + + ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); +} + uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -62,11 +69,35 @@ uint16_t KmsgLevelMap(uint16_t prio) return level; } +void KLog::BootTime() { + int uptimeFd = 0; + char* uptimeBuf = (char*)calloc(1024, sizeof(char)); + ssize_t uptimeCnt = 0; + struct timeval currentTime; + if ((uptimeFd = open ("/proc/uptime", O_RDONLY)) == -1) + return; + if ((uptimeCnt = read (uptimeFd, uptimeBuf, 1024)) <= 0) { + close(uptimeFd); + return; + } + close(uptimeFd); + gettimeofday(¤tTime, NULL); + if (strsep(&uptimeBuf, " ") != NULL) { + uint64_t uptime = strtoumax(strsep(&uptimeBuf, " "), NULL, DEC); + bootTime.tv_sec = uptime / US; + bootTime.tv_usec = uptime % US; + uint64_t tdiff = kmsgTimeDeviation(¤tTime, &bootTime); + bootTime.tv_sec = tdiff / US; + bootTime.tv_usec = tdiff % US; + } +} + ssize_t KLog::ReadOneKmsg() { ssize_t size = -1; char kmsgTag[] = "kmsg"; int tagLen = sizeof(kmsgTag); + BootTime(); do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); @@ -83,8 +114,8 @@ ssize_t KLog::ReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); - msg->tv_sec = timestamp / CUBIC_CUBIC; - msg->tv_nsec = (timestamp % CUBIC_CUBIC) * CUBIC; + msg->tv_sec = bootTime.tv_sec + timestamp / US; + msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; msg->pid = 0; msg->tid = 0; strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); -- Gitee From 507206a065c54922b13784d29baf674e91eef09c Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Fri, 24 Sep 2021 00:26:10 +0800 Subject: [PATCH 16/31] Sniff pid and tag Signed-off-by: Wu Shangwei <2826256824@qq.com> --- .../native/innerkits/include/hilog/log_c.h | 2 +- services/hilogd/log_kmsg.cpp | 37 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/interfaces/native/innerkits/include/hilog/log_c.h b/interfaces/native/innerkits/include/hilog/log_c.h index a32b3ed..9def3fd 100644 --- a/interfaces/native/innerkits/include/hilog/log_c.h +++ b/interfaces/native/innerkits/include/hilog/log_c.h @@ -38,9 +38,9 @@ typedef enum { LOG_TYPE_MIN = 0, LOG_APP = 0, LOG_INIT = 1, + LOG_KMSG = 2, // Used by core service, framework. LOG_CORE = 3, - LOG_KMSG = 4, LOG_TYPE_MAX } LogType; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index ec2a68d..32a9775 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -42,10 +42,21 @@ static struct timeval bootTime; static uint64_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) { - return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + + return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); } +uint32_t ParsePid(const char *str, bool *found) +{ + if (strncmp(str,"[pid=",5) != 0) { + *found = false; + return 0; + } + uint32_t pid = strtoul(str + 5, NULL, DEC); + *found = true; + return pid; +} + uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -95,8 +106,8 @@ void KLog::BootTime() { ssize_t KLog::ReadOneKmsg() { ssize_t size = -1; - char kmsgTag[] = "kmsg"; - int tagLen = sizeof(kmsgTag); + // char kmsgTag[] = "kmsg"; + // int tagLen = sizeof(kmsgTag); BootTime(); do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); @@ -106,6 +117,20 @@ ssize_t KLog::ReadOneKmsg() strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), NULL, DEC); strsep(&kmsgBuffer, ";"); // Kmsg flag is not needed at present + uint32_t mpid = 0; + bool found = false; + mpid = ParsePid(kmsgBuffer, &found); + if (found) strsep(&kmsgBuffer, "]"); + char mtag[MAX_TAG_LEN] = {'\0'}; + int tagLen = 0; + if (kmsgBuffer[0] == '[') { + char* sep = strsep(&kmsgBuffer, "]"); + strncpy_s(mtag, MAX_TAG_LEN - 1, sep, strlen(sep)); + tagLen = strlen(mtag); + } else { + strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", 4); + tagLen = 4; + } int len = strlen(kmsgBuffer) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); @@ -116,9 +141,9 @@ ssize_t KLog::ReadOneKmsg() msg->level = KmsgLevelMap(mLevel); msg->tv_sec = bootTime.tv_sec + timestamp / US; msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; - msg->pid = 0; - msg->tid = 0; - strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); + msg->pid = mpid; + msg->tid = mpid; + strncpy_s(msg->tag, tagLen, mtag, tagLen); strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); collector.InsertLogToBuffer(*msg); free(msg); -- Gitee From b049e9ba43ff2dc652b41a97f051b429516ce41a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 23 Sep 2021 10:07:20 +0000 Subject: [PATCH 17/31] add BootTime Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/format.cpp | 3 -- frameworks/native/include/hilog_common.h | 5 ++- services/hilogd/include/log_kmsg.h | 1 + services/hilogd/log_kmsg.cpp | 39 +++++++++++++++++++++--- services/hilogd/log_querier.cpp | 2 +- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/frameworks/native/format.cpp b/frameworks/native/format.cpp index b814ed7..900be11 100644 --- a/frameworks/native/format.cpp +++ b/frameworks/native/format.cpp @@ -30,9 +30,6 @@ static const int HILOG_COLOR_GREEN = 40; static const int HILOG_COLOR_ORANGE = 166; static const int HILOG_COLOR_RED = 196; static const int HILOG_COLOR_YELLOW = 226; -static const long long NS = 1000000000LL; -static const long long NS2US = 1000LL; -static const long long NS2MS = 1000000LL; const char* ParsedFromLevel(uint16_t level) { diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index e6e365f..135055c 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -44,7 +44,10 @@ #define ONE_MB (1UL<<20) #define ONE_GB (1UL<<30) #define ONE_TB (1ULL<<40) - +static const long long NS = 1000000000LL; +static const long long US = 1000000LL; +static const long long NS2US = 1000LL; +static const long long NS2MS = 1000000LL; const uint32_t MAX_BUFFER_SIZE = 1UL<<30; const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; const int MSG_MAX_LEN = 2048; diff --git a/services/hilogd/include/log_kmsg.h b/services/hilogd/include/log_kmsg.h index f8dd143..3d38799 100644 --- a/services/hilogd/include/log_kmsg.h +++ b/services/hilogd/include/log_kmsg.h @@ -27,6 +27,7 @@ public: ssize_t ReadOneKmsg(); int ReadAllKmsg(); void Start(); + void BootTime(); private: int kmsgCtl = -1; char *kmsgBuffer; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index b31e3c8..ec2a68d 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -24,14 +24,15 @@ #include #include #include +#include #include #include namespace OHOS { namespace HiviewDFX { using namespace std; -constexpr int CUBIC_CUBIC = 1000000; -constexpr int CUBIC = 1000; + constexpr int DEC = 10; +static struct timeval bootTime; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR @@ -39,6 +40,12 @@ constexpr int DEC = 10; #undef LOG_INFO #undef LOG_DEBUG +static uint64_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) +{ + return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + + ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); +} + uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -62,11 +69,35 @@ uint16_t KmsgLevelMap(uint16_t prio) return level; } +void KLog::BootTime() { + int uptimeFd = 0; + char* uptimeBuf = (char*)calloc(1024, sizeof(char)); + ssize_t uptimeCnt = 0; + struct timeval currentTime; + if ((uptimeFd = open ("/proc/uptime", O_RDONLY)) == -1) + return; + if ((uptimeCnt = read (uptimeFd, uptimeBuf, 1024)) <= 0) { + close(uptimeFd); + return; + } + close(uptimeFd); + gettimeofday(¤tTime, NULL); + if (strsep(&uptimeBuf, " ") != NULL) { + uint64_t uptime = strtoumax(strsep(&uptimeBuf, " "), NULL, DEC); + bootTime.tv_sec = uptime / US; + bootTime.tv_usec = uptime % US; + uint64_t tdiff = kmsgTimeDeviation(¤tTime, &bootTime); + bootTime.tv_sec = tdiff / US; + bootTime.tv_usec = tdiff % US; + } +} + ssize_t KLog::ReadOneKmsg() { ssize_t size = -1; char kmsgTag[] = "kmsg"; int tagLen = sizeof(kmsgTag); + BootTime(); do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); @@ -83,8 +114,8 @@ ssize_t KLog::ReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); - msg->tv_sec = timestamp / CUBIC_CUBIC; - msg->tv_nsec = (timestamp % CUBIC_CUBIC) * CUBIC; + msg->tv_sec = bootTime.tv_sec + timestamp / US; + msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; msg->pid = 0; msg->tid = 0; strncpy_s(msg->tag, tagLen, kmsgTag, tagLen); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 3575b5d..2a604e1 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -44,7 +44,7 @@ using namespace std; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = HILOG_FILE_DIR; constexpr int DEFAULT_LOG_LEVEL = 1< Date: Fri, 24 Sep 2021 09:10:29 +0000 Subject: [PATCH 18/31] add kmsg private && kmsg forbidden Signed-off-by: aajwy <13051180828@163.com> --- adapter/properties.cpp | 19 ++++++++++++++++++- adapter/properties.h | 2 ++ frameworks/native/include/hilog_common.h | 1 + frameworks/native/include/hilogtool_msg.h | 2 ++ services/hilogd/log_buffer.cpp | 2 +- services/hilogd/log_kmsg.cpp | 16 ++++++++++------ services/hilogd/log_querier.cpp | 16 ++++++++++++++++ services/hilogtool/include/hilogtool.h | 1 + services/hilogtool/log_controller.cpp | 16 +++++++++++++++- services/hilogtool/log_display.cpp | 8 +++++--- services/hilogtool/main.cpp | 18 ++++++++++++++++++ 11 files changed, 89 insertions(+), 12 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 939b0ea..f1b3f94 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -47,6 +47,7 @@ static pthread_mutex_t g_persistDebugLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_privateLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_kmsgLock = PTHREAD_MUTEX_INITIALIZER; using PropertyCache = struct { const void* pinfo; @@ -115,6 +116,9 @@ string GetPropertyName(uint32_t propType) case PROP_SINGLE_DEBUG: key = "hilog.debug.on"; break; + case PROP_KMSG: + key = "hilog.kmsg.on"; + break; case PROP_PERSIST_DEBUG: key = "persist.sys.hilog.debug.on"; break; @@ -141,6 +145,8 @@ static int LockByProp(uint32_t propType) return pthread_mutex_trylock(&g_tagLevelLock); case PROP_SINGLE_DEBUG: return pthread_mutex_trylock(&g_debugLock); + case PROP_KMSG: + return pthread_mutex_trylock(&g_kmsgLock); case PROP_PERSIST_DEBUG: return pthread_mutex_trylock(&g_persistDebugLock); default: @@ -172,6 +178,9 @@ static void UnlockByProp(uint32_t propType) case PROP_SINGLE_DEBUG: pthread_mutex_unlock(&g_debugLock); break; + case PROP_KMSG: + pthread_mutex_unlock(&g_kmsgLock); + break; case PROP_PERSIST_DEBUG: pthread_mutex_unlock(&g_persistDebugLock); break; @@ -271,6 +280,14 @@ bool IsDomainSwitchOn() return GetSwitchCache(isFirst, *switchCache, PROP_DOMAIN_FLOWCTRL, false); } +bool IsKmsgSwitchOn() +{ + static SwitchCache* switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_KMSG, false); +} + static uint16_t GetCacheLevel(char propertyChar) { uint16_t cacheLevel = LOG_LEVEL_MIN; @@ -394,4 +411,4 @@ uint16_t GetTagLevel(const string& tag) return it->second->logLevel; } } -} \ No newline at end of file +} diff --git a/adapter/properties.h b/adapter/properties.h index cebbaa1..12d2e5d 100644 --- a/adapter/properties.h +++ b/adapter/properties.h @@ -30,6 +30,7 @@ using PropType = enum { PROP_DOMAIN_LOG_LEVEL, PROP_TAG_LOG_LEVEL, PROP_SINGLE_DEBUG, + PROP_KMSG, PROP_PERSIST_DEBUG, }; @@ -44,6 +45,7 @@ bool IsPersistDebugOn(); bool IsPrivateSwitchOn(); bool IsProcessSwitchOn(); bool IsDomainSwitchOn(); +bool IsKmsgSwitchOn(); uint16_t GetGlobalLevel(); uint16_t GetDomainLevel(uint32_t domain); diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index 135055c..d248287 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -125,5 +125,6 @@ typedef enum { ERR_FLOWCTRL_SWITCH_VALUE_INVALID = -29, ERR_BUFF_SIZE_INVALID = -30, ERR_COMMAND_INVALID = -31, + ERR_KMSG_SWITCH_VALUE_INVALID = -32, } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index bf3b9f9..1123a5f 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -55,6 +55,7 @@ typedef enum { */ typedef enum { OT_PRIVATE_SWITCH = 0x01, + OT_KMSG_SWITCH, OT_LOG_LEVEL, OT_FLOW_SWITCH, } OperateType; @@ -310,6 +311,7 @@ typedef struct { typedef struct { std::string privateSwitchStr; + std::string kmsgSwitchStr; std::string flowSwitchStr; std::string logLevelStr; std::string domainStr; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 1ffba79..4a96f2c 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -127,7 +127,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) bool HilogBuffer::Query(std::shared_ptr reader) { uint16_t qTypes = reader->queryCondition.types; - std::list& list = (unlikely(qTypes == (0b01 << LOG_KMSG))) ? hilogKlogList : hilogDataList; + std::list& list = (qTypes == (0b01 << LOG_KMSG)) ? hilogKlogList : hilogDataList; hilogBufferMutex.lock_shared(); if (reader->GetReload()) { reader->readPos = list.begin(); diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index ec2a68d..065265f 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -27,12 +27,15 @@ #include #include #include +#include + namespace OHOS { namespace HiviewDFX { using namespace std; constexpr int DEC = 10; static struct timeval bootTime; + // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR @@ -40,10 +43,10 @@ static struct timeval bootTime; #undef LOG_INFO #undef LOG_DEBUG -static uint64_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) +static uint32_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) { - return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + - ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); + return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + + ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); } uint16_t KmsgLevelMap(uint16_t prio) @@ -86,7 +89,7 @@ void KLog::BootTime() { uint64_t uptime = strtoumax(strsep(&uptimeBuf, " "), NULL, DEC); bootTime.tv_sec = uptime / US; bootTime.tv_usec = uptime % US; - uint64_t tdiff = kmsgTimeDeviation(¤tTime, &bootTime); + uint32_t tdiff = kmsgTimeDeviation(¤tTime, &bootTime); bootTime.tv_sec = tdiff / US; bootTime.tv_usec = tdiff % US; } @@ -97,7 +100,7 @@ ssize_t KLog::ReadOneKmsg() ssize_t size = -1; char kmsgTag[] = "kmsg"; int tagLen = sizeof(kmsgTag); - BootTime(); + do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); @@ -114,6 +117,7 @@ ssize_t KLog::ReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); + BootTime(); msg->tv_sec = bootTime.tv_sec + timestamp / US; msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; msg->pid = 0; @@ -138,7 +142,7 @@ int KLog::ReadAllKmsg() while (sz > 0) { memset_s(kmsgBuffer, BUFSIZ, '\0', BUFSIZ); sz = ReadOneKmsg(); - } + } return 1; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 2a604e1..b4852fc 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -64,6 +64,17 @@ inline bool IsValidFileName(const std::string& strFileName) return bValid; } +inline bool logTypeForbidden(uint16_t queryTypes) +{ + if (queryTypes == (0b01 << LOG_KMSG) || (queryTypes & (0b01 << LOG_KMSG)) == 0) { + return true; + } else { + cout << "queryTypes can not contain app/core/init and kmsg at the same time,\ + try to -t app/core/init or -t kmsg separately" << endl; + return false; + } +} + LogPersisterRotator* MakeRotator(const LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; @@ -141,6 +152,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade string logPersisterPath; if (pLogPersistStartRst == nullptr) { return; + } else if (!logTypeForbidden(logReader->queryCondition.types)){ + pLogPersistStartRst->result = ERR_QUERY_TYPE_INVALID; } else if (pLogPersistStartMsg->jobId <= 0) { pLogPersistStartRst->result = ERR_LOG_PERSIST_JOBID_INVALID; } else if (pLogPersistStartMsg->fileSize < MAX_PERSISTER_BUFFER_SIZE) { @@ -463,6 +476,9 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) case LOG_QUERY_REQUEST: qRstMsg = (LogQueryRequest*) g_tempBuffer; SetCondition(logReader, *qRstMsg); + if (!logTypeForbidden(logReader->queryCondition.types)) { + return; + } HandleLogQueryRequest(logReader, *hilogBuffer); break; case NEXT_REQUEST: diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index 4bcce3a..ef74cc9 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -51,6 +51,7 @@ typedef struct { std::string fileNameArgs; std::string jobIdArgs; std::string personalArgs; + std::string kmsgArgs; std::string logClearArgs; std::string logTypeArgs; std::string domainArgs; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index f810c54..e6ff6d5 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -81,6 +81,8 @@ uint16_t GetLogType(const string& logTypeStr) logType = LOG_CORE; } else if (logTypeStr == "app") { logType = LOG_APP; + } else if (logTypeStr == "kmsg") { + logType = LOG_KMSG; } else { return 0xffff; } @@ -520,7 +522,19 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType return RET_FAIL; } break; - + case OT_KMSG_SWITCH: + key = GetPropertyName(PROP_KMSG); + if (propertyParm->kmsgSwitchStr == "on") { + PropertySet(key.c_str(), "true"); + std::cout << "hilog will store kmsg log" << std::endl; + } else if (propertyParm->kmsgSwitchStr == "off") { + PropertySet(key.c_str(), "false"); + std::cout << "hilog will not store kmsg log" << std::endl; + } else { + std::cout << ParseErrorCode(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl; + return RET_FAIL; + } + break; case OT_LOG_LEVEL: if (propertyParm->tagStr != "" && propertyParm->domainStr != "") { return RET_FAIL; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index e22465e..d9778e4 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -38,8 +38,9 @@ unordered_map errorMsg { {RET_FAIL, "Unexpected error"}, {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"}, - {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init"}, - {ERR_QUERY_TYPE_INVALID, "Query condition on both types and excluded types is undefined"}, + {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init/kmsg"}, + {ERR_QUERY_TYPE_INVALID, "Query condition on both types and excluded types is undefined or\ + queryTypes can not contain app/core/init and kmsg at the same time"}, {ERR_QUERY_LEVEL_INVALID, "Query condition on both levels and excluded levels is undefined"}, {ERR_QUERY_DOMAIN_INVALID, "Invalid domain format, a hexadecimal number is needed"}, {ERR_QUERY_TAG_INVALID, "Query condition on both tags and excluded tags is undefined"}, @@ -67,7 +68,8 @@ unordered_map errorMsg {ERR_FORMAT_INVALID, "Invalid format parameter"}, {ERR_BUFF_SIZE_INVALID, "Invalid buffer size, buffer size should be more than 0 and less than " + to_string(MAX_BUFFER_SIZE)}, - {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"} + {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"}, + {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"} }; string ParseErrorCode(ErrorCode errorCode) diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index be53430..131bdd3 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -50,6 +50,10 @@ static void Helper() " set privacy formatter feature on or off.\n" " on turn on\n" " off turn off\n" + " -k\n" + " store log type kmsg or not\n" + " on yes\n" + " off no\n" " -s, --statistics query hilogd statistic information.\n" " -S clear hilogd statistic information.\n" " -r remove the logs in hilog buffer, use -t to specify log type\n" @@ -330,6 +334,11 @@ int HilogEntry(int argc, char* argv[]) noLogOption = true; controlCount++; break; + case 'k': + context.kmsgArgs = optarg; + noLogOption = true; + controlCount++; + break; case 'r': context.logClearArgs = "clear"; noLogOption = true; @@ -526,6 +535,15 @@ int HilogEntry(int argc, char* argv[]) exit(-1); } exit(0); + } else if (context.kmsgArgs != "") { + SetPropertyParam propertyParam; + propertyParam.kmsgSwitchStr = context.kmsgArgs; + ret = SetPropertiesOp(controller, OT_KMSG_SWITCH, &propertyParam); + if (ret == RET_FAIL) { + std::cout << "set kmsg switch operation error!" << std::endl; + exit(-1); + } + exit(0); } else if (context.logClearArgs != "") { ret = LogClearOp(controller, MC_REQ_LOG_CLEAR, context.logTypeArgs); if (ret == RET_FAIL) { -- Gitee From 019314d4f8bf0f68f172508f856910f06320540b Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 26 Sep 2021 00:55:28 +0800 Subject: [PATCH 19/31] Use property to determine if read kmsg on start Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Ib6a6757c245e24dcd409428ba32aec600f929b42 --- services/hilogd/main.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index ad2d8ea..2aa54f2 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -26,6 +26,7 @@ #include "log_collector.h" #include "log_kmsg.h" #include "flow_control_init.h" +#include "properties.h" #ifdef DEBUG #include @@ -87,8 +88,10 @@ int HilogdEntry() server.RunServingThread(); } - KLog klog(logCollector); - auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); + if (IsKmsgSwitchOn()) { + KLog klog(logCollector); + auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); + } auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); logQuerier->RestorePersistJobs(hilogBuffer); -- Gitee From ad23295fe9dff5fdb97e18d793f1c3c2ce8ee9e9 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 26 Sep 2021 01:05:43 +0800 Subject: [PATCH 20/31] Fix compilation error Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I465f2bdf880126de0752fdc52b6ee935b177ec1a --- services/hilogd/log_kmsg.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index ff93efa..326e7b3 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -42,6 +42,17 @@ static struct timeval bootTime; #undef LOG_INFO #undef LOG_DEBUG +uint32_t ParsePid(const char *str, bool *found) +{ + if (strncmp(str,"[pid=",5) != 0) { + *found = false; + return 0; + } + uint32_t pid = strtoul(str + 5, NULL, DEC); + *found = true; + return pid; +} + static uint32_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) { return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + @@ -96,10 +107,7 @@ void KLog::BootTime() { ssize_t KLog::ReadOneKmsg() { - ssize_t size = -1; - char kmsgTag[] = "kmsg"; - int tagLen = sizeof(kmsgTag); - + ssize_t size = -1; do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); -- Gitee From 4beba51ca692d83be7e835807538e64a0bff770f Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 26 Sep 2021 15:10:41 +0800 Subject: [PATCH 21/31] Fix kmsg hang problem Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I13d9e0ca4b45c750649dc8af586bccc30893ac45 --- services/hilogd/include/log_kmsg.h | 5 ++-- services/hilogd/log_kmsg.cpp | 44 +++++++++++++++++++----------- services/hilogd/main.cpp | 7 ++--- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/services/hilogd/include/log_kmsg.h b/services/hilogd/include/log_kmsg.h index 3d38799..dc250c9 100644 --- a/services/hilogd/include/log_kmsg.h +++ b/services/hilogd/include/log_kmsg.h @@ -24,8 +24,9 @@ public: KLog(const LogCollector& collector) : collector(collector) {}; KLog(const LogCollector&&) = delete; ~KLog(); - ssize_t ReadOneKmsg(); - int ReadAllKmsg(); + ssize_t LinuxReadOneKmsg(); + int LinuxReadAllKmsg(); + void ReadAllKmsg(); void Start(); void BootTime(); private: diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 326e7b3..dd2c901 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -35,6 +35,7 @@ using namespace std; constexpr int DEC = 10; static struct timeval bootTime; + // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL #undef LOG_ERR @@ -105,32 +106,34 @@ void KLog::BootTime() { } } -ssize_t KLog::ReadOneKmsg() +ssize_t KLog::LinuxReadOneKmsg() { ssize_t size = -1; do { size = read(kmsgCtl, kmsgBuffer, BUFSIZ - 1); } while (size < 0 && errno == EPIPE); if (size > 0) { - uint16_t mLevel = strtoul(strsep(&kmsgBuffer, ","), NULL, DEC); - strsep(&kmsgBuffer, ","); // Kmsg seq is not needed at present - uint64_t timestamp = strtoumax(strsep(&kmsgBuffer, ","), NULL, DEC); - strsep(&kmsgBuffer, ";"); // Kmsg flag is not needed at present + char* mtag = (char*)calloc(MAX_TAG_LEN, sizeof(char)); + char* saveptr = NULL; + uint16_t mLevel = strtoul(strtok_r(kmsgBuffer, ",", &saveptr), NULL, DEC); + strtok_r(NULL, ",",&saveptr); // Kmsg seq is not needed at present + uint64_t timestamp = strtoumax(strtok_r(NULL, ",",&saveptr), NULL, DEC); + strtok_r(NULL, ";",&saveptr); // Kmsg flag is not needed at present + uint32_t mpid = 0; bool found = false; - mpid = ParsePid(kmsgBuffer, &found); - if (found) strsep(&kmsgBuffer, "]"); - char mtag[MAX_TAG_LEN] = {'\0'}; + mpid = ParsePid(saveptr, &found); + if (found) strtok_r(NULL, "]", &saveptr); int tagLen = 0; - if (kmsgBuffer[0] == '[') { - char* sep = strsep(&kmsgBuffer, "]"); + if (saveptr[0] == '[') { + char* sep = strtok_r(NULL, "]", &saveptr); strncpy_s(mtag, MAX_TAG_LEN - 1, sep, strlen(sep)); - tagLen = strlen(mtag); + tagLen = strlen(sep); } else { strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", 4); tagLen = 4; } - int len = strlen(kmsgBuffer) + 1; + int len = strlen(saveptr) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); msg->len = msgLen; @@ -143,14 +146,15 @@ ssize_t KLog::ReadOneKmsg() msg->pid = mpid; msg->tid = mpid; strncpy_s(msg->tag, tagLen, mtag, tagLen); - strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, kmsgBuffer, len); + strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, saveptr, len); collector.InsertLogToBuffer(*msg); + free(mtag); free(msg); } return size; } -int KLog::ReadAllKmsg() +int KLog::LinuxReadAllKmsg() { kmsgBuffer = (char*)calloc(BUFSIZ, sizeof(char)); kmsgCtl = open("/dev/kmsg", O_RDONLY); @@ -158,14 +162,22 @@ int KLog::ReadAllKmsg() std::cout << "Cannot open kmsg! Err=" << strerror(errno) << std::endl; return -1; } - ssize_t sz = ReadOneKmsg(); + ssize_t sz = LinuxReadOneKmsg(); while (sz > 0) { memset_s(kmsgBuffer, BUFSIZ, '\0', BUFSIZ); - sz = ReadOneKmsg(); + sz = LinuxReadOneKmsg(); } return 1; } +void KLog::ReadAllKmsg() +{ + #ifdef __linux__ + std::cout << "Linux!" << std::endl; + LinuxReadAllKmsg(); + #endif +} + void KLog::Start() { std::thread KmsgThread([this]() { diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index 2aa54f2..1e89ee5 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -92,14 +92,13 @@ int HilogdEntry() KLog klog(logCollector); auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); } + auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); logQuerier->RestorePersistJobs(hilogBuffer); }); - auto cmdExecutorTask = std::async(std::launch::async, [&hilogBuffer]() { - CmdExecutor cmdExecutor(&hilogBuffer); - cmdExecutor.StartCmdExecutorThread(); - }); + CmdExecutor cmdExecutor(&hilogBuffer); + cmdExecutor.StartCmdExecutorThread(); return 0; } } // namespace HiviewDFX -- Gitee From 4f73b972054cd9d51851328c092d52045174f550 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 26 Sep 2021 15:57:46 +0800 Subject: [PATCH 22/31] Kmsg: fix display of tag parse Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: If4d4153685050a9c784b6f993e16cf424641eec8 --- services/hilogd/log_kmsg.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index dd2c901..f49eb89 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -54,7 +54,7 @@ uint32_t ParsePid(const char *str, bool *found) return pid; } -static uint32_t kmsgTimeDeviation(struct timeval *t1, struct timeval *t2) +static uint32_t KmsgTimeDeviation(struct timeval *t1, struct timeval *t2) { return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); @@ -100,7 +100,7 @@ void KLog::BootTime() { uint64_t uptime = strtoumax(strsep(&uptimeBuf, " "), NULL, DEC); bootTime.tv_sec = uptime / US; bootTime.tv_usec = uptime % US; - uint32_t tdiff = kmsgTimeDeviation(¤tTime, &bootTime); + uint32_t tdiff = KmsgTimeDeviation(¤tTime, &bootTime); bootTime.tv_sec = tdiff / US; bootTime.tv_usec = tdiff % US; } @@ -127,7 +127,7 @@ ssize_t KLog::LinuxReadOneKmsg() int tagLen = 0; if (saveptr[0] == '[') { char* sep = strtok_r(NULL, "]", &saveptr); - strncpy_s(mtag, MAX_TAG_LEN - 1, sep, strlen(sep)); + strncpy_s(mtag, MAX_TAG_LEN - 1, sep + 1, strlen(sep)); tagLen = strlen(sep); } else { strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", 4); @@ -173,7 +173,7 @@ int KLog::LinuxReadAllKmsg() void KLog::ReadAllKmsg() { #ifdef __linux__ - std::cout << "Linux!" << std::endl; + std::cout << "Platform: Linux" << std::endl; LinuxReadAllKmsg(); #endif } -- Gitee From fc1adbd7dc2399c968fa2f99b753972fd8637399 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 26 Sep 2021 21:25:54 +0800 Subject: [PATCH 23/31] Fix tag display Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Idd00fc81ce2c6342662521c158fb377690019b88 --- services/hilogd/log_kmsg.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index f49eb89..8664fe4 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -88,7 +88,7 @@ void KLog::BootTime() { char* uptimeBuf = (char*)calloc(1024, sizeof(char)); ssize_t uptimeCnt = 0; struct timeval currentTime; - if ((uptimeFd = open ("/proc/uptime", O_RDONLY)) == -1) + if ((uptimeFd = open ("/proc/uptime", O_RDONLY | O_NONBLOCK)) == -1) return; if ((uptimeCnt = read (uptimeFd, uptimeBuf, 1024)) <= 0) { close(uptimeFd); @@ -96,14 +96,16 @@ void KLog::BootTime() { } close(uptimeFd); gettimeofday(¤tTime, NULL); - if (strsep(&uptimeBuf, " ") != NULL) { - uint64_t uptime = strtoumax(strsep(&uptimeBuf, " "), NULL, DEC); + char* tempPtr = nullptr; + if (strtok_r(uptimeBuf, " ", &tempPtr) != NULL) { + uint64_t uptime = strtoumax(strtok_r(NULL, " ", &tempPtr), NULL, DEC); bootTime.tv_sec = uptime / US; bootTime.tv_usec = uptime % US; uint32_t tdiff = KmsgTimeDeviation(¤tTime, &bootTime); bootTime.tv_sec = tdiff / US; bootTime.tv_usec = tdiff % US; } + free(uptimeBuf); } ssize_t KLog::LinuxReadOneKmsg() @@ -114,7 +116,7 @@ ssize_t KLog::LinuxReadOneKmsg() } while (size < 0 && errno == EPIPE); if (size > 0) { char* mtag = (char*)calloc(MAX_TAG_LEN, sizeof(char)); - char* saveptr = NULL; + char* saveptr = nullptr; uint16_t mLevel = strtoul(strtok_r(kmsgBuffer, ",", &saveptr), NULL, DEC); strtok_r(NULL, ",",&saveptr); // Kmsg seq is not needed at present uint64_t timestamp = strtoumax(strtok_r(NULL, ",",&saveptr), NULL, DEC); @@ -127,25 +129,29 @@ ssize_t KLog::LinuxReadOneKmsg() int tagLen = 0; if (saveptr[0] == '[') { char* sep = strtok_r(NULL, "]", &saveptr); - strncpy_s(mtag, MAX_TAG_LEN - 1, sep + 1, strlen(sep)); - tagLen = strlen(sep); + if (sep != NULL) { + tagLen = strlen(sep) - 1; + strncpy_s(mtag, MAX_TAG_LEN - 1, sep + 1, tagLen); + } } else { - strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", 4); - tagLen = 4; + char kmsgStr[] = "kmsg"; + tagLen = strlen(kmsgStr); + strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", tagLen); } int len = strlen(saveptr) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); msg->len = msgLen; - msg->tag_len = tagLen; + msg->tag_len = tagLen + 1; msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); + BootTime(); msg->tv_sec = bootTime.tv_sec + timestamp / US; msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; msg->pid = mpid; msg->tid = mpid; - strncpy_s(msg->tag, tagLen, mtag, tagLen); + strncpy_s(msg->tag, tagLen + 1, mtag, tagLen); strncpy_s(CONTENT_PTR((msg)), MAX_LOG_LEN, saveptr, len); collector.InsertLogToBuffer(*msg); free(mtag); -- Gitee From a80711010fe1030d142c4692c4b716c5863a05e7 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 27 Sep 2021 12:54:08 +0800 Subject: [PATCH 24/31] Fix kmsg time format; Omitting kmsg property for now Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I0d2992fe4fbf17fce504ec78de64da5ae27a9ce0 --- services/hilogd/include/log_kmsg.h | 2 +- services/hilogd/log_kmsg.cpp | 56 ++++++++++++------------------ services/hilogd/main.cpp | 6 ++-- 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/services/hilogd/include/log_kmsg.h b/services/hilogd/include/log_kmsg.h index dc250c9..50534a8 100644 --- a/services/hilogd/include/log_kmsg.h +++ b/services/hilogd/include/log_kmsg.h @@ -28,7 +28,7 @@ public: int LinuxReadAllKmsg(); void ReadAllKmsg(); void Start(); - void BootTime(); + std::chrono::time_point BootTime(); private: int kmsgCtl = -1; char *kmsgBuffer; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 8664fe4..3b39b9f 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -22,9 +22,11 @@ #include #include #include +#include +#include #include #include -#include +#include #include #include #include @@ -32,9 +34,9 @@ namespace OHOS { namespace HiviewDFX { using namespace std; +using namespace std::chrono; constexpr int DEC = 10; -static struct timeval bootTime; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL @@ -54,12 +56,6 @@ uint32_t ParsePid(const char *str, bool *found) return pid; } -static uint32_t KmsgTimeDeviation(struct timeval *t1, struct timeval *t2) -{ - return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + - ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); -} - uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -83,29 +79,20 @@ uint16_t KmsgLevelMap(uint16_t prio) return level; } -void KLog::BootTime() { - int uptimeFd = 0; - char* uptimeBuf = (char*)calloc(1024, sizeof(char)); - ssize_t uptimeCnt = 0; - struct timeval currentTime; - if ((uptimeFd = open ("/proc/uptime", O_RDONLY | O_NONBLOCK)) == -1) - return; - if ((uptimeCnt = read (uptimeFd, uptimeBuf, 1024)) <= 0) { - close(uptimeFd); - return; - } - close(uptimeFd); - gettimeofday(¤tTime, NULL); - char* tempPtr = nullptr; - if (strtok_r(uptimeBuf, " ", &tempPtr) != NULL) { - uint64_t uptime = strtoumax(strtok_r(NULL, " ", &tempPtr), NULL, DEC); - bootTime.tv_sec = uptime / US; - bootTime.tv_usec = uptime % US; - uint32_t tdiff = KmsgTimeDeviation(¤tTime, &bootTime); - bootTime.tv_sec = tdiff / US; - bootTime.tv_usec = tdiff % US; - } - free(uptimeBuf); +constexpr timespec TimepointToTimespec(time_point tp) +{ + auto secs = time_point_cast(tp); + auto nsecs = time_point_cast(tp) - time_point_cast(secs); + return timespec{secs.time_since_epoch().count(), nsecs.count()}; +} + +time_point KLog::BootTime() { + struct timespec t_uptime; + clock_gettime(CLOCK_BOOTTIME, &t_uptime); + auto uptime = seconds{t_uptime.tv_sec} + nanoseconds{t_uptime.tv_nsec}; + auto current = system_clock::now(); + auto boottime = current - uptime; + return boottime; } ssize_t KLog::LinuxReadOneKmsg() @@ -146,9 +133,10 @@ ssize_t KLog::LinuxReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); - BootTime(); - msg->tv_sec = bootTime.tv_sec + timestamp / US; - msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; + time_point logtime = BootTime() + microseconds{timestamp}; + struct timespec logts = TimepointToTimespec(logtime); + msg->tv_sec = logts.tv_sec; + msg->tv_nsec = logts.tv_nsec; msg->pid = mpid; msg->tid = mpid; strncpy_s(msg->tag, tagLen + 1, mtag, tagLen); diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index 1e89ee5..f75d028 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -88,10 +88,8 @@ int HilogdEntry() server.RunServingThread(); } - if (IsKmsgSwitchOn()) { - KLog klog(logCollector); - auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); - } + KLog klog(logCollector); + auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); -- Gitee From a29816a1fa6734ba6773cf022146063b57f4b667 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 27 Sep 2021 12:54:08 +0800 Subject: [PATCH 25/31] Fix kmsg time format; Omitting kmsg property for now Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I0d2992fe4fbf17fce504ec78de64da5ae27a9ce0 --- services/hilogd/include/log_kmsg.h | 2 +- services/hilogd/log_buffer.cpp | 2 +- services/hilogd/log_kmsg.cpp | 56 ++++++++++++------------------ services/hilogd/main.cpp | 6 ++-- 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/services/hilogd/include/log_kmsg.h b/services/hilogd/include/log_kmsg.h index dc250c9..50534a8 100644 --- a/services/hilogd/include/log_kmsg.h +++ b/services/hilogd/include/log_kmsg.h @@ -28,7 +28,7 @@ public: int LinuxReadAllKmsg(); void ReadAllKmsg(); void Start(); - void BootTime(); + std::chrono::time_point BootTime(); private: int kmsgCtl = -1; char *kmsgBuffer; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 4a96f2c..d63117a 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -58,7 +58,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) return 0; } - std::list& list = (unlikely(msg.type == LOG_KMSG)) ? hilogKlogList : hilogDataList; + std::list& list = (msg.type == LOG_KMSG) ? hilogKlogList : hilogDataList; // Delete old entries when full if (eleSize + sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { hilogBufferMutex.lock(); diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 8664fe4..3b39b9f 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -22,9 +22,11 @@ #include #include #include +#include +#include #include #include -#include +#include #include #include #include @@ -32,9 +34,9 @@ namespace OHOS { namespace HiviewDFX { using namespace std; +using namespace std::chrono; constexpr int DEC = 10; -static struct timeval bootTime; // Avoid name collision between sys/syslog.h and our log_c.h #undef LOG_FATAL @@ -54,12 +56,6 @@ uint32_t ParsePid(const char *str, bool *found) return pid; } -static uint32_t KmsgTimeDeviation(struct timeval *t1, struct timeval *t2) -{ - return ((uint64_t)t1->tv_sec - (uint64_t)t2->tv_sec) * US + - ((uint64_t)t1->tv_usec - (uint64_t)t2->tv_usec); -} - uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -83,29 +79,20 @@ uint16_t KmsgLevelMap(uint16_t prio) return level; } -void KLog::BootTime() { - int uptimeFd = 0; - char* uptimeBuf = (char*)calloc(1024, sizeof(char)); - ssize_t uptimeCnt = 0; - struct timeval currentTime; - if ((uptimeFd = open ("/proc/uptime", O_RDONLY | O_NONBLOCK)) == -1) - return; - if ((uptimeCnt = read (uptimeFd, uptimeBuf, 1024)) <= 0) { - close(uptimeFd); - return; - } - close(uptimeFd); - gettimeofday(¤tTime, NULL); - char* tempPtr = nullptr; - if (strtok_r(uptimeBuf, " ", &tempPtr) != NULL) { - uint64_t uptime = strtoumax(strtok_r(NULL, " ", &tempPtr), NULL, DEC); - bootTime.tv_sec = uptime / US; - bootTime.tv_usec = uptime % US; - uint32_t tdiff = KmsgTimeDeviation(¤tTime, &bootTime); - bootTime.tv_sec = tdiff / US; - bootTime.tv_usec = tdiff % US; - } - free(uptimeBuf); +constexpr timespec TimepointToTimespec(time_point tp) +{ + auto secs = time_point_cast(tp); + auto nsecs = time_point_cast(tp) - time_point_cast(secs); + return timespec{secs.time_since_epoch().count(), nsecs.count()}; +} + +time_point KLog::BootTime() { + struct timespec t_uptime; + clock_gettime(CLOCK_BOOTTIME, &t_uptime); + auto uptime = seconds{t_uptime.tv_sec} + nanoseconds{t_uptime.tv_nsec}; + auto current = system_clock::now(); + auto boottime = current - uptime; + return boottime; } ssize_t KLog::LinuxReadOneKmsg() @@ -146,9 +133,10 @@ ssize_t KLog::LinuxReadOneKmsg() msg->type = LOG_KMSG; msg->domain = 0xdfffffff; msg->level = KmsgLevelMap(mLevel); - BootTime(); - msg->tv_sec = bootTime.tv_sec + timestamp / US; - msg->tv_nsec = (bootTime.tv_usec + timestamp % US) * NS2US; + time_point logtime = BootTime() + microseconds{timestamp}; + struct timespec logts = TimepointToTimespec(logtime); + msg->tv_sec = logts.tv_sec; + msg->tv_nsec = logts.tv_nsec; msg->pid = mpid; msg->tid = mpid; strncpy_s(msg->tag, tagLen + 1, mtag, tagLen); diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index 1e89ee5..f75d028 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -88,10 +88,8 @@ int HilogdEntry() server.RunServingThread(); } - if (IsKmsgSwitchOn()) { - KLog klog(logCollector); - auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); - } + KLog klog(logCollector); + auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); -- Gitee From 14ee0d661195316328bf243dd7e5294629cdeb5e Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 27 Sep 2021 14:30:26 +0800 Subject: [PATCH 26/31] Remove unuseful signal handler etc from main.cpp Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I2fe430f00a1c9abd7f661b90fdca0e73a11acf49 --- services/hilogd/main.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index f75d028..c1d56aa 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include "cmd_executor.h" #include "log_querier.h" @@ -42,19 +41,6 @@ using namespace std; static int g_fd = -1; #endif - -static void SigHandler(int sig) -{ - if (sig == SIGINT) { -#ifdef DEBUG - if (g_fd > 0) { - close(g_fd); - } -#endif - std::cout<<"Exited!"< Date: Mon, 27 Sep 2021 16:09:14 +0800 Subject: [PATCH 27/31] temporarily fix /dev/kmsg permission Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I6526d009b70d1c8c859d780739e4f85cdc1e5386 --- services/hilogd/etc/hilogd.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/hilogd/etc/hilogd.cfg b/services/hilogd/etc/hilogd.cfg index e5516f3..340977e 100644 --- a/services/hilogd/etc/hilogd.cfg +++ b/services/hilogd/etc/hilogd.cfg @@ -9,6 +9,7 @@ "cmds" : [ "mkdir /data/log/ 0770 system log", "mkdir /data/log/hilog/ 0750 logd log", + "chmod 0664 /dev/kmsg", "start hilogd" ] } @@ -25,4 +26,4 @@ ] } ] -} \ No newline at end of file +} -- Gitee From bddd46c94f6cdf207ecc972409fe36039eb9ad2d Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 27 Sep 2021 17:41:17 +0800 Subject: [PATCH 28/31] Fix HilogBuffer startup crash Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Idfa9e044a661de5f7b75e2509ccea7093ccbf782 --- services/hilogd/log_kmsg.cpp | 10 ++++++++++ services/hilogd/main.cpp | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 3b39b9f..4c761ff 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -45,6 +45,7 @@ constexpr int DEC = 10; #undef LOG_INFO #undef LOG_DEBUG +// Parse pid if exists uint32_t ParsePid(const char *str, bool *found) { if (strncmp(str,"[pid=",5) != 0) { @@ -56,6 +57,7 @@ uint32_t ParsePid(const char *str, bool *found) return pid; } +// Log levels are different in syslog.h and hilog log_c.h uint16_t KmsgLevelMap(uint16_t prio) { uint16_t level; @@ -86,6 +88,7 @@ constexpr timespec TimepointToTimespec(time_point tp) return timespec{secs.time_since_epoch().count(), nsecs.count()}; } +// Kmsg has microseconds from system boot. Now get the time of system boot. time_point KLog::BootTime() { struct timespec t_uptime; clock_gettime(CLOCK_BOOTTIME, &t_uptime); @@ -104,15 +107,20 @@ ssize_t KLog::LinuxReadOneKmsg() if (size > 0) { char* mtag = (char*)calloc(MAX_TAG_LEN, sizeof(char)); char* saveptr = nullptr; + // Parses header of kmsg entry: priority,sequence,timestamp,flag; uint16_t mLevel = strtoul(strtok_r(kmsgBuffer, ",", &saveptr), NULL, DEC); strtok_r(NULL, ",",&saveptr); // Kmsg seq is not needed at present uint64_t timestamp = strtoumax(strtok_r(NULL, ",",&saveptr), NULL, DEC); strtok_r(NULL, ";",&saveptr); // Kmsg flag is not needed at present + // Parses pid if exists. Pid in kmsg content is like: [pid=xxx,...] uint32_t mpid = 0; bool found = false; mpid = ParsePid(saveptr, &found); if (found) strtok_r(NULL, "]", &saveptr); + + // If there are some other content wrapped in square brackets "[]", parse it as tag + // Otherwise, use default tag "kmsg" int tagLen = 0; if (saveptr[0] == '[') { char* sep = strtok_r(NULL, "]", &saveptr); @@ -125,6 +133,8 @@ ssize_t KLog::LinuxReadOneKmsg() tagLen = strlen(kmsgStr); strncpy_s(mtag, MAX_TAG_LEN - 1, "kmsg", tagLen); } + + // Now build HilogMsg and insert it into buffer int len = strlen(saveptr) + 1; int msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsg *msg = (HilogMsg*)malloc(msgLen); diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index c1d56aa..f58c246 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -43,7 +43,7 @@ static int g_fd = -1; int HilogdEntry() { - HilogBuffer hilogBuffer; + HilogBuffer* hilogBuffer = new HilogBuffer(); #ifdef DEBUG int fd = open(HILOG_FILE_DIR"hilogd.txt", O_WRONLY | O_APPEND); @@ -57,7 +57,7 @@ int HilogdEntry() InitDomainFlowCtrl(); // Start log_collector - LogCollector logCollector(&hilogBuffer); + LogCollector logCollector(hilogBuffer); HilogInputSocketServer server(logCollector.onDataRecv); if (server.Init() < 0) { #ifdef DEBUG @@ -69,15 +69,14 @@ int HilogdEntry() #endif server.RunServingThread(); } - KLog klog(logCollector); auto klogTask = std::async(std::launch::async, &KLog::ReadAllKmsg, klog); auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { - std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); - logQuerier->RestorePersistJobs(hilogBuffer); + std::shared_ptr logQuerier = std::make_shared(nullptr, hilogBuffer); + logQuerier->RestorePersistJobs(*hilogBuffer); }); - CmdExecutor cmdExecutor(&hilogBuffer); + CmdExecutor cmdExecutor(hilogBuffer); cmdExecutor.StartCmdExecutorThread(); return 0; } -- Gitee From 36611329a528c37f737a6ed3df00f04b63cd1b67 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 27 Sep 2021 11:27:37 +0000 Subject: [PATCH 29/31] add const && codeformat Signed-off-by: aajwy <13051180828@163.com> --- adapter/properties.cpp | 6 +++--- services/hilogd/log_kmsg.cpp | 20 ++++++++++---------- services/hilogd/log_querier.cpp | 8 ++++---- services/hilogtool/log_controller.cpp | 22 +++++++++++----------- services/hilogtool/log_display.cpp | 3 +++ services/hilogtool/main.cpp | 8 ++++---- 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index f1b3f94..7bd3f6a 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -116,9 +116,9 @@ string GetPropertyName(uint32_t propType) case PROP_SINGLE_DEBUG: key = "hilog.debug.on"; break; - case PROP_KMSG: - key = "hilog.kmsg.on"; - break; + case PROP_KMSG: + key = "const.hilog.kmsg.on"; + break; case PROP_PERSIST_DEBUG: key = "persist.sys.hilog.debug.on"; break; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 4c761ff..db8d007 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -52,7 +52,7 @@ uint32_t ParsePid(const char *str, bool *found) *found = false; return 0; } - uint32_t pid = strtoul(str + 5, NULL, DEC); + uint32_t pid = strtoul(str + 5, nullptr, DEC); *found = true; return pid; } @@ -108,23 +108,23 @@ ssize_t KLog::LinuxReadOneKmsg() char* mtag = (char*)calloc(MAX_TAG_LEN, sizeof(char)); char* saveptr = nullptr; // Parses header of kmsg entry: priority,sequence,timestamp,flag; - uint16_t mLevel = strtoul(strtok_r(kmsgBuffer, ",", &saveptr), NULL, DEC); - strtok_r(NULL, ",",&saveptr); // Kmsg seq is not needed at present - uint64_t timestamp = strtoumax(strtok_r(NULL, ",",&saveptr), NULL, DEC); - strtok_r(NULL, ";",&saveptr); // Kmsg flag is not needed at present + uint16_t mLevel = strtoul(strtok_r(kmsgBuffer, ",", &saveptr), nullptr, DEC); + strtok_r(nullptr, ",",&saveptr); // Kmsg seq is not needed at present + uint64_t timestamp = strtoumax(strtok_r(nullptr, ",",&saveptr), nullptr, DEC); + strtok_r(nullptr, ";",&saveptr); // Kmsg flag is not needed at present // Parses pid if exists. Pid in kmsg content is like: [pid=xxx,...] uint32_t mpid = 0; bool found = false; mpid = ParsePid(saveptr, &found); - if (found) strtok_r(NULL, "]", &saveptr); + if (found) strtok_r(nullptr, "]", &saveptr); // If there are some other content wrapped in square brackets "[]", parse it as tag // Otherwise, use default tag "kmsg" int tagLen = 0; if (saveptr[0] == '[') { - char* sep = strtok_r(NULL, "]", &saveptr); - if (sep != NULL) { + char* sep = strtok_r(nullptr, "]", &saveptr); + if (sep != nullptr) { tagLen = strlen(sep) - 1; strncpy_s(mtag, MAX_TAG_LEN - 1, sep + 1, tagLen); } @@ -176,10 +176,10 @@ int KLog::LinuxReadAllKmsg() void KLog::ReadAllKmsg() { - #ifdef __linux__ +#ifdef __linux__ std::cout << "Platform: Linux" << std::endl; LinuxReadAllKmsg(); - #endif +#endif } void KLog::Start() diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index b4852fc..d6dcdd1 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -44,7 +44,7 @@ using namespace std; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = HILOG_FILE_DIR; constexpr int DEFAULT_LOG_LEVEL = 1< logReade string logPersisterPath; if (pLogPersistStartRst == nullptr) { return; - } else if (!logTypeForbidden(logReader->queryCondition.types)){ + } else if (LogTypeForbidden(logReader->queryCondition.types) == false){ pLogPersistStartRst->result = ERR_QUERY_TYPE_INVALID; } else if (pLogPersistStartMsg->jobId <= 0) { pLogPersistStartRst->result = ERR_LOG_PERSIST_JOBID_INVALID; @@ -476,7 +476,7 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) case LOG_QUERY_REQUEST: qRstMsg = (LogQueryRequest*) g_tempBuffer; SetCondition(logReader, *qRstMsg); - if (!logTypeForbidden(logReader->queryCondition.types)) { + if (!LogTypeForbidden(logReader->queryCondition.types)) { return; } HandleLogQueryRequest(logReader, *hilogBuffer); diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index e6ff6d5..45895c8 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -523,18 +523,18 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType } break; case OT_KMSG_SWITCH: - key = GetPropertyName(PROP_KMSG); - if (propertyParm->kmsgSwitchStr == "on") { + key = GetPropertyName(PROP_KMSG); + if (propertyParm->kmsgSwitchStr == "on") { PropertySet(key.c_str(), "true"); - std::cout << "hilog will store kmsg log" << std::endl; - } else if (propertyParm->kmsgSwitchStr == "off") { - PropertySet(key.c_str(), "false"); - std::cout << "hilog will not store kmsg log" << std::endl; - } else { - std::cout << ParseErrorCode(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl; - return RET_FAIL; - } - break; + std::cout << "hilog will store kmsg log" << std::endl; + } else if (propertyParm->kmsgSwitchStr == "off") { + PropertySet(key.c_str(), "false"); + std::cout << "hilog will not store kmsg log" << std::endl; + } else { + std::cout << ParseErrorCode(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl; + return RET_FAIL; + } + break; case OT_LOG_LEVEL: if (propertyParm->tagStr != "" && propertyParm->domainStr != "") { return RET_FAIL; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index d9778e4..748870c 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -112,6 +112,9 @@ string GetLogTypeStr(uint16_t logType) if (logType == LOG_APP) { logTypeStr = "app"; } + if (logType == LOG_KMSG) { + logTypeStr = "kmsg"; + } return logTypeStr; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 131bdd3..a1d36c6 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -335,10 +335,10 @@ int HilogEntry(int argc, char* argv[]) controlCount++; break; case 'k': - context.kmsgArgs = optarg; - noLogOption = true; - controlCount++; - break; + context.kmsgArgs = optarg; + noLogOption = true; + controlCount++; + break; case 'r': context.logClearArgs = "clear"; noLogOption = true; -- Gitee From ad0d2ec32e56bddfe9ab5d35626a32bb9001a112 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 28 Sep 2021 12:10:26 +0000 Subject: [PATCH 30/31] kmsg persist Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 3 ++- services/hilogd/log_persister_rotator.cpp | 2 -- services/hilogd/log_querier.cpp | 6 ++++-- services/hilogtool/log_controller.cpp | 11 ++++++++--- services/hilogtool/log_display.cpp | 7 +++---- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index d248287..be8c22a 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -51,7 +51,8 @@ static const long long NS2MS = 1000000LL; const uint32_t MAX_BUFFER_SIZE = 1UL<<30; const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; const int MSG_MAX_LEN = 2048; - +constexpr uint64_t PRIME = 0x100000001B3ull; +constexpr uint64_t BASIS = 0xCBF29CE484222325ull; /* * header of log message from libhilog to hilogd */ diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 4865edd..470ba48 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -25,8 +25,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -constexpr uint64_t PRIME = 0x100000001B3ull; -constexpr uint64_t BASIS = 0xCBF29CE484222325ull; uint64_t GetInfoHash(const PersistRecoveryInfo &info) { uint64_t ret {BASIS}; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index d6dcdd1..ad93d48 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -152,7 +152,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade string logPersisterPath; if (pLogPersistStartRst == nullptr) { return; - } else if (LogTypeForbidden(logReader->queryCondition.types) == false){ + } else if (LogTypeForbidden(pLogPersistStartMsg->logType) == false){ pLogPersistStartRst->result = ERR_QUERY_TYPE_INVALID; } else if (pLogPersistStartMsg->jobId <= 0) { pLogPersistStartRst->result = ERR_LOG_PERSIST_JOBID_INVALID; @@ -163,7 +163,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade cout << "FileName is not valid!" << endl; pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_NAME_INVALID; } else { - logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + "hilog") + string logPersisterFileName = (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) ? "hilog_kmsg":"hilog"; + cout << "logPersisterFileName" << logPersisterFileName << endl; + logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + logPersisterFileName) : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); if (strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()) != 0) { pLogPersistStartRst->result = RET_FAIL; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 45895c8..2d4ffce 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -36,7 +36,7 @@ using namespace std; const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; - +const uint32_t DEFAULT_KMSG_JOBID = 2; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { @@ -419,8 +419,13 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; } - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID - : stoi(logPersistParam->jobIdStr); + if (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) { + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_KMSG_JOBID + : stoi(logPersistParam->jobIdStr); + } else { + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID + : stoi(logPersistParam->jobIdStr); + } if (pLogPersistStartMsg->jobId <= 0) { cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl; return RET_FAIL; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 748870c..2f9190c 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -30,10 +30,6 @@ namespace HiviewDFX { using namespace std; using hash_t = std::uint64_t; - -constexpr hash_t PRIME = 0x100000001B3ull; -constexpr hash_t BASIS = 0xCBF29CE484222325ull; - unordered_map errorMsg { {RET_FAIL, "Unexpected error"}, @@ -130,6 +126,9 @@ string GetOrigType(uint16_t shiftType) if (((1 << LOG_APP) & shiftType) != 0) { logType += "app,"; } + if (((1 << LOG_KMSG) & shiftType) != 0) { + logType += "kmsg,"; + } logType.erase(logType.end() - 1); return logType; } -- Gitee From 669ae0ebcae786d5cb73ab40717c7b46b9a37244 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 28 Sep 2021 12:10:26 +0000 Subject: [PATCH 31/31] kmsg persist Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 3 ++- services/hilogd/log_persister.cpp | 2 +- services/hilogd/log_persister_rotator.cpp | 2 -- services/hilogd/log_querier.cpp | 7 +++++-- services/hilogtool/log_controller.cpp | 13 ++++++++++--- services/hilogtool/log_display.cpp | 8 ++++---- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index d248287..be8c22a 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -51,7 +51,8 @@ static const long long NS2MS = 1000000LL; const uint32_t MAX_BUFFER_SIZE = 1UL<<30; const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; const int MSG_MAX_LEN = 2048; - +constexpr uint64_t PRIME = 0x100000001B3ull; +constexpr uint64_t BASIS = 0xCBF29CE484222325ull; /* * header of log message from libhilog to hilogd */ diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 93929f6..832100f 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -328,7 +328,7 @@ int LogPersister::Query(uint16_t logType, list &results) cout << "Persister.Query: logType " << logType << endl; for (auto it = logPersisters.begin(); it != logPersisters.end(); ++it) { cout << "Persister.Query: (*it)->queryCondition.types " - << (*it)->queryCondition.types << endl; + << unsigned((*it)->queryCondition.types) << endl; if (((*it)->queryCondition.types & logType) != 0) { LogPersistQueryResult response; response.logType = (*it)->queryCondition.types; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 4865edd..470ba48 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -25,8 +25,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -constexpr uint64_t PRIME = 0x100000001B3ull; -constexpr uint64_t BASIS = 0xCBF29CE484222325ull; uint64_t GetInfoHash(const PersistRecoveryInfo &info) { uint64_t ret {BASIS}; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index d6dcdd1..c39d408 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -152,7 +152,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade string logPersisterPath; if (pLogPersistStartRst == nullptr) { return; - } else if (LogTypeForbidden(logReader->queryCondition.types) == false){ + } else if (LogTypeForbidden(pLogPersistStartMsg->logType) == false){ pLogPersistStartRst->result = ERR_QUERY_TYPE_INVALID; } else if (pLogPersistStartMsg->jobId <= 0) { pLogPersistStartRst->result = ERR_LOG_PERSIST_JOBID_INVALID; @@ -163,7 +163,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade cout << "FileName is not valid!" << endl; pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_NAME_INVALID; } else { - logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + "hilog") + string logPersisterFileName = (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) ? "hilog_kmsg":"hilog"; + cout << "logPersisterFileName" << logPersisterFileName << endl; + logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + logPersisterFileName) : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); if (strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()) != 0) { pLogPersistStartRst->result = RET_FAIL; @@ -199,6 +201,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead } list resultList; list::iterator it; + cout << "DEFAULT_LOG_TYPE" << DEFAULT_LOG_TYPE << endl; rst = LogPersister::Query(DEFAULT_LOG_TYPE, resultList); if (pLogPersistStopMsg && recvMsgLen < msgLen) { if (pLogPersistStopMsg->jobId != JOB_ID_ALL) { diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 45895c8..e39b732 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -36,7 +36,7 @@ using namespace std; const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; - +const uint32_t DEFAULT_KMSG_JOBID = 2; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { @@ -154,6 +154,8 @@ string SetDefaultLogType(const std::string& logTypeStr) logType = "core app"; } else if (logTypeStr == "all") { logType = "core app init"; + // } else if (logTypeStr == "kmsg") { + // logType = "kmsg"; } else { logType = logTypeStr; } @@ -419,8 +421,13 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; } - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID - : stoi(logPersistParam->jobIdStr); + if (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) { + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_KMSG_JOBID + : stoi(logPersistParam->jobIdStr); + } else { + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID + : stoi(logPersistParam->jobIdStr); + } if (pLogPersistStartMsg->jobId <= 0) { cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl; return RET_FAIL; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 748870c..fbfc749 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -30,10 +30,6 @@ namespace HiviewDFX { using namespace std; using hash_t = std::uint64_t; - -constexpr hash_t PRIME = 0x100000001B3ull; -constexpr hash_t BASIS = 0xCBF29CE484222325ull; - unordered_map errorMsg { {RET_FAIL, "Unexpected error"}, @@ -130,6 +126,9 @@ string GetOrigType(uint16_t shiftType) if (((1 << LOG_APP) & shiftType) != 0) { logType += "app,"; } + if (((1 << LOG_KMSG) & shiftType) != 0) { + logType += "kmsg,"; + } logType.erase(logType.end() - 1); return logType; } @@ -354,6 +353,7 @@ int32_t ControlCmdResult(const char* message) } LogPersistQueryResult* pLogPersistQueryRst = (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; + cout << "logType" << unsigned(pLogPersistQueryRst->logType) << GetLogTypeStr(pLogPersistQueryRst->logType) << endl; while (pLogPersistQueryRst && resultLen < msgLen) { if (pLogPersistQueryRst->result < 0) { outputStr = "Persist task [logtype:"; -- Gitee