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 if (argv == nullptr) {
42 LOG_ERR("argument is null.\n");
43 return DumpStatus::DUMP_INVALID_ARG;
44 }
45 for (int i = 0; i < argc; i++) {
46 if (argv[i] == nullptr) {
47 LOG_ERR("argument(%d) is null.\n", i);
48 return DumpStatus::DUMP_INVALID_ARG;
49 }
50 size_t len = strlen(argv[i]);
51 if (len == 0) {
52 LOG_ERR("argument(%d) is empty.\n", i);
53 return DumpStatus::DUMP_INVALID_ARG;
54 }
55 if (len > SINGLE_ARG_MAXLEN) {
56 LOG_ERR("too long argument(%d), limit size %d.\n", i, SINGLE_ARG_MAXLEN);
57 return DumpStatus::DUMP_INVALID_ARG;
58 }
59 }
60 std::vector<std::u16string> args;
61 for (int i = 0; i < argc; i++) {
62 args.push_back(Str8ToStr16(std::string(argv[i])));
63 }
64
65 auto& dumpManagerClient = DumpManagerClient::GetInstance();
66 if (dumpManagerClient.Connect() != ERR_OK) {
67 (void)dprintf(outFd, "connect error\n");
68 return -1;
69 }
70 DumpUtils::IgnoreStdoutCache();
71 int32_t ret = dumpManagerClient.Request(args, outFd);
72 if (ret < DumpStatus::DUMP_OK) {
73 if (ret != DumpStatus::DUMP_INVALID_ARG) {
74 (void)dprintf(outFd, "request error\n");
75 }
76 return ret;
77 }
78 if (!dumpManagerClient.IsConnected()) {
79 (void)dprintf(outFd, "service error\n");
80 }
81 return ret;
82 }
83 } // namespace HiviewDFX
84 } // namespace OHOS
85