1/*
2 * Copyright (c) 2024 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#include "hitracedump_fuzzer.h"
17
18#include <cstddef>
19#include <cstdint>
20#include <string>
21#include <iostream>
22#include <unistd.h>
23#include <vector>
24
25#include "hitrace_dump.h"
26#include "hitrace_fuzztest_common.h"
27
28namespace OHOS {
29namespace HiviewDFX {
30namespace Hitrace {
31void HitraceDumpCmdModeTest(const uint8_t* data, size_t size)
32{
33    uint64_t traceTags = 0;
34    if (size < sizeof(traceTags)) {
35        return;
36    }
37    StreamToValueInfo(data, traceTags);
38    std::string hitraceTags = " sched freq binder idle disk";
39    GenerateTagStr(traceTags, hitraceTags);
40    std::cout << "trace mode : " << GetTraceMode() << std::endl;
41    (void)OpenTrace(hitraceTags);
42    std::cout << "trace mode : " << GetTraceMode() << std::endl;
43    (void)DumpTraceOn();
44    sleep(1);
45    (void)DumpTraceOff();
46    (void)CloseTrace();
47    std::cout << "trace mode : " << GetTraceMode() << std::endl;
48}
49
50void HitraceDumpServiceModeTest(const uint8_t* data, size_t size)
51{
52    int duration = 0;
53    uint64_t happenTime = 0;
54    uint64_t traceTags = 0;
55    if (size < sizeof(traceTags) + sizeof(duration) + sizeof(happenTime)) {
56        return;
57    }
58    StreamToValueInfo(data, duration);
59    StreamToValueInfo(data, happenTime);
60    StreamToValueInfo(data, traceTags);
61    duration = duration > 30 ? 30 : duration; // 30 : max duration
62    std::vector<std::string> hitraceTags = { "sched", "freq", "binder", "idle", "disk" };
63    GenerateTagVec(traceTags, hitraceTags);
64    std::cout << "trace mode : " << GetTraceMode() << std::endl;
65    (void)OpenTrace(hitraceTags);
66    std::cout << "trace mode : " << GetTraceMode() << std::endl;
67    sleep(1);
68    (void)DumpTrace();
69    sleep(1);
70    (void)DumpTrace(duration, happenTime);
71    (void)CloseTrace();
72    std::cout << "trace mode : " << GetTraceMode() << std::endl;
73}
74} // namespace Hitrace
75} // namespace HiviewDFX
76} // namespace OHOS
77
78/* Fuzzer entry point */
79extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80{
81    if (data == nullptr || size == 0) {
82        return 0;
83    }
84
85    /* Run your code on data */
86    OHOS::HiviewDFX::Hitrace::HitraceDumpCmdModeTest(data, size);
87    OHOS::HiviewDFX::Hitrace::HitraceDumpServiceModeTest(data, size);
88    return 0;
89}
90