Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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_KALLSYMS_H_ |
| 18 | #define SIMPLE_PERF_KALLSYMS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include "environment.h" |
| 23 | |
| 24 | namespace simpleperf { |
| 25 | |
| 26 | struct KernelSymbol { |
| 27 | uint64_t addr; |
| 28 | char type; |
| 29 | const char* name; |
| 30 | const char* module; // If nullptr, the symbol is not in a kernel module. |
| 31 | }; |
| 32 | |
| 33 | // Parses symbol_data as the content of /proc/kallsyms, calling the callback for |
| 34 | // each symbol that is found. Stops the parsing if the callback returns true. |
| 35 | bool ProcessKernelSymbols(std::string& symbol_data, |
| 36 | const std::function<bool(const KernelSymbol&)>& callback); |
| 37 | |
Yabin Cui | 36b57d9 | 2020-12-17 17:06:27 -0800 | [diff] [blame] | 38 | #if defined(__linux__) |
| 39 | |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 40 | // Returns the list of currently loaded kernel modules. |
| 41 | std::vector<KernelMmap> GetLoadedModules(); |
| 42 | |
| 43 | // Returns the start address of the kernel. It uses /proc/kallsyms to find this |
| 44 | // address. Returns 0 if unknown. |
| 45 | uint64_t GetKernelStartAddress(); |
| 46 | |
| 47 | // Loads the /proc/kallsyms file, requesting access if required. The value of |
| 48 | // kptr_restrict might be modified during the process. Its original value will |
| 49 | // be restored. This usually requires root privileges. |
Yabin Cui | 58740ff | 2021-03-23 13:34:51 -0700 | [diff] [blame] | 50 | bool LoadKernelSymbols(std::string* kallsyms); |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 51 | |
Yabin Cui | 6e7f33a | 2021-05-05 15:43:35 -0700 | [diff] [blame] | 52 | // only for testing |
| 53 | void ResetKernelAddressWarning(); |
| 54 | |
Yabin Cui | 36b57d9 | 2020-12-17 17:06:27 -0800 | [diff] [blame] | 55 | #endif // defined(__linux__) |
| 56 | |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 57 | } // namespace simpleperf |
| 58 | |
| 59 | #endif // SIMPLE_PERF_KALLSYMS_H_ |