< Previous PageNext Page > Hide TOC

Using Fix and Continue

Fix and Continue makes it possible to modify an executable image at debug time. You can see the results of your modification without interrupting or restarting the debugging session.

This feature is especially useful when it takes a lot of time to reach a particular state of execution while debugging your application. Rather than recompiling your project and starting a new debugging session, you can make minor changes to your code, fix your executable image, and see the results of your changes immediately. For detailed information on Fix and Continue, see Modifying Running Code in Xcode Debugging Guide.

This tutorial uses a sample project named Sketch, which implements a simple drawing program built using the Cocoa framework. You’re going to change the way Sketch draws shapes by changing the source code and using the Fix command in Xcode to patch the running program.

You don’t need to be familiar with Objective-C to work through this tutorial.

In this section:

Configuring the Project
Patching the Running Application
Summary


Configuring the Project

To configure the Sketch project to use the Fix command:

  1. Make a copy of the Sketch project, which is located in /Developer/Examples/AppKit. To avoid confusion, place your copy somewhere inside your home directory.

  2. In the Finder, drag your copy of the Sketch project folder onto the Xcode icon in the Dock. Notice that Xcode finds and opens the project file inside this folder.

  3. Make sure the active build configuration is Debug. From the Project menu, choose Set Active Build Configuration > Debug.

  4. From the Build menu, choose Build (Command-B) to build the target.

Patching the Running Application

  1. To start a debugging session, choose Run > Debug (Option–Command-Y).

  2. Click in the Sketch drawing window, select a drawing tool (rectangle or circle), and draw a few objects.

    Figure 4-1  The default drawing behavior of Sketch

    The default drawing behavior of Sketch
  3. Return to the project window and select the Sketch group.

  4. Open the source file named SKTGraphic.m.

  5. In the source editor, locate the -init method. It looks something like this:

    - (id)init {
         self = [super init];
         if (self) {
             _bounds = NSZeroRect;
             _isDrawingFill = NO;
             _fillColor = [[NSColor whiteColor] retain];
             _isDrawingStroke = YES;
             _strokeColor = [[NSColor blackColor] retain];
             _strokeWidth = 1.0f;
         }
         return self;
    }
  6. Change some of the values.

    For example, change the following lines:

             _isDrawingFill = NO;
             _fillColor = [[NSColor whiteColor] retain];
             _strokeWidth = 1.0f;

    to:

             _isDrawingFill = YES;
             _fillColor = [[NSColor yellowColor] retain];
             _strokeWidth = 8.0f;
  7. Save your changes.

  8. Choose Run > Fix to modify the running program.

  9. Return to the Sketch drawing window and draw some more objects.

    You should see a dramatic change in the appearance of these new objects, as illustrated in Figure 4-2.

    Figure 4-2  The new drawing behavior of Sketch

    The new drawing behavior of Sketch

Summary

This tutorial showed how to use Fix and Continue to patch changes into a running application.



< Previous PageNext Page > Hide TOC


© 2003, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.