• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 #define HST_LOG_TAG "DumpBuffer"
16 #define ALL_DUMP_FILES { DEMUXER_INPUT_PEEK, DEMUXER_INPUT_GET, DEMUXER_OUTPUT, DECODER_OUTPUT }
17 
18 #include "foundation/utils/dump_buffer.h"
19 #include <cstdio>
20 #include <cstring>
21 #ifdef _WIN32
22 #include <direct.h>
23 #else
24 #include <unistd.h>
25 #endif
26 #include <dirent.h>
27 #include "foundation/log.h"
28 #include "foundation/osal/filesystem/file_system.h"
29 #include "foundation/osal/utils/util.h"
30 
31 namespace {
32 constexpr size_t LENGTH_OF_STRING = 2;
33 constexpr size_t MAX_WRITE_SIZE = 3;
34 }
35 
36 namespace OHOS {
37 namespace Media {
38 namespace Pipeline {
39 #ifdef _WIN32
40 #define DUMP_FILE_DIR ""
41 #endif
42 // Specify the dump file dir in BUILD.gn
43 #ifndef DUMP_FILE_DIR
44 #define DUMP_FILE_DIR "/data/local/tmp"
45 #endif
46 
47 std::map<std::string, FILE*> allDumpFileFds;
48 
GetDumpFileDir()49 std::string GetDumpFileDir()
50 {
51     return std::string(DUMP_FILE_DIR);
52 }
53 
DumpBufferToFile(const std::string & fileName,const std::shared_ptr<Plugin::Buffer> & buffer)54 void DumpBufferToFile(const std::string& fileName, const std::shared_ptr<Plugin::Buffer>& buffer)
55 {
56     FALSE_RETURN_MSG(allDumpFileFds[fileName] != nullptr, "fd is null");
57     if (buffer->GetMemory() == nullptr) {
58         MEDIA_LOG_E("Get memory failed: nullptr");
59         return;
60     }
61     size_t bufferSize = buffer->GetMemory()->GetSize();
62     FALSE_RETURN(bufferSize != 0);
63     auto addr = reinterpret_cast<const char*>(buffer->GetMemory()->GetReadOnlyData());
64     if (addr == nullptr) {
65         MEDIA_LOG_E("Get address failed: nullptr");
66         return;
67     }
68     (void)fwrite(addr,
69                  1, bufferSize, allDumpFileFds[fileName]);
70 }
71 
PrepareDumpDir()72 void PrepareDumpDir()
73 {
74     MEDIA_LOG_I("Prepare dumpDir enter.");
75     for (auto iter : ALL_DUMP_FILES) {
76         std::string filePath = GetDumpFileDir() + "/" + iter;
77         MEDIA_LOG_I("Prepare dumpDir: " PUBLIC_LOG_S, filePath.c_str());
78         std::string fullPath;
79         bool isFileExist = OSAL::ConvertFullPath(filePath, fullPath);
80         if (isFileExist) { // 文件存在
81             OSAL::FileSystem::ClearFileContent(fullPath);
82         }
83         char path[PATH_MAX] = {0};
84         auto realPath = realpath(fullPath.c_str(), path);
85         FALSE_RETURN(realPath != nullptr);
86         allDumpFileFds[iter] = fopen(realPath, "ab+");
87         if (allDumpFileFds[iter] == nullptr) {
88             MEDIA_LOG_W("Open file failed(" PUBLIC_LOG_S ").", strerror(errno));
89         }
90     }
91 }
92 
EndDumpFile()93 void EndDumpFile()
94 {
95     MEDIA_LOG_I("End dump enter.");
96     for (auto iter : ALL_DUMP_FILES) {
97         if (allDumpFileFds[iter]) {
98             fclose(allDumpFileFds[iter]);
99             allDumpFileFds[iter] = nullptr;
100         }
101     }
102 }
103 
DumpBufferToLog(const char * desc,const std::shared_ptr<Plugin::Buffer> & buffer,uint64_t offset,size_t dumpSize)104 void DumpBufferToLog(const char* desc, const std::shared_ptr<Plugin::Buffer>& buffer, uint64_t offset, size_t dumpSize)
105 {
106     FALSE_RETURN_MSG(buffer && (!buffer->IsEmpty()),  PUBLIC_LOG_S " Buffer(null or empty)", desc);
107     size_t bufferSize = buffer->GetMemory()->GetSize();
108     size_t realDumpSize = std::min(dumpSize, bufferSize);
109     realDumpSize = std::min(realDumpSize, static_cast<size_t>(DUMP_BUFFER2LOG_SIZE)); // max DUMP_BUFFER2LOG_SIZE bytes
110     char tmpStr[2 * DUMP_BUFFER2LOG_SIZE + 10] = {0}; // 字符串长度是打印的buffer长度的2倍 + 1 (字符串结束符)
111     char* dstPtr = tmpStr;
112     int len;
113     const uint8_t* p = buffer->GetMemory()->GetReadOnlyData();
114     for (size_t i = 0; i < realDumpSize; i++) {
115         len = snprintf_s(dstPtr, MAX_WRITE_SIZE, LENGTH_OF_STRING, "%02x", p[i]); // max write 3 bytes, string len 2
116         FALSE_RETURN_MSG(len > 0 && len <= 2, "snprintf_s returned unexpected value " PUBLIC_LOG_D32, len); // max len 2
117         dstPtr += len;
118     }
119     MEDIA_LOG_I(PUBLIC_LOG_S " Buffer(offset " PUBLIC_LOG_D64 ", size " PUBLIC_LOG_ZU ", capacity "
120         PUBLIC_LOG_ZU ") : " PUBLIC_LOG_S, desc, offset, bufferSize, buffer->GetMemory()->GetCapacity(), tmpStr);
121 }
122 } // Pipeline
123 } // Media
124 } // OHOS