Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 13658b6520054bbdabd6abf42bf092cff2e939a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.debug.test.services;

import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.eclipse.tcf.debug.test.services.ResetMap.IResettable;
import org.eclipse.tcf.debug.test.util.AbstractCache;
import org.eclipse.tcf.debug.test.util.ICache;
import org.eclipse.tcf.debug.test.util.TokenCache;
import org.eclipse.tcf.debug.test.util.TransactionCache;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IMemoryMap;
import org.eclipse.tcf.services.IMemoryMap.MemoryMapListener;
import org.eclipse.tcf.services.IRunControl.RunControlContext;
import org.eclipse.tcf.services.IRunControl.RunControlListener;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.services.ISymbols.Symbol;

/**
 * 
 */
public class SymbolsCM extends AbstractCacheManager {
    
    private ResetMap fRunControlResetMap = new ResetMap();
    private ResetMap fMemoryResetMap = new ResetMap();
    
    private ISymbols fService;
    private IMemoryMap fMemoryMap;
    private RunControlCM fRunControlCM;
    
    public SymbolsCM(ISymbols service, RunControlCM runControl, IMemoryMap memoryMap) {
        fService = service;
        fRunControlCM = runControl;
        fRunControlCM.addListener(fRunControlListener);
        fMemoryMap = memoryMap;
        fMemoryMap.addListener(fMemoryListener);
    }

    @Override
    public void dispose() {
        fRunControlCM.removeListener(fRunControlListener);
        fMemoryMap.removeListener(fMemoryListener);
        super.dispose();
    }
    

    abstract private class SymbolCache<V> extends TransactionCache<V> {
        protected final AbstractCache<V> fInner;
        
        public SymbolCache(AbstractCache<V> inner) {
            fInner = inner;
        }
        
        abstract protected String getSymbolId();

        @Override
        protected V process() throws InvalidCacheException, ExecutionException {
            validate(fInner);
            Symbol sym = validate( getContext(getSymbolId()) );
            addPending(sym, fInner);
            RunControlContext rcContext = validate(fRunControlCM.getContext(sym.getOwnerID()));
            addValid(sym, rcContext, fInner);
            return validate(fInner);            
        }
    }
        
    private void addPending(Symbol sym, IResettable cache) {
        if (sym.getUpdatePolicy() == ISymbols.UPDATE_ON_EXE_STATE_CHANGES) {
            fRunControlResetMap.addPending(cache);
        }
        fMemoryResetMap.addPending(cache);
    }
    
    private void addValid(Symbol sym, RunControlContext rcContext, IResettable cache) {
        if (sym.getUpdatePolicy() == ISymbols.UPDATE_ON_EXE_STATE_CHANGES) {
            String ownerId = sym.getOwnerID();
            fRunControlResetMap.addValid(ownerId, cache);
        }
        fMemoryResetMap.addValid(rcContext.getProcessID(), cache);
    }
    
    private class ChildrenCache extends SymbolCache<String[]> {
        public ChildrenCache(InnerChildrenCache inner) {
            super(inner);
        }
        
        @Override
        protected String getSymbolId() {
            return ((InnerChildrenCache)fInner).fId;
        }
    }        
    
    private class InnerChildrenCache extends TokenCache<String[]> implements ISymbols.DoneGetChildren {
        private final String fId;
        public InnerChildrenCache(String id) {
            fId = id;
        }
        
        @Override
        protected IToken retrieveToken() {
            fRunControlResetMap.addPending(this);
            return fService.getChildren(fId, this);
        }
        public void doneGetChildren(IToken token, Exception error, String[] context_ids) {
            set(token, context_ids, error);
        }
    };

    private class ChildrenCacheKey extends IdKey<ChildrenCache> {
        public ChildrenCacheKey(String id) {
            super(ChildrenCache.class, id);
        }
        @Override ChildrenCache createCache() { return new ChildrenCache( new InnerChildrenCache(fId) ); }        
    }
    
    public ICache<String[]> getChildren(String id) {
        return mapCache(new ChildrenCacheKey(id));
    }

    public ICache<Symbol> getContext(final String id) {
        class MyCache extends TransactionCache<Symbol> {

            class InnerCache extends TokenCache<Symbol> implements ISymbols.DoneGetContext{
                @Override
                protected IToken retrieveToken() {
                    return fService.getContext(id, this);
                }
                
                @Override
                public void doneGetContext(IToken token, Exception error, Symbol context) {
                    set(token, context, error);
                }
            };
            
            private final InnerCache fInner = new InnerCache();
            
            @Override
            protected Symbol process() throws InvalidCacheException, ExecutionException {
                Symbol sym = validate(fInner);
                addPending(sym, fInner);
                RunControlContext rcContext = validate(fRunControlCM.getContext(sym.getOwnerID()));
                addValid(sym, rcContext, fInner);
                return validate(fInner);
            }
        };

        return mapCache(new IdKey<MyCache>(MyCache.class, id) {
            @Override MyCache createCache() { return new MyCache(); }
        });
    }

    public ICache<Map<String, Object>> getLocationInfo(final String symbol_id) {

        class InnerCache extends TokenCache<Map<String,Object>> implements ISymbols.DoneGetLocationInfo {
            @Override
            protected IToken retrieveToken() {
                return fService.getLocationInfo(symbol_id, this);
            }
            
            public void doneGetLocationInfo(IToken token, Exception error, Map<String,Object> props) {
                set(token, props, error);
            }
        }

        class MyCache extends SymbolCache<Map<String,Object>> {
            public MyCache() {
                super(new InnerCache());
            }
            @Override
            protected String getSymbolId() {
                return symbol_id;
            }
        }

        return mapCache(new IdKey<MyCache>(MyCache.class, symbol_id) {
            @Override MyCache createCache() { return new MyCache(); }
        });
    }

    /**
     * Client call back interface for getLocationInfo().
     */
    interface DoneGetLocationInfo {
        /**
         * Called when location information retrieval is done.
         * @param token - command handle.
         * @param error � error description if operation failed, null if succeeded.
         * @param props - symbol location properties, see LOC_*.
         */
        void doneGetLocationInfo(IToken token, Exception error, Map<String,Object> props);
    }

    private class FindCache extends SymbolCache<String> {
        public FindCache(InnerFindCache inner) {
            super(inner);
        }
        @Override
        protected String getSymbolId() {
            return fInner.getData();
        }
    }

    class InnerFindCache extends TokenCache<String> implements ISymbols.DoneFind {
        private final String fId;
        private final Number fIp;
        private final String fName;
        
        public InnerFindCache(String id, Number ip, String name) {
            fId = id;
            fIp = ip;
            fName = name;
        }
        @Override
        protected IToken retrieveToken() {
            return fService.find(fId, fIp, fName, this);
        }
        
        public void doneFind(IToken token, Exception error, String symbol_id) {
            set(token, symbol_id, error);
        }
    }
    
    private class FindCacheKey extends IdKey<FindCache> {
        private final Number fIp;
        private final String fName;        
        
        public FindCacheKey(String id, Number ip, String name) {
            super(FindCache.class, id);
            fIp = ip;
            fName = name;
        }
        @Override FindCache createCache() { return new FindCache(new InnerFindCache(fId, fIp, fName)); }
        
        @Override
        public boolean equals(Object obj) {
            if (super.equals(obj) && obj instanceof FindCacheKey) {
                FindCacheKey other = (FindCacheKey)obj;
                return fIp.equals(other.fIp) && fName.equals(other.fName);
            }
            return false;
        }
        @Override
        public int hashCode() {
            return super.hashCode() + fIp.hashCode() + fName.hashCode();
        }
    }

    public ICache<String> find(String context_id, Number ip, String name) {
        return mapCache(new FindCacheKey(context_id, ip, name));
    }

    private class FindByAddrCache extends SymbolCache<String>  {
        
        public FindByAddrCache(InnerFindByAddrCache inner) {
            super(inner);
        }
        
        @Override
        protected String getSymbolId() {
            return fInner.getData();
        }
    }

    private class InnerFindByAddrCache extends TokenCache<String> implements ISymbols.DoneFind {
        private final String fId;
        private final Number fAddr;
        
        public InnerFindByAddrCache(String id, Number addr) {
            fId = id;
            fAddr = addr;
        }
        @Override
        protected IToken retrieveToken() {
            return fService.findByAddr(fId, fAddr, this);
        }
        
        public void doneFind(IToken token, Exception error, String symbol_id) {
            set(token, symbol_id, error);
        }
    }
    
    private class FindByAddrCacheKey extends IdKey<FindByAddrCache> {
        private final Number fAddr;
        
        public FindByAddrCacheKey(String id, Number addr) {
            super(FindByAddrCache.class, id);
            fAddr = addr;
        }
        @Override FindByAddrCache createCache() { return new FindByAddrCache(new InnerFindByAddrCache(fId, fAddr)); }
        
        @Override
        public boolean equals(Object obj) {
            if (super.equals(obj) && obj instanceof FindByAddrCacheKey) {
                FindByAddrCacheKey other = (FindByAddrCacheKey)obj;
                return fAddr.equals(other.fAddr);
            }
            return false;
        }
        @Override
        public int hashCode() {
            return super.hashCode() + fAddr.hashCode();
        }
    }
    
    public ICache<String> findByAddr(String context_id, Number addr) {
        return mapCache(new FindByAddrCacheKey(context_id, addr));
    }

    private RunControlListener fRunControlListener = new RunControlListener() {
    
        public void contextAdded(RunControlContext[] contexts) {
        }
    
        public void contextChanged(RunControlContext[] contexts) {
            for (RunControlContext context : contexts) {
                resetRunControlContext(context.getID());
            }
        }
    
        public void contextRemoved(String[] context_ids) {
            for (String id : context_ids) {
                resetRunControlContext(id);
                fMemoryResetMap.reset(id);
            }
        }
    
        public void contextSuspended(String context, String pc, String reason, Map<String, Object> params) {
            resetRunControlContext(context);
        }
    
        public void contextResumed(String context) {
            resetRunControlContext(context);
        }
    
        public void containerSuspended(String context, String pc, String reason, Map<String, Object> params,
            String[] suspended_ids) 
        {
            for (String id : suspended_ids) {
                resetRunControlContext(id);
            }
        }
    
        public void containerResumed(String[] context_ids) {
            for (String id : context_ids) {
                resetRunControlContext(id);
            }
        }
    
        public void contextException(String context, String msg) {
            resetRunControlContext(context);
        }
    };

    private void resetRunControlContext(String id) {
        fRunControlResetMap.reset(id);
    }

    private MemoryMapListener fMemoryListener = new MemoryMapListener() {
        public void changed(String context_id) {
            fMemoryResetMap.reset(context_id);
        }
    };
}

Back to the top