blob: 72ff2eaf9b08fdb183626b08e3e8b905412d2c4a [file] [log] [blame]
Yabin Cui8f622512015-05-05 19:58:07 -07001/*
2 * Copyright (C) 2015 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_BUILD_ID_H_
18#define SIMPLE_PERF_BUILD_ID_H_
19
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020020#include <android-base/stringprintf.h>
Yabin Cui638c5582015-07-01 16:16:57 -070021#include <string.h>
22#include <algorithm>
Yabin Cuia0208222021-12-10 10:24:29 -080023#include <string>
24#include <string_view>
Yabin Cui8f622512015-05-05 19:58:07 -070025
Yabin Cuifaa7b922021-01-11 17:35:57 -080026namespace simpleperf {
27
Yabin Cui638c5582015-07-01 16:16:57 -070028constexpr size_t BUILD_ID_SIZE = 20;
Yabin Cui8f622512015-05-05 19:58:07 -070029
Yabin Cui42c8dc62016-06-03 15:06:53 -070030// Shared libraries can have a section called .note.gnu.build-id, containing
31// a ~20 bytes unique id. Build id is used to compare if two shared libraries
32// are actually the same. BuildId class is the representation of build id in
33// memory.
Yabin Cui638c5582015-07-01 16:16:57 -070034class BuildId {
35 public:
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020036 static size_t Size() { return BUILD_ID_SIZE; }
Yabin Cui638c5582015-07-01 16:16:57 -070037
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020038 BuildId() { memset(data_, '\0', BUILD_ID_SIZE); }
Yabin Cui638c5582015-07-01 16:16:57 -070039
Yabin Cuib1a885b2016-02-14 19:18:02 -080040 // Copy build id from a byte array, like {0x76, 0x00, 0x32,...}.
41 BuildId(const void* data, size_t len) : BuildId() {
Yabin Cui638c5582015-07-01 16:16:57 -070042 memcpy(data_, data, std::min(len, BUILD_ID_SIZE));
43 }
44
Yabin Cuib1a885b2016-02-14 19:18:02 -080045 // Read build id from a hex string, like "7600329e31058e12b145d153ef27cd40e1a5f7b9".
46 explicit BuildId(const std::string& s) : BuildId() {
47 for (size_t i = 0; i < s.size() && i < BUILD_ID_SIZE * 2; i += 2) {
48 unsigned char ch = 0;
49 for (size_t j = i; j < i + 2; ++j) {
50 ch <<= 4;
51 if (s[j] >= '0' && s[j] <= '9') {
52 ch |= s[j] - '0';
53 } else if (s[j] >= 'a' && s[j] <= 'f') {
54 ch |= s[j] - 'a' + 10;
55 } else if (s[j] >= 'A' && s[j] <= 'F') {
56 ch |= s[j] - 'A' + 10;
57 }
58 }
59 data_[i / 2] = ch;
60 }
61 }
62
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020063 const unsigned char* Data() const { return data_; }
Yabin Cui638c5582015-07-01 16:16:57 -070064
65 std::string ToString() const {
66 std::string s = "0x";
67 for (size_t i = 0; i < BUILD_ID_SIZE; ++i) {
Yabin Cui39d3cae2015-07-13 16:23:13 -070068 s += android::base::StringPrintf("%02x", data_[i]);
Yabin Cui638c5582015-07-01 16:16:57 -070069 }
70 return s;
71 }
72
73 bool operator==(const BuildId& build_id) const {
74 return memcmp(data_, build_id.data_, BUILD_ID_SIZE) == 0;
75 }
76
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020077 bool operator!=(const BuildId& build_id) const { return !(*this == build_id); }
Yabin Cuib1a885b2016-02-14 19:18:02 -080078
Yabin Cui04c70a62015-08-04 14:48:39 -070079 bool IsEmpty() const {
80 static BuildId empty_build_id;
81 return *this == empty_build_id;
82 }
83
Yabin Cui638c5582015-07-01 16:16:57 -070084 private:
85 unsigned char data_[BUILD_ID_SIZE];
86};
Yabin Cui8f622512015-05-05 19:58:07 -070087
Yabin Cui991477b2020-07-17 16:12:15 -070088inline std::ostream& operator<<(std::ostream& os, const BuildId& build_id) {
89 os << build_id.ToString();
90 return os;
91}
92
Yabin Cuifaa7b922021-01-11 17:35:57 -080093} // namespace simpleperf
94
Yabin Cuia0208222021-12-10 10:24:29 -080095namespace std {
96
97template <>
98struct hash<simpleperf::BuildId> {
99 size_t operator()(const simpleperf::BuildId& build_id) const noexcept {
100 return std::hash<std::string_view>()(
101 std::string_view(reinterpret_cast<const char*>(build_id.Data()), build_id.Size()));
102 }
103};
104
105} // namespace std
106
Yabin Cui8f622512015-05-05 19:58:07 -0700107#endif // SIMPLE_PERF_BUILD_ID_H_