Skip to content

Truly independent web browser

Utilities/js: Add a debugger backtrace command

Sam Atkins720f66f50bb0
diff --git a/Libraries/LibJS/Rust/src/utilities/js.rs b/Libraries/LibJS/Rust/src/utilities/js.rs
index d9738d89ab1..93bd673eaf6 100644
--- a/Libraries/LibJS/Rust/src/utilities/js.rs
+++ b/Libraries/LibJS/Rust/src/utilities/js.rs
@@ -759,8 +759,36 @@ fn pause_on_exceptions_name(mode: PauseOnExceptions) -> &'static [u8] {
}
}
+fn print_backtrace(pause_info: &PauseInfo) {
+ for (index, frame) in pause_info.stack_trace.iter().enumerate() {
+ let context = unsafe { frame.execution_context.as_ref() };
+ let executable = context
+ .executable
+ .get()
+ .map(crate::bytecode::executable::Executable::from_head);
+ let function_name = executable
+ .as_ref()
+ .map(|executable| executable.name());
+ let mut line = format!("#{index} ").into_bytes();
+ match function_name {
+ None => line.extend_from_slice(b"<native>"),
+ Some(name) if name.is_empty() => line.extend_from_slice(b"<global>"),
+ Some(name) => line.extend(Utf16View::of_fly_string(&name).to_wtf8()),
+ }
+ if let Some(source_range) = &frame.source_range {
+ line.extend_from_slice(b" at ");
+ line.extend(Utf16View::of_string(source_range.filename()).to_wtf8());
+ line.extend_from_slice(
+ format!(":{}:{}", source_range.start.line, source_range.start.column).as_bytes(),
+ );
+ }
+ standard_output::outln(&line);
+ }
+}
+
fn print_debugger_help() {
standard_output::outln(b"Debugger commands:");
+ standard_output::outln(b" backtrace (bt)");
standard_output::outln(b" break (b) [file:]<line>[:column]");
standard_output::outln(b" breakpoints");
standard_output::outln(b" continue (c)");
@@ -881,6 +909,11 @@ fn run_debugger_prompt(
continue;
}
+ if (command_name == b"backtrace" || command_name == b"bt") && command.is_eof() {
+ print_backtrace(pause_info);
+ continue;
+ }
+
if command_name == b"set" {
let setting = command.consume_command_name();
command.skip_whitespace();
diff --git a/Tests/LibJS/Debugger/expected/basic.txt b/Tests/LibJS/Debugger/expected/basic.txt
index 5e97b539706..e3e8648eb93 100644
--- a/Tests/LibJS/Debugger/expected/basic.txt
+++ b/Tests/LibJS/Debugger/expected/basic.txt
@@ -1,5 +1,6 @@
Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:7:1 (entry)
Debugger commands:
+ backtrace (bt)
break (b) [file:]<line>[:column]
breakpoints
continue (c)
@@ -16,6 +17,9 @@ No breakpoints.
Deleted breakpoint 1.
2: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)
Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:4:5 (breakpoint)
+#0 calculate at <ladybird>/Tests/LibJS/Debugger/input/basic.js:4:5
+#1 <global> at <ladybird>/Tests/LibJS/Debugger/input/basic.js:7:10
+#2 <native>
Deleted breakpoint 2.
Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:10:5 (exception)
pause-on-exceptions: none
diff --git a/Tests/LibJS/Debugger/input/basic.commands b/Tests/LibJS/Debugger/input/basic.commands
index e187fb92d77..876f6752b55 100644
--- a/Tests/LibJS/Debugger/input/basic.commands
+++ b/Tests/LibJS/Debugger/input/basic.commands
@@ -10,6 +10,7 @@ breakpoints
delete 1
b 4
c
+bt
delete 2
continue
set pause-on-exceptions none