• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
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 HOOK_RECORD_FACTORY_H
17 #define HOOK_RECORD_FACTORY_H
18 #include <memory>
19 #include <string>
20 #include <deque>
21 #include "hook_common.h"
22 #include "hook_record.h"
23 
24 namespace OHOS::Developtools::NativeDaemon {
25 constexpr int RAW_DATA_CACHE_INIT_SIZE = 40000;
26 constexpr int HOOK_RECORD_CACHE_INIT_SIZE = 20000;
27 constexpr int RAW_DATA_CACHE_MAX_SIZE = 400000;
28 constexpr int HOOK_RECORD_CACHE_MAX_SIZE = 200000;
29 
30 class HookRecordFactory {
31 public:
32     HookRecordFactory() = delete;
33     HookRecordFactory(NativeHookConfig hookConfig);
34     std::shared_ptr<HookRecord> GetHookRecord(const int8_t sharedMemData[], uint32_t size, bool storeData = true);
35     void SaveHookRecord(HookRecordPtr hookRecord);
36 
37     template <typename T>
38     std::shared_ptr<T> GetRecordFromCache(std::deque<std::shared_ptr<T>>& dataCache, int16_t type);
39     std::shared_ptr<RawStack> GetRawStackFromCache();
40     void SaveRawStack(RawStackPtr rawStack);
41     std::shared_ptr<HookRecord> CreateStackRecord(uint16_t type, RawStackPtr rawStack);
42     void SaveRecordToCache(uint16_t type, HookRecordPtr hookRecord);
43     void InitRawStack(RawStackPtr rawStack, const int8_t sharedMemData[],
44                       uint32_t size, bool storeData);
45 
46 private:
47     NativeHookConfig hookConfig_;
48     std::deque<std::shared_ptr<RawStack>> rawStackCache_;
49     std::deque<std::shared_ptr<HookRecord>> freeRecordSimpCache_;
50     std::deque<std::shared_ptr<HookRecord>> freeRecordCache_;
51     std::deque<std::shared_ptr<HookRecord>> mallocRecordCache_;
52     std::deque<std::shared_ptr<HookRecord>> mmapRecordCache_;
53     std::deque<std::shared_ptr<HookRecord>> munmapRecordCache_;
54     std::deque<std::shared_ptr<HookRecord>> jsRecordCache_;
55     std::mutex rawStackMutex_;
56     std::mutex recordMutex_;
57 };
58 }
59 #endif //HOOK_RECORD_FACTORY_H