# 附加到进程

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

> **TL;DR**
> `运行 | 附加到进程`  `Ctrl+Alt+F5` (Windows), `⌥ ⇧ F5` (macOS), `⌥ ⇧ F5` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ F5` (macOS System Shortcuts), `Ctrl+Alt+5` (XWin), `Ctrl+Alt+5` (GNOME), `Ctrl+Alt+5` (KDE), `Ctrl+Alt+F5` (Emacs), `Ctrl+Alt+F5` (Sublime Text), `⌥ ⇧ F5` (Sublime Text (macOS)), `⌥ ⇧ F5` (Xcode), `Ctrl+Alt+P` (Visual Studio), `⌘ ⌥ P` (Visual Studio (macOS)), `Ctrl+Alt+F5` (ReSharper), `⌃ ⌥ F5` (ReSharper (macOS)), `Ctrl+Alt+F5` (QtCreator), `⌥ ⇧ F5` (QtCreator (macOS)), `Ctrl+Alt+F5` (NetBeans), `Ctrl+Alt+F5` (Eclipse), `⌥ ⇧ F5` (Eclipse (macOS))

CLion 提供了一种连接调试器到本地和远程进程的方法。

对于 C/C++ 应用程序，您可以在 Windows 上使用 GDB 或 MSVC LLDB，在 macOS 上使用 LLDB，在 Linux 上同时使用 GDB 和 LLDB 附加到进程。 在适用的情况下，也可以使用其他语言的调试器（例如，Python 调试器）。

> **Note:**
> 如果您想调试可以从 CLion 启动的程序，最好的方法是 [启动本地调试会话](starting-the-debugger-session.html)。

Procedure: Ubuntu 的先决条件

在 Ubuntu 和其他一些 Linux 发行版上，您可能会在首次尝试附加到进程时收到  ptrace: Operation not permitted 错误消息。 请执行以下操作之一：

1. 要暂时禁用此限制，请输入以下命令：

```CONSOLE
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

```

2. 要永久禁用此限制，请打开  `/etc/sysctl.d/10-ptrace.conf` 文件进行编辑，并将行  `kernel.yama.ptrace_scope = 1` 更改为  `kernel.yama.ptrace_scope = 0` 。

要立即应用更改，请输入上述命令  `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope` 。

或者，运行  `sudo service procps restart` 或重新启动您的系统。

您可以在 [此处](http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process)找到更多详细信息。

Procedure: 附加到正在运行的进程

1. 按 `Ctrl+Alt+F5` (Windows), `⌥ ⇧ F5` (macOS), `⌥ ⇧ F5` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ F5` (macOS System Shortcuts), `Ctrl+Alt+5` (XWin), `Ctrl+Alt+5` (GNOME), `Ctrl+Alt+5` (KDE), `Ctrl+Alt+F5` (Emacs), `Ctrl+Alt+F5` (Sublime Text), `⌥ ⇧ F5` (Sublime Text (macOS)), `⌥ ⇧ F5` (Xcode), `Ctrl+Alt+P` (Visual Studio), `⌘ ⌥ P` (Visual Studio (macOS)), `Ctrl+Alt+F5` (ReSharper), `⌃ ⌥ F5` (ReSharper (macOS)), `Ctrl+Alt+F5` (QtCreator), `⌥ ⇧ F5` (QtCreator (macOS)), `Ctrl+Alt+F5` (NetBeans), `Ctrl+Alt+F5` (Eclipse), `⌥ ⇧ F5` (Eclipse (macOS)) 或从主菜单中选择  `运行 | 附加到进程` 。

附加到进程  对话框会打开所有检测到的正在运行的进程列表。

您可以将进程视为列表或树状结构，并按名称搜索特定进程：

![在“附加到进程”对话框中搜索](https://resources.jetbrains.com/help/img/idea/2026.3/cl_attachprocess_name.png)

对于每个进程，您可以找到可执行文件名称和进程 ID、启动它的用户、可用的调试器以及用于启动的命令。

使用下拉菜单按调试器类型过滤进程列表：

![可附加的调试器列表](https://resources.jetbrains.com/help/img/idea/2026.3/cl_attach_debuggers_dropdown.png)

您还可以通过设置   仅显示我的进程  来过滤列表：

![在“附加到进程”中仅显示我的进程](https://resources.jetbrains.com/help/img/idea/2026.3/cl_attach_showmyprocesses.png)

2. 附加到本地进程:

本地  选项卡显示本地进程列表。 选择进程并点击   附加方式...  。

附加到远程进程:

切换到   SSH  选项卡以查看远程进程列表。 请确保事先配置了 [带有 SSH 凭据的远程工具链](remote-projects-support.html#remote-toolchain)。

![正在附加到远程进程](https://resources.jetbrains.com/help/img/idea/2026.3/cl_attachtoprocess_remote.png)

在 Windows 上，切换到   WSL  选项卡以打开 WSL 进程列表。 您将能够使用 WSL GDB 调试器附加到它们（如果您的项目中至少配置了一个 [WSL 工具链](how-to-use-wsl-development-environment-in-product.html#wsl-tooclhain)）。

> **Tip:**
> 您可以附加到以管理员权限远程启动的进程。

3. 以与您通常在 CLion 中相同的方式 [调试](debugging-code.html)（ [设置断点](using-breakpoints.html#set-line-breakpoint)、 [逐步执行](stepping-through-the-program.html)、 [暂停和恢复](starting-the-debugger-session.html#pause)进程、 [评估表达式](examining-suspended-program.html#evaluating-expressions)等）。

> **Note:**
> 如果您附加的进程未提供调试信息，则断点将不可用，单步执行将在 [反汇编](disassembly-view.html)视图中进行。

Procedure: 附加到尚未启动的进程

如果您想要附加的进程尚未运行，您可以使用   附加到未启动的进程   操作预先配置附加方式：

1. [在代码中设置必要的断点](using-breakpoints.html#set-breakpoints)。

2. 从主菜单中选择  `运行 | 附加到未启动的进程` 。

3. 在打开的对话框中，指定进程的命令行或可执行文件名。

> **Tip:**
> 为识别进程，CLion 会将您提供的字符串包裹在  `*`  中（星号），并将其用作匹配模式。 通常，仅使用可执行文件名（例如  `终端` ）就足够了。 不过，您可以使用  `*`  和  `?`  来构建匹配模式。 例如，  `git.exe pu?? origin`  会匹配  `git.exe push origin`  和  `git.exe. pull origin`  —— CLion 将附加到第一个匹配的进程。

4. 如果希望在 CLion 附加后暂停进程，请勾选   在附加后保持进程挂起   复选框。

5. 如果不希望在调试会话结束后 CLion 重新打开此对话框，请取消选中   调试后重新打开对话框   复选框。

6. 点击   附加   并选择调试器（如适用）。

![CLion 正在等待指定的进程](https://resources.jetbrains.com/help/img/idea/2026.3/attach_to_unstarted_process_dialog_watching.png)

CLion 将开始监视。 一旦识别出进程，将自动附加并触发调试。

Procedure: 从进程中分离

从远程进程分离的过程与停止调试会话相同。 但是，请注意，当您分离时，调试会话会关闭，但进程会继续运行。

* 点击主工具栏中的   停止   ![停止按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.suspend.svg)按钮 [调试工具窗口](debug-tool-window.html)。

* 或者，点击   停止   ![停止按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.suspend.svg)主工具栏上的 `Ctrl+F2` (Windows), `⌘ F2` (macOS), `⌘ F2` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ .` (macOS System Shortcuts), `Ctrl+F2` (XWin), `Ctrl+F2` (GNOME), `Ctrl+2` (KDE), `Ctrl+F2` (Emacs), `Ctrl+F2` (Sublime Text), `Ctrl+F2` (Sublime Text (macOS)), `⌘ .` (Xcode), `Shift+F5` (Visual Studio), `⇧ F5` (Visual Studio (macOS)), `Shift+F5` (ReSharper), `⇧ F5` (ReSharper (macOS)), `Shift+F5` (QtCreator), `⌘ ⇧ Y` (QtCreator (macOS)), `Shift+F5` (NetBeans), `Ctrl+F2` (Eclipse), `⌘ F2` (Eclipse (macOS))，然后选择要关闭的会话。

## See also

### 参考

[调试工具窗口](debug-tool-window.html)

### 外部链接

[CLion Debugging Beyond the Basics - Part 3 - Beyond Local](https://blog.jetbrains.com/clion/2021/05/clion-debugging-beyond-the-basics-part-3-beyond-local)

