2

How do I share Javascript code between files in Windbg preview?

Right now I have several helper methods that I have copied and pasted into different javascript files. I'm not all that experienced with javascript, so my apologies if this is a stupid question.

As an example, let's say I want to use this function in more than one file:

function GetGuid( objectPtr )
{
    return ExecuteCommandToString( "dt nt!_GUID " + objectPtr )
    .FindLineContaining("{").trim().replace("{", "").replace("}","");
}
Derek
  • 7,615
  • 5
  • 33
  • 58

1 Answers1

3

I have a common.js which has a few functions that are normally reusable like host.diagnostics.debugLog()

i first load it using .scriptload

then in other js files I create a var to those functions and use it

see if this helps

contents of common function file

C:\>cat c:\wdscr\common.js
function log(instr) {
    host.diagnostics.debugLog(instr + "\n");
}
function exec (cmdstr){
    return host.namespace.Debugger.Utility.Control.ExecuteCommand(cmdstr);
}

a js file using the function from common.js

C:\>cat c:\wdscr\usecommon.js
function foo(){
    var commonlog = host.namespace.Debugger.State.Scripts.common.Contents.log
    var commonexec = host.namespace.Debugger.State.Scripts.common.Contents.exec
    commonlog("we are using the logging function from the common.js file")

    var blah = commonexec("lma @$exentry")
    for(var a of blah) {
        commonlog(a)
    }
}

actual usage

C:\>cdb calc
Microsoft (R) Windows Debugger Version 10.0.16299.15 X86

0:000> .load jsprovider

0:000> .scriptload c:\wdscr\common.js
JavaScript script successfully loaded from 'c:\wdscr\common.js'

0:000> .scriptload c:\wdscr\usecommon.js
JavaScript script successfully loaded from 'c:\wdscr\usecommon.js'

0:000> dx @$scriptContents.foo()

we are using the logging function from the common.js file 
start    end        module name
00f10000 00fd0000   calc       (deferred)
@$scriptContents.foo()
0:000>
blabb
  • 8,674
  • 1
  • 18
  • 27