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 
38 #if defined(__linux__)
39 
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.
50 bool LoadKernelSymbols(std::string* kallsyms);
51 
52 // only for testing
53 void ResetKernelAddressWarning();
54 
55 #endif  // defined(__linux__)
56 
57 }  // namespace simpleperf
58 
59 #endif  // SIMPLE_PERF_KALLSYMS_H_
60