Vicent Marti | 411f698 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 GitHub, Inc. |
| 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 | */ |
Teng Qin | 5440af3 | 2017-04-07 10:57:14 -0700 | [diff] [blame] | 16 | |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/mman.h> |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 23 | #include <math.h> |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 30 | |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 31 | #include "bcc_perf_map.h" |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 32 | #include "bcc_proc.h" |
| 33 | #include "bcc_elf.h" |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 34 | |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 35 | #ifdef __x86_64__ |
| 36 | // https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt |
| 37 | const unsigned long long kernelAddrSpace = 0x00ffffffffffffff; |
| 38 | #else |
| 39 | const unsigned long long kernelAddrSpace = 0x0; |
| 40 | #endif |
| 41 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 42 | char *bcc_procutils_which(const char *binpath) { |
| 43 | char buffer[4096]; |
| 44 | const char *PATH; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 45 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 46 | if (strchr(binpath, '/')) |
Teng Qin | d82e813 | 2017-05-04 12:09:14 -0700 | [diff] [blame] | 47 | return bcc_elf_is_exe(binpath) ? strdup(binpath) : 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 48 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 49 | if (!(PATH = getenv("PATH"))) |
| 50 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 51 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 52 | while (PATH) { |
| 53 | const char *next = strchr(PATH, ':') ?: strchr(PATH, '\0'); |
| 54 | const size_t path_len = next - PATH; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 55 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 56 | if (path_len) { |
Christian Resell | 5a7882e | 2017-05-26 21:44:07 +0200 | [diff] [blame] | 57 | int ret = snprintf(buffer, sizeof(buffer), "%.*s/%s", |
| 58 | (int)path_len, PATH, binpath); |
| 59 | if (ret < 0 || ret >= sizeof(buffer)) |
| 60 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 61 | |
Teng Qin | d82e813 | 2017-05-04 12:09:14 -0700 | [diff] [blame] | 62 | if (bcc_elf_is_exe(buffer)) |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 63 | return strdup(buffer); |
| 64 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 65 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 66 | PATH = *next ? (next + 1) : 0; |
| 67 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 68 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 69 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Vicent Marti | 8600ffc | 2017-06-27 15:22:38 +0200 | [diff] [blame] | 72 | #define STARTS_WITH(mapname, prefix) (!strncmp(mapname, prefix, sizeof(prefix)-1)) |
| 73 | |
Mark Drayton | 1368508 | 2017-02-22 02:05:10 +0000 | [diff] [blame] | 74 | int bcc_mapping_is_file_backed(const char *mapname) { |
Vicent Marti | 8600ffc | 2017-06-27 15:22:38 +0200 | [diff] [blame] | 75 | return mapname[0] && !( |
| 76 | STARTS_WITH(mapname, "//anon") || |
| 77 | STARTS_WITH(mapname, "/dev/zero") || |
| 78 | STARTS_WITH(mapname, "/anon_hugepage") || |
| 79 | STARTS_WITH(mapname, "[stack") || |
| 80 | STARTS_WITH(mapname, "/SYSV") || |
| 81 | STARTS_WITH(mapname, "[heap]") || |
Teng Qin | 1670d33 | 2017-10-06 18:08:33 -0700 | [diff] [blame] | 82 | STARTS_WITH(mapname, "[vsyscall]")); |
Mark Drayton | 1368508 | 2017-02-22 02:05:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 85 | int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, |
| 86 | void *payload) { |
| 87 | char procmap_filename[128]; |
| 88 | FILE *procmap; |
Teng Qin | a0c5de1 | 2017-09-25 15:13:23 -0700 | [diff] [blame] | 89 | snprintf(procmap_filename, sizeof(procmap_filename), "/proc/%ld/maps", |
| 90 | (long)pid); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 91 | procmap = fopen(procmap_filename, "r"); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 92 | if (!procmap) |
| 93 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 94 | |
Teng Qin | a9e5f53 | 2017-10-06 17:11:11 -0700 | [diff] [blame] | 95 | char buf[PATH_MAX + 1], perm[5], dev[8]; |
| 96 | char *name; |
| 97 | uint64_t begin, end, inode; |
| 98 | unsigned long long offset; |
| 99 | while (true) { |
| 100 | buf[0] = '\0'; |
| 101 | // From fs/proc/task_mmu.c:show_map_vma |
| 102 | if (fscanf(procmap, "%lx-%lx %s %llx %s %lu%[^\n]", &begin, &end, perm, |
| 103 | &offset, dev, &inode, buf) != 7) |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 104 | break; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 105 | |
Teng Qin | a9e5f53 | 2017-10-06 17:11:11 -0700 | [diff] [blame] | 106 | if (perm[2] != 'x') |
| 107 | continue; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 108 | |
Teng Qin | a9e5f53 | 2017-10-06 17:11:11 -0700 | [diff] [blame] | 109 | name = buf; |
| 110 | while (isspace(*name)) |
| 111 | name++; |
| 112 | if (!bcc_mapping_is_file_backed(name)) |
| 113 | continue; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 114 | |
Teng Qin | a9e5f53 | 2017-10-06 17:11:11 -0700 | [diff] [blame] | 115 | if (callback(name, begin, end, (uint64_t)offset, true, payload) < 0) |
| 116 | break; |
| 117 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 118 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 119 | fclose(procmap); |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 120 | |
Teng Qin | 3bfc877 | 2017-05-04 12:09:19 -0700 | [diff] [blame] | 121 | // Address mapping for the entire address space maybe in /tmp/perf-<PID>.map |
| 122 | // This will be used if symbols aren't resolved in an earlier mapping. |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 123 | char map_path[4096]; |
Teng Qin | 3bfc877 | 2017-05-04 12:09:19 -0700 | [diff] [blame] | 124 | // Try perf-<PID>.map path with process's mount namespace, chroot and NSPID, |
| 125 | // in case it is generated by the process itself. |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 126 | if (bcc_perf_map_path(map_path, sizeof(map_path), pid)) |
Teng Qin | a0c5de1 | 2017-09-25 15:13:23 -0700 | [diff] [blame] | 127 | if (callback(map_path, 0, -1, 0, true, payload) < 0) |
| 128 | return 0; |
Teng Qin | 3bfc877 | 2017-05-04 12:09:19 -0700 | [diff] [blame] | 129 | // Try perf-<PID>.map path with global root and PID, in case it is generated |
| 130 | // by other Process. Avoid checking mount namespace for this. |
| 131 | int res = snprintf(map_path, 4096, "/tmp/perf-%d.map", pid); |
| 132 | if (res > 0 && res < 4096) |
Teng Qin | a0c5de1 | 2017-09-25 15:13:23 -0700 | [diff] [blame] | 133 | if (callback(map_path, 0, -1, 0, false, payload) < 0) |
| 134 | return 0; |
| 135 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 136 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 139 | int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload) { |
| 140 | char line[2048]; |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 141 | char *symname, *endsym; |
Vicent Marti | 6b59b2c | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 142 | FILE *kallsyms; |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 143 | unsigned long long addr; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 144 | |
Vicent Marti | 6b59b2c | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 145 | /* root is needed to list ksym addresses */ |
| 146 | if (geteuid() != 0) |
| 147 | return -1; |
| 148 | |
| 149 | kallsyms = fopen("/proc/kallsyms", "r"); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 150 | if (!kallsyms) |
| 151 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 152 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 153 | while (fgets(line, sizeof(line), kallsyms)) { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 154 | addr = strtoull(line, &symname, 16); |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 155 | if (addr == 0 || addr == ULLONG_MAX) |
| 156 | continue; |
| 157 | if (addr < kernelAddrSpace) |
| 158 | continue; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 159 | |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 160 | symname++; |
| 161 | // Ignore data symbols |
| 162 | if (*symname == 'b' || *symname == 'B' || *symname == 'd' || |
| 163 | *symname == 'D' || *symname == 'r' || *symname =='R') |
| 164 | continue; |
| 165 | |
| 166 | endsym = (symname = symname + 2); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 167 | while (*endsym && !isspace(*endsym)) endsym++; |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 168 | *endsym = '\0'; |
Teng Qin | 8cc8fc3 | 2017-10-05 14:04:33 -0700 | [diff] [blame] | 169 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 170 | callback(symname, addr, payload); |
| 171 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 172 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 173 | fclose(kallsyms); |
| 174 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #define CACHE1_HEADER "ld.so-1.7.0" |
| 178 | #define CACHE1_HEADER_LEN (sizeof(CACHE1_HEADER) - 1) |
| 179 | |
| 180 | #define CACHE2_HEADER "glibc-ld.so.cache" |
| 181 | #define CACHE2_HEADER_LEN (sizeof(CACHE2_HEADER) - 1) |
| 182 | #define CACHE2_VERSION "1.1" |
| 183 | |
| 184 | struct ld_cache1_entry { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 185 | int32_t flags; |
| 186 | uint32_t key; |
| 187 | uint32_t value; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | struct ld_cache1 { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 191 | char header[CACHE1_HEADER_LEN]; |
| 192 | uint32_t entry_count; |
| 193 | struct ld_cache1_entry entries[0]; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | struct ld_cache2_entry { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 197 | int32_t flags; |
| 198 | uint32_t key; |
| 199 | uint32_t value; |
| 200 | uint32_t pad1_; |
| 201 | uint64_t pad2_; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | struct ld_cache2 { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 205 | char header[CACHE2_HEADER_LEN]; |
| 206 | char version[3]; |
| 207 | uint32_t entry_count; |
| 208 | uint32_t string_table_len; |
| 209 | uint32_t pad_[5]; |
| 210 | struct ld_cache2_entry entries[0]; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | static int lib_cache_count; |
| 214 | static struct ld_lib { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 215 | char *libname; |
| 216 | char *path; |
| 217 | int flags; |
| 218 | } * lib_cache; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 219 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 220 | static int read_cache1(const char *ld_map) { |
| 221 | struct ld_cache1 *ldcache = (struct ld_cache1 *)ld_map; |
| 222 | const char *ldstrings = |
| 223 | (const char *)(ldcache->entries + ldcache->entry_count); |
| 224 | uint32_t i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 225 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 226 | lib_cache = |
| 227 | (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); |
| 228 | lib_cache_count = (int)ldcache->entry_count; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 229 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 230 | for (i = 0; i < ldcache->entry_count; ++i) { |
| 231 | const char *key = ldstrings + ldcache->entries[i].key; |
| 232 | const char *val = ldstrings + ldcache->entries[i].value; |
| 233 | const int flags = ldcache->entries[i].flags; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 234 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 235 | lib_cache[i].libname = strdup(key); |
| 236 | lib_cache[i].path = strdup(val); |
| 237 | lib_cache[i].flags = flags; |
| 238 | } |
| 239 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 242 | static int read_cache2(const char *ld_map) { |
| 243 | struct ld_cache2 *ldcache = (struct ld_cache2 *)ld_map; |
| 244 | uint32_t i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 245 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 246 | if (memcmp(ld_map, CACHE2_HEADER, CACHE2_HEADER_LEN)) |
| 247 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 248 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 249 | lib_cache = |
| 250 | (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); |
| 251 | lib_cache_count = (int)ldcache->entry_count; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 252 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 253 | for (i = 0; i < ldcache->entry_count; ++i) { |
| 254 | const char *key = ld_map + ldcache->entries[i].key; |
| 255 | const char *val = ld_map + ldcache->entries[i].value; |
| 256 | const int flags = ldcache->entries[i].flags; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 257 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 258 | lib_cache[i].libname = strdup(key); |
| 259 | lib_cache[i].path = strdup(val); |
| 260 | lib_cache[i].flags = flags; |
| 261 | } |
| 262 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 265 | static int load_ld_cache(const char *cache_path) { |
| 266 | struct stat st; |
| 267 | size_t ld_size; |
| 268 | const char *ld_map; |
| 269 | int ret, fd = open(cache_path, O_RDONLY); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 270 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 271 | if (fd < 0) |
| 272 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 273 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 274 | if (fstat(fd, &st) < 0 || st.st_size < sizeof(struct ld_cache1)) { |
| 275 | close(fd); |
| 276 | return -1; |
| 277 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 278 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 279 | ld_size = st.st_size; |
| 280 | ld_map = (const char *)mmap(NULL, ld_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 281 | if (ld_map == MAP_FAILED) { |
| 282 | close(fd); |
| 283 | return -1; |
| 284 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 285 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 286 | if (memcmp(ld_map, CACHE1_HEADER, CACHE1_HEADER_LEN) == 0) { |
| 287 | const struct ld_cache1 *cache1 = (struct ld_cache1 *)ld_map; |
| 288 | size_t cache1_len = sizeof(struct ld_cache1) + |
| 289 | (cache1->entry_count * sizeof(struct ld_cache1_entry)); |
| 290 | cache1_len = (cache1_len + 0x7) & ~0x7ULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 291 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 292 | if (ld_size > (cache1_len + sizeof(struct ld_cache2))) |
| 293 | ret = read_cache2(ld_map + cache1_len); |
| 294 | else |
| 295 | ret = read_cache1(ld_map); |
| 296 | } else { |
| 297 | ret = read_cache2(ld_map); |
| 298 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 299 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 300 | munmap((void *)ld_map, ld_size); |
| 301 | close(fd); |
| 302 | return ret; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #define LD_SO_CACHE "/etc/ld.so.cache" |
| 306 | #define FLAG_TYPE_MASK 0x00ff |
| 307 | #define TYPE_ELF_LIBC6 0x0003 |
| 308 | #define FLAG_ABI_MASK 0xff00 |
| 309 | #define ABI_SPARC_LIB64 0x0100 |
| 310 | #define ABI_IA64_LIB64 0x0200 |
| 311 | #define ABI_X8664_LIB64 0x0300 |
| 312 | #define ABI_S390_LIB64 0x0400 |
| 313 | #define ABI_POWERPC_LIB64 0x0500 |
Yonghong Song | 7abe63a | 2017-10-24 09:50:55 -0700 | [diff] [blame] | 314 | #define ABI_AARCH64_LIB64 0x0a00 |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 315 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 316 | static bool match_so_flags(int flags) { |
| 317 | if ((flags & FLAG_TYPE_MASK) != TYPE_ELF_LIBC6) |
| 318 | return false; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 319 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 320 | switch (flags & FLAG_ABI_MASK) { |
Vicent Marti | 2153673 | 2016-04-20 13:37:55 +0200 | [diff] [blame] | 321 | case ABI_SPARC_LIB64: |
| 322 | case ABI_IA64_LIB64: |
| 323 | case ABI_X8664_LIB64: |
| 324 | case ABI_S390_LIB64: |
| 325 | case ABI_POWERPC_LIB64: |
Yonghong Song | 7abe63a | 2017-10-24 09:50:55 -0700 | [diff] [blame] | 326 | case ABI_AARCH64_LIB64: |
Vicent Marti | 2153673 | 2016-04-20 13:37:55 +0200 | [diff] [blame] | 327 | return (sizeof(void *) == 8); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 328 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 329 | |
Ulrich Drepper | ce0981b | 2017-05-04 17:09:51 +0200 | [diff] [blame] | 330 | return sizeof(void *) == 4; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 333 | static bool which_so_in_process(const char* libname, int pid, char* libpath) { |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 334 | int ret, found = false; |
| 335 | char endline[4096], *mapname = NULL, *newline; |
| 336 | char mappings_file[128]; |
| 337 | const size_t search_len = strlen(libname) + strlen("/lib."); |
| 338 | char search1[search_len + 1]; |
| 339 | char search2[search_len + 1]; |
| 340 | |
Nan Xiao | 0379779 | 2017-07-29 10:47:24 +0800 | [diff] [blame] | 341 | snprintf(mappings_file, sizeof(mappings_file), "/proc/%ld/maps", (long)pid); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 342 | FILE *fp = fopen(mappings_file, "r"); |
| 343 | if (!fp) |
| 344 | return NULL; |
| 345 | |
| 346 | snprintf(search1, search_len + 1, "/lib%s.", libname); |
| 347 | snprintf(search2, search_len + 1, "/lib%s-", libname); |
| 348 | |
| 349 | do { |
| 350 | ret = fscanf(fp, "%*x-%*x %*s %*x %*s %*d"); |
| 351 | if (!fgets(endline, sizeof(endline), fp)) |
| 352 | break; |
| 353 | |
| 354 | mapname = endline; |
| 355 | newline = strchr(endline, '\n'); |
| 356 | if (newline) |
| 357 | newline[0] = '\0'; |
| 358 | |
| 359 | while (isspace(mapname[0])) mapname++; |
| 360 | |
| 361 | if (strstr(mapname, ".so") && (strstr(mapname, search1) || |
| 362 | strstr(mapname, search2))) { |
| 363 | found = true; |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 364 | memcpy(libpath, mapname, strlen(mapname) + 1); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } while (ret != EOF); |
| 368 | |
| 369 | fclose(fp); |
| 370 | return found; |
| 371 | } |
| 372 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 373 | char *bcc_procutils_which_so(const char *libname, int pid) { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 374 | const size_t soname_len = strlen(libname) + strlen("lib.so"); |
| 375 | char soname[soname_len + 1]; |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 376 | char libpath[4096]; |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 377 | int i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 378 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 379 | if (strchr(libname, '/')) |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 380 | return strdup(libname); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 381 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 382 | if (pid && which_so_in_process(libname, pid, libpath)) |
| 383 | return strdup(libpath); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 384 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 385 | if (lib_cache_count < 0) |
| 386 | return NULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 387 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 388 | if (!lib_cache_count && load_ld_cache(LD_SO_CACHE) < 0) { |
| 389 | lib_cache_count = -1; |
| 390 | return NULL; |
| 391 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 392 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 393 | snprintf(soname, soname_len + 1, "lib%s.so", libname); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 394 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 395 | for (i = 0; i < lib_cache_count; ++i) { |
| 396 | if (!strncmp(lib_cache[i].libname, soname, soname_len) && |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 397 | match_so_flags(lib_cache[i].flags)) { |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 398 | return strdup(lib_cache[i].path); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 399 | } |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 400 | } |
| 401 | return NULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 402 | } |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 403 | |
| 404 | void bcc_procutils_free(const char *ptr) { |
| 405 | free((void *)ptr); |
| 406 | } |
kmjohansen | 4b87af0 | 2017-03-30 00:58:31 -0700 | [diff] [blame] | 407 | |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 408 | /* Detects the following languages + C. */ |
| 409 | const char *languages[] = {"java", "python", "ruby", "php", "node"}; |
| 410 | const char *language_c = "c"; |
| 411 | const int nb_languages = 5; |
| 412 | |
| 413 | const char *bcc_procutils_language(int pid) { |
Nan Xiao | 0379779 | 2017-07-29 10:47:24 +0800 | [diff] [blame] | 414 | char procfilename[24], line[4096], pathname[32], *str; |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 415 | FILE *procfile; |
| 416 | int i, ret; |
| 417 | |
| 418 | /* Look for clues in the absolute path to the executable. */ |
Nan Xiao | 0379779 | 2017-07-29 10:47:24 +0800 | [diff] [blame] | 419 | snprintf(procfilename, sizeof(procfilename), "/proc/%ld/exe", (long)pid); |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 420 | if (realpath(procfilename, line)) { |
| 421 | for (i = 0; i < nb_languages; i++) |
| 422 | if (strstr(line, languages[i])) |
| 423 | return languages[i]; |
| 424 | } |
| 425 | |
| 426 | |
Nan Xiao | 0379779 | 2017-07-29 10:47:24 +0800 | [diff] [blame] | 427 | snprintf(procfilename, sizeof(procfilename), "/proc/%ld/maps", (long)pid); |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 428 | procfile = fopen(procfilename, "r"); |
| 429 | if (!procfile) |
| 430 | return NULL; |
| 431 | |
| 432 | /* Look for clues in memory mappings. */ |
| 433 | bool libc = false; |
| 434 | do { |
| 435 | char perm[8], dev[8]; |
| 436 | long long begin, end, size, inode; |
| 437 | ret = fscanf(procfile, "%llx-%llx %s %llx %s %lld", &begin, &end, perm, |
| 438 | &size, dev, &inode); |
| 439 | if (!fgets(line, sizeof(line), procfile)) |
| 440 | break; |
| 441 | if (ret == 6) { |
| 442 | char *mapname = line; |
| 443 | char *newline = strchr(line, '\n'); |
| 444 | if (newline) |
| 445 | newline[0] = '\0'; |
| 446 | while (isspace(mapname[0])) mapname++; |
| 447 | for (i = 0; i < nb_languages; i++) { |
Nan Xiao | 0379779 | 2017-07-29 10:47:24 +0800 | [diff] [blame] | 448 | snprintf(pathname, sizeof(pathname), "/lib%s", languages[i]); |
jeromemarchand | b84714a | 2018-08-08 18:09:44 +0200 | [diff] [blame^] | 449 | if (strstr(mapname, pathname)) { |
| 450 | fclose(procfile); |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 451 | return languages[i]; |
jeromemarchand | b84714a | 2018-08-08 18:09:44 +0200 | [diff] [blame^] | 452 | } |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 453 | if ((str = strstr(mapname, "libc")) && |
| 454 | (str[4] == '-' || str[4] == '.')) |
| 455 | libc = true; |
| 456 | } |
| 457 | } |
| 458 | } while (ret && ret != EOF); |
| 459 | |
| 460 | fclose(procfile); |
| 461 | |
| 462 | /* Return C as the language if libc was found and nothing else. */ |
| 463 | return libc ? language_c : NULL; |
| 464 | } |