tmf: Use a symbol provider to locate symbols
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / symbols / BasicSymbolProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Movidius Inc. and others
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
11 package org.eclipse.tracecompass.internal.tmf.ui.symbols;
12
13 import java.io.File;
14 import java.util.Collections;
15 import java.util.Map;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.internal.tmf.core.callstack.FunctionNameMapper;
21 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
22 import org.eclipse.tracecompass.tmf.core.event.lookup.TmfCallsite;
23 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
24 import org.eclipse.tracecompass.tmf.ui.symbols.ISymbolProvider;
25 import org.eclipse.tracecompass.tmf.ui.symbols.ISymbolProviderPreferencePage;
26
27 /**
28 * The {@link BasicSymbolProvider} can use either an executable or a simple
29 * symbol mapping file to resolve symbols.
30 *
31 * @author Robert Kiss
32 *
33 */
34 public class BasicSymbolProvider implements ISymbolProvider {
35
36 private final @NonNull ITmfTrace fTrace;
37
38 private @NonNull Map<String, String> fMapping = Collections.emptyMap();
39
40 private String fSource;
41
42 private @NonNull SourceKind fKind = SourceKind.BINARY;
43
44 private boolean fConfigured;
45
46 /**
47 * The kind of source this provider is configured with
48 *
49 */
50 public static enum SourceKind {
51 /**
52 * Literal for binary configuration
53 */
54 BINARY,
55
56 /**
57 * Literal for mapping configuration
58 */
59 MAPPING;
60 }
61
62 /**
63 * Create a new {@link BasicSymbolProvider} for the given trace
64 *
65 * @param trace
66 * A non-null trace
67 */
68 public BasicSymbolProvider(@NonNull ITmfTrace trace) {
69 fTrace = trace;
70 }
71
72 /**
73 *
74 * @return the configured source
75 */
76 public String getConfiguredSource() {
77 return fSource;
78 }
79
80 /**
81 * @return the configured source kind
82 */
83 public @NonNull SourceKind getConfiguredSourceKind() {
84 return fKind;
85 }
86
87 /**
88 * Set the configuration to the given source and kind.
89 *
90 * @param fileSource
91 * File path to either a binary file or a mapping file.
92 * @param kind
93 * the type of the referenced file
94 */
95 public void setConfiguredSource(String fileSource, @NonNull SourceKind kind) {
96 fSource = fileSource;
97 fKind = kind;
98 fConfigured = false;
99 }
100
101 @Override
102 public @NonNull ITmfTrace getTrace() {
103 return fTrace;
104 }
105
106 @Override
107 public void loadConfiguration(IProgressMonitor monitor) {
108 if (!fConfigured) {
109 synchronized (this) {
110 if (!fConfigured) {
111 try {
112 fMapping = Collections.emptyMap();
113 if (fSource != null) {
114 File file = new File(fSource);
115 if (file.isFile()) {
116 Map<String, String> result;
117 if (fKind == SourceKind.BINARY) {
118 result = FunctionNameMapper.mapFromBinaryFile(file);
119 } else {
120 result = FunctionNameMapper.mapFromNmTextFile(file);
121 }
122 if (result != null) {
123 fMapping = result;
124 }
125 }
126 }
127 } finally {
128 fConfigured = true;
129 }
130 }
131 }
132 }
133 }
134
135 @Override
136 public @Nullable String getSymbolText(long address) {
137 loadConfiguration(null);
138 return fMapping.get(Long.toHexString(address));
139 }
140
141 @Override
142 public @Nullable ITmfCallsite getSymbolInfo(long address) {
143 loadConfiguration(null);
144 String symbolText = getSymbolText(address);
145 if (symbolText != null) {
146 return new TmfCallsite(null, symbolText, -1);
147 }
148 return null;
149 }
150
151 @Override
152 public ISymbolProviderPreferencePage createPreferencePage() {
153 return new BasicSymbolProviderPreferencePage(this);
154 }
155
156 }
This page took 0.035814 seconds and 5 git commands to generate.