Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 5 | <title>scan-build: running the analyzer from the command line</title> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 6 | <link type="text/css" rel="stylesheet" href="content.css" /> |
Ted Kremenek | fe77538 | 2010-02-09 23:09:48 +0000 | [diff] [blame] | 7 | <link type="text/css" rel="stylesheet" href="menu.css" /> |
Ted Kremenek | f4aed5f | 2010-02-12 21:05:44 +0000 | [diff] [blame] | 8 | <script type="text/javascript" src="scripts/menu.js"></script> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 9 | </head> |
| 10 | <body> |
| 11 | |
Ted Kremenek | 8bebc6e | 2010-02-09 23:05:59 +0000 | [diff] [blame] | 12 | <div id="page"> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 13 | <!--#include virtual="menu.html.incl"--> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 14 | <div id="content"> |
| 15 | |
Ted Kremenek | 90fc45f | 2010-02-09 23:10:47 +0000 | [diff] [blame] | 16 | <style> |
Ted Kremenek | c49d5d5 | 2010-02-09 23:32:46 +0000 | [diff] [blame] | 17 | table.options thead { |
| 18 | background-color:#eee; color:#666666; |
| 19 | font-weight: bold; cursor: default; |
| 20 | text-align:left; |
| 21 | border-top: 2px solid #cccccc; |
| 22 | border-bottom: 2px solid #cccccc; |
| 23 | font-weight: bold; font-family: Verdana |
| 24 | } |
| 25 | table.options { border: 1px #cccccc solid } |
| 26 | table.options { border-collapse: collapse; border-spacing: 0px } |
| 27 | table.options { margin-left:0px; margin-top:20px; margin-bottom:20px } |
| 28 | table.options td { border-bottom: 1px #cccccc dotted } |
| 29 | table.options td { padding:5px; padding-left:8px; padding-right:8px } |
| 30 | table.options td { text-align:left; font-size:9pt } |
Ted Kremenek | 90fc45f | 2010-02-09 23:10:47 +0000 | [diff] [blame] | 31 | </style> |
| 32 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 33 | <h1>scan-build: running the analyzer from the command line</h1> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 35 | <table style="margin-top:0px" width="100%" border="0" cellpadding="0px" cellspacing="0"> |
| 36 | <tr><td> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 38 | <h3>What is it?</h3> |
| 39 | <p><b>scan-build</b> is a command line utility that enables a user to run the |
| 40 | static analyzer over their codebase as part of performing a regular build (from |
| 41 | the command line).</p> |
| 42 | |
| 43 | <h3>How does it work?</h3> |
| 44 | <p>During a project build, as source files are compiled they are also analyzed |
| 45 | in tandem by the static analyzer.</p> |
| 46 | |
| 47 | <p>Upon completion of the build, results are then presented to the user within a |
| 48 | web browser.</p> |
| 49 | |
| 50 | <h3>Will it work with any build system?</h3> |
| 51 | <p><b>scan-build</b> has little or no knowledge about how you build your code. |
| 52 | It works by overriding the <tt>CC</tt> and <tt>CXX</tt> environment variables to |
| 53 | (hopefully) change your build to use a "fake" compiler instead of the |
| 54 | one that would normally build your project. By default, this fake compiler |
| 55 | executes <tt>gcc</tt> to compile your code (assuming that <tt>gcc</tt> is your |
| 56 | compiler) and then executes the static analyzer to analyze your code.</p> |
| 57 | |
| 58 | <p>This "poor man's interposition" works amazingly well in many cases |
| 59 | and falls down in others. Please consult the information on this page on making |
| 60 | the best use of <b>scan-build</b>, which includes getting it to work when the |
| 61 | aforementioned hack fails to work.</p> |
| 62 | |
| 63 | </td> |
| 64 | <td style="padding-left:10px"> |
| 65 | <center> |
| 66 | <img src="images/scan_build_cmd.png" width="450px" border=0><br> |
| 67 | <a href="images/analyzer_html.png"><img src="images/analyzer_html.png" width="450px" border=0></a> |
| 68 | <br><b>Viewing static analyzer results in a web browser</b></center> |
| 69 | </td></tr></table> |
| 70 | |
| 71 | <h2>Contents</h2> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 72 | |
| 73 | <ul> |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 74 | <li><a href="#scanbuild">Getting Started</a></li> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 75 | <ul> |
| 76 | <li><a href="#scanbuild_basicusage">Basic Usage</a></li> |
| 77 | <li><a href="#scanbuild_otheroptions">Other Options</a></li> |
| 78 | <li><a href="#scanbuild_output">Output of scan-build</a></li> |
| 79 | </ul> |
| 80 | <li><a href="#recommendedguidelines">Recommended Usage Guidelines</a></li> |
| 81 | <ul> |
| 82 | <li><a href="#recommended_debug">Always Analyze a Project in its "Debug" Configuration</a></li> |
| 83 | <li><a href="#recommended_verbose">Use Verbose Output when Debugging scan-build</a></li> |
| 84 | <li><a href="#recommended_autoconf">Run './configure' through scan-build</a></li> |
| 85 | </ul> |
| 86 | </ul> |
| 87 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 88 | <h2 id="scanbuild">Getting Started</h2> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 89 | |
| 90 | <p>The <tt>scan-build</tt> command can be used to analyze an entire project by |
| 91 | essentially interposing on a project's build process. This means that to run the |
| 92 | analyzer using <tt>scan-build</tt>, you will use <tt>scan-build</tt> to analyze |
| 93 | the source files compiled by <tt>gcc</tt> during a project build. This means |
| 94 | that any files that are not compiled will also not be analyzed.</p> |
| 95 | |
| 96 | <h3 id="scanbuild_basicusage">Basic Usage</h3> |
| 97 | |
| 98 | <p>Basic usage of <tt>scan-build</tt> is designed to be simple: just place the |
| 99 | word "scan-build" in front of your build command:</p> |
| 100 | |
| 101 | <pre class="code_example"> |
| 102 | $ <span class="code_highlight">scan-build</span> make |
| 103 | $ <span class="code_highlight">scan-build</span> xcodebuild |
| 104 | </pre> |
| 105 | |
| 106 | <p>In the first case <tt>scan-build</tt> analyzes the code of a project built |
| 107 | with <tt>make</tt> and in the second case <tt>scan-build</tt> analyzes a project |
| 108 | built using <tt>xcodebuild</tt>.<p> |
| 109 | |
| 110 | <p>Here is the general format for invoking <tt>scan-build</tt>:</p> |
| 111 | |
| 112 | <pre class="code_example"> |
| 113 | $ <span class="code_highlight">scan-build</span> <i>[scan-build options]</i> <span class="code_highlight"><command></span> <i>[command options]</i> |
| 114 | </pre> |
| 115 | |
Duncan Sands | 763f7d6 | 2010-01-20 12:40:56 +0000 | [diff] [blame] | 116 | <p>Operationally, <tt>scan-build</tt> literally runs <command> with all of the |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 117 | subsequent options passed to it. For example, one can pass <nobr><tt>-j4</tt></nobr> to |
| 118 | <tt>make</tt> get a parallel build over 4 cores:</p> |
| 119 | |
| 120 | <pre class="code_example"> |
| 121 | $ scan-build make <span class="code_highlight">-j4</span> |
| 122 | </pre> |
| 123 | |
| 124 | <p>In almost all cases, <tt>scan-build</tt> makes no effort to interpret the |
| 125 | options after the build command; it simply passes them through. In general, |
| 126 | <tt>scan-build</tt> should support parallel builds, but <b>not distributed |
| 127 | builds</b>.</p> |
| 128 | |
| 129 | <p>It is also possible to use <tt>scan-build</tt> to analyze specific |
| 130 | files:</p> |
| 131 | |
| 132 | <pre class="code_example"> |
| 133 | $ scan-build gcc -c <span class="code_highlight">t1.c t2.c</span> |
| 134 | </pre> |
| 135 | |
| 136 | <p>This example causes the files <tt>t1.c</tt> and <tt>t2.c</tt> to be analyzed. |
| 137 | </p> |
| 138 | |
| 139 | <h3 id="scanbuild_otheroptions">Other Options</h3> |
| 140 | |
| 141 | <p>As mentioned above, extra options can be passed to <tt>scan-build</tt>. These |
| 142 | options prefix the build command. For example:</p> |
| 143 | |
| 144 | <pre class="code_example"> |
| 145 | $ scan-build <span class="code_highlight">-k -V</span> make |
| 146 | $ scan-build <span class="code_highlight">-k -V</span> xcodebuild |
| 147 | </pre> |
| 148 | |
| 149 | <p>Here is a subset of useful options:</p> |
| 150 | |
Ted Kremenek | c49d5d5 | 2010-02-09 23:32:46 +0000 | [diff] [blame] | 151 | <table class="options"> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 152 | <thead><tr><td>Option</td><td>Description</td></tr></thead> |
| 153 | |
| 154 | <tr><td><b>-o</b></td><td>Target directory for HTML report files. Subdirectories |
| 155 | will be created as needed to represent separate "runs" of the analyzer. If this |
| 156 | option is not specified, a directory is created in <tt>/tmp</tt> to store the |
| 157 | reports.</td><tr> |
| 158 | |
| 159 | <tr><td><b>-h</b><br><i><nobr>(or no arguments)</nobr></i></td><td>Display all |
| 160 | <tt>scan-build</tt> options.</td></tr> |
| 161 | |
| 162 | <tr><td><b>-k</b><br><nobr><b>--keep-going</b></nobr></td><td>Add a "keep on |
| 163 | going" option to the specified build command. <p>This option currently supports |
| 164 | <tt>make</tt> and <tt>xcodebuild</tt>.</p> <p>This is a convenience option; one |
| 165 | can specify this behavior directly using build options.</p></td></tr> |
| 166 | |
| 167 | <tr><td><b>-v<b></td><td>Verbose output from scan-build and the analyzer. <b>A |
| 168 | second and third "-v" increases verbosity</b>, and is useful for filing bug |
| 169 | reports against the analyzer.</td></tr> |
| 170 | |
| 171 | <tr><td><b>-V</b></td><td>View analysis results in a web browser when the build |
| 172 | command completes.</td></tr> </table> |
| 173 | |
| 174 | <p>A complete list of options can be obtained by running <tt>scan-build</tt> |
| 175 | with no arguments.</p> |
| 176 | |
| 177 | <h3 id="scanbuild_output">Output of scan-build</h3> |
| 178 | |
| 179 | <p> |
| 180 | The output of scan-build is a set of HTML files, each one which represents a |
| 181 | separate bug report. A single <tt>index.html</tt> file is generated for |
| 182 | surveying all of the bugs. You can then just open <tt>index.html</tt> in a web |
| 183 | browser to view the bug reports. |
| 184 | </p> |
| 185 | |
| 186 | <p> |
| 187 | Where the HTML files are generated is specified with a <b>-o</b> option to |
| 188 | <tt>scan-build</tt>. If <b>-o</b> isn't specified, a directory in <tt>/tmp</tt> |
| 189 | is created to store the files (<tt>scan-build</tt> will print a message telling |
| 190 | you where they are). If you want to view the reports immediately after the build |
| 191 | completes, pass <b>-V</b> to <tt>scan-build</tt>. |
| 192 | </p> |
| 193 | |
| 194 | |
| 195 | <h2 id="recommendedguidelines">Recommended Usage Guidelines</h2> |
| 196 | |
| 197 | <p>This section describes a few recommendations with running the analyzer.</p> |
| 198 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 199 | <h3 id="recommended_debug">ALWAYS analyze a project in its "debug" configuration</h3> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 200 | |
| 201 | <p>Most projects can be built in a "debug" mode that enables assertions. |
| 202 | Assertions are picked up by the static analyzer to prune infeasible paths, which |
| 203 | in some cases can greatly reduce the number of false positives (bogus error |
| 204 | reports) emitted by the tool.</p> |
| 205 | |
Ted Kremenek | 831a46a | 2010-02-19 05:36:44 +0000 | [diff] [blame] | 206 | <h3 id="recommend_verbose">Use verbose output when debugging scan-build</h3> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 207 | |
| 208 | <p><tt>scan-build</tt> takes a <b>-v</b> option to emit verbose output about |
| 209 | what it's doing; two <b>-v</b> options emit more information. Redirecting the |
| 210 | output of <tt>scan-build</tt> to a text file (make sure to redirect standard |
| 211 | error) is useful for filing bug reports against <tt>scan-build</tt> or the |
| 212 | analyzer, as we can see the exact options (and files) passed to the analyzer. |
| 213 | For more comprehensible logs, don't perform a parallel build.</p> |
| 214 | |
| 215 | <h3 id="recommended_autoconf">Run './configure' through scan-build</h3> |
| 216 | |
| 217 | <p>If an analyzed project uses an autoconf generated <tt>configure</tt> script, |
| 218 | you will probably need to run <tt>configure</tt> script through |
| 219 | <tt>scan-build</tt> in order to analyze the project.</p> |
| 220 | |
| 221 | <p><b>Example</b></p> |
| 222 | |
| 223 | <pre class="code_example"> |
| 224 | $ scan-build ./configure |
| 225 | $ scan-build make |
| 226 | </pre> |
| 227 | |
| 228 | <p>The reason <tt>configure</tt> also needs to be run through |
| 229 | <tt>scan-build</tt> is because <tt>scan-build</tt> scans your source files by |
| 230 | <i>interposing</i> on the compiler. This interposition is currently done by |
| 231 | <tt>scan-build</tt> temporarily setting the environment variable <tt>CC</tt> to |
| 232 | <tt>ccc-analyzer</tt>. The program <tt>ccc-analyzer</tt> acts like a fake |
| 233 | compiler, forwarding its command line arguments over to <tt>gcc</tt> to perform |
| 234 | regular compilation and <tt>clang</tt> to perform static analysis.</p> |
| 235 | |
| 236 | <p>Running <tt>configure</tt> typically generates makefiles that have hardwired |
| 237 | paths to the compiler, and by running <tt>configure</tt> through |
| 238 | <tt>scan-build</tt> that path is set to <tt>ccc-analyzer</tt>.</p.> |
| 239 | |
| 240 | <!-- |
| 241 | <h2 id="Debugging">Debugging the Analyzer</h2> |
| 242 | |
| 243 | <p>This section provides information on debugging the analyzer, and troubleshooting |
| 244 | it when you have problems analyzing a particular project.</p> |
| 245 | |
| 246 | <h3>How it Works</h3> |
| 247 | |
| 248 | <p>To analyze a project, <tt>scan-build</tt> simply sets the environment variable |
| 249 | <tt>CC</tt> to the full path to <tt>ccc-analyzer</tt>. It also sets a few other |
| 250 | environment variables to communicate to <tt>ccc-analyzer</tt> where to dump HTML |
| 251 | report files.</p> |
| 252 | |
| 253 | <p>Some Makefiles (or equivalent project files) hardcode the compiler; for such |
| 254 | projects simply overriding <tt>CC</tt> won't cause <tt>ccc-analyzer</tt> to be |
| 255 | called. This will cause the compiled code <b>to not be analyzed.</b></p> If you |
| 256 | find that your code isn't being analyzed, check to see if <tt>CC</tt> is |
| 257 | hardcoded. If this is the case, you can hardcode it instead to the <b>full |
| 258 | path</b> to <tt>ccc-analyzer</tt>.</p> |
| 259 | |
| 260 | <p>When applicable, you can also run <tt>./configure</tt> for a project through |
| 261 | <tt>scan-build</tt> so that configure sets up the location of <tt>CC</tt> based |
| 262 | on the environment passed in from <tt>scan-build</tt>: |
| 263 | |
| 264 | <pre> |
| 265 | $ scan-build <b>./configure</b> |
| 266 | </pre> |
| 267 | |
| 268 | <p><tt>scan-build</tt> has special knowledge about <tt>configure</tt>, so it in |
| 269 | most cases will not actually analyze the configure tests run by |
| 270 | <tt>configure</tt>.</p> |
| 271 | |
| 272 | <p>Under the hood, <tt>ccc-analyzer</tt> directly invokes <tt>gcc</tt> to |
| 273 | compile the actual code in addition to running the analyzer (which occurs by it |
| 274 | calling <tt>clang</tt>). <tt>ccc-analyzer</tt> tries to correctly forward all |
| 275 | the arguments over to <tt>gcc</tt>, but this may not work perfectly (please |
| 276 | report bugs of this kind). |
| 277 | --> |
| 278 | |
| 279 | </div> |
Ted Kremenek | 8bebc6e | 2010-02-09 23:05:59 +0000 | [diff] [blame] | 280 | </div> |
Ted Kremenek | 591b907 | 2009-06-08 21:21:24 +0000 | [diff] [blame] | 281 | </body> |
| 282 | </html> |
| 283 | |