The Problem

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,

Open up your project or workspace

Choose the Project Settings

Choose the Target which is generating test coverage (may be more than one, both app target and test targets for example)

From the top tabs, choose the Build Phases tab

In the bottom right of the pane, choose the Add Build Phase > Add Run Script

In the script pane, enter the shell script shown below

Leave the other options as their defaults.

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.

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.

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.