Tests: use jq for extracting info from ctf2 traces
[lttng-tools.git] / tests / regression / tools / metadata / utils.sh
index 99a6cc6d856caeb3af20e9188a0729717733aef6..d457946aec972bf66ca558bc654ade49527edb64 100644 (file)
@@ -4,7 +4,7 @@
 #
 # SPDX-License-Identifier: LGPL-2.1-only
 
-function get_env_value ()
+function get_env_value_ctf1 ()
 {
        local env_file=$1
        local key=$2
@@ -34,6 +34,76 @@ function get_env_value ()
        fi
 }
 
+# Expects a json representing the environment of a trace_class fragment.
+function get_env_value_ctf2 ()
+{
+       local env_file=$1
+       local key=$2
+       local result
+       local ret
+
+       if [[ "$key" == "vpid =" ]]; then
+               # The "vpid =" key value is only because the ctf1 extractor uses
+               # grep. jq use the complete key.
+               key="vpid"
+       fi
+
+
+       result=$(jq -r ".${key}" < "$env_file")
+       ret=$?
+       if [ $ret -eq 1 ]; then
+               echo "invalid_value_extraction"
+               return 1
+       else
+               echo "$result"
+               return 0
+       fi
+}
+
+function get_env_value ()
+{
+       if [ "$TRACE_FORMAT_OUTPUT" == "ctf1" ]; then
+               get_env_value_ctf1 "$@"
+       elif [ "$TRACE_FORMAT_OUTPUT" == "ctf2" ]; then
+               get_env_value_ctf2 "$@"
+       else
+               return 1
+       fi
+}
+
+function extract_env_to_file_ctf1 ()
+{
+       local source_dir=$1
+       local destination=$2
+       local env
+
+       env=$($BABELTRACE_BIN --output-format=ctf-metadata "${source_dir}")
+
+       # Extract "env" scope
+       awk '/env {/,/};/' <<< "$env" > "$destination"
+}
+
+function extract_env_to_file_ctf2 ()
+{
+       local source_file="$1/metadata"
+       local destination=$2
+       local query="select(.type == \"trace-class\").environment"
+       jq  "$query" "$source_file" > "$destination"
+}
+
+function extract_env_to_file ()
+{
+       if [ "$TRACE_FORMAT_OUTPUT" == "ctf1" ]; then
+               extract_env_to_file_ctf1 "$@"
+               return $?
+       elif [ "$TRACE_FORMAT_OUTPUT" == "ctf2" ]; then
+               extract_env_to_file_ctf2 "$@"
+               return $?
+       else
+               return 1
+       fi
+}
+
 function iso8601_to_lttng_dir_datetime ()
 {
        local result=$1
This page took 0.025372 seconds and 5 git commands to generate.