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