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 | */ |
kmjohansen | 4b87af0 | 2017-03-30 00:58:31 -0700 | [diff] [blame] | 16 | #define _GNU_SOURCE |
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> |
| 20 | #include <fcntl.h> |
kmjohansen | 4b87af0 | 2017-03-30 00:58:31 -0700 | [diff] [blame] | 21 | #include <sched.h> |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <string.h> |
| 26 | #include <stdint.h> |
| 27 | #include <ctype.h> |
| 28 | #include <stdio.h> |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 29 | #include <math.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 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 35 | static bool is_exe(const char *path) { |
| 36 | struct stat s; |
| 37 | if (access(path, X_OK) < 0) |
| 38 | return false; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 39 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 40 | if (stat(path, &s) < 0) |
| 41 | return false; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 42 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 43 | return S_ISREG(s.st_mode); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 46 | char *bcc_procutils_which(const char *binpath) { |
| 47 | char buffer[4096]; |
| 48 | const char *PATH; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 49 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 50 | if (strchr(binpath, '/')) |
| 51 | return is_exe(binpath) ? strdup(binpath) : 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 52 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 53 | if (!(PATH = getenv("PATH"))) |
| 54 | return 0; |
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 | while (PATH) { |
| 57 | const char *next = strchr(PATH, ':') ?: strchr(PATH, '\0'); |
| 58 | const size_t path_len = next - PATH; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 59 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 60 | if (path_len) { |
| 61 | memcpy(buffer, PATH, path_len); |
| 62 | buffer[path_len] = '/'; |
| 63 | strcpy(buffer + path_len + 1, binpath); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 64 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 65 | if (is_exe(buffer)) |
| 66 | return strdup(buffer); |
| 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 | PATH = *next ? (next + 1) : 0; |
| 70 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 71 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 72 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Mark Drayton | 1368508 | 2017-02-22 02:05:10 +0000 | [diff] [blame] | 75 | int bcc_mapping_is_file_backed(const char *mapname) { |
| 76 | return mapname[0] && |
| 77 | strncmp(mapname, "//anon", sizeof("//anon") - 1) && |
| 78 | strncmp(mapname, "/dev/zero", sizeof("/dev/zero") - 1) && |
| 79 | strncmp(mapname, "/anon_hugepage", sizeof("/anon_hugepage") - 1) && |
| 80 | strncmp(mapname, "[stack", sizeof("[stack") - 1) && |
| 81 | strncmp(mapname, "/SYSV", sizeof("/SYSV") - 1) && |
| 82 | strncmp(mapname, "[heap]", sizeof("[heap]") - 1); |
| 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; |
| 89 | int ret; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 90 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 91 | sprintf(procmap_filename, "/proc/%ld/maps", (long)pid); |
| 92 | procmap = fopen(procmap_filename, "r"); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 93 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 94 | if (!procmap) |
| 95 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 96 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 97 | do { |
| 98 | char endline[4096]; |
| 99 | char perm[8], dev[8]; |
| 100 | long long begin, end, size, inode; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 101 | |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 102 | ret = fscanf(procmap, "%llx-%llx %s %llx %s %lld", &begin, &end, perm, |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 103 | &size, dev, &inode); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 104 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 105 | if (!fgets(endline, sizeof(endline), procmap)) |
| 106 | break; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 107 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 108 | if (ret == 6) { |
| 109 | char *mapname = endline; |
| 110 | char *newline = strchr(endline, '\n'); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 111 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 112 | if (newline) |
| 113 | newline[0] = '\0'; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 114 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 115 | while (isspace(mapname[0])) mapname++; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 116 | |
Mark Drayton | 1368508 | 2017-02-22 02:05:10 +0000 | [diff] [blame] | 117 | if (strchr(perm, 'x') && bcc_mapping_is_file_backed(mapname)) { |
Vicent Marti | 2ff8e57 | 2016-04-28 12:20:57 +0200 | [diff] [blame] | 118 | if (callback(mapname, (uint64_t)begin, (uint64_t)end, payload) < 0) |
| 119 | break; |
| 120 | } |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 121 | } |
| 122 | } while (ret && ret != EOF); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 123 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 124 | fclose(procmap); |
Mark Drayton | 769edf9 | 2016-06-15 11:53:24 +0100 | [diff] [blame] | 125 | |
| 126 | // Add a mapping to /tmp/perf-pid.map for the entire address space. This will |
| 127 | // be used if symbols aren't resolved in an earlier mapping. |
| 128 | char map_path[4096]; |
| 129 | if (bcc_perf_map_path(map_path, sizeof(map_path), pid)) |
| 130 | callback(map_path, 0, -1, payload); |
| 131 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 132 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 135 | int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload) { |
| 136 | char line[2048]; |
Vicent Marti | 6b59b2c | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 137 | FILE *kallsyms; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 138 | |
Vicent Marti | 6b59b2c | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 139 | /* root is needed to list ksym addresses */ |
| 140 | if (geteuid() != 0) |
| 141 | return -1; |
| 142 | |
| 143 | kallsyms = fopen("/proc/kallsyms", "r"); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 144 | if (!kallsyms) |
| 145 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 146 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 147 | if (!fgets(line, sizeof(line), kallsyms)) { |
| 148 | fclose(kallsyms); |
| 149 | return -1; |
| 150 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 151 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 152 | while (fgets(line, sizeof(line), kallsyms)) { |
| 153 | char *symname, *endsym; |
| 154 | unsigned long long addr; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 155 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 156 | addr = strtoull(line, &symname, 16); |
| 157 | endsym = symname = symname + 3; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 158 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 159 | while (*endsym && !isspace(*endsym)) endsym++; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 160 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 161 | *endsym = '\0'; |
| 162 | callback(symname, addr, payload); |
| 163 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 164 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 165 | fclose(kallsyms); |
| 166 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #define CACHE1_HEADER "ld.so-1.7.0" |
| 170 | #define CACHE1_HEADER_LEN (sizeof(CACHE1_HEADER) - 1) |
| 171 | |
| 172 | #define CACHE2_HEADER "glibc-ld.so.cache" |
| 173 | #define CACHE2_HEADER_LEN (sizeof(CACHE2_HEADER) - 1) |
| 174 | #define CACHE2_VERSION "1.1" |
| 175 | |
| 176 | struct ld_cache1_entry { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 177 | int32_t flags; |
| 178 | uint32_t key; |
| 179 | uint32_t value; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | struct ld_cache1 { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 183 | char header[CACHE1_HEADER_LEN]; |
| 184 | uint32_t entry_count; |
| 185 | struct ld_cache1_entry entries[0]; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | struct ld_cache2_entry { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 189 | int32_t flags; |
| 190 | uint32_t key; |
| 191 | uint32_t value; |
| 192 | uint32_t pad1_; |
| 193 | uint64_t pad2_; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | struct ld_cache2 { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 197 | char header[CACHE2_HEADER_LEN]; |
| 198 | char version[3]; |
| 199 | uint32_t entry_count; |
| 200 | uint32_t string_table_len; |
| 201 | uint32_t pad_[5]; |
| 202 | struct ld_cache2_entry entries[0]; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static int lib_cache_count; |
| 206 | static struct ld_lib { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 207 | char *libname; |
| 208 | char *path; |
| 209 | int flags; |
| 210 | } * lib_cache; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 211 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 212 | static int read_cache1(const char *ld_map) { |
| 213 | struct ld_cache1 *ldcache = (struct ld_cache1 *)ld_map; |
| 214 | const char *ldstrings = |
| 215 | (const char *)(ldcache->entries + ldcache->entry_count); |
| 216 | uint32_t i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 217 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 218 | lib_cache = |
| 219 | (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); |
| 220 | lib_cache_count = (int)ldcache->entry_count; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 221 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 222 | for (i = 0; i < ldcache->entry_count; ++i) { |
| 223 | const char *key = ldstrings + ldcache->entries[i].key; |
| 224 | const char *val = ldstrings + ldcache->entries[i].value; |
| 225 | const int flags = ldcache->entries[i].flags; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 226 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 227 | lib_cache[i].libname = strdup(key); |
| 228 | lib_cache[i].path = strdup(val); |
| 229 | lib_cache[i].flags = flags; |
| 230 | } |
| 231 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 234 | static int read_cache2(const char *ld_map) { |
| 235 | struct ld_cache2 *ldcache = (struct ld_cache2 *)ld_map; |
| 236 | uint32_t i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 237 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 238 | if (memcmp(ld_map, CACHE2_HEADER, CACHE2_HEADER_LEN)) |
| 239 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 240 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 241 | lib_cache = |
| 242 | (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); |
| 243 | lib_cache_count = (int)ldcache->entry_count; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 244 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 245 | for (i = 0; i < ldcache->entry_count; ++i) { |
| 246 | const char *key = ld_map + ldcache->entries[i].key; |
| 247 | const char *val = ld_map + ldcache->entries[i].value; |
| 248 | const int flags = ldcache->entries[i].flags; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 249 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 250 | lib_cache[i].libname = strdup(key); |
| 251 | lib_cache[i].path = strdup(val); |
| 252 | lib_cache[i].flags = flags; |
| 253 | } |
| 254 | return 0; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 257 | static int load_ld_cache(const char *cache_path) { |
| 258 | struct stat st; |
| 259 | size_t ld_size; |
| 260 | const char *ld_map; |
| 261 | int ret, fd = open(cache_path, O_RDONLY); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 262 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 263 | if (fd < 0) |
| 264 | return -1; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 265 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 266 | if (fstat(fd, &st) < 0 || st.st_size < sizeof(struct ld_cache1)) { |
| 267 | close(fd); |
| 268 | return -1; |
| 269 | } |
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 | ld_size = st.st_size; |
| 272 | ld_map = (const char *)mmap(NULL, ld_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 273 | if (ld_map == MAP_FAILED) { |
| 274 | close(fd); |
| 275 | return -1; |
| 276 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 277 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 278 | if (memcmp(ld_map, CACHE1_HEADER, CACHE1_HEADER_LEN) == 0) { |
| 279 | const struct ld_cache1 *cache1 = (struct ld_cache1 *)ld_map; |
| 280 | size_t cache1_len = sizeof(struct ld_cache1) + |
| 281 | (cache1->entry_count * sizeof(struct ld_cache1_entry)); |
| 282 | cache1_len = (cache1_len + 0x7) & ~0x7ULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 283 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 284 | if (ld_size > (cache1_len + sizeof(struct ld_cache2))) |
| 285 | ret = read_cache2(ld_map + cache1_len); |
| 286 | else |
| 287 | ret = read_cache1(ld_map); |
| 288 | } else { |
| 289 | ret = read_cache2(ld_map); |
| 290 | } |
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 | munmap((void *)ld_map, ld_size); |
| 293 | close(fd); |
| 294 | return ret; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | #define LD_SO_CACHE "/etc/ld.so.cache" |
| 298 | #define FLAG_TYPE_MASK 0x00ff |
| 299 | #define TYPE_ELF_LIBC6 0x0003 |
| 300 | #define FLAG_ABI_MASK 0xff00 |
| 301 | #define ABI_SPARC_LIB64 0x0100 |
| 302 | #define ABI_IA64_LIB64 0x0200 |
| 303 | #define ABI_X8664_LIB64 0x0300 |
| 304 | #define ABI_S390_LIB64 0x0400 |
| 305 | #define ABI_POWERPC_LIB64 0x0500 |
| 306 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 307 | static bool match_so_flags(int flags) { |
| 308 | if ((flags & FLAG_TYPE_MASK) != TYPE_ELF_LIBC6) |
| 309 | return false; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 310 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 311 | switch (flags & FLAG_ABI_MASK) { |
Vicent Marti | 2153673 | 2016-04-20 13:37:55 +0200 | [diff] [blame] | 312 | case ABI_SPARC_LIB64: |
| 313 | case ABI_IA64_LIB64: |
| 314 | case ABI_X8664_LIB64: |
| 315 | case ABI_S390_LIB64: |
| 316 | case ABI_POWERPC_LIB64: |
| 317 | return (sizeof(void *) == 8); |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 318 | } |
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 | return true; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 323 | 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] | 324 | int ret, found = false; |
| 325 | char endline[4096], *mapname = NULL, *newline; |
| 326 | char mappings_file[128]; |
| 327 | const size_t search_len = strlen(libname) + strlen("/lib."); |
| 328 | char search1[search_len + 1]; |
| 329 | char search2[search_len + 1]; |
| 330 | |
| 331 | sprintf(mappings_file, "/proc/%ld/maps", (long)pid); |
| 332 | FILE *fp = fopen(mappings_file, "r"); |
| 333 | if (!fp) |
| 334 | return NULL; |
| 335 | |
| 336 | snprintf(search1, search_len + 1, "/lib%s.", libname); |
| 337 | snprintf(search2, search_len + 1, "/lib%s-", libname); |
| 338 | |
| 339 | do { |
| 340 | ret = fscanf(fp, "%*x-%*x %*s %*x %*s %*d"); |
| 341 | if (!fgets(endline, sizeof(endline), fp)) |
| 342 | break; |
| 343 | |
| 344 | mapname = endline; |
| 345 | newline = strchr(endline, '\n'); |
| 346 | if (newline) |
| 347 | newline[0] = '\0'; |
| 348 | |
| 349 | while (isspace(mapname[0])) mapname++; |
| 350 | |
| 351 | if (strstr(mapname, ".so") && (strstr(mapname, search1) || |
| 352 | strstr(mapname, search2))) { |
| 353 | found = true; |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 354 | memcpy(libpath, mapname, strlen(mapname) + 1); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | } while (ret != EOF); |
| 358 | |
| 359 | fclose(fp); |
| 360 | return found; |
| 361 | } |
| 362 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 363 | char *bcc_procutils_which_so(const char *libname, int pid) { |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 364 | const size_t soname_len = strlen(libname) + strlen("lib.so"); |
| 365 | char soname[soname_len + 1]; |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 366 | char libpath[4096]; |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 367 | int i; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 368 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 369 | if (strchr(libname, '/')) |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 370 | return strdup(libname); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 371 | |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 372 | if (pid && which_so_in_process(libname, pid, libpath)) |
| 373 | return strdup(libpath); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 374 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 375 | if (lib_cache_count < 0) |
| 376 | return NULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 377 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 378 | if (!lib_cache_count && load_ld_cache(LD_SO_CACHE) < 0) { |
| 379 | lib_cache_count = -1; |
| 380 | return NULL; |
| 381 | } |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 382 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 383 | snprintf(soname, soname_len + 1, "lib%s.so", libname); |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 384 | |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 385 | for (i = 0; i < lib_cache_count; ++i) { |
| 386 | if (!strncmp(lib_cache[i].libname, soname, soname_len) && |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 387 | match_so_flags(lib_cache[i].flags)) { |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 388 | return strdup(lib_cache[i].path); |
Paul Chaignon | 690b7e6 | 2016-12-25 19:43:41 +0100 | [diff] [blame] | 389 | } |
Vicent Marti | c1c2373 | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 390 | } |
| 391 | return NULL; |
Vicent Marti | b366ede | 2016-04-20 13:24:54 +0200 | [diff] [blame] | 392 | } |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 393 | |
| 394 | void bcc_procutils_free(const char *ptr) { |
| 395 | free((void *)ptr); |
| 396 | } |
kmjohansen | 4b87af0 | 2017-03-30 00:58:31 -0700 | [diff] [blame] | 397 | |
| 398 | bool bcc_procutils_enter_mountns(int pid, struct ns_cookie *nc) { |
| 399 | char curnspath[4096]; |
| 400 | char newnspath[4096]; |
| 401 | int oldns = -1; |
| 402 | int newns = -1; |
| 403 | struct stat ons_stat; |
| 404 | struct stat nns_stat; |
| 405 | |
| 406 | if (nc == NULL) |
| 407 | return false; |
| 408 | |
| 409 | nc->nsc_oldns = -1; |
| 410 | nc->nsc_newns = -1; |
| 411 | |
| 412 | if (snprintf(curnspath, 4096, "/proc/self/ns/mnt") == 4096) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | if (snprintf(newnspath, 4096, "/proc/%d/ns/mnt", pid) == 4096) { |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | if ((oldns = open(curnspath, O_RDONLY)) < 0) { |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | if ((newns = open(newnspath, O_RDONLY)) < 0) { |
| 425 | goto errout; |
| 426 | } |
| 427 | |
| 428 | if (fstat(oldns, &ons_stat) < 0) { |
| 429 | goto errout; |
| 430 | } |
| 431 | |
| 432 | if (fstat(newns, &nns_stat) < 0) { |
| 433 | goto errout; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Only switch to the new namespace if it doesn't match the existing |
| 438 | * namespace. This prevents us from getting an EPERM when trying to enter an |
| 439 | * identical namespace. |
| 440 | */ |
| 441 | if (ons_stat.st_ino == nns_stat.st_ino) { |
| 442 | goto errout; |
| 443 | } |
| 444 | |
| 445 | if (setns(newns, CLONE_NEWNS) < 0) { |
| 446 | goto errout; |
| 447 | } |
| 448 | |
| 449 | nc->nsc_oldns = oldns; |
| 450 | nc->nsc_newns = newns; |
| 451 | |
| 452 | return true; |
| 453 | |
| 454 | errout: |
| 455 | if (oldns > -1) { |
| 456 | (void) close(oldns); |
| 457 | } |
| 458 | if (newns > -1) { |
| 459 | (void) close(newns); |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | bool bcc_procutils_exit_mountns(struct ns_cookie *nc) { |
| 465 | bool rc = false; |
| 466 | |
| 467 | if (nc == NULL) |
| 468 | return rc; |
| 469 | |
| 470 | if (nc->nsc_oldns == -1 || nc->nsc_newns == -1) |
| 471 | return rc; |
| 472 | |
| 473 | if (setns(nc->nsc_oldns, CLONE_NEWNS) == 0) { |
| 474 | rc = true; |
| 475 | } |
| 476 | |
| 477 | if (nc->nsc_oldns > -1) { |
| 478 | (void) close(nc->nsc_oldns); |
| 479 | nc->nsc_oldns = -1; |
| 480 | } |
| 481 | if (nc->nsc_newns > -1) { |
| 482 | (void) close(nc->nsc_newns); |
| 483 | nc->nsc_newns = -1; |
| 484 | } |
| 485 | |
| 486 | return rc; |
| 487 | } |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame^] | 488 | |
| 489 | /* Detects the following languages + C. */ |
| 490 | const char *languages[] = {"java", "python", "ruby", "php", "node"}; |
| 491 | const char *language_c = "c"; |
| 492 | const int nb_languages = 5; |
| 493 | |
| 494 | const char *bcc_procutils_language(int pid) { |
| 495 | char procfilename[22], line[4096], pathname[32], *str; |
| 496 | FILE *procfile; |
| 497 | int i, ret; |
| 498 | |
| 499 | /* Look for clues in the absolute path to the executable. */ |
| 500 | sprintf(procfilename, "/proc/%ld/exe", (long)pid); |
| 501 | if (realpath(procfilename, line)) { |
| 502 | for (i = 0; i < nb_languages; i++) |
| 503 | if (strstr(line, languages[i])) |
| 504 | return languages[i]; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | sprintf(procfilename, "/proc/%ld/maps", (long)pid); |
| 509 | procfile = fopen(procfilename, "r"); |
| 510 | if (!procfile) |
| 511 | return NULL; |
| 512 | |
| 513 | /* Look for clues in memory mappings. */ |
| 514 | bool libc = false; |
| 515 | do { |
| 516 | char perm[8], dev[8]; |
| 517 | long long begin, end, size, inode; |
| 518 | ret = fscanf(procfile, "%llx-%llx %s %llx %s %lld", &begin, &end, perm, |
| 519 | &size, dev, &inode); |
| 520 | if (!fgets(line, sizeof(line), procfile)) |
| 521 | break; |
| 522 | if (ret == 6) { |
| 523 | char *mapname = line; |
| 524 | char *newline = strchr(line, '\n'); |
| 525 | if (newline) |
| 526 | newline[0] = '\0'; |
| 527 | while (isspace(mapname[0])) mapname++; |
| 528 | for (i = 0; i < nb_languages; i++) { |
| 529 | sprintf(pathname, "/lib%s", languages[i]); |
| 530 | if (strstr(mapname, pathname)) |
| 531 | return languages[i]; |
| 532 | if ((str = strstr(mapname, "libc")) && |
| 533 | (str[4] == '-' || str[4] == '.')) |
| 534 | libc = true; |
| 535 | } |
| 536 | } |
| 537 | } while (ret && ret != EOF); |
| 538 | |
| 539 | fclose(procfile); |
| 540 | |
| 541 | /* Return C as the language if libc was found and nothing else. */ |
| 542 | return libc ? language_c : NULL; |
| 543 | } |