xref: /aosp_15_r20/external/pigweed/pw_tokenizer/get_started.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_tokenizer-get-started:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=============================
4*61c4878aSAndroid Build Coastguard WorkerGet started with pw_tokenizer
5*61c4878aSAndroid Build Coastguard Worker=============================
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_tokenizer
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. _module-pw_tokenizer-get-started-overview:
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker--------
12*61c4878aSAndroid Build Coastguard WorkerOverview
13*61c4878aSAndroid Build Coastguard Worker--------
14*61c4878aSAndroid Build Coastguard WorkerThere are two sides to ``pw_tokenizer``, which we call tokenization and
15*61c4878aSAndroid Build Coastguard Workerdetokenization.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker* **Tokenization** converts string literals in the source code to binary tokens
18*61c4878aSAndroid Build Coastguard Worker  at compile time. If the string has printf-style arguments, these are encoded
19*61c4878aSAndroid Build Coastguard Worker  to compact binary form at runtime.
20*61c4878aSAndroid Build Coastguard Worker* **Detokenization** converts tokenized strings back to the original
21*61c4878aSAndroid Build Coastguard Worker  human-readable strings.
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard WorkerHere's an overview of what happens when ``pw_tokenizer`` is used:
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker1. During compilation, the ``pw_tokenizer`` module hashes string literals to
26*61c4878aSAndroid Build Coastguard Worker   generate stable 32-bit tokens.
27*61c4878aSAndroid Build Coastguard Worker2. The tokenization macro removes these strings by declaring them in an ELF
28*61c4878aSAndroid Build Coastguard Worker   section that is excluded from the final binary.
29*61c4878aSAndroid Build Coastguard Worker3. After compilation, strings are extracted from the ELF to build a database of
30*61c4878aSAndroid Build Coastguard Worker   tokenized strings for use by the detokenizer. The ELF file may also be used
31*61c4878aSAndroid Build Coastguard Worker   directly.
32*61c4878aSAndroid Build Coastguard Worker4. During operation, the device encodes the string token and its arguments, if
33*61c4878aSAndroid Build Coastguard Worker   any.
34*61c4878aSAndroid Build Coastguard Worker5. The encoded tokenized strings are sent off-device or stored.
35*61c4878aSAndroid Build Coastguard Worker6. Off-device, the detokenizer tools use the token database to decode the
36*61c4878aSAndroid Build Coastguard Worker   strings to human-readable form.
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker.. _module-pw_tokenizer-get-started-integration:
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard WorkerIntegrating with Bazel / GN / CMake projects
41*61c4878aSAndroid Build Coastguard Worker============================================
42*61c4878aSAndroid Build Coastguard WorkerIntegrating ``pw_tokenizer`` requires a few steps beyond building the code. This
43*61c4878aSAndroid Build Coastguard Workersection describes one way ``pw_tokenizer`` might be integrated with a project.
44*61c4878aSAndroid Build Coastguard WorkerThese steps can be adapted as needed.
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker#. Add ``pw_tokenizer`` to your build. Build files for GN, CMake, and Bazel are
47*61c4878aSAndroid Build Coastguard Worker   provided. For Make or other build systems, add the files specified in the
48*61c4878aSAndroid Build Coastguard Worker   BUILD.gn's ``pw_tokenizer`` target to the build.
49*61c4878aSAndroid Build Coastguard Worker#. Use the tokenization macros in your code. See
50*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_tokenizer-tokenization`.
51*61c4878aSAndroid Build Coastguard Worker#. Ensure the ``.pw_tokenizer.*`` sections are included in your output ELF file:
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker   * In GN and CMake, this is done automatically.
54*61c4878aSAndroid Build Coastguard Worker   * In Bazel, include ``"@pigweed//pw_tokenizer:linker_script"`` in the
55*61c4878aSAndroid Build Coastguard Worker     ``deps`` of your main binary rule (assuming you're already overriding the
56*61c4878aSAndroid Build Coastguard Worker     default linker script).
57*61c4878aSAndroid Build Coastguard Worker   * If your binary does not use a custom linker script, you can pass
58*61c4878aSAndroid Build Coastguard Worker     ``add_tokenizer_sections_to_default_script.ld`` to the linker which will
59*61c4878aSAndroid Build Coastguard Worker     augment the default linker script (rather than override it).
60*61c4878aSAndroid Build Coastguard Worker   * Alternatively, add the contents of ``pw_tokenizer_linker_sections.ld`` to
61*61c4878aSAndroid Build Coastguard Worker     your project's linker script.
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker#. Compile your code to produce an ELF file.
64*61c4878aSAndroid Build Coastguard Worker#. Run ``database.py create`` on the ELF file to generate a CSV token
65*61c4878aSAndroid Build Coastguard Worker   database. See :ref:`module-pw_tokenizer-managing-token-databases`.
66*61c4878aSAndroid Build Coastguard Worker#. Commit the token database to your repository. See notes in
67*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_tokenizer-database-management`.
68*61c4878aSAndroid Build Coastguard Worker#. Integrate a ``database.py add`` command to your build to automatically update
69*61c4878aSAndroid Build Coastguard Worker   the committed token database. In GN, use the ``pw_tokenizer_database``
70*61c4878aSAndroid Build Coastguard Worker   template to do this. See :ref:`module-pw_tokenizer-update-token-database`.
71*61c4878aSAndroid Build Coastguard Worker#. Integrate ``detokenize.py`` or the C++ detokenization library with your tools
72*61c4878aSAndroid Build Coastguard Worker   to decode tokenized logs. See :ref:`module-pw_tokenizer-detokenization`.
73*61c4878aSAndroid Build Coastguard Worker
74*61c4878aSAndroid Build Coastguard WorkerUsing with Zephyr
75*61c4878aSAndroid Build Coastguard Worker=================
76*61c4878aSAndroid Build Coastguard WorkerWhen building ``pw_tokenizer`` with Zephyr, 3 Kconfigs can be used currently:
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker* ``CONFIG_PIGWEED_TOKENIZER`` will automatically link ``pw_tokenizer`` as well
79*61c4878aSAndroid Build Coastguard Worker  as any dependencies.
80*61c4878aSAndroid Build Coastguard Worker* ``CONFIG_PIGWEED_TOKENIZER_BASE64`` will automatically link
81*61c4878aSAndroid Build Coastguard Worker  ``pw_tokenizer.base64`` as well as any dependencies.
82*61c4878aSAndroid Build Coastguard Worker* ``CONFIG_PIGWEED_DETOKENIZER`` will automatically link
83*61c4878aSAndroid Build Coastguard Worker  ``pw_tokenizer.decoder`` as well as any dependencies.
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard WorkerOnce enabled, the tokenizer headers can be included like any Zephyr headers:
86*61c4878aSAndroid Build Coastguard Worker
87*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker   #include <pw_tokenizer/tokenize.h>
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker.. note::
92*61c4878aSAndroid Build Coastguard Worker  Zephyr handles the additional linker sections via
93*61c4878aSAndroid Build Coastguard Worker  ``pw_tokenizer_zephyr.ld`` which is added to the end of the linker file
94*61c4878aSAndroid Build Coastguard Worker  via a call to ``zephyr_linker_sources(SECTIONS ...)``.
95