• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
16 #ifndef HIVIEW_SERVICE_ABILITY_H
17 #define HIVIEW_SERVICE_ABILITY_H
18 
19 #include <string>
20 #include <memory>
21 
22 #include "hiview_err_code.h"
23 #include "hiview_file_info.h"
24 #include "hiview_service.h"
25 #include "hiview_service_ability_stub.h"
26 #include "ihiview_service_ability.h"
27 #include "hiview_logger.h"
28 #include "singleton.h"
29 #include "system_ability.h"
30 #include "client/trace_collector.h"
31 #include "client/memory_collector.h"
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 class HiviewServiceAbility : public SystemAbility,
36     public HiviewServiceAbilityStub {
37     DECLARE_SYSTEM_ABILITY(HiviewServiceAbility);
38 
39 public:
40     HiviewServiceAbility();
41     ~HiviewServiceAbility();
42     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
43     static void StartService(HiviewService *service);
44     static void StartServiceAbility(int sleepS);
45     static HiviewService* GetOrSetHiviewService(HiviewService *service = nullptr);
46 
47     int32_t List(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos) override;
48     int32_t Copy(const std::string& logType, const std::string& logName, const std::string& dest) override;
49     int32_t Move(const std::string& logType, const std::string& logName, const std::string& dest) override;
50     int32_t Remove(const std::string& logType, const std::string& logName) override;
51 
52     CollectResultParcelable<int32_t> OpenSnapshotTrace(const std::vector<std::string>& tagGroups) override;
53     CollectResultParcelable<std::vector<std::string>> DumpSnapshotTrace(int32_t caller) override;
54     CollectResultParcelable<int32_t> OpenRecordingTrace(const std::string& tags) override;
55     CollectResultParcelable<int32_t> RecordingTraceOn() override;
56     CollectResultParcelable<std::vector<std::string>> RecordingTraceOff() override;
57     CollectResultParcelable<int32_t> CloseTrace() override;
58     CollectResultParcelable<int32_t> RecoverTrace() override;
59     CollectResultParcelable<int32_t> CaptureDurationTrace(UCollectClient::AppCaller &appCaller) override;
60     CollectResultParcelable<double> GetSysCpuUsage() override;
61     CollectResultParcelable<int32_t> SetAppResourceLimit(UCollectClient::MemoryCaller& memoryCaller) override;
62 
63 protected:
64     void OnDump() override;
65     void OnStart() override;
66     void OnStop() override;
67 
68 private:
69     template<typename T>
TraceCalling(std::function<CollectResult<T> (HiviewService *)> traceRetHandler)70     CollectResultParcelable<T> TraceCalling(std::function<CollectResult<T>(HiviewService*)> traceRetHandler)
71     {
72         auto traceRet = CollectResultParcelable<T>::Init();
73         if (traceRetHandler == nullptr) {
74             return traceRet;
75         }
76         auto service = GetOrSetHiviewService();
77         if (service == nullptr) {
78             return traceRet;
79         }
80         auto collectRet = traceRetHandler(service);
81         return CollectResultParcelable<T>(collectRet);
82     }
83 
84 private:
85     void GetFileInfoUnderDir(const std::string& dirPath, std::vector<HiviewFileInfo>& fileInfos);
86     int32_t CopyOrMoveFile(
87         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
88 };
89 
90 class HiviewServiceAbilityDeathRecipient : public IRemoteObject::DeathRecipient {
91 public:
92     HiviewServiceAbilityDeathRecipient();
93     ~HiviewServiceAbilityDeathRecipient();
94     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
95 };
96 } // namespace HiviewDFX
97 } // namespace OHOS
98 #endif // IHIVIEW_SERVICE_H