1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SIMPLE_PERF_TRACING_H_
18 #define SIMPLE_PERF_TRACING_H_
19 
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 
24 #include "event_type.h"
25 #include "utils.h"
26 
27 struct TracingField {
28   std::string name;
29   size_t offset;
30   size_t elem_size;
31   size_t elem_count;
32   bool is_signed;
33 };
34 
35 struct TracingFieldPlace {
36   uint32_t offset;
37   uint32_t size;
38 
ReadFromDataTracingFieldPlace39   uint64_t ReadFromData(const char* raw_data) {
40     return ConvertBytesToValue(raw_data + offset, size);
41   }
42 };
43 
44 struct StringTracingFieldPlace {
45   uint32_t offset;
46   uint32_t size;
47 
ReadFromDataStringTracingFieldPlace48   std::string ReadFromData(const char* raw_data) {
49     char s[size + 1];
50     s[size] = '\0';
51     memcpy(s, raw_data + offset, size);
52     return s;
53   }
54 };
55 
56 struct TracingFormat {
57   std::string system_name;
58   std::string name;
59   uint64_t id;
60   std::vector<TracingField> fields;
61 
GetFieldTracingFormat62   void GetField(const std::string& name, TracingFieldPlace& place) {
63     const TracingField& field = GetField(name);
64     place.offset = field.offset;
65     place.size = field.elem_size;
66   }
67 
GetFieldTracingFormat68   void GetField(const std::string& name, StringTracingFieldPlace& place) {
69     const TracingField& field = GetField(name);
70     place.offset = field.offset;
71     place.size = field.elem_count;
72   }
73 
74  private:
GetFieldTracingFormat75   const TracingField& GetField(const std::string& name) {
76     for (const auto& field : fields) {
77       if (field.name == name) {
78         return field;
79       }
80     }
81     LOG(FATAL) << "Couldn't find field " << name << "in TracingFormat of "
82                << this->name;
83     return fields[0];
84   }
85 };
86 
87 class TracingFile;
88 
89 class Tracing {
90  public:
91   explicit Tracing(const std::vector<char>& data);
92   ~Tracing();
93   void Dump(size_t indent);
94   TracingFormat GetTracingFormatHavingId(uint64_t trace_event_id);
95   std::string GetTracingEventNameHavingId(uint64_t trace_event_id);
96   const std::string& GetKallsyms() const;
97   uint32_t GetPageSize() const;
98 
99  private:
100   TracingFile* tracing_file_;
101   std::vector<TracingFormat> tracing_formats_;
102 };
103 
104 bool GetTracingData(const std::vector<const EventType*>& event_types,
105                     std::vector<char>* data);
106 
107 #endif  // SIMPLE_PERF_TRACING_H_
108