> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Tracing

本页面介绍 Lynx DevTool 支持的 `Tracing` CDP domain。



:::warning 实验性参考
此生成的参考文档仍在完善中。

- CDP events 尚未包含。
- 部分 Lynx 扩展已列出，但尚未提供详细文档。
- 方法说明尚未根据实现自动校验，可能不完整或不准确。

在依赖本参考前，请根据实现及所链接的 upstream CDP reference 验证实际行为。
:::

## 概览

- 作用域：`global-and-instance`
- CDP domain：[Tracing](https://chromedevtools.github.io/devtools-protocol/tot/Tracing/)

## 标准方法

- [Tracing.end](https://chromedevtools.github.io/devtools-protocol/tot/Tracing/#method-end)
- [Tracing.start](https://chromedevtools.github.io/devtools-protocol/tot/Tracing/#method-start)

## Lynx 扩展

### `Tracing.getStartupTracingConfig`

Returns the stored startup tracing configuration string.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_tracing_agent.cc`

###### 参数

无参数。

###### 返回字段

| Name     | Type     | Description                                                                  |
| -------- | -------- | ---------------------------------------------------------------------------- |
| `config` | `string` | Startup tracing configuration string read from the trace controller storage. |

###### 备注

- Dispatches through LynxGlobalDevToolMediator on the global DevTool task runner.
- Reads GlobalDevToolPlatformFacade::GetTraceController()->GetStartupTracingConfig().
- The trace controller stores the configuration as an opaque string. Lynx does not parse or validate it in this CDP method.
- Returns a CDP error with message Failed to get trace controller when no trace controller is available.
- If the global mediator has no default task runner, the current handler does not send an explicit CDP response.

###### 示例

**Read startup tracing config**

Request:

```json
{
  "id": 1,
  "method": "Tracing.getStartupTracingConfig"
}
```

Response:

```json
{
  "id": 1,
  "result": {
    "config": "{\"traceConfig\":{\"includedCategories\":[\"*\"]}}"
  }
}
```

### `Tracing.getStartupTracingFile`

Emits a Tracing.tracingComplete event for the stored startup tracing file.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_tracing_agent.cc`

###### 参数

无参数。

###### 返回字段

无返回字段。

###### 事件

- Tracing.tracingComplete

###### 备注

- Dispatches through LynxGlobalDevToolMediator on the global DevTool task runner.
- Reads GlobalDevToolPlatformFacade::GetTraceController()->GetStartupTracingFilePath().
- When a startup tracing file path exists, the method first sends an empty OK response and then sends Tracing.tracingComplete.
- Tracing.tracingComplete.params.stream is a FileStream handle encoded as a string. Read it with IO.read and close it with IO.close.
- Tracing.tracingComplete.params.dataLossOccurred is true when the file cannot be opened as a stream.
- Tracing.tracingComplete.params.isStartupTracing is true for this method.
- Returns a CDP error with message Startup Tracing is running when no file is available because tracing is still active.
- Returns a CDP error with message Failed to get startup tracing file when no file is available and tracing is not active.
- Returns a CDP error with message Failed to get trace controller when no trace controller is available.

###### 示例

**Request the startup tracing file**

Request:

```json
{
  "id": 1,
  "method": "Tracing.getStartupTracingFile"
}
```

Response:

```json
{
  "id": 1,
  "result": {}
}
```

**Startup tracing complete event**

Response:

```json
{
  "method": "Tracing.tracingComplete",
  "params": {
    "dataLossOccurred": false,
    "stream": "1",
    "isStartupTracing": true
  }
}
```

### `Tracing.setStartupTracingConfig`

Stores an opaque startup tracing configuration string.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_tracing_agent.cc`

###### 参数

| Name     | Type     | Required | Description                                                                   |
| -------- | -------- | -------- | ----------------------------------------------------------------------------- |
| `config` | `string` | Yes      | Startup tracing configuration string to persist through the trace controller. |

###### 返回字段

无返回字段。

###### 备注

- Dispatches through LynxGlobalDevToolMediator on the global DevTool task runner.
- Writes params.config to GlobalDevToolPlatformFacade::GetTraceController()->SetStartupTracingConfig().
- The trace controller stores the configuration as an opaque string. Lynx does not parse or validate it in this CDP method.
- Returns an empty OK response when params.config is present.
- Returns a CDP error with message Set Startup Tracing config is null when params.config is missing.
- Returns a CDP error with message Failed to get trace controller when no trace controller is available.
- If the global mediator has no default task runner, the current handler does not send an explicit CDP response.

###### 示例

**Store startup tracing config**

Request:

```json
{
  "id": 1,
  "method": "Tracing.setStartupTracingConfig",
  "params": {
    "config": "{\"traceConfig\":{\"includedCategories\":[\"*\"]}}"
  }
}
```

Response:

```json
{
  "id": 1,
  "result": {}
}
```

### `Tracing.takeVMSnapshotByUrl`

为指定 URL 对应页面的 MTS 或 BTS 虚拟机创建快照。

- 来源类型：`lynx-extension`
- 源码：`devtool/lynx_devtool/agent/domain_agent/inspector_tracing_agent.cc`

###### 参数

| 名称     | 类型       | 必填 | 说明                          |
| ------ | -------- | -- | --------------------------- |
| `url`  | `string` | 是  | 要创建快照的页面 URL。               |
| `type` | `string` | 是  | 要创建的快照类型，取值为 `bts` 或 `mts`。 |

###### 返回字段

无返回字段。

###### 备注

- 通过 LynxGlobalDevToolMediator 在全局 DevTool 任务执行器上分发。
- tracing 会话处于活动状态，且提供了 `params.url` 和 `params.type` 时，返回空的成功响应。
- 缺少 `url` 或 `type` 参数时，返回 CDP 错误，消息为 `Argument error`。
- tracing 会话未处于活动状态时，返回 CDP 错误，消息为 `Tracing is not started`。
- 如果全局 mediator 没有默认任务执行器，当前处理函数不会发送显式的 CDP 响应。

###### 示例

**为指定 URL 对应的页面创建快照**

请求：

```json
{
  "id": 1,
  "method": "Tracing.takeVMSnapshotByUrl",
  "params": {
    "config": "{\"url\":\"https://xxx/template.js\",\"type\":\"bts\"}"
  }
}
```

响应：

```json
{
  "id": 1,
  "result": {}
}
```

