Attaching to a Running Process Using Code::Blocks | Each time you start a new application, you create one or more processes. A process is simply an executable code that is loaded into memory. The CPU reads and executes instructions to perform the tasks you are asking 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 the cap with p instead of l). PID is simply a number associated with the process for easy identification.
Attaching to a Running Process Using Code::Blocks
In most cases, you can debug an application by running it in the IDE in debug mode. However, there are some situations where you have to debug the application in a different way – by attaching it to its process. Attaching to a process means telling the CPU to send the instructions in the executable code to the debugger before they are executed by the CPU. In other words, you can place the debugger between the executable code and the CPU. Here are some of the most common reasons to perform this task:
The debugger’s executable code behaves differently than it does when executed as a normal application.
Instead of working with a patch version, you want to patch the release version.
It is important to see the disassembled code while it is being loaded into memory.
You don’t have to actually have source code to load into the debugger and execute.
There are many other reasons for attaching to a running process, but these are the most common. Of course, before you can attach the process, you need to know the PID. Determining the PID depends on the platform you are using. Here are some common ways:
Windows: Look at the Processes tab in the Windows Task Manager
Mac OS X: Use the PS utility in the Terminal window or Activity Monitor
Linux: Use the PS tool in the terminal window
Once you have the PID, you can use it to attach to the process in Code::Blocks. The following steps get you started.
- Unlock your copy of Code::Blocks using technology appropriate for your operating system.
- Choose Debug→Attach to Process
- 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.
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.