1b0e7dd80Sopenharmony_ci/*
2b0e7dd80Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b0e7dd80Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b0e7dd80Sopenharmony_ci * you may not use this file except in compliance with the License.
5b0e7dd80Sopenharmony_ci * You may obtain a copy of the License at
6b0e7dd80Sopenharmony_ci *
7b0e7dd80Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b0e7dd80Sopenharmony_ci *
9b0e7dd80Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b0e7dd80Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b0e7dd80Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b0e7dd80Sopenharmony_ci * See the License for the specific language governing permissions and
13b0e7dd80Sopenharmony_ci * limitations under the License.
14b0e7dd80Sopenharmony_ci */
15b0e7dd80Sopenharmony_ci
16b0e7dd80Sopenharmony_ci#include "hitracedump_fuzzer.h"
17b0e7dd80Sopenharmony_ci
18b0e7dd80Sopenharmony_ci#include <cstddef>
19b0e7dd80Sopenharmony_ci#include <cstdint>
20b0e7dd80Sopenharmony_ci#include <string>
21b0e7dd80Sopenharmony_ci#include <iostream>
22b0e7dd80Sopenharmony_ci#include <unistd.h>
23b0e7dd80Sopenharmony_ci#include <vector>
24b0e7dd80Sopenharmony_ci
25b0e7dd80Sopenharmony_ci#include "hitrace_dump.h"
26b0e7dd80Sopenharmony_ci#include "hitrace_fuzztest_common.h"
27b0e7dd80Sopenharmony_ci
28b0e7dd80Sopenharmony_cinamespace OHOS {
29b0e7dd80Sopenharmony_cinamespace HiviewDFX {
30b0e7dd80Sopenharmony_cinamespace Hitrace {
31b0e7dd80Sopenharmony_civoid HitraceDumpCmdModeTest(const uint8_t* data, size_t size)
32b0e7dd80Sopenharmony_ci{
33b0e7dd80Sopenharmony_ci    uint64_t traceTags = 0;
34b0e7dd80Sopenharmony_ci    if (size < sizeof(traceTags)) {
35b0e7dd80Sopenharmony_ci        return;
36b0e7dd80Sopenharmony_ci    }
37b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, traceTags);
38b0e7dd80Sopenharmony_ci    std::string hitraceTags = " sched freq binder idle disk";
39b0e7dd80Sopenharmony_ci    GenerateTagStr(traceTags, hitraceTags);
40b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
41b0e7dd80Sopenharmony_ci    (void)OpenTrace(hitraceTags);
42b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
43b0e7dd80Sopenharmony_ci    (void)DumpTraceOn();
44b0e7dd80Sopenharmony_ci    sleep(1);
45b0e7dd80Sopenharmony_ci    (void)DumpTraceOff();
46b0e7dd80Sopenharmony_ci    (void)CloseTrace();
47b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
48b0e7dd80Sopenharmony_ci}
49b0e7dd80Sopenharmony_ci
50b0e7dd80Sopenharmony_civoid HitraceDumpServiceModeTest(const uint8_t* data, size_t size)
51b0e7dd80Sopenharmony_ci{
52b0e7dd80Sopenharmony_ci    int duration = 0;
53b0e7dd80Sopenharmony_ci    uint64_t happenTime = 0;
54b0e7dd80Sopenharmony_ci    uint64_t traceTags = 0;
55b0e7dd80Sopenharmony_ci    if (size < sizeof(traceTags) + sizeof(duration) + sizeof(happenTime)) {
56b0e7dd80Sopenharmony_ci        return;
57b0e7dd80Sopenharmony_ci    }
58b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, duration);
59b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, happenTime);
60b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, traceTags);
61b0e7dd80Sopenharmony_ci    duration = duration > 30 ? 30 : duration; // 30 : max duration
62b0e7dd80Sopenharmony_ci    std::vector<std::string> hitraceTags = { "sched", "freq", "binder", "idle", "disk" };
63b0e7dd80Sopenharmony_ci    GenerateTagVec(traceTags, hitraceTags);
64b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
65b0e7dd80Sopenharmony_ci    (void)OpenTrace(hitraceTags);
66b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
67b0e7dd80Sopenharmony_ci    sleep(1);
68b0e7dd80Sopenharmony_ci    (void)DumpTrace();
69b0e7dd80Sopenharmony_ci    sleep(1);
70b0e7dd80Sopenharmony_ci    (void)DumpTrace(duration, happenTime);
71b0e7dd80Sopenharmony_ci    (void)CloseTrace();
72b0e7dd80Sopenharmony_ci    std::cout << "trace mode : " << GetTraceMode() << std::endl;
73b0e7dd80Sopenharmony_ci}
74b0e7dd80Sopenharmony_ci} // namespace Hitrace
75b0e7dd80Sopenharmony_ci} // namespace HiviewDFX
76b0e7dd80Sopenharmony_ci} // namespace OHOS
77b0e7dd80Sopenharmony_ci
78b0e7dd80Sopenharmony_ci/* Fuzzer entry point */
79b0e7dd80Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80b0e7dd80Sopenharmony_ci{
81b0e7dd80Sopenharmony_ci    if (data == nullptr || size == 0) {
82b0e7dd80Sopenharmony_ci        return 0;
83b0e7dd80Sopenharmony_ci    }
84b0e7dd80Sopenharmony_ci
85b0e7dd80Sopenharmony_ci    /* Run your code on data */
86b0e7dd80Sopenharmony_ci    OHOS::HiviewDFX::Hitrace::HitraceDumpCmdModeTest(data, size);
87b0e7dd80Sopenharmony_ci    OHOS::HiviewDFX::Hitrace::HitraceDumpServiceModeTest(data, size);
88b0e7dd80Sopenharmony_ci    return 0;
89b0e7dd80Sopenharmony_ci}
90