3.4.4 What Do I Need to Do When Compiling to Use a Debugger?

You can use debuggers, such as the MPLAB ICD 5 or PICkit 5, to debug code built with the MPLAB XC8 compiler. These debuggers might use some of the data and program device memory for its own use. If this is the case, it is important that your code does not also use these resources.

In the MPLAB X IDE, the appropriate memory is reserved when you perform a debug build. All the device memory will be available when you perform a regular Build Project or Clean and Build. If you are building on the command-line, then you must communicate to the compiler that you will be using a debugger. The compiler’s -mdebugger option will do this. (See 4.6.1.6 Debugger Option.)

The ReservedResourcesByDeviceFamilyAndTool.html file in the MPLAB X IDE’s docs/ReservedResources directory lets you check the memory and other resources used by each device for different debugger tools and that will be reserved by the -mdebugger option.

When device memory is being used by a debugger, there is less memory available for your program and it is possible that your code or data might not fit in the device when a debugger is selected. If you are only using a debugger tool as a programmer, then you do not need to use the -mdebugger option, as no device memory will be required by the tool.

To verify that the resources reserved by the compiler match those required by the debugger, do the following. Compile your code with and without the debugger selected and keep a copy of the map file produced for both builds. Compare the linker options in the map files and look for changes in the -A options (see 7.1.1 A: Define Linker Class Option). For example, the memory defined for the CODE class with no debugger might be specified by this option:
-ACODE=00h-0FFh,0100h-07FFh,0800h-0FFFhx3
and with the ICD 3 selected as the debugger, it becomes:
-ACODE=00h-0FFh,0100h-07FFh,0800h-0FFFhx2,01800h-01EFFh

This shows that a memory range from 1F00 to 1FFF has been removed by the compiler and cannot be used by your program (See also 3.5.16 Why Are Some Objects Positioned Into Memory That I Reserved?).