• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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             DUMPER_HILOGE(MODULE_SERVICE, "request error, ret: %{public}d.", ret);
72         }
73         return ret;
74     }
75     return ret;
76 }
77 
SetCmdArgs(int argc,char * argv[],std::vector<std::u16string> & args)78 void DumpClientMain::SetCmdArgs(int argc, char* argv[], std::vector<std::u16string>& args)
79 {
80     std::stringstream dumpCmdSs;
81     for (int i = 0; i < argc; i++) {
82         args.push_back(Str8ToStr16(std::string(argv[i])));
83         dumpCmdSs << std::string(argv[i]) << " ";
84     }
85     std::string ppid = std::to_string(getppid());
86     args.push_back(Str8ToStr16(ppid));
87     int32_t calllingUid = IPCSkeleton::GetCallingUid();
88     int32_t calllingPid = IPCSkeleton::GetCallingPid();
89     DUMPER_HILOGI(MODULE_SERVICE, "hidumper cmd:%{public}s, calllingUid=%{public}d, calllingPid=%{public}d,"
90         " ppid:%{public}s.", dumpCmdSs.str().c_str(), calllingUid, calllingPid, ppid.c_str());
91 }
92 } // namespace HiviewDFX
93 } // namespace OHOS
94