Exclude Sonar rules that are not working with jdt.annotation
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.core / src / org / eclipse / tracecompass / lttng2 / ust / core / analysis / debuginfo / UstDebugInfoSourceAspect.java
CommitLineData
ef7f180d
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
522dff53
AM
14import java.io.File;
15
ef7f180d 16import org.eclipse.jdt.annotation.Nullable;
522dff53
AM
17import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo.FileOffsetMapper;
18import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo.UstDebugInfoLoadedBinaryFile;
ef7f180d
AM
19import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace;
20import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout;
21import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
23import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
24import org.eclipse.tracecompass.tmf.core.event.lookup.TmfCallsite;
25import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
26
522dff53
AM
27import com.google.common.collect.Iterables;
28
ef7f180d
AM
29/**
30 * Event aspect of UST traces to generate a {@link TmfCallsite} using the debug
31 * info analysis and the IP (instruction pointer) context.
32 *
33 * @author Alexandre Montplaisir
34 * @since 2.0
35 */
a103fe67 36public class UstDebugInfoSourceAspect implements ITmfEventAspect {
ef7f180d
AM
37
38 /** Singleton instance */
a103fe67 39 public static final UstDebugInfoSourceAspect INSTANCE = new UstDebugInfoSourceAspect();
ef7f180d 40
a103fe67 41 private UstDebugInfoSourceAspect() {}
ef7f180d
AM
42
43 @Override
44 public String getName() {
45 return nullToEmptyString(Messages.UstDebugInfoAnalysis_AspectName);
46 }
47
48 @Override
49 public String getHelpText() {
50 return nullToEmptyString(Messages.UstDebugInfoAnalysis_AspectHelpText);
51 }
52
ef7f180d 53 @Override
522dff53 54 public @Nullable TmfCallsite resolve(ITmfEvent event) {
ef7f180d
AM
55 /* This aspect only supports UST traces */
56 if (!(event.getTrace() instanceof LttngUstTrace)) {
57 return null;
58 }
59
60 ILttngUstEventLayout layout = ((LttngUstTrace) event.getTrace()).getEventLayout();
61
62 /* We need both the vpid and ip contexts */
63 ITmfEventField vpidField = event.getContent().getField(layout.contextVpid());
64 ITmfEventField ipField = event.getContent().getField(layout.contextIp());
65 if (vpidField == null || ipField == null) {
66 return null;
67 }
68 Long vpid = (Long) vpidField.getValue();
69 Long ip = (Long) ipField.getValue();
70
71 /*
72 * First match the IP to the correct binary or library, by using the
73 * UstDebugInfoAnalysis.
74 */
75 UstDebugInfoAnalysisModule module =
76 TmfTraceUtils.getAnalysisModuleOfClass(event.getTrace(),
77 UstDebugInfoAnalysisModule.class, UstDebugInfoAnalysisModule.ID);
78 if (module == null) {
79 /*
80 * The analysis is not available for this trace, we won't be
81 * able to find the information.
82 */
83 return null;
84 }
85 long ts = event.getTimestamp().getValue();
522dff53
AM
86 UstDebugInfoLoadedBinaryFile file = module.getMatchingFile(ts, vpid, ip);
87 if (file == null) {
88 return null;
89 }
90
91 long offset;
92 if (isMainBinary(file)) {
93 /*
94 * In the case of the object being the main binary (loaded at a very
95 * low address), we must pass the actual ip address to addr2line.
96 */
97 offset = ip.longValue();
98 } else {
99 offset = (ip.longValue() - file.getBaseAddress());
100 }
101
102 if (offset < 0) {
103 throw new IllegalStateException();
104 }
ef7f180d 105
522dff53
AM
106 Iterable<TmfCallsite> callsites = FileOffsetMapper.getCallsiteFromOffset(new File(file.getFilePath()), offset);
107
108 if (callsites == null || Iterables.isEmpty(callsites)) {
109 return null;
110 }
111 /*
112 * TMF only supports the notion of one callsite per event at the moment.
113 * We will take the "deepest" one in the stack, which should refer to
114 * the initial, non-inlined location.
115 */
116 return Iterables.getLast(callsites);
117 }
118
119 private static boolean isMainBinary(UstDebugInfoLoadedBinaryFile file) {
120 /*
121 * Ghetto binary/library identification for now. It would be possible to
122 * parse the ELF binary to check if it is position-independent
123 * (-fPIC/-fPIE) or not.
124 */
125 return (!file.getFilePath().endsWith(".so")); //$NON-NLS-1$
ef7f180d
AM
126 }
127
128}
This page took 0.06385 seconds and 5 git commands to generate.