1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.analysis
.debuginfo
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.io
.BufferedReader
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStreamReader
;
18 import java
.nio
.file
.Files
;
19 import java
.util
.Arrays
;
20 import java
.util
.LinkedList
;
21 import java
.util
.List
;
22 import java
.util
.logging
.Logger
;
23 import java
.util
.stream
.Collectors
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.common
.core
.log
.TraceCompassLog
;
27 import org
.eclipse
.tracecompass
.lttng2
.ust
.core
.analysis
.debuginfo
.SourceCallsite
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.lookup
.TmfCallsite
;
30 import com
.google
.common
.base
.Objects
;
31 import com
.google
.common
.cache
.CacheBuilder
;
32 import com
.google
.common
.cache
.CacheLoader
;
33 import com
.google
.common
.cache
.LoadingCache
;
36 * Utility class to get file name, function/symbol name and line number from a
37 * given offset. In TMF this is represented as a {@link TmfCallsite}.
39 * @author Alexandre Montplaisir
41 public final class FileOffsetMapper
{
43 private static final Logger LOGGER
= TraceCompassLog
.getLogger(FileOffsetMapper
.class);
45 private static final String DISCRIMINATOR
= "\\(discriminator.*\\)"; //$NON-NLS-1$
46 private static final String ADDR2LINE_EXECUTABLE
= "addr2line"; //$NON-NLS-1$
48 private static final long CACHE_SIZE
= 1000;
50 private FileOffsetMapper() {}
53 * Class representing an offset in a specific file
55 private static class FileOffset
{
57 private final String fFilePath
;
58 private final @Nullable String fBuildId
;
59 private final long fOffset
;
61 public FileOffset(String filePath
, @Nullable String buildId
, long offset
) {
68 public int hashCode() {
69 return Objects
.hashCode(fFilePath
, fBuildId
, fOffset
);
73 public boolean equals(@Nullable Object obj
) {
80 if (getClass() != obj
.getClass()) {
83 FileOffset other
= (FileOffset
) obj
;
84 return Objects
.equal(fFilePath
, other
.fFilePath
) &&
85 Objects
.equal(fBuildId
, other
.fBuildId
) &&
86 Objects
.equal(fOffset
, other
.fOffset
);
90 public String
toString() {
91 return Objects
.toStringHelper(this)
92 .add("fFilePath", fFilePath
) //$NON-NLS-1$
93 .add("fBuildId", fBuildId
) //$NON-NLS-1$
94 .add("fOffset", String
.format("0x%h", fOffset
)) //$NON-NLS-1$ //$NON-NLS-2$
100 * Cache of all calls to 'addr2line', so that we can avoid recalling the
101 * external process repeatedly.
103 * It is static, meaning one cache for the whole application, since the
104 * symbols in a file on disk are independent from the trace referring to it.
106 private static final LoadingCache
<FileOffset
, @Nullable Iterable
<SourceCallsite
>> CALLSITE_CACHE
;
108 CALLSITE_CACHE
= checkNotNull(CacheBuilder
.newBuilder()
109 .maximumSize(CACHE_SIZE
)
110 .build(new CacheLoader
<FileOffset
, @Nullable Iterable
<SourceCallsite
>>() {
112 public @Nullable Iterable
<SourceCallsite
> load(FileOffset fo
) {
113 LOGGER
.fine(() -> "[FileOffsetMapper:CacheMiss] file/offset=" + fo
.toString()); //$NON-NLS-1$
114 return getCallsiteFromOffsetWithAddr2line(fo
);
120 * Generate the callsites from a given binary file and address offset.
122 * Due to function inlining, it is possible for one offset to actually have
123 * multiple call sites. This is why we can return more than one callsite per
127 * The binary file to look at
129 * The expected buildId of the binary file (is not verified at
132 * The memory offset in the file
133 * @return The list of callsites corresponding to the offset, reported from
134 * the "highest" inlining location, down to the initial definition.
136 public static @Nullable Iterable
<SourceCallsite
> getCallsiteFromOffset(File file
, @Nullable String buildId
, long offset
) {
137 LOGGER
.finer(() -> String
.format("[FileOffsetMapper:Request] file=%s, buildId=%s, offset=0x%h", //$NON-NLS-1$
138 file
.toString(), buildId
, offset
));
140 if (!Files
.exists((file
.toPath()))) {
141 LOGGER
.finer(() -> "[FileOffsetMapper:RequestFailed] File not found"); //$NON-NLS-1$
144 // TODO We should also eventually verify that the passed buildId matches
145 // the file we are attempting to open.
146 FileOffset fo
= new FileOffset(checkNotNull(file
.toString()), buildId
, offset
);
148 Iterable
<SourceCallsite
> callsites
= CALLSITE_CACHE
.getUnchecked(fo
);
149 LOGGER
.finer(() -> String
.format("[FileOffsetMapper:RequestComplete] callsites=%s", callsites
)); //$NON-NLS-1$
153 private static @Nullable Iterable
<SourceCallsite
> getCallsiteFromOffsetWithAddr2line(FileOffset fo
) {
154 String filePath
= fo
.fFilePath
;
155 long offset
= fo
.fOffset
;
157 List
<SourceCallsite
> callsites
= new LinkedList
<>();
159 // FIXME Could eventually use CDT's Addr2line class once it implements --inlines
160 List
<String
> output
= getOutputFromCommand(Arrays
.asList(
161 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$
163 if (output
== null) {
164 /* Command returned an error */
169 * When passing the -f flag, the output alternates between function
170 * names and file/line location.
172 boolean oddLine
= false; // We flip at the start, first loop will be odd
173 String currentFunctionName
= null;
174 for (String outputLine
: output
) {
175 /* Flip the boolean for the following line */
178 // Remove discriminator part, for example: /build/buildd/glibc-2.21/elf/dl-object.c:78 (discriminator 8)
179 outputLine
= outputLine
.replaceFirst(DISCRIMINATOR
, "").trim(); //$NON-NLS-1$
182 /* This is a line indicating the function name */
183 currentFunctionName
= outputLine
;
185 /* This is a line indicating a call site */
186 String
[] elems
= outputLine
.split(":"); //$NON-NLS-1$
187 String fileName
= elems
[0];
188 if (fileName
.equals("??")) { //$NON-NLS-1$
192 long lineNumber
= Long
.parseLong(elems
[1]);
193 callsites
.add(new SourceCallsite(fileName
, currentFunctionName
, lineNumber
));
195 } catch (NumberFormatException e
) {
197 * Probably a '?' output, meaning unknown line number.
208 private static @Nullable List
<String
> getOutputFromCommand(List
<String
> command
) {
210 ProcessBuilder builder
= new ProcessBuilder(command
);
211 builder
.redirectErrorStream(true);
213 Process p
= builder
.start();
214 try (BufferedReader br
= new BufferedReader(new InputStreamReader(p
.getInputStream()));) {
215 int ret
= p
.waitFor();
216 List
<String
> lines
= br
.lines().collect(Collectors
.toList());
218 return (ret
== 0 ? lines
: null);
220 } catch (IOException
| InterruptedException e
) {