探索组件

ffx component explore 命令会启动一个交互式 shell,让您可以探索在目标设备上运行的 Fuchsia 组件的内部结构。

概念

ffx component explore 命令会启动一个以目标组件为范围的 Dash 进程。通过此 Dash 流程,您可以:

  • 使用熟悉的 POSIX 命令,例如 lscatgrep
  • 探索组件的传入和传出功能。

Dash 是之前在 Fuchsia 项目的其他工具(例如 fx shell、串行控制台、终端窗口和 virtcon)中使用的命令解释器。Dash 为开发者提供了熟悉的 ffx component explore 体验,例如 cdls,用于在 Fuchsia 组件中导航空间,例如:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$ ls
exposed
ns
out
runtime
svc
$

不过,与 fx shell 不同的是,该命名空间仅包含组件的传入和传出功能。此限制意味着您可以在与组件所见几乎相同的环境中进行探索。

探索组件

如需连接到 Fuchsia 组件并启动交互式 shell,请运行以下命令:

ffx component explore COMPONENT_IDENTIFIER

COMPONENT_IDENTIFIER 替换为目标组件的 moniker网址实例 ID。该命令还接受对这些标识符的唯一部分匹配。

以下示例命令会启动一个交互式 shell ($),用于探索 /bootstrap/archivst 组件:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$

在本指南的所有示例中,宿主机终端的 shell 提示符表示为 [host]$,而组件的交互式 shell 的提示符表示为 $,不带 [host] 前缀。换句话说,[host]$ 表示命令在主机终端上运行,而 $ 表示命令在连接到目标组件的交互式 shell 上运行。

本指南还在大多数示例中使用 /bootstrap/archivist(这是一个别名)作为目标组件。在实践中,此实参应替换为目标组件的组件标识符。

探索组件可用的功能

如需探索目标组件的功能,请在组件的交互式 shell 中前往 /ns 目录。/ns 目录包含组件的命名空间,与组件看到的命名空间完全相同,例如:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$ cd ns
$ ls
config
events
pkg
svc

如果您希望 shell 的命名空间与组件命名空间一致,请使用 -l(或 --layout)标志,例如:

[host]$ ffx component explore /bootstrap/archivist -l namespace
Moniker: /bootstrap/archivist
$ ls
config
events
pkg
svc

如需详细了解这些目录,请参阅 ffx 组件探索中的命名空间根是什么?

探索组件公开的功能

/exposed 目录包含从目标组件公开给其父组件的功能,例如:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$ cd exposed
$ ls
diagnostics
fuchsia.diagnostics.ArchiveAccessor
fuchsia.diagnostics.ArchiveAccessor.feedback
fuchsia.diagnostics.ArchiveAccessor.legacy_metrics
fuchsia.diagnostics.ArchiveAccessor.lowpan
fuchsia.diagnostics.LogSettings
fuchsia.logger.Log
fuchsia.logger.LogSink

探索组件提供的功能

如果目标组件在设备上运行,则 /out 目录包含该组件当前提供的所有功能,例如:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$ cd out
$ ls
diagnostics
svc
$ cd svc
$ ls
fuchsia.diagnostics.ArchiveAccessor
fuchsia.diagnostics.ArchiveAccessor.feedback
fuchsia.diagnostics.ArchiveAccessor.legacy_metrics
fuchsia.diagnostics.ArchiveAccessor.lowpan
fuchsia.diagnostics.LogSettings
fuchsia.logger.Log

探索组件的调试运行时数据

如果目标组件在设备上运行,则 /runtime 目录包含组件运行程序提供的调试信息,例如:

[host]$ ffx component explore /bootstrap/archivist
Moniker: /bootstrap/archivist
$ cd runtime/elf
$ ls
job_id
process_id
process_start_time
$ cat process_id
2542

在组件的 shell 中使用自定义命令行工具

您可以通过以下方式将自定义命令行工具添加到组件的 shell 环境中:

为当前会话添加工具

如需临时添加工具,请使用 --tools 标志将工具的软件包网址传递给 ffx component explore 命令。

例如,以下命令会将 net-cli 软件包添加到 /core/network 组件的 shell 中,以用于当前会话:

[host]$ ffx component explore /core/network --tools fuchsia-pkg://fuchsia.com/net-cli
Moniker: /core/network
$ net help
Usage: net <command> [<args>]
...

使工具永久可用

作为组件所有者,您可以让用户始终可以使用特定工具。为此,请将工具软件包网址添加到组件清单中的 fuchsia.dash.launcher-tool-urls facet:

<rest of component manifest above>
facets: {
    "fuchsia.dash.launcher-tool-urls": [ "fuchsia-pkg://fuchsia.com/magma-debug-utils" ],
},

当用户在此组件上运行 ffx component explore 时,该命令会自动解析并从清单中加载工具。用户必须运行可提供指定工具的软件包服务器,否则会看到警告。

[host]$ ffx component explore <moniker>
Moniker: <moniker>
Using tool URLs from component manifest: ["fuchsia-pkg://fuchsia.com/magma-debug-utils"]
$

以非交互方式在组件的 shell 上运行命令

如需在组件的设备端 shell 中以非交互方式运行命令并接收 stdoutstderr 和退出代码,请使用 -c(或 --command)标志,例如:

[host]$ ffx component explore /bootstrap/archivist -c "cat /runtime/elf/process_id"
Moniker: /bootstrap/archivist
2542

附录

为什么我无法从父组件中看到子组件?

Fuchsia 不允许从父组件直接访问子组件。以前,使用组件拓扑知识来访问子组件的功能会使工具变得脆弱,并且依赖于对系统拓扑知识进行编码的硬编码路径。

建议改用以下替代方案:

  • 明确地将功能从子组件路由到父组件。
  • 探索子组件本身。

这与 ffx 组件运行有何不同?

ffx component run 命令用于在组件拓扑中指定集合内创建并启动组件。不过,ffx component run 不提供任何互动功能。另一方面,ffx component explore 允许以交互方式探索拓扑中的任何现有组件。总而言之,您可以使用 ffx component explore 了解刚刚使用 ffx component run 创建的组件。

ffx 组件探索中的命名空间根是什么?

默认情况下,ffx component explore 命令会在命名空间根目录 (/) 中创建一个包含以下目录的虚拟文件系统:

目录 说明
/.dash 包含 Dash 所需的二进制文件。
/exposed 包含所有公开的功能。
/ns 包含组件的命名空间,与组件看到的完全一致。
/svc 包含 Dash 所需的功能。

如果目标组件在设备上运行,则还存在以下目录:

目录 说明
/out 包含组件当前提供的所有功能。
/runtime 包含组件运行程序提供的调试信息。

如果在 ffx component explore 中设置了 --layout namespace 标志,shell 的命名空间将与组件的命名空间匹配。

我可以使用 Dash shell 访问 Zircon 句柄或进行 FIDL 调用吗?

命令解释器不支持直接执行此操作。

如何针对 FFX 组件探索功能提交功能请求?

请在 ComponentFramework > Tools 问题跟踪器组件下提交所有功能请求。