[gjs: 1/2] Add list command to debugger




commit 05d897237c22098bb79728d01bb3ac2a652a1dc0
Author: Nasah-Kuma <mantoh nasah go-groups net>
Date:   Thu Feb 11 22:22:34 2021 +0100

    Add list command to debugger

 installed-tests/debugger/list.debugger        | 12 ++++++
 installed-tests/debugger/list.debugger.js     | 11 ++++++
 installed-tests/debugger/list.debugger.output | 57 +++++++++++++++++++++++++++
 installed-tests/meson.build                   |  1 +
 modules/script/_bootstrap/debugger.js         | 38 ++++++++++++++++++
 5 files changed, 119 insertions(+)
---
diff --git a/installed-tests/debugger/list.debugger b/installed-tests/debugger/list.debugger
new file mode 100644
index 00000000..0a776dbe
--- /dev/null
+++ b/installed-tests/debugger/list.debugger
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+# SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+list
+list 4
+list 11
+list 12
+list 0
+list divide
+break 4
+c
+list
+q
diff --git a/installed-tests/debugger/list.debugger.js b/installed-tests/debugger/list.debugger.js
new file mode 100644
index 00000000..64e58c7f
--- /dev/null
+++ b/installed-tests/debugger/list.debugger.js
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+function divide(a, b) {
+    if (b === 0)
+        return undefined;
+    else if (a === undefined || b === undefined)
+        return undefined;
+    else
+        return a / b;
+}
+divide();
diff --git a/installed-tests/debugger/list.debugger.output b/installed-tests/debugger/list.debugger.output
new file mode 100644
index 00000000..62a6d307
--- /dev/null
+++ b/installed-tests/debugger/list.debugger.output
@@ -0,0 +1,57 @@
+GJS debugger. Type "help" for help
+db> # SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+db> # SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+db> list
+   6       else if (a === undefined || b === undefined)
+   7           return undefined;
+   8       else
+   9           return a / b;
+   10  }
+  *11      divide();
+db> list 4
+   1   // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+   2   // SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+   3   function divide(a, b) {
+  *4           if (b === 0)
+   5           return undefined;
+   6       else if (a === undefined || b === undefined)
+   7           return undefined;
+   8       else
+   9           return a / b;
+db> list 11
+   6       else if (a === undefined || b === undefined)
+   7           return undefined;
+   8       else
+   9           return a / b;
+   10  }
+  *11      divide();
+db> list 12
+   7           return undefined;
+   8       else
+   9           return a / b;
+   10  }
+   11  divide();
+db> list 0
+   1   // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+   2   // SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+   3   function divide(a, b) {
+   4       if (b === 0)
+   5           return undefined;
+db> list divide
+Unknown option
+db> break 4
+Breakpoint 1 at list.debugger.js:4:8
+db> c
+Breakpoint 1, divide() at list.debugger.js:4:8
+db> list
+   1   // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+   2   // SPDX-FileCopyrightText: 2021 Mantoh Nasah Kuma <nasahnash20 gmail com>
+   3   function divide(a, b) {
+  *4           if (b === 0)
+   5           return undefined;
+   6       else if (a === undefined || b === undefined)
+   7           return undefined;
+   8       else
+   9           return a / b;
+db> q
+Program exited with code 0
diff --git a/installed-tests/meson.build b/installed-tests/meson.build
index 11acbe4e..840ac529 100644
--- a/installed-tests/meson.build
+++ b/installed-tests/meson.build
@@ -57,6 +57,7 @@ debugger_tests = [
     'frame',
     'keys',
     'lastvalues',
+    'list',
     'next',
     'print',
     'quit',
diff --git a/modules/script/_bootstrap/debugger.js b/modules/script/_bootstrap/debugger.js
index 0cffe703..f6771f1a 100644
--- a/modules/script/_bootstrap/debugger.js
+++ b/modules/script/_bootstrap/debugger.js
@@ -236,6 +236,43 @@ 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)
+            print(`  *\x1b[1m${i}\t${sourceLines[i - 1]}\x1b[0m`);
+        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 setCommand(rest) {
     var space = rest.indexOf(' ');
     if (space === -1) {
@@ -694,6 +731,7 @@ var commandArray = [
     throwCommand, 't',
     untilCommand, 'u', 'upto',
     upCommand,
+    listCommand, 'li', 'l',
 ];
 // clang-format on
 var currentCmd = null;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]