• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef SUB_EVENT_PARSER_H
16 #define SUB_EVENT_PARSER_H
17 #include <cstdint>
18 #include <functional>
19 #include <map>
20 #include "ftrace_field_parser.h"
21 #include "trace_plugin_result.pb.h"
22 
23 FTRACE_NS_BEGIN
24 class SubEventParserRegisterar;
25 class SubEventParser {
26 public:
27     static SubEventParser& GetInstance();
28 
29     bool IsSupport(uint32_t eventId) const;
30     bool IsSupport(const std::string& eventName) const;
31     bool ParseEvent(FtraceEvent& event, uint8_t data[], size_t size, const EventFormat& format) const;
32     bool SetupEvent(const EventFormat& format);
33 
34     using ParseFunction = std::function<void(FtraceEvent&, uint8_t[], size_t, const EventFormat&)>;
35 
36 protected:
37     friend class SubEventParserRegisterar;
38     void RegisterParseFunction(const std::string& name, ParseFunction&& func);
39     void UnregisterParseFunction(const std::string& name);
40 
41 private:
42     SubEventParser();
43     ~SubEventParser();
44     DISALLOW_COPY_AND_MOVE(SubEventParser);
45     std::map<uint32_t, ParseFunction> idToFunctions_;
46     std::map<std::string, ParseFunction> nameToFunctions_;
47 };
48 
49 class SubEventParserRegisterar {
50 public:
51     SubEventParserRegisterar(const std::string& name, SubEventParser::ParseFunction&& func);
52     ~SubEventParserRegisterar();
53 
54 private:
55     DISALLOW_COPY_AND_MOVE(SubEventParserRegisterar);
56     std::string name_;
57 };
58 FTRACE_NS_END
59 
60 #define REGISTER_FTRACE_EVENT_PARSE_FUNCTION(name, func) \
61     static FTRACE_NS::SubEventParserRegisterar g_eventRegisterar##name(#name, func)
62 
63 #endif // SUB_EVENT_PARSER_H
64