Skip to main content

Electron 调试

¥Electron Debugging

Electron 中有许多不同的方法来调试问题和错误,其中许多方法是特定于平台的。

¥There are many different approaches to debugging issues and bugs in Electron, many of which are platform specific.

下面概述了一些更常见的方法。

¥Some of the more common approaches are outlined below.

通用调试

¥Generic Debugging

Chromium 包含日志记录宏,可以通过在 C++ 和 Objective-C++ 中将信息打印到控制台来帮助调试。

¥Chromium contains logging macros which can aid debugging by printing information to console in C++ and Objective-C++.

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

¥You might use this to print out variable values, function names, and line numbers, amongst other things.

一些例子:

¥Some examples:

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

LOG(INFO, bitmap.width() > 10) << "bitmap.width() is greater than 10!";

日志记录严重性也有不同级别:INFOWARNERROR

¥There are also different levels of logging severity: INFO, WARN, and ERROR.

有关更多信息和示例,请参阅 Chromium 源代码树中的 logging.h

¥See logging.h in Chromium's source tree for more information and examples.

打印堆栈跟踪

¥Printing Stacktraces

Chromium 包含一个辅助程序,可以在不中断程序的情况下将堆栈跟踪打印到控制台。

¥Chromium contains a helper to print stack traces to console without interrupting the program.

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

这将使你能够观察调用链并识别潜在的问题区域。

¥This will allow you to observe call chains and identify potential issue areas.

断点调试

¥Breakpoint Debugging

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

¥Note that this will increase the size of the build significantly, taking up around 50G of disk space

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

¥Write the following file to electron/.git/info/exclude/debug.gn

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

然后执行:

¥Then execute:

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

现在就可以使用 LLDB 进行断点调试了。

¥Now you can use LLDB for breakpoint debugging.

特定于平台的调试

¥Platform-Specific Debugging

使用符号服务器进行调试

¥Debugging with the Symbol Server

调试符号可以让你获得更好的调试会话。它们具有有关可执行文件和动态库中包含的函数的信息,并为你提供获取干净调用堆栈的信息。符号服务器允许调试器自动加载正确的符号、二进制文件和源代码,而无需强迫用户下载大型调试文件。

¥Debug symbols allow you to have better debugging 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 设置符号服务器的更多信息,请参阅 使用符号服务器进行调试

¥For more information about how to set up a symbol server for Electron, see debugging with a symbol server.