• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "binder_catcher.h"
16 
17 #include <fstream>
18 
19 #include "securec.h"
20 
21 #include "common_utils.h"
22 #include "defines.h"
23 namespace OHOS {
24 namespace HiviewDFX {
25 #ifdef BINDER_CATCHER_ENABLE
26 using namespace OHOS::HiviewDFX::CommonUtils;
BinderCatcher()27 BinderCatcher::BinderCatcher() : EventLogCatcher()
28 {
29     name_ = "BinderCatcher";
30 }
31 
Initialize(const std::string & strParam1,int intParam1,int intParam2)32 bool BinderCatcher::Initialize(const std::string& strParam1, int intParam1, int intParam2)
33 {
34     // this catcher do not need parameters, just return true
35     char buf[BUF_SIZE_512] = {0};
36     int ret = snprintf_s(buf, BUF_SIZE_512, BUF_SIZE_512 - 1,
37         "BinderCatcher --\n");
38     if (ret > 0) {
39         description_ = buf;
40     }
41     return true;
42 };
43 
Catch(int fd,int jsonFd)44 int BinderCatcher::Catch(int fd, int jsonFd)
45 {
46     std::string line;
47     int originSize = GetFdSize(fd);
48     std::ifstream fin;
49     fin.open("/proc/transaction_proc");
50     if (!fin.is_open()) {
51         std::string content = "open binder file failed :/proc/transaction_proc\r\n";
52         FileUtil::SaveStringToFd(fd, content);
53         goto end;
54     }
55     while (getline(fin, line)) {
56         FileUtil::SaveStringToFd(fd, line + "\n");
57     }
58     fin.close();
59 
60     end:
61     logSize_ = GetFdSize(fd) - originSize;
62     if (logSize_ <= 0) {
63         FileUtil::SaveStringToFd(fd, "binder content is empty!");
64     }
65     return logSize_;
66 };
67 #endif // BINDER_CATCHER_ENABLE
68 } // namespace HiviewDFX
69 } // namespace OHOS
70