< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/meta/HotSpotObjdumpDisassemblerProvider.java

Print this page




  22  */
  23 
  24 
  25 package org.graalvm.compiler.hotspot.meta;
  26 
  27 import java.io.BufferedReader;
  28 import java.io.File;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.InputStreamReader;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.regex.Matcher;
  36 import java.util.regex.Pattern;
  37 
  38 import org.graalvm.compiler.code.CompilationResult;
  39 import org.graalvm.compiler.code.CompilationResult.CodeAnnotation;
  40 import org.graalvm.compiler.code.DisassemblerProvider;
  41 import org.graalvm.compiler.serviceprovider.ServiceProvider;

  42 
  43 import jdk.vm.ci.code.CodeCacheProvider;
  44 import jdk.vm.ci.code.CodeUtil;
  45 import jdk.vm.ci.code.CodeUtil.DefaultRefMapFormatter;
  46 import jdk.vm.ci.code.CodeUtil.RefMapFormatter;
  47 import jdk.vm.ci.code.InstalledCode;
  48 import jdk.vm.ci.code.Register;
  49 import jdk.vm.ci.code.RegisterConfig;
  50 import jdk.vm.ci.code.TargetDescription;
  51 import jdk.vm.ci.code.site.Call;
  52 import jdk.vm.ci.code.site.DataPatch;
  53 import jdk.vm.ci.code.site.Infopoint;
  54 import jdk.vm.ci.code.site.Mark;
  55 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  56 import jdk.vm.ci.services.Services;
  57 
  58 /**
  59  * This disassembles the code immediatly with objdump.
  60  */
  61 @ServiceProvider(DisassemblerProvider.class)
  62 public class HotSpotObjdumpDisassemblerProvider extends HotSpotDisassemblerProvider {
  63 
  64     /**
  65      * Uses objdump to disassemble the compiled code.
  66      */
  67     @Override
  68     public String disassembleCompiledCode(CodeCacheProvider codeCache, CompilationResult compResult) {



  69         File tmp = null;
  70         try {
  71             tmp = File.createTempFile("compiledBinary", ".bin");
  72             try (FileOutputStream fos = new FileOutputStream(tmp)) {
  73                 fos.write(compResult.getTargetCode());
  74             }

  75             String[] cmdline;
  76             String arch = Services.getSavedProperties().get("os.arch");
  77             if (arch.equals("amd64")) {
  78                 cmdline = new String[]{"objdump", "-D", "-b", "binary", "-M", "x86-64", "-m", "i386", tmp.getAbsolutePath()};
  79             } else if (arch.equals("aarch64")) {
  80                 cmdline = new String[]{"objdump", "-D", "-b", "binary", "-m", "aarch64", tmp.getAbsolutePath()};
  81             } else {
  82                 return null;
  83             }
  84 
  85             Pattern p = Pattern.compile(" *(([0-9a-fA-F]+):\t.*)");
  86 
  87             TargetDescription target = codeCache.getTarget();
  88             RegisterConfig regConfig = codeCache.getRegisterConfig();
  89             Register fp = regConfig.getFrameRegister();
  90             RefMapFormatter slotFormatter = new DefaultRefMapFormatter(target.wordSize, fp, 0);
  91 
  92             Map<Integer, String> annotations = new HashMap<>();
  93             for (DataPatch site : compResult.getDataPatches()) {
  94                 putAnnotation(annotations, site.pcOffset, "{" + site.reference.toString() + "}");
  95             }
  96             for (Mark mark : compResult.getMarks()) {
  97                 putAnnotation(annotations, mark.pcOffset, codeCache.getMarkName(mark));
  98             }
  99             for (CodeAnnotation a : compResult.getCodeAnnotations()) {
 100                 putAnnotation(annotations, a.position, a.toString());
 101             }
 102             for (Infopoint infopoint : compResult.getInfopoints()) {
 103                 if (infopoint instanceof Call) {
 104                     Call call = (Call) infopoint;
 105                     if (call.debugInfo != null) {
 106                         putAnnotation(annotations, call.pcOffset + call.size, CodeUtil.append(new StringBuilder(100), call.debugInfo, slotFormatter).toString());
 107                     }
 108                     putAnnotation(annotations, call.pcOffset, "{" + codeCache.getTargetName(call) + "}");
 109                 } else {
 110                     if (infopoint.debugInfo != null) {
 111                         putAnnotation(annotations, infopoint.pcOffset, CodeUtil.append(new StringBuilder(100), infopoint.debugInfo, slotFormatter).toString());
 112                     }
 113                     putAnnotation(annotations, infopoint.pcOffset, "{infopoint: " + infopoint.reason + "}");
 114                 }
 115             }
 116 
 117             Process proc = Runtime.getRuntime().exec(cmdline);
 118             InputStream is = proc.getInputStream();

 119 
 120             InputStreamReader isr = new InputStreamReader(is);
 121             BufferedReader br = new BufferedReader(isr);
 122             String line;
 123 
 124             StringBuilder sb = new StringBuilder();
 125             while ((line = br.readLine()) != null) {
 126                 Matcher m = p.matcher(line);
 127                 if (m.find()) {
 128                     int address = Integer.parseInt(m.group(2), 16);
 129                     String annotation = annotations.get(address);
 130                     if (annotation != null) {
 131                         annotation = annotation.replace("\n", "\n; ");
 132                         sb.append("; ").append(annotation).append('\n');
 133                     }
 134                     line = m.replaceAll("0x$1");
 135                 }
 136                 sb.append(line).append("\n");
 137             }
 138             BufferedReader ebr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
 139             while ((line = ebr.readLine()) != null) {
 140                 System.err.println(line);






 141             }
 142             ebr.close();
 143             return sb.toString();
 144         } catch (IOException e) {



 145             if (tmp != null) {
 146                 tmp.delete();
 147             }
 148             e.printStackTrace();
 149             return null;
 150         }
 151     }
 152 


















































 153     private static void putAnnotation(Map<Integer, String> annotations, int idx, String txt) {
 154         String newAnnoation = annotations.getOrDefault(idx, "") + "\n" + txt;
 155         annotations.put(idx, newAnnoation);
 156     }
 157 
 158     @Override
 159     public String disassembleInstalledCode(CodeCacheProvider codeCache, CompilationResult compResult, InstalledCode code) {
 160         return ((HotSpotCodeCacheProvider) codeCache).disassemble(code);
 161     }
 162 
 163     @Override
 164     public String getName() {
 165         return "hsdis-objdump";
 166     }
 167 }


  22  */
  23 
  24 
  25 package org.graalvm.compiler.hotspot.meta;
  26 
  27 import java.io.BufferedReader;
  28 import java.io.File;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.InputStreamReader;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.regex.Matcher;
  36 import java.util.regex.Pattern;
  37 
  38 import org.graalvm.compiler.code.CompilationResult;
  39 import org.graalvm.compiler.code.CompilationResult.CodeAnnotation;
  40 import org.graalvm.compiler.code.DisassemblerProvider;
  41 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  42 import org.graalvm.util.CollectionsUtil;
  43 
  44 import jdk.vm.ci.code.CodeCacheProvider;
  45 import jdk.vm.ci.code.CodeUtil;
  46 import jdk.vm.ci.code.CodeUtil.DefaultRefMapFormatter;
  47 import jdk.vm.ci.code.CodeUtil.RefMapFormatter;
  48 import jdk.vm.ci.code.InstalledCode;
  49 import jdk.vm.ci.code.Register;
  50 import jdk.vm.ci.code.RegisterConfig;
  51 import jdk.vm.ci.code.TargetDescription;
  52 import jdk.vm.ci.code.site.Call;
  53 import jdk.vm.ci.code.site.DataPatch;
  54 import jdk.vm.ci.code.site.Infopoint;
  55 import jdk.vm.ci.code.site.Mark;
  56 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  57 import jdk.vm.ci.services.Services;
  58 
  59 /**
  60  * A provider that uses the {@code GNU objdump} utility to disassemble code.
  61  */
  62 @ServiceProvider(DisassemblerProvider.class)
  63 public class HotSpotObjdumpDisassemblerProvider extends HotSpotDisassemblerProvider {
  64 
  65     private final String objdump = getObjdump();
  66 

  67     @Override
  68     public String disassembleCompiledCode(CodeCacheProvider codeCache, CompilationResult compResult) {
  69         if (objdump == null) {
  70             return null;
  71         }
  72         File tmp = null;
  73         try {
  74             tmp = File.createTempFile("compiledBinary", ".bin");
  75             try (FileOutputStream fos = new FileOutputStream(tmp)) {
  76                 fos.write(compResult.getTargetCode());
  77             }
  78 
  79             String[] cmdline;
  80             String arch = Services.getSavedProperties().get("os.arch");
  81             if (arch.equals("amd64") || arch.equals("x86_64")) {
  82                 cmdline = new String[]{objdump, "-D", "-b", "binary", "-M", "x86-64", "-m", "i386", tmp.getAbsolutePath()};
  83             } else if (arch.equals("aarch64")) {
  84                 cmdline = new String[]{objdump, "-D", "-b", "binary", "-m", "aarch64", tmp.getAbsolutePath()};
  85             } else {
  86                 return null;
  87             }
  88 
  89             Pattern p = Pattern.compile(" *(([0-9a-fA-F]+):\t.*)");
  90 
  91             TargetDescription target = codeCache.getTarget();
  92             RegisterConfig regConfig = codeCache.getRegisterConfig();
  93             Register fp = regConfig.getFrameRegister();
  94             RefMapFormatter slotFormatter = new DefaultRefMapFormatter(target.wordSize, fp, 0);
  95 
  96             Map<Integer, String> annotations = new HashMap<>();
  97             for (DataPatch site : compResult.getDataPatches()) {
  98                 putAnnotation(annotations, site.pcOffset, "{" + site.reference.toString() + "}");
  99             }
 100             for (Mark mark : compResult.getMarks()) {
 101                 putAnnotation(annotations, mark.pcOffset, codeCache.getMarkName(mark));
 102             }
 103             for (CodeAnnotation a : compResult.getCodeAnnotations()) {
 104                 putAnnotation(annotations, a.position, a.toString());
 105             }
 106             for (Infopoint infopoint : compResult.getInfopoints()) {
 107                 if (infopoint instanceof Call) {
 108                     Call call = (Call) infopoint;
 109                     if (call.debugInfo != null) {
 110                         putAnnotation(annotations, call.pcOffset + call.size, CodeUtil.append(new StringBuilder(100), call.debugInfo, slotFormatter).toString());
 111                     }
 112                     putAnnotation(annotations, call.pcOffset, "{" + codeCache.getTargetName(call) + "}");
 113                 } else {
 114                     if (infopoint.debugInfo != null) {
 115                         putAnnotation(annotations, infopoint.pcOffset, CodeUtil.append(new StringBuilder(100), infopoint.debugInfo, slotFormatter).toString());
 116                     }
 117                     putAnnotation(annotations, infopoint.pcOffset, "{infopoint: " + infopoint.reason + "}");
 118                 }
 119             }
 120 
 121             Process proc = Runtime.getRuntime().exec(cmdline);
 122             InputStream is = proc.getInputStream();
 123             StringBuilder sb = new StringBuilder();
 124 
 125             InputStreamReader isr = new InputStreamReader(is);
 126             try (BufferedReader br = new BufferedReader(isr)) {
 127                 String line;
 128                 while ((line = br.readLine()) != null) {
 129                     Matcher m = p.matcher(line);
 130                     if (m.find()) {
 131                         int address = Integer.parseInt(m.group(2), 16);
 132                         String annotation = annotations.get(address);
 133                         if (annotation != null) {
 134                             annotation = annotation.replace("\n", "\n; ");
 135                             sb.append("; ").append(annotation).append('\n');
 136                         }
 137                         line = m.replaceAll("0x$1");
 138                     }
 139                     sb.append(line).append("\n");
 140                 }

 141             }
 142             try (BufferedReader ebr = new BufferedReader(new InputStreamReader(proc.getErrorStream()))) {
 143                 String errLine = ebr.readLine();
 144                 if (errLine != null) {
 145                     System.err.println("Error output from executing: " + CollectionsUtil.mapAndJoin(cmdline, e -> quoteShellArg(String.valueOf(e)), " "));
 146                     System.err.println(errLine);
 147                     while ((errLine = ebr.readLine()) != null) {
 148                         System.err.println(errLine);
 149                     }
 150                 }
 151             }

 152             return sb.toString();
 153         } catch (IOException e) {
 154             e.printStackTrace();
 155             return null;
 156         } finally {
 157             if (tmp != null) {
 158                 tmp.delete();
 159             }


 160         }
 161     }
 162 
 163     /**
 164      * Pattern for a single shell command argument that does not need to quoted.
 165      */
 166     private static final Pattern SAFE_SHELL_ARG = Pattern.compile("[A-Za-z0-9@%_\\-\\+=:,\\./]+");
 167 
 168     /**
 169      * Reliably quote a string as a single shell command argument.
 170      */
 171     public static String quoteShellArg(String arg) {
 172         if (arg.isEmpty()) {
 173             return "\"\"";
 174         }
 175         Matcher m = SAFE_SHELL_ARG.matcher(arg);
 176         if (m.matches()) {
 177             return arg;
 178         }
 179         // See http://stackoverflow.com/a/1250279
 180         return "'" + arg.replace("'", "'\"'\"'") + "'";
 181     }
 182 
 183     /**
 184      * Searches for a valid GNU objdump executable.
 185      */
 186     private static String getObjdump() {
 187         // On macOS, `brew install binutils` will provide
 188         // an executable named gobjdump
 189         for (String candidate : new String[]{"objdump", "gobjdump"}) {
 190             try {
 191                 String[] cmd = {candidate, "--version"};
 192                 Process proc = Runtime.getRuntime().exec(cmd);
 193                 InputStream is = proc.getInputStream();
 194                 int exitValue = proc.waitFor();
 195                 if (exitValue == 0) {
 196                     byte[] buf = new byte[is.available()];
 197                     int pos = 0;
 198                     while (pos < buf.length) {
 199                         int read = is.read(buf, pos, buf.length - pos);
 200                         pos += read;
 201                     }
 202                     String output = new String(buf);
 203                     if (output.contains("GNU objdump")) {
 204                         return candidate;
 205                     }
 206                 }
 207             } catch (IOException | InterruptedException e) {
 208             }
 209         }
 210         return null;
 211     }
 212 
 213     private static void putAnnotation(Map<Integer, String> annotations, int idx, String txt) {
 214         String newAnnotation = annotations.getOrDefault(idx, "") + "\n" + txt;
 215         annotations.put(idx, newAnnotation);
 216     }
 217 
 218     @Override
 219     public String disassembleInstalledCode(CodeCacheProvider codeCache, CompilationResult compResult, InstalledCode code) {
 220         return ((HotSpotCodeCacheProvider) codeCache).disassemble(code);
 221     }
 222 
 223     @Override
 224     public String getName() {
 225         return "hsdis-objdump";
 226     }
 227 }
< prev index next >