• 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 #include "parameter_ex.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 #ifdef BINDER_CATCHER_ENABLE
28 using namespace OHOS::HiviewDFX::CommonUtils;
BinderCatcher()29 BinderCatcher::BinderCatcher() : EventLogCatcher()
30 {
31     name_ = "BinderCatcher";
32 }
33 
Initialize(const std::string & strParam1,int intParam1,int intParam2)34 bool BinderCatcher::Initialize(const std::string& strParam1, int intParam1, int intParam2)
35 {
36     // this catcher do not need parameters, just return true
37     description_ = "BinderCatcher --\n";
38     return true;
39 };
40 
Catch(int fd,int jsonFd)41 int BinderCatcher::Catch(int fd, int jsonFd)
42 {
43     int originSize = GetFdSize(fd);
44     if (Parameter::IsOversea()) {
45         FileUtil::SaveStringToFd(fd, "binder info is not saved in oversea version\n");
46     } else {
47         std::string line;
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         } else {
54             while (getline(fin, line)) {
55                 FileUtil::SaveStringToFd(fd, line + "\n");
56             }
57             fin.close();
58         }
59     }
60 
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