# 27.3.开启 DTrace 支持

在 FreeBSD 9.2 和 10.0 中，DTrace 支持已内置于 **GENERIC** 内核中。使用较老版本的 FreeBSD 或希望将 DTrace 支持静态编译进内核的用户，应在自定义内核配置文件中添加以下行，并按照 [配置 FreeBSD 内核](https://docs.freebsd.org/en/books/handbook/kernelconfig/#kernelconfig) 中的说明重新编译内核：

```ini
options         KDTRACE_HOOKS
options         DDB_CTF
makeoptions	DEBUG=-g
makeoptions	WITH_CTF=1
```

AMD64 架构的用户还应添加以下行：

```ini
options         KDTRACE_FRAME
```

此选项提供了对 [dtrace\_fbt(4)](https://man.freebsd.org/cgi/man.cgi?query=dtrace_fbt\&sektion=4\&format=html) 的支持。虽然没有该选项 DTrace 仍可工作，但函数边界跟踪功能将受限。

当 FreeBSD 系统重启进入新内核，或使用 `kldload dtraceall` 加载 DTrace 内核模块后，可安装当前的 DTrace 工具包 ([sysutils/dtrace-toolkit](https://cgit.freebsd.org/ports/tree/sysutils/dtrace-toolkit/))，该工具包包含用于收集系统信息的现成脚本，包括检查打开的文件、内存、CPU 使用情况等。FreeBSD 基础系统中也包含一些脚本，存放于 **/usr/share/dtrace**。

> **注意**
>
> **/usr/share/dtrace** 中的脚本是专门为 FreeBSD 移植的。DTrace 工具包中的脚本并非全部可直接在 FreeBSD 上运行，有些脚本需要进行一定修改才能使用。

DTrace 工具包使用 DTrace 特有的脚本语言，这种语言称为 D 语言，与 C++ 非常相似。本文档不对 D 语言做深入讲解。如需概览，请参阅 FreeBSD 的 [d(7)](https://man.freebsd.org/cgi/man.cgi?query=d\&sektion=7\&format=html) 手册页。D 语言在 [illumos 动态跟踪指南](https://www.illumos.org/books/dtrace/bookinfo.html) 中也有详细介绍。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-27-zhang-dtrace/27.3.-kai-qi-dtrace-zhi-chi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
