Skip to content

[lldb][RFC] add JavaScript as a supported scripting language #165807

@cs01

Description

@cs01

#165805 implements support for JavaScript scripting in lldb. Similar to python and lua, it uses swig to generate C++ bindings for the language. V8 is embedded in lldb and used as a JavaScript engine. I am sharing here for discussion to see if there is interest in landing support for JavaScript scripting in lldb. There may be a few gaps in functionality like callback support, but I tried some of the standard scripting bridge functions and they worked.

For build instructions, check out the branch and see the file lldb/docs/use/javascript-reference.md.

To try this out save the following file

/tmp/test.js:

lldb.debugger.SetAsync(false);
let target = lldb.debugger.CreateTarget("/bin/ls");

let breakpoint = target.BreakpointCreateByName("main");
console.log("Breakpoint ID:", breakpoint.GetID());

let error = new lldb.SBError();
let proc = target.Launch(target.GetLaunchInfo(), error);

if (error.Fail()) {
    console.error("Error:", error.GetCString());
} else {
    console.log("PID:", proc.GetProcessID());

    let thread = proc.GetSelectedThread();
    console.log("Thread:", thread.GetThreadID());
    console.log("Stop reason:", thread.GetStopReason());

    console.log("\nStack trace:");
    for (let i = 0; i < Math.min(thread.GetNumFrames(), 4); i++) {
        let frame = thread.GetFrameAtIndex(i);
        let pc = frame.GetPCAddress().GetLoadAddress(target);
        let pcHex = "0x" + (pc >>> 0).toString(16).padStart(8, '0');
        console.log("Frame #" + i + ": " + pcHex);
    }

    console.log("Registers:");
    let frame0 = thread.GetFrameAtIndex(0);
    let gprs = frame0.GetRegisters().GetValueAtIndex(0);

    for (let i = 0; i < Math.min(gprs.GetNumChildren(), 10); i++) {
        let reg = gprs.GetChildAtIndex(i);
        console.log(reg.GetName() + " = " + reg.GetValue());
    }

    proc.Kill();
}

undefined;

then run

./build/bin/lldb -b -o 'settings set script-lang javascript' -o 'command script import /tmp/test.js'

which prints

bin/lldb -b -o 'settings set script-lang javascript' -o 'command script import /tmp/test_process_launch.js'
(lldb) settings set script-lang javascript
(lldb) command script import /tmp/test_process_launch.js
Breakpoint ID: 1
PID: 2351277
Thread: 2351277
Stop reason: 3

Stack trace:
Frame #0: 0x55558d80
Frame #1: 0xf7c2a610
Frame #2: 0xf7c2a6c0
Frame #3: 0x5555ab35

Registers:
rax = 0x0000555555558d80
rbx = 0x0000000000000000
rcx = 0x0000555555574f78
rdx = 0x00007fffffffcbf8
rdi = 0x0000000000000001
rsi = 0x00007fffffffcbe8
rbp = 0x0000000000000001
rsp = 0x00007fffffffcad8
r8 = 0x00007ffff7dfc330
r9 = 0x00007ffff7fcb060
> 

cc @JDevlieghere as we discussed at the LLVM developers' meeting this week

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImproving things as opposed to bug fixing, e.g. new or missing featurelldb

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions