xref: /aosp_15_r20/external/perfetto/src/trace_processor/util/build_id.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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 SRC_TRACE_PROCESSOR_UTIL_BUILD_ID_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_BUILD_ID_H_
19 
20 #include <string>
21 #include <utility>
22 
23 #include "perfetto/ext/base/hash.h"
24 #include "perfetto/ext/base/string_view.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 // Represents the unique identifier of an executable, shared library, or module.
30 // For example for ELF files this is the id stored in the .note.gnu.build-id
31 // section. Sometimes a breakpad module id is used.
32 // This class abstracts away the details of where this id comes from and how it
33 // is converted to a StringId which is the representation used by tables in
34 // trace_processor.
35 class BuildId {
36  public:
37   // Allow hashing with base::Hash.
38   static constexpr bool kHashable = true;
size()39   size_t size() const { return raw_.size(); }
data()40   const char* data() const { return raw_.data(); }
41 
42   static BuildId FromHex(base::StringView data);
43 
FromRaw(base::StringView data)44   static BuildId FromRaw(base::StringView data) {
45     return BuildId(data.ToStdString());
46   }
FromRaw(std::string data)47   static BuildId FromRaw(std::string data) { return BuildId(std::move(data)); }
FromRaw(const uint8_t * data,size_t size)48   static BuildId FromRaw(const uint8_t* data, size_t size) {
49     return BuildId(std::string(reinterpret_cast<const char*>(data), size));
50   }
51 
52   BuildId(const BuildId&) = default;
53   BuildId(BuildId&&) = default;
54 
55   BuildId& operator=(const BuildId&) = default;
56   BuildId& operator=(BuildId&&) = default;
57 
58   bool operator==(const BuildId& o) const { return raw_ == o.raw_; }
59 
60   bool operator!=(const BuildId& o) const { return !(*this == o); }
61 
62   bool operator<(const BuildId& o) const { return raw_ < o.raw_; }
63 
64   std::string ToHex() const;
65 
raw()66   const std::string& raw() const { return raw_; }
67 
68  private:
BuildId(std::string data)69   explicit BuildId(std::string data) : raw_(std::move(data)) {}
70   std::string raw_;
71 };
72 
73 }  // namespace trace_processor
74 }  // namespace perfetto
75 
76 template <>
77 struct std::hash<perfetto::trace_processor::BuildId> {
78   std::size_t operator()(
79       const perfetto::trace_processor::BuildId& o) const noexcept {
80     return perfetto::base::Hasher::Combine(o);
81   }
82 };
83 
84 #endif  // SRC_TRACE_PROCESSOR_UTIL_BUILD_ID_H_
85