common: Add a ProcessUtils for external process launching
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.core / src / org / eclipse / tracecompass / internal / lttng2 / ust / core / analysis / debuginfo / FileOffsetMapper.java
index d426042769be636d9e14de3b6730c56c6350f356..8b5b9396827c84227f98615d1345cf86bfb68a5b 100644 (file)
@@ -11,26 +11,25 @@ package org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo;
 
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.nio.file.Files;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.logging.Logger;
-import java.util.stream.Collectors;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.common.core.log.TraceCompassLog;
-import org.eclipse.tracecompass.lttng2.ust.core.analysis.debuginfo.SourceCallsite;
+import org.eclipse.tracecompass.common.core.process.ProcessUtils;
 import org.eclipse.tracecompass.tmf.core.event.lookup.TmfCallsite;
 
 import com.google.common.base.Objects;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Iterables;
 
 /**
  * Utility class to get file name, function/symbol name and line number from a
@@ -96,32 +95,41 @@ public final class FileOffsetMapper {
         }
     }
 
+
     /**
-     * Cache of all calls to 'addr2line', so that we can avoid recalling the
-     * external process repeatedly.
+     * Generate the callsite from a given binary file and address offset.
      *
-     * It is static, meaning one cache for the whole application, since the
-     * symbols in a file on disk are independent from the trace referring to it.
+     * Due to function inlining, it is possible for one offset to actually have
+     * multiple call sites. We will return the most precise one (inner-most) we
+     * have available.
+     *
+     * @param file
+     *            The binary file to look at
+     * @param buildId
+     *            The expected buildId of the binary file (is not verified at
+     *            the moment)
+     * @param offset
+     *            The memory offset in the file
+     * @return The corresponding call site
      */
-    private static final LoadingCache<FileOffset, @Nullable Iterable<SourceCallsite>> CALLSITE_CACHE;
-    static {
-        CALLSITE_CACHE = checkNotNull(CacheBuilder.newBuilder()
-            .maximumSize(CACHE_SIZE)
-            .build(new CacheLoader<FileOffset, @Nullable Iterable<SourceCallsite>>() {
-                @Override
-                public @Nullable Iterable<SourceCallsite> load(FileOffset fo) {
-                    LOGGER.fine(() -> "[FileOffsetMapper:CacheMiss] file/offset=" + fo.toString()); //$NON-NLS-1$
-                    return getCallsiteFromOffsetWithAddr2line(fo);
-                }
-            }));
+    public static @Nullable TmfCallsite getCallsiteFromOffset(File file, @Nullable String buildId, long offset) {
+       Iterable<Addr2lineInfo> output = getAddr2lineInfo(file, buildId, offset);
+       if (output == null || Iterables.isEmpty(output)) {
+           return null;
+       }
+       Addr2lineInfo info = Iterables.getLast(output);
+       String sourceFile = info.fSourceFileName;
+       Long sourceLine = info.fSourceLineNumber;
+
+       if (sourceFile == null) {
+           /* Not enough information to provide a callsite */
+           return null;
+       }
+       return new TmfCallsite(sourceFile, sourceLine);
     }
 
     /**
-     * Generate the callsites from a given binary file and address offset.
-     *
-     * Due to function inlining, it is possible for one offset to actually have
-     * multiple call sites. This is why we can return more than one callsite per
-     * call.
+     * Get the function/symbol name corresponding to binary file and offset.
      *
      * @param file
      *            The binary file to look at
@@ -130,11 +138,78 @@ public final class FileOffsetMapper {
      *            the moment)
      * @param offset
      *            The memory offset in the file
-     * @return The list of callsites corresponding to the offset, reported from
-     *         the "highest" inlining location, down to the initial definition.
+     * @return The corresponding function/symbol name
+     */
+    public static @Nullable String getFunctionNameFromOffset(File file, @Nullable String buildId, long offset) {
+        /*
+         * TODO We are currently also using 'addr2line' to resolve function
+         * names, which requires the binary's DWARF information to be available.
+         * A better approach would be to use the binary's symbol table (if it is
+         * not stripped), since this is usually more readily available than
+         * DWARF.
+         */
+        Iterable<Addr2lineInfo> output = getAddr2lineInfo(file, buildId, offset);
+        if (output == null || Iterables.isEmpty(output)) {
+            return null;
+        }
+        Addr2lineInfo info = Iterables.getLast(output);
+        return info.fFunctionName;
+    }
+
+    // ------------------------------------------------------------------------
+    // Utility methods making use of 'addr2line'
+    // ------------------------------------------------------------------------
+
+    /**
+     * Value used in addr2line output to represent unknown function names or
+     * source files.
+     */
+    private static final String UNKNOWN_VALUE = "??"; //$NON-NLS-1$
+
+    /**
+     * Cache of all calls to 'addr2line', so that we can avoid recalling the
+     * external process repeatedly.
+     *
+     * It is static, meaning one cache for the whole application, since the
+     * symbols in a file on disk are independent from the trace referring to it.
      */
-    public static @Nullable Iterable<SourceCallsite> getCallsiteFromOffset(File file, @Nullable String buildId, long offset) {
-        LOGGER.finer(() -> String.format("[FileOffsetMapper:Request] file=%s, buildId=%s, offset=0x%h", //$NON-NLS-1$
+    private static final LoadingCache<FileOffset, @NonNull Iterable<Addr2lineInfo>> ADDR2LINE_INFO_CACHE;
+    static {
+        ADDR2LINE_INFO_CACHE = checkNotNull(CacheBuilder.newBuilder()
+            .maximumSize(CACHE_SIZE)
+            .build(new CacheLoader<FileOffset, @NonNull Iterable<Addr2lineInfo>>() {
+                @Override
+                public @NonNull Iterable<Addr2lineInfo> load(FileOffset fo) {
+                    LOGGER.fine(() -> "[FileOffsetMapper:CacheMiss] file/offset=" + fo.toString()); //$NON-NLS-1$
+                    return callAddr2line(fo);
+                }
+            }));
+    }
+
+    private static class Addr2lineInfo {
+
+        private final @Nullable String fSourceFileName;
+        private final @Nullable Long fSourceLineNumber;
+        private final @Nullable String fFunctionName;
+
+        public Addr2lineInfo(@Nullable String sourceFileName,  @Nullable String functionName, @Nullable Long sourceLineNumber) {
+            fSourceFileName = sourceFileName;
+            fSourceLineNumber = sourceLineNumber;
+            fFunctionName = functionName;
+        }
+
+        @Override
+        public String toString() {
+            return Objects.toStringHelper(this)
+                    .add("fSourceFileName", fSourceFileName) //$NON-NLS-1$
+                    .add("fSourceLineNumber", fSourceLineNumber) //$NON-NLS-1$
+                    .add("fFunctionName", fFunctionName) //$NON-NLS-1$
+                    .toString();
+        }
+    }
+
+    private static @Nullable Iterable<Addr2lineInfo> getAddr2lineInfo(File file, @Nullable String buildId, long offset) {
+        LOGGER.finer(() -> String.format("[FileOffsetMapper:Addr2lineRequest] file=%s, buildId=%s, offset=0x%h", //$NON-NLS-1$
                 file.toString(), buildId, offset));
 
         if (!Files.exists((file.toPath()))) {
@@ -145,24 +220,24 @@ public final class FileOffsetMapper {
         // the file we are attempting to open.
         FileOffset fo = new FileOffset(checkNotNull(file.toString()), buildId, offset);
 
-        Iterable<SourceCallsite> callsites = CALLSITE_CACHE.getUnchecked(fo);
+        @Nullable Iterable<Addr2lineInfo> callsites = ADDR2LINE_INFO_CACHE.getUnchecked(fo);
         LOGGER.finer(() -> String.format("[FileOffsetMapper:RequestComplete] callsites=%s", callsites)); //$NON-NLS-1$
         return callsites;
     }
 
-    private static @Nullable Iterable<SourceCallsite> getCallsiteFromOffsetWithAddr2line(FileOffset fo) {
+    private static Iterable<Addr2lineInfo> callAddr2line(FileOffset fo) {
         String filePath = fo.fFilePath;
         long offset = fo.fOffset;
 
-        List<SourceCallsite> callsites = new LinkedList<>();
+        List<Addr2lineInfo> callsites = new LinkedList<>();
 
         // FIXME Could eventually use CDT's Addr2line class once it implements --inlines
-        List<String> output = getOutputFromCommand(Arrays.asList(
-                ADDR2LINE_EXECUTABLE, "-i", "-f", "-C", "-e", filePath, "0x" + Long.toHexString(offset)));  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
+        List<String> command = Arrays.asList(ADDR2LINE_EXECUTABLE, "-i", "-f", "-C", "-e", filePath, "0x" + Long.toHexString(offset)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
+        List<String> output = ProcessUtils.getOutputFromCommand(command);
 
         if (output == null) {
             /* Command returned an error */
-            return null;
+            return Collections.EMPTY_SET;
         }
 
         /*
@@ -180,45 +255,29 @@ public final class FileOffsetMapper {
 
             if (oddLine) {
                 /* This is a line indicating the function name */
-                currentFunctionName = outputLine;
+                if (outputLine.equals(UNKNOWN_VALUE)) {
+                    currentFunctionName = null;
+                } else {
+                    currentFunctionName = outputLine;
+                }
             } else {
                 /* This is a line indicating a call site */
                 String[] elems = outputLine.split(":"); //$NON-NLS-1$
                 String fileName = elems[0];
-                if (fileName.equals("??")) { //$NON-NLS-1$
-                    continue;
+                if (fileName.equals(UNKNOWN_VALUE)) {
+                    fileName = null;
                 }
+                Long lineNumber;
                 try {
-                    long lineNumber = Long.parseLong(elems[1]);
-                    callsites.add(new SourceCallsite(fileName, currentFunctionName, lineNumber));
-
+                    lineNumber = Long.valueOf(elems[1]);
                 } catch (NumberFormatException e) {
-                    /*
-                     * Probably a '?' output, meaning unknown line number.
-                     * Ignore this entry.
-                     */
-                    continue;
+                    /* Probably a '?' output, meaning unknown line number. */
+                    lineNumber = null;
                 }
+                callsites.add(new Addr2lineInfo(fileName, currentFunctionName, lineNumber));
             }
         }
 
         return callsites;
     }
-
-    private static @Nullable List<String> getOutputFromCommand(List<String> command) {
-        try {
-            ProcessBuilder builder = new ProcessBuilder(command);
-            builder.redirectErrorStream(true);
-
-            Process p = builder.start();
-            try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));) {
-                int ret = p.waitFor();
-                List<String> lines = br.lines().collect(Collectors.toList());
-
-                return (ret == 0 ? lines : null);
-            }
-        } catch (IOException | InterruptedException e) {
-            return null;
-        }
-    }
 }
This page took 0.03599 seconds and 5 git commands to generate.