探索组件

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 替换为目标组件的名称网址实例 ID。该命令还接受对这些标识符进行的唯一部分匹配。

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

[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.FeedbackArchiveAccessor
fuchsia.diagnostics.LegacyMetricsArchiveAccessor
fuchsia.diagnostics.LoWPANArchiveAccessor
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.FeedbackArchiveAccessor
fuchsia.diagnostics.LegacyMetricsArchiveAccessor
fuchsia.diagnostics.LoWPANArchiveAccessor
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 命令提供工具软件包的网址,例如:

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

commands for net-cli

Options:
  --help            display usage information

Commands:
  filter            commands for packet filter
  if                commands for network interfaces
  log               commands for logging
  neigh             commands for neighbor tables
  route             commands for routing tables
  dhcp              commands for an interfaces dhcp client
  dhcpd             commands to control a dhcp server
  dns               commands to control the dns resolver

以非交互方式在组件的 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 问题跟踪器组件下提交所有功能请求。