<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2001</year>
      <year>2022</year>
      <holder>Ericsson AB, All Rights Reserved</holder>
    </copyright>
    <legalnotice>
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  The Initial Developer of the Original Code is Ericsson AB.
    </legalnotice>

    <title>cover</title>
    <prepared></prepared>
    <docno></docno>
    <date></date>
    <rev></rev>
  </header>
  <module since="">cover</module>
  <modulesummary>A Coverage Analysis Tool for Erlang</modulesummary>
  <description>
    <p>The module <c>cover</c> provides a set of functions for coverage
      analysis of Erlang programs, counting how many times each
      <em>executable line</em> of code is executed when a program is run.      <br></br>

      An executable line contains an Erlang expression such as a matching
      or a function call. A blank line or a line containing a comment,
      function head or pattern in a <c>case</c>- or <c>receive</c> statement
      is not executable.</p>
    <p>Coverage analysis can be used to verify test cases, making sure all
      relevant code is covered, and may also be helpful when looking for
      bottlenecks in the code.</p>
    <p>Before any analysis can take place, the involved modules must be
      <em>Cover compiled</em>. This means that some extra information is
      added to the module before it is compiled into a binary which then
      is loaded. The source file of the module is not affected and no
      <c>.beam</c> file is created.</p>
    <p>Each time a function in a Cover compiled module is called,
      information about the call is added to an internal database of Cover.
      The coverage analysis is performed by examining the contents of
      the Cover database. The output <c>Answer</c> is determined by two
      parameters, <c>Level</c> and <c>Analysis</c>.</p>
    <list type="bulleted">
      <item>
        <p><c>Level = module</c></p>
        <p><c>Answer = {Module,Value}</c>, where <c>Module</c> is the module
          name.</p>
      </item>
      <item>
        <p><c>Level = function</c></p>
        <p><c>Answer = [{Function,Value}]</c>, one tuple for each function in
          the module. A function is specified by its module name <c>M</c>,
          function name <c>F</c> and arity <c>A</c> as a tuple
          <c>{M,F,A}</c>.</p>
      </item>
      <item>
        <p><c>Level = clause</c></p>
        <p><c>Answer = [{Clause,Value}]</c>, one tuple for each clause in
          the module. A clause is specified by its module name <c>M</c>,
          function name <c>F</c>, arity <c>A</c> and position in the function
          definition <c>C</c> as a tuple <c>{M,F,A,C}</c>.</p>
      </item>
      <item>
        <p><c>Level = line</c></p>
        <p><c>Answer = [{Line,Value}]</c>, one tuple for each executable
          line in the module. A line is specified by its module name <c>M</c>
          and line number in the source file <c>N</c> as a tuple
          <c>{M,N}</c>.</p>
      </item>
      <item>
        <p><c>Analysis = coverage</c></p>
        <p><c>Value = {Cov,NotCov}</c> where <c>Cov</c> is the number of
          executable lines in the module, function, clause or line that have
          been executed at least once and <c>NotCov</c> is the number of
          executable lines that have not been executed.</p>
      </item>
      <item>
        <p><c>Analysis = calls</c></p>
        <p><c>Value = Calls</c> which is the number of times the module,
          function, or clause has been called. In the case of line level
          analysis, <c>Calls</c> is the number of times the line has been
          executed.</p>
      </item>
    </list>
    <p><em>Distribution</em></p>
    <p>Cover can be used in a distributed Erlang system. One of the
      nodes in the system must then be selected as the <em>main node</em>, and all Cover commands must be executed from this
      node. The error reason <c>not_main_node</c> is returned if an
      interface function is called on one of the remote nodes.</p>
    <p>Use <c>cover:start/1</c> and <c>cover:stop/1</c> to add or
      remove nodes. The same Cover compiled code will be loaded on each
      node, and analysis will collect and sum up coverage data results
      from all nodes.</p>
    <p>To only collect data from remote nodes without stopping
      <c>cover</c> on those nodes, use <c>cover:flush/1</c></p>
    <p>If the connection to a remote node goes down, the main node
      will mark it as lost. If the node comes back it will be added
      again. If the remote node was alive during the disconnected
      period, cover data from before and during this period will be
      included in the analysis.</p>
  </description>
  <funcs>
    <func>
      <name name="start" arity="0" since=""></name>
      <fsummary>Start Cover.</fsummary>
      <desc>
        <p>Starts the Cover server which owns the Cover internal database.
          This function is called automatically by the other functions in
          the module.</p>
      </desc>
    </func>
    <func>
      <name name="local_only" arity="0" since="OTP 22.0"></name>
      <fsummary>Only support running Cover on the local node.</fsummary>
      <desc>
        <p>Only support running Cover on the local node. This function
        must be called before any modules have been compiled or any
        nodes added. When running in this mode, modules will be Cover
        compiled in a more efficient way, but the resulting code will
        only work on the same node they were compiled on.</p>
      </desc>
    </func>
    <func>
      <name name="start" arity="1" since=""></name>
      <fsummary>Start Cover on remote nodes.</fsummary>
      <desc>
        <p>Starts a Cover server on the each of given nodes, and loads
          all cover compiled modules. This call will fail if
          <c>cover:local_only/0</c> has been called.</p>
      </desc>
    </func>
    <func>
      <name name="compile" arity="1" since=""></name>
      <name name="compile" arity="2" since=""></name>
      <name name="compile_module" arity="1" since=""></name>
      <name name="compile_module" arity="2" since=""></name>
      <fsummary>Compile one or more modules for Cover analysis.</fsummary>
      <type name="mod_files"/>
      <type name="mod_file"/>
      <type name="option"/>
      <type_desc name="option">See <seemfa
        marker="compiler:compile#file/2"><c>compile:file/2</c></seemfa>.
      </type_desc>
      <type name="compile_result"/>
      <desc>
        <p>Compiles a module for Cover analysis. The module is given by its
          module name <c><anno>Module</anno></c> or by its file name
          <c><anno>File</anno></c>. The <c>.erl</c> extension may be
          omitted. If the module is located in another directory, the
          path has to be specified.</p>
        <p><c><anno>Options</anno></c> is a list of compiler options
          which defaults to <c>[]</c>. Only options defining include
          file directories and macros are passed to <seemfa
          marker="compiler:compile#file/2"><c>compile:file/2</c></seemfa>,
          everything else is ignored.</p>
        <p>If the module is successfully Cover compiled, the function
          returns <c>{ok, <anno>Module</anno>}</c>. Otherwise the
          function returns <c>{error, <anno>File</anno>}</c>. Errors and
          warnings are printed as they occur.</p>
        <p>If a list of <c><anno>ModFiles</anno></c> is given as
          input, a list of <c><anno>Result</anno></c> will be returned.
          The order of the returned list is undefined.</p>
        <p>Note that the internal database is (re-)initiated during
          the compilation, meaning any previously collected coverage data
          for the module will be lost.</p>
      </desc>
    </func>
    <func>
      <name name="compile_directory" arity="0" since=""></name>
      <name name="compile_directory" arity="1" since=""></name>
      <name name="compile_directory" arity="2" since=""></name>
      <fsummary>Compile all modules in a directory for Cover analysis.</fsummary>
      <type name="option"/>
      <type_desc name="option">See <seemfa
        marker="compiler:compile#file/2"><c>compile:file/2</c></seemfa>.
      </type_desc>
      <type name="file_error"/>
      <type name="compile_result"/>
      <desc>
        <p>Compiles all modules (<c>.erl</c> files) in a directory
          <c><anno>Dir</anno></c> for Cover analysis the same way as
          <seemfa marker="#compile_module/1"><c>compile_module/1,2</c></seemfa>
          and returns a list with the return values.</p>
        <p><c><anno>Dir</anno></c> defaults to the current working
          directory.</p>
        <p>The function returns <c>{error, eacces}</c> if the
          directory is not readable or <c>{error, enoent}</c> if the
          directory does not exist.</p>
      </desc>
    </func>
    <func>
      <name name="compile_beam" arity="1" since=""></name>
      <fsummary>Compile one or more modules for Cover analysis, using existing beam(s).</fsummary>
      <type name="beam_mod_files"/>
      <type name="beam_mod_file"/>
      <type name="compile_beam_result"/>
      <type name="compile_beam_rsn"/>
      <desc>
        <p>Does the same as <seemfa
          marker="#compile/1"><c>compile/1,2</c></seemfa>, but uses an
          existing <c>.beam</c> file as base, that is, the module is not
          compiled from source. Thus <c>compile_beam/1</c> is faster
          than <c>compile/1,2</c>.</p>
        <p>Note that the existing <c>.beam</c> file must contain
          <em>abstract code</em>, that is, it must have been compiled with the
          <seemfa marker="compiler:compile#file/2"><c>debug_info</c></seemfa>
          option. If not, the error reason
          <c>{no_abstract_code, <anno>BeamFile</anno>}</c> is returned.
          If the abstract code is encrypted, and no key is available
          for decrypting it, the error reason 
          <c>{encrypted_abstract_code, <anno>BeamFile</anno>}</c>
          is returned.</p>
         <p>If only the module name (that is, not the full name of the
          <c>.beam</c> file) is given to this function, the
          <c>.beam</c> file is found by calling
          <c>code:which(<anno>Module</anno>)</c>.
          If no <c>.beam</c> file is found,
          the error reason <c>non_existing</c> is returned. If the
          module is already cover compiled with <c>compile_beam/1</c>,
          the <c>.beam</c> file will be picked from the same location
          as the first time it was compiled. If the module is already
          cover compiled with <seemfa
          marker="#compile/1"><c>compile/1,2</c></seemfa>, there is no way to
          find the correct <c>.beam</c> file, so the error reason
          <c>{already_cover_compiled, no_beam_found, <anno>Module</anno>}</c> is
          returned.</p>
        <p><c>{error, <anno>BeamFile</anno>}</c> is returned if the compiled
          code cannot be loaded on the node.</p>
        <p>If a list of <c><anno>ModFiles</anno></c> is given as input, a list
          of <c><anno>Result</anno></c> will be returned. The order of
          the returned list is undefined.</p>
      </desc>
    </func>
    <func>
      <name name="compile_beam_directory" arity="0" since=""></name>
      <name name="compile_beam_directory" arity="1" since=""></name>
      <fsummary>Compile all .beam files in a directory for Cover analysis.</fsummary>
      <type name="compile_beam_result"/>
      <type name="compile_beam_rsn"/>
      <type name="file_error"/>
      <desc>
        <p>Compiles all modules (<c>.beam</c> files) in a directory
          <c><anno>Dir</anno></c> for Cover analysis the same way as
          <seemfa marker="#compile_beam/1"><c>compile_beam/1</c></seemfa>
          and returns a list with the return values.</p>
        <p><c><anno>Dir</anno></c> defaults to the current working directory.</p>
        <p>The function returns <c>{error, eacces}</c> if the directory is not
          readable or <c>{error, enoent}</c> if the directory does not exist.</p>
      </desc>
    </func>
    <func>
      <name name="analyse" arity="0" since="OTP 18.0"></name>
      <name name="analyse" arity="1" clause_i="1" since=""></name>
      <name name="analyse" arity="1" clause_i="2" since=""></name>
      <name name="analyse" arity="1" clause_i="3" since=""></name>
      <name name="analyse" arity="2" clause_i="1" since=""></name>
      <name name="analyse" arity="2" clause_i="2" since=""></name>
      <name name="analyse" arity="2" clause_i="3" since=""></name>
      <name name="analyse" arity="3" since=""></name>
      <fsummary>Analyse one or more Cover compiled modules.</fsummary>
      <type name="analysis"/>
      <type name="level"/>
      <type name="modules"/>
      <type name="one_result"/>
      <type name="analyse_fail"/>
      <type name="analyse_ok"/>
      <type name="analyse_value"/>
      <type name="analyse_item"/>
      <desc>
        <p>Performs analysis of one or more Cover compiled modules, as
          specified by <c><anno>Analysis</anno></c> and
          <c><anno>Level</anno></c> (see above), by examining the
          contents of the internal database.</p>
        <p><c><anno>Analysis</anno></c> defaults to <c>coverage</c>
          and <c><anno>Level</anno></c> defaults to <c>function</c>.</p>
        <p>If <c><anno>Modules</anno></c> is an atom (one module), the return will
          be <c><anno>OneResult</anno></c>, else the return will be
          <c>{result, Ok, Fail}</c>.</p>
        <p>If <c><anno>Modules</anno></c> is not given, all modules that have data
          in the cover data table, are analysed. Note that this
          includes both cover compiled modules and imported
          modules.</p>
        <p>If a given module is not Cover compiled, this is indicated
          by the error reason <c>{not_cover_compiled, <anno>Module</anno>}</c>.</p>
      </desc>
    </func>
    <func>
      <name name="analyse_to_file" arity="0" since="OTP 18.0"></name>
      <name name="analyse_to_file" arity="1" clause_i="1" since=""></name>
      <name name="analyse_to_file" arity="1" clause_i="2" since=""></name>
      <name name="analyse_to_file" arity="2" since=""></name>
      <fsummary>Detailed coverage analysis of one or more Cover compiled modules.</fsummary>
      <type name="modules"/>
      <type name="analyse_option"/>
      <type name="analyse_answer"/>
      <type name="analyse_file_ok"/>
      <type name="analyse_file_fail"/>
      <type name="analyse_rsn"/>
      <desc>
        <p>Makes copies of the source file for the given modules,
          where it for each executable line is specified
          how many times it has been executed.</p>
        <p>The output file <c><anno>OutFile</anno></c> defaults to
          <c><anno>Module</anno>.COVER.out</c>, or
          <c><anno>Module</anno>.COVER.html</c> if the option
          <c>html</c> was used.</p>
        <p>If <c><anno>Modules</anno></c> is an atom (one module), the
          return will be <c><anno>Answer</anno></c>, else the return
          will be a list, <c>{result, Ok, Fail}</c>.</p>
        <p>If <c><anno>Modules</anno></c> is not given, all modules
          that have da ta in the cover data table, are analysed. Note
          that this includes both cover compiled modules and imported
          modules.</p>
        <p>If a module is not Cover compiled, this is indicated by the
          error reason <c>{not_cover_compiled, <anno>Module</anno>}</c>.</p>
        <p>If the source file and/or the output file cannot be opened using
          <seemfa marker="kernel:file#open/2"><c>file:open/2</c></seemfa>,
          the function returns
          <c>{error, {file, <anno>File</anno>, <anno>Reason</anno>}}</c>
          where <c><anno>File</anno></c> is the file
          name and <c><anno>Reason</anno></c> is the error reason.</p>
        <p>If a module was cover compiled from the <c>.beam</c>
          file, that is, using <seemfa
          marker="#compile_beam/1"><c>compile_beam/1</c></seemfa> or
          <seemfa
          marker="#compile_beam_directory/0"><c>compile_beam_directory/0,1</c>
          </seemfa>,it is assumed that the
          source code can be found in the same directory as the
          <c>.beam</c> file, in <c>../src</c> relative to that
          directory, or using the source path in
          <c><anno>Module</anno>:module_info(compile)</c>. When using the latter,
          two paths are examined: first the one constructed by
          joining <c>../src</c> and the tail of the compiled path
          below a trailing <c>src</c> component, then the compiled
          path itself.
          If no source code is found, this is indicated by the error reason
          <c>{no_source_code_found, <anno>Module</anno>}</c>.</p>
      </desc>
    </func>
    <func>
      <name name="async_analyse_to_file" arity="1" since="OTP R14B02"></name>
      <name name="async_analyse_to_file" arity="2"
            clause_i="1" since="OTP R14B02"></name>
      <name name="async_analyse_to_file" arity="2"
            clause_i="2" since="OTP R14B02"></name>
      <name name="async_analyse_to_file" arity="3" since="OTP R14B02"></name>
      <fsummary>Asynchronous call to analyse_to_file.</fsummary>
      <type name="analyse_rsn"/>
      <desc>
        <p>This function works exactly the same way as 
        <seemfa marker="#analyse_to_file/1"><c>analyse_to_file</c></seemfa> except
        that it is asynchronous instead of synchronous. The spawned process
        will link with the caller when created. If an error of type
        <c>analyse_rsn()</c> occurs
        while doing the cover analysis the process will crash with the same
        error reason as <seemfa marker="#analyse_to_file/1"><c>analyse_to_file</c></seemfa> 
        would return.</p>
      </desc>
    </func>
    <func>
      <name name="modules" arity="0" since=""></name>
      <fsummary>Return all Cover compiled modules.</fsummary>
      <desc>
        <p>Returns a list with all modules that are currently Cover
          compiled.</p>
      </desc>
    </func>
    <func>
      <name name="imported_modules" arity="0" since=""></name>
      <fsummary>Return all modules for which there are imported data.</fsummary>
      <desc>
        <p>Returns a list with all modules for which there are
          imported data.</p>
      </desc>
    </func>
    <func>
      <name name="imported" arity="0" since=""></name>
      <fsummary>Return all imported files.</fsummary>
      <desc>
        <p>Returns a list with all imported files.</p>
      </desc>
    </func>
    <func>
      <name name="which_nodes" arity="0" since=""></name>
      <fsummary>Return all nodes that are part of the coverage analysis.</fsummary>
      <desc>
        <p>Returns a list with all nodes that are part of the coverage
          analysis. Note that the current node is not returned. This
          node is always part of the analysis.</p>
      </desc>
    </func>
    <func>
      <name name="is_compiled" arity="1" since=""></name>
      <fsummary>Check if a module is Cover compiled.</fsummary>
      <desc>
        <p>Returns <c>{file, <anno>File</anno>}</c> if the module
          <c><anno>Module</anno></c> is
          Cover compiled, or <c>false</c> otherwise. <c><anno>File</anno></c> is
          the <c>.erl</c> file used by
          <seemfa marker="#compile_module/1"><c>compile_module/1,2</c></seemfa>
          or the <c>.beam</c> file used by
          <seemfa marker="#compile_beam/1"><c>compile_beam/1</c></seemfa>.</p>
      </desc>
    </func>
    <func>
      <name name="reset" arity="0" since=""></name>
      <name name="reset" arity="1" since=""></name>
      <fsummary>Reset coverage data for Cover compiled modules.</fsummary>
      <desc>
        <p>Resets all coverage data for a Cover compiled module
          <c><anno>Module</anno></c> in the Cover database on all nodes. If the
          argument is omitted, the coverage data will be reset for all
          modules known by Cover.</p>
        <p>If <c><anno>Module</anno></c> is not Cover compiled, the function returns
          <c>{error, {not_cover_compiled, <anno>Module</anno>}}</c>.</p>
      </desc>
    </func>
    <func>
      <name name="export" arity="1" since=""></name>
      <name name="export" arity="2" since=""></name>
      <fsummary>Reset coverage data for Cover compiled modules.</fsummary>
      <type name="export_reason"/>
      <desc>
        <p>Exports the current coverage data for <c><anno>Module</anno></c> to the
          file <c><anno>ExportFile</anno></c>. It is recommended to name the
          <c><anno>ExportFile</anno></c> with the extension <c>.coverdata</c>, since
          other filenames cannot be read by the web based interface to
          cover.</p>
        <p>If <c><anno>Module</anno></c> is not given, data for all Cover compiled
          or earlier imported modules is exported.</p>
        <p>This function is useful if coverage data from different
          systems is to be merged.</p>
        <p>See also <seemfa marker="#import/1"><c>import/1</c></seemfa>.</p>
      </desc>
    </func>
    <func>
      <name name="import" arity="1" since=""></name>
      <fsummary>Reset coverage data for Cover compiled modules.</fsummary>
      <desc>
        <p>Imports coverage data from the file <c><anno>ExportFile</anno></c>
          created with <seemfa marker="#export/1"><c>export/1,2</c></seemfa>.
          Any analysis performed
          after this will include the imported data.</p>
        <p>Note that when compiling a module <em>all existing coverage data is removed</em>, including imported data. If a module is
          already compiled when data is imported, the imported data is
          <em>added</em> to the existing coverage data.</p>
        <p>Coverage data from several export files can be imported
          into one system. The coverage data is then added up when
          analysing.</p>
        <p>Coverage data for a module cannot be imported from the
          same file twice unless the module is first reset or
          compiled. The check is based on the filename, so you can
          easily fool the system by renaming your export file.</p>
        <p>See also <seemfa marker="#export/1"><c>export/1,2</c></seemfa>.</p>
      </desc>
    </func>
    <func>
      <name name="stop" arity="0" since=""></name>
      <fsummary>Stop Cover.</fsummary>
      <desc>
        <p>Stops the Cover server and unloads all Cover compiled code.</p>
      </desc>
    </func>
    <func>
      <name name="stop" arity="1" since=""></name>
      <fsummary>Stop Cover on remote nodes.</fsummary>
      <desc>
        <p>Stops the Cover server and unloads all Cover compiled code
          on the given nodes. Data stored in the Cover database on the
          remote nodes is fetched and stored on the main node.</p>
      </desc>
    </func>
    <func>
      <name name="flush" arity="1" since="OTP R16B"></name>
      <fsummary>Collect cover data from remote nodes.</fsummary>
      <desc>
        <p>Fetch data from the Cover database on the remote nodes and
        stored on the main node.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>SEE ALSO</title>
    <p>code(3), compile(3)</p>
  </section>
</erlref>

