summaryrefslogtreecommitdiff
path: root/modules/_bootstrap/debugger.js
blob: 6ae0d9c2d5cc5419ad009e18e5df7e5689af0c4e (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
/* global debuggee, quit, loadNative, readline, uneval */
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2011 Mozilla Foundation and contributors

/*
 * This is a simple command-line debugger for GJS programs. It is based on
 * jorendb, which is a toy debugger for shell-js programs included in the
 * SpiderMonkey source.
 *
 * To run it: gjs -d path/to/file.js
 * Execution will stop at debugger statements, and you'll get a prompt before
 * the first frame is executed.
 */

const {print, logError} = loadNative('_print');

// Debugger state.
var focusedFrame = null;
var topFrame = null;
var debuggeeValues = {};
var nextDebuggeeValueIndex = 1;
var lastExc = null;
var options = {pretty: true, colors: true, ignoreCaughtExceptions: true};
var breakpoints = [undefined];  // Breakpoint numbers start at 1

// Cleanup functions to run when we next re-enter the repl.
var replCleanups = [];

// Convert a debuggee value v to a string.
function dvToString(v) {
    if (typeof v === 'undefined')
        return 'undefined';  // uneval(undefined) === '(void 0)', confusing
    if (v === null)
        return 'null';  // typeof null === 'object', so avoid that case
    return typeof v !== 'object' || v === null ? uneval(v) : `[object ${v.class}]`;
}

function summarizeObject(dv) {
    const obj = {};
    for (var name of dv.getOwnPropertyNames()) {
        var v = dv.getOwnPropertyDescriptor(name).value;
        if (v instanceof Debugger.Object)
            v = '(...)';
        obj[name] = v;
    }
    return obj;
}

function debuggeeValueToString(dv, style = {pretty: options.pretty}) {
    // Special sentinel values returned by Debugger.Environment.getVariable()
    if (typeof dv === 'object' && dv !== null) {
        if (dv.missingArguments)
            return ['<missing>', undefined];
        if (dv.optimizedOut)
            return ['<optimized out>', undefined];
        if (dv.uninitialized)
            return ['<uninitialized>', undefined];
        if (!(dv instanceof Debugger.Object))
            return ['<unexpected object>', JSON.stringify(dv, null, 4)];
    }

    const dvrepr = dvToString(dv);
    if (!style.pretty || dv === null || typeof dv !== 'object')
        return [dvrepr, undefined];

    if (['TypeError', 'Error', 'GIRespositoryNamespace', 'GObject_Object'].includes(dv.class)) {
        const errval = debuggeeGlobalWrapper.executeInGlobalWithBindings(
            'v.toString()', {v: dv});
        return [dvrepr, errval['return']];
    }

    if (style.brief)
        return [dvrepr, JSON.stringify(summarizeObject(dv), null, 4)];

    const str = debuggeeGlobalWrapper.executeInGlobalWithBindings(
        'JSON.stringify(v, null, 4)', {v: dv});
    if ('throw' in str) {
        if (style.noerror)
            return [dvrepr, undefined];

        const substyle = {};
        Object.assign(substyle, style);
        substyle.noerror = true;
        return [dvrepr, debuggeeValueToString(str.throw, substyle)];
    }

    return [dvrepr, str['return']];
}

function showDebuggeeValue(dv, style = {pretty: options.pretty}) {
    const i = nextDebuggeeValueIndex++;
    debuggeeValues[`$${i}`] = dv;
    debuggeeValues['$$'] = dv;
    const [brief, full] = debuggeeValueToString(dv, style);
    print(`$${i} = ${brief}`);
    if (full !== undefined)
        print(full);
}

Object.defineProperty(Debugger.Frame.prototype, 'num', {
    configurable: true,
    enumerable: false,
    get() {
        let i = 0;
        let f;
        for (f = topFrame; f && f !== this; f = f.older)
            i++;
        return f === null ? undefined : i;
    },
});

Debugger.Frame.prototype.describeFrame = function () {
    if (this.type === 'call') {
        return `${this.callee.name || '<anonymous>'}(${
            this.arguments.map(dvToString).join(', ')})`;
    } else if (this.type === 'global') {
        return 'toplevel';
    } else {
        return `${this.type} code`;
    }
};

Debugger.Frame.prototype.describePosition = function () {
    if (this.script)
        return this.script.describeOffset(this.offset);
    return null;
};

Debugger.Frame.prototype.describeFull = function () {
    const fr = this.describeFrame();
    const pos = this.describePosition();
    if (pos)
        return `${fr} at ${pos}`;
    return fr;
};

Object.defineProperty(Debugger.Frame.prototype, 'line', {
    configurable: true,
    enumerable: false,
    get() {
        if (this.script)
            return this.script.getOffsetLocation(this.offset).lineNumber;
        else
            return null;
    },
});

Debugger.Script.prototype.describeOffset = function describeOffset(offset) {
    const {lineNumber, columnNumber} = this.getOffsetLocation(offset);
    const url = this.url || '<unknown>';
    return `${url}:${lineNumber}:${columnNumber}`;
};

function showFrame(f, n, option = {btCommand: false, fullOption: false}) {
    if (f === undefined || f === null) {
        f = focusedFrame;
        if (f === null) {
            print('No stack.');
            return;
        }
    }
    if (n === undefined) {
        n = f.num;
        if (n === undefined)
            throw new Error('Internal error: frame not on stack');
    }
    print(`#${n.toString().padEnd(4)} ${f.describeFull()}`);
    if (option.btCommand) {
        if (option.fullOption) {
            const variables = f.environment.names();
            for (let i = 0; i < variables.length; i++) {
                if (variables.length === 0)
                    print('No locals.');

                const value = f.environment.getVariable(variables[i]);
                const [brief] = debuggeeValueToString(value, {brief: false, pretty: false});
                print(`${variables[i]} = ${brief}`);
            }
        }
    } else {
        let lineNumber = f.line;
        print(`   ${lineNumber}\t${f.script.source.text.split('\n')[lineNumber - 1]}`);
    }
}


function saveExcursion(fn) {
    const tf = topFrame, ff = focusedFrame;
    try {
        return fn();
    } finally {
        topFrame = tf;
        focusedFrame = ff;
    }
}

// Accept debugger commands starting with '#' so that scripting the debugger
// can be annotated
function commentCommand(comment) {
    void comment;
}

// Evaluate an expression in the Debugger global - used for debugging the
// debugger
function evalCommand(expr) {
    eval(expr);
}

function quitCommand() {
    dbg.removeAllDebuggees();
    quit(0);
}
quitCommand.summary = 'Quit the debugger';
quitCommand.helpText = `USAGE
    quit`;

function backtraceCommand(option) {
    if (topFrame === null)
        print('No stack.');
    if (option === '') {
        for (let i = 0, f = topFrame; f; i++, f = f.older)
            showFrame(f, i, {btCommand: true, fullOption: false});
    } else if (option === 'full') {
        for (let i = 0, f = topFrame; f; i++, f = f.older)
            showFrame(f, i, {btCommand: true, fullOption: true});
    } else {
        print('Invalid option');
    }
}
backtraceCommand.summary = 'Print backtrace of all stack frames and details of all local variables if the full option is added';
backtraceCommand.helpText = `USAGE
    bt <option>

PARAMETERS
    · option: option name. Allowed options are:
        · full: prints the local variables in a stack frame`;

function listCommand(option) {
    if (focusedFrame === null) {
        print('No frame to list from');
        return;
    }
    let lineNumber = focusedFrame.line;
    if (option === '') {
        printSurroundingLines(lineNumber);
        return;
    }
    let currentLine = Number(option);
    if (Number.isNaN(currentLine) === false)
        printSurroundingLines(currentLine);

    else
        print('Unknown option');
}

function printSurroundingLines(currentLine = 1) {
    let sourceLines = focusedFrame.script.source.text.split('\n');
    let lastLine = sourceLines.length - 1;
    let maxLineLimit = Math.min(lastLine, currentLine + 5);
    let minLineLimit = Math.max(1, currentLine - 5);
    for (let i = minLineLimit; i < maxLineLimit + 1; i++) {
        if (i === currentLine) {
            const code = colorCode('1');
            print(`  *${code[0]}${i}\t${sourceLines[i - 1]}${code[1]}`);
        } else {
            print(`   ${i}\t${sourceLines[i - 1]}`);
        }
    }
}

listCommand.summary = 'Prints five lines of code before and five lines after the current line of code on which the debugger is running';
listCommand.helpText = `USAGE
    list <option>
PARAMETERS
    -option : option name. Allowed options are: line number`;

function colorCode(codeNumber) {
    if (options.colors === true)
        return [`\x1b[${codeNumber}m`, '\x1b[0m'];
    else
        return ['', ''];
}
function setCommand(rest) {
    var space = rest.indexOf(' ');
    if (space === -1) {
        print('Invalid set <option> <value> command');
    } else {
        var name = rest.substr(0, space);
        var value = rest.substr(space + 1);

        var yes = ['1', 'yes', 'true', 'on'];
        var no = ['0', 'no', 'false', 'off'];

        if (yes.includes(value))
            options[name] = true;
        else if (no.includes(value))
            options[name] = false;
        else
            options[name] = value;
    }
}
setCommand.summary = 'Sets the value of the given option';
setCommand.helpText = `USAGE
    set <option> <value>

PARAMETERS
    · option: option name. Allowed options are:
        · pretty: set print mode to pretty or brief. Allowed value true or false
        · colors: set printing with colors to true or false.
        · ignoreCaughtExceptions: do not stop on handled exceptions. Allowed value true or false
    · value: option value`;

function splitPrintOptions(s, style) {
    const m = /^\/(\w+)/.exec(s);
    if (!m)
        return [s, style];
    if (m[1].startsWith('p'))
        style.pretty = true;
    if (m[1].startsWith('b'))
        style.brief = true;
    return [s.substr(m[0].length).trimStart(), style];
}

function doPrint(expr, style) {
    // This is the real deal.
    const cv = saveExcursion(
        () => focusedFrame === null
            ? debuggeeGlobalWrapper.executeInGlobalWithBindings(expr, debuggeeValues)
            : focusedFrame.evalWithBindings(expr, debuggeeValues));
    if (cv === null) {
        print('Debuggee died.');
    } else if ('return' in cv) {
        showDebuggeeValue(cv['return'], style);
    } else {
        print("Exception caught. (To rethrow it, type 'throw'.)");
        lastExc = cv.throw;
        showDebuggeeValue(lastExc, style);
    }
}

function printCommand(rest) {
    var [expr, style] = splitPrintOptions(rest, {pretty: options.pretty});
    return doPrint(expr, style);
}
printCommand.summary = 'Prints the given expression';
printCommand.helpText = `USAGE
    print[/pretty|p|brief|b] <expr>

PARAMETER
    · expr: expression to be printed
    · pretty|p: prettify the output
    · brief|b: brief output

expr may also reference the variables $1, $2, ... for already printed
expressions, or $$ for the most recently printed expression.`;

function keysCommand(rest) {
    return doPrint(`Object.keys(${rest})`);
}
keysCommand.summary = 'Prints keys of the given object';
keysCommand.helpText = `USAGE
    keys <obj>

PARAMETER
    · obj: object to get keys of`;

function detachCommand() {
    dbg.removeAllDebuggees();
    return [undefined];
}
detachCommand.summary = 'Detach debugger from the script';
detachCommand.helpText = `USAGE
    detach`;

function continueCommand() {
    if (focusedFrame === null) {
        print('No stack.');
        return;
    }
    return [undefined];
}
continueCommand.summary = 'Continue program execution';
continueCommand.helpText = `USAGE
    cont`;

function throwOrReturn(rest, action, defaultCompletion) {
    if (focusedFrame !== topFrame) {
        print("To throw, you must select the newest frame (use 'frame 0').");
        return;
    }
    if (focusedFrame === null) {
        print('No stack.');
        return;
    }
    if (rest === '')
        return [defaultCompletion];

    const cv = saveExcursion(() => focusedFrame.eval(rest));
    if (cv === null) {
        print(`Debuggee died while determining what to ${action}. Stopped.`);
        return;
    }
    if ('return' in cv)
        return [{[action]: cv['return']}];
    print(`Exception determining what to ${action}. Stopped.`);
    showDebuggeeValue(cv.throw);
}

function throwCommand(rest) {
    return throwOrReturn(rest, 'throw', {throw: lastExc});
}
throwCommand.summary = 'Throws the given value';
throwCommand.helpText = `USAGE
    throw <expr>

PARAMETER
    · expr: expression to throw`;

function returnCommand(rest) {
    return throwOrReturn(rest, 'return', {return: undefined});
}
returnCommand.summary = 'Return the given value from the current frame';
returnCommand.helpText = `USAGE
    return <expr>

PARAMETER
    · expr: expression to return`;

function frameCommand(rest) {
    let n, f;
    if (rest.match(/[0-9]+/)) {
        n = Number(rest);
        f = topFrame;
        if (f === null) {
            print('No stack.');
            return;
        }
        for (let i = 0; i < n && f; i++) {
            if (!f.older) {
                print(`There is no frame ${rest}.`);
                return;
            }
            f.older.younger = f;
            f = f.older;
        }
        focusedFrame = f;
        showFrame(f, n);
    } else if (rest === '') {
        if (topFrame === null)
            print('No stack.');
        else
            showFrame();
    } else {
        print('do what now?');
    }
}
frameCommand.summary = 'Jump to specified frame or print current frame (if not specified)';
frameCommand.helpText = `USAGE
    frame [frame_num]

PARAMETER
    · frame_num: frame to jump to`;

function upCommand() {
    if (focusedFrame === null) {
        print('No stack.');
    } else if (focusedFrame.older === null) {
        print('Initial frame selected; you cannot go up.');
    } else {
        focusedFrame.older.younger = focusedFrame;
        focusedFrame = focusedFrame.older;
        showFrame();
    }
}
upCommand.summary = 'Jump to the parent frame';
upCommand.helpText = `USAGE
    up`;

function downCommand() {
    if (focusedFrame === null) {
        print('No stack.');
    } else if (!focusedFrame.younger) {
        print('Youngest frame selected; you cannot go down.');
    } else {
        focusedFrame = focusedFrame.younger;
        showFrame();
    }
}
downCommand.summary = 'Jump to the younger frame';
downCommand.helpText = `USAGE
    down`;

function printPop(c) {
    if (c['return']) {
        print('Value returned is:');
        showDebuggeeValue(c['return'], {brief: true});
    } else if (c['throw']) {
        print('Frame terminated by exception:');
        showDebuggeeValue(c['throw']);
        print("(To rethrow it, type 'throw'.)");
        lastExc = c['throw'];
    } else {
        print('No value returned.');
    }
}

// Set |prop| on |obj| to |value|, but then restore its current value
// when we next enter the repl.
function setUntilRepl(obj, prop, value) {
    var saved = obj[prop];
    obj[prop] = value;
    replCleanups.push(() => {
        obj[prop] = saved;
    });
}

function doStepOrNext(kind) {
    if (topFrame === null) {
        print('Program not running.');
        return;
    }

    // TODO: step or finish from any frame in the stack, not just the top one
    var startFrame = topFrame;
    var startLine = startFrame.line;
    if (kind.finish)
        print(`Run till exit from ${startFrame.describeFull()}`);
    else
        print(startFrame.describeFull());

    function stepPopped(completion) {
        // Note that we're popping this frame; we need to watch for
        // subsequent step events on its caller.
        this.reportedPop = true;
        printPop(completion);
        topFrame = focusedFrame = this;
        if (kind.finish || kind.until) {
            // We want to continue, but this frame is going to be invalid as
            // soon as this function returns, which will make the replCleanups
            // assert when it tries to access the dead frame's 'onPop'
            // property. So clear it out now while the frame is still valid,
            // and trade it for an 'onStep' callback on the frame we're popping to.
            preReplCleanups();
            setUntilRepl(this.older, 'onStep', stepStepped);
            return undefined;
        }
        return repl();
    }

    function stepEntered(newFrame) {
        print(`entered frame: ${newFrame.describeFull()}`);
        if (!kind.until || newFrame.line === kind.stopLine) {
            topFrame = focusedFrame = newFrame;
            return repl();
        }
        if (kind.until)
            setUntilRepl(newFrame, 'onStep', stepStepped);
    }

    function stepStepped() {
        // print('stepStepped: ' + this.describeFull());
        var stop = false;

        if (kind.finish) {
            // 'finish' set a one-time onStep for stopping at the frame it
            // wants to return to
            stop = true;
        } else if (kind.until) {
            // running until a given line is reached
            if (this.line === kind.stopLine)
                stop = true;
        } else if (this.line !== startLine || this !== startFrame) {
            // regular step; stop whenever the line number changes
            stop = true;
        }

        if (stop) {
            topFrame = focusedFrame = this;
            if (focusedFrame !== startFrame)
                print(focusedFrame.describeFull());
            return repl();
        }

        // Otherwise, let execution continue.
        return undefined;
    }

    if (kind.step || kind.until)
        setUntilRepl(dbg, 'onEnterFrame', stepEntered);

    // If we're stepping after an onPop, watch for steps and pops in the
    // next-older frame; this one is done.
    var stepFrame = startFrame.reportedPop ? startFrame.older : startFrame;
    if (!stepFrame || !stepFrame.script)
        stepFrame = null;
    if (stepFrame) {
        if (!kind.finish)
            setUntilRepl(stepFrame, 'onStep', stepStepped);
        setUntilRepl(stepFrame, 'onPop', stepPopped);
    }

    // Let the program continue!
    return [undefined];
}

function stepCommand() {
    return doStepOrNext({step: true});
}
stepCommand.summary = 'Step to next command';
stepCommand.helpText = `USAGE
    step`;

function nextCommand() {
    return doStepOrNext({next: true});
}
nextCommand.summary = 'Jump to next line';
nextCommand.helpText = `USAGE
    next`;

function finishCommand() {
    return doStepOrNext({finish: true});
}
finishCommand.summary = 'Run until the current frame is finished also prints the returned value';
finishCommand.helpText = `USAGE
    finish`;

function untilCommand(line) {
    return doStepOrNext({until: true, stopLine: Number(line)});
}
untilCommand.summary = 'Continue until given line';
untilCommand.helpText = `USAGE
    until <line_num>

PARAMETER
    · line_num: line_num to continue until`;

function findBreakpointOffsets(line, currentScript) {
    const offsets = currentScript.getLineOffsets(line);
    if (offsets.length !== 0)
        return [{script: currentScript, offsets}];

    const scripts = dbg.findScripts({line, url: currentScript.url});
    if (scripts.length === 0)
        return [];

    return scripts
        .map(script => ({script, offsets: script.getLineOffsets(line)}))
        .filter(({offsets: o}) => o.length !== 0);
}

class BreakpointHandler {
    constructor(num, script, offset) {
        this.num = num;
        this.script = script;
        this.offset = offset;
    }

    hit(frame) {
        return saveExcursion(() => {
            topFrame = focusedFrame = frame;
            print(`Breakpoint ${this.num}, ${frame.describeFull()}`);
            return repl();
        });
    }

    toString() {
        return `Breakpoint ${this.num} at ${this.script.describeOffset(this.offset)}`;
    }
}

function breakpointCommand(where) {
    // Only handles line numbers of the current file
    // TODO: make it handle function names and other files
    const line = Number(where);
    const possibleOffsets = findBreakpointOffsets(line, focusedFrame.script);

    if (possibleOffsets.length === 0) {
        print(`Unable to break at line ${where}`);
        return;
    }

    possibleOffsets.forEach(({script, offsets}) => {
        offsets.forEach(offset => {
            const bp = new BreakpointHandler(breakpoints.length, script, offset);
            script.setBreakpoint(offset, bp);
            breakpoints.push(bp);
            print(bp);
        });
    });
}
breakpointCommand.summary = 'Set breakpoint at the specified location.';
breakpointCommand.helpText = `USAGE
    break <line_num>

PARAMETERS
    · line_num: line number to place a breakpoint at.`;

function deleteCommand(breaknum) {
    const bp = breakpoints[breaknum];

    if (bp === undefined) {
        print(`Breakpoint ${breaknum} already deleted.`);
        return;
    }

    const {script, offset} = bp;
    script.clearBreakpoint(bp, offset);
    breakpoints[breaknum] = undefined;
    print(`${bp} deleted`);
}
deleteCommand.summary = 'Deletes breakpoint';
deleteCommand.helpText = `USAGE
    del <breakpoint_num>

PARAMETERS
    · breakpoint_num: breakpoint number to be removed.`;

// Build the table of commands.
var commands = {};
// clang-format off
var commandArray = [
    backtraceCommand, 'bt', 'where',
    breakpointCommand, 'b', 'break',
    commentCommand, '#',
    continueCommand, 'c', 'cont',
    deleteCommand, 'd', 'del',
    detachCommand,
    downCommand, 'dn',
    evalCommand, '!',
    finishCommand, 'fin',
    frameCommand, 'f',
    helpCommand, 'h',
    keysCommand, 'k',
    nextCommand, 'n',
    printCommand, 'p',
    quitCommand, 'q',
    returnCommand, 'ret',
    setCommand,
    stepCommand, 's',
    throwCommand, 't',
    untilCommand, 'u', 'upto',
    upCommand,
    listCommand, 'li', 'l',
];
// clang-format on
var currentCmd = null;
for (var i = 0; i < commandArray.length; i++) {
    let cmd = commandArray[i];
    if (typeof cmd === 'string')
        commands[cmd] = currentCmd;
    else
        currentCmd = commands[cmd.name.replace(/Command$/, '')] = cmd;
}

function _printCommandsList() {
    print('Available commands:');

    function printcmd(cmd) {
        print(`  ${cmd.aliases.join(', ')} -- ${cmd.summary}`);
    }

    var cmdGroups = _groupCommands();

    for (var group of cmdGroups)
        printcmd(group);
}

function _groupCommands() {
    var groups = [];

    for (var cmd of commandArray) {
        // Don't print commands for debugging the debugger
        if ([commentCommand, evalCommand].includes(cmd) ||
            ['#', '!'].includes(cmd))
            continue;

        if (typeof cmd === 'string') {
            groups[groups.length - 1]['aliases'].push(cmd);
        } else {
            groups.push({
                summary: cmd.summary,
                helpText: cmd.helpText,
                aliases: [cmd.name.replace(/Command$/, '')],
            });
        }
    }
    return groups;
}

function _printCommand(cmd) {
    print(`${cmd.summary}\n\n${cmd.helpText}`);

    if (cmd.aliases.length > 1) {
        print('\nALIASES');
        for (var alias of cmd.aliases)
            print(`    · ${alias}`);
    }
}

function helpCommand(cmd) {
    if (!cmd) {
        _printCommandsList();
    } else {
        var cmdGroups = _groupCommands();
        var command = cmdGroups.find(c => c.aliases.includes(cmd));

        if (command && command.helpText)
            _printCommand(command);
        else
            print(`No help found for ${cmd} command`);
    }
}
helpCommand.summary = 'Show help for the specified command else list all commands';
helpCommand.helpText = `USAGE
    help [command]

PARAMETERS
    · command: command to show help for`;

// Break cmd into two parts: its first word and everything else. If it begins
// with punctuation, treat that as a separate word. The first word is
// terminated with whitespace or the '/' character. So:
//
//   print x         => ['print', 'x']
//   print           => ['print', '']
//   !print x        => ['!', 'print x']
//   ?!wtf!?         => ['?', '!wtf!?']
//   print/b x       => ['print', '/b x']
//
function breakcmd(cmd) {
    cmd = cmd.trimStart();
    if ("!@#$%^&*_+=/?.,<>:;'\"".includes(cmd.substr(0, 1)))
        return [cmd.substr(0, 1), cmd.substr(1).trimStart()];
    var m = /\s+|(?=\/)/.exec(cmd);
    if (m === null)
        return [cmd, ''];
    return [cmd.slice(0, m.index), cmd.slice(m.index + m[0].length)];
}

function runcmd(cmd) {
    var pieces = breakcmd(cmd);
    if (pieces[0] === '')
        return undefined;

    var first = pieces[0], rest = pieces[1];
    if (!commands.hasOwnProperty(first)) {
        print(`unrecognized command '${first}'`);
        return undefined;
    }

    cmd = commands[first];
    if (cmd.length === 0 && rest !== '') {
        print('this command cannot take an argument');
        return undefined;
    }

    return cmd(rest);
}

function preReplCleanups() {
    while (replCleanups.length > 0)
        replCleanups.pop()();
}

var prevcmd;
function repl() {
    preReplCleanups();

    var cmd;
    for (;;) {
        cmd = readline();
        if (cmd === null /* eof */) {
            quitCommand();
            return;
        } else if (cmd === '') {
            cmd = prevcmd;
        }

        try {
            prevcmd = cmd;
            var result = runcmd(cmd);
            if (result === undefined) {
                // do nothing, return to prompt
            } else if (Array.isArray(result)) {
                return result[0];
            } else if (result === null) {
                return null;
            } else {
                throw new Error(
                    `Internal error: result of runcmd wasn't array or undefined: ${result}`);
            }
        } catch (exc) {
            logError(exc, '*** Internal error: exception in the debugger code');
        }
    }
}

function onInitialEnterFrame(frame) {
    print('GJS debugger. Type "help" for help');
    topFrame = focusedFrame = frame;
    return repl();
}

var dbg = new Debugger();
dbg.onNewPromise = function ({promiseID, promiseAllocationSite}) {
    const site = promiseAllocationSite.toString().split('\n')[0];
    print(`Promise ${promiseID} started from ${site}`);
    return undefined;
};
dbg.onPromiseSettled = function (promise) {
    let message = `Promise ${promise.promiseID} ${promise.promiseState} `;
    message += `after ${promise.promiseTimeToResolution.toFixed(3)} ms`;
    let brief, full;
    if (promise.promiseState === 'fulfilled' && typeof promise.promiseValue !== 'undefined') {
        [brief, full] = debuggeeValueToString(promise.promiseValue);
        message += ` with ${brief}`;
    } else if (promise.promiseState === 'rejected' &&
               typeof promise.promiseReason !== 'undefined') {
        [brief, full] = debuggeeValueToString(promise.promiseReason);
        message += ` with ${brief}`;
    }
    print(message);
    if (full !== undefined)
        print(full);
    return undefined;
};
dbg.onDebuggerStatement = function (frame) {
    return saveExcursion(() => {
        topFrame = focusedFrame = frame;
        print(`Debugger statement, ${frame.describeFull()}`);
        return repl();
    });
};
dbg.onExceptionUnwind = function (frame, value) {
    const willBeCaught = currentFrame => {
        while (currentFrame) {
            if (currentFrame.script.isInCatchScope(currentFrame.offset))
                return true;
            currentFrame = currentFrame.older;
        }
        return false;
    };

    if (options.ignoreCaughtExceptions && willBeCaught(frame))
        return undefined;

    return saveExcursion(() => {
        topFrame = focusedFrame = frame;
        print("Unwinding due to exception. (Type 'c' to continue unwinding.)");
        showFrame();
        print('Exception value is:');
        showDebuggeeValue(value);
        return repl();
    });
};

var debuggeeGlobalWrapper = dbg.addDebuggee(debuggee);

setUntilRepl(dbg, 'onEnterFrame', onInitialEnterFrame);