tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / tests / regression / kernel / test_callstack
1 #!/bin/bash
2 #
3 # Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
4 #
5 # SPDX-License-Identifier: GPL-2.0-only
6 #
7
8 TEST_DESC="Kernel tracer - Callstack context"
9
10 CURDIR=$(dirname "$0")/
11 TESTDIR=$CURDIR/../..
12 NUM_TESTS=11
13 TEST_APP_USERSPACE="$TESTDIR/utils/testapp/gen-syscall-events-callstack/gen-syscall-events-callstack"
14 TEST_APP_KERNELSPACE="$TESTDIR/utils/testapp/gen-syscall-events/gen-syscall-events"
15 PARSE_CALLSTACK="$TESTDIR/utils/parse-callstack.py"
16
17 SESSION_NAME="callstack"
18 CHANNEL_NAME="chan0"
19
20 source "$TESTDIR/utils/utils.sh"
21
22 function lttng_untrack_all()
23 {
24 lttng_untrack 0 "-s $SESSION_NAME --all --pid -k"
25 }
26
27 function lttng_track_pid()
28 {
29 local PID=$1
30 lttng_track 0 "-s $SESSION_NAME -k --pid=$PID"
31 }
32
33 function run_workload()
34 {
35 local TEST_APP=$1
36 local start_file_sync
37 start_file_sync=$(mktemp -u)
38
39 lttng_untrack_all
40
41 ./"$TEST_APP" "$start_file_sync" &
42 PID=$!
43 lttng_track_pid $PID
44
45 start_lttng_tracing_ok
46
47 # Create start file to launch the execution of the syscall call by the
48 # test app.
49 touch "$start_file_sync"
50
51 wait $PID
52
53 stop_lttng_tracing_ok
54
55 # Clean up the synchronization file.
56 rm -f "$start_file_sync"
57 }
58
59 function test_user_callstack()
60 {
61 TRACE_PATH=$(mktemp -d)
62 # This is the expected userspace callstack. (see gen-syscall-events-callstack.c)
63 USER_CS_EXPECTED="main fct_a fct_b fct_c my_gettid"
64 EVENT_NAME="gettid"
65
66 diag "Userspace callstack test"
67 create_lttng_session_ok $SESSION_NAME "$TRACE_PATH"
68 lttng_enable_kernel_channel_ok "$SESSION_NAME" "$CHANNEL_NAME"
69
70 lttng_enable_kernel_syscall_ok "$SESSION_NAME" "$EVENT_NAME" "$CHANNEL_NAME"
71 add_context_kernel_ok "$SESSION_NAME" "$CHANNEL_NAME" "callstack-user"
72
73 run_workload $TEST_APP_USERSPACE
74
75 destroy_lttng_session_ok "$SESSION_NAME"
76
77 "$BABELTRACE_BIN" "$TRACE_PATH" | grep $EVENT_NAME | ./"$PARSE_CALLSTACK" --user "$TEST_APP_USERSPACE" $USER_CS_EXPECTED
78 ok $? "Validate userspace callstack"
79
80 rm -rf "$TRACE_PATH"
81 }
82
83 function test_kernel_callstack()
84 {
85 TRACE_PATH=$(mktemp -d)
86 # Those are symbol expected to be present in the kernel callstack. This
87 # is not an exhaustive list since it's kernel dependent.
88
89 # FIXME: we used to test for the following symbols as well:
90 # save_stack_trace, lttng_callstack_get_size, but they were removed
91 # because:
92 # 1. kernel commit 77072f09 make it so that save_stack_trace is
93 # omitted from the callstack itself, and
94 #
95 # 2. the code (of this commit) can trigger Tail Call Optimization
96 # which mess up with the stacktrace by omiting the wrong address
97 # from the stacktrace.
98 # When this is fixed, we should add both save_stack_trace and
99 # lttng_callstack_get_size symbols back in the list of expected
100 # addresses.
101 KERNEL_CS_EXPECTED="lttng_event_reserve"
102 EVENT_NAME="read"
103
104 diag "Kernel callstack test"
105 create_lttng_session_ok $SESSION_NAME "$TRACE_PATH"
106 lttng_enable_kernel_channel_ok "$SESSION_NAME" "$CHANNEL_NAME"
107
108 lttng_enable_kernel_syscall_ok "$SESSION_NAME" "$EVENT_NAME" "$CHANNEL_NAME"
109 add_context_kernel_ok "$SESSION_NAME" "$CHANNEL_NAME" "callstack-kernel"
110
111 run_workload $TEST_APP_KERNELSPACE
112
113 destroy_lttng_session_ok "$SESSION_NAME"
114
115 "$BABELTRACE_BIN" "$TRACE_PATH" | grep $EVENT_NAME | ./"$PARSE_CALLSTACK" --kernel $KERNEL_CS_EXPECTED
116 ok $? "Validate kernel callstack"
117
118 rm -rf "$TRACE_PATH"
119 }
120
121 # Only run userspace callstack test on x86
122 uname -m | grep -E "x86" >/dev/null 2>&1
123 if test $? == 0; then
124 NUM_TESTS=$((NUM_TESTS+11))
125 RUN_USERSPACE_TEST=1
126 else
127 RUN_USERSPACE_TEST=0
128 fi
129
130 # MUST set TESTDIR before calling those functions
131 plan_tests $NUM_TESTS
132
133 print_test_banner "$TEST_DESC"
134
135 if [ "$(id -u)" == "0" ]; then
136 isroot=1
137 else
138 isroot=0
139 fi
140
141 skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" ||
142 {
143 which "$BABELTRACE_BIN" > /dev/null
144 test $? -ne 0
145 skip $? "Babeltrace binary not found. Skipping callstack tests" "$NUM_TESTS" ||
146 {
147 start_lttng_sessiond
148
149 if test $RUN_USERSPACE_TEST == 1; then
150 test_user_callstack
151 fi
152
153 test_kernel_callstack
154
155 stop_lttng_sessiond
156 }
157 }
This page took 0.034709 seconds and 6 git commands to generate.