40c5598d618d9f4f6cb858643d136c2d3430a4a3
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.core / src / org / eclipse / tracecompass / internal / lttng2 / ust / core / analysis / debuginfo / FileOffsetMapper.java
1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.io.BufferedReader;
15 import java.io.File;
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.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.tracecompass.tmf.core.event.lookup.TmfCallsite;
26
27 import com.google.common.base.Objects;
28 import com.google.common.cache.CacheBuilder;
29 import com.google.common.cache.CacheLoader;
30 import com.google.common.cache.LoadingCache;
31
32 /**
33 * Utility class to get file name, function/symbol name and line number from a
34 * given offset. In TMF this is represented as a {@link TmfCallsite}.
35 *
36 * @author Alexandre Montplaisir
37 */
38 public final class FileOffsetMapper {
39
40 private static final String DISCRIMINATOR = "\\(discriminator.*\\)"; //$NON-NLS-1$
41 private static final String ADDR2LINE_EXECUTABLE = "addr2line"; //$NON-NLS-1$
42
43 private static final long CACHE_SIZE = 1000;
44
45 private FileOffsetMapper() {}
46
47 /**
48 * Class representing an offset in a specific file
49 */
50 private static class FileOffset {
51
52 private final String fFilePath;
53 private final long fOffset;
54
55 public FileOffset(String filePath, long offset) {
56 fFilePath = filePath;
57 fOffset = offset;
58 }
59
60 @Override
61 public int hashCode() {
62 return Objects.hashCode(fFilePath, fOffset);
63 }
64
65 @Override
66 public boolean equals(@Nullable Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (obj == null) {
71 return false;
72 }
73 if (getClass() != obj.getClass()) {
74 return false;
75 }
76 FileOffset other = (FileOffset) obj;
77 if (!fFilePath.equals(other.fFilePath)) {
78 return false;
79 }
80 if (fOffset != other.fOffset) {
81 return false;
82 }
83 return true;
84 }
85 }
86
87 /**
88 * Cache of all calls to 'addr2line', so that we can avoid recalling the
89 * external process repeatedly.
90 *
91 * It is static, meaning one cache for the whole application, since the
92 * symbols in a file on disk are independent from the trace referring to it.
93 */
94 private static final LoadingCache<FileOffset, @Nullable Iterable<TmfCallsite>> CALLSITE_CACHE;
95 static {
96 CALLSITE_CACHE = checkNotNull(CacheBuilder.newBuilder()
97 .maximumSize(CACHE_SIZE)
98 .build(new CacheLoader<FileOffset, @Nullable Iterable<TmfCallsite>>() {
99 @Override
100 public @Nullable Iterable<TmfCallsite> load(FileOffset fo) {
101 return getCallsiteFromOffsetWithAddr2line(fo);
102 }
103 }));
104 }
105
106 /**
107 * Generate the callsites from a given binary file and address offset.
108 *
109 * Due to function inlining, it is possible for one offset to actually have
110 * multiple call sites. This is why we can return more than one callsite per
111 * call.
112 *
113 * @param file
114 * The binary file to look at
115 * @param offset
116 * The memory offset in the file
117 * @return The list of callsites corresponding to the offset, reported from
118 * the "highest" inlining location, down to the initial definition.
119 */
120 public static @Nullable Iterable<TmfCallsite> getCallsiteFromOffset(File file, long offset) {
121 if (!Files.exists((file.toPath()))) {
122 return null;
123 }
124 FileOffset fo = new FileOffset(checkNotNull(file.toString()), offset);
125 return CALLSITE_CACHE.getUnchecked(fo);
126 }
127
128 private static @Nullable Iterable<TmfCallsite> getCallsiteFromOffsetWithAddr2line(FileOffset fo) {
129 String filePath = fo.fFilePath;
130 long offset = fo.fOffset;
131
132 List<TmfCallsite> callsites = new LinkedList<>();
133
134 // FIXME Could eventually use CDT's Addr2line class once it implements --inlines
135 List<String> output = getOutputFromCommand(Arrays.asList(
136 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$
137
138 if (output == null) {
139 /* Command returned an error */
140 return null;
141 }
142
143 /*
144 * When passing the -f flag, the output alternates between function
145 * names and file/line location.
146 */
147 boolean oddLine = true;
148 String currentFunctionName = null;
149 for (String outputLine : output) {
150 // Remove discriminator part, for example: /build/buildd/glibc-2.21/elf/dl-object.c:78 (discriminator 8)
151 outputLine = outputLine.replaceFirst(DISCRIMINATOR, "").trim(); //$NON-NLS-1$
152
153 if (oddLine) {
154 /* This is a line indicating the function name */
155 currentFunctionName = outputLine;
156 } else {
157 /* This is a line indicating a call site */
158 String[] elems = outputLine.split(":"); //$NON-NLS-1$
159 String fileName = elems[0];
160 if (fileName.equals("??")) { //$NON-NLS-1$
161 continue;
162 }
163 long lineNumber = Long.parseLong(elems[1]);
164
165 callsites.add(new TmfCallsite(fileName, currentFunctionName, lineNumber));
166 }
167
168 /* Flip the boolean for the following line */
169 oddLine = !oddLine;
170 }
171
172 return callsites;
173 }
174
175 private static @Nullable List<String> getOutputFromCommand(List<String> command) {
176 try {
177 ProcessBuilder builder = new ProcessBuilder(command);
178 builder.redirectErrorStream(true);
179
180 Process p = builder.start();
181 try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));) {
182 int ret = p.waitFor();
183 List<String> lines = br.lines().collect(Collectors.toList());
184
185 return (ret == 0 ? lines : null);
186 }
187 } catch (IOException | InterruptedException e) {
188 return null;
189 }
190 }
191 }
This page took 0.036063 seconds and 4 git commands to generate.