GNU Linux-libre 4.19.304-gnu1
[releases.git] / tools / perf / tests / shell / test_uprobe_from_different_cu.sh
1 #!/bin/bash
2 # test perf probe of function from different CU
3 # SPDX-License-Identifier: GPL-2.0
4
5 set -e
6
7 # skip if there's no gcc
8 if ! [ -x "$(command -v gcc)" ]; then
9         echo "failed: no gcc compiler"
10         exit 2
11 fi
12
13 temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX)
14
15 cleanup()
16 {
17         trap - EXIT TERM INT
18         if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then
19                 echo "--- Cleaning up ---"
20                 perf probe -x ${temp_dir}/testfile -d foo || true
21                 rm -f "${temp_dir}/"*
22                 rmdir "${temp_dir}"
23         fi
24 }
25
26 trap_cleanup()
27 {
28         cleanup
29         exit 1
30 }
31
32 trap trap_cleanup EXIT TERM INT
33
34 cat > ${temp_dir}/testfile-foo.h << EOF
35 struct t
36 {
37   int *p;
38   int c;
39 };
40
41 extern int foo (int i, struct t *t);
42 EOF
43
44 cat > ${temp_dir}/testfile-foo.c << EOF
45 #include "testfile-foo.h"
46
47 int
48 foo (int i, struct t *t)
49 {
50   int j, res = 0;
51   for (j = 0; j < i && j < t->c; j++)
52     res += t->p[j];
53
54   return res;
55 }
56 EOF
57
58 cat > ${temp_dir}/testfile-main.c << EOF
59 #include "testfile-foo.h"
60
61 static struct t g;
62
63 int
64 main (int argc, char **argv)
65 {
66   int i;
67   int j[argc];
68   g.c = argc;
69   g.p = j;
70   for (i = 0; i < argc; i++)
71     j[i] = (int) argv[i][0];
72   return foo (3, &g);
73 }
74 EOF
75
76 gcc -g -Og -flto -c ${temp_dir}/testfile-foo.c -o ${temp_dir}/testfile-foo.o
77 gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o
78 gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o
79
80 perf probe -x ${temp_dir}/testfile --funcs foo
81 perf probe -x ${temp_dir}/testfile foo
82
83 cleanup