• 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 "command_catcher.h"
16 
17 #include "dump_client_main.h"
18 
19 #include "common_utils.h"
20 #include "log_catcher_utils.h"
21 namespace OHOS {
22 namespace HiviewDFX {
CommandCatcher()23 CommandCatcher::CommandCatcher() : EventLogCatcher()
24 {
25     name_ = "CommandCatcher";
26 }
27 
AddCmd(const std::vector<std::string> & cmd)28 void CommandCatcher::AddCmd(const std::vector<std::string>& cmd)
29 {
30     cmdString_.push_back(cmd);
31 }
32 
Initialize(const std::string & packageNam,int pid,int intParam)33 bool CommandCatcher::Initialize(const std::string& packageNam, int pid, int intParam)
34 {
35     if (pid <= 0 && packageNam.length() == 0) {
36         description_ = "CommandCatcher -- pid is null, packageName is null\n";
37         return false;
38     }
39     pid_ = pid;
40     packageName_ = packageNam;
41 
42     if (pid_ <= 0) {
43         description_ = "CommandCatcher -- packageName is " + packageName_ + " pid is null\n";
44         return false;
45     }
46 
47     if (packageName_.length() == 0) {
48         packageName_ = "(null)";
49     }
50 
51     description_ = "CommandCatcher -- pid==" + std::to_string(pid_) + " packageName is " + packageName_ + "\n";
52     return EventLogCatcher::Initialize(packageNam, pid, intParam);
53 }
54 
Catch(int fd)55 int CommandCatcher::Catch(int fd)
56 {
57     if (pid_ <= 0) {
58         return -1;
59     }
60     auto originSize = GetFdSize(fd);
61 
62     for (auto& args : cmdString_) {
63         HiDumper(fd, args);
64     }
65 
66     logSize_ = GetFdSize(fd) - originSize;
67     return logSize_;
68 }
69 
HiDumper(int fd,const std::vector<std::string> & args)70 int CommandCatcher::HiDumper(int fd, const std::vector<std::string> &args)
71 {
72     int argc = args.size();
73 
74     std::vector<char *> argv;
75     for (const auto &arg : args) {
76         argv.push_back(const_cast<char *>(arg.c_str()));
77     }
78 
79     return DumpClientMain::GetInstance().Main(argc, &argv[0], fd);
80 }
81 } // namespace HiviewDFX
82 } // namespace OHOS