1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dfx_ring_buffer_wrapper.h"
17
18 #include <securec.h>
19 #include <unistd.h>
20
21 #include "dfx_define.h"
22 #include "dfx_logger.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 static const int32_t INVALID_FD = -1;
27 static const int32_t UNUSED_FD = -2;
28 std::condition_variable DfxRingBufferWrapper::printCV_;
29 std::mutex DfxRingBufferWrapper::printMutex_;
30
GetInstance()31 DfxRingBufferWrapper &DfxRingBufferWrapper::GetInstance()
32 {
33 static DfxRingBufferWrapper ins;
34 return ins;
35 }
36
LoopPrintRingBuffer()37 void DfxRingBufferWrapper::LoopPrintRingBuffer()
38 {
39 if (DfxRingBufferWrapper::GetInstance().writeFunc_ == nullptr) {
40 DfxRingBufferWrapper::GetInstance().writeFunc_ = DfxRingBufferWrapper::DefaultWrite;
41 }
42
43 std::unique_lock<std::mutex> lck(printMutex_);
44 while (true) {
45 auto available = DfxRingBufferWrapper::GetInstance().ringBuffer_.Available();
46 auto item = DfxRingBufferWrapper::GetInstance().ringBuffer_.Read(available);
47
48 if (available != 0) {
49 if (item.At(0).empty()) {
50 DfxRingBufferWrapper::GetInstance().ringBuffer_.Skip(item.Length());
51 continue;
52 }
53
54 for (unsigned int i = 0; i < item.Length(); i++) {
55 DfxRingBufferWrapper::GetInstance().writeFunc_(DfxRingBufferWrapper::GetInstance().fd_, \
56 item.At(i).c_str(), item.At(i).length());
57 }
58 DfxRingBufferWrapper::GetInstance().ringBuffer_.Skip(item.Length());
59 } else {
60 if (DfxRingBufferWrapper::GetInstance().hasFinished_) {
61 DFXLOG_DEBUG("%s :: print finished, exit loop.\n", __func__);
62 break;
63 }
64
65 printCV_.wait_for(lck, std::chrono::milliseconds(BACK_TRACE_RING_BUFFER_PRINT_WAIT_TIME_MS));
66 }
67 }
68 }
69
AppendMsg(const std::string & msg)70 void DfxRingBufferWrapper::AppendMsg(const std::string& msg)
71 {
72 if (writeFunc_ == nullptr) {
73 writeFunc_ = DfxRingBufferWrapper::DefaultWrite;
74 }
75 writeFunc_(fd_, msg.c_str(), msg.length());
76 }
77
AppendBuf(const char * format,...)78 int DfxRingBufferWrapper::AppendBuf(const char *format, ...)
79 {
80 int ret = -1;
81 char buf[LOG_BUF_LEN] = {0};
82 (void)memset_s(&buf, sizeof(buf), 0, sizeof(buf));
83 va_list args;
84 va_start(args, format);
85 ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
86 va_end(args);
87 if (ret < 0) {
88 DFXLOG_ERROR("%s :: vsnprintf_s failed, line: %d.", __func__, __LINE__);
89 }
90
91 AppendMsg(std::string(buf));
92 return ret;
93 }
94
StartThread()95 void DfxRingBufferWrapper::StartThread()
96 {
97 hasFinished_ = false;
98 }
99
StopThread()100 void DfxRingBufferWrapper::StopThread()
101 {
102 if (fd_ != INVALID_FD) {
103 close(fd_);
104 }
105 fd_ = INVALID_FD;
106 }
107
SetWriteBufFd(int32_t fd)108 void DfxRingBufferWrapper::SetWriteBufFd(int32_t fd)
109 {
110 fd_ = fd;
111 if (fd_ < 0) {
112 DFXLOG_WARN("%s :: Failed to set fd.\n", __func__);
113 }
114 }
115
SetWriteFunc(RingBufferWriteFunc func)116 void DfxRingBufferWrapper::SetWriteFunc(RingBufferWriteFunc func)
117 {
118 writeFunc_ = func;
119 }
120
DefaultWrite(int32_t fd,const char * buf,const int len)121 int DfxRingBufferWrapper::DefaultWrite(int32_t fd, const char *buf, const int len)
122 {
123 if (buf == nullptr) {
124 return -1;
125 }
126 WriteLog(UNUSED_FD, "%s", buf);
127 if (fd > 0) {
128 return write(fd, buf, len);
129 }
130 return 0;
131 }
132 } // namespace HiviewDFX
133 } // namespace OHOS
134