<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Using the Linux Kernel Tracepoints — The Linux Kernel documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=3918102e" />
<script src="../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Notes on Analysing Behaviour Using Events and Tracepoints" href="tracepoint-analysis.html" />
<link rel="prev" title="Using the tracer for debugging" href="debugging.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
</head><body>
<div class="document">
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/logo.svg" alt="Logo of The Linux Kernel"/>
</a></p>
<h1 class="logo"><a href="../index.html">The Linux Kernel</a></h1>
<p class="blurb">6.18.50</p>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<p>
<h3 class="kernel-toc-contents">Contents</h3>
<input type="checkbox" class="kernel-toc-toggle" id = "kernel-toc-toggle" checked>
<label class="kernel-toc-title" for="kernel-toc-toggle"></label>
<div class="kerneltoc" id="kerneltoc">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../process/development-process.html">Development process</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/submitting-patches.html">Submitting patches</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/code-of-conduct.html">Code of conduct</a></li>
<li class="toctree-l1"><a class="reference internal" href="../maintainer/index.html">Maintainer handbook</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/index.html">All development-process docs</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../core-api/index.html">Core API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../driver-api/index.html">Driver APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../subsystem-apis.html">Subsystems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../locking/index.html">Locking</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../process/license-rules.html">Licensing rules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../doc-guide/index.html">Writing documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/index.html">Development tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/testing-overview.html">Testing guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kernel-hacking/index.html">Hacking guide</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Tracing</a><ul class="current">
<li class="toctree-l2 current"><a class="reference internal" href="index.html#introduction-to-tracing">Introduction to Tracing</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="debugging.html">Using the tracer for debugging</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">Using the Linux Kernel Tracepoints</a></li>
<li class="toctree-l3"><a class="reference internal" href="tracepoint-analysis.html">Notes on Analysing Behaviour Using Events and Tracepoints</a></li>
<li class="toctree-l3"><a class="reference internal" href="ring-buffer-map.html">Tracefs ring-buffer memory mapping</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="index.html#core-tracing-frameworks">Core Tracing Frameworks</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#event-tracing-and-analysis">Event Tracing and Analysis</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#hardware-and-performance-tracing">Hardware and Performance Tracing</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#user-space-tracing">User-Space Tracing</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../fault-injection/index.html">Fault injection</a></li>
<li class="toctree-l1"><a class="reference internal" href="../livepatch/index.html">Livepatching</a></li>
<li class="toctree-l1"><a class="reference internal" href="../rust/index.html">Rust</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/index.html">Administration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kbuild/index.html">Build system</a></li>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/reporting-issues.html">Reporting issues</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Userspace tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../userspace-api/index.html">Userspace API</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../firmware-guide/index.html">Firmware</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devicetree/index.html">Firmware and Devicetree</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../arch/index.html">CPU architectures</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../staging/index.html">Unsorted documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../translations/index.html">Translations</a></li>
</ul>
</div>
<script type="text/javascript"> <!--
var sbar = document.getElementsByClassName("sphinxsidebar")[0];
let currents = document.getElementsByClassName("current")
if (currents.length) {
sbar.scrollTop = currents[currents.length - 1].offsetTop;
}
--> </script>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/trace/tracepoints.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
</div>
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="using-the-linux-kernel-tracepoints">
<h1>Using the Linux Kernel Tracepoints<a class="headerlink" href="#using-the-linux-kernel-tracepoints" title="Link to this heading">¶</a></h1>
<dl class="field-list simple">
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Mathieu Desnoyers</p>
</dd>
</dl>
<p>This document introduces Linux Kernel Tracepoints and their use. It
provides examples of how to insert tracepoints in the kernel and
connect probe functions to them and provides some examples of probe
functions.</p>
<section id="purpose-of-tracepoints">
<h2>Purpose of tracepoints<a class="headerlink" href="#purpose-of-tracepoints" title="Link to this heading">¶</a></h2>
<p>A tracepoint placed in code provides a hook to call a function (probe)
that you can provide at runtime. A tracepoint can be “on” (a probe is
connected to it) or “off” (no probe is attached). When a tracepoint is
“off” it has no effect, except for adding a tiny time penalty
(checking a condition for a branch) and space penalty (adding a few
bytes for the function call at the end of the instrumented function
and adds a data structure in a separate section). When a tracepoint
is “on”, the function you provide is called each time the tracepoint
is executed, in the execution context of the caller. When the function
provided ends its execution, it returns to the caller (continuing from
the tracepoint site).</p>
<p>You can put tracepoints at important locations in the code. They are
lightweight hooks that can pass an arbitrary number of parameters,
whose prototypes are described in a tracepoint declaration placed in a
header file.</p>
<p>They can be used for tracing and performance accounting.</p>
</section>
<section id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Link to this heading">¶</a></h2>
<p>Two elements are required for tracepoints :</p>
<ul class="simple">
<li><p>A tracepoint definition, placed in a header file.</p></li>
<li><p>The tracepoint statement, in C code.</p></li>
</ul>
<p>In order to use tracepoints, you should include linux/tracepoint.h.</p>
<p>In include/trace/events/subsys.h:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#undef TRACE_SYSTEM
#define TRACE_SYSTEM subsys
#if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_SUBSYS_H
#include <linux/tracepoint.h>
DECLARE_TRACE(subsys_eventname,
TP_PROTO(int firstarg, struct task_struct *p),
TP_ARGS(firstarg, p));
#endif /* _TRACE_SUBSYS_H */
/* This part must be outside protection */
#include <trace/define_trace.h>
</pre></div>
</div>
<p>In subsys/file.c (where the tracing statement must be added):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#include <trace/events/subsys.h>
#define CREATE_TRACE_POINTS
DEFINE_TRACE(subsys_eventname);
void somefct(void)
{
...
trace_subsys_eventname_tp(arg, task);
...
}
</pre></div>
</div>
<dl class="simple">
<dt>Where :</dt><dd><ul class="simple">
<li><p>subsys_eventname is an identifier unique to your event</p>
<ul>
<li><p>subsys is the name of your subsystem.</p></li>
<li><p>eventname is the name of the event to trace.</p></li>
</ul>
</li>
<li><p><cite>TP_PROTO(int firstarg, <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">task_struct</span></code> *p)</cite> is the prototype of the
function called by this tracepoint.</p></li>
<li><p><cite>TP_ARGS(firstarg, p)</cite> are the parameters names, same as found in the
prototype.</p></li>
<li><p>if you use the header in multiple source files, <cite>#define CREATE_TRACE_POINTS</cite>
should appear only in one source file.</p></li>
</ul>
</dd>
</dl>
<p>Connecting a function (probe) to a tracepoint is done by providing a
probe (function to call) for the specific tracepoint through
<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">register_trace_subsys_eventname()</span></code>. Removing a probe is done through
<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">unregister_trace_subsys_eventname()</span></code>; it will remove the probe.</p>
<p><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">tracepoint_synchronize_unregister()</span></code> must be called before the end of
the module exit function to make sure there is no caller left using
the probe. This, and the fact that preemption is disabled around the
probe call, make sure that probe removal and module unload are safe.</p>
<p>The tracepoint mechanism supports inserting multiple instances of the
same tracepoint, but a single definition must be made of a given
tracepoint name over all the kernel to make sure no type conflict will
occur. Name mangling of the tracepoints is done using the prototypes
to make sure typing is correct. Verification of probe type correctness
is done at the registration site by the compiler. Tracepoints can be
put in inline functions, inlined static functions, and unrolled loops
as well as regular functions.</p>
<p>The naming scheme “subsys_event” is suggested here as a convention
intended to limit collisions. Tracepoint names are global to the
kernel: they are considered as being the same whether they are in the
core kernel image or in modules.</p>
<p>If the tracepoint has to be used in kernel modules, an
<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">EXPORT_TRACEPOINT_SYMBOL_GPL()</span></code> or <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">EXPORT_TRACEPOINT_SYMBOL()</span></code> can be
used to export the defined tracepoints.</p>
<p>If you need to do a bit of work for a tracepoint parameter, and
that work is only used for the tracepoint, that work can be encapsulated
within an if statement with the following:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>if (trace_foo_bar_enabled()) {
int i;
int tot = 0;
for (i = 0; i < count; i++)
tot += calculate_nuggets();
trace_foo_bar_tp(tot);
}
</pre></div>
</div>
<p>All trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_tp()</span></code> calls have a matching trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_enabled()</span></code>
function defined that returns true if the tracepoint is enabled and
false otherwise. The trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_tp()</span></code> should always be within the
block of the if (trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_enabled()</span></code>) to prevent races between
the tracepoint being enabled and the check being seen.</p>
<p>The advantage of using the trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_enabled()</span></code> is that it uses
the static_key of the tracepoint to allow the if statement to be implemented
with jump labels and avoid conditional branches.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The convenience macro TRACE_EVENT provides an alternative way to
define tracepoints. Note, DECLARE_TRACE(foo) creates a function
“<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">trace_foo_tp()</span></code>” whereas TRACE_EVENT(foo) creates a function
“<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">trace_foo()</span></code>”, and also exposes the tracepoint as a trace event in
/sys/kernel/tracing/events directory. Check <a class="reference external" href="http://lwn.net/Articles/379903">http://lwn.net/Articles/379903</a>,
<a class="reference external" href="http://lwn.net/Articles/381064">http://lwn.net/Articles/381064</a> and <a class="reference external" href="http://lwn.net/Articles/383362">http://lwn.net/Articles/383362</a>
for a series of articles with more details.</p>
</div>
<p>If you require calling a tracepoint from a header file, it is not
recommended to call one directly or to use the trace_<tracepoint><code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">_enabled()</span></code>
function call, as tracepoints in header files can have side effects if a
header is included from a file that has CREATE_TRACE_POINTS set, as
well as the trace_<tracepoint>() is not that small of an inline
and can bloat the kernel if used by other inlined functions. Instead,
include tracepoint-defs.h and use <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">tracepoint_enabled()</span></code>.</p>
<p>In a C file:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>void do_trace_foo_bar_wrapper(args)
{
trace_foo_bar_tp(args); // for tracepoints created via DECLARE_TRACE
// or
trace_foo_bar(args); // for tracepoints created via TRACE_EVENT
}
</pre></div>
</div>
<p>In the header file:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>DECLARE_TRACEPOINT(foo_bar);
static inline void some_inline_function()
{
[..]
if (tracepoint_enabled(foo_bar))
do_trace_foo_bar_wrapper(args);
[..]
}
</pre></div>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©The kernel development community.
|
Powered by <a href="https://www.sphinx-doc.org/">Sphinx 8.1.3</a>
& <a href="https://alabaster.readthedocs.io">Alabaster 0.7.16</a>
|
<a href="../_sources/trace/tracepoints.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>