Tests: use jq for extracting info from ctf2 traces
[lttng-tools.git] / tests / regression / tools / metadata / utils.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 #
5 # SPDX-License-Identifier: LGPL-2.1-only
6
7 function get_env_value_ctf1 ()
8 {
9 local env_file=$1
10 local key=$2
11 local result
12 local ret
13
14 result=$(grep "$key" < "$env_file")
15 ret=$?
16 if [ $ret -eq 1 ]; then
17 echo "invalid_value_extraction"
18 return 1
19 else
20 # Strip the key using bash substring removal.
21 # This remove all leading chars until the actual value.
22 result=${result#* = }
23
24 # Remove the trailing ';'
25 result=${result:0:-1}
26
27 # Remove enclosing '"' if present
28 if [ "${result:0:1}" == '"' ]; then
29 result=${result:1:-1}
30 fi
31
32 echo "$result"
33 return 0
34 fi
35 }
36
37 # Expects a json representing the environment of a trace_class fragment.
38 function get_env_value_ctf2 ()
39 {
40 local env_file=$1
41 local key=$2
42 local result
43 local ret
44
45 if [[ "$key" == "vpid =" ]]; then
46 # The "vpid =" key value is only because the ctf1 extractor uses
47 # grep. jq use the complete key.
48 key="vpid"
49 fi
50
51
52 result=$(jq -r ".${key}" < "$env_file")
53 ret=$?
54 if [ $ret -eq 1 ]; then
55 echo "invalid_value_extraction"
56 return 1
57 else
58 echo "$result"
59 return 0
60 fi
61 }
62
63 function get_env_value ()
64 {
65 if [ "$TRACE_FORMAT_OUTPUT" == "ctf1" ]; then
66 get_env_value_ctf1 "$@"
67 elif [ "$TRACE_FORMAT_OUTPUT" == "ctf2" ]; then
68 get_env_value_ctf2 "$@"
69 else
70 return 1
71 fi
72 }
73
74 function extract_env_to_file_ctf1 ()
75 {
76 local source_dir=$1
77 local destination=$2
78 local env
79
80 env=$($BABELTRACE_BIN --output-format=ctf-metadata "${source_dir}")
81
82 # Extract "env" scope
83 awk '/env {/,/};/' <<< "$env" > "$destination"
84 }
85
86 function extract_env_to_file_ctf2 ()
87 {
88 local source_file="$1/metadata"
89 local destination=$2
90 local query="select(.type == \"trace-class\").environment"
91 jq "$query" "$source_file" > "$destination"
92 }
93
94 function extract_env_to_file ()
95 {
96 if [ "$TRACE_FORMAT_OUTPUT" == "ctf1" ]; then
97 extract_env_to_file_ctf1 "$@"
98 return $?
99 elif [ "$TRACE_FORMAT_OUTPUT" == "ctf2" ]; then
100 extract_env_to_file_ctf2 "$@"
101 return $?
102 else
103 return 1
104 fi
105 }
106
107 function iso8601_to_lttng_dir_datetime ()
108 {
109 local result=$1
110
111 result=${result/T/-}
112
113 # Remove trailing timezone information including the '-'.
114 result=${result:0:-5}
115
116 echo "$result"
117 return 0
118 }
This page took 0.043228 seconds and 5 git commands to generate.