tests/.../data_from_mp.py: add CTF 2 support
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 12 May 2024 11:31:45 +0000 (07:31 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
This patch changes `tests/data/plugins/src.ctf.fs/field/data_from_mp.py`
to add CTF 2 support.

The script generates a CTF 2 trace when the stripped field class text
starts with `{` (full field class) or `"` (field class alias), which are
exclusive to the CTF 2 metadata syntax (any CTF 1.8 field class starts
with some keyword).

The same automatic field class aliases as the CTF 1.8 case
are available.

_make_ctf_2_metadata() doesn't need the `@` replacement trick of
_make_ctf_1_metadata() because CTF 2 array field classes aren't
different from any CTF 2 field class, for example:

    {
      "type": "static-length-array",
      "length": 4,
      "element-field-class": "u8"
    }

vs.

    u8 @[4]

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I6abe0776c64fde62633cfc7a89094c0aedf6b9be
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12770

tests/data/plugins/src.ctf.fs/field/data_from_mp.py

index 2a1a170a79f9b91aeaf45219848568588821a03e..b8e651cb9b534ff2a2ccfd38d36db335f9662345 100644 (file)
@@ -13,8 +13,6 @@ import moultipart
 
 
 def _make_ctf_1_metadata(payload_fc: str):
-    payload_fc = payload_fc.strip()
-
     if "@" in payload_fc:
         payload_fc = payload_fc.replace("@", "root")
     else:
@@ -72,9 +70,281 @@ event {
     ).substitute(payload_fc=payload_fc)
 
 
+def _make_ctf_2_metadata(payload_fc: str):
+    return string.Template(
+        """\
+\x1e{
+  "type": "preamble",
+  "version": "2"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u8le",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 8,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u16le",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 16,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u32le",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 32,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u64le",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 64,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u8",
+  "field-class": "u8le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u16",
+  "field-class": "u16le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u32",
+  "field-class": "u32le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u64",
+  "field-class": "u64le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u8be",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 8,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u16be",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 16,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u32be",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 32,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u64be",
+  "field-class": {
+    "type": "fixed-length-unsigned-integer",
+    "length": 64,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i8le",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 8,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i16le",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 16,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i32le",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 32,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i64le",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 64,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i8be",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 8,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i16be",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 16,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "i32be",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 32,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "u64be",
+  "field-class": {
+    "type": "fixed-length-signed-integer",
+    "length": 64,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt32le",
+  "field-class": {
+    "type": "fixed-length-floating-point-number",
+    "length": 32,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt64le",
+  "field-class": {
+    "type": "fixed-length-floating-point-number",
+    "length": 64,
+    "byte-order": "little-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt32",
+  "field-class": "flt32le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt64",
+  "field-class": "flt64le"
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt32be",
+  "field-class": {
+    "type": "fixed-length-floating-point-number",
+    "length": 32,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "flt64be",
+  "field-class": {
+    "type": "fixed-length-floating-point-number",
+    "length": 64,
+    "byte-order": "big-endian",
+    "alignment": 8
+  }
+}
+\x1e{
+  "type": "field-class-alias",
+  "name": "nt-str",
+  "field-class": {
+    "type": "null-terminated-string"
+  }
+}
+\x1e{
+  "type": "data-stream-class"
+}
+\x1e{
+  "type": "event-record-class",
+  "payload-field-class": {
+    "type": "structure",
+    "member-field-classes": [
+      {
+        "name": "root",
+        "field-class": ${payload_fc}
+      }
+    ]
+  }
+}
+"""
+    ).substitute(payload_fc=payload_fc)
+
+
+def _make_ctf_metadata(payload_fc: str):
+    if payload_fc.startswith("{") or payload_fc.startswith('"'):
+        # CTF 2
+        return _make_ctf_2_metadata(payload_fc)
+    else:
+        # Assume CTF 1.8
+        return _make_ctf_1_metadata(payload_fc)
+
+
 def _make_ctf_1_data(normand_text: str):
-    # Default to little-endian because that's also the TSDL default in
-    # _make_ctf_1_metadata() above.
+    # Default to little-endian because that's also the default in
+    # _make_ctf_1_metadata() and _make_ctf_2_metadata() above.
     return normand.parse("!le\n" + normand_text).data
 
 
@@ -89,7 +359,7 @@ def _create_files_from_mp(mp_path: str, output_dir: str):
         parts = moultipart.parse(f)
 
     with open(metadata_path, "w") as f:
-        f.write(_make_ctf_1_metadata(parts[0].content))
+        f.write(_make_ctf_metadata(parts[0].content.strip()))
 
     with open(data_path, "wb") as f:
         f.write(_make_ctf_1_data(parts[1].content))
This page took 0.030467 seconds and 4 git commands to generate.