|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 | package sun.jvm.hotspot.asm;
|
26 | 26 |
|
27 | 27 | import java.io.PrintStream;
|
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.Properties; |
28 | 32 | import sun.jvm.hotspot.code.CodeBlob;
|
29 | 33 | import sun.jvm.hotspot.code.NMethod;
|
30 | 34 | import sun.jvm.hotspot.debugger.Address;
|
| 35 | +import sun.jvm.hotspot.debugger.DebuggerException; |
31 | 36 | import sun.jvm.hotspot.runtime.VM;
|
32 | 37 |
|
33 | 38 | public class Disassembler {
|
@@ -59,44 +64,60 @@ private Disassembler(long startPc, byte[] code) {
|
59 | 64 |
|
60 | 65 | // Lazily load hsdis
|
61 | 66 | if (decode_function == 0) {
|
62 |
| - StringBuilder path = new StringBuilder(System.getProperty("java.home")); |
63 |
| - String sep = System.getProperty("file.separator"); |
64 |
| - String os = System.getProperty("os.name"); |
65 |
| - String libname = "hsdis"; |
66 |
| - String arch = System.getProperty("os.arch"); |
67 |
| - if (os.lastIndexOf("Windows", 0) != -1) { |
68 |
| - if (arch.equals("x86")) { |
69 |
| - libname += "-i386"; |
70 |
| - } else if (arch.equals("amd64")) { |
71 |
| - libname += "-amd64"; |
72 |
| - } else { |
73 |
| - libname += "-" + arch; |
74 |
| - } |
75 |
| - path.append(sep + "bin" + sep); |
76 |
| - libname += ".dll"; |
77 |
| - } else if (os.lastIndexOf("Linux", 0) != -1) { |
78 |
| - if (arch.equals("x86") || arch.equals("i386")) { |
79 |
| - path.append(sep + "lib" + sep + "i386" + sep); |
80 |
| - libname += "-i386.so"; |
81 |
| - } else if (arch.equals("amd64") || arch.equals("x86_64")) { |
82 |
| - path.append(sep + "lib" + sep + "amd64" + sep); |
83 |
| - libname += "-amd64.so"; |
84 |
| - } else { |
85 |
| - path.append(sep + "lib" + sep + arch + sep); |
86 |
| - libname += "-" + arch + ".so"; |
| 67 | + // Search for hsdis library in the following 4 locations: |
| 68 | + // 1. <home>/lib/<vm>/libhsdis-<arch>.so |
| 69 | + // 2. <home>/lib/<vm>/hsdis-<arch>.so |
| 70 | + // 3. <home>/lib/hsdis-<arch>.so |
| 71 | + // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) |
| 72 | + Properties targetSysProps = VM.getVM().getSystemProperties(); |
| 73 | + String os = targetSysProps.getProperty("os.name"); |
| 74 | + String ext = ".so"; |
| 75 | + if (os.contains("Windows")) { |
| 76 | + ext = ".dll"; |
| 77 | + } else if (os.contains("Mac OS")) { |
| 78 | + ext = ".dylib"; |
| 79 | + } |
| 80 | + |
| 81 | + // Find the full path to libjvm.so (jvm.dll and libjvm.dylib on Windows and OSX). |
| 82 | + String jvmPattern = "^(lib)?jvm\\" + ext + "$"; |
| 83 | + Path jvmPath = VM.getVM() |
| 84 | + .getDebugger() |
| 85 | + .getCDebugger() |
| 86 | + .getLoadObjectList() |
| 87 | + .stream() |
| 88 | + .map(o -> Path.of(o.getName())) |
| 89 | + .filter(p -> p.getFileName().toString().matches(jvmPattern)) |
| 90 | + .findAny() |
| 91 | + .get(); |
| 92 | + |
| 93 | + String arch = targetSysProps.getProperty("os.arch"); |
| 94 | + String libname = "hsdis-" + arch + ext; |
| 95 | + |
| 96 | + List<String> libs = List.of( |
| 97 | + // 1. <home>/lib/<vm>/libhsdis-<arch>.so |
| 98 | + jvmPath.resolveSibling("lib" + libname).toString(), |
| 99 | + // 2. <home>/lib/<vm>/hsdis-<arch>.so |
| 100 | + jvmPath.resolveSibling(libname).toString(), |
| 101 | + // 3. <home>/lib/hsdis-<arch>.so |
| 102 | + jvmPath.getParent().resolveSibling(libname).toString(), |
| 103 | + // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) |
| 104 | + libname |
| 105 | + ); |
| 106 | + |
| 107 | + var itr = libs.iterator(); |
| 108 | + while (itr.hasNext() && (decode_function == 0L)) { |
| 109 | + try { |
| 110 | + decode_function = load_library(itr.next()); |
| 111 | + } catch (DebuggerException e) { |
| 112 | + if (!itr.hasNext()) { |
| 113 | + throw e; |
| 114 | + } |
87 | 115 | }
|
88 |
| - } else if (os.lastIndexOf("Mac OS X", 0) != -1) { |
89 |
| - path.append(sep + "lib" + sep); |
90 |
| - libname += "-amd64" + ".dylib"; // x86_64 => amd64 |
91 |
| - } else { |
92 |
| - path.append(sep + "lib" + sep + "arch" + sep); |
93 |
| - libname += "-" + arch + ".so"; |
94 | 116 | }
|
95 |
| - decode_function = load_library(path.toString(), libname); |
96 | 117 | }
|
97 | 118 | }
|
98 | 119 |
|
99 |
| - private static native long load_library(String installed_jrepath, String hsdis_library_name); |
| 120 | + private static native long load_library(String hsdis_library_name); |
100 | 121 |
|
101 | 122 | private native void decode(InstructionVisitor visitor, long pc, byte[] code,
|
102 | 123 | String options, long decode_function);
|
|
0 commit comments