1 /*
2  * Copyright (c) 2021-2022 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 "dump_client_main.h"
16 
17 #include <ipc_skeleton.h>
18 #include <sstream>
19 #include <string_ex.h>
20 #include <vector>
21 
22 #include "common.h"
23 #include "dump_controller.h"
24 #include "dump_manager_client.h"
25 #include "dump_utils.h"
26 #include "hilog_wrapper.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
DumpClientMain()30 DumpClientMain::DumpClientMain()
31 {
32 }
33 
~DumpClientMain()34 DumpClientMain::~DumpClientMain()
35 {
36 }
37 
Main(int argc,char * argv[],int outFd)38 int DumpClientMain::Main(int argc, char* argv[], int outFd)
39 {
40     if (argc > ARG_MAX_COUNT) {
41         LOG_ERR("too many arguments(%d), limit size %d.\n", argc, ARG_MAX_COUNT);
42         return DumpStatus::DUMP_INVALID_ARG;
43     }
44     if (argv == nullptr) {
45         LOG_ERR("argument is null.\n");
46         return DumpStatus::DUMP_INVALID_ARG;
47     }
48     for (int i = 0; i < argc; i++) {
49         if (argv[i] == nullptr) {
50             LOG_ERR("argument(%d) is null.\n", i);
51             return DumpStatus::DUMP_INVALID_ARG;
52         }
53         size_t len = strlen(argv[i]);
54         if (len == 0) {
55             LOG_ERR("argument(%d) is empty.\n", i);
56             return DumpStatus::DUMP_INVALID_ARG;
57         }
58         if (len > SINGLE_ARG_MAXLEN) {
59             LOG_ERR("too long argument(%d), limit size %d.\n", i, SINGLE_ARG_MAXLEN);
60             return DumpStatus::DUMP_INVALID_ARG;
61         }
62     }
63     std::vector<std::u16string> args;
64     SetCmdArgs(argc, argv, args);
65     auto& dumpManagerClient = DumpManagerClient::GetInstance();
66     if (dumpManagerClient.Connect() != ERR_OK) {
67         (void)dprintf(outFd, "connect error\n");
68         DUMPER_HILOGE(MODULE_SERVICE, "connect error.");
69         return -1;
70     }
71     DumpUtils::IgnoreStdoutCache();
72     int32_t ret = dumpManagerClient.Request(args, outFd);
73     if (ret < DumpStatus::DUMP_OK) {
74         if (ret != DumpStatus::DUMP_INVALID_ARG) {
75             (void)dprintf(outFd, "request error\n");
76             DUMPER_HILOGE(MODULE_SERVICE, "request error, ret: %{public}d.", ret);
77         }
78         return ret;
79     }
80     if (!dumpManagerClient.IsConnected()) {
81         (void)dprintf(outFd, "service error\n");
82         DUMPER_HILOGE(MODULE_SERVICE, "service error.");
83     }
84     return ret;
85 }
86 
SetCmdArgs(int argc,char * argv[],std::vector<std::u16string> & args)87 void DumpClientMain::SetCmdArgs(int argc, char* argv[], std::vector<std::u16string>& args)
88 {
89     std::stringstream dumpCmdSs;
90     for (int i = 0; i < argc; i++) {
91         args.push_back(Str8ToStr16(std::string(argv[i])));
92         dumpCmdSs << std::string(argv[i]) << " ";
93     }
94     int32_t calllingUid = IPCSkeleton::GetCallingUid();
95     int32_t calllingPid = IPCSkeleton::GetCallingPid();
96     DUMPER_HILOGI(MODULE_SERVICE, "hidumper cmd:%{public}s, calllingUid=%{public}d, calllingPid=%{public}d.",
97         dumpCmdSs.str().c_str(), calllingUid, calllingPid);
98 }
99 } // namespace HiviewDFX
100 } // namespace OHOS
101