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