How to Fix ‘Profiling: Invalid Magic Number’ in Xcode 4.6 When ‘Generate Test Coverage Files’ is enabled

The Problem

You are:

  1. Running Xcode 4.6
  2. Have ‘Generate Test Coverage Files’ enabled in your build settings
  3. Getting ‘Profiling: invalid magic number’ and other profiler related messages at the end of a run or unit test run

The Explanation

The build tools are trying to merge the run results into the *.gcda files that are located in the ${OBJECT_FILE_DIR_normal} output directory. You are probably using a code analysis tool on these files, such as CoverStory, and as a result the analysis tool will show incomplete and missing coverage results.

The simple fix is to do a Product > Clean in order to clear out the intermediate files, then re-run the tests, but this can become very tedious. The ‘magic number’ error suggests that the build tools don’t like something about the existing files when it tries to merge the results, and as a result fails to add some of the result detail.

In order to make this problem go away, I’ve found that a solution is to add a build script that cleans out the gcda files on every build, meaning that the build tools don’t have to do a merge. So far I’ve not found no downside to this.

The Workaround

To create a build script to clear out the Test Coverage files,

  1. Open up your project or workspace
  2. Choose the Project Settings
  3. Choose the Target which is generating test coverage (may be more than one, both app target and test targets for example)
  4. From the top tabs, choose the Build Phases tab
  5. In the bottom right of the pane, choose the Add Build Phase > Add Run Script
  6. In the script pane, enter the shell script shown below
  7. Leave the other options as their defaults.
  8. Drag the Run Script phase (grab the header of the script) that you’ve just created up the order so that it appears below the Target Dependencies phase.

Here is the shell script:

echo "Cleaning Profiler Information"
cd "${OBJECT_FILE_DIR_normal}/${CURRENT_ARCH}"
# Delete *.gcda files in the current target
rm -f *.gcda

And here is what it looks like in Xcode.

Build Phase Settings with a Run Script to clear Test Coverage files at the start of the build.

Build Phase Settings with a Run Script to clear Test Coverage files at the start of the build.

When you now run your app or unit tests, the test coverage files will be removed (rm -f) from the output directory, and the project should build, run and merge the coverage results without producing the error messages.

Updated 09/04/2013 Thanks to this response to my post I realised why the error message was creeping back in – I was only clearing the gcda files from the Test Target directories, and not from the Application Target as well. I simplified the script and you now need, as noted above, to insert this script into both app and test targets. As noted in one of the comments, the IFS setting should not be needed in the script as the path is quoted so spaces should not be an issue.




6 thoughts on “How to Fix ‘Profiling: Invalid Magic Number’ in Xcode 4.6 When ‘Generate Test Coverage Files’ is enabled

    1. Dave Post author

      Good think Maciek.

      Following up on this post, I’ve found that I still occasionally get the same error messages and the coverage analysis is missing details. There must be other files than the gcno and gcda ones that are part of the data, but have yet to discover which ones they are.

      Reply
  1. Pingback: Setting up for Unit Testing | Finalize.com: My journey with iOS and other code adventures

  2. Hans

    Thanks for the explanation and suggested fix! I hate it when I get cryptic warnings that leaves me clueless.

    Hope the motorhome adventure is treating you well!

    Regards, /Hans

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *