1# MIPI System Software Trace (MIPI SyS-T) - Example Code #
2
3The **MIPI Alliance Specification for System Software-Trace (MIPI SyS-T℠**)
4defines a platform-independent, low bandwidth trace data protocol and software
5API for generating trace debug information from mobile or mobile influenced
6devices.
7
8This project provides an example implementation of a MIPI SyS-T
9instrumentation library as its main component. The library exposes the
10MIPI SyS-T API which generates the MIPI SyS-T data protocol.
11
12The project includes the following additional components to provide a
13working trace cross debug solution:
14
15  * An example implementation for a MIPI SyS-T data protocol printer for
16    converting the binary protocol into comma separated value (CSV)
17    text format.
18  * A PERL based collateral generator that automates the creation and
19    updating of MIPI SyS-T decode collateral in XML format. The generator
20    parses the catalog instrumentation calls inside the client source.
21    It can be embedded into the software build process to keep builds
22    and decode collateral updated at the same time.
23  * Doxygen based documentation for the instrumentation library.
24  * A unit test suite for the instrumentation library
25
26## SyS-T Trace Debug Environment ##
27SyS-T is intended for, but not limited to, cross-development environments
28where two machines, named target system (TS) and debug test system (DTS),
29exchange data over a transport layer. The TS uses the SyS-T library to generate
30a trace data protocol that is sent via a transport to the DTS. The tool
31stack on the DTS reads the data and decodes it into a format suitable for human
32or automated analysis. The decode step utilizes collateral that
33contains static trace data information like string to numeric key mappings. The
34collateral is used to reduce both trace transport bandwidth and compiled
35software space needs.
36
37![MIPI SyS-T Overview](library/doxygen/mipi_sys_t_overview.png)
38
39The  MIPI specification for SyS-T is available from the
40[MIPI Alliance Website](https://mipi.org/specifications/sys-t).
41
42
43## Supported Platforms ##
44
45   * Windows
46   * Mac OS X
47   * Linux
48
49## Requirements ##
50
51The instrumentation library is designed to have minimal dependencies for
52building. The library core is written using self-contained C-language code.
53Only the platform dependent adaptation software layer relies on C-runtime
54headers and libraries.
55
56The protocol printer is written in C++11 and requires a C++11 compliant
57compiler and runtime environment.
58
59The following third party components are used for building the project:
60
61  * [CMake](https://cmake.org/) for generating the platform dependent build system (required).
62  * [Doxygen](http://www.stack.nl/~dimitri/doxygen/) for building the library documentation (optional).
63  * [Google Test](https://github.com/google/googletest) for building the library unit tests (optional).
64  * [PugiXML](https://github.com/zeux/pugixml) for building the printer project (required).
65
66The Google Test and PugiXML components are configured as git modules
67in the external folder of this project. If the folders are not populated,
68run the following command to pull the required components from GitHub.
69```
70git submodule update --init --recursive
71```
72## Building ##
73The project is split into the following sub projects.
74
75| Project                          | Location    |
76| ---------------------------------|-------------|
77| SyS-T instrumentation library    | library     |
78| Instrumented code examples       | examples    |
79| SyS-T protocol printer tool      | printer     |
80| Collateral generator tool        | collateral  |
81
82The following chapters describe how to build the individual projects.
83
84### Building the SyS-T Instrumentation Library ###
85The instrumentation library uses the CMake build system. Building
86the project follows the normal CMake flow. It requires an initial CMake run
87to generate the platform dependent build system, for example Makefiles on
88Linux or Visual Studio Projects on Windows. The native build tools are then
89used to do the actual build. Note that CMake follows the "out of source"
90build concept. That means that you create a build folder for CMake
91projects outside of the source sandbox. All build artifacts are
92created inside this build folder.
93
94The CMake script for the instrumentation library uses the following
95configuration switches:
96
97| Option                           | Description |   Default Value |
98| ---------------------------------|-------------|-----------------|
99| CMAKE_INSTALL_PREFIX| Path prefix for the compiled library binaries and include files install location| (defined by CMake) |
100| SYST_BUILD_PLATFORM_NAME|Name of the platform adaption code folder| example |
101| SYST_BUILD_DOC|Enable build of Doxygen documentation| True if Doxygen installation was found, False otherwise |
102| SYST_BUILD_GTEST_DIR|Installation location of Google Test sources. Set this for building the unit tests| (unset) |
103
104The following transcript shows an example for configuring and building the
105library on a Linux console. Replace "../sys-t/library" with the location of
106your SyS-T project sandbox.
107
108```
109$ mkdir build
110$ cd build
111$ cmake ../sys-t/library -DCMAKE_INSTALL_PREFIX=../deploy -DSYST_BUILD_PLATFORM_NAME=example -DCMAKE_BUILD_TYPE=Release
112(...)
113-- Configuring done
114-- Generating done
115-- Build files have been written to: /users/mipi/prj/build
116
117 $ make install
118 (...)
119 -- Installing: /users/mipi/prj/deploy/lib/libmipi_syst.so
120 -- Installing: /users/mipi/prj/deploy/lib/libmipi_syst_static.a
121 -- Installing: /users/mipi/prj/deploy/include
122 $
123```
124The install target builds the projects and copies the libraries
125and SyS-T header files to the deploy location. This location was defined
126by the CMake variable ``CMAKE_INSTALL_PREFIX``. It forms a SyS-T SDK
127that applications compile and link against.
128
129If the unit tests are built as well, they can be run using one of the
130following commands:
131
132```bash
133$ ctest -verbose
134$ make RUN_TEST_VERBOSE
135$ ./test/unit/syst_unittest
136```
137
138### Building the Example Applications ###
139The project ships with example applications that show how to instrument
140source code with the SyS-T API. The examples use the CMake build
141system and require that the instrumentation library project was built and
142installed first. The building depends on the SyS-T libraries and header
143files in the install location. The location of the install folder needs
144to be specified by setting the CMake variable ``SYST_SDK`` when configuring
145the example project.
146
147The following transcript shows the configuring and building of the examples
148in a Linux console. Replace "../sys-t/examples" with the location of
149your SyS-T sandbox examples folder and replace the value for SYST_SDK
150with the location of the install location from a previously built
151instrumentation library.
152
153
154```
155$ mkdir build_examples
156$ cd build_examples
157$ cmake ../sys-t/examples -DSYST_SDK=../deploy -DCMAKE_BUILD_TYPE=Release
158
159-- Found SYST: /users/mipi/prj/deploy/include
160-- Configuring done
161-- Generating done
162-- Build files have been written to: /users/mipi/prj/build_examples
163
164$ make
165Scanning dependencies of target hello
166[ 20%] Building C object hello/CMakeFiles/hello.dir/hello.c.o
167[ 40%] Linking C executable hello
168[ 40%] Built target hello
169Scanning dependencies of target systclient
170[ 60%] Building C object client/CMakeFiles/systclient.dir/systclient.c.o
171[ 80%] Building C object client/CMakeFiles/systclient.dir/othersource.c.o
172[100%] Linking C executable systclient
173[100%] Built target systclient
174```
175
176The examples code builds into standalone applications that can be run
177directly from the console. To run the minimal hello example, enter the
178following command:
179
180```
181$ hello/hello
182  in SyS-T platform init hook: "mipi_syst_platform_state_init()"
183                              systh = 0x6250c0, platform_data = (nil)
184  in SyS-T platform handle init hook: systh = 0x1edd420
185
186STP Protocol Output:
187     0 <D32TS>  01801042
188     1 <D64>    704caea243544e49
189     2 <D64>    35ea9c9ea7d1b5ab
190     3 <D64>    7953206f6c6c6548
191     4 <D32>    21542d53
192     5 <D8>     00
193     6 <FLAG>
194SYS-T RAW DATA: 42108001494E5443A2AE4C70ABB5D1A79E9CEA3548656C6C6F205379532D542100
195
196  in SyS-T platform handle release hook:systh = 0x1edd420
197$
198```
199
200The transcript shows the output from the example platform code in
201the instrumentation library. The example platform code only prints
202output actions to the console. It does not interface to any real
203trace transport. The output shows the raw messages as hex dumps.
204This output can be fed into the protocol printer tool to convert
205it into human readable CSV textual data.
206
207### Building the Data Protocol Printer ###
208The project includes a SyS-T data protocol pretty printer tool
209in the printer subdirectory. The printer is a standalone application
210written in C++11. It supports reading the output from instrumented
211applications using the example platform from the SyS-T
212instrumentation library.
213It scans the output for lines starting with ``SYS-T RAW DATA:`` and
214converts the hex dumps into binary data for decoding. The printer can
215be easily adapted to real trace data transports by replacing the code
216in ``printer/src/mipi_syst_main.cpp`` with an appropriate data reader.
217
218The following transcript shows how to build the printer on a Linux console.
219The printer is a standalone application and independent from the
220instrumentation or example projects.
221
222```
223$ cmake ../../sys-t/printer
224-- The C compiler identification is GNU 5.4.0
225-- The CXX compiler identification is GNU 5.4.0
226-- Check for working C compiler: /usr/bin/cc
227-- Check for working C compiler: /usr/bin/cc -- works
228-- Detecting C compiler ABI info
229-- Detecting C compiler ABI info - done
230-- Detecting C compile features
231-- Detecting C compile features - done
232-- Check for working CXX compiler: /usr/bin/c++
233-- Check for working CXX compiler: /usr/bin/c++ -- works
234-- Detecting CXX compiler ABI info
235-- Detecting CXX compiler ABI info - done
236-- Detecting CXX compile features
237-- Detecting CXX compile features - done
238-- Configuring done
239-- Generating done
240-- Build files have been written to: /users/mipi/prj/syst_build/printer
241
242$ make
243Scanning dependencies of target systprint
244[ 14%] Building CXX object CMakeFiles/systprint.dir/src/mipi_syst_main.cpp.o
245[ 28%] Building CXX object CMakeFiles/systprint.dir/src/mipi_syst_collateral.cpp.o
246[ 42%] Building CXX object CMakeFiles/systprint.dir/src/mipi_syst_printf.cpp.o
247[ 57%] Building CXX object CMakeFiles/systprint.dir/src/mipi_syst_decode.cpp.o
248[ 71%] Building CXX object CMakeFiles/systprint.dir/src/mipi_syst_message.cpp.o
249[100%] Linking CXX executable systprint
250[100%] Built target systprint
251```
252
253The printer project comes with a self test feature. The ``printer/test``
254folder contains reference input and output files collected using the
255``example/client`` example application. To run the printer test use the
256following command (or the cmake test driver command ``ctest``) in the
257printer build folder:
258
259```
260$ make test
261Running tests...
262Test project /users/mipi/prj/syst_build/printer
263    Start 1: print_client_example
2641/3 Test #1: print_client_example ...............   Passed    0.01 sec
265    Start 2: diff_output_with_32bit_reference
2662/3 Test #2: diff_output_with_32bit_reference ...   Passed    0.04 sec
267    Start 3: diff_output_with_64bit_reference
2683/3 Test #3: diff_output_with_64bit_reference ...   Passed    0.03 sec
269
270100% tests passed, 0 tests failed out of 3
271
272Total Test time (real) =   0.11 sec
273```
274
275To actually see the printer output, run the printer directly using command
276line arguments, or indirectly through the test driver in verbose mode
277(```ctest --verbose```). The following transcript shows how to call the
278printer directly:
279
280```
281$systprint --short_guid {494E5443-8A9C-4014-A65A-2F36A36D96E4} --collateral ../../sys-t/printer/test/collateral.xml ../../sys-t/printer/test/input_client64.txt
282
283Decode Status,Payload,Type,Severity,Origin,Unit,Message TimeStamp,Context TimeStamp,Location,Raw Length,Checksum,Collateral
284OK,"0x0000000000010000 version banner string",BUILD:LONG,MAX,example,1,0x00054A4B376A70E9,0x0000000000000000,,62,0x4DDEF5B9,../../sys-t/printer/test/collateral.xml
285OK,"SyS-T Library version 1.0.0",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000001,./systclient.c:64,48,0x7A34B527,../../sys-t/printer/test/collateral.xml
286OK,"+-------------------------------------------------------+",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000002,./othersource.c:40,36,0x7CBB44B6,../../sys-t/printer/test/collateral.xml
287OK,"|               ____         _____   _______            |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000003,./othersource.c:41,36,0x2761EBF4,../../sys-t/printer/test/collateral.xml
288OK,"|              / ___|       / ____| |__   __|           |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000004,./othersource.c:42,36,0x55C63EAB,../../sys-t/printer/test/collateral.xml
289OK,"|             | |___  __  _| |___ _____| |              |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000005,./othersource.c:43,36,0xE3885FB4,../../sys-t/printer/test/collateral.xml
290OK,"|              \___ \| | | |\___ \_____| |              |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000006,./othersource.c:44,36,0x4C13A7F5,../../sys-t/printer/test/collateral.xml
291OK,"|              ____| | |_| |____| |    | |              |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000007,./othersource.c:45,36,0xE2C8BDC2,../../sys-t/printer/test/collateral.xml
292OK,"|             |_____/ \__| |_____/     |_|              |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000008,./othersource.c:46,36,0xD0734297,../../sys-t/printer/test/collateral.xml
293OK,"|                      _/ /                             |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x0000000000000009,./othersource.c:47,36,0x6D704426,../../sys-t/printer/test/collateral.xml
294OK,"|                     |__/                              |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x000000000000000A,./othersource.c:48,36,0x0A8FD609,../../sys-t/printer/test/collateral.xml
295OK,"+-------------------------------------------------------+",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x000000000000000B,./othersource.c:49,36,0x1E99CD8F,../../sys-t/printer/test/collateral.xml
296OK,"|    catalog Format  |         Printed Result           |",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A70E9,0x000000000000000C,./othersource.c:231,36,0xA17B5C1C,../../sys-t/printer/test/collateral.xml
297OK,"|---------------------------------strings---------------|",CATALOG:ID32P64,INFO,example,1,0x00054A4B376A74D1,0x000000000000000D,./othersource.c:232,36,0x11A215E6,../../sys-t/printer/test
298(...)
299$
300```
301## Decode Collateral Creation Tool ##
302The project includes a PERL based collateral generator that automates the
303creation and updating of SyS-T decode collateral in XML format. This
304generator parses the catalog instrumentation points by scanning the
305client source code. The tool can be embedded into a software build process to
306keep software builds and decode collateral updated at the same time. The tool
307is stored in the ```collateral\generator``` folder of the project with the
308name ```syst_cgen.pl```.
309
310### Dependencies ###
311The generator is written in PERL and requires a PERL installation with the
312following optional modules installed.
313 * String::Escape
314 * Xml::Simple
315
316Refer to the documentation for your PERL installation on module installation.
317On Linux, the following commands can be used:
318
319```
320$ perl -MCPAN -e 'install XML::Simple'
321$ perl -MCPAN -e 'install String::Escape'
322```
323
324### Collateral Generation Process ###
325The collateral generator takes a SyS-T collateral template and
326a configuration file as input. The configuration file defines the locations
327and file extensions of the source files to be scanned and how the catalog
328message calls
329inside the source code are named. The tool can then detect the catalog
330calls, and extracts the format strings, source locations, and
331catalog IDs to update the collateral template file. The result is a
332new collateral file that matches the actual state of the source code.
333
334### Catalog Generation Example ###
335The client application in ```example/client``` uses various catalog calls.
336It therefore provides a configuration file for```syst_cgen.pl``` to detect
337the SyS-T catalog message calls, and a collateral template file that is
338updated by the generator. It is executed in the following way:
339
340```
341$ perl ../../collateral/generator/syst_cgen.pl -config collateral_config.xml
342syst_catgen.pl: Parsing: ./othersource.c
343syst_catgen.pl: Add ./othersource.c with file id 1 to file catalog
344syst_catgen.pl: Parsing finished: ./othersource.c, found 127 call(s)
345syst_catgen.pl: Parsing: ./systclient.c
346syst_catgen.pl: Add ./systclient.c with file id 2 to file catalog
347syst_catgen.pl: Parsing finished: ./systclient.c, found 4 call(s)
348syst_catgen.pl: Generating XML structure
349syst_catgen.pl: Loaded template collateral file template.xml
350syst_catgen.pl: Generating XML structure finished
351syst_catgen.pl: Writing XML file: generated_catalog.xml
352syst_catgen.pl: Writing XML file finished
353```
354
355This call creates the file ``generated_catalog.xml``. It is used by
356SyS-T data protocol processing tools to decode the catalog messages
357from this application. For an example of such an application, see the
358earlier section about the protocol printer. The printer tool uses the
359``--collateral <file>`` argument to load collateral files.
360
361## Integration Build Test ##
362The bash script in ``examples/scripts/bldall.sh`` can be used to run an
363integration test for the different projects. The script builds all projects
364sequentially using the example library platform. It then runs components
365tests and finally calls the printer tool to format the output of
366the ``hello`` example application. The following transcript shows how to
367run execute the script. The BLD_ROOT variable sets the location of the build
368folder. If unset, the script creates a local build folder in the scripts
369folder.
370
371```
372$ cd sys-t/examples/scripts
373$ BLD_ROOT=/tmp/sys_t_test_bld ./bldall.sh
374```
375
376## License
377
378See [LICENSE](LICENSE)
379