Attaching to a Running Process Using Code::Blocks

By John Paul Mueller, Jeff Cogswell

Every time you start a new application, you create one or more processes. A process is simply executable code that is loaded into memory. The CPU reads and executes the instructions to perform the tasks you ask the application to do. When the CPU loads your application into memory, it assigns each process the application creates a Process IDentifier (PID), which is pronounced pid (think of lid with a p instead of an l). The PID is simply a number associated with the process for easy identification.

In most cases, you debug an application by running it in the IDE in debug mode. However, there are some situations where you must debug the application in a different way — by attaching to its process. Attaching to the process means telling the CPU to send the instructions in the executable code to a debugger before they’re executed by the CPU. In other words, you place the debugger between the executable code and the CPU. Here are some of the most common reasons for performing this task:

  • The executable code is behaving differently in the debugger than it does when executed as a regular application.

  • Instead of working with a debug version, you want to debug the release version.

  • It’s important to see the disassembled code as it loads in memory.

  • You don’t actually have source code to load into the debugger and execute.

There are many other reasons to attach to a running process, but these are the most common reasons. Of course, before you can attach to the process, you need to know the PID. Determining the PID depends on the platform you’re using. Here are some common approaches:

  • Windows: Look at the Processes tab of the Windows Task Manager

  • Mac OS X: Use the PS utility in the Terminal window or the Activity Monitor

  • Linux: Use the PS utility in the Terminal window

Once you have a PID, you can use it to attach to the process in Code::Blocks. The following steps get you started.

  1. Open your copy of Code::Blocks using a technique appropriate for your operating system.

    You see the Code::Blocks IDE open with the Start Here tab opened.

    Code Blocks start screen.

  2. Choose Debug→Attach to Process

    The Input Text dialog box appears.

    The Input Text dialog box in Code Blocks

  3. Type the PID in the PID to Attach To field and then click OK.

    You see the Debugger tab of the Logs and Others window appear. This tab contains information about the current process. You can type commands in the Command field to perform debugging tasks.

    The debugger window in Code Blocks.

At this point, you can type commands in the Command field to perform debugging tasks. The most common commands are:

  • Break: Stops application execution so you can examine the application state.

  • Step: Steps one source line (which may be several assembly lines).

  • Continue: Restarts application execution.

  • Go: Continues application execution to a specific point in the code.

  • Detach: Detaches a previously attached PID so that you can safely shut the debugger down.

  • Help: Displays additional command information.