使用 Visual Studio 调试器附加到正在运行的进程Attach to running processes with the Visual Studio debugger

可将 Visual Studio 调试器附加到本地或远程计算机上正在运行的进程。You can attach the Visual Studio debugger to a running process on a local or remote computer. 进程运行后,在 Visual Studio 中选择“调试” > “附加到进程”,或按 Ctrl+Alt+P,然后使用“附加到进程”对话框将调试器附加到进程 。After the process is running, select Debug > Attach to Process or press Ctrl+Alt+P in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

可以使用“附加到进程” 来调试本地或远程计算机上正在运行的应用、同时调试多个进程、 调试并非在 Visual Studio 中创建的应用或未使用附带调试器从 Visual Studio 启动的任何应用 。You can use Attach to Process to debug running apps on local or remote computers, debug multiple processes simultaneously, debug apps that weren't created in Visual Studio, or debug any app you didn't start from Visual Studio with the debugger attached. 例如,如果运行的是不带调试器的应用,并触发异常,则可以将调试器附加到运行应用的进程并开始调试。For example, if you're running an app without the debugger and hit an exception, you can then attach the debugger to the process running the app and begin debugging.

提示

不确定自己的调试方案是否需要使用“附加到进程” ?Not sure whether to use Attach to Process for your debugging scenario? 请参阅常见调试方案See Common debugging scenarios.

附加到本地计算机上正在运行的进程Attach to a running process on your local machine

若要快速重新附加到以前附加到的进程,请参阅重新附加到进程To quickly reattach to a process you attached to previously, see Reattach to a process.

若要调试远程计算机上的进程,请参阅附加到远程计算机上的进程To debug a process on a remote computer, see Attach to a process on a remote computer.

若要调试 Linux Docker 容器上的 .NET Core 进程,请参阅附加到 Linux Docker 容器To debug a .NET Core process on a Linux Docker container, see Attach to a Linux Docker container.

若要附加到本地计算机上的进程,请执行以下操作: To attach to a process on your local computer:

  1. 在 Visual Studio 中,选择“调试” > “附加到进程”(或按 Ctrl+Alt+P),打开“附加到进程”对话框 。In Visual Studio, select Debug > Attach to Process (or press Ctrl+Alt+P) to open the Attach to Process dialog box.

    “连接类型”应设置为“默认” 。Connection type should be set to Default. “连接目标”应该是本地计算机名称 。Connection target should be your local machine name.

    DBG_Basics_Attach_To_ProcessDBG_Basics_Attach_To_Process

  2. 在“可用进程”列表中,查找并选择要附加到的一个或多个进程 。In the Available processes list, find and select the process or processes you want to attach to.

    • 若要快速选择一个进程,请在“筛选进程”框中键入其名称或首字母 。To quickly select a process, type its name or first letter in the Filter processes box.

    • 如果不知道进程名称,请浏览列表或参阅常见调试方案,了解一些常见的进程名称。If you don't know the process name, browse through the list, or see Common debugging scenarios for some common process names.

    提示

    “附加到进程”对话框处于打开状态时,进程可以在后台启动和停止,因此正在运行的进程列表可能不总是最新内容 。Processes can start and stop in the background while the Attach to Process dialog box is open, so the list of running processes may not always be current. 可随时选择“刷新”查看当前列表 。You can select Refresh at any time to see the current list.

  3. 在“附加到”字段中,确保已列出计划调试的代码类型 。In the Attach to field, make sure the type of code you plan to debug is listed. 默认的“自动”设置适用于大多数应用类型 。The default Automatic setting works for most app types.

    手动选择代码类型:To select code types manually:

    1. 单击“选择” 。Click Select.
    2. 在“选择代码类型”对话框中,选择“调试这些代码类型” 。In the Select Code Type dialog box, select Debug these code types.
    3. 选择要调试的代码类型。Select the code types you want to debug.
    4. 选择“确定” 。Select OK.
  4. 选择“附加” 。Select Attach.

备注

可附加到多个应用进行调试,但在调试器中一次只能有一个应用处于活动状态。You can be attached to multiple apps for debugging, but only one app is active in the debugger at a time. 可在 Visual Studio 的“调试位置”工具栏或“进程”窗口中设置活动的应用 。You can set the active app in the Visual Studio Debug Location toolbar or Processes window.

附加到远程计算机上的进程Attach to a process on a remote computer

还可以在“附加到进程”对话框中选择远程计算机,查看该计算机上运行的可用进程列表,并附加到一个或多个进程以进行调试 。You can also select a remote computer in the Attach to Process dialog box, view a list of available processes running on that computer, and attach to one or more of the processes for debugging. 远程调试器 (msvsmon.exe) 必须在远程计算机上运行 。The remote debugger (msvsmon.exe) must be running on the remote computer. 有关详细信息,请参阅远程调试For more information, see Remote debugging.

有关调试已部署到 IIS 的 ASP.NET 应用程序的更完整说明,请参阅远程调试远程 IIS 计算机上的 ASP.NETFor more complete instructions for debugging ASP.NET applications that have been deployed to IIS, see Remote debugging ASP.NET on a remote IIS computer.

附加到远程计算机上正在运行的进程:To attach to a running process on a remote computer:

  1. 在 Visual Studio 中,选择“调试” > “附加到进程”(或按 Ctrl+Alt+P),打开“附加到进程”对话框 。In Visual Studio, select Debug > Attach to Process (or press Ctrl+Alt+P) to open the Attach to Process dialog box.

  2. 在大多数情况下,“连接类型”应为“默认” 。Connection type should be Default for most cases. 在“连接目标”框中,使用以下方法之一选择远程计算机 :In the Connection target box, select the remote computer, using one of the following methods:

    • 选择下拉箭头旁边的“连接目标”,并从下拉列表中选择计算机名称 。Select the drop-down arrow next to Connection target, and select the computer name from the drop-down list.

    • 在“连接目标”框中键入计算机名称,然后按 Enter 。Type the computer name in the Connection target box and press Enter.

      确保 Visual Studio 将所需的端口添加到计算机名称中,其格式为:<remote computer name>:port Verify that Visual Studio adds the required port to the computer name, which appears in the format: <remote computer name>:port

      备注

      如果无法使用远程计算机名称进行连接,请尝试使用 IP 和端口地址(例如 123.45.678.9:4022)。If you can't connect using the remote computer name, try using the IP and port address (for example, 123.45.678.9:4022). 4024 是 Visual Studio 2019 x64 远程调试器的默认端口。4024 is the default port for the Visual Studio 2019 x64 remote debugger. 有关其他远程调试器端口分配,请参阅远程调试器端口分配For other remote debugger port assignments, see Remote debugger port assignments.

      备注

      如果无法使用远程计算机名称进行连接,请尝试使用 IP 和端口地址(例如 123.45.678.9:4022)。If you can't connect using the remote computer name, try using the IP and port address (for example, 123.45.678.9:4022). 4022 是 Visual Studio 2017 x64 远程调试器的默认端口。4022 is the default port for the Visual Studio 2017 x64 remote debugger. 有关其他远程调试器端口分配,请参阅远程调试器端口分配For other remote debugger port assignments, see Remote debugger port assignments.

    • 选择“连接目标” 框旁边的“查找” 按钮,以打开“远程连接” 对话框。Select the Find button next to the Connection target box to open the Remote Connections dialog box. “远程连接” 对话框列出了本地子网上或直接附加到计算机的所有设备。The Remote Connections dialog box lists all the devices that are on your local subnet or directly attached to your computer. 可能需要在服务器上打开 UDP 端口 3702 才能发现远程设备。You may need to open UDP port 3702 on the server to discover remote devices. 选择所需的计算机或设备,然后单击“选择” 。Select the computer or device you want, and then click Select.

    备注

    “连接类型”设置在调试会话之间保持不变 。The Connection type setting persists between debugging sessions. 而“连接目标”设置只有在成功与该目标建立了调试连接时才会在调试会话之间保持不变 。The Connection target setting persists between debugging sessions only if a successful debugging connection occurred with that target.

  3. 单击“刷新”,填充“可用进程”列表 。Click Refresh to populate the Available processes list.

    提示

    “附加到进程”对话框处于打开状态时,进程可以在后台启动和停止,因此正在运行的进程列表可能不总是最新内容 。Processes can start and stop in the background while the Attach to Process dialog box is open, so the list of running processes may not always be current. 可随时选择“刷新”查看当前列表 。You can select Refresh at any time to see the current list.

  4. 在“可用进程”列表中,查找并选择要附加到的一个或多个进程 。In the Available processes list, find and select the process or processes you want to attach to.

  5. 在“附加到”字段中,确保已列出计划调试的代码类型 。In the Attach to field, make sure the type of code you plan to debug is listed. 默认的“自动”设置适用于大多数应用类型 。The default Automatic setting works for most app types.

    手动选择代码类型:To select code types manually:

    1. 单击“选择” 。Click Select.
    2. 在“选择代码类型”对话框中,选择“调试这些代码类型” 。In the Select Code Type dialog box, select Debug these code types.
    3. 选择要调试的代码类型。Select the code types you want to debug.
    4. 选择“确定” 。Select OK.
  6. 选择“附加” 。Select Attach.

备注

可附加到多个应用进行调试,但在调试器中一次只能有一个应用处于活动状态。You can be attached to multiple apps for debugging, but only one app is active in the debugger at a time. 可在 Visual Studio 的“调试位置”工具栏或“进程”窗口中设置活动的应用 。You can set the active app in the Visual Studio Debug Location toolbar or Processes window.

在某些情况下,在远程桌面(终端服务)会话中进行调试时,“可用进程”列表时不会显示所有可用进程 。In some cases, when you debug in a Remote Desktop (Terminal Services) session, the Available processes list won't display all available processes. 如果以受限制的用户帐户的用户身份运行 Visual Studio,则“可用进程”列表不会显示在会话 0 中运行的进程 。If you are running Visual Studio as a user who has a limited user account, the Available processes list won't show processes that are running in Session 0. 会话 0 用于服务和其他服务器进程,包括 w3wp.exe 。Session 0 is used for services and other server processes, including w3wp.exe. 可通过以下方法解决该问题:使用管理员帐户运行 Visual StudioVisual Studio 或从服务器控制台(而不是“终端服务”会话)运行 Visual StudioVisual StudioYou can solve the problem by running Visual StudioVisual Studio under an administrator account or by running Visual StudioVisual Studio from the server console instead of a Terminal Services session.

如果这两种解决方法都不可行,第三种方法是通过从 Windows 命令行运行 vsjitdebugger.exe -p <ProcessId> 来附加到进程。If neither of those workarounds is possible, a third option is to attach to the process by running vsjitdebugger.exe -p <ProcessId> from the Windows command line. 可使用 tlist.exe 来确定进程 ID 。You can determine the process ID using tlist.exe. 若要获取“tlist.exe”,请从 WDK 和 WinDbg 下载中下载并安装适用于 Windows 的调试工具 。To obtain tlist.exe, download and install Debugging Tools for Windows, available at WDK and WinDbg downloads.

使用 SSH 附加到正在 Linux 上运行的 .NET Core 进程Attach to a .NET Core process running on Linux using SSH

有关详细信息,请参阅使用 SSH 远程调试正在 Linux 上运行的 .NET CoreFor more information, see Remote debug .NET Core running on Linux using SSH.

附加到正在 Linux Docker 容器上运行的进程Attach to a process running on a Linux Docker container

可以使用“附加到进程” 对话框,将 Visual Studio 调试器附加到正在本地或远程计算机的 Linux .NET Core Docker 容器中运行的进程。You can attach the Visual Studio debugger to a process running in a Linux .NET Core Docker container on your local or remote machine using the Attach to Process dialog box.

重要

若要使用此功能,必须安装 .NET Core 跨平台开发工作负载,并对源代码具有本地访问权限。To use this feature, you must install the .NET Core Cross-Platform Development workload and have local access to the source code.

附加到 Linux Docker 容器中正在运行的进程:To attach to a running process in a Linux Docker container:

  1. 在 Visual Studio 中,选择“调试 > 附加到进程”(CTRL+ALT+P),打开“附加到进程”对话框 。In Visual Studio, select Debug > Attach to Process (CTRL+ALT+P) to open the Attach to Process dialog box.

“附加到进程”菜单Attach to Process Menu

  1. 将“连接类型” 设置为“Docker (Linux 容器)” 。Set the Connection type to Docker (Linux Container).

  2. 通过“选择 Docker 容器” 对话框选择“查找...” ,以设置“连接目标” 。Select Find... to set the Connection target via the Select Docker Container dialog box.

    可以在本地或远程调试 Docker 容器进程。You can debug a Docker container process either locally or remotely.

    在本地调试 Docker 容器进程:To debug a Docker container process locally:

    1. 将“Docker CLI 主机” 设置为“本地计算机” 。Set Docker CLI host to Local Machine.
    2. 从列表中选择要附加到的正在运行的容器,然后点击“确定” 。Select a running container to attach to from the list and hit OK.

    “选择 Docker 容器”菜单Select Docker Container Menu

    B.远程调试 Docker 容器进程:B. To debug a Docker container process remotely:

    备注

    可通过两个选项远程连接到 Docker 容器中正在运行的进程。There are two options for connecting remotely to a running process in a Docker container. 第一个选项是使用 SSH,它适用于未在本地计算机上安装 Docker 工具的情况。The first option, to use SSH, is ideal if you don't have Docker tools installed on your local machine. 如果在本地安装了 Docker 工具,并且具有配置为接受远程请求的 Docker 守护程序,请尝试第二个选项 - 使用 Docker 守护程序。If you do have Docker tools installed locally and you have a Docker daemon that's configured to accept remote requests, try the second option, using a Docker daemon.

    1. 通过 SSH 连接到远程计算机:To connect to a remote machine via SSH:

      1. 选择“添加...” 以连接到远程系统。Select Add... to connect to a remote system.
        连接到远程系统Connect to a Remote System
      2. 成功连接到 SSH 或守护程序后,选择要附加到的正在运行的容器,然后点击“确定” 。Select a running container to attach to after connecting to the SSH or daemon successfully and hit OK.
    2. 通过 Docker 守护程序将目标设置为正在运行进程的远程容器To set the target to a remote container running a process via a Docker daemon

      1. 在“Docker 主机(可选)” 下指定守护程序地址(即通过 TCP、IP 等),然后单击刷新链接。Specify the daemon address (i.e. via TCP, IP, etc.) under Docker host (Optional) and click the refresh link.
      2. 成功连接到守护程序后,选择要附加到的正在运行的容器,然后点击“确定” 。Select a running container to attach to after connecting to the daemon successfully and hit OK.
  3. 从“可用进程” 列表中选择相应的容器进程并选择“附加” ,开始在 Visual Studio 中调试 C# 容器进程!Choose the corresponding container process from the list of Available processes and select Attach to start debugging your C# container process in Visual Studio!

    已完成的 Docker 附加菜单Completed Docker Attach Menu

附加到正在 Windows Docker 容器上运行的进程Attach to a process running on a Windows Docker container

可以使用“附加到进程” 对话框将 Visual Studio 调试器附加到正在本地计算机的 Windows Docker 容器中运行的进程。You can attach the Visual Studio debugger to a process running in a Windows Docker container on your local machine using the Attach to Process dialog box.

重要

若要对 .NET Core 进程使用此功能,必须安装 .NET Core 跨平台开发工作负载,并对源代码具有本地访问权限。To use this feature with a .NET Core process, you must install the .NET Core Cross-Platform Development workload and have local access to the source code.

附加到 Windows Docker 容器中正在运行的进程:To attach to a running process in a Windows Docker container:

  1. 在 Visual Studio 中,选择“调试 > 附加到进程”(或 CTRL+ALT+P),打开“附加到进程”对话框 。In Visual Studio, select Debug > Attach to Process (or CTRL+ALT+P) to open the Attach to Process dialog box.

    “附加到进程”菜单Attach to Process Menu

  2. 将“连接类型” 设置为“Docker (Windows 容器)” 。Set the Connection type to Docker (Windows Container).

  3. 使用“选择 Docker 容器” 对话框选择“查找...” ,以设置“连接目标” 。Select Find... to set the Connection target using the Select Docker Container dialog box.

    重要

    目标进程必须与运行它的 Docker Windows 容器具有相同的处理器体系结构。The target process must have the same processor architecture as the Docker Windows container it is running on.

    当前无法通过 SSH 将目标设置为远程容器,而只能使用 Docker 守护程序来完成。Setting the target to a remote container via SSH is currently unavailable and can only be done using a Docker daemon.

    通过 Docker 守护程序将目标设置为正在运行进程的远程容器To set the target to a remote container running a process via a Docker daemon

    1. 在“Docker 主机(可选)” 下指定守护程序地址(即通过 TCP、IP 等),然后单击刷新链接。Specify the daemon address (i.e. via TCP, IP, etc.) under Docker host (Optional) and click the refresh link.

    2. 成功连接到守护程序后,选择要附加到的正在运行的容器,然后选择“确定”。Select a running container to attach to after connecting to the daemon successfully and choose OK.

  4. 从“可用进程” 列表中选择相应的容器进程并选择“附加” ,以开始调试 C# 容器进程。Choose the corresponding container process from the list of Available processes and select Attach to start debugging your C# container process.

    已完成的 Docker 附加菜单Completed Docker Attach Menu

  5. 从可用进程列表中选择相应的容器进程并选择“附加” ,以开始调试 C# 容器进程。Choose the corresponding container process from the list of available processes and choose Attach to start debugging your C# container process.

重新附加到进程Reattach to a process

您可以快速重新附加到先前已通过选择附加到的进程 “调试” > “重新附加到进程” (Shift+Alt+P)。You can quickly reattach to processes that you were previously attached to by choosing Debug > Reattach to Process (Shift+Alt+P). 当选择此命令时,调试器会立即尝试附加到最后连接的进程,方法是首次尝试匹配先前的进程 ID ,如果失败,将匹配先前的进程名称。When you choose this command, the debugger will immediately try to attach to the last processes you attached to by first attempting to match the previous process ID and if that fails, by matching to the previous process name. 如果不找到任何匹配项,或多个进程具有相同的名称,“附加到进程” 对话框将打开,这样您就可以选择正确的进程 。If no matches are found, or if several processes have the same name, the Attach to Process dialog box will open so you can select the correct process.

备注

从 Visual Studio 2017 开始,可以使用“重新附加到进程” 命令。The Reattach to Process command is available starting in Visual Studio 2017.

常见调试方案Common debugging scenarios

为帮助确定是否使用“附加到进程”以及要附加到的进程,下表显示了一些常见调试方案,并提供了指向更多可用说明的链接 。To help you determine whether to use Attach to Process and what process to attach to, the following table shows a few common debugging scenarios, with links to more instructions where available. (该列表并未列出详尽信息。)(The list is not exhaustive.)

对于某些应用类型,如通用 Windows 应用 (UWP) ,不能直接附加到进程名称,而需使用 Visual Studio 中的“调试安装的应用程序包”菜单选项(请参阅表格) 。For some app types, like Universal Windows App (UWP) apps, you don't attach directly to a process name, but use the Debug Installed App Package menu option in Visual Studio instead (see table).

为使调试器附加到用 C++ 编写的代码,该代码需要发出 DebuggableAttributeFor the debugger to attach to code written in C++, the code needs to emit DebuggableAttribute. 可通过链接 /ASSEMBLYDEBUG 链接器选项将它自动添加到代码中。You can add this to your code automatically by linking with the /ASSEMBLYDEBUG linker option.

对于客户端脚本调试,必须在浏览器中启用脚本调试。For client-side script debugging, script debugging must be enabled in the browser. 若要在 Chrome 上调试客户端脚本,请选择“JavaScript (Chrome)”或“JavaScript (Microsoft Edge - Chromium)”作为代码类型,根据应用类型的不同,可能需要关闭所有 Chrome 实例并在调试模式下启动浏览器(在命令行中键入 chrome.exe --remote-debugging-port=9222) 。For debugging client-side script on Chrome, choose JavaScript (Chrome) or JavaScript (Microsoft Edge - Chromium) as the code type, and depending on your app type, you may need to close all Chrome instances and start the browser in debugging mode (type chrome.exe --remote-debugging-port=9222 from a command line). 在 Visual Studio 的早期版本中,Chrome 的脚本调试器是 Web 工具包 。In earlier versions of Visual Studio, the script debugger for Chrome was Web kit.

若要快速选择要附加到的正在运行的进程,请在 Visual Studio 中键入 Ctrl+Alt+P,然后键入进程名称的第一个字母 。To quickly select a running process to attach to, in Visual Studio, type Ctrl+Alt+P, and then type the first letter of the process name.

方案Scenario 调试方法Debug method 进程名Process name 备注和链接Notes and links
远程调试 IIS 服务器上的 ASP.NET 4 或 4.5Remote debug ASP.NET 4 or 4.5 on an IIS server 使用远程工具和“附加到进程” Use remote tools and Attach to Process w3wp.exe w3wp.exe 请参阅远程调试远程 IIS 计算机上的 ASP.NETSee Remote debugging ASP.NET on a remote IIS computer
远程调试 IIS 服务器上的 ASP.NET CoreRemote debug ASP.NET Core on an IIS server 使用远程工具和“附加到进程” Use remote tools and Attach to Process w3wp.exe 或 dotnet.exe w3wp.exe or dotnet.exe 从 .NET Core 3 开始,w3wp.exe 进程用于默认的应用内托管模型Starting in .NET Core 3, the w3wp.exe process is used for the default in-app hosting model. 有关应用部署,请参阅发布到 IISFor app deployment, see Publish to IIS. 有关更多详细信息,请参阅远程调试远程 IIS 计算机上的 ASP.NET CoreFor more detailed information, see Remote debugging ASP.NET Core on a remote IIS computer
针对受支持的应用类型,调试本地 IIS 服务器上的客户端脚本Debug client-side script on a local IIS server, for supported app types 使用“附加到进程” Use Attach to Process chrome.exe 、MicrosoftEdgeCP.exe 或 iexplore.exe chrome.exe, MicrosoftEdgeCP.exe, or iexplore.exe 必须启用脚本调试。Script debugging must be enabled. 对于 Chrome,还必须在调试模式下运行 Chrome(在命令行中键入 chrome.exe --remote-debugging-port=9222),并在“附加到” 字段中选择“JavaScript (Chrome)” 。For Chrome, you must also run Chrome in debug mode (type chrome.exe --remote-debugging-port=9222 from a command line) and select JavaScript (Chrome) in the Attach to field.
调试本地计算机上的 C#、Visual Basic 或 C++ 应用Debug a C#, Visual Basic, or C++ app on the local machine 使用标准调试 (F5 ) 或“附加到进程” Use either standard debugging (F5) or Attach to Process <appname>.exe<appname>.exe 在大多数情况下,请使用标准调试,而不是“附加到进程” 。In most scenarios, use standard debugging and not Attach to Process.
远程调试 Windows 桌面应用Remote debug a Windows desktop app 远程工具Remote tools 不可用N/A 请参阅远程调试 C# 或 Visual Basic 应用远程调试 C++ 应用See Remote debug a C# or Visual Basic app or Remote debug a C++ app
调试 Linux 上的 .NET CoreDebug .NET Core on Linux 使用“附加到进程” Use Attach to Process dotnet.exedotnet.exe 若要使用 SSH,请参阅使用 SSH 远程调试正在 Linux 上运行的 .NET CoreTo use SSH, see Remote debug .NET Core running on Linux using SSH.
在没有调试器的情况下,在启动本地计算机上的 ASP.NET 应用后调试该应用Debug an ASP.NET app on the local machine after you start the app without the debugger 使用“附加到进程” Use Attach to Process iiexpress.exeiiexpress.exe 这可能有助于加快应用加载速度,例如在进行分析时。This may be helpful to make your app load faster, such as (for example) when profiling.
在服务器进程上调试其他受支持的应用类型Debug other supported app types on a server process 如果是远程服务器,请使用远程工具和“附加到进程” If server is remote, use remote tools, and Attach to Process chrome.exe 、iexplore.exe 或其他进程chrome.exe, iexplore.exe, or other processes 如有必要,请使用资源监视器来帮助识别进程。If necessary, use Resource Monitor to help identify the process. 请参阅远程调试See Remote debugging.
远程调试通用 Windows 应用 (UWP)、OneCore、HoloLens 或 IoT 应用Remote debug a Universal Windows App (UWP), OneCore, HoloLens, or IoT app 调试安装的应用包Debug installed app package 不可用N/A 请参阅调试安装的应用包,而不是使用“附加到进程” See Debug an installed app package instead of using Attach to Process
调试不是从 Visual Studio 启动的通用 Windows 应用 (UWP)、OneCore、HoloLens 或 IoT 应用Debug a Universal Windows App (UWP), OneCore, HoloLens, or IoT app that you didn't start from Visual Studio 调试安装的应用包Debug installed app package 不可用N/A 请参阅调试安装的应用包,而不是使用“附加到进程” See Debug an installed app package instead of using Attach to Process

使用调试器功能Use debugger features

若要在附加到进程时使用 Visual Studio 调试器的全部功能(例如命中断点),应用必须与本地源和符号完全匹配。To use the full features of the Visual Studio debugger (like hitting breakpoints) when attaching to a process, the app must exactly match your local source and symbols. 也就是说,调试器必须能够加载正确的符号 (.pdb) 文件That is, the debugger must be able to load the correct symbol (.pdb) files. 默认情况下,这需要调试生成。By default, this requires a debug build.

对于远程调试方案,必须已在 Visual Studio 中打开了源代码(或源代码的副本)。For remote debugging scenarios, you must have the source code (or a copy of the source code) already open in Visual Studio. 远程计算机上编译的应用二进制文件必须与本地计算机上的源自同一版本。The compiled app binaries on the remote machine must come from the same build as on the local machine.

在某些本地调试方案中,如果应用中存在正确的符号文件,则可以在 Visual Studio 中进行调试而无法访问源。In some local debugging scenarios, you can debug in Visual Studio with no access to the source if the correct symbol files are present with the app. 默认情况下,这需要调试生成。By default, this requires a debug build. 有关详细信息,请参阅指定符号文件和源文件For more information, see Specify symbol and source files.

排查附加错误Troubleshoot attach errors

当调试器附加到一个正在运行的进程时,该进程可能包含一种或多种类型的代码。When the debugger attaches to a running process, the process can contain one or more types of code. 可在 “选择代码类型” 对话框中显示并选择可将调试器附加到的代码类型。The code types the debugger can attach to are displayed and selected in the Select Code Type dialog box.

有时,调试器能够成功附加到一种代码类型,但不能附加到另一种代码类型。Sometimes, the debugger can successfully attach to one code type, but not to another code type. 这种情况可能发生在你尝试附加到远程计算机上运行的进程时。This might occur if you are trying to attach to a process that is running on a remote computer. 原因是远程计算机上可能安装了一些代码类型的远程调试组件,但没有安装另一些代码类型的远程调试组件。The remote computer might have remote debugging components installed for some code types but not for others. 这种情况还可能发生在你尝试为直接数据库调试附加到两个或多个进程时。It can also occur if you try to attach to two or more processes for direct database debugging. SQL 调试仅支持附加到单个进程。SQL debugging supports attaching to a single process only.

如果调试器能够附加到某些代码类型而不是所有代码类型,你会看到一条消息,指示无法附加的类型。If the debugger is able to attach to some, but not all, code types, you see a message identifying which types failed to attach.

如果调试器成功地附加到至少一种代码类型,你就可以继续调试进程。If the debugger successfully attaches to at least one code type, you can proceed to debug the process. 你只能调试那些已被成功附加的代码类型。You will be able to debug only the code types that were successfully attached. 进程中未附加的代码将继续运行,但你无法对该代码设置断点、查看数据或执行其他调试操作。The unattached code in the process will still run, but you won't be able to set breakpoints, view data, or perform other debugging operations on that code.

如果想了解有关调试器未能附加到某种代码类型的详细原因,请尝试仅重新附加到该代码类型。If you want more specific information about why the debugger failed to attach to a code type, try to reattach to only that code type.

获得有关代码类型未能附加的具体信息:To obtain specific information about why a code type failed to attach:

  1. 从进程中分离。Detach from the process. 在“调试” 菜单上,选择“全部分离” 。On the Debug menu, select Detach All.

  2. 重新附加到该进程,但仅选择未能附加的代码类型。Reattach to the process, selecting only the code type that failed to attach.

    1. 在“附加到进程”对话框,选择“可用进程”列表中的进程 。In the Attach to Process dialog box, select the process in the Available processes list.

    2. 选择“选择” 。Select Select.

    3. “选择代码类型” 对话框中,选择 “调试以下代码类型” 和未能附加的代码类型。In the Select Code Type dialog box, select Debug these code types and the code type that failed to attach. 取消选择其他代码类型。Deselect the other code types.

    4. 选择“确定” 。Select OK.

    5. 在“附加到进程” 对话框中,选择“附加” 。In the Attach to Process dialog box, select Attach.

    此时,附加将彻底失败,并且你将收到一条特定的错误消息。This time, the attach will fail completely, and you will get a specific error message.

请参阅See also