OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Defines StructTraits specializations for translating between mojo types and | |
6 // base::StackSamplingProfiler types, with data validity checks. | |
7 | |
8 #ifndef COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
9 #define COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
10 | |
11 #include <vector> | |
12 | |
13 #include "base/files/file_path.h" | |
14 #include "base/profiler/stack_sampling_profiler.h" | |
15 #include "components/metrics/public/interfaces/call_stack_profile_collector.mojo
m.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 template <> | |
20 struct StructTraits<metrics::mojom::CallStackModule, | |
21 base::StackSamplingProfiler::Module> { | |
22 static uint64_t base_address( | |
23 const base::StackSamplingProfiler::Module& module) { | |
24 return module.base_address; | |
25 } | |
26 static const std::string& id( | |
27 const base::StackSamplingProfiler::Module& module) { | |
28 return module.id; | |
29 } | |
30 static const base::FilePath& filename( | |
31 const base::StackSamplingProfiler::Module& module) { | |
32 return module.filename; | |
33 } | |
34 | |
35 static bool Read(metrics::mojom::CallStackModuleDataView data, | |
36 base::StackSamplingProfiler::Module* out) { | |
37 // Linux has the longest build id at 40 bytes. | |
38 static const size_t kMaxIDSize = 40; | |
39 | |
40 std::string id; | |
41 base::FilePath filename; | |
42 if (!data.ReadId(&id) || id.size() > kMaxIDSize || | |
43 !data.ReadFilename(&filename)) | |
44 return false; | |
45 | |
46 *out = | |
47 base::StackSamplingProfiler::Module(data.base_address(), id, filename); | |
48 return true; | |
49 } | |
50 }; | |
51 | |
52 template <> | |
53 struct StructTraits<metrics::mojom::CallStackFrame, | |
54 base::StackSamplingProfiler::Frame> { | |
55 static uint64_t instruction_pointer( | |
56 const base::StackSamplingProfiler::Frame& frame) { | |
57 return frame.instruction_pointer; | |
58 } | |
59 static uint64_t module_index( | |
60 const base::StackSamplingProfiler::Frame& frame) { | |
61 return frame.module_index == | |
62 base::StackSamplingProfiler::Frame::kUnknownModuleIndex ? | |
63 static_cast<uint64_t>(-1) : | |
64 frame.module_index; | |
65 } | |
66 | |
67 static bool Read(metrics::mojom::CallStackFrameDataView data, | |
68 base::StackSamplingProfiler::Frame* out) { | |
69 size_t module_index = data.module_index() == static_cast<uint64_t>(-1) ? | |
70 base::StackSamplingProfiler::Frame::kUnknownModuleIndex : | |
71 data.module_index(); | |
72 | |
73 // We can't know whether the module_index field is valid at this point since | |
74 // we don't have access to the number of modules here. This will be checked | |
75 // in CallStackProfile's Read function below. | |
76 *out = base::StackSamplingProfiler::Frame(data.instruction_pointer(), | |
77 module_index); | |
78 return true; | |
79 } | |
80 }; | |
81 | |
82 template <> | |
83 struct StructTraits<metrics::mojom::CallStackProfile, | |
84 base::StackSamplingProfiler::CallStackProfile> { | |
85 static const std::vector<base::StackSamplingProfiler::Module>& modules( | |
86 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
87 return profile.modules; | |
88 } | |
89 static const std::vector<base::StackSamplingProfiler::Sample>& samples( | |
90 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
91 return profile.samples; | |
92 } | |
93 static const base::TimeDelta profile_duration( | |
94 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
95 return profile.profile_duration; | |
96 } | |
97 static const base::TimeDelta sampling_period( | |
98 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
99 return profile.sampling_period; | |
100 } | |
101 | |
102 static bool ValidateSamples( | |
103 std::vector<base::StackSamplingProfiler::Sample> samples, | |
104 size_t module_count) { | |
105 for (const base::StackSamplingProfiler::Sample& sample : samples) { | |
106 for (const base::StackSamplingProfiler::Frame& frame : sample) { | |
107 if (frame.module_index >= module_count && | |
108 frame.module_index != | |
109 base::StackSamplingProfiler::Frame::kUnknownModuleIndex) | |
110 return false; | |
111 } | |
112 } | |
113 return true; | |
114 } | |
115 | |
116 static bool Read(metrics::mojom::CallStackProfileDataView data, | |
117 base::StackSamplingProfiler::CallStackProfile* out) { | |
118 std::vector<base::StackSamplingProfiler::Module> modules; | |
119 std::vector<base::StackSamplingProfiler::Sample> samples; | |
120 base::TimeDelta profile_duration, sampling_period; | |
121 if (!data.ReadModules(&modules) || !data.ReadSamples(&samples) || | |
122 !data.ReadProfileDuration(&profile_duration) || | |
123 !data.ReadSamplingPeriod(&sampling_period) || | |
124 !ValidateSamples(samples, modules.size())) | |
125 return false; | |
126 | |
127 *out = base::StackSamplingProfiler::CallStackProfile(); | |
128 out->modules = std::move(modules); | |
129 out->samples = std::move(samples); | |
130 out->profile_duration = profile_duration; | |
131 out->sampling_period = sampling_period; | |
132 return true; | |
133 } | |
134 }; | |
135 | |
136 } // mojo | |
137 | |
138 #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
OLD | NEW |