• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const int BACK_TRACE_RING_BUFFER_PRINT_WAIT_TIME_MS = 10;
29 std::condition_variable DfxRingBufferWrapper::printCV_;
30 std::mutex DfxRingBufferWrapper::printMutex_;
31 
GetInstance()32 DfxRingBufferWrapper &DfxRingBufferWrapper::GetInstance()
33 {
34     static DfxRingBufferWrapper ins;
35     return ins;
36 }
37 
LoopPrintRingBuffer()38 void DfxRingBufferWrapper::LoopPrintRingBuffer()
39 {
40     if (DfxRingBufferWrapper::GetInstance().writeFunc_ == nullptr) {
41         DfxRingBufferWrapper::GetInstance().writeFunc_ = DfxRingBufferWrapper::DefaultWrite;
42     }
43 
44     std::unique_lock<std::mutex> lck(printMutex_);
45     while (true) {
46         auto available = DfxRingBufferWrapper::GetInstance().ringBuffer_.Available();
47         auto item = DfxRingBufferWrapper::GetInstance().ringBuffer_.Read(available);
48 
49         if (available != 0) {
50             if (item.At(0).empty()) {
51                 DfxRingBufferWrapper::GetInstance().ringBuffer_.Skip(item.Length());
52                 continue;
53             }
54 
55             for (unsigned int i = 0; i < item.Length(); i++) {
56                 DfxRingBufferWrapper::GetInstance().writeFunc_(DfxRingBufferWrapper::GetInstance().fd_, \
57                     item.At(i).c_str(), item.At(i).length());
58             }
59             DfxRingBufferWrapper::GetInstance().ringBuffer_.Skip(item.Length());
60         } else {
61             if (DfxRingBufferWrapper::GetInstance().hasFinished_) {
62                 DFXLOG_DEBUG("%s :: print finished, exit loop.\n", __func__);
63                 break;
64             }
65 
66             printCV_.wait_for(lck, std::chrono::milliseconds(BACK_TRACE_RING_BUFFER_PRINT_WAIT_TIME_MS));
67         }
68     }
69 }
70 
AppendMsg(const std::string & msg)71 void DfxRingBufferWrapper::AppendMsg(const std::string& msg)
72 {
73     if (writeFunc_ == nullptr) {
74         writeFunc_ = DfxRingBufferWrapper::DefaultWrite;
75     }
76     writeFunc_(fd_, msg.c_str(), msg.length());
77 }
78 
AppendBuf(const char * format,...)79 int DfxRingBufferWrapper::AppendBuf(const char *format, ...)
80 {
81     int ret = -1;
82     char buf[LINE_BUF_SIZE] = {0};
83     (void)memset_s(&buf, sizeof(buf), 0, sizeof(buf));
84     va_list args;
85     va_start(args, format);
86     ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
87     va_end(args);
88     if (ret < 0) {
89         DFXLOG_ERROR("%s :: vsnprintf_s failed, line: %d.", __func__, __LINE__);
90     }
91 
92     AppendMsg(std::string(buf));
93     return ret;
94 }
95 
StartThread()96 void DfxRingBufferWrapper::StartThread()
97 {
98     hasFinished_ = false;
99 }
100 
StopThread()101 void DfxRingBufferWrapper::StopThread()
102 {
103     if (fd_ != INVALID_FD) {
104         close(fd_);
105     }
106     fd_ = INVALID_FD;
107 }
108 
SetWriteBufFd(int32_t fd)109 void DfxRingBufferWrapper::SetWriteBufFd(int32_t fd)
110 {
111     fd_ = fd;
112     if (fd_ < 0) {
113         DFXLOG_WARN("%s :: Failed to set fd.\n", __func__);
114     }
115 }
116 
SetWriteFunc(RingBufferWriteFunc func)117 void DfxRingBufferWrapper::SetWriteFunc(RingBufferWriteFunc func)
118 {
119     writeFunc_ = func;
120 }
121 
DefaultWrite(int32_t fd,const char * buf,const int len)122 int DfxRingBufferWrapper::DefaultWrite(int32_t fd, const char *buf, const int len)
123 {
124     if (buf == nullptr) {
125         return -1;
126     }
127     WriteLog(UNUSED_FD, "%s", buf);
128     if (fd > 0) {
129         return write(fd, buf, len);
130     }
131     return 0;
132 }
133 } // namespace HiviewDFX
134 } // namespace OHOS
135