lttng.ust: Introduce a cache of the calls to addr2line
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.core / src / org / eclipse / tracecompass / lttng2 / ust / core / analysis / debuginfo / UstDebugInfoBinaryAspect.java
CommitLineData
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
10package org.eclipse.tracecompass.lttng2.ust.core.analysis.debuginfo;
11
12import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
13
14import org.eclipse.jdt.annotation.Nullable;
15import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo.UstDebugInfoLoadedBinaryFile;
16import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace;
17import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout;
18import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
19import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
20import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
21import 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 */
ec48d248 33public 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 }
56
57 ILttngUstEventLayout layout = ((LttngUstTrace) event.getTrace()).getEventLayout();
58
59 /* We need both the vpid and ip contexts */
60 ITmfEventField vpidField = event.getContent().getField(layout.contextVpid());
61 ITmfEventField ipField = event.getContent().getField(layout.contextIp());
62 if (vpidField == null || ipField == null) {
63 return null;
64 }
65 Long vpid = (Long) vpidField.getValue();
66 Long ip = (Long) ipField.getValue();
67
68 /*
69 * First match the IP to the correct binary or library, by using the
70 * UstDebugInfoAnalysis.
71 */
72 UstDebugInfoAnalysisModule module =
73 TmfTraceUtils.getAnalysisModuleOfClass(event.getTrace(),
74 UstDebugInfoAnalysisModule.class, UstDebugInfoAnalysisModule.ID);
75 if (module == null) {
76 /*
77 * The analysis is not available for this trace, we won't be
78 * able to find the information.
79 */
80 return null;
81 }
82 long ts = event.getTimestamp().getValue();
83 UstDebugInfoLoadedBinaryFile file = module.getMatchingFile(ts, vpid, ip);
84 if (file == null) {
85 return null;
86 }
87
88 long offset;
89 if (isPIC(file)) {
90 offset = (ip.longValue() - file.getBaseAddress());
91 } else {
92 /*
93 * In the case of the object being the main binary (loaded at a very
94 * low address), we must pass the actual ip address to addr2line.
95 */
96 offset = ip.longValue();
97 }
98
99 // TODO If the binary is present on the current file system, we could
100 // try to get the symbol name from it.
101
102 return new BinaryCallsite(file.getFilePath(), EMPTY_STRING, offset);
103 }
104
105 /**
106 * Return if the given file (binary or library) is Position-Independent Code
107 * or not. This indicates if addr2line considers the addresses as absolute
108 * addresses or as offsets.
109 */
110 private static boolean isPIC(UstDebugInfoLoadedBinaryFile file) {
111 /*
112 * Ghetto binary/library identification for now. It would be possible to
113 * parse the ELF binary to check if it is position-independent
114 * (-fPIC/-fPIE) or not.
115 */
116 String filePath = file.getFilePath();
117 return (filePath.endsWith(".so") || filePath.contains(".so.")); //$NON-NLS-1$ //$NON-NLS-2$
118 }
119
120}
This page took 0.03455 seconds and 5 git commands to generate.