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 TracingFormat {
45   std::string system_name;
46   std::string name;
47   uint64_t id;
48   std::vector<TracingField> fields;
49 
GetFieldTracingFormat50   void GetField(const std::string& name, TracingFieldPlace& place) {
51     const TracingField& field = GetField(name);
52     place.offset = field.offset;
53     place.size = field.elem_size;
54   }
55 
56  private:
GetFieldTracingFormat57   const TracingField& GetField(const std::string& name) {
58     for (const auto& field : fields) {
59       if (field.name == name) {
60         return field;
61       }
62     }
63     LOG(FATAL) << "Couldn't find field " << name << "in TracingFormat of "
64                << this->name;
65     return fields[0];
66   }
67 };
68 
69 class TracingFile;
70 
71 class Tracing {
72  public:
73   explicit Tracing(const std::vector<char>& data);
74   ~Tracing();
75   void Dump(size_t indent);
76   TracingFormat GetTracingFormatHavingId(uint64_t trace_event_id);
77   std::string GetTracingEventNameHavingId(uint64_t trace_event_id);
78   const std::string& GetKallsyms() const;
79   uint32_t GetPageSize() const;
80 
81  private:
82   TracingFile* tracing_file_;
83   std::vector<TracingFormat> tracing_formats_;
84 };
85 
86 bool GetTracingData(const std::vector<const EventType*>& event_types,
87                     std::vector<char>* data);
88 
89 #endif  // SIMPLE_PERF_TRACING_H_
90