Commit | Line | Data |
---|---|---|
df993132 AM |
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.lttng2.ust.core.analysis.debuginfo; | |
11 | ||
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString; | |
13 | ||
14 | import org.eclipse.jdt.annotation.Nullable; | |
15 | import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo.UstDebugInfoLoadedBinaryFile; | |
16 | import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace; | |
17 | import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout; | |
18 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
19 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
20 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; | |
21 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
22 | ||
23 | /** | |
24 | * Event aspect of UST traces that indicate the binary callsite (binary, symbol | |
25 | * and offset) from an IP (instruction pointer) context. | |
26 | * | |
27 | * Unlike the {@link UstDebugInfoSourceAspect}, this information should be | |
28 | * available even without debug information. | |
29 | * | |
30 | * @author Alexandre Montplaisir | |
31 | * @since 2.0 | |
32 | */ | |
ac52feb8 | 33 | public final class UstDebugInfoBinaryAspect implements ITmfEventAspect<BinaryCallsite> { |
df993132 AM |
34 | |
35 | /** Singleton instance */ | |
36 | public static final UstDebugInfoBinaryAspect INSTANCE = new UstDebugInfoBinaryAspect(); | |
37 | ||
38 | private UstDebugInfoBinaryAspect() {} | |
39 | ||
40 | @Override | |
41 | public String getName() { | |
42 | return nullToEmptyString(Messages.UstDebugInfoAnalysis_BinaryAspectName); | |
43 | } | |
44 | ||
45 | @Override | |
46 | public String getHelpText() { | |
47 | return nullToEmptyString(Messages.UstDebugInfoAnalysis_BinaryAspectHelpText); | |
48 | } | |
49 | ||
50 | @Override | |
51 | public @Nullable BinaryCallsite resolve(ITmfEvent event) { | |
52 | /* This aspect only supports UST traces */ | |
53 | if (!(event.getTrace() instanceof LttngUstTrace)) { | |
54 | return null; | |
55 | } | |
cb2b5e56 | 56 | LttngUstTrace trace = (LttngUstTrace) event.getTrace(); |
df993132 | 57 | |
cb2b5e56 | 58 | ILttngUstEventLayout layout = trace.getEventLayout(); |
df993132 AM |
59 | |
60 | /* We need both the vpid and ip contexts */ | |
61 | ITmfEventField vpidField = event.getContent().getField(layout.contextVpid()); | |
62 | ITmfEventField ipField = event.getContent().getField(layout.contextIp()); | |
63 | if (vpidField == null || ipField == null) { | |
64 | return null; | |
65 | } | |
66 | Long vpid = (Long) vpidField.getValue(); | |
67 | Long ip = (Long) ipField.getValue(); | |
cb2b5e56 | 68 | long ts = event.getTimestamp().toNanos(); |
df993132 | 69 | |
cb2b5e56 AM |
70 | return getBinaryCallsite(trace, vpid.intValue(), ts, ip.longValue()); |
71 | } | |
72 | ||
73 | /** | |
74 | * Get the binary callsite (which means binary file and offset in this file) | |
75 | * corresponding to the given instruction pointer, for the given PID and | |
76 | * timetamp. | |
77 | * | |
78 | * @param trace | |
79 | * The trace, from which we will get the debug info analysis | |
80 | * @param pid | |
81 | * The PID for which we want the symbol | |
82 | * @param ts | |
83 | * The timestamp of the query | |
84 | * @param ip | |
85 | * The instruction pointer address | |
86 | * @return The {@link BinaryCallsite} object with the relevant information | |
87 | */ | |
88 | public static @Nullable BinaryCallsite getBinaryCallsite(LttngUstTrace trace, int pid, long ts, long ip) { | |
df993132 AM |
89 | /* |
90 | * First match the IP to the correct binary or library, by using the | |
91 | * UstDebugInfoAnalysis. | |
92 | */ | |
93 | UstDebugInfoAnalysisModule module = | |
cb2b5e56 | 94 | TmfTraceUtils.getAnalysisModuleOfClass(trace, |
df993132 AM |
95 | UstDebugInfoAnalysisModule.class, UstDebugInfoAnalysisModule.ID); |
96 | if (module == null) { | |
97 | /* | |
98 | * The analysis is not available for this trace, we won't be | |
99 | * able to find the information. | |
100 | */ | |
101 | return null; | |
102 | } | |
cb2b5e56 | 103 | UstDebugInfoLoadedBinaryFile file = module.getMatchingFile(ts, pid, ip); |
df993132 AM |
104 | if (file == null) { |
105 | return null; | |
106 | } | |
107 | ||
cb2b5e56 AM |
108 | /* Apply the path prefix defined by the trace, if any */ |
109 | String fullPath = (trace.getSymbolProviderConfig().getActualRootDirPath() + file.getFilePath()); | |
1b084865 | 110 | boolean isPIC = isPIC(fullPath); |
cb2b5e56 | 111 | |
df993132 | 112 | long offset; |
1b084865 | 113 | if (isPIC) { |
cb2b5e56 | 114 | offset = (ip - file.getBaseAddress()); |
df993132 AM |
115 | } else { |
116 | /* | |
1b084865 AM |
117 | * In the case of the object being non-position-independant (loaded |
118 | * at a very low address), we must pass the actual 'ip' address | |
119 | * directly to addr2line. | |
df993132 | 120 | */ |
cb2b5e56 | 121 | offset = ip; |
df993132 AM |
122 | } |
123 | ||
1b084865 | 124 | return new BinaryCallsite(fullPath, file.getBuildId(), offset, isPIC); |
df993132 AM |
125 | } |
126 | ||
127 | /** | |
128 | * Return if the given file (binary or library) is Position-Independent Code | |
129 | * or not. This indicates if addr2line considers the addresses as absolute | |
130 | * addresses or as offsets. | |
131 | */ | |
cb2b5e56 | 132 | private static boolean isPIC(String filePath) { |
df993132 AM |
133 | /* |
134 | * Ghetto binary/library identification for now. It would be possible to | |
135 | * parse the ELF binary to check if it is position-independent | |
136 | * (-fPIC/-fPIE) or not. | |
137 | */ | |
df993132 AM |
138 | return (filePath.endsWith(".so") || filePath.contains(".so.")); //$NON-NLS-1$ //$NON-NLS-2$ |
139 | } | |
140 | ||
141 | } |