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](http://code.google.com/p/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
1. Choose the Project Settings
1. Choose the Target which is generating test coverage (may be more than one, both app target and test targets for example)
1. From the top tabs, choose the Build Phases tab
1. In the bottom right of the pane, choose the Add Build Phase > Add Run Script
1. In the script pane, enter the shell script shown below
1. Leave the other options as their defaults.
1. 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:

#!bash
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](http://finalize.com/2013/04/08/setting-up-for-unit-testing/) 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.

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

    1. 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.

  1. 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

Leave a Reply to DerWOK Cancel reply

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