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