Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 24bb022cb2c0286d18445a888764ddf662c598f4 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# *******************************************************************************
# * Copyright (c) 2011 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
# *******************************************************************************

import sys, time, threading, exceptions
import tcf
from tcf import protocol, channel, errors
from tcf.util import sync

__TRACE = False
class TraceListener(channel.TraceListener):
    def onMessageReceived(self, type, token, service, name, data):
        print "<<<", type, token, service, name, data
    def onMessageSent(self, type, token, service, name, data):
        print ">>>", type, token, service, name, data
    def onChannelClosed(self, error):
        print>>sys.stderr, "*** closed ***", error

_suspended = []

def test():
    protocol.startEventQueue()
    try:
        c = tcf.connect("TCP:127.0.0.1:1534")
    except exceptions.Exception as e:
        protocol.log(e)
        sys.exit()
    assert c.state == channel.STATE_OPEN
    if __TRACE: protocol.invokeLater(c.addTraceListener, TraceListener())
    def r2():
        print "services=", c.getRemoteServices()
    protocol.invokeLater(r2)

    try:
        testRunControl(c)
        testStackTrace(c)
        testBreakpoints(c)
        testSymbols(c)
        testRegisters(c)
        testExpressions(c)
        testLineNumbers(c)
        testSyncCommands(c)
        testEvents(c)
        testDataCache(c)
        testProcesses(c)
    except exceptions.Exception as e:
        protocol.log(e)

    if c.state == channel.STATE_OPEN:
        time.sleep(10)
        protocol.invokeLater(c.close)
    time.sleep(5)

def testRunControl(c):
    lock = threading.Condition()
    from tcf.services import runcontrol
    def r3():
        rctrl = c.getRemoteService(runcontrol.NAME)
        pending = []
        class DoneGetContext(runcontrol.DoneGetContext):
            def doneGetContext(self, token, error, context):
                pending.remove(token)
                if error:
                    protocol.log("Error from RunControl.getContext", error)
                else:
                    print context
                class DoneGetState(runcontrol.DoneGetState):
                    def doneGetState(self, token, error, suspended, pc, reason, params):
                        pending.remove(token)
                        if error:
                            protocol.log("Error from RunControl.getState", error)
                        else:
                            print "suspended: ", suspended
                            print "pc:        ", pc
                            print "reason:    ", reason
                            print "params:    ", params
                        if suspended:
                            _suspended.append(context.getID())
                        if len(pending) == 0:
                            with lock:
                                lock.notify()
                if context and context.hasState(): pending.append(context.getState(DoneGetState()))
                if len(pending) == 0:
                    with lock:
                        lock.notify()
        class DoneGetChildren(runcontrol.DoneGetChildren):
            def doneGetChildren(self, token, error, context_ids):
                pending.remove(token)
                if error:
                    protocol.log("Error from RunControl.GetChildren", error)
                else:
                    for c in context_ids:
                        pending.append(rctrl.getContext(c, DoneGetContext()))
                        pending.append(rctrl.getChildren(c, self))
                if len(pending) == 0:
                    with lock:
                        lock.notify()
        pending.append(rctrl.getChildren(None, DoneGetChildren()))
    with lock:
        protocol.invokeLater(r3)
        lock.wait(5000)
    def r4():
        rc = c.getRemoteService(runcontrol.NAME)
        class RCListener(runcontrol.RunControlListener):
            def contextSuspended(self, *args):
                print "context suspended: ", args
                rc.removeListener(self)
            def contextResumed(self, *args):
                print "context resumed: ", args
            def containerSuspended(self, *args):
                print "container suspended:", args
                rc.removeListener(self)
            def containerResumed(self, *args):
                print "container resumed:", args
        rc.addListener(RCListener())
        class DoneGetContext(runcontrol.DoneGetContext):
            def doneGetContext(self, token, error, context):
                if error:
                    protocol.log("Error from RunControl.getContext", error)
                    with lock: lock.notify()
                    return
                class DoneResume(runcontrol.DoneCommand):
                    def doneCommand(self, token, error):
                        if error:
                            protocol.log("Error from RunControl.resume", error)
                        else:
                            context.suspend(runcontrol.DoneCommand())
                        with lock: lock.notify()
                context.resume(runcontrol.RM_RESUME, 1, None, DoneResume())
        rc.getContext(_suspended[0], DoneGetContext())
        
    if _suspended:
        with lock:
            protocol.invokeLater(r4)
            lock.wait(5000)

def testBreakpoints(c):
    from tcf.services import breakpoints
    def r3():
        bps = c.getRemoteService(breakpoints.NAME)
        class DoneGetIDs(breakpoints.DoneGetIDs):
            def doneGetIDs(self, token, error, ids):
                if error:
                    protocol.log("Error from Breakpoints.getIDs", error)
                    return
                print "Breakpoints :", ids
                class DoneGetProperties(breakpoints.DoneGetProperties):
                    def doneGetProperties(self, token, error, props):
                        if error:
                            protocol.log("Error from Breakpoints.getProperties", error)
                            return
                        print "Breakpoint Properties: ", props
                class DoneGetStatus(breakpoints.DoneGetStatus):
                    def doneGetProperties(self, token, error, props):
                        if error:
                            protocol.log("Error from Breakpoints.getStatus", error)
                            return
                        print "Breakpoint Status: ", props
                for id in ids:
                    bps.getProperties(id, DoneGetProperties())
                    bps.getStatus(id, DoneGetStatus())
        bps.getIDs(DoneGetIDs())
    protocol.invokeLater(r3)
    def r4():
        bpsvc = c.getRemoteService(breakpoints.NAME)
        class BPListener(breakpoints.BreakpointsListener):
            def breakpointStatusChanged(self, id, status):
                print "breakpointStatusChanged", id, status
            def contextAdded(self, bps):
                print "breakpointAdded", bps
                bpsvc.removeListener(self)
            def contextChanged(self, bps):
                print "breakpointChanged", bps
            def contextRemoved(self, ids):
                print "breakpointRemoved", ids
        bpsvc.addListener(BPListener())
        class DoneSet(breakpoints.DoneCommand):
            def doneCommand(self, token, error):
                if error:
                    protocol.log("Error from Breakpoints.set", error)
                    return
        bp = {
            breakpoints.PROP_ID : "python:1",
            breakpoints.PROP_ENABLED : True,
            breakpoints.PROP_LOCATION : "sysClkRateGet"
        }
        bpsvc.set([bp], DoneSet())
    protocol.invokeLater(r4)

def testStackTrace(c):
    from tcf.services import stacktrace
    def stackTest(ctx_id):
        stack = c.getRemoteService(stacktrace.NAME)
        class DoneGetChildren(stacktrace.DoneGetChildren):
            def doneGetChildren(self, token, error, ctx_ids):
                if error:
                    protocol.log("Error from StackTrace.getChildren", error)
                    return
                class DoneGetContext(stacktrace.DoneGetContext):
                    def doneGetContext(self, token, error, ctxs):
                        if error:
                            protocol.log("Error from StackTrace.getContext", error)
                            return
                        if ctxs:
                            for ctx in ctxs:
                                print ctx
                stack.getContext(ctx_ids, DoneGetContext())
        stack.getChildren(ctx_id, DoneGetChildren())
    for ctx_id in _suspended:
        protocol.invokeLater(stackTest, ctx_id)

def testSymbols(c):
    from tcf.services import symbols
    def symTest(ctx_id):
        syms = c.getRemoteService(symbols.NAME)
        class DoneList(symbols.DoneList):
            def doneList(self, token, error, ctx_ids):
                if error:
                    protocol.log("Error from Symbols.list", error)
                    return
                class DoneGetContext(symbols.DoneGetContext):
                    def doneGetContext(self, token, error, ctx):
                        if error:
                            protocol.log("Error from Symbols.getContext", error)
                            return
                        print ctx
                if ctx_ids:
                    for ctx_id in ctx_ids:
                        syms.getContext(ctx_id, DoneGetContext())
        syms.list(ctx_id, DoneList())
    for ctx_id in _suspended:
        protocol.invokeLater(symTest, ctx_id)

def testRegisters(c):
    if not _suspended: return
    from tcf.services import registers
    lock = threading.Condition()
    def regTest(ctx_id):
        regs = c.getRemoteService(registers.NAME)
        pending = []
        def onDone():
            with lock: lock.notify()
        class DoneGetChildren(registers.DoneGetChildren):
            def doneGetChildren(self, token, error, ctx_ids):
                pending.remove(token)
                if error:
                    protocol.log("Error from Registers.getChildren", error)
                if not pending:
                    onDone()
                class DoneGetContext(registers.DoneGetContext):
                    def doneGetContext(self, token, error, ctx):
                        pending.remove(token)
                        if error:
                            protocol.log("Error from Registers.getContext", error)
                        else:
                            print ctx
                            if ctx.isReadable() and not ctx.isReadOnce() and ctx.getSize() >= 2:
                                locs = []
                                locs.append(registers.Location(ctx.getID(), 0, 1))
                                locs.append(registers.Location(ctx.getID(), 1, 1))
                                class DoneGetM(registers.DoneGet):
                                    def doneGet(self, token, error, value):
                                        pending.remove(token)
                                        if error:
                                            protocol.log("Error from Registers.getm", error)
                                        else:
                                            print "getm", ctx.getID(), map(ord, value)
                                        if not pending:
                                            onDone()
                                pending.append(regs.getm(locs, DoneGetM()))
                            if ctx.isWriteable() and not ctx.isWriteOnce() and ctx.getSize() >= 2:
                                locs = []
                                locs.append(registers.Location(ctx.getID(), 0, 1))
                                locs.append(registers.Location(ctx.getID(), 1, 1))
                                class DoneSetM(registers.DoneSet):
                                    def doneGet(self, token, error):
                                        pending.remove(token)
                                        if error:
                                            protocol.log("Error from Registers.setm", error)
                                        if not pending:
                                            onDone()
                                pending.append(regs.setm(locs, (255, 255), DoneSetM()))
                        if not pending:
                            onDone()
                if ctx_ids:
                    for ctx_id in ctx_ids:
                        pending.append(regs.getContext(ctx_id, DoneGetContext()))
        pending.append(regs.getChildren(ctx_id, DoneGetChildren()))
    with lock:
        for ctx_id in _suspended:
            protocol.invokeLater(regTest, ctx_id)
        lock.wait(5000)

def testExpressions(c):
    if not _suspended: return
    from tcf.services import expressions
    ctl = sync.CommandControl(c)
    exprs = ctl.Expressions
    e = exprs.create(_suspended[0], None, "1+2*(3-4/2)").getE()
    id = e.get(expressions.PROP_ID)
    val, cls = exprs.evaluate(id).getE()
    print e.get(expressions.PROP_EXPRESSION), "=", val
    exprs.dispose(id)

def testLineNumbers(c):
    if not _suspended: return
    from tcf.services import linenumbers
    from tcf.services import stacktrace
    ctl = sync.CommandControl(c)
    stack = ctl.StackTrace
    lineNumbers = ctl.LineNumbers
    for ctx_id in _suspended:
        bt = stack.getChildren(ctx_id).get()
        if bt:
            bt = stack.getContext(bt).get()
            for frame in bt:
                addr = frame.get(stacktrace.PROP_INSTRUCTION_ADDRESS)
                area = lineNumbers.mapToSource(ctx_id, addr, addr+1).get()
                print "Frame %d - CodeArea: %s" % (frame.get(stacktrace.PROP_LEVEL), area)

def testSyncCommands(c):
    # simplified command execution
    ctl = sync.CommandControl(c)
    diag = ctl.Diagnostics
    s = "Hello TCF World"
    r = diag.echo(s).getE()
    assert s == r
    pi = 3.141592654
    r = diag.echoFP(pi).getE()
    assert pi == r
    e = errors.ErrorReport("Test", errors.TCF_ERROR_OTHER)
    r = diag.echoERR(e.getAttributes()).getE()
    assert e.getAttributes() == r
    print "Diagnostic tests:", diag.getTestList().getE()

    for ctx_id in _suspended:
        print "Symbols:", ctl.Symbols.list(ctx_id)
    for ctx_id in _suspended:
        frame_ids = ctl.StackTrace.getChildren(ctx_id).get()
        if frame_ids:
            error, args = ctl.StackTrace.getContext(frame_ids)
            if not error: print "Stack contexts:", args
    def gotBreakpoints(error, bps):
        print "Got breakpoint list:", bps
    ctl.Breakpoints.getIDs(onDone=gotBreakpoints)
    try:
        print ctl.Processes.getChildren(None, False)
    except:
        pass # no Processes service

def testEvents(c):
    from tcf.util import event
    recorder = event.EventRecorder(c)
    recorder.record("RunControl")
    ctl = sync.CommandControl(c)
    rc = ctl.RunControl
    ctx = rc.getChildren(None).get()[0]
    rc.resume(ctx, 0, 1, None).wait()
    print recorder
    rc.suspend(ctx).wait()
    print recorder
    recorder.stop()

def testDataCache(c):
    from tcf.util import cache
    from tcf.services import runcontrol
    class ContextsCache(cache.DataCache):
        def startDataRetrieval(self):
            rc = self._channel.getRemoteService(runcontrol.NAME)
            if not rc:
                self.set(None, exceptions.Exception("No RunControl service"), None)
                return
            cache = self
            pending = []
            contexts = []
            class DoneGetChildren(runcontrol.DoneGetChildren):
                def doneGetChildren(self, token, error, context_ids):
                    pending.remove(token)
                    if error:
                        protocol.log("Error from RunControl.GetChildren", error)
                    else:
                        for c in context_ids:
                            contexts.append(c)
                            pending.append(rc.getChildren(c, self))
                    if len(pending) == 0:
                        cache.set(None, None, contexts)
            pending.append(rc.getChildren(None, DoneGetChildren()))
    contextsCache = ContextsCache(c)
    def done():
        print "ContextsCache is valid:", contextsCache.getData()
    protocol.invokeLater(contextsCache.validate, done)

def testProcesses(c):
    from tcf.services import processes
    def processTest():
        proc = c.getRemoteService(processes.NAME)
        if not proc:
            return
        class DoneGetChildren(processes.DoneGetChildren):
            def doneGetChildren(self, token, error, context_ids):
                if error:
                    protocol.log("Error from Processes.GetChildren", error)
                else:
                    print "Processes:", context_ids
        proc.getChildren(None, DoneGetChildren())
    protocol.invokeLater(processTest)

if __name__ == '__main__':
    test()

Back to the top