剧集

碎片整理工具 #170 - 调试器 - JavaScript 脚本

在这集碎片整理工具中,安德鲁·理查兹Windows 调试工具团队与 Andy LuhrsBill Messmer 交谈。 我们将介绍 WDK 和 SDK 内部版本 14951 及更新版本中提供的 WinDbg 中的新的 JavaScript 扩展性和脚本功能。

Bill 利用了以前在这些情节中的调试器对象模型:

时间线:

[00:00] 欢迎和简介
[00:24] 新 SDK 删除
[00:29] 为什么 JavaScript
[02:07] 新建命令
[03:50] Visual Studio Code
[04:00] 示例 - Hello World
[07:15] 调试器默认命名空间
[09:07] 示例 - 打印所有线程
[10:26] 示例 - 条件断点
[18:13] 'g' vs. 'gc' - 安德鲁是对的! “gc”以与启动相同的方式恢复执行。 因此,如果命中“p”,然后命中包含“gc”的条件断点,则“gc”将完成初始“p”。
[20:40] 示例 - 唯一堆栈
[34:40] 示例 - 加法

问题/评论? 请发送电子邮件到 defragtools@microsoft.com 与我们联系

JavaScript MSDN 文档:

唯一堆栈示例(右图):

“使用严格”;

class __stackEntry { constructor(frameString) { this.threads = []; this.frameString = frameString; this.children = new Map(; }

display(indent) 
{
    for (var child of this.children.values())
    {
        host.diagnostics.debugLog(indent, child.frameString, " [Threads In Branch: ");
        for (var thread of child.threads)
        {
            host.diagnostics.debugLog(thread.Id, " ");
        }
        host.diagnostics.debugLog("]\n");
        child.display(indent + "    ");
    }
}

}

class __stackMap { constructor(process) { this.__process = process; this.__root = new __stackEntry(“”);this.build();}

build()
{
    for (var thread of this.__process.Threads)
    {
        var current = this.__root;
        var frameNum = 0;

        var frameCount = thread.Stack.Frames.Count();
        for (var frameNum = frameCount - 1; frameNum >= 0; --frameNum) {
            var frame = thread.Stack.Frames[frameNum];
            var frameString = frame.toString();
            if (current.children.has(frameString)) {
                current = current.children.get(frameString);
                current.threads.push(thread);
            }
            else {
                var newEntry = new __stackEntry(frameString);
                current.children.set(frameString, newEntry);
                current = newEntry;
                current.threads.push(thread);
            }
        }
    }
}

findEntry(thread)
{
    var current = this.__root;
    var frameCount = thread.Stack.Frames.Count();
    for (var frameNum = frameCount - 1; frameNum >= 0; --frameNum)
    {
        var frame = thread.Stack.Frames[frameNum];
        var frameString = frame.toString();
        if (!current.children.has(frameString))
        {
            return null;
        }
        current = current.children.get(frameString);
    }
    return current;
}

display()
{
    this.__root.display("");
}

}

class __threadSameStacks { constructor(thread) { this.__thread = thread; }

getDimensionality()
{
    return 1;
}

getValueAt(idx)
{
    for (var idxVal of this)
    {
        var tid = idxVal[Symbol.indicies][0];
        if (idxVal[Symbol.indicies][0] == idx && tid != this.__thread.Id)
        {
            return idxVal.value;
        }
    }
    return undefined;
}

*[Symbol.iterator]()
{
    var context = this.__thread.hostContext;
    var session = host.namespace.Debugger.Sessions.getValueAt(context);
    var process = session.Processes.getValueAt(context);
    var map = new __stackMap(process);
    var entry = map.findEntry(this.__thread);
    if (entry != null)
    {
        for (var sharingThread of entry.threads)
        {
            if (sharingThread.Id != this.__thread.Id)
            {
                yield new host.indexedValue(sharingThread, [sharingThread.Id]);
            }
        }
    }
}

}

class __threadExtension { get IdenticalStacks() { return new __threadSameStacks(this); } } }

function invokeScript() { var map = new __stackMap(host.currentProcess); map.display(); }

function initializeScript() { return [new host.namedModelParent(__threadExtension, “Debugger.Models.Thread”)]; }