• 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 "logger.h"
28 #include "singleton.h"
29 #include "system_ability.h"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 class HiviewServiceAbility : public SystemAbility,
34     public HiviewServiceAbilityStub {
35     DECLARE_SYSTEM_ABILITY(HiviewServiceAbility);
36 
37 public:
38     HiviewServiceAbility();
39     ~HiviewServiceAbility();
40     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
41     static void StartService(HiviewService *service);
42     static void StartServiceAbility(int sleepS);
43     static HiviewService* GetOrSetHiviewService(HiviewService *service = nullptr);
44 
45     int32_t List(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos) override;
46     int32_t Copy(const std::string& logType, const std::string& logName, const std::string& dest) override;
47     int32_t Move(const std::string& logType, const std::string& logName, const std::string& dest) override;
48     int32_t Remove(const std::string& logType, const std::string& logName) override;
49 
50     CollectResultParcelable<int32_t> OpenSnapshotTrace(const std::vector<std::string>& tagGroups) override;
51     CollectResultParcelable<std::vector<std::string>> DumpSnapshotTrace(int32_t caller) override;
52     CollectResultParcelable<int32_t> OpenRecordingTrace(const std::string& tags) override;
53     CollectResultParcelable<int32_t> RecordingTraceOn() override;
54     CollectResultParcelable<std::vector<std::string>> RecordingTraceOff() override;
55     CollectResultParcelable<int32_t> CloseTrace() override;
56     CollectResultParcelable<int32_t> RecoverTrace() override;
57 
58 protected:
59     void OnDump() override;
60     void OnStart() override;
61     void OnStop() override;
62 
63 private:
64     template<typename T>
TraceCalling(std::function<CollectResult<T> (HiviewService *)> traceRetHandler)65     CollectResultParcelable<T> TraceCalling(std::function<CollectResult<T>(HiviewService*)> traceRetHandler)
66     {
67         auto traceRet = CollectResultParcelable<T>::Init();
68         if (traceRetHandler == nullptr) {
69             return traceRet;
70         }
71         auto service = GetOrSetHiviewService();
72         if (service == nullptr) {
73             return traceRet;
74         }
75         auto collectRet = traceRetHandler(service);
76         return CollectResultParcelable<T>(collectRet);
77     }
78 
79 private:
80     void GetFileInfoUnderDir(const std::string& dirPath, std::vector<HiviewFileInfo>& fileInfos);
81     int32_t CopyOrMoveFile(
82         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
83 };
84 
85 class HiviewServiceAbilityDeathRecipient : public IRemoteObject::DeathRecipient {
86 public:
87     HiviewServiceAbilityDeathRecipient();
88     ~HiviewServiceAbilityDeathRecipient();
89     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
90 };
91 } // namespace HiviewDFX
92 } // namespace OHOS
93 #endif // IHIVIEW_SERVICE_H