106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#ifndef STACK_DATA_REPEATER_H
1606f6ba60Sopenharmony_ci#define STACK_DATA_REPEATER_H
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include <condition_variable>
1906f6ba60Sopenharmony_ci#include <deque>
2006f6ba60Sopenharmony_ci#include <memory>
2106f6ba60Sopenharmony_ci#include <mutex>
2206f6ba60Sopenharmony_ci#include "logging.h"
2306f6ba60Sopenharmony_ci#include "nocopyable.h"
2406f6ba60Sopenharmony_ci#include "native_hook_result.pb.h"
2506f6ba60Sopenharmony_ci#include "hook_common.h"
2606f6ba60Sopenharmony_ci#include "utilities.h"
2706f6ba60Sopenharmony_ci
2806f6ba60Sopenharmony_ciusing BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData);
2906f6ba60Sopenharmony_ciconstexpr const int CACHE_DATA_SIZE = 40000;
3006f6ba60Sopenharmony_ciconstexpr const int32_t CACHE_ARRAY_SIZE = 10;
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_ciclass StackDataRepeater {
3306f6ba60Sopenharmony_cipublic:
3406f6ba60Sopenharmony_ci    struct RawStack {
3506f6ba60Sopenharmony_ci        std::unique_ptr<uint8_t[]> baseStackData = nullptr; // save the shared memory data
3606f6ba60Sopenharmony_ci        BaseStackRawData* stackConext = nullptr; // points to the foundation type data
3706f6ba60Sopenharmony_ci        union {
3806f6ba60Sopenharmony_ci            uint8_t* stackData;
3906f6ba60Sopenharmony_ci            const char* jsStackData = nullptr;
4006f6ba60Sopenharmony_ci        };
4106f6ba60Sopenharmony_ci        uint8_t* data = nullptr; // fp mode data is ip, dwarf mode data is regs
4206f6ba60Sopenharmony_ci        uint64_t freeData = 0;
4306f6ba60Sopenharmony_ci        uint32_t stackSize = 0;
4406f6ba60Sopenharmony_ci        uint8_t fpDepth = 0; // fp mode fpDepth is ip depth, dwarf mode is invalid
4506f6ba60Sopenharmony_ci        bool reportFlag = false;
4606f6ba60Sopenharmony_ci        bool reduceStackFlag = false;
4706f6ba60Sopenharmony_ci        void Reset()
4806f6ba60Sopenharmony_ci        {
4906f6ba60Sopenharmony_ci            baseStackData = nullptr;
5006f6ba60Sopenharmony_ci            stackConext = nullptr;
5106f6ba60Sopenharmony_ci            data = nullptr;
5206f6ba60Sopenharmony_ci            stackData = nullptr;
5306f6ba60Sopenharmony_ci            freeData = 0;
5406f6ba60Sopenharmony_ci            stackSize = 0;
5506f6ba60Sopenharmony_ci            fpDepth = 0;
5606f6ba60Sopenharmony_ci            reportFlag = false;
5706f6ba60Sopenharmony_ci            reduceStackFlag = false;
5806f6ba60Sopenharmony_ci        }
5906f6ba60Sopenharmony_ci    };
6006f6ba60Sopenharmony_ci
6106f6ba60Sopenharmony_ci    explicit StackDataRepeater(size_t maxSize);
6206f6ba60Sopenharmony_ci    ~StackDataRepeater();
6306f6ba60Sopenharmony_ci    bool PutRawStack(const std::shared_ptr<RawStack>& rawData, bool isRecordAccurately);
6406f6ba60Sopenharmony_ci    bool PutRawStackArray(std::array<std::shared_ptr<RawStack>, CACHE_ARRAY_SIZE>& rawDataArray, uint32_t batchCount);
6506f6ba60Sopenharmony_ci    std::shared_ptr<RawStack> TakeRawData(uint32_t during, clockid_t clockId, uint32_t batchCount,
6606f6ba60Sopenharmony_ci        std::shared_ptr<RawStack> batchRawStack[], uint32_t statInterval, bool& isTimeOut);
6706f6ba60Sopenharmony_ci    void Close();
6806f6ba60Sopenharmony_ci    void Reset();
6906f6ba60Sopenharmony_ci    std::shared_ptr<RawStack> GetRawStack();
7006f6ba60Sopenharmony_ci    void ReturnRawStack(std::shared_ptr<RawStack> rawStack);
7106f6ba60Sopenharmony_ci    size_t Size();
7206f6ba60Sopenharmony_ci    void ClearCache()
7306f6ba60Sopenharmony_ci    {
7406f6ba60Sopenharmony_ci        std::unique_lock<std::mutex> lock(cacheMutex_);
7506f6ba60Sopenharmony_ci        rawDataCacheQueue_.clear();
7606f6ba60Sopenharmony_ci    }
7706f6ba60Sopenharmony_ci
7806f6ba60Sopenharmony_ciprivate:
7906f6ba60Sopenharmony_ci    std::mutex mutex_;
8006f6ba60Sopenharmony_ci    std::mutex cacheMutex_;
8106f6ba60Sopenharmony_ci    std::deque<std::shared_ptr<RawStack>> rawDataCacheQueue_;
8206f6ba60Sopenharmony_ci    std::condition_variable slotCondVar_;
8306f6ba60Sopenharmony_ci    std::condition_variable itemCondVar_;
8406f6ba60Sopenharmony_ci    std::deque<std::shared_ptr<RawStack>> rawDataQueue_;
8506f6ba60Sopenharmony_ci    std::unordered_map<void *, std::shared_ptr<RawStack>> mallocMap_ = {};
8606f6ba60Sopenharmony_ci    size_t maxSize_;
8706f6ba60Sopenharmony_ci    uint64_t reducedStackCount_;
8806f6ba60Sopenharmony_ci    std::atomic_bool closed_;
8906f6ba60Sopenharmony_ci
9006f6ba60Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(StackDataRepeater);
9106f6ba60Sopenharmony_ci};
9206f6ba60Sopenharmony_ci
9306f6ba60Sopenharmony_ciusing RawStackPtr = STD_PTR(shared, StackDataRepeater::RawStack);
9406f6ba60Sopenharmony_ci
9506f6ba60Sopenharmony_ciusing StackDataRepeaterPtr = STD_PTR(shared, StackDataRepeater);
9606f6ba60Sopenharmony_ci
9706f6ba60Sopenharmony_ci#endif // STACK_DATA_REPEATER_H