aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/simulator/cli-debugger.test.ts
blob: 3c4db48f410b1d4466a087aa84fc65fcd897bc15 (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import { getIsa } from '../isa';
import { CliDebugger } from './cli-debugger';
import { Readable, Writable } from 'stream';

jest.mock('readline');
import * as readline from 'readline';

describe('cli debugger', () => {
    let dbg: CliDebugger;
    let stdinActual: string, stdoutActual: string;
    let stdin: Readable, stdout: Writable;
    let mockInterface: readline.Interface;

    beforeEach(() => {
        stdinActual = stdoutActual = '';
        // @ts-ignore
        readline.createInterface.mockReturnValue(mockInterface);
    });

    beforeAll(() => {
        stdin = new Readable({
            read(n) {
                n = Math.min(n, stdinActual.length);
                if (!n) {
                    return null;
                } else {
                    const res = Buffer.from(stdinActual.slice(0, n));
                    stdinActual = stdinActual.slice(n);
                    return res;
                }
            }
        });
        stdout = new Writable({
            write(data, enc, cb) {
                stdoutActual += data.toString();
                if (cb) cb();
            }
        });

        // @ts-ignore
        mockInterface = {
            close: jest.fn(),
            on: jest.fn(),
            question: jest.fn(),
        };
    });

    afterEach(() => {
        // @ts-ignore
        readline.createInterface.mockReset();
        // @ts-ignore
        mockInterface.close.mockReset();
        // @ts-ignore
        mockInterface.on.mockReset();
        // @ts-ignore
        mockInterface.question.mockReset();
    });

    function runCmd(cmd: string): void {
        // @ts-ignore
        mockInterface.question.mockImplementationOnce((q, cb) => {
            expect(q).toEqual('(novice) ');
            cb(cmd);
        });
    }

    function sendInputChar(c: string): void {
        // @ts-ignore
        mockInterface.question.mockImplementationOnce((q, cb) => {
            expect(q).toEqual('');
            cb(c);
        });
    }

    describe('lc3 debugging', () => {
        beforeEach(() => {
            dbg = new CliDebugger(getIsa('lc3'), stdin, stdout);
        });

        it('quits', () => {
            runCmd('q');

            return dbg.run().then(() => {
                // @ts-ignore
                expect(mockInterface.question.mock.calls.length).toEqual(1);
                // @ts-ignore
                expect(mockInterface.question.mock.calls[0][0]).toEqual('(novice) ');
                expect(stdoutActual).toMatch('==> 0x3000');
            });
        });

        it('displays help', () => {
            runCmd('h');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('novice debugger usage');
                expect(stdoutActual).toMatch(/h\[elp\]\s+show this message/);
                expect(stdoutActual).toMatch(/q\[uit\]\s+escape this foul debugger/);
            });
        });

        it('re-runs last command on empty input', () => {
            runCmd('h');
            runCmd('');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/novice debugger usage[^]+novice debugger usage/);
            });
        });

        it('does not step past halt', () => {
            dbg.store(0x3000, 0xf025);
            runCmd('s');
            runCmd('s');
            runCmd('s');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/==> 0x3000[^]+==> 0x3000[^]+==> 0x3000/);
            });
        });

        it('blows up with wrong number of operands', () => {
            runCmd('s 1');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('command step expects 0 operands but got 1');
            });
        });

        it('catches exceptions thrown by command', () => {
            runCmd('u');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('error: already at the beginning of time');
            });
        });

        it('handles invalid command', () => {
            runCmd('bogus');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('unknown command `bogus\'');
            });
        });

        it('closes readline on close()', () => {
            dbg.close();
            // @ts-ignore
            expect(mockInterface.close.mock.calls).toEqual([[]]);
        });

        it('does not show state again after an error (bogus breakpoint)', () => {
            runCmd('b asdf');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('unknown label `asdf\'');
                // Don't print state again after an error
                expect(stdoutActual).not.toMatch(/==>[^]+==>/);
            });
        });

        it('steps work', () => {
            dbg.store(0x3000, 0x5020); // and r0, r0, 0
            dbg.store(0x3001, 0x1023); // add r0, r0, 3
            dbg.store(0x3002, 0x5220); // and r1, r0, 0
            dbg.store(0x3003, 0x1264); // add r1, r1, 4
            dbg.store(0x3004, 0xf025); // halt

            for (let i = 0x3000; i <= 0x3004; i++) runCmd('s');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/==> 0x3000[^]+==> 0x3001[^]+==> 0x3002[^]+==> 0x3003[^]+==> 0x3004[^]+==> 0x3004/);
                expect(stdoutActual).toMatch(/r0: 0x0000[^]+r0: 0x0000[^]+r0: 0x0003[^]+r0: 0x0003[^]+r0: 0x0003[^]+r0: 0x0003/);
                expect(stdoutActual).toMatch(/r1: 0x0000[^]+r1: 0x0000[^]+r1: 0x0000[^]+r1: 0x0000[^]+r1: 0x0004[^]+r1: 0x0004/);
            });
        });

        it('breakpoints, continue work', () => {
            dbg.store(0x3000, 0x5020); // and r0, r0, 0
            dbg.store(0x3001, 0x1023); // add r0, r0, 3
            dbg.store(0x3002, 0x5220); // and r1, r0, 0
            dbg.store(0x3003, 0x1264); // add r1, r1, 4
            dbg.store(0x3004, 0xf025); // halt

            runCmd('b 0x3002');
            runCmd('c');
            runCmd('c');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('breakpoint set at 0x3002');
                expect(stdoutActual).toMatch(/==> 0x3000[^]+==> 0x3002[^]+==> 0x3004/);
                expect(stdoutActual).toMatch(/r0: 0x0000[^]+r0: 0x0003[^]+r0: 0x0003/);
                expect(stdoutActual).toMatch(/r1: 0x0000[^]+r1: 0x0000[^]+r1: 0x0004/);
            });
        });

        it('breakpoints with labels, continue work', () => {
            dbg.store(0x3000, 0x5020); // and r0, r0, 0
            dbg.store(0x3001, 0x1023); // add r0, r0, 3
            dbg.store(0x3002, 0x5220); // and r1, r0, 0
            dbg.store(0x3003, 0x1264); // add r1, r1, 4
            dbg.store(0x3004, 0xf025); // halt

            dbg.setSymbols({
                tim_brown: 0x3002,
            });

            runCmd('b tim_brown');
            runCmd('c');
            runCmd('c');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('breakpoint set at 0x3002');
                expect(stdoutActual).toMatch(/==> 0x3000[^]+==> 0x3002[^]+==> 0x3004/);
                expect(stdoutActual).toMatch(/r0: 0x0000[^]+r0: 0x0003[^]+r0: 0x0003/);
                expect(stdoutActual).toMatch(/r1: 0x0000[^]+r1: 0x0000[^]+r1: 0x0004/);
            });
        });

        it('does not double unstep for halts', () => {
            dbg.store(0x3000, 0x0000); // nop
            dbg.store(0x3001, 0xf025); // halt

            runCmd('c');
            runCmd('u');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/==> 0x3000[^]+==> 0x3001[^]+==> 0x3000/);
                expect(stdoutActual).not.toMatch(/==> 0x3001[^]+==> 0x3001/);
            });
        });

        it('puts works', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0068); // message .stringz "hello dad\n"
            dbg.store(0x3004, 0x0065);
            dbg.store(0x3005, 0x006c);
            dbg.store(0x3006, 0x006c);
            dbg.store(0x3007, 0x006f);
            dbg.store(0x3008, 0x0020);
            dbg.store(0x3009, 0x0064);
            dbg.store(0x300a, 0x0061);
            dbg.store(0x300b, 0x0064);
            dbg.store(0x300c, 0x000a);
            dbg.store(0x300d, 0x0000);

            runCmd('c');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('hello dad\n');
            });
        });

        it('puts works but puts ↲ after fake linebreak', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0064); // message .stringz "dad"
            dbg.store(0x3004, 0x0061);
            dbg.store(0x3005, 0x0064);
            dbg.store(0x3006, 0x0000);

            runCmd('c');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/^dad↲$/m);
            });
        });

        it('getc works', () => {
            // outputs stdin to stdout until it hits a character which is not a lowercase
            // letter a-z
            dbg.store(0x3000, 0x56e0); // and r3, r3, 0 ; r3 <- 0
            dbg.store(0x3001, 0xf020); // loop getc ; r0 <- input
            dbg.store(0x3002, 0x2411); // ld r2, ascii_a
            dbg.store(0x3003, 0x94bf); // not r2, r2
            dbg.store(0x3004, 0x14a1); // add r2, r2, 1 ; r2 <- -'a'
            dbg.store(0x3005, 0x1480); // add r2, r2, r0 ; r2 <- c - 'a'
            dbg.store(0x3006, 0x0808); // brn done
            dbg.store(0x3007, 0x240d); // ld r2, ascii_z
            dbg.store(0x3008, 0x94bf); // not r2, r2
            dbg.store(0x3009, 0x14a1); // add r2, r2, 1 ; r2 <- -'z'
            dbg.store(0x300a, 0x1480); // add r2, r2, r0 ; r2 <- c - 'z'
            dbg.store(0x300b, 0x0203); // brp done
            dbg.store(0x300c, 0xf021); // out
            dbg.store(0x300d, 0x1620); // add r3, r0, 0
            dbg.store(0x300e, 0x0ff2); // br loop
            dbg.store(0x300f, 0x16e0); // done add r3, r3, 0
            dbg.store(0x3010, 0x0402); // brz die
            dbg.store(0x3011, 0x2004); // ld r0, ascii_lf
            dbg.store(0x3012, 0xf021); // out
            dbg.store(0x3013, 0xf025); // die halt
            dbg.store(0x3014, 0x0061); // ascii_a .fill 'a'
            dbg.store(0x3015, 0x007a); // ascii_z .fill 'z'
            dbg.store(0x3016, 0x000a); // ascii_lf .fill '\n'

            runCmd('c');
            'ilikebigstringsandicannotlie '.split('').forEach(sendInputChar);
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/^ilikebigstringsandicannotlie$/m);
            });
        });

        it('complains if you type more than 1 char for getc', () => {
            dbg.store(0x3000, 0xf020); // getc
            dbg.store(0x3001, 0xf025); // halt

            runCmd('c');
            sendInputChar('whoopsie');
            sendInputChar('x');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('please type exactly 1 char');
            });
        });

        it('prints memory with range including pc', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0068); // message .stringz "hello dad\n"
            dbg.store(0x3004, 0x0065);
            dbg.store(0x3005, 0x006c);
            dbg.store(0x3006, 0x006c);
            dbg.store(0x3007, 0x006f);
            dbg.store(0x3008, 0x0020);
            dbg.store(0x3009, 0x0064);
            dbg.store(0x300a, 0x0061);
            dbg.store(0x300b, 0x0064);
            dbg.store(0x300c, 0x000a);
            dbg.store(0x300d, 0x0000);

            runCmd('p 0x3000-0x3004');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/0x3000[^]+^==> 0x3000:  0xe002  -8190   lea r0, 2  $/m);
                expect(stdoutActual).toMatch(/0x3001[^]+^    0x3001:  0xf022  -4062   puts       $/m);
                expect(stdoutActual).toMatch(/0x3002[^]+^    0x3002:  0xf025  -4059   halt       $/m);
                expect(stdoutActual).toMatch(/0x3003[^]+^    0x3003:  0x0068  104     'h'        $/m);
                expect(stdoutActual).toMatch(/0x3004[^]+^    0x3004:  0x0065  101     'e'        $/m);
            });
        });

        it('prints memory with range not including pc', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0068); // message .stringz "hello dad\n"
            dbg.store(0x3004, 0x0065);
            dbg.store(0x3005, 0x006c);
            dbg.store(0x3006, 0x006c);
            dbg.store(0x3007, 0x006f);
            dbg.store(0x3008, 0x0020);
            dbg.store(0x3009, 0x0064);
            dbg.store(0x300a, 0x0061);
            dbg.store(0x300b, 0x0064);
            dbg.store(0x300c, 0x000a);
            dbg.store(0x300d, 0x0000);

            runCmd('p 0x3001-0x3004');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/0x3001[^]+^0x3001:  0xf022  -4062   puts  $/m);
                expect(stdoutActual).toMatch(/0x3002[^]+^0x3002:  0xf025  -4059   halt  $/m);
                expect(stdoutActual).toMatch(/0x3003[^]+^0x3003:  0x0068  104     'h'   $/m);
                expect(stdoutActual).toMatch(/0x3004[^]+^0x3004:  0x0065  101     'e'   $/m);
            });
        });

        it('prints memory with decimal range', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0068); // message .stringz "hello dad\n"
            dbg.store(0x3004, 0x0065);
            dbg.store(0x3005, 0x006c);
            dbg.store(0x3006, 0x006c);
            dbg.store(0x3007, 0x006f);
            dbg.store(0x3008, 0x0020);
            dbg.store(0x3009, 0x0064);
            dbg.store(0x300a, 0x0061);
            dbg.store(0x300b, 0x0064);
            dbg.store(0x300c, 0x000a);
            dbg.store(0x300d, 0x0000);

            runCmd('p 12289-0x3002');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/0x3001[^]+^0x3001:  0xf022  -4062   puts  $/m);
                expect(stdoutActual).toMatch(/0x3002[^]+^0x3002:  0xf025  -4059   halt  $/m);
            });
        });

        it('prints single address', () => {
            dbg.store(0x3000, 0xe002); // lea r0, message
            dbg.store(0x3001, 0xf022); // puts
            dbg.store(0x3002, 0xf025); // halt
            dbg.store(0x3003, 0x0068); // message .stringz "hello dad\n"
            dbg.store(0x3004, 0x0065);
            dbg.store(0x3005, 0x006c);
            dbg.store(0x3006, 0x006c);
            dbg.store(0x3007, 0x006f);
            dbg.store(0x3008, 0x0020);
            dbg.store(0x3009, 0x0064);
            dbg.store(0x300a, 0x0061);
            dbg.store(0x300b, 0x0064);
            dbg.store(0x300c, 0x000a);
            dbg.store(0x300d, 0x0000);

            runCmd('p 0x3001');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch(/0x3001[^]+^0x3001:  0xf022  -4062   puts  $/m);
            });
        });

        it('errors when weird operand is passed to print', () => {
            runCmd('p 0x3000-0x3001-0x3002');
            runCmd('q');

            return dbg.run().then(() => {
                expect(stdoutActual).toMatch('error: expected range');
            });
        });
    });

    describe('lc2200 debugging', () => {
        beforeEach(() => {
            dbg = new CliDebugger(getIsa('lc2200'), stdin, stdout);
        });

        it('displays register aliases properly', () => {
            runCmd('q');

            return dbg.run().then(() => {
                // @ts-ignore
                expect(mockInterface.question.mock.calls.length).toEqual(1);
                // @ts-ignore
                expect(mockInterface.question.mock.calls[0][0]).toEqual('(novice) ');
                expect(stdoutActual).toMatch('==> 0x00000000');
                expect(stdoutActual).toMatch(/^[^]*\$zero:[^]+\$at:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$v0:[^]+\$a0:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$a1:[^]+\$a2:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$t0:[^]+\$t1:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$t2:[^]+\$s0:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$s1:[^]+\$s2:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$k0:[^]+\$sp:[^]+$/m);
                expect(stdoutActual).toMatch(/^[^]*\$fp:[^]+\$ra:[^]+$/m);
            });
        });
    });
});