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