1#!/bin/bash
2# test perf probe of function from different CU
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7# skip if there's no gcc
8if ! [ -x "$(command -v gcc)" ]; then
9        echo "failed: no gcc compiler"
10        exit 2
11fi
12
13temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX)
14
15cleanup()
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
26trap_cleanup()
27{
28        cleanup
29        exit 1
30}
31
32trap trap_cleanup EXIT TERM INT
33
34cat > ${temp_dir}/testfile-foo.h << EOF
35struct t
36{
37  int *p;
38  int c;
39};
40
41extern int foo (int i, struct t *t);
42EOF
43
44cat > ${temp_dir}/testfile-foo.c << EOF
45#include "testfile-foo.h"
46
47int
48foo (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}
56EOF
57
58cat > ${temp_dir}/testfile-main.c << EOF
59#include "testfile-foo.h"
60
61static struct t g;
62
63int
64main (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}
74EOF
75
76gcc -g -Og -flto -c ${temp_dir}/testfile-foo.c -o ${temp_dir}/testfile-foo.o
77gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o
78gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o
79
80perf probe -x ${temp_dir}/testfile --funcs foo
81perf probe -x ${temp_dir}/testfile foo
82
83cleanup
84