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 <string_ex.h>
18 #include <vector>
19
20 #include "common.h"
21 #include "dump_controller.h"
22 #include "dump_manager_client.h"
23 #include "dump_utils.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
DumpClientMain()27 DumpClientMain::DumpClientMain()
28 {
29 }
30
~DumpClientMain()31 DumpClientMain::~DumpClientMain()
32 {
33 }
34
Main(int argc,char * argv[],int outFd)35 int DumpClientMain::Main(int argc, char* argv[], int outFd)
36 {
37 if (argc > ARG_MAX_COUNT) {
38 LOG_ERR("too many arguments(%d), limit size %d.\n", argc, ARG_MAX_COUNT);
39 return DumpStatus::DUMP_INVALID_ARG;
40 }
41 for (int i = 0; i < argc; i++) {
42 if (argv[i] == nullptr) {
43 LOG_ERR("argument(%d) is null.\n", i);
44 return DumpStatus::DUMP_INVALID_ARG;
45 }
46 size_t len = strlen(argv[i]);
47 if (len == 0) {
48 LOG_ERR("argument(%d) is empty.\n", i);
49 return DumpStatus::DUMP_INVALID_ARG;
50 }
51 if (len > SINGLE_ARG_MAXLEN) {
52 LOG_ERR("too long argument(%d), limit size %d.\n", i, SINGLE_ARG_MAXLEN);
53 return DumpStatus::DUMP_INVALID_ARG;
54 }
55 }
56 std::vector<std::u16string> args;
57 for (int i = 0; i < argc; i++) {
58 args.push_back(Str8ToStr16(std::string(argv[i])));
59 }
60
61 auto& dumpManagerClient = DumpManagerClient::GetInstance();
62 if (dumpManagerClient.Connect() != ERR_OK) {
63 (void)dprintf(outFd, "connect error\n");
64 return -1;
65 }
66 DumpUtils::IgnoreStdoutCache();
67 int32_t ret = dumpManagerClient.Request(args, outFd);
68 if (ret < DumpStatus::DUMP_OK) {
69 if (ret != DumpStatus::DUMP_INVALID_ARG) {
70 (void)dprintf(outFd, "request error\n");
71 }
72 return ret;
73 }
74 if (!dumpManagerClient.IsConnected()) {
75 (void)dprintf(outFd, "service error\n");
76 }
77 return ret;
78 }
79 } // namespace HiviewDFX
80 } // namespace OHOS
81