跳转到主内容

Electron Debugging

Electron 中有许多不同的方法来调试问题和错误,其中许多方式是根据平台来选择的。

下文概述了一些比较常见的做法。

通用调试

Chromium 含有日志宏,可以在 C++ 和 Objective-C++ 中打印信息来帮助调试操作。

您可以用它来打印变量值、函数名称和行号等信息。

一些示例:

LOG(INFO) << "bitmap.width(): " << bitmap.width();

LOG(INFO, bitmap.width() > 10) << "bitmap.width() 大于 10!";

还有不同级别的日志严重级别: INFOWARNERROR

查看 logging.h 在 Chromium 的源代码树中获取更多信息和示例。

打印堆栈信息

Chromium含有一个能够将堆栈痕迹打印到控制台而不中断程序的助手。

#include "base/debug/stack_trace.h"
...
base::debug::StackTrace().Print();

这将使你能够观察调用链并确定潜在的问题范围。

断点调试

请注意这将大大增加构建的大小,占用大约50G磁盘空间

将以下文件写入到 electron/.git/info/exclude/debug.gn

import("//electron/build/args/testing.gn")
is_debug = true
symbol_level = 2
forbid_non_component_debug_builds = false

然后执行:

$ gn gen out/Debug --args="import(\"//electron/.git/info/exclude/debug.gn\") $GN_EXTRA_ARGS"
$ ninja -C out/Debug electron

现在你可以使用 LLDB 进行断点调试了.

指定平台调试

使用 Symbol Server 调试

调试 symbols 让你能更好的调试 sessions. They have information about the functions contained in executables and dynamic libraries and provide you with information to get clean call stacks. A Symbol Server allows the debugger to load the correct symbols, binaries and sources automatically without forcing users to download large debugging files.

关于如何为 Electron 设置 symbol server 的更多信息,详见 使用 Symbol Server 调试