1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include <jni.h> // just include something, or else solaris compiler will complain that this file is empty 26 27 #if defined(LINUX) || defined(__APPLE__) 28 #include <unistd.h> 29 #include <fcntl.h> 30 #include <string.h> 31 #include <stdlib.h> 32 #include <stddef.h> 33 #ifdef LINUX 34 #include <elf.h> 35 #include <link.h> 36 #include "proc_service.h" 37 #include "salibelf.h" 38 #endif 39 #include "libproc_impl.h" 40 #include "cds.h" 41 42 #ifdef __APPLE__ 43 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h" 44 #endif 45 46 #ifdef LINUX 47 // I have no idea why this function is called ps_pread() on macos but ps_pdread on linux. 48 #define ps_pread ps_pdread 49 #endif 50 51 // Common code shared between linux/native/libsaproc/ps_core.c and macosx/native/libsaproc/ps_core.c 52 53 //---------------------------------------------------------------------- 54 // ps_prochandle cleanup helper functions 55 56 // close all file descriptors 57 static void close_files(struct ps_prochandle* ph) { 58 lib_info* lib = NULL; 59 60 // close core file descriptor 61 if (ph->core->core_fd >= 0) 62 close(ph->core->core_fd); 63 64 // close exec file descriptor 65 if (ph->core->exec_fd >= 0) 66 close(ph->core->exec_fd); 67 68 // close interp file descriptor 69 if (ph->core->interp_fd >= 0) 70 close(ph->core->interp_fd); 71 72 // close class share archive file 73 if (ph->core->classes_jsa_fd >= 0) 74 close(ph->core->classes_jsa_fd); 75 76 // close all library file descriptors 77 lib = ph->libs; 78 while (lib) { 79 int fd = lib->fd; 80 if (fd >= 0 && fd != ph->core->exec_fd) { 81 close(fd); 82 } 83 lib = lib->next; 84 } 85 } 86 87 // clean all map_info stuff 88 static void destroy_map_info(struct ps_prochandle* ph) { 89 map_info* map = ph->core->maps; 90 while (map) { 91 map_info* next = map->next; 92 free(map); 93 map = next; 94 } 95 96 if (ph->core->map_array) { 97 free(ph->core->map_array); 98 } 99 100 // Part of the class sharing workaround 101 map = ph->core->class_share_maps; 102 while (map) { 103 map_info* next = map->next; 104 free(map); 105 map = next; 106 } 107 } 108 109 // ps_prochandle operations 110 void core_release(struct ps_prochandle* ph) { 111 if (ph->core) { 112 close_files(ph); 113 destroy_map_info(ph); 114 free(ph->core); 115 } 116 } 117 118 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { 119 map_info* map; 120 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { 121 print_debug("can't allocate memory for map_info\n"); 122 return NULL; 123 } 124 125 // initialize map 126 map->fd = fd; 127 map->offset = offset; 128 map->vaddr = vaddr; 129 map->memsz = memsz; 130 return map; 131 } 132 133 // add map info with given fd, offset, vaddr and memsz 134 map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, 135 uintptr_t vaddr, size_t memsz) { 136 map_info* map; 137 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { 138 return NULL; 139 } 140 141 // add this to map list 142 map->next = ph->core->maps; 143 ph->core->maps = map; 144 ph->core->num_maps++; 145 146 return map; 147 } 148 149 // Part of the class sharing workaround 150 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, 151 uintptr_t vaddr, size_t memsz) { 152 map_info* map; 153 if ((map = allocate_init_map(ph->core->classes_jsa_fd, 154 offset, vaddr, memsz)) == NULL) { 155 return NULL; 156 } 157 158 map->next = ph->core->class_share_maps; 159 ph->core->class_share_maps = map; 160 return map; 161 } 162 163 // Return the map_info for the given virtual address. We keep a sorted 164 // array of pointers in ph->map_array, so we can binary search. 165 map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { 166 int mid, lo = 0, hi = ph->core->num_maps - 1; 167 map_info *mp; 168 169 while (hi - lo > 1) { 170 mid = (lo + hi) / 2; 171 if (addr >= ph->core->map_array[mid]->vaddr) { 172 lo = mid; 173 } else { 174 hi = mid; 175 } 176 } 177 178 if (addr < ph->core->map_array[hi]->vaddr) { 179 mp = ph->core->map_array[lo]; 180 } else { 181 mp = ph->core->map_array[hi]; 182 } 183 184 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { 185 return (mp); 186 } 187 188 189 // Part of the class sharing workaround 190 // Unfortunately, we have no way of detecting -Xshare state. 191 // Check out the share maps atlast, if we don't find anywhere. 192 // This is done this way so to avoid reading share pages 193 // ahead of other normal maps. For eg. with -Xshare:off we don't 194 // want to prefer class sharing data to data from core. 195 mp = ph->core->class_share_maps; 196 if (mp) { 197 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); 198 } 199 while (mp) { 200 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { 201 print_debug("located map_info at 0x%lx from class share maps\n", addr); 202 return (mp); 203 } 204 mp = mp->next; 205 } 206 207 print_debug("can't locate map_info at 0x%lx\n", addr); 208 return (NULL); 209 } 210 211 //--------------------------------------------------------------- 212 // Part of the class sharing workaround: 213 // 214 // With class sharing, pages are mapped from classes.jsa file. 215 // The read-only class sharing pages are mapped as MAP_SHARED, 216 // PROT_READ pages. These pages are not dumped into core dump. 217 // With this workaround, these pages are read from classes.jsa. 218 219 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { 220 jboolean i; 221 if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { 222 *pvalue = i; 223 return true; 224 } else { 225 return false; 226 } 227 } 228 229 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { 230 uintptr_t uip; 231 if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { 232 *pvalue = uip; 233 return true; 234 } else { 235 return false; 236 } 237 } 238 239 // used to read strings from debuggee 240 bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { 241 size_t i = 0; 242 char c = ' '; 243 244 while (c != '\0') { 245 if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { 246 return false; 247 } 248 if (i < size - 1) { 249 buf[i] = c; 250 } else { 251 // smaller buffer 252 return false; 253 } 254 i++; addr++; 255 } 256 buf[i] = '\0'; 257 return true; 258 } 259 260 #ifdef LINUX 261 // mangled name of Arguments::SharedArchivePath 262 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" 263 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" 264 #define LIBJVM_NAME "/libjvm.so" 265 #endif 266 267 #ifdef __APPLE__ 268 // mangled name of Arguments::SharedArchivePath 269 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE" 270 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces" 271 #define LIBJVM_NAME "/libjvm.dylib" 272 #endif 273 274 bool init_classsharing_workaround(struct ps_prochandle* ph) { 275 lib_info* lib = ph->libs; 276 while (lib != NULL) { 277 // we are iterating over shared objects from the core dump. look for 278 // libjvm.so. 279 const char *jvm_name = 0; 280 if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { 281 char classes_jsa[PATH_MAX]; 282 CDSFileMapHeaderBase header; 283 int fd = -1; 284 uintptr_t base = 0, useSharedSpacesAddr = 0; 285 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; 286 jboolean useSharedSpaces = 0; 287 int m; 288 size_t n; 289 290 memset(classes_jsa, 0, sizeof(classes_jsa)); 291 jvm_name = lib->name; 292 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); 293 if (useSharedSpacesAddr == 0) { 294 print_debug("can't lookup 'UseSharedSpaces' flag\n"); 295 return false; 296 } 297 298 // Hotspot vm types are not exported to build this library. So 299 // using equivalent type jboolean to read the value of 300 // UseSharedSpaces which is same as hotspot type "bool". 301 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { 302 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); 303 return false; 304 } 305 306 if ((int)useSharedSpaces == 0) { 307 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); 308 return true; 309 } 310 311 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); 312 if (sharedArchivePathAddrAddr == 0) { 313 print_debug("can't lookup shared archive path symbol\n"); 314 return false; 315 } 316 317 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { 318 print_debug("can't read shared archive path pointer\n"); 319 return false; 320 } 321 322 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { 323 print_debug("can't read shared archive path value\n"); 324 return false; 325 } 326 327 print_debug("looking for %s\n", classes_jsa); 328 // open the class sharing archive file 329 fd = pathmap_open(classes_jsa); 330 if (fd < 0) { 331 print_debug("can't open %s!\n", classes_jsa); 332 ph->core->classes_jsa_fd = -1; 333 return false; 334 } else { 335 print_debug("opened %s\n", classes_jsa); 336 } 337 338 // read CDSFileMapHeaderBase from the file 339 memset(&header, 0, sizeof(CDSFileMapHeaderBase)); 340 if ((n = read(fd, &header, sizeof(CDSFileMapHeaderBase))) 341 != sizeof(CDSFileMapHeaderBase)) { 342 print_debug("can't read shared archive file map header from %s\n", classes_jsa); 343 close(fd); 344 return false; 345 } 346 347 // check file magic 348 if (header._magic != CDS_ARCHIVE_MAGIC) { 349 print_debug("%s has bad shared archive file magic number 0x%x, expecting 0x%x\n", 350 classes_jsa, header._magic, CDS_ARCHIVE_MAGIC); 351 close(fd); 352 return false; 353 } 354 355 // check version 356 if (header._version != CURRENT_CDS_ARCHIVE_VERSION) { 357 print_debug("%s has wrong shared archive file version %d, expecting %d\n", 358 classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION); 359 close(fd); 360 return false; 361 } 362 363 ph->core->classes_jsa_fd = fd; 364 // add read-only maps from classes.jsa to the list of maps 365 for (m = 0; m < NUM_CDS_REGIONS; m++) { 366 if (header._space[m]._read_only) { 367 // With *some* linux versions, the core file doesn't include read-only mmap'ed 368 // files regions, so let's add them here. This is harmless if the core file also 369 // include these regions. 370 base = (uintptr_t) header._space[m]._addr._base; 371 // no need to worry about the fractional pages at-the-end. 372 // possible fractional pages are handled by core_read_data. 373 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, 374 base, (size_t) header._space[m]._used); 375 print_debug("added a share archive map at 0x%lx\n", base); 376 } 377 } 378 return true; 379 } 380 lib = lib->next; 381 } 382 return true; 383 } 384 385 #endif // defined(LINUX) || defined(__APPLE__)