Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf40d6a4b9d6750aab8e6a892e4383c09e09d1d8 (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
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems, Inc. 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.tm.internal.tcf.dsf.services;

import java.util.Hashtable;

import org.eclipse.cdt.core.IAddress;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.dd.dsf.concurrent.DataRequestMonitor;
import org.eclipse.dd.dsf.concurrent.RequestMonitor;
import org.eclipse.dd.dsf.datamodel.IDMContext;
import org.eclipse.dd.dsf.service.AbstractDsfService;
import org.eclipse.dd.dsf.service.DsfSession;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.tm.internal.tcf.dsf.Activator;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IMemory.MemoryContext;
import org.eclipse.tm.tcf.services.IMemory.MemoryError;
import org.osgi.framework.BundleContext;


public class TCFDSFMemory extends AbstractDsfService implements org.eclipse.dd.dsf.debug.service.IMemory {
    
    private class MemoryCache implements TCFDSFExecutionDMC.DataCache {
        
        final TCFDataCache<org.eclipse.tm.tcf.services.IMemory.MemoryContext> context;
        
        MemoryCache(final IChannel channel, final TCFDSFExecutionDMC exe) {
            context = new TCFDataCache<org.eclipse.tm.tcf.services.IMemory.MemoryContext>(channel) {
                @Override
                public boolean startDataRetrieval() {
                    assert command == null;
                    String id = exe.getTcfContextId();
                    if (id == null || tcf_mem_service == null) {
                        data = null;
                        valid = true;
                        return true;
                    }
                    command = tcf_mem_service.getContext(id,
                            new org.eclipse.tm.tcf.services.IMemory.DoneGetContext() {
                        public void doneGetContext(IToken token, Exception err,
                                org.eclipse.tm.tcf.services.IMemory.MemoryContext ctx) {
                            if (command != token) return;
                            command = null;
                            if (err != null) {
                                error = err;
                            }
                            else {
                                data = ctx;
                            }
                            valid = true;
                            validate();
                        }
                    });
                    return false;
                }
            };
        }
    }
    
    private final org.eclipse.tm.tcf.services.IMemory.MemoryListener mem_listener =
        new org.eclipse.tm.tcf.services.IMemory.MemoryListener() {

            public void contextAdded(MemoryContext[] contexts) {
            }

            public void contextChanged(MemoryContext[] contexts) {
            }

            public void contextRemoved(String[] context_ids) {
            }

            public void memoryChanged(String context_id, Number[] addr, long[] size) {
                TCFDSFRunControl rc = getServicesTracker().getService(TCFDSFRunControl.class);
                TCFDSFExecutionDMC exe = rc.getContext(context_id);
                if (exe == null || exe.memory_cache == null) return;
                for (int n = 0; n < addr.length; n++) {
                    long count = size[n];
                    // TODO: DSF does not support address ranges
                    if (count > 256) count = 256;
                    IAddress[] addresses = new IAddress[(int)count];
                    for (int i = 0; i < (int)count; i++) {
                        addresses[i] = new TCFAddress(addr[n]).add(i);
                    }
                    getSession().dispatchEvent(new MemoryChangedEvent(exe, addresses), getProperties());
                }
            }
    };
    
    private final IChannel channel;
    private final org.eclipse.tm.tcf.services.IMemory tcf_mem_service;

    public TCFDSFMemory(DsfSession session, IChannel channel, final RequestMonitor monitor) {
        super(session);
        this.channel = channel;
        tcf_mem_service = channel.getRemoteService(org.eclipse.tm.tcf.services.IMemory.class);
        if (tcf_mem_service != null) tcf_mem_service.addListener(mem_listener);
        initialize(new RequestMonitor(getExecutor(), monitor) { 
            @Override
            protected void handleOK() {
                String[] class_names = {
                        org.eclipse.dd.dsf.debug.service.IMemory.class.getName(),
                        TCFDSFMemory.class.getName()
                };
                register(class_names, new Hashtable<String,String>());
                monitor.done();
            }
        });
    }

    @Override 
    public void shutdown(RequestMonitor monitor) {
        unregister();
        super.shutdown(monitor);
    }

    @Override
    protected BundleContext getBundleContext() {
        return Activator.getBundleContext();
    }

    public void fillMemory(final IDMContext dmc, final IAddress address, final long offset,
            final int word_size, final int count, final byte[] pattern, final RequestMonitor rm) {
        if (tcf_mem_service == null) {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Memory access service is not available", null)); //$NON-NLS-1$
            rm.done();
        }
        else if (dmc instanceof TCFDSFExecutionDMC) {
            final TCFDSFExecutionDMC ctx = (TCFDSFExecutionDMC)dmc;
            if (ctx.memory_cache == null) ctx.memory_cache = new MemoryCache(channel, ctx);
            MemoryCache cache = (MemoryCache)ctx.memory_cache;
            if (!cache.context.validate()) {
                cache.context.addWaitingRequest(new IDataRequest() {
                    public void cancel() {
                        rm.setStatus(new Status(IStatus.CANCEL, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Canceled", null)); //$NON-NLS-1$
                        rm.setCanceled(true);
                        rm.done();
                    }
                    public void done() {
                        fillMemory(dmc, address, offset, word_size, count, pattern, rm);
                    }
                });
                return;
            }
            if (cache.context.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.context.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            org.eclipse.tm.tcf.services.IMemory.MemoryContext mem = cache.context.getData();
            if (mem == null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Invalid DMC", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
            mem.fill(address.add(offset).getValue(), word_size, pattern, count * word_size, 0,
                    new org.eclipse.tm.tcf.services.IMemory.DoneMemory() {
                public void doneMemory(IToken token, MemoryError error) {
                    if (rm.isCanceled()) return;
                    if (error != null) {
                        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Command error", error)); //$NON-NLS-1$
                    }
                    rm.done();
                }
            });
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void getMemory(final IDMContext dmc, final IAddress address, final long offset,
            final int word_size, final int count, final DataRequestMonitor<MemoryByte[]> rm) {
        if (tcf_mem_service == null) {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Memory access service is not available", null)); //$NON-NLS-1$
            rm.done();
        }
        else if (dmc instanceof TCFDSFExecutionDMC) {
            final TCFDSFExecutionDMC ctx = (TCFDSFExecutionDMC)dmc;
            if (ctx.memory_cache == null) ctx.memory_cache = new MemoryCache(channel, ctx);
            MemoryCache cache = (MemoryCache)ctx.memory_cache;
            if (!cache.context.validate()) {
                cache.context.addWaitingRequest(new IDataRequest() {
                    public void cancel() {
                        rm.setStatus(new Status(IStatus.CANCEL, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Canceled", null)); //$NON-NLS-1$
                        rm.setCanceled(true);
                        rm.done();
                    }
                    public void done() {
                        getMemory(dmc, address, offset, word_size, count, rm);
                    }
                });
                return;
            }
            if (cache.context.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.context.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            org.eclipse.tm.tcf.services.IMemory.MemoryContext mem = cache.context.getData();
            if (mem == null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Invalid DMC", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
            final byte[] buffer = new byte[word_size * count];
            mem.get(address.add(offset).getValue(), word_size, buffer, 0, count * word_size, 0,
                    new org.eclipse.tm.tcf.services.IMemory.DoneMemory() {
                public void doneMemory(IToken token, MemoryError error) {
                    if (rm.isCanceled()) return;
                    if (error != null) {
                        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Command error", error)); //$NON-NLS-1$
                    }
                    MemoryByte[] res = new MemoryByte[buffer.length];
                    for (int i = 0; i < buffer.length; i++) {
                        res[i] = new MemoryByte(buffer[i]);
                    }
                    rm.done();
                }
            });
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void setMemory(final IDMContext dmc, final IAddress address, final long offset,
            final int word_size, final int count, final byte[] buffer, final RequestMonitor rm) {
        if (tcf_mem_service == null) {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Memory access service is not available", null)); //$NON-NLS-1$
            rm.done();
        }
        else if (dmc instanceof TCFDSFExecutionDMC) {
            final TCFDSFExecutionDMC ctx = (TCFDSFExecutionDMC)dmc;
            if (ctx.memory_cache == null) ctx.memory_cache = new MemoryCache(channel, ctx);
            MemoryCache cache = (MemoryCache)ctx.memory_cache;
            if (!cache.context.validate()) {
                cache.context.addWaitingRequest(new IDataRequest() {
                    public void cancel() {
                        rm.setStatus(new Status(IStatus.CANCEL, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Canceled", null)); //$NON-NLS-1$
                        rm.setCanceled(true);
                        rm.done();
                    }
                    public void done() {
                        setMemory(dmc, address, offset, word_size, count, buffer, rm);
                    }
                });
                return;
            }
            if (cache.context.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.context.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            org.eclipse.tm.tcf.services.IMemory.MemoryContext mem = cache.context.getData();
            if (mem == null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Invalid DMC", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
            mem.set(address.add(offset).getValue(), word_size, buffer, 0, count * word_size, 0,
                    new org.eclipse.tm.tcf.services.IMemory.DoneMemory() {
                public void doneMemory(IToken token, MemoryError error) {
                    if (rm.isCanceled()) return;
                    if (error != null) {
                        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Command error", error)); //$NON-NLS-1$
                    }
                    rm.done();
                }
            });
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }
}

Back to the top